9cbd308138973e072a31d14d9263f6ddbf4f056c
[libfirm] / ir / ana / irextbb.c
1 /*
2  * Copyrigth (C) 1995-2007 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 typedef struct _env {
41   struct obstack *obst;   /**< the obstack where allocations took place */
42   ir_extblk *head;        /**< head of the list of all extended blocks */
43   ir_node *start_block;   /**< the start block of the current graph */
44 } env_t;
45
46 int (is_ir_extbb)(const void *thing) {
47   return _is_ir_extbb(thing);
48 }
49
50 /**
51  * allocate a new extended block header.
52  */
53 static void allocate_extblk(ir_node *block, env_t *env)
54 {
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 {
92   int n = get_Block_n_cfgpreds(block);
93   env_t *env = ctx;
94
95   if (n <= 0 || n > 1 || block == env->start_block) {
96     /*
97      * block is a JOIN-node ie the control flow from
98      * many other blocks joins here. block is a leader.
99      * Note that we handle unreachable blocks (n <= 0) here too.
100      */
101     allocate_extblk(block, env);
102   }
103   else {    /* we have only one control flow predecessor */
104     ir_node *add_to = get_Block_cfgpred_block(block, 0);
105
106     /* blocks with only one BAD predecessors are leaders too */
107     if (is_Bad(add_to)) {
108       allocate_extblk(block, env);
109     } else {
110       /*
111        * Only one control flow predecessor. This block belongs
112        * to the same extended basic block as its predecessor.
113        */
114       ir_node *cf_op = skip_Proj(get_Block_cfgpred(block, 0));
115
116       if (irn_not_visited(cf_op)) {
117         ir_node *pred_bl = get_nodes_block(cf_op);
118         if (get_block_n_succs(pred_bl) > 2) {
119           /* More than two successors means we have a jump table.
120            * we cannot include a jump target into the current extended
121            * basic block, so create a new one here.
122            */
123           allocate_extblk(block, env);
124         } else {
125           /* either the previous block has only one successor or
126            * this is the first successor after an if, include it.
127            */
128           set_Block_extbb(block, NULL);
129         }
130         mark_irn_visited(cf_op);
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 };
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(sizeof(*irg->extbb_obst));
210   }
211   obstack_init(irg->extbb_obst);
212
213   env.obst        = irg->extbb_obst;
214   env.head        = NULL;
215   env.start_block = get_irg_start_block(irg);
216
217   if (! edges_activated(irg)) {
218     /* we don't have edges */
219     assure_irg_outs(irg);
220   }
221
222   /* we must mark nodes, so increase the visited flag */
223   inc_irg_visited(irg);
224   irg_block_walk_graph(irg, pre_walk_calc_extbb, post_walk_calc_extbb, &env);
225
226   /*
227    * Ok, we have now the list of all extended blocks starting with env.head
228    * every extended block "knowns" the number of blocks in visited and
229    * the blocks are linked in link.
230    * Now we can create arrays that hold the blocks, some kind of "out" edges
231    * for the extended block
232    */
233   for (extbb = env.head; extbb; extbb = next) {
234     int i, len = (int)extbb->visited;
235     ir_node *block;
236
237     next = (ir_extblk *)extbb->blks;
238
239     extbb->blks = NEW_ARR_D(ir_node *, env.obst, len);
240
241     for (block = extbb->link, i = 0; i < len; ++i) {
242       ir_node *nblock = get_irn_link(block);
243
244       /* ensure that the leader is the first one */
245       extbb->blks[len - 1 - i] = block;
246       set_irn_link(block, NULL);
247       block = nblock;
248     }
249
250 #ifndef NDEBUG
251     /* check it */
252     for (i = len - 1; i > 0; --i) {
253       ir_node *blk = extbb->blks[i];
254
255       if (get_Block_n_cfgpreds(blk) != 1) {
256         assert(!"Block for more than one predecessors is no leader");
257       } else if (get_Block_cfgpred_block(blk, 0) != extbb->blks[i - 1]) {
258         assert(!"extbb block order wrong");
259       }
260     }
261 #endif
262
263     extbb->link    = NULL;
264     extbb->visited = 0;
265   }
266
267   irg->extblk_state = extblk_valid;
268 }
269
270 /* free all extended block info. */
271 void free_extbb(ir_graph *irg) {
272   if (irg->extbb_obst) {
273     obstack_free(irg->extbb_obst, NULL);
274     xfree(irg->extbb_obst);
275     irg->extbb_obst = NULL;
276   }
277   irg->extblk_state = extblk_none;
278 }
279
280 /* Return the extended block of a node. */
281 ir_extblk *get_nodes_extbb(ir_node *node) {
282   ir_node *block = is_Block(node) ? node : get_irn_n(node, -1);
283   return get_Block_extbb(block);
284 }
285
286 /* Gets the visited counter of an extended block. */
287 unsigned long (get_extbb_visited)(const ir_extblk *blk) {
288   return _get_extbb_visited(blk);
289 }
290
291 /* Sets the visited counter of an extended block. */
292 void (set_extbb_visited)(ir_extblk *blk, unsigned long visited) {
293   _set_extbb_visited(blk, visited);
294 }
295
296 /* Mark an extended block as visited in a graph. */
297 void (mark_extbb_visited)(ir_extblk *blk) {
298   _mark_extbb_visited(blk);
299 }
300
301 /* Returns non-zero if an extended was visited. */
302 int (extbb_visited)(const ir_extblk *blk) {
303   return _extbb_visited(blk);
304 }
305
306 /* Returns non-zero if an extended block was NOT visited. */
307 int (extbb_not_visited)(const ir_extblk *blk) {
308   return _extbb_not_visited(blk);
309 }
310
311 /* Returns the link field of an extended block. */
312 void *(get_extbb_link)(const ir_extblk *blk) {
313   return _get_extbb_link(blk);
314 }
315
316 /* Sets the link field of an extended block. */
317 void (set_extbb_link)(ir_extblk *blk, void *link) {
318   _set_extbb_link(blk, link);
319 }
320
321 /* Return the number of basic blocks of an extended block */
322 int (get_extbb_n_blocks)(const ir_extblk *blk) {
323   return _get_extbb_n_blocks(blk);
324 }
325
326 /* Return the i'th basic block of an extended block */
327 ir_node *(get_extbb_block)(ir_extblk *blk, int pos) {
328   return _get_extbb_block(blk, pos);
329 }
330
331 /* Return the leader basis block of an extended block. */
332 ir_node *(get_extbb_leader)(ir_extblk *blk) {
333   return _get_extbb_leader(blk);
334 }
335
336 /* Return the node number of an extended block. */
337 long get_extbb_node_nr(ir_extblk *blk) {
338   return get_irn_node_nr(get_extbb_leader(blk));
339 }
340
341 static void irg_extblock_walk_2(ir_extblk *blk, extbb_walk_func *pre, extbb_walk_func *post, void *env)
342 {
343   int i;
344   ir_node *node;
345
346   if (extbb_not_visited(blk)) {
347     mark_extbb_visited(blk);
348
349     if (pre) pre(blk, env);
350
351     node = get_extbb_leader(blk);
352     for (i = get_Block_n_cfgpreds(node) - 1; i >= 0; --i) {
353       /* find the corresponding predecessor block. */
354       ir_node *pred = get_Block_cfgpred_block(node, i);
355       if (is_Block(pred)) {
356         /* recursion */
357         irg_extblock_walk_2(get_Block_extbb(pred), pre, post, env);
358       }
359       else {
360         assert(is_Bad(pred));
361       }
362     }
363
364     if (post) post(blk, env);
365   }
366 }
367
368 /* walks only over extended Block nodes in the graph.  Has it's own visited
369    flag, so that it can be interleaved with the other walker.         */
370 void irg_extblock_walk(ir_extblk *blk, extbb_walk_func *pre, extbb_walk_func *post, void *env)
371 {
372   ir_node *pred, *start_bl = get_irg_start_block(current_ir_graph);
373   ir_extblk *start_blk = get_Block_extbb(start_bl);
374   int i;
375
376   assert(blk);
377   assert(!get_interprocedural_view());   /* interprocedural_view not implemented */
378   inc_irg_block_visited(current_ir_graph);
379
380   /* assure the start block is the first one */
381   mark_extbb_visited(start_blk);
382   if (post)
383     post(start_blk, env);
384   irg_extblock_walk_2(blk, pre, post, env);
385
386   /* keepalive: the endless loops ... */
387   if (blk == get_Block_extbb(get_irg_end_block(current_ir_graph))) {
388     ir_node *node = get_irg_end(current_ir_graph);
389     int arity = get_irn_arity(node);
390     for (i = 0; i < arity; i++) {
391       pred = get_irn_n(node, i);
392       if (is_Block(pred))
393         irg_extblock_walk_2(get_Block_extbb(pred), pre, post, env);
394       else if (is_Phi(pred)) {
395         /* Sometimes the blocks died, but are still reachable through Phis.
396          * Make sure the algorithms that try to remove these reach them. */
397         ir_node *block = get_nodes_block(pred);
398
399         if (! is_Bad(block))
400           irg_extblock_walk_2(get_Block_extbb(block), pre, post, env);
401       }
402     }
403   }
404
405   if (pre)
406     pre(start_blk, env);
407 }
408
409 /* Walks only over reachable Extended Basic Block nodes in the graph. */
410 void irg_extblock_walk_graph(ir_graph *irg, extbb_walk_func *pre, extbb_walk_func *post, void *env)
411 {
412   ir_node *endbl = get_irg_end_block(irg);
413   ir_extblk *blk = get_Block_extbb(endbl);
414   ir_graph *rem  = current_ir_graph;
415   current_ir_graph = irg;
416   irg_extblock_walk(blk, pre, post, env);
417   current_ir_graph = rem;
418 }