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
23 * backedge/loop datastructures.
24 * A variation on the Tarjan algorithm. See also [Trapp:99],
26 * @author Goetz Lindenmaier
44 #include "irgraph_t.h"
51 /* A variant of the loop tree that avoids loops without head.
52 This reduces the depth of the loop tree. */
53 #define NO_LOOPS_WITHOUT_HEAD 1
55 /** The outermost graph the scc is computed for. */
56 static ir_graph *outermost_ir_graph;
57 /** Current loop construction is working on. */
58 static ir_loop *current_loop;
59 /** Counts the number of allocated loop nodes.
60 * Each loop node gets a unique number.
61 * @todo What for? ev. remove.
63 static int loop_node_cnt = 0;
64 /** Counter to generate depth first numbering of visited nodes. */
65 static int current_dfn = 1;
67 static int max_loop_depth = 0;
69 void link_to_reg_end(ir_node *n, void *env);
70 void set_projx_link(ir_node *cb_projx, ir_node *end_projx);
71 ir_node *get_projx_link(ir_node *cb_projx);
73 /**********************************************************************/
74 /* Node attributes **/
75 /**********************************************************************/
77 /**********************************************************************/
78 /* Node attributes needed for the construction. **/
79 /**********************************************************************/
81 typedef struct scc_info {
82 int in_stack; /**< Marks whether node is on the stack. */
83 int dfn; /**< Depth first search number. */
84 int uplink; /**< dfn number of ancestor. */
85 /* ir_loop *loop; *//* Refers to the containing loop. */
87 struct section *section;
94 * Allocates a new SCC info on the given obstack.
96 static INLINE scc_info *new_scc_info(struct obstack *obst) {
97 scc_info *info = obstack_alloc(obst, sizeof(*info));
98 memset(info, 0, sizeof(*info));
103 * Mark node n being on the SCC stack.
105 static INLINE void mark_irn_in_stack(ir_node *n) {
106 scc_info *scc = get_irn_link(n);
112 * Mark node n NOT being on the SCC stack.
114 static INLINE void mark_irn_not_in_stack(ir_node *n) {
115 scc_info *scc = get_irn_link(n);
121 * Checks if a node is on the SCC stack.
123 static INLINE int irn_is_in_stack(ir_node *n) {
124 scc_info *scc = get_irn_link(n);
126 return scc->in_stack;
130 * Sets the uplink number for a node.
132 static INLINE void set_irn_uplink(ir_node *n, int uplink) {
133 scc_info *scc = get_irn_link(n);
135 scc->uplink = uplink;
139 * Returns the uplink number for a node.
141 static int get_irn_uplink(ir_node *n) {
142 scc_info *scc = get_irn_link(n);
148 * Sets the depth-first-search number for a node.
150 static INLINE void set_irn_dfn(ir_node *n, int dfn) {
151 scc_info *scc = get_irn_link(n);
157 * Returns the depth-first-search number of a node.
159 static int get_irn_dfn(ir_node *n) {
160 scc_info *scc = get_irn_link(n);
166 static ir_loop *find_nodes_loop(ir_node *n, ir_loop *l) {
170 /* Test whether n is contained in this loop. */
171 for (i = 0; i < get_loop_n_nodes(l); i++)
172 if (n == get_loop_node(l, i)) return l;
174 /* Is this a leave in the loop tree? If so loop not found. */
175 if (get_loop_n_sons(l) == 0) return NULL;
177 /* Else descend in the loop tree. */
178 for (i = 0; i < get_loop_n_sons(l); i++) {
179 res = find_nodes_loop(n, get_loop_son(l, i));
185 /* @@@ temporary implementation, costly!!! */
186 ir_loop * get_irn_loop(ir_node *n) {
187 ir_loop *l = get_irg_loop(current_ir_graph);
188 l = find_nodes_loop(n, l);
193 /**********************************************************************/
195 /**********************************************************************/
197 static ir_node **stack = NULL;
198 static int tos = 0; /* top of stack */
201 * initializes the stack
203 static INLINE void init_stack(void) {
205 ARR_RESIZE(ir_node *, stack, 1000);
207 stack = NEW_ARR_F(ir_node *, 1000);
215 static void finish_stack(void) {
221 * push a node onto the stack
223 * @param n The node to push
225 static INLINE void push(ir_node *n) {
226 if (tos == ARR_LEN(stack)) {
227 int nlen = ARR_LEN(stack) * 2;
228 ARR_RESIZE(ir_node *, stack, nlen);
231 mark_irn_in_stack(n);
235 * pop a node from the stack
237 * @return The topmost node
239 static INLINE ir_node *pop(void) {
240 ir_node *n = stack[--tos];
241 mark_irn_not_in_stack(n);
246 * The nodes up to n belong to the current loop.
247 * Removes them from the stack and adds them to the current loop.
249 static INLINE void pop_scc_to_loop(ir_node *n) {
257 set_irn_dfn(m, loop_node_cnt);
258 add_loop_node(current_loop, m);
259 set_irn_loop(m, current_loop);
263 /* i might be bigger than 1 for dead (and that's why bad) loops */
265 printf("Mehr als eine Iteration!!!!!!!!!!!!!!!!!!!!!!!!!!!!11111\n");
269 /* GL ??? my last son is my grandson??? Removes loops with no
270 ir_nodes in them. Such loops have only another loop as son. (Why
271 can't they have two loops as sons? Does it never get that far? ) */
272 static void close_loop(ir_loop *l) {
273 int last = get_loop_n_elements(l) - 1;
274 loop_element lelement = get_loop_element(l, last);
275 ir_loop *last_son = lelement.son;
277 if (get_kind(last_son) == k_ir_loop &&
278 get_loop_n_elements(last_son) == 1) {
281 lelement = get_loop_element(last_son, 0);
284 if (get_kind(gson) == k_ir_loop) {
285 loop_element new_last_son;
287 gson->outer_loop = l;
288 new_last_son.son = gson;
289 l->children[last] = new_last_son;
296 /* Removes and unmarks all nodes up to n from the stack.
297 The nodes must be visited once more to assign them to a scc. */
298 static INLINE void pop_scc_unmark_visit(ir_node *n) {
303 set_irn_visited(m, 0);
307 /**********************************************************************/
308 /* The loop datastructure. **/
309 /**********************************************************************/
311 /* Allocates a new loop as son of current_loop. Sets current_loop
312 to the new loop and returns the father. */
313 static ir_loop *new_loop(void) {
314 ir_loop *father = current_loop;
315 ir_loop *son = alloc_loop(father, outermost_ir_graph->obst);
317 if (son->depth > max_loop_depth) max_loop_depth = son->depth;
322 /**********************************************************************/
323 /* Constructing and destructing the loop/backedge information. **/
324 /**********************************************************************/
326 /* Initialization steps. **********************************************/
328 static INLINE void init_node(ir_node *n, void *env) {
329 struct obstack *obst = env;
330 set_irn_link(n, new_scc_info(obst));
334 static INLINE void init_scc_common(void) {
340 static INLINE void init_scc(ir_graph *irg, struct obstack *obst) {
342 irg_walk_graph(irg, init_node, NULL, obst);
344 irg_walk (irg, link_to_reg_end, NULL, NULL);
348 static INLINE void finish_scc(void)
353 #ifdef INTERPROCEDURAL_VIEW
354 static INLINE void init_ip_scc(struct obstack *obst) {
356 cg_walk(init_node, NULL, obst);
358 #if EXPERIMENTAL_LOOP_TREE
359 cg_walk(link_to_reg_end, NULL, NULL);
365 * Check weather a given node represents the outer most Start
366 * block. In intra-procedural view this is the start block of the
367 * current graph, in interprocedural view it is the start block
368 * of the outer most graph.
370 * @param n the node to check
372 * This is the condition for breaking the scc recursion.
374 static int is_outermost_Start(ir_node *n) {
375 /* Test whether this is the outermost Start node. */
376 if (is_Block(n) && get_Block_n_cfgpreds(n) == 1) {
377 ir_node *pred = skip_Proj(get_Block_cfgpred(n, 0));
378 if (is_Start(pred) && get_nodes_block(pred) == n)
384 /* When to walk from nodes to blocks. Only for Control flow operations? */
385 static INLINE int get_start_index(ir_node *n) {
386 #undef BLOCK_BEFORE_NODE
387 #define BLOCK_BEFORE_NODE 1
389 #if BLOCK_BEFORE_NODE
391 /* This version assures, that all nodes are ordered absolutely. This allows
392 to undef all nodes in the heap analysis if the block is false, which means
394 I.e., with this code, the order on the loop tree is correct. But a (single)
395 test showed the loop tree is deeper. */
396 if (get_irn_op(n) == op_Phi ||
398 (is_Filter(n) && get_interprocedural_view()) || (
399 get_irg_pinned(get_irn_irg(n)) == op_pin_state_floats &&
400 get_irn_pinned(n) == op_pin_state_floats
402 // Here we could test for backedge at -1 which is illegal
409 /* This version causes deeper loop trees (at least we verified this
411 But it guarantees that Blocks are analysed before nodes contained in the
412 block. If so, we can set the value to undef if the block is not \
414 if (is_cfop(n) || is_fragile_op(n) || is_Start(n))
423 * Return non-zero if the given node is a legal loop header:
424 * Block, Phi, Filter.
426 * @param n the node to check
428 static INLINE int is_possible_loop_head(ir_node *n) {
429 ir_op *op = get_irn_op(n);
430 return ((op == op_Block) ||
432 ((op == op_Filter) && get_interprocedural_view()));
436 * Returns non-zero if n is a loop header, i.e., it is a Block, Phi
437 * or Filter node and has predecessors within the loop and out
440 * @param n the node to check
441 * @param root only needed for assertion.
443 static int is_head(ir_node *n, ir_node *root) {
445 int some_outof_loop = 0, some_in_loop = 0;
447 /* Test for legal loop header: Block, Phi, ... */
448 if (!is_possible_loop_head(n))
451 if (!is_outermost_Start(n)) {
453 int uplink = get_irn_uplink(root);
455 arity = get_irn_arity(n);
456 for (i = get_start_index(n); i < arity; i++) {
458 if (is_backedge(n, i))
460 pred = get_irn_n(n, i);
461 if (! irn_is_in_stack(pred)) {
464 assert(get_irn_uplink(pred) >= uplink);
469 return some_outof_loop & some_in_loop;
473 * Returns non-zero if n is possible loop head of an endless loop.
474 * I.e., it is a Block, Phi or Filter node and has only predecessors
477 * @param n the node to check
478 * @param root only needed for assertion.
480 static int is_endless_head(ir_node *n, ir_node *root) {
482 int none_outof_loop = 1, some_in_loop = 0;
484 /* Test for legal loop header: Block, Phi, ... */
485 if (!is_possible_loop_head(n))
488 if (!is_outermost_Start(n)) {
490 int uplink = get_irn_uplink(root);
492 arity = get_irn_arity(n);
493 for (i = get_start_index(n); i < arity; i++) {
495 if (is_backedge(n, i))
497 pred = get_irn_n(n, i);
498 if (!irn_is_in_stack(pred)) {
501 assert(get_irn_uplink(pred) >= uplink);
506 return none_outof_loop & some_in_loop;
509 /** Returns index of the predecessor with the smallest dfn number
510 greater-equal than limit. */
511 static int smallest_dfn_pred(ir_node *n, int limit) {
512 int i, index = -2, min = -1;
514 if (!is_outermost_Start(n)) {
515 int arity = get_irn_arity(n);
516 for (i = get_start_index(n); i < arity; i++) {
517 ir_node *pred = get_irn_n(n, i);
518 if (is_backedge(n, i) || !irn_is_in_stack(pred))
520 if (get_irn_dfn(pred) >= limit && (min == -1 || get_irn_dfn(pred) < min)) {
522 min = get_irn_dfn(pred);
530 * Returns index of the predecessor with the largest dfn number.
532 static int largest_dfn_pred(ir_node *n) {
533 int i, index = -2, max = -1;
535 if (!is_outermost_Start(n)) {
536 int arity = get_irn_arity(n);
537 for (i = get_start_index(n); i < arity; i++) {
538 ir_node *pred = get_irn_n(n, i);
539 if (is_backedge (n, i) || !irn_is_in_stack(pred))
541 if (get_irn_dfn(pred) > max) {
543 max = get_irn_dfn(pred);
551 * Searches the stack for possible loop heads. Tests these for backedges.
552 * If it finds a head with an unmarked backedge it marks this edge and
553 * returns the tail of the loop.
554 * If it finds no backedge returns NULL.
555 * ("disable_backedge" in fiasco)
557 * @param n A node where uplink == dfn.
559 static ir_node *find_tail(ir_node *n) {
561 int i, res_index = -2;
564 if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
566 m = stack[tos-1]; /* tos = top of stack */
568 res_index = smallest_dfn_pred(m, 0);
569 if ((res_index == -2) && /* no smallest dfn pred found. */
573 if (m == n) return NULL; // Is this to catch Phi - self loops?
574 for (i = tos-2; i >= 0; --i) {
578 res_index = smallest_dfn_pred(m, get_irn_dfn(m) + 1);
579 if (res_index == -2) /* no smallest dfn pred found. */
580 res_index = largest_dfn_pred(m);
582 if ((m == n) && (res_index == -2)) { /* don't walk past loop head. */
588 /* We should not walk past our selves on the stack: The upcoming nodes
589 are not in this loop. We assume a loop not reachable from Start. */
597 /* A dead loop not reachable from Start. */
598 for (i = tos-2; i >= 0; --i) {
600 if (is_endless_head(m, n)) {
601 res_index = smallest_dfn_pred(m, get_irn_dfn(m) + 1);
602 if (res_index == -2) /* no smallest dfn pred found. */
603 res_index = largest_dfn_pred (m);
606 if (m == n) { break; } /* It's not an unreachable loop, either. */
608 //assert(0 && "no head found on stack");
612 if (res_index <= -2) {
613 /* It's a completely bad loop: without Phi/Block nodes that can
614 be a head. I.e., the code is "dying". We break the loop by
615 setting Bad nodes. */
616 int arity = get_irn_arity(n);
617 ir_node *bad = get_irg_bad(get_irn_irg(n));
618 for (i = -1; i < arity; ++i) {
619 set_irn_n(n, i, bad);
623 assert(res_index > -2);
625 set_backedge(m, res_index);
626 return is_outermost_Start(n) ? NULL : get_irn_n(m, res_index);
630 #if EXPERIMENTAL_LOOP_TREE
632 /* ----------------------------------------------------------------
633 AS: This is experimental code to build loop trees suitable for
634 the heap analysis. Does not work correctly right now... :-(
637 Search in stack for the corresponding first Call-End-ProjX that
638 corresponds to one of the control flow predecessors of the given
639 block, that is the possible callers.
640 returns: the control predecessor to chose\
641 or -1 if no corresponding Call-End-Node could be found
643 - -------------------------------------------------------------- */
645 int search_endproj_in_stack(ir_node *start_block) {
647 assert(is_Block(start_block));
648 for(i = tos - 1; i >= 0; --i)
650 if(get_irn_op(stack[i]) == op_Proj && get_irn_mode(stack[i]) == mode_X &&
651 get_irn_op(get_irn_n(stack[i], 0)) == op_EndReg)
653 printf("FOUND PROJ!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
654 ir_node *end_projx = stack[i];
656 int arity = get_irn_arity(start_block);
657 for(j = 0; j < arity; j++)
659 ir_node *begin_projx = get_Block_cfgpred(get_irg_start_block(get_irn_irg(end_projx)),
660 get_Proj_proj(end_projx));
661 if(get_irn_n(start_block, j) == begin_projx)
663 printf("FOUND IT!!!!!!!!!!!!!!!!!!\n");
673 static pmap *projx_link = NULL;
675 void link_to_reg_end (ir_node *n, void *env) {
676 if(get_irn_op(n) == op_Proj &&
677 get_irn_mode(n) == mode_X &&
678 get_irn_op(get_irn_n(n, 0)) == op_EndReg) {
679 /* Reg End Projx -> Find the CallBegin Projx and hash it */
680 ir_node *end_projx = n;
681 ir_node *begin_projx = get_Block_cfgpred(get_irg_start_block(get_irn_irg(end_projx)),
682 get_Proj_proj(end_projx));
683 set_projx_link(begin_projx, end_projx);
687 void set_projx_link(ir_node *cb_projx, ir_node *end_projx) {
688 if(projx_link == NULL)
689 projx_link = pmap_create();
690 pmap_insert(projx_link, (void *)cb_projx, (void *)end_projx);
693 ir_node *get_projx_link(ir_node *cb_projx) {
694 return((ir_node *) pmap_get(projx_link, (void *)cb_projx));
699 static INLINE int is_outermost_loop(ir_loop *l) {
700 return l == get_loop_outer_loop(l);
704 /*-----------------------------------------------------------*
705 * The core algorithm. *
706 *-----------------------------------------------------------*/
709 * The core algorithm: Find strongly coupled components.
711 * @param n node to start
713 static void scc(ir_node *n) {
718 /* Initialize the node */
719 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
720 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
721 set_irn_loop(n, NULL);
725 /* AS: get_start_index might return -1 for Control Flow Nodes, and thus a negative
726 array index would be passed to is_backedge(). But CFG Nodes dont't have a backedge array,
727 so is_backedge does not access array[-1] but correctly returns false! */
729 if (!is_outermost_Start(n)) {
730 int i, arity = get_irn_arity(n);
732 for (i = get_start_index(n); i < arity; ++i) {
734 if (is_backedge(n, i))
738 if (irn_is_in_stack(m)) {
739 /* Uplink of m is smaller if n->m is a backedge.
740 Propagate the uplink to mark the loop. */
741 if (get_irn_uplink(m) < get_irn_uplink(n))
742 set_irn_uplink(n, get_irn_uplink(m));
747 if (get_irn_dfn(n) == get_irn_uplink(n)) {
748 /* This condition holds for
749 1) the node with the incoming backedge.
750 That is: We found a loop!
751 2) Straight line code, because no uplink has been propagated, so the
752 uplink still is the same as the dfn.
754 But n might not be a proper loop head for the analysis. Proper loop
755 heads are Block and Phi nodes. find_tail() searches the stack for
756 Block's and Phi's and takes those nodes as loop heads for the current
757 loop instead and marks the incoming edge as backedge. */
759 ir_node *tail = find_tail(n);
761 /* We have a loop, that is no straight line code,
762 because we found a loop head!
763 Next actions: Open a new loop on the loop tree and
764 try to find inner loops */
766 #if NO_LOOPS_WITHOUT_HEAD
767 /* This is an adaption of the algorithm from fiasco / optscc to
768 * avoid loops without Block or Phi as first node. This should
769 * severely reduce the number of evaluations of nodes to detect
770 * a fixpoint in the heap analysis.
771 * Further it avoids loops without firm nodes that cause errors
772 * in the heap analyses.
773 * But attention: don't do it for the outermost loop: This loop
774 * is not iterated. A first block can be a loop head in case of
775 * an endless recursion. */
779 if ((get_loop_n_elements(current_loop) > 0) || (is_outermost_loop(current_loop))) {
787 ir_loop *l = new_loop();
790 /* Remove the loop from the stack ... */
791 pop_scc_unmark_visit(n);
793 /* The current backedge has been marked, that is temporarily eliminated,
794 by find tail. Start the scc algorithm
795 again on the subgraph that is left (the current loop without the backedge)
796 in order to find more inner loops. */
799 assert(irn_visited(n));
800 #if NO_LOOPS_WITHOUT_HEAD
805 /* No loop head was found, that is we have straight line code.
806 Pop all nodes from the stack to the current loop. */
812 #ifdef INTERPROCEDURAL_VIEW
813 static void my_scc(ir_node *n) {
819 /* Initialize the node */
820 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
821 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
822 set_irn_loop(n, NULL);
826 /* AS: get_start_index might return -1 for Control Flow Nodes, and thus a negative
827 array index would be passed to is_backedge(). But CFG Nodes dont't have a backedge array,
828 so is_backedge does not access array[-1] but correctly returns false! */
830 if (!is_outermost_Start(n)) {
831 int arity = get_irn_arity(n);
833 for (i = get_start_index(n); i < arity; i++) {
835 if (is_backedge(n, i)) continue;
836 m = get_irn_n(n, i); /* get_irn_ip_pred(n, i); */
837 /* if (!m || is_Unknown(m)) continue; */
839 if (irn_is_in_stack(m)) {
840 /* Uplink of m is smaller if n->m is a backedge.
841 Propagate the uplink to mark the loop. */
842 if (get_irn_uplink(m) < get_irn_uplink(n))
843 set_irn_uplink(n, get_irn_uplink(m));
848 if (get_irn_dfn(n) == get_irn_uplink(n)) {
849 /* This condition holds for
850 1) the node with the incoming backedge.
851 That is: We found a loop!
852 2) Straight line code, because no uplink has been propagated, so the
853 uplink still is the same as the dfn.
855 But n might not be a proper loop head for the analysis. Proper loop
856 heads are Block and Phi nodes. find_tail searches the stack for
857 Block's and Phi's and takes those nodes as loop heads for the current
858 loop instead and marks the incoming edge as backedge. */
860 ir_node *tail = find_tail(n);
862 /* We have a loop, that is no straight line code,
863 because we found a loop head!
864 Next actions: Open a new loop on the loop tree and
865 try to find inner loops */
867 #if NO_LOOPS_WITHOUT_HEAD
868 /* This is an adaption of the algorithm from fiasco / optscc to
869 * avoid loops without Block or Phi as first node. This should
870 * severely reduce the number of evaluations of nodes to detect
871 * a fixpoint in the heap analysis.
872 * Further it avoids loops without firm nodes that cause errors
873 * in the heap analyses. */
877 if ((get_loop_n_elements(current_loop) > 0) || (is_outermost_loop(current_loop))) {
885 ir_loop *l = new_loop();
888 /* Remove the loop from the stack ... */
889 pop_scc_unmark_visit(n);
891 /* The current backedge has been marked, that is temporarily eliminated,
892 by find tail. Start the scc algorithm
893 anew on the subgraph that is left (the current loop without the backedge)
894 in order to find more inner loops. */
897 assert(irn_visited(n));
898 #if NO_LOOPS_WITHOUT_HEAD
903 /* No loop head was found, that is we have straightline code.
904 Pop all nodes from the stack to the current loop. */
909 #endif /* INTERPROCEDURAL_VIEW */
911 /* Constructs backedge information for irg. In interprocedural view constructs
912 backedges for all methods called by irg, too. */
913 int construct_backedges(ir_graph *irg) {
914 ir_graph *rem = current_ir_graph;
918 assert(!get_interprocedural_view() &&
919 "not implemented, use construct_ip_backedges()");
922 current_ir_graph = irg;
923 outermost_ir_graph = irg;
926 init_scc(irg, &temp);
929 new_loop(); /* sets current_loop */
930 head_rem = current_loop; /* Just for assertion */
932 inc_irg_visited(irg);
934 scc(get_irg_end(irg));
937 obstack_free(&temp, NULL);
939 assert(head_rem == current_loop);
940 mature_loops(current_loop, irg->obst);
941 set_irg_loop(irg, current_loop);
942 set_irg_loopinfo_state(irg, loopinfo_consistent);
943 assert(get_irg_loop(irg)->kind == k_ir_loop);
944 current_ir_graph = rem;
945 return max_loop_depth;
949 #ifdef INTERPROCEDURAL_VIEW
950 int construct_ip_backedges(void) {
951 ir_graph *rem = current_ir_graph;
952 int rem_ipv = get_interprocedural_view();
957 assert(get_irp_ip_view_state() == ip_view_valid);
959 outermost_ir_graph = get_irp_main_irg();
965 new_loop(); /* sets current_loop */
966 set_interprocedural_view(1);
968 inc_max_irg_visited();
969 for (i = 0; i < get_irp_n_irgs(); i++)
970 set_irg_visited(get_irp_irg(i), get_max_irg_visited());
972 /** We have to start the walk at the same nodes as cg_walk. **/
973 /* Walk starting at unreachable procedures. Only these
974 * have End blocks visible in interprocedural view. */
975 for (i = 0; i < get_irp_n_irgs(); i++) {
977 current_ir_graph = get_irp_irg(i);
979 sb = get_irg_start_block(current_ir_graph);
981 if ((get_Block_n_cfgpreds(sb) > 1) ||
982 (get_nodes_block(get_Block_cfgpred(sb, 0)) != sb))
985 scc(get_irg_end(current_ir_graph));
988 /* Check whether we walked all procedures: there could be procedures
989 with cyclic calls but no call from the outside. */
990 for (i = 0; i < get_irp_n_irgs(); i++) {
992 current_ir_graph = get_irp_irg(i);
994 /* Test start block: if inner procedure end and end block are not
995 * visible and therefore not marked. */
996 sb = get_irg_start_block(current_ir_graph);
997 if (get_irn_visited(sb) < get_irg_visited(current_ir_graph)) scc(sb);
1000 /* Walk all endless loops in inner procedures.
1001 * We recognize an inner procedure if the End node is not visited. */
1002 for (i = 0; i < get_irp_n_irgs(); i++) {
1004 current_ir_graph = get_irp_irg(i);
1006 e = get_irg_end(current_ir_graph);
1007 if (get_irn_visited(e) < get_irg_visited(current_ir_graph)) {
1009 /* Don't visit the End node. */
1010 for (j = 0; j < get_End_n_keepalives(e); j++)
1011 scc(get_End_keepalive(e, j));
1015 set_irg_loop(outermost_ir_graph, current_loop);
1016 set_irg_loopinfo_state(current_ir_graph, loopinfo_ip_consistent);
1017 assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
1019 obstack_free(&temp, NULL);
1020 current_ir_graph = rem;
1021 set_interprocedural_view(rem_ipv);
1022 return max_loop_depth;
1025 void my_construct_ip_backedges(void) {
1026 ir_graph *rem = current_ir_graph;
1027 int rem_ipv = get_interprocedural_view();
1030 assert(get_irp_ip_view_state() == ip_view_valid);
1032 outermost_ir_graph = get_irp_main_irg();
1036 current_loop = NULL;
1037 new_loop(); /* sets current_loop */
1038 set_interprocedural_view(1);
1040 inc_max_irg_visited();
1041 for (i = 0; i < get_irp_n_irgs(); i++)
1042 set_irg_visited(get_irp_irg(i), get_max_irg_visited());
1044 /** We have to start the walk at the same nodes as cg_walk. **/
1045 /* Walk starting at unreachable procedures. Only these
1046 * have End blocks visible in interprocedural view. */
1047 for (i = 0; i < get_irp_n_irgs(); i++) {
1049 current_ir_graph = get_irp_irg(i);
1051 sb = get_irg_start_block(current_ir_graph);
1053 if ((get_Block_n_cfgpreds(sb) > 1) ||
1054 (get_nodes_block(get_Block_cfgpred(sb, 0)) != sb)) continue;
1056 my_scc(get_irg_end(current_ir_graph));
1059 /* Check whether we walked all procedures: there could be procedures
1060 with cyclic calls but no call from the outside. */
1061 for (i = 0; i < get_irp_n_irgs(); i++) {
1063 current_ir_graph = get_irp_irg(i);
1065 /* Test start block: if inner procedure end and end block are not
1066 * visible and therefore not marked. */
1067 sb = get_irg_start_block(current_ir_graph);
1068 if (get_irn_visited(sb) < get_irg_visited(current_ir_graph))
1072 /* Walk all endless loops in inner procedures.
1073 * We recognize an inner procedure if the End node is not visited. */
1074 for (i = 0; i < get_irp_n_irgs(); i++) {
1076 current_ir_graph = get_irp_irg(i);
1078 e = get_irg_end(current_ir_graph);
1079 if (get_irn_visited(e) < get_irg_visited(current_ir_graph)) {
1081 /* Don't visit the End node. */
1082 for (j = 0; j < get_End_n_keepalives(e); j++) scc(get_End_keepalive(e, j));
1086 set_irg_loop(outermost_ir_graph, current_loop);
1087 set_irg_loopinfo_state(current_ir_graph, loopinfo_ip_consistent);
1088 assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
1090 current_ir_graph = rem;
1091 set_interprocedural_view(rem_ipv);
1095 static void reset_backedges(ir_node *n) {
1096 if (is_possible_loop_head(n)) {
1097 #ifdef INTERPROCEDURAL_VIEW
1098 int rem = get_interprocedural_view();
1100 set_interprocedural_view(1);
1102 set_interprocedural_view(1);
1104 set_interprocedural_view(rem);
1113 static void loop_reset_backedges(ir_loop *l) {
1115 reset_backedges(get_loop_node(l, 0));
1116 for (i = 0; i < get_loop_n_nodes(l); ++i)
1117 set_irn_loop(get_loop_node(l, i), NULL);
1118 for (i = 0; i < get_loop_n_sons(l); ++i) {
1119 loop_reset_backedges(get_loop_son(l, i));
1124 static void loop_reset_node(ir_node *n, void *env) {
1126 set_irn_loop(n, NULL);
1131 /** Removes all loop information.
1132 Resets all backedges */
1133 void free_loop_information(ir_graph *irg) {
1134 /* We can not use this recursion, as the loop might contain
1135 illegal nodes by now. Why else would we throw away the
1137 if (get_irg_loop(irg)) loop_reset_backedges(get_irg_loop(irg));
1139 irg_walk_graph(irg, loop_reset_node, NULL, NULL);
1140 set_irg_loop(irg, NULL);
1141 set_irg_loopinfo_state(current_ir_graph, loopinfo_none);
1142 /* We cannot free the loop nodes, they are on the obstack. */
1146 void free_all_loop_information(void) {
1148 #ifdef INTERPROCEDURAL_VIEW
1149 int rem = get_interprocedural_view();
1150 set_interprocedural_view(1); /* To visit all filter nodes */
1152 for (i = 0; i < get_irp_n_irgs(); i++) {
1153 free_loop_information(get_irp_irg(i));
1155 #ifdef INTERPROCEDURAL_VIEW
1156 set_interprocedural_view(rem);
1164 /* Debug stuff *************************************************/
1166 static int test_loop_node(ir_loop *l) {
1167 int i, has_node = 0, found_problem = 0;
1170 assert(l && l->kind == k_ir_loop);
1172 if (get_loop_n_elements(l) == 0) {
1174 dump_loop(l, "-ha");
1177 le = get_loop_element(l, 0);
1178 if (*(le.kind) != k_ir_node) {
1179 assert(le.kind && *(le.kind) == k_ir_loop);
1182 dump_loop(l, "-ha");
1185 if ((*(le.kind) == k_ir_node) && !is_possible_loop_head(le.node)) {
1187 dump_loop(l, "-ha");
1190 if ((get_loop_depth(l) != 0) &&
1191 (*(le.kind) == k_ir_node) && !has_backedges(le.node)) {
1193 dump_loop(l, "-ha");
1198 for (i = 0; i < get_loop_n_elements(l); ++i) {
1199 le = get_loop_element(l, i);
1200 if (*(le.kind) == k_ir_node)
1203 if (test_loop_node(le.son)) found_problem = 1;
1206 if (has_node == 0) {
1208 dump_loop(l, "-ha");
1211 return found_problem;
1214 /** Prints all loop nodes that
1215 * - do not have any firm nodes, only loop sons
1216 * - the header is not a Phi, Block or Filter.
1218 void find_strange_loop_nodes(ir_loop *l) {
1219 int found_problem = 0;
1220 found_problem = test_loop_node(l);
1221 printf("Finished Test\n\n");
1222 if (found_problem) exit(0);
1226 /* ------------------------------------------------------------------- */
1227 /* Simple analyses based on the loop information */
1228 /* ------------------------------------------------------------------- */
1230 int is_loop_variant(ir_loop *l, ir_loop *b) {
1233 if (l == b) return 1;
1235 n_elems = get_loop_n_elements(l);
1236 for (i = 0; i < n_elems; ++i) {
1237 loop_element e = get_loop_element(l, i);
1238 if (is_ir_loop(e.kind))
1239 if (is_loop_variant(e.son, b))
1246 /* Test whether a value is loop invariant.
1248 * @param n The node to be tested.
1249 * @param block A block node. We pass the block, not the loop as we must
1250 * start off with a block loop to find all proper uses.
1252 * Returns non-zero, if the node n is not changed in the loop block
1253 * belongs to or in inner loops of this blocks loop. */
1254 int is_loop_invariant(const ir_node *n, const ir_node *block) {
1255 ir_loop *l = get_irn_loop(block);
1256 const ir_node *b = is_Block(n) ? n : get_nodes_block(n);
1257 return !is_loop_variant(l, get_irn_loop(b));