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.
18 #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) {
307 assert(loop && loop->kind == k_ir_loop);
308 assert(get_kind(son) == k_ir_loop);
309 ARR_APP1 (loop_element, loop->children, lson);
313 /* Returns the number of nodes in the loop */
314 int get_loop_n_nodes (ir_loop *loop) {
315 assert(loop); assert(loop->kind == k_ir_loop);
316 return loop -> n_nodes;
317 /* return ARR_LEN(loop->nodes); */
320 /* Returns the pos`th ir_node-child *
321 * TODO: This method isn`t very efficient ! *
322 * Returns NULL if there isnt`t a pos`th ir_node */
323 ir_node *get_loop_node (ir_loop *loop, int pos) {
324 int child_nr, node_nr = -1;
326 assert(loop && loop->kind == k_ir_loop);
327 assert(pos < get_loop_n_nodes(loop));
329 for (child_nr = 0; child_nr < ARR_LEN(loop->children); child_nr++) {
330 if(*(loop -> children[child_nr].kind) == k_ir_node)
333 return(loop -> children[child_nr].node);
335 assert(0 && "no child at pos found");
339 /* Use EXCLUSIVELY this function to add nodes, otherwise the loop->n_nodes
343 add_loop_node(ir_loop *loop, ir_node *n) {
346 assert(loop && loop->kind == k_ir_loop);
347 assert(get_kind(n) == k_ir_node);
348 ARR_APP1 (loop_element, loop->children, ln);
352 /** Returns the number of elements contained in loop. */
353 int get_loop_n_elements (ir_loop *loop) {
354 assert(loop && loop->kind == k_ir_loop);
355 return(ARR_LEN(loop->children));
359 Returns the pos`th loop element.
360 This may be a loop_node or a ir_node. The caller of this function has
361 to check the *(loop_element.kind) field for "k_ir_node" or "k_ir_loop"
362 and then select the apropriate "loop_element.node" or "loop_element.son".
365 loop_element get_loop_element (ir_loop *loop, int pos) {
366 assert(loop && loop->kind == k_ir_loop && pos < ARR_LEN(loop->children));
368 return(loop -> children[pos]);
371 /* The outermost loop is remarked in the surrounding graph. */
372 void set_irg_loop(ir_graph *irg, ir_loop *loop) {
376 ir_loop *get_irg_loop(ir_graph *irg) {
381 /**********************************************************************/
382 /* Constructing and destructing the loop/backedge information. **/
383 /**********************************************************************/
385 /* Initialization steps. **********************************************/
388 init_node (ir_node *n, void *env) {
389 set_irn_link (n, new_scc_info());
392 /* Also init nodes not visible in intraproc_view. */
393 /* @@@ init_node is called for too many nodes -- this wastes memory!.
394 The mem is not lost as its on the obstack. */
395 if (get_irn_op(n) == op_Filter) {
396 for (i = 0; i < get_Filter_n_cg_preds(n); i++)
397 init_node(get_Filter_cg_pred(n, i), NULL);
399 if (get_irn_op(n) == op_Block) {
400 for (i = 0; i < get_Block_cg_n_cfgpreds(n); i++) {
401 init_node(get_Block_cg_cfgpred(n, i), NULL);
404 /* The following pattern matches only after a call from above pattern. */
405 if ((get_irn_op(n) == op_Proj) /*&& (get_Proj_proj(n) == 0)*/) {
406 /* @@@ init_node is called for every proj -- this wastes memory!.
407 The mem is not lost as its on the obstack. */
408 ir_node *cb = get_Proj_pred(n);
409 if ((get_irn_op(cb) == op_CallBegin) ||
410 (get_irn_op(cb) == op_EndReg) ||
411 (get_irn_op(cb) == op_EndExcept)) {
413 init_node(get_nodes_Block(cb), NULL);
419 init_scc (ir_graph *irg) {
423 irg_walk_graph (irg, init_node, NULL, NULL);
425 irg_walk (irg, link_to_reg_end, NULL, NULL);
434 cg_walk (init_node, NULL, NULL);
438 Works, but is inefficient.
442 interprocedural_view = 1;
446 for (i = 0; i < get_irp_n_irgs(); i++) {
447 current_ir_graph = get_irp_irg(i);
448 irg_walk_graph (current_ir_graph, init_node, NULL, NULL);
449 /* @@@ decrease max_visited to avoide double walks */
454 /* Condition for breaking the recursion. */
455 static bool is_outermost_Start(ir_node *n) {
456 /* Test whether this is the outermost Start node. If so
457 recursion must end. */
458 if ((get_irn_op(n) == op_Block) &&
459 (get_Block_n_cfgpreds(n) == 1) &&
460 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Start) &&
461 (get_nodes_Block(skip_Proj(get_Block_cfgpred(n, 0))) == n)) {
465 /* @@@ Bad condition:
466 not possible in interprocedural view as outermost_graph is
467 not necessarily the only with a dead-end start block.
468 Besides current_ir_graph is not set properly. */
469 if ((get_irn_op(n) == op_Block) &&
470 (n == get_irg_start_block(current_ir_graph))) {
471 if ((!interprocedural_view) ||
472 (current_ir_graph == outermost_ir_graph))
479 /* Don't walk from nodes to blocks except for Control flow operations. */
481 get_start_index(ir_node *n) {
482 if (is_cfop(n) || is_fragile_op(n) || get_irn_op(n) == op_Start)
488 /* Returns current_ir_graph and set it to the irg of predecessor index
490 static INLINE ir_graph *
491 switch_irg (ir_node *n, int index) {
492 ir_graph *old_current = current_ir_graph;
494 if (interprocedural_view) {
495 /* Only Filter and Block nodes can have predecessors in other graphs. */
496 if (get_irn_op(n) == op_Filter)
497 n = get_nodes_Block(n);
498 if (get_irn_op(n) == op_Block) {
499 ir_node *cfop = skip_Proj(get_Block_cfgpred(n, index));
500 if (is_ip_cfop(cfop)) {
501 current_ir_graph = get_irn_irg(cfop);
502 set_irg_visited(current_ir_graph, get_max_irg_visited());
510 /* Walks up the stack passing n and then finding the node
511 where we walked into the irg n is contained in.
512 Here we switch the irg. */
514 find_irg_on_stack (ir_node *n) {
516 ir_graph *old_current = current_ir_graph;
519 if (interprocedural_view) {
520 for (i = tos; i >= 0; i--) {
521 if (stack[i] == n) break;
526 for (; i >= 0; i--) {
528 /*printf(" Visiting %d ", i); DDMN(m);*/
530 current_ir_graph = get_irn_irg(m);
533 if (get_irn_op(m) == op_Filter) {
534 /* Find the corresponding ip_cfop */
535 ir_node *pred = stack[i+1];
537 for (j = 0; j < get_Filter_n_cg_preds(m); j++)
538 if (get_Filter_cg_pred(m, j) == pred) break;
539 if (j >= get_Filter_n_cg_preds(m))
540 /* It is a filter we didn't pass as the predecessors are marked. */
542 assert(get_Filter_cg_pred(m, j) == pred);
553 static void test(ir_node *pred, ir_node *root, ir_node *this) {
555 if (get_irn_uplink(pred) >= get_irn_uplink(root)) return;
557 printf("this: %d ", get_irn_uplink(this)); DDMN(this);
558 printf("pred: %d ", get_irn_uplink(pred)); DDMN(pred);
559 printf("root: %d ", get_irn_uplink(root)); DDMN(root);
561 printf("tos: %d\n", tos);
563 for (i = tos; i >= 0; i--) {
564 ir_node *n = stack[i];
566 printf(" uplink: %d, pos: %d ", get_irn_uplink(n), i); DDMN(n);
571 /* Returns true if n is a loop header, i.e., it is a Block, Phi
572 or Filter node and has predecessors within the loop and out
575 is_head (ir_node *n, ir_node *root)
578 int some_outof_loop = 0, some_in_loop = 0;
580 /* Test for legal loop header */
581 if (!((get_irn_op(n) == op_Block) ||
582 (get_irn_op(n) == op_Phi) ||
583 ((get_irn_op(n) == op_Filter) && interprocedural_view)))
586 if (!is_outermost_Start(n)) {
587 for (i = get_start_index(n); i < get_irn_arity(n); i++) {
588 ir_node *pred = get_irn_n(n, i);
590 if (is_backedge(n, i)) continue;
591 if (!irn_is_in_stack(pred)) {
594 assert(get_irn_uplink(pred) >= get_irn_uplink(root));
599 return some_outof_loop && some_in_loop;
602 /* Returns index of the predecessor with the smallest dfn number
603 greater-equal than limit. */
605 smallest_dfn_pred (ir_node *n, int limit)
607 int i, index = -2, min = -1;
609 if (!is_outermost_Start(n)) {
610 for (i = get_start_index(n); i < get_irn_arity(n); i++) {
611 ir_node *pred = get_irn_n(n, i);
613 if (is_backedge(n, i) || !irn_is_in_stack(pred)) continue;
614 if (get_irn_dfn(pred) >= limit
615 && (min == -1 || get_irn_dfn(pred) < min)) {
617 min = get_irn_dfn(pred);
624 /* Returns index of the predecessor with the largest dfn number. */
626 largest_dfn_pred (ir_node *n)
628 int i, index = -2, max = -1;
630 if (!is_outermost_Start(n)) {
631 for (i = get_start_index(n); i < get_irn_arity(n); i++) {
632 ir_node *pred = get_irn_n(n, i);
633 if (is_backedge (n, i) || !irn_is_in_stack(pred)) continue;
634 if (get_irn_dfn(pred) > max) {
636 max = get_irn_dfn(pred);
643 /* Searches the stack for possible loop heads. Tests these for backedges.
644 If it finds a head with an unmarked backedge it marks this edge and
645 returns the tail of the loop.
646 If it finds no backedge returns NULL. */
648 find_tail (ir_node *n) {
650 int i, res_index = -2;
653 if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
657 if (is_head (m, n)) {
658 res_index = smallest_dfn_pred(m, 0);
659 if ((res_index == -2) && /* no smallest dfn pred found. */
663 if (m == n) return NULL;
664 for (i = tos-2; ; --i) {
666 if (is_head (m, n)) {
667 res_index = smallest_dfn_pred (m, get_irn_dfn(m) + 1);
668 if (res_index == -2) /* no smallest dfn pred found. */
669 res_index = largest_dfn_pred (m);
674 assert (res_index > -2);
676 set_backedge (m, res_index);
677 return is_outermost_Start(n) ? NULL : get_irn_n(m, res_index);
681 /* The core algorithm. *****************************************/
683 static void scc (ir_node *n) {
687 if (irn_visited(n)) return;
689 /*printf("mark: %d ", get_irn_visited(n)); DDMN(n);
690 DDME(get_irg_ent(current_ir_graph));*/
692 /* Initialize the node */
693 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
694 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
695 set_irn_loop_tmp(n, NULL);
698 /* What's this good for?
699 n->ana.scc.section = NULL;
704 if (!is_outermost_Start(n)) {
705 for (i = get_start_index(n); i < get_irn_arity(n); i++) {
707 if (is_backedge(n, i)) continue;
709 m = get_irn_n(n, i); /*get_irn_ip_pred(n, i);*/
710 if ((!m) || (get_irn_op(m) == op_Unknown)) continue;
712 /*return_recur(n, i);*/
714 if (irn_is_in_stack(m)) {
715 /* Uplink of m is smaller if n->m is a backedge.
716 Propagate the uplink to mark the loop. */
717 if (get_irn_uplink(m) < get_irn_uplink(n))
718 set_irn_uplink(n, get_irn_uplink(m));
722 if (get_irn_dfn(n) == get_irn_uplink(n)) {
723 /* This condition holds for the node with the incoming backedge. */
724 ir_node *tail = find_tail(n);
726 /* We found a new loop! */
727 ir_loop *l = new_loop();
728 /* Remove the loop from the stack ... */
729 pop_scc_unmark_visit (n);
730 /* and recompute it in a better order; and so that it goes into
732 rem = find_irg_on_stack(tail);
734 current_ir_graph = rem;
736 assert (irn_visited(n));
745 /* Constructs backedge information for irg. In interprocedural view constructs
746 backedges for all methods called by irg, too. */
747 void construct_backedges(ir_graph *irg) {
748 ir_graph *rem = current_ir_graph;
752 assert(!interprocedural_view &&
753 "not implemented, use construct_ip_backedges");
755 current_ir_graph = irg;
756 outermost_ir_graph = irg;
761 new_loop(); /* sets current_loop */
762 head_rem = current_loop; /* Just for assertion */
764 if (interprocedural_view) {
765 set_irg_visited(irg, inc_max_irg_visited());
768 inc_irg_visited(irg);
771 scc(get_irg_end(irg));
772 for (i = 0; i < get_End_n_keepalives(get_irg_end(irg)); i++)
773 scc(get_End_keepalive(get_irg_end(irg), i));
775 if (interprocedural_view) finish_ip_walk();
777 assert(head_rem == current_loop);
778 set_irg_loop(irg, current_loop);
779 assert(get_irg_loop(irg)->kind == k_ir_loop);
781 irg->loops = current_loop;
785 count_loop (the_loop, &count, &depth);
789 current_ir_graph = rem;
794 void construct_ip_backedges (void) {
795 ir_graph *rem = current_ir_graph;
796 int rem_ipv = interprocedural_view;
799 outermost_ir_graph = get_irp_main_irg();
804 new_loop(); /* sets current_loop */
805 interprocedural_view = 1;
807 inc_max_irg_visited();
808 for (i = 0; i < get_irp_n_irgs(); i++)
809 set_irg_visited(get_irp_irg(i), get_max_irg_visited());
811 for (i = 0; i < get_irp_n_irgs(); i++) {
813 current_ir_graph = get_irp_irg(i);
814 /*DDME(get_irg_ent(current_ir_graph));*/
815 /* Find real entry points */
816 sb = get_irg_start_block(current_ir_graph);
817 if ((get_Block_n_cfgpreds(sb) > 1) ||
818 (get_nodes_Block(get_Block_cfgpred(sb, 0)) != sb)) continue;
819 /* printf("running scc for "); DDME(get_irg_ent(current_ir_graph)); */
820 /* Compute scc for this graph */
821 outermost_ir_graph = current_ir_graph;
822 set_irg_visited(outermost_ir_graph, get_max_irg_visited());
823 scc(get_irg_end(current_ir_graph));
824 for (j = 0; j < get_End_n_keepalives(get_irg_end(outermost_ir_graph)); j++)
825 scc(get_End_keepalive(get_irg_end(outermost_ir_graph), j));
828 set_irg_loop(outermost_ir_graph, current_loop);
829 assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
831 current_ir_graph = rem;
832 interprocedural_view = rem_ipv;