2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Compute the strongly connected regions and build
23 * backedge/loop datastructures.
24 * A variation on the Tarjan algorithm. See also [Trapp:99],
26 * @author Goetz Lindenmaier
38 #include "irgraph_t.h"
45 /* A variant of the loop tree that avoids loops without head.
46 This reduces the depth of the loop tree. */
47 #define NO_LOOPS_WITHOUT_HEAD 1
49 /** The outermost graph the scc is computed for. */
50 static ir_graph *outermost_ir_graph;
51 /** Current loop construction is working on. */
52 static ir_loop *current_loop;
53 /** Counts the number of allocated loop nodes.
54 * Each loop node gets a unique number.
55 * @todo What for? ev. remove.
57 static int loop_node_cnt = 0;
58 /** Counter to generate depth first numbering of visited nodes. */
59 static int current_dfn = 1;
61 static int max_loop_depth = 0;
63 void link_to_reg_end(ir_node *n, void *env);
64 void set_projx_link(ir_node *cb_projx, ir_node *end_projx);
65 ir_node *get_projx_link(ir_node *cb_projx);
67 /**********************************************************************/
68 /* Node attributes **/
69 /**********************************************************************/
71 /**********************************************************************/
72 /* Node attributes needed for the construction. **/
73 /**********************************************************************/
75 typedef struct scc_info {
76 int in_stack; /**< Marks whether node is on the stack. */
77 int dfn; /**< Depth first search number. */
78 int uplink; /**< dfn number of ancestor. */
79 /* ir_loop *loop; *//* Refers to the containing loop. */
81 struct section *section;
88 * Allocates a new SCC info on the given obstack.
90 static inline scc_info *new_scc_info(struct obstack *obst)
92 return OALLOCZ(obst, scc_info);
96 * Mark node n being on the SCC stack.
98 static inline void mark_irn_in_stack(ir_node *n)
100 scc_info *scc = get_irn_link(n);
106 * Mark node n NOT being on the SCC stack.
108 static inline void mark_irn_not_in_stack(ir_node *n)
110 scc_info *scc = get_irn_link(n);
116 * Checks if a node is on the SCC stack.
118 static inline int irn_is_in_stack(ir_node *n)
120 scc_info *scc = get_irn_link(n);
122 return scc->in_stack;
126 * Sets the uplink number for a node.
128 static inline void set_irn_uplink(ir_node *n, int uplink)
130 scc_info *scc = get_irn_link(n);
132 scc->uplink = uplink;
136 * Returns the uplink number for a node.
138 static int get_irn_uplink(ir_node *n)
140 scc_info *scc = get_irn_link(n);
146 * Sets the depth-first-search number for a node.
148 static inline void set_irn_dfn(ir_node *n, int dfn)
150 scc_info *scc = get_irn_link(n);
156 * Returns the depth-first-search number of a node.
158 static int get_irn_dfn(ir_node *n)
160 scc_info *scc = get_irn_link(n);
166 static ir_loop *find_nodes_loop(ir_node *n, ir_loop *l)
171 /* Test whether n is contained in this loop. */
172 for (i = 0; i < get_loop_n_nodes(l); i++)
173 if (n == get_loop_node(l, i)) return l;
175 /* Is this a leave in the loop tree? If so loop not found. */
176 if (get_loop_n_sons(l) == 0) return NULL;
178 /* Else descend in the loop tree. */
179 for (i = 0; i < get_loop_n_sons(l); i++) {
180 res = find_nodes_loop(n, get_loop_son(l, i));
186 /* @@@ temporary implementation, costly!!! */
187 ir_loop * get_irn_loop(ir_node *n)
189 ir_loop *l = get_irg_loop(current_ir_graph);
190 l = find_nodes_loop(n, l);
195 /**********************************************************************/
197 /**********************************************************************/
199 static ir_node **stack = NULL;
200 static int tos = 0; /* top of stack */
203 * initializes the stack
205 static inline void init_stack(void)
208 ARR_RESIZE(ir_node *, stack, 1000);
210 stack = NEW_ARR_F(ir_node *, 1000);
218 static void finish_stack(void)
225 * push a node onto the stack
227 * @param n The node to push
229 static inline void push(ir_node *n)
231 if (tos == ARR_LEN(stack)) {
232 int nlen = ARR_LEN(stack) * 2;
233 ARR_RESIZE(ir_node *, stack, nlen);
236 mark_irn_in_stack(n);
240 * pop a node from the stack
242 * @return The topmost node
244 static inline ir_node *pop(void)
246 ir_node *n = stack[--tos];
247 mark_irn_not_in_stack(n);
252 * The nodes up to n belong to the current loop.
253 * Removes them from the stack and adds them to the current loop.
255 static inline void pop_scc_to_loop(ir_node *n)
263 set_irn_dfn(m, loop_node_cnt);
264 add_loop_node(current_loop, m);
265 set_irn_loop(m, current_loop);
269 /* GL ??? my last son is my grandson??? Removes loops with no
270 ir_nodes in them. Such loops have only another loop as son. (Why
271 can't they have two loops as sons? Does it never get that far? ) */
272 static void close_loop(ir_loop *l)
274 int last = get_loop_n_elements(l) - 1;
275 loop_element lelement = get_loop_element(l, last);
276 ir_loop *last_son = lelement.son;
278 if (get_kind(last_son) == k_ir_loop &&
279 get_loop_n_elements(last_son) == 1) {
282 lelement = get_loop_element(last_son, 0);
285 if (get_kind(gson) == k_ir_loop) {
286 loop_element new_last_son;
288 gson->outer_loop = l;
289 new_last_son.son = gson;
290 l->children[last] = new_last_son;
297 /* Removes and unmarks all nodes up to n from the stack.
298 The nodes must be visited once more to assign them to a scc. */
299 static inline void pop_scc_unmark_visit(ir_node *n)
305 set_irn_visited(m, 0);
309 /**********************************************************************/
310 /* The loop datastructure. **/
311 /**********************************************************************/
313 /* Allocates a new loop as son of current_loop. Sets current_loop
314 to the new loop and returns the father. */
315 static ir_loop *new_loop(void)
317 ir_loop *father = current_loop;
318 ir_loop *son = alloc_loop(father, outermost_ir_graph->obst);
320 if (son->depth > max_loop_depth) max_loop_depth = son->depth;
325 /**********************************************************************/
326 /* Constructing and destructing the loop/backedge information. **/
327 /**********************************************************************/
329 /* Initialization steps. **********************************************/
331 static inline void init_node(ir_node *n, void *env)
333 struct obstack *obst = env;
334 set_irn_link(n, new_scc_info(obst));
338 static inline void init_scc_common(void)
345 static inline void init_scc(ir_graph *irg, struct obstack *obst)
348 irg_walk_graph(irg, init_node, NULL, obst);
350 irg_walk (irg, link_to_reg_end, NULL, NULL);
354 static inline void finish_scc(void)
360 * Check weather a given node represents the outer most Start
361 * block. In intra-procedural view this is the start block of the
362 * current graph, in interprocedural view it is the start block
363 * of the outer most graph.
365 * @param n the node to check
367 * This is the condition for breaking the scc recursion.
369 static int is_outermost_Start(ir_node *n)
371 /* Test whether this is the outermost Start node. */
372 if (is_Block(n) && get_Block_n_cfgpreds(n) == 1) {
373 ir_node *pred = skip_Proj(get_Block_cfgpred(n, 0));
374 if (is_Start(pred) && get_nodes_block(pred) == n)
380 /* When to walk from nodes to blocks. Only for Control flow operations? */
381 static inline int get_start_index(ir_node *n)
383 #undef BLOCK_BEFORE_NODE
384 #define BLOCK_BEFORE_NODE 1
386 #if BLOCK_BEFORE_NODE
388 /* This version assures, that all nodes are ordered absolutely. This allows
389 to undef all nodes in the heap analysis if the block is false, which
391 I.e., with this code, the order on the loop tree is correct. But a
392 (single) test showed the loop tree is deeper. */
393 if (get_irn_op(n) == op_Phi ||
395 (get_irg_pinned(get_irn_irg(n)) == op_pin_state_floats &&
396 get_irn_pinned(n) == op_pin_state_floats))
397 // Here we could test for backedge at -1 which is illegal
404 /* This version causes deeper loop trees (at least we verified this
406 But it guarantees that Blocks are analysed before nodes contained in the
407 block. If so, we can set the value to undef if the block is not \
409 if (is_cfop(n) || is_fragile_op(n) || is_Start(n))
418 * Return non-zero if the given node is a legal loop header:
419 * Block, Phi, Filter.
421 * @param n the node to check
423 static inline int is_possible_loop_head(ir_node *n)
425 ir_op *op = get_irn_op(n);
426 return ((op == op_Block) ||
431 * Returns non-zero if n is a loop header, i.e., it is a Block, Phi
432 * or Filter node and has predecessors within the loop and out
435 * @param n the node to check
436 * @param root only needed for assertion.
438 static int is_head(ir_node *n, ir_node *root)
441 int some_outof_loop = 0, some_in_loop = 0;
443 /* Test for legal loop header: Block, Phi, ... */
444 if (!is_possible_loop_head(n))
447 if (!is_outermost_Start(n)) {
449 int uplink = get_irn_uplink(root);
453 arity = get_irn_arity(n);
454 for (i = get_start_index(n); i < arity; i++) {
456 if (is_backedge(n, i))
458 pred = get_irn_n(n, i);
459 if (! irn_is_in_stack(pred)) {
462 assert(get_irn_uplink(pred) >= uplink);
467 return some_outof_loop & some_in_loop;
471 * Returns non-zero if n is possible loop head of an endless loop.
472 * I.e., it is a Block, Phi or Filter node and has only predecessors
475 * @param n the node to check
476 * @param root only needed for assertion.
478 static int is_endless_head(ir_node *n, ir_node *root)
481 int none_outof_loop = 1, some_in_loop = 0;
483 /* Test for legal loop header: Block, Phi, ... */
484 if (!is_possible_loop_head(n))
487 if (!is_outermost_Start(n)) {
489 int uplink = get_irn_uplink(root);
493 arity = get_irn_arity(n);
494 for (i = get_start_index(n); i < arity; i++) {
496 if (is_backedge(n, i))
498 pred = get_irn_n(n, i);
499 if (!irn_is_in_stack(pred)) {
502 assert(get_irn_uplink(pred) >= uplink);
507 return none_outof_loop & some_in_loop;
510 /** Returns index of the predecessor with the smallest dfn number
511 greater-equal than limit. */
512 static int smallest_dfn_pred(ir_node *n, int limit)
514 int i, index = -2, min = -1;
516 if (!is_outermost_Start(n)) {
517 int arity = get_irn_arity(n);
518 for (i = get_start_index(n); i < arity; i++) {
519 ir_node *pred = get_irn_n(n, i);
520 if (is_backedge(n, i) || !irn_is_in_stack(pred))
522 if (get_irn_dfn(pred) >= limit && (min == -1 || get_irn_dfn(pred) < min)) {
524 min = get_irn_dfn(pred);
532 * Returns index of the predecessor with the largest dfn number.
534 static int largest_dfn_pred(ir_node *n)
536 int i, index = -2, max = -1;
538 if (!is_outermost_Start(n)) {
539 int arity = get_irn_arity(n);
540 for (i = get_start_index(n); i < arity; i++) {
541 ir_node *pred = get_irn_n(n, i);
542 if (is_backedge (n, i) || !irn_is_in_stack(pred))
544 if (get_irn_dfn(pred) > max) {
546 max = get_irn_dfn(pred);
554 * Searches the stack for possible loop heads. Tests these for backedges.
555 * If it finds a head with an unmarked backedge it marks this edge and
556 * returns the tail of the loop.
557 * If it finds no backedge returns NULL.
558 * ("disable_backedge" in fiasco)
560 * @param n A node where uplink == dfn.
562 static ir_node *find_tail(ir_node *n)
565 int i, res_index = -2;
568 if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
570 m = stack[tos-1]; /* tos = top of stack */
572 res_index = smallest_dfn_pred(m, 0);
573 if ((res_index == -2) && /* no smallest dfn pred found. */
577 if (m == n) return NULL; // Is this to catch Phi - self loops?
578 for (i = tos-2; i >= 0; --i) {
582 res_index = smallest_dfn_pred(m, get_irn_dfn(m) + 1);
583 if (res_index == -2) /* no smallest dfn pred found. */
584 res_index = largest_dfn_pred(m);
586 if ((m == n) && (res_index == -2)) { /* don't walk past loop head. */
592 /* We should not walk past our selves on the stack: The upcoming nodes
593 are not in this loop. We assume a loop not reachable from Start. */
601 /* A dead loop not reachable from Start. */
602 for (i = tos-2; i >= 0; --i) {
604 if (is_endless_head(m, n)) {
605 res_index = smallest_dfn_pred(m, get_irn_dfn(m) + 1);
606 if (res_index == -2) /* no smallest dfn pred found. */
607 res_index = largest_dfn_pred (m);
610 if (m == n) { break; } /* It's not an unreachable loop, either. */
612 //assert(0 && "no head found on stack");
616 if (res_index <= -2) {
617 /* It's a completely bad loop: without Phi/Block nodes that can
618 be a head. I.e., the code is "dying". We break the loop by
619 setting Bad nodes. */
620 int arity = get_irn_arity(n);
621 ir_node *bad = get_irg_bad(get_irn_irg(n));
622 for (i = -1; i < arity; ++i) {
623 set_irn_n(n, i, bad);
627 assert(res_index > -2);
629 set_backedge(m, res_index);
630 return is_outermost_Start(n) ? NULL : get_irn_n(m, res_index);
634 #if EXPERIMENTAL_LOOP_TREE
636 /* ----------------------------------------------------------------
637 AS: This is experimental code to build loop trees suitable for
638 the heap analysis. Does not work correctly right now... :-(
641 Search in stack for the corresponding first Call-End-ProjX that
642 corresponds to one of the control flow predecessors of the given
643 block, that is the possible callers.
644 returns: the control predecessor to chose\
645 or -1 if no corresponding Call-End-Node could be found
647 - -------------------------------------------------------------- */
649 int search_endproj_in_stack(ir_node *start_block)
652 assert(is_Block(start_block));
653 for (i = tos - 1; i >= 0; --i)
655 if (get_irn_op(stack[i]) == op_Proj && get_irn_mode(stack[i]) == mode_X &&
656 get_irn_op(get_irn_n(stack[i], 0)) == op_EndReg)
658 printf("FOUND PROJ!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
659 ir_node *end_projx = stack[i];
661 int arity = get_irn_arity(start_block);
662 for (j = 0; j < arity; j++)
664 ir_node *begin_projx = get_Block_cfgpred(get_irg_start_block(get_irn_irg(end_projx)),
665 get_Proj_proj(end_projx));
666 if (get_irn_n(start_block, j) == begin_projx)
668 printf("FOUND IT!!!!!!!!!!!!!!!!!!\n");
678 static pmap *projx_link = NULL;
680 void link_to_reg_end (ir_node *n, void *env)
682 if (get_irn_op(n) == op_Proj &&
683 get_irn_mode(n) == mode_X &&
684 get_irn_op(get_irn_n(n, 0)) == op_EndReg) {
685 /* Reg End Projx -> Find the CallBegin Projx and hash it */
686 ir_node *end_projx = n;
687 ir_node *begin_projx = get_Block_cfgpred(get_irg_start_block(get_irn_irg(end_projx)),
688 get_Proj_proj(end_projx));
689 set_projx_link(begin_projx, end_projx);
693 void set_projx_link(ir_node *cb_projx, ir_node *end_projx)
695 if (projx_link == NULL)
696 projx_link = pmap_create();
697 pmap_insert(projx_link, (void *)cb_projx, (void *)end_projx);
700 ir_node *get_projx_link(ir_node *cb_projx)
702 return((ir_node *) pmap_get(projx_link, (void *)cb_projx));
707 static inline int is_outermost_loop(ir_loop *l)
709 return l == get_loop_outer_loop(l);
713 /*-----------------------------------------------------------*
714 * The core algorithm. *
715 *-----------------------------------------------------------*/
718 * The core algorithm: Find strongly coupled components.
720 * @param n node to start
722 static void scc(ir_node *n)
724 if (irn_visited_else_mark(n))
727 /* Initialize the node */
728 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
729 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
730 set_irn_loop(n, NULL);
734 /* AS: get_start_index might return -1 for Control Flow Nodes, and thus a negative
735 array index would be passed to is_backedge(). But CFG Nodes dont't have a backedge array,
736 so is_backedge does not access array[-1] but correctly returns false! */
738 if (!is_outermost_Start(n)) {
739 int i, arity = get_irn_arity(n);
741 for (i = get_start_index(n); i < arity; ++i) {
743 if (is_backedge(n, i))
747 if (irn_is_in_stack(m)) {
748 /* Uplink of m is smaller if n->m is a backedge.
749 Propagate the uplink to mark the loop. */
750 if (get_irn_uplink(m) < get_irn_uplink(n))
751 set_irn_uplink(n, get_irn_uplink(m));
756 if (get_irn_dfn(n) == get_irn_uplink(n)) {
757 /* This condition holds for
758 1) the node with the incoming backedge.
759 That is: We found a loop!
760 2) Straight line code, because no uplink has been propagated, so the
761 uplink still is the same as the dfn.
763 But n might not be a proper loop head for the analysis. Proper loop
764 heads are Block and Phi nodes. find_tail() searches the stack for
765 Block's and Phi's and takes those nodes as loop heads for the current
766 loop instead and marks the incoming edge as backedge. */
768 ir_node *tail = find_tail(n);
770 /* We have a loop, that is no straight line code,
771 because we found a loop head!
772 Next actions: Open a new loop on the loop tree and
773 try to find inner loops */
775 #if NO_LOOPS_WITHOUT_HEAD
776 /* This is an adaption of the algorithm from fiasco / optscc to
777 * avoid loops without Block or Phi as first node. This should
778 * severely reduce the number of evaluations of nodes to detect
779 * a fixpoint in the heap analysis.
780 * Further it avoids loops without firm nodes that cause errors
781 * in the heap analyses.
782 * But attention: don't do it for the outermost loop: This loop
783 * is not iterated. A first block can be a loop head in case of
784 * an endless recursion. */
788 if ((get_loop_n_elements(current_loop) > 0) || (is_outermost_loop(current_loop))) {
796 ir_loop *l = new_loop();
799 /* Remove the loop from the stack ... */
800 pop_scc_unmark_visit(n);
802 /* The current backedge has been marked, that is temporarily eliminated,
803 by find tail. Start the scc algorithm
804 again on the subgraph that is left (the current loop without the backedge)
805 in order to find more inner loops. */
808 assert(irn_visited(n));
809 #if NO_LOOPS_WITHOUT_HEAD
814 /* No loop head was found, that is we have straight line code.
815 Pop all nodes from the stack to the current loop. */
821 /* Constructs backedge information for irg. In interprocedural view constructs
822 backedges for all methods called by irg, too. */
823 int construct_backedges(ir_graph *irg)
825 ir_graph *rem = current_ir_graph;
830 current_ir_graph = irg;
831 outermost_ir_graph = irg;
834 init_scc(irg, &temp);
837 new_loop(); /* sets current_loop */
838 head_rem = current_loop; /* Just for assertion */
840 inc_irg_visited(irg);
842 scc(get_irg_end(irg));
845 obstack_free(&temp, NULL);
847 assert(head_rem == current_loop);
848 mature_loops(current_loop, irg->obst);
849 set_irg_loop(irg, current_loop);
850 set_irg_loopinfo_state(irg, loopinfo_consistent);
851 assert(get_irg_loop(irg)->kind == k_ir_loop);
852 current_ir_graph = rem;
853 return max_loop_depth;
856 static void reset_backedges(ir_node *n)
858 if (is_possible_loop_head(n)) {
864 static void loop_reset_backedges(ir_loop *l)
867 reset_backedges(get_loop_node(l, 0));
868 for (i = 0; i < get_loop_n_nodes(l); ++i)
869 set_irn_loop(get_loop_node(l, i), NULL);
870 for (i = 0; i < get_loop_n_sons(l); ++i) {
871 loop_reset_backedges(get_loop_son(l, i));
876 static void loop_reset_node(ir_node *n, void *env)
879 set_irn_loop(n, NULL);
883 /** Removes all loop information.
884 Resets all backedges */
885 void free_loop_information(ir_graph *irg)
887 /* We can not use this recursion, as the loop might contain
888 illegal nodes by now. Why else would we throw away the
890 if (get_irg_loop(irg)) loop_reset_backedges(get_irg_loop(irg));
892 irg_walk_graph(irg, loop_reset_node, NULL, NULL);
893 set_irg_loop(irg, NULL);
894 set_irg_loopinfo_state(current_ir_graph, loopinfo_none);
895 /* We cannot free the loop nodes, they are on the obstack. */
898 void free_all_loop_information(void)
901 for (i = 0; i < get_irp_n_irgs(); i++) {
902 free_loop_information(get_irp_irg(i));
906 /* ------------------------------------------------------------------- */
907 /* Simple analyses based on the loop information */
908 /* ------------------------------------------------------------------- */
910 static int is_loop_variant(ir_loop *l, ir_loop *b)
914 if (l == b) return 1;
916 n_elems = get_loop_n_elements(l);
917 for (i = 0; i < n_elems; ++i) {
918 loop_element e = get_loop_element(l, i);
919 if (is_ir_loop(e.kind))
920 if (is_loop_variant(e.son, b))
927 /* Test whether a value is loop invariant.
929 * @param n The node to be tested.
930 * @param block A block node. We pass the block, not the loop as we must
931 * start off with a block loop to find all proper uses.
933 * Returns non-zero, if the node n is not changed in the loop block
934 * belongs to or in inner loops of this blocks loop. */
935 int is_loop_invariant(const ir_node *n, const ir_node *block)
937 ir_loop *l = get_irn_loop(block);
938 const ir_node *b = is_Block(n) ? n : get_nodes_block(n);
939 return !is_loop_variant(l, get_irn_loop(b));