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