1 /* $Id: sr.c,v 1.11 1994/06/24 10:31:56 ceriel Exp $ */
3 * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
4 * See the copyright notice in the ACK home directory, in the file "Copyright".
6 /* S T R E N G T H R E D U C T I O N */
10 #include "../share/types.h"
12 #include "../share/debug.h"
13 #include "../share/global.h"
14 #include "../share/files.h"
15 #include "../share/get.h"
16 #include "../share/put.h"
17 #include "../share/lset.h"
18 #include "../share/map.h"
19 #include "../share/alloc.h"
20 #include "../share/go.h"
21 #include "../share/aux.h"
25 /* Strength reduction tries to change expensive operators occurring
26 * in a loop into cheaper operators. The expensive operators considered
27 * are multiplication, left-shift and array referencing.
28 * The transformations can be expressed in C as:
30 * [1]: for (i = e1; i <= e2; i++)
33 * for (i = e1, t = 118*e1; i <= e2; i++, t += 118)
36 * [2]: for (i = e1; i <= e2; i++)
39 * for (i = e1, p = &a[i]; i <= e2; i++, p++)
41 * The latter optimization is suppressed if array bound checking
45 /* Machine and/or language dependent parameters: */
51 int Ssr; /* #optimizations found */
56 /* Read target machine dependent information */
61 while(getc(f) != '\n');
63 if (strcmp(s,"%%SR") == 0)break;
65 fscanf(f,"%d",&ovfl_harmful);
66 fscanf(f,"%d",&arrbound_harmful);
67 fscanf(f,"%d",&sli_threshold);
73 /* Delete the set of iv structs */
77 for (i = Lfirst(ivs); i != (Lindex) 0; i = Lnext(i,ivs)) {
89 OUTTRACE("going to process loop %d",loop->lp_id);
90 induc_vars(loop,&ivs, &vars);
91 /* Build a set of iv_structs, one for every induction
92 * variable of the loop, i.e. a variable i that
93 * is changed only by i := i + c, where c is a loop constant.
94 * Also detects variables that are changed (including induction
97 OUTTRACE("loop has %d induction variables",Lnrelems(ivs));
98 if (Lnrelems(ivs) > 0) {
99 strength_reduction(loop,ivs,vars);
100 /* Perform strength reduction. Reduce:
102 * a[iv] to indirection (*p)
103 * (unless array bound checking is required)
115 /* Compute the LP_BLOCKS sets for all loops of p */
120 for (b = p->p_start; b != (bblock_p) 0; b = b->b_next) {
121 for (i = Lfirst(b->b_loops); i != (Lindex) 0;
122 i = Lnext(i,b->b_loops)) {
123 Ladd(b,&(((loop_p) Lelem(i))->LP_BLOCKS));
133 /* Optimize all loops of one procedure. We first do all
134 * outer loops at the lowest nesting level and proceed
135 * in the inwards direction.
145 for (i = Lfirst(p->p_loops); i != (Lindex) 0;
146 i = Lnext(i,p->p_loops)) {
147 lp = (loop_p) Lelem(i);
148 if (!lp->LP_DONE && lp->lp_level < min_level) {
149 min_level = lp->lp_level;
153 if (! outermost) break;
155 outermost->LP_DONE = TRUE;
156 OUTTRACE("loop %d processed",outermost->lp_id);
162 STATIC bblock_p header(lp)
165 /* Try to determine the 'header' block of loop lp.
166 * If 'e' is the entry block of loop L, then block 'b' is
167 * called the header block of L, iff:
168 * SUCC(b) = {e} & b dominates e.
169 * If lp has no header block, 0 is returned.
172 bblock_p x = lp->lp_entry->b_idom;
174 if (x != (bblock_p) 0 && Lnrelems(x->b_succ) == 1 &&
175 (bblock_p) Lelem(Lfirst(x->b_succ)) == lp->lp_entry) {
186 /* Allocate the extended data structures for procedure p */
191 for (pi = Lfirst(p->p_loops); pi != (Lindex) 0;
192 pi = Lnext(pi,p->p_loops)) {
193 lp = (loop_p) Lelem(pi);
194 lp->lp_extend = newsrlpx();
195 lp->LP_HEADER = header(lp);
197 lp->LP_INSTR = last_instr(lp->LP_HEADER);
203 STATIC sr_cleanproc(p)
206 /* Remove the extended data structures for procedure p */
212 for (pi = Lfirst(p->p_loops); pi != (Lindex) 0;
213 pi = Lnext(pi,p->p_loops)) {
214 lp = (loop_p) Lelem(pi);
215 oldsrlpx(lp->lp_extend);
223 if (IS_ENTERED_WITH_GTO(p)) return;
236 go(argc,argv,no_action,sr_optimize,sr_machinit,no_action);
237 report("strength reductions",Ssr);