1bc8e47a51aae599c77f57ccfeaa07f7422822d6
[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 #include "irouts.h"
18
19 #include "irgwalk.h"
20 #include "irdom_t.h"
21 #include "irgraph_t.h"   /* To access state field. */
22 #include "irnode_t.h"
23
24 /**********************************************************************/
25 /** Accessing the dominator data structures                          **/
26 /**********************************************************************/
27
28 ir_node *get_Block_idom(ir_node *bl) {
29   assert(get_irn_op(bl) == op_Block);
30   return bl->attr.block.dom.idom;
31 }
32
33 void set_Block_idom(ir_node *bl, ir_node *n) {
34   assert(get_irn_op(bl) == op_Block);
35   bl->attr.block.dom.idom = n;
36 }
37
38 int get_Block_pre_num(ir_node *bl) {
39   assert(get_irn_op(bl) == op_Block);
40   return bl->attr.block.dom.pre_num;
41 }
42
43 void set_Block_pre_num(ir_node *bl, int num) {
44   assert(get_irn_op(bl) == op_Block);
45   bl->attr.block.dom.pre_num = num;
46 }
47
48 int get_Block_dom_depth(ir_node *bl) {
49   assert(get_irn_op(bl) == op_Block);
50   return bl->attr.block.dom.dom_depth;
51 }
52
53 void set_Block_dom_depth(ir_node *bl, int depth) {
54   assert(get_irn_op(bl) == op_Block);
55   bl->attr.block.dom.dom_depth = depth;
56 }
57
58
59
60 /**********************************************************************/
61 /** Building and Removing the dominator datasturcture                **/
62 /**                                                                  **/
63 /**  **/
64 /**  **/
65 /**  **/
66 /**  **/
67 /**  **/
68 /**  **/
69 /** .**/
70 /**  **/
71 /**  **/
72 /**  **/
73 /**  **/
74 /**  **/
75 /**  **/
76 /**********************************************************************/
77
78 static void count_and_init_blocks(ir_node *bl, void *env) {
79   int *n_blocks = (int *) env;
80   (*n_blocks) ++;
81
82   set_Block_idom(bl, NULL);
83   set_Block_pre_num(bl, -1);
84   set_Block_dom_depth(bl, -1);
85 }
86
87 /* temporary type used while constructing the dominator tree. */
88 typedef struct tmp_dom_info {
89   ir_node *block;               /* backlink */
90
91   struct tmp_dom_info *semi;    /* semidominator */
92   struct tmp_dom_info *parent;
93   struct tmp_dom_info *label;   /* used for LINK and EVAL */
94   struct tmp_dom_info *ancestor;/* used for LINK and EVAL */
95   struct tmp_dom_info *dom;     /* After step 3, if the semidominator of w is
96                                    its immediate dominator, then w->dom is the
97                                    immediate dominator of w.  Otherwise w->dom
98                                    is a vertex v whose number is smaller than
99                                    w and whose immediate dominator is also w's
100                                    immediate dominator. After step 4, w->dom
101                                    is the immediate dominator of w.  */
102   struct tmp_dom_info *bucket;  /* set of vertices with same semidominator */
103 } tmp_dom_info;
104
105 /* Struct to pass info through walker. */
106 typedef struct {
107   tmp_dom_info *d;
108   int used;
109
110 } dom_env;
111
112
113 /* Walks Blocks along the out datastructure.  If recursion started with
114    Start block misses control dead blocks. */
115 static void init_tmp_dom_info(ir_node *bl, tmp_dom_info *parent,
116                               tmp_dom_info *tdi_list, int* used) {
117   tmp_dom_info *tdi;
118   int i;
119
120   assert(get_irn_op(bl) == op_Block);
121   if (get_irg_block_visited(current_ir_graph) == get_Block_block_visited(bl))
122     return;
123   mark_Block_block_visited(bl);
124   set_Block_pre_num(bl, *used);
125
126   tdi = &tdi_list[*used];
127   ++(*used);
128
129   tdi->semi = tdi;
130   tdi->label = tdi;
131   tdi->ancestor = NULL;
132   tdi->bucket = NULL;
133   tdi->parent = parent;
134   tdi->block = bl;
135
136   /* Iterate */
137   for(i = 0; i < get_Block_n_cfg_outs(bl); i++) {
138     ir_node *pred = get_Block_cfg_out(bl, i);
139     assert(get_irn_opcode(pred) == iro_Block);
140     init_tmp_dom_info(pred, tdi, tdi_list, used);
141   }
142 }
143
144 static void
145 dom_compress (tmp_dom_info *v)
146 {
147   assert (v->ancestor);
148   if (v->ancestor->ancestor) {
149     dom_compress (v->ancestor);
150     if (v->ancestor->label->semi < v->label->semi) {
151       v->label = v->ancestor->label;
152     }
153     v->ancestor = v->ancestor->ancestor;
154   }
155 }
156
157 /* if V is a root, return v, else return the vertex u, not being the
158    root, with minimum u->semi on the path from v to its root. */
159 INLINE static tmp_dom_info*
160 dom_eval (tmp_dom_info *v)
161 {
162   if (!v->ancestor) return v;
163   dom_compress (v);
164   return v->label;
165 }
166
167 /* make V W's ancestor */
168 INLINE static void
169 dom_link (tmp_dom_info *v, tmp_dom_info *w)
170 {
171   w->ancestor = v;
172 }
173
174 /* Computes the dominator trees.  Sets a flag in irg to "dom_consistent".
175    If the control flow of the graph is changed this flag must be set to
176    "dom_inconsistent".  */
177 void compute_doms(ir_graph *irg) {
178   ir_graph *rem = current_ir_graph;
179   int n_blocks, used, i, j;
180   tmp_dom_info *tdi_list;   /* Ein Golf? */
181
182   current_ir_graph = irg;
183
184   /* Update graph state */
185   assert(get_irg_phase_state(current_ir_graph) != phase_building);
186   current_ir_graph->dom_state = dom_consistent;
187
188   /* Count the number of blocks in the graph. */
189   n_blocks = 0;
190   irg_block_walk(get_irg_end(current_ir_graph), count_and_init_blocks, NULL, &n_blocks);
191
192   /* Memory for temporary information. */
193   tdi_list = (tmp_dom_info *) calloc(n_blocks, sizeof(tmp_dom_info));
194
195   /* We need the out datastructure. */
196   if (current_ir_graph->outs_state != outs_consistent)
197     compute_outs(current_ir_graph);
198
199   /* this with a standard walker as passing the parent to the sons isn't
200      simple. */
201   used = 0;
202   inc_irg_block_visited(current_ir_graph);
203   init_tmp_dom_info(get_irg_start_block(current_ir_graph), NULL, tdi_list, &used);
204   /* If not all blocks are reachable from Start by out edges this assertion
205      fails.
206      assert(used == n_blocks && "Precondition for dom construction violated"); */
207   n_blocks = used;
208
209
210   for (i = n_blocks-1; i > 0; i--) {  /* Don't iterate the root, it's done. */
211     int irn_arity;
212     tmp_dom_info *w = &tdi_list[i];
213     tmp_dom_info *v;
214
215     /* Step 2 */
216     irn_arity = get_irn_arity(w->block);
217     for (j = 0;  j < irn_arity;  j++) {
218       ir_node *pred = get_nodes_block(get_Block_cfgpred(w->block, j));
219       tmp_dom_info *u;
220
221       if ((is_Bad(get_Block_cfgpred(w->block, j))) ||
222           (get_Block_pre_num (pred) == -1))
223         continue;       /* control-dead */
224
225       u = dom_eval (&tdi_list[get_Block_pre_num(pred)]);
226       if (u->semi < w->semi) w->semi = u->semi;
227     }
228     /* Add w to w->semi's bucket.  w is in exactly one bucket, so
229        buckets can ben implemented as linked lists. */
230     w->bucket = w->semi->bucket;
231     w->semi->bucket = w;
232
233     dom_link (w->parent, w);
234
235     /* Step 3 */
236     while (w->parent->bucket) {
237       tmp_dom_info *u;
238       v = w->parent->bucket;
239       /* remove v from w->parent->bucket */
240       w->parent->bucket = v->bucket;
241       v->bucket = NULL;
242
243       u = dom_eval (v);
244       if (u->semi < v->semi)
245         v->dom = u;
246       else
247         v->dom = w->parent;
248     }
249   }
250   /* Step 4 */
251   tdi_list[0].dom = NULL;
252   set_Block_idom(tdi_list[0].block, NULL);
253   set_Block_dom_depth(tdi_list[0].block, 1);
254   for (i = 1;  i < n_blocks;  i++) {
255     tmp_dom_info *w = &tdi_list[i];
256
257     if (w->dom != w->semi) w->dom = w->dom->dom;
258     set_Block_idom(w->block, w->dom->block);
259     set_Block_dom_depth(w->block, get_Block_dom_depth(w->dom->block) + 1);
260   }
261
262   /* clean up */
263   /*  free(tdi_list); @@@ does not work !!?? */
264   current_ir_graph = rem;
265 }
266
267 void free_dom_and_peace(ir_graph *irg) {
268   /* Update graph state */
269   assert(get_irg_phase_state(current_ir_graph) != phase_building);
270   current_ir_graph->dom_state = dom_none;
271
272   /* With the implementation right now there is nothing to free,
273      but better call it anyways... */
274 }