3 * File name: ir/ana/irextbb.c
4 * Purpose: Extended basis block support.
9 * Copyright: (c) 2002-2005 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
16 * Computes extended basic blocks.
18 * @author Michael Beck
20 #include "irextbb_t.h"
23 #include "irgraph_t.h"
24 #include "iredges_t.h"
30 struct obstack *obst; /**< the obstack where allocations took place */
31 ir_extblk *head; /**< head of the list of all extended blocks */
34 int (is_ir_extbb)(const void *thing) {
35 return _is_ir_extbb(thing);
39 * allocate a new extended block header.
41 static void allocate_extblk(ir_node *block, env_t *env)
43 ir_extblk *extblk = obstack_alloc(env->obst, sizeof(*extblk));
45 extblk->kind = k_ir_extblk;
47 extblk->blks = (ir_node **)env->head;
51 set_Block_extbb(block, extblk);
52 set_irn_link(block, NULL);
56 * add a block to an extended block
58 static void addto_extblk(ir_extblk *extblk, ir_node *block)
60 /* link all blocks belonging to this extended block */
61 set_irn_link(block, extblk->link);
66 set_Block_extbb(block, extblk);
70 * Returns the number of block successors.
71 * we are interested only in 1, 2 and >2.
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;
78 edge = get_block_succ_first(block);
81 edge = get_block_succ_next(block, edge);
84 edge = get_block_succ_next(block, edge);
88 return get_Block_n_cfg_outs(block);
92 * Pre block-walker. Calculates the extended block info.
94 static void pre_walk_calc_extbb(ir_node *block, void *ctx)
96 int n = get_Block_n_cfgpreds(block);
99 if (n <= 0 || n > 1 ||
100 block == get_irg_start_block(current_ir_graph)) {
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.
106 allocate_extblk(block, env);
108 else { /* we have only one control flow predecessor */
109 ir_node *add_to = get_Block_cfgpred_block(block, 0);
111 /* blocks with only one BAD predecessors are leaders too */
112 if (is_Bad(add_to)) {
113 allocate_extblk(block, env);
117 * Only one control flow predecessor. This block belongs
118 * to the same extended basic block as its predecessor.
120 ir_node *cf_op = skip_Proj(get_Block_cfgpred(block, 0));
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.
129 allocate_extblk(block, env);
132 /* either the previous block has only one successor or
133 * this is the first successor after an if, include it.
135 set_Block_extbb(block, NULL);
137 mark_irn_visited(cf_op);
140 /* already marked, so begin a new extended block here */
141 allocate_extblk(block, env);
147 /** A special extended block used as sentinel */
148 static ir_extblk _sentinel = { k_ir_extblk, 0xFEA1DEAD };
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.
155 static void post_walk_calc_extbb(ir_node *block, void *ctx)
157 ir_extblk *extbb = get_Block_extbb(block);
159 ir_extblk *sentinel = &_sentinel;
162 ir_node *curr, *prev;
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.
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);
175 if (extbb == sentinel) {
176 /* We detect a dead loop. We fix this by allocating an
177 * special Extended block
179 ir_printf("Dead loop detected starting with %+F::%+F\n", get_irg_entity(current_ir_graph), block);
181 allocate_extblk(block, env);
182 extbb = get_Block_extbb(block);
183 set_Block_extbb(block, sentinel);
186 /* replace all sentinels by the extbb info */
189 if (get_Block_extbb(prev) != sentinel)
191 set_Block_extbb(prev, extbb);
193 set_irn_link(prev, extbb->link);
195 prev = get_Block_cfgpred_block(prev, 0);
201 * Compute the extended basic blocks for a graph
203 void compute_extbb(ir_graph *irg) {
205 ir_extblk *extbb, *next;
208 obstack_free(irg->extbb_obst, NULL);
210 irg->extbb_obst = xmalloc(sizeof(*irg->extbb_obst));
212 obstack_init(irg->extbb_obst);
214 env.obst = irg->extbb_obst;
217 #ifdef FIRM_EDGES_INPLACE
218 if (edges_activated(irg)) {
223 if (get_irg_outs_state(irg) != outs_consistent)
224 compute_irg_outs(irg);
226 /* we must mark nodes, so increase the visited flag */
227 inc_irg_visited(irg);
228 irg_block_walk_graph(irg, pre_walk_calc_extbb, post_walk_calc_extbb, &env);
231 * Ok, we have now the list of all extended blocks starting with env.head
232 * every extended block "knowns" the number of blocks in visited and
233 * the blocks are linked in link.
234 * Now we can create arrays that hold the blocks, some kind of "out" edges
235 * for the extended block
237 for (extbb = env.head; extbb; extbb = next) {
238 int i, len = (int)extbb->visited;
241 next = (ir_extblk *)extbb->blks;
243 extbb->blks = NEW_ARR_D(ir_node *, env.obst, len);
245 for (block = extbb->link, i = 0; i < len; ++i) {
246 ir_node *nblock = get_irn_link(block);
248 /* ensure that the leader is the first one */
249 extbb->blks[len - 1 - i] = block;
250 set_irn_link(block, NULL);
258 irg->extblk_state = ir_extblk_info_valid;
261 /* free all extended block info. */
262 void free_extbb(ir_graph *irg) {
263 if (irg->extbb_obst) {
264 obstack_free(irg->extbb_obst, NULL);
265 xfree(irg->extbb_obst);
266 irg->extbb_obst = NULL;
268 irg->extblk_state = ir_extblk_info_none;
271 /* Return the extended block of a node. */
272 ir_extblk *get_nodes_extbb(ir_node *node) {
273 ir_node *block = is_Block(node) ? node : get_irn_n(node, -1);
274 return get_Block_extbb(block);
277 /* Gets the visited counter of an extended block. */
278 unsigned long (get_extbb_visited)(const ir_extblk *blk) {
279 return _get_extbb_visited(blk);
282 /* Sets the visited counter of an extended block. */
283 void (set_extbb_visited)(ir_extblk *blk, unsigned long visited) {
284 _set_extbb_visited(blk, visited);
287 /* Mark an extended block as visited in a graph. */
288 void (mark_extbb_visited)(ir_extblk *blk) {
289 _mark_extbb_visited(blk);
292 /* Returns non-zero if an extended was visited. */
293 int (extbb_visited)(const ir_extblk *blk) {
294 return _extbb_visited(blk);
297 /* Returns non-zero if an extended block was NOT visited. */
298 int (extbb_not_visited)(const ir_extblk *blk) {
299 return _extbb_not_visited(blk);
302 /* Returns the link field of an extended block. */
303 void *(get_extbb_link)(const ir_extblk *blk) {
304 return _get_extbb_link(blk);
307 /* Sets the link field of an extended block. */
308 void (set_extbb_link)(ir_extblk *blk, void *link) {
309 _set_extbb_link(blk, link);
312 /* Return the number of basic blocks of an extended block */
313 int (get_extbb_n_blocks)(const ir_extblk *blk) {
314 return _get_extbb_n_blocks(blk);
317 /* Return the i'th basic block of an extended block */
318 ir_node *(get_extbb_block)(ir_extblk *blk, int pos) {
319 return _get_extbb_block(blk, pos);
322 /* Return the leader basis block of an extended block. */
323 ir_node *(get_extbb_leader)(ir_extblk *blk) {
324 return _get_extbb_leader(blk);
327 /* Return the node number of an extended block. */
328 long get_extbb_node_nr(ir_extblk *blk) {
329 return get_irn_node_nr(get_extbb_leader(blk));
332 static void irg_extblock_walk_2(ir_extblk *blk, extbb_walk_func *pre, extbb_walk_func *post, void *env)
337 if (extbb_not_visited(blk)) {
338 mark_extbb_visited(blk);
340 if (pre) pre(blk, env);
342 node = get_extbb_leader(blk);
343 for (i = get_Block_n_cfgpreds(node) - 1; i >= 0; --i) {
344 /* find the corresponding predecessor block. */
345 ir_node *pred = get_Block_cfgpred_block(node, i);
346 if (is_Block(pred)) {
348 irg_extblock_walk_2(get_Block_extbb(pred), pre, post, env);
351 assert(is_Bad(pred));
355 if (post) post(blk, env);
359 /* walks only over extended Block nodes in the graph. Has it's own visited
360 flag, so that it can be interleaved with the other walker. */
361 void irg_extblock_walk(ir_extblk *blk, extbb_walk_func *pre, extbb_walk_func *post, void *env)
367 assert(!get_interprocedural_view()); /* interprocedural_view not implemented */
368 inc_irg_block_visited(current_ir_graph);
369 irg_extblock_walk_2(blk, pre, post, env);
371 /* keepalive: the endless loops ... */
372 if (get_extbb_leader(blk) == get_irg_end_block(current_ir_graph)) {
373 ir_node *node = get_irg_end(current_ir_graph);
374 int arity = get_irn_arity(node);
375 for (i = 0; i < arity; i++) {
376 pred = get_irn_n(node, i);
378 irg_extblock_walk_2(get_Block_extbb(pred), pre, post, env);
379 else if (is_Phi(pred)) {
380 /* Sometimes the blocks died, but are still reachable through Phis.
381 * Make sure the algorithms that try to remove these reach them. */
382 ir_node *block = get_nodes_block(pred);
385 irg_extblock_walk_2(get_Block_extbb(block), pre, post, env);
391 /* Walks only over reachable Extended Basic Block nodes in the graph. */
392 void irg_extblock_walk_graph(ir_graph *irg, extbb_walk_func *pre, extbb_walk_func *post, void *env)
394 ir_node *end = get_irg_end(irg);
395 ir_extblk *blk = get_Block_extbb(end);
396 ir_graph *rem = current_ir_graph;
397 current_ir_graph = irg;
398 irg_extblock_walk(blk, pre, post, env);
399 current_ir_graph = rem;