1 /* Copyright (C) 2002 by Universitaet Karlsruhe
2 ** All rights reserved.
4 ** Authors: Goetz Lindenmaier
6 ** irdom.c --- Dominator tree.
15 #include "irgraph_t.h" /* To access state field. */
18 /**********************************************************************/
19 /** Accessing the dominator datastructures **/
20 /**********************************************************************/
22 ir_node *get_Block_idom(ir_node *bl) {
23 assert(get_irn_op(bl) == op_Block);
24 return bl->attr.block.dom.idom;
27 void set_Block_idom(ir_node *bl, ir_node *n) {
28 assert(get_irn_op(bl) == op_Block);
29 bl->attr.block.dom.idom = n;
32 int get_Block_pre_num(ir_node *bl) {
33 assert(get_irn_op(bl) == op_Block);
34 return bl->attr.block.dom.pre_num;
37 void set_Block_pre_num(ir_node *bl, int num) {
38 assert(get_irn_op(bl) == op_Block);
39 bl->attr.block.dom.pre_num = num;
42 int get_Block_dom_depth(ir_node *bl) {
43 assert(get_irn_op(bl) == op_Block);
44 return bl->attr.block.dom.dom_depth;
47 void set_Block_dom_depth(ir_node *bl, int depth) {
48 assert(get_irn_op(bl) == op_Block);
49 bl->attr.block.dom.dom_depth = depth;
54 /**********************************************************************/
55 /** Building and Removing the dominator datasturcture **/
70 /**********************************************************************/
72 void count_and_init_blocks(ir_node *bl, void *env) {
73 int *n_blocks = (int *) env;
76 set_Block_idom(bl, NULL);
77 set_Block_pre_num(bl, -1);
78 set_Block_dom_depth(bl, -1);
81 /* temporary type used while constructing the dominator tree. */
82 typedef struct tmp_dom_info {
83 ir_node *block; /* backlink */
85 struct tmp_dom_info *semi; /* semidominator */
86 struct tmp_dom_info *parent;
87 struct tmp_dom_info *label; /* used for LINK and EVAL */
88 struct tmp_dom_info *ancestor;/* used for LINK and EVAL */
89 struct tmp_dom_info *dom; /* After step 3, if the semidominator of w is
90 its immediate dominator, then w->dom is the
91 immediate dominator of w. Otherwise w->dom
92 is a vertex v whose number is smaller than
93 w and whose immediate dominator is also w's
94 immediate dominator. After step 4, w->dom
95 is the immediate dominator of w. */
96 struct tmp_dom_info *bucket; /* set of vertices with same semidominator */
99 /* Struct to pass info through walker. */
106 void init_tmp_dom_info(ir_node *bl, tmp_dom_info *parent, tmp_dom_info *tdi_list, int* used) {
110 assert(get_irn_op(bl) == op_Block);
111 if (get_irg_block_visited(current_ir_graph) == get_Block_block_visited(bl)) return;
112 mark_Block_block_visited(bl);
113 set_Block_pre_num(bl, *used);
115 //printf(" used: %d ", *used); DDMN(bl);
117 tdi = &tdi_list[*used];
122 tdi->ancestor = NULL;
124 tdi->parent = parent;
128 for(i = 0; i < get_Block_n_cfg_outs(bl); i++) {
129 ir_node *pred = get_Block_cfg_out(bl, i);
130 assert(get_irn_opcode(pred) == iro_Block);
131 init_tmp_dom_info(pred, tdi, tdi_list, used);
137 dom_compress (tmp_dom_info *v)
139 assert (v->ancestor);
140 if (v->ancestor->ancestor) {
141 dom_compress (v->ancestor);
142 if (v->ancestor->label->semi < v->label->semi) {
143 v->label = v->ancestor->label;
145 v->ancestor = v->ancestor->ancestor;
149 /* if V is a root, return v, else return the vertex u, not being the
150 root, with minimum u->semi on the path from v to its root. */
151 inline static tmp_dom_info*
152 dom_eval (tmp_dom_info *v)
154 if (!v->ancestor) return v;
159 /* make V W's ancestor */
161 dom_link (tmp_dom_info *v, tmp_dom_info *w)
166 /* Computes the dominator trees. Sets a flag in irg to "dom_consistent".
167 If the control flow of the graph is changed this flag must be set to
168 "dom_inconsistent". */
169 void compute_doms(ir_graph *irg) {
170 ir_graph *rem = current_ir_graph;
171 int n_blocks, used, i, j;
172 tmp_dom_info *tdi_list; /* Ein Golf? */
175 current_ir_graph = irg;
177 /* Update graph state */
178 assert(get_irg_phase_state(current_ir_graph) != phase_building);
179 current_ir_graph->dom_state = dom_consistent;
181 /* Count the number of blocks in the graph. */
183 irg_block_walk(get_irg_end(current_ir_graph), count_and_init_blocks, NULL, &n_blocks);
185 //printf("n_blocks is %d\n", n_blocks);
187 /* Memory for temporary information. */
188 tdi_list = (tmp_dom_info *) calloc(n_blocks, sizeof(tmp_dom_info));
190 /* We need the out datastructure. */
191 if (current_ir_graph->outs_state != outs_consistent)
192 compute_outs(current_ir_graph);
194 /** Initialize the temporary information, add link to parent. We don't do
195 this with a standard walker as passing the parent to the sons isn't
198 inc_irg_block_visited(current_ir_graph);
199 init_tmp_dom_info(get_irg_start_block(current_ir_graph), NULL, tdi_list, &used);
200 /* If not all blocks are reachable from Start by out edges this assertion
202 //assert(used == n_blocks && "Precondition for dom construction violated");
205 //printf("used is %d\n", used);
208 for (i = n_blocks-1; i > 0; i--) { /* Don't iterate the root, it's done. */
209 tmp_dom_info *w = &tdi_list[i];
212 //printf(" cfgpreds: %d ", get_Block_n_cfgpreds(w->block)); DDMN(w->block);
215 for (j = 0; j < get_irn_arity(w->block); j++) {
216 ir_node *pred = get_nodes_Block(get_Block_cfgpred(w->block, j));
219 if ((is_Bad(pred)) || (get_Block_pre_num (pred) == -1))
220 continue; /* control-dead */
222 u = dom_eval (&tdi_list[get_Block_pre_num(pred)]);
223 if (u->semi < w->semi) w->semi = u->semi;
225 /* Add w to w->semi's bucket. w is in exactly one bucket, so
226 buckets can ben implemented as linked lists. */
227 w->bucket = w->semi->bucket;
230 dom_link (w->parent, w);
233 while (w->parent->bucket) {
235 v = w->parent->bucket;
236 /* remove v from w->parent->bucket */
237 w->parent->bucket = v->bucket;
241 if (u->semi < v->semi)
248 tdi_list[0].dom = NULL;
249 set_Block_idom(tdi_list[0].block, NULL);
250 set_Block_dom_depth(tdi_list[0].block, 1);
251 for (i = 1; i < n_blocks; i++) {
252 tmp_dom_info *w = &tdi_list[i];
254 if (w->dom != w->semi) w->dom = w->dom->dom;
255 set_Block_idom(w->block, w->dom->block);
256 set_Block_dom_depth(w->block, get_Block_dom_depth(w->dom->block) + 1);
261 current_ir_graph = rem;
264 void free_dom_and_peace(ir_graph *irg) {
265 /* Update graph state */
266 assert(get_irg_phase_state(current_ir_graph) != phase_building);
267 current_ir_graph->dom_state = no_dom;
276 /* temporary type used while constructing the dominator tree. */
277 typedef struct tmp_dom_info tmp_dom_info;
278 struct tmp_dom_info {
281 tmp_dom_info *semi; /* semidominator */
282 tmp_dom_info *parent;
283 tmp_dom_info *label; /* used for LINK and EVAL */
284 tmp_dom_info *ancestor; /* used for LINK and EVAL */
285 tmp_dom_info *dom; /* After step 3, if the semidominator
286 of w is its immediate dominator, then w->dom is the immediate
287 dominator of w. Otherwise w->dom is a vertex v whose number is
288 smaller than w and whose immediate dominator is also w's immediate
289 dominator. After step 4, w->dom is the immediate dominator of w. */
290 tmp_dom_info *bucket; /* set of vertices with same semidominator */
294 dom_count_regions (ir_node *n)
298 n->visit = ir_visited;
300 for (i = IR_ARITY (n); i > 0; --i) {
301 ir_node *pr = prev_region (n, i);
302 if (pr && pr->visit != ir_visited) {
303 count += dom_count_regions (pr);
309 struct dt_desc { tmp_dom_info *dt; int used;};
312 dom_setup (ir_node *n, tmp_dom_info *parent, struct dt_desc *dt_desc)
314 tmp_dom_info *dt = &dt_desc->dt[dt_desc->used];
317 if (n->visit == ir_visited) return;
318 n->visit = ir_visited;
320 assert (IR_CFG_NODE (n));
322 n->data.r.pre_num = dt_desc->used;
331 for (i = 0; i < n->data.r.cfg_outs; ++i) {
332 dom_setup (n->data.r.cfg_out[i], dt, dt_desc);
337 dom_compress (tmp_dom_info *v)
339 assert (v->ancestor);
340 if (v->ancestor->ancestor) {
341 dom_compress (v->ancestor);
342 if (v->ancestor->label->semi < v->label->semi) {
343 v->label = v->ancestor->label;
345 v->ancestor = v->ancestor->ancestor;
349 /* if V is a root, return v, else return the vertex u, not being the
350 root, with minimum u->semi on the path from v to its root. */
352 dom_eval (tmp_dom_info *v)
354 if (!v->ancestor) return v;
359 /* make V W's ancestor */
361 dom_link (tmp_dom_info *v, tmp_dom_info *w)
367 irg_gen_idom (ir_graph *irg)
371 struct dt_desc dt_desc;
373 if (!(irg->state & irgs_has_CFG)) irg_gen_out (irg);
377 /* walk all the artificially kept alive parts of the CFG instead of
378 the CFG beginning from the Start just for fun and safety */
379 keep_alives_in_arr (irg);
380 for (i = ARR_LEN (irg->keep.alive) - 1; i >= 0; --i)
381 if ( IR_CFG_NODE (irg->keep.alive[i])
382 && irg->keep.alive[i]->visit != ir_visited)
383 regions += dom_count_regions (irg->keep.alive[i]);
385 dt = alloca ((regions+1) * sizeof (tmp_dom_info));
386 memset (dt, 0, (regions+1) * sizeof (tmp_dom_info));
392 dom_setup (irg->start, NULL, &dt_desc);
394 /* This assert will fail, if not all Regions are reachable by
395 walking the CFG starting from Start, that is when there is
396 [control] dead code, violating the single entry precondition of
398 assert (dt_desc.used == regions + 1);
400 for (i = regions; i > 1; --i) {
401 tmp_dom_info *w = &dt[i];
406 r_ins = IR_ARITY (w->region);
407 for (j = 1; j <= r_ins; ++j) {
408 ir_node *prev = prev_region (w->region, j);
411 if (!prev) continue; /* control-dead */
413 u = dom_eval (&dt[prev->data.r.pre_num]);
414 if (u->semi < w->semi) w->semi = u->semi;
416 /* Add w to w->semi's bucket. w is in exactly one bucket, so
417 buckets can ben implemented as linked lists. */
418 w->bucket = w->semi->bucket;
421 dom_link (w->parent, w);
424 while ((v = w->parent->bucket)) {
426 /* remove v from w->parent->bucket */
427 w->parent->bucket = v->bucket;
431 v->dom = u->semi < v->semi ? u : w->parent;
436 dt[1].region->data.r.idom = NULL;
437 dt[1].region->data.r.dom_depth = 1;
438 for (i = 2; i <= regions; ++i) {
439 tmp_dom_info *w = &dt[i];
441 if (w->dom != w->semi) w->dom = w->dom->dom;
442 w->region->data.r.idom = w->dom->region;
443 w->region->data.r.dom_depth = w->dom->region->data.r.dom_depth + 1;
445 current_ir_graph = sirg;