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