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"
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 unsigned max_loop_depth = 0;
58 /**********************************************************************/
59 /* Node attributes needed for the construction. **/
60 /**********************************************************************/
63 * The SCC info. Additional fields for an ir-node needed for the
66 typedef struct scc_info {
67 int in_stack; /**< Marks whether node is on the stack. */
68 int dfn; /**< Depth first search number. */
69 int uplink; /**< dfn number of ancestor. */
72 /** Allocate a new scc_info on the given obstack */
73 static inline scc_info *new_scc_info(struct obstack *obst)
75 return OALLOCZ(obst, scc_info);
79 * Marks the node n to be on the stack.
81 static inline void mark_irn_in_stack(ir_node *n)
83 scc_info *info = (scc_info*) get_irn_link(n);
88 * Marks the node n to be not on the stack.
90 static inline void mark_irn_not_in_stack(ir_node *n)
92 scc_info *info = (scc_info*) get_irn_link(n);
97 * Returns whether node n is on the stack.
99 static inline int irn_is_in_stack(ir_node *n)
101 scc_info *info = (scc_info*) get_irn_link(n);
102 return info->in_stack;
106 * Sets node n uplink value.
108 static inline void set_irn_uplink(ir_node *n, int uplink)
110 scc_info *info = (scc_info*) get_irn_link(n);
111 info->uplink = uplink;
115 * Return node n uplink value.
117 static inline int get_irn_uplink(ir_node *n)
119 scc_info *info = (scc_info*) get_irn_link(n);
124 * Sets node n dfn value.
126 static inline void set_irn_dfn(ir_node *n, int dfn)
128 scc_info *info = (scc_info*) get_irn_link(n);
133 * Returns node n dfn value.
135 static inline int get_irn_dfn(ir_node *n)
137 scc_info *info = (scc_info*) get_irn_link(n);
141 /**********************************************************************/
143 /**********************************************************************/
145 /** An IR-node stack */
146 static ir_node **stack = NULL;
147 /** The top (index) of the IR-node stack */
148 static size_t tos = 0;
151 * Initializes the IR-node stack
153 static inline void init_stack(void)
156 ARR_RESIZE(ir_node *, stack, 1000);
158 stack = NEW_ARR_F(ir_node *, 1000);
163 static void finish_stack(void)
170 * Push a node n onto the IR-node stack.
172 static inline void push(ir_node *n)
174 if (tos == ARR_LEN(stack)) {
175 size_t nlen = ARR_LEN(stack) * 2;
176 ARR_RESIZE(ir_node *, stack, nlen);
179 mark_irn_in_stack(n);
183 * Pop a node from the IR-node stack and return it.
185 static inline ir_node *pop(void)
187 ir_node *n = stack[--tos];
188 mark_irn_not_in_stack(n);
193 * The nodes from tos up to n belong to the current loop.
194 * Removes them from the stack and adds them to the current loop.
196 static inline void pop_scc_to_loop(ir_node *n)
203 set_irn_dfn(m, loop_node_cnt);
204 add_loop_node(current_loop, m);
205 set_irn_loop(m, current_loop);
209 /* GL ??? my last son is my grandson??? Removes cfloops with no
210 ir_nodes in them. Such loops have only another loop as son. (Why
211 can't they have two loops as sons? Does it never get that far? ) */
212 static void close_loop(ir_loop *l)
214 size_t last = get_loop_n_elements(l) - 1;
215 loop_element lelement = get_loop_element(l, last);
216 ir_loop *last_son = lelement.son;
218 if (get_kind(last_son) == k_ir_loop &&
219 get_loop_n_elements(last_son) == 1) {
222 lelement = get_loop_element(last_son, 0);
224 if (get_kind(gson) == k_ir_loop) {
225 loop_element new_last_son;
227 gson->outer_loop = l;
228 new_last_son.son = gson;
229 l->children[last] = new_last_son;
231 /* the loop last_son is dead now, recover at least some memory */
232 DEL_ARR_F(last_son->children);
240 * Removes and unmarks all nodes up to n from the stack.
241 * The nodes must be visited once more to assign them to a scc.
243 static inline void pop_scc_unmark_visit(ir_node *n)
249 set_irn_visited(m, 0);
253 /**********************************************************************/
254 /* The loop datastructure. **/
255 /**********************************************************************/
258 * Allocates a new loop as son of current_loop. Sets current_loop
259 * to the new loop and returns its father.
260 * The loop is allocated on the outermost_ir_graphs's obstack.
262 static ir_loop *new_loop(void)
264 ir_loop *father = current_loop;
265 ir_loop *son = alloc_loop(father, get_irg_obstack(outermost_ir_graph));
267 if (son->depth > max_loop_depth) max_loop_depth = son->depth;
272 /**********************************************************************/
273 /* Constructing and destructing the loop/backedge information. **/
274 /**********************************************************************/
276 /* Initialization steps. **********************************************/
279 * Allocates a scc_info for every Block node n.
280 * Clear the backedges for all nodes.
281 * Called from a walker.
283 static inline void init_node(ir_node *n, void *env)
285 struct obstack *obst = (struct obstack*) env;
287 set_irn_link(n, new_scc_info(obst));
292 * Initializes the common global settings for the scc algorithm
294 static inline void init_scc_common(void)
302 * Initializes the scc algorithm for the intraprocedural case.
303 * Add scc info to every block node.
305 static inline void init_scc(ir_graph *irg, struct obstack *obst)
308 irg_walk_graph(irg, init_node, NULL, obst);
311 static inline void finish_scc(void)
316 /** Returns non-zero if n is a loop header, i.e., it is a Block node
317 * and has predecessors within the cfloop and out of the cfloop.
319 * @param n the block node to check
320 * @param root only needed for assertion.
322 static int is_head(ir_node *n, ir_node *root)
324 int some_outof_loop = 0, some_in_loop = 0;
327 int const arity = get_Block_n_cfgpreds(n);
328 for (int i = 0; i < arity; i++) {
329 ir_node *pred = get_Block_cfgpred_block(n, i);
330 /* ignore Bad control flow: it cannot happen */
333 if (is_backedge(n, i))
335 if (!irn_is_in_stack(pred)) {
338 assert(get_irn_uplink(pred) >= get_irn_uplink(root));
342 return some_outof_loop & some_in_loop;
347 * Returns non-zero if n is possible loop head of an endless loop.
348 * I.e., it is a Block node and has only predecessors
351 * @param n the block node to check
352 * @param root only needed for assertion.
354 static int is_endless_head(ir_node *n, ir_node *root)
356 int none_outof_loop = 1, some_in_loop = 0;
359 /* Test for legal loop header: Block, Phi, ... */
360 int const arity = get_Block_n_cfgpreds(n);
361 for (int i = 0; i < arity; i++) {
362 ir_node *pred = get_Block_cfgpred_block(n, i);
363 /* ignore Bad control flow: it cannot happen */
366 if (is_backedge(n, i))
368 if (!irn_is_in_stack(pred)) {
371 assert(get_irn_uplink(pred) >= get_irn_uplink(root));
375 return none_outof_loop && some_in_loop;
379 * Returns index of the predecessor with the smallest dfn number
380 * greater-equal than limit.
382 static int smallest_dfn_pred(ir_node *n, int limit)
384 int i, index = -2, min = -1;
386 int arity = get_Block_n_cfgpreds(n);
387 for (i = 0; i < arity; i++) {
388 ir_node *pred = get_Block_cfgpred_block(n, i);
389 /* ignore Bad control flow: it cannot happen */
392 if (is_backedge(n, i) || !irn_is_in_stack(pred))
394 if (get_irn_dfn(pred) >= limit && (min == -1 || get_irn_dfn(pred) < min)) {
396 min = get_irn_dfn(pred);
403 * Returns index of the predecessor with the largest dfn number.
405 static int largest_dfn_pred(ir_node *n)
407 int i, index = -2, max = -1;
409 int arity = get_Block_n_cfgpreds(n);
410 for (i = 0; i < arity; i++) {
411 ir_node *pred = get_Block_cfgpred_block(n, i);
412 /* ignore Bad control flow: it cannot happen */
415 if (is_backedge(n, i) || !irn_is_in_stack(pred))
417 if (get_irn_dfn(pred) > max) {
419 max = get_irn_dfn(pred);
426 * Searches the stack for possible loop heads. Tests these for backedges.
427 * If it finds a head with an unmarked backedge it marks this edge and
428 * returns the tail of the loop.
429 * If it finds no backedge returns NULL.
431 static ir_node *find_tail(ir_node *n)
437 m = stack[tos - 1]; /* tos = top of stack */
439 res_index = smallest_dfn_pred(m, 0);
440 if ((res_index == -2) && /* no smallest dfn pred found. */
446 for (i = tos - 1; i != 0;) {
449 res_index = smallest_dfn_pred(m, get_irn_dfn(m) + 1);
450 if (res_index == -2) /* no smallest dfn pred found. */
451 res_index = largest_dfn_pred(m);
453 if ((m == n) && (res_index == -2)) {
460 /* We should not walk past our selves on the stack: The upcoming nodes
461 are not in this loop. We assume a loop not reachable from Start. */
468 if (i == (size_t)-1) {
469 /* A dead loop not reachable from Start. */
470 for (i = tos - 1; i != 0;) {
472 if (is_endless_head(m, n)) {
473 res_index = smallest_dfn_pred (m, get_irn_dfn(m) + 1);
474 if (res_index == -2) /* no smallest dfn pred found. */
475 res_index = largest_dfn_pred(m);
478 if (m == n) break; /* It's not an unreachable loop, either. */
480 //assert(0 && "no head found on stack");
483 assert(res_index > -2);
485 set_backedge(m, res_index);
486 return get_Block_cfgpred_block(m, res_index);
490 * returns non.zero if l is the outermost loop.
492 inline static int is_outermost_loop(ir_loop *l)
494 return l == get_loop_outer_loop(l);
497 /*-----------------------------------------------------------*
498 * The core algorithm. *
499 *-----------------------------------------------------------*/
502 * Walks over all blocks of a graph
504 static void cfscc(ir_node *n)
511 if (irn_visited_else_mark(n)) return;
513 /* Initialize the node */
514 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
515 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
516 set_irn_loop(n, NULL);
520 arity = get_Block_n_cfgpreds(n);
522 for (i = 0; i < arity; i++) {
525 if (is_backedge(n, i))
527 m = get_Block_cfgpred_block(n, i);
528 /* ignore Bad control flow: it cannot happen */
533 if (irn_is_in_stack(m)) {
534 /* Uplink of m is smaller if n->m is a backedge.
535 Propagate the uplink to mark the cfloop. */
536 if (get_irn_uplink(m) < get_irn_uplink(n))
537 set_irn_uplink(n, get_irn_uplink(m));
541 if (get_irn_dfn(n) == get_irn_uplink(n)) {
542 /* This condition holds for
543 1) the node with the incoming backedge.
544 That is: We found a cfloop!
545 2) Straight line code, because no uplink has been propagated, so the
546 uplink still is the same as the dfn.
548 But n might not be a proper cfloop head for the analysis. Proper cfloop
549 heads are Block and Phi nodes. find_tail searches the stack for
550 Block's and Phi's and takes those nodes as cfloop heads for the current
551 cfloop instead and marks the incoming edge as backedge. */
553 ir_node *tail = find_tail(n);
555 /* We have a cfloop, that is no straight line code,
556 because we found a cfloop head!
557 Next actions: Open a new cfloop on the cfloop tree and
558 try to find inner cfloops */
560 #if NO_CFLOOPS_WITHOUT_HEAD
562 /* This is an adaption of the algorithm from fiasco / optscc to
563 * avoid cfloops without Block or Phi as first node. This should
564 * severely reduce the number of evaluations of nodes to detect
565 * a fixpoint in the heap analysis.
566 * Further it avoids cfloops without firm nodes that cause errors
567 * in the heap analyses. */
571 if ((get_loop_n_elements(current_loop) > 0) || (is_outermost_loop(current_loop))) {
581 ir_loop *l = new_loop();
585 /* Remove the cfloop from the stack ... */
586 pop_scc_unmark_visit(n);
588 /* The current backedge has been marked, that is temporarily eliminated,
589 by find tail. Start the scc algorithm
590 anew on the subgraph thats left (the current cfloop without the backedge)
591 in order to find more inner cfloops. */
595 assert(irn_visited(n));
596 #if NO_CFLOOPS_WITHOUT_HEAD
601 /* AS: No cfloop head was found, that is we have straight line code.
602 Pop all nodes from the stack to the current cfloop. */
608 int construct_cf_backedges(ir_graph *irg)
611 ir_node *end = get_irg_end(irg);
617 outermost_ir_graph = irg;
620 init_scc(irg, &temp);
623 new_loop(); /* sets current_loop */
624 head_rem = current_loop; /* Just for assertion */
626 inc_irg_visited(irg);
628 /* walk over all blocks of the graph, including keep alives */
629 cfscc(get_irg_end_block(irg));
630 for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
631 ir_node *el = get_End_keepalive(end, i);
636 obstack_free(&temp, NULL);
638 assert(head_rem == current_loop);
639 mature_loops(current_loop, get_irg_obstack(irg));
640 set_irg_loop(irg, current_loop);
641 add_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO);
643 return max_loop_depth;
646 void assure_loopinfo(ir_graph *irg)
648 if (irg_has_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO))
650 construct_cf_backedges(irg);