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)
198 set_irn_dfn(m, loop_node_cnt);
200 add_loop_node(current_loop, m);
201 set_irn_loop_tmp(m, current_loop);
206 /* Removes and unmarks all nodes up to n from the stack.
207 The nodes must be visited once more to assign them to a scc. */
209 pop_scc_unmark_visit (ir_node *n)
215 set_irn_visited(m, 0);
219 /**********************************************************************/
220 /* The loop datastructure. **/
221 /**********************************************************************/
223 /* Allocates a new loop as son of current_loop. Sets current_loop
224 to the new loop and returns the father. */
225 static ir_loop *new_loop (void) {
226 ir_loop *father, *son;
228 father = current_loop;
230 son = (ir_loop *) obstack_alloc (outermost_ir_graph->obst, sizeof (ir_loop));
231 memset (son, 0, sizeof (ir_loop));
232 son->kind = k_ir_loop;
233 /* son->sons = NEW_ARR_F (ir_loop *, 0);
234 son->nodes = NEW_ARR_F (ir_node *, 0);
235 A. Schoesser: Removed, because array children was introduced,
236 which contains both, nodes AND sons.
237 This comment may be removed after beeing read by all important persons :) */
238 son->children = NEW_ARR_F (loop_element, 0);
242 son->outer_loop = father;
243 add_loop_son(father, son);
244 son->depth = father->depth+1;
245 } else { /* The root loop */
246 son->outer_loop = son;
255 /* Finishes the datastructures, copies the arrays to the obstack
257 A. Schoesser: Caution: loop -> sons is gone. */
258 static void mature_loop (ir_loop *loop) {
261 new_sons = NEW_ARR_D (ir_loop *, current_ir_graph->obst, ARR_LEN(loop->sons));
262 memcpy (new_sons, loop->sons, sizeof (ir_loop *) * ARR_LEN(loop->sons));
263 DEL_ARR_F(loop->sons);
264 loop->sons = new_sons;
268 /* Returns outer loop, itself if outermost. */
269 ir_loop *get_loop_outer_loop (ir_loop *loop) {
270 assert(loop && loop->kind == k_ir_loop);
271 return loop->outer_loop;
274 /* Returns nesting depth of this loop */
275 int get_loop_depth (ir_loop *loop) {
276 assert(loop); assert(loop->kind == k_ir_loop);
280 /* Returns the number of inner loops */
281 int get_loop_n_sons (ir_loop *loop) {
282 assert(loop && loop->kind == k_ir_loop);
283 return(loop -> n_sons);
286 /* Returns the pos`th loop_node-child *
287 * TODO: This method isn`t very efficient ! *
288 * Returns NULL if there isnt`t a pos`th loop_node */
289 ir_loop *get_loop_son (ir_loop *loop, int pos) {
290 int child_nr = 0, loop_nr = -1;
292 assert(loop && loop->kind == k_ir_loop);
293 while(child_nr < ARR_LEN(loop->children))
295 if(*(loop -> children[child_nr].kind) == k_ir_loop)
298 return(loop -> children[child_nr].son);
304 /* Use EXCLUSIVELY this function to add sons, otherwise the loop->n_sons
308 add_loop_son(ir_loop *loop, ir_loop *son) {
311 assert(loop && loop->kind == k_ir_loop);
312 assert(get_kind(son) == k_ir_loop);
313 ARR_APP1 (loop_element, loop->children, lson);
317 /* Returns the number of nodes in the loop */
318 int get_loop_n_nodes (ir_loop *loop) {
319 assert(loop); assert(loop->kind == k_ir_loop);
320 return loop -> n_nodes;
321 /* return ARR_LEN(loop->nodes); */
324 /* Returns the pos`th ir_node-child *
325 * TODO: This method isn`t very efficient ! *
326 * Returns NULL if there isnt`t a pos`th ir_node */
327 ir_node *get_loop_node (ir_loop *loop, int pos) {
328 int child_nr, node_nr = -1;
330 assert(loop && loop->kind == k_ir_loop);
331 assert(pos < get_loop_n_nodes(loop));
333 for (child_nr = 0; child_nr < ARR_LEN(loop->children); child_nr++) {
334 if(*(loop -> children[child_nr].kind) == k_ir_node)
337 return(loop -> children[child_nr].node);
339 assert(0 && "no child at pos found");
343 /* Use EXCLUSIVELY this function to add nodes, otherwise the loop->n_nodes
347 add_loop_node(ir_loop *loop, ir_node *n) {
350 assert(loop && loop->kind == k_ir_loop);
351 assert(get_kind(n) == k_ir_node);
352 ARR_APP1 (loop_element, loop->children, ln);
356 /** Returns the number of elements contained in loop. */
357 int get_loop_n_elements (ir_loop *loop) {
358 assert(loop && loop->kind == k_ir_loop);
359 return(ARR_LEN(loop->children));
363 Returns the pos`th loop element.
364 This may be a loop_node or a ir_node. The caller of this function has
365 to check the *(loop_element.kind) field for "k_ir_node" or "k_ir_loop"
366 and then select the apropriate "loop_element.node" or "loop_element.son".
369 loop_element get_loop_element (ir_loop *loop, int pos) {
370 assert(loop && loop->kind == k_ir_loop && pos < ARR_LEN(loop->children));
372 return(loop -> children[pos]);
375 /* The outermost loop is remarked in the surrounding graph. */
376 void set_irg_loop(ir_graph *irg, ir_loop *loop) {
380 ir_loop *get_irg_loop(ir_graph *irg) {
385 /**********************************************************************/
386 /* Constructing and destructing the loop/backedge information. **/
387 /**********************************************************************/
389 /* Initialization steps. **********************************************/
392 init_node (ir_node *n, void *env) {
393 set_irn_link (n, new_scc_info());
396 /* Also init nodes not visible in intraproc_view. */
397 /* @@@ init_node is called for too many nodes -- this wastes memory!.
398 The mem is not lost as its on the obstack. */
399 if (get_irn_op(n) == op_Filter) {
400 for (i = 0; i < get_Filter_n_cg_preds(n); i++)
401 init_node(get_Filter_cg_pred(n, i), NULL);
403 if (get_irn_op(n) == op_Block) {
404 for (i = 0; i < get_Block_cg_n_cfgpreds(n); i++) {
405 init_node(get_Block_cg_cfgpred(n, i), NULL);
408 /* The following pattern matches only after a call from above pattern. */
409 if ((get_irn_op(n) == op_Proj) /*&& (get_Proj_proj(n) == 0)*/) {
410 /* @@@ init_node is called for every proj -- this wastes memory!.
411 The mem is not lost as its on the obstack. */
412 ir_node *cb = get_Proj_pred(n);
413 if ((get_irn_op(cb) == op_CallBegin) ||
414 (get_irn_op(cb) == op_EndReg) ||
415 (get_irn_op(cb) == op_EndExcept)) {
417 init_node(get_nodes_Block(cb), NULL);
423 init_scc (ir_graph *irg) {
427 irg_walk_graph (irg, init_node, NULL, NULL);
429 irg_walk (irg, link_to_reg_end, NULL, NULL);
438 cg_walk (init_node, NULL, NULL);
442 Works, but is inefficient.
446 interprocedural_view = 1;
450 for (i = 0; i < get_irp_n_irgs(); i++) {
451 current_ir_graph = get_irp_irg(i);
452 irg_walk_graph (current_ir_graph, init_node, NULL, NULL);
453 /* @@@ decrease max_visited to avoide double walks */
458 /* Condition for breaking the recursion. */
459 static bool is_outermost_Start(ir_node *n) {
460 /* Test whether this is the outermost Start node. If so
461 recursion must end. */
462 if ((get_irn_op(n) == op_Block) &&
463 (get_Block_n_cfgpreds(n) == 1) &&
464 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Start) &&
465 (get_nodes_Block(skip_Proj(get_Block_cfgpred(n, 0))) == n)) {
469 /* @@@ Bad condition:
470 not possible in interprocedural view as outermost_graph is
471 not necessarily the only with a dead-end start block.
472 Besides current_ir_graph is not set properly. */
473 if ((get_irn_op(n) == op_Block) &&
474 (n == get_irg_start_block(current_ir_graph))) {
475 if ((!interprocedural_view) ||
476 (current_ir_graph == outermost_ir_graph))
483 /* Don't walk from nodes to blocks except for Control flow operations. */
485 get_start_index(ir_node *n) {
486 if (is_cfop(n) || is_fragile_op(n) || get_irn_op(n) == op_Start)
492 /* Returns current_ir_graph and set it to the irg of predecessor index
494 static INLINE ir_graph *
495 switch_irg (ir_node *n, int index) {
496 ir_graph *old_current = current_ir_graph;
498 if (interprocedural_view) {
499 /* Only Filter and Block nodes can have predecessors in other graphs. */
500 if (get_irn_op(n) == op_Filter)
501 n = get_nodes_Block(n);
502 if (get_irn_op(n) == op_Block) {
503 ir_node *cfop = skip_Proj(get_Block_cfgpred(n, index));
504 if (is_ip_cfop(cfop)) {
505 current_ir_graph = get_irn_irg(cfop);
506 set_irg_visited(current_ir_graph, get_max_irg_visited());
514 /* Walks up the stack passing n and then finding the node
515 where we walked into the irg n is contained in.
516 Here we switch the irg. */
518 find_irg_on_stack (ir_node *n) {
520 ir_graph *old_current = current_ir_graph;
523 if (interprocedural_view) {
524 for (i = tos; i >= 0; i--) {
525 if (stack[i] == n) break;
530 for (; i >= 0; i--) {
532 /*printf(" Visiting %d ", i); DDMN(m);*/
534 current_ir_graph = get_irn_irg(m);
537 if (get_irn_op(m) == op_Filter) {
538 /* Find the corresponding ip_cfop */
539 ir_node *pred = stack[i+1];
541 for (j = 0; j < get_Filter_n_cg_preds(m); j++)
542 if (get_Filter_cg_pred(m, j) == pred) break;
543 if (j >= get_Filter_n_cg_preds(m))
544 /* It is a filter we didn't pass as the predecessors are marked. */
546 assert(get_Filter_cg_pred(m, j) == pred);
557 static void test(ir_node *pred, ir_node *root, ir_node *this) {
559 if (get_irn_uplink(pred) >= get_irn_uplink(root)) return;
561 printf("this: %d ", get_irn_uplink(this)); DDMN(this);
562 printf("pred: %d ", get_irn_uplink(pred)); DDMN(pred);
563 printf("root: %d ", get_irn_uplink(root)); DDMN(root);
565 printf("tos: %d\n", tos);
567 for (i = tos; i >= 0; i--) {
568 ir_node *n = stack[i];
570 printf(" uplink: %d, pos: %d ", get_irn_uplink(n), i); DDMN(n);
575 /* Returns true if n is a loop header, i.e., it is a Block, Phi
576 or Filter node and has predecessors within the loop and out
579 is_head (ir_node *n, ir_node *root)
582 int some_outof_loop = 0, some_in_loop = 0;
584 /* Test for legal loop header */
585 if (!((get_irn_op(n) == op_Block) ||
586 (get_irn_op(n) == op_Phi) ||
587 ((get_irn_op(n) == op_Filter) && interprocedural_view)))
590 if (!is_outermost_Start(n)) {
591 for (i = get_start_index(n); i < get_irn_arity(n); i++) {
592 ir_node *pred = get_irn_n(n, i);
594 if (is_backedge(n, i)) continue;
595 if (!irn_is_in_stack(pred)) {
598 assert(get_irn_uplink(pred) >= get_irn_uplink(root));
603 return some_outof_loop && some_in_loop;
606 /* Returns index of the predecessor with the smallest dfn number
607 greater-equal than limit. */
609 smallest_dfn_pred (ir_node *n, int limit)
611 int i, index = -2, min = -1;
613 if (!is_outermost_Start(n)) {
614 for (i = get_start_index(n); i < get_irn_arity(n); i++) {
615 ir_node *pred = get_irn_n(n, i);
617 if (is_backedge(n, i) || !irn_is_in_stack(pred)) continue;
618 if (get_irn_dfn(pred) >= limit
619 && (min == -1 || get_irn_dfn(pred) < min)) {
621 min = get_irn_dfn(pred);
628 /* Returns index of the predecessor with the largest dfn number. */
630 largest_dfn_pred (ir_node *n)
632 int i, index = -2, max = -1;
634 if (!is_outermost_Start(n)) {
635 for (i = get_start_index(n); i < get_irn_arity(n); i++) {
636 ir_node *pred = get_irn_n(n, i);
637 if (is_backedge (n, i) || !irn_is_in_stack(pred)) continue;
638 if (get_irn_dfn(pred) > max) {
640 max = get_irn_dfn(pred);
647 /* Searches the stack for possible loop heads. Tests these for backedges.
648 If it finds a head with an unmarked backedge it marks this edge and
649 returns the tail of the loop.
650 If it finds no backedge returns NULL. */
652 find_tail (ir_node *n) {
654 int i, res_index = -2;
657 if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
661 if (is_head (m, n)) {
662 res_index = smallest_dfn_pred(m, 0);
663 if ((res_index == -2) && /* no smallest dfn pred found. */
667 if (m == n) return NULL;
668 for (i = tos-2; ; --i) {
670 if (is_head (m, n)) {
671 res_index = smallest_dfn_pred (m, get_irn_dfn(m) + 1);
672 if (res_index == -2) /* no smallest dfn pred found. */
673 res_index = largest_dfn_pred (m);
678 assert (res_index > -2);
680 set_backedge (m, res_index);
681 return is_outermost_Start(n) ? NULL : get_irn_n(m, res_index);
685 /* The core algorithm. *****************************************/
687 static void scc (ir_node *n) {
691 if (irn_visited(n)) return;
693 /*printf("mark: %d ", get_irn_visited(n)); DDMN(n);
694 DDME(get_irg_ent(current_ir_graph));*/
696 /* Initialize the node */
697 set_irn_dfn(n, current_dfn); /* Depth first number for this node */
698 set_irn_uplink(n, current_dfn); /* ... is default uplink. */
699 set_irn_loop_tmp(n, NULL);
702 /* What's this good for?
703 n->ana.scc.section = NULL;
708 if (!is_outermost_Start(n)) {
709 for (i = get_start_index(n); i < get_irn_arity(n); i++) {
711 if (is_backedge(n, i)) continue;
713 m = get_irn_n(n, i); /*get_irn_ip_pred(n, i);*/
714 if ((!m) || (get_irn_op(m) == op_Unknown)) continue;
716 /*return_recur(n, i);*/
718 if (irn_is_in_stack(m)) {
719 /* Uplink of m is smaller if n->m is a backedge.
720 Propagate the uplink to mark the loop. */
721 if (get_irn_uplink(m) < get_irn_uplink(n))
722 set_irn_uplink(n, get_irn_uplink(m));
726 if (get_irn_dfn(n) == get_irn_uplink(n)) {
727 /* This condition holds for the node with the incoming backedge. */
728 ir_node *tail = find_tail(n);
730 /* We found a new loop! */
731 ir_loop *l = new_loop();
732 /* Remove the loop from the stack ... */
733 pop_scc_unmark_visit (n);
734 /* and recompute it in a better order; and so that it goes into
736 rem = find_irg_on_stack(tail);
738 current_ir_graph = rem;
740 assert (irn_visited(n));
749 /* Constructs backedge information for irg. In interprocedural view constructs
750 backedges for all methods called by irg, too. */
751 void construct_backedges(ir_graph *irg) {
752 ir_graph *rem = current_ir_graph;
756 assert(!interprocedural_view &&
757 "not implemented, use construct_ip_backedges");
759 current_ir_graph = irg;
760 outermost_ir_graph = irg;
765 new_loop(); /* sets current_loop */
766 head_rem = current_loop; /* Just for assertion */
768 if (interprocedural_view) {
769 set_irg_visited(irg, inc_max_irg_visited());
772 inc_irg_visited(irg);
775 scc(get_irg_end(irg));
776 for (i = 0; i < get_End_n_keepalives(get_irg_end(irg)); i++)
777 scc(get_End_keepalive(get_irg_end(irg), i));
779 if (interprocedural_view) finish_ip_walk();
781 assert(head_rem == current_loop);
782 set_irg_loop(irg, current_loop);
783 assert(get_irg_loop(irg)->kind == k_ir_loop);
785 irg->loops = current_loop;
789 count_loop (the_loop, &count, &depth);
793 current_ir_graph = rem;
798 void construct_ip_backedges (void) {
799 ir_graph *rem = current_ir_graph;
800 int rem_ipv = interprocedural_view;
803 outermost_ir_graph = get_irp_main_irg();
808 new_loop(); /* sets current_loop */
809 interprocedural_view = 1;
811 inc_max_irg_visited();
812 for (i = 0; i < get_irp_n_irgs(); i++)
813 set_irg_visited(get_irp_irg(i), get_max_irg_visited());
815 for (i = 0; i < get_irp_n_irgs(); i++) {
817 current_ir_graph = get_irp_irg(i);
818 /*DDME(get_irg_ent(current_ir_graph));*/
819 /* Find real entry points */
820 sb = get_irg_start_block(current_ir_graph);
821 if ((get_Block_n_cfgpreds(sb) > 1) ||
822 (get_nodes_Block(get_Block_cfgpred(sb, 0)) != sb)) continue;
823 /* printf("running scc for "); DDME(get_irg_ent(current_ir_graph)); */
824 /* Compute scc for this graph */
825 outermost_ir_graph = current_ir_graph;
826 set_irg_visited(outermost_ir_graph, get_max_irg_visited());
827 scc(get_irg_end(current_ir_graph));
828 for (j = 0; j < get_End_n_keepalives(get_irg_end(outermost_ir_graph)); j++)
829 scc(get_End_keepalive(get_irg_end(outermost_ir_graph), j));
832 set_irg_loop(outermost_ir_graph, current_loop);
833 assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
835 current_ir_graph = rem;
836 interprocedural_view = rem_ipv;