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)
47 if (get_Block_dom_depth(bl) == -1) {
48 /* This block is not reachable from Start */
49 ir_graph *irg = get_irn_irg(bl);
50 return new_r_Bad(irg, mode_BB);
52 return get_dom_info(bl)->idom;
55 void set_Block_idom(ir_node *bl, ir_node *n)
57 ir_dom_info *bli = get_dom_info(bl);
61 /* Set the immediate dominator of bl to n */
65 * If we don't set the root of the dominator tree
66 * Append bl to the dominates queue of n.
69 ir_dom_info *ni = get_dom_info(n);
71 bli->next = ni->first;
76 ir_node *get_Block_ipostdom(const ir_node *bl)
78 if (get_Block_postdom_depth(bl) == -1) {
79 /* This block is not reachable from Start */
80 ir_graph *irg = get_irn_irg(bl);
81 return new_r_Bad(irg, mode_BB);
83 return get_pdom_info(bl)->idom;
86 void set_Block_ipostdom(ir_node *bl, ir_node *n)
88 ir_dom_info *bli = get_pdom_info(bl);
92 /* Set the immediate post dominator of bl to n */
96 * If we don't set the root of the post dominator tree
97 * Append bl to the post dominates queue of n.
100 ir_dom_info *ni = get_pdom_info(n);
102 bli->next = ni->first;
107 int get_Block_dom_pre_num(const ir_node *bl)
109 assert(is_Block(bl));
110 return get_dom_info(bl)->pre_num;
113 void set_Block_dom_pre_num(ir_node *bl, int num)
115 assert(is_Block(bl));
116 get_dom_info(bl)->pre_num = num;
119 int get_Block_dom_depth(const ir_node *bl)
121 assert(is_Block(bl));
122 return get_dom_info(bl)->dom_depth;
125 void set_Block_dom_depth(ir_node *bl, int depth)
127 assert(is_Block(bl));
128 get_dom_info(bl)->dom_depth = depth;
132 int get_Block_postdom_pre_num(const ir_node *bl)
134 assert(is_Block(bl));
135 return get_pdom_info(bl)->pre_num;
138 void set_Block_postdom_pre_num(ir_node *bl, int num)
140 assert(is_Block(bl));
141 get_pdom_info(bl)->pre_num = num;
144 int get_Block_postdom_depth(const ir_node *bl)
146 assert(is_Block(bl));
147 return get_pdom_info(bl)->dom_depth;
150 void set_Block_postdom_depth(ir_node *bl, int depth)
152 assert(is_Block(bl));
153 get_pdom_info(bl)->dom_depth = depth;
156 unsigned get_Block_dom_tree_pre_num(const ir_node *bl)
158 assert(is_Block(bl));
159 return get_dom_info(bl)->tree_pre_num;
162 unsigned get_Block_dom_max_subtree_pre_num(const ir_node *bl)
164 assert(is_Block(bl));
165 return get_dom_info(bl)->max_subtree_pre_num;
168 unsigned get_Block_pdom_tree_pre_num(const ir_node *bl)
170 assert(is_Block(bl));
171 return get_pdom_info(bl)->tree_pre_num;
174 unsigned get_Block_pdom_max_subtree_pre_num(const ir_node *bl)
176 assert(is_Block(bl));
177 return get_pdom_info(bl)->max_subtree_pre_num;
180 int block_dominates(const ir_node *a, const ir_node *b)
182 const ir_dom_info *ai, *bi;
184 if (is_Block(a) && is_Block(b)) {
185 ai = get_dom_info(a);
186 bi = get_dom_info(b);
187 return bi->tree_pre_num - ai->tree_pre_num
188 <= ai->max_subtree_pre_num - ai->tree_pre_num;
194 int block_strictly_dominates(const ir_node *a, const ir_node *b)
196 return (a != b) && block_dominates(a, b);
199 ir_node *node_smallest_common_dominator(ir_node *a, ir_node *b)
201 ir_node *bl_a = is_Block(a) ? a : get_nodes_block(a);
202 ir_node *bl_b = is_Block(b) ? b : get_nodes_block(b);
203 ir_node *dom_bl = NULL;
205 /* Check if block of a dominates block of b */
206 if (block_dominates(bl_a, bl_b))
208 /* Check if block of b dominates block of a */
209 else if (block_dominates(bl_b, bl_a))
212 /* walk up dominator tree and search for first block dominating a and b */
214 bl_a = get_Block_idom(bl_a);
216 assert(! is_Bad(bl_a) && "block is dead?");
218 if (block_dominates(bl_a, bl_b))
226 /* Get the first node in the list of nodes dominated by a given block. */
227 ir_node *get_Block_dominated_first(const ir_node *bl)
229 assert(is_Block(bl));
230 return get_dom_info(bl)->first;
233 ir_node *get_Block_dominated_next(const ir_node *bl)
235 assert(is_Block(bl));
236 return get_dom_info(bl)->next;
239 int block_postdominates(const ir_node *a, const ir_node *b)
241 const ir_dom_info *ai, *bi;
243 if (is_Block(a) && is_Block(b)) {
244 ai = get_pdom_info(a);
245 bi = get_pdom_info(b);
246 return bi->tree_pre_num - ai->tree_pre_num
247 <= ai->max_subtree_pre_num - ai->tree_pre_num;
253 int block_strictly_postdominates(const ir_node *a, const ir_node *b)
255 return (a != b) && block_postdominates(a, b);
258 ir_node *get_Block_postdominated_first(const ir_node *bl)
260 assert(is_Block(bl));
261 return get_pdom_info(bl)->first;
264 ir_node *get_Block_postdominated_next(const ir_node *bl)
266 assert(is_Block(bl));
267 return get_pdom_info(bl)->next;
270 void dom_tree_walk(ir_node *bl, irg_walk_func *pre,
271 irg_walk_func *post, void *env)
278 dominates_for_each(bl, p) {
279 dom_tree_walk(p, pre, post, env);
286 void postdom_tree_walk(ir_node *bl, irg_walk_func *pre,
287 irg_walk_func *post, void *env)
294 postdominates_for_each(bl, p) {
295 postdom_tree_walk(p, pre, post, env);
302 void dom_tree_walk_irg(ir_graph *irg, irg_walk_func *pre,
303 irg_walk_func *post, void *env)
305 /* The root of the dominator tree should be the Start block. */
306 ir_node *root = get_irg_start_block(irg);
308 assert(irg_has_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE)
309 && "The dominators of the irg must be consistent");
310 assert(root && "The start block of the graph is NULL?");
311 assert(get_dom_info(root)->idom == NULL
312 && "The start node in the graph must be the root of the dominator tree");
313 dom_tree_walk(root, pre, post, env);
316 void postdom_tree_walk_irg(ir_graph *irg, irg_walk_func *pre,
317 irg_walk_func *post, void *env)
319 /* The root of the post dominator tree should be the End block. */
320 ir_node *root = get_irg_end_block(irg);
322 assert(irg_has_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_POSTDOMINANCE)
323 && "The dominators of the irg must be consistent");
324 assert(root && "The end block of the graph is NULL?");
325 assert(get_pdom_info(root)->idom == NULL
326 && "The End block node in the graph must be the root of the post dominator tree");
327 postdom_tree_walk(root, pre, post, env);
331 static void assign_tree_dom_pre_order(ir_node *bl, void *data)
333 unsigned *num = (unsigned*) data;
334 ir_dom_info *bi = get_dom_info(bl);
336 bi->tree_pre_num = (*num)++;
339 static void assign_tree_dom_pre_order_max(ir_node *bl, void *data)
341 ir_dom_info *bi = get_dom_info(bl);
344 unsigned children = 0;
347 for (p = bi->first; p; p = get_dom_info(p)->next) {
348 unsigned max_p = get_dom_info(p)->max_subtree_pre_num;
349 max = max > max_p ? max : max_p;
353 bi->max_subtree_pre_num = children > 0 ? max : bi->tree_pre_num;
354 assert(bi->max_subtree_pre_num >= bi->tree_pre_num);
357 static void assign_tree_postdom_pre_order(ir_node *bl, void *data)
359 unsigned *num = (unsigned*) data;
360 ir_dom_info *bi = get_pdom_info(bl);
362 bi->tree_pre_num = (*num)++;
365 static void assign_tree_postdom_pre_order_max(ir_node *bl, void *data)
367 ir_dom_info *bi = get_pdom_info(bl);
370 unsigned children = 0;
373 for (p = bi->first; p; p = get_pdom_info(p)->next) {
374 unsigned max_p = get_pdom_info(p)->max_subtree_pre_num;
375 max = max > max_p ? max : max_p;
379 bi->max_subtree_pre_num = children > 0 ? max : bi->tree_pre_num;
380 assert(bi->max_subtree_pre_num >= bi->tree_pre_num);
384 * count the number of blocks and clears the post dominance info
386 static void count_and_init_blocks_pdom(ir_node *bl, void *env)
388 int *n_blocks = (int *) env;
392 memset(get_pdom_info(bl), 0, sizeof(ir_dom_info));
393 set_Block_ipostdom(bl, NULL);
394 set_Block_postdom_pre_num(bl, -1);
395 set_Block_postdom_depth(bl, -1);
398 /** temporary type used while constructing the dominator / post dominator tree. */
399 typedef struct tmp_dom_info {
400 ir_node *block; /**< backlink */
402 struct tmp_dom_info *semi; /**< semidominator */
403 struct tmp_dom_info *parent;
404 struct tmp_dom_info *label; /**< used for LINK and EVAL */
405 struct tmp_dom_info *ancestor;/**< used for LINK and EVAL */
406 struct tmp_dom_info *dom; /**< After step 3, if the semidominator of w is
407 its immediate dominator, then w->dom is the
408 immediate dominator of w. Otherwise w->dom
409 is a vertex v whose number is smaller than
410 w and whose immediate dominator is also w's
411 immediate dominator. After step 4, w->dom
412 is the immediate dominator of w. */
413 struct tmp_dom_info *bucket; /**< set of vertices with same semidominator */
416 /** Struct to pass info through walker. */
424 * Walks Blocks along the out data structure. If recursion started with
425 * Start block misses control dead blocks.
427 static void init_tmp_dom_info(ir_node *bl, tmp_dom_info *parent,
428 tmp_dom_info *tdi_list, int *used, int n_blocks)
433 if (Block_block_visited(bl))
435 mark_Block_block_visited(bl);
436 set_Block_dom_pre_num(bl, *used);
438 assert(*used < n_blocks);
439 tdi = &tdi_list[*used];
444 tdi->ancestor = NULL;
446 tdi->parent = parent;
450 for (i = get_Block_n_cfg_outs_ka(bl) - 1; i >= 0; --i) {
451 ir_node *pred = get_Block_cfg_out_ka(bl, i);
452 /* can happen for half-optimized dead code (I've seen this in student
457 init_tmp_dom_info(pred, tdi, tdi_list, used, n_blocks);
462 * Walks Blocks along the control flow. If recursion started with
463 * End block misses blocks in endless loops.
465 static void init_tmp_pdom_info(ir_node *bl, tmp_dom_info *parent,
466 tmp_dom_info *tdi_list, int* used, int n_blocks)
471 if (get_irg_block_visited(current_ir_graph) == get_Block_block_visited(bl))
473 mark_Block_block_visited(bl);
474 set_Block_postdom_pre_num(bl, *used);
476 assert(*used < n_blocks);
477 tdi = &tdi_list[*used];
482 tdi->ancestor = NULL;
484 tdi->parent = parent;
488 for (i = get_Block_n_cfgpreds(bl) - 1; i >= 0; --i) {
489 ir_node *pred = get_Block_cfgpred_block(bl, i);
492 init_tmp_pdom_info(pred, tdi, tdi_list, used, n_blocks);
495 /* Handle keep-alives. Note that the preprocessing
496 in init_construction() had already killed all
497 phantom keep-alive edges. All remaining block keep-alives
498 are really edges to endless loops.
500 if (bl == get_irg_end_block(current_ir_graph)) {
501 ir_node *end = get_irg_end(current_ir_graph);
503 for (i = get_irn_arity(end) - 1; i >= 0; --i) {
504 ir_node *pred = get_irn_n(end, i);
507 init_tmp_pdom_info(pred, tdi, tdi_list, used, n_blocks);
512 static void dom_compress(tmp_dom_info *v)
514 assert (v->ancestor);
515 if (v->ancestor->ancestor) {
516 dom_compress (v->ancestor);
517 if (v->ancestor->label->semi < v->label->semi) {
518 v->label = v->ancestor->label;
520 v->ancestor = v->ancestor->ancestor;
525 * if V is a root, return v, else return the vertex u, not being the
526 * root, with minimum u->semi on the path from v to its root.
528 inline static tmp_dom_info *dom_eval(tmp_dom_info *v)
530 if (!v->ancestor) return v;
535 /** make V W's ancestor */
536 inline static void dom_link(tmp_dom_info *v, tmp_dom_info *w)
542 * Walker: count the number of blocks and clears the dominance info
544 static void count_and_init_blocks_dom(ir_node *bl, void *env)
546 int *n_blocks = (int *) env;
550 memset(get_dom_info(bl), 0, sizeof(ir_dom_info));
551 set_Block_idom(bl, NULL);
552 set_Block_dom_pre_num(bl, -1);
553 set_Block_dom_depth(bl, -1);
556 void compute_doms(ir_graph *irg)
558 ir_graph *rem = current_ir_graph;
559 int n_blocks, used, i, j;
560 tmp_dom_info *tdi_list; /* Ein Golf? */
562 current_ir_graph = irg;
564 /* Update graph state */
565 assert(!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_CONSTRUCTION));
567 /* Count the number of blocks in the graph. */
569 irg_block_walk_graph(irg, count_and_init_blocks_dom, NULL, &n_blocks);
571 /* Memory for temporary information. */
572 tdi_list = XMALLOCNZ(tmp_dom_info, n_blocks);
574 /* We need the out data structure. */
575 assure_irg_outs(irg);
577 /* this with a standard walker as passing the parent to the sons isn't
580 inc_irg_block_visited(irg);
581 init_tmp_dom_info(get_irg_start_block(irg), NULL, tdi_list, &used, n_blocks);
582 /* If not all blocks are reachable from Start by out edges this assertion
584 assert(used == n_blocks && "Precondition for dom construction violated"); */
585 assert(used <= n_blocks && "Precondition for dom construction violated");
589 for (i = n_blocks-1; i > 0; i--) { /* Don't iterate the root, it's done. */
590 tmp_dom_info *w = &tdi_list[i];
591 ir_node *block = w->block;
596 irn_arity = get_irn_arity(block);
597 for (j = 0; j < irn_arity; j++) {
598 ir_node *pred = get_Block_cfgpred(block, j);
599 ir_node *pred_block = get_nodes_block(pred);
602 if (is_Bad(pred) || (get_Block_dom_pre_num (pred_block) == -1))
603 continue; /* unreachable */
605 u = dom_eval (&tdi_list[get_Block_dom_pre_num(pred_block)]);
606 if (u->semi < w->semi)
610 /* handle keep-alives if we are at the end block */
611 if (block == get_irg_end_block(irg)) {
612 ir_node *end = get_irg_end(irg);
614 irn_arity = get_irn_arity(end);
615 for (j = 0; j < irn_arity; j++) {
616 ir_node *pred = get_irn_n(end, j);
619 if (!is_Block(pred) || get_Block_dom_pre_num(pred) == -1)
620 continue; /* unreachable */
622 u = dom_eval (&tdi_list[get_Block_dom_pre_num(pred)]);
623 if (u->semi < w->semi)
628 /* Add w to w->semi's bucket. w is in exactly one bucket, so
629 buckets can been implemented as linked lists. */
630 w->bucket = w->semi->bucket;
633 dom_link (w->parent, w);
636 while (w->parent->bucket) {
638 v = w->parent->bucket;
639 /* remove v from w->parent->bucket */
640 w->parent->bucket = v->bucket;
644 if (u->semi < v->semi)
651 tdi_list[0].dom = NULL;
652 set_Block_idom(tdi_list[0].block, NULL);
653 set_Block_dom_depth(tdi_list[0].block, 1);
654 for (i = 1; i < n_blocks; i++) {
655 tmp_dom_info *w = &tdi_list[i];
659 continue; /* control dead */
661 if (w->dom != w->semi)
662 w->dom = w->dom->dom;
663 set_Block_idom(w->block, w->dom->block);
665 /* blocks dominated by dead one's are still dead */
666 depth = get_Block_dom_depth(w->dom->block);
669 set_Block_dom_depth(w->block, depth);
675 /* Do a walk over the tree and assign the tree pre orders. */
677 unsigned tree_pre_order = 0;
678 dom_tree_walk(get_irg_start_block(irg), assign_tree_dom_pre_order,
679 assign_tree_dom_pre_order_max, &tree_pre_order);
681 current_ir_graph = rem;
682 add_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
685 void assure_doms(ir_graph *irg)
687 if (! irg_has_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE))
691 void free_dom(ir_graph *irg)
693 /* Update graph state */
694 clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE);
696 /* With the implementation right now there is nothing to free */
698 /* free dominance frontiers */
699 ir_free_dominance_frontiers(irg);
702 void compute_postdoms(ir_graph *irg)
704 ir_graph *rem = current_ir_graph;
705 int n_blocks, used, i, j;
706 tmp_dom_info *tdi_list;
708 current_ir_graph = irg;
710 /* Update graph state */
711 assert(!irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_CONSTRUCTION));
713 /* Count the number of blocks in the graph. */
715 irg_block_walk_graph(irg, count_and_init_blocks_pdom, NULL, &n_blocks);
717 /* Memory for temporary information. */
718 tdi_list = XMALLOCNZ(tmp_dom_info, n_blocks);
720 /* We need the out data structure. */
721 assure_irg_outs(irg);
723 /* this with a standard walker as passing the parent to the sons isn't
726 inc_irg_block_visited(irg);
727 init_tmp_pdom_info(get_irg_end_block(irg), NULL, tdi_list, &used, n_blocks);
728 /* If not all blocks are reachable from End by cfg edges this assertion
730 assert(used == n_blocks && "Precondition for dom construction violated"); */
734 for (i = n_blocks-1; i > 0; i--) { /* Don't iterate the root, it's done. */
736 tmp_dom_info *w = &tdi_list[i];
740 irn_arity = get_Block_n_cfg_outs_ka(w->block);
741 for (j = 0; j < irn_arity; j++) {
742 ir_node *succ = get_Block_cfg_out_ka(w->block, j);
745 if (get_Block_postdom_pre_num (succ) == -1)
746 continue; /* endless-loop */
748 u = dom_eval (&tdi_list[get_Block_postdom_pre_num(succ)]);
749 if (u->semi < w->semi) w->semi = u->semi;
751 /* Add w to w->semi's bucket. w is in exactly one bucket, so
752 buckets can be implemented as linked lists. */
753 w->bucket = w->semi->bucket;
756 dom_link (w->parent, w);
759 while (w->parent->bucket) {
761 v = w->parent->bucket;
762 /* remove v from w->parent->bucket */
763 w->parent->bucket = v->bucket;
767 if (u->semi < v->semi)
774 tdi_list[0].dom = NULL;
775 set_Block_ipostdom(tdi_list[0].block, NULL);
776 set_Block_postdom_depth(tdi_list[0].block, 1);
777 for (i = 1; i < n_blocks; i++) {
778 tmp_dom_info *w = &tdi_list[i];
780 if (w->dom != w->semi) w->dom = w->dom->dom;
781 set_Block_ipostdom(w->block, w->dom->block);
782 set_Block_postdom_depth(w->block, get_Block_postdom_depth(w->dom->block) + 1);
788 /* Do a walk over the tree and assign the tree pre orders. */
790 unsigned tree_pre_order = 0;
791 postdom_tree_walk(get_irg_end_block(irg), assign_tree_postdom_pre_order,
792 assign_tree_postdom_pre_order_max, &tree_pre_order);
794 current_ir_graph = rem;
795 add_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_POSTDOMINANCE);
798 void assure_postdoms(ir_graph *irg)
800 if (! irg_has_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_POSTDOMINANCE))
801 compute_postdoms(irg);
804 void free_postdom(ir_graph *irg)
806 /* Update graph state */
807 clear_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_POSTDOMINANCE);