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. */
68 * Allocates a new SCC info on the given obstack.
70 static inline scc_info *new_scc_info(struct obstack *obst)
72 return OALLOCZ(obst, scc_info);
76 * Mark node n being on the SCC stack.
78 static inline void mark_irn_in_stack(ir_node *n)
80 scc_info *scc = (scc_info*) get_irn_link(n);
86 * Mark node n NOT being on the SCC stack.
88 static inline void mark_irn_not_in_stack(ir_node *n)
90 scc_info *scc = (scc_info*) get_irn_link(n);
96 * Checks if a node is on the SCC stack.
98 static inline int irn_is_in_stack(ir_node *n)
100 scc_info *scc = (scc_info*) get_irn_link(n);
102 return scc->in_stack;
106 * Sets the uplink number for a node.
108 static inline void set_irn_uplink(ir_node *n, int uplink)
110 scc_info *scc = (scc_info*) get_irn_link(n);
112 scc->uplink = uplink;
116 * Returns the uplink number for a node.
118 static int get_irn_uplink(ir_node *n)
120 scc_info *scc = (scc_info*) get_irn_link(n);
126 * Sets the depth-first-search number for a node.
128 static inline void set_irn_dfn(ir_node *n, int dfn)
130 scc_info *scc = (scc_info*) get_irn_link(n);
136 * Returns the depth-first-search number of a node.
138 static int get_irn_dfn(ir_node *n)
140 scc_info *scc = (scc_info*) get_irn_link(n);
145 /**********************************************************************/
147 /**********************************************************************/
149 static ir_node **stack = NULL;
150 static size_t tos = 0; /* top of stack */
153 * initializes the stack
155 static inline void init_stack(void)
158 ARR_RESIZE(ir_node *, stack, 1000);
160 stack = NEW_ARR_F(ir_node *, 1000);
168 static void finish_stack(void)
175 * push a node onto the stack
177 * @param n The node to push
179 static inline void push(ir_node *n)
181 if (tos == ARR_LEN(stack)) {
182 size_t nlen = ARR_LEN(stack) * 2;
183 ARR_RESIZE(ir_node *, stack, nlen);
186 mark_irn_in_stack(n);
190 * pop a node from the stack
192 * @return The topmost node
194 static inline ir_node *pop(void)
200 mark_irn_not_in_stack(n);
205 * The nodes up to n belong to the current loop.
206 * Removes them from the stack and adds them to the current loop.
208 static inline void pop_scc_to_loop(ir_node *n)
216 set_irn_dfn(m, loop_node_cnt);
217 add_loop_node(current_loop, m);
218 set_irn_loop(m, current_loop);
222 /* GL ??? my last son is my grandson??? Removes loops with no
223 ir_nodes in them. Such loops have only another loop as son. (Why
224 can't they have two loops as sons? Does it never get that far? ) */
225 static void close_loop(ir_loop *l)
227 size_t last = get_loop_n_elements(l) - 1;
228 loop_element lelement = get_loop_element(l, last);
229 ir_loop *last_son = lelement.son;
231 if (get_kind(last_son) == k_ir_loop &&
232 get_loop_n_elements(last_son) == 1) {
235 lelement = get_loop_element(last_son, 0);
238 if (get_kind(gson) == k_ir_loop) {
239 loop_element new_last_son;
241 gson->outer_loop = l;
242 new_last_son.son = gson;
243 l->children[last] = new_last_son;
250 /* Removes and unmarks all nodes up to n from the stack.
251 The nodes must be visited once more to assign them to a scc. */
252 static inline void pop_scc_unmark_visit(ir_node *n)
258 set_irn_visited(m, 0);
262 /**********************************************************************/
263 /* The loop datastructure. **/
264 /**********************************************************************/
266 /* Allocates a new loop as son of current_loop. Sets current_loop
267 to the new loop and returns the father. */
268 static ir_loop *new_loop(void)
270 ir_loop *father = current_loop;
271 ir_loop *son = alloc_loop(father, get_irg_obstack(outermost_ir_graph));
277 /**********************************************************************/
278 /* Constructing and destructing the loop/backedge information. **/
279 /**********************************************************************/
281 /* Initialization steps. **********************************************/
283 static inline void init_node(ir_node *n, void *env)
285 struct obstack *obst = (struct obstack*) env;
286 set_irn_link(n, new_scc_info(obst));
290 static inline void init_scc_common(void)
297 static inline void init_scc(ir_graph *irg, struct obstack *obst)
300 irg_walk_graph(irg, init_node, NULL, obst);
303 static inline void finish_scc(void)
309 * Check whether a given node represents the outermost Start
310 * block. In intra-procedural view this is the start block of the
311 * current graph, in interprocedural view it is the start block
312 * of the outer most graph.
314 * @param n the node to check
316 * This is the condition for breaking the scc recursion.
318 static int is_outermost_Start(ir_node *n)
320 /* Test whether this is the outermost Start node. */
321 if (is_Block(n) && get_Block_n_cfgpreds(n) == 1) {
322 ir_node *pred = skip_Proj(get_Block_cfgpred(n, 0));
323 if (is_Start(pred) && get_nodes_block(pred) == n)
329 /* When to walk from nodes to blocks. Only for Control flow operations? */
330 static inline int get_start_index(ir_node *n)
332 /* This version assures, that all nodes are ordered absolutely. This allows
333 to undef all nodes in the heap analysis if the block is false, which
335 I.e., with this code, the order on the loop tree is correct. But a
336 (single) test showed the loop tree is deeper. */
339 (get_irg_pinned(get_irn_irg(n)) == op_pin_state_floats &&
340 get_irn_pinned(n) == op_pin_state_floats))
341 // Here we could test for backedge at -1 which is illegal
348 * Return non-zero if the given node is a legal loop header:
351 * @param n the node to check
353 static inline int is_possible_loop_head(ir_node *n)
355 return is_Block(n) || is_Phi(n);
359 * Returns non-zero if n is a loop header, i.e., it is a Block or Phi
360 * node and has predecessors within the loop and out of the loop.
362 * @param n the node to check
363 * @param root only needed for assertion.
365 static int is_head(ir_node *n, ir_node *root)
368 int some_outof_loop = 0, some_in_loop = 0;
370 /* Test for legal loop header: Block, Phi, ... */
371 if (!is_possible_loop_head(n))
374 if (!is_outermost_Start(n)) {
376 int uplink = get_irn_uplink(root);
380 arity = get_irn_arity(n);
381 for (i = get_start_index(n); i < arity; i++) {
383 if (is_backedge(n, i))
385 pred = get_irn_n(n, i);
386 if (! irn_is_in_stack(pred)) {
389 assert(get_irn_uplink(pred) >= uplink);
394 return some_outof_loop & some_in_loop;
398 * Returns non-zero if n is possible loop head of an endless loop.
399 * I.e., it is a Block or Phi node and has only predecessors
402 * @param n the node to check
403 * @param root only needed for assertion.
405 static int is_endless_head(ir_node *n, ir_node *root)
408 int none_outof_loop = 1, some_in_loop = 0;
410 /* Test for legal loop header: Block, Phi, ... */
411 if (!is_possible_loop_head(n))
414 if (!is_outermost_Start(n)) {
416 int uplink = get_irn_uplink(root);
420 arity = get_irn_arity(n);
421 for (i = get_start_index(n); i < arity; i++) {
423 if (is_backedge(n, i))
425 pred = get_irn_n(n, i);
426 if (!irn_is_in_stack(pred)) {
429 assert(get_irn_uplink(pred) >= uplink);
434 return none_outof_loop & some_in_loop;
437 /** Returns index of the predecessor with the smallest dfn number
438 greater-equal than limit. */
439 static int smallest_dfn_pred(ir_node *n, int limit)
441 int i, index = -2, min = -1;
443 if (!is_outermost_Start(n)) {
444 int arity = get_irn_arity(n);
445 for (i = get_start_index(n); i < arity; i++) {
446 ir_node *pred = get_irn_n(n, i);
447 if (is_backedge(n, i) || !irn_is_in_stack(pred))
449 if (get_irn_dfn(pred) >= limit && (min == -1 || get_irn_dfn(pred) < min)) {
451 min = get_irn_dfn(pred);
459 * Returns index of the predecessor with the largest dfn number.
461 static int largest_dfn_pred(ir_node *n)
463 int i, index = -2, max = -1;
465 if (!is_outermost_Start(n)) {
466 int arity = get_irn_arity(n);
467 for (i = get_start_index(n); i < arity; i++) {
468 ir_node *pred = get_irn_n(n, i);
469 if (is_backedge (n, i) || !irn_is_in_stack(pred))
471 if (get_irn_dfn(pred) > max) {
473 max = get_irn_dfn(pred);
481 * Searches the stack for possible loop heads. Tests these for backedges.
482 * If it finds a head with an unmarked backedge it marks this edge and
483 * returns the tail of the loop.
484 * If it finds no backedge returns NULL.
485 * ("disable_backedge" in fiasco)
487 * @param n A node where uplink == dfn.
489 static ir_node *find_tail(ir_node *n)
492 int i, res_index = -2;
494 m = stack[tos-1]; /* tos = top of stack */
496 res_index = smallest_dfn_pred(m, 0);
497 if ((res_index == -2) && /* no smallest dfn pred found. */
501 if (m == n) return NULL; // Is this to catch Phi - self loops?
502 for (i = tos-2; i >= 0; --i) {
506 res_index = smallest_dfn_pred(m, get_irn_dfn(m) + 1);
507 if (res_index == -2) /* no smallest dfn pred found. */
508 res_index = largest_dfn_pred(m);
510 if ((m == n) && (res_index == -2)) { /* don't walk past loop head. */
516 /* We should not walk past our selves on the stack: The upcoming nodes
517 are not in this loop. We assume a loop not reachable from Start. */
525 /* A dead loop not reachable from Start. */
526 for (i = tos-2; i >= 0; --i) {
528 if (is_endless_head(m, n)) {
529 res_index = smallest_dfn_pred(m, get_irn_dfn(m) + 1);
530 if (res_index == -2) /* no smallest dfn pred found. */
531 res_index = largest_dfn_pred (m);
534 /* It's not an unreachable loop, either. */
538 //assert(0 && "no head found on stack");
542 if (res_index <= -2) {
543 /* It's a completely bad loop: without Phi/Block nodes that can
544 be a head. I.e., the code is "dying". We break the loop by
545 setting Bad nodes. */
546 ir_graph *irg = get_irn_irg(n);
547 ir_mode *mode = get_irn_mode(n);
548 ir_node *bad = new_r_Bad(irg, mode);
549 int arity = get_irn_arity(n);
550 for (i = -1; i < arity; ++i) {
551 set_irn_n(n, i, bad);
555 assert(res_index > -2);
557 set_backedge(m, res_index);
558 return is_outermost_Start(n) ? NULL : get_irn_n(m, res_index);
561 static inline int is_outermost_loop(ir_loop *l)
563 return l == get_loop_outer_loop(l);
566 /*-----------------------------------------------------------*
567 * The core algorithm. *
568 *-----------------------------------------------------------*/
571 * The core algorithm: Find strongly coupled components.
573 * @param n node to start
575 static void scc(ir_node *n)
577 if (irn_visited_else_mark(n))
580 /* Initialize the node */
581 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
582 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
583 set_irn_loop(n, NULL);
587 /* AS: get_start_index might return -1 for Control Flow Nodes, and thus a negative
588 array index would be passed to is_backedge(). But CFG Nodes dont't have a backedge array,
589 so is_backedge does not access array[-1] but correctly returns false! */
591 if (!is_outermost_Start(n)) {
592 int i, arity = get_irn_arity(n);
594 for (i = get_start_index(n); i < arity; ++i) {
596 if (is_backedge(n, i))
600 if (irn_is_in_stack(m)) {
601 /* Uplink of m is smaller if n->m is a backedge.
602 Propagate the uplink to mark the loop. */
603 if (get_irn_uplink(m) < get_irn_uplink(n))
604 set_irn_uplink(n, get_irn_uplink(m));
609 if (get_irn_dfn(n) == get_irn_uplink(n)) {
610 /* This condition holds for
611 1) the node with the incoming backedge.
612 That is: We found a loop!
613 2) Straight line code, because no uplink has been propagated, so the
614 uplink still is the same as the dfn.
616 But n might not be a proper loop head for the analysis. Proper loop
617 heads are Block and Phi nodes. find_tail() searches the stack for
618 Block's and Phi's and takes those nodes as loop heads for the current
619 loop instead and marks the incoming edge as backedge. */
621 ir_node *tail = find_tail(n);
623 /* We have a loop, that is no straight line code,
624 because we found a loop head!
625 Next actions: Open a new loop on the loop tree and
626 try to find inner loops */
628 /* This is an adaption of the algorithm from fiasco / optscc to
629 * avoid loops without Block or Phi as first node. This should
630 * severely reduce the number of evaluations of nodes to detect
631 * a fixpoint in the heap analysis.
632 * Further it avoids loops without firm nodes that cause errors
633 * in the heap analyses.
634 * But attention: don't do it for the outermost loop: This loop
635 * is not iterated. A first block can be a loop head in case of
636 * an endless recursion. */
640 if ((get_loop_n_elements(current_loop) > 0) || (is_outermost_loop(current_loop))) {
648 /* Remove the loop from the stack ... */
649 pop_scc_unmark_visit(n);
651 /* The current backedge has been marked, that is temporarily eliminated,
652 by find tail. Start the scc algorithm
653 again on the subgraph that is left (the current loop without the backedge)
654 in order to find more inner loops. */
657 assert(irn_visited(n));
661 /* No loop head was found, that is we have straight line code.
662 Pop all nodes from the stack to the current loop. */
668 void construct_backedges(ir_graph *irg)
670 ir_graph *rem = current_ir_graph;
674 current_ir_graph = irg;
675 outermost_ir_graph = irg;
678 init_scc(irg, &temp);
681 new_loop(); /* sets current_loop */
682 head_rem = current_loop; /* Just for assertion */
684 inc_irg_visited(irg);
686 scc(get_irg_end(irg));
689 obstack_free(&temp, NULL);
691 assert(head_rem == current_loop);
692 mature_loops(current_loop, get_irg_obstack(irg));
693 set_irg_loop(irg, current_loop);
694 add_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO);
695 assert(get_irg_loop(irg)->kind == k_ir_loop);
696 current_ir_graph = rem;
699 static void reset_backedges(ir_node *n)
701 if (is_possible_loop_head(n)) {
706 static void loop_reset_node(ir_node *n, void *env)
709 set_irn_loop(n, NULL);
713 void free_loop_information(ir_graph *irg)
715 irg_walk_graph(irg, loop_reset_node, NULL, NULL);
716 set_irg_loop(irg, NULL);
717 clear_irg_properties(current_ir_graph, IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO);
718 /* We cannot free the loop nodes, they are on the obstack. */
721 void free_all_loop_information(void)
724 for (i = 0; i < get_irp_n_irgs(); i++) {
725 free_loop_information(get_irp_irg(i));
729 /* ------------------------------------------------------------------- */
730 /* Simple analyses based on the loop information */
731 /* ------------------------------------------------------------------- */
733 static int is_loop_variant(ir_loop *l, ir_loop *b)
737 if (l == b) return 1;
739 n_elems = get_loop_n_elements(l);
740 for (i = 0; i < n_elems; ++i) {
741 loop_element e = get_loop_element(l, i);
742 if (is_ir_loop(e.kind))
743 if (is_loop_variant(e.son, b))
750 int is_loop_invariant(const ir_node *n, const ir_node *block)
752 ir_loop *l = get_irn_loop(block);
753 const ir_node *b = is_Block(n) ? n : get_nodes_block(n);
754 return !is_loop_variant(l, get_irn_loop(b));