3 * File name: ir/ana/irdom.c
4 * Purpose: Construct and access dominator / post dominator tree.
5 * Author: Goetz Lindenmaier
6 * Modified by: Michael Beck, Rubino Geiss
9 * Copyright: (c) 2002-2003 Universitaet Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
32 #include "irgraph_t.h" /* To access state field. */
38 #define get_dom_info(bl) (&(bl)->attr.block.dom)
39 #define get_pdom_info(bl) (&(bl)->attr.block.pdom)
41 /*--------------------------------------------------------------------*/
42 /** Accessing the dominator and post dominator data structures **/
43 /*--------------------------------------------------------------------*/
45 ir_node *get_Block_idom(const ir_node *bl) {
47 if (get_Block_dom_depth(bl) == -1) {
48 /* This block is not reachable from Start */
51 return get_dom_info(bl)->idom;
54 void set_Block_idom(ir_node *bl, ir_node *n) {
55 dom_info *bli = get_dom_info(bl);
57 assert(get_irn_op(bl) == op_Block);
59 /* Set the immediate dominator of bl to n */
63 * If we don't set the root of the dominator tree
64 * Append bl to the dominates queue of n.
67 dom_info *ni = get_dom_info(n);
69 bli->next = ni->first;
74 ir_node *get_Block_ipostdom(const ir_node *bl) {
76 if (get_Block_postdom_depth(bl) == -1) {
77 /* This block is not reachable from Start */
80 return get_pdom_info(bl)->idom;
83 void set_Block_ipostdom(ir_node *bl, ir_node *n) {
84 dom_info *bli = get_pdom_info(bl);
86 assert(get_irn_op(bl) == op_Block);
88 /* Set the immediate post dominator of bl to n */
92 * If we don't set the root of the post dominator tree
93 * Append bl to the post dominates queue of n.
96 dom_info *ni = get_pdom_info(n);
98 bli->next = ni->first;
103 int get_Block_dom_pre_num(const ir_node *bl) {
104 assert(get_irn_op(bl) == op_Block);
105 return get_dom_info(bl)->pre_num;
108 void set_Block_dom_pre_num(ir_node *bl, int num) {
109 assert(get_irn_op(bl) == op_Block);
110 get_dom_info(bl)->pre_num = num;
113 int get_Block_dom_depth(const ir_node *bl) {
114 assert(get_irn_op(bl) == op_Block);
115 return get_dom_info(bl)->dom_depth;
118 void set_Block_dom_depth(ir_node *bl, int depth) {
119 assert(get_irn_op(bl) == op_Block);
120 get_dom_info(bl)->dom_depth = depth;
124 int get_Block_postdom_pre_num(const ir_node *bl) {
125 assert(get_irn_op(bl) == op_Block);
126 return get_pdom_info(bl)->pre_num;
129 void set_Block_postdom_pre_num(ir_node *bl, int num) {
130 assert(get_irn_op(bl) == op_Block);
131 get_pdom_info(bl)->pre_num = num;
134 int get_Block_postdom_depth(const ir_node *bl) {
135 assert(get_irn_op(bl) == op_Block);
136 return get_pdom_info(bl)->dom_depth;
139 void set_Block_postdom_depth(ir_node *bl, int depth) {
140 assert(get_irn_op(bl) == op_Block);
141 get_pdom_info(bl)->dom_depth = depth;
144 unsigned get_Block_dom_tree_pre_num(const ir_node *bl)
146 assert(is_Block(bl));
147 return get_dom_info(bl)->tree_pre_num;
150 unsigned get_Block_dom_max_subtree_pre_num(const ir_node *bl)
152 assert(is_Block(bl));
153 return get_dom_info(bl)->max_subtree_pre_num;
156 unsigned get_Block_pdom_tree_pre_num(const ir_node *bl)
158 assert(is_Block(bl));
159 return get_pdom_info(bl)->tree_pre_num;
162 unsigned get_Block_pdom_max_subtree_pre_num(const ir_node *bl)
164 assert(is_Block(bl));
165 return get_pdom_info(bl)->max_subtree_pre_num;
168 /* Check, if a block dominates another block. */
169 int block_dominates(const ir_node *a, const ir_node *b)
171 const dom_info *ai, *bi;
173 if (is_Block(a) && is_Block(b)) {
174 ai = get_dom_info(a);
175 bi = get_dom_info(b);
176 return bi->tree_pre_num - ai->tree_pre_num
177 <= ai->max_subtree_pre_num - ai->tree_pre_num;
183 /* Get the first node in the list of nodes dominated by a given block. */
184 ir_node *get_Block_dominated_first(const ir_node *bl)
186 assert(is_Block(bl));
187 return get_dom_info(bl)->first;
190 /* Get the next node in a list of nodes which are dominated by some
192 ir_node *get_Block_dominated_next(const ir_node *bl)
194 assert(is_Block(bl));
195 return get_dom_info(bl)->next;
198 /* Check, if a block post dominates another block. */
199 int block_postdominates(const ir_node *a, const ir_node *b)
201 const dom_info *ai, *bi;
203 if (is_Block(a) && is_Block(b)) {
204 ai = get_pdom_info(a);
205 bi = get_pdom_info(b);
206 return bi->tree_pre_num - ai->tree_pre_num
207 <= ai->max_subtree_pre_num - ai->tree_pre_num;
213 /* Get the first node in the list of nodes post dominated by a given block. */
214 ir_node *get_Block_postdominated_first(const ir_node *bl)
216 assert(is_Block(bl));
217 return get_pdom_info(bl)->first;
220 /* Get the next node in a list of nodes which are post dominated by some
222 ir_node *get_Block_postdominated_next(const ir_node *bl)
224 assert(is_Block(bl));
225 return get_pdom_info(bl)->next;
228 /* Visit all nodes in the dominator subtree of a given node. */
229 void dom_tree_walk(ir_node *bl, irg_walk_func *pre,
230 irg_walk_func *post, void *env)
237 dominates_for_each(bl, p) {
238 dom_tree_walk(p, pre, post, env);
245 /* Visit all nodes in the post dominator subtree of a given node. */
246 void postdom_tree_walk(ir_node *bl, irg_walk_func *pre,
247 irg_walk_func *post, void *env)
254 postdominates_for_each(bl, p) {
255 postdom_tree_walk(p, pre, post, env);
262 /* Walk over the dominator tree of an irg starting at the root. */
263 void dom_tree_walk_irg(ir_graph *irg, irg_walk_func *pre,
264 irg_walk_func *post, void *env)
266 /* The root of the dominator tree should be the Start block. */
267 ir_node *root = get_irg_start_block(irg);
269 assert(irg->dom_state == dom_consistent
270 && "The dominators of the irg must be consistent");
271 assert(root && "The start block of the graph is NULL?");
272 assert(get_dom_info(root)->idom == NULL
273 && "The start node in the graph must be the root of the dominator tree");
274 dom_tree_walk(root, pre, post, env);
277 /* Walk over the post dominator tree of an irg starting at the root. */
278 void postdom_tree_walk_irg(ir_graph *irg, irg_walk_func *pre,
279 irg_walk_func *post, void *env)
281 /* The root of the dominator tree should be the End block. */
282 ir_node *root = get_irg_end_block(irg);
284 assert(irg->pdom_state == dom_consistent
285 && "The dominators of the irg must be consistent");
286 assert(root && "The end block of the graph is NULL?");
287 assert(get_pdom_info(root)->idom == NULL
288 && "The End block node in the graph must be the root of the post dominator tree");
289 postdom_tree_walk(root, pre, post, env);
293 static void assign_tree_dom_pre_order(ir_node *bl, void *data)
295 unsigned *num = data;
296 dom_info *bi = get_dom_info(bl);
298 bi->tree_pre_num = (*num)++;
301 static void assign_tree_dom_pre_order_max(ir_node *bl, void *data)
303 dom_info *bi = get_dom_info(bl);
306 unsigned children = 0;
308 for(p = bi->first; p; p = get_dom_info(p)->next) {
309 unsigned max_p = get_dom_info(p)->max_subtree_pre_num;
310 max = max > max_p ? max : max_p;
314 bi->max_subtree_pre_num = children > 0 ? max : bi->tree_pre_num;
315 assert(bi->max_subtree_pre_num >= bi->tree_pre_num);
318 static void assign_tree_postdom_pre_order(ir_node *bl, void *data)
320 unsigned *num = data;
321 dom_info *bi = get_pdom_info(bl);
323 bi->tree_pre_num = (*num)++;
326 static void assign_tree_postdom_pre_order_max(ir_node *bl, void *data)
328 dom_info *bi = get_pdom_info(bl);
331 unsigned children = 0;
333 for(p = bi->first; p; p = get_pdom_info(p)->next) {
334 unsigned max_p = get_pdom_info(p)->max_subtree_pre_num;
335 max = max > max_p ? max : max_p;
339 bi->max_subtree_pre_num = children > 0 ? max : bi->tree_pre_num;
340 assert(bi->max_subtree_pre_num >= bi->tree_pre_num);
343 /*--------------------------------------------------------------------*/
344 /* Building and Removing the dominator data structure */
345 /*--------------------------------------------------------------------*/
348 * count the number of blocks and clears the post dominance info
350 static void count_and_init_blocks_pdom(ir_node *bl, void *env) {
351 int *n_blocks = (int *) env;
354 memset(get_pdom_info(bl), 0, sizeof(dom_info));
355 set_Block_ipostdom(bl, NULL);
356 set_Block_postdom_pre_num(bl, -1);
357 set_Block_postdom_depth(bl, -1);
360 /** temporary type used while constructing the dominator / post dominator tree. */
361 typedef struct tmp_dom_info {
362 ir_node *block; /**< backlink */
364 struct tmp_dom_info *semi; /**< semidominator */
365 struct tmp_dom_info *parent;
366 struct tmp_dom_info *label; /**< used for LINK and EVAL */
367 struct tmp_dom_info *ancestor;/**< used for LINK and EVAL */
368 struct tmp_dom_info *dom; /**< After step 3, if the semidominator of w is
369 its immediate dominator, then w->dom is the
370 immediate dominator of w. Otherwise w->dom
371 is a vertex v whose number is smaller than
372 w and whose immediate dominator is also w's
373 immediate dominator. After step 4, w->dom
374 is the immediate dominator of w. */
375 struct tmp_dom_info *bucket; /**< set of vertices with same semidominator */
378 /** Struct to pass info through walker. */
386 * Walks Blocks along the out data structure. If recursion started with
387 * Start block misses control dead blocks.
389 static void init_tmp_dom_info(ir_node *bl, tmp_dom_info *parent,
390 tmp_dom_info *tdi_list, int* used) {
394 assert(is_Block(bl));
395 if (get_irg_block_visited(current_ir_graph) == get_Block_block_visited(bl))
397 mark_Block_block_visited(bl);
398 set_Block_dom_pre_num(bl, *used);
400 tdi = &tdi_list[*used];
405 tdi->ancestor = NULL;
407 tdi->parent = parent;
411 for (i = get_Block_n_cfg_outs_ka(bl) - 1; i >= 0; --i) {
412 ir_node *pred = get_Block_cfg_out_ka(bl, i);
413 assert(is_Block(pred));
414 init_tmp_dom_info(pred, tdi, tdi_list, used);
419 * Walks Blocks along the control flow. If recursion started with
420 * End block misses blocks in endless loops.
422 static void init_tmp_pdom_info(ir_node *bl, tmp_dom_info *parent,
423 tmp_dom_info *tdi_list, int* used) {
427 assert(is_Block(bl));
428 if (get_irg_block_visited(current_ir_graph) == get_Block_block_visited(bl))
430 mark_Block_block_visited(bl);
431 set_Block_postdom_pre_num(bl, *used);
433 tdi = &tdi_list[*used];
438 tdi->ancestor = NULL;
440 tdi->parent = parent;
444 for (i = get_Block_n_cfgpreds(bl) - 1; i >= 0; --i) {
445 ir_node *pred = get_Block_cfgpred_block(bl, i);
448 assert(is_Block(pred));
449 init_tmp_pdom_info(pred, tdi, tdi_list, used);
453 static void dom_compress(tmp_dom_info *v)
455 assert (v->ancestor);
456 if (v->ancestor->ancestor) {
457 dom_compress (v->ancestor);
458 if (v->ancestor->label->semi < v->label->semi) {
459 v->label = v->ancestor->label;
461 v->ancestor = v->ancestor->ancestor;
466 * if V is a root, return v, else return the vertex u, not being the
467 * root, with minimum u->semi on the path from v to its root.
469 INLINE static tmp_dom_info *dom_eval (tmp_dom_info *v)
471 if (!v->ancestor) return v;
476 /** make V W's ancestor */
477 INLINE static void dom_link(tmp_dom_info *v, tmp_dom_info *w)
483 * Walker: count the number of blocks and clears the dominance info
485 static void count_and_init_blocks_dom(ir_node *bl, void *env) {
486 int *n_blocks = (int *) env;
489 memset(get_dom_info(bl), 0, sizeof(dom_info));
490 set_Block_idom(bl, NULL);
491 set_Block_dom_pre_num(bl, -1);
492 set_Block_dom_depth(bl, -1);
496 * Initialize the dominance/postdominance construction:
498 * - count the number of blocks
499 * - clear the dominance info
500 * - remove block-keepalives of live blocks to reduce
501 * the number of "phantom" block edges
503 * @param irg the graph
504 * @param pre a walker function that will be called for every block in the graph
506 static int init_construction(ir_graph *irg, irg_walk_func *pre) {
507 ir_graph *rem = current_ir_graph;
512 current_ir_graph = irg;
514 /* this visits only the reachable blocks */
515 irg_block_walk(get_irg_end_block(irg), pre, NULL, &n_blocks);
517 /* now visit the unreachable (from End) Blocks and remove unnecessary keep-alives */
518 end = get_irg_end(irg);
519 arity = get_irn_arity(end);
520 if (arity) { /* we have keep-alives */
524 NEW_ARR_A(ir_node *, in, arity);
525 for (i = j = 0; i < arity; i++) {
526 ir_node *pred = get_irn_n(end, i);
528 if (get_irn_op(pred) == op_Block) {
529 if (Block_not_block_visited(pred)) {
530 /* we found a endless loop */
531 dec_irg_block_visited(irg);
532 irg_block_walk(pred, pre, NULL, &n_blocks);
540 /* we kill some Block keep-alives */
541 set_irn_in(end, j, in);
542 set_irg_outs_inconsistent(irg);
546 current_ir_graph = rem;
551 /* Computes the dominator trees. Sets a flag in irg to "dom_consistent".
552 If the control flow of the graph is changed this flag must be set to
553 "dom_inconsistent". */
554 void compute_doms(ir_graph *irg) {
555 ir_graph *rem = current_ir_graph;
556 int n_blocks, used, i, j;
557 tmp_dom_info *tdi_list; /* Ein Golf? */
559 current_ir_graph = irg;
561 /* Update graph state */
562 assert(get_irg_phase_state(irg) != phase_building);
563 irg->dom_state = dom_consistent;
565 /* Count the number of blocks in the graph. */
566 n_blocks = init_construction(irg, count_and_init_blocks_dom);
568 /* Memory for temporary information. */
569 tdi_list = xcalloc(n_blocks, sizeof(tdi_list[0]));
571 /* We need the out data structure. */
572 if (irg->outs_state != outs_consistent)
573 compute_irg_outs(irg);
575 /* this with a standard walker as passing the parent to the sons isn't
578 inc_irg_block_visited(irg);
579 init_tmp_dom_info(get_irg_start_block(irg), NULL, tdi_list, &used);
580 /* If not all blocks are reachable from Start by out edges this assertion
582 assert(used == n_blocks && "Precondition for dom construction violated"); */
586 for (i = n_blocks-1; i > 0; i--) { /* Don't iterate the root, it's done. */
588 tmp_dom_info *w = &tdi_list[i];
592 irn_arity = get_irn_arity(w->block);
593 for (j = 0; j < irn_arity; j++) {
594 ir_node *pred = get_Block_cfgpred_block(w->block, j);
597 if (is_Bad(pred) || (get_Block_dom_pre_num (pred) == -1))
598 continue; /* control-dead */
600 u = dom_eval (&tdi_list[get_Block_dom_pre_num(pred)]);
601 if (u->semi < w->semi) w->semi = u->semi;
604 /* handle keep-alives if we are at the end block */
605 if (w->block == get_irg_end_block(irg)) {
606 ir_node *end = get_irg_end(irg);
608 irn_arity = get_irn_arity(end);
609 for (j = 0; j < irn_arity; j++) {
610 ir_node *pred = get_irn_n(end, j);
613 if (is_no_Block(pred))
616 if (get_Block_dom_pre_num(pred) == -1)
617 continue; /* control-dead */
619 u = dom_eval (&tdi_list[get_Block_dom_pre_num(pred)]);
620 if (u->semi < w->semi) w->semi = u->semi;
624 /* Add w to w->semi's bucket. w is in exactly one bucket, so
625 buckets can been implemented as linked lists. */
626 w->bucket = w->semi->bucket;
629 dom_link (w->parent, w);
632 while (w->parent->bucket) {
634 v = w->parent->bucket;
635 /* remove v from w->parent->bucket */
636 w->parent->bucket = v->bucket;
640 if (u->semi < v->semi)
647 tdi_list[0].dom = NULL;
648 set_Block_idom(tdi_list[0].block, NULL);
649 set_Block_dom_depth(tdi_list[0].block, 1);
650 for (i = 1; i < n_blocks; i++) {
651 tmp_dom_info *w = &tdi_list[i];
653 if (w->dom != w->semi) w->dom = w->dom->dom;
654 set_Block_idom(w->block, w->dom->block);
655 set_Block_dom_depth(w->block, get_Block_dom_depth(w->dom->block) + 1);
660 current_ir_graph = rem;
662 /* Do a walk over the tree and assign the tree pre orders. */
664 unsigned tree_pre_order = 0;
665 dom_tree_walk_irg(irg, assign_tree_dom_pre_order,
666 assign_tree_dom_pre_order_max, &tree_pre_order);
670 void free_dom(ir_graph *irg) {
671 /* Update graph state */
672 assert(get_irg_phase_state(current_ir_graph) != phase_building);
673 current_ir_graph->dom_state = dom_none;
675 /* With the implementation right now there is nothing to free,
676 but better call it anyways... */
679 /* Computes the post dominator trees. Sets a flag in irg to "dom_consistent".
680 If the control flow of the graph is changed this flag must be set to
681 "dom_inconsistent". */
682 void compute_postdoms(ir_graph *irg) {
683 ir_graph *rem = current_ir_graph;
684 int n_blocks, used, i, j;
685 tmp_dom_info *tdi_list;
687 current_ir_graph = irg;
689 /* Update graph state */
690 assert(get_irg_phase_state(irg) != phase_building);
691 irg->pdom_state = dom_consistent;
693 /* Count the number of blocks in the graph. */
694 n_blocks = init_construction(irg, count_and_init_blocks_pdom);
696 /* Memory for temporary information. */
697 tdi_list = xcalloc(n_blocks, sizeof(tdi_list[0]));
699 /* We need the out data structure. */
700 if (irg->outs_state != outs_consistent)
701 compute_irg_outs(irg);
703 /* this with a standard walker as passing the parent to the sons isn't
706 inc_irg_block_visited(irg);
707 init_tmp_pdom_info(get_irg_end_block(irg), NULL, tdi_list, &used);
708 /* If not all blocks are reachable from End by cfg edges this assertion
710 assert(used == n_blocks && "Precondition for dom construction violated"); */
714 for (i = n_blocks-1; i > 0; i--) { /* Don't iterate the root, it's done. */
716 tmp_dom_info *w = &tdi_list[i];
720 irn_arity = get_Block_n_cfg_outs(w->block);
721 for (j = 0; j < irn_arity; j++) {
722 ir_node *succ = get_Block_cfg_out(w->block, j);
725 if (get_Block_postdom_pre_num (succ) == -1)
726 continue; /* endless-loop */
728 u = dom_eval (&tdi_list[get_Block_postdom_pre_num(succ)]);
729 if (u->semi < w->semi) w->semi = u->semi;
731 /* Add w to w->semi's bucket. w is in exactly one bucket, so
732 buckets can be implemented as linked lists. */
733 w->bucket = w->semi->bucket;
736 dom_link (w->parent, w);
739 while (w->parent->bucket) {
741 v = w->parent->bucket;
742 /* remove v from w->parent->bucket */
743 w->parent->bucket = v->bucket;
747 if (u->semi < v->semi)
754 tdi_list[0].dom = NULL;
755 set_Block_ipostdom(tdi_list[0].block, NULL);
756 set_Block_postdom_depth(tdi_list[0].block, 1);
757 for (i = 1; i < n_blocks; i++) {
758 tmp_dom_info *w = &tdi_list[i];
760 if (w->dom != w->semi) w->dom = w->dom->dom;
761 set_Block_ipostdom(w->block, w->dom->block);
762 set_Block_postdom_depth(w->block, get_Block_postdom_depth(w->dom->block) + 1);
769 /* Do a walk over the tree and assign the tree pre orders. */
771 unsigned tree_pre_order = 0;
772 postdom_tree_walk_irg(irg, assign_tree_postdom_pre_order,
773 assign_tree_postdom_pre_order_max, &tree_pre_order);
777 void free_postdom(ir_graph *irg) {
778 /* Update graph state */
779 assert(get_irg_phase_state(current_ir_graph) != phase_building);
780 current_ir_graph->pdom_state = dom_none;
782 /* With the implementation right now there is nothing to free,
783 but better call it anyways... */