1 /* $Id: cf_loop.c,v 1.4 1994/06/24 10:20:23 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 /* C O N T R O L F L O W
12 #include "../share/types.h"
13 #include "../share/debug.h"
14 #include "../share/lset.h"
15 #include "../share/alloc.h"
16 #include "../share/aux.h"
19 #define MARK_STRONG(b) b->b_flags |= BF_STRONG
20 #define MARK_FIRM(b) b->b_flags |= BF_FIRM
22 #define MARK(b) b->b_flags |= BF_MARK
23 #define MARKED(b) (b->b_flags&BF_MARK)
24 #define INSIDE_LOOP(b,lp) Lis_elem(b,lp->LP_BLOCKS)
28 /* The algorithm to detect loops that is used here is taken
29 * from: Aho & Ullman, Principles of Compiler Design, section 13.1.
30 * The algorithm uses the dominator relation between nodes
31 * of the control flow graph:
32 * d DOM n => every path from the initial node to n goes through d.
33 * The dominator relation is recorded via the immediate dominator tree
34 * (b_idom field of bblock struct) from which the dominator relation
35 * can be easily computed (see procedure 'dom' below).
36 * The algorithm first finds 'back edges'. A back edge is an edge
37 * a->b in the flow graph whose head (b) dominates its tail (a).
38 * The 'natural loop' of back edge n->d consists of those nodes
39 * that can reach n without going through d. These nodes, plus d
41 * The whole process is rather complex, because different back edges
42 * may result in the same loop and because loops may partly overlap
43 * each other (without one being nested inside the other).
48 STATIC bool same_loop(l1,l2)
51 /* Two loops are the same if:
52 * (1) they have the same number of basic blocks, and
53 * (2) the head of the back edge of the first loop
54 * also is part of the second loop, and
55 * (3) the tail of the back edge of the first loop
56 * also is part of the second loop.
59 return (l1->LP_COUNT == l2->LP_COUNT &&
60 Lis_elem(l1->lp_entry, l2->LP_BLOCKS) &&
61 Lis_elem(l1->lp_end, l2->LP_BLOCKS));
66 STATIC bool inner_loop(l1,l2)
69 /* Loop l1 is an inner loop of l2 if:
70 * (1) the first loop has fewer basic blocks than
72 * (2) the head of the back edge of the first loop
73 * also is part of the second loop, and
74 * (3) the tail of the back edge of the first loop
75 * also is part of the second loop.
78 return (l1->LP_COUNT < l2->LP_COUNT &&
79 Lis_elem(l1->lp_entry, l2->LP_BLOCKS) &&
80 Lis_elem(l1->lp_end, l2->LP_BLOCKS));
85 STATIC insrt(b,lpb,s_p)
90 /* Auxiliary routine used by 'natural_loop'.
91 * Note that we use a set rather than a stack,
95 if (!Lis_elem(b,*lpb)) {
102 STATIC loop_p natural_loop(d,n)
105 /* Find the basic blocks of the natural loop of the
106 * back edge 'n->d' (i.e. n->d is an edge in the control
107 * flow graph and d dominates n). The natural loop consists
108 * of those blocks which can reach n without going through d.
109 * We find these blocks by finding all predecessors of n,
120 lp->lp_extend = newcflpx();
121 lp->lp_entry = d; /* loop entry block */
122 lp->lp_end = n; /* tail of back edge */
124 loopblocks = Lempty_set();
126 insrt(n,&loopblocks,&s);
127 while ((pi = Lfirst(s)) != (Lindex) 0) {
128 m = (bblock_p) Lelem(pi);
130 for (pi = Lfirst(m->b_pred); pi != (Lindex) 0;
131 pi = Lnext(pi,m->b_pred)) {
132 insrt((bblock_p) Lelem(pi),&loopblocks,&s);
135 lp->LP_BLOCKS = loopblocks;
136 lp->LP_COUNT = Lnrelems(loopblocks);
141 STATIC loop_p org_loop(lp,loops)
145 /* See if the loop lp was already found via another
146 * back edge; if so return this loop; else return 0.
151 for (li = Lfirst(loops); li != (Lindex) 0; li = Lnext(li,loops)) {
152 if (same_loop((loop_p) Lelem(li), lp)) {
154 /* printf("messy loop found\n"); */
156 return (loop_p) Lelem(li);
164 STATIC collapse_loops(loops_p)
167 register Lindex li1, li2;
168 register loop_p lp1,lp2;
170 for (li1 = Lfirst(*loops_p); li1 != (Lindex) 0; li1 = Lnext(li1,*loops_p)) {
171 lp1 = (loop_p) Lelem(li1);
172 lp1->lp_level = (short) 0;
173 for (li2 = Lfirst(*loops_p); li2 != (Lindex) 0;
174 li2 = Lnext(li2,*loops_p)) {
175 lp2 = (loop_p) Lelem(li2);
176 if (lp1 != lp2 && lp1->lp_entry == lp2->lp_entry) {
177 Ljoin(lp2->LP_BLOCKS,&lp1->LP_BLOCKS);
178 oldcflpx(lp2->lp_extend);
179 Lremove(lp2,loops_p);
186 STATIC loop_per_block(lp)
191 /* Update the b_loops sets */
195 for (bi = Lfirst(lp->LP_BLOCKS); bi != (Lindex) 0;
196 bi = Lnext(bi,lp->LP_BLOCKS)) {
197 b = (bblock_p) Lelem(bi);
198 Ladd(lp,&(b->b_loops));
204 STATIC loop_attrib(loops)
207 /* Compute several attributes */
211 loop_id lastlpid = 0;
213 for (li = Lfirst(loops); li != (Lindex) 0; li = Lnext(li,loops)) {
214 lp = (loop_p) Lelem(li);
215 lp->lp_id = ++lastlpid;
222 STATIC nest_levels(loops)
225 /* Compute the nesting levels of all loops of
226 * the current procedure. For every loop we just count
227 * all loops of which the former is an inner loop.
228 * The running time is quadratic in the number of loops
229 * of the current procedure. As this number tends to be
230 * very small, there is no cause for alarm.
233 register Lindex li1, li2;
236 for (li1 = Lfirst(loops); li1 != (Lindex) 0; li1 = Lnext(li1,loops)) {
237 lp = (loop_p) Lelem(li1);
238 lp->lp_level = (short) 0;
239 for (li2 = Lfirst(loops); li2 != (Lindex) 0;
240 li2 = Lnext(li2,loops)) {
241 if (inner_loop(lp,(loop_p) Lelem(li2))) {
249 STATIC cleanup(loops)
252 /* Throw away the LP_BLOCKS sets */
256 for (i = Lfirst(loops); i != (Lindex) 0; i = Lnext(i,loops)) {
257 Ldeleteset(((loop_p) Lelem(i))->LP_BLOCKS);
262 STATIC bool does_exit(b,lp)
266 /* See if b may exit the loop, i.e. if it
267 * has a successor outside the loop
272 for (i = Lfirst(b->b_succ); i != (Lindex) 0; i = Lnext(i,b->b_succ)) {
273 if (!INSIDE_LOOP(Lelem(i),lp)) return TRUE;
279 STATIC mark_succ(b,lp)
286 for (i = Lfirst(b->b_succ); i != (Lindex) 0; i = Lnext(i,b->b_succ)) {
287 succ = (bblock_p) Lelem(i);
288 if (succ != b && succ != lp->lp_entry && INSIDE_LOOP(succ,lp) &&
297 STATIC mark_blocks(lp)
300 /* Mark the strong and firm blocks of a loop.
301 * The last set of blocks consists of the end-block
302 * of the loop (i.e. the head of the back edge
303 * of the natural loop) and its dominators
304 * (including the loop entry block, i.e. the
305 * tail of the back edge).
310 /* First mark all blocks that are the successor of a
311 * block that may exit the loop (i.e. contains a
312 * -possibly conditional- jump to somewhere outside
316 if (lp->LP_MESSY) return; /* messy loops are hopeless cases */
317 for (b = lp->lp_entry; b != (bblock_p) 0; b = b->b_next) {
318 if (!MARKED(b) && does_exit(b,lp)) {
323 /* Now find all firm blocks. A block is strong
324 * if it is firm and not marked.
327 for (b = lp->lp_end; ; b = b->b_idom) {
332 if (b == lp->lp_entry) break;
338 STATIC mark_loopblocks(loops)
341 /* Determine for all loops which basic blocks
342 * of the loop are strong (i.e. are executed
343 * during every iteration) and which blocks are
344 * firm (i.e. executed during every iteration with
345 * the only possible exception of the last one).
351 for (i = Lfirst(loops); i != (Lindex) 0; i = Lnext(i,loops)) {
352 lp = (loop_p) Lelem(i);
362 /* Find all natural loops of procedure p. Every loop is
363 * assigned a unique identifying number, a set of basic
364 * blocks, a loop entry block and a nesting level number.
365 * Every basic block is assigned a nesting level number
366 * and a set of loops it is part of.
369 lset loops; /* the set of all loops */
375 loops = Lempty_set();
376 for (b = p->p_start; b != (bblock_p) 0; b = b->b_next) {
377 for (si = Lfirst(b->b_succ); si != (Lindex) 0;
378 si = Lnext(si,b->b_succ)) {
379 s = (bblock_p) Lelem(si);
381 /* 'b->s' is a back edge */
382 lp = natural_loop(s,b);
383 if ((org = org_loop(lp,loops)) == (loop_p) 0) {
387 /* Same loop, generated by several back
388 * edges; such a loop is called a messy
391 org->LP_MESSY = TRUE;
392 Ldeleteset(lp->LP_BLOCKS);
393 oldcflpx(lp->lp_extend);
399 collapse_loops(&loops);
402 mark_loopblocks(loops); /* determine firm and strong blocks */