2 * Copyright (C) 1995-2011 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
37 #include "irgraph_t.h"
45 /** The outermost graph the scc is computed for. */
46 static ir_graph *outermost_ir_graph;
47 /** Current loop construction is working on. */
48 static ir_loop *current_loop;
49 /** Counts the number of allocated loop nodes.
50 * Each loop node gets a unique number.
51 * @todo What for? ev. remove.
53 static int loop_node_cnt = 0;
54 /** Counter to generate depth first numbering of visited nodes. */
55 static int current_dfn = 1;
57 /**********************************************************************/
58 /* Node attributes needed for the construction. **/
59 /**********************************************************************/
61 typedef struct scc_info {
62 int in_stack; /**< Marks whether node is on the stack. */
63 int dfn; /**< Depth first search number. */
64 int uplink; /**< dfn number of ancestor. */
65 /* ir_loop *loop; *//* Refers to the containing loop. */
67 struct section *section;
74 * Allocates a new SCC info on the given obstack.
76 static inline scc_info *new_scc_info(struct obstack *obst)
78 return OALLOCZ(obst, scc_info);
82 * Mark node n being on the SCC stack.
84 static inline void mark_irn_in_stack(ir_node *n)
86 scc_info *scc = (scc_info*) get_irn_link(n);
92 * Mark node n NOT being on the SCC stack.
94 static inline void mark_irn_not_in_stack(ir_node *n)
96 scc_info *scc = (scc_info*) get_irn_link(n);
102 * Checks if a node is on the SCC stack.
104 static inline int irn_is_in_stack(ir_node *n)
106 scc_info *scc = (scc_info*) get_irn_link(n);
108 return scc->in_stack;
112 * Sets the uplink number for a node.
114 static inline void set_irn_uplink(ir_node *n, int uplink)
116 scc_info *scc = (scc_info*) get_irn_link(n);
118 scc->uplink = uplink;
122 * Returns the uplink number for a node.
124 static int get_irn_uplink(ir_node *n)
126 scc_info *scc = (scc_info*) get_irn_link(n);
132 * Sets the depth-first-search number for a node.
134 static inline void set_irn_dfn(ir_node *n, int dfn)
136 scc_info *scc = (scc_info*) get_irn_link(n);
142 * Returns the depth-first-search number of a node.
144 static int get_irn_dfn(ir_node *n)
146 scc_info *scc = (scc_info*) get_irn_link(n);
152 static ir_loop *find_nodes_loop(ir_node *n, ir_loop *l)
157 /* Test whether n is contained in this loop. */
158 for (i = 0; i < get_loop_n_nodes(l); i++)
159 if (n == get_loop_node(l, i)) return l;
161 /* Is this a leave in the loop tree? If so loop not found. */
162 if (get_loop_n_sons(l) == 0) return NULL;
164 /* Else descend in the loop tree. */
165 for (i = 0; i < get_loop_n_sons(l); i++) {
166 res = find_nodes_loop(n, get_loop_son(l, i));
172 /* @@@ temporary implementation, costly!!! */
173 ir_loop * get_irn_loop(ir_node *n)
175 ir_loop *l = get_irg_loop(current_ir_graph);
176 l = find_nodes_loop(n, l);
181 /**********************************************************************/
183 /**********************************************************************/
185 static ir_node **stack = NULL;
186 static size_t tos = 0; /* top of stack */
189 * initializes the stack
191 static inline void init_stack(void)
194 ARR_RESIZE(ir_node *, stack, 1000);
196 stack = NEW_ARR_F(ir_node *, 1000);
204 static void finish_stack(void)
211 * push a node onto the stack
213 * @param n The node to push
215 static inline void push(ir_node *n)
217 if (tos == ARR_LEN(stack)) {
218 size_t nlen = ARR_LEN(stack) * 2;
219 ARR_RESIZE(ir_node *, stack, nlen);
222 mark_irn_in_stack(n);
226 * pop a node from the stack
228 * @return The topmost node
230 static inline ir_node *pop(void)
236 mark_irn_not_in_stack(n);
241 * The nodes up to n belong to the current loop.
242 * Removes them from the stack and adds them to the current loop.
244 static inline void pop_scc_to_loop(ir_node *n)
252 set_irn_dfn(m, loop_node_cnt);
253 add_loop_node(current_loop, m);
254 set_irn_loop(m, current_loop);
258 /* GL ??? my last son is my grandson??? Removes loops with no
259 ir_nodes in them. Such loops have only another loop as son. (Why
260 can't they have two loops as sons? Does it never get that far? ) */
261 static void close_loop(ir_loop *l)
263 size_t last = get_loop_n_elements(l) - 1;
264 loop_element lelement = get_loop_element(l, last);
265 ir_loop *last_son = lelement.son;
267 if (get_kind(last_son) == k_ir_loop &&
268 get_loop_n_elements(last_son) == 1) {
271 lelement = get_loop_element(last_son, 0);
274 if (get_kind(gson) == k_ir_loop) {
275 loop_element new_last_son;
277 gson->outer_loop = l;
278 new_last_son.son = gson;
279 l->children[last] = new_last_son;
286 /* Removes and unmarks all nodes up to n from the stack.
287 The nodes must be visited once more to assign them to a scc. */
288 static inline void pop_scc_unmark_visit(ir_node *n)
294 set_irn_visited(m, 0);
298 /**********************************************************************/
299 /* The loop datastructure. **/
300 /**********************************************************************/
302 /* Allocates a new loop as son of current_loop. Sets current_loop
303 to the new loop and returns the father. */
304 static ir_loop *new_loop(void)
306 ir_loop *father = current_loop;
307 ir_loop *son = alloc_loop(father, get_irg_obstack(outermost_ir_graph));
313 /**********************************************************************/
314 /* Constructing and destructing the loop/backedge information. **/
315 /**********************************************************************/
317 /* Initialization steps. **********************************************/
319 static inline void init_node(ir_node *n, void *env)
321 struct obstack *obst = (struct obstack*) env;
322 set_irn_link(n, new_scc_info(obst));
326 static inline void init_scc_common(void)
333 static inline void init_scc(ir_graph *irg, struct obstack *obst)
336 irg_walk_graph(irg, init_node, NULL, obst);
339 static inline void finish_scc(void)
345 * Check whether a given node represents the outermost Start
346 * block. In intra-procedural view this is the start block of the
347 * current graph, in interprocedural view it is the start block
348 * of the outer most graph.
350 * @param n the node to check
352 * This is the condition for breaking the scc recursion.
354 static int is_outermost_Start(ir_node *n)
356 /* Test whether this is the outermost Start node. */
357 if (is_Block(n) && get_Block_n_cfgpreds(n) == 1) {
358 ir_node *pred = skip_Proj(get_Block_cfgpred(n, 0));
359 if (is_Start(pred) && get_nodes_block(pred) == n)
365 /* When to walk from nodes to blocks. Only for Control flow operations? */
366 static inline int get_start_index(ir_node *n)
368 #undef BLOCK_BEFORE_NODE
369 #define BLOCK_BEFORE_NODE 1
371 #if BLOCK_BEFORE_NODE
373 /* This version assures, that all nodes are ordered absolutely. This allows
374 to undef all nodes in the heap analysis if the block is false, which
376 I.e., with this code, the order on the loop tree is correct. But a
377 (single) test showed the loop tree is deeper. */
380 (get_irg_pinned(get_irn_irg(n)) == op_pin_state_floats &&
381 get_irn_pinned(n) == op_pin_state_floats))
382 // Here we could test for backedge at -1 which is illegal
389 /* This version causes deeper loop trees (at least we verified this
391 But it guarantees that Blocks are analysed before nodes contained in the
392 block. If so, we can set the value to undef if the block is not \
394 if (is_cfop(n) || is_fragile_op(n) || is_Start(n))
403 * Return non-zero if the given node is a legal loop header:
406 * @param n the node to check
408 static inline int is_possible_loop_head(ir_node *n)
410 return is_Block(n) || is_Phi(n);
414 * Returns non-zero if n is a loop header, i.e., it is a Block or Phi
415 * node and has predecessors within the loop and out of the loop.
417 * @param n the node to check
418 * @param root only needed for assertion.
420 static int is_head(ir_node *n, ir_node *root)
423 int some_outof_loop = 0, some_in_loop = 0;
425 /* Test for legal loop header: Block, Phi, ... */
426 if (!is_possible_loop_head(n))
429 if (!is_outermost_Start(n)) {
431 int uplink = get_irn_uplink(root);
435 arity = get_irn_arity(n);
436 for (i = get_start_index(n); i < arity; i++) {
438 if (is_backedge(n, i))
440 pred = get_irn_n(n, i);
441 if (! irn_is_in_stack(pred)) {
444 assert(get_irn_uplink(pred) >= uplink);
449 return some_outof_loop & some_in_loop;
453 * Returns non-zero if n is possible loop head of an endless loop.
454 * I.e., it is a Block or Phi node and has only predecessors
457 * @param n the node to check
458 * @param root only needed for assertion.
460 static int is_endless_head(ir_node *n, ir_node *root)
463 int none_outof_loop = 1, some_in_loop = 0;
465 /* Test for legal loop header: Block, Phi, ... */
466 if (!is_possible_loop_head(n))
469 if (!is_outermost_Start(n)) {
471 int uplink = get_irn_uplink(root);
475 arity = get_irn_arity(n);
476 for (i = get_start_index(n); i < arity; i++) {
478 if (is_backedge(n, i))
480 pred = get_irn_n(n, i);
481 if (!irn_is_in_stack(pred)) {
484 assert(get_irn_uplink(pred) >= uplink);
489 return none_outof_loop & some_in_loop;
492 /** Returns index of the predecessor with the smallest dfn number
493 greater-equal than limit. */
494 static int smallest_dfn_pred(ir_node *n, int limit)
496 int i, index = -2, min = -1;
498 if (!is_outermost_Start(n)) {
499 int arity = get_irn_arity(n);
500 for (i = get_start_index(n); i < arity; i++) {
501 ir_node *pred = get_irn_n(n, i);
502 if (is_backedge(n, i) || !irn_is_in_stack(pred))
504 if (get_irn_dfn(pred) >= limit && (min == -1 || get_irn_dfn(pred) < min)) {
506 min = get_irn_dfn(pred);
514 * Returns index of the predecessor with the largest dfn number.
516 static int largest_dfn_pred(ir_node *n)
518 int i, index = -2, max = -1;
520 if (!is_outermost_Start(n)) {
521 int arity = get_irn_arity(n);
522 for (i = get_start_index(n); i < arity; i++) {
523 ir_node *pred = get_irn_n(n, i);
524 if (is_backedge (n, i) || !irn_is_in_stack(pred))
526 if (get_irn_dfn(pred) > max) {
528 max = get_irn_dfn(pred);
536 * Searches the stack for possible loop heads. Tests these for backedges.
537 * If it finds a head with an unmarked backedge it marks this edge and
538 * returns the tail of the loop.
539 * If it finds no backedge returns NULL.
540 * ("disable_backedge" in fiasco)
542 * @param n A node where uplink == dfn.
544 static ir_node *find_tail(ir_node *n)
547 int i, res_index = -2;
550 if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
552 m = stack[tos-1]; /* tos = top of stack */
554 res_index = smallest_dfn_pred(m, 0);
555 if ((res_index == -2) && /* no smallest dfn pred found. */
559 if (m == n) return NULL; // Is this to catch Phi - self loops?
560 for (i = tos-2; i >= 0; --i) {
564 res_index = smallest_dfn_pred(m, get_irn_dfn(m) + 1);
565 if (res_index == -2) /* no smallest dfn pred found. */
566 res_index = largest_dfn_pred(m);
568 if ((m == n) && (res_index == -2)) { /* don't walk past loop head. */
574 /* We should not walk past our selves on the stack: The upcoming nodes
575 are not in this loop. We assume a loop not reachable from Start. */
583 /* A dead loop not reachable from Start. */
584 for (i = tos-2; i >= 0; --i) {
586 if (is_endless_head(m, n)) {
587 res_index = smallest_dfn_pred(m, get_irn_dfn(m) + 1);
588 if (res_index == -2) /* no smallest dfn pred found. */
589 res_index = largest_dfn_pred (m);
592 /* It's not an unreachable loop, either. */
596 //assert(0 && "no head found on stack");
600 if (res_index <= -2) {
601 /* It's a completely bad loop: without Phi/Block nodes that can
602 be a head. I.e., the code is "dying". We break the loop by
603 setting Bad nodes. */
604 ir_graph *irg = get_irn_irg(n);
605 ir_mode *mode = get_irn_mode(n);
606 ir_node *bad = new_r_Bad(irg, mode);
607 int arity = get_irn_arity(n);
608 for (i = -1; i < arity; ++i) {
609 set_irn_n(n, i, bad);
613 assert(res_index > -2);
615 set_backedge(m, res_index);
616 return is_outermost_Start(n) ? NULL : get_irn_n(m, res_index);
619 static inline int is_outermost_loop(ir_loop *l)
621 return l == get_loop_outer_loop(l);
624 /*-----------------------------------------------------------*
625 * The core algorithm. *
626 *-----------------------------------------------------------*/
629 * The core algorithm: Find strongly coupled components.
631 * @param n node to start
633 static void scc(ir_node *n)
635 if (irn_visited_else_mark(n))
638 /* Initialize the node */
639 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
640 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
641 set_irn_loop(n, NULL);
645 /* AS: get_start_index might return -1 for Control Flow Nodes, and thus a negative
646 array index would be passed to is_backedge(). But CFG Nodes dont't have a backedge array,
647 so is_backedge does not access array[-1] but correctly returns false! */
649 if (!is_outermost_Start(n)) {
650 int i, arity = get_irn_arity(n);
652 for (i = get_start_index(n); i < arity; ++i) {
654 if (is_backedge(n, i))
658 if (irn_is_in_stack(m)) {
659 /* Uplink of m is smaller if n->m is a backedge.
660 Propagate the uplink to mark the loop. */
661 if (get_irn_uplink(m) < get_irn_uplink(n))
662 set_irn_uplink(n, get_irn_uplink(m));
667 if (get_irn_dfn(n) == get_irn_uplink(n)) {
668 /* This condition holds for
669 1) the node with the incoming backedge.
670 That is: We found a loop!
671 2) Straight line code, because no uplink has been propagated, so the
672 uplink still is the same as the dfn.
674 But n might not be a proper loop head for the analysis. Proper loop
675 heads are Block and Phi nodes. find_tail() searches the stack for
676 Block's and Phi's and takes those nodes as loop heads for the current
677 loop instead and marks the incoming edge as backedge. */
679 ir_node *tail = find_tail(n);
681 /* We have a loop, that is no straight line code,
682 because we found a loop head!
683 Next actions: Open a new loop on the loop tree and
684 try to find inner loops */
686 /* This is an adaption of the algorithm from fiasco / optscc to
687 * avoid loops without Block or Phi as first node. This should
688 * severely reduce the number of evaluations of nodes to detect
689 * a fixpoint in the heap analysis.
690 * Further it avoids loops without firm nodes that cause errors
691 * in the heap analyses.
692 * But attention: don't do it for the outermost loop: This loop
693 * is not iterated. A first block can be a loop head in case of
694 * an endless recursion. */
698 if ((get_loop_n_elements(current_loop) > 0) || (is_outermost_loop(current_loop))) {
706 /* Remove the loop from the stack ... */
707 pop_scc_unmark_visit(n);
709 /* The current backedge has been marked, that is temporarily eliminated,
710 by find tail. Start the scc algorithm
711 again on the subgraph that is left (the current loop without the backedge)
712 in order to find more inner loops. */
715 assert(irn_visited(n));
719 /* No loop head was found, that is we have straight line code.
720 Pop all nodes from the stack to the current loop. */
726 void construct_backedges(ir_graph *irg)
728 ir_graph *rem = current_ir_graph;
732 current_ir_graph = irg;
733 outermost_ir_graph = irg;
736 init_scc(irg, &temp);
739 new_loop(); /* sets current_loop */
740 head_rem = current_loop; /* Just for assertion */
742 inc_irg_visited(irg);
744 scc(get_irg_end(irg));
747 obstack_free(&temp, NULL);
749 assert(head_rem == current_loop);
750 mature_loops(current_loop, get_irg_obstack(irg));
751 set_irg_loop(irg, current_loop);
752 add_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO);
753 assert(get_irg_loop(irg)->kind == k_ir_loop);
754 current_ir_graph = rem;
757 static void reset_backedges(ir_node *n)
759 if (is_possible_loop_head(n)) {
765 static void loop_reset_backedges(ir_loop *l)
768 reset_backedges(get_loop_node(l, 0));
769 for (i = 0; i < get_loop_n_nodes(l); ++i)
770 set_irn_loop(get_loop_node(l, i), NULL);
771 for (i = 0; i < get_loop_n_sons(l); ++i) {
772 loop_reset_backedges(get_loop_son(l, i));
777 static void loop_reset_node(ir_node *n, void *env)
780 set_irn_loop(n, NULL);
784 void free_loop_information(ir_graph *irg)
786 /* We can not use this recursion, as the loop might contain
787 illegal nodes by now. Why else would we throw away the
789 if (get_irg_loop(irg)) loop_reset_backedges(get_irg_loop(irg));
791 irg_walk_graph(irg, loop_reset_node, NULL, NULL);
792 set_irg_loop(irg, NULL);
793 clear_irg_properties(current_ir_graph, IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO);
794 /* We cannot free the loop nodes, they are on the obstack. */
797 void free_all_loop_information(void)
800 for (i = 0; i < get_irp_n_irgs(); i++) {
801 free_loop_information(get_irp_irg(i));
805 /* ------------------------------------------------------------------- */
806 /* Simple analyses based on the loop information */
807 /* ------------------------------------------------------------------- */
809 static int is_loop_variant(ir_loop *l, ir_loop *b)
813 if (l == b) return 1;
815 n_elems = get_loop_n_elements(l);
816 for (i = 0; i < n_elems; ++i) {
817 loop_element e = get_loop_element(l, i);
818 if (is_ir_loop(e.kind))
819 if (is_loop_variant(e.son, b))
826 int is_loop_invariant(const ir_node *n, const ir_node *block)
828 ir_loop *l = get_irn_loop(block);
829 const ir_node *b = is_Block(n) ? n : get_nodes_block(n);
830 return !is_loop_variant(l, get_irn_loop(b));