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