1331d02bb1f847c6f02bf4285bd7d588c1e70e59
[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
24 #include "irprog_t.h"
25 #include "irgraph_t.h"
26 #include "irnode_t.h"
27 #include "irgwalk.h"
28 #include "array.h"
29 #include "pmap.h"
30
31 #include "irdump.h"
32
33 ir_graph *outermost_ir_graph;      /* The outermost graph the scc is computed
34                       for */
35 static ir_loop *current_loop;      /* Current loop construction is working
36                       on. */
37 static int loop_node_cnt = 0;      /* Counts the number of allocated loop nodes.
38                       Each loop node gets a unique number.
39                       What for? ev. remove. @@@ */
40 static int current_dfn = 1;        /* Counter to generate depth first numbering
41                       of visited nodes.  */
42
43 void link_to_reg_end (ir_node *n, void *env);
44 void set_projx_link(ir_node *cb_projx, ir_node *end_projx);
45 ir_node *get_projx_link(ir_node *cb_projx);
46
47 /**********************************************************************/
48 /* Node attributes                                                   **/
49 /**********************************************************************/
50
51 /* A map to get from irnodes to loop nodes. */
52 static pmap *node_loop_map = NULL;
53
54 /**********************************************************************/
55 /* Node attributes needed for the construction.                      **/
56 /**********************************************************************/
57
58 typedef struct scc_info {
59   bool in_stack;         /* Marks whether node is on the stack. */
60   int dfn;               /* Depth first search number. */
61   int uplink;            /* dfn number of ancestor. */
62   /*  ir_loop *loop;         *//* Refers to the containing loop. */
63   /*
64       struct section *section;
65       xset def;
66       xset use;
67   */
68 } scc_info;
69
70 static INLINE scc_info* new_scc_info(void) {
71   scc_info *info = obstack_alloc (outermost_ir_graph->obst, sizeof (scc_info));
72   memset (info, 0, sizeof (scc_info));
73   return info;
74 }
75
76 static INLINE void
77 mark_irn_in_stack (ir_node *n) {
78   assert(get_irn_link(n));
79   /*  to slow */
80   /* ((scc_info *)get_irn_link(n))->in_stack = true; */
81   ((scc_info *)n->link)->in_stack = true;
82 }
83
84 static INLINE void
85 mark_irn_not_in_stack (ir_node *n) {
86   assert(get_irn_link(n));
87   /*  to slow */
88   /* ((scc_info *)get_irn_link(n))->in_stack = false; */
89   ((scc_info *)n->link)->in_stack = false;
90 }
91
92 static INLINE bool
93 irn_is_in_stack (ir_node *n) {
94   assert(get_irn_link(n));
95   /*  to slow */
96   /* return ((scc_info *)get_irn_link(n))->in_stack; */
97   return ((scc_info *)n->link)->in_stack;
98 }
99
100 static INLINE void
101 set_irn_uplink (ir_node *n, int uplink) {
102   assert(get_irn_link(n));
103   /*  to slow */
104   /* ((scc_info *)get_irn_link(n))->uplink = uplink; */
105   ((scc_info *)n->link)->uplink = uplink;
106 }
107
108 INLINE int
109 get_irn_uplink (ir_node *n) {
110   assert(get_irn_link(n));
111   /*  from fast to slow */
112   /* return ((scc_info *)get_irn_link(n))->uplink; */
113   return ((scc_info *)n->link)->uplink;
114 }
115
116 static INLINE void
117 set_irn_dfn (ir_node *n, int dfn) {
118   assert(get_irn_link(n));
119   /*  to slow */
120   /* ((scc_info *)get_irn_link(n))->dfn = dfn; */
121   ((scc_info *)n->link)->dfn = dfn;
122 }
123
124 INLINE int
125 get_irn_dfn (ir_node *n) {
126   assert(get_irn_link(n));
127   /*  to slow */
128   /* return ((scc_info *)get_irn_link(n))->dfn; */
129   return ((scc_info *)n->link)->dfn;
130 }
131
132 #if 0
133 /* Replaced node loop map by real field as hash access dominates runtime
134  * of the algorithm. ! */
135 /* Uses temporary information to set the loop */
136 INLINE void
137 set_irn_loop (ir_node *n, ir_loop* loop) {
138   assert(node_loop_map && "not initialized!");
139   pmap_insert(node_loop_map, (void *)n, (void *)loop);
140 }
141
142 /* Uses temporary information to get the loop */
143 INLINE ir_loop *
144 get_irn_loop (ir_node *n) {
145   ir_loop *res = NULL;
146   if (!node_loop_map) return NULL;
147
148   if (pmap_contains(node_loop_map, (void *)n))
149     res = (ir_loop *) pmap_get(node_loop_map, (void *)n);
150
151   return res;
152 }
153 #else
154 INLINE void
155 set_irn_loop (ir_node *n, ir_loop* loop) {
156   n->loop = loop;
157 }
158
159 /* Uses temporary information to get the loop */
160 INLINE ir_loop *
161 get_irn_loop (ir_node *n) {
162   return n->loop;
163 }
164 #endif
165
166
167 #if 0
168 static ir_loop *find_nodes_loop (ir_node *n, ir_loop *l) {
169   int i;
170   ir_loop *res = NULL;
171
172   /* Test whether n is contained in this loop. */
173   for (i = 0; i < get_loop_n_nodes(l); i++)
174     if (n == get_loop_node(l, i)) return l;
175
176   /* Is this a leave in the loop tree? If so loop not found. */
177   if (get_loop_n_sons(l) == 0) return NULL;
178
179   /* Else descend in the loop tree. */
180   for (i = 0; i < get_loop_n_sons(l); i++) {
181     res = find_nodes_loop(n, get_loop_son(l, i));
182     if (res) break;
183   }
184   return res;
185 }
186
187 /* @@@ temporary implementation, costly!!! */
188 ir_loop * get_irn_loop(ir_node *n) {
189   ir_loop *l = get_irg_loop(current_ir_graph);
190   l = find_nodes_loop(n, l);
191   return l;
192 }
193 #endif
194
195 /**********************************************************************/
196 /* A stack.                                                          **/
197 /**********************************************************************/
198
199 static ir_node **stack = NULL;
200 static int tos = 0;                /* top of stack */
201
202 static INLINE void init_stack(void) {
203   if (stack) {
204     ARR_RESIZE (ir_node *, stack, 1000);
205   } else {
206     stack = NEW_ARR_F (ir_node *, 1000);
207   }
208   tos = 0;
209 }
210
211 #if 0
212 static INLINE void free_stack(void) {
213   DEL_ARR_F(stack);
214   stack = NULL;
215   tos = 0;
216 }
217 #endif
218
219 static INLINE void
220 push (ir_node *n)
221 {
222   /*DDMN(n);*/
223
224   if (tos == ARR_LEN (stack)) {
225     int nlen = ARR_LEN (stack) * 2;
226     ARR_RESIZE (ir_node *, stack, nlen);
227   }
228   stack [tos++] = n;
229   mark_irn_in_stack(n);
230 }
231
232 static INLINE ir_node *
233 pop (void)
234 {
235   ir_node *n = stack[--tos];
236   mark_irn_not_in_stack(n);
237   return n;
238 }
239
240 /* The nodes up to n belong to the current loop.
241    Removes them from the stack and adds them to the current loop. */
242 static INLINE void
243 pop_scc_to_loop (ir_node *n)
244 {
245   ir_node *m;
246   int i = 0;
247
248   do {
249     m = pop();
250
251     //printf(" dfn: %d, upl %d upl-new %d ", get_irn_dfn(m), get_irn_uplink(m), loop_node_cnt+1); DDMN(m);
252
253     loop_node_cnt++;
254     set_irn_dfn(m, loop_node_cnt);
255     add_loop_node(current_loop, m);
256     set_irn_loop(m, current_loop);
257     i++;
258     /*    if (m==n) break;*/
259   } while(m != n);
260
261   /* i might be bigger than 1 for dead (and that's why bad) loops */
262   /* if(i > 1)
263     printf("Mehr als eine Iteration!!!!!!!!!!!!!!!!!!!!!!!!!!!!11111\n");
264    */
265 }
266
267 /* GL ??? my last son is my grandson???  Removes loops with no
268    ir_nodes in them.  Such loops have only another loop as son. (Why
269    can't they have two loops as sons? Does it never get that far? ) */
270 static void close_loop (ir_loop *l)
271 {
272   int last = get_loop_n_elements(l) - 1;
273   loop_element lelement = get_loop_element(l, last);
274   ir_loop *last_son = lelement.son;
275
276   if (get_kind(last_son) == k_ir_loop &&
277       get_loop_n_elements(last_son) == 1)
278     {
279       ir_loop *gson;
280
281       lelement = get_loop_element(last_son, 0);
282       gson = lelement.son;
283       if(get_kind(gson) == k_ir_loop)
284     {
285           loop_element new_last_son;
286
287       gson -> outer_loop = l;
288           new_last_son.son = gson;
289       l -> children[last] = new_last_son;
290     }
291     }
292
293   current_loop = l;
294 }
295
296 /* Removes and unmarks all nodes up to n from the stack.
297    The nodes must be visited once more to assign them to a scc. */
298 static INLINE void
299 pop_scc_unmark_visit (ir_node *n)
300 {
301   ir_node *m = NULL;
302
303   while (m != n) {
304     m = pop();
305     set_irn_visited(m, 0);
306   }
307 }
308
309 /**********************************************************************/
310 /* The loop datastructure.                                           **/
311 /**********************************************************************/
312
313 /* Allocates a new loop as son of current_loop.  Sets current_loop
314    to the new loop and returns the father. */
315 ir_loop *new_loop (void) {
316   ir_loop *father, *son;
317
318   father = current_loop;
319
320   son = (ir_loop *) obstack_alloc (outermost_ir_graph->obst, sizeof (ir_loop));
321   memset (son, 0, sizeof (ir_loop));
322   son->kind = k_ir_loop;
323   son->children = NEW_ARR_F (loop_element, 0);
324   son->n_nodes = 0;
325   son->n_sons=0;
326   if (father) {
327     son->outer_loop = father;
328     add_loop_son(father, son);
329     son->depth = father->depth+1;
330   } else {  /* The root loop */
331     son->outer_loop = son;
332     son->depth = 0;
333   }
334
335 #ifdef DEBUG_libfirm
336   son->loop_nr = get_irp_new_node_nr();
337   son->link = NULL;
338 #endif
339
340   current_loop = son;
341   return father;
342 }
343
344 #if 0
345 /* Finishes the datastructures, copies the arrays to the obstack
346    of current_ir_graph.
347    A. Schoesser: Caution: loop -> sons is gone. */
348 static void mature_loop (ir_loop *loop) {
349   ir_loop **new_sons;
350
351   new_sons = NEW_ARR_D (ir_loop *, current_ir_graph->obst, ARR_LEN(loop->sons));
352   memcpy (new_sons, loop->sons, sizeof (ir_loop *) * ARR_LEN(loop->sons));
353   DEL_ARR_F(loop->sons);
354   loop->sons = new_sons;
355 }
356 #endif
357
358 /* Returns outer loop, itself if outermost. */
359 ir_loop *get_loop_outer_loop (ir_loop *loop) {
360   assert(loop && loop->kind == k_ir_loop);
361   return loop->outer_loop;
362 }
363
364 /* Returns nesting depth of this loop */
365 int get_loop_depth (ir_loop *loop) {
366   assert(loop); assert(loop->kind == k_ir_loop);
367   return loop->depth;
368 }
369
370 /* Returns the number of inner loops */
371 int      get_loop_n_sons (ir_loop *loop) {
372   assert(loop && loop->kind == k_ir_loop);
373   return(loop -> n_sons);
374 }
375
376 /* Returns the pos`th loop_node-child              *
377  * TODO: This method isn`t very efficient !        *
378  * Returns NULL if there isnt`t a pos`th loop_node */
379 ir_loop *get_loop_son (ir_loop *loop, int pos) {
380   int child_nr = 0, loop_nr = -1;
381
382   assert(loop && loop->kind == k_ir_loop);
383   while(child_nr < ARR_LEN(loop->children))
384    {
385     if(*(loop -> children[child_nr].kind) == k_ir_loop)
386       loop_nr++;
387     if(loop_nr == pos)
388       return(loop -> children[child_nr].son);
389     child_nr++;
390    }
391   return NULL;
392 }
393
394 /* Use EXCLUSIVELY this function to add sons, otherwise the loop->n_sons
395    is invalid! */
396
397 INLINE void
398 add_loop_son(ir_loop *loop, ir_loop *son) {
399   loop_element lson;
400   lson.son = son;
401   assert(loop && loop->kind == k_ir_loop);
402   assert(get_kind(son) == k_ir_loop);
403   ARR_APP1 (loop_element, loop->children, lson);
404   loop -> n_sons++;
405 }
406
407 /* Returns the number of nodes in the loop */
408 int      get_loop_n_nodes (ir_loop *loop) {
409   assert(loop); assert(loop->kind == k_ir_loop);
410   return loop -> n_nodes;
411 /*  return ARR_LEN(loop->nodes); */
412 }
413
414 /* Returns the pos`th ir_node-child                *
415  * TODO: This method isn`t very efficient !        *
416  * Returns NULL if there isnt`t a pos`th ir_node   */
417 ir_node *get_loop_node (ir_loop *loop, int pos) {
418   int child_nr, node_nr = -1;
419
420   assert(loop && loop->kind == k_ir_loop);
421   assert(pos < get_loop_n_nodes(loop));
422
423   for (child_nr = 0; child_nr < ARR_LEN(loop->children); child_nr++) {
424     if(*(loop -> children[child_nr].kind) == k_ir_node)
425       node_nr++;
426     if(node_nr == pos)
427       return(loop -> children[child_nr].node);
428   }
429   DDML(loop);
430   printf("pos: %d\n", pos);
431   assert(0 && "no child at pos found");
432   return NULL;
433 }
434
435 /* Use EXCLUSIVELY this function to add nodes, otherwise the loop->n_nodes
436    is invalid! */
437
438 INLINE void
439 add_loop_node(ir_loop *loop, ir_node *n) {
440   loop_element ln;
441   ln.node = n;
442   assert(loop && loop->kind == k_ir_loop);
443   assert(get_kind(n) == k_ir_node);
444   ARR_APP1 (loop_element, loop->children, ln);
445   loop->n_nodes++;
446 }
447
448 /** Returns the number of elements contained in loop.  */
449 int get_loop_n_elements (ir_loop *loop) {
450   assert(loop && loop->kind == k_ir_loop);
451   return(ARR_LEN(loop->children));
452 }
453
454 /*
455  Returns the pos`th loop element.
456  This may be a loop_node or a ir_node. The caller of this function has
457  to check the *(loop_element.kind) field for "k_ir_node" or "k_ir_loop"
458  and then select the apropriate "loop_element.node" or "loop_element.son".
459 */
460
461 loop_element get_loop_element (ir_loop *loop, int pos) {
462   assert(loop && loop->kind == k_ir_loop && pos < ARR_LEN(loop->children));
463
464   return(loop -> children[pos]);
465 }
466
467 int get_loop_element_pos(ir_loop *loop, void *le) {
468   int i;
469   assert(loop && loop->kind == k_ir_loop);
470
471   for (i = 0; i < get_loop_n_elements(loop); i++)
472     if (get_loop_element(loop, i).node == le) return i;
473   return -1;
474 }
475
476 int get_loop_loop_nr(ir_loop *loop) {
477   assert(loop && loop->kind == k_ir_loop);
478 #ifdef DEBUG_libfirm
479   return loop->loop_nr;
480 #else
481   return (int)loop;
482 #endif
483 }
484
485
486 /** A field to connect additional information to a loop.  Only valid
487     if libfirm_debug is set. */
488 void  set_loop_link (ir_loop *loop, void *link) {
489   assert(loop && loop->kind == k_ir_loop);
490 #ifdef DEBUG_libfirm
491   loop->link = link;
492 #endif
493 }
494 void *get_loop_link (const ir_loop *loop) {
495   assert(loop && loop->kind == k_ir_loop);
496 #ifdef DEBUG_libfirm
497   return loop->link;
498 #else
499   return NULL;
500 #endif
501 }
502
503 /* The outermost loop is remarked in the surrounding graph. */
504 void     set_irg_loop(ir_graph *irg, ir_loop *loop) {
505   assert(irg);
506   irg->loop = loop;
507 }
508 ir_loop *get_irg_loop(ir_graph *irg) {
509   assert(irg);
510   return irg->loop;
511 }
512
513
514 /**********************************************************************/
515 /* Constructing and destructing the loop/backedge information.       **/
516 /**********************************************************************/
517
518 /* Initialization steps. **********************************************/
519
520 static INLINE void
521 init_node (ir_node *n, void *env) {
522   set_irn_link (n, new_scc_info());
523   clear_backedges(n);
524 #if 0
525   /* Also init nodes not visible in intraproc_view. */
526     /* @@@ init_node is called for too many nodes -- this wastes memory!.
527        The mem is not lost as its on the obstack. */
528   if (get_irn_op(n) == op_Filter) {
529     for (i = 0; i < get_Filter_n_cg_preds(n); i++)
530       init_node(get_Filter_cg_pred(n, i), NULL);
531   }
532   if (get_irn_op(n) == op_Block) {
533     for (i = 0; i < get_Block_cg_n_cfgpreds(n); i++) {
534       init_node(get_Block_cg_cfgpred(n, i), NULL);
535     }
536   }
537   /* The following pattern matches only after a call from above pattern. */
538   if ((get_irn_op(n) == op_Proj) /*&& (get_Proj_proj(n) == 0)*/) {
539     /* @@@ init_node is called for every proj -- this wastes memory!.
540        The mem is not lost as its on the obstack. */
541     ir_node *cb = get_Proj_pred(n);
542     if ((get_irn_op(cb) == op_CallBegin) ||
543     (get_irn_op(cb) == op_EndReg) ||
544     (get_irn_op(cb) == op_EndExcept)) {
545       init_node(cb, NULL);
546       init_node(get_nodes_Block(cb), NULL);
547     }
548   }
549 #endif
550 }
551
552 static INLINE void
553 init_scc_common (void) {
554   current_dfn = 1;
555   loop_node_cnt = 0;
556   if (!node_loop_map) node_loop_map = pmap_create();
557   init_stack();
558 }
559
560 static INLINE void
561 init_scc (ir_graph *irg) {
562   init_scc_common();
563   irg_walk_graph (irg, init_node, NULL, NULL);
564   /*
565   irg_walk (irg, link_to_reg_end, NULL, NULL);
566   */
567 }
568
569 static INLINE void
570 init_ip_scc (void) {
571   init_scc_common();
572   cg_walk (init_node, NULL, NULL);
573
574 #if EXPERIMENTAL_LOOP_TREE
575   cg_walk (link_to_reg_end, NULL, NULL);
576 #endif
577 }
578
579 /* Condition for breaking the recursion. */
580 static bool is_outermost_Start(ir_node *n) {
581   /* Test whether this is the outermost Start node.  If so
582      recursion must end. */
583   if ((get_irn_op(n) == op_Block)     &&
584       (get_Block_n_cfgpreds(n) == 1)  &&
585       (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Start) &&
586       (get_nodes_Block(skip_Proj(get_Block_cfgpred(n, 0))) == n)) {
587     return true;
588   }
589 #if 0
590   /*  @@@ Bad condition:
591       not possible in interprocedural view as outermost_graph is
592       not necessarily the only with a dead-end start block.
593       Besides current_ir_graph is not set properly. */
594   if ((get_irn_op(n) == op_Block) &&
595       (n == get_irg_start_block(current_ir_graph))) {
596     if ((!interprocedural_view)  ||
597     (current_ir_graph == outermost_ir_graph))
598       return true;
599   }
600 #endif
601   return false;
602 }
603
604 /* When to walk from nodes to blocks. Only for Control flow operations? */
605 static INLINE int
606 get_start_index(ir_node *n) {
607 #undef BLOCK_BEFORE_NODE
608 #define BLOCK_BEFORE_NODE 1
609
610 #if BLOCK_BEFORE_NODE
611
612   /* This version assures, that all nodes are ordered absolutely.  This allows
613      to undef all nodes in the heap analysis if the block is false, which means
614      not reachable.
615      I.e., with this code, the order on the loop tree is correct. But a (single)
616      test showed the loop tree is deeper.   */
617   if (get_irn_op(n) == op_Phi   ||
618       get_irn_op(n) == op_Block ||
619       (get_irn_op(n) == op_Filter && interprocedural_view) ||
620       (get_irg_pinned(get_irn_irg(n)) == floats &&
621        get_op_pinned(get_irn_op(n)) == floats))
622     // Here we could test for backedge at -1 which is illegal
623     return 0;
624   else
625     return -1;
626
627 #else
628
629   /* This version causes deeper loop trees (at least we verified this
630      for Polymor).
631      But it guarantees that Blocks are analysed before nodes contained in the
632      block.  If so, we can set the value to undef if the block is not \
633      executed. */
634    if (is_cfop(n) || is_fragile_op(n) || get_irn_op(n) == op_Start)
635      return -1;
636    else
637      return 0;
638
639 #endif
640 }
641
642
643 #if 0
644 static void test(ir_node *pred, ir_node *root, ir_node *this) {
645   int i;
646   if (get_irn_uplink(pred) >= get_irn_uplink(root)) return;
647
648   printf("this: %d ", get_irn_uplink(this)); DDMN(this);
649   printf("pred: %d ", get_irn_uplink(pred)); DDMN(pred);
650   printf("root: %d ", get_irn_uplink(root)); DDMN(root);
651
652   printf("tos: %d\n", tos);
653
654   for (i = tos; i >= 0; i--) {
655     ir_node *n = stack[i];
656     if (!n) continue;
657     printf(" uplink: %d, pos: %d ", get_irn_uplink(n), i); DDMN(n);
658   }
659 }
660 #endif
661
662 /* Test for legal loop header: Block, Phi, ... */
663 INLINE static bool is_possible_loop_head(ir_node *n) {
664   ir_op *op = get_irn_op(n);
665   return ((op == op_Block) ||
666           (op == op_Phi) ||
667           ((op == op_Filter) && interprocedural_view));
668 }
669
670 /* Returns true if n is a loop header, i.e., it is a Block, Phi
671    or Filter node and has predecessors within the loop and out
672    of the loop.
673    @arg root: only needed for assertion. */
674 static bool
675 is_head (ir_node *n, ir_node *root)
676 {
677   int i, arity;
678   int some_outof_loop = 0, some_in_loop = 0;
679
680   /* Test for legal loop header: Block, Phi, ... */
681   if (!is_possible_loop_head(n))
682     return false;
683
684   if (!is_outermost_Start(n)) {
685     arity = get_irn_arity(n);
686     for (i = get_start_index(n); i < arity; i++) {
687       ir_node *pred = get_irn_n(n, i);
688       assert(pred);
689       if (is_backedge(n, i)) continue;
690       if (!irn_is_in_stack(pred)) {
691         some_outof_loop = 1;
692       } else {
693         if(get_irn_uplink(pred) < get_irn_uplink(root)) {
694           DDMN(n); DDMN(pred); DDMN(root);
695           assert(get_irn_uplink(pred) >= get_irn_uplink(root));
696         }
697         some_in_loop = 1;
698       }
699     }
700   }
701   return some_outof_loop && some_in_loop;
702 }
703
704 /* Returns true if n is possible loop head of an endless loop.
705    I.e., it is a Block, Phi or Filter node and has only predecessors
706    within the loop.
707    @arg root: only needed for assertion. */
708 static bool
709 is_endless_head (ir_node *n, ir_node *root)
710 {
711   int i, arity;
712   int some_outof_loop = 0, some_in_loop = 0;
713
714   /* Test for legal loop header: Block, Phi, ... */
715   if (!is_possible_loop_head(n))
716     return false;
717
718   if (!is_outermost_Start(n)) {
719     arity = get_irn_arity(n);
720     for (i = get_start_index(n); i < arity; i++) {
721       ir_node *pred = get_irn_n(n, i);
722       assert(pred);
723       if (is_backedge(n, i)) { continue; }
724       if (!irn_is_in_stack(pred)) {
725         some_outof_loop = 1; //printf(" some out of loop ");
726       } else {
727         if(get_irn_uplink(pred) < get_irn_uplink(root)) {
728           DDMN(pred); DDMN(root);
729           assert(get_irn_uplink(pred) >= get_irn_uplink(root));
730         }
731         some_in_loop = 1;
732       }
733     }
734   }
735   return !some_outof_loop && some_in_loop;
736 }
737
738 /* Returns index of the predecessor with the smallest dfn number
739    greater-equal than limit. */
740 static int
741 smallest_dfn_pred (ir_node *n, int limit)
742 {
743   int i, index = -2, min = -1;
744
745   if (!is_outermost_Start(n)) {
746     int arity = get_irn_arity(n);
747     for (i = get_start_index(n); i < arity; i++) {
748       ir_node *pred = get_irn_n(n, i);
749       assert(pred);
750       if (is_backedge(n, i) || !irn_is_in_stack(pred)) continue;
751       if (get_irn_dfn(pred) >= limit && (min == -1 || get_irn_dfn(pred) < min)) {
752         index = i;
753         min = get_irn_dfn(pred);
754       }
755     }
756   }
757   return index;
758 }
759
760 /* Returns index of the predecessor with the largest dfn number. */
761 static int
762 largest_dfn_pred (ir_node *n)
763 {
764   int i, index = -2, max = -1;
765
766   if (!is_outermost_Start(n)) {
767     int arity = get_irn_arity(n);
768     for (i = get_start_index(n); i < arity; i++) {
769       ir_node *pred = get_irn_n(n, i);
770       if (is_backedge (n, i) || !irn_is_in_stack(pred)) continue;
771       if (get_irn_dfn(pred) > max) {
772         index = i;
773         max = get_irn_dfn(pred);
774       }
775     }
776   }
777   return index;
778 }
779
780 /** Searches the stack for possible loop heads.  Tests these for backedges.
781     If it finds a head with an unmarked backedge it marks this edge and
782     returns the tail of the loop.
783     If it finds no backedge returns NULL.
784     ("disable_backedge" in fiasco)
785 *
786 *  @param n  A node where uplink == dfn.
787 **/
788
789 static ir_node *
790 find_tail (ir_node *n) {
791   ir_node *m;
792   int i, res_index = -2;
793
794   /*
795     if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
796   */
797   m = stack[tos-1];  /* tos = top of stack */
798   if (is_head (m, n)) {
799     res_index = smallest_dfn_pred(m, 0);
800     if ((res_index == -2) &&  /* no smallest dfn pred found. */
801     (n ==  m))
802       return NULL;
803   } else {
804     if (m == n) return NULL;    // Is this to catch Phi - self loops?
805     for (i = tos-2; i >= 0; --i) {
806       m = stack[i];
807
808       if (is_head (m, n)) {
809         res_index = smallest_dfn_pred (m, get_irn_dfn(m) + 1);
810         if (res_index == -2)  /* no smallest dfn pred found. */
811           res_index = largest_dfn_pred (m);
812
813         if ((m == n) && (res_index == -2)) {
814           i = -1;
815         }
816         break;
817       }
818
819       /* We should not walk past our selves on the stack:  The upcoming nodes
820          are not in this loop. We assume a loop not reachable from Start. */
821       if (m == n) {
822         i = -1;
823         break;
824       }
825
826     }
827
828     if (i < 0) {
829       /* A dead loop not reachable from Start. */
830       for (i = tos-2; i >= 0; --i) {
831         m = stack[i];
832         if (is_endless_head (m, n)) {
833           res_index = smallest_dfn_pred (m, get_irn_dfn(m) + 1);
834           if (res_index == -2)  /* no smallest dfn pred found. */
835             res_index = largest_dfn_pred (m);
836           break;
837         }
838         if (m == n) { break; }  /* It's not an unreachable loop, either. */
839       }
840       //assert(0 && "no head found on stack");
841     }
842
843   }
844   if (res_index <= -2) {
845     /* It's a completely bad loop: without Phi/Block nodes that can
846        be a head. I.e., the code is "dying".  We break the loop by
847        setting Bad nodes. */
848     int arity = get_irn_arity(n);
849     for (i = -1; i < arity; ++i) {
850       set_irn_n(n, i, get_irg_bad(get_irn_irg(n)));
851     }
852     return NULL;
853   }
854   assert (res_index > -2);
855
856   set_backedge (m, res_index);
857   return is_outermost_Start(n) ? NULL : get_irn_n(m, res_index);
858 }
859
860
861 #if EXPERIMENTAL_LOOP_TREE
862
863 /*  ----------------------------------------------------------------
864     AS:  This is experimantal code to build loop trees suitable for
865     the heap analysis. Does not work correctly right now... :-(
866
867
868     Search in stack for the corresponding first Call-End-ProjX that
869     corresponds to one of the control flow predecessors of the given
870     block, that is the possible callers.
871     returns: the control predecessor to chose\
872     or       -1 if no corresponding Call-End-Node could be found
873              on the stack.
874     - -------------------------------------------------------------- */
875
876 int search_endproj_in_stack(ir_node *start_block)
877 {
878   int i, j;
879   assert(is_Block(start_block));
880   for(i = tos - 1; i >= 0; --i)
881     {
882       DDMN(stack[i]);
883       if(get_irn_op(stack[i]) == op_Proj && get_irn_mode(stack[i]) == mode_X &&
884          get_irn_op(get_irn_n(stack[i], 0)) == op_EndReg)
885         {
886           printf("FOUND PROJ!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
887           ir_node *end_projx = stack[i];
888
889           int arity = get_irn_arity(start_block);
890           for(j = 0; j < arity; j++)
891             {
892               ir_node *begin_projx = get_Block_cfgpred(get_irg_start_block(get_irn_irg(end_projx)), get_Proj_proj(end_projx));
893               DDMN(begin_projx);
894               if(get_irn_n(start_block, j) == begin_projx)
895                 {
896                   printf("FOUND IT!!!!!!!!!!!!!!!!!!\n");
897                   return(j);
898                 }
899             }
900         }
901     }
902   return(-1);
903 }
904
905
906 static pmap *projx_link = NULL;
907
908 void link_to_reg_end (ir_node *n, void *env) {
909   if(get_irn_op(n) == op_Proj && get_irn_mode(n) == mode_X && get_irn_op(get_irn_n(n, 0)) == op_EndReg)
910     {
911       /* Reg End Projx -> Find the CallBegin Projx and hash it */
912       ir_node *end_projx = n;
913       ir_node *begin_projx = get_Block_cfgpred(get_irg_start_block(get_irn_irg(end_projx)), get_Proj_proj(end_projx));
914       printf("Linked the following ProjxNodes:\n");
915       DDMN(begin_projx);
916       DDMN(end_projx);
917       set_projx_link(begin_projx, end_projx);
918     }
919 }
920
921 void set_projx_link(ir_node *cb_projx, ir_node *end_projx)
922 {
923   if(projx_link == NULL)
924     projx_link = pmap_create();
925   pmap_insert(projx_link, (void *)cb_projx, (void *)end_projx);
926 }
927
928 ir_node *get_projx_link(ir_node *cb_projx)
929 {
930   return((ir_node *) pmap_get(projx_link, (void *)cb_projx));
931 }
932
933 #endif
934
935
936
937 /*-----------------------------------------------------------*
938  *                   The core algorithm.                     *
939  *-----------------------------------------------------------*/
940
941
942 static void scc (ir_node *n) {
943   int i;
944   if (irn_visited(n)) return;
945   mark_irn_visited(n);
946
947   /* Initialize the node */
948   set_irn_dfn(n, current_dfn);      /* Depth first number for this node */
949   set_irn_uplink(n, current_dfn);   /* ... is default uplink. */
950   set_irn_loop(n, NULL);
951   current_dfn ++;
952   push(n);
953
954   /* AS: get_start_index might return -1 for Control Flow Nodes, and thus a negative
955      array index would be passed to is_backedge(). But CFG Nodes dont't have a backedge array,
956      so is_backedge does not access array[-1] but correctly returns false! */
957
958   if (!is_outermost_Start(n)) {
959     int arity = get_irn_arity(n);
960
961 #if EXPERIMENTAL_LOOP_TREE
962
963     /* This is meant to be used with the experimenatl code above.
964        If the above code is not used any more, this can be deleted, too.... */
965
966     if(interprocedural_view &&
967        is_Block(n) &&
968        get_irn_op(get_irn_n(n, 0)) == op_Proj &&
969        get_irn_op(get_irn_n(get_irn_n(n, 0), 0)) == op_CallBegin)
970       {
971         /* We are at the start node of a function:
972            Walk to the callers in the correct order! */
973         DDMN(n);
974         DDMN(get_irn_n(get_irn_n(n, 0), 0));
975         for(i = 0; i < arity; i++)
976           {
977             int pred_nr;
978             ir_node *m;
979
980             pred_nr = search_endproj_in_stack(n);
981             assert(pred_nr >= 0);
982             if(is_backedge(n, pred_nr))
983               continue;
984             m = get_irn_n(n, pred_nr);
985             scc(m);
986
987             if (irn_is_in_stack(m)) {
988               /* Uplink of m is smaller if n->m is a backedge.
989                  Propagate the uplink to mark the loop. */
990               if (get_irn_uplink(m) < get_irn_uplink(n))
991                 set_irn_uplink(n, get_irn_uplink(m));
992             }
993           }
994       }
995     else
996
997 #endif
998
999       {
1000         for (i = get_start_index(n); i < arity; i++) {
1001           ir_node *m;
1002           if (is_backedge(n, i)) continue;
1003           m = get_irn_n(n, i); /* get_irn_ip_pred(n, i); */
1004           /* if ((!m) || (get_irn_op(m) == op_Unknown)) continue; */
1005           scc (m);
1006           if (irn_is_in_stack(m)) {
1007             /* Uplink of m is smaller if n->m is a backedge.
1008                Propagate the uplink to mark the loop. */
1009             if (get_irn_uplink(m) < get_irn_uplink(n))
1010               set_irn_uplink(n, get_irn_uplink(m));
1011           }
1012         }
1013       }
1014   }
1015
1016   if (get_irn_dfn(n) == get_irn_uplink(n)) {
1017     /* This condition holds for
1018        1) the node with the incoming backedge.
1019           That is: We found a loop!
1020        2) Straight line code, because no uplink has been propagated, so the
1021           uplink still is the same as the dfn.
1022
1023        But n might not be a proper loop head for the analysis. Proper loop
1024        heads are Block and Phi nodes. find_tail searches the stack for
1025        Block's and Phi's and takes those nodes as loop heads for the current
1026        loop instead and marks the incoming edge as backedge. */
1027
1028     ir_node *tail = find_tail(n);
1029     if (tail) {
1030       /* We have a loop, that is no straight line code,
1031          because we found a loop head!
1032          Next actions: Open a new loop on the loop tree and
1033                        try to find inner loops */
1034
1035
1036 #define NO_LOOPS_WITHOUT_HEAD 1
1037 #if NO_LOOPS_WITHOUT_HEAD
1038
1039       /* This is an adaption of the algorithm from fiasco / optscc to
1040        * avoid loops without Block or Phi as first node.  This should
1041        * severely reduce the number of evaluations of nodes to detect
1042        * a fixpoint in the heap analysis.
1043        * Further it avoids loops without firm nodes that cause errors
1044        * in the heap analyses. */
1045
1046       ir_loop *l;
1047       int close;
1048       if (get_loop_n_elements(current_loop) > 0) {
1049         l = new_loop();
1050         close = 1;
1051       } else {
1052         l = current_loop;
1053         close = 0;
1054       }
1055
1056 #else
1057
1058       ir_loop *l = new_loop();
1059
1060 #endif
1061
1062       /* Remove the loop from the stack ... */
1063       pop_scc_unmark_visit (n);
1064
1065       /*  GL @@@ remove experimental stuff rem = find_irg_on_stack(tail); */
1066
1067       /* The current backedge has been marked, that is temporarily eliminated,
1068          by find tail. Start the scc algorithm
1069          anew on the subgraph thats left (the current loop without the backedge)
1070          in order to find more inner loops. */
1071
1072       scc (tail);
1073
1074       /*  GL @@@ remove experimental stuff current_ir_graph = rem; */
1075
1076       assert (irn_visited(n));
1077 #if NO_LOOPS_WITHOUT_HEAD
1078       if (close)
1079 #endif
1080         close_loop(l);
1081     }
1082     else
1083       {
1084         /* AS: No loop head was found, that is we have straightline code.
1085                Pop all nodes from the stack to the current loop. */
1086       pop_scc_to_loop(n);
1087     }
1088   }
1089 }
1090
1091 /* Constructs backedge information for irg. In interprocedural view constructs
1092    backedges for all methods called by irg, too. */
1093 void construct_backedges(ir_graph *irg) {
1094   ir_graph *rem = current_ir_graph;
1095   ir_loop *head_rem;
1096
1097   assert(!interprocedural_view &&
1098      "not implemented, use construct_ip_backedges");
1099
1100   current_ir_graph = irg;
1101   outermost_ir_graph = irg;
1102
1103   init_scc(current_ir_graph);
1104
1105   current_loop = NULL;
1106   new_loop();  /* sets current_loop */
1107   head_rem = current_loop; /* Just for assertion */
1108
1109   if (interprocedural_view) {
1110     set_irg_visited(current_ir_graph, inc_max_irg_visited());
1111     init_ip_walk ();
1112   } else {
1113     inc_irg_visited(current_ir_graph);
1114   }
1115
1116   scc(get_irg_end(current_ir_graph));
1117
1118   if (interprocedural_view) finish_ip_walk();
1119
1120   assert(head_rem == current_loop);
1121   set_irg_loop(current_ir_graph, current_loop);
1122   set_irg_loopinfo_state(current_ir_graph, loopinfo_consistent);
1123   assert(get_irg_loop(current_ir_graph)->kind == k_ir_loop);
1124   /*
1125   irg->loops = current_loop;
1126   if (icfg == 1) {
1127     int count = 0;
1128     int depth = 0;
1129     count_loop (the_loop, &count, &depth);
1130     }
1131   }
1132   */
1133   current_ir_graph = rem;
1134 }
1135
1136
1137 #if 0
1138 void construct_ip_backedges (void) {
1139   ir_graph *rem = current_ir_graph;
1140   int rem_ipv = interprocedural_view;
1141   int i, j;
1142
1143   outermost_ir_graph = get_irp_main_irg();
1144
1145   init_ip_scc();
1146
1147   current_loop = NULL;
1148   new_loop();  /* sets current_loop */
1149   interprocedural_view = 1;
1150
1151   inc_max_irg_visited();
1152   for (i = 0; i < get_irp_n_irgs(); i++)
1153     set_irg_visited(get_irp_irg(i), get_max_irg_visited());
1154
1155   for (i = 0; i < get_irp_n_irgs(); i++) {
1156     ir_node *sb;
1157     current_ir_graph = get_irp_irg(i);
1158     /* Find real entry points */
1159     sb = get_irg_start_block(current_ir_graph);
1160     if ((get_Block_n_cfgpreds(sb) > 1) ||
1161     (get_nodes_Block(get_Block_cfgpred(sb, 0)) != sb)) continue;
1162     /* Compute scc for this graph */
1163     outermost_ir_graph = current_ir_graph;
1164     set_irg_visited(outermost_ir_graph, get_max_irg_visited());
1165     scc(get_irg_end(current_ir_graph));
1166     for (j = 0; j < get_End_n_keepalives(get_irg_end(outermost_ir_graph)); j++)
1167       scc(get_End_keepalive(get_irg_end(outermost_ir_graph), j));
1168   }
1169
1170   set_irg_loop(outermost_ir_graph, current_loop);
1171   set_irg_loopinfo_state(current_ir_graph, loopinfo_ip_consistent);
1172   assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
1173
1174   current_ir_graph = rem;
1175   interprocedural_view = rem_ipv;
1176 }
1177 #else
1178 void construct_ip_backedges (void) {
1179   ir_graph *rem = current_ir_graph;
1180   int rem_ipv = interprocedural_view;
1181   int i;
1182
1183   assert(get_irp_ip_view_state() == ip_view_valid);
1184
1185   outermost_ir_graph = get_irp_main_irg();
1186
1187   init_ip_scc();
1188
1189   current_loop = NULL;
1190   new_loop();  /* sets current_loop */
1191   interprocedural_view = 1;
1192
1193   inc_max_irg_visited();
1194   for (i = 0; i < get_irp_n_irgs(); i++)
1195     set_irg_visited(get_irp_irg(i), get_max_irg_visited());
1196
1197   /** We have to start the walk at the same nodes as cg_walk. **/
1198   /* Walk starting at unreachable procedures. Only these
1199    * have End blocks visible in interprocedural view. */
1200   for (i = 0; i < get_irp_n_irgs(); i++) {
1201     ir_node *sb;
1202     current_ir_graph = get_irp_irg(i);
1203
1204     sb = get_irg_start_block(current_ir_graph);
1205
1206     if ((get_Block_n_cfgpreds(sb) > 1) ||
1207     (get_nodes_block(get_Block_cfgpred(sb, 0)) != sb)) continue;
1208
1209     scc(get_irg_end(current_ir_graph));
1210   }
1211
1212   /* Check whether we walked all procedures: there could be procedures
1213      with cyclic calls but no call from the outside. */
1214   for (i = 0; i < get_irp_n_irgs(); i++) {
1215     ir_node *sb;
1216     current_ir_graph = get_irp_irg(i);
1217
1218     /* Test start block: if inner procedure end and end block are not
1219      * visible and therefore not marked. */
1220     sb = get_irg_start_block(current_ir_graph);
1221     if (get_irn_visited(sb) < get_irg_visited(current_ir_graph)) scc(sb);
1222   }
1223
1224   /* Walk all endless loops in inner procedures.
1225    * We recognize an inner procedure if the End node is not visited. */
1226   for (i = 0; i < get_irp_n_irgs(); i++) {
1227     ir_node *e;
1228     current_ir_graph = get_irp_irg(i);
1229
1230     e = get_irg_end(current_ir_graph);
1231     if (get_irn_visited(e) < get_irg_visited(current_ir_graph)) {
1232       int j;
1233       /* Don't visit the End node. */
1234       for (j = 0; j < get_End_n_keepalives(e); j++) scc(get_End_keepalive(e, j));
1235     }
1236   }
1237
1238   set_irg_loop(outermost_ir_graph, current_loop);
1239   set_irg_loopinfo_state(current_ir_graph, loopinfo_ip_consistent);
1240   assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
1241
1242   current_ir_graph = rem;
1243   interprocedural_view = rem_ipv;
1244 }
1245 #endif
1246
1247 static void reset_backedges(ir_node *n) {
1248   if (is_possible_loop_head(n)) {
1249     int rem = interprocedural_view;
1250     interprocedural_view = 1;
1251     clear_backedges(n);
1252     interprocedural_view = 0;
1253     clear_backedges(n);
1254     interprocedural_view = rem;
1255   }
1256 }
1257
1258
1259 /*
1260 static void loop_reset_backedges(ir_loop *l) {
1261   int i;
1262   reset_backedges(get_loop_node(l, 0));
1263   for (i = 0; i < get_loop_n_nodes(l); ++i)
1264     set_irn_loop(get_loop_node(l, i), NULL);
1265   for (i = 0; i < get_loop_n_sons(l); ++i) {
1266     loop_reset_backedges(get_loop_son(l, i));
1267   }
1268 }
1269 */
1270
1271 static void loop_reset_node(ir_node *n, void *env) {
1272   set_irn_loop(n, NULL);
1273   reset_backedges(n);
1274 }
1275
1276
1277 /** Removes all loop information.
1278     Resets all backedges */
1279 void free_loop_information(ir_graph *irg) {
1280   /* We can not use this recursion, as the loop might contain
1281      illegal nodes by now.  Why else would we throw away the
1282      representation?
1283   if (get_irg_loop(irg)) loop_reset_backedges(get_irg_loop(irg));
1284   */
1285   irg_walk_graph(irg, loop_reset_node, NULL, NULL);
1286   set_irg_loop(irg, NULL);
1287   set_irg_loopinfo_state(current_ir_graph, loopinfo_none);
1288   /* We cannot free the loop nodes, they are on the obstack. */
1289 }
1290
1291
1292 void free_all_loop_information (void) {
1293   int i;
1294   int rem = interprocedural_view;
1295   interprocedural_view = 1;  /* To visit all filter nodes */
1296   for (i = 0; i < get_irp_n_irgs(); i++) {
1297     free_loop_information(get_irp_irg(i));
1298   }
1299   pmap_destroy(node_loop_map);
1300   node_loop_map = NULL;
1301   interprocedural_view = rem;
1302 }
1303
1304
1305
1306
1307
1308 /* Debug stuff *************************************************/
1309
1310 static int test_loop_node(ir_loop *l) {
1311   int i, has_node = 0, found_problem = 0;
1312   loop_element le;
1313
1314   assert(l && l->kind == k_ir_loop);
1315
1316   if (get_loop_n_elements(l) == 0) {
1317     printf(" Loop completely empty! "); DDML(l);
1318     found_problem = 1;
1319     dump_loop(l, "-ha");
1320   }
1321
1322   le = get_loop_element(l, 0);
1323   if (*(le.kind) != k_ir_node) {
1324     assert(le.kind && *(le.kind) == k_ir_loop);
1325     printf(" First loop element is not a node! "); DDML(l);
1326     printf("                                   "); DDML(le.son);
1327
1328     found_problem = 1;
1329     dump_loop(l, "-ha");
1330   }
1331
1332   if ((*(le.kind) == k_ir_node) && !is_possible_loop_head(le.node)) {
1333     printf(" Wrong node as head! "); DDML(l);
1334     printf("                     "); DDMN(le.node);
1335     found_problem = 1;
1336     dump_loop(l, "-ha");
1337   }
1338
1339   if ((get_loop_depth(l) != 0) &&
1340       (*(le.kind) == k_ir_node) && !has_backedges(le.node)) {
1341     printf(" Loop head has no backedges! "); DDML(l);
1342     printf("                             "); DDMN(le.node);
1343     found_problem = 1;
1344     dump_loop(l, "-ha");
1345   }
1346
1347   /* Recur */
1348   has_node = 0;
1349   for (i = 0; i < get_loop_n_elements(l); ++i) {
1350     le = get_loop_element(l, i);
1351     if (*(le.kind) == k_ir_node)
1352       has_node++;
1353     else
1354       if (test_loop_node(le.son)) found_problem = 1;
1355   }
1356
1357   if (has_node == 0) {
1358     printf(" Loop has no firm node! "); DDML(l);
1359     found_problem = 1;
1360     dump_loop(l, "-ha");
1361   }
1362
1363   if (get_loop_loop_nr(l) == 11819)
1364     dump_loop(l, "-ha-debug");
1365
1366   return found_problem;
1367 }
1368
1369 /** Prints all loop nodes that
1370  *  - do not have any firm nodes, only loop sons
1371  *  - the header is not a Phi, Block or Filter.
1372  */
1373 void find_strange_loop_nodes(ir_loop *l) {
1374   int found_problem = 0;
1375   printf("\nTesting loop "); DDML(l);
1376   found_problem = test_loop_node(l);
1377   printf("Finished Test\n\n");
1378   if (found_problem) exit(0);
1379
1380 }