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 */
32 ir_node *start_block; /**< the start block of the current graph */
35 int (is_ir_extbb)(const void *thing) {
36 return _is_ir_extbb(thing);
40 * allocate a new extended block header.
42 static void allocate_extblk(ir_node *block, env_t *env)
44 ir_extblk *extblk = obstack_alloc(env->obst, sizeof(*extblk));
46 extblk->kind = k_ir_extblk;
48 extblk->blks = (ir_node **)env->head;
52 set_Block_extbb(block, extblk);
53 set_irn_link(block, NULL);
57 * add a block to an extended block
59 static void addto_extblk(ir_extblk *extblk, ir_node *block)
61 /* link all blocks belonging to this extended block */
62 set_irn_link(block, extblk->link);
67 set_Block_extbb(block, extblk);
71 * Returns the number of block successors.
72 * we are interested only in 1, 2 and >2.
74 static int get_block_n_succs(ir_node *block) {
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);
87 return get_Block_n_cfg_outs(block);
91 * Pre block-walker. Calculates the extended block info.
93 static void pre_walk_calc_extbb(ir_node *block, void *ctx)
95 int n = get_Block_n_cfgpreds(block);
98 if (n <= 0 || n > 1 || block == env->start_block) {
100 * block is a JOIN-node ie the control flow from
101 * many other blocks joins here. block is a leader.
102 * Note that we handle unreachable blocks (n <= 0) here too.
104 allocate_extblk(block, env);
106 else { /* we have only one control flow predecessor */
107 ir_node *add_to = get_Block_cfgpred_block(block, 0);
109 /* blocks with only one BAD predecessors are leaders too */
110 if (is_Bad(add_to)) {
111 allocate_extblk(block, env);
114 * Only one control flow predecessor. This block belongs
115 * to the same extended basic block as its predecessor.
117 ir_node *cf_op = skip_Proj(get_Block_cfgpred(block, 0));
119 if (irn_not_visited(cf_op)) {
120 ir_node *pred_bl = get_nodes_block(cf_op);
121 if (get_block_n_succs(pred_bl) > 2) {
122 /* More than two successors means we have a jump table.
123 * we cannot include a jump target into the current extended
124 * basic block, so create a new one here.
126 allocate_extblk(block, env);
128 /* either the previous block has only one successor or
129 * this is the first successor after an if, include it.
131 set_Block_extbb(block, NULL);
133 mark_irn_visited(cf_op);
135 /* already marked, so begin a new extended block here */
136 allocate_extblk(block, env);
142 /** A special extended block used as sentinel */
143 static ir_extblk _sentinel = { k_ir_extblk, 0xFEA1DEAD };
146 * Post block-walker. Calculates the extended block info.
147 * During construction, we use the (free) block input of all basic blocks
148 * to point to there previous block.
150 static void post_walk_calc_extbb(ir_node *block, void *ctx)
152 ir_extblk *extbb = get_Block_extbb(block);
154 ir_extblk *sentinel = &_sentinel;
157 ir_node *curr, *prev, *list;
160 * Search the leader. It can happen, that we fall into an endless
161 * loop, because we enter an unreachable loop that is not yet detected.
162 * We break the loop using a sentinel.
164 for (curr = block; !extbb; curr = prev) {
165 prev = get_Block_cfgpred_block(curr, 0);
166 extbb = get_Block_extbb(prev);
167 set_Block_extbb(curr, sentinel);
170 if (extbb == sentinel) {
171 /* We detect a dead loop. We fix this by allocating a
172 * special Extended block
174 ir_printf("Dead loop detected starting with %+F::%+F\n", get_irg_entity(current_ir_graph), block);
176 allocate_extblk(block, env);
177 extbb = get_Block_extbb(block);
178 set_Block_extbb(block, sentinel);
181 /* replace all sentinels by the extbb info */
185 if (get_Block_extbb(prev) != sentinel)
187 set_irn_link(prev, list);
189 prev = get_Block_cfgpred_block(prev, 0);
191 /* arg, the list is in wrong order, turn around and add to the extbb list */
192 for (curr = list; curr; curr = prev) {
193 prev = get_irn_link(curr);
194 set_irn_link(curr, extbb->link);
196 set_Block_extbb(curr, extbb);
203 * Compute the extended basic blocks for a graph
205 void compute_extbb(ir_graph *irg) {
207 ir_extblk *extbb, *next;
210 obstack_free(irg->extbb_obst, NULL);
212 irg->extbb_obst = xmalloc(sizeof(*irg->extbb_obst));
214 obstack_init(irg->extbb_obst);
216 env.obst = irg->extbb_obst;
218 env.start_block = get_irg_start_block(irg);
220 if (! edges_activated(irg)) {
221 /* we don't have edges */
222 assure_irg_outs(irg);
225 /* we must mark nodes, so increase the visited flag */
226 inc_irg_visited(irg);
227 irg_block_walk_graph(irg, pre_walk_calc_extbb, post_walk_calc_extbb, &env);
230 * Ok, we have now the list of all extended blocks starting with env.head
231 * every extended block "knowns" the number of blocks in visited and
232 * the blocks are linked in link.
233 * Now we can create arrays that hold the blocks, some kind of "out" edges
234 * for the extended block
236 for (extbb = env.head; extbb; extbb = next) {
237 int i, len = (int)extbb->visited;
240 next = (ir_extblk *)extbb->blks;
242 extbb->blks = NEW_ARR_D(ir_node *, env.obst, len);
244 for (block = extbb->link, i = 0; i < len; ++i) {
245 ir_node *nblock = get_irn_link(block);
247 /* ensure that the leader is the first one */
248 extbb->blks[len - 1 - i] = block;
249 set_irn_link(block, NULL);
255 for (i = len - 1; i > 0; --i) {
256 ir_node *blk = extbb->blks[i];
258 if (get_Block_n_cfgpreds(blk) != 1) {
259 assert(!"Block for more than one predecessors is no leader");
260 } else if (get_Block_cfgpred_block(blk, 0) != extbb->blks[i - 1]) {
261 assert(!"extbb block order wrong");
270 irg->extblk_state = extblk_valid;
273 /* free all extended block info. */
274 void free_extbb(ir_graph *irg) {
275 if (irg->extbb_obst) {
276 obstack_free(irg->extbb_obst, NULL);
277 xfree(irg->extbb_obst);
278 irg->extbb_obst = NULL;
280 irg->extblk_state = extblk_none;
283 /* Return the extended block of a node. */
284 ir_extblk *get_nodes_extbb(ir_node *node) {
285 ir_node *block = is_Block(node) ? node : get_irn_n(node, -1);
286 return get_Block_extbb(block);
289 /* Gets the visited counter of an extended block. */
290 unsigned long (get_extbb_visited)(const ir_extblk *blk) {
291 return _get_extbb_visited(blk);
294 /* Sets the visited counter of an extended block. */
295 void (set_extbb_visited)(ir_extblk *blk, unsigned long visited) {
296 _set_extbb_visited(blk, visited);
299 /* Mark an extended block as visited in a graph. */
300 void (mark_extbb_visited)(ir_extblk *blk) {
301 _mark_extbb_visited(blk);
304 /* Returns non-zero if an extended was visited. */
305 int (extbb_visited)(const ir_extblk *blk) {
306 return _extbb_visited(blk);
309 /* Returns non-zero if an extended block was NOT visited. */
310 int (extbb_not_visited)(const ir_extblk *blk) {
311 return _extbb_not_visited(blk);
314 /* Returns the link field of an extended block. */
315 void *(get_extbb_link)(const ir_extblk *blk) {
316 return _get_extbb_link(blk);
319 /* Sets the link field of an extended block. */
320 void (set_extbb_link)(ir_extblk *blk, void *link) {
321 _set_extbb_link(blk, link);
324 /* Return the number of basic blocks of an extended block */
325 int (get_extbb_n_blocks)(const ir_extblk *blk) {
326 return _get_extbb_n_blocks(blk);
329 /* Return the i'th basic block of an extended block */
330 ir_node *(get_extbb_block)(ir_extblk *blk, int pos) {
331 return _get_extbb_block(blk, pos);
334 /* Return the leader basis block of an extended block. */
335 ir_node *(get_extbb_leader)(ir_extblk *blk) {
336 return _get_extbb_leader(blk);
339 /* Return the node number of an extended block. */
340 long get_extbb_node_nr(ir_extblk *blk) {
341 return get_irn_node_nr(get_extbb_leader(blk));
344 static void irg_extblock_walk_2(ir_extblk *blk, extbb_walk_func *pre, extbb_walk_func *post, void *env)
349 if (extbb_not_visited(blk)) {
350 mark_extbb_visited(blk);
352 if (pre) pre(blk, env);
354 node = get_extbb_leader(blk);
355 for (i = get_Block_n_cfgpreds(node) - 1; i >= 0; --i) {
356 /* find the corresponding predecessor block. */
357 ir_node *pred = get_Block_cfgpred_block(node, i);
358 if (is_Block(pred)) {
360 irg_extblock_walk_2(get_Block_extbb(pred), pre, post, env);
363 assert(is_Bad(pred));
367 if (post) post(blk, env);
371 /* walks only over extended Block nodes in the graph. Has it's own visited
372 flag, so that it can be interleaved with the other walker. */
373 void irg_extblock_walk(ir_extblk *blk, extbb_walk_func *pre, extbb_walk_func *post, void *env)
375 ir_node *pred, *start_bl = get_irg_start_block(current_ir_graph);
376 ir_extblk *start_blk = get_Block_extbb(start_bl);
380 assert(!get_interprocedural_view()); /* interprocedural_view not implemented */
381 inc_irg_block_visited(current_ir_graph);
383 /* assure the start block is the first one */
384 mark_extbb_visited(start_blk);
386 post(start_blk, env);
387 irg_extblock_walk_2(blk, pre, post, env);
389 /* keepalive: the endless loops ... */
390 if (blk == get_Block_extbb(get_irg_end_block(current_ir_graph))) {
391 ir_node *node = get_irg_end(current_ir_graph);
392 int arity = get_irn_arity(node);
393 for (i = 0; i < arity; i++) {
394 pred = get_irn_n(node, i);
396 irg_extblock_walk_2(get_Block_extbb(pred), pre, post, env);
397 else if (is_Phi(pred)) {
398 /* Sometimes the blocks died, but are still reachable through Phis.
399 * Make sure the algorithms that try to remove these reach them. */
400 ir_node *block = get_nodes_block(pred);
403 irg_extblock_walk_2(get_Block_extbb(block), pre, post, env);
412 /* Walks only over reachable Extended Basic Block nodes in the graph. */
413 void irg_extblock_walk_graph(ir_graph *irg, extbb_walk_func *pre, extbb_walk_func *post, void *env)
415 ir_node *endbl = get_irg_end_block(irg);
416 ir_extblk *blk = get_Block_extbb(endbl);
417 ir_graph *rem = current_ir_graph;
418 current_ir_graph = irg;
419 irg_extblock_walk(blk, pre, post, env);
420 current_ir_graph = rem;