2 * Copyright (C) 1995-2008 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 backedge/cfloop
23 * datastructures. A variation on the Tarjan algorithm. See also
24 * [Trapp:99], Chapter 5.2.1.2.
25 * @author Goetz Lindenmaier
39 #include "irgraph_t.h"
46 #define NO_CFLOOPS_WITHOUT_HEAD 1
48 /** The outermost graph the scc is computed for */
49 static ir_graph *outermost_ir_graph;
50 /** Current cfloop construction is working on. */
51 static ir_loop *current_loop;
52 /** Counts the number of allocated cfloop nodes.
53 * Each cfloop 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 int max_loop_depth = 0;
62 void link_to_reg_end(ir_node *n, void *env);
64 /**********************************************************************/
65 /* Node attributes **/
66 /**********************************************************************/
68 /**********************************************************************/
69 /* Node attributes needed for the construction. **/
70 /**********************************************************************/
73 * The SCC info. Additional fields for an ir-node needed for the
76 typedef struct scc_info {
77 int in_stack; /**< Marks whether node is on the stack. */
78 int dfn; /**< Depth first search number. */
79 int uplink; /**< dfn number of ancestor. */
82 /** Allocate a new scc_info on the given obstack */
83 static INLINE scc_info *new_scc_info(struct obstack *obst) {
84 scc_info *info = obstack_alloc(obst, sizeof(*info));
85 memset(info, 0, sizeof(*info));
90 * Marks the node n to be on the stack.
92 static INLINE void mark_irn_in_stack(ir_node *n) {
93 scc_info *info = get_irn_link(n);
98 * Marks the node n to be not on the stack.
100 static INLINE void mark_irn_not_in_stack(ir_node *n) {
101 scc_info *info = get_irn_link(n);
106 * Returns whether node n is on the stack.
108 static INLINE int irn_is_in_stack(ir_node *n) {
109 scc_info *info = get_irn_link(n);
110 return info->in_stack;
114 * Sets node n uplink value.
116 static INLINE void set_irn_uplink(ir_node *n, int uplink) {
117 scc_info *info = get_irn_link(n);
118 info->uplink = uplink;
122 * Return node n uplink value.
124 static INLINE int get_irn_uplink(ir_node *n) {
125 scc_info *info = get_irn_link(n);
130 * Sets node n dfn value.
132 static INLINE void set_irn_dfn(ir_node *n, int dfn) {
133 scc_info *info = get_irn_link(n);
138 * Returns node n dfn value.
140 static INLINE int get_irn_dfn(ir_node *n) {
141 scc_info *info = get_irn_link(n);
145 /**********************************************************************/
147 /**********************************************************************/
149 /** An IR-node stack */
150 static ir_node **stack = NULL;
151 /** The top (index) of the IR-node stack */
155 * Initializes the IR-node stack
157 static INLINE void init_stack(void) {
159 ARR_RESIZE(ir_node *, stack, 1000);
161 stack = NEW_ARR_F(ir_node *, 1000);
167 * Push a node n onto the IR-node stack.
169 static INLINE void push(ir_node *n) {
170 if (tos == ARR_LEN(stack)) {
171 int nlen = ARR_LEN(stack) * 2;
172 ARR_RESIZE(ir_node *, stack, nlen);
175 mark_irn_in_stack(n);
179 * Pop a node from the IR-node stack and return it.
181 static INLINE ir_node *pop(void) {
182 ir_node *n = stack[--tos];
183 mark_irn_not_in_stack(n);
188 * The nodes from tos up to n belong to the current loop.
189 * Removes them from the stack and adds them to the current loop.
191 static INLINE void pop_scc_to_loop(ir_node *n) {
197 set_irn_dfn(m, loop_node_cnt);
198 add_loop_node(current_loop, m);
199 set_irn_loop(m, current_loop);
203 /* GL ??? my last son is my grandson??? Removes cfloops with no
204 ir_nodes in them. Such loops have only another loop as son. (Why
205 can't they have two loops as sons? Does it never get that far? ) */
206 static void close_loop(ir_loop *l) {
207 int last = get_loop_n_elements(l) - 1;
208 loop_element lelement = get_loop_element(l, last);
209 ir_loop *last_son = lelement.son;
211 if (get_kind(last_son) == k_ir_loop &&
212 get_loop_n_elements(last_son) == 1) {
215 lelement = get_loop_element(last_son, 0);
217 if (get_kind(gson) == k_ir_loop) {
218 loop_element new_last_son;
220 gson->outer_loop = l;
221 new_last_son.son = gson;
222 l->children[last] = new_last_son;
224 /* the loop last_son is dead now, recover at least some memory */
225 DEL_ARR_F(last_son->children);
233 * Removes and unmarks all nodes up to n from the stack.
234 * The nodes must be visited once more to assign them to a scc.
236 static INLINE void pop_scc_unmark_visit(ir_node *n) {
241 set_irn_visited(m, 0);
245 /**********************************************************************/
246 /* The loop datastructure. **/
247 /**********************************************************************/
250 * Allocates a new loop as son of current_loop. Sets current_loop
251 * to the new loop and returns its father.
252 * The loop is allocated on the outermost_ir_graphs's obstack.
254 static ir_loop *new_loop(void) {
255 ir_loop *father = current_loop;
256 ir_loop *son = alloc_loop(father, outermost_ir_graph->obst);
258 if (son->depth > max_loop_depth) max_loop_depth = son->depth;
263 /**********************************************************************/
264 /* Constructing and destructing the loop/backedge information. **/
265 /**********************************************************************/
267 /* Initialization steps. **********************************************/
270 * Allocates a scc_info for every Block node n.
271 * Clear the backedges for all nodes.
272 * Called from a walker.
274 static INLINE void init_node(ir_node *n, void *env) {
275 struct obstack *obst = env;
277 set_irn_link(n, new_scc_info(obst));
282 * Initializes the common global settings for the scc algorithm
284 static INLINE void init_scc_common(void) {
291 * Initializes the scc algorithm for the intraprocedural case.
292 * Add scc info to every block node.
294 static INLINE void init_scc(ir_graph *irg, struct obstack *obst) {
296 irg_walk_graph(irg, init_node, NULL, obst);
299 #ifdef INTERPROCEDURAL_VIEW
301 * Initializes the scc algorithm for the interprocedural case.
303 static INLINE void init_ip_scc(struct obstack *obst) {
305 cg_walk(init_node, NULL, obst);
307 #if EXPERIMENTAL_CFLOOP_TREE
308 cg_walk(link_to_reg_end, NULL, NULL);
314 * Condition for breaking the recursion: n is the block
315 * that gets the initial control flow from the Start node.
317 static int is_outermost_StartBlock(ir_node *n) {
318 /* Test whether this is the outermost Start node. If so
319 recursion must end. */
321 if (get_Block_n_cfgpreds(n) == 1 &&
322 is_Start(skip_Proj(get_Block_cfgpred(n, 0))) &&
323 get_Block_cfgpred_block(n, 0) == n) {
329 /** Returns non-zero if n is a loop header, i.e., it is a Block node
330 * and has predecessors within the cfloop and out of the cfloop.
332 * @param n the block node to check
333 * @param root only needed for assertion.
335 static int is_head(ir_node *n, ir_node *root) {
337 int some_outof_loop = 0, some_in_loop = 0;
341 if (!is_outermost_StartBlock(n)) {
342 arity = get_Block_n_cfgpreds(n);
343 for (i = 0; i < arity; i++) {
344 ir_node *pred = get_Block_cfgpred_block(n, i);
345 /* ignore Bad control flow: it cannot happen */
348 if (is_backedge(n, i))
350 if (!irn_is_in_stack(pred)) {
353 assert(get_irn_uplink(pred) >= get_irn_uplink(root));
358 return some_outof_loop & some_in_loop;
363 * Returns non-zero if n is possible loop head of an endless loop.
364 * I.e., it is a Block node and has only predecessors
367 * @param n the block node to check
368 * @param root only needed for assertion.
370 static int is_endless_head(ir_node *n, ir_node *root) {
372 int none_outof_loop = 1, some_in_loop = 0;
375 /* Test for legal loop header: Block, Phi, ... */
376 if (!is_outermost_StartBlock(n)) {
377 arity = get_Block_n_cfgpreds(n);
378 for (i = 0; i < arity; i++) {
379 ir_node *pred = get_Block_cfgpred_block(n, i);
380 /* ignore Bad control flow: it cannot happen */
383 if (is_backedge(n, i))
385 if (!irn_is_in_stack(pred)) {
388 assert(get_irn_uplink(pred) >= get_irn_uplink(root));
393 return none_outof_loop && some_in_loop;
397 * Returns index of the predecessor with the smallest dfn number
398 * greater-equal than limit.
400 static int smallest_dfn_pred(ir_node *n, int limit) {
401 int i, index = -2, min = -1;
403 if (!is_outermost_StartBlock(n)) {
404 int arity = get_Block_n_cfgpreds(n);
405 for (i = 0; i < arity; i++) {
406 ir_node *pred = get_Block_cfgpred_block(n, i);
407 /* ignore Bad control flow: it cannot happen */
410 if (is_backedge(n, i) || !irn_is_in_stack(pred))
412 if (get_irn_dfn(pred) >= limit && (min == -1 || get_irn_dfn(pred) < min)) {
414 min = get_irn_dfn(pred);
422 * Returns index of the predecessor with the largest dfn number.
424 static int largest_dfn_pred(ir_node *n) {
425 int i, index = -2, max = -1;
427 if (!is_outermost_StartBlock(n)) {
428 int arity = get_Block_n_cfgpreds(n);
429 for (i = 0; i < arity; i++) {
430 ir_node *pred = get_Block_cfgpred_block(n, i);
431 /* ignore Bad control flow: it cannot happen */
434 if (is_backedge(n, i) || !irn_is_in_stack(pred))
436 if (get_irn_dfn(pred) > max) {
438 max = get_irn_dfn(pred);
446 * Searches the stack for possible loop heads. Tests these for backedges.
447 * If it finds a head with an unmarked backedge it marks this edge and
448 * returns the tail of the loop.
449 * If it finds no backedge returns NULL.
451 static ir_node *find_tail(ir_node *n) {
453 int i, res_index = -2;
455 m = stack[tos-1]; /* tos = top of stack */
457 res_index = smallest_dfn_pred(m, 0);
458 if ((res_index == -2) && /* no smallest dfn pred found. */
464 for (i = tos-2; i >= 0; --i) {
468 res_index = smallest_dfn_pred(m, get_irn_dfn(m) + 1);
469 if (res_index == -2) /* no smallest dfn pred found. */
470 res_index = largest_dfn_pred(m);
472 if ((m == n) && (res_index == -2)) {
479 /* We should not walk past our selves on the stack: The upcoming nodes
480 are not in this loop. We assume a loop not reachable from Start. */
488 /* A dead loop not reachable from Start. */
489 for (i = tos-2; i >= 0; --i) {
491 if (is_endless_head(m, n)) {
492 res_index = smallest_dfn_pred (m, get_irn_dfn(m) + 1);
493 if (res_index == -2) /* no smallest dfn pred found. */
494 res_index = largest_dfn_pred(m);
497 if (m == n) break; /* It's not an unreachable loop, either. */
499 //assert(0 && "no head found on stack");
502 assert(res_index > -2);
504 set_backedge(m, res_index);
505 return is_outermost_StartBlock(n) ? NULL : get_Block_cfgpred_block(m, res_index);
509 * returns non.zero if l is the outermost loop.
511 INLINE static int is_outermost_loop(ir_loop *l) {
512 return l == get_loop_outer_loop(l);
515 /*-----------------------------------------------------------*
516 * The core algorithm. *
517 *-----------------------------------------------------------*/
520 * Walks over all blocks of a graph
522 static void cfscc(ir_node *n) {
527 if (irn_visited(n)) return;
530 /* Initialize the node */
531 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
532 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
533 set_irn_loop(n, NULL);
537 if (!is_outermost_StartBlock(n)) {
538 int arity = get_Block_n_cfgpreds(n);
540 for (i = 0; i < arity; i++) {
543 if (is_backedge(n, i))
545 m = get_Block_cfgpred_block(n, i);
546 /* ignore Bad control flow: it cannot happen */
551 if (irn_is_in_stack(m)) {
552 /* Uplink of m is smaller if n->m is a backedge.
553 Propagate the uplink to mark the cfloop. */
554 if (get_irn_uplink(m) < get_irn_uplink(n))
555 set_irn_uplink(n, get_irn_uplink(m));
560 if (get_irn_dfn(n) == get_irn_uplink(n)) {
561 /* This condition holds for
562 1) the node with the incoming backedge.
563 That is: We found a cfloop!
564 2) Straight line code, because no uplink has been propagated, so the
565 uplink still is the same as the dfn.
567 But n might not be a proper cfloop head for the analysis. Proper cfloop
568 heads are Block and Phi nodes. find_tail searches the stack for
569 Block's and Phi's and takes those nodes as cfloop heads for the current
570 cfloop instead and marks the incoming edge as backedge. */
572 ir_node *tail = find_tail(n);
574 /* We have a cfloop, that is no straight line code,
575 because we found a cfloop head!
576 Next actions: Open a new cfloop on the cfloop tree and
577 try to find inner cfloops */
579 #if NO_CFLOOPS_WITHOUT_HEAD
581 /* This is an adaption of the algorithm from fiasco / optscc to
582 * avoid cfloops without Block or Phi as first node. This should
583 * severely reduce the number of evaluations of nodes to detect
584 * a fixpoint in the heap analysis.
585 * Further it avoids cfloops without firm nodes that cause errors
586 * in the heap analyses. */
590 if ((get_loop_n_elements(current_loop) > 0) || (is_outermost_loop(current_loop))) {
600 ir_loop *l = new_loop();
604 /* Remove the cfloop from the stack ... */
605 pop_scc_unmark_visit(n);
607 /* The current backedge has been marked, that is temporarily eliminated,
608 by find tail. Start the scc algorithm
609 anew on the subgraph thats left (the current cfloop without the backedge)
610 in order to find more inner cfloops. */
614 assert(irn_visited(n));
615 #if NO_CFLOOPS_WITHOUT_HEAD
620 /* AS: No cfloop head was found, that is we have straight line code.
621 Pop all nodes from the stack to the current cfloop. */
627 /* Constructs control flow backedge information for irg. */
628 int construct_cf_backedges(ir_graph *irg) {
629 ir_graph *rem = current_ir_graph;
631 ir_node *end = get_irg_end(irg);
635 assert(!get_interprocedural_view() &&
636 "use construct_ip_cf_backedges()");
639 current_ir_graph = irg;
640 outermost_ir_graph = irg;
643 init_scc(irg, &temp);
646 new_loop(); /* sets current_loop */
647 head_rem = current_loop; /* Just for assertion */
649 inc_irg_visited(irg);
651 /* walk over all blocks of the graph, including keep alives */
652 cfscc(get_irg_end_block(irg));
653 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
654 ir_node *el = get_End_keepalive(end, i);
658 obstack_free(&temp, NULL);
660 assert(head_rem == current_loop);
661 mature_loops(current_loop, irg->obst);
662 set_irg_loop(irg, current_loop);
663 set_irg_loopinfo_state(irg, loopinfo_cf_consistent);
664 assert(get_irg_loop(irg)->kind == k_ir_loop);
666 current_ir_graph = rem;
667 return max_loop_depth;
670 void assure_cf_loop(ir_graph *irg) {
671 irg_loopinfo_state state = get_irg_loopinfo_state(irg);
673 if (state != loopinfo_cf_consistent)
674 construct_cf_backedges(irg);
677 #ifdef INTERPROCEDURAL_VIEW
678 int construct_ip_cf_backedges (void) {
679 ir_graph *rem = current_ir_graph;
680 int rem_ipv = get_interprocedural_view();
684 assert(get_irp_ip_view_state() == ip_view_valid);
686 outermost_ir_graph = get_irp_main_irg();
692 new_loop(); /* sets current_loop */
693 set_interprocedural_view(1);
695 inc_max_irg_visited();
696 for (i = 0; i < get_irp_n_irgs(); i++)
697 set_irg_visited(get_irp_irg(i), get_max_irg_visited());
699 /** We have to start the walk at the same nodes as cg_walk. **/
700 /* Walk starting at unreachable procedures. Only these
701 * have End blocks visible in interprocedural view. */
702 for (i = 0; i < get_irp_n_irgs(); i++) {
704 current_ir_graph = get_irp_irg(i);
706 sb = get_irg_start_block(current_ir_graph);
708 if ((get_Block_n_cfgpreds(sb) > 1) ||
709 (get_nodes_block(get_Block_cfgpred(sb, 0)) != sb)) continue;
711 cfscc(get_irg_end_block(current_ir_graph));
714 /* Check whether we walked all procedures: there could be procedures
715 with cyclic calls but no call from the outside. */
716 for (i = 0; i < get_irp_n_irgs(); i++) {
718 current_ir_graph = get_irp_irg(i);
720 /* Test start block: if inner procedure end and end block are not
721 * visible and therefore not marked. */
722 sb = get_irg_start_block(current_ir_graph);
723 if (get_irn_visited(sb) < get_irg_visited(current_ir_graph)) cfscc(sb);
726 /* Walk all endless cfloops in inner procedures.
727 * We recognize an inner procedure if the End node is not visited. */
728 for (i = 0; i < get_irp_n_irgs(); i++) {
730 current_ir_graph = get_irp_irg(i);
732 e = get_irg_end(current_ir_graph);
733 if (get_irn_visited(e) < get_irg_visited(current_ir_graph)) {
735 /* Don't visit the End node. */
736 for (j = 0; j < get_End_n_keepalives(e); j++) {
737 ir_node *el = get_End_keepalive(e, j);
738 if (is_Block(el)) cfscc(el);
743 set_irg_loop(outermost_ir_graph, current_loop);
744 set_irg_loopinfo_state(current_ir_graph, loopinfo_cf_ip_consistent);
745 assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
747 obstack_free(&temp, NULL);
748 current_ir_graph = rem;
749 set_interprocedural_view(rem_ipv);
750 return max_loop_depth;
755 * Clear the intra- and the interprocedural
756 * backedge information pf a block.
758 static void reset_backedges(ir_node *block) {
761 assert(is_Block(block));
762 #ifdef INTERPROCEDURAL_VIEW
763 rem = get_interprocedural_view();
764 set_interprocedural_view(1);
765 clear_backedges(block);
766 set_interprocedural_view(0);
767 clear_backedges(block);
768 set_interprocedural_view(rem);
771 clear_backedges(block);
776 * Reset all backedges of the first block of
777 * a loop as well as all loop info for all nodes of this loop.
778 * Recurse into all nested loops.
780 static void loop_reset_backedges(ir_loop *l) {
782 reset_backedges(get_loop_node(l, 0));
783 for (i = 0; i < get_loop_n_nodes(l); ++i)
784 set_irn_loop(get_loop_node(l, i), NULL);
785 for (i = 0; i < get_loop_n_sons(l); ++i) {
786 loop_reset_backedges(get_loop_son(l, i));
790 /* Removes all cfloop information.
791 Resets all backedges */
792 void free_cfloop_information(ir_graph *irg) {
793 ir_loop *loop = get_irg_loop(irg);
795 loop_reset_backedges(loop);
796 set_irg_loop(irg, NULL);
798 set_irg_loopinfo_state(irg, loopinfo_none);
799 /* We cannot free the cfloop nodes, they are on the obstack. */
803 void free_all_cfloop_information(void) {
805 #ifdef INTERPROCEDURAL_VIEW
806 int rem = get_interprocedural_view();
807 set_interprocedural_view(1); /* To visit all filter nodes */
809 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
810 free_cfloop_information(get_irp_irg(i));
812 #ifdef INTERPROCEDURAL_VIEW
813 set_interprocedural_view(rem);