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