2 * Copyright (C) 1995-2007 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"
52 /* A variant of the loop tree that avoids loops without head.
53 This reduces the depth of the loop tree. */
54 #define NO_LOOPS_WITHOUT_HEAD 1
56 static ir_graph *outermost_ir_graph; /* The outermost graph the scc is computed
58 static ir_loop *current_loop; /* Current loop construction is working
60 static int loop_node_cnt = 0; /* Counts the number of allocated loop nodes.
61 Each loop node gets a unique number.
62 What for? ev. remove. @@@ */
63 static int current_dfn = 1; /* Counter to generate depth first numbering
66 static int max_loop_depth = 0;
68 void link_to_reg_end (ir_node *n, void *env);
69 void set_projx_link(ir_node *cb_projx, ir_node *end_projx);
70 ir_node *get_projx_link(ir_node *cb_projx);
72 /**********************************************************************/
73 /* Node attributes **/
74 /**********************************************************************/
76 /**********************************************************************/
77 /* Node attributes needed for the construction. **/
78 /**********************************************************************/
80 typedef struct scc_info {
81 int in_stack; /**< Marks whether node is on the stack. */
82 int dfn; /**< Depth first search number. */
83 int uplink; /**< dfn number of ancestor. */
84 /* ir_loop *loop; *//* Refers to the containing loop. */
86 struct section *section;
92 static INLINE scc_info* new_scc_info(void) {
93 scc_info *info = obstack_alloc (outermost_ir_graph->obst, sizeof (scc_info));
94 memset (info, 0, sizeof (scc_info));
99 mark_irn_in_stack (ir_node *n) {
100 scc_info *scc = get_irn_link(n);
106 mark_irn_not_in_stack (ir_node *n) {
107 scc_info *scc = get_irn_link(n);
113 irn_is_in_stack (ir_node *n) {
114 scc_info *scc = get_irn_link(n);
116 return scc->in_stack;
120 set_irn_uplink (ir_node *n, int uplink) {
121 scc_info *scc = get_irn_link(n);
123 scc->uplink = uplink;
127 get_irn_uplink (ir_node *n) {
128 scc_info *scc = get_irn_link(n);
134 set_irn_dfn (ir_node *n, int dfn) {
135 scc_info *scc = get_irn_link(n);
141 get_irn_dfn (ir_node *n) {
142 scc_info *scc = get_irn_link(n);
149 set_irn_loop (ir_node *n, ir_loop *loop) {
153 /* Uses temporary information to get the loop */
154 ir_loop *(get_irn_loop)(const ir_node *n) {
155 return _get_irn_loop(n);
160 static ir_loop *find_nodes_loop (ir_node *n, ir_loop *l) {
164 /* Test whether n is contained in this loop. */
165 for (i = 0; i < get_loop_n_nodes(l); i++)
166 if (n == get_loop_node(l, i)) return l;
168 /* Is this a leave in the loop tree? If so loop not found. */
169 if (get_loop_n_sons(l) == 0) return NULL;
171 /* Else descend in the loop tree. */
172 for (i = 0; i < get_loop_n_sons(l); i++) {
173 res = find_nodes_loop(n, get_loop_son(l, i));
179 /* @@@ temporary implementation, costly!!! */
180 ir_loop * get_irn_loop(ir_node *n) {
181 ir_loop *l = get_irg_loop(current_ir_graph);
182 l = find_nodes_loop(n, l);
187 /**********************************************************************/
189 /**********************************************************************/
191 static ir_node **stack = NULL;
192 static int tos = 0; /* top of stack */
195 * initializes the stack
197 static INLINE void init_stack(void) {
199 ARR_RESIZE (ir_node *, stack, 1000);
201 stack = NEW_ARR_F (ir_node *, 1000);
207 static INLINE void free_stack(void) {
215 * push a node onto the stack
217 * @param n The node to push
222 if (tos == ARR_LEN (stack)) {
223 int nlen = ARR_LEN (stack) * 2;
224 ARR_RESIZE (ir_node *, stack, nlen);
227 mark_irn_in_stack(n);
231 * pop a node from the stack
233 * @return The topmost node
235 static INLINE ir_node *
238 ir_node *n = stack[--tos];
239 mark_irn_not_in_stack(n);
244 * The nodes up to n belong to the current loop.
245 * Removes them from the stack and adds them to the current loop.
248 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);
262 /* if (m==n) break;*/
265 /* i might be bigger than 1 for dead (and that's why bad) loops */
267 printf("Mehr als eine Iteration!!!!!!!!!!!!!!!!!!!!!!!!!!!!11111\n");
271 /* GL ??? my last son is my grandson??? Removes loops with no
272 ir_nodes in them. Such loops have only another loop as son. (Why
273 can't they have two loops as sons? Does it never get that far? ) */
274 static void close_loop (ir_loop *l)
276 int last = get_loop_n_elements(l) - 1;
277 loop_element lelement = get_loop_element(l, last);
278 ir_loop *last_son = lelement.son;
280 if (get_kind(last_son) == k_ir_loop &&
281 get_loop_n_elements(last_son) == 1) {
284 lelement = get_loop_element(last_son, 0);
287 if (get_kind(gson) == k_ir_loop) {
288 loop_element new_last_son;
290 gson->outer_loop = l;
291 new_last_son.son = gson;
292 l->children[last] = new_last_son;
299 /* Removes and unmarks all nodes up to n from the stack.
300 The nodes must be visited once more to assign them to a scc. */
302 pop_scc_unmark_visit (ir_node *n)
308 set_irn_visited(m, 0);
312 /**********************************************************************/
313 /* The loop datastructure. **/
314 /**********************************************************************/
316 /* Allocates a new loop as son of current_loop. Sets current_loop
317 to the new loop and returns the father. */
318 ir_loop *new_loop (void) {
319 ir_loop *father, *son;
321 father = current_loop;
323 son = (ir_loop *) obstack_alloc (outermost_ir_graph->obst, sizeof (ir_loop));
324 memset (son, 0, sizeof (ir_loop));
325 son->kind = k_ir_loop;
326 son->children = NEW_ARR_F (loop_element, 0);
330 son->outer_loop = father;
331 add_loop_son(father, son);
332 son->depth = father->depth+1;
333 if (son->depth > max_loop_depth) max_loop_depth = son->depth;
334 } else { /* The root loop */
335 son->outer_loop = son;
340 son->loop_nr = get_irp_new_node_nr();
349 /* Finishes the datastructures, copies the arrays to the obstack
351 A. Schoesser: Caution: loop -> sons is gone. */
352 static void mature_loop (ir_loop *loop) {
355 new_sons = NEW_ARR_D (ir_loop *, current_ir_graph->obst, ARR_LEN(loop->sons));
356 memcpy (new_sons, loop->sons, sizeof (ir_loop *) * ARR_LEN(loop->sons));
357 DEL_ARR_F(loop->sons);
358 loop->sons = new_sons;
362 /* Returns outer loop, itself if outermost. */
363 ir_loop *(get_loop_outer_loop)(const ir_loop *loop) {
364 return _get_loop_outer_loop(loop);
367 /* Returns nesting depth of this loop */
368 int (get_loop_depth)(const ir_loop *loop) {
369 return _get_loop_depth(loop);
372 /* Returns the number of inner loops */
373 int (get_loop_n_sons)(const ir_loop *loop) {
374 return _get_loop_n_sons(loop);
377 /* Returns the pos`th loop_node-child *
378 * TODO: This method isn`t very efficient ! *
379 * Returns NULL if there isn`t a pos`th loop_node */
380 ir_loop *get_loop_son (ir_loop *loop, int pos) {
381 int child_nr = 0, loop_nr = -1;
383 assert(loop && loop->kind == k_ir_loop);
384 while(child_nr < ARR_LEN(loop->children))
386 if(*(loop -> children[child_nr].kind) == k_ir_loop)
389 return(loop -> children[child_nr].son);
395 /* Use EXCLUSIVELY this function to add sons, otherwise the loop->n_sons
399 add_loop_son(ir_loop *loop, ir_loop *son) {
402 assert(loop && loop->kind == k_ir_loop);
403 assert(get_kind(son) == k_ir_loop);
404 ARR_APP1 (loop_element, loop->children, lson);
408 /* Returns the number of nodes in the loop */
409 int get_loop_n_nodes (ir_loop *loop) {
410 assert(loop); assert(loop->kind == k_ir_loop);
411 return loop -> n_nodes;
412 /* return ARR_LEN(loop->nodes); */
415 /* Returns the pos`th ir_node-child *
416 * TODO: This method isn`t very efficient ! *
417 * Returns NULL if there isn`t a pos`th ir_node */
418 ir_node *get_loop_node (ir_loop *loop, int pos) {
419 int child_nr, node_nr = -1;
421 assert(loop && loop->kind == k_ir_loop);
422 assert(pos < get_loop_n_nodes(loop));
424 for (child_nr = 0; child_nr < ARR_LEN(loop->children); child_nr++) {
425 if(*(loop -> children[child_nr].kind) == k_ir_node)
428 return(loop -> children[child_nr].node);
431 assert(0 && "no child at pos found");
435 /* Use EXCLUSIVELY this function to add nodes, otherwise the loop->n_nodes
439 add_loop_node(ir_loop *loop, ir_node *n) {
442 assert(loop && loop->kind == k_ir_loop);
443 assert(get_kind(n) == k_ir_node || get_kind(n) == k_ir_graph); /* used in callgraph.c */
444 ARR_APP1 (loop_element, loop->children, ln);
448 /* Returns the number of elements contained in loop. */
449 int get_loop_n_elements (const ir_loop *loop) {
450 assert(loop && loop->kind == k_ir_loop);
451 return(ARR_LEN(loop->children));
455 Returns the pos`th loop element.
456 This may be a loop_node or a ir_node. The caller of this function has
457 to check the *(loop_element.kind) field for "k_ir_node" or "k_ir_loop"
458 and then select the appropriate "loop_element.node" or "loop_element.son".
461 loop_element get_loop_element(const ir_loop *loop, int pos) {
462 assert(loop && loop->kind == k_ir_loop && pos < ARR_LEN(loop->children));
463 return(loop -> children[pos]);
466 int get_loop_element_pos(const ir_loop *loop, void *le) {
468 assert(loop && loop->kind == k_ir_loop);
470 n = get_loop_n_elements(loop);
471 for (i = 0; i < n; i++)
472 if (get_loop_element(loop, i).node == le) return i;
476 int get_loop_loop_nr(const ir_loop *loop) {
477 assert(loop && loop->kind == k_ir_loop);
479 return loop->loop_nr;
486 /** A field to connect additional information to a loop. Only valid
487 if libfirm_debug is set. */
488 void set_loop_link (ir_loop *loop, void *link) {
489 assert(loop && loop->kind == k_ir_loop);
492 void *get_loop_link (const ir_loop *loop) {
493 assert(loop && loop->kind == k_ir_loop);
497 int (is_ir_loop)(const void *thing) {
498 return _is_ir_loop(thing);
501 /* The outermost loop is remarked in the surrounding graph. */
502 void (set_irg_loop)(ir_graph *irg, ir_loop *loop) {
503 _set_irg_loop(irg, loop);
506 /* Returns the root loop info (if exists) for an irg. */
507 ir_loop *(get_irg_loop)(ir_graph *irg) {
508 return _get_irg_loop(irg);
512 /**********************************************************************/
513 /* Constructing and destructing the loop/backedge information. **/
514 /**********************************************************************/
516 /* Initialization steps. **********************************************/
519 init_node (ir_node *n, void *env) {
521 set_irn_link (n, new_scc_info());
526 init_scc_common (void) {
533 init_scc (ir_graph *irg) {
535 irg_walk_graph (irg, init_node, NULL, NULL);
537 irg_walk (irg, link_to_reg_end, NULL, NULL);
541 #ifdef INTERPROCEDURAL_VIEW
545 cg_walk (init_node, NULL, NULL);
547 #if EXPERIMENTAL_LOOP_TREE
548 cg_walk (link_to_reg_end, NULL, NULL);
553 /* Condition for breaking the recursion. */
554 static int is_outermost_Start(ir_node *n) {
555 /* Test whether this is the outermost Start node. If so
556 recursion must end. */
557 if ((get_irn_op(n) == op_Block) &&
558 (get_Block_n_cfgpreds(n) == 1) &&
559 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Start) &&
560 (get_nodes_block(skip_Proj(get_Block_cfgpred(n, 0))) == n)) {
564 /* @@@ Bad condition:
565 not possible in interprocedural view as outermost_graph is
566 not necessarily the only with a dead-end start block.
567 Besides current_ir_graph is not set properly. */
568 if ((get_irn_op(n) == op_Block) &&
569 (n == get_irg_start_block(current_ir_graph))) {
570 if ((!get_interprocedural_view()) ||
571 (current_ir_graph == outermost_ir_graph))
578 /* When to walk from nodes to blocks. Only for Control flow operations? */
580 get_start_index(ir_node *n) {
581 #undef BLOCK_BEFORE_NODE
582 #define BLOCK_BEFORE_NODE 1
584 #if BLOCK_BEFORE_NODE
586 /* This version assures, that all nodes are ordered absolutely. This allows
587 to undef all nodes in the heap analysis if the block is false, which means
589 I.e., with this code, the order on the loop tree is correct. But a (single)
590 test showed the loop tree is deeper. */
591 if (get_irn_op(n) == op_Phi ||
592 get_irn_op(n) == op_Block ||
593 (get_irn_op(n) == op_Filter && get_interprocedural_view()) ||
594 (get_irg_pinned(get_irn_irg(n)) == op_pin_state_floats &&
595 get_irn_pinned(n) == op_pin_state_floats))
596 // Here we could test for backedge at -1 which is illegal
603 /* This version causes deeper loop trees (at least we verified this
605 But it guarantees that Blocks are analysed before nodes contained in the
606 block. If so, we can set the value to undef if the block is not \
608 if (is_cfop(n) || is_fragile_op(n) || get_irn_op(n) == op_Start)
616 /* Test for legal loop header: Block, Phi, ... */
617 static INLINE int is_possible_loop_head(ir_node *n) {
618 ir_op *op = get_irn_op(n);
619 return ((op == op_Block) ||
621 ((op == op_Filter) && get_interprocedural_view()));
624 /* Returns non-zero if n is a loop header, i.e., it is a Block, Phi
625 or Filter node and has predecessors within the loop and out
627 @arg root: only needed for assertion. */
629 is_head (ir_node *n, ir_node *root)
632 int some_outof_loop = 0, some_in_loop = 0;
634 /* Test for legal loop header: Block, Phi, ... */
635 if (!is_possible_loop_head(n))
638 if (!is_outermost_Start(n)) {
639 arity = get_irn_arity(n);
640 for (i = get_start_index(n); i < arity; i++) {
641 ir_node *pred = get_irn_n(n, i);
643 if (is_backedge(n, i)) continue;
644 if (!irn_is_in_stack(pred)) {
647 if(get_irn_uplink(pred) < get_irn_uplink(root)) {
648 assert(get_irn_uplink(pred) >= get_irn_uplink(root));
654 return some_outof_loop && some_in_loop;
658 * Returns non-zero if n is possible loop head of an endless loop.
659 * I.e., it is a Block, Phi or Filter node and has only predecessors
661 * @param root: only needed for assertion.
664 is_endless_head (ir_node *n, ir_node *root)
667 int some_outof_loop = 0, some_in_loop = 0;
669 /* Test for legal loop header: Block, Phi, ... */
670 if (!is_possible_loop_head(n))
673 if (!is_outermost_Start(n)) {
674 arity = get_irn_arity(n);
675 for (i = get_start_index(n); i < arity; i++) {
676 ir_node *pred = get_irn_n(n, i);
678 if (is_backedge(n, i)) { continue; }
679 if (!irn_is_in_stack(pred)) {
680 some_outof_loop = 1; //printf(" some out of loop ");
682 if(get_irn_uplink(pred) < get_irn_uplink(root)) {
683 assert(get_irn_uplink(pred) >= get_irn_uplink(root));
689 return !some_outof_loop && some_in_loop;
692 /* Returns index of the predecessor with the smallest dfn number
693 greater-equal than limit. */
695 smallest_dfn_pred (ir_node *n, int limit)
697 int i, index = -2, min = -1;
699 if (!is_outermost_Start(n)) {
700 int arity = get_irn_arity(n);
701 for (i = get_start_index(n); i < arity; i++) {
702 ir_node *pred = get_irn_n(n, i);
704 if (is_backedge(n, i) || !irn_is_in_stack(pred)) continue;
705 if (get_irn_dfn(pred) >= limit && (min == -1 || get_irn_dfn(pred) < min)) {
707 min = get_irn_dfn(pred);
714 /* Returns index of the predecessor with the largest dfn number. */
716 largest_dfn_pred (ir_node *n)
718 int i, index = -2, max = -1;
720 if (!is_outermost_Start(n)) {
721 int arity = get_irn_arity(n);
722 for (i = get_start_index(n); i < arity; i++) {
723 ir_node *pred = get_irn_n(n, i);
724 if (is_backedge (n, i) || !irn_is_in_stack(pred)) continue;
725 if (get_irn_dfn(pred) > max) {
727 max = get_irn_dfn(pred);
734 /** Searches the stack for possible loop heads. Tests these for backedges.
735 If it finds a head with an unmarked backedge it marks this edge and
736 returns the tail of the loop.
737 If it finds no backedge returns NULL.
738 ("disable_backedge" in fiasco)
740 * @param n A node where uplink == dfn.
744 find_tail (ir_node *n) {
746 int i, res_index = -2;
749 if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
751 m = stack[tos-1]; /* tos = top of stack */
752 if (is_head (m, n)) {
753 res_index = smallest_dfn_pred(m, 0);
754 if ((res_index == -2) && /* no smallest dfn pred found. */
758 if (m == n) return NULL; // Is this to catch Phi - self loops?
759 for (i = tos-2; i >= 0; --i) {
762 if (is_head (m, n)) {
763 res_index = smallest_dfn_pred (m, get_irn_dfn(m) + 1);
764 if (res_index == -2) /* no smallest dfn pred found. */
765 res_index = largest_dfn_pred (m);
767 if ((m == n) && (res_index == -2)) { /* dont walk past loop head. */
773 /* We should not walk past our selves on the stack: The upcoming nodes
774 are not in this loop. We assume a loop not reachable from Start. */
783 /* A dead loop not reachable from Start. */
784 for (i = tos-2; i >= 0; --i) {
786 if (is_endless_head (m, n)) {
787 res_index = smallest_dfn_pred (m, get_irn_dfn(m) + 1);
788 if (res_index == -2) /* no smallest dfn pred found. */
789 res_index = largest_dfn_pred (m);
792 if (m == n) { break; } /* It's not an unreachable loop, either. */
794 //assert(0 && "no head found on stack");
798 if (res_index <= -2) {
799 /* It's a completely bad loop: without Phi/Block nodes that can
800 be a head. I.e., the code is "dying". We break the loop by
801 setting Bad nodes. */
802 int arity = get_irn_arity(n);
803 for (i = -1; i < arity; ++i) {
804 set_irn_n(n, i, get_irg_bad(get_irn_irg(n)));
808 assert (res_index > -2);
810 set_backedge (m, res_index);
811 return is_outermost_Start(n) ? NULL : get_irn_n(m, res_index);
815 #if EXPERIMENTAL_LOOP_TREE
817 /* ----------------------------------------------------------------
818 AS: This is experimental code to build loop trees suitable for
819 the heap analysis. Does not work correctly right now... :-(
822 Search in stack for the corresponding first Call-End-ProjX that
823 corresponds to one of the control flow predecessors of the given
824 block, that is the possible callers.
825 returns: the control predecessor to chose\
826 or -1 if no corresponding Call-End-Node could be found
828 - -------------------------------------------------------------- */
830 int search_endproj_in_stack(ir_node *start_block)
833 assert(is_Block(start_block));
834 for(i = tos - 1; i >= 0; --i)
836 if(get_irn_op(stack[i]) == op_Proj && get_irn_mode(stack[i]) == mode_X &&
837 get_irn_op(get_irn_n(stack[i], 0)) == op_EndReg)
839 printf("FOUND PROJ!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
840 ir_node *end_projx = stack[i];
842 int arity = get_irn_arity(start_block);
843 for(j = 0; j < arity; j++)
845 ir_node *begin_projx = get_Block_cfgpred(get_irg_start_block(get_irn_irg(end_projx)),
846 get_Proj_proj(end_projx));
847 if(get_irn_n(start_block, j) == begin_projx)
849 printf("FOUND IT!!!!!!!!!!!!!!!!!!\n");
859 static pmap *projx_link = NULL;
861 void link_to_reg_end (ir_node *n, void *env) {
862 if(get_irn_op(n) == op_Proj &&
863 get_irn_mode(n) == mode_X &&
864 get_irn_op(get_irn_n(n, 0)) == op_EndReg) {
865 /* Reg End Projx -> Find the CallBegin Projx and hash it */
866 ir_node *end_projx = n;
867 ir_node *begin_projx = get_Block_cfgpred(get_irg_start_block(get_irn_irg(end_projx)),
868 get_Proj_proj(end_projx));
869 set_projx_link(begin_projx, end_projx);
873 void set_projx_link(ir_node *cb_projx, ir_node *end_projx)
875 if(projx_link == NULL)
876 projx_link = pmap_create();
877 pmap_insert(projx_link, (void *)cb_projx, (void *)end_projx);
880 ir_node *get_projx_link(ir_node *cb_projx)
882 return((ir_node *) pmap_get(projx_link, (void *)cb_projx));
888 is_outermost_loop(ir_loop *l) {
889 return l == get_loop_outer_loop(l);
893 /*-----------------------------------------------------------*
894 * The core algorithm. *
895 *-----------------------------------------------------------*/
897 static void scc (ir_node *n) {
899 if (irn_visited(n)) return;
902 /* Initialize the node */
903 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
904 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
905 set_irn_loop(n, NULL);
909 /* AS: get_start_index might return -1 for Control Flow Nodes, and thus a negative
910 array index would be passed to is_backedge(). But CFG Nodes dont't have a backedge array,
911 so is_backedge does not access array[-1] but correctly returns false! */
913 if (!is_outermost_Start(n)) {
914 int arity = get_irn_arity(n);
916 for (i = get_start_index(n); i < arity; i++) {
918 if (is_backedge(n, i)) continue;
919 m = get_irn_n(n, i); /* get_irn_ip_pred(n, i); */
920 /* if ((!m) || (get_irn_op(m) == op_Unknown)) continue; */
922 if (irn_is_in_stack(m)) {
923 /* Uplink of m is smaller if n->m is a backedge.
924 Propagate the uplink to mark the loop. */
925 if (get_irn_uplink(m) < get_irn_uplink(n))
926 set_irn_uplink(n, get_irn_uplink(m));
931 if (get_irn_dfn(n) == get_irn_uplink(n)) {
932 /* This condition holds for
933 1) the node with the incoming backedge.
934 That is: We found a loop!
935 2) Straight line code, because no uplink has been propagated, so the
936 uplink still is the same as the dfn.
938 But n might not be a proper loop head for the analysis. Proper loop
939 heads are Block and Phi nodes. find_tail searches the stack for
940 Block's and Phi's and takes those nodes as loop heads for the current
941 loop instead and marks the incoming edge as backedge. */
943 ir_node *tail = find_tail(n);
945 /* We have a loop, that is no straight line code,
946 because we found a loop head!
947 Next actions: Open a new loop on the loop tree and
948 try to find inner loops */
950 #if NO_LOOPS_WITHOUT_HEAD
951 /* This is an adaption of the algorithm from fiasco / optscc to
952 * avoid loops without Block or Phi as first node. This should
953 * severely reduce the number of evaluations of nodes to detect
954 * a fixpoint in the heap analysis.
955 * Further it avoids loops without firm nodes that cause errors
956 * in the heap analyses.
957 * But attention: don't do it for the outermost loop: This loop
958 * is not iterated. A first block can be a loop head in case of
959 * an endless recursion. */
963 if ((get_loop_n_elements(current_loop) > 0) || (is_outermost_loop(current_loop))) {
971 ir_loop *l = new_loop();
974 /* Remove the loop from the stack ... */
975 pop_scc_unmark_visit (n);
977 /* The current backedge has been marked, that is temporarily eliminated,
978 by find tail. Start the scc algorithm
979 anew on the subgraph that is left (the current loop without the backedge)
980 in order to find more inner loops. */
983 assert (irn_visited(n));
984 #if NO_LOOPS_WITHOUT_HEAD
991 /* No loop head was found, that is we have straightline code.
992 Pop all nodes from the stack to the current loop. */
998 static void my_scc (ir_node *n) {
1000 if (irn_visited(n)) return;
1001 mark_irn_visited(n);
1003 /* Initialize the node */
1004 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
1005 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
1006 set_irn_loop(n, NULL);
1010 /* AS: get_start_index might return -1 for Control Flow Nodes, and thus a negative
1011 array index would be passed to is_backedge(). But CFG Nodes dont't have a backedge array,
1012 so is_backedge does not access array[-1] but correctly returns false! */
1014 if (!is_outermost_Start(n)) {
1015 int arity = get_irn_arity(n);
1017 for (i = get_start_index(n); i < arity; i++) {
1019 if (is_backedge(n, i)) continue;
1020 m = get_irn_n(n, i); /* get_irn_ip_pred(n, i); */
1021 /* if ((!m) || (get_irn_op(m) == op_Unknown)) continue; */
1023 if (irn_is_in_stack(m)) {
1024 /* Uplink of m is smaller if n->m is a backedge.
1025 Propagate the uplink to mark the loop. */
1026 if (get_irn_uplink(m) < get_irn_uplink(n))
1027 set_irn_uplink(n, get_irn_uplink(m));
1032 if (get_irn_dfn(n) == get_irn_uplink(n)) {
1033 /* This condition holds for
1034 1) the node with the incoming backedge.
1035 That is: We found a loop!
1036 2) Straight line code, because no uplink has been propagated, so the
1037 uplink still is the same as the dfn.
1039 But n might not be a proper loop head for the analysis. Proper loop
1040 heads are Block and Phi nodes. find_tail searches the stack for
1041 Block's and Phi's and takes those nodes as loop heads for the current
1042 loop instead and marks the incoming edge as backedge. */
1044 ir_node *tail = find_tail(n);
1046 /* We have a loop, that is no straight line code,
1047 because we found a loop head!
1048 Next actions: Open a new loop on the loop tree and
1049 try to find inner loops */
1051 #if NO_LOOPS_WITHOUT_HEAD
1052 /* This is an adaption of the algorithm from fiasco / optscc to
1053 * avoid loops without Block or Phi as first node. This should
1054 * severely reduce the number of evaluations of nodes to detect
1055 * a fixpoint in the heap analysis.
1056 * Further it avoids loops without firm nodes that cause errors
1057 * in the heap analyses. */
1061 if ((get_loop_n_elements(current_loop) > 0) || (is_outermost_loop(current_loop))) {
1069 ir_loop *l = new_loop();
1072 /* Remove the loop from the stack ... */
1073 pop_scc_unmark_visit (n);
1075 /* The current backedge has been marked, that is temporarily eliminated,
1076 by find tail. Start the scc algorithm
1077 anew on the subgraph that is left (the current loop without the backedge)
1078 in order to find more inner loops. */
1081 assert (irn_visited(n));
1082 #if NO_LOOPS_WITHOUT_HEAD
1089 /* No loop head was found, that is we have straightline code.
1090 Pop all nodes from the stack to the current loop. */
1096 /* Constructs backedge information for irg. In interprocedural view constructs
1097 backedges for all methods called by irg, too. */
1098 int construct_backedges(ir_graph *irg) {
1099 ir_graph *rem = current_ir_graph;
1102 assert(!get_interprocedural_view() &&
1103 "not implemented, use construct_ip_backedges");
1106 current_ir_graph = irg;
1107 outermost_ir_graph = irg;
1109 init_scc(current_ir_graph);
1111 current_loop = NULL;
1112 new_loop(); /* sets current_loop */
1113 head_rem = current_loop; /* Just for assertion */
1115 inc_irg_visited(current_ir_graph);
1117 scc(get_irg_end(current_ir_graph));
1119 assert(head_rem == current_loop);
1120 set_irg_loop(current_ir_graph, current_loop);
1121 set_irg_loopinfo_state(current_ir_graph, loopinfo_consistent);
1122 assert(get_irg_loop(current_ir_graph)->kind == k_ir_loop);
1124 irg->loops = current_loop;
1128 count_loop (the_loop, &count, &depth);
1132 current_ir_graph = rem;
1134 return max_loop_depth;
1138 #ifdef INTERPROCEDURAL_VIEW
1139 int construct_ip_backedges (void) {
1140 ir_graph *rem = current_ir_graph;
1141 int rem_ipv = get_interprocedural_view();
1145 assert(get_irp_ip_view_state() == ip_view_valid);
1147 outermost_ir_graph = get_irp_main_irg();
1151 current_loop = NULL;
1152 new_loop(); /* sets current_loop */
1153 set_interprocedural_view(1);
1155 inc_max_irg_visited();
1156 for (i = 0; i < get_irp_n_irgs(); i++)
1157 set_irg_visited(get_irp_irg(i), get_max_irg_visited());
1159 /** We have to start the walk at the same nodes as cg_walk. **/
1160 /* Walk starting at unreachable procedures. Only these
1161 * have End blocks visible in interprocedural view. */
1162 for (i = 0; i < get_irp_n_irgs(); i++) {
1164 current_ir_graph = get_irp_irg(i);
1166 sb = get_irg_start_block(current_ir_graph);
1168 if ((get_Block_n_cfgpreds(sb) > 1) ||
1169 (get_nodes_block(get_Block_cfgpred(sb, 0)) != sb)) continue;
1171 scc(get_irg_end(current_ir_graph));
1174 /* Check whether we walked all procedures: there could be procedures
1175 with cyclic calls but no call from the outside. */
1176 for (i = 0; i < get_irp_n_irgs(); i++) {
1178 current_ir_graph = get_irp_irg(i);
1180 /* Test start block: if inner procedure end and end block are not
1181 * visible and therefore not marked. */
1182 sb = get_irg_start_block(current_ir_graph);
1183 if (get_irn_visited(sb) < get_irg_visited(current_ir_graph)) scc(sb);
1186 /* Walk all endless loops in inner procedures.
1187 * We recognize an inner procedure if the End node is not visited. */
1188 for (i = 0; i < get_irp_n_irgs(); i++) {
1190 current_ir_graph = get_irp_irg(i);
1192 e = get_irg_end(current_ir_graph);
1193 if (get_irn_visited(e) < get_irg_visited(current_ir_graph)) {
1195 /* Don't visit the End node. */
1196 for (j = 0; j < get_End_n_keepalives(e); j++) scc(get_End_keepalive(e, j));
1200 set_irg_loop(outermost_ir_graph, current_loop);
1201 set_irg_loopinfo_state(current_ir_graph, loopinfo_ip_consistent);
1202 assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
1204 current_ir_graph = rem;
1205 set_interprocedural_view(rem_ipv);
1206 return max_loop_depth;
1209 void my_construct_ip_backedges (void) {
1210 ir_graph *rem = current_ir_graph;
1211 int rem_ipv = get_interprocedural_view();
1214 assert(get_irp_ip_view_state() == ip_view_valid);
1216 outermost_ir_graph = get_irp_main_irg();
1220 current_loop = NULL;
1221 new_loop(); /* sets current_loop */
1222 set_interprocedural_view(1);
1224 inc_max_irg_visited();
1225 for (i = 0; i < get_irp_n_irgs(); i++)
1226 set_irg_visited(get_irp_irg(i), get_max_irg_visited());
1228 /** We have to start the walk at the same nodes as cg_walk. **/
1229 /* Walk starting at unreachable procedures. Only these
1230 * have End blocks visible in interprocedural view. */
1231 for (i = 0; i < get_irp_n_irgs(); i++) {
1233 current_ir_graph = get_irp_irg(i);
1235 sb = get_irg_start_block(current_ir_graph);
1237 if ((get_Block_n_cfgpreds(sb) > 1) ||
1238 (get_nodes_block(get_Block_cfgpred(sb, 0)) != sb)) continue;
1240 my_scc(get_irg_end(current_ir_graph));
1243 /* Check whether we walked all procedures: there could be procedures
1244 with cyclic calls but no call from the outside. */
1245 for (i = 0; i < get_irp_n_irgs(); i++) {
1247 current_ir_graph = get_irp_irg(i);
1249 /* Test start block: if inner procedure end and end block are not
1250 * visible and therefore not marked. */
1251 sb = get_irg_start_block(current_ir_graph);
1252 if (get_irn_visited(sb) < get_irg_visited(current_ir_graph)) scc(sb);
1255 /* Walk all endless loops in inner procedures.
1256 * We recognize an inner procedure if the End node is not visited. */
1257 for (i = 0; i < get_irp_n_irgs(); i++) {
1259 current_ir_graph = get_irp_irg(i);
1261 e = get_irg_end(current_ir_graph);
1262 if (get_irn_visited(e) < get_irg_visited(current_ir_graph)) {
1264 /* Don't visit the End node. */
1265 for (j = 0; j < get_End_n_keepalives(e); j++) scc(get_End_keepalive(e, j));
1269 set_irg_loop(outermost_ir_graph, current_loop);
1270 set_irg_loopinfo_state(current_ir_graph, loopinfo_ip_consistent);
1271 assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
1273 current_ir_graph = rem;
1274 set_interprocedural_view(rem_ipv);
1278 static void reset_backedges(ir_node *n) {
1279 if (is_possible_loop_head(n)) {
1280 #ifdef INTERPROCEDURAL_VIEW
1281 int rem = get_interprocedural_view();
1283 set_interprocedural_view(1);
1285 set_interprocedural_view(1);
1287 set_interprocedural_view(rem);
1296 static void loop_reset_backedges(ir_loop *l) {
1298 reset_backedges(get_loop_node(l, 0));
1299 for (i = 0; i < get_loop_n_nodes(l); ++i)
1300 set_irn_loop(get_loop_node(l, i), NULL);
1301 for (i = 0; i < get_loop_n_sons(l); ++i) {
1302 loop_reset_backedges(get_loop_son(l, i));
1307 static void loop_reset_node(ir_node *n, void *env) {
1309 set_irn_loop(n, NULL);
1314 /** Removes all loop information.
1315 Resets all backedges */
1316 void free_loop_information(ir_graph *irg) {
1317 /* We can not use this recursion, as the loop might contain
1318 illegal nodes by now. Why else would we throw away the
1320 if (get_irg_loop(irg)) loop_reset_backedges(get_irg_loop(irg));
1322 irg_walk_graph(irg, loop_reset_node, NULL, NULL);
1323 set_irg_loop(irg, NULL);
1324 set_irg_loopinfo_state(current_ir_graph, loopinfo_none);
1325 /* We cannot free the loop nodes, they are on the obstack. */
1329 void free_all_loop_information (void) {
1331 #ifdef INTERPROCEDURAL_VIEW
1332 int rem = get_interprocedural_view();
1333 set_interprocedural_view(1); /* To visit all filter nodes */
1335 for (i = 0; i < get_irp_n_irgs(); i++) {
1336 free_loop_information(get_irp_irg(i));
1338 #ifdef INTERPROCEDURAL_VIEW
1339 set_interprocedural_view(rem);
1347 /* Debug stuff *************************************************/
1349 static int test_loop_node(ir_loop *l) {
1350 int i, has_node = 0, found_problem = 0;
1353 assert(l && l->kind == k_ir_loop);
1355 if (get_loop_n_elements(l) == 0) {
1357 dump_loop(l, "-ha");
1360 le = get_loop_element(l, 0);
1361 if (*(le.kind) != k_ir_node) {
1362 assert(le.kind && *(le.kind) == k_ir_loop);
1365 dump_loop(l, "-ha");
1368 if ((*(le.kind) == k_ir_node) && !is_possible_loop_head(le.node)) {
1370 dump_loop(l, "-ha");
1373 if ((get_loop_depth(l) != 0) &&
1374 (*(le.kind) == k_ir_node) && !has_backedges(le.node)) {
1376 dump_loop(l, "-ha");
1381 for (i = 0; i < get_loop_n_elements(l); ++i) {
1382 le = get_loop_element(l, i);
1383 if (*(le.kind) == k_ir_node)
1386 if (test_loop_node(le.son)) found_problem = 1;
1389 if (has_node == 0) {
1391 dump_loop(l, "-ha");
1394 return found_problem;
1397 /** Prints all loop nodes that
1398 * - do not have any firm nodes, only loop sons
1399 * - the header is not a Phi, Block or Filter.
1401 void find_strange_loop_nodes(ir_loop *l) {
1402 int found_problem = 0;
1403 found_problem = test_loop_node(l);
1404 printf("Finished Test\n\n");
1405 if (found_problem) exit(0);
1409 /* ------------------------------------------------------------------- */
1410 /* Simple analyses based on the loop information */
1411 /* ------------------------------------------------------------------- */
1413 int is_loop_variant(ir_loop *l, ir_loop *b) {
1416 if (l == b) return 1;
1418 n_elems = get_loop_n_elements(l);
1419 for (i = 0; i < n_elems; ++i) {
1420 loop_element e = get_loop_element(l, i);
1421 if (is_ir_loop(e.kind))
1422 if (is_loop_variant(e.son, b))
1429 /* Test whether a value is loop invariant.
1431 * @param n The node to be tested.
1432 * @param block A block node. We pass the block, not the loop as we must
1433 * start off with a block loop to find all proper uses.
1435 * Returns non-zero, if the node n is not changed in the loop block
1436 * belongs to or in inner loops of this blocks loop. */
1437 int is_loop_invariant(ir_node *n, ir_node *block) {
1438 ir_loop *l = get_irn_loop(block);
1439 ir_node *b = (is_Block(n)) ? n : get_nodes_block(n);
1440 return !is_loop_variant(l, get_irn_loop(b));