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"
27 struct obstack *obst; /**< the obstack where allocations took place */
28 ir_extblk *head; /**< head of the list of all extended blocks */
32 * allocate a new extended block header.
34 static void allocate_extblk(ir_node *block, env_t *env)
36 ir_extblk *extblk = obstack_alloc(env->obst, sizeof(*extblk));
38 extblk->kind = k_ir_extblk;
40 extblk->blks = (ir_node **)env->head;
44 set_Block_extbb(block, extblk);
45 set_irn_link(block, NULL);
49 * add a block to an extended block
51 static void addto_extblk(ir_extblk *extblk, ir_node *block)
53 /* link all blocks belonging to this extended block */
54 set_irn_link(block, extblk->link);
59 set_Block_extbb(block, extblk);
64 * Pre block-walker. Calculates the extended block info.
65 * Currently, we use the (free) block input of all basis blocks
66 * to point to the leader block.
68 static void pre_walk_calc_extbb(ir_node *block, void *ctx)
70 int n = get_Block_n_cfgpreds(block);
73 if (n > 1 || block == get_irg_start_block(current_ir_graph)) {
75 * block is a JOIN-node ie he control flow from
76 * many other blocks joins here. block is a leader.
78 allocate_extblk(block, env);
81 ir_node *add_to = get_Block_cfgpred(block, 0);
84 add_to = get_nodes_block(add_to);
86 /* blocks with only one BAD predecessors are leaders too */
88 allocate_extblk(block, env);
92 * Only on control flow predecessor. This block belongs
93 * to the same extended basic block as its predecessor.
95 set_Block_extbb(block, NULL);
101 * Post block-walker. Calculates the extended block info.
102 * During construction, we use the (free) block input of all basic blocks
103 * to point to there previous block.
105 static void post_walk_calc_extbb(ir_node *block, void *env)
107 ir_extblk *extbb = get_Block_extbb(block);
110 ir_node *prev = block;
112 /* search the leader */
114 prev = get_nodes_block(get_Block_cfgpred(prev, 0));
115 extbb = get_Block_extbb(prev);
118 addto_extblk(extbb, block);
123 * Compute the extended basic blocks for a graph
125 void compute_extbb(ir_graph *irg) {
127 ir_extblk *extbb, *next;
130 obstack_free(irg->extbb_obst, NULL);
132 irg->extbb_obst = xmalloc(sizeof(*irg->extbb_obst));
134 obstack_init(irg->extbb_obst);
136 env.obst = irg->extbb_obst;
139 irg_block_walk_graph(irg, pre_walk_calc_extbb, post_walk_calc_extbb, &env);
142 * Ok, we have now the list of all extended blocks starting with env.head
143 * every extended block "knowns" the number of blocks in visited and
144 * the blocks are linked in link.
145 * Now we can create arrays that hold the blocks, some kind of "out" edges
146 * for the extended block
148 for (extbb = env.head; extbb; extbb = next) {
149 int i, len = (int)extbb->visited;
152 next = (ir_extblk *)extbb->blks;
154 extbb->blks = NEW_ARR_D(ir_node *, env.obst, len);
156 for (block = extbb->link, i = 0; i < len; ++i) {
157 ir_node *nblock = get_irn_link(block);
159 extbb->blks[i] = block;
160 set_irn_link(block, NULL);
168 irg->extblk_state = ir_extblk_info_valid;
171 /* free all extended block info. */
172 void free_extbb(ir_graph *irg) {
173 if (irg->extbb_obst) {
174 obstack_free(irg->extbb_obst, NULL);
175 xfree(irg->extbb_obst);
176 irg->extbb_obst = NULL;
178 irg->extblk_state = ir_extblk_info_none;
181 /* Return the extended block of a node. */
182 ir_extblk *get_nodes_extbb(ir_node *node) {
183 ir_node *block = is_Block(node) ? node : get_nodes_block(node);
184 return get_Block_extbb(block);
187 /* Gets the visited counter of an extended block. */
188 unsigned long (get_extbb_visited)(const ir_extblk *blk) {
189 return _get_extbb_visited(blk);
192 /* Sets the visited counter of an extended block. */
193 void (set_extbb_visited)(ir_extblk *blk, unsigned long visited) {
194 _set_extbb_visited(blk, visited);
197 /* Mark an extended block as visited in a graph. */
198 void (mark_extbb_visited)(ir_extblk *blk) {
199 _mark_extbb_visited(blk);
202 /* Returns non-zero if an extended was visited. */
203 int (extbb_visited)(const ir_extblk *blk) {
204 return _extbb_visited(blk);
207 /* Returns non-zero if an extended block was NOT visited. */
208 int (extbb_not_visited)(const ir_extblk *blk) {
209 return _extbb_not_visited(blk);
212 /* Returns the link field of an extended block. */
213 void *(get_extbb_link)(const ir_extblk *blk) {
214 return _get_extbb_link(blk);
217 /* Sets the link field of an extended block. */
218 void (set_extbb_link)(ir_extblk *blk, void *link) {
219 _set_extbb_link(blk, link);
222 /* Return the number of basis blocks of an extended block */
223 int (get_extbb_n_blocks)(const ir_extblk *blk) {
224 return _get_extbb_n_blocks(blk);
227 /* Return the i'th basis block of an extended block */
228 ir_node *(get_extbb_block)(ir_extblk *blk, int pos) {
229 return _get_extbb_block(blk, pos);