b130f035893190756dc7b5bd6da405c3ccd6a737
[libfirm] / ir / ana / irdom.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ana/irdom.c
4  * Purpose:     Construct and access dominator tree.
5  * Author:      Goetz Lindenmaier
6  * Modified by:
7  * Created:     2.2002
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2002-2003 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 #include "config.h"
15 #endif
16
17 #ifdef HAVE_STRING_H
18 #include <string.h>
19 #endif
20
21 #include "irouts.h"
22
23 #include "xmalloc.h"
24 #include "irgwalk.h"
25 #include "irdom_t.h"
26 #include "irgraph_t.h"   /* To access state field. */
27 #include "irnode_t.h"
28 #include "ircons_t.h"
29
30 #define get_dom_info(bl) (&(bl)->attr.block.dom)
31
32
33 /*--------------------------------------------------------------------*/
34 /** Accessing the dominator data structures                          **/
35 /*--------------------------------------------------------------------*/
36
37 ir_node *get_Block_idom(const ir_node *bl) {
38   assert(is_Block(bl));
39   if (get_Block_dom_depth(bl) == -1) {
40     /* This block is not reachable from Start */
41     return new_Bad();
42   }
43   return get_dom_info(bl)->idom;
44 }
45
46 void set_Block_idom(ir_node *bl, ir_node *n) {
47         dom_info *bli = get_dom_info(bl);
48
49   assert(get_irn_op(bl) == op_Block);
50
51         /* Set the immediate dominator of bl to n */
52         bli->idom = n;
53
54         /*
55          * If we don't set the root of the dominator tree
56          * Append bl to the dominates queue of n.
57          */
58         if(n != NULL) {
59                 dom_info *ni = get_dom_info(n);
60
61                 bli->next = ni->first;
62                 ni->first = bl;
63         }
64 }
65
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;
69 }
70
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;
74 }
75
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;
79 }
80
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;
84 }
85
86 unsigned get_Block_dom_tree_pre_num(const ir_node *bl)
87 {
88         assert(is_Block(bl));
89         return get_dom_info(bl)->tree_pre_num;
90 }
91
92 unsigned get_Block_dom_max_subtree_pre_num(const ir_node *bl)
93 {
94         assert(is_Block(bl));
95         return get_dom_info(bl)->max_subtree_pre_num;
96 }
97
98 int block_dominates(const ir_node *a, const ir_node *b)
99 {
100         const dom_info *ai, *bi;
101
102         assert(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;
107 }
108
109 ir_node *get_Block_dominated_first(const ir_node *bl)
110 {
111         assert(is_Block(bl));
112         return get_dom_info(bl)->first;
113 }
114
115 ir_node *get_Block_dominated_next(const ir_node *bl)
116 {
117         assert(is_Block(bl));
118         return get_dom_info(bl)->next;
119 }
120
121 void dom_tree_walk(ir_node *n, irg_walk_func *pre,
122                 irg_walk_func *post, void *env)
123 {
124         ir_node *p;
125
126   assert(get_irn_irg(n)->dom_state == dom_consistent
127                         && "The dominators of the irg must be consistent");
128
129         for(p = get_dom_info(p)->first; p; p = get_dom_info(p)->next) {
130                 if(pre)
131                         pre(p, env);
132
133                 dom_tree_walk(p, pre, post, env);
134
135                 if(post)
136                         post(p, env);
137         }
138 }
139
140 void dom_tree_walk_irg(ir_graph *irg, irg_walk_func *pre,
141                 irg_walk_func *post, void *env)
142 {
143         /* The root of the dominator tree should be the start node. */
144         ir_node *root = get_irg_start_block(irg);
145
146   assert(irg->dom_state == dom_consistent
147                         && "The dominators of the irg must be consistent");
148         assert(root && "The start block of the graph is NULL?");
149         assert(get_dom_info(root)->idom == NULL
150                         && "The start node in the graph must be the root of the dominator tree");
151         dom_tree_walk(root, pre, post, env);
152 }
153
154 static void assign_tree_pre_order(ir_node *bl, void *data)
155 {
156         unsigned *num = data;
157         dom_info *bi = get_dom_info(bl);
158         bi->tree_pre_num = (*num)++;
159 }
160
161 static void assign_tree_pre_order_max(ir_node *bl, void *data)
162 {
163         dom_info *bi = get_dom_info(bl);
164         ir_node *p;
165         unsigned max = 0;
166
167         for(p = bi->first; p; p = get_dom_info(p)->next) {
168                 unsigned max_p = get_dom_info(p)->max_subtree_pre_num;
169                 max = max > max_p ? max : max_p;
170         }
171
172         bi->max_subtree_pre_num = max;
173 }
174
175 /*--------------------------------------------------------------------*/
176 /*  Building and Removing the dominator datastructure                 */
177 /*--------------------------------------------------------------------*/
178
179 static void count_and_init_blocks(ir_node *bl, void *env) {
180   int *n_blocks = (int *) env;
181   (*n_blocks) ++;
182
183   set_Block_idom(bl, NULL);
184   set_Block_pre_num(bl, -1);
185   set_Block_dom_depth(bl, -1);
186 }
187
188 /* temporary type used while constructing the dominator tree. */
189 typedef struct tmp_dom_info {
190   ir_node *block;               /* backlink */
191
192   struct tmp_dom_info *semi;    /* semidominator */
193   struct tmp_dom_info *parent;
194   struct tmp_dom_info *label;   /* used for LINK and EVAL */
195   struct tmp_dom_info *ancestor;/* used for LINK and EVAL */
196   struct tmp_dom_info *dom;     /* After step 3, if the semidominator of w is
197                                    its immediate dominator, then w->dom is the
198                                    immediate dominator of w.  Otherwise w->dom
199                                    is a vertex v whose number is smaller than
200                                    w and whose immediate dominator is also w's
201                                    immediate dominator. After step 4, w->dom
202                                    is the immediate dominator of w.  */
203   struct tmp_dom_info *bucket;  /* set of vertices with same semidominator */
204 } tmp_dom_info;
205
206 /* Struct to pass info through walker. */
207 typedef struct {
208   tmp_dom_info *d;
209   int used;
210
211 } dom_env;
212
213
214 /* Walks Blocks along the out datastructure.  If recursion started with
215    Start block misses control dead blocks. */
216 static void init_tmp_dom_info(ir_node *bl, tmp_dom_info *parent,
217                               tmp_dom_info *tdi_list, int* used) {
218   tmp_dom_info *tdi;
219   int i;
220
221   assert(get_irn_op(bl) == op_Block);
222   if (get_irg_block_visited(current_ir_graph) == get_Block_block_visited(bl))
223     return;
224   mark_Block_block_visited(bl);
225   set_Block_pre_num(bl, *used);
226
227   tdi = &tdi_list[*used];
228   ++(*used);
229
230   tdi->semi = tdi;
231   tdi->label = tdi;
232   tdi->ancestor = NULL;
233   tdi->bucket = NULL;
234   tdi->parent = parent;
235   tdi->block = bl;
236
237   /* Iterate */
238   for(i = 0; i < get_Block_n_cfg_outs(bl); i++) {
239     ir_node *pred = get_Block_cfg_out(bl, i);
240     assert(get_irn_opcode(pred) == iro_Block);
241     init_tmp_dom_info(pred, tdi, tdi_list, used);
242   }
243 }
244
245 static void
246 dom_compress (tmp_dom_info *v)
247 {
248   assert (v->ancestor);
249   if (v->ancestor->ancestor) {
250     dom_compress (v->ancestor);
251     if (v->ancestor->label->semi < v->label->semi) {
252       v->label = v->ancestor->label;
253     }
254     v->ancestor = v->ancestor->ancestor;
255   }
256 }
257
258 /* if V is a root, return v, else return the vertex u, not being the
259    root, with minimum u->semi on the path from v to its root. */
260 INLINE static tmp_dom_info*
261 dom_eval (tmp_dom_info *v)
262 {
263   if (!v->ancestor) return v;
264   dom_compress (v);
265   return v->label;
266 }
267
268 /* make V W's ancestor */
269 INLINE static void
270 dom_link (tmp_dom_info *v, tmp_dom_info *w)
271 {
272   w->ancestor = v;
273 }
274
275 /* Computes the dominator trees.  Sets a flag in irg to "dom_consistent".
276    If the control flow of the graph is changed this flag must be set to
277    "dom_inconsistent".  */
278 void compute_doms(ir_graph *irg) {
279   ir_graph *rem = current_ir_graph;
280   int n_blocks, used, i, j;
281   tmp_dom_info *tdi_list;   /* Ein Golf? */
282
283   current_ir_graph = irg;
284
285   /* Update graph state */
286   assert(get_irg_phase_state(current_ir_graph) != phase_building);
287   current_ir_graph->dom_state = dom_consistent;
288
289   /* Count the number of blocks in the graph. */
290   n_blocks = 0;
291   irg_block_walk(get_irg_end(current_ir_graph), count_and_init_blocks, NULL, &n_blocks);
292
293   /* Memory for temporary information. */
294   tdi_list = xcalloc(n_blocks, sizeof(tdi_list[0]));
295
296   /* We need the out datastructure. */
297   if (current_ir_graph->outs_state != outs_consistent)
298     compute_outs(current_ir_graph);
299
300   /* this with a standard walker as passing the parent to the sons isn't
301      simple. */
302   used = 0;
303   inc_irg_block_visited(current_ir_graph);
304   init_tmp_dom_info(get_irg_start_block(current_ir_graph), NULL, tdi_list, &used);
305   /* If not all blocks are reachable from Start by out edges this assertion
306      fails.
307      assert(used == n_blocks && "Precondition for dom construction violated"); */
308   n_blocks = used;
309
310
311   for (i = n_blocks-1; i > 0; i--) {  /* Don't iterate the root, it's done. */
312     int irn_arity;
313     tmp_dom_info *w = &tdi_list[i];
314     tmp_dom_info *v;
315
316     /* Step 2 */
317     irn_arity = get_irn_arity(w->block);
318     for (j = 0;  j < irn_arity;  j++) {
319       ir_node *cf_op = get_Block_cfgpred(w->block, j);
320       ir_node *pred  = get_nodes_block(cf_op);
321       tmp_dom_info *u;
322
323       if ((is_Bad(cf_op)) || (is_Bad(pred)) ||
324           (get_Block_pre_num (pred) == -1))
325         continue;       /* control-dead */
326
327       u = dom_eval (&tdi_list[get_Block_pre_num(pred)]);
328       if (u->semi < w->semi) w->semi = u->semi;
329     }
330     /* Add w to w->semi's bucket.  w is in exactly one bucket, so
331        buckets can ben implemented as linked lists. */
332     w->bucket = w->semi->bucket;
333     w->semi->bucket = w;
334
335     dom_link (w->parent, w);
336
337     /* Step 3 */
338     while (w->parent->bucket) {
339       tmp_dom_info *u;
340       v = w->parent->bucket;
341       /* remove v from w->parent->bucket */
342       w->parent->bucket = v->bucket;
343       v->bucket = NULL;
344
345       u = dom_eval (v);
346       if (u->semi < v->semi)
347         v->dom = u;
348       else
349         v->dom = w->parent;
350     }
351   }
352   /* Step 4 */
353   tdi_list[0].dom = NULL;
354         memset(get_dom_info(tdi_list[0].block), 0, sizeof(dom_info));
355   set_Block_idom(tdi_list[0].block, NULL);
356   set_Block_dom_depth(tdi_list[0].block, 1);
357   for (i = 1;  i < n_blocks;  i++) {
358     tmp_dom_info *w = &tdi_list[i];
359
360     if (w->dom != w->semi) w->dom = w->dom->dom;
361                 memset(get_dom_info(tdi_list[i].block), 0, sizeof(dom_info));
362     set_Block_idom(w->block, w->dom->block);
363     set_Block_dom_depth(w->block, get_Block_dom_depth(w->dom->block) + 1);
364   }
365
366   /* clean up */
367   /*  free(tdi_list); @@@ does not work !!?? */
368   current_ir_graph = rem;
369
370         /* Do a walk over the tree and assign the tree pre orders. */
371         {
372                 unsigned tree_pre_order = 0;
373                 dom_tree_walk_irg(irg, assign_tree_pre_order,
374                                 assign_tree_pre_order_max, &tree_pre_order);
375         }
376 }
377
378 void free_dom_and_peace(ir_graph *irg) {
379   /* Update graph state */
380   assert(get_irg_phase_state(current_ir_graph) != phase_building);
381   current_ir_graph->dom_state = dom_none;
382
383   /* With the implementation right now there is nothing to free,
384      but better call it anyways... */
385 }