1 /* Copyright (C) 2002 by Universitaet Karlsruhe
4 * Authors: Goetz Lindenmaier
6 * irscc.c Computing the strongly connected regions and building
7 * backedge/loop datastructures.
17 #include "irgraph_t.h"
22 ir_graph *outermost_ir_graph; /* The outermost graph the scc is computed
24 static ir_loop *current_loop; /* Current loop construction is working
26 static int loop_node_cnt = 0; /* Counts the number of allocated loop nodes.
27 Each loop node gets a unique number.
28 What for? ev. remove. @@@ */
29 static int current_dfn = 1; /* Counter to generate depth first numbering
32 /**********************************************************************/
33 /* Node attributes needed for the construction. **/
34 /**********************************************************************/
36 typedef struct scc_info {
37 bool in_stack; /* Marks whether node is on the stack. */
38 int dfn; /* Depth first search number. */
39 int uplink; /* dfn number of ancestor. */
40 ir_loop *loop; /* Refers to the containing loop. */
42 struct section *section;
48 static INLINE scc_info* new_scc_info(void) {
49 scc_info *info = obstack_alloc (outermost_ir_graph->obst, sizeof (scc_info));
50 memset (info, 0, sizeof (scc_info));
55 mark_irn_in_stack (ir_node *n) {
56 assert(get_irn_link(n));
57 ((scc_info *)get_irn_link(n))->in_stack = true;
61 mark_irn_not_in_stack (ir_node *n) {
62 assert(get_irn_link(n));
63 ((scc_info *)get_irn_link(n))->in_stack = false;
67 irn_is_in_stack (ir_node *n) {
68 assert(get_irn_link(n));
69 return ((scc_info *)get_irn_link(n))->in_stack;
73 set_irn_uplink (ir_node *n, int uplink) {
74 assert(get_irn_link(n));
75 ((scc_info *)get_irn_link(n))->uplink = uplink;
79 get_irn_uplink (ir_node *n) {
80 assert(get_irn_link(n));
81 return ((scc_info *)get_irn_link(n))->uplink;
85 set_irn_dfn (ir_node *n, int dfn) {
86 if (! get_irn_link(n)) { DDMN(n); DDME(get_irg_ent(current_ir_graph));}
87 assert(get_irn_link(n));
88 ((scc_info *)get_irn_link(n))->dfn = dfn;
92 get_irn_dfn (ir_node *n) {
93 assert(get_irn_link(n));
94 return ((scc_info *)get_irn_link(n))->dfn;
97 /* Uses temporary information to set the loop */
99 set_irn_loop_tmp (ir_node *n, ir_loop* loop) {
100 assert(get_irn_link(n));
101 ((scc_info *)get_irn_link(n))->loop = loop;
105 /* Uses temporary information to get the loop */
106 static INLINE ir_loop *
107 get_irn_loop_tmp (ir_node *n) {
108 assert(get_irn_link(n));
109 return ((scc_info *)get_irn_link(n))->loop;
113 static ir_loop *find_nodes_loop (ir_node *n, ir_loop *l) {
117 /* Test whether n is contained in this loop. */
118 for (i = 0; i < get_loop_n_nodes(l); i++)
119 if (n == get_loop_node(l, i)) return l;
121 /* Is this a leave in the loop tree? If so loop not found. */
122 if (get_loop_n_sons(l) == 0) return NULL;
124 /* Else descend in the loop tree. */
125 for (i = 0; i < get_loop_n_sons(l); i++) {
126 res = find_nodes_loop(n, get_loop_son(l, i));
132 /* @@@ temporary implementation, costly!!! */
133 ir_loop * get_irn_loop(ir_node *n) {
134 ir_loop *l = get_irg_loop(current_ir_graph);
135 l = find_nodes_loop(n, l);
139 /**********************************************************************/
141 /**********************************************************************/
143 static ir_node **stack = NULL;
144 static int tos = 0; /* top of stack */
146 static INLINE void init_stack(void) {
148 ARR_RESIZE (ir_node *, stack, 1000);
150 stack = NEW_ARR_F (ir_node *, 1000);
156 static INLINE void free_stack(void) {
168 if (tos == ARR_LEN (stack)) {
169 int nlen = ARR_LEN (stack) * 2;
170 ARR_RESIZE (ir_node *, stack, nlen);
173 mark_irn_in_stack(n);
176 static INLINE ir_node *
179 ir_node *n = stack[--tos];
180 mark_irn_not_in_stack(n);
184 /* The nodes up to n belong to the current loop.
185 Removes them from the stack and adds them to the current loop. */
187 pop_scc_to_loop (ir_node *n)
193 set_irn_dfn(m, loop_node_cnt);
195 add_loop_node(current_loop, m);
196 set_irn_loop_tmp(m, current_loop);
201 /* Removes and unmarks all nodes up to n from the stack.
202 The nodes must be visited once more to assign them to a scc. */
204 pop_scc_unmark_visit (ir_node *n)
210 set_irn_visited(m, 0);
214 /**********************************************************************/
215 /* The loop datastructure. **/
216 /**********************************************************************/
218 /* Allocates a new loop as son of current_loop. Sets current_loop
219 to the new loop and returns the father. */
220 static ir_loop *new_loop (void) {
221 ir_loop *father, *son;
223 father = current_loop;
225 son = (ir_loop *) obstack_alloc (outermost_ir_graph->obst, sizeof (ir_loop));
226 memset (son, 0, sizeof (ir_loop));
227 son->kind = k_ir_loop;
228 /* son->sons = NEW_ARR_F (ir_loop *, 0);
229 son->nodes = NEW_ARR_F (ir_node *, 0);
230 A. Schoesser: Removed, because array children was introduced,
231 which contains both, nodes AND sons.
232 This comment may be removed after beeing read by all important persons :) */
233 son->children = NEW_ARR_F (loop_element, 0);
237 son->outer_loop = father;
238 add_loop_son(father, son);
239 son->depth = father->depth+1;
240 } else { /* The root loop */
241 son->outer_loop = son;
250 /* Finishes the datastructures, copies the arrays to the obstack
252 A. Schoesser: Caution: loop -> sons is gone. */
253 static void mature_loop (ir_loop *loop) {
256 new_sons = NEW_ARR_D (ir_loop *, current_ir_graph->obst, ARR_LEN(loop->sons));
257 memcpy (new_sons, loop->sons, sizeof (ir_loop *) * ARR_LEN(loop->sons));
258 DEL_ARR_F(loop->sons);
259 loop->sons = new_sons;
263 /* Returns outer loop, itself if outermost. */
264 ir_loop *get_loop_outer_loop (ir_loop *loop) {
265 assert(loop && loop->kind == k_ir_loop);
266 return loop->outer_loop;
269 /* Returns nesting depth of this loop */
270 int get_loop_depth (ir_loop *loop) {
271 assert(loop); assert(loop->kind == k_ir_loop);
275 /* Returns the number of inner loops */
276 int get_loop_n_sons (ir_loop *loop) {
277 assert(loop && loop->kind == k_ir_loop);
278 return(loop -> n_sons);
281 /* Returns the pos`th loop_node-child *
282 * TODO: This method isn`t very efficient ! *
283 * Returns NULL if there isnt`t a pos`th loop_node */
284 ir_loop *get_loop_son (ir_loop *loop, int pos) {
285 int child_nr = 0, loop_nr = -1;
287 assert(loop && loop->kind == k_ir_loop);
288 while(child_nr < ARR_LEN(loop->children))
290 if(*(loop -> children[child_nr].kind) == k_ir_loop)
293 return(loop -> children[child_nr].son);
299 /* Use EXCLUSIVELY this function to add sons, otherwise the loop->n_sons
303 add_loop_son(ir_loop *loop, ir_loop *son) {
306 assert(loop && loop->kind == k_ir_loop);
307 assert(get_kind(son) == k_ir_loop);
308 ARR_APP1 (loop_element, loop->children, lson);
312 /* Returns the number of nodes in the loop */
313 int get_loop_n_nodes (ir_loop *loop) {
314 assert(loop); assert(loop->kind == k_ir_loop);
315 return loop -> n_nodes;
316 /* return ARR_LEN(loop->nodes); */
319 /* Returns the pos`th ir_node-child *
320 * TODO: This method isn`t very efficient ! *
321 * Returns NULL if there isnt`t a pos`th ir_node */
322 ir_node *get_loop_node (ir_loop *loop, int pos) {
323 int child_nr, node_nr = -1;
325 assert(loop && loop->kind == k_ir_loop);
326 assert(pos < get_loop_n_nodes(loop));
328 for (child_nr = 0; child_nr < ARR_LEN(loop->children); child_nr++) {
329 if(*(loop -> children[child_nr].kind) == k_ir_node)
332 return(loop -> children[child_nr].node);
334 assert(0 && "no child at pos found");
338 /* Use EXCLUSIVELY this function to add nodes, otherwise the loop->n_nodes
342 add_loop_node(ir_loop *loop, ir_node *n) {
345 assert(loop && loop->kind == k_ir_loop);
346 assert(get_kind(n) == k_ir_node);
347 ARR_APP1 (loop_element, loop->children, ln);
351 /** Returns the number of elements contained in loop. */
352 int get_loop_n_elements (ir_loop *loop) {
353 assert(loop && loop->kind == k_ir_loop);
354 return(ARR_LEN(loop->children));
358 Returns the pos`th loop element.
359 This may be a loop_node or a ir_node. The caller of this function has
360 to check the *(loop_element.kind) field for "k_ir_node" or "k_ir_loop"
361 and then select the apropriate "loop_element.node" or "loop_element.son".
364 loop_element get_loop_element (ir_loop *loop, int pos) {
365 assert(loop && loop->kind == k_ir_loop && pos < ARR_LEN(loop->children));
367 return(loop -> children[pos]);
370 /* The outermost loop is remarked in the surrounding graph. */
371 void set_irg_loop(ir_graph *irg, ir_loop *loop) {
375 ir_loop *get_irg_loop(ir_graph *irg) {
380 /**********************************************************************/
381 /* Constructing and destructing the loop/backedge information. **/
382 /**********************************************************************/
384 /* Initialization steps. **********************************************/
387 init_node (ir_node *n, void *env) {
388 set_irn_link (n, new_scc_info());
391 /* Also init nodes not visible in intraproc_view. */
392 /* @@@ init_node is called for too many nodes -- this wastes memory!.
393 The mem is not lost as its on the obstack. */
394 if (get_irn_op(n) == op_Filter) {
395 for (i = 0; i < get_Filter_n_cg_preds(n); i++)
396 init_node(get_Filter_cg_pred(n, i), NULL);
398 if (get_irn_op(n) == op_Block) {
399 for (i = 0; i < get_Block_cg_n_cfgpreds(n); i++) {
400 init_node(get_Block_cg_cfgpred(n, i), NULL);
403 /* The following pattern matches only after a call from above pattern. */
404 if ((get_irn_op(n) == op_Proj) /*&& (get_Proj_proj(n) == 0)*/) {
405 /* @@@ init_node is called for every proj -- this wastes memory!.
406 The mem is not lost as its on the obstack. */
407 ir_node *cb = get_Proj_pred(n);
408 if ((get_irn_op(cb) == op_CallBegin) ||
409 (get_irn_op(cb) == op_EndReg) ||
410 (get_irn_op(cb) == op_EndExcept)) {
412 init_node(get_nodes_Block(cb), NULL);
418 init_scc (ir_graph *irg) {
422 irg_walk_graph (irg, init_node, NULL, NULL);
424 irg_walk (irg, link_to_reg_end, NULL, NULL);
433 cg_walk (init_node, NULL, NULL);
437 Works, but is inefficient.
441 interprocedural_view = 1;
445 for (i = 0; i < get_irp_n_irgs(); i++) {
446 current_ir_graph = get_irp_irg(i);
447 irg_walk_graph (current_ir_graph, init_node, NULL, NULL);
448 /* @@@ decrease max_visited to avoide double walks */
453 /* Condition for breaking the recursion. */
454 static bool is_outermost_Start(ir_node *n) {
455 /* Test whether this is the outermost Start node. If so
456 recursion must end. */
457 if ((get_irn_op(n) == op_Block) &&
458 (get_Block_n_cfgpreds(n) == 1) &&
459 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Start) &&
460 (get_nodes_Block(skip_Proj(get_Block_cfgpred(n, 0))) == n)) {
464 /* @@@ Bad condition:
465 not possible in interprocedural view as outermost_graph is
466 not necessarily the only with a dead-end start block.
467 Besides current_ir_graph is not set properly. */
468 if ((get_irn_op(n) == op_Block) &&
469 (n == get_irg_start_block(current_ir_graph))) {
470 if ((!interprocedural_view) ||
471 (current_ir_graph == outermost_ir_graph))
478 /* Don't walk from nodes to blocks except for Control flow operations. */
480 get_start_index(ir_node *n) {
481 if (is_cfop(n) || is_fragile_op(n) || get_irn_op(n) == op_Start)
487 /* Returns current_ir_graph and set it to the irg of predecessor index
489 static INLINE ir_graph *
490 switch_irg (ir_node *n, int index) {
491 ir_graph *old_current = current_ir_graph;
493 if (interprocedural_view) {
494 /* Only Filter and Block nodes can have predecessors in other graphs. */
495 if (get_irn_op(n) == op_Filter)
496 n = get_nodes_Block(n);
497 if (get_irn_op(n) == op_Block) {
498 ir_node *cfop = skip_Proj(get_Block_cfgpred(n, index));
499 if (is_ip_cfop(cfop)) {
500 current_ir_graph = get_irn_irg(cfop);
501 set_irg_visited(current_ir_graph, get_max_irg_visited());
509 /* Walks up the stack passing n and then finding the node
510 where we walked into the irg n is contained in.
511 Here we switch the irg. */
513 find_irg_on_stack (ir_node *n) {
515 ir_graph *old_current = current_ir_graph;
518 if (interprocedural_view) {
519 for (i = tos; i >= 0; i--) {
520 if (stack[i] == n) break;
525 for (; i >= 0; i--) {
527 /*printf(" Visiting %d ", i); DDMN(m);*/
529 current_ir_graph = get_irn_irg(m);
532 if (get_irn_op(m) == op_Filter) {
533 /* Find the corresponding ip_cfop */
534 ir_node *pred = stack[i+1];
536 for (j = 0; j < get_Filter_n_cg_preds(m); j++)
537 if (get_Filter_cg_pred(m, j) == pred) break;
538 if (j >= get_Filter_n_cg_preds(m))
539 /* It is a filter we didn't pass as the predecessors are marked. */
541 assert(get_Filter_cg_pred(m, j) == pred);
552 static void test(ir_node *pred, ir_node *root, ir_node *this) {
554 if (get_irn_uplink(pred) >= get_irn_uplink(root)) return;
556 printf("this: %d ", get_irn_uplink(this)); DDMN(this);
557 printf("pred: %d ", get_irn_uplink(pred)); DDMN(pred);
558 printf("root: %d ", get_irn_uplink(root)); DDMN(root);
560 printf("tos: %d\n", tos);
562 for (i = tos; i >= 0; i--) {
563 ir_node *n = stack[i];
565 printf(" uplink: %d, pos: %d ", get_irn_uplink(n), i); DDMN(n);
570 /* Returns true if n is a loop header, i.e., it is a Block, Phi
571 or Filter node and has predecessors within the loop and out
574 is_head (ir_node *n, ir_node *root)
577 int some_outof_loop = 0, some_in_loop = 0;
579 /* Test for legal loop header */
580 if (!((get_irn_op(n) == op_Block) ||
581 (get_irn_op(n) == op_Phi) ||
582 ((get_irn_op(n) == op_Filter) && interprocedural_view)))
585 if (!is_outermost_Start(n)) {
586 for (i = get_start_index(n); i < get_irn_arity(n); i++) {
587 ir_node *pred = get_irn_n(n, i);
589 if (is_backedge(n, i)) continue;
590 if (!irn_is_in_stack(pred)) {
593 assert(get_irn_uplink(pred) >= get_irn_uplink(root));
598 return some_outof_loop && some_in_loop;
601 /* Returns index of the predecessor with the smallest dfn number
602 greater-equal than limit. */
604 smallest_dfn_pred (ir_node *n, int limit)
606 int i, index = -2, min = -1;
608 if (!is_outermost_Start(n)) {
609 for (i = get_start_index(n); i < get_irn_arity(n); i++) {
610 ir_node *pred = get_irn_n(n, i);
612 if (is_backedge(n, i) || !irn_is_in_stack(pred)) continue;
613 if (get_irn_dfn(pred) >= limit
614 && (min == -1 || get_irn_dfn(pred) < min)) {
616 min = get_irn_dfn(pred);
623 /* Returns index of the predecessor with the largest dfn number. */
625 largest_dfn_pred (ir_node *n)
627 int i, index = -2, max = -1;
629 if (!is_outermost_Start(n)) {
630 for (i = get_start_index(n); i < get_irn_arity(n); i++) {
631 ir_node *pred = get_irn_n(n, i);
632 if (is_backedge (n, i) || !irn_is_in_stack(pred)) continue;
633 if (get_irn_dfn(pred) > max) {
635 max = get_irn_dfn(pred);
642 /* Searches the stack for possible loop heads. Tests these for backedges.
643 If it finds a head with an unmarked backedge it marks this edge and
644 returns the tail of the loop.
645 If it finds no backedge returns NULL. */
647 find_tail (ir_node *n) {
649 int i, res_index = -2;
652 if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
656 if (is_head (m, n)) {
657 res_index = smallest_dfn_pred(m, 0);
658 if ((res_index == -2) && /* no smallest dfn pred found. */
662 if (m == n) return NULL;
663 for (i = tos-2; ; --i) {
665 if (is_head (m, n)) {
666 res_index = smallest_dfn_pred (m, get_irn_dfn(m) + 1);
667 if (res_index == -2) /* no smallest dfn pred found. */
668 res_index = largest_dfn_pred (m);
673 assert (res_index > -2);
675 set_backedge (m, res_index);
676 return is_outermost_Start(n) ? NULL : get_irn_n(m, res_index);
680 /* The core algorithm. *****************************************/
682 static void scc (ir_node *n) {
686 if (irn_visited(n)) return;
688 /*printf("mark: %d ", get_irn_visited(n)); DDMN(n);
689 DDME(get_irg_ent(current_ir_graph));*/
691 /* Initialize the node */
692 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
693 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
694 set_irn_loop_tmp(n, NULL);
697 /* What's this good for?
698 n->ana.scc.section = NULL;
703 if (!is_outermost_Start(n)) {
704 for (i = get_start_index(n); i < get_irn_arity(n); i++) {
706 if (is_backedge(n, i)) continue;
708 m = get_irn_n(n, i); /*get_irn_ip_pred(n, i);*/
709 if ((!m) || (get_irn_op(m) == op_Unknown)) continue;
711 /*return_recur(n, i);*/
713 if (irn_is_in_stack(m)) {
714 /* Uplink of m is smaller if n->m is a backedge.
715 Propagate the uplink to mark the loop. */
716 if (get_irn_uplink(m) < get_irn_uplink(n))
717 set_irn_uplink(n, get_irn_uplink(m));
721 if (get_irn_dfn(n) == get_irn_uplink(n)) {
722 /* This condition holds for the node with the incoming backedge. */
723 ir_node *tail = find_tail(n);
725 /* We found a new loop! */
726 ir_loop *l = new_loop();
727 /* Remove the loop from the stack ... */
728 pop_scc_unmark_visit (n);
729 /* and recompute it in a better order; and so that it goes into
731 rem = find_irg_on_stack(tail);
733 current_ir_graph = rem;
735 assert (irn_visited(n));
744 /* Constructs backedge information for irg. In interprocedural view constructs
745 backedges for all methods called by irg, too. */
746 void construct_backedges(ir_graph *irg) {
747 ir_graph *rem = current_ir_graph;
751 assert(!interprocedural_view &&
752 "not implemented, use construct_ip_backedges");
754 current_ir_graph = irg;
755 outermost_ir_graph = irg;
760 new_loop(); /* sets current_loop */
761 head_rem = current_loop; /* Just for assertion */
763 if (interprocedural_view) {
764 set_irg_visited(irg, inc_max_irg_visited());
767 inc_irg_visited(irg);
770 scc(get_irg_end(irg));
771 for (i = 0; i < get_End_n_keepalives(get_irg_end(irg)); i++)
772 scc(get_End_keepalive(get_irg_end(irg), i));
774 if (interprocedural_view) finish_ip_walk();
776 assert(head_rem == current_loop);
777 set_irg_loop(irg, current_loop);
778 assert(get_irg_loop(irg)->kind == k_ir_loop);
780 irg->loops = current_loop;
784 count_loop (the_loop, &count, &depth);
788 current_ir_graph = rem;
793 void construct_ip_backedges (void) {
794 ir_graph *rem = current_ir_graph;
795 int rem_ipv = interprocedural_view;
798 outermost_ir_graph = get_irp_main_irg();
803 new_loop(); /* sets current_loop */
804 interprocedural_view = 1;
806 inc_max_irg_visited();
807 for (i = 0; i < get_irp_n_irgs(); i++)
808 set_irg_visited(get_irp_irg(i), get_max_irg_visited());
810 for (i = 0; i < get_irp_n_irgs(); i++) {
812 current_ir_graph = get_irp_irg(i);
813 /*DDME(get_irg_ent(current_ir_graph));*/
814 /* Find real entry points */
815 sb = get_irg_start_block(current_ir_graph);
816 if ((get_Block_n_cfgpreds(sb) > 1) ||
817 (get_nodes_Block(get_Block_cfgpred(sb, 0)) != sb)) continue;
818 /* printf("running scc for "); DDME(get_irg_ent(current_ir_graph)); */
819 /* Compute scc for this graph */
820 outermost_ir_graph = current_ir_graph;
821 set_irg_visited(outermost_ir_graph, get_max_irg_visited());
822 scc(get_irg_end(current_ir_graph));
823 for (j = 0; j < get_End_n_keepalives(get_irg_end(outermost_ir_graph)); j++)
824 scc(get_End_keepalive(get_irg_end(outermost_ir_graph), j));
827 set_irg_loop(outermost_ir_graph, current_loop);
828 assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
830 current_ir_graph = rem;
831 interprocedural_view = rem_ipv;