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