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