3 * File name: ir/ir/irgwalk_blk.c
9 * Copyright: (c) 1999-2004 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
17 #include "irgraph_t.h" /* visited flag */
25 * Metadata for block walker
27 typedef struct _blk_collect_data_t {
28 struct obstack obst; /**< obstack to allocate objects on */
29 pset *blk_map; /**< Hash map: Block -> List */
30 ir_node **blk_list; /**< the Block list */
34 * An entry for a block in the blk_map
36 typedef struct _block_entry_t {
37 ir_node *block; /**< the block */
38 ir_node **phi_list; /**< the list of Phi instruction */
39 ir_node **df_list; /**< the list of data flow instruction */
40 ir_node **cf_list; /**< the list of control flow instructions */
41 ir_node **entry_list; /**< list of all block entries */
45 * compare two block_entries
47 static int addr_cmp(const void *elt, const void *key) {
48 const block_entry_t *e1 = elt;
49 const block_entry_t *e2 = key;
51 return e1->block != e2->block;
55 * Returns the associates block_entry_t for an block
57 static block_entry_t *block_find_entry(ir_node *block, blk_collect_data_t *ctx)
63 elem = pset_find(ctx->blk_map, &key, HASH_PTR(block));
67 elem = obstack_alloc(&ctx->obst, sizeof(*elem));
70 elem->phi_list = NEW_ARR_F(ir_node *, 0);
71 elem->df_list = NEW_ARR_F(ir_node *, 0);
72 elem->cf_list = NEW_ARR_F(ir_node *, 0);
73 elem->entry_list = NEW_ARR_F(ir_node *, 0);
75 return pset_insert(ctx->blk_map, elem, HASH_PTR(block));
79 * traverse the pre order only, from End to Start
81 static void traverse_pre(blk_collect_data_t* blks, irg_walk_func *pre, void *env)
85 for (i = ARR_LEN(blks->blk_list) - 1; i >= 0; --i) {
86 ir_node *block = blks->blk_list[i];
87 block_entry_t *entry = block_find_entry(block, blks);
89 for (j = ARR_LEN(entry->cf_list) - 1; j >= 0; --j) {
90 ir_node *node = entry->cf_list[j];
94 for (j = ARR_LEN(entry->df_list) - 1; j >= 0; --j) {
95 ir_node *node = entry->df_list[j];
99 for (j = ARR_LEN(entry->phi_list) - 1; j >= 0; --j) {
100 ir_node *node = entry->phi_list[j];
106 DEL_ARR_F(entry->entry_list);
107 DEL_ARR_F(entry->phi_list);
108 DEL_ARR_F(entry->df_list);
109 DEL_ARR_F(entry->cf_list);
114 * traverse the post order only, from Start to End
116 static void traverse_post(blk_collect_data_t* blks, irg_walk_func *post, void *env)
120 for (i = 0; i < ARR_LEN(blks->blk_list); ++i) {
121 ir_node *block = blks->blk_list[i];
122 block_entry_t *entry = block_find_entry(block, blks);
126 for (j = 0; j < ARR_LEN(entry->phi_list); ++j) {
127 ir_node *node = entry->phi_list[j];
131 for (j = 0; j < ARR_LEN(entry->df_list); ++j) {
132 ir_node *node = entry->df_list[j];
136 for (j = 0; j < ARR_LEN(entry->cf_list); ++j) {
137 ir_node *node = entry->cf_list[j];
141 DEL_ARR_F(entry->entry_list);
142 DEL_ARR_F(entry->phi_list);
143 DEL_ARR_F(entry->df_list);
144 DEL_ARR_F(entry->cf_list);
151 static void traverse_both(blk_collect_data_t* blks, irg_walk_func *pre, irg_walk_func *post, void *env)
155 for (i = ARR_LEN(blks->blk_list) - 1; i >= 0; --i) {
156 ir_node *block = blks->blk_list[i];
157 block_entry_t *entry = block_find_entry(block, blks);
159 for (j = ARR_LEN(entry->cf_list) - 1; j >= 0; --j) {
160 ir_node *node = entry->cf_list[j];
164 for (j = ARR_LEN(entry->df_list) - 1; j >= 0; --j) {
165 ir_node *node = entry->df_list[j];
169 for (j = ARR_LEN(entry->phi_list) - 1; j >= 0; --j) {
170 ir_node *node = entry->phi_list[j];
178 traverse_post(blks, post, env);
185 static void traverse(blk_collect_data_t* blks, irg_walk_func *pre, irg_walk_func *post, void *env)
187 if (!post) traverse_pre (blks, pre, env);
188 else if (!pre) traverse_post(blks, post, env);
189 else traverse_both(blks, pre, post, env);
194 * walks over the graph and collects all blocks and all block entries
196 static void collect_walk(ir_node *node, blk_collect_data_t *env)
199 block_entry_t *entry;
202 mark_irn_visited(node);
204 if (node->op == op_Block) {
205 /* predecessors of a block are control flow nodes */
206 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
207 ir_node *pred = get_irn_n(node, i);
208 ir_node *blk = get_nodes_block(pred);
210 if (irn_not_visited(pred)) {
211 collect_walk(pred, env);
213 /* control flow predecessors are always block inputs */
214 entry = block_find_entry(blk, env);
215 ARR_APP1(ir_node *, entry->entry_list, pred);
219 /* it's a block, put it into the block list */
220 if (node == get_irg_end_block(current_ir_graph)) {
221 /* Put the end block always last. If we don't do it here,
222 * it might be placed elsewhere if the graph contains
227 ARR_APP1(ir_node *, env->blk_list, node);
231 block = get_nodes_block(node);
233 if (irn_not_visited(block))
234 collect_walk(block, env);
236 is_phi = is_Phi(node);
237 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
238 ir_node *pred = get_irn_n(node, i);
240 if (irn_not_visited(pred)) {
241 collect_walk(pred, env);
243 /* BEWARE: predecessors of End nodes might be blocks */
244 if (is_no_Block(pred)) {
245 ir_node *blk = get_nodes_block(pred);
248 * Note that Phi predecessors are always block entries
249 * because Phi edges are always "outside" a block
251 if (block != blk || is_phi) {
252 entry = block_find_entry(blk, env);
253 ARR_APP1(ir_node *, entry->entry_list, pred);
262 * walks over the nodes of a block
263 * and collects them into the right list
265 static void collect_blks_lists(ir_node *node, ir_node *block,
266 block_entry_t *entry, blk_collect_data_t *env)
270 mark_irn_visited(node);
273 * Do not descent into Phi predecessors, these are always
274 * outside the current block because Phi edges are always
277 if (! is_Phi(node)) {
278 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
279 ir_node *pred = get_irn_n(node, i);
281 /* BEWARE: predecessors of End nodes might be blocks */
282 if (is_no_Block(pred)) {
283 ir_node *blk = get_nodes_block(pred);
285 if (irn_not_visited(pred)) {
288 collect_blks_lists(pred, block, entry, env);
294 ARR_APP1(ir_node *, entry->phi_list, node);
298 if (get_irn_mode(node) == mode_X) {
299 ARR_APP1(ir_node *, entry->cf_list, node);
302 ARR_APP1(ir_node *, entry->df_list, node);
307 * walk over the graph and collect all lists
309 static void collect_lists(blk_collect_data_t *env)
312 ir_node *block, *node;
313 block_entry_t *entry;
315 inc_irg_visited(current_ir_graph);
317 for (i = ARR_LEN(env->blk_list) - 1; i >= 0; --i) {
318 block = env->blk_list[i];
319 entry = block_find_entry(block, env);
321 for (j = ARR_LEN(entry->entry_list) - 1; j >= 0; --j) {
322 node = entry->entry_list[j];
324 /* a entry might already be visited due to Phi loops */
325 if (node->visited < current_ir_graph->visited)
326 collect_blks_lists(node, block, entry, env);
332 * Intraprozedural graph walker over blocks.
335 do_irg_walk_blk(ir_graph *irg, irg_walk_func *pre, irg_walk_func *post, void *env)
337 ir_node *end_node = get_irg_end(irg);
338 ir_node *end_blk = get_irg_end_block(irg);
339 blk_collect_data_t blks;
340 int old_view = get_interprocedural_view();
341 block_entry_t *entry;
343 /* switch off interprocedural view */
344 set_interprocedural_view(0);
346 obstack_init(&blks.obst);
347 blks.blk_map = new_pset(addr_cmp, 1);
348 blks.blk_list = NEW_ARR_F(ir_node *, 0);
350 /* first step: traverse the graph and fill the lists */
351 inc_irg_visited(irg);
352 collect_walk(end_node, &blks);
354 /* add the end block */
355 ARR_APP1(ir_node *, blks.blk_list, end_blk);
357 /* and the end node */
358 entry = block_find_entry(end_blk, &blks);
359 ARR_APP1(ir_node *, entry->entry_list, end_node);
361 collect_lists(&blks);
363 /* second step: traverse the list */
364 traverse(&blks, pre, post, env);
366 DEL_ARR_F(blks.blk_list);
367 del_pset(blks.blk_map);
368 obstack_free(&blks.obst, NULL);
370 set_interprocedural_view(old_view);
373 void irg_walk_blkwise_graph(ir_graph *irg, irg_walk_func *pre, irg_walk_func *post, void *env)
375 ir_graph * rem = current_ir_graph;
377 hook_irg_walk_blkwise(irg, (generic_func *)pre, (generic_func *)post);
378 current_ir_graph = irg;
379 do_irg_walk_blk(irg, pre, post, env);
380 current_ir_graph = rem;