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"
28 struct obstack *obst; /**< the obstack where allocations took place */
29 ir_extblk *head; /**< head of the list of all extended blocks */
33 * allocate a new extended block header.
35 static void allocate_extblk(ir_node *block, env_t *env)
37 ir_extblk *extblk = obstack_alloc(env->obst, sizeof(*extblk));
39 extblk->kind = k_ir_extblk;
41 extblk->blks = (ir_node **)env->head;
45 set_Block_extbb(block, extblk);
46 set_irn_link(block, NULL);
50 * add a block to an extended block
52 static void addto_extblk(ir_extblk *extblk, ir_node *block)
54 /* link all blocks belonging to this extended block */
55 set_irn_link(block, extblk->link);
60 set_Block_extbb(block, extblk);
65 * Pre block-walker. Calculates the extended block info.
67 static void pre_walk_calc_extbb(ir_node *block, void *ctx)
69 int n = get_Block_n_cfgpreds(block);
72 if (n <= 0 || n > 1 ||
73 block == get_irg_start_block(current_ir_graph) ||
74 block == get_irg_end_block(current_ir_graph)) {
76 * block is a JOIN-node ie the control flow from
77 * many other blocks joins here. block is a leader.
78 * Note that we handle unreachable blocks (n <= 0) here too.
80 allocate_extblk(block, env);
82 else { /* we have only one control flow predecessor */
83 ir_node *add_to = get_Block_cfgpred_block(block, 0);
85 /* blocks with only one BAD predecessors are leaders too */
87 allocate_extblk(block, env);
91 * Only one control flow predecessor. This block belongs
92 * to the same extended basic block as its predecessor.
94 set_Block_extbb(block, NULL);
102 * Post block-walker. Calculates the extended block info.
103 * During construction, we use the (free) block input of all basic blocks
104 * to point to there previous block.
106 static void post_walk_calc_extbb(ir_node *block, void *ctx)
108 ir_extblk *extbb = get_Block_extbb(block);
110 ir_extblk *sentinel = (ir_extblk *)&_sentinel;
113 ir_node *curr, *prev;
116 * Search the leader. It can happen, that we fall into an endless
117 * loop, because we enter an unreachable loop that is not yet detected.
118 * We break the loop using a sentinel.
120 for (curr = block; !extbb; curr = prev) {
121 prev = get_Block_cfgpred_block(curr, 0);
122 extbb = get_Block_extbb(prev);
123 set_Block_extbb(curr, sentinel);
126 if (extbb == sentinel) {
127 /* We detect a dead loop. We fix this by allocating an
128 * special Extended block
130 ir_printf("Dead loop detected starting with %+F::%+F\n", get_irg_entity(current_ir_graph), block);
132 allocate_extblk(block, env);
133 extbb = get_Block_extbb(block);
134 set_Block_extbb(block, sentinel);
137 /* replace all sentinels by the extbb info */
140 if (get_Block_extbb(prev) != sentinel)
142 set_Block_extbb(prev, extbb);
144 set_irn_link(prev, extbb->link);
146 prev = get_Block_cfgpred_block(prev, 0);
152 * Compute the extended basic blocks for a graph
154 void compute_extbb(ir_graph *irg) {
156 ir_extblk *extbb, *next;
159 obstack_free(irg->extbb_obst, NULL);
161 irg->extbb_obst = xmalloc(sizeof(*irg->extbb_obst));
163 obstack_init(irg->extbb_obst);
165 env.obst = irg->extbb_obst;
168 irg_block_walk_graph(irg, pre_walk_calc_extbb, post_walk_calc_extbb, &env);
171 * Ok, we have now the list of all extended blocks starting with env.head
172 * every extended block "knowns" the number of blocks in visited and
173 * the blocks are linked in link.
174 * Now we can create arrays that hold the blocks, some kind of "out" edges
175 * for the extended block
177 for (extbb = env.head; extbb; extbb = next) {
178 int i, len = (int)extbb->visited;
181 next = (ir_extblk *)extbb->blks;
183 extbb->blks = NEW_ARR_D(ir_node *, env.obst, len);
185 for (block = extbb->link, i = 0; i < len; ++i) {
186 ir_node *nblock = get_irn_link(block);
188 /* ensure that the leader is the first one */
189 extbb->blks[len - 1 - i] = block;
190 set_irn_link(block, NULL);
198 irg->extblk_state = ir_extblk_info_valid;
201 /* free all extended block info. */
202 void free_extbb(ir_graph *irg) {
203 if (irg->extbb_obst) {
204 obstack_free(irg->extbb_obst, NULL);
205 xfree(irg->extbb_obst);
206 irg->extbb_obst = NULL;
208 irg->extblk_state = ir_extblk_info_none;
211 /* Return the extended block of a node. */
212 ir_extblk *get_nodes_extbb(ir_node *node) {
213 ir_node *block = is_Block(node) ? node : get_irn_n(node, -1);
214 return get_Block_extbb(block);
217 /* Gets the visited counter of an extended block. */
218 unsigned long (get_extbb_visited)(const ir_extblk *blk) {
219 return _get_extbb_visited(blk);
222 /* Sets the visited counter of an extended block. */
223 void (set_extbb_visited)(ir_extblk *blk, unsigned long visited) {
224 _set_extbb_visited(blk, visited);
227 /* Mark an extended block as visited in a graph. */
228 void (mark_extbb_visited)(ir_extblk *blk) {
229 _mark_extbb_visited(blk);
232 /* Returns non-zero if an extended was visited. */
233 int (extbb_visited)(const ir_extblk *blk) {
234 return _extbb_visited(blk);
237 /* Returns non-zero if an extended block was NOT visited. */
238 int (extbb_not_visited)(const ir_extblk *blk) {
239 return _extbb_not_visited(blk);
242 /* Returns the link field of an extended block. */
243 void *(get_extbb_link)(const ir_extblk *blk) {
244 return _get_extbb_link(blk);
247 /* Sets the link field of an extended block. */
248 void (set_extbb_link)(ir_extblk *blk, void *link) {
249 _set_extbb_link(blk, link);
252 /* Return the number of basic blocks of an extended block */
253 int (get_extbb_n_blocks)(const ir_extblk *blk) {
254 return _get_extbb_n_blocks(blk);
257 /* Return the i'th basic block of an extended block */
258 ir_node *(get_extbb_block)(ir_extblk *blk, int pos) {
259 return _get_extbb_block(blk, pos);
262 /* Return the leader basis block of an extended block. */
263 ir_node *(get_extbb_leader)(ir_extblk *blk) {
264 return _get_extbb_leader(blk);
267 /* Return the node number of an extended block. */
268 long get_extbb_node_nr(ir_extblk *blk) {
269 return get_irn_node_nr(get_extbb_leader(blk));
272 static void irg_extblock_walk_2(ir_extblk *blk, extbb_walk_func *pre, extbb_walk_func *post, void *env)
277 if (extbb_not_visited(blk)) {
278 mark_extbb_visited(blk);
280 if (pre) pre(blk, env);
282 node = get_extbb_leader(blk);
283 for (i = get_Block_n_cfgpreds(node) - 1; i >= 0; --i) {
284 /* find the corresponding predecessor block. */
285 ir_node *pred = get_Block_cfgpred_block(node, i);
286 if (is_Block(pred)) {
288 irg_extblock_walk_2(get_Block_extbb(pred), pre, post, env);
291 assert(is_Bad(pred));
295 if (post) post(blk, env);
299 /* walks only over extended Block nodes in the graph. Has it's own visited
300 flag, so that it can be interleaved with the other walker. */
301 void irg_extblock_walk(ir_extblk *blk, extbb_walk_func *pre, extbb_walk_func *post, void *env)
303 ir_node *block, *pred;
307 assert(!get_interprocedural_view()); /* interprocedural_view not implemented */
308 inc_irg_block_visited(current_ir_graph);
309 irg_extblock_walk_2(block, pre, post, env);
311 /* keepalive: the endless loops ... */
312 if (get_extbb_leader(blk) == get_irg_end_block(current_ir_graph)) {
313 ir_node *node = get_irg_end(current_ir_graph);
314 int arity = get_irn_arity(node);
315 for (i = 0; i < arity; i++) {
316 pred = get_irn_n(node, i);
318 irg_extblock_walk_2(get_Block_extbb(pred), pre, post, env);
319 else if (is_Phi(pred)) {
320 /* Sometimes the blocks died, but are still reachable through Phis.
321 * Make sure the algorithms that try to remove these reach them. */
322 ir_node *block = get_nodes_block(pred);
325 irg_extblock_walk_2(get_Block_extbb(block), pre, post, env);
331 /* Walks only over reachable Extended Basic Block nodes in the graph. */
332 void irg_extblock_walk_graph(ir_graph *irg, extbb_walk_func *pre, extbb_walk_func *post, void *env)
334 ir_node *end = get_irg_end(irg);
335 ir_extblk *blk = get_Block_extbb(end);
336 ir_graph *rem = current_ir_graph;
337 current_ir_graph = irg;
338 irg_extblock_walk(blk, pre, post, env);
339 current_ir_graph = rem;