2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Extended basis block support.
23 * @author Michael Beck
31 #include "irextbb_t.h"
34 #include "irgraph_t.h"
35 #include "iredges_t.h"
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 */
46 int (is_ir_extbb)(const void *thing) {
47 return _is_ir_extbb(thing);
51 * allocate a new extended block header.
53 static void allocate_extblk(ir_node *block, env_t *env)
55 ir_extblk *extblk = obstack_alloc(env->obst, sizeof(*extblk));
57 extblk->kind = k_ir_extblk;
59 extblk->blks = (ir_node **)env->head;
63 set_Block_extbb(block, extblk);
64 set_irn_link(block, NULL);
68 * Returns the number of block successors.
69 * we are interested only in 1, 2 and >2.
71 static int get_block_n_succs(ir_node *block) {
72 if (edges_activated(current_ir_graph)) {
73 const ir_edge_t *edge;
75 edge = get_block_succ_first(block);
78 edge = get_block_succ_next(block, edge);
81 edge = get_block_succ_next(block, edge);
84 return get_Block_n_cfg_outs(block);
88 * Pre block-walker. Calculates the extended block info.
90 static void pre_walk_calc_extbb(ir_node *block, void *ctx)
92 int n = get_Block_n_cfgpreds(block);
95 if (n <= 0 || n > 1 || block == env->start_block) {
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.
101 allocate_extblk(block, env);
103 else { /* we have only one control flow predecessor */
104 ir_node *add_to = get_Block_cfgpred_block(block, 0);
106 /* blocks with only one BAD predecessors are leaders too */
107 if (is_Bad(add_to)) {
108 allocate_extblk(block, env);
111 * Only one control flow predecessor. This block belongs
112 * to the same extended basic block as its predecessor.
114 ir_node *cf_op = skip_Proj(get_Block_cfgpred(block, 0));
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.
123 allocate_extblk(block, env);
125 /* either the previous block has only one successor or
126 * this is the first successor after an if, include it.
128 set_Block_extbb(block, NULL);
130 mark_irn_visited(cf_op);
132 /* already marked, so begin a new extended block here */
133 allocate_extblk(block, env);
139 /** A special extended block used as sentinel */
140 static ir_extblk _sentinel = { k_ir_extblk, 0xFEA1DEAD };
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.
147 static void post_walk_calc_extbb(ir_node *block, void *ctx)
149 ir_extblk *extbb = get_Block_extbb(block);
151 ir_extblk *sentinel = &_sentinel;
154 ir_node *curr, *prev, *list;
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.
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);
167 if (extbb == sentinel) {
168 /* We detect a dead loop. We fix this by allocating a
169 * special Extended block
171 ir_printf("Dead loop detected starting with %+F::%+F\n", get_irg_entity(current_ir_graph), block);
173 allocate_extblk(block, env);
174 extbb = get_Block_extbb(block);
175 set_Block_extbb(block, sentinel);
178 /* replace all sentinels by the extbb info */
182 if (get_Block_extbb(prev) != sentinel)
184 set_irn_link(prev, list);
186 prev = get_Block_cfgpred_block(prev, 0);
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);
193 set_Block_extbb(curr, extbb);
200 * Compute the extended basic blocks for a graph
202 void compute_extbb(ir_graph *irg) {
204 ir_extblk *extbb, *next;
207 obstack_free(irg->extbb_obst, NULL);
209 irg->extbb_obst = xmalloc(sizeof(*irg->extbb_obst));
211 obstack_init(irg->extbb_obst);
213 env.obst = irg->extbb_obst;
215 env.start_block = get_irg_start_block(irg);
217 if (! edges_activated(irg)) {
218 /* we don't have edges */
219 assure_irg_outs(irg);
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);
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
233 for (extbb = env.head; extbb; extbb = next) {
234 int i, len = (int)extbb->visited;
237 next = (ir_extblk *)extbb->blks;
239 extbb->blks = NEW_ARR_D(ir_node *, env.obst, len);
241 for (block = extbb->link, i = 0; i < len; ++i) {
242 ir_node *nblock = get_irn_link(block);
244 /* ensure that the leader is the first one */
245 extbb->blks[len - 1 - i] = block;
246 set_irn_link(block, NULL);
252 for (i = len - 1; i > 0; --i) {
253 ir_node *blk = extbb->blks[i];
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");
267 irg->extblk_state = extblk_valid;
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;
277 irg->extblk_state = extblk_none;
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);
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);
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);
296 /* Mark an extended block as visited in a graph. */
297 void (mark_extbb_visited)(ir_extblk *blk) {
298 _mark_extbb_visited(blk);
301 /* Returns non-zero if an extended was visited. */
302 int (extbb_visited)(const ir_extblk *blk) {
303 return _extbb_visited(blk);
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);
311 /* Returns the link field of an extended block. */
312 void *(get_extbb_link)(const ir_extblk *blk) {
313 return _get_extbb_link(blk);
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);
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);
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);
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);
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));
341 static void irg_extblock_walk_2(ir_extblk *blk, extbb_walk_func *pre, extbb_walk_func *post, void *env)
346 if (extbb_not_visited(blk)) {
347 mark_extbb_visited(blk);
349 if (pre) pre(blk, env);
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)) {
357 irg_extblock_walk_2(get_Block_extbb(pred), pre, post, env);
360 assert(is_Bad(pred));
364 if (post) post(blk, env);
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)
372 ir_node *pred, *start_bl = get_irg_start_block(current_ir_graph);
373 ir_extblk *start_blk = get_Block_extbb(start_bl);
377 assert(!get_interprocedural_view()); /* interprocedural_view not implemented */
378 inc_irg_block_visited(current_ir_graph);
380 /* assure the start block is the first one */
381 mark_extbb_visited(start_blk);
383 post(start_blk, env);
384 irg_extblock_walk_2(blk, pre, post, env);
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);
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);
400 irg_extblock_walk_2(get_Block_extbb(block), pre, post, env);
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)
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;