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);
232 son->outer_loop = father;
233 add_loop_son(father, son);
234 son->depth = father->depth+1;
235 } else { /* The root loop */
236 son->outer_loop = son;
245 /* Finishes the datastructures, copies the arrays to the obstack
246 of current_ir_graph. */
247 static void mature_loop (ir_loop *loop) {
250 new_sons = NEW_ARR_D (ir_loop *, current_ir_graph->obst, ARR_LEN(loop->sons));
251 memcpy (new_sons, loop->sons, sizeof (ir_loop *) * ARR_LEN(loop->sons));
252 DEL_ARR_F(loop->sons);
253 loop->sons = new_sons;
257 /* Returns outer loop, itself if outermost. */
258 ir_loop *get_loop_outer_loop (ir_loop *loop) {
259 assert(loop && loop->kind == k_ir_loop);
260 return loop->outer_loop;
263 /* Returns nesting depth of this loop */
264 int get_loop_depth (ir_loop *loop) {
265 assert(loop); assert(loop->kind == k_ir_loop);
269 /* Returns the number of inner loops */
270 int get_loop_n_sons (ir_loop *loop) {
271 assert(loop && loop->kind == k_ir_loop);
272 return ARR_LEN(loop->sons);
274 ir_loop *get_loop_son (ir_loop *loop, int pos) {
275 assert(loop && loop->kind == k_ir_loop);
276 return loop->sons[pos];
279 add_loop_son(ir_loop *loop, ir_loop *son) {
280 assert(loop && loop->kind == k_ir_loop);
281 ARR_APP1 (ir_loop *, loop->sons, son);
284 /* Returns the number of nodes in the loop */
285 int get_loop_n_nodes (ir_loop *loop) {
286 assert(loop); assert(loop->kind == k_ir_loop);
287 return ARR_LEN(loop->nodes);
289 ir_node *get_loop_node (ir_loop *loop, int pos) {
290 assert(loop && loop->kind == k_ir_loop);
291 return loop->nodes[pos];
294 add_loop_node(ir_loop *loop, ir_node *n) {
295 assert(loop && loop->kind == k_ir_loop);
296 ARR_APP1 (ir_node *, loop->nodes, n);
299 /* The outermost loop is remarked in the surrounding graph. */
300 void set_irg_loop(ir_graph *irg, ir_loop *loop) {
304 ir_loop *get_irg_loop(ir_graph *irg) {
309 /**********************************************************************/
310 /* Constructing and destructing the loop/backedge information. **/
311 /**********************************************************************/
313 /* Initialization steps. **********************************************/
316 init_node (ir_node *n, void *env) {
317 set_irn_link (n, new_scc_info());
320 /* Also init nodes not visible in intraproc_view. */
321 /* @@@ init_node is called for too many nodes -- this wastes memory!.
322 The mem is not lost as its on the obstack. */
323 if (get_irn_op(n) == op_Filter) {
324 for (i = 0; i < get_Filter_n_cg_preds(n); i++)
325 init_node(get_Filter_cg_pred(n, i), NULL);
327 if (get_irn_op(n) == op_Block) {
328 for (i = 0; i < get_Block_cg_n_cfgpreds(n); i++) {
329 init_node(get_Block_cg_cfgpred(n, i), NULL);
332 /* The following pattern matches only after a call from above pattern. */
333 if ((get_irn_op(n) == op_Proj) /*&& (get_Proj_proj(n) == 0)*/) {
334 /* @@@ init_node is called for every proj -- this wastes memory!.
335 The mem is not lost as its on the obstack. */
336 ir_node *cb = get_Proj_pred(n);
337 if ((get_irn_op(cb) == op_CallBegin) ||
338 (get_irn_op(cb) == op_EndReg) ||
339 (get_irn_op(cb) == op_EndExcept)) {
341 init_node(get_nodes_Block(cb), NULL);
347 init_scc (ir_graph *irg) {
351 irg_walk_graph (irg, init_node, NULL, NULL);
353 irg_walk (irg, link_to_reg_end, NULL, NULL);
362 cg_walk (init_node, NULL, NULL);
366 Works, but is inefficient.
370 interprocedural_view = 1;
374 for (i = 0; i < get_irp_n_irgs(); i++) {
375 current_ir_graph = get_irp_irg(i);
376 irg_walk_graph (current_ir_graph, init_node, NULL, NULL);
377 /* @@@ decrease max_visited to avoide double walks */
382 /* Condition for breaking the recursion. */
383 static bool is_outermost_Start(ir_node *n) {
384 /* Test whether this is the outermost Start node. If so
385 recursion must end. */
386 if ((get_irn_op(n) == op_Block) &&
387 (get_Block_n_cfgpreds(n) == 1) &&
388 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Start) &&
389 (get_nodes_Block(skip_Proj(get_Block_cfgpred(n, 0))) == n)) {
393 /* @@@ Bad condition:
394 not possible in interprocedural view as outermost_graph is
395 not necessarily the only with a dead-end start block.
396 Besides current_ir_graph is not set properly. */
397 if ((get_irn_op(n) == op_Block) &&
398 (n == get_irg_start_block(current_ir_graph))) {
399 if ((!interprocedural_view) ||
400 (current_ir_graph == outermost_ir_graph))
407 /* Don't walk from nodes to blocks except for Control flow operations. */
409 get_start_index(ir_node *n) {
410 if (is_cfop(n) || is_fragile_op(n) || get_irn_op(n) == op_Start)
416 /* Returns current_ir_graph and set it to the irg of predecessor index
418 static INLINE ir_graph *
419 switch_irg (ir_node *n, int index) {
420 ir_graph *old_current = current_ir_graph;
422 if (interprocedural_view) {
423 /* Only Filter and Block nodes can have predecessors in other graphs. */
424 if (get_irn_op(n) == op_Filter)
425 n = get_nodes_Block(n);
426 if (get_irn_op(n) == op_Block) {
427 ir_node *cfop = skip_Proj(get_Block_cfgpred(n, index));
428 if (is_ip_cfop(cfop)) {
429 current_ir_graph = get_irn_irg(cfop);
430 set_irg_visited(current_ir_graph, get_max_irg_visited());
438 /* Walks up the stack passing n and then finding the node
439 where we walked into the irg n is contained in.
440 Here we switch the irg. */
442 find_irg_on_stack (ir_node *n) {
444 ir_graph *old_current = current_ir_graph;
447 if (interprocedural_view) {
448 for (i = tos; i >= 0; i--) {
449 if (stack[i] == n) break;
456 for (; i >= 0; i--) {
458 //printf(" Visiting %d ", i); DDMN(m);
460 current_ir_graph = get_irn_irg(m);
463 if (get_irn_op(m) == op_Filter) {
464 /* Find the corresponding ip_cfop */
465 ir_node *pred = stack[i+1];
467 for (j = 0; j < get_Filter_n_cg_preds(m); j++)
468 if (get_Filter_cg_pred(m, j) == pred) break;
469 if (j >= get_Filter_n_cg_preds(m))
470 /* It is a filter we didn't pass as the predecessors are marked. */
472 assert(get_Filter_cg_pred(m, j) == pred);
483 static void test(ir_node *pred, ir_node *root, ir_node *this) {
485 if (get_irn_uplink(pred) >= get_irn_uplink(root)) return;
487 printf("this: %d ", get_irn_uplink(this)); DDMN(this);
488 printf("pred: %d ", get_irn_uplink(pred)); DDMN(pred);
489 printf("root: %d ", get_irn_uplink(root)); DDMN(root);
491 printf("tos: %d\n", tos);
493 for (i = tos; i >= 0; i--) {
494 ir_node *n = stack[i];
496 printf(" uplink: %d, pos: %d ", get_irn_uplink(n), i); DDMN(n);
501 /* Returns true if n is a loop header, i.e., it is a Block, Phi
502 or Filter node and has predecessors within the loop and out
505 is_head (ir_node *n, ir_node *root)
508 int some_outof_loop = 0, some_in_loop = 0;
510 /* Test for legal loop header */
511 if (!((get_irn_op(n) == op_Block) ||
512 (get_irn_op(n) == op_Phi) ||
513 ((get_irn_op(n) == op_Filter) && interprocedural_view)))
516 if (!is_outermost_Start(n)) {
517 for (i = get_start_index(n); i < get_irn_arity(n); i++) {
518 ir_node *pred = get_irn_n(n, i);
520 if (is_backedge(n, i)) continue;
521 if (!irn_is_in_stack(pred)) {
524 assert(get_irn_uplink(pred) >= get_irn_uplink(root));
529 return some_outof_loop && some_in_loop;
532 /* Returns index of the predecessor with the smallest dfn number
533 greater-equal than limit. */
535 smallest_dfn_pred (ir_node *n, int limit)
537 int i, index = -2, min = -1;
539 if (!is_outermost_Start(n)) {
540 for (i = get_start_index(n); i < get_irn_arity(n); i++) {
541 ir_node *pred = get_irn_n(n, i);
543 if (is_backedge(n, i) || !irn_is_in_stack(pred)) continue;
544 if (get_irn_dfn(pred) >= limit
545 && (min == -1 || get_irn_dfn(pred) < min)) {
547 min = get_irn_dfn(pred);
554 /* Returns index of the predecessor with the largest dfn number. */
556 largest_dfn_pred (ir_node *n)
558 int i, index = -2, max = -1;
560 if (!is_outermost_Start(n)) {
561 for (i = get_start_index(n); i < get_irn_arity(n); i++) {
562 ir_node *pred = get_irn_n(n, i);
563 if (is_backedge (n, i) || !irn_is_in_stack(pred)) continue;
564 if (get_irn_dfn(pred) > max) {
566 max = get_irn_dfn(pred);
573 /* Searches the stack for possible loop heads. Tests these for backedges.
574 If it finds a head with an unmarked backedge it marks this edge and
575 returns the tail of the loop.
576 If it finds no backedge returns NULL. */
578 find_tail (ir_node *n) {
580 int i, res_index = -2;
583 if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
587 if (is_head (m, n)) {
588 res_index = smallest_dfn_pred(m, 0);
589 if ((res_index == -2) && /* no smallest dfn pred found. */
593 if (m == n) return NULL;
594 for (i = tos-2; ; --i) {
596 if (is_head (m, n)) {
597 res_index = smallest_dfn_pred (m, get_irn_dfn(m) + 1);
598 if (res_index == -2) /* no smallest dfn pred found. */
599 res_index = largest_dfn_pred (m);
604 assert (res_index > -2);
606 set_backedge (m, res_index);
607 return is_outermost_Start(n) ? NULL : get_irn_n(m, res_index);
611 /* The core algorithm. *****************************************/
613 static void scc (ir_node *n) {
617 if (irn_visited(n)) return;
619 //printf("mark: %d ", get_irn_visited(n)); DDMN(n);
620 //DDME(get_irg_ent(current_ir_graph));
622 /* Initialize the node */
623 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
624 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
625 set_irn_loop_tmp(n, NULL);
628 /* What's this good for?
629 n->ana.scc.section = NULL;
634 if (!is_outermost_Start(n)) {
635 for (i = get_start_index(n); i < get_irn_arity(n); i++) {
637 if (is_backedge(n, i)) continue;
639 m = get_irn_n(n, i); //get_irn_ip_pred(n, i);
640 if ((!m) || (get_irn_op(m) == op_Unknown)) continue;
642 //return_recur(n, i);
644 if (irn_is_in_stack(m)) {
645 /* Uplink of m is smaller if n->m is a backedge.
646 Propagate the uplink to mark the loop. */
647 if (get_irn_uplink(m) < get_irn_uplink(n))
648 set_irn_uplink(n, get_irn_uplink(m));
652 if (get_irn_dfn(n) == get_irn_uplink(n)) {
653 /* This condition holds for the node with the incoming backedge. */
654 ir_node *tail = find_tail(n);
656 /* We found a new loop! */
657 ir_loop *l = new_loop();
658 /* Remove the loop from the stack ... */
659 pop_scc_unmark_visit (n);
660 /* and recompute it in a better order; and so that it goes into
662 rem = find_irg_on_stack(tail);
664 current_ir_graph = rem;
666 assert (irn_visited(n));
675 /* Constructs backedge information for irg. In interprocedural view constructs
676 backedges for all methods called by irg, too. */
677 void construct_backedges(ir_graph *irg) {
678 ir_graph *rem = current_ir_graph;
682 assert(!interprocedural_view &&
683 "not implemented, use construct_ip_backedges");
685 current_ir_graph = irg;
686 outermost_ir_graph = irg;
691 new_loop(); /* sets current_loop */
692 head_rem = current_loop; /* Just for assertion */
694 if (interprocedural_view) {
695 set_irg_visited(irg, inc_max_irg_visited());
698 inc_irg_visited(irg);
701 scc(get_irg_end(irg));
702 for (i = 0; i < get_End_n_keepalives(get_irg_end(irg)); i++)
703 scc(get_End_keepalive(get_irg_end(irg), i));
705 if (interprocedural_view) finish_ip_walk();
707 assert(head_rem == current_loop);
708 set_irg_loop(irg, current_loop);
709 assert(get_irg_loop(irg)->kind == k_ir_loop);
711 irg->loops = current_loop;
715 count_loop (the_loop, &count, &depth);
719 current_ir_graph = rem;
724 void construct_ip_backedges (void) {
725 ir_graph *rem = current_ir_graph;
726 int rem_ipv = interprocedural_view;
729 outermost_ir_graph = get_irp_main_irg();
734 new_loop(); /* sets current_loop */
735 interprocedural_view = 1;
737 inc_max_irg_visited();
738 for (i = 0; i < get_irp_n_irgs(); i++)
739 set_irg_visited(get_irp_irg(i), get_max_irg_visited());
741 for (i = 0; i < get_irp_n_irgs(); i++) {
743 current_ir_graph = get_irp_irg(i);
744 //DDME(get_irg_ent(current_ir_graph));
745 /* Find real entry points */
746 sb = get_irg_start_block(current_ir_graph);
747 if ((get_Block_n_cfgpreds(sb) > 1) ||
748 (get_nodes_Block(get_Block_cfgpred(sb, 0)) != sb)) continue;
749 // printf("running scc for "); DDME(get_irg_ent(current_ir_graph));
750 /* Compute scc for this graph */
751 outermost_ir_graph = current_ir_graph;
752 set_irg_visited(outermost_ir_graph, get_max_irg_visited());
753 scc(get_irg_end(current_ir_graph));
754 for (j = 0; j < get_End_n_keepalives(get_irg_end(outermost_ir_graph)); j++)
755 scc(get_End_keepalive(get_irg_end(outermost_ir_graph), j));
758 set_irg_loop(outermost_ir_graph, current_loop);
759 assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
761 current_ir_graph = rem;
762 interprocedural_view = rem_ipv;