2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Construct and access dominator / post dominator tree.
23 * @author Goetz Lindenmaier, Michael Beck, Rubino Geiss
35 #include "irgraph_t.h"
42 #define get_dom_info(bl) (&(bl)->attr.block.dom)
43 #define get_pdom_info(bl) (&(bl)->attr.block.pdom)
45 ir_node *get_Block_idom(const ir_node *bl)
48 if (get_Block_dom_depth(bl) == -1) {
49 /* This block is not reachable from Start */
50 ir_graph *irg = get_irn_irg(bl);
51 return new_r_Bad(irg, mode_BB);
53 return get_dom_info(bl)->idom;
56 void set_Block_idom(ir_node *bl, ir_node *n)
58 ir_dom_info *bli = get_dom_info(bl);
62 /* Set the immediate dominator of bl to n */
66 * If we don't set the root of the dominator tree
67 * Append bl to the dominates queue of n.
70 ir_dom_info *ni = get_dom_info(n);
72 bli->next = ni->first;
77 ir_node *get_Block_ipostdom(const ir_node *bl)
80 if (get_Block_postdom_depth(bl) == -1) {
81 /* This block is not reachable from Start */
82 ir_graph *irg = get_irn_irg(bl);
83 return new_r_Bad(irg, mode_BB);
85 return get_pdom_info(bl)->idom;
88 void set_Block_ipostdom(ir_node *bl, ir_node *n)
90 ir_dom_info *bli = get_pdom_info(bl);
94 /* Set the immediate post dominator of bl to n */
98 * If we don't set the root of the post dominator tree
99 * Append bl to the post dominates queue of n.
102 ir_dom_info *ni = get_pdom_info(n);
104 bli->next = ni->first;
109 int get_Block_dom_pre_num(const ir_node *bl)
111 assert(is_Block(bl));
112 return get_dom_info(bl)->pre_num;
115 void set_Block_dom_pre_num(ir_node *bl, int num)
117 assert(is_Block(bl));
118 get_dom_info(bl)->pre_num = num;
121 int get_Block_dom_depth(const ir_node *bl)
123 assert(is_Block(bl));
124 return get_dom_info(bl)->dom_depth;
127 void set_Block_dom_depth(ir_node *bl, int depth)
129 assert(is_Block(bl));
130 get_dom_info(bl)->dom_depth = depth;
134 int get_Block_postdom_pre_num(const ir_node *bl)
136 assert(is_Block(bl));
137 return get_pdom_info(bl)->pre_num;
140 void set_Block_postdom_pre_num(ir_node *bl, int num)
142 assert(is_Block(bl));
143 get_pdom_info(bl)->pre_num = num;
146 int get_Block_postdom_depth(const ir_node *bl)
148 assert(is_Block(bl));
149 return get_pdom_info(bl)->dom_depth;
152 void set_Block_postdom_depth(ir_node *bl, int depth)
154 assert(is_Block(bl));
155 get_pdom_info(bl)->dom_depth = depth;
158 unsigned get_Block_dom_tree_pre_num(const ir_node *bl)
160 assert(is_Block(bl));
161 return get_dom_info(bl)->tree_pre_num;
164 unsigned get_Block_dom_max_subtree_pre_num(const ir_node *bl)
166 assert(is_Block(bl));
167 return get_dom_info(bl)->max_subtree_pre_num;
170 unsigned get_Block_pdom_tree_pre_num(const ir_node *bl)
172 assert(is_Block(bl));
173 return get_pdom_info(bl)->tree_pre_num;
176 unsigned get_Block_pdom_max_subtree_pre_num(const ir_node *bl)
178 assert(is_Block(bl));
179 return get_pdom_info(bl)->max_subtree_pre_num;
182 int block_dominates(const ir_node *a, const ir_node *b)
184 const ir_dom_info *ai, *bi;
186 if (is_Block(a) && is_Block(b)) {
187 ai = get_dom_info(a);
188 bi = get_dom_info(b);
189 return bi->tree_pre_num - ai->tree_pre_num
190 <= ai->max_subtree_pre_num - ai->tree_pre_num;
196 int block_strictly_dominates(const ir_node *a, const ir_node *b)
198 return (a != b) && block_dominates(a, b);
201 ir_node *node_smallest_common_dominator(ir_node *a, ir_node *b)
203 ir_node *bl_a = is_Block(a) ? a : get_nodes_block(a);
204 ir_node *bl_b = is_Block(b) ? b : get_nodes_block(b);
205 ir_node *dom_bl = NULL;
207 /* Check if block of a dominates block of b */
208 if (block_dominates(bl_a, bl_b))
210 /* Check if block of b dominates block of a */
211 else if (block_dominates(bl_b, bl_a))
214 /* walk up dominator tree and search for first block dominating a and b */
216 bl_a = get_Block_idom(bl_a);
218 assert(! is_Bad(bl_a) && "block is dead?");
220 if (block_dominates(bl_a, bl_b))
228 /* Get the first node in the list of nodes dominated by a given block. */
229 ir_node *get_Block_dominated_first(const ir_node *bl)
231 assert(is_Block(bl));
232 return get_dom_info(bl)->first;
235 ir_node *get_Block_dominated_next(const ir_node *bl)
237 assert(is_Block(bl));
238 return get_dom_info(bl)->next;
241 int block_postdominates(const ir_node *a, const ir_node *b)
243 const ir_dom_info *ai, *bi;
245 if (is_Block(a) && is_Block(b)) {
246 ai = get_pdom_info(a);
247 bi = get_pdom_info(b);
248 return bi->tree_pre_num - ai->tree_pre_num
249 <= ai->max_subtree_pre_num - ai->tree_pre_num;
255 int block_strictly_postdominates(const ir_node *a, const ir_node *b)
257 return (a != b) && block_postdominates(a, b);
260 ir_node *get_Block_postdominated_first(const ir_node *bl)
262 assert(is_Block(bl));
263 return get_pdom_info(bl)->first;
266 ir_node *get_Block_postdominated_next(const ir_node *bl)
268 assert(is_Block(bl));
269 return get_pdom_info(bl)->next;
272 void dom_tree_walk(ir_node *bl, irg_walk_func *pre,
273 irg_walk_func *post, void *env)
280 dominates_for_each(bl, p) {
281 dom_tree_walk(p, pre, post, env);
288 void postdom_tree_walk(ir_node *bl, irg_walk_func *pre,
289 irg_walk_func *post, void *env)
296 postdominates_for_each(bl, p) {
297 postdom_tree_walk(p, pre, post, env);
304 void dom_tree_walk_irg(ir_graph *irg, irg_walk_func *pre,
305 irg_walk_func *post, void *env)
307 /* The root of the dominator tree should be the Start block. */
308 ir_node *root = get_irg_start_block(irg);
310 assert(is_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE)
311 && "The dominators of the irg must be consistent");
312 assert(root && "The start block of the graph is NULL?");
313 assert(get_dom_info(root)->idom == NULL
314 && "The start node in the graph must be the root of the dominator tree");
315 dom_tree_walk(root, pre, post, env);
318 void postdom_tree_walk_irg(ir_graph *irg, irg_walk_func *pre,
319 irg_walk_func *post, void *env)
321 /* The root of the post dominator tree should be the End block. */
322 ir_node *root = get_irg_end_block(irg);
324 assert(is_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_POSTDOMINANCE)
325 && "The dominators of the irg must be consistent");
326 assert(root && "The end block of the graph is NULL?");
327 assert(get_pdom_info(root)->idom == NULL
328 && "The End block node in the graph must be the root of the post dominator tree");
329 postdom_tree_walk(root, pre, post, env);
333 static void assign_tree_dom_pre_order(ir_node *bl, void *data)
335 unsigned *num = (unsigned*) data;
336 ir_dom_info *bi = get_dom_info(bl);
338 bi->tree_pre_num = (*num)++;
341 static void assign_tree_dom_pre_order_max(ir_node *bl, void *data)
343 ir_dom_info *bi = get_dom_info(bl);
346 unsigned children = 0;
349 for (p = bi->first; p; p = get_dom_info(p)->next) {
350 unsigned max_p = get_dom_info(p)->max_subtree_pre_num;
351 max = max > max_p ? max : max_p;
355 bi->max_subtree_pre_num = children > 0 ? max : bi->tree_pre_num;
356 assert(bi->max_subtree_pre_num >= bi->tree_pre_num);
359 static void assign_tree_postdom_pre_order(ir_node *bl, void *data)
361 unsigned *num = (unsigned*) data;
362 ir_dom_info *bi = get_pdom_info(bl);
364 bi->tree_pre_num = (*num)++;
367 static void assign_tree_postdom_pre_order_max(ir_node *bl, void *data)
369 ir_dom_info *bi = get_pdom_info(bl);
372 unsigned children = 0;
375 for (p = bi->first; p; p = get_pdom_info(p)->next) {
376 unsigned max_p = get_pdom_info(p)->max_subtree_pre_num;
377 max = max > max_p ? max : max_p;
381 bi->max_subtree_pre_num = children > 0 ? max : bi->tree_pre_num;
382 assert(bi->max_subtree_pre_num >= bi->tree_pre_num);
386 * count the number of blocks and clears the post dominance info
388 static void count_and_init_blocks_pdom(ir_node *bl, void *env)
390 int *n_blocks = (int *) env;
394 memset(get_pdom_info(bl), 0, sizeof(ir_dom_info));
395 set_Block_ipostdom(bl, NULL);
396 set_Block_postdom_pre_num(bl, -1);
397 set_Block_postdom_depth(bl, -1);
400 /** temporary type used while constructing the dominator / post dominator tree. */
401 typedef struct tmp_dom_info {
402 ir_node *block; /**< backlink */
404 struct tmp_dom_info *semi; /**< semidominator */
405 struct tmp_dom_info *parent;
406 struct tmp_dom_info *label; /**< used for LINK and EVAL */
407 struct tmp_dom_info *ancestor;/**< used for LINK and EVAL */
408 struct tmp_dom_info *dom; /**< After step 3, if the semidominator of w is
409 its immediate dominator, then w->dom is the
410 immediate dominator of w. Otherwise w->dom
411 is a vertex v whose number is smaller than
412 w and whose immediate dominator is also w's
413 immediate dominator. After step 4, w->dom
414 is the immediate dominator of w. */
415 struct tmp_dom_info *bucket; /**< set of vertices with same semidominator */
418 /** Struct to pass info through walker. */
426 * Walks Blocks along the out data structure. If recursion started with
427 * Start block misses control dead blocks.
429 static void init_tmp_dom_info(ir_node *bl, tmp_dom_info *parent,
430 tmp_dom_info *tdi_list, int *used, int n_blocks)
435 assert(is_Block(bl));
436 if (Block_block_visited(bl))
438 mark_Block_block_visited(bl);
439 set_Block_dom_pre_num(bl, *used);
441 assert(*used < n_blocks);
442 tdi = &tdi_list[*used];
447 tdi->ancestor = NULL;
449 tdi->parent = parent;
453 for (i = get_Block_n_cfg_outs_ka(bl) - 1; i >= 0; --i) {
454 ir_node *pred = get_Block_cfg_out_ka(bl, i);
455 /* can happen for half-optimized dead code (I've seen this in student
460 init_tmp_dom_info(pred, tdi, tdi_list, used, n_blocks);
465 * Walks Blocks along the control flow. If recursion started with
466 * End block misses blocks in endless loops.
468 static void init_tmp_pdom_info(ir_node *bl, tmp_dom_info *parent,
469 tmp_dom_info *tdi_list, int* used, int n_blocks)
474 assert(is_Block(bl));
475 if (get_irg_block_visited(current_ir_graph) == get_Block_block_visited(bl))
477 mark_Block_block_visited(bl);
478 set_Block_postdom_pre_num(bl, *used);
480 assert(*used < n_blocks);
481 tdi = &tdi_list[*used];
486 tdi->ancestor = NULL;
488 tdi->parent = parent;
492 for (i = get_Block_n_cfgpreds(bl) - 1; i >= 0; --i) {
493 ir_node *pred = get_Block_cfgpred_block(bl, i);
496 assert(is_Block(pred));
497 init_tmp_pdom_info(pred, tdi, tdi_list, used, n_blocks);
500 /* Handle keep-alives. Note that the preprocessing
501 in init_construction() had already killed all
502 phantom keep-alive edges. All remaining block keep-alives
503 are really edges to endless loops.
505 if (bl == get_irg_end_block(current_ir_graph)) {
506 ir_node *end = get_irg_end(current_ir_graph);
508 for (i = get_irn_arity(end) - 1; i >= 0; --i) {
509 ir_node *pred = get_irn_n(end, i);
512 init_tmp_pdom_info(pred, tdi, tdi_list, used, n_blocks);
517 static void dom_compress(tmp_dom_info *v)
519 assert (v->ancestor);
520 if (v->ancestor->ancestor) {
521 dom_compress (v->ancestor);
522 if (v->ancestor->label->semi < v->label->semi) {
523 v->label = v->ancestor->label;
525 v->ancestor = v->ancestor->ancestor;
530 * if V is a root, return v, else return the vertex u, not being the
531 * root, with minimum u->semi on the path from v to its root.
533 inline static tmp_dom_info *dom_eval(tmp_dom_info *v)
535 if (!v->ancestor) return v;
540 /** make V W's ancestor */
541 inline static void dom_link(tmp_dom_info *v, tmp_dom_info *w)
547 * Walker: count the number of blocks and clears the dominance info
549 static void count_and_init_blocks_dom(ir_node *bl, void *env)
551 int *n_blocks = (int *) env;
555 memset(get_dom_info(bl), 0, sizeof(ir_dom_info));
556 set_Block_idom(bl, NULL);
557 set_Block_dom_pre_num(bl, -1);
558 set_Block_dom_depth(bl, -1);
561 void compute_doms(ir_graph *irg)
563 ir_graph *rem = current_ir_graph;
564 int n_blocks, used, i, j;
565 tmp_dom_info *tdi_list; /* Ein Golf? */
567 current_ir_graph = irg;
569 /* Update graph state */
570 assert(get_irg_phase_state(irg) != phase_building);
572 /* Count the number of blocks in the graph. */
574 irg_block_walk_graph(irg, count_and_init_blocks_dom, NULL, &n_blocks);
576 /* Memory for temporary information. */
577 tdi_list = XMALLOCNZ(tmp_dom_info, n_blocks);
579 /* We need the out data structure. */
580 assure_irg_outs(irg);
582 /* this with a standard walker as passing the parent to the sons isn't
585 inc_irg_block_visited(irg);
586 init_tmp_dom_info(get_irg_start_block(irg), NULL, tdi_list, &used, n_blocks);
587 /* If not all blocks are reachable from Start by out edges this assertion
589 assert(used == n_blocks && "Precondition for dom construction violated"); */
590 assert(used <= n_blocks && "Precondition for dom construction violated");
594 for (i = n_blocks-1; i > 0; i--) { /* Don't iterate the root, it's done. */
595 tmp_dom_info *w = &tdi_list[i];
596 ir_node *block = w->block;
601 irn_arity = get_irn_arity(block);
602 for (j = 0; j < irn_arity; j++) {
603 ir_node *pred = get_Block_cfgpred(block, j);
604 ir_node *pred_block = get_nodes_block(pred);
607 if (is_Bad(pred) || (get_Block_dom_pre_num (pred_block) == -1))
608 continue; /* unreachable */
610 u = dom_eval (&tdi_list[get_Block_dom_pre_num(pred_block)]);
611 if (u->semi < w->semi)
615 /* handle keep-alives if we are at the end block */
616 if (block == get_irg_end_block(irg)) {
617 ir_node *end = get_irg_end(irg);
619 irn_arity = get_irn_arity(end);
620 for (j = 0; j < irn_arity; j++) {
621 ir_node *pred = get_irn_n(end, j);
624 if (!is_Block(pred) || get_Block_dom_pre_num(pred) == -1)
625 continue; /* unreachable */
627 u = dom_eval (&tdi_list[get_Block_dom_pre_num(pred)]);
628 if (u->semi < w->semi)
633 /* Add w to w->semi's bucket. w is in exactly one bucket, so
634 buckets can been implemented as linked lists. */
635 w->bucket = w->semi->bucket;
638 dom_link (w->parent, w);
641 while (w->parent->bucket) {
643 v = w->parent->bucket;
644 /* remove v from w->parent->bucket */
645 w->parent->bucket = v->bucket;
649 if (u->semi < v->semi)
656 tdi_list[0].dom = NULL;
657 set_Block_idom(tdi_list[0].block, NULL);
658 set_Block_dom_depth(tdi_list[0].block, 1);
659 for (i = 1; i < n_blocks; i++) {
660 tmp_dom_info *w = &tdi_list[i];
664 continue; /* control dead */
666 if (w->dom != w->semi)
667 w->dom = w->dom->dom;
668 set_Block_idom(w->block, w->dom->block);
670 /* blocks dominated by dead one's are still dead */
671 depth = get_Block_dom_depth(w->dom->block);
674 set_Block_dom_depth(w->block, depth);
680 /* Do a walk over the tree and assign the tree pre orders. */
682 unsigned tree_pre_order = 0;
683 dom_tree_walk(get_irg_start_block(irg), assign_tree_dom_pre_order,
684 assign_tree_dom_pre_order_max, &tree_pre_order);
686 current_ir_graph = rem;
687 set_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
690 void assure_doms(ir_graph *irg)
692 if (! is_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE))
696 void free_dom(ir_graph *irg)
698 /* Update graph state */
699 assert(get_irg_phase_state(irg) != phase_building);
700 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_DOMINANCE);
702 /* With the implementation right now there is nothing to free,
703 but better call it anyways... */
706 void compute_postdoms(ir_graph *irg)
708 ir_graph *rem = current_ir_graph;
709 int n_blocks, used, i, j;
710 tmp_dom_info *tdi_list;
712 current_ir_graph = irg;
714 /* Update graph state */
715 assert(get_irg_phase_state(irg) != phase_building);
717 /* Count the number of blocks in the graph. */
719 irg_block_walk_graph(irg, count_and_init_blocks_pdom, NULL, &n_blocks);
721 /* Memory for temporary information. */
722 tdi_list = XMALLOCNZ(tmp_dom_info, n_blocks);
724 /* We need the out data structure. */
725 assure_irg_outs(irg);
727 /* this with a standard walker as passing the parent to the sons isn't
730 inc_irg_block_visited(irg);
731 init_tmp_pdom_info(get_irg_end_block(irg), NULL, tdi_list, &used, n_blocks);
732 /* If not all blocks are reachable from End by cfg edges this assertion
734 assert(used == n_blocks && "Precondition for dom construction violated"); */
738 for (i = n_blocks-1; i > 0; i--) { /* Don't iterate the root, it's done. */
740 tmp_dom_info *w = &tdi_list[i];
744 irn_arity = get_Block_n_cfg_outs_ka(w->block);
745 for (j = 0; j < irn_arity; j++) {
746 ir_node *succ = get_Block_cfg_out_ka(w->block, j);
749 if (get_Block_postdom_pre_num (succ) == -1)
750 continue; /* endless-loop */
752 u = dom_eval (&tdi_list[get_Block_postdom_pre_num(succ)]);
753 if (u->semi < w->semi) w->semi = u->semi;
755 /* Add w to w->semi's bucket. w is in exactly one bucket, so
756 buckets can be implemented as linked lists. */
757 w->bucket = w->semi->bucket;
760 dom_link (w->parent, w);
763 while (w->parent->bucket) {
765 v = w->parent->bucket;
766 /* remove v from w->parent->bucket */
767 w->parent->bucket = v->bucket;
771 if (u->semi < v->semi)
778 tdi_list[0].dom = NULL;
779 set_Block_ipostdom(tdi_list[0].block, NULL);
780 set_Block_postdom_depth(tdi_list[0].block, 1);
781 for (i = 1; i < n_blocks; i++) {
782 tmp_dom_info *w = &tdi_list[i];
784 if (w->dom != w->semi) w->dom = w->dom->dom;
785 set_Block_ipostdom(w->block, w->dom->block);
786 set_Block_postdom_depth(w->block, get_Block_postdom_depth(w->dom->block) + 1);
792 /* Do a walk over the tree and assign the tree pre orders. */
794 unsigned tree_pre_order = 0;
795 postdom_tree_walk(get_irg_end_block(irg), assign_tree_postdom_pre_order,
796 assign_tree_postdom_pre_order_max, &tree_pre_order);
798 current_ir_graph = rem;
799 set_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_POSTDOMINANCE);
802 void assure_postdoms(ir_graph *irg)
804 if (! is_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_POSTDOMINANCE))
805 compute_postdoms(irg);
808 void free_postdom(ir_graph *irg)
810 /* Update graph state */
811 assert(get_irg_phase_state(irg) != phase_building);
812 clear_irg_state(irg, IR_GRAPH_STATE_CONSISTENT_POSTDOMINANCE);
814 /* With the implementation right now there is nothing to free,
815 but better call it anyways... */