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