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