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 /* A variant of the loop tree that avoids loops without head.
34 This reduces the depth of the loop tree. */
35 #define NO_LOOPS_WITHOUT_HEAD 1
38 INLINE void add_loop_son(ir_loop *loop, ir_loop *son);
40 INLINE void add_loop_node(ir_loop *loop, ir_node *n);
42 static ir_graph *outermost_ir_graph; /* The outermost graph the scc is computed
44 static ir_loop *current_loop; /* Current loop construction is working
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
52 void link_to_reg_end (ir_node *n, void *env);
53 void set_projx_link(ir_node *cb_projx, ir_node *end_projx);
54 ir_node *get_projx_link(ir_node *cb_projx);
56 /**********************************************************************/
57 /* Node attributes **/
58 /**********************************************************************/
60 /**********************************************************************/
61 /* Node attributes needed for the construction. **/
62 /**********************************************************************/
64 typedef struct scc_info {
65 bool in_stack; /* Marks whether node is on the stack. */
66 int dfn; /* Depth first search number. */
67 int uplink; /* dfn number of ancestor. */
68 /* ir_loop *loop; *//* Refers to the containing loop. */
70 struct section *section;
76 static INLINE scc_info* new_scc_info(void) {
77 scc_info *info = obstack_alloc (outermost_ir_graph->obst, sizeof (scc_info));
78 memset (info, 0, sizeof (scc_info));
83 mark_irn_in_stack (ir_node *n) {
84 assert(get_irn_link(n));
86 /* ((scc_info *)get_irn_link(n))->in_stack = true; */
87 ((scc_info *)n->link)->in_stack = true;
91 mark_irn_not_in_stack (ir_node *n) {
92 assert(get_irn_link(n));
94 /* ((scc_info *)get_irn_link(n))->in_stack = false; */
95 ((scc_info *)n->link)->in_stack = false;
99 irn_is_in_stack (ir_node *n) {
100 assert(get_irn_link(n));
102 /* return ((scc_info *)get_irn_link(n))->in_stack; */
103 return ((scc_info *)n->link)->in_stack;
107 set_irn_uplink (ir_node *n, int uplink) {
108 assert(get_irn_link(n));
110 /* ((scc_info *)get_irn_link(n))->uplink = uplink; */
111 ((scc_info *)n->link)->uplink = uplink;
115 get_irn_uplink (ir_node *n) {
116 assert(get_irn_link(n));
117 /* from fast to slow */
118 /* return ((scc_info *)get_irn_link(n))->uplink; */
119 return ((scc_info *)n->link)->uplink;
123 set_irn_dfn (ir_node *n, int dfn) {
124 assert(get_irn_link(n));
126 /* ((scc_info *)get_irn_link(n))->dfn = dfn; */
127 ((scc_info *)n->link)->dfn = dfn;
131 get_irn_dfn (ir_node *n) {
132 assert(get_irn_link(n));
134 /* return ((scc_info *)get_irn_link(n))->dfn; */
135 return ((scc_info *)n->link)->dfn;
140 set_irn_loop (ir_node *n, ir_loop* loop) {
144 /* Uses temporary information to get the loop */
146 get_irn_loop (ir_node *n) {
152 static ir_loop *find_nodes_loop (ir_node *n, ir_loop *l) {
156 /* Test whether n is contained in this loop. */
157 for (i = 0; i < get_loop_n_nodes(l); i++)
158 if (n == get_loop_node(l, i)) return l;
160 /* Is this a leave in the loop tree? If so loop not found. */
161 if (get_loop_n_sons(l) == 0) return NULL;
163 /* Else descend in the loop tree. */
164 for (i = 0; i < get_loop_n_sons(l); i++) {
165 res = find_nodes_loop(n, get_loop_son(l, i));
171 /* @@@ temporary implementation, costly!!! */
172 ir_loop * get_irn_loop(ir_node *n) {
173 ir_loop *l = get_irg_loop(current_ir_graph);
174 l = find_nodes_loop(n, l);
179 /**********************************************************************/
181 /**********************************************************************/
183 static ir_node **stack = NULL;
184 static int tos = 0; /* top of stack */
186 static INLINE void init_stack(void) {
188 ARR_RESIZE (ir_node *, stack, 1000);
190 stack = NEW_ARR_F (ir_node *, 1000);
196 static INLINE void free_stack(void) {
208 if (tos == ARR_LEN (stack)) {
209 int nlen = ARR_LEN (stack) * 2;
210 ARR_RESIZE (ir_node *, stack, nlen);
213 mark_irn_in_stack(n);
216 static INLINE ir_node *
219 ir_node *n = stack[--tos];
220 mark_irn_not_in_stack(n);
224 /* The nodes up to n belong to the current loop.
225 Removes them from the stack and adds them to the current loop. */
227 pop_scc_to_loop (ir_node *n)
235 //printf(" dfn: %d, upl %d upl-new %d ", get_irn_dfn(m), get_irn_uplink(m), loop_node_cnt+1); DDMN(m);
238 set_irn_dfn(m, loop_node_cnt);
239 add_loop_node(current_loop, m);
240 set_irn_loop(m, current_loop);
242 /* if (m==n) break;*/
245 /* i might be bigger than 1 for dead (and that's why bad) loops */
247 printf("Mehr als eine Iteration!!!!!!!!!!!!!!!!!!!!!!!!!!!!11111\n");
251 /* GL ??? my last son is my grandson??? Removes loops with no
252 ir_nodes in them. Such loops have only another loop as son. (Why
253 can't they have two loops as sons? Does it never get that far? ) */
254 static void close_loop (ir_loop *l)
256 int last = get_loop_n_elements(l) - 1;
257 loop_element lelement = get_loop_element(l, last);
258 ir_loop *last_son = lelement.son;
260 if (get_kind(last_son) == k_ir_loop &&
261 get_loop_n_elements(last_son) == 1)
265 lelement = get_loop_element(last_son, 0);
267 if(get_kind(gson) == k_ir_loop)
269 loop_element new_last_son;
271 gson -> outer_loop = l;
272 new_last_son.son = gson;
273 l -> children[last] = new_last_son;
280 /* Removes and unmarks all nodes up to n from the stack.
281 The nodes must be visited once more to assign them to a scc. */
283 pop_scc_unmark_visit (ir_node *n)
289 set_irn_visited(m, 0);
293 /**********************************************************************/
294 /* The loop datastructure. **/
295 /**********************************************************************/
297 /* Allocates a new loop as son of current_loop. Sets current_loop
298 to the new loop and returns the father. */
299 ir_loop *new_loop (void) {
300 ir_loop *father, *son;
302 father = current_loop;
304 son = (ir_loop *) obstack_alloc (outermost_ir_graph->obst, sizeof (ir_loop));
305 memset (son, 0, sizeof (ir_loop));
306 son->kind = k_ir_loop;
307 son->children = NEW_ARR_F (loop_element, 0);
311 son->outer_loop = father;
312 add_loop_son(father, son);
313 son->depth = father->depth+1;
314 } else { /* The root loop */
315 son->outer_loop = son;
320 son->loop_nr = get_irp_new_node_nr();
329 /* Finishes the datastructures, copies the arrays to the obstack
331 A. Schoesser: Caution: loop -> sons is gone. */
332 static void mature_loop (ir_loop *loop) {
335 new_sons = NEW_ARR_D (ir_loop *, current_ir_graph->obst, ARR_LEN(loop->sons));
336 memcpy (new_sons, loop->sons, sizeof (ir_loop *) * ARR_LEN(loop->sons));
337 DEL_ARR_F(loop->sons);
338 loop->sons = new_sons;
342 /* Returns outer loop, itself if outermost. */
343 ir_loop *get_loop_outer_loop (ir_loop *loop) {
344 assert(loop && loop->kind == k_ir_loop);
345 return loop->outer_loop;
348 /* Returns nesting depth of this loop */
349 int get_loop_depth (ir_loop *loop) {
350 assert(loop); assert(loop->kind == k_ir_loop);
354 /* Returns the number of inner loops */
355 int get_loop_n_sons (ir_loop *loop) {
356 assert(loop && loop->kind == k_ir_loop);
357 return(loop -> n_sons);
360 /* Returns the pos`th loop_node-child *
361 * TODO: This method isn`t very efficient ! *
362 * Returns NULL if there isnt`t a pos`th loop_node */
363 ir_loop *get_loop_son (ir_loop *loop, int pos) {
364 int child_nr = 0, loop_nr = -1;
366 assert(loop && loop->kind == k_ir_loop);
367 while(child_nr < ARR_LEN(loop->children))
369 if(*(loop -> children[child_nr].kind) == k_ir_loop)
372 return(loop -> children[child_nr].son);
378 /* Use EXCLUSIVELY this function to add sons, otherwise the loop->n_sons
382 add_loop_son(ir_loop *loop, ir_loop *son) {
385 assert(loop && loop->kind == k_ir_loop);
386 assert(get_kind(son) == k_ir_loop);
387 ARR_APP1 (loop_element, loop->children, lson);
391 /* Returns the number of nodes in the loop */
392 int get_loop_n_nodes (ir_loop *loop) {
393 assert(loop); assert(loop->kind == k_ir_loop);
394 return loop -> n_nodes;
395 /* return ARR_LEN(loop->nodes); */
398 /* Returns the pos`th ir_node-child *
399 * TODO: This method isn`t very efficient ! *
400 * Returns NULL if there isnt`t a pos`th ir_node */
401 ir_node *get_loop_node (ir_loop *loop, int pos) {
402 int child_nr, node_nr = -1;
404 assert(loop && loop->kind == k_ir_loop);
405 assert(pos < get_loop_n_nodes(loop));
407 for (child_nr = 0; child_nr < ARR_LEN(loop->children); child_nr++) {
408 if(*(loop -> children[child_nr].kind) == k_ir_node)
411 return(loop -> children[child_nr].node);
414 printf("pos: %d\n", pos);
415 assert(0 && "no child at pos found");
419 /* Use EXCLUSIVELY this function to add nodes, otherwise the loop->n_nodes
423 add_loop_node(ir_loop *loop, ir_node *n) {
426 assert(loop && loop->kind == k_ir_loop);
427 assert(get_kind(n) == k_ir_node || get_kind(n) == k_ir_graph); /* used in callgraph.c */
428 ARR_APP1 (loop_element, loop->children, ln);
432 /** Returns the number of elements contained in loop. */
433 int get_loop_n_elements (ir_loop *loop) {
434 assert(loop && loop->kind == k_ir_loop);
435 return(ARR_LEN(loop->children));
439 Returns the pos`th loop element.
440 This may be a loop_node or a ir_node. The caller of this function has
441 to check the *(loop_element.kind) field for "k_ir_node" or "k_ir_loop"
442 and then select the apropriate "loop_element.node" or "loop_element.son".
445 loop_element get_loop_element (ir_loop *loop, int pos) {
446 assert(loop && loop->kind == k_ir_loop && pos < ARR_LEN(loop->children));
448 return(loop -> children[pos]);
451 int get_loop_element_pos(ir_loop *loop, void *le) {
453 assert(loop && loop->kind == k_ir_loop);
455 for (i = 0; i < get_loop_n_elements(loop); i++)
456 if (get_loop_element(loop, i).node == le) return i;
460 int get_loop_loop_nr(ir_loop *loop) {
461 assert(loop && loop->kind == k_ir_loop);
463 return loop->loop_nr;
470 /** A field to connect additional information to a loop. Only valid
471 if libfirm_debug is set. */
472 void set_loop_link (ir_loop *loop, void *link) {
473 assert(loop && loop->kind == k_ir_loop);
478 void *get_loop_link (const ir_loop *loop) {
479 assert(loop && loop->kind == k_ir_loop);
487 /* The outermost loop is remarked in the surrounding graph. */
488 void set_irg_loop(ir_graph *irg, ir_loop *loop) {
492 ir_loop *get_irg_loop(ir_graph *irg) {
498 /**********************************************************************/
499 /* Constructing and destructing the loop/backedge information. **/
500 /**********************************************************************/
502 /* Initialization steps. **********************************************/
505 init_node (ir_node *n, void *env) {
506 set_irn_link (n, new_scc_info());
509 /* Also init nodes not visible in intraproc_view. */
510 /* @@@ init_node is called for too many nodes -- this wastes memory!.
511 The mem is not lost as its on the obstack. */
512 if (get_irn_op(n) == op_Filter) {
513 for (i = 0; i < get_Filter_n_cg_preds(n); i++)
514 init_node(get_Filter_cg_pred(n, i), NULL);
516 if (get_irn_op(n) == op_Block) {
517 for (i = 0; i < get_Block_cg_n_cfgpreds(n); i++) {
518 init_node(get_Block_cg_cfgpred(n, i), NULL);
521 /* The following pattern matches only after a call from above pattern. */
522 if ((get_irn_op(n) == op_Proj) /*&& (get_Proj_proj(n) == 0)*/) {
523 /* @@@ init_node is called for every proj -- this wastes memory!.
524 The mem is not lost as its on the obstack. */
525 ir_node *cb = get_Proj_pred(n);
526 if ((get_irn_op(cb) == op_CallBegin) ||
527 (get_irn_op(cb) == op_EndReg) ||
528 (get_irn_op(cb) == op_EndExcept)) {
530 init_node(get_nodes_block(cb), NULL);
537 init_scc_common (void) {
544 init_scc (ir_graph *irg) {
546 irg_walk_graph (irg, init_node, NULL, NULL);
548 irg_walk (irg, link_to_reg_end, NULL, NULL);
555 cg_walk (init_node, NULL, NULL);
557 #if EXPERIMENTAL_LOOP_TREE
558 cg_walk (link_to_reg_end, NULL, NULL);
562 /* Condition for breaking the recursion. */
563 static bool is_outermost_Start(ir_node *n) {
564 /* Test whether this is the outermost Start node. If so
565 recursion must end. */
566 if ((get_irn_op(n) == op_Block) &&
567 (get_Block_n_cfgpreds(n) == 1) &&
568 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Start) &&
569 (get_nodes_block(skip_Proj(get_Block_cfgpred(n, 0))) == n)) {
573 /* @@@ Bad condition:
574 not possible in interprocedural view as outermost_graph is
575 not necessarily the only with a dead-end start block.
576 Besides current_ir_graph is not set properly. */
577 if ((get_irn_op(n) == op_Block) &&
578 (n == get_irg_start_block(current_ir_graph))) {
579 if ((!interprocedural_view) ||
580 (current_ir_graph == outermost_ir_graph))
587 /* When to walk from nodes to blocks. Only for Control flow operations? */
589 get_start_index(ir_node *n) {
590 #undef BLOCK_BEFORE_NODE
591 #define BLOCK_BEFORE_NODE 1
593 #if BLOCK_BEFORE_NODE
595 /* This version assures, that all nodes are ordered absolutely. This allows
596 to undef all nodes in the heap analysis if the block is false, which means
598 I.e., with this code, the order on the loop tree is correct. But a (single)
599 test showed the loop tree is deeper. */
600 if (get_irn_op(n) == op_Phi ||
601 get_irn_op(n) == op_Block ||
602 (get_irn_op(n) == op_Filter && interprocedural_view) ||
603 (get_irg_pinned(get_irn_irg(n)) == op_pin_state_floats &&
604 get_op_pinned(get_irn_op(n)) == op_pin_state_floats))
605 // Here we could test for backedge at -1 which is illegal
612 /* This version causes deeper loop trees (at least we verified this
614 But it guarantees that Blocks are analysed before nodes contained in the
615 block. If so, we can set the value to undef if the block is not \
617 if (is_cfop(n) || is_fragile_op(n) || get_irn_op(n) == op_Start)
627 static void test(ir_node *pred, ir_node *root, ir_node *this) {
629 if (get_irn_uplink(pred) >= get_irn_uplink(root)) return;
631 printf("this: %d ", get_irn_uplink(this)); DDMN(this);
632 printf("pred: %d ", get_irn_uplink(pred)); DDMN(pred);
633 printf("root: %d ", get_irn_uplink(root)); DDMN(root);
635 printf("tos: %d\n", tos);
637 for (i = tos; i >= 0; i--) {
638 ir_node *n = stack[i];
640 printf(" uplink: %d, pos: %d ", get_irn_uplink(n), i); DDMN(n);
645 /* Test for legal loop header: Block, Phi, ... */
646 INLINE static bool is_possible_loop_head(ir_node *n) {
647 ir_op *op = get_irn_op(n);
648 return ((op == op_Block) ||
650 ((op == op_Filter) && interprocedural_view));
653 /* Returns true if n is a loop header, i.e., it is a Block, Phi
654 or Filter node and has predecessors within the loop and out
656 @arg root: only needed for assertion. */
658 is_head (ir_node *n, ir_node *root)
661 int some_outof_loop = 0, some_in_loop = 0;
663 /* Test for legal loop header: Block, Phi, ... */
664 if (!is_possible_loop_head(n))
667 if (!is_outermost_Start(n)) {
668 arity = get_irn_arity(n);
669 for (i = get_start_index(n); i < arity; i++) {
670 ir_node *pred = get_irn_n(n, i);
672 if (is_backedge(n, i)) continue;
673 if (!irn_is_in_stack(pred)) {
676 if(get_irn_uplink(pred) < get_irn_uplink(root)) {
677 DDMN(n); DDMN(pred); DDMN(root);
678 assert(get_irn_uplink(pred) >= get_irn_uplink(root));
684 return some_outof_loop && some_in_loop;
687 /* Returns true if n is possible loop head of an endless loop.
688 I.e., it is a Block, Phi or Filter node and has only predecessors
690 @arg root: only needed for assertion. */
692 is_endless_head (ir_node *n, ir_node *root)
695 int some_outof_loop = 0, some_in_loop = 0;
697 /* Test for legal loop header: Block, Phi, ... */
698 if (!is_possible_loop_head(n))
701 if (!is_outermost_Start(n)) {
702 arity = get_irn_arity(n);
703 for (i = get_start_index(n); i < arity; i++) {
704 ir_node *pred = get_irn_n(n, i);
706 if (is_backedge(n, i)) { continue; }
707 if (!irn_is_in_stack(pred)) {
708 some_outof_loop = 1; //printf(" some out of loop ");
710 if(get_irn_uplink(pred) < get_irn_uplink(root)) {
711 DDMN(pred); DDMN(root);
712 assert(get_irn_uplink(pred) >= get_irn_uplink(root));
718 return !some_outof_loop && some_in_loop;
721 /* Returns index of the predecessor with the smallest dfn number
722 greater-equal than limit. */
724 smallest_dfn_pred (ir_node *n, int limit)
726 int i, index = -2, min = -1;
728 if (!is_outermost_Start(n)) {
729 int arity = get_irn_arity(n);
730 for (i = get_start_index(n); i < arity; i++) {
731 ir_node *pred = get_irn_n(n, i);
733 if (is_backedge(n, i) || !irn_is_in_stack(pred)) continue;
734 if (get_irn_dfn(pred) >= limit && (min == -1 || get_irn_dfn(pred) < min)) {
736 min = get_irn_dfn(pred);
743 /* Returns index of the predecessor with the largest dfn number. */
745 largest_dfn_pred (ir_node *n)
747 int i, index = -2, max = -1;
749 if (!is_outermost_Start(n)) {
750 int arity = get_irn_arity(n);
751 for (i = get_start_index(n); i < arity; i++) {
752 ir_node *pred = get_irn_n(n, i);
753 if (is_backedge (n, i) || !irn_is_in_stack(pred)) continue;
754 if (get_irn_dfn(pred) > max) {
756 max = get_irn_dfn(pred);
763 /** Searches the stack for possible loop heads. Tests these for backedges.
764 If it finds a head with an unmarked backedge it marks this edge and
765 returns the tail of the loop.
766 If it finds no backedge returns NULL.
767 ("disable_backedge" in fiasco)
769 * @param n A node where uplink == dfn.
773 find_tail (ir_node *n) {
775 int i, res_index = -2;
778 if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
780 m = stack[tos-1]; /* tos = top of stack */
781 if (is_head (m, n)) {
782 res_index = smallest_dfn_pred(m, 0);
783 if ((res_index == -2) && /* no smallest dfn pred found. */
787 if (m == n) return NULL; // Is this to catch Phi - self loops?
788 for (i = tos-2; i >= 0; --i) {
791 if (is_head (m, n)) {
792 res_index = smallest_dfn_pred (m, get_irn_dfn(m) + 1);
793 if (res_index == -2) /* no smallest dfn pred found. */
794 res_index = largest_dfn_pred (m);
796 if ((m == n) && (res_index == -2)) { /* dont walk past loop head. */
802 /* We should not walk past our selves on the stack: The upcoming nodes
803 are not in this loop. We assume a loop not reachable from Start. */
812 /* A dead loop not reachable from Start. */
813 for (i = tos-2; i >= 0; --i) {
815 if (is_endless_head (m, n)) {
816 res_index = smallest_dfn_pred (m, get_irn_dfn(m) + 1);
817 if (res_index == -2) /* no smallest dfn pred found. */
818 res_index = largest_dfn_pred (m);
821 if (m == n) { break; } /* It's not an unreachable loop, either. */
823 //assert(0 && "no head found on stack");
827 if (res_index <= -2) {
828 /* It's a completely bad loop: without Phi/Block nodes that can
829 be a head. I.e., the code is "dying". We break the loop by
830 setting Bad nodes. */
831 int arity = get_irn_arity(n);
832 for (i = -1; i < arity; ++i) {
833 set_irn_n(n, i, get_irg_bad(get_irn_irg(n)));
837 assert (res_index > -2);
839 set_backedge (m, res_index);
840 return is_outermost_Start(n) ? NULL : get_irn_n(m, res_index);
844 #if EXPERIMENTAL_LOOP_TREE
846 /* ----------------------------------------------------------------
847 AS: This is experimantal code to build loop trees suitable for
848 the heap analysis. Does not work correctly right now... :-(
851 Search in stack for the corresponding first Call-End-ProjX that
852 corresponds to one of the control flow predecessors of the given
853 block, that is the possible callers.
854 returns: the control predecessor to chose\
855 or -1 if no corresponding Call-End-Node could be found
857 - -------------------------------------------------------------- */
859 int search_endproj_in_stack(ir_node *start_block)
862 assert(is_Block(start_block));
863 for(i = tos - 1; i >= 0; --i)
866 if(get_irn_op(stack[i]) == op_Proj && get_irn_mode(stack[i]) == mode_X &&
867 get_irn_op(get_irn_n(stack[i], 0)) == op_EndReg)
869 printf("FOUND PROJ!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
870 ir_node *end_projx = stack[i];
872 int arity = get_irn_arity(start_block);
873 for(j = 0; j < arity; j++)
875 ir_node *begin_projx = get_Block_cfgpred(get_irg_start_block(get_irn_irg(end_projx)),
876 get_Proj_proj(end_projx));
878 if(get_irn_n(start_block, j) == begin_projx)
880 printf("FOUND IT!!!!!!!!!!!!!!!!!!\n");
890 static pmap *projx_link = NULL;
892 void link_to_reg_end (ir_node *n, void *env) {
893 if(get_irn_op(n) == op_Proj &&
894 get_irn_mode(n) == mode_X &&
895 get_irn_op(get_irn_n(n, 0)) == op_EndReg) {
896 /* Reg End Projx -> Find the CallBegin Projx and hash it */
897 ir_node *end_projx = n;
898 ir_node *begin_projx = get_Block_cfgpred(get_irg_start_block(get_irn_irg(end_projx)),
899 get_Proj_proj(end_projx));
900 printf("Linked the following ProjxNodes:\n");
903 set_projx_link(begin_projx, end_projx);
907 void set_projx_link(ir_node *cb_projx, ir_node *end_projx)
909 if(projx_link == NULL)
910 projx_link = pmap_create();
911 pmap_insert(projx_link, (void *)cb_projx, (void *)end_projx);
914 ir_node *get_projx_link(ir_node *cb_projx)
916 return((ir_node *) pmap_get(projx_link, (void *)cb_projx));
922 is_outermost_loop(ir_loop *l) {
923 return l == get_loop_outer_loop(l);
927 /*-----------------------------------------------------------*
928 * The core algorithm. *
929 *-----------------------------------------------------------*/
932 static void scc (ir_node *n) {
934 if (irn_visited(n)) return;
937 /* Initialize the node */
938 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
939 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
940 set_irn_loop(n, NULL);
944 /* AS: get_start_index might return -1 for Control Flow Nodes, and thus a negative
945 array index would be passed to is_backedge(). But CFG Nodes dont't have a backedge array,
946 so is_backedge does not access array[-1] but correctly returns false! */
948 if (!is_outermost_Start(n)) {
949 int arity = get_irn_arity(n);
951 for (i = get_start_index(n); i < arity; i++) {
953 if (is_backedge(n, i)) continue;
954 m = get_irn_n(n, i); /* get_irn_ip_pred(n, i); */
955 /* if ((!m) || (get_irn_op(m) == op_Unknown)) continue; */
957 if (irn_is_in_stack(m)) {
958 /* Uplink of m is smaller if n->m is a backedge.
959 Propagate the uplink to mark the loop. */
960 if (get_irn_uplink(m) < get_irn_uplink(n))
961 set_irn_uplink(n, get_irn_uplink(m));
966 if (get_irn_dfn(n) == get_irn_uplink(n)) {
967 /* This condition holds for
968 1) the node with the incoming backedge.
969 That is: We found a loop!
970 2) Straight line code, because no uplink has been propagated, so the
971 uplink still is the same as the dfn.
973 But n might not be a proper loop head for the analysis. Proper loop
974 heads are Block and Phi nodes. find_tail searches the stack for
975 Block's and Phi's and takes those nodes as loop heads for the current
976 loop instead and marks the incoming edge as backedge. */
978 ir_node *tail = find_tail(n);
980 /* We have a loop, that is no straight line code,
981 because we found a loop head!
982 Next actions: Open a new loop on the loop tree and
983 try to find inner loops */
985 #if NO_LOOPS_WITHOUT_HEAD
986 /* This is an adaption of the algorithm from fiasco / optscc to
987 * avoid loops without Block or Phi as first node. This should
988 * severely reduce the number of evaluations of nodes to detect
989 * a fixpoint in the heap analysis.
990 * Further it avoids loops without firm nodes that cause errors
991 * in the heap analyses.
992 * But attention: don't do it for the outermost loop: This loop
993 * is not iterated. A first block can be a loop head in case of
994 * an endless recursion. */
998 if ((get_loop_n_elements(current_loop) > 0) || (is_outermost_loop(current_loop))) {
1006 ir_loop *l = new_loop();
1009 /* Remove the loop from the stack ... */
1010 pop_scc_unmark_visit (n);
1012 /* The current backedge has been marked, that is temporarily eliminated,
1013 by find tail. Start the scc algorithm
1014 anew on the subgraph thats left (the current loop without the backedge)
1015 in order to find more inner loops. */
1018 assert (irn_visited(n));
1019 #if NO_LOOPS_WITHOUT_HEAD
1026 /* No loop head was found, that is we have straightline code.
1027 Pop all nodes from the stack to the current loop. */
1033 static void my_scc (ir_node *n) {
1035 if (irn_visited(n)) return;
1036 mark_irn_visited(n);
1038 /* Initialize the node */
1039 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
1040 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
1041 set_irn_loop(n, NULL);
1045 /* AS: get_start_index might return -1 for Control Flow Nodes, and thus a negative
1046 array index would be passed to is_backedge(). But CFG Nodes dont't have a backedge array,
1047 so is_backedge does not access array[-1] but correctly returns false! */
1049 if (!is_outermost_Start(n)) {
1050 int arity = get_irn_arity(n);
1052 for (i = get_start_index(n); i < arity; i++) {
1054 if (is_backedge(n, i)) continue;
1055 m = get_irn_n(n, i); /* get_irn_ip_pred(n, i); */
1056 /* if ((!m) || (get_irn_op(m) == op_Unknown)) continue; */
1058 if (irn_is_in_stack(m)) {
1059 /* Uplink of m is smaller if n->m is a backedge.
1060 Propagate the uplink to mark the loop. */
1061 if (get_irn_uplink(m) < get_irn_uplink(n))
1062 set_irn_uplink(n, get_irn_uplink(m));
1067 if (get_irn_dfn(n) == get_irn_uplink(n)) {
1068 /* This condition holds for
1069 1) the node with the incoming backedge.
1070 That is: We found a loop!
1071 2) Straight line code, because no uplink has been propagated, so the
1072 uplink still is the same as the dfn.
1074 But n might not be a proper loop head for the analysis. Proper loop
1075 heads are Block and Phi nodes. find_tail searches the stack for
1076 Block's and Phi's and takes those nodes as loop heads for the current
1077 loop instead and marks the incoming edge as backedge. */
1079 ir_node *tail = find_tail(n);
1081 /* We have a loop, that is no straight line code,
1082 because we found a loop head!
1083 Next actions: Open a new loop on the loop tree and
1084 try to find inner loops */
1086 #if NO_LOOPS_WITHOUT_HEAD
1087 /* This is an adaption of the algorithm from fiasco / optscc to
1088 * avoid loops without Block or Phi as first node. This should
1089 * severely reduce the number of evaluations of nodes to detect
1090 * a fixpoint in the heap analysis.
1091 * Further it avoids loops without firm nodes that cause errors
1092 * in the heap analyses. */
1096 if ((get_loop_n_elements(current_loop) > 0) || (is_outermost_loop(current_loop))) {
1104 ir_loop *l = new_loop();
1107 /* Remove the loop from the stack ... */
1108 pop_scc_unmark_visit (n);
1110 /* The current backedge has been marked, that is temporarily eliminated,
1111 by find tail. Start the scc algorithm
1112 anew on the subgraph thats left (the current loop without the backedge)
1113 in order to find more inner loops. */
1116 assert (irn_visited(n));
1117 #if NO_LOOPS_WITHOUT_HEAD
1124 /* No loop head was found, that is we have straightline code.
1125 Pop all nodes from the stack to the current loop. */
1131 /* Constructs backedge information for irg. In interprocedural view constructs
1132 backedges for all methods called by irg, too. */
1133 void construct_backedges(ir_graph *irg) {
1134 ir_graph *rem = current_ir_graph;
1137 assert(!interprocedural_view &&
1138 "not implemented, use construct_ip_backedges");
1140 current_ir_graph = irg;
1141 outermost_ir_graph = irg;
1143 init_scc(current_ir_graph);
1145 current_loop = NULL;
1146 new_loop(); /* sets current_loop */
1147 head_rem = current_loop; /* Just for assertion */
1149 inc_irg_visited(current_ir_graph);
1151 scc(get_irg_end(current_ir_graph));
1153 assert(head_rem == current_loop);
1154 set_irg_loop(current_ir_graph, current_loop);
1155 set_irg_loopinfo_state(current_ir_graph, loopinfo_consistent);
1156 assert(get_irg_loop(current_ir_graph)->kind == k_ir_loop);
1158 irg->loops = current_loop;
1162 count_loop (the_loop, &count, &depth);
1166 current_ir_graph = rem;
1171 void construct_ip_backedges (void) {
1172 ir_graph *rem = current_ir_graph;
1173 int rem_ipv = interprocedural_view;
1176 outermost_ir_graph = get_irp_main_irg();
1180 current_loop = NULL;
1181 new_loop(); /* sets current_loop */
1182 interprocedural_view = 1;
1184 inc_max_irg_visited();
1185 for (i = 0; i < get_irp_n_irgs(); i++)
1186 set_irg_visited(get_irp_irg(i), get_max_irg_visited());
1188 for (i = 0; i < get_irp_n_irgs(); i++) {
1190 current_ir_graph = get_irp_irg(i);
1191 /* Find real entry points */
1192 sb = get_irg_start_block(current_ir_graph);
1193 if ((get_Block_n_cfgpreds(sb) > 1) ||
1194 (get_nodes_block(get_Block_cfgpred(sb, 0)) != sb)) continue;
1195 /* Compute scc for this graph */
1196 outermost_ir_graph = current_ir_graph;
1197 set_irg_visited(outermost_ir_graph, get_max_irg_visited());
1198 scc(get_irg_end(current_ir_graph));
1199 for (j = 0; j < get_End_n_keepalives(get_irg_end(outermost_ir_graph)); j++)
1200 scc(get_End_keepalive(get_irg_end(outermost_ir_graph), j));
1203 set_irg_loop(outermost_ir_graph, current_loop);
1204 set_irg_loopinfo_state(current_ir_graph, loopinfo_ip_consistent);
1205 assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
1207 current_ir_graph = rem;
1208 interprocedural_view = rem_ipv;
1211 void construct_ip_backedges (void) {
1212 ir_graph *rem = current_ir_graph;
1213 int rem_ipv = interprocedural_view;
1216 assert(get_irp_ip_view_state() == ip_view_valid);
1218 outermost_ir_graph = get_irp_main_irg();
1222 current_loop = NULL;
1223 new_loop(); /* sets current_loop */
1224 interprocedural_view = 1;
1226 inc_max_irg_visited();
1227 for (i = 0; i < get_irp_n_irgs(); i++)
1228 set_irg_visited(get_irp_irg(i), get_max_irg_visited());
1230 /** We have to start the walk at the same nodes as cg_walk. **/
1231 /* Walk starting at unreachable procedures. Only these
1232 * have End blocks visible in interprocedural view. */
1233 for (i = 0; i < get_irp_n_irgs(); i++) {
1235 current_ir_graph = get_irp_irg(i);
1237 sb = get_irg_start_block(current_ir_graph);
1239 if ((get_Block_n_cfgpreds(sb) > 1) ||
1240 (get_nodes_block(get_Block_cfgpred(sb, 0)) != sb)) continue;
1242 scc(get_irg_end(current_ir_graph));
1245 /* Check whether we walked all procedures: there could be procedures
1246 with cyclic calls but no call from the outside. */
1247 for (i = 0; i < get_irp_n_irgs(); i++) {
1249 current_ir_graph = get_irp_irg(i);
1251 /* Test start block: if inner procedure end and end block are not
1252 * visible and therefore not marked. */
1253 sb = get_irg_start_block(current_ir_graph);
1254 if (get_irn_visited(sb) < get_irg_visited(current_ir_graph)) scc(sb);
1257 /* Walk all endless loops in inner procedures.
1258 * We recognize an inner procedure if the End node is not visited. */
1259 for (i = 0; i < get_irp_n_irgs(); i++) {
1261 current_ir_graph = get_irp_irg(i);
1263 e = get_irg_end(current_ir_graph);
1264 if (get_irn_visited(e) < get_irg_visited(current_ir_graph)) {
1266 /* Don't visit the End node. */
1267 for (j = 0; j < get_End_n_keepalives(e); j++) scc(get_End_keepalive(e, j));
1271 set_irg_loop(outermost_ir_graph, current_loop);
1272 set_irg_loopinfo_state(current_ir_graph, loopinfo_ip_consistent);
1273 assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
1275 current_ir_graph = rem;
1276 interprocedural_view = rem_ipv;
1279 void my_construct_ip_backedges (void) {
1280 ir_graph *rem = current_ir_graph;
1281 int rem_ipv = interprocedural_view;
1284 assert(get_irp_ip_view_state() == ip_view_valid);
1286 outermost_ir_graph = get_irp_main_irg();
1290 current_loop = NULL;
1291 new_loop(); /* sets current_loop */
1292 interprocedural_view = 1;
1294 inc_max_irg_visited();
1295 for (i = 0; i < get_irp_n_irgs(); i++)
1296 set_irg_visited(get_irp_irg(i), get_max_irg_visited());
1298 /** We have to start the walk at the same nodes as cg_walk. **/
1299 /* Walk starting at unreachable procedures. Only these
1300 * have End blocks visible in interprocedural view. */
1301 for (i = 0; i < get_irp_n_irgs(); i++) {
1303 current_ir_graph = get_irp_irg(i);
1305 sb = get_irg_start_block(current_ir_graph);
1307 if ((get_Block_n_cfgpreds(sb) > 1) ||
1308 (get_nodes_block(get_Block_cfgpred(sb, 0)) != sb)) continue;
1310 my_scc(get_irg_end(current_ir_graph));
1313 /* Check whether we walked all procedures: there could be procedures
1314 with cyclic calls but no call from the outside. */
1315 for (i = 0; i < get_irp_n_irgs(); i++) {
1317 current_ir_graph = get_irp_irg(i);
1319 /* Test start block: if inner procedure end and end block are not
1320 * visible and therefore not marked. */
1321 sb = get_irg_start_block(current_ir_graph);
1322 if (get_irn_visited(sb) < get_irg_visited(current_ir_graph)) scc(sb);
1325 /* Walk all endless loops in inner procedures.
1326 * We recognize an inner procedure if the End node is not visited. */
1327 for (i = 0; i < get_irp_n_irgs(); i++) {
1329 current_ir_graph = get_irp_irg(i);
1331 e = get_irg_end(current_ir_graph);
1332 if (get_irn_visited(e) < get_irg_visited(current_ir_graph)) {
1334 /* Don't visit the End node. */
1335 for (j = 0; j < get_End_n_keepalives(e); j++) scc(get_End_keepalive(e, j));
1339 set_irg_loop(outermost_ir_graph, current_loop);
1340 set_irg_loopinfo_state(current_ir_graph, loopinfo_ip_consistent);
1341 assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
1343 current_ir_graph = rem;
1344 interprocedural_view = rem_ipv;
1347 static void reset_backedges(ir_node *n) {
1348 if (is_possible_loop_head(n)) {
1349 int rem = interprocedural_view;
1350 interprocedural_view = 1;
1352 interprocedural_view = 0;
1354 interprocedural_view = rem;
1360 static void loop_reset_backedges(ir_loop *l) {
1362 reset_backedges(get_loop_node(l, 0));
1363 for (i = 0; i < get_loop_n_nodes(l); ++i)
1364 set_irn_loop(get_loop_node(l, i), NULL);
1365 for (i = 0; i < get_loop_n_sons(l); ++i) {
1366 loop_reset_backedges(get_loop_son(l, i));
1371 static void loop_reset_node(ir_node *n, void *env) {
1372 set_irn_loop(n, NULL);
1377 /** Removes all loop information.
1378 Resets all backedges */
1379 void free_loop_information(ir_graph *irg) {
1380 /* We can not use this recursion, as the loop might contain
1381 illegal nodes by now. Why else would we throw away the
1383 if (get_irg_loop(irg)) loop_reset_backedges(get_irg_loop(irg));
1385 irg_walk_graph(irg, loop_reset_node, NULL, NULL);
1386 set_irg_loop(irg, NULL);
1387 set_irg_loopinfo_state(current_ir_graph, loopinfo_none);
1388 /* We cannot free the loop nodes, they are on the obstack. */
1392 void free_all_loop_information (void) {
1394 int rem = interprocedural_view;
1395 interprocedural_view = 1; /* To visit all filter nodes */
1396 for (i = 0; i < get_irp_n_irgs(); i++) {
1397 free_loop_information(get_irp_irg(i));
1399 interprocedural_view = rem;
1406 /* Debug stuff *************************************************/
1408 static int test_loop_node(ir_loop *l) {
1409 int i, has_node = 0, found_problem = 0;
1412 assert(l && l->kind == k_ir_loop);
1414 if (get_loop_n_elements(l) == 0) {
1415 printf(" Loop completely empty! "); DDML(l);
1417 dump_loop(l, "-ha");
1420 le = get_loop_element(l, 0);
1421 if (*(le.kind) != k_ir_node) {
1422 assert(le.kind && *(le.kind) == k_ir_loop);
1423 printf(" First loop element is not a node! "); DDML(l);
1424 printf(" "); DDML(le.son);
1427 dump_loop(l, "-ha");
1430 if ((*(le.kind) == k_ir_node) && !is_possible_loop_head(le.node)) {
1431 printf(" Wrong node as head! "); DDML(l);
1432 printf(" "); DDMN(le.node);
1434 dump_loop(l, "-ha");
1437 if ((get_loop_depth(l) != 0) &&
1438 (*(le.kind) == k_ir_node) && !has_backedges(le.node)) {
1439 printf(" Loop head has no backedges! "); DDML(l);
1440 printf(" "); DDMN(le.node);
1442 dump_loop(l, "-ha");
1447 for (i = 0; i < get_loop_n_elements(l); ++i) {
1448 le = get_loop_element(l, i);
1449 if (*(le.kind) == k_ir_node)
1452 if (test_loop_node(le.son)) found_problem = 1;
1455 if (has_node == 0) {
1456 printf(" Loop has no firm node! "); DDML(l);
1458 dump_loop(l, "-ha");
1461 if (get_loop_loop_nr(l) == 11819)
1462 dump_loop(l, "-ha-debug");
1464 return found_problem;
1467 /** Prints all loop nodes that
1468 * - do not have any firm nodes, only loop sons
1469 * - the header is not a Phi, Block or Filter.
1471 void find_strange_loop_nodes(ir_loop *l) {
1472 int found_problem = 0;
1473 printf("\nTesting loop "); DDML(l);
1474 found_problem = test_loop_node(l);
1475 printf("Finished Test\n\n");
1476 if (found_problem) exit(0);