2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Blockwise walker implementation
23 * @author Michael Beck
31 #include "irgraph_t.h" /* visited flag */
38 #define _get_walk_arity(env, node) \
39 ((env)->follow_deps ? get_irn_ins_or_deps((node)) : get_irn_arity((node)))
40 #define _get_walk_irn_n(env, node, pos) \
41 ((env)->follow_deps ? get_irn_in_or_dep((node), (pos)) : get_irn_n((node), (pos)))
44 * Metadata for block walker
46 typedef struct _blk_collect_data_t {
47 struct obstack obst; /**< obstack to allocate objects on */
48 pset *blk_map; /**< Hash map: Block -> List */
49 ir_node **blk_list; /**< the Block list */
50 unsigned follow_deps : 1; /**< follow dependency edges */
54 * An entry for a block in the blk_map
56 typedef struct _block_entry_t {
57 ir_node *block; /**< the block */
58 ir_node **phi_list; /**< the list of Phi instruction */
59 ir_node **df_list; /**< the list of data flow instruction */
60 ir_node **cf_list; /**< the list of control flow instructions */
61 ir_node **entry_list; /**< list of all block entries */
65 * compare two block_entries
67 static int addr_cmp(const void *elt, const void *key) {
68 const block_entry_t *e1 = elt;
69 const block_entry_t *e2 = key;
71 return e1->block != e2->block;
75 * Returns the associates block_entry_t for an block
77 static block_entry_t *block_find_entry(ir_node *block, blk_collect_data_t *ctx)
83 elem = pset_find(ctx->blk_map, &key, HASH_PTR(block));
87 elem = obstack_alloc(&ctx->obst, sizeof(*elem));
90 elem->phi_list = NEW_ARR_F(ir_node *, 0);
91 elem->df_list = NEW_ARR_F(ir_node *, 0);
92 elem->cf_list = NEW_ARR_F(ir_node *, 0);
93 elem->entry_list = NEW_ARR_F(ir_node *, 0);
95 return pset_insert(ctx->blk_map, elem, HASH_PTR(block));
99 * traverse the pre order only, from End to Start
101 static void traverse_pre(blk_collect_data_t* blks, irg_walk_func *pre, void *env)
105 for (i = ARR_LEN(blks->blk_list) - 1; i >= 0; --i) {
106 ir_node *block = blks->blk_list[i];
107 block_entry_t *entry = block_find_entry(block, blks);
109 for (j = ARR_LEN(entry->cf_list) - 1; j >= 0; --j) {
110 ir_node *node = entry->cf_list[j];
114 for (j = ARR_LEN(entry->df_list) - 1; j >= 0; --j) {
115 ir_node *node = entry->df_list[j];
119 for (j = ARR_LEN(entry->phi_list) - 1; j >= 0; --j) {
120 ir_node *node = entry->phi_list[j];
126 DEL_ARR_F(entry->entry_list);
127 DEL_ARR_F(entry->phi_list);
128 DEL_ARR_F(entry->df_list);
129 DEL_ARR_F(entry->cf_list);
134 * traverse the post order only, from Start to End
136 static void traverse_post(blk_collect_data_t* blks, irg_walk_func *post, void *env)
140 for (i = 0; i < ARR_LEN(blks->blk_list); ++i) {
141 ir_node *block = blks->blk_list[i];
142 block_entry_t *entry = block_find_entry(block, blks);
146 for (j = 0; j < ARR_LEN(entry->phi_list); ++j) {
147 ir_node *node = entry->phi_list[j];
151 for (j = 0; j < ARR_LEN(entry->df_list); ++j) {
152 ir_node *node = entry->df_list[j];
156 for (j = 0; j < ARR_LEN(entry->cf_list); ++j) {
157 ir_node *node = entry->cf_list[j];
161 DEL_ARR_F(entry->entry_list);
162 DEL_ARR_F(entry->phi_list);
163 DEL_ARR_F(entry->df_list);
164 DEL_ARR_F(entry->cf_list);
171 static void traverse_both(blk_collect_data_t* blks, irg_walk_func *pre, irg_walk_func *post, void *env)
175 for (i = ARR_LEN(blks->blk_list) - 1; i >= 0; --i) {
176 ir_node *block = blks->blk_list[i];
177 block_entry_t *entry = block_find_entry(block, blks);
179 for (j = ARR_LEN(entry->cf_list) - 1; j >= 0; --j) {
180 ir_node *node = entry->cf_list[j];
184 for (j = ARR_LEN(entry->df_list) - 1; j >= 0; --j) {
185 ir_node *node = entry->df_list[j];
189 for (j = ARR_LEN(entry->phi_list) - 1; j >= 0; --j) {
190 ir_node *node = entry->phi_list[j];
198 traverse_post(blks, post, env);
204 static void traverse(blk_collect_data_t* blks, irg_walk_func *pre, irg_walk_func *post, void *env)
206 if (!post) traverse_pre (blks, pre, env);
207 else if (!pre) traverse_post(blks, post, env);
208 else traverse_both(blks, pre, post, env);
212 * walks over the graph and collects all blocks and all block entries
214 static void collect_walk(ir_node *node, blk_collect_data_t *env)
217 block_entry_t *entry;
220 mark_irn_visited(node);
222 if (node->op == op_Block) {
223 /* predecessors of a block are control flow nodes */
224 for (i = _get_walk_arity(env, node) - 1; i >= 0; --i) {
225 ir_node *pred = _get_walk_irn_n(env, node, i);
226 ir_node *blk = get_nodes_block(pred);
228 if (!irn_visited(pred)) {
229 collect_walk(pred, env);
231 /* control flow predecessors are always block inputs */
232 entry = block_find_entry(blk, env);
233 ARR_APP1(ir_node *, entry->entry_list, pred);
237 /* it's a block, put it into the block list */
238 if (node == get_irg_end_block(current_ir_graph)) {
239 /* Put the end block always last. If we don't do it here,
240 * it might be placed elsewhere if the graph contains
244 ARR_APP1(ir_node *, env->blk_list, node);
248 block = get_nodes_block(node);
250 if (!irn_visited(block))
251 collect_walk(block, env);
253 is_phi = is_Phi(node);
254 for (i = _get_walk_arity(env, node) - 1; i >= 0; --i) {
255 ir_node *pred = _get_walk_irn_n(env, node, i);
257 if (!irn_visited(pred)) {
258 collect_walk(pred, env);
260 /* BEWARE: predecessors of End nodes might be blocks */
261 if (is_no_Block(pred)) {
262 ir_node *blk = get_nodes_block(pred);
265 * Note that Phi predecessors are always block entries
266 * because Phi edges are always "outside" a block
268 if (block != blk || is_phi) {
269 entry = block_find_entry(blk, env);
270 ARR_APP1(ir_node *, entry->entry_list, pred);
279 * walks over the nodes of a block
280 * and collects them into the right list
282 static void collect_blks_lists(ir_node *node, ir_node *block,
283 block_entry_t *entry, blk_collect_data_t *env)
287 mark_irn_visited(node);
290 * Do not descent into Phi predecessors, these are always
291 * outside the current block because Phi edges are always
294 if (! is_Phi(node)) {
295 for (i = _get_walk_arity(env, node) - 1; i >= 0; --i) {
296 ir_node *pred = _get_walk_irn_n(env, node, i);
298 /* BEWARE: predecessors of End nodes might be blocks */
299 if (is_no_Block(pred)) {
300 ir_node *blk = get_nodes_block(pred);
302 if (!irn_visited(pred)) {
305 collect_blks_lists(pred, block, entry, env);
310 ARR_APP1(ir_node *, entry->phi_list, node);
314 if (get_irn_mode(node) == mode_X) {
315 ARR_APP1(ir_node *, entry->cf_list, node);
317 ARR_APP1(ir_node *, entry->df_list, node);
322 * walk over the graph and collect all lists
324 static void collect_lists(blk_collect_data_t *env)
327 ir_node *block, *node;
328 block_entry_t *entry;
330 inc_irg_visited(current_ir_graph);
332 for (i = ARR_LEN(env->blk_list) - 1; i >= 0; --i) {
333 block = env->blk_list[i];
334 entry = block_find_entry(block, env);
336 for (j = ARR_LEN(entry->entry_list) - 1; j >= 0; --j) {
337 node = entry->entry_list[j];
339 /* a entry might already be visited due to Phi loops */
340 if (node->visited < current_ir_graph->visited)
341 collect_blks_lists(node, block, entry, env);
347 * Intraprozedural graph walker over blocks.
350 do_irg_walk_blk(ir_graph *irg, irg_walk_func *pre, irg_walk_func *post, void *env, unsigned follow_deps)
352 ir_node *end_node = get_irg_end(irg);
353 ir_node *end_blk = get_irg_end_block(irg);
354 blk_collect_data_t blks;
355 block_entry_t *entry;
357 #ifdef INTERPROCEDURAL_VIEW
358 /* switch off interprocedural view */
359 int old_view = get_interprocedural_view();
360 set_interprocedural_view(0);
363 obstack_init(&blks.obst);
364 blks.blk_map = new_pset(addr_cmp, 1);
365 blks.blk_list = NEW_ARR_F(ir_node *, 0);
366 blks.follow_deps = follow_deps != 0;
368 /* first step: traverse the graph and fill the lists */
369 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
370 inc_irg_visited(irg);
371 collect_walk(end_node, &blks);
373 /* add the end block */
374 ARR_APP1(ir_node *, blks.blk_list, end_blk);
376 /* and the end node */
377 entry = block_find_entry(end_blk, &blks);
378 ARR_APP1(ir_node *, entry->entry_list, end_node);
380 collect_lists(&blks);
382 /* second step: traverse the list */
383 traverse(&blks, pre, post, env);
385 DEL_ARR_F(blks.blk_list);
386 del_pset(blks.blk_map);
387 obstack_free(&blks.obst, NULL);
389 #ifdef INTERPROCEDURAL_VIEW
390 set_interprocedural_view(old_view);
392 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
395 void irg_walk_blkwise_graph(ir_graph *irg, irg_walk_func *pre, irg_walk_func *post, void *env) {
396 ir_graph * rem = current_ir_graph;
398 hook_irg_walk_blkwise(irg, (generic_func *)pre, (generic_func *)post);
399 current_ir_graph = irg;
400 do_irg_walk_blk(irg, pre, post, env, 0);
401 current_ir_graph = rem;
405 void irg_walk_in_or_dep_blkwise_graph(ir_graph *irg, irg_walk_func *pre, irg_walk_func *post, void *env) {
406 ir_graph * rem = current_ir_graph;
408 hook_irg_walk_blkwise(irg, (generic_func *)pre, (generic_func *)post);
409 current_ir_graph = irg;
410 do_irg_walk_blk(irg, pre, post, env, 1);
411 current_ir_graph = rem;