- add comment
[libfirm] / ir / ana / irextbb.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    Extended basis block support.
23  * @author   Michael Beck
24  * @date     5.2005
25  * @version  $Id$
26  */
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include "irextbb_t.h"
32 #include "irgwalk.h"
33 #include "irnode_t.h"
34 #include "irgraph_t.h"
35 #include "iredges_t.h"
36 #include "irouts.h"
37 #include "xmalloc.h"
38 #include "irprintf.h"
39
40 /**
41  * Environment for extbb construction.
42  */
43 typedef struct _env {
44         struct obstack *obst;   /**< the obstack where allocations took place */
45         ir_extblk *head;        /**< head of the list of all extended blocks */
46         ir_node *start_block;   /**< the start block of the current graph */
47 } env_t;
48
49 int (is_ir_extbb)(const void *thing) {
50         return _is_ir_extbb(thing);
51 }
52
53 /**
54  * allocate a new extended block header.
55  */
56 static void allocate_extblk(ir_node *block, env_t *env) {
57         ir_extblk *extblk = obstack_alloc(env->obst, sizeof(*extblk));
58
59         extblk->kind    = k_ir_extblk;
60         extblk->visited = 1;
61         extblk->blks    = (ir_node **)env->head;
62         extblk->link    = block;
63         env->head       = extblk;
64
65         set_Block_extbb(block, extblk);
66         set_irn_link(block, NULL);
67 }
68
69 /**
70  * Returns the number of block successors.
71  * we are interested only in 1, 2 and >2.
72  */
73 static int get_block_n_succs(ir_node *block) {
74         if (edges_activated(current_ir_graph)) {
75                 const ir_edge_t *edge;
76
77                 edge = get_block_succ_first(block);
78                 if (! edge)
79                         return 0;
80                 edge = get_block_succ_next(block, edge);
81                 if (! edge)
82                         return 1;
83                 edge = get_block_succ_next(block, edge);
84                 return edge ? 3 : 2;
85         }
86         return get_Block_n_cfg_outs(block);
87 }
88
89 /**
90  * Pre block-walker. Calculates the extended block info.
91  */
92 static void pre_walk_calc_extbb(ir_node *block, void *ctx) {
93         int n = get_Block_n_cfgpreds(block);
94         env_t *env = ctx;
95
96         if (n <= 0 || n > 1 || block == env->start_block) {
97                 /*
98                  * block is a JOIN-node ie the control flow from
99                  * many other blocks joins here. block is a leader.
100                  * Note that we handle unreachable blocks (n <= 0) here too.
101                  */
102                 allocate_extblk(block, env);
103         }
104         else {    /* we have only one control flow predecessor */
105                 ir_node *add_to = get_Block_cfgpred_block(block, 0);
106
107                 /* blocks with only one BAD predecessors are leaders too */
108                 if (is_Bad(add_to)) {
109                         allocate_extblk(block, env);
110                 } else {
111                         /*
112                          * Only one control flow predecessor. This block belongs
113                          * to the same extended basic block as its predecessor.
114                          */
115                         ir_node *cf_op = skip_Proj(get_Block_cfgpred(block, 0));
116
117                         if (!irn_visited_else_mark(cf_op)) {
118                                 ir_node *pred_bl = get_nodes_block(cf_op);
119                                 if (get_block_n_succs(pred_bl) > 2) {
120                                         /* More than two successors means we have a jump table.
121                                          * we cannot include a jump target into the current extended
122                                          * basic block, so create a new one here.
123                                          */
124                                         allocate_extblk(block, env);
125                                 } else {
126                                         /* either the previous block has only one successor or
127                                          * this is the first successor after an if, include it.
128                                          */
129                                         set_Block_extbb(block, NULL);
130                                 }
131                         } else {
132                                 /* already marked, so begin a new extended block here */
133                                 allocate_extblk(block, env);
134                         }
135                 }
136         }
137 }
138
139 /** A special extended block used as sentinel */
140 static ir_extblk _sentinel = { k_ir_extblk, 0xFEA1DEAD, NULL, NULL };
141
142 /**
143  * Post block-walker. Calculates the extended block info.
144  * During construction, we use the (free) block input of all basic blocks
145  * to point to there previous block.
146  */
147 static void post_walk_calc_extbb(ir_node *block, void *ctx)
148 {
149         ir_extblk *extbb = get_Block_extbb(block);
150         env_t *env = ctx;
151         ir_extblk *sentinel = &_sentinel;
152
153         if (! extbb) {
154                 ir_node *curr, *prev, *list;
155
156                 /*
157                  * Search the leader. It can happen, that we fall into an endless
158                  * loop, because we enter an unreachable loop that is not yet detected.
159                  * We break the loop using a sentinel.
160                  */
161                 for (curr = block; !extbb; curr = prev) {
162                         prev = get_Block_cfgpred_block(curr, 0);
163                         extbb = get_Block_extbb(prev);
164                         set_Block_extbb(curr, sentinel);
165                 }
166
167                 if (extbb == sentinel) {
168                         /* We detect a dead loop. We fix this by allocating a
169                          * special Extended block
170                          */
171                         ir_printf("Dead loop detected starting with %+F::%+F\n", get_irg_entity(current_ir_graph), block);
172
173                         allocate_extblk(block, env);
174                         extbb = get_Block_extbb(block);
175                         set_Block_extbb(block, sentinel);
176                 }
177
178                 /* replace all sentinels by the extbb info */
179                 prev = block;
180                 list = NULL;
181                 while (1) {
182                         if (get_Block_extbb(prev) != sentinel)
183                                 break;
184                         set_irn_link(prev, list);
185                         list = prev;
186                         prev = get_Block_cfgpred_block(prev, 0);
187                 }
188                 /* arg, the list is in wrong order, turn around and add to the extbb list */
189                 for (curr = list; curr; curr = prev) {
190                         prev = get_irn_link(curr);
191                         set_irn_link(curr, extbb->link);
192                         extbb->link = curr;
193                         set_Block_extbb(curr, extbb);
194                         ++extbb->visited;
195                 }
196         }
197 }
198
199 /*
200  * Compute the extended basic blocks for a graph
201  */
202 void compute_extbb(ir_graph *irg) {
203         env_t env;
204         ir_extblk *extbb, *next;
205
206         if (irg->extbb_obst)
207                 obstack_free(irg->extbb_obst, NULL);
208         else
209                 irg->extbb_obst = XMALLOC(struct obstack);
210         obstack_init(irg->extbb_obst);
211
212         env.obst        = irg->extbb_obst;
213         env.head        = NULL;
214         env.start_block = get_irg_start_block(irg);
215
216         if (! edges_activated(irg)) {
217                 /* we don't have edges */
218                 assure_irg_outs(irg);
219         }
220
221         /* we must mark nodes, so increase the visited flag */
222         inc_irg_visited(irg);
223         irg_block_walk_graph(irg, pre_walk_calc_extbb, post_walk_calc_extbb, &env);
224
225         /*
226          * Ok, we have now the list of all extended blocks starting with env.head
227          * every extended block "knowns" the number of blocks in visited and
228          * the blocks are linked in link.
229          * Now we can create arrays that hold the blocks, some kind of "out" edges
230          * for the extended block
231          */
232         for (extbb = env.head; extbb; extbb = next) {
233                 int i, len = (int)extbb->visited;
234                 ir_node *block;
235
236                 next = (ir_extblk *)extbb->blks;
237
238                 extbb->blks = NEW_ARR_D(ir_node *, env.obst, len);
239
240                 for (block = extbb->link, i = 0; i < len; ++i) {
241                         ir_node *nblock = get_irn_link(block);
242
243                         /* ensure that the leader is the first one */
244                         extbb->blks[len - 1 - i] = block;
245                         set_irn_link(block, NULL);
246                         block = nblock;
247                 }
248
249 #ifndef NDEBUG
250                 /* check it */
251                 for (i = len - 1; i > 0; --i) {
252                         ir_node *blk = extbb->blks[i];
253
254                         if (get_Block_n_cfgpreds(blk) != 1) {
255                                 assert(!"Block for more than one predecessors is no leader");
256                         } else if (get_Block_cfgpred_block(blk, 0) != extbb->blks[i - 1]) {
257                                 assert(!"extbb block order wrong");
258                         }
259                 }
260 #endif
261
262                 extbb->link    = NULL;
263                 extbb->visited = 0;
264         }
265
266         irg->extblk_state = extblk_valid;
267 }
268
269 /* free all extended block info. */
270 void free_extbb(ir_graph *irg) {
271         if (irg->extbb_obst) {
272                 obstack_free(irg->extbb_obst, NULL);
273                 xfree(irg->extbb_obst);
274                 irg->extbb_obst = NULL;
275         }
276         irg->extblk_state = extblk_none;
277 }
278
279 /* Return the extended block of a node. */
280 ir_extblk *get_nodes_extbb(ir_node *node) {
281         ir_node *block = is_Block(node) ? node : get_irn_n(node, -1);
282         return get_Block_extbb(block);
283 }
284
285 /* Gets the visited counter of an extended block. */
286 ir_visited_t (get_extbb_visited)(const ir_extblk *blk) {
287         return _get_extbb_visited(blk);
288 }
289
290 /* Sets the visited counter of an extended block. */
291 void (set_extbb_visited)(ir_extblk *blk, ir_visited_t visited) {
292         _set_extbb_visited(blk, visited);
293 }
294
295 /* Mark an extended block as visited in a graph. */
296 void (mark_extbb_visited)(ir_extblk *blk) {
297         _mark_extbb_visited(blk);
298 }
299
300 /* Returns non-zero if an extended was visited. */
301 int (extbb_visited)(const ir_extblk *blk) {
302         return _extbb_visited(blk);
303 }
304
305 /* Returns non-zero if an extended block was NOT visited. */
306 int (extbb_not_visited)(const ir_extblk *blk) {
307         return _extbb_not_visited(blk);
308 }
309
310 /* Returns the link field of an extended block. */
311 void *(get_extbb_link)(const ir_extblk *blk) {
312         return _get_extbb_link(blk);
313 }
314
315 /* Sets the link field of an extended block. */
316 void (set_extbb_link)(ir_extblk *blk, void *link) {
317         _set_extbb_link(blk, link);
318 }
319
320 /* Return the number of basic blocks of an extended block */
321 int (get_extbb_n_blocks)(const ir_extblk *blk) {
322         return _get_extbb_n_blocks(blk);
323 }
324
325 /* Return the i'th basic block of an extended block */
326 ir_node *(get_extbb_block)(ir_extblk *blk, int pos) {
327         return _get_extbb_block(blk, pos);
328 }
329
330 /* Return the leader basis block of an extended block. */
331 ir_node *(get_extbb_leader)(ir_extblk *blk) {
332         return _get_extbb_leader(blk);
333 }
334
335 /* Return the node number of an extended block. */
336 long get_extbb_node_nr(ir_extblk *blk) {
337         return get_irn_node_nr(get_extbb_leader(blk));
338 }
339
340 static void irg_extblock_walk_2(ir_extblk *blk, extbb_walk_func *pre, extbb_walk_func *post, void *env) {
341         int i;
342         ir_node *node;
343
344         if (extbb_not_visited(blk)) {
345                 mark_extbb_visited(blk);
346
347                 if (pre) pre(blk, env);
348
349                 node = get_extbb_leader(blk);
350                 for (i = get_Block_n_cfgpreds(node) - 1; i >= 0; --i) {
351                         /* find the corresponding predecessor block. */
352                         ir_node *pred = get_Block_cfgpred_block(node, i);
353                         if (is_Block(pred)) {
354                                 /* recursion */
355                                 irg_extblock_walk_2(get_Block_extbb(pred), pre, post, env);
356                         }
357                         else {
358                                 assert(is_Bad(pred));
359                         }
360                 }
361
362                 if (post) post(blk, env);
363         }
364 }
365
366 /* walks only over extended Block nodes in the graph.  Has it's own visited
367    flag, so that it can be interleaved with the other walker.         */
368 void irg_extblock_walk(ir_extblk *blk, extbb_walk_func *pre, extbb_walk_func *post, void *env) {
369         ir_node *pred, *start_bl = get_irg_start_block(current_ir_graph);
370         ir_extblk *start_blk = get_Block_extbb(start_bl);
371         int i;
372
373         assert(blk);
374         inc_irg_block_visited(current_ir_graph);
375
376         /* assure the start block is the first one */
377         mark_extbb_visited(start_blk);
378         if (post)
379                 post(start_blk, env);
380         irg_extblock_walk_2(blk, pre, post, env);
381
382         /* keepalive: the endless loops ... */
383         if (blk == get_Block_extbb(get_irg_end_block(current_ir_graph))) {
384                 ir_node *node = get_irg_end(current_ir_graph);
385                 int arity = get_irn_arity(node);
386                 for (i = 0; i < arity; i++) {
387                         pred = get_irn_n(node, i);
388                         if (is_Block(pred))
389                                 irg_extblock_walk_2(get_Block_extbb(pred), pre, post, env);
390                         else if (is_Phi(pred)) {
391                                 /* Sometimes the blocks died, but are still reachable through Phis.
392                                  * Make sure the algorithms that try to remove these reach them. */
393                                 ir_node *block = get_nodes_block(pred);
394
395                                 if (! is_Bad(block))
396                                         irg_extblock_walk_2(get_Block_extbb(block), pre, post, env);
397                         }
398                 }
399         }
400
401         if (pre)
402                 pre(start_blk, env);
403 }
404
405 /* Walks only over reachable Extended Basic Block nodes in the graph. */
406 void irg_extblock_walk_graph(ir_graph *irg, extbb_walk_func *pre, extbb_walk_func *post, void *env) {
407         ir_node *endbl = get_irg_end_block(irg);
408         ir_extblk *blk = get_Block_extbb(endbl);
409         ir_graph *rem  = current_ir_graph;
410         current_ir_graph = irg;
411         irg_extblock_walk(blk, pre, post, env);
412         current_ir_graph = rem;
413 }