54ffcf59d7de6af89547a893a36d1b19bfa09e85
[libfirm] / ir / ana / irdom.c
1 /* Copyright (C) 2002 by Universitaet Karlsruhe
2 ** All rights reserved.
3 **
4 ** Authors:  Goetz Lindenmaier
5 **
6 ** irdom.c --- Dominator tree.
7 **
8 */
9
10 /* $Id$ */
11
12 #include "irouts.h"
13
14 #include "irdom_t.h"
15 #include "irgraph_t.h"   /* To access state field. */
16 #include "irnode_t.h"
17
18 /**********************************************************************/
19 /** Accessing the dominator datastructures                           **/
20 /**********************************************************************/
21
22 ir_node *get_Block_idom(ir_node *bl) {
23   assert(get_irn_op(bl) == op_Block);
24   return bl->attr.block.dom.idom;
25 }
26
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;
30 }
31
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;
35 }
36
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;
40 }
41
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;
45 }
46
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;
50 }
51
52
53
54 /**********************************************************************/
55 /** Building and Removing the dominator datasturcture                **/
56 /**                                                                  **/
57 /**  **/
58 /**  **/
59 /**  **/
60 /**  **/
61 /**  **/
62 /**  **/
63 /** .**/
64 /**  **/
65 /**  **/
66 /**  **/
67 /**  **/
68 /**  **/
69 /**  **/
70 /**********************************************************************/
71
72 void count_and_init_blocks(ir_node *bl, void *env) {
73   int *n_blocks = (int *) env;
74   (*n_blocks) ++;
75
76   set_Block_idom(bl, NULL);
77   set_Block_pre_num(bl, -1);
78   set_Block_dom_depth(bl, -1);
79 }
80
81 /* temporary type used while constructing the dominator tree. */
82 typedef struct tmp_dom_info {
83   ir_node *block;               /* backlink */
84
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 */
97 } tmp_dom_info;
98
99 /* Struct to pass info through walker. */
100 typedef struct {
101   tmp_dom_info *d;
102   int used;
103
104 } dom_env;
105
106 void init_tmp_dom_info(ir_node *bl, tmp_dom_info *parent, tmp_dom_info *tdi_list, int* used) {
107   tmp_dom_info *tdi;
108   int i;
109
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);
114
115   //printf(" used: %d ", *used); DDMN(bl);
116
117   tdi = &tdi_list[*used];
118   ++(*used);
119
120   tdi->semi = tdi;
121   tdi->label = tdi;
122   tdi->ancestor = NULL;
123   tdi->bucket = NULL;
124   tdi->parent = parent;
125   tdi->block = bl;
126
127   /* Iterate */
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);
132   }
133 }
134
135
136 static void
137 dom_compress (tmp_dom_info *v)
138 {
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;
144     }
145     v->ancestor = v->ancestor->ancestor;
146   }
147 }
148
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)
153 {
154   if (!v->ancestor) return v;
155   dom_compress (v);
156   return v->label;
157 }
158
159 /* make V W's ancestor */
160 inline static void
161 dom_link (tmp_dom_info *v, tmp_dom_info *w)
162 {
163   w->ancestor = v;
164 }
165
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? */
173   dom_env de;
174
175   current_ir_graph = irg;
176
177   /* Update graph state */
178   assert(get_irg_phase_state(current_ir_graph) != phase_building);
179   current_ir_graph->dom_state = dom_consistent;
180
181   /* Count the number of blocks in the graph. */
182   n_blocks = 0;
183   irg_block_walk(get_irg_end(current_ir_graph), count_and_init_blocks, NULL, &n_blocks);
184
185   //printf("n_blocks is %d\n", n_blocks);
186
187   /* Memory for temporary information. */
188   tdi_list = (tmp_dom_info *) calloc(n_blocks, sizeof(tmp_dom_info));
189
190   /* We need the out datastructure. */
191   if (current_ir_graph->outs_state != outs_consistent)
192     compute_outs(current_ir_graph);
193
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
196      simple. **/
197   used = 0;
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
201      fails. */
202   //assert(used == n_blocks && "Precondition for dom construction violated");
203   n_blocks = used;
204
205   //printf("used is %d\n", used);
206
207
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];
210     tmp_dom_info *v;
211
212     //printf(" cfgpreds: %d ", get_Block_n_cfgpreds(w->block)); DDMN(w->block);
213
214     /* Step 2 */
215     for (j = 0;  j < get_irn_arity(w->block);  j++) {
216       ir_node *pred = get_nodes_Block(get_Block_cfgpred(w->block, j));
217       tmp_dom_info *u;
218
219       if ((is_Bad(pred)) || (get_Block_pre_num (pred) == -1))
220         continue;       /* control-dead */
221
222       u = dom_eval (&tdi_list[get_Block_pre_num(pred)]);
223       if (u->semi < w->semi) w->semi = u->semi;
224     }
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;
228     w->semi->bucket = w;
229
230     dom_link (w->parent, w);
231
232     /* Step 3 */
233     while (w->parent->bucket) {
234       tmp_dom_info *u;
235       v = w->parent->bucket;
236       /* remove v from w->parent->bucket */
237       w->parent->bucket = v->bucket;
238       v->bucket = NULL;
239
240       u = dom_eval (v);
241       if (u->semi < v->semi)
242         v->dom = u;
243       else
244         v->dom = w->parent;
245     }
246   }
247   /* Step 4 */
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];
253
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);
257   }
258
259   /* clean up */
260   free(tdi_list);
261   current_ir_graph = rem;
262 }
263
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;
268
269   /* @@@ free */
270 }
271
272
273 #if 0
274 /* Dominator Tree */
275
276 /* temporary type used while constructing the dominator tree. */
277 typedef struct tmp_dom_info tmp_dom_info;
278 struct tmp_dom_info {
279   ir_node *region;
280
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 */
291 };
292
293 static int
294 dom_count_regions (ir_node *n)
295 {
296   int i, count = 1;
297
298   n->visit = ir_visited;
299
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);
304     }
305   }
306   return count;
307 }
308
309 struct dt_desc { tmp_dom_info *dt;  int used;};
310
311 static void
312 dom_setup (ir_node *n, tmp_dom_info *parent, struct dt_desc *dt_desc)
313 {
314   tmp_dom_info *dt = &dt_desc->dt[dt_desc->used];
315   int i;
316
317   if (n->visit == ir_visited) return;
318   n->visit = ir_visited;
319
320   assert (IR_CFG_NODE (n));
321
322   n->data.r.pre_num = dt_desc->used;
323   dt->semi = dt;
324   dt->label = dt;
325   dt->ancestor = NULL;
326   dt->bucket = NULL;
327   dt->parent = parent;
328   dt->region = n;
329   ++(dt_desc->used);
330
331   for (i = 0;  i < n->data.r.cfg_outs;  ++i) {
332     dom_setup (n->data.r.cfg_out[i], dt, dt_desc);
333   }
334 }
335
336 static void
337 dom_compress (tmp_dom_info *v)
338 {
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;
344     }
345     v->ancestor = v->ancestor->ancestor;
346   }
347 }
348
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. */
351 static tmp_dom_info*
352 dom_eval (tmp_dom_info *v)
353 {
354   if (!v->ancestor) return v;
355   dom_compress (v);
356   return v->label;
357 }
358
359 /* make V W's ancestor */
360 static void
361 dom_link (tmp_dom_info *v, tmp_dom_info *w)
362 {
363   w->ancestor = v;
364 }
365
366 void
367 irg_gen_idom (ir_graph *irg)
368 {
369   int regions, i;
370   tmp_dom_info *dt;
371   struct dt_desc dt_desc;
372
373   if (!(irg->state & irgs_has_CFG)) irg_gen_out (irg);
374
375   ++ir_visited;
376   regions = 0;
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]);
384
385   dt = alloca ((regions+1) * sizeof (tmp_dom_info));
386   memset (dt, 0, (regions+1) * sizeof (tmp_dom_info));
387
388   /* Step 1 */
389   dt_desc.dt = dt;
390   dt_desc.used = 1;
391   ++ir_visited;
392   dom_setup (irg->start, NULL, &dt_desc);
393
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
397      this algorithm.  */
398   assert (dt_desc.used == regions + 1);
399
400   for (i = regions;  i > 1;  --i) {
401     tmp_dom_info *w = &dt[i];
402     tmp_dom_info *v;
403     int j, r_ins;
404
405     /* Step 2 */
406     r_ins = IR_ARITY (w->region);
407     for (j = 1;  j <= r_ins;  ++j) {
408       ir_node *prev = prev_region (w->region, j);
409       tmp_dom_info *u;
410
411       if (!prev) continue;      /* control-dead */
412
413       u = dom_eval (&dt[prev->data.r.pre_num]);
414       if (u->semi < w->semi) w->semi = u->semi;
415     }
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;
419     w->semi->bucket = w;
420
421     dom_link (w->parent, w);
422
423     /* Step 3 */
424     while ((v = w->parent->bucket)) {
425       tmp_dom_info *u;
426       /* remove v from w->parent->bucket */
427       w->parent->bucket = v->bucket;
428       v->bucket = NULL;
429
430       u = dom_eval (v);
431       v->dom = u->semi < v->semi ? u : w->parent;
432     }
433   }
434   /* Step 4 */
435   dt[1].dom = NULL;
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];
440
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;
444   }
445   current_ir_graph = sirg;
446 }
447
448 #endif