Added Exception marking support --flo
[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
107 /* Walks Blocks along the out datastructure.  If recursion started with
108    Start block misses control dead blocks. */
109 void init_tmp_dom_info(ir_node *bl, tmp_dom_info *parent,
110                        tmp_dom_info *tdi_list, int* used) {
111   tmp_dom_info *tdi;
112   int i;
113
114   assert(get_irn_op(bl) == op_Block);
115   if (get_irg_block_visited(current_ir_graph) == get_Block_block_visited(bl))
116     return;
117   mark_Block_block_visited(bl);
118   set_Block_pre_num(bl, *used);
119
120   //printf(" used: %d ", *used); DDMN(bl);
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
141 static void
142 dom_compress (tmp_dom_info *v)
143 {
144   assert (v->ancestor);
145   if (v->ancestor->ancestor) {
146     dom_compress (v->ancestor);
147     if (v->ancestor->label->semi < v->label->semi) {
148       v->label = v->ancestor->label;
149     }
150     v->ancestor = v->ancestor->ancestor;
151   }
152 }
153
154 /* if V is a root, return v, else return the vertex u, not being the
155    root, with minimum u->semi on the path from v to its root. */
156 inline static tmp_dom_info*
157 dom_eval (tmp_dom_info *v)
158 {
159   if (!v->ancestor) return v;
160   dom_compress (v);
161   return v->label;
162 }
163
164 /* make V W's ancestor */
165 inline static void
166 dom_link (tmp_dom_info *v, tmp_dom_info *w)
167 {
168   w->ancestor = v;
169 }
170
171 /* Computes the dominator trees.  Sets a flag in irg to "dom_consistent".
172    If the control flow of the graph is changed this flag must be set to
173    "dom_inconsistent".  */
174 void compute_doms(ir_graph *irg) {
175   ir_graph *rem = current_ir_graph;
176   int n_blocks, used, i, j;
177   tmp_dom_info *tdi_list;   /* Ein Golf? */
178   dom_env de;
179
180   current_ir_graph = irg;
181
182   /* Update graph state */
183   assert(get_irg_phase_state(current_ir_graph) != phase_building);
184   current_ir_graph->dom_state = dom_consistent;
185
186   /* Count the number of blocks in the graph. */
187   n_blocks = 0;
188   irg_block_walk(get_irg_end(current_ir_graph), count_and_init_blocks, NULL, &n_blocks);
189
190   //printf("n_blocks is %d\n", 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   /** Initialize the temporary information, add link to parent.  We don't do
200       this with a standard walker as passing the parent to the sons isn't
201       simple. **/
202   used = 0;
203   inc_irg_block_visited(current_ir_graph);
204   init_tmp_dom_info(get_irg_start_block(current_ir_graph), NULL, tdi_list, &used);
205   /* If not all blocks are reachable from Start by out edges this assertion
206      fails. */
207   //assert(used == n_blocks && "Precondition for dom construction violated");
208   n_blocks = used;
209
210   //printf("used is %d\n", used);
211
212
213   for (i = n_blocks-1; i > 0; i--) {  /* Don't iterate the root, it's done. */
214     tmp_dom_info *w = &tdi_list[i];
215     tmp_dom_info *v;
216
217     //printf(" cfgpreds: %d ", get_Block_n_cfgpreds(w->block)); DDMN(w->block);
218
219     /* Step 2 */
220     for (j = 0;  j < get_irn_arity(w->block);  j++) {
221       ir_node *pred = get_nodes_Block(get_Block_cfgpred(w->block, j));
222       tmp_dom_info *u;
223
224       if ((is_Bad(get_Block_cfgpred(w->block, j))) || (get_Block_pre_num (pred) == -1))
225         continue;       /* control-dead */
226
227       u = dom_eval (&tdi_list[get_Block_pre_num(pred)]);
228       if (u->semi < w->semi) w->semi = u->semi;
229     }
230     /* Add w to w->semi's bucket.  w is in exactly one bucket, so
231        buckets can ben implemented as linked lists. */
232     w->bucket = w->semi->bucket;
233     w->semi->bucket = w;
234
235     dom_link (w->parent, w);
236
237     /* Step 3 */
238     while (w->parent->bucket) {
239       tmp_dom_info *u;
240       v = w->parent->bucket;
241       /* remove v from w->parent->bucket */
242       w->parent->bucket = v->bucket;
243       v->bucket = NULL;
244
245       u = dom_eval (v);
246       if (u->semi < v->semi)
247         v->dom = u;
248       else
249         v->dom = w->parent;
250     }
251   }
252   /* Step 4 */
253   tdi_list[0].dom = NULL;
254   set_Block_idom(tdi_list[0].block, NULL);
255   set_Block_dom_depth(tdi_list[0].block, 1);
256   for (i = 1;  i < n_blocks;  i++) {
257     tmp_dom_info *w = &tdi_list[i];
258
259     if (w->dom != w->semi) w->dom = w->dom->dom;
260     set_Block_idom(w->block, w->dom->block);
261     set_Block_dom_depth(w->block, get_Block_dom_depth(w->dom->block) + 1);
262   }
263
264   /* clean up */
265   // free(tdi_list);
266   current_ir_graph = rem;
267 }
268
269 void free_dom_and_peace(ir_graph *irg) {
270   /* Update graph state */
271   assert(get_irg_phase_state(current_ir_graph) != phase_building);
272   current_ir_graph->dom_state = no_dom;
273
274   /* With the implementation right now there is nothing to free,
275      but better call it anyways... */
276 }