1947e8db69651f81feb8c1c1e03febc885e41a39
[libfirm] / ir / ir / irgwalk_blk.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Blockwise walker implementation
23  * @author  Michael Beck
24  * @version $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "irnode_t.h"
31 #include "irgraph_t.h" /* visited flag */
32 #include "irgwalk.h"
33 #include "pset.h"
34 #include "irhooks.h"
35 #include "array.h"
36 #include "hashptr.h"
37
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)))
42
43 /**
44  * Metadata for block walker
45  */
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 */
51 } blk_collect_data_t;
52
53 /**
54  * An entry for a block in the blk_map
55  */
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 */
62 } block_entry_t;
63
64 /**
65  * compare two block_entries
66  */
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;
70
71         return e1->block != e2->block;
72 }
73
74 /**
75  * Returns the associates block_entry_t for an block
76  */
77 static block_entry_t *block_find_entry(ir_node *block, blk_collect_data_t *ctx)
78 {
79         block_entry_t key;
80         block_entry_t *elem;
81
82         key.block = block;
83         elem = pset_find(ctx->blk_map, &key, HASH_PTR(block));
84         if (elem)
85                 return elem;
86
87         elem = obstack_alloc(&ctx->obst, sizeof(*elem));
88
89         elem->block      = block;
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);
94
95         return pset_insert(ctx->blk_map, elem, HASH_PTR(block));
96 }
97
98 /**
99  * traverse the pre order only, from End to Start
100  */
101 static void traverse_pre(blk_collect_data_t* blks, irg_walk_func *pre, void *env)
102 {
103         int i, j;
104
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);
108
109                 for (j = ARR_LEN(entry->cf_list) - 1; j >= 0; --j) {
110                         ir_node *node = entry->cf_list[j];
111                         pre(node, env);
112                 }
113
114                 for (j = ARR_LEN(entry->df_list) - 1; j >= 0; --j) {
115                         ir_node *node = entry->df_list[j];
116                         pre(node, env);
117                 }
118
119                 for (j = ARR_LEN(entry->phi_list) - 1; j >= 0; --j) {
120                         ir_node *node = entry->phi_list[j];
121                         pre(node, env);
122                 }
123
124                 pre(block, env);
125
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);
130         }
131 }
132
133 /**
134  * traverse the post order only, from Start to End
135  */
136 static void traverse_post(blk_collect_data_t* blks, irg_walk_func *post, void *env)
137 {
138         int i, j;
139
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);
143
144                 post(block, env);
145
146                 for (j = 0; j < ARR_LEN(entry->phi_list); ++j) {
147                         ir_node *node = entry->phi_list[j];
148                         post(node, env);
149                 }
150
151                 for (j = 0; j < ARR_LEN(entry->df_list); ++j) {
152                         ir_node *node = entry->df_list[j];
153                         post(node, env);
154                 }
155
156                 for (j = 0; j < ARR_LEN(entry->cf_list); ++j) {
157                         ir_node *node = entry->cf_list[j];
158                         post(node, env);
159                 }
160
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);
165         }
166 }
167
168 /**
169  * traverse both
170  */
171 static void traverse_both(blk_collect_data_t* blks, irg_walk_func *pre, irg_walk_func *post, void *env)
172 {
173         int i, j;
174
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);
178
179                 for (j = ARR_LEN(entry->cf_list) - 1; j >= 0; --j) {
180                         ir_node *node = entry->cf_list[j];
181                         pre(node, env);
182                 }
183
184                 for (j = ARR_LEN(entry->df_list) - 1; j >= 0; --j) {
185                         ir_node *node = entry->df_list[j];
186                         pre(node, env);
187                 }
188
189                 for (j = ARR_LEN(entry->phi_list) - 1; j >= 0; --j) {
190                         ir_node *node = entry->phi_list[j];
191                         pre(node, env);
192                 }
193
194                 pre(block, env);
195         }
196
197         /* second step */
198         traverse_post(blks, post, env);
199 }
200
201 /**
202  * Do the traversal
203  */
204 static void traverse(blk_collect_data_t* blks, irg_walk_func *pre, irg_walk_func *post, void *env)
205 {
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);
209 }
210
211 /**
212  * walks over the graph and collects all blocks and all block entries
213  */
214 static void collect_walk(ir_node *node, blk_collect_data_t *env)
215 {
216         int           i, is_phi;
217         block_entry_t *entry;
218         ir_node       *block;
219
220         mark_irn_visited(node);
221
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);
227
228                         if (!irn_visited(pred)) {
229                                 collect_walk(pred, env);
230
231                                 /* control flow predecessors are always block inputs */
232                                 entry = block_find_entry(blk, env);
233                                 ARR_APP1(ir_node *, entry->entry_list, pred);
234                         }
235                 }
236
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
241                          * endless loops.
242                          */
243                 } else {
244                         ARR_APP1(ir_node *, env->blk_list, node);
245                 }
246         }
247         else {
248                 block = get_nodes_block(node);
249
250                 if (!irn_visited(block))
251                         collect_walk(block, env);
252
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);
256
257                         if (!irn_visited(pred)) {
258                                 collect_walk(pred, env);
259
260                                 /* BEWARE: predecessors of End nodes might be blocks */
261                                 if (is_no_Block(pred)) {
262                                         ir_node *blk  = get_nodes_block(pred);
263
264                                         /*
265                                          * Note that Phi predecessors are always block entries
266                                          * because Phi edges are always "outside" a block
267                                          */
268                                         if (block != blk || is_phi) {
269                                                 entry = block_find_entry(blk, env);
270                                                 ARR_APP1(ir_node *, entry->entry_list, pred);
271                                         }
272                                 }
273                         }
274                 }
275         }
276 }
277
278 /**
279  * walks over the nodes of a block
280  * and collects them into the right list
281  */
282 static void collect_blks_lists(ir_node *node, ir_node *block,
283                                block_entry_t *entry, blk_collect_data_t *env)
284 {
285         int i;
286
287         mark_irn_visited(node);
288
289         /*
290          * Do not descent into Phi predecessors, these are always
291          * outside the current block because Phi edges are always
292          * "outside".
293          */
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);
297
298                         /* BEWARE: predecessors of End nodes might be blocks */
299                         if (is_no_Block(pred)) {
300                                 ir_node *blk  = get_nodes_block(pred);
301
302                                 if (!irn_visited(pred)) {
303                                         if (block != blk)
304                                                 continue;
305                                         collect_blks_lists(pred, block, entry, env);
306                                 }
307                         }
308                 }
309         } else {
310                 ARR_APP1(ir_node *, entry->phi_list, node);
311                 return;
312         }
313
314         if (get_irn_mode(node) == mode_X) {
315                 ARR_APP1(ir_node *, entry->cf_list, node);
316         } else {
317                 ARR_APP1(ir_node *, entry->df_list, node);
318         }
319 }
320
321 /**
322  * walk over the graph and collect all lists
323  */
324 static void collect_lists(blk_collect_data_t *env)
325 {
326         int             i, j;
327         ir_node         *block, *node;
328         block_entry_t   *entry;
329
330         inc_irg_visited(current_ir_graph);
331
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);
335
336                 for (j = ARR_LEN(entry->entry_list) - 1; j >= 0; --j) {
337                         node = entry->entry_list[j];
338
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);
342                 }
343         }
344 }
345
346 /**
347  * Intraprozedural graph walker over blocks.
348  */
349 static void
350 do_irg_walk_blk(ir_graph *irg, irg_walk_func *pre, irg_walk_func *post, void *env, unsigned follow_deps)
351 {
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;
356
357 #ifdef INTERPROCEDURAL_VIEW
358         /* switch off interprocedural view */
359         int old_view       = get_interprocedural_view();
360         set_interprocedural_view(0);
361 #endif
362
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;
367
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);
372
373         /* add the end block */
374         ARR_APP1(ir_node *, blks.blk_list, end_blk);
375
376         /* and the end node */
377         entry = block_find_entry(end_blk, &blks);
378         ARR_APP1(ir_node *, entry->entry_list, end_node);
379
380         collect_lists(&blks);
381
382         /* second step: traverse the list */
383         traverse(&blks, pre, post, env);
384
385         DEL_ARR_F(blks.blk_list);
386         del_pset(blks.blk_map);
387         obstack_free(&blks.obst, NULL);
388
389 #ifdef INTERPROCEDURAL_VIEW
390         set_interprocedural_view(old_view);
391 #endif
392         ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
393 }
394
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;
397
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;
402 }
403
404
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;
407
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;
412 }