d62387a7970d0b04f164c9c3c73a0ebf141b1edd
[libfirm] / ir / ana / irscc.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ana/irscc.c
4  * Purpose:     Compute the strongly connected regions and build
5  *              backedge/loop datastructures.
6  * Author:      Goetz Lindenmaier
7  * Modified by:
8  * Created:     7.2002
9  * CVS-ID:      $Id$
10  * Copyright:   (c) 2002-2003 Universität Karlsruhe
11  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
12  */
13
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
17
18 #include <string.h>
19
20 #include "irloop_t.h"
21 #include "irnode.h"
22 #include "irgraph_t.h"
23 #include "array.h"
24 #include "pmap.h"
25 #include "irgwalk.h"
26 #include "irprog.h"
27
28 ir_graph *outermost_ir_graph;      /* The outermost graph the scc is computed
29                                       for */
30 static ir_loop *current_loop;      /* Current loop construction is working
31                                       on. */
32 static int loop_node_cnt = 0;      /* Counts the number of allocated loop nodes.
33                                       Each loop node gets a unique number.
34                                       What for? ev. remove. @@@ */
35 static int current_dfn = 1;        /* Counter to generate depth first numbering
36                                       of visited nodes.  */
37
38 /**********************************************************************/
39 /* Node attributes                                                   **/
40 /**********************************************************************/
41
42 /* A map to get from irnodes to loop nodes. */
43 static pmap *node_loop_map = NULL;
44
45 /**********************************************************************/
46 /* Node attributes needed for the construction.                      **/
47 /**********************************************************************/
48
49 typedef struct scc_info {
50   bool in_stack;         /* Marks whether node is on the stack. */
51   int dfn;               /* Depth first search number. */
52   int uplink;            /* dfn number of ancestor. */
53   //  ir_loop *loop;         /* Refers to the containing loop. */
54   /*
55       struct section *section;
56       xset def;
57       xset use;
58   */
59 } scc_info;
60
61 static INLINE scc_info* new_scc_info(void) {
62   scc_info *info = obstack_alloc (outermost_ir_graph->obst, sizeof (scc_info));
63   memset (info, 0, sizeof (scc_info));
64   return info;
65 }
66
67 static INLINE void
68 mark_irn_in_stack (ir_node *n) {
69   assert(get_irn_link(n));
70   ((scc_info *)get_irn_link(n))->in_stack = true;
71 }
72
73 static INLINE void
74 mark_irn_not_in_stack (ir_node *n) {
75   assert(get_irn_link(n));
76   ((scc_info *)get_irn_link(n))->in_stack = false;
77 }
78
79 static INLINE bool
80 irn_is_in_stack (ir_node *n) {
81   assert(get_irn_link(n));
82   return ((scc_info *)get_irn_link(n))->in_stack;
83 }
84
85 static INLINE void
86 set_irn_uplink (ir_node *n, int uplink) {
87   assert(get_irn_link(n));
88   ((scc_info *)get_irn_link(n))->uplink = uplink;
89 }
90
91 static INLINE int
92 get_irn_uplink (ir_node *n) {
93   assert(get_irn_link(n));
94   return ((scc_info *)get_irn_link(n))->uplink;
95 }
96
97 static INLINE void
98 set_irn_dfn (ir_node *n, int dfn) {
99   if (! get_irn_link(n)) { DDMN(n); DDME(get_irg_ent(current_ir_graph));}
100   assert(get_irn_link(n));
101   ((scc_info *)get_irn_link(n))->dfn = dfn;
102 }
103
104 static INLINE int
105 get_irn_dfn (ir_node *n) {
106   assert(get_irn_link(n));
107   return ((scc_info *)get_irn_link(n))->dfn;
108 }
109
110 /* Uses temporary information to set the loop */
111 static INLINE void
112 set_irn_loop_tmp (ir_node *n, ir_loop* loop) {
113   //assert(get_irn_link(n));
114   //((scc_info *)get_irn_link(n))->loop = loop;
115   assert(node_loop_map && "not initialized!");
116   pmap_insert(node_loop_map, (void *)n, (void *)loop);
117 }
118
119 /* Uses temporary information to get the loop */
120 INLINE ir_loop *
121 get_irn_loop (ir_node *n) {
122   ir_loop *res = NULL;
123   //assert(get_irn_link(n));
124   //return ((scc_info *)get_irn_link(n))->loop;
125   assert(node_loop_map && "not initialized!");
126
127   if (pmap_contains(node_loop_map, (void *)n))
128     res = (ir_loop *) pmap_get(node_loop_map, (void *)n);
129
130   return res;
131 }
132
133 #if 0
134 static ir_loop *find_nodes_loop (ir_node *n, ir_loop *l) {
135   int i;
136   ir_loop *res = NULL;
137
138   /* Test whether n is contained in this loop. */
139   for (i = 0; i < get_loop_n_nodes(l); i++)
140     if (n == get_loop_node(l, i)) return l;
141
142   /* Is this a leave in the loop tree? If so loop not found. */
143   if (get_loop_n_sons(l) == 0) return NULL;
144
145   /* Else descend in the loop tree. */
146   for (i = 0; i < get_loop_n_sons(l); i++) {
147     res = find_nodes_loop(n, get_loop_son(l, i));
148     if (res) break;
149   }
150   return res;
151 }
152
153 /* @@@ temporary implementation, costly!!! */
154 ir_loop * get_irn_loop(ir_node *n) {
155   ir_loop *l = get_irg_loop(current_ir_graph);
156   l = find_nodes_loop(n, l);
157   return l;
158 }
159 #endif
160
161 /**********************************************************************/
162 /* A stack.                                                          **/
163 /**********************************************************************/
164
165 static ir_node **stack = NULL;
166 static int tos = 0;                /* top of stack */
167
168 static INLINE void init_stack(void) {
169   if (stack) {
170     ARR_RESIZE (ir_node *, stack, 1000);
171   } else {
172     stack = NEW_ARR_F (ir_node *, 1000);
173   }
174   tos = 0;
175 }
176
177 #if 0
178 static INLINE void free_stack(void) {
179   DEL_ARR_F(stack);
180   stack = NULL;
181   tos = 0;
182 }
183 #endif
184
185 static INLINE void
186 push (ir_node *n)
187 {
188   /*DDMN(n);*/
189
190   if (tos == ARR_LEN (stack)) {
191     int nlen = ARR_LEN (stack) * 2;
192     ARR_RESIZE (ir_node *, stack, nlen);
193   }
194   stack [tos++] = n;
195   mark_irn_in_stack(n);
196 }
197
198 static INLINE ir_node *
199 pop (void)
200 {
201   ir_node *n = stack[--tos];
202   mark_irn_not_in_stack(n);
203   return n;
204 }
205
206 /* The nodes up to n belong to the current loop.
207    Removes them from the stack and adds them to the current loop. */
208 static INLINE void
209 pop_scc_to_loop (ir_node *n)
210 {
211   ir_node *m;
212
213   /*for (;;) {*/
214   do
215     {
216     m = pop();
217     loop_node_cnt++;
218     set_irn_dfn(m, loop_node_cnt);
219     add_loop_node(current_loop, m);
220     set_irn_loop_tmp(m, current_loop);
221     /*    if (m==n) break;*/
222     } while(m != n);
223 }
224
225 /* GL ??? my last son is my grandson???  Removes loops with no
226    ir_nodes in them.  Such loops have only another loop as son. (Why
227    can't they have two loops as sons? Does it never get that far? ) */
228 void close_loop (ir_loop *l)
229 {
230   int last = get_loop_n_elements(l) - 1;
231   loop_element lelement = get_loop_element(l, last);
232   ir_loop *last_son = lelement.son;
233
234   if (get_kind(last_son) == k_ir_loop &&
235       get_loop_n_elements(last_son) == 1)
236     {
237       lelement = get_loop_element(last_son, 0);
238       ir_loop *gson = lelement.son;
239       if(get_kind(gson) == k_ir_loop)
240         {
241           gson -> outer_loop = l;
242           loop_element new_last_son;
243           new_last_son.son = gson;
244           l -> children[last] = new_last_son;
245         }
246     }
247
248   current_loop = l;
249 }
250
251 /* Removes and unmarks all nodes up to n from the stack.
252    The nodes must be visited once more to assign them to a scc. */
253 static INLINE void
254 pop_scc_unmark_visit (ir_node *n)
255 {
256   ir_node *m = NULL;
257
258   while (m != n) {
259     m = pop();
260     set_irn_visited(m, 0);
261   }
262 }
263
264 /**********************************************************************/
265 /* The loop datastructure.                                           **/
266 /**********************************************************************/
267
268 /* Allocates a new loop as son of current_loop.  Sets current_loop
269    to the new loop and returns the father. */
270 static ir_loop *new_loop (void) {
271   ir_loop *father, *son;
272
273   father = current_loop;
274
275   son = (ir_loop *) obstack_alloc (outermost_ir_graph->obst, sizeof (ir_loop));
276   memset (son, 0, sizeof (ir_loop));
277   son->kind = k_ir_loop;
278   son->children = NEW_ARR_F (loop_element, 0);
279   son->n_nodes = 0;
280   son->n_sons=0;
281   if (father) {
282     son->outer_loop = father;
283     add_loop_son(father, son);
284     son->depth = father->depth+1;
285   } else {  /* The root loop */
286     son->outer_loop = son;
287     son->depth = 0;
288   }
289
290   current_loop = son;
291   return father;
292 }
293
294 #if 0
295 /* Finishes the datastructures, copies the arrays to the obstack
296    of current_ir_graph.
297    A. Schoesser: Caution: loop -> sons is gone. */
298 static void mature_loop (ir_loop *loop) {
299   ir_loop **new_sons;
300
301   new_sons = NEW_ARR_D (ir_loop *, current_ir_graph->obst, ARR_LEN(loop->sons));
302   memcpy (new_sons, loop->sons, sizeof (ir_loop *) * ARR_LEN(loop->sons));
303   DEL_ARR_F(loop->sons);
304   loop->sons = new_sons;
305 }
306 #endif
307
308 /* Returns outer loop, itself if outermost. */
309 ir_loop *get_loop_outer_loop (ir_loop *loop) {
310   assert(loop && loop->kind == k_ir_loop);
311   return loop->outer_loop;
312 }
313
314 /* Returns nesting depth of this loop */
315 int get_loop_depth (ir_loop *loop) {
316   assert(loop); assert(loop->kind == k_ir_loop);
317   return loop->depth;
318 }
319
320 /* Returns the number of inner loops */
321 int      get_loop_n_sons (ir_loop *loop) {
322   assert(loop && loop->kind == k_ir_loop);
323   return(loop -> n_sons);
324 }
325
326 /* Returns the pos`th loop_node-child              *
327  * TODO: This method isn`t very efficient !        *
328  * Returns NULL if there isnt`t a pos`th loop_node */
329 ir_loop *get_loop_son (ir_loop *loop, int pos) {
330   int child_nr = 0, loop_nr = -1;
331
332   assert(loop && loop->kind == k_ir_loop);
333   while(child_nr < ARR_LEN(loop->children))
334    {
335     if(*(loop -> children[child_nr].kind) == k_ir_loop)
336       loop_nr++;
337     if(loop_nr == pos)
338       return(loop -> children[child_nr].son);
339     child_nr++;
340    }
341   return NULL;
342 }
343
344 /* Use EXCLUSIVELY this function to add sons, otherwise the loop->n_sons
345    is invalid! */
346
347 static INLINE void
348 add_loop_son(ir_loop *loop, ir_loop *son) {
349   loop_element lson;
350   lson.son = son;
351   assert(loop && loop->kind == k_ir_loop);
352   assert(get_kind(son) == k_ir_loop);
353   ARR_APP1 (loop_element, loop->children, lson);
354   loop -> n_sons++;
355 }
356
357 /* Returns the number of nodes in the loop */
358 int      get_loop_n_nodes (ir_loop *loop) {
359   assert(loop); assert(loop->kind == k_ir_loop);
360   return loop -> n_nodes;
361 /*  return ARR_LEN(loop->nodes); */
362 }
363
364 /* Returns the pos`th ir_node-child                *
365  * TODO: This method isn`t very efficient !        *
366  * Returns NULL if there isnt`t a pos`th ir_node   */
367 ir_node *get_loop_node (ir_loop *loop, int pos) {
368   int child_nr, node_nr = -1;
369
370   assert(loop && loop->kind == k_ir_loop);
371   assert(pos < get_loop_n_nodes(loop));
372
373   for (child_nr = 0; child_nr < ARR_LEN(loop->children); child_nr++) {
374     if(*(loop -> children[child_nr].kind) == k_ir_node)
375       node_nr++;
376     if(node_nr == pos)
377       return(loop -> children[child_nr].node);
378   }
379   assert(0 && "no child at pos found");
380   return NULL;
381 }
382
383 /* Use EXCLUSIVELY this function to add nodes, otherwise the loop->n_nodes
384    is invalid! */
385
386 static INLINE void
387 add_loop_node(ir_loop *loop, ir_node *n) {
388   loop_element ln;
389   ln.node=n;
390   assert(loop && loop->kind == k_ir_loop);
391   assert(get_kind(n) == k_ir_node);
392   ARR_APP1 (loop_element, loop->children, ln);
393   loop->n_nodes++;
394 }
395
396 /** Returns the number of elements contained in loop.  */
397 int get_loop_n_elements (ir_loop *loop) {
398   assert(loop && loop->kind == k_ir_loop);
399   return(ARR_LEN(loop->children));
400 }
401
402 /*
403  Returns the pos`th loop element.
404  This may be a loop_node or a ir_node. The caller of this function has
405  to check the *(loop_element.kind) field for "k_ir_node" or "k_ir_loop"
406  and then select the apropriate "loop_element.node" or "loop_element.son".
407 */
408
409 loop_element get_loop_element (ir_loop *loop, int pos) {
410   assert(loop && loop->kind == k_ir_loop && pos < ARR_LEN(loop->children));
411
412   return(loop -> children[pos]);
413 }
414
415 /* The outermost loop is remarked in the surrounding graph. */
416 void     set_irg_loop(ir_graph *irg, ir_loop *loop) {
417   assert(irg);
418   irg->loop = loop;
419 }
420 ir_loop *get_irg_loop(ir_graph *irg) {
421   assert(irg);
422   return irg->loop;
423 }
424
425
426 /**********************************************************************/
427 /* Constructing and destructing the loop/backedge information.       **/
428 /**********************************************************************/
429
430 /* Initialization steps. **********************************************/
431
432 static INLINE void
433 init_node (ir_node *n, void *env) {
434   set_irn_link (n, new_scc_info());
435   clear_backedges(n);
436 #if 0
437   /* Also init nodes not visible in intraproc_view. */
438     /* @@@ init_node is called for too many nodes -- this wastes memory!.
439        The mem is not lost as its on the obstack. */
440   if (get_irn_op(n) == op_Filter) {
441     for (i = 0; i < get_Filter_n_cg_preds(n); i++)
442       init_node(get_Filter_cg_pred(n, i), NULL);
443   }
444   if (get_irn_op(n) == op_Block) {
445     for (i = 0; i < get_Block_cg_n_cfgpreds(n); i++) {
446       init_node(get_Block_cg_cfgpred(n, i), NULL);
447     }
448   }
449   /* The following pattern matches only after a call from above pattern. */
450   if ((get_irn_op(n) == op_Proj) /*&& (get_Proj_proj(n) == 0)*/) {
451     /* @@@ init_node is called for every proj -- this wastes memory!.
452        The mem is not lost as its on the obstack. */
453     ir_node *cb = get_Proj_pred(n);
454     if ((get_irn_op(cb) == op_CallBegin) ||
455         (get_irn_op(cb) == op_EndReg) ||
456         (get_irn_op(cb) == op_EndExcept)) {
457       init_node(cb, NULL);
458       init_node(get_nodes_Block(cb), NULL);
459     }
460 #endif
461 }
462
463 static INLINE void
464 init_scc (ir_graph *irg) {
465   current_dfn = 1;
466   loop_node_cnt = 0;
467   if (!node_loop_map) node_loop_map = pmap_create();
468   init_stack();
469   irg_walk_graph (irg, init_node, NULL, NULL);
470   /*
471   irg_walk (irg, link_to_reg_end, NULL, NULL);
472   */
473 }
474
475 static INLINE void
476 init_ip_scc (void) {
477   current_dfn = 1;
478   loop_node_cnt = 0;
479   init_stack();
480   cg_walk (init_node, NULL, NULL);
481 }
482
483 #if 0
484 Works, but is inefficient.
485 static INLINE void
486 init_ip_scc (void) {
487   int i;
488   interprocedural_view = 1;
489   current_dfn = 1;
490   loop_node_cnt = 0;
491   init_stack();
492   for (i = 0; i < get_irp_n_irgs(); i++) {
493     current_ir_graph = get_irp_irg(i);
494     irg_walk_graph (current_ir_graph, init_node, NULL, NULL);
495     /* @@@ decrease max_visited to avoide double walks */
496   }
497 }
498 #endif
499
500 /* Condition for breaking the recursion. */
501 static bool is_outermost_Start(ir_node *n) {
502   /* Test whether this is the outermost Start node.  If so
503      recursion must end. */
504   if ((get_irn_op(n) == op_Block)     &&
505       (get_Block_n_cfgpreds(n) == 1)  &&
506       (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Start) &&
507       (get_nodes_Block(skip_Proj(get_Block_cfgpred(n, 0))) == n)) {
508     return true;
509   }
510 #if 0
511   /*  @@@ Bad condition:
512       not possible in interprocedural view as outermost_graph is
513       not necessarily the only with a dead-end start block.
514       Besides current_ir_graph is not set properly. */
515   if ((get_irn_op(n) == op_Block) &&
516       (n == get_irg_start_block(current_ir_graph))) {
517     if ((!interprocedural_view)  ||
518         (current_ir_graph == outermost_ir_graph))
519       return true;
520   }
521 #endif
522   return false;
523 }
524
525 /* Don't walk from nodes to blocks except for Control flow operations. */
526 static INLINE int
527 get_start_index(ir_node *n) {
528   if (is_cfop(n) || is_fragile_op(n) || get_irn_op(n) == op_Start)
529     return -1;
530   else
531     return 0;
532 }
533
534 /* Returns current_ir_graph and set it to the irg of predecessor index
535    of node n. */
536 static INLINE ir_graph *
537 switch_irg (ir_node *n, int index) {
538   ir_graph *old_current = current_ir_graph;
539
540   if (interprocedural_view) {
541     /* Only Filter and Block nodes can have predecessors in other graphs. */
542     if (get_irn_op(n) == op_Filter)
543       n = get_nodes_Block(n);
544     if (get_irn_op(n) == op_Block) {
545       ir_node *cfop = skip_Proj(get_Block_cfgpred(n, index));
546       if (is_ip_cfop(cfop)) {
547         current_ir_graph = get_irn_irg(cfop);
548         set_irg_visited(current_ir_graph, get_max_irg_visited());
549       }
550     }
551   }
552
553   return old_current;
554 }
555
556 /* Walks up the stack passing n and then finding the node
557    where we walked into the irg n is contained in.
558    Here we switch the irg. */
559 static ir_graph *
560 find_irg_on_stack (ir_node *n) {
561   ir_node *m;
562   ir_graph *old_current = current_ir_graph;
563   int i;
564
565   if (interprocedural_view) {
566     for (i = tos; i >= 0; i--) {
567       if (stack[i] == n) break;
568     }
569     if (i < 0) i = tos;
570
571     assert (i >= 0);
572     for (; i >= 0; i--) {
573       m = stack[i];
574       /*printf(" Visiting %d ", i); DDMN(m);*/
575       if (is_ip_cfop(m)) {
576         current_ir_graph = get_irn_irg(m);
577         break;
578       }
579       if (get_irn_op(m) == op_Filter) {
580         /* Find the corresponding ip_cfop */
581         ir_node *pred = stack[i+1];
582         int j;
583         for (j = 0; j < get_Filter_n_cg_preds(m); j++)
584           if (get_Filter_cg_pred(m, j) == pred) break;
585         if (j >= get_Filter_n_cg_preds(m))
586           /* It is a filter we didn't pass as the predecessors are marked. */
587           continue;
588         assert(get_Filter_cg_pred(m, j) == pred);
589         switch_irg(m, j);
590         break;
591       }
592     }
593   }
594
595   return old_current;
596 }
597
598 #if 0
599 static void test(ir_node *pred, ir_node *root, ir_node *this) {
600   int i;
601   if (get_irn_uplink(pred) >= get_irn_uplink(root)) return;
602
603   printf("this: %d ", get_irn_uplink(this)); DDMN(this);
604   printf("pred: %d ", get_irn_uplink(pred)); DDMN(pred);
605   printf("root: %d ", get_irn_uplink(root)); DDMN(root);
606
607   printf("tos: %d\n", tos);
608
609   for (i = tos; i >= 0; i--) {
610     ir_node *n = stack[i];
611     if (!n) continue;
612     printf(" uplink: %d, pos: %d ", get_irn_uplink(n), i); DDMN(n);
613   }
614 }
615 #endif
616
617 /* Test for legal loop header: Block, Phi, ... */
618 INLINE static bool is_possible_loop_head(ir_node *n) {
619   return ((get_irn_op(n) == op_Block) ||
620           (get_irn_op(n) == op_Phi) ||
621           ((get_irn_op(n) == op_Filter) && interprocedural_view));
622 }
623
624 /* Returns true if n is a loop header, i.e., it is a Block, Phi
625    or Filter node and has predecessors within the loop and out
626    of the loop. */
627 static bool
628 is_head (ir_node *n, ir_node *root)
629 {
630   int i;
631   int some_outof_loop = 0,  some_in_loop = 0;
632
633   /* Test for legal loop header: Block, Phi, ... */
634   if (!is_possible_loop_head(n))
635     return false;
636
637   if (!is_outermost_Start(n)) {
638     for (i = get_start_index(n); i < get_irn_arity(n); i++) {
639       ir_node *pred = get_irn_n(n, i);
640       assert(pred);
641       if (is_backedge(n, i)) continue;
642       if (!irn_is_in_stack(pred)) {
643         some_outof_loop = 1;
644       } else {
645         assert(get_irn_uplink(pred) >= get_irn_uplink(root));
646         some_in_loop = 1;
647       }
648     }
649   }
650   return some_outof_loop && some_in_loop;
651 }
652
653 /* Returns index of the predecessor with the smallest dfn number
654    greater-equal than limit. */
655 static int
656 smallest_dfn_pred (ir_node *n, int limit)
657 {
658   int i, index = -2, min = -1;
659
660   if (!is_outermost_Start(n)) {
661     for (i = get_start_index(n); i < get_irn_arity(n); i++) {
662       ir_node *pred = get_irn_n(n, i);
663       assert(pred);
664       if (is_backedge(n, i) || !irn_is_in_stack(pred)) continue;
665       if (get_irn_dfn(pred) >= limit
666         && (min == -1 || get_irn_dfn(pred) < min)) {
667         index = i;
668         min = get_irn_dfn(pred);
669       }
670     }
671   }
672   return index;
673 }
674
675 /* Returns index of the predecessor with the largest dfn number. */
676 static int
677 largest_dfn_pred (ir_node *n)
678 {
679   int i, index = -2, max = -1;
680
681   if (!is_outermost_Start(n)) {
682     for (i = get_start_index(n); i < get_irn_arity(n); i++) {
683       ir_node *pred = get_irn_n(n, i);
684       if (is_backedge (n, i) || !irn_is_in_stack(pred)) continue;
685       if (get_irn_dfn(pred) > max) {
686         index = i;
687         max = get_irn_dfn(pred);
688       }
689     }
690   }
691   return index;
692 }
693
694 /* Searches the stack for possible loop heads.  Tests these for backedges.
695    If it finds a head with an unmarked backedge it marks this edge and
696    returns the tail of the loop.
697    If it finds no backedge returns NULL.
698    ("disable_backedge" in fiasco) */
699
700 static ir_node *
701 find_tail (ir_node *n) {
702   ir_node *m;
703   int i, res_index = -2;
704
705   /*
706     if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
707   */
708
709   m = stack[tos-1];  /* tos = top of stack */
710   if (is_head (m, n)) {
711     res_index = smallest_dfn_pred(m, 0);
712     if ((res_index == -2) &&  /* no smallest dfn pred found. */
713         (n == m))
714       return NULL;
715   } else {
716     if (m == n) return NULL;
717     for (i = tos-2; ; --i) {
718       m = stack[i];
719       if (is_head (m, n)) {
720         res_index = smallest_dfn_pred (m, get_irn_dfn(m) + 1);
721         if (res_index == -2)  /* no smallest dfn pred found. */
722           res_index = largest_dfn_pred (m);
723         break;
724       }
725     }
726   }
727   assert (res_index > -2);
728
729   set_backedge (m, res_index);
730   return is_outermost_Start(n) ? NULL : get_irn_n(m, res_index);
731 }
732
733
734 /* The core algorithm. *****************************************/
735
736 static void scc (ir_node *n) {
737   int i;
738   ir_graph *rem;
739
740   if (irn_visited(n)) return;
741   mark_irn_visited(n);
742   /*printf("mark: %d ", get_irn_visited(n)); DDMN(n);
743   DDME(get_irg_ent(current_ir_graph));*/
744
745   /* Initialize the node */
746   set_irn_dfn(n, current_dfn);      /* Depth first number for this node */
747   set_irn_uplink(n, current_dfn);   /* ... is default uplink. */
748   set_irn_loop_tmp(n, NULL);
749   current_dfn ++;
750
751   /* What's this good for?
752   n->ana.scc.section = NULL;
753   */
754
755   push(n);
756
757   if (!is_outermost_Start(n)) {
758     for (i = get_start_index(n); i < get_irn_arity(n); i++) {
759       ir_node *m;
760       if (is_backedge(n, i)) continue;
761
762       m = get_irn_n(n, i); /*get_irn_ip_pred(n, i);*/
763       if ((!m) || (get_irn_op(m) == op_Unknown)) continue;
764       scc (m);
765       /*return_recur(n, i);*/
766
767       if (irn_is_in_stack(m)) {
768         /* Uplink of m is smaller if n->m is a backedge.
769            Propagate the uplink to mark the loop. */
770         if (get_irn_uplink(m) < get_irn_uplink(n))
771           set_irn_uplink(n, get_irn_uplink(m));
772       }
773     }
774   }
775   if (get_irn_dfn(n) == get_irn_uplink(n)) {
776     /* This condition holds for the node with the incoming backedge. */
777     ir_node *tail = find_tail(n);
778     if (tail) {
779       /* We found a new loop! */
780       ir_loop *l = new_loop();
781
782       /* Remove the loop from the stack ... */
783       pop_scc_unmark_visit (n);
784       /* and recompute it in a better order; and so that it goes into
785          the new loop. */
786       rem = find_irg_on_stack(tail);
787
788       scc (tail);
789       current_ir_graph = rem;
790
791       assert (irn_visited(n));
792       close_loop(l);
793
794       /*      current_loop = l; AS: This is done close_loop */
795     } else {
796       pop_scc_to_loop(n);
797     }
798   }
799 }
800
801 /* Constructs backedge information for irg. In interprocedural view constructs
802    backedges for all methods called by irg, too. */
803 void construct_backedges(ir_graph *irg) {
804   ir_graph *rem = current_ir_graph;
805   ir_loop *head_rem;
806   int i;
807
808   assert(!interprocedural_view &&
809          "not implemented, use construct_ip_backedges");
810
811   current_ir_graph = irg;
812   outermost_ir_graph = irg;
813
814   init_scc(irg);
815
816   current_loop = NULL;
817   new_loop();  /* sets current_loop */
818   head_rem = current_loop; /* Just for assertion */
819
820   if (interprocedural_view) {
821     set_irg_visited(irg, inc_max_irg_visited());
822     init_ip_walk ();
823   } else {
824     inc_irg_visited(irg);
825   }
826
827   scc(get_irg_end(irg));
828   for (i = 0; i < get_End_n_keepalives(get_irg_end(irg)); i++)
829     scc(get_End_keepalive(get_irg_end(irg), i));
830
831   if (interprocedural_view) finish_ip_walk();
832
833   assert(head_rem == current_loop);
834   set_irg_loop(irg, current_loop);
835   assert(get_irg_loop(irg)->kind == k_ir_loop);
836   /*
837   irg->loops = current_loop;
838   if (icfg == 1) {
839     int count = 0;
840     int depth = 0;
841     count_loop (the_loop, &count, &depth);
842     }
843   }
844   */
845   current_ir_graph = rem;
846 }
847
848
849
850 void construct_ip_backedges (void) {
851   ir_graph *rem = current_ir_graph;
852   int rem_ipv = interprocedural_view;
853   int i, j;
854
855   outermost_ir_graph = get_irp_main_irg();
856
857   init_ip_scc();
858
859   current_loop = NULL;
860   new_loop();  /* sets current_loop */
861   interprocedural_view = 1;
862
863   inc_max_irg_visited();
864   for (i = 0; i < get_irp_n_irgs(); i++)
865     set_irg_visited(get_irp_irg(i), get_max_irg_visited());
866
867   for (i = 0; i < get_irp_n_irgs(); i++) {
868     ir_node *sb;
869     current_ir_graph = get_irp_irg(i);
870     /*DDME(get_irg_ent(current_ir_graph));*/
871     /* Find real entry points */
872     sb = get_irg_start_block(current_ir_graph);
873     if ((get_Block_n_cfgpreds(sb) > 1) ||
874         (get_nodes_Block(get_Block_cfgpred(sb, 0)) != sb)) continue;
875     /*    printf("running scc for "); DDME(get_irg_ent(current_ir_graph));   */
876     /* Compute scc for this graph */
877     outermost_ir_graph = current_ir_graph;
878     set_irg_visited(outermost_ir_graph, get_max_irg_visited());
879     scc(get_irg_end(current_ir_graph));
880     for (j = 0; j < get_End_n_keepalives(get_irg_end(outermost_ir_graph)); j++)
881       scc(get_End_keepalive(get_irg_end(outermost_ir_graph), j));
882   }
883
884   set_irg_loop(outermost_ir_graph, current_loop);
885   assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
886
887   current_ir_graph = rem;
888   interprocedural_view = rem_ipv;
889 }
890
891
892 static void reset_backedges(ir_node *n, void *env) {
893   if (is_possible_loop_head(n))
894     clear_backedges(n);
895 }
896
897 /** Removes all loop information.
898     Resets all backedges */
899 void free_loop_information(ir_graph *irg) {
900   set_irg_loop(irg, NULL);
901   /* We cannot free the loop nodes, they are on the obstack. */
902   irg_walk_graph(irg, NULL, reset_backedges, NULL);
903 }
904
905
906 void free_all_loop_information (void) {
907   int i;
908   int rem = interprocedural_view;
909   interprocedural_view = 1;  /* To visit all filter nodes */
910   for (i = 0; i < get_irp_n_irgs(); i++) {
911     free_loop_information(get_irp_irg(i));
912   }
913   pmap_destroy(node_loop_map);
914   node_loop_map = NULL;
915   interprocedural_view = rem;
916 }