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