2 * Copyright (C) 1995-2008 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 Alternative extended basic block computation
23 * @author Matthias Braun
27 * Alternative algorithm for computing extended basic blocks (using out edges
28 * and execution frequencies)
34 #include "irextbb_t.h"
37 #include "irgraph_t.h"
38 #include "iredges_t.h"
45 struct obstack *obst; /**< the obstack where allocations took place */
46 ir_extblk *head; /**< head of the list of all extended blocks */
47 ir_exec_freq *execfreqs;
51 * allocate a new extended block header.
53 static ir_extblk *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);
70 * add a block to an extended block
72 static void addto_extblk(ir_extblk *extblk, ir_node *block)
74 /* link all blocks belonging to this extended block */
75 set_irn_link(block, extblk->link);
80 set_Block_extbb(block, extblk);
84 * Returns the number of block successors.
85 * we are interested only in 1, 2 and >2.
87 static int get_block_n_succs(ir_node *block) {
88 if (edges_activated(current_ir_graph)) {
89 const ir_edge_t *edge;
91 edge = get_block_succ_first(block);
95 edge = get_block_succ_next(block, edge);
99 edge = get_block_succ_next(block, edge);
103 return get_Block_n_cfg_outs(block);
106 static void pick_successor(ir_node *block, ir_extblk *extblk, env_t *env);
108 static void create_extblk(ir_node *block, env_t *env)
112 if (irn_visited_else_mark(block))
115 extblk = allocate_extblk(block, env);
117 pick_successor(block, extblk, env);
120 static void pick_successor(ir_node *block, ir_extblk *extblk, env_t *env)
122 const ir_edge_t *edge;
123 ir_node *best_succ = NULL;
124 double best_execfreq = -1;
127 More than two successors means we have a jump table.
128 we cannot include a jump target into the current extended
129 basic block, so create a new one here.
131 if (get_block_n_succs(block) > 2) {
132 const ir_edge_t *edge;
134 foreach_block_succ(block, edge) {
135 ir_node *succ = get_edge_src_irn(edge);
136 create_extblk(succ, env);
142 foreach_block_succ(block, edge) {
143 ir_node *succ = get_edge_src_irn(edge);
146 if(irn_visited(succ))
149 if(get_Block_n_cfgpreds(succ) > 1) {
150 create_extblk(succ, env);
154 execfreq = get_block_execfreq(env->execfreqs, succ);
157 Remember best successor and make non best successor with only 1
158 pred block to new extbb leaders.
160 if (execfreq > best_execfreq) {
161 if (best_succ != NULL) {
162 create_extblk(best_succ, env);
165 best_execfreq = execfreq;
169 create_extblk(succ, env);
173 /* add best successor and recursively try to pick more */
174 if(best_succ != NULL) {
175 addto_extblk(extblk, best_succ);
176 mark_irn_visited(best_succ);
177 pick_successor(best_succ, extblk, env);
182 * Compute the extended basic blocks for a graph
184 void compute_extbb_execfreqs(ir_graph *irg, ir_exec_freq *execfreqs) {
186 ir_extblk *extbb, *next;
189 if (irg->extbb_obst) {
190 obstack_free(irg->extbb_obst, NULL);
193 irg->extbb_obst = XMALLOC(struct obstack);
195 obstack_init(irg->extbb_obst);
197 env.obst = irg->extbb_obst;
199 env.execfreqs = execfreqs;
201 assure_irg_outs(irg);
203 /* we must mark nodes, so increase the visited flag */
204 inc_irg_visited(irg);
205 create_extblk(get_irg_start_block(irg), &env);
207 /* the end block needs a extbb assigned (even for endless loops) */
208 endblock = get_irg_end_block(irg);
209 create_extblk(endblock, &env);
212 Ok, we have now the list of all extended blocks starting with env.head
213 every extended block "knowns" the number of blocks in visited and
214 the blocks are linked in link.
215 Now we can create arrays that hold the blocks, some kind of "out" edges
216 for the extended block
218 for (extbb = env.head; extbb; extbb = next) {
219 int i, len = (int)extbb->visited;
222 next = (ir_extblk *)extbb->blks;
224 extbb->blks = NEW_ARR_D(ir_node *, env.obst, len);
226 for (block = extbb->link, i = 0; i < len; ++i) {
227 ir_node *nblock = get_irn_link(block);
229 /* ensure that the leader is the first one */
230 extbb->blks[len - 1 - i] = block;
231 set_irn_link(block, NULL);
236 for(i = 0; i < len; ++i) {
239 ir_printf("%+F", extbb->blks[i]);
248 irg->extblk_state = extblk_valid;