3 * File name: ir/ana/irdom.c
4 * Purpose: Construct and access dominator tree.
5 * Author: Goetz Lindenmaier
9 * Copyright: (c) 2002-2003 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
26 #include "irgraph_t.h" /* To access state field. */
30 #define get_dom_info(bl) (&(bl)->attr.block.dom)
33 /*--------------------------------------------------------------------*/
34 /** Accessing the dominator data structures **/
35 /*--------------------------------------------------------------------*/
37 ir_node *get_Block_idom(const ir_node *bl) {
39 if (get_Block_dom_depth(bl) == -1) {
40 /* This block is not reachable from Start */
43 return get_dom_info(bl)->idom;
46 void set_Block_idom(ir_node *bl, ir_node *n) {
47 dom_info *bli = get_dom_info(bl);
49 assert(get_irn_op(bl) == op_Block);
51 /* Set the immediate dominator of bl to n */
55 * If we don't set the root of the dominator tree
56 * Append bl to the dominates queue of n.
59 dom_info *ni = get_dom_info(n);
61 bli->next = ni->first;
66 int get_Block_pre_num(const ir_node *bl) {
67 assert(get_irn_op(bl) == op_Block);
68 return bl->attr.block.dom.pre_num;
71 void set_Block_pre_num(ir_node *bl, int num) {
72 assert(get_irn_op(bl) == op_Block);
73 bl->attr.block.dom.pre_num = num;
76 int get_Block_dom_depth(const ir_node *bl) {
77 assert(get_irn_op(bl) == op_Block);
78 return bl->attr.block.dom.dom_depth;
81 void set_Block_dom_depth(ir_node *bl, int depth) {
82 assert(get_irn_op(bl) == op_Block);
83 bl->attr.block.dom.dom_depth = depth;
86 unsigned get_Block_dom_tree_pre_num(const ir_node *bl)
89 return get_dom_info(bl)->tree_pre_num;
92 unsigned get_Block_dom_max_subtree_pre_num(const ir_node *bl)
95 return get_dom_info(bl)->max_subtree_pre_num;
98 int block_dominates(const ir_node *a, const ir_node *b)
100 const dom_info *ai, *bi;
102 if (is_Block(a) && is_Block(b)) {
103 ai = get_dom_info(a);
104 bi = get_dom_info(b);
105 return bi->tree_pre_num - ai->tree_pre_num
106 <= ai->max_subtree_pre_num - ai->tree_pre_num;
111 ir_node *get_Block_dominated_first(const ir_node *bl)
113 assert(is_Block(bl));
114 return get_dom_info(bl)->first;
117 ir_node *get_Block_dominated_next(const ir_node *bl)
119 assert(is_Block(bl));
120 return get_dom_info(bl)->next;
123 void dom_tree_walk(ir_node *bl, irg_walk_func *pre,
124 irg_walk_func *post, void *env)
131 dominates_for_each(bl, p) {
132 dom_tree_walk(p, pre, post, env);
139 void dom_tree_walk_irg(ir_graph *irg, irg_walk_func *pre,
140 irg_walk_func *post, void *env)
142 /* The root of the dominator tree should be the start node. */
143 ir_node *root = get_irg_start_block(irg);
145 assert(irg->dom_state == dom_consistent
146 && "The dominators of the irg must be consistent");
147 assert(root && "The start block of the graph is NULL?");
148 assert(get_dom_info(root)->idom == NULL
149 && "The start node in the graph must be the root of the dominator tree");
150 dom_tree_walk(root, pre, post, env);
153 static void assign_tree_pre_order(ir_node *bl, void *data)
155 unsigned *num = data;
156 dom_info *bi = get_dom_info(bl);
158 bi->tree_pre_num = (*num)++;
161 static void assign_tree_pre_order_max(ir_node *bl, void *data)
163 dom_info *bi = get_dom_info(bl);
166 unsigned children = 0;
168 for(p = bi->first; p; p = get_dom_info(p)->next) {
169 unsigned max_p = get_dom_info(p)->max_subtree_pre_num;
170 max = max > max_p ? max : max_p;
174 bi->max_subtree_pre_num = children > 0 ? max : bi->tree_pre_num;
175 assert(bi->max_subtree_pre_num >= bi->tree_pre_num);
178 /*--------------------------------------------------------------------*/
179 /* Building and Removing the dominator datastructure */
180 /*--------------------------------------------------------------------*/
182 static void count_and_init_blocks(ir_node *bl, void *env) {
183 int *n_blocks = (int *) env;
186 memset(get_dom_info(bl), 0, sizeof(dom_info));
187 set_Block_idom(bl, NULL);
188 set_Block_pre_num(bl, -1);
189 set_Block_dom_depth(bl, -1);
192 /* temporary type used while constructing the dominator tree. */
193 typedef struct tmp_dom_info {
194 ir_node *block; /* backlink */
196 struct tmp_dom_info *semi; /* semidominator */
197 struct tmp_dom_info *parent;
198 struct tmp_dom_info *label; /* used for LINK and EVAL */
199 struct tmp_dom_info *ancestor;/* used for LINK and EVAL */
200 struct tmp_dom_info *dom; /* After step 3, if the semidominator of w is
201 its immediate dominator, then w->dom is the
202 immediate dominator of w. Otherwise w->dom
203 is a vertex v whose number is smaller than
204 w and whose immediate dominator is also w's
205 immediate dominator. After step 4, w->dom
206 is the immediate dominator of w. */
207 struct tmp_dom_info *bucket; /* set of vertices with same semidominator */
210 /* Struct to pass info through walker. */
218 /* Walks Blocks along the out datastructure. If recursion started with
219 Start block misses control dead blocks. */
220 static void init_tmp_dom_info(ir_node *bl, tmp_dom_info *parent,
221 tmp_dom_info *tdi_list, int* used) {
225 assert(get_irn_op(bl) == op_Block);
226 if (get_irg_block_visited(current_ir_graph) == get_Block_block_visited(bl))
228 mark_Block_block_visited(bl);
229 set_Block_pre_num(bl, *used);
231 tdi = &tdi_list[*used];
236 tdi->ancestor = NULL;
238 tdi->parent = parent;
242 for(i = 0; i < get_Block_n_cfg_outs(bl); i++) {
243 ir_node *pred = get_Block_cfg_out(bl, i);
244 assert(get_irn_opcode(pred) == iro_Block);
245 init_tmp_dom_info(pred, tdi, tdi_list, used);
250 dom_compress (tmp_dom_info *v)
252 assert (v->ancestor);
253 if (v->ancestor->ancestor) {
254 dom_compress (v->ancestor);
255 if (v->ancestor->label->semi < v->label->semi) {
256 v->label = v->ancestor->label;
258 v->ancestor = v->ancestor->ancestor;
262 /* if V is a root, return v, else return the vertex u, not being the
263 root, with minimum u->semi on the path from v to its root. */
264 INLINE static tmp_dom_info*
265 dom_eval (tmp_dom_info *v)
267 if (!v->ancestor) return v;
272 /* make V W's ancestor */
274 dom_link (tmp_dom_info *v, tmp_dom_info *w)
279 /* Computes the dominator trees. Sets a flag in irg to "dom_consistent".
280 If the control flow of the graph is changed this flag must be set to
281 "dom_inconsistent". */
282 void compute_doms(ir_graph *irg) {
283 ir_graph *rem = current_ir_graph;
284 int n_blocks, used, i, j;
285 tmp_dom_info *tdi_list; /* Ein Golf? */
287 current_ir_graph = irg;
289 /* Update graph state */
290 assert(get_irg_phase_state(current_ir_graph) != phase_building);
291 current_ir_graph->dom_state = dom_consistent;
293 /* Count the number of blocks in the graph. */
295 irg_block_walk(get_irg_end(current_ir_graph), count_and_init_blocks, NULL, &n_blocks);
297 /* Memory for temporary information. */
298 tdi_list = xcalloc(n_blocks, sizeof(tdi_list[0]));
300 /* We need the out datastructure. */
301 if (current_ir_graph->outs_state != outs_consistent)
302 compute_outs(current_ir_graph);
304 /* this with a standard walker as passing the parent to the sons isn't
307 inc_irg_block_visited(current_ir_graph);
308 init_tmp_dom_info(get_irg_start_block(current_ir_graph), NULL, tdi_list, &used);
309 /* If not all blocks are reachable from Start by out edges this assertion
311 assert(used == n_blocks && "Precondition for dom construction violated"); */
315 for (i = n_blocks-1; i > 0; i--) { /* Don't iterate the root, it's done. */
317 tmp_dom_info *w = &tdi_list[i];
321 irn_arity = get_irn_arity(w->block);
322 for (j = 0; j < irn_arity; j++) {
323 ir_node *cf_op = get_Block_cfgpred(w->block, j);
324 ir_node *pred = get_nodes_block(cf_op);
327 if ((is_Bad(cf_op)) || (is_Bad(pred)) ||
328 (get_Block_pre_num (pred) == -1))
329 continue; /* control-dead */
331 u = dom_eval (&tdi_list[get_Block_pre_num(pred)]);
332 if (u->semi < w->semi) w->semi = u->semi;
334 /* Add w to w->semi's bucket. w is in exactly one bucket, so
335 buckets can ben implemented as linked lists. */
336 w->bucket = w->semi->bucket;
339 dom_link (w->parent, w);
342 while (w->parent->bucket) {
344 v = w->parent->bucket;
345 /* remove v from w->parent->bucket */
346 w->parent->bucket = v->bucket;
350 if (u->semi < v->semi)
357 tdi_list[0].dom = NULL;
358 set_Block_idom(tdi_list[0].block, NULL);
359 set_Block_dom_depth(tdi_list[0].block, 1);
360 for (i = 1; i < n_blocks; i++) {
361 tmp_dom_info *w = &tdi_list[i];
363 if (w->dom != w->semi) w->dom = w->dom->dom;
364 set_Block_idom(w->block, w->dom->block);
365 set_Block_dom_depth(w->block, get_Block_dom_depth(w->dom->block) + 1);
369 /* free(tdi_list); @@@ does not work !!?? */
370 current_ir_graph = rem;
372 /* Do a walk over the tree and assign the tree pre orders. */
374 unsigned tree_pre_order = 0;
375 dom_tree_walk_irg(irg, assign_tree_pre_order,
376 assign_tree_pre_order_max, &tree_pre_order);
380 void free_dom_and_peace(ir_graph *irg) {
381 /* Update graph state */
382 assert(get_irg_phase_state(current_ir_graph) != phase_building);
383 current_ir_graph->dom_state = dom_none;
385 /* With the implementation right now there is nothing to free,
386 but better call it anyways... */