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
35 #include "irgraph_t.h"
42 #define NO_CFLOOPS_WITHOUT_HEAD 1
44 /** The outermost graph the scc is computed for */
45 static ir_graph *outermost_ir_graph;
46 /** Current cfloop construction is working on. */
47 static ir_loop *current_loop;
48 /** Counts the number of allocated cfloop nodes.
49 * Each cfloop node gets a unique number.
50 * @todo What for? ev. remove.
52 static int loop_node_cnt = 0;
53 /** Counter to generate depth first numbering of visited nodes. */
54 static int current_dfn = 1;
56 static int max_loop_depth = 0;
58 void link_to_reg_end(ir_node *n, void *env);
60 /**********************************************************************/
61 /* Node attributes **/
62 /**********************************************************************/
64 /**********************************************************************/
65 /* Node attributes needed for the construction. **/
66 /**********************************************************************/
69 * The SCC info. Additional fields for an ir-node needed for the
72 typedef struct scc_info {
73 int in_stack; /**< Marks whether node is on the stack. */
74 int dfn; /**< Depth first search number. */
75 int uplink; /**< dfn number of ancestor. */
78 /** Allocate a new scc_info on the given obstack */
79 static inline scc_info *new_scc_info(struct obstack *obst)
81 return OALLOCZ(obst, scc_info);
85 * Marks the node n to be on the stack.
87 static inline void mark_irn_in_stack(ir_node *n)
89 scc_info *info = get_irn_link(n);
94 * Marks the node n to be not on the stack.
96 static inline void mark_irn_not_in_stack(ir_node *n)
98 scc_info *info = get_irn_link(n);
103 * Returns whether node n is on the stack.
105 static inline int irn_is_in_stack(ir_node *n)
107 scc_info *info = get_irn_link(n);
108 return info->in_stack;
112 * Sets node n uplink value.
114 static inline void set_irn_uplink(ir_node *n, int uplink)
116 scc_info *info = get_irn_link(n);
117 info->uplink = uplink;
121 * Return node n uplink value.
123 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)
134 scc_info *info = get_irn_link(n);
139 * Returns node n dfn value.
141 static inline int get_irn_dfn(ir_node *n)
143 scc_info *info = get_irn_link(n);
147 /**********************************************************************/
149 /**********************************************************************/
151 /** An IR-node stack */
152 static ir_node **stack = NULL;
153 /** The top (index) of the IR-node stack */
157 * Initializes the IR-node stack
159 static inline void init_stack(void)
162 ARR_RESIZE(ir_node *, stack, 1000);
164 stack = NEW_ARR_F(ir_node *, 1000);
169 static void finish_stack(void)
176 * Push a node n onto the IR-node stack.
178 static inline void push(ir_node *n)
180 if (tos == ARR_LEN(stack)) {
181 int nlen = ARR_LEN(stack) * 2;
182 ARR_RESIZE(ir_node *, stack, nlen);
185 mark_irn_in_stack(n);
189 * Pop a node from the IR-node stack and return it.
191 static inline ir_node *pop(void)
193 ir_node *n = stack[--tos];
194 mark_irn_not_in_stack(n);
199 * The nodes from tos up to n belong to the current loop.
200 * Removes them from the stack and adds them to the current loop.
202 static inline void pop_scc_to_loop(ir_node *n)
209 set_irn_dfn(m, loop_node_cnt);
210 add_loop_node(current_loop, m);
211 set_irn_loop(m, current_loop);
215 /* GL ??? my last son is my grandson??? Removes cfloops with no
216 ir_nodes in them. Such loops have only another loop as son. (Why
217 can't they have two loops as sons? Does it never get that far? ) */
218 static void close_loop(ir_loop *l)
220 int last = get_loop_n_elements(l) - 1;
221 loop_element lelement = get_loop_element(l, last);
222 ir_loop *last_son = lelement.son;
224 if (get_kind(last_son) == k_ir_loop &&
225 get_loop_n_elements(last_son) == 1) {
228 lelement = get_loop_element(last_son, 0);
230 if (get_kind(gson) == k_ir_loop) {
231 loop_element new_last_son;
233 gson->outer_loop = l;
234 new_last_son.son = gson;
235 l->children[last] = new_last_son;
237 /* the loop last_son is dead now, recover at least some memory */
238 DEL_ARR_F(last_son->children);
246 * Removes and unmarks all nodes up to n from the stack.
247 * The nodes must be visited once more to assign them to a scc.
249 static inline void pop_scc_unmark_visit(ir_node *n)
255 set_irn_visited(m, 0);
259 /**********************************************************************/
260 /* The loop datastructure. **/
261 /**********************************************************************/
264 * Allocates a new loop as son of current_loop. Sets current_loop
265 * to the new loop and returns its father.
266 * The loop is allocated on the outermost_ir_graphs's obstack.
268 static ir_loop *new_loop(void)
270 ir_loop *father = current_loop;
271 ir_loop *son = alloc_loop(father, outermost_ir_graph->obst);
273 if (son->depth > max_loop_depth) max_loop_depth = son->depth;
278 /**********************************************************************/
279 /* Constructing and destructing the loop/backedge information. **/
280 /**********************************************************************/
282 /* Initialization steps. **********************************************/
285 * Allocates a scc_info for every Block node n.
286 * Clear the backedges for all nodes.
287 * Called from a walker.
289 static inline void init_node(ir_node *n, void *env)
291 struct obstack *obst = env;
293 set_irn_link(n, new_scc_info(obst));
298 * Initializes the common global settings for the scc algorithm
300 static inline void init_scc_common(void)
308 * Initializes the scc algorithm for the intraprocedural case.
309 * Add scc info to every block node.
311 static inline void init_scc(ir_graph *irg, struct obstack *obst)
314 irg_walk_graph(irg, init_node, NULL, obst);
317 static inline void finish_scc(void)
323 * Condition for breaking the recursion: n is the block
324 * that gets the initial control flow from the Start node.
326 static int is_outermost_StartBlock(ir_node *n)
328 /* Test whether this is the outermost Start node. If so
329 recursion must end. */
331 if (get_Block_n_cfgpreds(n) == 1 &&
332 is_Start(skip_Proj(get_Block_cfgpred(n, 0))) &&
333 get_Block_cfgpred_block(n, 0) == n) {
339 /** Returns non-zero if n is a loop header, i.e., it is a Block node
340 * and has predecessors within the cfloop and out of the cfloop.
342 * @param n the block node to check
343 * @param root only needed for assertion.
345 static int is_head(ir_node *n, ir_node *root)
348 int some_outof_loop = 0, some_in_loop = 0;
353 if (!is_outermost_StartBlock(n)) {
354 arity = get_Block_n_cfgpreds(n);
355 for (i = 0; i < arity; i++) {
356 ir_node *pred = get_Block_cfgpred_block(n, i);
357 /* ignore Bad control flow: it cannot happen */
360 if (is_backedge(n, i))
362 if (!irn_is_in_stack(pred)) {
365 assert(get_irn_uplink(pred) >= get_irn_uplink(root));
370 return some_outof_loop & some_in_loop;
375 * Returns non-zero if n is possible loop head of an endless loop.
376 * I.e., it is a Block node and has only predecessors
379 * @param n the block node to check
380 * @param root only needed for assertion.
382 static int is_endless_head(ir_node *n, ir_node *root)
385 int none_outof_loop = 1, some_in_loop = 0;
389 /* Test for legal loop header: Block, Phi, ... */
390 if (!is_outermost_StartBlock(n)) {
391 arity = get_Block_n_cfgpreds(n);
392 for (i = 0; i < arity; i++) {
393 ir_node *pred = get_Block_cfgpred_block(n, i);
394 /* ignore Bad control flow: it cannot happen */
397 if (is_backedge(n, i))
399 if (!irn_is_in_stack(pred)) {
402 assert(get_irn_uplink(pred) >= get_irn_uplink(root));
407 return none_outof_loop && some_in_loop;
411 * Returns index of the predecessor with the smallest dfn number
412 * greater-equal than limit.
414 static int smallest_dfn_pred(ir_node *n, int limit)
416 int i, index = -2, min = -1;
418 if (!is_outermost_StartBlock(n)) {
419 int arity = get_Block_n_cfgpreds(n);
420 for (i = 0; i < arity; i++) {
421 ir_node *pred = get_Block_cfgpred_block(n, i);
422 /* ignore Bad control flow: it cannot happen */
425 if (is_backedge(n, i) || !irn_is_in_stack(pred))
427 if (get_irn_dfn(pred) >= limit && (min == -1 || get_irn_dfn(pred) < min)) {
429 min = get_irn_dfn(pred);
437 * Returns index of the predecessor with the largest dfn number.
439 static int largest_dfn_pred(ir_node *n)
441 int i, index = -2, max = -1;
443 if (!is_outermost_StartBlock(n)) {
444 int arity = get_Block_n_cfgpreds(n);
445 for (i = 0; i < arity; i++) {
446 ir_node *pred = get_Block_cfgpred_block(n, i);
447 /* ignore Bad control flow: it cannot happen */
450 if (is_backedge(n, i) || !irn_is_in_stack(pred))
452 if (get_irn_dfn(pred) > max) {
454 max = get_irn_dfn(pred);
462 * Searches the stack for possible loop heads. Tests these for backedges.
463 * If it finds a head with an unmarked backedge it marks this edge and
464 * returns the tail of the loop.
465 * If it finds no backedge returns NULL.
467 static ir_node *find_tail(ir_node *n)
470 int i, res_index = -2;
472 m = stack[tos-1]; /* tos = top of stack */
474 res_index = smallest_dfn_pred(m, 0);
475 if ((res_index == -2) && /* no smallest dfn pred found. */
481 for (i = tos-2; i >= 0; --i) {
485 res_index = smallest_dfn_pred(m, get_irn_dfn(m) + 1);
486 if (res_index == -2) /* no smallest dfn pred found. */
487 res_index = largest_dfn_pred(m);
489 if ((m == n) && (res_index == -2)) {
496 /* We should not walk past our selves on the stack: The upcoming nodes
497 are not in this loop. We assume a loop not reachable from Start. */
505 /* A dead loop not reachable from Start. */
506 for (i = tos-2; i >= 0; --i) {
508 if (is_endless_head(m, n)) {
509 res_index = smallest_dfn_pred (m, get_irn_dfn(m) + 1);
510 if (res_index == -2) /* no smallest dfn pred found. */
511 res_index = largest_dfn_pred(m);
514 if (m == n) break; /* It's not an unreachable loop, either. */
516 //assert(0 && "no head found on stack");
519 assert(res_index > -2);
521 set_backedge(m, res_index);
522 return is_outermost_StartBlock(n) ? NULL : get_Block_cfgpred_block(m, res_index);
526 * returns non.zero if l is the outermost loop.
528 inline static int is_outermost_loop(ir_loop *l)
530 return l == get_loop_outer_loop(l);
533 /*-----------------------------------------------------------*
534 * The core algorithm. *
535 *-----------------------------------------------------------*/
538 * Walks over all blocks of a graph
540 static void cfscc(ir_node *n)
546 if (irn_visited_else_mark(n)) return;
548 /* Initialize the node */
549 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
550 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
551 set_irn_loop(n, NULL);
555 if (!is_outermost_StartBlock(n)) {
556 int arity = get_Block_n_cfgpreds(n);
558 for (i = 0; i < arity; i++) {
561 if (is_backedge(n, i))
563 m = get_Block_cfgpred_block(n, i);
564 /* ignore Bad control flow: it cannot happen */
569 if (irn_is_in_stack(m)) {
570 /* Uplink of m is smaller if n->m is a backedge.
571 Propagate the uplink to mark the cfloop. */
572 if (get_irn_uplink(m) < get_irn_uplink(n))
573 set_irn_uplink(n, get_irn_uplink(m));
578 if (get_irn_dfn(n) == get_irn_uplink(n)) {
579 /* This condition holds for
580 1) the node with the incoming backedge.
581 That is: We found a cfloop!
582 2) Straight line code, because no uplink has been propagated, so the
583 uplink still is the same as the dfn.
585 But n might not be a proper cfloop head for the analysis. Proper cfloop
586 heads are Block and Phi nodes. find_tail searches the stack for
587 Block's and Phi's and takes those nodes as cfloop heads for the current
588 cfloop instead and marks the incoming edge as backedge. */
590 ir_node *tail = find_tail(n);
592 /* We have a cfloop, that is no straight line code,
593 because we found a cfloop head!
594 Next actions: Open a new cfloop on the cfloop tree and
595 try to find inner cfloops */
597 #if NO_CFLOOPS_WITHOUT_HEAD
599 /* This is an adaption of the algorithm from fiasco / optscc to
600 * avoid cfloops without Block or Phi as first node. This should
601 * severely reduce the number of evaluations of nodes to detect
602 * a fixpoint in the heap analysis.
603 * Further it avoids cfloops without firm nodes that cause errors
604 * in the heap analyses. */
608 if ((get_loop_n_elements(current_loop) > 0) || (is_outermost_loop(current_loop))) {
618 ir_loop *l = new_loop();
622 /* Remove the cfloop from the stack ... */
623 pop_scc_unmark_visit(n);
625 /* The current backedge has been marked, that is temporarily eliminated,
626 by find tail. Start the scc algorithm
627 anew on the subgraph thats left (the current cfloop without the backedge)
628 in order to find more inner cfloops. */
632 assert(irn_visited(n));
633 #if NO_CFLOOPS_WITHOUT_HEAD
638 /* AS: No cfloop head was found, that is we have straight line code.
639 Pop all nodes from the stack to the current cfloop. */
645 /* Constructs control flow backedge information for irg. */
646 int construct_cf_backedges(ir_graph *irg)
648 ir_graph *rem = current_ir_graph;
650 ir_node *end = get_irg_end(irg);
656 current_ir_graph = irg;
657 outermost_ir_graph = irg;
660 init_scc(irg, &temp);
663 new_loop(); /* sets current_loop */
664 head_rem = current_loop; /* Just for assertion */
666 inc_irg_visited(irg);
668 /* walk over all blocks of the graph, including keep alives */
669 cfscc(get_irg_end_block(irg));
670 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
671 ir_node *el = get_End_keepalive(end, i);
676 obstack_free(&temp, NULL);
678 assert(head_rem == current_loop);
679 mature_loops(current_loop, irg->obst);
680 set_irg_loop(irg, current_loop);
681 set_irg_loopinfo_state(irg, loopinfo_cf_consistent);
682 assert(get_irg_loop(irg)->kind == k_ir_loop);
684 current_ir_graph = rem;
685 return max_loop_depth;
688 void assure_cf_loop(ir_graph *irg)
690 irg_loopinfo_state state = get_irg_loopinfo_state(irg);
692 if (state != loopinfo_cf_consistent)
693 construct_cf_backedges(irg);