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
36 #include "irgraph_t.h"
43 #define get_dom_info(bl) (&(bl)->attr.block.dom)
44 #define get_pdom_info(bl) (&(bl)->attr.block.pdom)
46 /*--------------------------------------------------------------------*/
47 /** Accessing the dominator and post dominator data structures **/
48 /*--------------------------------------------------------------------*/
50 ir_node *get_Block_idom(const ir_node *bl)
53 if (get_Block_dom_depth(bl) == -1) {
54 /* This block is not reachable from Start */
55 ir_graph *irg = get_irn_irg(bl);
56 return new_r_Bad(irg, mode_BB);
58 return get_dom_info(bl)->idom;
61 void set_Block_idom(ir_node *bl, ir_node *n)
63 ir_dom_info *bli = get_dom_info(bl);
67 /* Set the immediate dominator of bl to n */
71 * If we don't set the root of the dominator tree
72 * Append bl to the dominates queue of n.
75 ir_dom_info *ni = get_dom_info(n);
77 bli->next = ni->first;
82 ir_node *get_Block_ipostdom(const ir_node *bl)
85 if (get_Block_postdom_depth(bl) == -1) {
86 /* This block is not reachable from Start */
87 ir_graph *irg = get_irn_irg(bl);
88 return new_r_Bad(irg, mode_BB);
90 return get_pdom_info(bl)->idom;
93 void set_Block_ipostdom(ir_node *bl, ir_node *n)
95 ir_dom_info *bli = get_pdom_info(bl);
99 /* Set the immediate post dominator of bl to n */
103 * If we don't set the root of the post dominator tree
104 * Append bl to the post dominates queue of n.
107 ir_dom_info *ni = get_pdom_info(n);
109 bli->next = ni->first;
114 int get_Block_dom_pre_num(const ir_node *bl)
116 assert(is_Block(bl));
117 return get_dom_info(bl)->pre_num;
120 void set_Block_dom_pre_num(ir_node *bl, int num)
122 assert(is_Block(bl));
123 get_dom_info(bl)->pre_num = num;
126 int get_Block_dom_depth(const ir_node *bl)
128 assert(is_Block(bl));
129 return get_dom_info(bl)->dom_depth;
132 void set_Block_dom_depth(ir_node *bl, int depth)
134 assert(is_Block(bl));
135 get_dom_info(bl)->dom_depth = depth;
139 int get_Block_postdom_pre_num(const ir_node *bl)
141 assert(is_Block(bl));
142 return get_pdom_info(bl)->pre_num;
145 void set_Block_postdom_pre_num(ir_node *bl, int num)
147 assert(is_Block(bl));
148 get_pdom_info(bl)->pre_num = num;
151 int get_Block_postdom_depth(const ir_node *bl)
153 assert(is_Block(bl));
154 return get_pdom_info(bl)->dom_depth;
157 void set_Block_postdom_depth(ir_node *bl, int depth)
159 assert(is_Block(bl));
160 get_pdom_info(bl)->dom_depth = depth;
163 unsigned get_Block_dom_tree_pre_num(const ir_node *bl)
165 assert(is_Block(bl));
166 return get_dom_info(bl)->tree_pre_num;
169 unsigned get_Block_dom_max_subtree_pre_num(const ir_node *bl)
171 assert(is_Block(bl));
172 return get_dom_info(bl)->max_subtree_pre_num;
175 unsigned get_Block_pdom_tree_pre_num(const ir_node *bl)
177 assert(is_Block(bl));
178 return get_pdom_info(bl)->tree_pre_num;
181 unsigned get_Block_pdom_max_subtree_pre_num(const ir_node *bl)
183 assert(is_Block(bl));
184 return get_pdom_info(bl)->max_subtree_pre_num;
187 /* Check, if a block dominates another block. */
188 int block_dominates(const ir_node *a, const ir_node *b)
190 const ir_dom_info *ai, *bi;
192 if (is_Block(a) && is_Block(b)) {
193 ai = get_dom_info(a);
194 bi = get_dom_info(b);
195 return bi->tree_pre_num - ai->tree_pre_num
196 <= ai->max_subtree_pre_num - ai->tree_pre_num;
202 /* Check, if a block strictly dominates another block. */
203 int block_strictly_dominates(const ir_node *a, const ir_node *b)
205 return (a != b) && block_dominates(a, b);
208 /* Returns the smallest common dominator block of two nodes. */
209 ir_node *node_smallest_common_dominator(ir_node *a, ir_node *b)
211 ir_node *bl_a = is_Block(a) ? a : get_nodes_block(a);
212 ir_node *bl_b = is_Block(b) ? b : get_nodes_block(b);
213 ir_node *dom_bl = NULL;
215 /* Check if block of a dominates block of b */
216 if (block_dominates(bl_a, bl_b))
218 /* Check if block of b dominates block of a */
219 else if (block_dominates(bl_b, bl_a))
222 /* walk up dominator tree and search for first block dominating a and b */
224 bl_a = get_Block_idom(bl_a);
226 assert(! is_Bad(bl_a) && "block is dead?");
228 if (block_dominates(bl_a, bl_b))
236 /* Returns the smallest common dominator block of all users of a node. */
237 ir_node *node_users_smallest_common_dominator(ir_node *irn, int handle_phi)
239 int n, j, i = 0, success;
240 ir_node **user_blocks, *dom_bl;
241 const ir_edge_t *edge;
243 assert(! is_Block(irn) && "WRONG USAGE of node_users_smallest_common_dominator");
244 assert(edges_activated(get_irn_irg(irn)) && "need edges activated");
246 n = get_irn_n_edges(irn);
248 /* get array to hold all block of the node users */
249 NEW_ARR_A(ir_node *, user_blocks, n);
250 foreach_out_edge(irn, edge) {
251 ir_node *src = get_edge_src_irn(edge);
253 if (is_Phi(src) && handle_phi) {
254 /* get the corresponding cfg predecessor block if phi handling requested */
255 j = get_edge_src_pos(edge);
256 assert(j >= 0 && "kaputt");
257 user_blocks[i++] = get_Block_cfgpred_block(get_nodes_block(src), j);
260 user_blocks[i++] = is_Block(src) ? src : get_nodes_block(src);
263 assert(i == n && "get_irn_n_edges probably broken");
265 /* in case of only one user: return the block of the user */
267 return user_blocks[0];
270 /* search the smallest block dominating all user blocks */
272 dom_bl = node_smallest_common_dominator(user_blocks[i], user_blocks[i + 1]);
275 /* check if this block dominates all remaining blocks as well */
276 for (j = i + 2; j < n; j++) {
277 if (! block_dominates(dom_bl, user_blocks[j]))
284 /* inherit the dominator block of the first (i + 1) users */
285 user_blocks[++i] = dom_bl;
288 assert(success && "no block found dominating all users");
294 /* Get the first node in the list of nodes dominated by a given block. */
295 ir_node *get_Block_dominated_first(const ir_node *bl)
297 assert(is_Block(bl));
298 return get_dom_info(bl)->first;
301 /* Get the next node in a list of nodes which are dominated by some
303 ir_node *get_Block_dominated_next(const ir_node *bl)
305 assert(is_Block(bl));
306 return get_dom_info(bl)->next;
309 /* Check, if a block post dominates another block. */
310 int block_postdominates(const ir_node *a, const ir_node *b)
312 const ir_dom_info *ai, *bi;
314 if (is_Block(a) && is_Block(b)) {
315 ai = get_pdom_info(a);
316 bi = get_pdom_info(b);
317 return bi->tree_pre_num - ai->tree_pre_num
318 <= ai->max_subtree_pre_num - ai->tree_pre_num;
324 /* Check, if a block strictly dominates another block. */
325 int block_strictly_postdominates(const ir_node *a, const ir_node *b)
327 return (a != b) && block_postdominates(a, b);
331 /* Get the first node in the list of nodes post dominated by a given block. */
332 ir_node *get_Block_postdominated_first(const ir_node *bl)
334 assert(is_Block(bl));
335 return get_pdom_info(bl)->first;
338 /* Get the next node in a list of nodes which are post dominated by some
340 ir_node *get_Block_postdominated_next(const ir_node *bl)
342 assert(is_Block(bl));
343 return get_pdom_info(bl)->next;
346 /* Visit all nodes in the dominator subtree of a given node. */
347 void dom_tree_walk(ir_node *bl, irg_walk_func *pre,
348 irg_walk_func *post, void *env)
355 dominates_for_each(bl, p) {
356 dom_tree_walk(p, pre, post, env);
363 /* Visit all nodes in the post dominator subtree of a given node. */
364 void postdom_tree_walk(ir_node *bl, irg_walk_func *pre,
365 irg_walk_func *post, void *env)
372 postdominates_for_each(bl, p) {
373 postdom_tree_walk(p, pre, post, env);
380 /* Walk over the dominator tree of an irg starting at the root. */
381 void dom_tree_walk_irg(ir_graph *irg, irg_walk_func *pre,
382 irg_walk_func *post, void *env)
384 /* The root of the dominator tree should be the Start block. */
385 ir_node *root = get_irg_start_block(irg);
387 assert(irg->dom_state == dom_consistent
388 && "The dominators of the irg must be consistent");
389 assert(root && "The start block of the graph is NULL?");
390 assert(get_dom_info(root)->idom == NULL
391 && "The start node in the graph must be the root of the dominator tree");
392 dom_tree_walk(root, pre, post, env);
395 /* Walk over the post dominator tree of an irg starting at the root. */
396 void postdom_tree_walk_irg(ir_graph *irg, irg_walk_func *pre,
397 irg_walk_func *post, void *env)
399 /* The root of the dominator tree should be the End block. */
400 ir_node *root = get_irg_end_block(irg);
402 assert(irg->pdom_state == dom_consistent
403 && "The dominators of the irg must be consistent");
404 assert(root && "The end block of the graph is NULL?");
405 assert(get_pdom_info(root)->idom == NULL
406 && "The End block node in the graph must be the root of the post dominator tree");
407 postdom_tree_walk(root, pre, post, env);
411 static void assign_tree_dom_pre_order(ir_node *bl, void *data)
413 unsigned *num = (unsigned*) data;
414 ir_dom_info *bi = get_dom_info(bl);
416 bi->tree_pre_num = (*num)++;
419 static void assign_tree_dom_pre_order_max(ir_node *bl, void *data)
421 ir_dom_info *bi = get_dom_info(bl);
424 unsigned children = 0;
427 for (p = bi->first; p; p = get_dom_info(p)->next) {
428 unsigned max_p = get_dom_info(p)->max_subtree_pre_num;
429 max = max > max_p ? max : max_p;
433 bi->max_subtree_pre_num = children > 0 ? max : bi->tree_pre_num;
434 assert(bi->max_subtree_pre_num >= bi->tree_pre_num);
437 static void assign_tree_postdom_pre_order(ir_node *bl, void *data)
439 unsigned *num = (unsigned*) data;
440 ir_dom_info *bi = get_pdom_info(bl);
442 bi->tree_pre_num = (*num)++;
445 static void assign_tree_postdom_pre_order_max(ir_node *bl, void *data)
447 ir_dom_info *bi = get_pdom_info(bl);
450 unsigned children = 0;
453 for (p = bi->first; p; p = get_pdom_info(p)->next) {
454 unsigned max_p = get_pdom_info(p)->max_subtree_pre_num;
455 max = max > max_p ? max : max_p;
459 bi->max_subtree_pre_num = children > 0 ? max : bi->tree_pre_num;
460 assert(bi->max_subtree_pre_num >= bi->tree_pre_num);
463 /*--------------------------------------------------------------------*/
464 /* Building and Removing the dominator data structure */
465 /*--------------------------------------------------------------------*/
468 * count the number of blocks and clears the post dominance info
470 static void count_and_init_blocks_pdom(ir_node *bl, void *env)
472 int *n_blocks = (int *) env;
476 memset(get_pdom_info(bl), 0, sizeof(ir_dom_info));
477 set_Block_ipostdom(bl, NULL);
478 set_Block_postdom_pre_num(bl, -1);
479 set_Block_postdom_depth(bl, -1);
482 /** temporary type used while constructing the dominator / post dominator tree. */
483 typedef struct tmp_dom_info {
484 ir_node *block; /**< backlink */
486 struct tmp_dom_info *semi; /**< semidominator */
487 struct tmp_dom_info *parent;
488 struct tmp_dom_info *label; /**< used for LINK and EVAL */
489 struct tmp_dom_info *ancestor;/**< used for LINK and EVAL */
490 struct tmp_dom_info *dom; /**< After step 3, if the semidominator of w is
491 its immediate dominator, then w->dom is the
492 immediate dominator of w. Otherwise w->dom
493 is a vertex v whose number is smaller than
494 w and whose immediate dominator is also w's
495 immediate dominator. After step 4, w->dom
496 is the immediate dominator of w. */
497 struct tmp_dom_info *bucket; /**< set of vertices with same semidominator */
500 /** Struct to pass info through walker. */
508 * Walks Blocks along the out data structure. If recursion started with
509 * Start block misses control dead blocks.
511 static void init_tmp_dom_info(ir_node *bl, tmp_dom_info *parent,
512 tmp_dom_info *tdi_list, int *used, int n_blocks)
517 assert(is_Block(bl));
518 if (Block_block_visited(bl))
520 mark_Block_block_visited(bl);
521 set_Block_dom_pre_num(bl, *used);
523 assert(*used < n_blocks);
524 tdi = &tdi_list[*used];
529 tdi->ancestor = NULL;
531 tdi->parent = parent;
535 for (i = get_Block_n_cfg_outs_ka(bl) - 1; i >= 0; --i) {
536 ir_node *pred = get_Block_cfg_out_ka(bl, i);
537 /* can happen for half-optimized dead code (I've seen this in student
542 init_tmp_dom_info(pred, tdi, tdi_list, used, n_blocks);
547 * Walks Blocks along the control flow. If recursion started with
548 * End block misses blocks in endless loops.
550 static void init_tmp_pdom_info(ir_node *bl, tmp_dom_info *parent,
551 tmp_dom_info *tdi_list, int* used, int n_blocks)
556 assert(is_Block(bl));
557 if (get_irg_block_visited(current_ir_graph) == get_Block_block_visited(bl))
559 mark_Block_block_visited(bl);
560 set_Block_postdom_pre_num(bl, *used);
562 assert(*used < n_blocks);
563 tdi = &tdi_list[*used];
568 tdi->ancestor = NULL;
570 tdi->parent = parent;
574 for (i = get_Block_n_cfgpreds(bl) - 1; i >= 0; --i) {
575 ir_node *pred = get_Block_cfgpred_block(bl, i);
578 assert(is_Block(pred));
579 init_tmp_pdom_info(pred, tdi, tdi_list, used, n_blocks);
582 /* Handle keep-alives. Note that the preprocessing
583 in init_construction() had already killed all
584 phantom keep-alive edges. All remaining block keep-alives
585 are really edges to endless loops.
587 if (bl == get_irg_end_block(current_ir_graph)) {
588 ir_node *end = get_irg_end(current_ir_graph);
590 for (i = get_irn_arity(end) - 1; i >= 0; --i) {
591 ir_node *pred = get_irn_n(end, i);
594 init_tmp_pdom_info(pred, tdi, tdi_list, used, n_blocks);
599 static void dom_compress(tmp_dom_info *v)
601 assert (v->ancestor);
602 if (v->ancestor->ancestor) {
603 dom_compress (v->ancestor);
604 if (v->ancestor->label->semi < v->label->semi) {
605 v->label = v->ancestor->label;
607 v->ancestor = v->ancestor->ancestor;
612 * if V is a root, return v, else return the vertex u, not being the
613 * root, with minimum u->semi on the path from v to its root.
615 inline static tmp_dom_info *dom_eval(tmp_dom_info *v)
617 if (!v->ancestor) return v;
622 /** make V W's ancestor */
623 inline static void dom_link(tmp_dom_info *v, tmp_dom_info *w)
629 * Walker: count the number of blocks and clears the dominance info
631 static void count_and_init_blocks_dom(ir_node *bl, void *env)
633 int *n_blocks = (int *) env;
637 memset(get_dom_info(bl), 0, sizeof(ir_dom_info));
638 set_Block_idom(bl, NULL);
639 set_Block_dom_pre_num(bl, -1);
640 set_Block_dom_depth(bl, -1);
643 /* Computes the dominator trees. Sets a flag in irg to "dom_consistent".
644 If the control flow of the graph is changed this flag must be set to
645 "dom_inconsistent". */
646 void compute_doms(ir_graph *irg)
648 ir_graph *rem = current_ir_graph;
649 int n_blocks, used, i, j;
650 tmp_dom_info *tdi_list; /* Ein Golf? */
652 current_ir_graph = irg;
654 /* Update graph state */
655 assert(get_irg_phase_state(irg) != phase_building);
656 irg->dom_state = dom_consistent;
658 /* Count the number of blocks in the graph. */
660 irg_block_walk_graph(irg, count_and_init_blocks_dom, NULL, &n_blocks);
662 /* Memory for temporary information. */
663 tdi_list = XMALLOCNZ(tmp_dom_info, n_blocks);
665 /* We need the out data structure. */
666 assure_irg_outs(irg);
668 /* this with a standard walker as passing the parent to the sons isn't
671 inc_irg_block_visited(irg);
672 init_tmp_dom_info(get_irg_start_block(irg), NULL, tdi_list, &used, n_blocks);
673 /* If not all blocks are reachable from Start by out edges this assertion
675 assert(used == n_blocks && "Precondition for dom construction violated"); */
676 assert(used <= n_blocks && "Precondition for dom construction violated");
680 for (i = n_blocks-1; i > 0; i--) { /* Don't iterate the root, it's done. */
681 tmp_dom_info *w = &tdi_list[i];
682 ir_node *block = w->block;
687 irn_arity = get_irn_arity(block);
688 for (j = 0; j < irn_arity; j++) {
689 ir_node *pred = get_Block_cfgpred(block, j);
690 ir_node *pred_block = get_nodes_block(pred);
693 if (is_Bad(pred) || (get_Block_dom_pre_num (pred_block) == -1))
694 continue; /* unreachable */
696 u = dom_eval (&tdi_list[get_Block_dom_pre_num(pred_block)]);
697 if (u->semi < w->semi)
701 /* handle keep-alives if we are at the end block */
702 if (block == get_irg_end_block(irg)) {
703 ir_node *end = get_irg_end(irg);
705 irn_arity = get_irn_arity(end);
706 for (j = 0; j < irn_arity; j++) {
707 ir_node *pred = get_irn_n(end, j);
710 if (!is_Block(pred) || get_Block_dom_pre_num(pred) == -1)
711 continue; /* unreachable */
713 u = dom_eval (&tdi_list[get_Block_dom_pre_num(pred)]);
714 if (u->semi < w->semi)
719 /* Add w to w->semi's bucket. w is in exactly one bucket, so
720 buckets can been implemented as linked lists. */
721 w->bucket = w->semi->bucket;
724 dom_link (w->parent, w);
727 while (w->parent->bucket) {
729 v = w->parent->bucket;
730 /* remove v from w->parent->bucket */
731 w->parent->bucket = v->bucket;
735 if (u->semi < v->semi)
742 tdi_list[0].dom = NULL;
743 set_Block_idom(tdi_list[0].block, NULL);
744 set_Block_dom_depth(tdi_list[0].block, 1);
745 for (i = 1; i < n_blocks; i++) {
746 tmp_dom_info *w = &tdi_list[i];
750 continue; /* control dead */
752 if (w->dom != w->semi)
753 w->dom = w->dom->dom;
754 set_Block_idom(w->block, w->dom->block);
756 /* blocks dominated by dead one's are still dead */
757 depth = get_Block_dom_depth(w->dom->block);
760 set_Block_dom_depth(w->block, depth);
766 /* Do a walk over the tree and assign the tree pre orders. */
768 unsigned tree_pre_order = 0;
769 dom_tree_walk_irg(irg, assign_tree_dom_pre_order,
770 assign_tree_dom_pre_order_max, &tree_pre_order);
772 current_ir_graph = rem;
775 void assure_doms(ir_graph *irg)
777 if (get_irg_dom_state(irg) != dom_consistent)
781 void free_dom(ir_graph *irg)
783 /* Update graph state */
784 assert(get_irg_phase_state(irg) != phase_building);
785 irg->dom_state = dom_none;
787 /* With the implementation right now there is nothing to free,
788 but better call it anyways... */
791 /* Computes the post dominator trees. Sets a flag in irg to "dom_consistent".
792 If the control flow of the graph is changed this flag must be set to
793 "dom_inconsistent". */
794 void compute_postdoms(ir_graph *irg)
796 ir_graph *rem = current_ir_graph;
797 int n_blocks, used, i, j;
798 tmp_dom_info *tdi_list;
800 current_ir_graph = irg;
802 /* Update graph state */
803 assert(get_irg_phase_state(irg) != phase_building);
804 irg->pdom_state = dom_consistent;
806 /* Count the number of blocks in the graph. */
808 irg_block_walk_graph(irg, count_and_init_blocks_pdom, NULL, &n_blocks);
810 /* Memory for temporary information. */
811 tdi_list = XMALLOCNZ(tmp_dom_info, n_blocks);
813 /* We need the out data structure. */
814 assure_irg_outs(irg);
816 /* this with a standard walker as passing the parent to the sons isn't
819 inc_irg_block_visited(irg);
820 init_tmp_pdom_info(get_irg_end_block(irg), NULL, tdi_list, &used, n_blocks);
821 /* If not all blocks are reachable from End by cfg edges this assertion
823 assert(used == n_blocks && "Precondition for dom construction violated"); */
827 for (i = n_blocks-1; i > 0; i--) { /* Don't iterate the root, it's done. */
829 tmp_dom_info *w = &tdi_list[i];
833 irn_arity = get_Block_n_cfg_outs_ka(w->block);
834 for (j = 0; j < irn_arity; j++) {
835 ir_node *succ = get_Block_cfg_out_ka(w->block, j);
838 if (get_Block_postdom_pre_num (succ) == -1)
839 continue; /* endless-loop */
841 u = dom_eval (&tdi_list[get_Block_postdom_pre_num(succ)]);
842 if (u->semi < w->semi) w->semi = u->semi;
844 /* Add w to w->semi's bucket. w is in exactly one bucket, so
845 buckets can be implemented as linked lists. */
846 w->bucket = w->semi->bucket;
849 dom_link (w->parent, w);
852 while (w->parent->bucket) {
854 v = w->parent->bucket;
855 /* remove v from w->parent->bucket */
856 w->parent->bucket = v->bucket;
860 if (u->semi < v->semi)
867 tdi_list[0].dom = NULL;
868 set_Block_ipostdom(tdi_list[0].block, NULL);
869 set_Block_postdom_depth(tdi_list[0].block, 1);
870 for (i = 1; i < n_blocks; i++) {
871 tmp_dom_info *w = &tdi_list[i];
873 if (w->dom != w->semi) w->dom = w->dom->dom;
874 set_Block_ipostdom(w->block, w->dom->block);
875 set_Block_postdom_depth(w->block, get_Block_postdom_depth(w->dom->block) + 1);
881 /* Do a walk over the tree and assign the tree pre orders. */
883 unsigned tree_pre_order = 0;
884 postdom_tree_walk_irg(irg, assign_tree_postdom_pre_order,
885 assign_tree_postdom_pre_order_max, &tree_pre_order);
887 current_ir_graph = rem;
890 void assure_postdoms(ir_graph *irg)
892 if (get_irg_postdom_state(irg) != dom_consistent)
893 compute_postdoms(irg);
896 void free_postdom(ir_graph *irg)
898 /* Update graph state */
899 assert(get_irg_phase_state(irg) != phase_building);
900 irg->pdom_state = dom_none;
902 /* With the implementation right now there is nothing to free,
903 but better call it anyways... */