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 backedge/cfloop
23 * datastructures. A variation on the Tarjan algorithm. See also
24 * [Trapp:99], Chapter 5.2.1.2.
25 * @author Goetz Lindenmaier
34 #include "irgraph_t.h"
41 #define NO_CFLOOPS_WITHOUT_HEAD 1
43 /** The outermost graph the scc is computed for */
44 static ir_graph *outermost_ir_graph;
45 /** Current cfloop construction is working on. */
46 static ir_loop *current_loop;
47 /** Counts the number of allocated cfloop nodes.
48 * Each cfloop node gets a unique number.
49 * @todo What for? ev. remove.
51 static int loop_node_cnt = 0;
52 /** Counter to generate depth first numbering of visited nodes. */
53 static int current_dfn = 1;
55 static unsigned max_loop_depth = 0;
57 void link_to_reg_end(ir_node *n, void *env);
59 /**********************************************************************/
60 /* Node attributes **/
61 /**********************************************************************/
63 /**********************************************************************/
64 /* Node attributes needed for the construction. **/
65 /**********************************************************************/
68 * The SCC info. Additional fields for an ir-node needed for the
71 typedef struct scc_info {
72 int in_stack; /**< Marks whether node is on the stack. */
73 int dfn; /**< Depth first search number. */
74 int uplink; /**< dfn number of ancestor. */
77 /** Allocate a new scc_info on the given obstack */
78 static inline scc_info *new_scc_info(struct obstack *obst)
80 return OALLOCZ(obst, scc_info);
84 * Marks the node n to be on the stack.
86 static inline void mark_irn_in_stack(ir_node *n)
88 scc_info *info = (scc_info*) get_irn_link(n);
93 * Marks the node n to be not on the stack.
95 static inline void mark_irn_not_in_stack(ir_node *n)
97 scc_info *info = (scc_info*) get_irn_link(n);
102 * Returns whether node n is on the stack.
104 static inline int irn_is_in_stack(ir_node *n)
106 scc_info *info = (scc_info*) get_irn_link(n);
107 return info->in_stack;
111 * Sets node n uplink value.
113 static inline void set_irn_uplink(ir_node *n, int uplink)
115 scc_info *info = (scc_info*) get_irn_link(n);
116 info->uplink = uplink;
120 * Return node n uplink value.
122 static inline int get_irn_uplink(ir_node *n)
124 scc_info *info = (scc_info*) get_irn_link(n);
129 * Sets node n dfn value.
131 static inline void set_irn_dfn(ir_node *n, int dfn)
133 scc_info *info = (scc_info*) get_irn_link(n);
138 * Returns node n dfn value.
140 static inline int get_irn_dfn(ir_node *n)
142 scc_info *info = (scc_info*) get_irn_link(n);
146 /**********************************************************************/
148 /**********************************************************************/
150 /** An IR-node stack */
151 static ir_node **stack = NULL;
152 /** The top (index) of the IR-node stack */
153 static size_t tos = 0;
156 * Initializes the IR-node stack
158 static inline void init_stack(void)
161 ARR_RESIZE(ir_node *, stack, 1000);
163 stack = NEW_ARR_F(ir_node *, 1000);
168 static void finish_stack(void)
175 * Push a node n onto the IR-node stack.
177 static inline void push(ir_node *n)
179 if (tos == ARR_LEN(stack)) {
180 size_t nlen = ARR_LEN(stack) * 2;
181 ARR_RESIZE(ir_node *, stack, nlen);
184 mark_irn_in_stack(n);
188 * Pop a node from the IR-node stack and return it.
190 static inline ir_node *pop(void)
192 ir_node *n = stack[--tos];
193 mark_irn_not_in_stack(n);
198 * The nodes from tos up to n belong to the current loop.
199 * Removes them from the stack and adds them to the current loop.
201 static inline void pop_scc_to_loop(ir_node *n)
208 set_irn_dfn(m, loop_node_cnt);
209 add_loop_node(current_loop, m);
210 set_irn_loop(m, current_loop);
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 size_t 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;
236 /* the loop last_son is dead now, recover at least some memory */
237 DEL_ARR_F(last_son->children);
245 * Removes and unmarks all nodes up to n from the stack.
246 * The nodes must be visited once more to assign them to a scc.
248 static inline void pop_scc_unmark_visit(ir_node *n)
254 set_irn_visited(m, 0);
258 /**********************************************************************/
259 /* The loop datastructure. **/
260 /**********************************************************************/
263 * Allocates a new loop as son of current_loop. Sets current_loop
264 * to the new loop and returns its father.
265 * The loop is allocated on the outermost_ir_graphs's obstack.
267 static ir_loop *new_loop(void)
269 ir_loop *father = current_loop;
270 ir_loop *son = alloc_loop(father, outermost_ir_graph->obst);
272 if (son->depth > max_loop_depth) max_loop_depth = son->depth;
277 /**********************************************************************/
278 /* Constructing and destructing the loop/backedge information. **/
279 /**********************************************************************/
281 /* Initialization steps. **********************************************/
284 * Allocates a scc_info for every Block node n.
285 * Clear the backedges for all nodes.
286 * Called from a walker.
288 static inline void init_node(ir_node *n, void *env)
290 struct obstack *obst = (struct obstack*) env;
292 set_irn_link(n, new_scc_info(obst));
297 * Initializes the common global settings for the scc algorithm
299 static inline void init_scc_common(void)
307 * Initializes the scc algorithm for the intraprocedural case.
308 * Add scc info to every block node.
310 static inline void init_scc(ir_graph *irg, struct obstack *obst)
313 irg_walk_graph(irg, init_node, NULL, obst);
316 static inline void finish_scc(void)
321 /** Returns non-zero if n is a loop header, i.e., it is a Block node
322 * and has predecessors within the cfloop and out of the cfloop.
324 * @param n the block node to check
325 * @param root only needed for assertion.
327 static int is_head(ir_node *n, ir_node *root)
330 int some_outof_loop = 0, some_in_loop = 0;
335 arity = get_Block_n_cfgpreds(n);
336 for (i = 0; i < arity; i++) {
337 ir_node *pred = get_Block_cfgpred_block(n, i);
338 /* ignore Bad control flow: it cannot happen */
341 if (is_backedge(n, i))
343 if (!irn_is_in_stack(pred)) {
346 assert(get_irn_uplink(pred) >= get_irn_uplink(root));
350 return some_outof_loop & some_in_loop;
355 * Returns non-zero if n is possible loop head of an endless loop.
356 * I.e., it is a Block node and has only predecessors
359 * @param n the block node to check
360 * @param root only needed for assertion.
362 static int is_endless_head(ir_node *n, ir_node *root)
365 int none_outof_loop = 1, some_in_loop = 0;
369 /* Test for legal loop header: Block, Phi, ... */
370 arity = get_Block_n_cfgpreds(n);
371 for (i = 0; i < arity; i++) {
372 ir_node *pred = get_Block_cfgpred_block(n, i);
373 /* ignore Bad control flow: it cannot happen */
376 if (is_backedge(n, i))
378 if (!irn_is_in_stack(pred)) {
381 assert(get_irn_uplink(pred) >= get_irn_uplink(root));
385 return none_outof_loop && some_in_loop;
389 * Returns index of the predecessor with the smallest dfn number
390 * greater-equal than limit.
392 static int smallest_dfn_pred(ir_node *n, int limit)
394 int i, index = -2, min = -1;
396 int arity = get_Block_n_cfgpreds(n);
397 for (i = 0; i < arity; i++) {
398 ir_node *pred = get_Block_cfgpred_block(n, i);
399 /* ignore Bad control flow: it cannot happen */
402 if (is_backedge(n, i) || !irn_is_in_stack(pred))
404 if (get_irn_dfn(pred) >= limit && (min == -1 || get_irn_dfn(pred) < min)) {
406 min = get_irn_dfn(pred);
413 * Returns index of the predecessor with the largest dfn number.
415 static int largest_dfn_pred(ir_node *n)
417 int i, index = -2, max = -1;
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) > max) {
429 max = get_irn_dfn(pred);
436 * Searches the stack for possible loop heads. Tests these for backedges.
437 * If it finds a head with an unmarked backedge it marks this edge and
438 * returns the tail of the loop.
439 * If it finds no backedge returns NULL.
441 static ir_node *find_tail(ir_node *n)
447 m = stack[tos - 1]; /* tos = top of stack */
449 res_index = smallest_dfn_pred(m, 0);
450 if ((res_index == -2) && /* no smallest dfn pred found. */
456 for (i = tos - 1; i != 0;) {
459 res_index = smallest_dfn_pred(m, get_irn_dfn(m) + 1);
460 if (res_index == -2) /* no smallest dfn pred found. */
461 res_index = largest_dfn_pred(m);
463 if ((m == n) && (res_index == -2)) {
470 /* We should not walk past our selves on the stack: The upcoming nodes
471 are not in this loop. We assume a loop not reachable from Start. */
478 if (i == (size_t)-1) {
479 /* A dead loop not reachable from Start. */
480 for (i = tos - 1; i != 0;) {
482 if (is_endless_head(m, n)) {
483 res_index = smallest_dfn_pred (m, get_irn_dfn(m) + 1);
484 if (res_index == -2) /* no smallest dfn pred found. */
485 res_index = largest_dfn_pred(m);
488 if (m == n) break; /* It's not an unreachable loop, either. */
490 //assert(0 && "no head found on stack");
493 assert(res_index > -2);
495 set_backedge(m, res_index);
496 return get_Block_cfgpred_block(m, res_index);
500 * returns non.zero if l is the outermost loop.
502 inline static int is_outermost_loop(ir_loop *l)
504 return l == get_loop_outer_loop(l);
507 /*-----------------------------------------------------------*
508 * The core algorithm. *
509 *-----------------------------------------------------------*/
512 * Walks over all blocks of a graph
514 static void cfscc(ir_node *n)
521 if (irn_visited_else_mark(n)) return;
523 /* Initialize the node */
524 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
525 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
526 set_irn_loop(n, NULL);
530 arity = get_Block_n_cfgpreds(n);
532 for (i = 0; i < arity; i++) {
535 if (is_backedge(n, i))
537 m = get_Block_cfgpred_block(n, i);
538 /* ignore Bad control flow: it cannot happen */
543 if (irn_is_in_stack(m)) {
544 /* Uplink of m is smaller if n->m is a backedge.
545 Propagate the uplink to mark the cfloop. */
546 if (get_irn_uplink(m) < get_irn_uplink(n))
547 set_irn_uplink(n, get_irn_uplink(m));
551 if (get_irn_dfn(n) == get_irn_uplink(n)) {
552 /* This condition holds for
553 1) the node with the incoming backedge.
554 That is: We found a cfloop!
555 2) Straight line code, because no uplink has been propagated, so the
556 uplink still is the same as the dfn.
558 But n might not be a proper cfloop head for the analysis. Proper cfloop
559 heads are Block and Phi nodes. find_tail searches the stack for
560 Block's and Phi's and takes those nodes as cfloop heads for the current
561 cfloop instead and marks the incoming edge as backedge. */
563 ir_node *tail = find_tail(n);
565 /* We have a cfloop, that is no straight line code,
566 because we found a cfloop head!
567 Next actions: Open a new cfloop on the cfloop tree and
568 try to find inner cfloops */
570 #if NO_CFLOOPS_WITHOUT_HEAD
572 /* This is an adaption of the algorithm from fiasco / optscc to
573 * avoid cfloops without Block or Phi as first node. This should
574 * severely reduce the number of evaluations of nodes to detect
575 * a fixpoint in the heap analysis.
576 * Further it avoids cfloops without firm nodes that cause errors
577 * in the heap analyses. */
581 if ((get_loop_n_elements(current_loop) > 0) || (is_outermost_loop(current_loop))) {
591 ir_loop *l = new_loop();
595 /* Remove the cfloop from the stack ... */
596 pop_scc_unmark_visit(n);
598 /* The current backedge has been marked, that is temporarily eliminated,
599 by find tail. Start the scc algorithm
600 anew on the subgraph thats left (the current cfloop without the backedge)
601 in order to find more inner cfloops. */
605 assert(irn_visited(n));
606 #if NO_CFLOOPS_WITHOUT_HEAD
611 /* AS: No cfloop head was found, that is we have straight line code.
612 Pop all nodes from the stack to the current cfloop. */
618 /* Constructs control flow backedge information for irg. */
619 int construct_cf_backedges(ir_graph *irg)
621 ir_graph *rem = current_ir_graph;
623 ir_node *end = get_irg_end(irg);
629 current_ir_graph = irg;
630 outermost_ir_graph = irg;
633 init_scc(irg, &temp);
636 new_loop(); /* sets current_loop */
637 head_rem = current_loop; /* Just for assertion */
639 inc_irg_visited(irg);
641 /* walk over all blocks of the graph, including keep alives */
642 cfscc(get_irg_end_block(irg));
643 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
644 ir_node *el = get_End_keepalive(end, i);
649 obstack_free(&temp, NULL);
651 assert(head_rem == current_loop);
652 mature_loops(current_loop, irg->obst);
653 set_irg_loop(irg, current_loop);
654 set_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_LOOPINFO);
656 current_ir_graph = rem;
657 return max_loop_depth;
660 void assure_loopinfo(ir_graph *irg)
662 if (is_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_LOOPINFO))
664 construct_cf_backedges(irg);