1 /* Copyright (c) 2002 by Universität Karlsruhe (TH). All Rights Reserved */
3 // Time-stamp: <Monday, 13.05.2002, 13:27:22 goetz@i44pc2.info.uni-karlsruhe.de>
10 provide some auxilliary structures for firm graphs.
14 liekweg - Feb 26, 2002: Created.
25 # endif /* def DEBUG_libfirm */
29 static dtree_t *trees = 0;
30 static dtree_t *last = 0;
32 // --------------------------------------------------------------------
34 // --------------------------------------------------------------------
36 Helper function for get_n_blocks
38 static void count_block (ir_node *block, void *env)
43 assert (block && "no block");
44 assert (env && "no env");
45 # endif /* def DEBUG_libfirm */
47 fprintf (stdout, "%s: Block(%p) has # (%i)\n",
48 __FILE__ ":" __PRETTY_FUNCTION__, block, *n);
54 Count the number of blocks in the given graph
56 static int get_n_blocks (ir_graph *graph)
60 ir_node *end_block = get_irg_end (graph);
63 assert (graph && "no graph");
64 assert (end_block && "no end block");
65 # endif /* def DEBUG_libfirm */
67 irg_block_walk (end_block, count_block, NULL, &n);
68 // irg_block_walk (end_block, NULL, NULL, NULL);
72 fprintf (stdout, "%s: Graph(%p) has (%i) blocks\n",
73 __FILE__ ":" __PRETTY_FUNCTION__, graph, n);
79 Build an empty dominator relation entry
81 static dtree_t *new_dtree (dt_t *tree, ir_graph *graph)
83 dtree_t *res = (dtree_t*) malloc (sizeof (dtree_t));
86 assert (res && "no memory for dtree");
87 # endif /* def DEBUG_libfirm */
97 Build an empty dominator relation
99 static dt_t *new_dt (ir_graph *graph)
101 int n_blocks = get_n_blocks (graph);
103 dt_t *res = (dt_t*) malloc (sizeof (dt_t));
105 # ifdef DEBUG_libfirm
106 assert (n_blocks && "no blocks for dt");
107 assert (res && "no memory for dt");
108 # endif /* def DEBUG_libfirm */
110 res->n_blocks = n_blocks;
112 res->blocks = (ir_node**) calloc (n_blocks, sizeof (ir_node*));
113 res->idoms = (ir_node**) calloc (n_blocks, sizeof (ir_node*));
114 res->masks = (bs_t*) calloc (n_blocks, sizeof (bs_t));
116 assert (res && "no dt");
121 static void free_dt (dt_t *dt)
123 free (dt->blocks); dt->blocks = 0;
124 free (dt->masks); dt->masks = 0;
129 // --------------------------------------------------------------------
131 // --------------------------------------------------------------------
134 Given a graph, find its dominator tree in the global list:
136 @return The tree, if it exists, and 0 else
138 static dt_t *get_dominator_tree (ir_graph *graph)
140 dtree_t *iter = trees;
142 # ifdef DEBUG_libfirm
143 assert (graph && "no graph");
144 // assert (iter && "no trees");
145 # endif /* def DEBUG_libfirm */
147 while ((0 != iter) && (graph != iter->graph))
152 # ifdef DEBUG_libfirm
153 assert ((graph == iter->tree->graph) && "wrong graph");
154 # endif /* def DEBUG_libfirm */
165 Given a dominator tree and a graph, enter the tree into the global list.
167 static void add_dominator_tree (dt_t *tree)
170 ir_graph *graph = tree->graph;
172 # ifdef DEBUG_libfirm
173 assert (tree && "no tree" );
174 assert (graph && "no graph");
175 # endif /* def DEBUG_libfirm */
177 dtree = new_dtree (tree, graph);
179 # ifdef VERBOSE_libfirm
180 fprintf (stdout, "%s: Adding dtree(%p) into trees(%p)\n",
181 __FILE__ ":" __PRETTY_FUNCTION__, dtree, trees);
182 # endif /* def VERBOSE_libfirm */
184 //enter in global list:
190 Get (or create) the index for a given block
192 static int get_index (dt_t *dt, ir_node *block)
196 # ifdef DEBUG_libfirm
197 assert (dt && "no dt");
198 assert (block && "no block");
199 # endif /* def DEBUG_libfirm */
201 for (i = 0; (i < dt->n_blocks) && (0 != dt->blocks [i]); i ++)
202 if (block == dt->blocks [i])
205 /* not found . . . enter new one: */
206 dt->blocks [i] = block;
207 if (block == get_irg_start_block (dt->graph))
209 dt->masks [i] = 0x00000001 << i;
211 # ifdef VERBOSE_libfirm
212 fprintf (stdout, "%s: Adding block(%p)[%i] with [%#010lx]\n",
213 __FILE__ ":" __PRETTY_FUNCTION__, block, i, dt->masks [i]);
214 # endif /* def VERBOSE_libfirm */
218 dt->masks [i] = ~0x00000000;
220 # ifdef VERBOSE_libfirm
221 fprintf (stdout, "%s: Adding block(%p)[%i] with [%#010lx]\n",
222 __FILE__ ":" __PRETTY_FUNCTION__, block, i, dt->masks [i]);
223 # endif /* def VERBOSE_libfirm */
230 Get the bit mask for a block
232 static bs_t _get_mask (dt_t*, int);
233 static bs_t get_mask (dt_t *dt, ir_node *block)
235 int index = get_index (dt, block);
237 # ifdef DEBUG_libfirm
238 assert (dt && "no dt");
239 assert (block && "no block");
240 # endif /* def DEBUG_libfirm */
242 return (_get_mask (dt, index));
246 Get the bit mask for a block index
248 static bs_t _get_mask (dt_t *dt, int index)
250 # ifdef DEBUG_libfirm
251 assert (dt && "no dt");
252 # endif /* def DEBUG_libfirm */
254 return (dt->masks [index]);
258 Set the bit mask for a block
260 static void _set_mask (dt_t*, int, bs_t);
261 static void set_mask (dt_t *dt, ir_node *block, bs_t mask)
263 int index = get_index (dt, block);
265 # ifdef DEBUG_libfirm
266 assert (dt && "no dt");
267 assert (block && "no block");
268 # endif /* def DEBUG_libfirm */
270 _set_mask (dt, index, mask);
274 Set the bit mask for a block index
276 static void _set_mask (dt_t *dt, int index, bs_t mask)
278 # ifdef DEBUG_libfirm
279 assert (dt && "no dt");
280 # endif /* def DEBUG_libfirm */
282 dt->masks [index] = mask;
286 Update the list of dominators of a given block
288 typedef struct dt_walk_env_t // grrr
290 dt_t *dt; // the dominator relation we're building
291 ir_node *start_block; // need to know the start block of this graph
292 bool changed; // wether the relation has changed recently
297 Helper function for build_dominator_tree
299 static void update_dominators (ir_node *block, void *env)
302 dt_walk_env_t *w = (dt_walk_env_t*) env;
305 int block_index = get_index (dt, block);
306 int n_ins = get_irn_arity (block);
308 bs_t old_mask = _get_mask (dt, block_index);
309 bs_t new_mask = ~0x00000000;
311 // Special handling of Start Block:
312 if (block == w->start_block)
316 for (i = 0; i < n_ins; i ++)
318 ir_node *in = get_nodes_Block (get_irn_n (block, i)); // hope that's the block
319 bs_t in_mask = get_mask (dt, in);
324 // and remember ourselves:
325 new_mask |= (0x00000001 << block_index);
327 if (new_mask != old_mask)
330 _set_mask (dt, block_index, new_mask);
332 # ifdef VERBOSE_libfirm
333 fprintf (stdout, "%s: Updating block(%p)[%i] from [%#010lx] to [%#010lx]\n",
334 __FILE__ ":" __PRETTY_FUNCTION__,
335 block, block_index, old_mask, new_mask);
336 # endif /* def VERBOSE_libfirm */
341 Say wether a dominates b
344 static bool _dominates (dt_t *dt, ir_node *a, ir_node *b)
346 int index_a = get_index (dt, a);
347 bs_t b_mask = get_mask (dt, b);
349 return (0 != (b_mask & (0x00000001 << index_a)));
353 Return the immediate dominator of block a
355 static ir_node *_get_idom (dt_t *dt, ir_node *block)
358 int block_index = get_index (dt, block);
360 if (0 != (idom = dt->idoms [block_index]))
363 // check all CFG preds:
364 // Question: Shouldn't it be good enough to just ask our first CFG predecessor?
367 int n = get_irn_arity (block);
370 idom = block; // prime the loop:
372 for (i = 0; i < n; i ++)
376 pred = get_nodes_Block (get_irn_n (block, i));
377 ndom = _get_idom (dt, pred);
380 if (_dominates (dt, idom, ndom))
385 assert (idom && "Something terribly wrong in _get_idom");
387 # ifdef VERBOSE_libfirm
388 fprintf (stdout, "%s: idom(%p) = %p\n",
389 __FILE__ ":" __PRETTY_FUNCTION__,
391 # endif /* def VERBOSE_libfirm */
394 dt->idoms [block_index] = idom;
399 // --------------------------------------------------------------------
401 // --------------------------------------------------------------------
404 Say wether a dominates b
406 bool dominates (ir_graph *g, ir_node *a, ir_node *b)
408 dt_t *dt = get_dominator_tree (g);
410 return (_dominates (dt, a, b));
414 Return the immediate dominator of block a
416 ir_node *get_idom (ir_graph *g, ir_node *a)
418 dt_t *dt = get_dominator_tree (g);
420 return (_get_idom (dt, a));
424 Allow for precomputation of necessary data
425 This will allow for a slightly faster lookup of the dominator
426 relation if one node is checked for dominance against many other nodes.
428 dom_env_t *get_dom_env (ir_graph *graph, ir_node *a)
430 dom_env_t *env = (dom_env_t*) calloc (1, sizeof (dom_env_t));
433 env->dt = get_dominator_tree (graph);
435 env->index_a = get_index (env->dt, a);
440 Dispose a dominator environment
442 void delete_dom_env (dom_env_t *env)
453 Say wether the node in env dominates b
457 bool dominates_l (dom_env_t *env, ir_node *b)
459 int index_a = env->index_a;
461 bs_t b_mask = get_mask (dt, b);
463 return (0 != (b_mask & (0x00000001 << index_a)));
467 Build a new dominator tree for the given graph
469 void build_dominator_tree (ir_graph *graph)
472 dt_walk_env_t *w = (dt_walk_env_t*) alloca (sizeof (dt_walk_env_t));
474 ir_node *end_block = get_irg_end_block (graph);
475 ir_node *start_block = get_irg_start_block (graph);
476 int n_blocks = get_n_blocks (graph);
477 dt_t *dt = get_dominator_tree (graph);
480 dt_walk_env_t *w = 0;
481 ir_node *end_block = 0;
482 ir_node *start_block = 0;
486 w = (dt_walk_env_t*) alloca (sizeof (dt_walk_env_t));
487 end_block = get_irg_end_block (graph);
488 start_block = get_irg_start_block (graph);
489 n_blocks = get_n_blocks (graph);
490 dt = get_dominator_tree (graph);
492 # ifdef DEBUG_libfirm
493 assert (graph && "no graph");
494 # endif /* def DEBUG_libfirm */
496 # ifdef VERBOSE_libfirm
497 fprintf (stdout, "%s: for graph(%p)\n", __FILE__ ":" __PRETTY_FUNCTION__, graph);
498 # endif /* def VERBOSE_libfirm */
506 w->start_block = start_block; // grrr
507 w->changed = TRUE; // at least one walk
516 irg_block_walk (end_block, update_dominators, update_dominators, (void*) w);
520 # ifdef VERBOSE_libfirm
521 fprintf (stdout, "%s: for graph(%p): %i passes\n",
522 __FILE__ ":" __PRETTY_FUNCTION__, graph, walks);
523 # endif /* def VERBOSE_libfirm */
526 /* ok, now remember it: */
527 add_dominator_tree (dt);