2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Construct and access dominator / post dominator tree.
9 * @author Goetz Lindenmaier, Michael Beck, Rubino Geiss
21 #include "irgraph_t.h"
28 #define get_dom_info(bl) (&(bl)->attr.block.dom)
29 #define get_pdom_info(bl) (&(bl)->attr.block.pdom)
31 ir_node *get_Block_idom(const ir_node *bl)
33 if (get_Block_dom_depth(bl) == -1) {
34 /* This block is not reachable from Start */
35 ir_graph *irg = get_irn_irg(bl);
36 return new_r_Bad(irg, mode_BB);
38 return get_dom_info(bl)->idom;
41 void set_Block_idom(ir_node *bl, ir_node *n)
43 ir_dom_info *bli = get_dom_info(bl);
47 /* Set the immediate dominator of bl to n */
51 * If we don't set the root of the dominator tree
52 * Append bl to the dominates queue of n.
55 ir_dom_info *ni = get_dom_info(n);
57 bli->next = ni->first;
62 ir_node *get_Block_ipostdom(const ir_node *bl)
64 if (get_Block_postdom_depth(bl) == -1) {
65 /* This block is not reachable from Start */
66 ir_graph *irg = get_irn_irg(bl);
67 return new_r_Bad(irg, mode_BB);
69 return get_pdom_info(bl)->idom;
72 void set_Block_ipostdom(ir_node *bl, ir_node *n)
74 ir_dom_info *bli = get_pdom_info(bl);
78 /* Set the immediate post dominator of bl to n */
82 * If we don't set the root of the post dominator tree
83 * Append bl to the post dominates queue of n.
86 ir_dom_info *ni = get_pdom_info(n);
88 bli->next = ni->first;
93 int get_Block_dom_pre_num(const ir_node *bl)
96 return get_dom_info(bl)->pre_num;
99 void set_Block_dom_pre_num(ir_node *bl, int num)
101 assert(is_Block(bl));
102 get_dom_info(bl)->pre_num = num;
105 int get_Block_dom_depth(const ir_node *bl)
107 assert(is_Block(bl));
108 return get_dom_info(bl)->dom_depth;
111 void set_Block_dom_depth(ir_node *bl, int depth)
113 assert(is_Block(bl));
114 get_dom_info(bl)->dom_depth = depth;
118 int get_Block_postdom_pre_num(const ir_node *bl)
120 assert(is_Block(bl));
121 return get_pdom_info(bl)->pre_num;
124 void set_Block_postdom_pre_num(ir_node *bl, int num)
126 assert(is_Block(bl));
127 get_pdom_info(bl)->pre_num = num;
130 int get_Block_postdom_depth(const ir_node *bl)
132 assert(is_Block(bl));
133 return get_pdom_info(bl)->dom_depth;
136 void set_Block_postdom_depth(ir_node *bl, int depth)
138 assert(is_Block(bl));
139 get_pdom_info(bl)->dom_depth = depth;
142 unsigned get_Block_dom_tree_pre_num(const ir_node *bl)
144 assert(is_Block(bl));
145 return get_dom_info(bl)->tree_pre_num;
148 unsigned get_Block_dom_max_subtree_pre_num(const ir_node *bl)
150 assert(is_Block(bl));
151 return get_dom_info(bl)->max_subtree_pre_num;
154 unsigned get_Block_pdom_tree_pre_num(const ir_node *bl)
156 assert(is_Block(bl));
157 return get_pdom_info(bl)->tree_pre_num;
160 unsigned get_Block_pdom_max_subtree_pre_num(const ir_node *bl)
162 assert(is_Block(bl));
163 return get_pdom_info(bl)->max_subtree_pre_num;
166 int block_dominates(const ir_node *a, const ir_node *b)
168 const ir_dom_info *ai, *bi;
170 if (is_Block(a) && is_Block(b)) {
171 ai = get_dom_info(a);
172 bi = get_dom_info(b);
173 return bi->tree_pre_num - ai->tree_pre_num
174 <= ai->max_subtree_pre_num - ai->tree_pre_num;
180 int block_strictly_dominates(const ir_node *a, const ir_node *b)
182 return (a != b) && block_dominates(a, b);
185 ir_node *node_smallest_common_dominator(ir_node *a, ir_node *b)
187 ir_node *bl_a = is_Block(a) ? a : get_nodes_block(a);
188 ir_node *bl_b = is_Block(b) ? b : get_nodes_block(b);
189 ir_node *dom_bl = NULL;
191 /* Check if block of a dominates block of b */
192 if (block_dominates(bl_a, bl_b))
194 /* Check if block of b dominates block of a */
195 else if (block_dominates(bl_b, bl_a))
198 /* walk up dominator tree and search for first block dominating a and b */
200 bl_a = get_Block_idom(bl_a);
202 assert(! is_Bad(bl_a) && "block is dead?");
204 if (block_dominates(bl_a, bl_b))
212 /* Get the first node in the list of nodes dominated by a given block. */
213 ir_node *get_Block_dominated_first(const ir_node *bl)
215 assert(is_Block(bl));
216 return get_dom_info(bl)->first;
219 ir_node *get_Block_dominated_next(const ir_node *bl)
221 assert(is_Block(bl));
222 return get_dom_info(bl)->next;
225 int block_postdominates(const ir_node *a, const ir_node *b)
227 const ir_dom_info *ai, *bi;
229 if (is_Block(a) && is_Block(b)) {
230 ai = get_pdom_info(a);
231 bi = get_pdom_info(b);
232 return bi->tree_pre_num - ai->tree_pre_num
233 <= ai->max_subtree_pre_num - ai->tree_pre_num;
239 int block_strictly_postdominates(const ir_node *a, const ir_node *b)
241 return (a != b) && block_postdominates(a, b);
244 ir_node *get_Block_postdominated_first(const ir_node *bl)
246 assert(is_Block(bl));
247 return get_pdom_info(bl)->first;
250 ir_node *get_Block_postdominated_next(const ir_node *bl)
252 assert(is_Block(bl));
253 return get_pdom_info(bl)->next;
256 void dom_tree_walk(ir_node *bl, irg_walk_func *pre,
257 irg_walk_func *post, void *env)
264 dominates_for_each(bl, p) {
265 dom_tree_walk(p, pre, post, env);
272 void postdom_tree_walk(ir_node *bl, irg_walk_func *pre,
273 irg_walk_func *post, void *env)
280 postdominates_for_each(bl, p) {
281 postdom_tree_walk(p, pre, post, env);
288 void dom_tree_walk_irg(ir_graph *irg, irg_walk_func *pre,
289 irg_walk_func *post, void *env)
291 /* The root of the dominator tree should be the Start block. */
292 ir_node *root = get_irg_start_block(irg);
294 assert(irg_has_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE)
295 && "The dominators of the irg must be consistent");
296 assert(root && "The start block of the graph is NULL?");
297 assert(get_dom_info(root)->idom == NULL
298 && "The start node in the graph must be the root of the dominator tree");
299 dom_tree_walk(root, pre, post, env);
302 void postdom_tree_walk_irg(ir_graph *irg, irg_walk_func *pre,
303 irg_walk_func *post, void *env)
305 /* The root of the post dominator tree should be the End block. */
306 ir_node *root = get_irg_end_block(irg);
308 assert(irg_has_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_POSTDOMINANCE)
309 && "The dominators of the irg must be consistent");
310 assert(root && "The end block of the graph is NULL?");
311 assert(get_pdom_info(root)->idom == NULL
312 && "The End block node in the graph must be the root of the post dominator tree");
313 postdom_tree_walk(root, pre, post, env);
317 static void assign_tree_dom_pre_order(ir_node *bl, void *data)
319 unsigned *num = (unsigned*) data;
320 ir_dom_info *bi = get_dom_info(bl);
322 bi->tree_pre_num = (*num)++;
325 static void assign_tree_dom_pre_order_max(ir_node *bl, void *data)
327 ir_dom_info *bi = get_dom_info(bl);
330 unsigned children = 0;
333 for (p = bi->first; p; p = get_dom_info(p)->next) {
334 unsigned max_p = get_dom_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 static void assign_tree_postdom_pre_order(ir_node *bl, void *data)
345 unsigned *num = (unsigned*) data;
346 ir_dom_info *bi = get_pdom_info(bl);
348 bi->tree_pre_num = (*num)++;
351 static void assign_tree_postdom_pre_order_max(ir_node *bl, void *data)
353 ir_dom_info *bi = get_pdom_info(bl);
356 unsigned children = 0;
359 for (p = bi->first; p; p = get_pdom_info(p)->next) {
360 unsigned max_p = get_pdom_info(p)->max_subtree_pre_num;
361 max = max > max_p ? max : max_p;
365 bi->max_subtree_pre_num = children > 0 ? max : bi->tree_pre_num;
366 assert(bi->max_subtree_pre_num >= bi->tree_pre_num);
370 * count the number of blocks and clears the post dominance info
372 static void count_and_init_blocks_pdom(ir_node *bl, void *env)
374 int *n_blocks = (int *) env;
378 memset(get_pdom_info(bl), 0, sizeof(ir_dom_info));
379 set_Block_ipostdom(bl, NULL);
380 set_Block_postdom_pre_num(bl, -1);
381 set_Block_postdom_depth(bl, -1);
384 /** temporary type used while constructing the dominator / post dominator tree. */
385 typedef struct tmp_dom_info {
386 ir_node *block; /**< backlink */
388 struct tmp_dom_info *semi; /**< semidominator */
389 struct tmp_dom_info *parent;
390 struct tmp_dom_info *label; /**< used for LINK and EVAL */
391 struct tmp_dom_info *ancestor;/**< used for LINK and EVAL */
392 struct tmp_dom_info *dom; /**< After step 3, if the semidominator of w is
393 its immediate dominator, then w->dom is the
394 immediate dominator of w. Otherwise w->dom
395 is a vertex v whose number is smaller than
396 w and whose immediate dominator is also w's
397 immediate dominator. After step 4, w->dom
398 is the immediate dominator of w. */
399 struct tmp_dom_info *bucket; /**< set of vertices with same semidominator */
402 /** Struct to pass info through walker. */
410 * Walks Blocks along the out data structure. If recursion started with
411 * Start block misses control dead blocks.
413 static void init_tmp_dom_info(ir_node *bl, tmp_dom_info *parent,
414 tmp_dom_info *tdi_list, int *used, int n_blocks)
419 if (Block_block_visited(bl))
421 mark_Block_block_visited(bl);
422 set_Block_dom_pre_num(bl, *used);
424 assert(*used < n_blocks);
425 tdi = &tdi_list[*used];
430 tdi->ancestor = NULL;
432 tdi->parent = parent;
436 for (i = get_Block_n_cfg_outs_ka(bl) - 1; i >= 0; --i) {
437 ir_node *pred = get_Block_cfg_out_ka(bl, i);
438 /* can happen for half-optimized dead code (I've seen this in student
443 init_tmp_dom_info(pred, tdi, tdi_list, used, n_blocks);
448 * Walks Blocks along the control flow. If recursion started with
449 * End block misses blocks in endless loops.
451 static void init_tmp_pdom_info(ir_node *bl, tmp_dom_info *parent,
452 tmp_dom_info *tdi_list, int* used, int n_blocks)
457 if (get_irg_block_visited(current_ir_graph) == get_Block_block_visited(bl))
459 mark_Block_block_visited(bl);
460 set_Block_postdom_pre_num(bl, *used);
462 assert(*used < n_blocks);
463 tdi = &tdi_list[*used];
468 tdi->ancestor = NULL;
470 tdi->parent = parent;
474 for (i = get_Block_n_cfgpreds(bl) - 1; i >= 0; --i) {
475 ir_node *pred = get_Block_cfgpred_block(bl, i);
478 init_tmp_pdom_info(pred, tdi, tdi_list, used, n_blocks);
481 /* Handle keep-alives. Note that the preprocessing
482 in init_construction() had already killed all
483 phantom keep-alive edges. All remaining block keep-alives
484 are really edges to endless loops.
486 if (bl == get_irg_end_block(current_ir_graph)) {
487 ir_node *end = get_irg_end(current_ir_graph);
489 for (i = get_irn_arity(end) - 1; i >= 0; --i) {
490 ir_node *pred = get_irn_n(end, i);
493 init_tmp_pdom_info(pred, tdi, tdi_list, used, n_blocks);
498 static void dom_compress(tmp_dom_info *v)
500 assert (v->ancestor);
501 if (v->ancestor->ancestor) {
502 dom_compress (v->ancestor);
503 if (v->ancestor->label->semi < v->label->semi) {
504 v->label = v->ancestor->label;
506 v->ancestor = v->ancestor->ancestor;
511 * if V is a root, return v, else return the vertex u, not being the
512 * root, with minimum u->semi on the path from v to its root.
514 inline static tmp_dom_info *dom_eval(tmp_dom_info *v)
516 if (!v->ancestor) return v;
521 /** make V W's ancestor */
522 inline static void dom_link(tmp_dom_info *v, tmp_dom_info *w)
528 * Walker: count the number of blocks and clears the dominance info
530 static void count_and_init_blocks_dom(ir_node *bl, void *env)
532 int *n_blocks = (int *) env;
536 memset(get_dom_info(bl), 0, sizeof(ir_dom_info));
537 set_Block_idom(bl, NULL);
538 set_Block_dom_pre_num(bl, -1);
539 set_Block_dom_depth(bl, -1);
542 void compute_doms(ir_graph *irg)
544 ir_graph *rem = current_ir_graph;
545 int n_blocks, used, i, j;
546 tmp_dom_info *tdi_list; /* Ein Golf? */
548 current_ir_graph = irg;
550 /* Update graph state */
551 assert(!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_CONSTRUCTION));
553 /* Count the number of blocks in the graph. */
555 irg_block_walk_graph(irg, count_and_init_blocks_dom, NULL, &n_blocks);
557 /* Memory for temporary information. */
558 tdi_list = XMALLOCNZ(tmp_dom_info, n_blocks);
560 /* We need the out data structure. */
561 assure_irg_outs(irg);
563 /* this with a standard walker as passing the parent to the sons isn't
566 inc_irg_block_visited(irg);
567 init_tmp_dom_info(get_irg_start_block(irg), NULL, tdi_list, &used, n_blocks);
568 /* If not all blocks are reachable from Start by out edges this assertion
570 assert(used == n_blocks && "Precondition for dom construction violated"); */
571 assert(used <= n_blocks && "Precondition for dom construction violated");
575 for (i = n_blocks-1; i > 0; i--) { /* Don't iterate the root, it's done. */
576 tmp_dom_info *w = &tdi_list[i];
577 ir_node *block = w->block;
582 irn_arity = get_irn_arity(block);
583 for (j = 0; j < irn_arity; j++) {
584 ir_node *pred = get_Block_cfgpred(block, j);
585 ir_node *pred_block = get_nodes_block(pred);
588 if (is_Bad(pred) || (get_Block_dom_pre_num (pred_block) == -1))
589 continue; /* unreachable */
591 u = dom_eval (&tdi_list[get_Block_dom_pre_num(pred_block)]);
592 if (u->semi < w->semi)
596 /* handle keep-alives if we are at the end block */
597 if (block == get_irg_end_block(irg)) {
598 ir_node *end = get_irg_end(irg);
600 irn_arity = get_irn_arity(end);
601 for (j = 0; j < irn_arity; j++) {
602 ir_node *pred = get_irn_n(end, j);
605 if (!is_Block(pred) || get_Block_dom_pre_num(pred) == -1)
606 continue; /* unreachable */
608 u = dom_eval (&tdi_list[get_Block_dom_pre_num(pred)]);
609 if (u->semi < w->semi)
614 /* Add w to w->semi's bucket. w is in exactly one bucket, so
615 buckets can been implemented as linked lists. */
616 w->bucket = w->semi->bucket;
619 dom_link (w->parent, w);
622 while (w->parent->bucket) {
624 v = w->parent->bucket;
625 /* remove v from w->parent->bucket */
626 w->parent->bucket = v->bucket;
630 if (u->semi < v->semi)
637 tdi_list[0].dom = NULL;
638 set_Block_idom(tdi_list[0].block, NULL);
639 set_Block_dom_depth(tdi_list[0].block, 1);
640 for (i = 1; i < n_blocks; i++) {
641 tmp_dom_info *w = &tdi_list[i];
645 continue; /* control dead */
647 if (w->dom != w->semi)
648 w->dom = w->dom->dom;
649 set_Block_idom(w->block, w->dom->block);
651 /* blocks dominated by dead one's are still dead */
652 depth = get_Block_dom_depth(w->dom->block);
655 set_Block_dom_depth(w->block, depth);
661 /* Do a walk over the tree and assign the tree pre orders. */
663 unsigned tree_pre_order = 0;
664 dom_tree_walk(get_irg_start_block(irg), assign_tree_dom_pre_order,
665 assign_tree_dom_pre_order_max, &tree_pre_order);
667 current_ir_graph = rem;
668 add_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
671 void assure_doms(ir_graph *irg)
673 if (! irg_has_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE))
677 void free_dom(ir_graph *irg)
679 /* Update graph state */
680 clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
682 /* With the implementation right now there is nothing to free */
684 /* free dominance frontiers */
685 ir_free_dominance_frontiers(irg);
688 void compute_postdoms(ir_graph *irg)
690 ir_graph *rem = current_ir_graph;
691 int n_blocks, used, i, j;
692 tmp_dom_info *tdi_list;
694 current_ir_graph = irg;
696 /* Update graph state */
697 assert(!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_CONSTRUCTION));
699 /* Count the number of blocks in the graph. */
701 irg_block_walk_graph(irg, count_and_init_blocks_pdom, NULL, &n_blocks);
703 /* Memory for temporary information. */
704 tdi_list = XMALLOCNZ(tmp_dom_info, n_blocks);
706 /* We need the out data structure. */
707 assure_irg_outs(irg);
709 /* this with a standard walker as passing the parent to the sons isn't
712 inc_irg_block_visited(irg);
713 init_tmp_pdom_info(get_irg_end_block(irg), NULL, tdi_list, &used, n_blocks);
714 /* If not all blocks are reachable from End by cfg edges this assertion
716 assert(used == n_blocks && "Precondition for dom construction violated"); */
720 for (i = n_blocks-1; i > 0; i--) { /* Don't iterate the root, it's done. */
722 tmp_dom_info *w = &tdi_list[i];
726 irn_arity = get_Block_n_cfg_outs_ka(w->block);
727 for (j = 0; j < irn_arity; j++) {
728 ir_node *succ = get_Block_cfg_out_ka(w->block, j);
731 if (get_Block_postdom_pre_num (succ) == -1)
732 continue; /* endless-loop */
734 u = dom_eval (&tdi_list[get_Block_postdom_pre_num(succ)]);
735 if (u->semi < w->semi) w->semi = u->semi;
737 /* Add w to w->semi's bucket. w is in exactly one bucket, so
738 buckets can be implemented as linked lists. */
739 w->bucket = w->semi->bucket;
742 dom_link (w->parent, w);
745 while (w->parent->bucket) {
747 v = w->parent->bucket;
748 /* remove v from w->parent->bucket */
749 w->parent->bucket = v->bucket;
753 if (u->semi < v->semi)
760 tdi_list[0].dom = NULL;
761 set_Block_ipostdom(tdi_list[0].block, NULL);
762 set_Block_postdom_depth(tdi_list[0].block, 1);
763 for (i = 1; i < n_blocks; i++) {
764 tmp_dom_info *w = &tdi_list[i];
766 if (w->dom != w->semi) w->dom = w->dom->dom;
767 set_Block_ipostdom(w->block, w->dom->block);
768 set_Block_postdom_depth(w->block, get_Block_postdom_depth(w->dom->block) + 1);
774 /* Do a walk over the tree and assign the tree pre orders. */
776 unsigned tree_pre_order = 0;
777 postdom_tree_walk(get_irg_end_block(irg), assign_tree_postdom_pre_order,
778 assign_tree_postdom_pre_order_max, &tree_pre_order);
780 current_ir_graph = rem;
781 add_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_POSTDOMINANCE);
784 void assure_postdoms(ir_graph *irg)
786 if (! irg_has_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_POSTDOMINANCE))
787 compute_postdoms(irg);
790 void free_postdom(ir_graph *irg)
792 /* Update graph state */
793 clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_POSTDOMINANCE);