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);
151 /**********************************************************************/
153 /**********************************************************************/
155 static ir_node **stack = NULL;
156 static size_t tos = 0; /* top of stack */
159 * initializes the stack
161 static inline void init_stack(void)
164 ARR_RESIZE(ir_node *, stack, 1000);
166 stack = NEW_ARR_F(ir_node *, 1000);
174 static void finish_stack(void)
181 * push a node onto the stack
183 * @param n The node to push
185 static inline void push(ir_node *n)
187 if (tos == ARR_LEN(stack)) {
188 size_t nlen = ARR_LEN(stack) * 2;
189 ARR_RESIZE(ir_node *, stack, nlen);
192 mark_irn_in_stack(n);
196 * pop a node from the stack
198 * @return The topmost node
200 static inline ir_node *pop(void)
206 mark_irn_not_in_stack(n);
211 * The nodes up to n belong to the current loop.
212 * Removes them from the stack and adds them to the current loop.
214 static inline void pop_scc_to_loop(ir_node *n)
222 set_irn_dfn(m, loop_node_cnt);
223 add_loop_node(current_loop, m);
224 set_irn_loop(m, current_loop);
228 /* GL ??? my last son is my grandson??? Removes loops with no
229 ir_nodes in them. Such loops have only another loop as son. (Why
230 can't they have two loops as sons? Does it never get that far? ) */
231 static void close_loop(ir_loop *l)
233 size_t last = get_loop_n_elements(l) - 1;
234 loop_element lelement = get_loop_element(l, last);
235 ir_loop *last_son = lelement.son;
237 if (get_kind(last_son) == k_ir_loop &&
238 get_loop_n_elements(last_son) == 1) {
241 lelement = get_loop_element(last_son, 0);
244 if (get_kind(gson) == k_ir_loop) {
245 loop_element new_last_son;
247 gson->outer_loop = l;
248 new_last_son.son = gson;
249 l->children[last] = new_last_son;
256 /* Removes and unmarks all nodes up to n from the stack.
257 The nodes must be visited once more to assign them to a scc. */
258 static inline void pop_scc_unmark_visit(ir_node *n)
264 set_irn_visited(m, 0);
268 /**********************************************************************/
269 /* The loop datastructure. **/
270 /**********************************************************************/
272 /* Allocates a new loop as son of current_loop. Sets current_loop
273 to the new loop and returns the father. */
274 static ir_loop *new_loop(void)
276 ir_loop *father = current_loop;
277 ir_loop *son = alloc_loop(father, get_irg_obstack(outermost_ir_graph));
283 /**********************************************************************/
284 /* Constructing and destructing the loop/backedge information. **/
285 /**********************************************************************/
287 /* Initialization steps. **********************************************/
289 static inline void init_node(ir_node *n, void *env)
291 struct obstack *obst = (struct obstack*) env;
292 set_irn_link(n, new_scc_info(obst));
296 static inline void init_scc_common(void)
303 static inline void init_scc(ir_graph *irg, struct obstack *obst)
306 irg_walk_graph(irg, init_node, NULL, obst);
309 static inline void finish_scc(void)
315 * Check whether a given node represents the outermost Start
316 * block. In intra-procedural view this is the start block of the
317 * current graph, in interprocedural view it is the start block
318 * of the outer most graph.
320 * @param n the node to check
322 * This is the condition for breaking the scc recursion.
324 static int is_outermost_Start(ir_node *n)
326 /* Test whether this is the outermost Start node. */
327 if (is_Block(n) && get_Block_n_cfgpreds(n) == 1) {
328 ir_node *pred = skip_Proj(get_Block_cfgpred(n, 0));
329 if (is_Start(pred) && get_nodes_block(pred) == n)
335 /* When to walk from nodes to blocks. Only for Control flow operations? */
336 static inline int get_start_index(ir_node *n)
338 #undef BLOCK_BEFORE_NODE
339 #define BLOCK_BEFORE_NODE 1
341 #if BLOCK_BEFORE_NODE
343 /* This version assures, that all nodes are ordered absolutely. This allows
344 to undef all nodes in the heap analysis if the block is false, which
346 I.e., with this code, the order on the loop tree is correct. But a
347 (single) test showed the loop tree is deeper. */
350 (get_irg_pinned(get_irn_irg(n)) == op_pin_state_floats &&
351 get_irn_pinned(n) == op_pin_state_floats))
352 // Here we could test for backedge at -1 which is illegal
359 /* This version causes deeper loop trees (at least we verified this
361 But it guarantees that Blocks are analysed before nodes contained in the
362 block. If so, we can set the value to undef if the block is not \
364 if (is_cfop(n) || is_fragile_op(n) || is_Start(n))
373 * Return non-zero if the given node is a legal loop header:
376 * @param n the node to check
378 static inline int is_possible_loop_head(ir_node *n)
380 return is_Block(n) || is_Phi(n);
384 * Returns non-zero if n is a loop header, i.e., it is a Block or Phi
385 * node and has predecessors within the loop and out of the loop.
387 * @param n the node to check
388 * @param root only needed for assertion.
390 static int is_head(ir_node *n, ir_node *root)
393 int some_outof_loop = 0, some_in_loop = 0;
395 /* Test for legal loop header: Block, Phi, ... */
396 if (!is_possible_loop_head(n))
399 if (!is_outermost_Start(n)) {
401 int uplink = get_irn_uplink(root);
405 arity = get_irn_arity(n);
406 for (i = get_start_index(n); i < arity; i++) {
408 if (is_backedge(n, i))
410 pred = get_irn_n(n, i);
411 if (! irn_is_in_stack(pred)) {
414 assert(get_irn_uplink(pred) >= uplink);
419 return some_outof_loop & some_in_loop;
423 * Returns non-zero if n is possible loop head of an endless loop.
424 * I.e., it is a Block or Phi node and has only predecessors
427 * @param n the node to check
428 * @param root only needed for assertion.
430 static int is_endless_head(ir_node *n, ir_node *root)
433 int none_outof_loop = 1, some_in_loop = 0;
435 /* Test for legal loop header: Block, Phi, ... */
436 if (!is_possible_loop_head(n))
439 if (!is_outermost_Start(n)) {
441 int uplink = get_irn_uplink(root);
445 arity = get_irn_arity(n);
446 for (i = get_start_index(n); i < arity; i++) {
448 if (is_backedge(n, i))
450 pred = get_irn_n(n, i);
451 if (!irn_is_in_stack(pred)) {
454 assert(get_irn_uplink(pred) >= uplink);
459 return none_outof_loop & some_in_loop;
462 /** Returns index of the predecessor with the smallest dfn number
463 greater-equal than limit. */
464 static int smallest_dfn_pred(ir_node *n, int limit)
466 int i, index = -2, min = -1;
468 if (!is_outermost_Start(n)) {
469 int arity = get_irn_arity(n);
470 for (i = get_start_index(n); i < arity; i++) {
471 ir_node *pred = get_irn_n(n, i);
472 if (is_backedge(n, i) || !irn_is_in_stack(pred))
474 if (get_irn_dfn(pred) >= limit && (min == -1 || get_irn_dfn(pred) < min)) {
476 min = get_irn_dfn(pred);
484 * Returns index of the predecessor with the largest dfn number.
486 static int largest_dfn_pred(ir_node *n)
488 int i, index = -2, max = -1;
490 if (!is_outermost_Start(n)) {
491 int arity = get_irn_arity(n);
492 for (i = get_start_index(n); i < arity; i++) {
493 ir_node *pred = get_irn_n(n, i);
494 if (is_backedge (n, i) || !irn_is_in_stack(pred))
496 if (get_irn_dfn(pred) > max) {
498 max = get_irn_dfn(pred);
506 * Searches the stack for possible loop heads. Tests these for backedges.
507 * If it finds a head with an unmarked backedge it marks this edge and
508 * returns the tail of the loop.
509 * If it finds no backedge returns NULL.
510 * ("disable_backedge" in fiasco)
512 * @param n A node where uplink == dfn.
514 static ir_node *find_tail(ir_node *n)
517 int i, res_index = -2;
519 m = stack[tos-1]; /* tos = top of stack */
521 res_index = smallest_dfn_pred(m, 0);
522 if ((res_index == -2) && /* no smallest dfn pred found. */
526 if (m == n) return NULL; // Is this to catch Phi - self loops?
527 for (i = tos-2; i >= 0; --i) {
531 res_index = smallest_dfn_pred(m, get_irn_dfn(m) + 1);
532 if (res_index == -2) /* no smallest dfn pred found. */
533 res_index = largest_dfn_pred(m);
535 if ((m == n) && (res_index == -2)) { /* don't walk past loop head. */
541 /* We should not walk past our selves on the stack: The upcoming nodes
542 are not in this loop. We assume a loop not reachable from Start. */
550 /* A dead loop not reachable from Start. */
551 for (i = tos-2; i >= 0; --i) {
553 if (is_endless_head(m, n)) {
554 res_index = smallest_dfn_pred(m, get_irn_dfn(m) + 1);
555 if (res_index == -2) /* no smallest dfn pred found. */
556 res_index = largest_dfn_pred (m);
559 /* It's not an unreachable loop, either. */
563 //assert(0 && "no head found on stack");
567 if (res_index <= -2) {
568 /* It's a completely bad loop: without Phi/Block nodes that can
569 be a head. I.e., the code is "dying". We break the loop by
570 setting Bad nodes. */
571 ir_graph *irg = get_irn_irg(n);
572 ir_mode *mode = get_irn_mode(n);
573 ir_node *bad = new_r_Bad(irg, mode);
574 int arity = get_irn_arity(n);
575 for (i = -1; i < arity; ++i) {
576 set_irn_n(n, i, bad);
580 assert(res_index > -2);
582 set_backedge(m, res_index);
583 return is_outermost_Start(n) ? NULL : get_irn_n(m, res_index);
586 static inline int is_outermost_loop(ir_loop *l)
588 return l == get_loop_outer_loop(l);
591 /*-----------------------------------------------------------*
592 * The core algorithm. *
593 *-----------------------------------------------------------*/
596 * The core algorithm: Find strongly coupled components.
598 * @param n node to start
600 static void scc(ir_node *n)
602 if (irn_visited_else_mark(n))
605 /* Initialize the node */
606 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
607 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
608 set_irn_loop(n, NULL);
612 /* AS: get_start_index might return -1 for Control Flow Nodes, and thus a negative
613 array index would be passed to is_backedge(). But CFG Nodes dont't have a backedge array,
614 so is_backedge does not access array[-1] but correctly returns false! */
616 if (!is_outermost_Start(n)) {
617 int i, arity = get_irn_arity(n);
619 for (i = get_start_index(n); i < arity; ++i) {
621 if (is_backedge(n, i))
625 if (irn_is_in_stack(m)) {
626 /* Uplink of m is smaller if n->m is a backedge.
627 Propagate the uplink to mark the loop. */
628 if (get_irn_uplink(m) < get_irn_uplink(n))
629 set_irn_uplink(n, get_irn_uplink(m));
634 if (get_irn_dfn(n) == get_irn_uplink(n)) {
635 /* This condition holds for
636 1) the node with the incoming backedge.
637 That is: We found a loop!
638 2) Straight line code, because no uplink has been propagated, so the
639 uplink still is the same as the dfn.
641 But n might not be a proper loop head for the analysis. Proper loop
642 heads are Block and Phi nodes. find_tail() searches the stack for
643 Block's and Phi's and takes those nodes as loop heads for the current
644 loop instead and marks the incoming edge as backedge. */
646 ir_node *tail = find_tail(n);
648 /* We have a loop, that is no straight line code,
649 because we found a loop head!
650 Next actions: Open a new loop on the loop tree and
651 try to find inner loops */
653 /* This is an adaption of the algorithm from fiasco / optscc to
654 * avoid loops without Block or Phi as first node. This should
655 * severely reduce the number of evaluations of nodes to detect
656 * a fixpoint in the heap analysis.
657 * Further it avoids loops without firm nodes that cause errors
658 * in the heap analyses.
659 * But attention: don't do it for the outermost loop: This loop
660 * is not iterated. A first block can be a loop head in case of
661 * an endless recursion. */
665 if ((get_loop_n_elements(current_loop) > 0) || (is_outermost_loop(current_loop))) {
673 /* Remove the loop from the stack ... */
674 pop_scc_unmark_visit(n);
676 /* The current backedge has been marked, that is temporarily eliminated,
677 by find tail. Start the scc algorithm
678 again on the subgraph that is left (the current loop without the backedge)
679 in order to find more inner loops. */
682 assert(irn_visited(n));
686 /* No loop head was found, that is we have straight line code.
687 Pop all nodes from the stack to the current loop. */
693 void construct_backedges(ir_graph *irg)
695 ir_graph *rem = current_ir_graph;
699 current_ir_graph = irg;
700 outermost_ir_graph = irg;
703 init_scc(irg, &temp);
706 new_loop(); /* sets current_loop */
707 head_rem = current_loop; /* Just for assertion */
709 inc_irg_visited(irg);
711 scc(get_irg_end(irg));
714 obstack_free(&temp, NULL);
716 assert(head_rem == current_loop);
717 mature_loops(current_loop, get_irg_obstack(irg));
718 set_irg_loop(irg, current_loop);
719 add_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO);
720 assert(get_irg_loop(irg)->kind == k_ir_loop);
721 current_ir_graph = rem;
724 static void reset_backedges(ir_node *n)
726 if (is_possible_loop_head(n)) {
731 static void loop_reset_node(ir_node *n, void *env)
734 set_irn_loop(n, NULL);
738 void free_loop_information(ir_graph *irg)
740 irg_walk_graph(irg, loop_reset_node, NULL, NULL);
741 set_irg_loop(irg, NULL);
742 clear_irg_properties(current_ir_graph, IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO);
743 /* We cannot free the loop nodes, they are on the obstack. */
746 void free_all_loop_information(void)
749 for (i = 0; i < get_irp_n_irgs(); i++) {
750 free_loop_information(get_irp_irg(i));
754 /* ------------------------------------------------------------------- */
755 /* Simple analyses based on the loop information */
756 /* ------------------------------------------------------------------- */
758 static int is_loop_variant(ir_loop *l, ir_loop *b)
762 if (l == b) return 1;
764 n_elems = get_loop_n_elements(l);
765 for (i = 0; i < n_elems; ++i) {
766 loop_element e = get_loop_element(l, i);
767 if (is_ir_loop(e.kind))
768 if (is_loop_variant(e.son, b))
775 int is_loop_invariant(const ir_node *n, const ir_node *block)
777 ir_loop *l = get_irn_loop(block);
778 const ir_node *b = is_Block(n) ? n : get_nodes_block(n);
779 return !is_loop_variant(l, get_irn_loop(b));