faster irn-loop access
[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 (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(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   }
461 #endif
462 }
463
464 static INLINE void
465 init_scc_common (void) {
466   current_dfn = 1;
467   loop_node_cnt = 0;
468   if (!node_loop_map) node_loop_map = pmap_create();
469   init_stack();
470 }
471
472 static INLINE void
473 init_scc (ir_graph *irg) {
474   init_scc_common();
475   irg_walk_graph (irg, init_node, NULL, NULL);
476   /*
477   irg_walk (irg, link_to_reg_end, NULL, NULL);
478   */
479 }
480
481 static INLINE void
482 init_ip_scc (void) {
483   init_scc_common();
484   cg_walk (init_node, NULL, NULL);
485 }
486
487 #if 0
488 Works, but is inefficient.
489 static INLINE void
490 init_ip_scc (void) {
491   int i;
492   interprocedural_view = 1;
493   current_dfn = 1;
494   loop_node_cnt = 0;
495   init_stack();
496   for (i = 0; i < get_irp_n_irgs(); i++) {
497     current_ir_graph = get_irp_irg(i);
498     irg_walk_graph (current_ir_graph, init_node, NULL, NULL);
499     /* @@@ decrease max_visited to avoide double walks */
500   }
501 }
502 #endif
503
504 /* Condition for breaking the recursion. */
505 static bool is_outermost_Start(ir_node *n) {
506   /* Test whether this is the outermost Start node.  If so
507      recursion must end. */
508   if ((get_irn_op(n) == op_Block)     &&
509       (get_Block_n_cfgpreds(n) == 1)  &&
510       (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Start) &&
511       (get_nodes_Block(skip_Proj(get_Block_cfgpred(n, 0))) == n)) {
512     return true;
513   }
514 #if 0
515   /*  @@@ Bad condition:
516       not possible in interprocedural view as outermost_graph is
517       not necessarily the only with a dead-end start block.
518       Besides current_ir_graph is not set properly. */
519   if ((get_irn_op(n) == op_Block) &&
520       (n == get_irg_start_block(current_ir_graph))) {
521     if ((!interprocedural_view)  ||
522         (current_ir_graph == outermost_ir_graph))
523       return true;
524   }
525 #endif
526   return false;
527 }
528
529 /* Don't walk from nodes to blocks except for Control flow operations. */
530 static INLINE int
531 get_start_index(ir_node *n) {
532   if (is_cfop(n) || is_fragile_op(n) || get_irn_op(n) == op_Start)
533     return -1;
534   else
535     return 0;
536 }
537
538 /* Returns current_ir_graph and set it to the irg of predecessor index
539    of node n. */
540 static INLINE ir_graph *
541 switch_irg (ir_node *n, int index) {
542   ir_graph *old_current = current_ir_graph;
543
544   if (interprocedural_view) {
545     /* Only Filter and Block nodes can have predecessors in other graphs. */
546     if (get_irn_op(n) == op_Filter)
547       n = get_nodes_Block(n);
548     if (get_irn_op(n) == op_Block) {
549       ir_node *cfop = skip_Proj(get_Block_cfgpred(n, index));
550       if (is_ip_cfop(cfop)) {
551         current_ir_graph = get_irn_irg(cfop);
552         set_irg_visited(current_ir_graph, get_max_irg_visited());
553       }
554     }
555   }
556
557   return old_current;
558 }
559
560 /* Walks up the stack passing n and then finding the node
561    where we walked into the irg n is contained in.
562    Here we switch the irg. */
563 static ir_graph *
564 find_irg_on_stack (ir_node *n) {
565   ir_node *m;
566   ir_graph *old_current = current_ir_graph;
567   int i;
568
569   if (interprocedural_view) {
570     for (i = tos; i >= 0; i--) {
571       if (stack[i] == n) break;
572     }
573     if (i < 0) i = tos;
574
575     assert (i >= 0);
576     for (; i >= 0; i--) {
577       m = stack[i];
578       /*printf(" Visiting %d ", i); DDMN(m);*/
579       if (is_ip_cfop(m)) {
580         current_ir_graph = get_irn_irg(m);
581         break;
582       }
583       if (get_irn_op(m) == op_Filter) {
584         /* Find the corresponding ip_cfop */
585         ir_node *pred = stack[i+1];
586         int j;
587         for (j = 0; j < get_Filter_n_cg_preds(m); j++)
588           if (get_Filter_cg_pred(m, j) == pred) break;
589         if (j >= get_Filter_n_cg_preds(m))
590           /* It is a filter we didn't pass as the predecessors are marked. */
591           continue;
592         assert(get_Filter_cg_pred(m, j) == pred);
593         switch_irg(m, j);
594         break;
595       }
596     }
597   }
598
599   return old_current;
600 }
601
602 #if 0
603 static void test(ir_node *pred, ir_node *root, ir_node *this) {
604   int i;
605   if (get_irn_uplink(pred) >= get_irn_uplink(root)) return;
606
607   printf("this: %d ", get_irn_uplink(this)); DDMN(this);
608   printf("pred: %d ", get_irn_uplink(pred)); DDMN(pred);
609   printf("root: %d ", get_irn_uplink(root)); DDMN(root);
610
611   printf("tos: %d\n", tos);
612
613   for (i = tos; i >= 0; i--) {
614     ir_node *n = stack[i];
615     if (!n) continue;
616     printf(" uplink: %d, pos: %d ", get_irn_uplink(n), i); DDMN(n);
617   }
618 }
619 #endif
620
621 /* Test for legal loop header: Block, Phi, ... */
622 INLINE static bool is_possible_loop_head(ir_node *n) {
623   return ((get_irn_op(n) == op_Block) ||
624           (get_irn_op(n) == op_Phi) ||
625           ((get_irn_op(n) == op_Filter) && interprocedural_view));
626 }
627
628 /* Returns true if n is a loop header, i.e., it is a Block, Phi
629    or Filter node and has predecessors within the loop and out
630    of the loop. */
631 static bool
632 is_head (ir_node *n, ir_node *root)
633 {
634   int i;
635   int some_outof_loop = 0,  some_in_loop = 0;
636
637   /* Test for legal loop header: Block, Phi, ... */
638   if (!is_possible_loop_head(n))
639     return false;
640
641   if (!is_outermost_Start(n)) {
642     for (i = get_start_index(n); i < get_irn_arity(n); i++) {
643       ir_node *pred = get_irn_n(n, i);
644       assert(pred);
645       if (is_backedge(n, i)) continue;
646       if (!irn_is_in_stack(pred)) {
647         some_outof_loop = 1;
648       } else {
649         assert(get_irn_uplink(pred) >= get_irn_uplink(root));
650         some_in_loop = 1;
651       }
652     }
653   }
654   return some_outof_loop && some_in_loop;
655 }
656
657 /* Returns index of the predecessor with the smallest dfn number
658    greater-equal than limit. */
659 static int
660 smallest_dfn_pred (ir_node *n, int limit)
661 {
662   int i, index = -2, min = -1;
663
664   if (!is_outermost_Start(n)) {
665     for (i = get_start_index(n); i < get_irn_arity(n); i++) {
666       ir_node *pred = get_irn_n(n, i);
667       assert(pred);
668       if (is_backedge(n, i) || !irn_is_in_stack(pred)) continue;
669       if (get_irn_dfn(pred) >= limit
670         && (min == -1 || get_irn_dfn(pred) < min)) {
671         index = i;
672         min = get_irn_dfn(pred);
673       }
674     }
675   }
676   return index;
677 }
678
679 /* Returns index of the predecessor with the largest dfn number. */
680 static int
681 largest_dfn_pred (ir_node *n)
682 {
683   int i, index = -2, max = -1;
684
685   if (!is_outermost_Start(n)) {
686     for (i = get_start_index(n); i < get_irn_arity(n); i++) {
687       ir_node *pred = get_irn_n(n, i);
688       if (is_backedge (n, i) || !irn_is_in_stack(pred)) continue;
689       if (get_irn_dfn(pred) > max) {
690         index = i;
691         max = get_irn_dfn(pred);
692       }
693     }
694   }
695   return index;
696 }
697
698 /* Searches the stack for possible loop heads.  Tests these for backedges.
699    If it finds a head with an unmarked backedge it marks this edge and
700    returns the tail of the loop.
701    If it finds no backedge returns NULL.
702    ("disable_backedge" in fiasco) */
703
704 static ir_node *
705 find_tail (ir_node *n) {
706   ir_node *m;
707   int i, res_index = -2;
708
709   /*
710     if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
711   */
712
713   m = stack[tos-1];  /* tos = top of stack */
714   if (is_head (m, n)) {
715     res_index = smallest_dfn_pred(m, 0);
716     if ((res_index == -2) &&  /* no smallest dfn pred found. */
717         (n == m))
718       return NULL;
719   } else {
720     if (m == n) return NULL;
721     for (i = tos-2; ; --i) {
722       m = stack[i];
723       if (is_head (m, n)) {
724         res_index = smallest_dfn_pred (m, get_irn_dfn(m) + 1);
725         if (res_index == -2)  /* no smallest dfn pred found. */
726           res_index = largest_dfn_pred (m);
727         break;
728       }
729     }
730   }
731   assert (res_index > -2);
732
733   set_backedge (m, res_index);
734   return is_outermost_Start(n) ? NULL : get_irn_n(m, res_index);
735 }
736
737
738 /* The core algorithm. *****************************************/
739
740 static void scc (ir_node *n) {
741   int i;
742   ir_graph *rem;
743
744   if (irn_visited(n)) return;
745   mark_irn_visited(n);
746   /*printf("mark: %d ", get_irn_visited(n)); DDMN(n);
747   DDME(get_irg_ent(current_ir_graph));*/
748
749   /* Initialize the node */
750   set_irn_dfn(n, current_dfn);      /* Depth first number for this node */
751   set_irn_uplink(n, current_dfn);   /* ... is default uplink. */
752   set_irn_loop(n, NULL);
753   current_dfn ++;
754
755   /* What's this good for?
756   n->ana.scc.section = NULL;
757   */
758
759   push(n);
760
761   if (!is_outermost_Start(n)) {
762     for (i = get_start_index(n); i < get_irn_arity(n); i++) {
763       ir_node *m;
764       if (is_backedge(n, i)) continue;
765
766       m = get_irn_n(n, i); /*get_irn_ip_pred(n, i);*/
767       if ((!m) || (get_irn_op(m) == op_Unknown)) continue;
768       scc (m);
769       /*return_recur(n, i);*/
770
771       if (irn_is_in_stack(m)) {
772         /* Uplink of m is smaller if n->m is a backedge.
773            Propagate the uplink to mark the loop. */
774         if (get_irn_uplink(m) < get_irn_uplink(n))
775           set_irn_uplink(n, get_irn_uplink(m));
776       }
777     }
778   }
779   if (get_irn_dfn(n) == get_irn_uplink(n)) {
780     /* This condition holds for the node with the incoming backedge. */
781     ir_node *tail = find_tail(n);
782     if (tail) {
783       /* We found a new loop! */
784       ir_loop *l = new_loop();
785
786       /* Remove the loop from the stack ... */
787       pop_scc_unmark_visit (n);
788       /* and recompute it in a better order; and so that it goes into
789          the new loop. */
790       rem = find_irg_on_stack(tail);
791
792       scc (tail);
793       current_ir_graph = rem;
794
795       assert (irn_visited(n));
796       close_loop(l);
797
798       /*      current_loop = l; AS: This is done close_loop */
799     } else {
800       pop_scc_to_loop(n);
801     }
802   }
803 }
804
805 /* Constructs backedge information for irg. In interprocedural view constructs
806    backedges for all methods called by irg, too. */
807 void construct_backedges(ir_graph *irg) {
808   ir_graph *rem = current_ir_graph;
809   ir_loop *head_rem;
810   int i;
811
812   assert(!interprocedural_view &&
813          "not implemented, use construct_ip_backedges");
814
815   current_ir_graph = irg;
816   outermost_ir_graph = irg;
817
818   init_scc(irg);
819
820   current_loop = NULL;
821   new_loop();  /* sets current_loop */
822   head_rem = current_loop; /* Just for assertion */
823
824   if (interprocedural_view) {
825     set_irg_visited(irg, inc_max_irg_visited());
826     init_ip_walk ();
827   } else {
828     inc_irg_visited(irg);
829   }
830
831   scc(get_irg_end(irg));
832   for (i = 0; i < get_End_n_keepalives(get_irg_end(irg)); i++)
833     scc(get_End_keepalive(get_irg_end(irg), i));
834
835   if (interprocedural_view) finish_ip_walk();
836
837   assert(head_rem == current_loop);
838   set_irg_loop(irg, current_loop);
839   assert(get_irg_loop(irg)->kind == k_ir_loop);
840   /*
841   irg->loops = current_loop;
842   if (icfg == 1) {
843     int count = 0;
844     int depth = 0;
845     count_loop (the_loop, &count, &depth);
846     }
847   }
848   */
849   current_ir_graph = rem;
850 }
851
852
853
854 void construct_ip_backedges (void) {
855   ir_graph *rem = current_ir_graph;
856   int rem_ipv = interprocedural_view;
857   int i, j;
858
859   outermost_ir_graph = get_irp_main_irg();
860
861   init_ip_scc();
862
863   current_loop = NULL;
864   new_loop();  /* sets current_loop */
865   interprocedural_view = 1;
866
867   inc_max_irg_visited();
868   for (i = 0; i < get_irp_n_irgs(); i++)
869     set_irg_visited(get_irp_irg(i), get_max_irg_visited());
870
871   for (i = 0; i < get_irp_n_irgs(); i++) {
872     ir_node *sb;
873     current_ir_graph = get_irp_irg(i);
874     /*DDME(get_irg_ent(current_ir_graph));*/
875     /* Find real entry points */
876     sb = get_irg_start_block(current_ir_graph);
877     if ((get_Block_n_cfgpreds(sb) > 1) ||
878         (get_nodes_Block(get_Block_cfgpred(sb, 0)) != sb)) continue;
879     /*    printf("running scc for "); DDME(get_irg_ent(current_ir_graph));   */
880     /* Compute scc for this graph */
881     outermost_ir_graph = current_ir_graph;
882     set_irg_visited(outermost_ir_graph, get_max_irg_visited());
883     scc(get_irg_end(current_ir_graph));
884     for (j = 0; j < get_End_n_keepalives(get_irg_end(outermost_ir_graph)); j++)
885       scc(get_End_keepalive(get_irg_end(outermost_ir_graph), j));
886   }
887
888   set_irg_loop(outermost_ir_graph, current_loop);
889   assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
890
891   current_ir_graph = rem;
892   interprocedural_view = rem_ipv;
893 }
894
895
896 static void reset_backedges(ir_node *n, void *env) {
897   if (is_possible_loop_head(n))
898     clear_backedges(n);
899 }
900
901 /** Removes all loop information.
902     Resets all backedges */
903 void free_loop_information(ir_graph *irg) {
904   set_irg_loop(irg, NULL);
905   /* We cannot free the loop nodes, they are on the obstack. */
906   irg_walk_graph(irg, NULL, reset_backedges, NULL);
907 }
908
909
910 void free_all_loop_information (void) {
911   int i;
912   int rem = interprocedural_view;
913   interprocedural_view = 1;  /* To visit all filter nodes */
914   for (i = 0; i < get_irp_n_irgs(); i++) {
915     free_loop_information(get_irp_irg(i));
916   }
917   pmap_destroy(node_loop_map);
918   node_loop_map = NULL;
919   interprocedural_view = rem;
920 }