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 static unsigned max_loop_depth = 0;
59 /**********************************************************************/
60 /* Node attributes needed for the construction. **/
61 /**********************************************************************/
63 typedef struct scc_info {
64 int in_stack; /**< Marks whether node is on the stack. */
65 int dfn; /**< Depth first search number. */
66 int uplink; /**< dfn number of ancestor. */
67 /* ir_loop *loop; *//* Refers to the containing loop. */
69 struct section *section;
76 * Allocates a new SCC info on the given obstack.
78 static inline scc_info *new_scc_info(struct obstack *obst)
80 return OALLOCZ(obst, scc_info);
84 * Mark node n being on the SCC stack.
86 static inline void mark_irn_in_stack(ir_node *n)
88 scc_info *scc = (scc_info*) get_irn_link(n);
94 * Mark node n NOT being on the SCC stack.
96 static inline void mark_irn_not_in_stack(ir_node *n)
98 scc_info *scc = (scc_info*) get_irn_link(n);
104 * Checks if a node is on the SCC stack.
106 static inline int irn_is_in_stack(ir_node *n)
108 scc_info *scc = (scc_info*) get_irn_link(n);
110 return scc->in_stack;
114 * Sets the uplink number for a node.
116 static inline void set_irn_uplink(ir_node *n, int uplink)
118 scc_info *scc = (scc_info*) get_irn_link(n);
120 scc->uplink = uplink;
124 * Returns the uplink number for a node.
126 static int get_irn_uplink(ir_node *n)
128 scc_info *scc = (scc_info*) get_irn_link(n);
134 * Sets the depth-first-search number for a node.
136 static inline void set_irn_dfn(ir_node *n, int dfn)
138 scc_info *scc = (scc_info*) get_irn_link(n);
144 * Returns the depth-first-search number of a node.
146 static int get_irn_dfn(ir_node *n)
148 scc_info *scc = (scc_info*) get_irn_link(n);
154 static ir_loop *find_nodes_loop(ir_node *n, ir_loop *l)
159 /* Test whether n is contained in this loop. */
160 for (i = 0; i < get_loop_n_nodes(l); i++)
161 if (n == get_loop_node(l, i)) return l;
163 /* Is this a leave in the loop tree? If so loop not found. */
164 if (get_loop_n_sons(l) == 0) return NULL;
166 /* Else descend in the loop tree. */
167 for (i = 0; i < get_loop_n_sons(l); i++) {
168 res = find_nodes_loop(n, get_loop_son(l, i));
174 /* @@@ temporary implementation, costly!!! */
175 ir_loop * get_irn_loop(ir_node *n)
177 ir_loop *l = get_irg_loop(current_ir_graph);
178 l = find_nodes_loop(n, l);
183 /**********************************************************************/
185 /**********************************************************************/
187 static ir_node **stack = NULL;
188 static size_t tos = 0; /* top of stack */
191 * initializes the stack
193 static inline void init_stack(void)
196 ARR_RESIZE(ir_node *, stack, 1000);
198 stack = NEW_ARR_F(ir_node *, 1000);
206 static void finish_stack(void)
213 * push a node onto the stack
215 * @param n The node to push
217 static inline void push(ir_node *n)
219 if (tos == ARR_LEN(stack)) {
220 size_t nlen = ARR_LEN(stack) * 2;
221 ARR_RESIZE(ir_node *, stack, nlen);
224 mark_irn_in_stack(n);
228 * pop a node from the stack
230 * @return The topmost node
232 static inline ir_node *pop(void)
238 mark_irn_not_in_stack(n);
243 * The nodes up to n belong to the current loop.
244 * Removes them from the stack and adds them to the current loop.
246 static inline void pop_scc_to_loop(ir_node *n)
254 set_irn_dfn(m, loop_node_cnt);
255 add_loop_node(current_loop, m);
256 set_irn_loop(m, current_loop);
260 /* GL ??? my last son is my grandson??? Removes loops with no
261 ir_nodes in them. Such loops have only another loop as son. (Why
262 can't they have two loops as sons? Does it never get that far? ) */
263 static void close_loop(ir_loop *l)
265 size_t last = get_loop_n_elements(l) - 1;
266 loop_element lelement = get_loop_element(l, last);
267 ir_loop *last_son = lelement.son;
269 if (get_kind(last_son) == k_ir_loop &&
270 get_loop_n_elements(last_son) == 1) {
273 lelement = get_loop_element(last_son, 0);
276 if (get_kind(gson) == k_ir_loop) {
277 loop_element new_last_son;
279 gson->outer_loop = l;
280 new_last_son.son = gson;
281 l->children[last] = new_last_son;
288 /* Removes and unmarks all nodes up to n from the stack.
289 The nodes must be visited once more to assign them to a scc. */
290 static inline void pop_scc_unmark_visit(ir_node *n)
296 set_irn_visited(m, 0);
300 /**********************************************************************/
301 /* The loop datastructure. **/
302 /**********************************************************************/
304 /* Allocates a new loop as son of current_loop. Sets current_loop
305 to the new loop and returns the father. */
306 static ir_loop *new_loop(void)
308 ir_loop *father = current_loop;
309 ir_loop *son = alloc_loop(father, get_irg_obstack(outermost_ir_graph));
311 if (son->depth > max_loop_depth) max_loop_depth = son->depth;
316 /**********************************************************************/
317 /* Constructing and destructing the loop/backedge information. **/
318 /**********************************************************************/
320 /* Initialization steps. **********************************************/
322 static inline void init_node(ir_node *n, void *env)
324 struct obstack *obst = (struct obstack*) env;
325 set_irn_link(n, new_scc_info(obst));
329 static inline void init_scc_common(void)
336 static inline void init_scc(ir_graph *irg, struct obstack *obst)
339 irg_walk_graph(irg, init_node, NULL, obst);
342 static inline void finish_scc(void)
348 * Check whether a given node represents the outermost Start
349 * block. In intra-procedural view this is the start block of the
350 * current graph, in interprocedural view it is the start block
351 * of the outer most graph.
353 * @param n the node to check
355 * This is the condition for breaking the scc recursion.
357 static int is_outermost_Start(ir_node *n)
359 /* Test whether this is the outermost Start node. */
360 if (is_Block(n) && get_Block_n_cfgpreds(n) == 1) {
361 ir_node *pred = skip_Proj(get_Block_cfgpred(n, 0));
362 if (is_Start(pred) && get_nodes_block(pred) == n)
368 /* When to walk from nodes to blocks. Only for Control flow operations? */
369 static inline int get_start_index(ir_node *n)
371 #undef BLOCK_BEFORE_NODE
372 #define BLOCK_BEFORE_NODE 1
374 #if BLOCK_BEFORE_NODE
376 /* This version assures, that all nodes are ordered absolutely. This allows
377 to undef all nodes in the heap analysis if the block is false, which
379 I.e., with this code, the order on the loop tree is correct. But a
380 (single) test showed the loop tree is deeper. */
383 (get_irg_pinned(get_irn_irg(n)) == op_pin_state_floats &&
384 get_irn_pinned(n) == op_pin_state_floats))
385 // Here we could test for backedge at -1 which is illegal
392 /* This version causes deeper loop trees (at least we verified this
394 But it guarantees that Blocks are analysed before nodes contained in the
395 block. If so, we can set the value to undef if the block is not \
397 if (is_cfop(n) || is_fragile_op(n) || is_Start(n))
406 * Return non-zero if the given node is a legal loop header:
409 * @param n the node to check
411 static inline int is_possible_loop_head(ir_node *n)
413 return is_Block(n) || is_Phi(n);
417 * Returns non-zero if n is a loop header, i.e., it is a Block or Phi
418 * node and has predecessors within the loop and out of the loop.
420 * @param n the node to check
421 * @param root only needed for assertion.
423 static int is_head(ir_node *n, ir_node *root)
426 int some_outof_loop = 0, some_in_loop = 0;
428 /* Test for legal loop header: Block, Phi, ... */
429 if (!is_possible_loop_head(n))
432 if (!is_outermost_Start(n)) {
434 int uplink = get_irn_uplink(root);
438 arity = get_irn_arity(n);
439 for (i = get_start_index(n); i < arity; i++) {
441 if (is_backedge(n, i))
443 pred = get_irn_n(n, i);
444 if (! irn_is_in_stack(pred)) {
447 assert(get_irn_uplink(pred) >= uplink);
452 return some_outof_loop & some_in_loop;
456 * Returns non-zero if n is possible loop head of an endless loop.
457 * I.e., it is a Block or Phi node and has only predecessors
460 * @param n the node to check
461 * @param root only needed for assertion.
463 static int is_endless_head(ir_node *n, ir_node *root)
466 int none_outof_loop = 1, some_in_loop = 0;
468 /* Test for legal loop header: Block, Phi, ... */
469 if (!is_possible_loop_head(n))
472 if (!is_outermost_Start(n)) {
474 int uplink = get_irn_uplink(root);
478 arity = get_irn_arity(n);
479 for (i = get_start_index(n); i < arity; i++) {
481 if (is_backedge(n, i))
483 pred = get_irn_n(n, i);
484 if (!irn_is_in_stack(pred)) {
487 assert(get_irn_uplink(pred) >= uplink);
492 return none_outof_loop & some_in_loop;
495 /** Returns index of the predecessor with the smallest dfn number
496 greater-equal than limit. */
497 static int smallest_dfn_pred(ir_node *n, int limit)
499 int i, index = -2, min = -1;
501 if (!is_outermost_Start(n)) {
502 int arity = get_irn_arity(n);
503 for (i = get_start_index(n); i < arity; i++) {
504 ir_node *pred = get_irn_n(n, i);
505 if (is_backedge(n, i) || !irn_is_in_stack(pred))
507 if (get_irn_dfn(pred) >= limit && (min == -1 || get_irn_dfn(pred) < min)) {
509 min = get_irn_dfn(pred);
517 * Returns index of the predecessor with the largest dfn number.
519 static int largest_dfn_pred(ir_node *n)
521 int i, index = -2, max = -1;
523 if (!is_outermost_Start(n)) {
524 int arity = get_irn_arity(n);
525 for (i = get_start_index(n); i < arity; i++) {
526 ir_node *pred = get_irn_n(n, i);
527 if (is_backedge (n, i) || !irn_is_in_stack(pred))
529 if (get_irn_dfn(pred) > max) {
531 max = get_irn_dfn(pred);
539 * Searches the stack for possible loop heads. Tests these for backedges.
540 * If it finds a head with an unmarked backedge it marks this edge and
541 * returns the tail of the loop.
542 * If it finds no backedge returns NULL.
543 * ("disable_backedge" in fiasco)
545 * @param n A node where uplink == dfn.
547 static ir_node *find_tail(ir_node *n)
550 int i, res_index = -2;
553 if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
555 m = stack[tos-1]; /* tos = top of stack */
557 res_index = smallest_dfn_pred(m, 0);
558 if ((res_index == -2) && /* no smallest dfn pred found. */
562 if (m == n) return NULL; // Is this to catch Phi - self loops?
563 for (i = tos-2; i >= 0; --i) {
567 res_index = smallest_dfn_pred(m, get_irn_dfn(m) + 1);
568 if (res_index == -2) /* no smallest dfn pred found. */
569 res_index = largest_dfn_pred(m);
571 if ((m == n) && (res_index == -2)) { /* don't walk past loop head. */
577 /* We should not walk past our selves on the stack: The upcoming nodes
578 are not in this loop. We assume a loop not reachable from Start. */
586 /* A dead loop not reachable from Start. */
587 for (i = tos-2; i >= 0; --i) {
589 if (is_endless_head(m, n)) {
590 res_index = smallest_dfn_pred(m, get_irn_dfn(m) + 1);
591 if (res_index == -2) /* no smallest dfn pred found. */
592 res_index = largest_dfn_pred (m);
595 /* It's not an unreachable loop, either. */
599 //assert(0 && "no head found on stack");
603 if (res_index <= -2) {
604 /* It's a completely bad loop: without Phi/Block nodes that can
605 be a head. I.e., the code is "dying". We break the loop by
606 setting Bad nodes. */
607 ir_graph *irg = get_irn_irg(n);
608 ir_mode *mode = get_irn_mode(n);
609 ir_node *bad = new_r_Bad(irg, mode);
610 int arity = get_irn_arity(n);
611 for (i = -1; i < arity; ++i) {
612 set_irn_n(n, i, bad);
616 assert(res_index > -2);
618 set_backedge(m, res_index);
619 return is_outermost_Start(n) ? NULL : get_irn_n(m, res_index);
622 static inline int is_outermost_loop(ir_loop *l)
624 return l == get_loop_outer_loop(l);
627 /*-----------------------------------------------------------*
628 * The core algorithm. *
629 *-----------------------------------------------------------*/
632 * The core algorithm: Find strongly coupled components.
634 * @param n node to start
636 static void scc(ir_node *n)
638 if (irn_visited_else_mark(n))
641 /* Initialize the node */
642 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
643 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
644 set_irn_loop(n, NULL);
648 /* AS: get_start_index might return -1 for Control Flow Nodes, and thus a negative
649 array index would be passed to is_backedge(). But CFG Nodes dont't have a backedge array,
650 so is_backedge does not access array[-1] but correctly returns false! */
652 if (!is_outermost_Start(n)) {
653 int i, arity = get_irn_arity(n);
655 for (i = get_start_index(n); i < arity; ++i) {
657 if (is_backedge(n, i))
661 if (irn_is_in_stack(m)) {
662 /* Uplink of m is smaller if n->m is a backedge.
663 Propagate the uplink to mark the loop. */
664 if (get_irn_uplink(m) < get_irn_uplink(n))
665 set_irn_uplink(n, get_irn_uplink(m));
670 if (get_irn_dfn(n) == get_irn_uplink(n)) {
671 /* This condition holds for
672 1) the node with the incoming backedge.
673 That is: We found a loop!
674 2) Straight line code, because no uplink has been propagated, so the
675 uplink still is the same as the dfn.
677 But n might not be a proper loop head for the analysis. Proper loop
678 heads are Block and Phi nodes. find_tail() searches the stack for
679 Block's and Phi's and takes those nodes as loop heads for the current
680 loop instead and marks the incoming edge as backedge. */
682 ir_node *tail = find_tail(n);
684 /* We have a loop, that is no straight line code,
685 because we found a loop head!
686 Next actions: Open a new loop on the loop tree and
687 try to find inner loops */
689 /* This is an adaption of the algorithm from fiasco / optscc to
690 * avoid loops without Block or Phi as first node. This should
691 * severely reduce the number of evaluations of nodes to detect
692 * a fixpoint in the heap analysis.
693 * Further it avoids loops without firm nodes that cause errors
694 * in the heap analyses.
695 * But attention: don't do it for the outermost loop: This loop
696 * is not iterated. A first block can be a loop head in case of
697 * an endless recursion. */
701 if ((get_loop_n_elements(current_loop) > 0) || (is_outermost_loop(current_loop))) {
709 /* Remove the loop from the stack ... */
710 pop_scc_unmark_visit(n);
712 /* The current backedge has been marked, that is temporarily eliminated,
713 by find tail. Start the scc algorithm
714 again on the subgraph that is left (the current loop without the backedge)
715 in order to find more inner loops. */
718 assert(irn_visited(n));
722 /* No loop head was found, that is we have straight line code.
723 Pop all nodes from the stack to the current loop. */
729 int construct_backedges(ir_graph *irg)
731 ir_graph *rem = current_ir_graph;
736 current_ir_graph = irg;
737 outermost_ir_graph = irg;
740 init_scc(irg, &temp);
743 new_loop(); /* sets current_loop */
744 head_rem = current_loop; /* Just for assertion */
746 inc_irg_visited(irg);
748 scc(get_irg_end(irg));
751 obstack_free(&temp, NULL);
753 assert(head_rem == current_loop);
754 mature_loops(current_loop, get_irg_obstack(irg));
755 set_irg_loop(irg, current_loop);
756 add_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO);
757 assert(get_irg_loop(irg)->kind == k_ir_loop);
758 current_ir_graph = rem;
759 return max_loop_depth;
762 static void reset_backedges(ir_node *n)
764 if (is_possible_loop_head(n)) {
770 static void loop_reset_backedges(ir_loop *l)
773 reset_backedges(get_loop_node(l, 0));
774 for (i = 0; i < get_loop_n_nodes(l); ++i)
775 set_irn_loop(get_loop_node(l, i), NULL);
776 for (i = 0; i < get_loop_n_sons(l); ++i) {
777 loop_reset_backedges(get_loop_son(l, i));
782 static void loop_reset_node(ir_node *n, void *env)
785 set_irn_loop(n, NULL);
789 void free_loop_information(ir_graph *irg)
791 /* We can not use this recursion, as the loop might contain
792 illegal nodes by now. Why else would we throw away the
794 if (get_irg_loop(irg)) loop_reset_backedges(get_irg_loop(irg));
796 irg_walk_graph(irg, loop_reset_node, NULL, NULL);
797 set_irg_loop(irg, NULL);
798 clear_irg_properties(current_ir_graph, IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO);
799 /* We cannot free the loop nodes, they are on the obstack. */
802 void free_all_loop_information(void)
805 for (i = 0; i < get_irp_n_irgs(); i++) {
806 free_loop_information(get_irp_irg(i));
810 /* ------------------------------------------------------------------- */
811 /* Simple analyses based on the loop information */
812 /* ------------------------------------------------------------------- */
814 static int is_loop_variant(ir_loop *l, ir_loop *b)
818 if (l == b) return 1;
820 n_elems = get_loop_n_elements(l);
821 for (i = 0; i < n_elems; ++i) {
822 loop_element e = get_loop_element(l, i);
823 if (is_ir_loop(e.kind))
824 if (is_loop_variant(e.son, b))
831 int is_loop_invariant(const ir_node *n, const ir_node *block)
833 ir_loop *l = get_irn_loop(block);
834 const ir_node *b = is_Block(n) ? n : get_nodes_block(n);
835 return !is_loop_variant(l, get_irn_loop(b));