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
38 #include "irgraph_t.h"
45 /* A variant of the loop tree that avoids loops without head.
46 This reduces the depth of the loop tree. */
47 #define NO_LOOPS_WITHOUT_HEAD 1
49 /** The outermost graph the scc is computed for. */
50 static ir_graph *outermost_ir_graph;
51 /** Current loop construction is working on. */
52 static ir_loop *current_loop;
53 /** Counts the number of allocated loop nodes.
54 * Each loop node gets a unique number.
55 * @todo What for? ev. remove.
57 static int loop_node_cnt = 0;
58 /** Counter to generate depth first numbering of visited nodes. */
59 static int current_dfn = 1;
61 static int max_loop_depth = 0;
63 void link_to_reg_end(ir_node *n, void *env);
64 void set_projx_link(ir_node *cb_projx, ir_node *end_projx);
65 ir_node *get_projx_link(ir_node *cb_projx);
67 /**********************************************************************/
68 /* Node attributes **/
69 /**********************************************************************/
71 /**********************************************************************/
72 /* Node attributes needed for the construction. **/
73 /**********************************************************************/
75 typedef struct scc_info {
76 int in_stack; /**< Marks whether node is on the stack. */
77 int dfn; /**< Depth first search number. */
78 int uplink; /**< dfn number of ancestor. */
79 /* ir_loop *loop; *//* Refers to the containing loop. */
81 struct section *section;
88 * Allocates a new SCC info on the given obstack.
90 static inline scc_info *new_scc_info(struct obstack *obst)
92 return OALLOCZ(obst, scc_info);
96 * Mark node n being on the SCC stack.
98 static inline void mark_irn_in_stack(ir_node *n)
100 scc_info *scc = get_irn_link(n);
106 * Mark node n NOT being on the SCC stack.
108 static inline void mark_irn_not_in_stack(ir_node *n)
110 scc_info *scc = get_irn_link(n);
116 * Checks if a node is on the SCC stack.
118 static inline int irn_is_in_stack(ir_node *n)
120 scc_info *scc = get_irn_link(n);
122 return scc->in_stack;
126 * Sets the uplink number for a node.
128 static inline void set_irn_uplink(ir_node *n, int uplink)
130 scc_info *scc = get_irn_link(n);
132 scc->uplink = uplink;
136 * Returns the uplink number for a node.
138 static int get_irn_uplink(ir_node *n)
140 scc_info *scc = get_irn_link(n);
146 * Sets the depth-first-search number for a node.
148 static inline void set_irn_dfn(ir_node *n, int dfn)
150 scc_info *scc = get_irn_link(n);
156 * Returns the depth-first-search number of a node.
158 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)
171 /* Test whether n is contained in this loop. */
172 for (i = 0; i < get_loop_n_nodes(l); i++)
173 if (n == get_loop_node(l, i)) return l;
175 /* Is this a leave in the loop tree? If so loop not found. */
176 if (get_loop_n_sons(l) == 0) return NULL;
178 /* Else descend in the loop tree. */
179 for (i = 0; i < get_loop_n_sons(l); i++) {
180 res = find_nodes_loop(n, get_loop_son(l, i));
186 /* @@@ temporary implementation, costly!!! */
187 ir_loop * get_irn_loop(ir_node *n)
189 ir_loop *l = get_irg_loop(current_ir_graph);
190 l = find_nodes_loop(n, l);
195 /**********************************************************************/
197 /**********************************************************************/
199 static ir_node **stack = NULL;
200 static int tos = 0; /* top of stack */
203 * initializes the stack
205 static inline void init_stack(void)
208 ARR_RESIZE(ir_node *, stack, 1000);
210 stack = NEW_ARR_F(ir_node *, 1000);
218 static void finish_stack(void)
225 * push a node onto the stack
227 * @param n The node to push
229 static inline void push(ir_node *n)
231 if (tos == ARR_LEN(stack)) {
232 int nlen = ARR_LEN(stack) * 2;
233 ARR_RESIZE(ir_node *, stack, nlen);
236 mark_irn_in_stack(n);
240 * pop a node from the stack
242 * @return The topmost node
244 static inline ir_node *pop(void)
246 ir_node *n = stack[--tos];
247 mark_irn_not_in_stack(n);
252 * The nodes up to n belong to the current loop.
253 * Removes them from the stack and adds them to the current loop.
255 static inline void pop_scc_to_loop(ir_node *n)
264 set_irn_dfn(m, loop_node_cnt);
265 add_loop_node(current_loop, m);
266 set_irn_loop(m, current_loop);
270 /* i might be bigger than 1 for dead (and that's why bad) loops */
272 printf("Mehr als eine Iteration!!!!!!!!!!!!!!!!!!!!!!!!!!!!11111\n");
276 /* GL ??? my last son is my grandson??? Removes loops with no
277 ir_nodes in them. Such loops have only another loop as son. (Why
278 can't they have two loops as sons? Does it never get that far? ) */
279 static void close_loop(ir_loop *l)
281 int last = get_loop_n_elements(l) - 1;
282 loop_element lelement = get_loop_element(l, last);
283 ir_loop *last_son = lelement.son;
285 if (get_kind(last_son) == k_ir_loop &&
286 get_loop_n_elements(last_son) == 1) {
289 lelement = get_loop_element(last_son, 0);
292 if (get_kind(gson) == k_ir_loop) {
293 loop_element new_last_son;
295 gson->outer_loop = l;
296 new_last_son.son = gson;
297 l->children[last] = new_last_son;
304 /* Removes and unmarks all nodes up to n from the stack.
305 The nodes must be visited once more to assign them to a scc. */
306 static inline void pop_scc_unmark_visit(ir_node *n)
312 set_irn_visited(m, 0);
316 /**********************************************************************/
317 /* The loop datastructure. **/
318 /**********************************************************************/
320 /* Allocates a new loop as son of current_loop. Sets current_loop
321 to the new loop and returns the father. */
322 static ir_loop *new_loop(void)
324 ir_loop *father = current_loop;
325 ir_loop *son = alloc_loop(father, outermost_ir_graph->obst);
327 if (son->depth > max_loop_depth) max_loop_depth = son->depth;
332 /**********************************************************************/
333 /* Constructing and destructing the loop/backedge information. **/
334 /**********************************************************************/
336 /* Initialization steps. **********************************************/
338 static inline void init_node(ir_node *n, void *env)
340 struct obstack *obst = env;
341 set_irn_link(n, new_scc_info(obst));
345 static inline void init_scc_common(void)
352 static inline void init_scc(ir_graph *irg, struct obstack *obst)
355 irg_walk_graph(irg, init_node, NULL, obst);
357 irg_walk (irg, link_to_reg_end, NULL, NULL);
361 static inline void finish_scc(void)
366 #ifdef INTERPROCEDURAL_VIEW
367 static inline void init_ip_scc(struct obstack *obst)
370 cg_walk(init_node, NULL, obst);
372 #if EXPERIMENTAL_LOOP_TREE
373 cg_walk(link_to_reg_end, NULL, NULL);
379 * Check weather a given node represents the outer most Start
380 * block. In intra-procedural view this is the start block of the
381 * current graph, in interprocedural view it is the start block
382 * of the outer most graph.
384 * @param n the node to check
386 * This is the condition for breaking the scc recursion.
388 static int is_outermost_Start(ir_node *n)
390 /* Test whether this is the outermost Start node. */
391 if (is_Block(n) && get_Block_n_cfgpreds(n) == 1) {
392 ir_node *pred = skip_Proj(get_Block_cfgpred(n, 0));
393 if (is_Start(pred) && get_nodes_block(pred) == n)
399 static inline int is_ip_Filter(ir_node *n)
401 #ifdef INTERPROCEDURAL_VIEW
402 return is_Filter(n) && get_interprocedural_view();
409 /* When to walk from nodes to blocks. Only for Control flow operations? */
410 static inline int get_start_index(ir_node *n)
412 #undef BLOCK_BEFORE_NODE
413 #define BLOCK_BEFORE_NODE 1
415 #if BLOCK_BEFORE_NODE
417 /* This version assures, that all nodes are ordered absolutely. This allows
418 to undef all nodes in the heap analysis if the block is false, which
420 I.e., with this code, the order on the loop tree is correct. But a
421 (single) test showed the loop tree is deeper. */
422 if (get_irn_op(n) == op_Phi ||
424 (is_ip_Filter(n)) || (
425 get_irg_pinned(get_irn_irg(n)) == op_pin_state_floats &&
426 get_irn_pinned(n) == op_pin_state_floats
428 // Here we could test for backedge at -1 which is illegal
435 /* This version causes deeper loop trees (at least we verified this
437 But it guarantees that Blocks are analysed before nodes contained in the
438 block. If so, we can set the value to undef if the block is not \
440 if (is_cfop(n) || is_fragile_op(n) || is_Start(n))
449 * Return non-zero if the given node is a legal loop header:
450 * Block, Phi, Filter.
452 * @param n the node to check
454 static inline int is_possible_loop_head(ir_node *n)
456 ir_op *op = get_irn_op(n);
457 return ((op == op_Block) ||
463 * Returns non-zero if n is a loop header, i.e., it is a Block, Phi
464 * or Filter node and has predecessors within the loop and out
467 * @param n the node to check
468 * @param root only needed for assertion.
470 static int is_head(ir_node *n, ir_node *root)
473 int some_outof_loop = 0, some_in_loop = 0;
475 /* Test for legal loop header: Block, Phi, ... */
476 if (!is_possible_loop_head(n))
479 if (!is_outermost_Start(n)) {
481 int uplink = get_irn_uplink(root);
485 arity = get_irn_arity(n);
486 for (i = get_start_index(n); i < arity; i++) {
488 if (is_backedge(n, i))
490 pred = get_irn_n(n, i);
491 if (! irn_is_in_stack(pred)) {
494 assert(get_irn_uplink(pred) >= uplink);
499 return some_outof_loop & some_in_loop;
503 * Returns non-zero if n is possible loop head of an endless loop.
504 * I.e., it is a Block, Phi or Filter node and has only predecessors
507 * @param n the node to check
508 * @param root only needed for assertion.
510 static int is_endless_head(ir_node *n, ir_node *root)
513 int none_outof_loop = 1, some_in_loop = 0;
515 /* Test for legal loop header: Block, Phi, ... */
516 if (!is_possible_loop_head(n))
519 if (!is_outermost_Start(n)) {
521 int uplink = get_irn_uplink(root);
525 arity = get_irn_arity(n);
526 for (i = get_start_index(n); i < arity; i++) {
528 if (is_backedge(n, i))
530 pred = get_irn_n(n, i);
531 if (!irn_is_in_stack(pred)) {
534 assert(get_irn_uplink(pred) >= uplink);
539 return none_outof_loop & some_in_loop;
542 /** Returns index of the predecessor with the smallest dfn number
543 greater-equal than limit. */
544 static int smallest_dfn_pred(ir_node *n, int limit)
546 int i, index = -2, min = -1;
548 if (!is_outermost_Start(n)) {
549 int arity = get_irn_arity(n);
550 for (i = get_start_index(n); i < arity; i++) {
551 ir_node *pred = get_irn_n(n, i);
552 if (is_backedge(n, i) || !irn_is_in_stack(pred))
554 if (get_irn_dfn(pred) >= limit && (min == -1 || get_irn_dfn(pred) < min)) {
556 min = get_irn_dfn(pred);
564 * Returns index of the predecessor with the largest dfn number.
566 static int largest_dfn_pred(ir_node *n)
568 int i, index = -2, max = -1;
570 if (!is_outermost_Start(n)) {
571 int arity = get_irn_arity(n);
572 for (i = get_start_index(n); i < arity; i++) {
573 ir_node *pred = get_irn_n(n, i);
574 if (is_backedge (n, i) || !irn_is_in_stack(pred))
576 if (get_irn_dfn(pred) > max) {
578 max = get_irn_dfn(pred);
586 * Searches the stack for possible loop heads. Tests these for backedges.
587 * If it finds a head with an unmarked backedge it marks this edge and
588 * returns the tail of the loop.
589 * If it finds no backedge returns NULL.
590 * ("disable_backedge" in fiasco)
592 * @param n A node where uplink == dfn.
594 static ir_node *find_tail(ir_node *n)
597 int i, res_index = -2;
600 if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
602 m = stack[tos-1]; /* tos = top of stack */
604 res_index = smallest_dfn_pred(m, 0);
605 if ((res_index == -2) && /* no smallest dfn pred found. */
609 if (m == n) return NULL; // Is this to catch Phi - self loops?
610 for (i = tos-2; i >= 0; --i) {
614 res_index = smallest_dfn_pred(m, get_irn_dfn(m) + 1);
615 if (res_index == -2) /* no smallest dfn pred found. */
616 res_index = largest_dfn_pred(m);
618 if ((m == n) && (res_index == -2)) { /* don't walk past loop head. */
624 /* We should not walk past our selves on the stack: The upcoming nodes
625 are not in this loop. We assume a loop not reachable from Start. */
633 /* A dead loop not reachable from Start. */
634 for (i = tos-2; i >= 0; --i) {
636 if (is_endless_head(m, n)) {
637 res_index = smallest_dfn_pred(m, get_irn_dfn(m) + 1);
638 if (res_index == -2) /* no smallest dfn pred found. */
639 res_index = largest_dfn_pred (m);
642 if (m == n) { break; } /* It's not an unreachable loop, either. */
644 //assert(0 && "no head found on stack");
648 if (res_index <= -2) {
649 /* It's a completely bad loop: without Phi/Block nodes that can
650 be a head. I.e., the code is "dying". We break the loop by
651 setting Bad nodes. */
652 int arity = get_irn_arity(n);
653 ir_node *bad = get_irg_bad(get_irn_irg(n));
654 for (i = -1; i < arity; ++i) {
655 set_irn_n(n, i, bad);
659 assert(res_index > -2);
661 set_backedge(m, res_index);
662 return is_outermost_Start(n) ? NULL : get_irn_n(m, res_index);
666 #if EXPERIMENTAL_LOOP_TREE
668 /* ----------------------------------------------------------------
669 AS: This is experimental code to build loop trees suitable for
670 the heap analysis. Does not work correctly right now... :-(
673 Search in stack for the corresponding first Call-End-ProjX that
674 corresponds to one of the control flow predecessors of the given
675 block, that is the possible callers.
676 returns: the control predecessor to chose\
677 or -1 if no corresponding Call-End-Node could be found
679 - -------------------------------------------------------------- */
681 int search_endproj_in_stack(ir_node *start_block)
684 assert(is_Block(start_block));
685 for (i = tos - 1; i >= 0; --i)
687 if (get_irn_op(stack[i]) == op_Proj && get_irn_mode(stack[i]) == mode_X &&
688 get_irn_op(get_irn_n(stack[i], 0)) == op_EndReg)
690 printf("FOUND PROJ!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
691 ir_node *end_projx = stack[i];
693 int arity = get_irn_arity(start_block);
694 for (j = 0; j < arity; j++)
696 ir_node *begin_projx = get_Block_cfgpred(get_irg_start_block(get_irn_irg(end_projx)),
697 get_Proj_proj(end_projx));
698 if (get_irn_n(start_block, j) == begin_projx)
700 printf("FOUND IT!!!!!!!!!!!!!!!!!!\n");
710 static pmap *projx_link = NULL;
712 void link_to_reg_end (ir_node *n, void *env)
714 if (get_irn_op(n) == op_Proj &&
715 get_irn_mode(n) == mode_X &&
716 get_irn_op(get_irn_n(n, 0)) == op_EndReg) {
717 /* Reg End Projx -> Find the CallBegin Projx and hash it */
718 ir_node *end_projx = n;
719 ir_node *begin_projx = get_Block_cfgpred(get_irg_start_block(get_irn_irg(end_projx)),
720 get_Proj_proj(end_projx));
721 set_projx_link(begin_projx, end_projx);
725 void set_projx_link(ir_node *cb_projx, ir_node *end_projx)
727 if (projx_link == NULL)
728 projx_link = pmap_create();
729 pmap_insert(projx_link, (void *)cb_projx, (void *)end_projx);
732 ir_node *get_projx_link(ir_node *cb_projx)
734 return((ir_node *) pmap_get(projx_link, (void *)cb_projx));
739 static inline int is_outermost_loop(ir_loop *l)
741 return l == get_loop_outer_loop(l);
745 /*-----------------------------------------------------------*
746 * The core algorithm. *
747 *-----------------------------------------------------------*/
750 * The core algorithm: Find strongly coupled components.
752 * @param n node to start
754 static void scc(ir_node *n)
756 if (irn_visited_else_mark(n))
759 /* Initialize the node */
760 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
761 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
762 set_irn_loop(n, NULL);
766 /* AS: get_start_index might return -1 for Control Flow Nodes, and thus a negative
767 array index would be passed to is_backedge(). But CFG Nodes dont't have a backedge array,
768 so is_backedge does not access array[-1] but correctly returns false! */
770 if (!is_outermost_Start(n)) {
771 int i, arity = get_irn_arity(n);
773 for (i = get_start_index(n); i < arity; ++i) {
775 if (is_backedge(n, i))
779 if (irn_is_in_stack(m)) {
780 /* Uplink of m is smaller if n->m is a backedge.
781 Propagate the uplink to mark the loop. */
782 if (get_irn_uplink(m) < get_irn_uplink(n))
783 set_irn_uplink(n, get_irn_uplink(m));
788 if (get_irn_dfn(n) == get_irn_uplink(n)) {
789 /* This condition holds for
790 1) the node with the incoming backedge.
791 That is: We found a loop!
792 2) Straight line code, because no uplink has been propagated, so the
793 uplink still is the same as the dfn.
795 But n might not be a proper loop head for the analysis. Proper loop
796 heads are Block and Phi nodes. find_tail() searches the stack for
797 Block's and Phi's and takes those nodes as loop heads for the current
798 loop instead and marks the incoming edge as backedge. */
800 ir_node *tail = find_tail(n);
802 /* We have a loop, that is no straight line code,
803 because we found a loop head!
804 Next actions: Open a new loop on the loop tree and
805 try to find inner loops */
807 #if NO_LOOPS_WITHOUT_HEAD
808 /* This is an adaption of the algorithm from fiasco / optscc to
809 * avoid loops without Block or Phi as first node. This should
810 * severely reduce the number of evaluations of nodes to detect
811 * a fixpoint in the heap analysis.
812 * Further it avoids loops without firm nodes that cause errors
813 * in the heap analyses.
814 * But attention: don't do it for the outermost loop: This loop
815 * is not iterated. A first block can be a loop head in case of
816 * an endless recursion. */
820 if ((get_loop_n_elements(current_loop) > 0) || (is_outermost_loop(current_loop))) {
828 ir_loop *l = new_loop();
831 /* Remove the loop from the stack ... */
832 pop_scc_unmark_visit(n);
834 /* The current backedge has been marked, that is temporarily eliminated,
835 by find tail. Start the scc algorithm
836 again on the subgraph that is left (the current loop without the backedge)
837 in order to find more inner loops. */
840 assert(irn_visited(n));
841 #if NO_LOOPS_WITHOUT_HEAD
846 /* No loop head was found, that is we have straight line code.
847 Pop all nodes from the stack to the current loop. */
853 #ifdef INTERPROCEDURAL_VIEW
854 static void my_scc(ir_node *n)
857 if (irn_visited_else_mark(n))
860 /* Initialize the node */
861 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
862 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
863 set_irn_loop(n, NULL);
867 /* AS: get_start_index might return -1 for Control Flow Nodes, and thus a negative
868 array index would be passed to is_backedge(). But CFG Nodes dont't have a backedge array,
869 so is_backedge does not access array[-1] but correctly returns false! */
871 if (!is_outermost_Start(n)) {
872 int arity = get_irn_arity(n);
874 for (i = get_start_index(n); i < arity; i++) {
876 if (is_backedge(n, i)) continue;
877 m = get_irn_n(n, i); /* get_irn_ip_pred(n, i); */
878 /* if (!m || is_Unknown(m)) continue; */
880 if (irn_is_in_stack(m)) {
881 /* Uplink of m is smaller if n->m is a backedge.
882 Propagate the uplink to mark the loop. */
883 if (get_irn_uplink(m) < get_irn_uplink(n))
884 set_irn_uplink(n, get_irn_uplink(m));
889 if (get_irn_dfn(n) == get_irn_uplink(n)) {
890 /* This condition holds for
891 1) the node with the incoming backedge.
892 That is: We found a loop!
893 2) Straight line code, because no uplink has been propagated, so the
894 uplink still is the same as the dfn.
896 But n might not be a proper loop head for the analysis. Proper loop
897 heads are Block and Phi nodes. find_tail searches the stack for
898 Block's and Phi's and takes those nodes as loop heads for the current
899 loop instead and marks the incoming edge as backedge. */
901 ir_node *tail = find_tail(n);
903 /* We have a loop, that is no straight line code,
904 because we found a loop head!
905 Next actions: Open a new loop on the loop tree and
906 try to find inner loops */
908 #if NO_LOOPS_WITHOUT_HEAD
909 /* This is an adaption of the algorithm from fiasco / optscc to
910 * avoid loops without Block or Phi as first node. This should
911 * severely reduce the number of evaluations of nodes to detect
912 * a fixpoint in the heap analysis.
913 * Further it avoids loops without firm nodes that cause errors
914 * in the heap analyses. */
918 if ((get_loop_n_elements(current_loop) > 0) || (is_outermost_loop(current_loop))) {
926 ir_loop *l = new_loop();
929 /* Remove the loop from the stack ... */
930 pop_scc_unmark_visit(n);
932 /* The current backedge has been marked, that is temporarily eliminated,
933 by find tail. Start the scc algorithm
934 anew on the subgraph that is left (the current loop without the backedge)
935 in order to find more inner loops. */
938 assert(irn_visited(n));
939 #if NO_LOOPS_WITHOUT_HEAD
944 /* No loop head was found, that is we have straightline code.
945 Pop all nodes from the stack to the current loop. */
950 #endif /* INTERPROCEDURAL_VIEW */
952 /* Constructs backedge information for irg. In interprocedural view constructs
953 backedges for all methods called by irg, too. */
954 int construct_backedges(ir_graph *irg)
956 ir_graph *rem = current_ir_graph;
960 #ifdef INTERPROCEDURAL_VIEW
961 assert(!get_interprocedural_view() &&
962 "not implemented, use construct_ip_backedges()");
966 current_ir_graph = irg;
967 outermost_ir_graph = irg;
970 init_scc(irg, &temp);
973 new_loop(); /* sets current_loop */
974 head_rem = current_loop; /* Just for assertion */
976 inc_irg_visited(irg);
978 scc(get_irg_end(irg));
981 obstack_free(&temp, NULL);
983 assert(head_rem == current_loop);
984 mature_loops(current_loop, irg->obst);
985 set_irg_loop(irg, current_loop);
986 set_irg_loopinfo_state(irg, loopinfo_consistent);
987 assert(get_irg_loop(irg)->kind == k_ir_loop);
988 current_ir_graph = rem;
989 return max_loop_depth;
993 #ifdef INTERPROCEDURAL_VIEW
994 int construct_ip_backedges(void)
996 ir_graph *rem = current_ir_graph;
997 int rem_ipv = get_interprocedural_view();
1002 assert(get_irp_ip_view_state() == ip_view_valid);
1004 outermost_ir_graph = get_irp_main_irg();
1006 obstack_init(&temp);
1009 current_loop = NULL;
1010 new_loop(); /* sets current_loop */
1011 set_interprocedural_view(1);
1013 inc_max_irg_visited();
1014 for (i = 0; i < get_irp_n_irgs(); i++)
1015 set_irg_visited(get_irp_irg(i), get_max_irg_visited());
1017 /** We have to start the walk at the same nodes as cg_walk. **/
1018 /* Walk starting at unreachable procedures. Only these
1019 * have End blocks visible in interprocedural view. */
1020 for (i = 0; i < get_irp_n_irgs(); i++) {
1022 current_ir_graph = get_irp_irg(i);
1024 sb = get_irg_start_block(current_ir_graph);
1026 if ((get_Block_n_cfgpreds(sb) > 1) ||
1027 (get_nodes_block(get_Block_cfgpred(sb, 0)) != sb))
1030 scc(get_irg_end(current_ir_graph));
1033 /* Check whether we walked all procedures: there could be procedures
1034 with cyclic calls but no call from the outside. */
1035 for (i = 0; i < get_irp_n_irgs(); i++) {
1037 current_ir_graph = get_irp_irg(i);
1039 /* Test start block: if inner procedure end and end block are not
1040 * visible and therefore not marked. */
1041 sb = get_irg_start_block(current_ir_graph);
1042 if (get_irn_visited(sb) < get_irg_visited(current_ir_graph)) scc(sb);
1045 /* Walk all endless loops in inner procedures.
1046 * We recognize an inner procedure if the End node is not visited. */
1047 for (i = 0; i < get_irp_n_irgs(); i++) {
1049 current_ir_graph = get_irp_irg(i);
1051 e = get_irg_end(current_ir_graph);
1052 if (get_irn_visited(e) < get_irg_visited(current_ir_graph)) {
1054 /* Don't visit the End node. */
1055 for (j = 0; j < get_End_n_keepalives(e); j++)
1056 scc(get_End_keepalive(e, j));
1060 set_irg_loop(outermost_ir_graph, current_loop);
1061 set_irg_loopinfo_state(current_ir_graph, loopinfo_ip_consistent);
1062 assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
1064 obstack_free(&temp, NULL);
1065 current_ir_graph = rem;
1066 set_interprocedural_view(rem_ipv);
1067 return max_loop_depth;
1070 void my_construct_ip_backedges(void)
1072 ir_graph *rem = current_ir_graph;
1073 int rem_ipv = get_interprocedural_view();
1076 assert(get_irp_ip_view_state() == ip_view_valid);
1078 outermost_ir_graph = get_irp_main_irg();
1082 current_loop = NULL;
1083 new_loop(); /* sets current_loop */
1084 set_interprocedural_view(1);
1086 inc_max_irg_visited();
1087 for (i = 0; i < get_irp_n_irgs(); i++)
1088 set_irg_visited(get_irp_irg(i), get_max_irg_visited());
1090 /** We have to start the walk at the same nodes as cg_walk. **/
1091 /* Walk starting at unreachable procedures. Only these
1092 * have End blocks visible in interprocedural view. */
1093 for (i = 0; i < get_irp_n_irgs(); i++) {
1095 current_ir_graph = get_irp_irg(i);
1097 sb = get_irg_start_block(current_ir_graph);
1099 if ((get_Block_n_cfgpreds(sb) > 1) ||
1100 (get_nodes_block(get_Block_cfgpred(sb, 0)) != sb)) continue;
1102 my_scc(get_irg_end(current_ir_graph));
1105 /* Check whether we walked all procedures: there could be procedures
1106 with cyclic calls but no call from the outside. */
1107 for (i = 0; i < get_irp_n_irgs(); i++) {
1109 current_ir_graph = get_irp_irg(i);
1111 /* Test start block: if inner procedure end and end block are not
1112 * visible and therefore not marked. */
1113 sb = get_irg_start_block(current_ir_graph);
1114 if (get_irn_visited(sb) < get_irg_visited(current_ir_graph))
1118 /* Walk all endless loops in inner procedures.
1119 * We recognize an inner procedure if the End node is not visited. */
1120 for (i = 0; i < get_irp_n_irgs(); i++) {
1122 current_ir_graph = get_irp_irg(i);
1124 e = get_irg_end(current_ir_graph);
1125 if (get_irn_visited(e) < get_irg_visited(current_ir_graph)) {
1127 /* Don't visit the End node. */
1128 for (j = 0; j < get_End_n_keepalives(e); j++) scc(get_End_keepalive(e, j));
1132 set_irg_loop(outermost_ir_graph, current_loop);
1133 set_irg_loopinfo_state(current_ir_graph, loopinfo_ip_consistent);
1134 assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
1136 current_ir_graph = rem;
1137 set_interprocedural_view(rem_ipv);
1141 static void reset_backedges(ir_node *n)
1143 if (is_possible_loop_head(n)) {
1144 #ifdef INTERPROCEDURAL_VIEW
1145 int rem = get_interprocedural_view();
1147 set_interprocedural_view(1);
1149 set_interprocedural_view(1);
1151 set_interprocedural_view(rem);
1160 static void loop_reset_backedges(ir_loop *l)
1163 reset_backedges(get_loop_node(l, 0));
1164 for (i = 0; i < get_loop_n_nodes(l); ++i)
1165 set_irn_loop(get_loop_node(l, i), NULL);
1166 for (i = 0; i < get_loop_n_sons(l); ++i) {
1167 loop_reset_backedges(get_loop_son(l, i));
1172 static void loop_reset_node(ir_node *n, void *env)
1175 set_irn_loop(n, NULL);
1180 /** Removes all loop information.
1181 Resets all backedges */
1182 void free_loop_information(ir_graph *irg)
1184 /* We can not use this recursion, as the loop might contain
1185 illegal nodes by now. Why else would we throw away the
1187 if (get_irg_loop(irg)) loop_reset_backedges(get_irg_loop(irg));
1189 irg_walk_graph(irg, loop_reset_node, NULL, NULL);
1190 set_irg_loop(irg, NULL);
1191 set_irg_loopinfo_state(current_ir_graph, loopinfo_none);
1192 /* We cannot free the loop nodes, they are on the obstack. */
1196 void free_all_loop_information(void)
1199 #ifdef INTERPROCEDURAL_VIEW
1200 int rem = get_interprocedural_view();
1201 set_interprocedural_view(1); /* To visit all filter nodes */
1203 for (i = 0; i < get_irp_n_irgs(); i++) {
1204 free_loop_information(get_irp_irg(i));
1206 #ifdef INTERPROCEDURAL_VIEW
1207 set_interprocedural_view(rem);
1211 /* ------------------------------------------------------------------- */
1212 /* Simple analyses based on the loop information */
1213 /* ------------------------------------------------------------------- */
1215 static int is_loop_variant(ir_loop *l, ir_loop *b)
1219 if (l == b) return 1;
1221 n_elems = get_loop_n_elements(l);
1222 for (i = 0; i < n_elems; ++i) {
1223 loop_element e = get_loop_element(l, i);
1224 if (is_ir_loop(e.kind))
1225 if (is_loop_variant(e.son, b))
1232 /* Test whether a value is loop invariant.
1234 * @param n The node to be tested.
1235 * @param block A block node. We pass the block, not the loop as we must
1236 * start off with a block loop to find all proper uses.
1238 * Returns non-zero, if the node n is not changed in the loop block
1239 * belongs to or in inner loops of this blocks loop. */
1240 int is_loop_invariant(const ir_node *n, const ir_node *block)
1242 ir_loop *l = get_irn_loop(block);
1243 const ir_node *b = is_Block(n) ? n : get_nodes_block(n);
1244 return !is_loop_variant(l, get_irn_loop(b));