3 * File name: ir/ana/irscc.c
4 * Purpose: Compute the strongly connected regions and build
5 * backedge/loop datastructures.
6 * A variation on the Tarjan algorithm. See also [Trapp:99],
8 * Author: Goetz Lindenmaier
12 * Copyright: (c) 2002-2003 Universität Karlsruhe
13 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
24 #include "irgraph_t.h"
31 ir_graph *outermost_ir_graph; /* The outermost graph the scc is computed
33 static ir_loop *current_loop; /* Current loop construction is working
35 static int loop_node_cnt = 0; /* Counts the number of allocated loop nodes.
36 Each loop node gets a unique number.
37 What for? ev. remove. @@@ */
38 static int current_dfn = 1; /* Counter to generate depth first numbering
41 /**********************************************************************/
42 /* Node attributes **/
43 /**********************************************************************/
45 /* A map to get from irnodes to loop nodes. */
46 static pmap *node_loop_map = NULL;
48 /**********************************************************************/
49 /* Node attributes needed for the construction. **/
50 /**********************************************************************/
52 typedef struct scc_info {
53 bool in_stack; /* Marks whether node is on the stack. */
54 int dfn; /* Depth first search number. */
55 int uplink; /* dfn number of ancestor. */
56 // ir_loop *loop; /* Refers to the containing loop. */
58 struct section *section;
64 static INLINE scc_info* new_scc_info(void) {
65 scc_info *info = obstack_alloc (outermost_ir_graph->obst, sizeof (scc_info));
66 memset (info, 0, sizeof (scc_info));
71 mark_irn_in_stack (ir_node *n) {
72 assert(get_irn_link(n));
74 //((scc_info *)get_irn_link(n))->in_stack = true;
75 ((scc_info *)n->link)->in_stack = true;
79 mark_irn_not_in_stack (ir_node *n) {
80 assert(get_irn_link(n));
82 //((scc_info *)get_irn_link(n))->in_stack = false;
83 ((scc_info *)n->link)->in_stack = false;
87 irn_is_in_stack (ir_node *n) {
88 assert(get_irn_link(n));
90 //return ((scc_info *)get_irn_link(n))->in_stack;
91 return ((scc_info *)n->link)->in_stack;
95 set_irn_uplink (ir_node *n, int uplink) {
96 assert(get_irn_link(n));
98 //((scc_info *)get_irn_link(n))->uplink = uplink;
99 ((scc_info *)n->link)->uplink = uplink;
103 get_irn_uplink (ir_node *n) {
104 assert(get_irn_link(n));
106 //return ((scc_info *)get_irn_link(n))->uplink;
107 return ((scc_info *)n->link)->uplink;
111 set_irn_dfn (ir_node *n, int dfn) {
112 assert(get_irn_link(n));
114 //((scc_info *)get_irn_link(n))->dfn = dfn;
115 ((scc_info *)n->link)->dfn = dfn;
119 get_irn_dfn (ir_node *n) {
120 assert(get_irn_link(n));
122 //return ((scc_info *)get_irn_link(n))->dfn;
123 return ((scc_info *)n->link)->dfn;
127 /* Replaced node loop map by real field as hash access dominates runtime
128 * of the algorithm. ! */
129 /* Uses temporary information to set the loop */
131 set_irn_loop (ir_node *n, ir_loop* loop) {
132 assert(node_loop_map && "not initialized!");
133 pmap_insert(node_loop_map, (void *)n, (void *)loop);
136 /* Uses temporary information to get the loop */
138 get_irn_loop (ir_node *n) {
140 if (!node_loop_map) return NULL;
142 if (pmap_contains(node_loop_map, (void *)n))
143 res = (ir_loop *) pmap_get(node_loop_map, (void *)n);
149 set_irn_loop (ir_node *n, ir_loop* loop) {
153 /* Uses temporary information to get the loop */
155 get_irn_loop (ir_node *n) {
162 static ir_loop *find_nodes_loop (ir_node *n, ir_loop *l) {
166 /* Test whether n is contained in this loop. */
167 for (i = 0; i < get_loop_n_nodes(l); i++)
168 if (n == get_loop_node(l, i)) return l;
170 /* Is this a leave in the loop tree? If so loop not found. */
171 if (get_loop_n_sons(l) == 0) return NULL;
173 /* Else descend in the loop tree. */
174 for (i = 0; i < get_loop_n_sons(l); i++) {
175 res = find_nodes_loop(n, get_loop_son(l, i));
181 /* @@@ temporary implementation, costly!!! */
182 ir_loop * get_irn_loop(ir_node *n) {
183 ir_loop *l = get_irg_loop(current_ir_graph);
184 l = find_nodes_loop(n, l);
189 /**********************************************************************/
191 /**********************************************************************/
193 static ir_node **stack = NULL;
194 static int tos = 0; /* top of stack */
196 static INLINE void init_stack(void) {
198 ARR_RESIZE (ir_node *, stack, 1000);
200 stack = NEW_ARR_F (ir_node *, 1000);
206 static INLINE void free_stack(void) {
218 if (tos == ARR_LEN (stack)) {
219 int nlen = ARR_LEN (stack) * 2;
220 ARR_RESIZE (ir_node *, stack, nlen);
223 mark_irn_in_stack(n);
226 static INLINE ir_node *
229 ir_node *n = stack[--tos];
230 mark_irn_not_in_stack(n);
234 /* The nodes up to n belong to the current loop.
235 Removes them from the stack and adds them to the current loop. */
237 pop_scc_to_loop (ir_node *n)
247 set_irn_dfn(m, loop_node_cnt);
248 add_loop_node(current_loop, m);
249 set_irn_loop(m, current_loop);
251 /* if (m==n) break;*/
255 printf("Mehr als eine Iteration!!!!!!!!!!!!!!!!!!!!!!!!!!!!11111\n");
258 /* GL ??? my last son is my grandson??? Removes loops with no
259 ir_nodes in them. Such loops have only another loop as son. (Why
260 can't they have two loops as sons? Does it never get that far? ) */
261 void close_loop (ir_loop *l)
263 int last = get_loop_n_elements(l) - 1;
264 loop_element lelement = get_loop_element(l, last);
265 ir_loop *last_son = lelement.son;
267 if (get_kind(last_son) == k_ir_loop &&
268 get_loop_n_elements(last_son) == 1)
272 lelement = get_loop_element(last_son, 0);
274 if(get_kind(gson) == k_ir_loop)
276 loop_element new_last_son;
278 gson -> outer_loop = l;
279 new_last_son.son = gson;
280 l -> children[last] = new_last_son;
287 /* Removes and unmarks all nodes up to n from the stack.
288 The nodes must be visited once more to assign them to a scc. */
290 pop_scc_unmark_visit (ir_node *n)
296 set_irn_visited(m, 0);
300 /**********************************************************************/
301 /* The loop datastructure. **/
302 /**********************************************************************/
304 /* Allocates a new loop as son of current_loop. Sets current_loop
305 to the new loop and returns the father. */
306 static ir_loop *new_loop (void) {
307 ir_loop *father, *son;
309 father = current_loop;
311 son = (ir_loop *) obstack_alloc (outermost_ir_graph->obst, sizeof (ir_loop));
312 memset (son, 0, sizeof (ir_loop));
313 son->kind = k_ir_loop;
314 son->children = NEW_ARR_F (loop_element, 0);
318 son->outer_loop = father;
319 add_loop_son(father, son);
320 son->depth = father->depth+1;
321 } else { /* The root loop */
322 son->outer_loop = son;
327 son->loop_nr = get_irp_new_node_nr();
336 /* Finishes the datastructures, copies the arrays to the obstack
338 A. Schoesser: Caution: loop -> sons is gone. */
339 static void mature_loop (ir_loop *loop) {
342 new_sons = NEW_ARR_D (ir_loop *, current_ir_graph->obst, ARR_LEN(loop->sons));
343 memcpy (new_sons, loop->sons, sizeof (ir_loop *) * ARR_LEN(loop->sons));
344 DEL_ARR_F(loop->sons);
345 loop->sons = new_sons;
349 /* Returns outer loop, itself if outermost. */
350 ir_loop *get_loop_outer_loop (ir_loop *loop) {
351 assert(loop && loop->kind == k_ir_loop);
352 return loop->outer_loop;
355 /* Returns nesting depth of this loop */
356 int get_loop_depth (ir_loop *loop) {
357 assert(loop); assert(loop->kind == k_ir_loop);
361 /* Returns the number of inner loops */
362 int get_loop_n_sons (ir_loop *loop) {
363 assert(loop && loop->kind == k_ir_loop);
364 return(loop -> n_sons);
367 /* Returns the pos`th loop_node-child *
368 * TODO: This method isn`t very efficient ! *
369 * Returns NULL if there isnt`t a pos`th loop_node */
370 ir_loop *get_loop_son (ir_loop *loop, int pos) {
371 int child_nr = 0, loop_nr = -1;
373 assert(loop && loop->kind == k_ir_loop);
374 while(child_nr < ARR_LEN(loop->children))
376 if(*(loop -> children[child_nr].kind) == k_ir_loop)
379 return(loop -> children[child_nr].son);
385 /* Use EXCLUSIVELY this function to add sons, otherwise the loop->n_sons
389 add_loop_son(ir_loop *loop, ir_loop *son) {
392 assert(loop && loop->kind == k_ir_loop);
393 assert(get_kind(son) == k_ir_loop);
394 ARR_APP1 (loop_element, loop->children, lson);
398 /* Returns the number of nodes in the loop */
399 int get_loop_n_nodes (ir_loop *loop) {
400 assert(loop); assert(loop->kind == k_ir_loop);
401 return loop -> n_nodes;
402 /* return ARR_LEN(loop->nodes); */
405 /* Returns the pos`th ir_node-child *
406 * TODO: This method isn`t very efficient ! *
407 * Returns NULL if there isnt`t a pos`th ir_node */
408 ir_node *get_loop_node (ir_loop *loop, int pos) {
409 int child_nr, node_nr = -1;
411 assert(loop && loop->kind == k_ir_loop);
412 assert(pos < get_loop_n_nodes(loop));
414 for (child_nr = 0; child_nr < ARR_LEN(loop->children); child_nr++) {
415 if(*(loop -> children[child_nr].kind) == k_ir_node)
418 return(loop -> children[child_nr].node);
420 assert(0 && "no child at pos found");
424 /* Use EXCLUSIVELY this function to add nodes, otherwise the loop->n_nodes
428 add_loop_node(ir_loop *loop, ir_node *n) {
431 assert(loop && loop->kind == k_ir_loop);
432 assert(get_kind(n) == k_ir_node);
433 ARR_APP1 (loop_element, loop->children, ln);
437 /** Returns the number of elements contained in loop. */
438 int get_loop_n_elements (ir_loop *loop) {
439 assert(loop && loop->kind == k_ir_loop);
440 return(ARR_LEN(loop->children));
444 Returns the pos`th loop element.
445 This may be a loop_node or a ir_node. The caller of this function has
446 to check the *(loop_element.kind) field for "k_ir_node" or "k_ir_loop"
447 and then select the apropriate "loop_element.node" or "loop_element.son".
450 loop_element get_loop_element (ir_loop *loop, int pos) {
451 assert(loop && loop->kind == k_ir_loop && pos < ARR_LEN(loop->children));
453 return(loop -> children[pos]);
456 int get_loop_element_pos(ir_loop *loop, void *le) {
458 assert(loop && loop->kind == k_ir_loop);
460 for (i = 0; i < get_loop_n_elements(loop); i++)
461 if (get_loop_element(loop, i).node == le) return i;
465 int get_loop_loop_nr(ir_loop *loop) {
466 assert(loop && loop->kind == k_ir_loop);
468 return loop->loop_nr;
475 /** A field to connect additional information to a loop. Only valid
476 if libfirm_debug is set. */
477 void set_loop_link (ir_loop *loop, void *link) {
478 assert(loop && loop->kind == k_ir_loop);
483 void *get_loop_link (const ir_loop *loop) {
484 assert(loop && loop->kind == k_ir_loop);
492 /* The outermost loop is remarked in the surrounding graph. */
493 void set_irg_loop(ir_graph *irg, ir_loop *loop) {
497 ir_loop *get_irg_loop(ir_graph *irg) {
503 /**********************************************************************/
504 /* Constructing and destructing the loop/backedge information. **/
505 /**********************************************************************/
507 /* Initialization steps. **********************************************/
510 init_node (ir_node *n, void *env) {
511 set_irn_link (n, new_scc_info());
514 /* Also init nodes not visible in intraproc_view. */
515 /* @@@ init_node is called for too many nodes -- this wastes memory!.
516 The mem is not lost as its on the obstack. */
517 if (get_irn_op(n) == op_Filter) {
518 for (i = 0; i < get_Filter_n_cg_preds(n); i++)
519 init_node(get_Filter_cg_pred(n, i), NULL);
521 if (get_irn_op(n) == op_Block) {
522 for (i = 0; i < get_Block_cg_n_cfgpreds(n); i++) {
523 init_node(get_Block_cg_cfgpred(n, i), NULL);
526 /* The following pattern matches only after a call from above pattern. */
527 if ((get_irn_op(n) == op_Proj) /*&& (get_Proj_proj(n) == 0)*/) {
528 /* @@@ init_node is called for every proj -- this wastes memory!.
529 The mem is not lost as its on the obstack. */
530 ir_node *cb = get_Proj_pred(n);
531 if ((get_irn_op(cb) == op_CallBegin) ||
532 (get_irn_op(cb) == op_EndReg) ||
533 (get_irn_op(cb) == op_EndExcept)) {
535 init_node(get_nodes_Block(cb), NULL);
542 init_scc_common (void) {
545 if (!node_loop_map) node_loop_map = pmap_create();
550 init_scc (ir_graph *irg) {
552 irg_walk_graph (irg, init_node, NULL, NULL);
554 irg_walk (irg, link_to_reg_end, NULL, NULL);
561 cg_walk (init_node, NULL, NULL);
564 /* Condition for breaking the recursion. */
565 static bool is_outermost_Start(ir_node *n) {
566 /* Test whether this is the outermost Start node. If so
567 recursion must end. */
568 if ((get_irn_op(n) == op_Block) &&
569 (get_Block_n_cfgpreds(n) == 1) &&
570 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Start) &&
571 (get_nodes_Block(skip_Proj(get_Block_cfgpred(n, 0))) == n)) {
575 /* @@@ Bad condition:
576 not possible in interprocedural view as outermost_graph is
577 not necessarily the only with a dead-end start block.
578 Besides current_ir_graph is not set properly. */
579 if ((get_irn_op(n) == op_Block) &&
580 (n == get_irg_start_block(current_ir_graph))) {
581 if ((!interprocedural_view) ||
582 (current_ir_graph == outermost_ir_graph))
589 /* Don't walk from nodes to blocks except for Control flow operations. */
591 get_start_index(ir_node *n) {
592 if (is_cfop(n) || is_fragile_op(n) || get_irn_op(n) == op_Start)
598 /* Returns current_ir_graph and set it to the irg of predecessor index
600 static INLINE ir_graph *
601 switch_irg (ir_node *n, int index) {
602 ir_graph *old_current = current_ir_graph;
604 if (interprocedural_view) {
605 /* Only Filter and Block nodes can have predecessors in other graphs. */
606 if (get_irn_op(n) == op_Filter)
607 n = get_nodes_Block(n);
608 if (get_irn_op(n) == op_Block) {
609 ir_node *cfop = skip_Proj(get_Block_cfgpred(n, index));
610 if (is_ip_cfop(cfop)) {
611 current_ir_graph = get_irn_irg(cfop);
612 set_irg_visited(current_ir_graph, get_max_irg_visited());
620 /* Walks up the stack passing n and then finding the node
621 where we walked into the irg n is contained in.
622 Here we switch the irg. */
624 find_irg_on_stack (ir_node *n) {
626 ir_graph *old_current = current_ir_graph;
629 if (interprocedural_view) {
630 for (i = tos; i >= 0; i--) {
631 if (stack[i] == n) break;
636 for (; i >= 0; i--) {
638 /*printf(" Visiting %d ", i); DDMN(m);*/
640 current_ir_graph = get_irn_irg(m);
643 if (get_irn_op(m) == op_Filter) {
644 /* Find the corresponding ip_cfop */
645 ir_node *pred = stack[i+1];
647 for (j = 0; j < get_Filter_n_cg_preds(m); j++)
648 if (get_Filter_cg_pred(m, j) == pred) break;
649 if (j >= get_Filter_n_cg_preds(m))
650 /* It is a filter we didn't pass as the predecessors are marked. */
652 assert(get_Filter_cg_pred(m, j) == pred);
664 static void test(ir_node *pred, ir_node *root, ir_node *this) {
666 if (get_irn_uplink(pred) >= get_irn_uplink(root)) return;
668 printf("this: %d ", get_irn_uplink(this)); DDMN(this);
669 printf("pred: %d ", get_irn_uplink(pred)); DDMN(pred);
670 printf("root: %d ", get_irn_uplink(root)); DDMN(root);
672 printf("tos: %d\n", tos);
674 for (i = tos; i >= 0; i--) {
675 ir_node *n = stack[i];
677 printf(" uplink: %d, pos: %d ", get_irn_uplink(n), i); DDMN(n);
682 /* Test for legal loop header: Block, Phi, ... */
683 INLINE static bool is_possible_loop_head(ir_node *n) {
684 ir_op *op = get_irn_op(n);
685 return ((op == op_Block) ||
687 ((op == op_Filter) && interprocedural_view));
690 /* Returns true if n is a loop header, i.e., it is a Block, Phi
691 or Filter node and has predecessors within the loop and out
693 @arg root: only needed for assertion. */
695 is_head (ir_node *n, ir_node *root)
698 int some_outof_loop = 0, some_in_loop = 0;
700 /* Test for legal loop header: Block, Phi, ... */
701 if (!is_possible_loop_head(n))
703 if (!is_outermost_Start(n)) {
704 arity = get_irn_arity(n);
705 for (i = get_start_index(n); i < arity; i++) {
706 ir_node *pred = get_irn_n(n, i);
708 if (is_backedge(n, i)) continue;
709 if (!irn_is_in_stack(pred)) {
712 assert(get_irn_uplink(pred) >= get_irn_uplink(root));
717 return some_outof_loop && some_in_loop;
720 /* Returns index of the predecessor with the smallest dfn number
721 greater-equal than limit. */
723 smallest_dfn_pred (ir_node *n, int limit)
725 int i, index = -2, min = -1;
727 if (!is_outermost_Start(n)) {
728 int arity = get_irn_arity(n);
729 for (i = get_start_index(n); i < arity; i++) {
730 ir_node *pred = get_irn_n(n, i);
732 if (is_backedge(n, i) || !irn_is_in_stack(pred)) continue;
733 if (get_irn_dfn(pred) >= limit && (min == -1 || get_irn_dfn(pred) < min)) {
735 min = get_irn_dfn(pred);
742 /* Returns index of the predecessor with the largest dfn number. */
744 largest_dfn_pred (ir_node *n)
746 int i, index = -2, max = -1;
748 if (!is_outermost_Start(n)) {
749 int arity = get_irn_arity(n);
750 for (i = get_start_index(n); i < arity; i++) {
751 ir_node *pred = get_irn_n(n, i);
752 if (is_backedge (n, i) || !irn_is_in_stack(pred)) continue;
753 if (get_irn_dfn(pred) > max) {
755 max = get_irn_dfn(pred);
762 /* Searches the stack for possible loop heads. Tests these for backedges.
763 If it finds a head with an unmarked backedge it marks this edge and
764 returns the tail of the loop.
765 If it finds no backedge returns NULL.
766 ("disable_backedge" in fiasco) */
769 find_tail (ir_node *n) {
771 int i, res_index = -2;
774 if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
777 m = stack[tos-1]; /* tos = top of stack */
778 if (is_head (m, n)) {
779 res_index = smallest_dfn_pred(m, 0);
780 if ((res_index == -2) && /* no smallest dfn pred found. */
784 if (m == n) return NULL;
785 for (i = tos-2; ; --i) {
787 if (is_head (m, n)) {
788 res_index = smallest_dfn_pred (m, get_irn_dfn(m) + 1);
789 if (res_index == -2) /* no smallest dfn pred found. */
790 res_index = largest_dfn_pred (m);
795 assert (res_index > -2);
797 set_backedge (m, res_index);
798 return is_outermost_Start(n) ? NULL : get_irn_n(m, res_index);
802 /* The core algorithm. *****************************************/
804 static void scc (ir_node *n) {
806 if (irn_visited(n)) return;
809 /* Initialize the node */
810 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
811 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
812 set_irn_loop(n, NULL);
816 /* AS: get_start_index might return -1 for Control Flow Nodes, and thus a negative
817 array index would be passed to is_backedge(). But CFG Nodes dont't have a backedge array,
818 so is_backedge does not access array[-1] but correctly returns false! */
820 if (!is_outermost_Start(n)) {
821 int arity = get_irn_arity(n);
822 for (i = get_start_index(n); i < arity; i++) {
824 if (is_backedge(n, i)) continue;
826 m = get_irn_n(n, i); /* get_irn_ip_pred(n, i); */
827 //if ((!m) || (get_irn_op(m) == op_Unknown)) continue;
829 if (irn_is_in_stack(m)) {
830 /* Uplink of m is smaller if n->m is a backedge.
831 Propagate the uplink to mark the loop. */
832 if (get_irn_uplink(m) < get_irn_uplink(n))
833 set_irn_uplink(n, get_irn_uplink(m));
838 if (get_irn_dfn(n) == get_irn_uplink(n)) {
839 /* This condition holds for the node with the incoming backedge.
840 AS: That is: For the loop head. */
841 ir_node *tail = find_tail(n);
843 /* We found a new inner loop! */
845 /* This is an adaption of the algorithm from fiasco / optscc to
846 * avoid loops without Block or Phi as first node. This should
847 * severely reduce the number of evaluations of nodes to detect
848 * a fixpoint in the heap analyses.
849 * Further it avoids loops without firm nodes that cause errors
850 * in the heap analyses. */
851 #define NO_LOOPS_WITHOUT_HEAD 1
852 #if NO_LOOPS_WITHOUT_HEAD
855 if (get_loop_n_elements(current_loop) > 0) {
863 ir_loop *l = new_loop();
866 /* Remove the loop from the stack ... */
867 pop_scc_unmark_visit (n);
868 /* and recompute it in a better order; and so that it goes into
870 // GL @@@ remove experimental stuff rem = find_irg_on_stack(tail);
873 // GL @@@ remove experimental stuff current_ir_graph = rem;
875 assert (irn_visited(n));
876 #if NO_LOOPS_WITHOUT_HEAD
881 /* AS: No inner loop was found. Pop all nodes from the stack
882 to the current loop. */
888 /* Constructs backedge information for irg. In interprocedural view constructs
889 backedges for all methods called by irg, too. */
890 void construct_backedges(ir_graph *irg) {
891 ir_graph *rem = current_ir_graph;
894 assert(!interprocedural_view &&
895 "not implemented, use construct_ip_backedges");
897 current_ir_graph = irg;
898 outermost_ir_graph = irg;
900 init_scc(current_ir_graph);
903 new_loop(); /* sets current_loop */
904 head_rem = current_loop; /* Just for assertion */
906 if (interprocedural_view) {
907 set_irg_visited(current_ir_graph, inc_max_irg_visited());
910 inc_irg_visited(current_ir_graph);
913 scc(get_irg_end(current_ir_graph));
915 if (interprocedural_view) finish_ip_walk();
917 assert(head_rem == current_loop);
918 set_irg_loop(current_ir_graph, current_loop);
919 set_irg_loopinfo_state(current_ir_graph, loopinfo_consistent);
920 assert(get_irg_loop(current_ir_graph)->kind == k_ir_loop);
922 irg->loops = current_loop;
926 count_loop (the_loop, &count, &depth);
930 current_ir_graph = rem;
935 void construct_ip_backedges (void) {
936 ir_graph *rem = current_ir_graph;
937 int rem_ipv = interprocedural_view;
940 outermost_ir_graph = get_irp_main_irg();
945 new_loop(); /* sets current_loop */
946 interprocedural_view = 1;
948 inc_max_irg_visited();
949 for (i = 0; i < get_irp_n_irgs(); i++)
950 set_irg_visited(get_irp_irg(i), get_max_irg_visited());
952 for (i = 0; i < get_irp_n_irgs(); i++) {
954 current_ir_graph = get_irp_irg(i);
955 /* Find real entry points */
956 sb = get_irg_start_block(current_ir_graph);
957 if ((get_Block_n_cfgpreds(sb) > 1) ||
958 (get_nodes_Block(get_Block_cfgpred(sb, 0)) != sb)) continue;
959 /* Compute scc for this graph */
960 outermost_ir_graph = current_ir_graph;
961 set_irg_visited(outermost_ir_graph, get_max_irg_visited());
962 scc(get_irg_end(current_ir_graph));
963 for (j = 0; j < get_End_n_keepalives(get_irg_end(outermost_ir_graph)); j++)
964 scc(get_End_keepalive(get_irg_end(outermost_ir_graph), j));
967 set_irg_loop(outermost_ir_graph, current_loop);
968 set_irg_loopinfo_state(current_ir_graph, loopinfo_ip_consistent);
969 assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
971 current_ir_graph = rem;
972 interprocedural_view = rem_ipv;
975 void construct_ip_backedges (void) {
976 ir_graph *rem = current_ir_graph;
977 int rem_ipv = interprocedural_view;
980 outermost_ir_graph = get_irp_main_irg();
985 new_loop(); /* sets current_loop */
986 interprocedural_view = 1;
988 inc_max_irg_visited();
989 for (i = 0; i < get_irp_n_irgs(); i++)
990 set_irg_visited(get_irp_irg(i), get_max_irg_visited());
992 /** We have to start the walk at the same nodes as cg_walk. **/
993 /* Walk starting at unreachable procedures. Only these
994 * have End blocks visible in interprocedural view. */
995 for (i = 0; i < get_irp_n_irgs(); i++) {
997 current_ir_graph = get_irp_irg(i);
999 sb = get_irg_start_block(current_ir_graph);
1001 if ((get_Block_n_cfgpreds(sb) > 1) ||
1002 (get_nodes_block(get_Block_cfgpred(sb, 0)) != sb)) continue;
1004 scc(get_irg_end(current_ir_graph));
1007 /* Check whether we walked all procedures: there could be procedures
1008 with cyclic calls but no call from the outside. */
1009 for (i = 0; i < get_irp_n_irgs(); i++) {
1011 current_ir_graph = get_irp_irg(i);
1013 /* Test start block: if inner procedure end and end block are not
1014 * visible and therefore not marked. */
1015 sb = get_irg_start_block(current_ir_graph);
1016 if (get_irn_visited(sb) < get_irg_visited(current_ir_graph)) scc(sb);
1019 /* Walk all endless loops in inner procedures.
1020 * We recognize an inner procedure if the End node is not visited. */
1021 for (i = 0; i < get_irp_n_irgs(); i++) {
1023 current_ir_graph = get_irp_irg(i);
1025 e = get_irg_end(current_ir_graph);
1026 if (get_irn_visited(e) < get_irg_visited(current_ir_graph)) {
1028 /* Don't visit the End node. */
1029 for (j = 0; j < get_End_n_keepalives(e); j++) scc(get_End_keepalive(e, j));
1033 set_irg_loop(outermost_ir_graph, current_loop);
1034 set_irg_loopinfo_state(current_ir_graph, loopinfo_ip_consistent);
1035 assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
1037 current_ir_graph = rem;
1038 interprocedural_view = rem_ipv;
1042 static void reset_backedges(ir_node *n) {
1043 if (is_possible_loop_head(n)) {
1044 int rem = interprocedural_view;
1045 interprocedural_view = 1;
1047 interprocedural_view = 0;
1049 interprocedural_view = rem;
1053 static void loop_reset_backedges(ir_loop *l) {
1055 reset_backedges(get_loop_node(l, 0));
1056 for (i = 0; i < get_loop_n_nodes(l); ++i)
1057 set_irn_loop(get_loop_node(l, i), NULL);
1058 for (i = 0; i < get_loop_n_sons(l); ++i) {
1059 loop_reset_backedges(get_loop_son(l, i));
1063 /** Removes all loop information.
1064 Resets all backedges */
1065 void free_loop_information(ir_graph *irg) {
1066 if (get_irg_loop(irg))
1067 loop_reset_backedges(get_irg_loop(irg));
1068 set_irg_loop(irg, NULL);
1069 set_irg_loopinfo_state(current_ir_graph, loopinfo_none);
1070 /* We cannot free the loop nodes, they are on the obstack. */
1074 void free_all_loop_information (void) {
1076 int rem = interprocedural_view;
1077 interprocedural_view = 1; /* To visit all filter nodes */
1078 for (i = 0; i < get_irp_n_irgs(); i++) {
1079 free_loop_information(get_irp_irg(i));
1081 pmap_destroy(node_loop_map);
1082 node_loop_map = NULL;
1083 interprocedural_view = rem;
1090 /* Debug stuff *************************************************/
1092 static int test_loop_node(ir_loop *l) {
1093 int i, has_node = 0, found_problem = 0;
1096 assert(l && l->kind == k_ir_loop);
1098 if (get_loop_n_elements(l) == 0) {
1099 printf(" Loop completely empty! "); DDML(l);
1101 dump_loop(l, "-ha");
1104 le = get_loop_element(l, 0);
1105 if (*(le.kind) != k_ir_node) {
1106 assert(le.kind && *(le.kind) == k_ir_loop);
1107 printf(" First loop element is not a node! "); DDML(l);
1108 printf(" "); DDML(le.son);
1111 dump_loop(l, "-ha");
1114 if ((*(le.kind) == k_ir_node) && !is_possible_loop_head(le.node)) {
1115 printf(" Wrong node as head! "); DDML(l);
1116 printf(" "); DDMN(le.node);
1118 dump_loop(l, "-ha");
1121 if ((get_loop_depth(l) != 0) &&
1122 (*(le.kind) == k_ir_node) && !has_backedges(le.node)) {
1123 printf(" Loop head has no backedges! "); DDML(l);
1124 printf(" "); DDMN(le.node);
1126 dump_loop(l, "-ha");
1131 for (i = 0; i < get_loop_n_elements(l); ++i) {
1132 le = get_loop_element(l, i);
1133 if (*(le.kind) == k_ir_node)
1136 if (test_loop_node(le.son)) found_problem = 1;
1139 if (has_node == 0) {
1140 printf(" Loop has no firm node! "); DDML(l);
1142 dump_loop(l, "-ha");
1145 if (get_loop_loop_nr(l) == 11819)
1146 dump_loop(l, "-ha-debug");
1148 return found_problem;
1151 /** Prints all loop nodes that
1152 * - do not have any firm nodes, only loop sons
1153 * - the header is not a Phi, Block or Filter.
1155 void find_strange_loop_nodes(ir_loop *l) {
1156 int found_problem = 0;
1157 printf("\nTesting loop "); DDML(l);
1158 found_problem = test_loop_node(l);
1159 printf("Finished Test\n\n");
1160 if (found_problem) exit(0);