Fixed keep-alive visiting for extbb walker
[libfirm] / ir / ana / irextbb.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ana/irextbb.c
4  * Purpose:     Extended basis block support.
5  * Author:      Michael Beck
6  * Modified by:
7  * Created:     5.2005
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2002-2005 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 /**
14  * @file irextbb.c
15  *
16  *  Computes extended basic blocks.
17  *
18  *  @author Michael Beck
19  */
20 #include "irextbb_t.h"
21 #include "irgwalk.h"
22 #include "irnode_t.h"
23 #include "irgraph_t.h"
24 #include "iredges_t.h"
25 #include "irouts.h"
26 #include "xmalloc.h"
27 #include "irprintf.h"
28
29 typedef struct _env {
30   struct obstack *obst;   /**< the obstack where allocations took place */
31   ir_extblk *head;        /**< head of the list of all extended blocks */
32 } env_t;
33
34 int (is_ir_extbb)(const void *thing) {
35         return _is_ir_extbb(thing);
36 }
37
38 /**
39  * allocate a new extended block header.
40  */
41 static void allocate_extblk(ir_node *block, env_t *env)
42 {
43   ir_extblk *extblk = obstack_alloc(env->obst, sizeof(*extblk));
44
45   extblk->kind    = k_ir_extblk;
46   extblk->visited = 1;
47   extblk->blks    = (ir_node **)env->head;
48   extblk->link    = block;
49   env->head       = extblk;
50
51   set_Block_extbb(block, extblk);
52   set_irn_link(block, NULL);
53 }
54
55 /**
56  * add a block to an extended block
57  */
58 static void addto_extblk(ir_extblk *extblk, ir_node *block)
59 {
60   /* link all blocks belonging to this extended block */
61   set_irn_link(block, extblk->link);
62
63   extblk->link = block;
64   extblk->visited++;
65
66   set_Block_extbb(block, extblk);
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 #ifdef FIRM_EDGES_INPLACE
75   if (edges_activated(current_ir_graph)) {
76     const ir_edge_t *edge;
77
78     edge = get_block_succ_first(block);
79     if (! edge)
80       return 0;
81     edge = get_block_succ_next(block, edge);
82     if (! edge)
83       return 1;
84     edge = get_block_succ_next(block, edge);
85     return edge ? 3 : 2;
86   }
87 #endif
88   return get_Block_n_cfg_outs(block);
89 }
90
91 /**
92  * Pre block-walker. Calculates the extended block info.
93  */
94 static void pre_walk_calc_extbb(ir_node *block, void *ctx)
95 {
96   int n = get_Block_n_cfgpreds(block);
97   env_t *env = ctx;
98
99   if (n <= 0 || n > 1 ||
100     block == get_irg_start_block(current_ir_graph)) {
101     /*
102      * block is a JOIN-node ie the control flow from
103      * many other blocks joins here. block is a leader.
104      * Note that we handle unreachable blocks (n <= 0) here too.
105      */
106     allocate_extblk(block, env);
107   }
108   else {    /* we have only one control flow predecessor */
109     ir_node *add_to = get_Block_cfgpred_block(block, 0);
110
111     /* blocks with only one BAD predecessors are leaders too */
112     if (is_Bad(add_to)) {
113       allocate_extblk(block, env);
114     }
115     else {
116       /*
117        * Only one control flow predecessor. This block belongs
118        * to the same extended basic block as its predecessor.
119        */
120       ir_node *cf_op = skip_Proj(get_Block_cfgpred(block, 0));
121
122       if (irn_not_visited(cf_op)) {
123         ir_node *pred_bl = get_nodes_block(cf_op);
124         if (get_block_n_succs(pred_bl) > 2) {
125           /* More than two successors means we have a jump table.
126            * we cannot include a jump target into the current extended
127            * basic block, so create a new one here.
128            */
129           allocate_extblk(block, env);
130         }
131         else {
132           /* either the previous block has only one successor or
133            * this is the first successor after an if, include it.
134            */
135           set_Block_extbb(block, NULL);
136         }
137         mark_irn_visited(cf_op);
138       }
139       else {
140         /* already marked, so begin a new extended block here */
141         allocate_extblk(block, env);
142       }
143     }
144   }
145 }
146
147 /** A special extended block used as sentinel */
148 static ir_extblk _sentinel = { k_ir_extblk, 0xFEA1DEAD };
149
150 /**
151  * Post block-walker. Calculates the extended block info.
152  * During construction, we use the (free) block input of all basic blocks
153  * to point to there previous block.
154  */
155 static void post_walk_calc_extbb(ir_node *block, void *ctx)
156 {
157   ir_extblk *extbb = get_Block_extbb(block);
158   env_t *env = ctx;
159   ir_extblk *sentinel = &_sentinel;
160
161   if (! extbb) {
162     ir_node *curr, *prev;
163
164     /*
165      * Search the leader. It can happen, that we fall into an endless
166      * loop, because we enter an unreachable loop that is not yet detected.
167      * We break the loop using a sentinel.
168      */
169     for (curr = block; !extbb; curr = prev) {
170       prev = get_Block_cfgpred_block(curr, 0);
171       extbb = get_Block_extbb(prev);
172       set_Block_extbb(curr, sentinel);
173     }
174
175     if (extbb == sentinel) {
176       /* We detect a dead loop. We fix this by allocating an
177        * special Extended block
178        */
179       ir_printf("Dead loop detected starting with %+F::%+F\n", get_irg_entity(current_ir_graph), block);
180
181       allocate_extblk(block, env);
182       extbb = get_Block_extbb(block);
183       set_Block_extbb(block, sentinel);
184     }
185
186     /* replace all sentinels by the extbb info */
187     prev = block;
188     while (1) {
189       if (get_Block_extbb(prev) != sentinel)
190         break;
191       set_Block_extbb(prev, extbb);
192                         ++extbb->visited;
193                         set_irn_link(prev, extbb->link);
194                         extbb->link = prev;
195       prev = get_Block_cfgpred_block(prev, 0);
196     }
197   }
198 }
199
200 #ifdef NEED_SORT
201 /**
202  * Walker for cf_dependent_on().
203  * This function searches a node tgt recursively from a given node
204  * but is restricted to the given extended basic block.
205  *
206  * @return 1 if tgt was reachable from curr, 0 if not.
207  */
208 static int check_cf_dependence(ir_node *curr, ir_node *tgt, ir_extblk *extbb)
209 {
210         int n, i;
211
212   /* Note: we did not include the leader in our serach, so
213      there could not be any loops here. */
214         if (get_Block_extbb(curr) != extbb)
215                 return 0;
216
217         if (curr == tgt)
218                 return 1;
219
220         for (i = 0, n = get_Block_n_cfgpreds(curr); i < n; ++i) {
221                 if (check_cf_dependence(get_Block_cfgpred_block(curr, i), tgt, extbb))
222                         return 1;
223         }
224         return 0;
225 }
226
227 /**
228  * Check if a block is control dependent on another one.
229  * Both blocks must be in the same extended basic block.
230  * @param blk1  The first block.
231  * @param blk2  The second block.
232  * @return 1, if blk1 is control dependent (transitively) on blk2, 0 if not.
233  */
234 static int cf_dependent_on(ir_node *blk1, ir_node *blk2)
235 {
236         ir_extblk *extbb = get_Block_extbb(blk1);
237         ir_graph *irg = get_irn_irg(blk1);
238
239         assert(extbb == get_Block_extbb(blk2));
240         return check_cf_dependence(blk1, blk2, extbb);
241 }
242
243 /**
244  * Compare two blocks for dependency.
245  *
246  * @return
247  *      0  if both blocks nodes are equal
248  *      1  block b1 depends on block b2
249  *      -1 block b2 depends on block d2
250  */
251 static int block_order(const void *b1, const void *b2)
252 {
253         ir_node *n1 = *(ir_node **)b1;
254         ir_node *n2 = *(ir_node **)b2;
255
256         return n1 == n2 ? 0 : (cf_dependent_on(n1, n2) ? -1 : 1);
257 }
258 #endif /* NEED_SORT */
259
260 /*
261  * Compute the extended basic blocks for a graph
262  */
263 void compute_extbb(ir_graph *irg) {
264   env_t env;
265   ir_extblk *extbb, *next;
266
267   if (irg->extbb_obst)
268     obstack_free(irg->extbb_obst, NULL);
269   else {
270     irg->extbb_obst = xmalloc(sizeof(*irg->extbb_obst));
271   }
272   obstack_init(irg->extbb_obst);
273
274   env.obst = irg->extbb_obst;
275   env.head = NULL;
276
277 #ifdef FIRM_EDGES_INPLACE
278   if (edges_activated(irg)) {
279     /* we have edges */
280   }
281   else
282 #endif
283   if (get_irg_outs_state(irg) != outs_consistent)
284     compute_irg_outs(irg);
285
286   /* we must mark nodes, so increase the visited flag */
287   inc_irg_visited(irg);
288   irg_block_walk_graph(irg, pre_walk_calc_extbb, post_walk_calc_extbb, &env);
289
290   /*
291    * Ok, we have now the list of all extended blocks starting with env.head
292    * every extended block "knowns" the number of blocks in visited and
293    * the blocks are linked in link.
294    * Now we can create arrays that hold the blocks, some kind of "out" edges
295    * for the extended block
296    */
297   for (extbb = env.head; extbb; extbb = next) {
298     int i, len = (int)extbb->visited;
299     ir_node *block;
300
301     next = (ir_extblk *)extbb->blks;
302
303     extbb->blks = NEW_ARR_D(ir_node *, env.obst, len);
304
305     for (block = extbb->link, i = 0; i < len; ++i) {
306       ir_node *nblock = get_irn_link(block);
307
308       /* ensure that the leader is the first one */
309       extbb->blks[len - 1 - i] = block;
310       set_irn_link(block, NULL);
311       block = nblock;
312     }
313
314     extbb->link    = NULL;
315     extbb->visited = 0;
316
317 #ifdef NEED_SORT
318     /* we want the blocks in scheduled order, so sort them */
319     /* FIXME: is this really needed? Or generates the algorithm automagically ordered ones? */
320     if (len > 1) {
321       /* temporary remove the leader from the extended block to break loops */
322       set_Block_extbb(extbb->blks[0], NULL);
323       qsort(&extbb->blks[1], len-1, sizeof(extbb->blks[0]), block_order);
324       set_Block_extbb(extbb->blks[0], extbb);
325     }
326 #endif
327   }
328
329   irg->extblk_state = extblk_valid;
330 }
331
332 /* free all extended block info. */
333 void free_extbb(ir_graph *irg) {
334   if (irg->extbb_obst) {
335     obstack_free(irg->extbb_obst, NULL);
336     xfree(irg->extbb_obst);
337     irg->extbb_obst = NULL;
338   }
339   irg->extblk_state = extblk_none;
340 }
341
342 /* Return the extended block of a node. */
343 ir_extblk *get_nodes_extbb(ir_node *node) {
344   ir_node *block = is_Block(node) ? node : get_irn_n(node, -1);
345   return get_Block_extbb(block);
346 }
347
348 /* Gets the visited counter of an extended block. */
349 unsigned long (get_extbb_visited)(const ir_extblk *blk) {
350   return _get_extbb_visited(blk);
351 }
352
353 /* Sets the visited counter of an extended block. */
354 void (set_extbb_visited)(ir_extblk *blk, unsigned long visited) {
355   _set_extbb_visited(blk, visited);
356 }
357
358 /* Mark an extended block as visited in a graph. */
359 void (mark_extbb_visited)(ir_extblk *blk) {
360   _mark_extbb_visited(blk);
361 }
362
363 /* Returns non-zero if an extended was visited. */
364 int (extbb_visited)(const ir_extblk *blk) {
365   return _extbb_visited(blk);
366 }
367
368 /* Returns non-zero if an extended block was NOT visited. */
369 int (extbb_not_visited)(const ir_extblk *blk) {
370   return _extbb_not_visited(blk);
371 }
372
373 /* Returns the link field of an extended block. */
374 void *(get_extbb_link)(const ir_extblk *blk) {
375   return _get_extbb_link(blk);
376 }
377
378 /* Sets the link field of an extended block. */
379 void (set_extbb_link)(ir_extblk *blk, void *link) {
380   _set_extbb_link(blk, link);
381 }
382
383 /* Return the number of basic blocks of an extended block */
384 int (get_extbb_n_blocks)(const ir_extblk *blk) {
385   return _get_extbb_n_blocks(blk);
386 }
387
388 /* Return the i'th basic block of an extended block */
389 ir_node *(get_extbb_block)(ir_extblk *blk, int pos) {
390   return _get_extbb_block(blk, pos);
391 }
392
393 /* Return the leader basis block of an extended block. */
394 ir_node *(get_extbb_leader)(ir_extblk *blk) {
395   return _get_extbb_leader(blk);
396 }
397
398 /* Return the node number of an extended block. */
399 long get_extbb_node_nr(ir_extblk *blk) {
400   return get_irn_node_nr(get_extbb_leader(blk));
401 }
402
403 static void irg_extblock_walk_2(ir_extblk *blk, extbb_walk_func *pre, extbb_walk_func *post, void *env)
404 {
405   int i;
406   ir_node *node;
407
408   if (extbb_not_visited(blk)) {
409     mark_extbb_visited(blk);
410
411     if (pre) pre(blk, env);
412
413     node = get_extbb_leader(blk);
414     for (i = get_Block_n_cfgpreds(node) - 1; i >= 0; --i) {
415       /* find the corresponding predecessor block. */
416       ir_node *pred = get_Block_cfgpred_block(node, i);
417       if (is_Block(pred)) {
418         /* recursion */
419         irg_extblock_walk_2(get_Block_extbb(pred), pre, post, env);
420       }
421       else {
422         assert(is_Bad(pred));
423       }
424     }
425
426     if (post) post(blk, env);
427   }
428 }
429
430 /* walks only over extended Block nodes in the graph.  Has it's own visited
431    flag, so that it can be interleaved with the other walker.         */
432 void irg_extblock_walk(ir_extblk *blk, extbb_walk_func *pre, extbb_walk_func *post, void *env)
433 {
434   ir_node *pred, *start_bl = get_irg_start_block(current_ir_graph);
435   ir_extblk *start_blk = get_Block_extbb(start_bl);
436   int i;
437
438   assert(blk);
439   assert(!get_interprocedural_view());   /* interprocedural_view not implemented */
440   inc_irg_block_visited(current_ir_graph);
441
442   /* assure the start block is the first one */
443   mark_extbb_visited(start_blk);
444   if (post)
445     post(start_blk, env);
446   irg_extblock_walk_2(blk, pre, post, env);
447
448   /* keepalive: the endless loops ... */
449   if (blk == get_Block_extbb(get_irg_end_block(current_ir_graph))) {
450     ir_node *node = get_irg_end(current_ir_graph);
451     int arity = get_irn_arity(node);
452     for (i = 0; i < arity; i++) {
453       pred = get_irn_n(node, i);
454       if (is_Block(pred))
455         irg_extblock_walk_2(get_Block_extbb(pred), pre, post, env);
456       else if (is_Phi(pred)) {
457         /* Sometimes the blocks died, but are still reachable through Phis.
458          * Make sure the algorithms that try to remove these reach them. */
459         ir_node *block = get_nodes_block(pred);
460
461         if (! is_Bad(block))
462           irg_extblock_walk_2(get_Block_extbb(block), pre, post, env);
463       }
464     }
465   }
466
467   if (pre)
468     pre(start_blk, env);
469 }
470
471 /* Walks only over reachable Extended Basic Block nodes in the graph. */
472 void irg_extblock_walk_graph(ir_graph *irg, extbb_walk_func *pre, extbb_walk_func *post, void *env)
473 {
474   ir_node *endbl = get_irg_end_block(irg);
475   ir_extblk *blk = get_Block_extbb(endbl);
476   ir_graph *rem  = current_ir_graph;
477   current_ir_graph = irg;
478   irg_extblock_walk(blk, pre, post, env);
479   current_ir_graph = rem;
480 }