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"
44 /* A variant of the loop tree that avoids loops without head.
45 This reduces the depth of the loop tree. */
46 #define NO_LOOPS_WITHOUT_HEAD 1
48 /** The outermost graph the scc is computed for. */
49 static ir_graph *outermost_ir_graph;
50 /** Current loop construction is working on. */
51 static ir_loop *current_loop;
52 /** Counts the number of allocated loop nodes.
53 * Each loop node gets a unique number.
54 * @todo What for? ev. remove.
56 static int loop_node_cnt = 0;
57 /** Counter to generate depth first numbering of visited nodes. */
58 static int current_dfn = 1;
60 static unsigned max_loop_depth = 0;
62 void link_to_reg_end(ir_node *n, void *env);
63 void set_projx_link(ir_node *cb_projx, ir_node *end_projx);
64 ir_node *get_projx_link(ir_node *cb_projx);
66 /**********************************************************************/
67 /* Node attributes **/
68 /**********************************************************************/
70 /**********************************************************************/
71 /* Node attributes needed for the construction. **/
72 /**********************************************************************/
74 typedef struct scc_info {
75 int in_stack; /**< Marks whether node is on the stack. */
76 int dfn; /**< Depth first search number. */
77 int uplink; /**< dfn number of ancestor. */
78 /* ir_loop *loop; *//* Refers to the containing loop. */
80 struct section *section;
87 * Allocates a new SCC info on the given obstack.
89 static inline scc_info *new_scc_info(struct obstack *obst)
91 return OALLOCZ(obst, scc_info);
95 * Mark node n being on the SCC stack.
97 static inline void mark_irn_in_stack(ir_node *n)
99 scc_info *scc = (scc_info*) get_irn_link(n);
105 * Mark node n NOT being on the SCC stack.
107 static inline void mark_irn_not_in_stack(ir_node *n)
109 scc_info *scc = (scc_info*) get_irn_link(n);
115 * Checks if a node is on the SCC stack.
117 static inline int irn_is_in_stack(ir_node *n)
119 scc_info *scc = (scc_info*) get_irn_link(n);
121 return scc->in_stack;
125 * Sets the uplink number for a node.
127 static inline void set_irn_uplink(ir_node *n, int uplink)
129 scc_info *scc = (scc_info*) get_irn_link(n);
131 scc->uplink = uplink;
135 * Returns the uplink number for a node.
137 static int get_irn_uplink(ir_node *n)
139 scc_info *scc = (scc_info*) get_irn_link(n);
145 * Sets the depth-first-search number for a node.
147 static inline void set_irn_dfn(ir_node *n, int dfn)
149 scc_info *scc = (scc_info*) get_irn_link(n);
155 * Returns the depth-first-search number of a node.
157 static int get_irn_dfn(ir_node *n)
159 scc_info *scc = (scc_info*) get_irn_link(n);
165 static ir_loop *find_nodes_loop(ir_node *n, ir_loop *l)
170 /* Test whether n is contained in this loop. */
171 for (i = 0; i < get_loop_n_nodes(l); i++)
172 if (n == get_loop_node(l, i)) return l;
174 /* Is this a leave in the loop tree? If so loop not found. */
175 if (get_loop_n_sons(l) == 0) return NULL;
177 /* Else descend in the loop tree. */
178 for (i = 0; i < get_loop_n_sons(l); i++) {
179 res = find_nodes_loop(n, get_loop_son(l, i));
185 /* @@@ temporary implementation, costly!!! */
186 ir_loop * get_irn_loop(ir_node *n)
188 ir_loop *l = get_irg_loop(current_ir_graph);
189 l = find_nodes_loop(n, l);
194 /**********************************************************************/
196 /**********************************************************************/
198 static ir_node **stack = NULL;
199 static size_t tos = 0; /* top of stack */
202 * initializes the stack
204 static inline void init_stack(void)
207 ARR_RESIZE(ir_node *, stack, 1000);
209 stack = NEW_ARR_F(ir_node *, 1000);
217 static void finish_stack(void)
224 * push a node onto the stack
226 * @param n The node to push
228 static inline void push(ir_node *n)
230 if (tos == ARR_LEN(stack)) {
231 size_t nlen = ARR_LEN(stack) * 2;
232 ARR_RESIZE(ir_node *, stack, nlen);
235 mark_irn_in_stack(n);
239 * pop a node from the stack
241 * @return The topmost node
243 static inline ir_node *pop(void)
249 mark_irn_not_in_stack(n);
254 * The nodes up to n belong to the current loop.
255 * Removes them from the stack and adds them to the current loop.
257 static inline void pop_scc_to_loop(ir_node *n)
265 set_irn_dfn(m, loop_node_cnt);
266 add_loop_node(current_loop, m);
267 set_irn_loop(m, current_loop);
271 /* GL ??? my last son is my grandson??? Removes loops with no
272 ir_nodes in them. Such loops have only another loop as son. (Why
273 can't they have two loops as sons? Does it never get that far? ) */
274 static void close_loop(ir_loop *l)
276 size_t last = get_loop_n_elements(l) - 1;
277 loop_element lelement = get_loop_element(l, last);
278 ir_loop *last_son = lelement.son;
280 if (get_kind(last_son) == k_ir_loop &&
281 get_loop_n_elements(last_son) == 1) {
284 lelement = get_loop_element(last_son, 0);
287 if (get_kind(gson) == k_ir_loop) {
288 loop_element new_last_son;
290 gson->outer_loop = l;
291 new_last_son.son = gson;
292 l->children[last] = new_last_son;
299 /* Removes and unmarks all nodes up to n from the stack.
300 The nodes must be visited once more to assign them to a scc. */
301 static inline void pop_scc_unmark_visit(ir_node *n)
307 set_irn_visited(m, 0);
311 /**********************************************************************/
312 /* The loop datastructure. **/
313 /**********************************************************************/
315 /* Allocates a new loop as son of current_loop. Sets current_loop
316 to the new loop and returns the father. */
317 static ir_loop *new_loop(void)
319 ir_loop *father = current_loop;
320 ir_loop *son = alloc_loop(father, outermost_ir_graph->obst);
322 if (son->depth > max_loop_depth) max_loop_depth = son->depth;
327 /**********************************************************************/
328 /* Constructing and destructing the loop/backedge information. **/
329 /**********************************************************************/
331 /* Initialization steps. **********************************************/
333 static inline void init_node(ir_node *n, void *env)
335 struct obstack *obst = (struct obstack*) env;
336 set_irn_link(n, new_scc_info(obst));
340 static inline void init_scc_common(void)
347 static inline void init_scc(ir_graph *irg, struct obstack *obst)
350 irg_walk_graph(irg, init_node, NULL, obst);
352 irg_walk (irg, link_to_reg_end, NULL, NULL);
356 static inline void finish_scc(void)
362 * Check weather a given node represents the outer most Start
363 * block. In intra-procedural view this is the start block of the
364 * current graph, in interprocedural view it is the start block
365 * of the outer most graph.
367 * @param n the node to check
369 * This is the condition for breaking the scc recursion.
371 static int is_outermost_Start(ir_node *n)
373 /* Test whether this is the outermost Start node. */
374 if (is_Block(n) && get_Block_n_cfgpreds(n) == 1) {
375 ir_node *pred = skip_Proj(get_Block_cfgpred(n, 0));
376 if (is_Start(pred) && get_nodes_block(pred) == n)
382 /* When to walk from nodes to blocks. Only for Control flow operations? */
383 static inline int get_start_index(ir_node *n)
385 #undef BLOCK_BEFORE_NODE
386 #define BLOCK_BEFORE_NODE 1
388 #if BLOCK_BEFORE_NODE
390 /* This version assures, that all nodes are ordered absolutely. This allows
391 to undef all nodes in the heap analysis if the block is false, which
393 I.e., with this code, the order on the loop tree is correct. But a
394 (single) test showed the loop tree is deeper. */
395 if (get_irn_op(n) == op_Phi ||
397 (get_irg_pinned(get_irn_irg(n)) == op_pin_state_floats &&
398 get_irn_pinned(n) == op_pin_state_floats))
399 // Here we could test for backedge at -1 which is illegal
406 /* This version causes deeper loop trees (at least we verified this
408 But it guarantees that Blocks are analysed before nodes contained in the
409 block. If so, we can set the value to undef if the block is not \
411 if (is_cfop(n) || is_fragile_op(n) || is_Start(n))
420 * Return non-zero if the given node is a legal loop header:
423 * @param n the node to check
425 static inline int is_possible_loop_head(ir_node *n)
427 ir_op *op = get_irn_op(n);
428 return ((op == op_Block) ||
433 * Returns non-zero if n is a loop header, i.e., it is a Block or Phi
434 * node and has predecessors within the loop and out of the loop.
436 * @param n the node to check
437 * @param root only needed for assertion.
439 static int is_head(ir_node *n, ir_node *root)
442 int some_outof_loop = 0, some_in_loop = 0;
444 /* Test for legal loop header: Block, Phi, ... */
445 if (!is_possible_loop_head(n))
448 if (!is_outermost_Start(n)) {
450 int uplink = get_irn_uplink(root);
454 arity = get_irn_arity(n);
455 for (i = get_start_index(n); i < arity; i++) {
457 if (is_backedge(n, i))
459 pred = get_irn_n(n, i);
460 if (! irn_is_in_stack(pred)) {
463 assert(get_irn_uplink(pred) >= uplink);
468 return some_outof_loop & some_in_loop;
472 * Returns non-zero if n is possible loop head of an endless loop.
473 * I.e., it is a Block or Phi node and has only predecessors
476 * @param n the node to check
477 * @param root only needed for assertion.
479 static int is_endless_head(ir_node *n, ir_node *root)
482 int none_outof_loop = 1, some_in_loop = 0;
484 /* Test for legal loop header: Block, Phi, ... */
485 if (!is_possible_loop_head(n))
488 if (!is_outermost_Start(n)) {
490 int uplink = get_irn_uplink(root);
494 arity = get_irn_arity(n);
495 for (i = get_start_index(n); i < arity; i++) {
497 if (is_backedge(n, i))
499 pred = get_irn_n(n, i);
500 if (!irn_is_in_stack(pred)) {
503 assert(get_irn_uplink(pred) >= uplink);
508 return none_outof_loop & some_in_loop;
511 /** Returns index of the predecessor with the smallest dfn number
512 greater-equal than limit. */
513 static int smallest_dfn_pred(ir_node *n, int limit)
515 int i, index = -2, min = -1;
517 if (!is_outermost_Start(n)) {
518 int arity = get_irn_arity(n);
519 for (i = get_start_index(n); i < arity; i++) {
520 ir_node *pred = get_irn_n(n, i);
521 if (is_backedge(n, i) || !irn_is_in_stack(pred))
523 if (get_irn_dfn(pred) >= limit && (min == -1 || get_irn_dfn(pred) < min)) {
525 min = get_irn_dfn(pred);
533 * Returns index of the predecessor with the largest dfn number.
535 static int largest_dfn_pred(ir_node *n)
537 int i, index = -2, max = -1;
539 if (!is_outermost_Start(n)) {
540 int arity = get_irn_arity(n);
541 for (i = get_start_index(n); i < arity; i++) {
542 ir_node *pred = get_irn_n(n, i);
543 if (is_backedge (n, i) || !irn_is_in_stack(pred))
545 if (get_irn_dfn(pred) > max) {
547 max = get_irn_dfn(pred);
555 * Searches the stack for possible loop heads. Tests these for backedges.
556 * If it finds a head with an unmarked backedge it marks this edge and
557 * returns the tail of the loop.
558 * If it finds no backedge returns NULL.
559 * ("disable_backedge" in fiasco)
561 * @param n A node where uplink == dfn.
563 static ir_node *find_tail(ir_node *n)
566 int i, res_index = -2;
569 if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
571 m = stack[tos-1]; /* tos = top of stack */
573 res_index = smallest_dfn_pred(m, 0);
574 if ((res_index == -2) && /* no smallest dfn pred found. */
578 if (m == n) return NULL; // Is this to catch Phi - self loops?
579 for (i = tos-2; i >= 0; --i) {
583 res_index = smallest_dfn_pred(m, get_irn_dfn(m) + 1);
584 if (res_index == -2) /* no smallest dfn pred found. */
585 res_index = largest_dfn_pred(m);
587 if ((m == n) && (res_index == -2)) { /* don't walk past loop head. */
593 /* We should not walk past our selves on the stack: The upcoming nodes
594 are not in this loop. We assume a loop not reachable from Start. */
602 /* A dead loop not reachable from Start. */
603 for (i = tos-2; i >= 0; --i) {
605 if (is_endless_head(m, n)) {
606 res_index = smallest_dfn_pred(m, get_irn_dfn(m) + 1);
607 if (res_index == -2) /* no smallest dfn pred found. */
608 res_index = largest_dfn_pred (m);
611 /* It's not an unreachable loop, either. */
615 //assert(0 && "no head found on stack");
619 if (res_index <= -2) {
620 /* It's a completely bad loop: without Phi/Block nodes that can
621 be a head. I.e., the code is "dying". We break the loop by
622 setting Bad nodes. */
623 ir_graph *irg = get_irn_irg(n);
624 ir_mode *mode = get_irn_mode(n);
625 ir_node *bad = new_r_Bad(irg, mode);
626 int arity = get_irn_arity(n);
627 for (i = -1; i < arity; ++i) {
628 set_irn_n(n, i, bad);
632 assert(res_index > -2);
634 set_backedge(m, res_index);
635 return is_outermost_Start(n) ? NULL : get_irn_n(m, res_index);
638 static inline int is_outermost_loop(ir_loop *l)
640 return l == get_loop_outer_loop(l);
643 /*-----------------------------------------------------------*
644 * The core algorithm. *
645 *-----------------------------------------------------------*/
648 * The core algorithm: Find strongly coupled components.
650 * @param n node to start
652 static void scc(ir_node *n)
654 if (irn_visited_else_mark(n))
657 /* Initialize the node */
658 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
659 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
660 set_irn_loop(n, NULL);
664 /* AS: get_start_index might return -1 for Control Flow Nodes, and thus a negative
665 array index would be passed to is_backedge(). But CFG Nodes dont't have a backedge array,
666 so is_backedge does not access array[-1] but correctly returns false! */
668 if (!is_outermost_Start(n)) {
669 int i, arity = get_irn_arity(n);
671 for (i = get_start_index(n); i < arity; ++i) {
673 if (is_backedge(n, i))
677 if (irn_is_in_stack(m)) {
678 /* Uplink of m is smaller if n->m is a backedge.
679 Propagate the uplink to mark the loop. */
680 if (get_irn_uplink(m) < get_irn_uplink(n))
681 set_irn_uplink(n, get_irn_uplink(m));
686 if (get_irn_dfn(n) == get_irn_uplink(n)) {
687 /* This condition holds for
688 1) the node with the incoming backedge.
689 That is: We found a loop!
690 2) Straight line code, because no uplink has been propagated, so the
691 uplink still is the same as the dfn.
693 But n might not be a proper loop head for the analysis. Proper loop
694 heads are Block and Phi nodes. find_tail() searches the stack for
695 Block's and Phi's and takes those nodes as loop heads for the current
696 loop instead and marks the incoming edge as backedge. */
698 ir_node *tail = find_tail(n);
700 /* We have a loop, that is no straight line code,
701 because we found a loop head!
702 Next actions: Open a new loop on the loop tree and
703 try to find inner loops */
705 #if NO_LOOPS_WITHOUT_HEAD
706 /* This is an adaption of the algorithm from fiasco / optscc to
707 * avoid loops without Block or Phi as first node. This should
708 * severely reduce the number of evaluations of nodes to detect
709 * a fixpoint in the heap analysis.
710 * Further it avoids loops without firm nodes that cause errors
711 * in the heap analyses.
712 * But attention: don't do it for the outermost loop: This loop
713 * is not iterated. A first block can be a loop head in case of
714 * an endless recursion. */
718 if ((get_loop_n_elements(current_loop) > 0) || (is_outermost_loop(current_loop))) {
726 ir_loop *l = new_loop();
729 /* Remove the loop from the stack ... */
730 pop_scc_unmark_visit(n);
732 /* The current backedge has been marked, that is temporarily eliminated,
733 by find tail. Start the scc algorithm
734 again on the subgraph that is left (the current loop without the backedge)
735 in order to find more inner loops. */
738 assert(irn_visited(n));
739 #if NO_LOOPS_WITHOUT_HEAD
744 /* No loop head was found, that is we have straight line code.
745 Pop all nodes from the stack to the current loop. */
751 /* Constructs backedge information for irg. In interprocedural view constructs
752 backedges for all methods called by irg, too. */
753 int construct_backedges(ir_graph *irg)
755 ir_graph *rem = current_ir_graph;
760 current_ir_graph = irg;
761 outermost_ir_graph = irg;
764 init_scc(irg, &temp);
767 new_loop(); /* sets current_loop */
768 head_rem = current_loop; /* Just for assertion */
770 inc_irg_visited(irg);
772 scc(get_irg_end(irg));
775 obstack_free(&temp, NULL);
777 assert(head_rem == current_loop);
778 mature_loops(current_loop, irg->obst);
779 set_irg_loop(irg, current_loop);
780 set_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_LOOPINFO);
781 assert(get_irg_loop(irg)->kind == k_ir_loop);
782 current_ir_graph = rem;
783 return max_loop_depth;
786 static void reset_backedges(ir_node *n)
788 if (is_possible_loop_head(n)) {
794 static void loop_reset_backedges(ir_loop *l)
797 reset_backedges(get_loop_node(l, 0));
798 for (i = 0; i < get_loop_n_nodes(l); ++i)
799 set_irn_loop(get_loop_node(l, i), NULL);
800 for (i = 0; i < get_loop_n_sons(l); ++i) {
801 loop_reset_backedges(get_loop_son(l, i));
806 static void loop_reset_node(ir_node *n, void *env)
809 set_irn_loop(n, NULL);
813 /** Removes all loop information.
814 Resets all backedges */
815 void free_loop_information(ir_graph *irg)
817 /* We can not use this recursion, as the loop might contain
818 illegal nodes by now. Why else would we throw away the
820 if (get_irg_loop(irg)) loop_reset_backedges(get_irg_loop(irg));
822 irg_walk_graph(irg, loop_reset_node, NULL, NULL);
823 set_irg_loop(irg, NULL);
824 clear_irg_state(current_ir_graph, IR_GRAPH_STATE_CONSISTENT_LOOPINFO);
825 /* We cannot free the loop nodes, they are on the obstack. */
828 void free_all_loop_information(void)
831 for (i = 0; i < get_irp_n_irgs(); i++) {
832 free_loop_information(get_irp_irg(i));
836 /* ------------------------------------------------------------------- */
837 /* Simple analyses based on the loop information */
838 /* ------------------------------------------------------------------- */
840 static int is_loop_variant(ir_loop *l, ir_loop *b)
844 if (l == b) return 1;
846 n_elems = get_loop_n_elements(l);
847 for (i = 0; i < n_elems; ++i) {
848 loop_element e = get_loop_element(l, i);
849 if (is_ir_loop(e.kind))
850 if (is_loop_variant(e.son, b))
857 /* Test whether a value is loop invariant.
859 * @param n The node to be tested.
860 * @param block A block node. We pass the block, not the loop as we must
861 * start off with a block loop to find all proper uses.
863 * Returns non-zero, if the node n is not changed in the loop block
864 * belongs to or in inner loops of this blocks loop. */
865 int is_loop_invariant(const ir_node *n, const ir_node *block)
867 ir_loop *l = get_irn_loop(block);
868 const ir_node *b = is_Block(n) ? n : get_nodes_block(n);
869 return !is_loop_variant(l, get_irn_loop(b));