2 * Copyright (C) 1995-2007 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 static ir_graph *outermost_ir_graph; /* The outermost graph the scc is computed
50 static ir_loop *current_loop; /* Current cfloop construction is working
52 static int loop_node_cnt = 0; /* Counts the number of allocated cfloop nodes.
53 Each cfloop node gets a unique number.
54 What for? ev. remove. @@@ */
55 static int current_dfn = 1; /* Counter to generate depth first numbering
58 static int max_loop_depth = 0;
60 void link_to_reg_end (ir_node *n, void *env);
62 /**********************************************************************/
63 /* Node attributes **/
64 /**********************************************************************/
66 /**********************************************************************/
67 /* Node attributes needed for the construction. **/
68 /**********************************************************************/
71 * The SCC info. Additional fields for an ir-node needed for the
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. */
80 /** Allocate a new scc_info on the obstack of the outermost graph */
81 static INLINE scc_info *new_scc_info(void) {
82 scc_info *info = obstack_alloc (outermost_ir_graph->obst, sizeof (scc_info));
83 memset (info, 0, sizeof (scc_info));
88 * Marks the node n to be on the stack.
91 mark_irn_in_stack (ir_node *n) {
92 scc_info *info = get_irn_link(n);
97 * Marks the node n to be not on the stack.
100 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.
109 irn_is_in_stack (ir_node *n) {
110 scc_info *info = get_irn_link(n);
111 return info->in_stack;
115 * Sets node n uplink value.
118 set_irn_uplink (ir_node *n, int uplink) {
119 scc_info *info = get_irn_link(n);
120 info->uplink = uplink;
124 * Return node n uplink value.
127 get_irn_uplink (ir_node *n) {
128 scc_info *info = get_irn_link(n);
133 * Sets node n dfn value.
136 set_irn_dfn (ir_node *n, int dfn) {
137 scc_info *info = get_irn_link(n);
142 * Returns node n dfn value.
145 get_irn_dfn (ir_node *n) {
146 scc_info *info = get_irn_link(n);
150 /**********************************************************************/
152 /**********************************************************************/
154 static ir_node **stack = NULL; /**< An IR-node stack */
155 static int tos = 0; /**< The top (index) of the IR-node stack */
158 * Initializes the IR-node stack
160 static INLINE void init_stack(void) {
162 ARR_RESIZE(ir_node *, stack, 1000);
164 stack = NEW_ARR_F(ir_node *, 1000);
170 * Push a node n onto the IR-node stack.
175 if (tos == ARR_LEN(stack)) {
176 int nlen = ARR_LEN(stack) * 2;
177 ARR_RESIZE(ir_node *, stack, nlen);
180 mark_irn_in_stack(n);
184 * Pop a node from the IR-node stack and return it.
186 static INLINE ir_node *
189 ir_node *n = stack[--tos];
190 mark_irn_not_in_stack(n);
195 * The nodes from tos up to n belong to the current loop.
196 * Removes them from the stack and adds them to the current loop.
199 pop_scc_to_loop(ir_node *n)
207 set_irn_dfn(m, loop_node_cnt);
208 add_loop_node(current_loop, m);
209 set_irn_loop(m, current_loop);
210 /* if (m==n) break;*/
214 /* GL ??? my last son is my grandson??? Removes cfloops with no
215 ir_nodes in them. Such loops have only another loop as son. (Why
216 can't they have two loops as sons? Does it never get that far? ) */
217 static void close_loop (ir_loop *l)
219 int last = get_loop_n_elements(l) - 1;
220 loop_element lelement = get_loop_element(l, last);
221 ir_loop *last_son = lelement.son;
223 if (get_kind(last_son) == k_ir_loop &&
224 get_loop_n_elements(last_son) == 1) {
227 lelement = get_loop_element(last_son, 0);
229 if(get_kind(gson) == k_ir_loop) {
230 loop_element new_last_son;
232 gson -> outer_loop = l;
233 new_last_son.son = gson;
234 l -> children[last] = new_last_son;
242 * Removes and unmarks all nodes up to n from the stack.
243 * The nodes must be visited once more to assign them to a scc.
246 pop_scc_unmark_visit (ir_node *n)
252 set_irn_visited(m, 0);
256 /**********************************************************************/
257 /* The loop datastructure. **/
258 /**********************************************************************/
261 * Allocates a new loop as son of current_loop. Sets current_loop
262 * to the new loop and returns its father.
264 static ir_loop *new_loop (void) {
265 ir_loop *father, *son;
267 father = current_loop;
269 son = obstack_alloc(outermost_ir_graph->obst, sizeof(*son));
270 memset(son, 0, sizeof(*son));
271 son->kind = k_ir_loop;
272 son->children = NEW_ARR_F(loop_element, 0);
276 son->outer_loop = father;
277 add_loop_son(father, son);
278 son->depth = father->depth+1;
279 if (son->depth > max_loop_depth) max_loop_depth = son->depth;
281 else { /* The root loop */
282 son->outer_loop = son;
287 son->loop_nr = get_irp_new_node_nr();
295 /**********************************************************************/
296 /* Constructing and destructing the loop/backedge information. **/
297 /**********************************************************************/
299 /* Initialization steps. **********************************************/
302 * Allocates a scc_info for every Block node n.
303 * Clear the backedges for all nodes.
304 * Called from a walker.
307 init_node (ir_node *n, void *env) {
309 set_irn_link (n, new_scc_info());
314 * Initializes the common global settings for the scc algorithm
317 init_scc_common (void) {
324 * Initializes the scc algorithm for the intraprocedural case.
325 * Add scc info to every block node.
328 init_scc (ir_graph *irg) {
330 irg_walk_graph(irg, init_node, NULL, NULL);
334 * Initializes the scc algorithm for the interprocedural case.
339 cg_walk (init_node, NULL, NULL);
341 #if EXPERIMENTAL_CFLOOP_TREE
342 cg_walk (link_to_reg_end, NULL, NULL);
347 * Condition for breaking the recursion: n is the block
348 * that gets the initial control flow from the Start node.
350 static int is_outermost_StartBlock(ir_node *n) {
351 /* Test whether this is the outermost Start node. If so
352 recursion must end. */
354 if ((get_Block_n_cfgpreds(n) == 1) &&
355 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Start) &&
356 (get_nodes_block(skip_Proj(get_Block_cfgpred(n, 0))) == n)) {
362 /** Returns non-zero if n is a loop header, i.e., it is a Block node
363 * and has predecessors within the cfloop and out of the cfloop.
365 * @param n the block node to check
366 * @param root only needed for assertion.
369 is_head (ir_node *n, ir_node *root)
372 int some_outof_loop = 0, some_in_loop = 0;
376 if (!is_outermost_StartBlock(n)) {
377 arity = get_irn_arity(n);
378 for (i = 0; i < arity; i++) {
379 ir_node *pred = get_nodes_block(skip_Proj(get_irn_n(n, i)));
380 if (is_backedge(n, i)) continue;
381 if (!irn_is_in_stack(pred)) {
384 if (get_irn_uplink(pred) < get_irn_uplink(root)) {
385 DDMN(pred); DDMN(root);
386 assert(get_irn_uplink(pred) >= get_irn_uplink(root));
392 return some_outof_loop & some_in_loop;
397 * Returns non-zero if n is possible loop head of an endless loop.
398 * I.e., it is a Block, Phi or Filter node and has only predecessors
401 * @param n the block node to check
402 * @param root only needed for assertion.
405 is_endless_head (ir_node *n, ir_node *root)
408 int some_outof_loop = 0, some_in_loop = 0;
411 /* Test for legal loop header: Block, Phi, ... */
412 if (!is_outermost_StartBlock(n)) {
413 arity = get_irn_arity(n);
414 for (i = 0; i < arity; i++) {
415 ir_node *pred = get_nodes_block(skip_Proj(get_irn_n(n, i)));
417 if (is_backedge(n, i)) { continue; }
418 if (!irn_is_in_stack(pred)) {
419 some_outof_loop = 1; //printf(" some out of loop ");
421 if(get_irn_uplink(pred) < get_irn_uplink(root)) {
422 DDMN(pred); DDMN(root);
423 assert(get_irn_uplink(pred) >= get_irn_uplink(root));
429 return !some_outof_loop && some_in_loop;
433 * Returns index of the predecessor with the smallest dfn number
434 * greater-equal than limit.
437 smallest_dfn_pred (ir_node *n, int limit)
439 int i, index = -2, min = -1;
441 if (!is_outermost_StartBlock(n)) {
442 int arity = get_irn_arity(n);
443 for (i = 0; i < arity; i++) {
444 ir_node *pred = get_nodes_block(skip_Proj(get_irn_n(n, i)));
445 if (is_backedge(n, i) || !irn_is_in_stack(pred))
447 if (get_irn_dfn(pred) >= limit && (min == -1 || get_irn_dfn(pred) < min)) {
449 min = get_irn_dfn(pred);
457 * Returns index of the predecessor with the largest dfn number.
460 largest_dfn_pred (ir_node *n)
462 int i, index = -2, max = -1;
464 if (!is_outermost_StartBlock(n)) {
465 int arity = get_irn_arity(n);
466 for (i = 0; i < arity; i++) {
467 ir_node *pred = get_nodes_block(skip_Proj(get_irn_n(n, i)));
468 if (is_backedge (n, i) || !irn_is_in_stack(pred))
470 if (get_irn_dfn(pred) > max) {
472 max = get_irn_dfn(pred);
480 * Searches the stack for possible loop heads. Tests these for backedges.
481 * If it finds a head with an unmarked backedge it marks this edge and
482 * returns the tail of the loop.
483 * If it finds no backedge returns NULL.
486 find_tail (ir_node *n) {
488 int i, res_index = -2;
490 m = stack[tos-1]; /* tos = top of stack */
492 res_index = smallest_dfn_pred(m, 0);
493 if ((res_index == -2) && /* no smallest dfn pred found. */
497 if (m == n) return NULL;
498 for (i = tos-2; i >= 0; --i) {
501 if (is_head (m, n)) {
502 res_index = smallest_dfn_pred (m, get_irn_dfn(m) + 1);
503 if (res_index == -2) /* no smallest dfn pred found. */
504 res_index = largest_dfn_pred (m);
506 if ((m == n) && (res_index == -2)) {
513 /* We should not walk past our selves on the stack: The upcoming nodes
514 are not in this loop. We assume a loop not reachable from Start. */
522 /* A dead loop not reachable from Start. */
523 for (i = tos-2; i >= 0; --i) {
525 if (is_endless_head (m, n)) {
526 res_index = smallest_dfn_pred (m, get_irn_dfn(m) + 1);
527 if (res_index == -2) /* no smallest dfn pred found. */
528 res_index = largest_dfn_pred (m);
531 if (m == n) break; /* It's not an unreachable loop, either. */
533 //assert(0 && "no head found on stack");
537 assert (res_index > -2);
539 set_backedge (m, res_index);
540 return is_outermost_StartBlock(n) ? NULL : get_nodes_block(skip_Proj(get_irn_n(m, res_index)));
544 * returns non.zero if l is the outermost loop.
547 is_outermost_loop(ir_loop *l) {
548 return l == get_loop_outer_loop(l);
551 /*-----------------------------------------------------------*
552 * The core algorithm. *
553 *-----------------------------------------------------------*/
556 * Walks over all blocks of a graph
558 static void cfscc (ir_node *n) {
563 if (irn_visited(n)) return;
566 /* Initialize the node */
567 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
568 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
569 set_irn_loop(n, NULL);
573 if (!is_outermost_StartBlock(n)) {
574 int arity = get_irn_arity(n);
576 for (i = 0; i < arity; i++) {
579 if (is_backedge(n, i))
581 m = get_nodes_block(skip_Proj(get_irn_n(n, i)));
584 if (irn_is_in_stack(m)) {
585 /* Uplink of m is smaller if n->m is a backedge.
586 Propagate the uplink to mark the cfloop. */
587 if (get_irn_uplink(m) < get_irn_uplink(n))
588 set_irn_uplink(n, get_irn_uplink(m));
593 if (get_irn_dfn(n) == get_irn_uplink(n)) {
594 /* This condition holds for
595 1) the node with the incoming backedge.
596 That is: We found a cfloop!
597 2) Straight line code, because no uplink has been propagated, so the
598 uplink still is the same as the dfn.
600 But n might not be a proper cfloop head for the analysis. Proper cfloop
601 heads are Block and Phi nodes. find_tail searches the stack for
602 Block's and Phi's and takes those nodes as cfloop heads for the current
603 cfloop instead and marks the incoming edge as backedge. */
605 ir_node *tail = find_tail(n);
607 /* We have a cfloop, that is no straight line code,
608 because we found a cfloop head!
609 Next actions: Open a new cfloop on the cfloop tree and
610 try to find inner cfloops */
612 #if NO_CFLOOPS_WITHOUT_HEAD
614 /* This is an adaption of the algorithm from fiasco / optscc to
615 * avoid cfloops without Block or Phi as first node. This should
616 * severely reduce the number of evaluations of nodes to detect
617 * a fixpoint in the heap analysis.
618 * Further it avoids cfloops without firm nodes that cause errors
619 * in the heap analyses. */
623 if ((get_loop_n_elements(current_loop) > 0) || (is_outermost_loop(current_loop))) {
633 ir_loop *l = new_loop();
637 /* Remove the cfloop from the stack ... */
638 pop_scc_unmark_visit (n);
640 /* The current backedge has been marked, that is temporarily eliminated,
641 by find tail. Start the scc algorithm
642 anew on the subgraph thats left (the current cfloop without the backedge)
643 in order to find more inner cfloops. */
647 assert (irn_visited(n));
648 #if NO_CFLOOPS_WITHOUT_HEAD
654 /* AS: No cfloop head was found, that is we have straight line code.
655 Pop all nodes from the stack to the current cfloop. */
661 /* Constructs control flow backedge information for irg. */
662 int construct_cf_backedges(ir_graph *irg) {
663 ir_graph *rem = current_ir_graph;
665 ir_node *end = get_irg_end(irg);
668 assert(!get_interprocedural_view() &&
669 "use construct_ip_cf_backedges()");
672 current_ir_graph = irg;
673 outermost_ir_graph = irg;
675 init_scc(current_ir_graph);
678 new_loop(); /* sets current_loop */
679 head_rem = current_loop; /* Just for assertion */
681 inc_irg_visited(current_ir_graph);
683 /* walk over all blocks of the graph, including keep alives */
684 cfscc(get_irg_end_block(current_ir_graph));
685 for (i = 0; i < get_End_n_keepalives(end); i++) {
686 ir_node *el = get_End_keepalive(end, i);
687 if (is_Block(el)) cfscc(el);
690 assert(head_rem == current_loop);
691 set_irg_loop(current_ir_graph, current_loop);
692 set_irg_loopinfo_state(current_ir_graph, loopinfo_cf_consistent);
693 assert(get_irg_loop(current_ir_graph)->kind == k_ir_loop);
695 current_ir_graph = rem;
696 return max_loop_depth;
700 int construct_ip_cf_backedges (void) {
701 ir_graph *rem = current_ir_graph;
702 int rem_ipv = get_interprocedural_view();
705 assert(get_irp_ip_view_state() == ip_view_valid);
707 outermost_ir_graph = get_irp_main_irg();
712 new_loop(); /* sets current_loop */
713 set_interprocedural_view(1);
715 inc_max_irg_visited();
716 for (i = 0; i < get_irp_n_irgs(); i++)
717 set_irg_visited(get_irp_irg(i), get_max_irg_visited());
719 /** We have to start the walk at the same nodes as cg_walk. **/
720 /* Walk starting at unreachable procedures. Only these
721 * have End blocks visible in interprocedural view. */
722 for (i = 0; i < get_irp_n_irgs(); i++) {
724 current_ir_graph = get_irp_irg(i);
726 sb = get_irg_start_block(current_ir_graph);
728 if ((get_Block_n_cfgpreds(sb) > 1) ||
729 (get_nodes_block(get_Block_cfgpred(sb, 0)) != sb)) continue;
731 cfscc(get_irg_end_block(current_ir_graph));
734 /* Check whether we walked all procedures: there could be procedures
735 with cyclic calls but no call from the outside. */
736 for (i = 0; i < get_irp_n_irgs(); i++) {
738 current_ir_graph = get_irp_irg(i);
740 /* Test start block: if inner procedure end and end block are not
741 * visible and therefore not marked. */
742 sb = get_irg_start_block(current_ir_graph);
743 if (get_irn_visited(sb) < get_irg_visited(current_ir_graph)) cfscc(sb);
746 /* Walk all endless cfloops in inner procedures.
747 * We recognize an inner procedure if the End node is not visited. */
748 for (i = 0; i < get_irp_n_irgs(); i++) {
750 current_ir_graph = get_irp_irg(i);
752 e = get_irg_end(current_ir_graph);
753 if (get_irn_visited(e) < get_irg_visited(current_ir_graph)) {
755 /* Don't visit the End node. */
756 for (j = 0; j < get_End_n_keepalives(e); j++) {
757 ir_node *el = get_End_keepalive(e, j);
758 if (is_Block(el)) cfscc(el);
763 set_irg_loop(outermost_ir_graph, current_loop);
764 set_irg_loopinfo_state(current_ir_graph, loopinfo_cf_ip_consistent);
765 assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
767 current_ir_graph = rem;
768 set_interprocedural_view(rem_ipv);
769 return max_loop_depth;
773 * Clear the intra- and the interprocedural
774 * backedge information pf a block.
776 static void reset_backedges(ir_node *block) {
777 int rem = get_interprocedural_view();
779 assert(is_Block(block));
780 set_interprocedural_view(1);
781 clear_backedges(block);
782 set_interprocedural_view(0);
783 clear_backedges(block);
784 set_interprocedural_view(rem);
788 * Reset all backedges of the first block of
789 * a loop as well as all loop info for all nodes of this loop.
790 * Recurse into all nested loops.
792 static void loop_reset_backedges(ir_loop *l) {
794 reset_backedges(get_loop_node(l, 0));
795 for (i = 0; i < get_loop_n_nodes(l); ++i)
796 set_irn_loop(get_loop_node(l, i), NULL);
797 for (i = 0; i < get_loop_n_sons(l); ++i) {
798 loop_reset_backedges(get_loop_son(l, i));
802 /* Removes all cfloop information.
803 Resets all backedges */
804 void free_cfloop_information(ir_graph *irg) {
805 if (get_irg_loop(irg))
806 loop_reset_backedges(get_irg_loop(irg));
807 set_irg_loop(irg, NULL);
808 set_irg_loopinfo_state(irg, loopinfo_none);
809 /* We cannot free the cfloop nodes, they are on the obstack. */
813 void free_all_cfloop_information (void) {
815 int rem = get_interprocedural_view();
816 set_interprocedural_view(1); /* To visit all filter nodes */
817 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
818 free_cfloop_information(get_irp_irg(i));
820 set_interprocedural_view(rem);