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],
8 * Author: Goetz Lindenmaier
12 * Copyright: (c) 2002-2003 Universität Karlsruhe
13 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
25 #include "irgraph_t.h"
33 INLINE void add_loop_son(ir_loop *loop, ir_loop *son);
35 INLINE void add_loop_node(ir_loop *loop, ir_node *n);
37 static ir_graph *outermost_ir_graph; /* The outermost graph the scc is computed
39 static ir_loop *current_loop; /* Current loop construction is working
41 static int loop_node_cnt = 0; /* Counts the number of allocated loop nodes.
42 Each loop node gets a unique number.
43 What for? ev. remove. @@@ */
44 static int current_dfn = 1; /* Counter to generate depth first numbering
47 void link_to_reg_end (ir_node *n, void *env);
48 void set_projx_link(ir_node *cb_projx, ir_node *end_projx);
49 ir_node *get_projx_link(ir_node *cb_projx);
51 /**********************************************************************/
52 /* Node attributes **/
53 /**********************************************************************/
55 /**********************************************************************/
56 /* Node attributes needed for the construction. **/
57 /**********************************************************************/
59 typedef struct scc_info {
60 bool in_stack; /* Marks whether node is on the stack. */
61 int dfn; /* Depth first search number. */
62 int uplink; /* dfn number of ancestor. */
63 /* ir_loop *loop; *//* Refers to the containing loop. */
65 struct section *section;
71 static INLINE scc_info* new_scc_info(void) {
72 scc_info *info = obstack_alloc (outermost_ir_graph->obst, sizeof (scc_info));
73 memset (info, 0, sizeof (scc_info));
78 mark_irn_in_stack (ir_node *n) {
79 assert(get_irn_link(n));
81 /* ((scc_info *)get_irn_link(n))->in_stack = true; */
82 ((scc_info *)n->link)->in_stack = true;
86 mark_irn_not_in_stack (ir_node *n) {
87 assert(get_irn_link(n));
89 /* ((scc_info *)get_irn_link(n))->in_stack = false; */
90 ((scc_info *)n->link)->in_stack = false;
94 irn_is_in_stack (ir_node *n) {
95 assert(get_irn_link(n));
97 /* return ((scc_info *)get_irn_link(n))->in_stack; */
98 return ((scc_info *)n->link)->in_stack;
102 set_irn_uplink (ir_node *n, int uplink) {
103 assert(get_irn_link(n));
105 /* ((scc_info *)get_irn_link(n))->uplink = uplink; */
106 ((scc_info *)n->link)->uplink = uplink;
110 get_irn_uplink (ir_node *n) {
111 assert(get_irn_link(n));
112 /* from fast to slow */
113 /* return ((scc_info *)get_irn_link(n))->uplink; */
114 return ((scc_info *)n->link)->uplink;
118 set_irn_dfn (ir_node *n, int dfn) {
119 assert(get_irn_link(n));
121 /* ((scc_info *)get_irn_link(n))->dfn = dfn; */
122 ((scc_info *)n->link)->dfn = dfn;
126 get_irn_dfn (ir_node *n) {
127 assert(get_irn_link(n));
129 /* return ((scc_info *)get_irn_link(n))->dfn; */
130 return ((scc_info *)n->link)->dfn;
135 set_irn_loop (ir_node *n, ir_loop* loop) {
139 /* Uses temporary information to get the loop */
141 get_irn_loop (ir_node *n) {
147 static ir_loop *find_nodes_loop (ir_node *n, ir_loop *l) {
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;
155 /* Is this a leave in the loop tree? If so loop not found. */
156 if (get_loop_n_sons(l) == 0) return NULL;
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));
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);
174 /**********************************************************************/
176 /**********************************************************************/
178 static ir_node **stack = NULL;
179 static int tos = 0; /* top of stack */
181 static INLINE void init_stack(void) {
183 ARR_RESIZE (ir_node *, stack, 1000);
185 stack = NEW_ARR_F (ir_node *, 1000);
191 static INLINE void free_stack(void) {
203 if (tos == ARR_LEN (stack)) {
204 int nlen = ARR_LEN (stack) * 2;
205 ARR_RESIZE (ir_node *, stack, nlen);
208 mark_irn_in_stack(n);
211 static INLINE ir_node *
214 ir_node *n = stack[--tos];
215 mark_irn_not_in_stack(n);
219 /* The nodes up to n belong to the current loop.
220 Removes them from the stack and adds them to the current loop. */
222 pop_scc_to_loop (ir_node *n)
230 //printf(" dfn: %d, upl %d upl-new %d ", get_irn_dfn(m), get_irn_uplink(m), loop_node_cnt+1); DDMN(m);
233 set_irn_dfn(m, loop_node_cnt);
234 add_loop_node(current_loop, m);
235 set_irn_loop(m, current_loop);
237 /* if (m==n) break;*/
240 /* i might be bigger than 1 for dead (and that's why bad) loops */
242 printf("Mehr als eine Iteration!!!!!!!!!!!!!!!!!!!!!!!!!!!!11111\n");
246 /* GL ??? my last son is my grandson??? Removes loops with no
247 ir_nodes in them. Such loops have only another loop as son. (Why
248 can't they have two loops as sons? Does it never get that far? ) */
249 static void close_loop (ir_loop *l)
251 int last = get_loop_n_elements(l) - 1;
252 loop_element lelement = get_loop_element(l, last);
253 ir_loop *last_son = lelement.son;
255 if (get_kind(last_son) == k_ir_loop &&
256 get_loop_n_elements(last_son) == 1)
260 lelement = get_loop_element(last_son, 0);
262 if(get_kind(gson) == k_ir_loop)
264 loop_element new_last_son;
266 gson -> outer_loop = l;
267 new_last_son.son = gson;
268 l -> children[last] = new_last_son;
275 /* Removes and unmarks all nodes up to n from the stack.
276 The nodes must be visited once more to assign them to a scc. */
278 pop_scc_unmark_visit (ir_node *n)
284 set_irn_visited(m, 0);
288 /**********************************************************************/
289 /* The loop datastructure. **/
290 /**********************************************************************/
292 /* Allocates a new loop as son of current_loop. Sets current_loop
293 to the new loop and returns the father. */
294 ir_loop *new_loop (void) {
295 ir_loop *father, *son;
297 father = current_loop;
299 son = (ir_loop *) obstack_alloc (outermost_ir_graph->obst, sizeof (ir_loop));
300 memset (son, 0, sizeof (ir_loop));
301 son->kind = k_ir_loop;
302 son->children = NEW_ARR_F (loop_element, 0);
306 son->outer_loop = father;
307 add_loop_son(father, son);
308 son->depth = father->depth+1;
309 } else { /* The root loop */
310 son->outer_loop = son;
315 son->loop_nr = get_irp_new_node_nr();
324 /* Finishes the datastructures, copies the arrays to the obstack
326 A. Schoesser: Caution: loop -> sons is gone. */
327 static void mature_loop (ir_loop *loop) {
330 new_sons = NEW_ARR_D (ir_loop *, current_ir_graph->obst, ARR_LEN(loop->sons));
331 memcpy (new_sons, loop->sons, sizeof (ir_loop *) * ARR_LEN(loop->sons));
332 DEL_ARR_F(loop->sons);
333 loop->sons = new_sons;
337 /* Returns outer loop, itself if outermost. */
338 ir_loop *get_loop_outer_loop (ir_loop *loop) {
339 assert(loop && loop->kind == k_ir_loop);
340 return loop->outer_loop;
343 /* Returns nesting depth of this loop */
344 int get_loop_depth (ir_loop *loop) {
345 assert(loop); assert(loop->kind == k_ir_loop);
349 /* Returns the number of inner loops */
350 int get_loop_n_sons (ir_loop *loop) {
351 assert(loop && loop->kind == k_ir_loop);
352 return(loop -> n_sons);
355 /* Returns the pos`th loop_node-child *
356 * TODO: This method isn`t very efficient ! *
357 * Returns NULL if there isnt`t a pos`th loop_node */
358 ir_loop *get_loop_son (ir_loop *loop, int pos) {
359 int child_nr = 0, loop_nr = -1;
361 assert(loop && loop->kind == k_ir_loop);
362 while(child_nr < ARR_LEN(loop->children))
364 if(*(loop -> children[child_nr].kind) == k_ir_loop)
367 return(loop -> children[child_nr].son);
373 /* Use EXCLUSIVELY this function to add sons, otherwise the loop->n_sons
377 add_loop_son(ir_loop *loop, ir_loop *son) {
380 assert(loop && loop->kind == k_ir_loop);
381 assert(get_kind(son) == k_ir_loop);
382 ARR_APP1 (loop_element, loop->children, lson);
386 /* Returns the number of nodes in the loop */
387 int get_loop_n_nodes (ir_loop *loop) {
388 assert(loop); assert(loop->kind == k_ir_loop);
389 return loop -> n_nodes;
390 /* return ARR_LEN(loop->nodes); */
393 /* Returns the pos`th ir_node-child *
394 * TODO: This method isn`t very efficient ! *
395 * Returns NULL if there isnt`t a pos`th ir_node */
396 ir_node *get_loop_node (ir_loop *loop, int pos) {
397 int child_nr, node_nr = -1;
399 assert(loop && loop->kind == k_ir_loop);
400 assert(pos < get_loop_n_nodes(loop));
402 for (child_nr = 0; child_nr < ARR_LEN(loop->children); child_nr++) {
403 if(*(loop -> children[child_nr].kind) == k_ir_node)
406 return(loop -> children[child_nr].node);
409 printf("pos: %d\n", pos);
410 assert(0 && "no child at pos found");
414 /* Use EXCLUSIVELY this function to add nodes, otherwise the loop->n_nodes
418 add_loop_node(ir_loop *loop, ir_node *n) {
421 assert(loop && loop->kind == k_ir_loop);
422 assert(get_kind(n) == k_ir_node || get_kind(n) == k_ir_graph); /* used in callgraph.c */
423 ARR_APP1 (loop_element, loop->children, ln);
427 /** Returns the number of elements contained in loop. */
428 int get_loop_n_elements (ir_loop *loop) {
429 assert(loop && loop->kind == k_ir_loop);
430 return(ARR_LEN(loop->children));
434 Returns the pos`th loop element.
435 This may be a loop_node or a ir_node. The caller of this function has
436 to check the *(loop_element.kind) field for "k_ir_node" or "k_ir_loop"
437 and then select the apropriate "loop_element.node" or "loop_element.son".
440 loop_element get_loop_element (ir_loop *loop, int pos) {
441 assert(loop && loop->kind == k_ir_loop && pos < ARR_LEN(loop->children));
443 return(loop -> children[pos]);
446 int get_loop_element_pos(ir_loop *loop, void *le) {
448 assert(loop && loop->kind == k_ir_loop);
450 for (i = 0; i < get_loop_n_elements(loop); i++)
451 if (get_loop_element(loop, i).node == le) return i;
455 int get_loop_loop_nr(ir_loop *loop) {
456 assert(loop && loop->kind == k_ir_loop);
458 return loop->loop_nr;
465 /** A field to connect additional information to a loop. Only valid
466 if libfirm_debug is set. */
467 void set_loop_link (ir_loop *loop, void *link) {
468 assert(loop && loop->kind == k_ir_loop);
473 void *get_loop_link (const ir_loop *loop) {
474 assert(loop && loop->kind == k_ir_loop);
482 /* The outermost loop is remarked in the surrounding graph. */
483 void set_irg_loop(ir_graph *irg, ir_loop *loop) {
487 ir_loop *get_irg_loop(ir_graph *irg) {
493 /**********************************************************************/
494 /* Constructing and destructing the loop/backedge information. **/
495 /**********************************************************************/
497 /* Initialization steps. **********************************************/
500 init_node (ir_node *n, void *env) {
501 set_irn_link (n, new_scc_info());
504 /* Also init nodes not visible in intraproc_view. */
505 /* @@@ init_node is called for too many nodes -- this wastes memory!.
506 The mem is not lost as its on the obstack. */
507 if (get_irn_op(n) == op_Filter) {
508 for (i = 0; i < get_Filter_n_cg_preds(n); i++)
509 init_node(get_Filter_cg_pred(n, i), NULL);
511 if (get_irn_op(n) == op_Block) {
512 for (i = 0; i < get_Block_cg_n_cfgpreds(n); i++) {
513 init_node(get_Block_cg_cfgpred(n, i), NULL);
516 /* The following pattern matches only after a call from above pattern. */
517 if ((get_irn_op(n) == op_Proj) /*&& (get_Proj_proj(n) == 0)*/) {
518 /* @@@ init_node is called for every proj -- this wastes memory!.
519 The mem is not lost as its on the obstack. */
520 ir_node *cb = get_Proj_pred(n);
521 if ((get_irn_op(cb) == op_CallBegin) ||
522 (get_irn_op(cb) == op_EndReg) ||
523 (get_irn_op(cb) == op_EndExcept)) {
525 init_node(get_nodes_Block(cb), NULL);
532 init_scc_common (void) {
539 init_scc (ir_graph *irg) {
541 irg_walk_graph (irg, init_node, NULL, NULL);
543 irg_walk (irg, link_to_reg_end, NULL, NULL);
550 cg_walk (init_node, NULL, NULL);
552 #if EXPERIMENTAL_LOOP_TREE
553 cg_walk (link_to_reg_end, NULL, NULL);
557 /* Condition for breaking the recursion. */
558 static bool is_outermost_Start(ir_node *n) {
559 /* Test whether this is the outermost Start node. If so
560 recursion must end. */
561 if ((get_irn_op(n) == op_Block) &&
562 (get_Block_n_cfgpreds(n) == 1) &&
563 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Start) &&
564 (get_nodes_Block(skip_Proj(get_Block_cfgpred(n, 0))) == n)) {
568 /* @@@ Bad condition:
569 not possible in interprocedural view as outermost_graph is
570 not necessarily the only with a dead-end start block.
571 Besides current_ir_graph is not set properly. */
572 if ((get_irn_op(n) == op_Block) &&
573 (n == get_irg_start_block(current_ir_graph))) {
574 if ((!interprocedural_view) ||
575 (current_ir_graph == outermost_ir_graph))
582 /* When to walk from nodes to blocks. Only for Control flow operations? */
584 get_start_index(ir_node *n) {
585 #undef BLOCK_BEFORE_NODE
586 #define BLOCK_BEFORE_NODE 1
588 #if BLOCK_BEFORE_NODE
590 /* This version assures, that all nodes are ordered absolutely. This allows
591 to undef all nodes in the heap analysis if the block is false, which means
593 I.e., with this code, the order on the loop tree is correct. But a (single)
594 test showed the loop tree is deeper. */
595 if (get_irn_op(n) == op_Phi ||
596 get_irn_op(n) == op_Block ||
597 (get_irn_op(n) == op_Filter && interprocedural_view) ||
598 (get_irg_pinned(get_irn_irg(n)) == floats &&
599 get_op_pinned(get_irn_op(n)) == floats))
600 // Here we could test for backedge at -1 which is illegal
607 /* This version causes deeper loop trees (at least we verified this
609 But it guarantees that Blocks are analysed before nodes contained in the
610 block. If so, we can set the value to undef if the block is not \
612 if (is_cfop(n) || is_fragile_op(n) || get_irn_op(n) == op_Start)
622 static void test(ir_node *pred, ir_node *root, ir_node *this) {
624 if (get_irn_uplink(pred) >= get_irn_uplink(root)) return;
626 printf("this: %d ", get_irn_uplink(this)); DDMN(this);
627 printf("pred: %d ", get_irn_uplink(pred)); DDMN(pred);
628 printf("root: %d ", get_irn_uplink(root)); DDMN(root);
630 printf("tos: %d\n", tos);
632 for (i = tos; i >= 0; i--) {
633 ir_node *n = stack[i];
635 printf(" uplink: %d, pos: %d ", get_irn_uplink(n), i); DDMN(n);
640 /* Test for legal loop header: Block, Phi, ... */
641 INLINE static bool is_possible_loop_head(ir_node *n) {
642 ir_op *op = get_irn_op(n);
643 return ((op == op_Block) ||
645 ((op == op_Filter) && interprocedural_view));
648 /* Returns true if n is a loop header, i.e., it is a Block, Phi
649 or Filter node and has predecessors within the loop and out
651 @arg root: only needed for assertion. */
653 is_head (ir_node *n, ir_node *root)
656 int some_outof_loop = 0, some_in_loop = 0;
658 /* Test for legal loop header: Block, Phi, ... */
659 if (!is_possible_loop_head(n))
662 if (!is_outermost_Start(n)) {
663 arity = get_irn_arity(n);
664 for (i = get_start_index(n); i < arity; i++) {
665 ir_node *pred = get_irn_n(n, i);
667 if (is_backedge(n, i)) continue;
668 if (!irn_is_in_stack(pred)) {
671 if(get_irn_uplink(pred) < get_irn_uplink(root)) {
672 DDMN(n); DDMN(pred); DDMN(root);
673 assert(get_irn_uplink(pred) >= get_irn_uplink(root));
679 return some_outof_loop && some_in_loop;
682 /* Returns true if n is possible loop head of an endless loop.
683 I.e., it is a Block, Phi or Filter node and has only predecessors
685 @arg root: only needed for assertion. */
687 is_endless_head (ir_node *n, ir_node *root)
690 int some_outof_loop = 0, some_in_loop = 0;
692 /* Test for legal loop header: Block, Phi, ... */
693 if (!is_possible_loop_head(n))
696 if (!is_outermost_Start(n)) {
697 arity = get_irn_arity(n);
698 for (i = get_start_index(n); i < arity; i++) {
699 ir_node *pred = get_irn_n(n, i);
701 if (is_backedge(n, i)) { continue; }
702 if (!irn_is_in_stack(pred)) {
703 some_outof_loop = 1; //printf(" some out of loop ");
705 if(get_irn_uplink(pred) < get_irn_uplink(root)) {
706 DDMN(pred); DDMN(root);
707 assert(get_irn_uplink(pred) >= get_irn_uplink(root));
713 return !some_outof_loop && some_in_loop;
716 /* Returns index of the predecessor with the smallest dfn number
717 greater-equal than limit. */
719 smallest_dfn_pred (ir_node *n, int limit)
721 int i, index = -2, min = -1;
723 if (!is_outermost_Start(n)) {
724 int arity = get_irn_arity(n);
725 for (i = get_start_index(n); i < arity; i++) {
726 ir_node *pred = get_irn_n(n, i);
728 if (is_backedge(n, i) || !irn_is_in_stack(pred)) continue;
729 if (get_irn_dfn(pred) >= limit && (min == -1 || get_irn_dfn(pred) < min)) {
731 min = get_irn_dfn(pred);
738 /* Returns index of the predecessor with the largest dfn number. */
740 largest_dfn_pred (ir_node *n)
742 int i, index = -2, max = -1;
744 if (!is_outermost_Start(n)) {
745 int arity = get_irn_arity(n);
746 for (i = get_start_index(n); i < arity; i++) {
747 ir_node *pred = get_irn_n(n, i);
748 if (is_backedge (n, i) || !irn_is_in_stack(pred)) continue;
749 if (get_irn_dfn(pred) > max) {
751 max = get_irn_dfn(pred);
758 /** Searches the stack for possible loop heads. Tests these for backedges.
759 If it finds a head with an unmarked backedge it marks this edge and
760 returns the tail of the loop.
761 If it finds no backedge returns NULL.
762 ("disable_backedge" in fiasco)
764 * @param n A node where uplink == dfn.
768 find_tail (ir_node *n) {
770 int i, res_index = -2;
773 if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
775 m = stack[tos-1]; /* tos = top of stack */
776 if (is_head (m, n)) {
777 res_index = smallest_dfn_pred(m, 0);
778 if ((res_index == -2) && /* no smallest dfn pred found. */
782 if (m == n) return NULL; // Is this to catch Phi - self loops?
783 for (i = tos-2; i >= 0; --i) {
786 if (is_head (m, n)) {
787 res_index = smallest_dfn_pred (m, get_irn_dfn(m) + 1);
788 if (res_index == -2) /* no smallest dfn pred found. */
789 res_index = largest_dfn_pred (m);
791 if ((m == n) && (res_index == -2)) {
797 /* We should not walk past our selves on the stack: The upcoming nodes
798 are not in this loop. We assume a loop not reachable from Start. */
807 /* A dead loop not reachable from Start. */
808 for (i = tos-2; i >= 0; --i) {
810 if (is_endless_head (m, n)) {
811 res_index = smallest_dfn_pred (m, get_irn_dfn(m) + 1);
812 if (res_index == -2) /* no smallest dfn pred found. */
813 res_index = largest_dfn_pred (m);
816 if (m == n) { break; } /* It's not an unreachable loop, either. */
818 //assert(0 && "no head found on stack");
822 if (res_index <= -2) {
823 /* It's a completely bad loop: without Phi/Block nodes that can
824 be a head. I.e., the code is "dying". We break the loop by
825 setting Bad nodes. */
826 int arity = get_irn_arity(n);
827 for (i = -1; i < arity; ++i) {
828 set_irn_n(n, i, get_irg_bad(get_irn_irg(n)));
832 assert (res_index > -2);
834 set_backedge (m, res_index);
835 return is_outermost_Start(n) ? NULL : get_irn_n(m, res_index);
839 #if EXPERIMENTAL_LOOP_TREE
841 /* ----------------------------------------------------------------
842 AS: This is experimantal code to build loop trees suitable for
843 the heap analysis. Does not work correctly right now... :-(
846 Search in stack for the corresponding first Call-End-ProjX that
847 corresponds to one of the control flow predecessors of the given
848 block, that is the possible callers.
849 returns: the control predecessor to chose\
850 or -1 if no corresponding Call-End-Node could be found
852 - -------------------------------------------------------------- */
854 int search_endproj_in_stack(ir_node *start_block)
857 assert(is_Block(start_block));
858 for(i = tos - 1; i >= 0; --i)
861 if(get_irn_op(stack[i]) == op_Proj && get_irn_mode(stack[i]) == mode_X &&
862 get_irn_op(get_irn_n(stack[i], 0)) == op_EndReg)
864 printf("FOUND PROJ!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
865 ir_node *end_projx = stack[i];
867 int arity = get_irn_arity(start_block);
868 for(j = 0; j < arity; j++)
870 ir_node *begin_projx = get_Block_cfgpred(get_irg_start_block(get_irn_irg(end_projx)), get_Proj_proj(end_projx));
872 if(get_irn_n(start_block, j) == begin_projx)
874 printf("FOUND IT!!!!!!!!!!!!!!!!!!\n");
884 static pmap *projx_link = NULL;
886 void link_to_reg_end (ir_node *n, void *env) {
887 if(get_irn_op(n) == op_Proj && get_irn_mode(n) == mode_X && get_irn_op(get_irn_n(n, 0)) == op_EndReg)
889 /* Reg End Projx -> Find the CallBegin Projx and hash it */
890 ir_node *end_projx = n;
891 ir_node *begin_projx = get_Block_cfgpred(get_irg_start_block(get_irn_irg(end_projx)), get_Proj_proj(end_projx));
892 printf("Linked the following ProjxNodes:\n");
895 set_projx_link(begin_projx, end_projx);
899 void set_projx_link(ir_node *cb_projx, ir_node *end_projx)
901 if(projx_link == NULL)
902 projx_link = pmap_create();
903 pmap_insert(projx_link, (void *)cb_projx, (void *)end_projx);
906 ir_node *get_projx_link(ir_node *cb_projx)
908 return((ir_node *) pmap_get(projx_link, (void *)cb_projx));
915 /*-----------------------------------------------------------*
916 * The core algorithm. *
917 *-----------------------------------------------------------*/
920 static void scc (ir_node *n) {
922 if (irn_visited(n)) return;
925 /* Initialize the node */
926 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
927 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
928 set_irn_loop(n, NULL);
932 /* AS: get_start_index might return -1 for Control Flow Nodes, and thus a negative
933 array index would be passed to is_backedge(). But CFG Nodes dont't have a backedge array,
934 so is_backedge does not access array[-1] but correctly returns false! */
936 if (!is_outermost_Start(n)) {
937 int arity = get_irn_arity(n);
939 for (i = get_start_index(n); i < arity; i++) {
941 if (is_backedge(n, i)) continue;
942 m = get_irn_n(n, i); /* get_irn_ip_pred(n, i); */
943 /* if ((!m) || (get_irn_op(m) == op_Unknown)) continue; */
945 if (irn_is_in_stack(m)) {
946 /* Uplink of m is smaller if n->m is a backedge.
947 Propagate the uplink to mark the loop. */
948 if (get_irn_uplink(m) < get_irn_uplink(n))
949 set_irn_uplink(n, get_irn_uplink(m));
954 if (get_irn_dfn(n) == get_irn_uplink(n)) {
955 /* This condition holds for
956 1) the node with the incoming backedge.
957 That is: We found a loop!
958 2) Straight line code, because no uplink has been propagated, so the
959 uplink still is the same as the dfn.
961 But n might not be a proper loop head for the analysis. Proper loop
962 heads are Block and Phi nodes. find_tail searches the stack for
963 Block's and Phi's and takes those nodes as loop heads for the current
964 loop instead and marks the incoming edge as backedge. */
966 ir_node *tail = find_tail(n);
968 /* We have a loop, that is no straight line code,
969 because we found a loop head!
970 Next actions: Open a new loop on the loop tree and
971 try to find inner loops */
973 #define NO_LOOPS_WITHOUT_HEAD 1
974 #if NO_LOOPS_WITHOUT_HEAD
975 /* This is an adaption of the algorithm from fiasco / optscc to
976 * avoid loops without Block or Phi as first node. This should
977 * severely reduce the number of evaluations of nodes to detect
978 * a fixpoint in the heap analysis.
979 * Further it avoids loops without firm nodes that cause errors
980 * in the heap analyses. */
984 if (get_loop_n_elements(current_loop) > 0) {
992 ir_loop *l = new_loop();
995 /* Remove the loop from the stack ... */
996 pop_scc_unmark_visit (n);
998 /* The current backedge has been marked, that is temporarily eliminated,
999 by find tail. Start the scc algorithm
1000 anew on the subgraph thats left (the current loop without the backedge)
1001 in order to find more inner loops. */
1004 assert (irn_visited(n));
1005 #if NO_LOOPS_WITHOUT_HEAD
1012 /* No loop head was found, that is we have straightline code.
1013 Pop all nodes from the stack to the current loop. */
1019 static void my_scc (ir_node *n) {
1021 if (irn_visited(n)) return;
1022 mark_irn_visited(n);
1024 /* Initialize the node */
1025 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
1026 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
1027 set_irn_loop(n, NULL);
1031 /* AS: get_start_index might return -1 for Control Flow Nodes, and thus a negative
1032 array index would be passed to is_backedge(). But CFG Nodes dont't have a backedge array,
1033 so is_backedge does not access array[-1] but correctly returns false! */
1035 if (!is_outermost_Start(n)) {
1036 int arity = get_irn_arity(n);
1038 for (i = get_start_index(n); i < arity; i++) {
1040 if (is_backedge(n, i)) continue;
1041 m = get_irn_n(n, i); /* get_irn_ip_pred(n, i); */
1042 /* if ((!m) || (get_irn_op(m) == op_Unknown)) continue; */
1044 if (irn_is_in_stack(m)) {
1045 /* Uplink of m is smaller if n->m is a backedge.
1046 Propagate the uplink to mark the loop. */
1047 if (get_irn_uplink(m) < get_irn_uplink(n))
1048 set_irn_uplink(n, get_irn_uplink(m));
1053 if (get_irn_dfn(n) == get_irn_uplink(n)) {
1054 /* This condition holds for
1055 1) the node with the incoming backedge.
1056 That is: We found a loop!
1057 2) Straight line code, because no uplink has been propagated, so the
1058 uplink still is the same as the dfn.
1060 But n might not be a proper loop head for the analysis. Proper loop
1061 heads are Block and Phi nodes. find_tail searches the stack for
1062 Block's and Phi's and takes those nodes as loop heads for the current
1063 loop instead and marks the incoming edge as backedge. */
1065 ir_node *tail = find_tail(n);
1067 /* We have a loop, that is no straight line code,
1068 because we found a loop head!
1069 Next actions: Open a new loop on the loop tree and
1070 try to find inner loops */
1072 #define NO_LOOPS_WITHOUT_HEAD 1
1073 #if NO_LOOPS_WITHOUT_HEAD
1074 /* This is an adaption of the algorithm from fiasco / optscc to
1075 * avoid loops without Block or Phi as first node. This should
1076 * severely reduce the number of evaluations of nodes to detect
1077 * a fixpoint in the heap analysis.
1078 * Further it avoids loops without firm nodes that cause errors
1079 * in the heap analyses. */
1083 if (get_loop_n_elements(current_loop) > 0) {
1091 ir_loop *l = new_loop();
1094 /* Remove the loop from the stack ... */
1095 pop_scc_unmark_visit (n);
1097 /* The current backedge has been marked, that is temporarily eliminated,
1098 by find tail. Start the scc algorithm
1099 anew on the subgraph thats left (the current loop without the backedge)
1100 in order to find more inner loops. */
1103 assert (irn_visited(n));
1104 #if NO_LOOPS_WITHOUT_HEAD
1111 /* No loop head was found, that is we have straightline code.
1112 Pop all nodes from the stack to the current loop. */
1118 /* Constructs backedge information for irg. In interprocedural view constructs
1119 backedges for all methods called by irg, too. */
1120 void construct_backedges(ir_graph *irg) {
1121 ir_graph *rem = current_ir_graph;
1124 assert(!interprocedural_view &&
1125 "not implemented, use construct_ip_backedges");
1127 current_ir_graph = irg;
1128 outermost_ir_graph = irg;
1130 init_scc(current_ir_graph);
1132 current_loop = NULL;
1133 new_loop(); /* sets current_loop */
1134 head_rem = current_loop; /* Just for assertion */
1136 if (interprocedural_view) {
1137 set_irg_visited(current_ir_graph, inc_max_irg_visited());
1140 inc_irg_visited(current_ir_graph);
1143 scc(get_irg_end(current_ir_graph));
1145 if (interprocedural_view) finish_ip_walk();
1147 assert(head_rem == current_loop);
1148 set_irg_loop(current_ir_graph, current_loop);
1149 set_irg_loopinfo_state(current_ir_graph, loopinfo_consistent);
1150 assert(get_irg_loop(current_ir_graph)->kind == k_ir_loop);
1152 irg->loops = current_loop;
1156 count_loop (the_loop, &count, &depth);
1160 current_ir_graph = rem;
1165 void construct_ip_backedges (void) {
1166 ir_graph *rem = current_ir_graph;
1167 int rem_ipv = interprocedural_view;
1170 outermost_ir_graph = get_irp_main_irg();
1174 current_loop = NULL;
1175 new_loop(); /* sets current_loop */
1176 interprocedural_view = 1;
1178 inc_max_irg_visited();
1179 for (i = 0; i < get_irp_n_irgs(); i++)
1180 set_irg_visited(get_irp_irg(i), get_max_irg_visited());
1182 for (i = 0; i < get_irp_n_irgs(); i++) {
1184 current_ir_graph = get_irp_irg(i);
1185 /* Find real entry points */
1186 sb = get_irg_start_block(current_ir_graph);
1187 if ((get_Block_n_cfgpreds(sb) > 1) ||
1188 (get_nodes_Block(get_Block_cfgpred(sb, 0)) != sb)) continue;
1189 /* Compute scc for this graph */
1190 outermost_ir_graph = current_ir_graph;
1191 set_irg_visited(outermost_ir_graph, get_max_irg_visited());
1192 scc(get_irg_end(current_ir_graph));
1193 for (j = 0; j < get_End_n_keepalives(get_irg_end(outermost_ir_graph)); j++)
1194 scc(get_End_keepalive(get_irg_end(outermost_ir_graph), j));
1197 set_irg_loop(outermost_ir_graph, current_loop);
1198 set_irg_loopinfo_state(current_ir_graph, loopinfo_ip_consistent);
1199 assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
1201 current_ir_graph = rem;
1202 interprocedural_view = rem_ipv;
1205 void construct_ip_backedges (void) {
1206 ir_graph *rem = current_ir_graph;
1207 int rem_ipv = interprocedural_view;
1210 assert(get_irp_ip_view_state() == ip_view_valid);
1212 outermost_ir_graph = get_irp_main_irg();
1216 current_loop = NULL;
1217 new_loop(); /* sets current_loop */
1218 interprocedural_view = 1;
1220 inc_max_irg_visited();
1221 for (i = 0; i < get_irp_n_irgs(); i++)
1222 set_irg_visited(get_irp_irg(i), get_max_irg_visited());
1224 /** We have to start the walk at the same nodes as cg_walk. **/
1225 /* Walk starting at unreachable procedures. Only these
1226 * have End blocks visible in interprocedural view. */
1227 for (i = 0; i < get_irp_n_irgs(); i++) {
1229 current_ir_graph = get_irp_irg(i);
1231 sb = get_irg_start_block(current_ir_graph);
1233 if ((get_Block_n_cfgpreds(sb) > 1) ||
1234 (get_nodes_block(get_Block_cfgpred(sb, 0)) != sb)) continue;
1236 scc(get_irg_end(current_ir_graph));
1239 /* Check whether we walked all procedures: there could be procedures
1240 with cyclic calls but no call from the outside. */
1241 for (i = 0; i < get_irp_n_irgs(); i++) {
1243 current_ir_graph = get_irp_irg(i);
1245 /* Test start block: if inner procedure end and end block are not
1246 * visible and therefore not marked. */
1247 sb = get_irg_start_block(current_ir_graph);
1248 if (get_irn_visited(sb) < get_irg_visited(current_ir_graph)) scc(sb);
1251 /* Walk all endless loops in inner procedures.
1252 * We recognize an inner procedure if the End node is not visited. */
1253 for (i = 0; i < get_irp_n_irgs(); i++) {
1255 current_ir_graph = get_irp_irg(i);
1257 e = get_irg_end(current_ir_graph);
1258 if (get_irn_visited(e) < get_irg_visited(current_ir_graph)) {
1260 /* Don't visit the End node. */
1261 for (j = 0; j < get_End_n_keepalives(e); j++) scc(get_End_keepalive(e, j));
1265 set_irg_loop(outermost_ir_graph, current_loop);
1266 set_irg_loopinfo_state(current_ir_graph, loopinfo_ip_consistent);
1267 assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
1269 current_ir_graph = rem;
1270 interprocedural_view = rem_ipv;
1273 void my_construct_ip_backedges (void) {
1274 ir_graph *rem = current_ir_graph;
1275 int rem_ipv = interprocedural_view;
1278 assert(get_irp_ip_view_state() == ip_view_valid);
1280 outermost_ir_graph = get_irp_main_irg();
1284 current_loop = NULL;
1285 new_loop(); /* sets current_loop */
1286 interprocedural_view = 1;
1288 inc_max_irg_visited();
1289 for (i = 0; i < get_irp_n_irgs(); i++)
1290 set_irg_visited(get_irp_irg(i), get_max_irg_visited());
1292 /** We have to start the walk at the same nodes as cg_walk. **/
1293 /* Walk starting at unreachable procedures. Only these
1294 * have End blocks visible in interprocedural view. */
1295 for (i = 0; i < get_irp_n_irgs(); i++) {
1297 current_ir_graph = get_irp_irg(i);
1299 sb = get_irg_start_block(current_ir_graph);
1301 if ((get_Block_n_cfgpreds(sb) > 1) ||
1302 (get_nodes_block(get_Block_cfgpred(sb, 0)) != sb)) continue;
1304 my_scc(get_irg_end(current_ir_graph));
1307 /* Check whether we walked all procedures: there could be procedures
1308 with cyclic calls but no call from the outside. */
1309 for (i = 0; i < get_irp_n_irgs(); i++) {
1311 current_ir_graph = get_irp_irg(i);
1313 /* Test start block: if inner procedure end and end block are not
1314 * visible and therefore not marked. */
1315 sb = get_irg_start_block(current_ir_graph);
1316 if (get_irn_visited(sb) < get_irg_visited(current_ir_graph)) scc(sb);
1319 /* Walk all endless loops in inner procedures.
1320 * We recognize an inner procedure if the End node is not visited. */
1321 for (i = 0; i < get_irp_n_irgs(); i++) {
1323 current_ir_graph = get_irp_irg(i);
1325 e = get_irg_end(current_ir_graph);
1326 if (get_irn_visited(e) < get_irg_visited(current_ir_graph)) {
1328 /* Don't visit the End node. */
1329 for (j = 0; j < get_End_n_keepalives(e); j++) scc(get_End_keepalive(e, j));
1333 set_irg_loop(outermost_ir_graph, current_loop);
1334 set_irg_loopinfo_state(current_ir_graph, loopinfo_ip_consistent);
1335 assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
1337 current_ir_graph = rem;
1338 interprocedural_view = rem_ipv;
1341 static void reset_backedges(ir_node *n) {
1342 if (is_possible_loop_head(n)) {
1343 int rem = interprocedural_view;
1344 interprocedural_view = 1;
1346 interprocedural_view = 0;
1348 interprocedural_view = rem;
1354 static void loop_reset_backedges(ir_loop *l) {
1356 reset_backedges(get_loop_node(l, 0));
1357 for (i = 0; i < get_loop_n_nodes(l); ++i)
1358 set_irn_loop(get_loop_node(l, i), NULL);
1359 for (i = 0; i < get_loop_n_sons(l); ++i) {
1360 loop_reset_backedges(get_loop_son(l, i));
1365 static void loop_reset_node(ir_node *n, void *env) {
1366 set_irn_loop(n, NULL);
1371 /** Removes all loop information.
1372 Resets all backedges */
1373 void free_loop_information(ir_graph *irg) {
1374 /* We can not use this recursion, as the loop might contain
1375 illegal nodes by now. Why else would we throw away the
1377 if (get_irg_loop(irg)) loop_reset_backedges(get_irg_loop(irg));
1379 irg_walk_graph(irg, loop_reset_node, NULL, NULL);
1380 set_irg_loop(irg, NULL);
1381 set_irg_loopinfo_state(current_ir_graph, loopinfo_none);
1382 /* We cannot free the loop nodes, they are on the obstack. */
1386 void free_all_loop_information (void) {
1388 int rem = interprocedural_view;
1389 interprocedural_view = 1; /* To visit all filter nodes */
1390 for (i = 0; i < get_irp_n_irgs(); i++) {
1391 free_loop_information(get_irp_irg(i));
1393 interprocedural_view = rem;
1400 /* Debug stuff *************************************************/
1402 static int test_loop_node(ir_loop *l) {
1403 int i, has_node = 0, found_problem = 0;
1406 assert(l && l->kind == k_ir_loop);
1408 if (get_loop_n_elements(l) == 0) {
1409 printf(" Loop completely empty! "); DDML(l);
1411 dump_loop(l, "-ha");
1414 le = get_loop_element(l, 0);
1415 if (*(le.kind) != k_ir_node) {
1416 assert(le.kind && *(le.kind) == k_ir_loop);
1417 printf(" First loop element is not a node! "); DDML(l);
1418 printf(" "); DDML(le.son);
1421 dump_loop(l, "-ha");
1424 if ((*(le.kind) == k_ir_node) && !is_possible_loop_head(le.node)) {
1425 printf(" Wrong node as head! "); DDML(l);
1426 printf(" "); DDMN(le.node);
1428 dump_loop(l, "-ha");
1431 if ((get_loop_depth(l) != 0) &&
1432 (*(le.kind) == k_ir_node) && !has_backedges(le.node)) {
1433 printf(" Loop head has no backedges! "); DDML(l);
1434 printf(" "); DDMN(le.node);
1436 dump_loop(l, "-ha");
1441 for (i = 0; i < get_loop_n_elements(l); ++i) {
1442 le = get_loop_element(l, i);
1443 if (*(le.kind) == k_ir_node)
1446 if (test_loop_node(le.son)) found_problem = 1;
1449 if (has_node == 0) {
1450 printf(" Loop has no firm node! "); DDML(l);
1452 dump_loop(l, "-ha");
1455 if (get_loop_loop_nr(l) == 11819)
1456 dump_loop(l, "-ha-debug");
1458 return found_problem;
1461 /** Prints all loop nodes that
1462 * - do not have any firm nodes, only loop sons
1463 * - the header is not a Phi, Block or Filter.
1465 void find_strange_loop_nodes(ir_loop *l) {
1466 int found_problem = 0;
1467 printf("\nTesting loop "); DDML(l);
1468 found_problem = test_loop_node(l);
1469 printf("Finished Test\n\n");
1470 if (found_problem) exit(0);