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 Block-scheduling strategies.
23 * @author Matthias Braun, Christoph Mallon
27 * The goals of the greedy (and ILP) algorithm here works by assuming that
28 * we want to change as many jumps to fallthroughs as possible (executed jumps
29 * actually, we have to look at the execution frequencies). The algorithms
30 * do this by collecting execution frequencies of all branches (which is easily
31 * possible when all critical edges are split) then removes critical edges where
32 * possible as we don't need and want them anymore now. The algorithms then try
33 * to change as many edges to fallthroughs as possible, this is done by setting
34 * a next and prev pointers on blocks. The greedy algorithm sorts the edges by
35 * execution frequencies and tries to transform them to fallthroughs in this order
39 #include "beblocksched.h"
49 #include "irgraph_t.h"
62 #include "lc_opts_enum.h"
66 #include <lpp/lpp_net.h>
69 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
71 typedef enum _blocksched_algos_t {
72 BLOCKSCHED_NAIV, BLOCKSCHED_GREEDY, BLOCKSCHED_ILP
75 static int algo = BLOCKSCHED_GREEDY;
77 static const lc_opt_enum_int_items_t blockschedalgo_items[] = {
78 { "naiv", BLOCKSCHED_NAIV },
79 { "greedy", BLOCKSCHED_GREEDY },
81 { "ilp", BLOCKSCHED_ILP },
86 static lc_opt_enum_int_var_t algo_var = {
87 &algo, blockschedalgo_items
90 static const lc_opt_table_entry_t be_blocksched_options[] = {
91 LC_OPT_ENT_ENUM_INT ("algo", "the block scheduling algorithm", &algo_var),
97 * / ___|_ __ ___ ___ __| |_ _
98 * | | _| '__/ _ \/ _ \/ _` | | | |
99 * | |_| | | | __/ __/ (_| | |_| |
100 * \____|_| \___|\___|\__,_|\__, |
104 typedef struct blocksched_entry_t blocksched_entry_t;
105 struct blocksched_entry_t {
107 blocksched_entry_t *next;
108 blocksched_entry_t *prev;
111 typedef struct edge_t edge_t;
113 ir_node *block; /**< source block */
114 int pos; /**< number of cfg predecessor (target) */
115 double execfreq; /**< the frequency */
116 double outedge_penalty_freq; /**< for edges leaving the loop this is the
117 penality when we make them a
119 int highest_execfreq; /**< flag that indicates whether this edge is
120 the edge with the highest execfreq pointing
121 away from this block */
124 typedef struct blocksched_env_t blocksched_env_t;
125 struct blocksched_env_t {
127 struct obstack *obst;
128 ir_exec_freq *execfreqs;
135 * Collect cfg frequencies of all edges between blocks.
136 * Also determines edge with highest frequency.
138 static void collect_egde_frequency(ir_node *block, void *data)
140 blocksched_env_t *env = data;
143 blocksched_entry_t *entry;
146 memset(&edge, 0, sizeof(edge));
148 entry = OALLOCZ(env->obst, blocksched_entry_t);
149 entry->block = block;
150 set_irn_link(block, entry);
152 loop = get_irn_loop(block);
154 arity = get_Block_n_cfgpreds(block);
157 /* must be the start block (or end-block for endless loops),
158 * everything else is dead code and should be removed by now */
159 assert(block == get_irg_start_block(env->irg)
160 || block == get_irg_end_block(env->irg));
161 /* nothing to do here */
163 } else if (arity == 1) {
164 ir_node *pred_block = get_Block_cfgpred_block(block, 0);
165 ir_loop *pred_loop = get_irn_loop(pred_block);
166 float freq = (float)get_block_execfreq(env->execfreqs, block);
168 /* is it an edge leaving a loop */
169 if (get_loop_depth(pred_loop) > get_loop_depth(loop)) {
170 float pred_freq = (float)get_block_execfreq(env->execfreqs, pred_block);
171 edge.outedge_penalty_freq = -(pred_freq - freq);
176 edge.execfreq = freq;
177 edge.highest_execfreq = 1;
178 ARR_APP1(edge_t, env->edges, edge);
181 double highest_execfreq = -1.0;
182 int highest_edge_num = -1;
185 for (i = 0; i < arity; ++i) {
187 ir_node *pred_block = get_Block_cfgpred_block(block, i);
189 execfreq = get_block_execfreq(env->execfreqs, pred_block);
192 edge.execfreq = execfreq;
193 edge.highest_execfreq = 0;
194 ARR_APP1(edge_t, env->edges, edge);
196 if (execfreq > highest_execfreq) {
197 highest_execfreq = execfreq;
198 highest_edge_num = ARR_LEN(env->edges) - 1;
202 if (highest_edge_num >= 0)
203 env->edges[highest_edge_num].highest_execfreq = 1;
207 static int cmp_edges(const void *d1, const void *d2)
209 const edge_t *e1 = d1;
210 const edge_t *e2 = d2;
212 return QSORT_CMP(e2->execfreq, e1->execfreq);
215 static int cmp_edges_outedge_penalty(const void *d1, const void *d2)
217 const edge_t *e1 = d1;
218 const edge_t *e2 = d2;
219 /* reverse sorting as penalties are negative */
220 return QSORT_CMP(e1->outedge_penalty_freq, e2->outedge_penalty_freq);
223 static void clear_loop_links(ir_loop *loop)
227 set_loop_link(loop, NULL);
228 n = get_loop_n_elements(loop);
229 for (i = 0; i < n; ++i) {
230 loop_element elem = get_loop_element(loop, i);
231 if (*elem.kind == k_ir_loop) {
232 clear_loop_links(elem.son);
237 static void coalesce_blocks(blocksched_env_t *env)
240 int edge_count = ARR_LEN(env->edges);
241 edge_t *edges = env->edges;
243 /* sort interblock edges by execution frequency */
244 qsort(edges, ARR_LEN(edges), sizeof(edges[0]), cmp_edges);
246 /* run1: only look at jumps */
247 for (i = 0; i < edge_count; ++i) {
248 const edge_t *edge = &edges[i];
249 ir_node *block = edge->block;
252 blocksched_entry_t *entry, *pred_entry;
254 /* only check edge with highest frequency */
255 if (! edge->highest_execfreq)
258 /* the block might have been removed already... */
259 if (is_Bad(get_Block_cfgpred(block, 0)))
262 pred_block = get_Block_cfgpred_block(block, pos);
263 entry = get_irn_link(block);
264 pred_entry = get_irn_link(pred_block);
266 if (pred_entry->next != NULL || entry->prev != NULL)
269 /* only coalesce jumps */
270 if (get_block_succ_next(pred_block, get_block_succ_first(pred_block)) != NULL)
273 /* schedule the 2 blocks behind each other */
274 DB((dbg, LEVEL_1, "Coalesce (Jump) %+F -> %+F (%.3g)\n",
275 pred_entry->block, entry->block, edge->execfreq));
276 pred_entry->next = entry;
277 entry->prev = pred_entry;
280 /* run2: pick loop fallthroughs */
281 clear_loop_links(get_irg_loop(env->irg));
283 qsort(edges, ARR_LEN(edges), sizeof(edges[0]), cmp_edges_outedge_penalty);
284 for (i = 0; i < edge_count; ++i) {
285 const edge_t *edge = &edges[i];
286 ir_node *block = edge->block;
289 blocksched_entry_t *entry, *pred_entry;
293 /* already seen all loop outedges? */
294 if (edge->outedge_penalty_freq == 0)
297 /* the block might have been removed already... */
298 if (is_Bad(get_Block_cfgpred(block, pos)))
301 pred_block = get_Block_cfgpred_block(block, pos);
302 entry = get_irn_link(block);
303 pred_entry = get_irn_link(pred_block);
305 if (pred_entry->next != NULL || entry->prev != NULL)
308 /* we want at most 1 outedge fallthrough per loop */
309 loop = get_irn_loop(pred_block);
310 if (get_loop_link(loop) != NULL)
313 /* schedule the 2 blocks behind each other */
314 DB((dbg, LEVEL_1, "Coalesce (Loop Outedge) %+F -> %+F (%.3g)\n",
315 pred_entry->block, entry->block, edge->execfreq));
316 pred_entry->next = entry;
317 entry->prev = pred_entry;
319 /* all loops left have an outedge now */
320 outer_loop = get_irn_loop(block);
322 /* we set loop link to loop to mark it */
323 set_loop_link(loop, loop);
324 loop = get_loop_outer_loop(loop);
325 } while (loop != outer_loop);
328 /* sort interblock edges by execution frequency */
329 qsort(edges, ARR_LEN(edges), sizeof(edges[0]), cmp_edges);
331 /* run3: remaining edges */
332 for (i = 0; i < edge_count; ++i) {
333 const edge_t *edge = &edges[i];
334 ir_node *block = edge->block;
337 blocksched_entry_t *entry, *pred_entry;
339 /* the block might have been removed already... */
340 if (is_Bad(get_Block_cfgpred(block, pos)))
343 pred_block = get_Block_cfgpred_block(block, pos);
344 entry = get_irn_link(block);
345 pred_entry = get_irn_link(pred_block);
347 /* is 1 of the blocks already attached to another block? */
348 if (pred_entry->next != NULL || entry->prev != NULL)
351 /* schedule the 2 blocks behind each other */
352 DB((dbg, LEVEL_1, "Coalesce (CondJump) %+F -> %+F (%.3g)\n",
353 pred_entry->block, entry->block, edge->execfreq));
354 pred_entry->next = entry;
355 entry->prev = pred_entry;
359 static void pick_block_successor(blocksched_entry_t *entry, blocksched_env_t *env)
361 ir_node *block = entry->block;
362 ir_node *succ = NULL;
363 blocksched_entry_t *succ_entry;
364 const ir_edge_t *edge;
365 double best_succ_execfreq;
367 if (irn_visited_else_mark(block))
372 DB((dbg, LEVEL_1, "Pick succ of %+F\n", block));
374 /* put all successors into the worklist */
375 foreach_block_succ(block, edge) {
376 ir_node *succ_block = get_edge_src_irn(edge);
378 if (irn_visited(succ_block))
381 /* we only need to put the first of a series of already connected
382 * blocks into the worklist */
383 succ_entry = get_irn_link(succ_block);
384 while (succ_entry->prev != NULL) {
385 /* break cycles... */
386 if (succ_entry->prev->block == succ_block) {
387 succ_entry->prev->next = NULL;
388 succ_entry->prev = NULL;
391 succ_entry = succ_entry->prev;
394 if (irn_visited(succ_entry->block))
397 DB((dbg, LEVEL_1, "Put %+F into worklist\n", succ_entry->block));
398 pdeq_putr(env->worklist, succ_entry->block);
401 if (entry->next != NULL) {
402 pick_block_successor(entry->next, env);
406 DB((dbg, LEVEL_1, "deciding...\n"));
407 best_succ_execfreq = -1;
409 /* no successor yet: pick the successor block with the highest execution
410 * frequency which has no predecessor yet */
412 foreach_block_succ(block, edge) {
413 ir_node *succ_block = get_edge_src_irn(edge);
416 if (irn_visited(succ_block))
419 succ_entry = get_irn_link(succ_block);
420 if (succ_entry->prev != NULL)
423 execfreq = get_block_execfreq(env->execfreqs, succ_block);
424 if (execfreq > best_succ_execfreq) {
425 best_succ_execfreq = execfreq;
431 DB((dbg, LEVEL_1, "pick from worklist\n"));
434 if (pdeq_empty(env->worklist)) {
435 DB((dbg, LEVEL_1, "worklist empty\n"));
438 succ = pdeq_getl(env->worklist);
439 } while (irn_visited(succ));
442 succ_entry = get_irn_link(succ);
443 entry->next = succ_entry;
444 succ_entry->prev = entry;
446 pick_block_successor(succ_entry, env);
449 static blocksched_entry_t *finish_block_schedule(blocksched_env_t *env)
451 ir_graph *irg = env->irg;
452 ir_node *startblock = get_irg_start_block(irg);
453 blocksched_entry_t *entry = get_irn_link(startblock);
455 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
456 inc_irg_visited(irg);
458 env->worklist = new_pdeq();
459 pick_block_successor(entry, env);
460 assert(pdeq_empty(env->worklist));
461 del_pdeq(env->worklist);
463 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
468 static ir_node **create_blocksched_array(blocksched_env_t *env, blocksched_entry_t *first,
469 int count, struct obstack* obst)
472 ir_node **block_list;
473 blocksched_entry_t *entry;
476 block_list = NEW_ARR_D(ir_node *, obst, count);
477 DB((dbg, LEVEL_1, "Blockschedule:\n"));
479 for (entry = first; entry != NULL; entry = entry->next) {
481 block_list[i++] = entry->block;
482 DB((dbg, LEVEL_1, "\t%+F\n", entry->block));
489 static ir_node **create_block_schedule_greedy(ir_graph *irg, ir_exec_freq *execfreqs)
491 blocksched_env_t env;
493 blocksched_entry_t *start_entry;
494 ir_node **block_list;
500 env.execfreqs = execfreqs;
501 env.edges = NEW_ARR_F(edge_t, 0);
505 /* make sure loopinfo is up-to-date */
506 if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
507 construct_cf_backedges(irg);
510 // collect edge execution frequencies
511 irg_block_walk_graph(irg, collect_egde_frequency, NULL, &env);
513 (void)be_remove_empty_blocks(irg);
515 if (algo != BLOCKSCHED_NAIV)
516 coalesce_blocks(&env);
518 start_entry = finish_block_schedule(&env);
519 block_list = create_blocksched_array(&env, start_entry, env.blockcount,
520 be_get_be_obst(irg));
522 DEL_ARR_F(env.edges);
523 obstack_free(&obst, NULL);
538 typedef struct _ilp_edge_t {
539 ir_node *block; /**< source block */
540 int pos; /**< number of cfg predecessor (target) */
544 typedef struct _blocksched_ilp_env_t {
545 blocksched_env_t env;
546 ilp_edge_t *ilpedges;
548 } blocksched_ilp_env_t;
550 typedef struct _blocksched_ilp_entry_t {
552 struct _blocksched_entry_t *next;
553 struct _blocksched_entry_t *prev;
556 } blocksched_ilp_entry_t;
558 static int add_ilp_edge(ir_node *block, int pos, double execfreq, blocksched_ilp_env_t *env)
562 int edgeidx = ARR_LEN(env->ilpedges);
564 snprintf(name, sizeof(name), "edge%d", edgeidx);
568 edge.ilpvar = lpp_add_var_default(env->lpp, name, lpp_binary, execfreq, 1.0);
570 ARR_APP1(ilp_edge_t, env->ilpedges, edge);
574 static void collect_egde_frequency_ilp(ir_node *block, void *data)
576 blocksched_ilp_env_t *env = data;
577 ir_graph *irg = env->env.irg;
578 ir_node *startblock = get_irg_start_block(irg);
583 blocksched_ilp_entry_t *entry;
585 snprintf(name, sizeof(name), "block_out_constr_%ld", get_irn_node_nr(block));
586 out_count = get_irn_n_edges_kind(block, EDGE_KIND_BLOCK);
588 entry = OALLOC(env->env.obst, blocksched_ilp_entry_t);
589 entry->block = block;
592 entry->out_cst = lpp_add_cst_uniq(env->lpp, name, lpp_greater, out_count - 1);
593 set_irn_link(block, entry);
595 if (block == startblock)
598 arity = get_irn_arity(block);
600 double execfreq = get_block_execfreq(env->env.execfreqs, block);
601 add_ilp_edge(block, 0, execfreq, env);
606 snprintf(name, sizeof(name), "block_in_constr_%ld", get_irn_node_nr(block));
607 cst = lpp_add_cst_uniq(env->lpp, name, lpp_greater, arity - 1);
609 for (i = 0; i < arity; ++i) {
613 ir_node *pred_block = get_Block_cfgpred_block(block, i);
615 execfreq = get_block_execfreq(env->env.execfreqs, pred_block);
616 edgenum = add_ilp_edge(block, i, execfreq, env);
617 edge = &env->ilpedges[edgenum];
618 lpp_set_factor_fast(env->lpp, cst, edge->ilpvar, 1.0);
624 static void coalesce_blocks_ilp(blocksched_ilp_env_t *env)
626 int edge_count = ARR_LEN(env->ilpedges);
627 be_options_t *options = be_get_irg_options(env->env.irg);
630 /* complete out constraints */
631 for (i = 0; i < edge_count; ++i) {
632 const ilp_edge_t *edge = &env->ilpedges[i];
633 ir_node *block = edge->block;
635 blocksched_ilp_entry_t *entry;
637 /* the block might have been removed already... */
638 if (is_Bad(get_Block_cfgpred(block, 0)))
641 pred = get_Block_cfgpred_block(block, edge->pos);
642 entry = get_irn_link(pred);
644 DB((dbg, LEVEL_1, "Adding out cst to %+F from %+F,%d\n",
645 pred, block, edge->pos));
646 lpp_set_factor_fast(env->lpp, entry->out_cst, edge->ilpvar, 1.0);
649 lpp_solve_net(env->lpp, options->ilp_server, options->ilp_solver);
650 assert(lpp_is_sol_valid(env->lpp));
652 /* Apply results to edges */
653 for (i = 0; i < edge_count; ++i) {
654 const ilp_edge_t *edge = &env->ilpedges[i];
655 ir_node *block = edge->block;
658 blocksched_entry_t *entry;
659 blocksched_entry_t *pred_entry;
661 /* the block might have been removed already... */
662 if (is_Bad(get_Block_cfgpred(block, 0)))
665 is_jump = (int)lpp_get_var_sol(env->lpp, edge->ilpvar);
669 pred = get_Block_cfgpred_block(block, edge->pos);
670 entry = get_irn_link(block);
671 pred_entry = get_irn_link(pred);
673 assert(entry->prev == NULL && pred_entry->next == NULL);
674 entry->prev = pred_entry;
675 pred_entry->next = entry;
679 static ir_node **create_block_schedule_ilp(ir_graph *irg, ir_exec_freq *execfreqs)
681 blocksched_ilp_env_t env;
683 blocksched_entry_t *start_entry;
684 ir_node **block_list;
689 env.env.obst = &obst;
690 env.env.execfreqs = execfreqs;
691 env.env.worklist = NULL;
692 env.env.blockcount = 0;
693 env.ilpedges = NEW_ARR_F(ilp_edge_t, 0);
695 env.lpp = new_lpp("blockschedule", lpp_minimize);
696 lpp_set_time_limit(env.lpp, 20);
697 lpp_set_log(env.lpp, stdout);
699 irg_block_walk_graph(irg, collect_egde_frequency_ilp, NULL, &env);
701 (void)be_remove_empty_blocks(irg);
702 coalesce_blocks_ilp(&env);
704 start_entry = finish_block_schedule(&env.env);
705 block_list = create_blocksched_array(&env.env, start_entry,
707 be_get_be_obst(irg));
709 DEL_ARR_F(env.ilpedges);
711 obstack_free(&obst, NULL);
715 #endif /* WITH_ILP */
720 * | |\/| |/ _` | | '_ \
721 * | | | | (_| | | | | |
722 * |_| |_|\__,_|_|_| |_|
725 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_blocksched);
726 void be_init_blocksched(void)
728 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
729 lc_opt_entry_t *blocksched_grp = lc_opt_get_grp(be_grp, "blocksched");
731 lc_opt_add_table(blocksched_grp, be_blocksched_options);
733 FIRM_DBG_REGISTER(dbg, "firm.be.blocksched");
736 ir_node **be_create_block_schedule(ir_graph *irg)
738 ir_exec_freq *execfreqs = be_get_irg_exec_freq(irg);
741 case BLOCKSCHED_GREEDY:
742 case BLOCKSCHED_NAIV:
743 return create_block_schedule_greedy(irg, execfreqs);
746 return create_block_schedule_ilp(irg, execfreqs);
747 #endif /* WITH_ILP */
750 panic("unknown blocksched algo");