3 * File name: ir/ana/irscc.c
4 * Purpose: Compute the strongly connected regions and build
5 * backedge/loop datastructures.
6 * Author: Goetz Lindenmaier
10 * Copyright: (c) 2002-2003 Universität Karlsruhe
11 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
22 #include "irgraph_t.h"
27 ir_graph *outermost_ir_graph; /* The outermost graph the scc is computed
29 static ir_loop *current_loop; /* Current loop construction is working
31 static int loop_node_cnt = 0; /* Counts the number of allocated loop nodes.
32 Each loop node gets a unique number.
33 What for? ev. remove. @@@ */
34 static int current_dfn = 1; /* Counter to generate depth first numbering
37 /**********************************************************************/
38 /* Node attributes needed for the construction. **/
39 /**********************************************************************/
41 typedef struct scc_info {
42 bool in_stack; /* Marks whether node is on the stack. */
43 int dfn; /* Depth first search number. */
44 int uplink; /* dfn number of ancestor. */
45 ir_loop *loop; /* Refers to the containing loop. */
47 struct section *section;
53 static INLINE scc_info* new_scc_info(void) {
54 scc_info *info = obstack_alloc (outermost_ir_graph->obst, sizeof (scc_info));
55 memset (info, 0, sizeof (scc_info));
60 mark_irn_in_stack (ir_node *n) {
61 assert(get_irn_link(n));
62 ((scc_info *)get_irn_link(n))->in_stack = true;
66 mark_irn_not_in_stack (ir_node *n) {
67 assert(get_irn_link(n));
68 ((scc_info *)get_irn_link(n))->in_stack = false;
72 irn_is_in_stack (ir_node *n) {
73 assert(get_irn_link(n));
74 return ((scc_info *)get_irn_link(n))->in_stack;
78 set_irn_uplink (ir_node *n, int uplink) {
79 assert(get_irn_link(n));
80 ((scc_info *)get_irn_link(n))->uplink = uplink;
84 get_irn_uplink (ir_node *n) {
85 assert(get_irn_link(n));
86 return ((scc_info *)get_irn_link(n))->uplink;
90 set_irn_dfn (ir_node *n, int dfn) {
91 if (! get_irn_link(n)) { DDMN(n); DDME(get_irg_ent(current_ir_graph));}
92 assert(get_irn_link(n));
93 ((scc_info *)get_irn_link(n))->dfn = dfn;
97 get_irn_dfn (ir_node *n) {
98 assert(get_irn_link(n));
99 return ((scc_info *)get_irn_link(n))->dfn;
102 /* Uses temporary information to set the loop */
104 set_irn_loop_tmp (ir_node *n, ir_loop* loop) {
105 assert(get_irn_link(n));
106 ((scc_info *)get_irn_link(n))->loop = loop;
110 /* Uses temporary information to get the loop */
111 static INLINE ir_loop *
112 get_irn_loop_tmp (ir_node *n) {
113 assert(get_irn_link(n));
114 return ((scc_info *)get_irn_link(n))->loop;
118 static ir_loop *find_nodes_loop (ir_node *n, ir_loop *l) {
122 /* Test whether n is contained in this loop. */
123 for (i = 0; i < get_loop_n_nodes(l); i++)
124 if (n == get_loop_node(l, i)) return l;
126 /* Is this a leave in the loop tree? If so loop not found. */
127 if (get_loop_n_sons(l) == 0) return NULL;
129 /* Else descend in the loop tree. */
130 for (i = 0; i < get_loop_n_sons(l); i++) {
131 res = find_nodes_loop(n, get_loop_son(l, i));
137 /* @@@ temporary implementation, costly!!! */
138 ir_loop * get_irn_loop(ir_node *n) {
139 ir_loop *l = get_irg_loop(current_ir_graph);
140 l = find_nodes_loop(n, l);
144 /**********************************************************************/
146 /**********************************************************************/
148 static ir_node **stack = NULL;
149 static int tos = 0; /* top of stack */
151 static INLINE void init_stack(void) {
153 ARR_RESIZE (ir_node *, stack, 1000);
155 stack = NEW_ARR_F (ir_node *, 1000);
161 static INLINE void free_stack(void) {
173 if (tos == ARR_LEN (stack)) {
174 int nlen = ARR_LEN (stack) * 2;
175 ARR_RESIZE (ir_node *, stack, nlen);
178 mark_irn_in_stack(n);
181 static INLINE ir_node *
184 ir_node *n = stack[--tos];
185 mark_irn_not_in_stack(n);
189 /* The nodes up to n belong to the current loop.
190 Removes them from the stack and adds them to the current loop. */
192 pop_scc_to_loop (ir_node *n)
201 set_irn_dfn(m, loop_node_cnt);
202 add_loop_node(current_loop, m);
203 set_irn_loop_tmp(m, current_loop);
204 /* if (m==n) break;*/
208 /* GL ??? my last son is my grandson??? Removes loops with no
209 ir_nodes in them. Such loops have only another loop as son. (Why
210 can't they have two loops as sons? Does it never get that far? ) */
211 void close_loop (ir_loop *l)
213 int last = get_loop_n_elements(l) - 1;
214 loop_element lelement = get_loop_element(l, last);
215 ir_loop *last_son = lelement.son;
217 if (get_kind(last_son) == k_ir_loop &&
218 get_loop_n_elements(last_son) == 1)
220 lelement = get_loop_element(last_son, 0);
221 ir_loop *gson = lelement.son;
222 if(get_kind(gson) == k_ir_loop)
224 gson -> outer_loop = l;
225 loop_element new_last_son;
226 new_last_son.son = gson;
227 l -> children[last] = new_last_son;
234 /* Removes and unmarks all nodes up to n from the stack.
235 The nodes must be visited once more to assign them to a scc. */
237 pop_scc_unmark_visit (ir_node *n)
243 set_irn_visited(m, 0);
247 /**********************************************************************/
248 /* The loop datastructure. **/
249 /**********************************************************************/
251 /* Allocates a new loop as son of current_loop. Sets current_loop
252 to the new loop and returns the father. */
253 static ir_loop *new_loop (void) {
254 ir_loop *father, *son;
256 father = current_loop;
258 son = (ir_loop *) obstack_alloc (outermost_ir_graph->obst, sizeof (ir_loop));
259 memset (son, 0, sizeof (ir_loop));
260 son->kind = k_ir_loop;
261 son->children = NEW_ARR_F (loop_element, 0);
265 son->outer_loop = father;
266 add_loop_son(father, son);
267 son->depth = father->depth+1;
268 } else { /* The root loop */
269 son->outer_loop = son;
278 /* Finishes the datastructures, copies the arrays to the obstack
280 A. Schoesser: Caution: loop -> sons is gone. */
281 static void mature_loop (ir_loop *loop) {
284 new_sons = NEW_ARR_D (ir_loop *, current_ir_graph->obst, ARR_LEN(loop->sons));
285 memcpy (new_sons, loop->sons, sizeof (ir_loop *) * ARR_LEN(loop->sons));
286 DEL_ARR_F(loop->sons);
287 loop->sons = new_sons;
291 /* Returns outer loop, itself if outermost. */
292 ir_loop *get_loop_outer_loop (ir_loop *loop) {
293 assert(loop && loop->kind == k_ir_loop);
294 return loop->outer_loop;
297 /* Returns nesting depth of this loop */
298 int get_loop_depth (ir_loop *loop) {
299 assert(loop); assert(loop->kind == k_ir_loop);
303 /* Returns the number of inner loops */
304 int get_loop_n_sons (ir_loop *loop) {
305 assert(loop && loop->kind == k_ir_loop);
306 return(loop -> n_sons);
309 /* Returns the pos`th loop_node-child *
310 * TODO: This method isn`t very efficient ! *
311 * Returns NULL if there isnt`t a pos`th loop_node */
312 ir_loop *get_loop_son (ir_loop *loop, int pos) {
313 int child_nr = 0, loop_nr = -1;
315 assert(loop && loop->kind == k_ir_loop);
316 while(child_nr < ARR_LEN(loop->children))
318 if(*(loop -> children[child_nr].kind) == k_ir_loop)
321 return(loop -> children[child_nr].son);
327 /* Use EXCLUSIVELY this function to add sons, otherwise the loop->n_sons
331 add_loop_son(ir_loop *loop, ir_loop *son) {
334 assert(loop && loop->kind == k_ir_loop);
335 assert(get_kind(son) == k_ir_loop);
336 ARR_APP1 (loop_element, loop->children, lson);
340 /* Returns the number of nodes in the loop */
341 int get_loop_n_nodes (ir_loop *loop) {
342 assert(loop); assert(loop->kind == k_ir_loop);
343 return loop -> n_nodes;
344 /* return ARR_LEN(loop->nodes); */
347 /* Returns the pos`th ir_node-child *
348 * TODO: This method isn`t very efficient ! *
349 * Returns NULL if there isnt`t a pos`th ir_node */
350 ir_node *get_loop_node (ir_loop *loop, int pos) {
351 int child_nr, node_nr = -1;
353 assert(loop && loop->kind == k_ir_loop);
354 assert(pos < get_loop_n_nodes(loop));
356 for (child_nr = 0; child_nr < ARR_LEN(loop->children); child_nr++) {
357 if(*(loop -> children[child_nr].kind) == k_ir_node)
360 return(loop -> children[child_nr].node);
362 assert(0 && "no child at pos found");
366 /* Use EXCLUSIVELY this function to add nodes, otherwise the loop->n_nodes
370 add_loop_node(ir_loop *loop, ir_node *n) {
373 assert(loop && loop->kind == k_ir_loop);
374 assert(get_kind(n) == k_ir_node);
375 ARR_APP1 (loop_element, loop->children, ln);
379 /** Returns the number of elements contained in loop. */
380 int get_loop_n_elements (ir_loop *loop) {
381 assert(loop && loop->kind == k_ir_loop);
382 return(ARR_LEN(loop->children));
386 Returns the pos`th loop element.
387 This may be a loop_node or a ir_node. The caller of this function has
388 to check the *(loop_element.kind) field for "k_ir_node" or "k_ir_loop"
389 and then select the apropriate "loop_element.node" or "loop_element.son".
392 loop_element get_loop_element (ir_loop *loop, int pos) {
393 assert(loop && loop->kind == k_ir_loop && pos < ARR_LEN(loop->children));
395 return(loop -> children[pos]);
398 /* The outermost loop is remarked in the surrounding graph. */
399 void set_irg_loop(ir_graph *irg, ir_loop *loop) {
403 ir_loop *get_irg_loop(ir_graph *irg) {
409 /**********************************************************************/
410 /* Constructing and destructing the loop/backedge information. **/
411 /**********************************************************************/
413 /* Initialization steps. **********************************************/
416 init_node (ir_node *n, void *env) {
417 set_irn_link (n, new_scc_info());
420 /* Also init nodes not visible in intraproc_view. */
421 /* @@@ init_node is called for too many nodes -- this wastes memory!.
422 The mem is not lost as its on the obstack. */
423 if (get_irn_op(n) == op_Filter) {
424 for (i = 0; i < get_Filter_n_cg_preds(n); i++)
425 init_node(get_Filter_cg_pred(n, i), NULL);
427 if (get_irn_op(n) == op_Block) {
428 for (i = 0; i < get_Block_cg_n_cfgpreds(n); i++) {
429 init_node(get_Block_cg_cfgpred(n, i), NULL);
432 /* The following pattern matches only after a call from above pattern. */
433 if ((get_irn_op(n) == op_Proj) /*&& (get_Proj_proj(n) == 0)*/) {
434 /* @@@ init_node is called for every proj -- this wastes memory!.
435 The mem is not lost as its on the obstack. */
436 ir_node *cb = get_Proj_pred(n);
437 if ((get_irn_op(cb) == op_CallBegin) ||
438 (get_irn_op(cb) == op_EndReg) ||
439 (get_irn_op(cb) == op_EndExcept)) {
441 init_node(get_nodes_Block(cb), NULL);
447 init_scc (ir_graph *irg) {
451 irg_walk_graph (irg, init_node, NULL, NULL);
453 irg_walk (irg, link_to_reg_end, NULL, NULL);
462 cg_walk (init_node, NULL, NULL);
466 Works, but is inefficient.
470 interprocedural_view = 1;
474 for (i = 0; i < get_irp_n_irgs(); i++) {
475 current_ir_graph = get_irp_irg(i);
476 irg_walk_graph (current_ir_graph, init_node, NULL, NULL);
477 /* @@@ decrease max_visited to avoide double walks */
482 /* Condition for breaking the recursion. */
483 static bool is_outermost_Start(ir_node *n) {
484 /* Test whether this is the outermost Start node. If so
485 recursion must end. */
486 if ((get_irn_op(n) == op_Block) &&
487 (get_Block_n_cfgpreds(n) == 1) &&
488 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Start) &&
489 (get_nodes_Block(skip_Proj(get_Block_cfgpred(n, 0))) == n)) {
493 /* @@@ Bad condition:
494 not possible in interprocedural view as outermost_graph is
495 not necessarily the only with a dead-end start block.
496 Besides current_ir_graph is not set properly. */
497 if ((get_irn_op(n) == op_Block) &&
498 (n == get_irg_start_block(current_ir_graph))) {
499 if ((!interprocedural_view) ||
500 (current_ir_graph == outermost_ir_graph))
507 /* Don't walk from nodes to blocks except for Control flow operations. */
509 get_start_index(ir_node *n) {
510 if (is_cfop(n) || is_fragile_op(n) || get_irn_op(n) == op_Start)
516 /* Returns current_ir_graph and set it to the irg of predecessor index
518 static INLINE ir_graph *
519 switch_irg (ir_node *n, int index) {
520 ir_graph *old_current = current_ir_graph;
522 if (interprocedural_view) {
523 /* Only Filter and Block nodes can have predecessors in other graphs. */
524 if (get_irn_op(n) == op_Filter)
525 n = get_nodes_Block(n);
526 if (get_irn_op(n) == op_Block) {
527 ir_node *cfop = skip_Proj(get_Block_cfgpred(n, index));
528 if (is_ip_cfop(cfop)) {
529 current_ir_graph = get_irn_irg(cfop);
530 set_irg_visited(current_ir_graph, get_max_irg_visited());
538 /* Walks up the stack passing n and then finding the node
539 where we walked into the irg n is contained in.
540 Here we switch the irg. */
542 find_irg_on_stack (ir_node *n) {
544 ir_graph *old_current = current_ir_graph;
547 if (interprocedural_view) {
548 for (i = tos; i >= 0; i--) {
549 if (stack[i] == n) break;
554 for (; i >= 0; i--) {
556 /*printf(" Visiting %d ", i); DDMN(m);*/
558 current_ir_graph = get_irn_irg(m);
561 if (get_irn_op(m) == op_Filter) {
562 /* Find the corresponding ip_cfop */
563 ir_node *pred = stack[i+1];
565 for (j = 0; j < get_Filter_n_cg_preds(m); j++)
566 if (get_Filter_cg_pred(m, j) == pred) break;
567 if (j >= get_Filter_n_cg_preds(m))
568 /* It is a filter we didn't pass as the predecessors are marked. */
570 assert(get_Filter_cg_pred(m, j) == pred);
581 static void test(ir_node *pred, ir_node *root, ir_node *this) {
583 if (get_irn_uplink(pred) >= get_irn_uplink(root)) return;
585 printf("this: %d ", get_irn_uplink(this)); DDMN(this);
586 printf("pred: %d ", get_irn_uplink(pred)); DDMN(pred);
587 printf("root: %d ", get_irn_uplink(root)); DDMN(root);
589 printf("tos: %d\n", tos);
591 for (i = tos; i >= 0; i--) {
592 ir_node *n = stack[i];
594 printf(" uplink: %d, pos: %d ", get_irn_uplink(n), i); DDMN(n);
599 /* Returns true if n is a loop header, i.e., it is a Block, Phi
600 or Filter node and has predecessors within the loop and out
603 is_head (ir_node *n, ir_node *root)
606 int some_outof_loop = 0, some_in_loop = 0;
608 /* Test for legal loop header */
609 if (!((get_irn_op(n) == op_Block) ||
610 (get_irn_op(n) == op_Phi) ||
611 ((get_irn_op(n) == op_Filter) && interprocedural_view)))
614 if (!is_outermost_Start(n)) {
615 for (i = get_start_index(n); i < get_irn_arity(n); i++) {
616 ir_node *pred = get_irn_n(n, i);
618 if (is_backedge(n, i)) continue;
619 if (!irn_is_in_stack(pred)) {
622 assert(get_irn_uplink(pred) >= get_irn_uplink(root));
627 return some_outof_loop && some_in_loop;
630 /* Returns index of the predecessor with the smallest dfn number
631 greater-equal than limit. */
633 smallest_dfn_pred (ir_node *n, int limit)
635 int i, index = -2, min = -1;
637 if (!is_outermost_Start(n)) {
638 for (i = get_start_index(n); i < get_irn_arity(n); i++) {
639 ir_node *pred = get_irn_n(n, i);
641 if (is_backedge(n, i) || !irn_is_in_stack(pred)) continue;
642 if (get_irn_dfn(pred) >= limit
643 && (min == -1 || get_irn_dfn(pred) < min)) {
645 min = get_irn_dfn(pred);
652 /* Returns index of the predecessor with the largest dfn number. */
654 largest_dfn_pred (ir_node *n)
656 int i, index = -2, max = -1;
658 if (!is_outermost_Start(n)) {
659 for (i = get_start_index(n); i < get_irn_arity(n); i++) {
660 ir_node *pred = get_irn_n(n, i);
661 if (is_backedge (n, i) || !irn_is_in_stack(pred)) continue;
662 if (get_irn_dfn(pred) > max) {
664 max = get_irn_dfn(pred);
671 /* Searches the stack for possible loop heads. Tests these for backedges.
672 If it finds a head with an unmarked backedge it marks this edge and
673 returns the tail of the loop.
674 If it finds no backedge returns NULL.
675 ("disable_backedge" in fiasco) */
678 find_tail (ir_node *n) {
680 int i, res_index = -2;
683 if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
686 m = stack[tos-1]; /* tos = top of stack */
687 if (is_head (m, n)) {
688 res_index = smallest_dfn_pred(m, 0);
689 if ((res_index == -2) && /* no smallest dfn pred found. */
693 if (m == n) return NULL;
694 for (i = tos-2; ; --i) {
696 if (is_head (m, n)) {
697 res_index = smallest_dfn_pred (m, get_irn_dfn(m) + 1);
698 if (res_index == -2) /* no smallest dfn pred found. */
699 res_index = largest_dfn_pred (m);
704 assert (res_index > -2);
706 set_backedge (m, res_index);
707 return is_outermost_Start(n) ? NULL : get_irn_n(m, res_index);
711 /* The core algorithm. *****************************************/
713 static void scc (ir_node *n) {
717 if (irn_visited(n)) return;
719 /*printf("mark: %d ", get_irn_visited(n)); DDMN(n);
720 DDME(get_irg_ent(current_ir_graph));*/
722 /* Initialize the node */
723 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
724 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
725 set_irn_loop_tmp(n, NULL);
728 /* What's this good for?
729 n->ana.scc.section = NULL;
734 if (!is_outermost_Start(n)) {
735 for (i = get_start_index(n); i < get_irn_arity(n); i++) {
737 if (is_backedge(n, i)) continue;
739 m = get_irn_n(n, i); /*get_irn_ip_pred(n, i);*/
740 if ((!m) || (get_irn_op(m) == op_Unknown)) continue;
742 /*return_recur(n, i);*/
744 if (irn_is_in_stack(m)) {
745 /* Uplink of m is smaller if n->m is a backedge.
746 Propagate the uplink to mark the loop. */
747 if (get_irn_uplink(m) < get_irn_uplink(n))
748 set_irn_uplink(n, get_irn_uplink(m));
752 if (get_irn_dfn(n) == get_irn_uplink(n)) {
753 /* This condition holds for the node with the incoming backedge. */
754 ir_node *tail = find_tail(n);
756 /* We found a new loop! */
757 ir_loop *l = new_loop();
759 /* Remove the loop from the stack ... */
760 pop_scc_unmark_visit (n);
761 /* and recompute it in a better order; and so that it goes into
763 rem = find_irg_on_stack(tail);
766 current_ir_graph = rem;
768 assert (irn_visited(n));
771 /* current_loop = l; AS: This is done close_loop */
778 /* Constructs backedge information for irg. In interprocedural view constructs
779 backedges for all methods called by irg, too. */
780 void construct_backedges(ir_graph *irg) {
781 ir_graph *rem = current_ir_graph;
785 assert(!interprocedural_view &&
786 "not implemented, use construct_ip_backedges");
788 current_ir_graph = irg;
789 outermost_ir_graph = irg;
794 new_loop(); /* sets current_loop */
795 head_rem = current_loop; /* Just for assertion */
797 if (interprocedural_view) {
798 set_irg_visited(irg, inc_max_irg_visited());
801 inc_irg_visited(irg);
804 scc(get_irg_end(irg));
805 for (i = 0; i < get_End_n_keepalives(get_irg_end(irg)); i++)
806 scc(get_End_keepalive(get_irg_end(irg), i));
808 if (interprocedural_view) finish_ip_walk();
810 assert(head_rem == current_loop);
811 set_irg_loop(irg, current_loop);
812 assert(get_irg_loop(irg)->kind == k_ir_loop);
814 irg->loops = current_loop;
818 count_loop (the_loop, &count, &depth);
822 current_ir_graph = rem;
827 void construct_ip_backedges (void) {
828 ir_graph *rem = current_ir_graph;
829 int rem_ipv = interprocedural_view;
832 outermost_ir_graph = get_irp_main_irg();
837 new_loop(); /* sets current_loop */
838 interprocedural_view = 1;
840 inc_max_irg_visited();
841 for (i = 0; i < get_irp_n_irgs(); i++)
842 set_irg_visited(get_irp_irg(i), get_max_irg_visited());
844 for (i = 0; i < get_irp_n_irgs(); i++) {
846 current_ir_graph = get_irp_irg(i);
847 /*DDME(get_irg_ent(current_ir_graph));*/
848 /* Find real entry points */
849 sb = get_irg_start_block(current_ir_graph);
850 if ((get_Block_n_cfgpreds(sb) > 1) ||
851 (get_nodes_Block(get_Block_cfgpred(sb, 0)) != sb)) continue;
852 /* printf("running scc for "); DDME(get_irg_ent(current_ir_graph)); */
853 /* Compute scc for this graph */
854 outermost_ir_graph = current_ir_graph;
855 set_irg_visited(outermost_ir_graph, get_max_irg_visited());
856 scc(get_irg_end(current_ir_graph));
857 for (j = 0; j < get_End_n_keepalives(get_irg_end(outermost_ir_graph)); j++)
858 scc(get_End_keepalive(get_irg_end(outermost_ir_graph), j));
861 set_irg_loop(outermost_ir_graph, current_loop);
862 assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
864 current_ir_graph = rem;
865 interprocedural_view = rem_ipv;