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"
23 ir_graph *outermost_ir_graph; /* The outermost graph the scc is computed
25 static ir_loop *current_loop; /* Current loop construction is working
27 static int loop_node_cnt = 0; /* Counts the number of allocated loop nodes.
28 Each loop node gets a unique number.
29 What for? ev. remove. @@@ */
30 static int current_dfn = 1; /* Counter to generate depth first numbering
33 /**********************************************************************/
34 /* Node attributes needed for the construction. **/
35 /**********************************************************************/
37 typedef struct scc_info {
38 bool in_stack; /* Marks whether node is on the stack. */
39 int dfn; /* Depth first search number. */
40 int uplink; /* dfn number of ancestor. */
41 ir_loop *loop; /* Refers to the containing loop. */
43 struct section *section;
49 static INLINE scc_info* new_scc_info(void) {
50 scc_info *info = obstack_alloc (outermost_ir_graph->obst, sizeof (scc_info));
51 memset (info, 0, sizeof (scc_info));
56 mark_irn_in_stack (ir_node *n) {
57 assert(get_irn_link(n));
58 ((scc_info *)get_irn_link(n))->in_stack = true;
62 mark_irn_not_in_stack (ir_node *n) {
63 assert(get_irn_link(n));
64 ((scc_info *)get_irn_link(n))->in_stack = false;
68 irn_is_in_stack (ir_node *n) {
69 assert(get_irn_link(n));
70 return ((scc_info *)get_irn_link(n))->in_stack;
74 set_irn_uplink (ir_node *n, int uplink) {
75 assert(get_irn_link(n));
76 ((scc_info *)get_irn_link(n))->uplink = uplink;
80 get_irn_uplink (ir_node *n) {
81 assert(get_irn_link(n));
82 return ((scc_info *)get_irn_link(n))->uplink;
86 set_irn_dfn (ir_node *n, int dfn) {
87 if (! get_irn_link(n)) { DDMN(n); DDME(get_irg_ent(current_ir_graph));}
88 assert(get_irn_link(n));
89 ((scc_info *)get_irn_link(n))->dfn = dfn;
93 get_irn_dfn (ir_node *n) {
94 assert(get_irn_link(n));
95 return ((scc_info *)get_irn_link(n))->dfn;
98 /* Uses temporary information to set the loop */
100 set_irn_loop_tmp (ir_node *n, ir_loop* loop) {
101 assert(get_irn_link(n));
102 ((scc_info *)get_irn_link(n))->loop = loop;
106 /* Uses temporary information to get the loop */
107 static INLINE ir_loop *
108 get_irn_loop_tmp (ir_node *n) {
109 assert(get_irn_link(n));
110 return ((scc_info *)get_irn_link(n))->loop;
114 static ir_loop *find_nodes_loop (ir_node *n, ir_loop *l) {
118 /* Test whether n is contained in this loop. */
119 for (i = 0; i < get_loop_n_nodes(l); i++)
120 if (n == get_loop_node(l, i)) return l;
122 /* Is this a leave in the loop tree? If so loop not found. */
123 if (get_loop_n_sons(l) == 0) return NULL;
125 /* Else descend in the loop tree. */
126 for (i = 0; i < get_loop_n_sons(l); i++) {
127 res = find_nodes_loop(n, get_loop_son(l, i));
133 /* @@@ temporary implementation, costly!!! */
134 ir_loop * get_irn_loop(ir_node *n) {
135 ir_loop *l = get_irg_loop(current_ir_graph);
136 l = find_nodes_loop(n, l);
140 /**********************************************************************/
142 /**********************************************************************/
144 static ir_node **stack = NULL;
145 static int tos = 0; /* top of stack */
147 static INLINE void init_stack(void) {
149 ARR_RESIZE (ir_node *, stack, 1000);
151 stack = NEW_ARR_F (ir_node *, 1000);
157 static INLINE void free_stack(void) {
169 if (tos == ARR_LEN (stack)) {
170 int nlen = ARR_LEN (stack) * 2;
171 ARR_RESIZE (ir_node *, stack, nlen);
174 mark_irn_in_stack(n);
177 static INLINE ir_node *
180 ir_node *n = stack[--tos];
181 mark_irn_not_in_stack(n);
185 /* The nodes up to n belong to the current loop.
186 Removes them from the stack and adds them to the current loop. */
188 pop_scc_to_loop (ir_node *n)
194 set_irn_dfn(m, loop_node_cnt);
196 add_loop_node(current_loop, m);
197 set_irn_loop_tmp(m, current_loop);
202 /* Removes and unmarks all nodes up to n from the stack.
203 The nodes must be visited once more to assign them to a scc. */
205 pop_scc_unmark_visit (ir_node *n)
211 set_irn_visited(m, 0);
215 /**********************************************************************/
216 /* The loop datastructure. **/
217 /**********************************************************************/
219 /* Allocates a new loop as son of current_loop. Sets current_loop
220 to the new loop and returns the father. */
221 static ir_loop *new_loop (void) {
222 ir_loop *father, *son;
224 father = current_loop;
226 son = (ir_loop *) obstack_alloc (outermost_ir_graph->obst, sizeof (ir_loop));
227 memset (son, 0, sizeof (ir_loop));
228 son->kind = k_ir_loop;
229 /* son->sons = NEW_ARR_F (ir_loop *, 0);
230 son->nodes = NEW_ARR_F (ir_node *, 0);
231 A. Schoesser: Removed, because array children was introduced,
232 which contains both, nodes AND sons.
233 This comment may be removed after beeing read by all important persons :) */
234 son->children = NEW_ARR_F (loop_element, 0);
238 son->outer_loop = father;
239 add_loop_son(father, son);
240 son->depth = father->depth+1;
241 } else { /* The root loop */
242 son->outer_loop = son;
251 /* Finishes the datastructures, copies the arrays to the obstack
253 A. Schoesser: Caution: loop -> sons is gone. */
254 static void mature_loop (ir_loop *loop) {
257 new_sons = NEW_ARR_D (ir_loop *, current_ir_graph->obst, ARR_LEN(loop->sons));
258 memcpy (new_sons, loop->sons, sizeof (ir_loop *) * ARR_LEN(loop->sons));
259 DEL_ARR_F(loop->sons);
260 loop->sons = new_sons;
264 /* Returns outer loop, itself if outermost. */
265 ir_loop *get_loop_outer_loop (ir_loop *loop) {
266 assert(loop && loop->kind == k_ir_loop);
267 return loop->outer_loop;
270 /* Returns nesting depth of this loop */
271 int get_loop_depth (ir_loop *loop) {
272 assert(loop); assert(loop->kind == k_ir_loop);
276 /* Returns the number of inner loops */
277 int get_loop_n_sons (ir_loop *loop) {
278 assert(loop && loop->kind == k_ir_loop);
279 return(loop -> n_sons);
282 /* Returns the pos`th loop_node-child *
283 * TODO: This method isn`t very efficient ! *
284 * Returns NULL if there isnt`t a pos`th loop_node */
285 ir_loop *get_loop_son (ir_loop *loop, int pos) {
286 int child_nr = 0, loop_nr = -1;
288 assert(loop && loop->kind == k_ir_loop);
289 while(child_nr < ARR_LEN(loop->children))
291 if(*(loop -> children[child_nr].kind) == k_ir_loop)
294 return(loop -> children[child_nr].son);
300 /* Use EXCLUSIVELY this function to add sons, otherwise the loop->n_sons
304 add_loop_son(ir_loop *loop, ir_loop *son) {
305 assert(loop && loop->kind == k_ir_loop);
306 assert(get_kind(son) == k_ir_loop);
307 ARR_APP1 (loop_element, loop->children, (loop_element) son);
311 /* Returns the number of nodes in the loop */
312 int get_loop_n_nodes (ir_loop *loop) {
313 assert(loop); assert(loop->kind == k_ir_loop);
314 return loop -> n_nodes;
315 /* return ARR_LEN(loop->nodes); */
318 /* Returns the pos`th ir_node-child *
319 * TODO: This method isn`t very efficient ! *
320 * Returns NULL if there isnt`t a pos`th ir_node */
321 ir_node *get_loop_node (ir_loop *loop, int pos) {
322 int child_nr, node_nr = -1;
324 assert(loop && loop->kind == k_ir_loop);
325 assert(pos < get_loop_n_nodes(loop));
327 for (child_nr = 0; child_nr < ARR_LEN(loop->children); child_nr++) {
328 if(*(loop -> children[child_nr].kind) == k_ir_node)
331 return(loop -> children[child_nr].node);
333 assert(0 && "no child at pos found");
337 /* Use EXCLUSIVELY this function to add nodes, otherwise the loop->n_nodes
341 add_loop_node(ir_loop *loop, ir_node *n) {
342 assert(loop && loop->kind == k_ir_loop);
343 assert(get_kind(n) == k_ir_node);
344 ARR_APP1 (loop_element, loop->children, (loop_element) n);
348 /** Returns the number of elements contained in loop. */
349 int get_loop_n_elements (ir_loop *loop) {
350 assert(loop && loop->kind == k_ir_loop);
351 return(ARR_LEN(loop->children));
355 Returns the pos`th loop element.
356 This may be a loop_node or a ir_node. The caller of this function has
357 to check the *(loop_element.kind) field for "k_ir_node" or "k_ir_loop"
358 and then select the apropriate "loop_element.node" or "loop_element.son".
361 loop_element get_loop_element (ir_loop *loop, int pos) {
362 assert(loop && loop->kind == k_ir_loop && pos < ARR_LEN(loop->children));
364 return(loop -> children[pos]);
367 /* The outermost loop is remarked in the surrounding graph. */
368 void set_irg_loop(ir_graph *irg, ir_loop *loop) {
372 ir_loop *get_irg_loop(ir_graph *irg) {
377 /**********************************************************************/
378 /* Constructing and destructing the loop/backedge information. **/
379 /**********************************************************************/
381 /* Initialization steps. **********************************************/
384 init_node (ir_node *n, void *env) {
385 set_irn_link (n, new_scc_info());
388 /* Also init nodes not visible in intraproc_view. */
389 /* @@@ init_node is called for too many nodes -- this wastes memory!.
390 The mem is not lost as its on the obstack. */
391 if (get_irn_op(n) == op_Filter) {
392 for (i = 0; i < get_Filter_n_cg_preds(n); i++)
393 init_node(get_Filter_cg_pred(n, i), NULL);
395 if (get_irn_op(n) == op_Block) {
396 for (i = 0; i < get_Block_cg_n_cfgpreds(n); i++) {
397 init_node(get_Block_cg_cfgpred(n, i), NULL);
400 /* The following pattern matches only after a call from above pattern. */
401 if ((get_irn_op(n) == op_Proj) /*&& (get_Proj_proj(n) == 0)*/) {
402 /* @@@ init_node is called for every proj -- this wastes memory!.
403 The mem is not lost as its on the obstack. */
404 ir_node *cb = get_Proj_pred(n);
405 if ((get_irn_op(cb) == op_CallBegin) ||
406 (get_irn_op(cb) == op_EndReg) ||
407 (get_irn_op(cb) == op_EndExcept)) {
409 init_node(get_nodes_Block(cb), NULL);
415 init_scc (ir_graph *irg) {
419 irg_walk_graph (irg, init_node, NULL, NULL);
421 irg_walk (irg, link_to_reg_end, NULL, NULL);
430 cg_walk (init_node, NULL, NULL);
434 Works, but is inefficient.
438 interprocedural_view = 1;
442 for (i = 0; i < get_irp_n_irgs(); i++) {
443 current_ir_graph = get_irp_irg(i);
444 irg_walk_graph (current_ir_graph, init_node, NULL, NULL);
445 /* @@@ decrease max_visited to avoide double walks */
450 /* Condition for breaking the recursion. */
451 static bool is_outermost_Start(ir_node *n) {
452 /* Test whether this is the outermost Start node. If so
453 recursion must end. */
454 if ((get_irn_op(n) == op_Block) &&
455 (get_Block_n_cfgpreds(n) == 1) &&
456 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Start) &&
457 (get_nodes_Block(skip_Proj(get_Block_cfgpred(n, 0))) == n)) {
461 /* @@@ Bad condition:
462 not possible in interprocedural view as outermost_graph is
463 not necessarily the only with a dead-end start block.
464 Besides current_ir_graph is not set properly. */
465 if ((get_irn_op(n) == op_Block) &&
466 (n == get_irg_start_block(current_ir_graph))) {
467 if ((!interprocedural_view) ||
468 (current_ir_graph == outermost_ir_graph))
475 /* Don't walk from nodes to blocks except for Control flow operations. */
477 get_start_index(ir_node *n) {
478 if (is_cfop(n) || is_fragile_op(n) || get_irn_op(n) == op_Start)
484 /* Returns current_ir_graph and set it to the irg of predecessor index
486 static INLINE ir_graph *
487 switch_irg (ir_node *n, int index) {
488 ir_graph *old_current = current_ir_graph;
490 if (interprocedural_view) {
491 /* Only Filter and Block nodes can have predecessors in other graphs. */
492 if (get_irn_op(n) == op_Filter)
493 n = get_nodes_Block(n);
494 if (get_irn_op(n) == op_Block) {
495 ir_node *cfop = skip_Proj(get_Block_cfgpred(n, index));
496 if (is_ip_cfop(cfop)) {
497 current_ir_graph = get_irn_irg(cfop);
498 set_irg_visited(current_ir_graph, get_max_irg_visited());
506 /* Walks up the stack passing n and then finding the node
507 where we walked into the irg n is contained in.
508 Here we switch the irg. */
510 find_irg_on_stack (ir_node *n) {
512 ir_graph *old_current = current_ir_graph;
515 if (interprocedural_view) {
516 for (i = tos; i >= 0; i--) {
517 if (stack[i] == n) break;
524 for (; i >= 0; i--) {
526 //printf(" Visiting %d ", i); DDMN(m);
528 current_ir_graph = get_irn_irg(m);
531 if (get_irn_op(m) == op_Filter) {
532 /* Find the corresponding ip_cfop */
533 ir_node *pred = stack[i+1];
535 for (j = 0; j < get_Filter_n_cg_preds(m); j++)
536 if (get_Filter_cg_pred(m, j) == pred) break;
537 if (j >= get_Filter_n_cg_preds(m))
538 /* It is a filter we didn't pass as the predecessors are marked. */
540 assert(get_Filter_cg_pred(m, j) == pred);
551 static void test(ir_node *pred, ir_node *root, ir_node *this) {
553 if (get_irn_uplink(pred) >= get_irn_uplink(root)) return;
555 printf("this: %d ", get_irn_uplink(this)); DDMN(this);
556 printf("pred: %d ", get_irn_uplink(pred)); DDMN(pred);
557 printf("root: %d ", get_irn_uplink(root)); DDMN(root);
559 printf("tos: %d\n", tos);
561 for (i = tos; i >= 0; i--) {
562 ir_node *n = stack[i];
564 printf(" uplink: %d, pos: %d ", get_irn_uplink(n), i); DDMN(n);
569 /* Returns true if n is a loop header, i.e., it is a Block, Phi
570 or Filter node and has predecessors within the loop and out
573 is_head (ir_node *n, ir_node *root)
576 int some_outof_loop = 0, some_in_loop = 0;
578 /* Test for legal loop header */
579 if (!((get_irn_op(n) == op_Block) ||
580 (get_irn_op(n) == op_Phi) ||
581 ((get_irn_op(n) == op_Filter) && interprocedural_view)))
584 if (!is_outermost_Start(n)) {
585 for (i = get_start_index(n); i < get_irn_arity(n); i++) {
586 ir_node *pred = get_irn_n(n, i);
588 if (is_backedge(n, i)) continue;
589 if (!irn_is_in_stack(pred)) {
592 assert(get_irn_uplink(pred) >= get_irn_uplink(root));
597 return some_outof_loop && some_in_loop;
600 /* Returns index of the predecessor with the smallest dfn number
601 greater-equal than limit. */
603 smallest_dfn_pred (ir_node *n, int limit)
605 int i, index = -2, min = -1;
607 if (!is_outermost_Start(n)) {
608 for (i = get_start_index(n); i < get_irn_arity(n); i++) {
609 ir_node *pred = get_irn_n(n, i);
611 if (is_backedge(n, i) || !irn_is_in_stack(pred)) continue;
612 if (get_irn_dfn(pred) >= limit
613 && (min == -1 || get_irn_dfn(pred) < min)) {
615 min = get_irn_dfn(pred);
622 /* Returns index of the predecessor with the largest dfn number. */
624 largest_dfn_pred (ir_node *n)
626 int i, index = -2, max = -1;
628 if (!is_outermost_Start(n)) {
629 for (i = get_start_index(n); i < get_irn_arity(n); i++) {
630 ir_node *pred = get_irn_n(n, i);
631 if (is_backedge (n, i) || !irn_is_in_stack(pred)) continue;
632 if (get_irn_dfn(pred) > max) {
634 max = get_irn_dfn(pred);
641 /* Searches the stack for possible loop heads. Tests these for backedges.
642 If it finds a head with an unmarked backedge it marks this edge and
643 returns the tail of the loop.
644 If it finds no backedge returns NULL. */
646 find_tail (ir_node *n) {
648 int i, res_index = -2;
651 if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
655 if (is_head (m, n)) {
656 res_index = smallest_dfn_pred(m, 0);
657 if ((res_index == -2) && /* no smallest dfn pred found. */
661 if (m == n) return NULL;
662 for (i = tos-2; ; --i) {
664 if (is_head (m, n)) {
665 res_index = smallest_dfn_pred (m, get_irn_dfn(m) + 1);
666 if (res_index == -2) /* no smallest dfn pred found. */
667 res_index = largest_dfn_pred (m);
672 assert (res_index > -2);
674 set_backedge (m, res_index);
675 return is_outermost_Start(n) ? NULL : get_irn_n(m, res_index);
679 /* The core algorithm. *****************************************/
681 static void scc (ir_node *n) {
685 if (irn_visited(n)) return;
687 //printf("mark: %d ", get_irn_visited(n)); DDMN(n);
688 //DDME(get_irg_ent(current_ir_graph));
690 /* Initialize the node */
691 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
692 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
693 set_irn_loop_tmp(n, NULL);
696 /* What's this good for?
697 n->ana.scc.section = NULL;
702 if (!is_outermost_Start(n)) {
703 for (i = get_start_index(n); i < get_irn_arity(n); i++) {
705 if (is_backedge(n, i)) continue;
707 m = get_irn_n(n, i); //get_irn_ip_pred(n, i);
708 if ((!m) || (get_irn_op(m) == op_Unknown)) continue;
710 //return_recur(n, i);
712 if (irn_is_in_stack(m)) {
713 /* Uplink of m is smaller if n->m is a backedge.
714 Propagate the uplink to mark the loop. */
715 if (get_irn_uplink(m) < get_irn_uplink(n))
716 set_irn_uplink(n, get_irn_uplink(m));
720 if (get_irn_dfn(n) == get_irn_uplink(n)) {
721 /* This condition holds for the node with the incoming backedge. */
722 ir_node *tail = find_tail(n);
724 /* We found a new loop! */
725 ir_loop *l = new_loop();
726 /* Remove the loop from the stack ... */
727 pop_scc_unmark_visit (n);
728 /* and recompute it in a better order; and so that it goes into
730 rem = find_irg_on_stack(tail);
732 current_ir_graph = rem;
734 assert (irn_visited(n));
743 /* Constructs backedge information for irg. In interprocedural view constructs
744 backedges for all methods called by irg, too. */
745 void construct_backedges(ir_graph *irg) {
746 ir_graph *rem = current_ir_graph;
750 assert(!interprocedural_view &&
751 "not implemented, use construct_ip_backedges");
753 current_ir_graph = irg;
754 outermost_ir_graph = irg;
759 new_loop(); /* sets current_loop */
760 head_rem = current_loop; /* Just for assertion */
762 if (interprocedural_view) {
763 set_irg_visited(irg, inc_max_irg_visited());
766 inc_irg_visited(irg);
769 scc(get_irg_end(irg));
770 for (i = 0; i < get_End_n_keepalives(get_irg_end(irg)); i++)
771 scc(get_End_keepalive(get_irg_end(irg), i));
773 if (interprocedural_view) finish_ip_walk();
775 assert(head_rem == current_loop);
776 set_irg_loop(irg, current_loop);
777 assert(get_irg_loop(irg)->kind == k_ir_loop);
779 irg->loops = current_loop;
783 count_loop (the_loop, &count, &depth);
787 current_ir_graph = rem;
792 void construct_ip_backedges (void) {
793 ir_graph *rem = current_ir_graph;
794 int rem_ipv = interprocedural_view;
797 outermost_ir_graph = get_irp_main_irg();
802 new_loop(); /* sets current_loop */
803 interprocedural_view = 1;
805 inc_max_irg_visited();
806 for (i = 0; i < get_irp_n_irgs(); i++)
807 set_irg_visited(get_irp_irg(i), get_max_irg_visited());
809 for (i = 0; i < get_irp_n_irgs(); i++) {
811 current_ir_graph = get_irp_irg(i);
812 //DDME(get_irg_ent(current_ir_graph));
813 /* Find real entry points */
814 sb = get_irg_start_block(current_ir_graph);
815 if ((get_Block_n_cfgpreds(sb) > 1) ||
816 (get_nodes_Block(get_Block_cfgpred(sb, 0)) != sb)) continue;
817 // printf("running scc for "); DDME(get_irg_ent(current_ir_graph));
818 /* Compute scc for this graph */
819 outermost_ir_graph = current_ir_graph;
820 set_irg_visited(outermost_ir_graph, get_max_irg_visited());
821 scc(get_irg_end(current_ir_graph));
822 for (j = 0; j < get_End_n_keepalives(get_irg_end(outermost_ir_graph)); j++)
823 scc(get_End_keepalive(get_irg_end(outermost_ir_graph), j));
826 set_irg_loop(outermost_ir_graph, current_loop);
827 assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
829 current_ir_graph = rem;
830 interprocedural_view = rem_ipv;