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
41 #include "beblocksched.h"
51 #include "irgraph_t.h"
62 #include <libcore/lc_opts.h>
63 #include <libcore/lc_opts_enum.h>
64 #include <libcore/lc_timing.h>
68 #include <lpp/lpp_net.h>
71 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
73 typedef enum _blocksched_algos_t {
74 BLOCKSCHED_NAIV, BLOCKSCHED_EXTBB, BLOCKSCHED_GREEDY, BLOCKSCHED_ILP
77 static int algo = BLOCKSCHED_GREEDY;
79 static const lc_opt_enum_int_items_t blockschedalgo_items[] = {
80 { "naiv", BLOCKSCHED_NAIV },
81 { "extbb", BLOCKSCHED_EXTBB },
82 { "greedy", BLOCKSCHED_GREEDY },
84 { "ilp", BLOCKSCHED_ILP },
89 static lc_opt_enum_int_var_t algo_var = {
90 &algo, blockschedalgo_items
93 static const lc_opt_table_entry_t be_blocksched_options[] = {
94 LC_OPT_ENT_ENUM_INT ("algo", "the block scheduling algorithm", &algo_var),
100 * / ___|_ __ ___ ___ __| |_ _
101 * | | _| '__/ _ \/ _ \/ _` | | | |
102 * | |_| | | | __/ __/ (_| | |_| |
103 * \____|_| \___|\___|\__,_|\__, |
107 typedef struct _blocksched_entry_t {
109 struct _blocksched_entry_t *next;
110 struct _blocksched_entry_t *prev;
111 } blocksched_entry_t;
113 typedef struct _edge_t {
114 ir_node *block; /**< source block */
115 int pos; /**< number of cfg predecessor (target) */
116 double execfreq; /**< the frequency */
117 int highest_execfreq; /**< flag that indicates whether this edge is the edge with the highest
118 execfreq pointing away from this block */
121 typedef struct _blocksched_env_t {
123 struct obstack *obst;
124 ir_exec_freq *execfreqs;
131 * Collect cfg frequencies of all edges between blocks.
132 * Also determines edge with highest frequency.
134 static void collect_egde_frequency(ir_node *block, void *data)
136 blocksched_env_t *env = data;
139 blocksched_entry_t *entry;
141 entry = obstack_alloc(env->obst, sizeof(entry[0]));
142 entry->block = block;
145 set_irn_link(block, entry);
147 arity = get_Block_n_cfgpreds(block);
150 assert(block == get_irg_start_block(env->irg)
151 || block == get_irg_end_block(env->irg));
152 /* must be the start block (or end-block for endless loops), nothing to
155 } else if (arity == 1) {
158 edge.execfreq = get_block_execfreq(env->execfreqs, block);
159 edge.highest_execfreq = 1;
160 ARR_APP1(edge_t, env->edges, edge);
163 double highest_execfreq = -1.0;
164 int highest_edge_num = -1;
167 for (i = 0; i < arity; ++i) {
169 ir_node *pred_block = get_Block_cfgpred_block(block, i);
171 execfreq = get_block_execfreq(env->execfreqs, pred_block);
174 edge.execfreq = execfreq;
175 edge.highest_execfreq = 0;
176 ARR_APP1(edge_t, env->edges, edge);
178 if (execfreq > highest_execfreq) {
179 highest_execfreq = execfreq;
180 highest_edge_num = ARR_LEN(env->edges) - 1;
184 if(highest_edge_num >= 0)
185 env->edges[highest_edge_num].highest_execfreq = 1;
189 static int cmp_edges(const void *d1, const void *d2)
191 const edge_t *e1 = d1;
192 const edge_t *e2 = d2;
194 return QSORT_CMP(e2->execfreq, e1->execfreq);
197 static void coalesce_blocks(blocksched_env_t *env)
200 int edge_count = ARR_LEN(env->edges);
202 /* run1: only look at jumps */
203 for (i = 0; i < edge_count; ++i) {
204 const edge_t *edge = &env->edges[i];
205 ir_node *block = edge->block;
208 blocksched_entry_t *entry, *pred_entry;
210 /* only check edge with highest frequency */
211 if (! edge->highest_execfreq)
214 /* the block might have been removed already... */
215 if (is_Bad(get_Block_cfgpred(block, 0)))
218 pred_block = get_Block_cfgpred_block(block, pos);
219 entry = get_irn_link(block);
220 pred_entry = get_irn_link(pred_block);
222 if (pred_entry->next != NULL || entry->prev != NULL)
225 /* only coalesce jumps */
226 if (get_block_succ_next(pred_block, get_block_succ_first(pred_block)) != NULL)
229 /* schedule the 2 blocks behind each other */
230 DBG((dbg, LEVEL_1, "Coalesce (Jump) %+F -> %+F (%.3g)\n",
231 pred_entry->block, entry->block, edge->execfreq));
232 pred_entry->next = entry;
233 entry->prev = pred_entry;
236 /* run2: remaining edges */
237 for (i = 0; i < edge_count; ++i) {
238 const edge_t *edge = &env->edges[i];
239 ir_node *block = edge->block;
242 blocksched_entry_t *entry, *pred_entry;
244 /* the block might have been removed already... */
245 if (is_Bad(get_Block_cfgpred(block, 0)))
248 /* we can't do fallthroughs in backedges */
249 if (is_backedge(block, pos))
252 pred_block = get_Block_cfgpred_block(block, pos);
253 entry = get_irn_link(block);
254 pred_entry = get_irn_link(pred_block);
256 /* is 1 of the blocks already attached to another block? */
257 if (pred_entry->next != NULL || entry->prev != NULL)
260 /* schedule the 2 blocks behind each other */
261 DBG((dbg, LEVEL_1, "Coalesce (CondJump) %+F -> %+F (%.3g)\n",
262 pred_entry->block, entry->block, edge->execfreq));
263 pred_entry->next = entry;
264 entry->prev = pred_entry;
268 static void pick_block_successor(blocksched_entry_t *entry, blocksched_env_t *env)
270 ir_node *block = entry->block;
271 ir_node *succ = NULL;
272 blocksched_entry_t *succ_entry;
273 const ir_edge_t *edge;
274 double best_succ_execfreq;
276 if (irn_visited(block))
280 mark_irn_visited(block);
282 DBG((dbg, LEVEL_1, "Pick succ of %+F\n", block));
284 /* put all successors into the worklist */
285 foreach_block_succ(block, edge) {
286 ir_node *succ_block = get_edge_src_irn(edge);
288 if (irn_visited(succ_block))
291 /* we only need to put the first of a series of already connected
292 * blocks into the worklist */
293 succ_entry = get_irn_link(succ_block);
294 while (succ_entry->prev != NULL) {
295 /* break cycles... */
296 if (succ_entry->prev->block == succ_block) {
297 succ_entry->prev->next = NULL;
298 succ_entry->prev = NULL;
301 succ_entry = succ_entry->prev;
304 if (irn_visited(succ_entry->block))
307 DBG((dbg, LEVEL_1, "Put %+F into worklist\n", succ_entry->block));
308 pdeq_putr(env->worklist, succ_entry->block);
311 if (entry->next != NULL) {
312 pick_block_successor(entry->next, env);
316 DBG((dbg, LEVEL_1, "deciding...\n"));
317 best_succ_execfreq = -1;
319 /* no successor yet: pick the successor block with the highest execution
320 * frequency which has no predecessor yet */
322 foreach_block_succ(block, edge) {
323 ir_node *succ_block = get_edge_src_irn(edge);
326 if (irn_visited(succ_block))
329 succ_entry = get_irn_link(succ_block);
330 if (succ_entry->prev != NULL)
333 execfreq = get_block_execfreq(env->execfreqs, succ_block);
334 if (execfreq > best_succ_execfreq) {
335 best_succ_execfreq = execfreq;
341 DBG((dbg, LEVEL_1, "pick from worklist\n"));
344 if (pdeq_empty(env->worklist)) {
345 DBG((dbg, LEVEL_1, "worklist empty\n"));
348 succ = pdeq_getl(env->worklist);
349 } while (irn_visited(succ));
352 succ_entry = get_irn_link(succ);
353 entry->next = succ_entry;
354 succ_entry->prev = entry;
356 pick_block_successor(succ_entry, env);
359 static blocksched_entry_t *finish_block_schedule(blocksched_env_t *env)
361 ir_graph *irg = env->irg;
362 ir_node *startblock = get_irg_start_block(irg);
363 blocksched_entry_t *entry = get_irn_link(startblock);
365 set_using_visited(irg);
366 inc_irg_visited(irg);
368 env->worklist = new_pdeq();
369 pick_block_successor(entry, env);
370 assert(pdeq_empty(env->worklist));
371 del_pdeq(env->worklist);
373 clear_using_visited(irg);
378 static ir_node **create_blocksched_array(blocksched_env_t *env, blocksched_entry_t *first,
379 int count, struct obstack* obst)
382 ir_node **block_list;
383 blocksched_entry_t *entry;
386 block_list = NEW_ARR_D(ir_node *, obst, count);
387 DBG((dbg, LEVEL_1, "Blockschedule:\n"));
389 for (entry = first; entry != NULL; entry = entry->next) {
391 block_list[i++] = entry->block;
392 DBG((dbg, LEVEL_1, "\t%+F\n", entry->block));
399 static ir_node **create_block_schedule_greedy(ir_graph *irg, ir_exec_freq *execfreqs)
401 blocksched_env_t env;
403 blocksched_entry_t *start_entry;
404 ir_node **block_list;
410 env.execfreqs = execfreqs;
411 env.edges = NEW_ARR_F(edge_t, 0);
415 // collect edge execution frequencies
416 irg_block_walk_graph(irg, collect_egde_frequency, NULL, &env);
418 // sort interblock edges by execution frequency
419 qsort(env.edges, ARR_LEN(env.edges), sizeof(env.edges[0]), cmp_edges);
421 (void)be_remove_empty_blocks(irg);
423 if (algo != BLOCKSCHED_NAIV)
424 coalesce_blocks(&env);
426 start_entry = finish_block_schedule(&env);
427 block_list = create_blocksched_array(&env, start_entry, env.blockcount, get_irg_obstack(irg));
429 DEL_ARR_F(env.edges);
430 obstack_free(&obst, NULL);
445 typedef struct _ilp_edge_t {
446 ir_node *block; /**< source block */
447 int pos; /**< number of cfg predecessor (target) */
451 typedef struct _blocksched_ilp_env_t {
452 blocksched_env_t env;
453 ilp_edge_t *ilpedges;
455 } blocksched_ilp_env_t;
457 typedef struct _blocksched_ilp_entry_t {
459 struct _blocksched_entry_t *next;
460 struct _blocksched_entry_t *prev;
463 } blocksched_ilp_entry_t;
465 static int add_ilp_edge(ir_node *block, int pos, double execfreq, blocksched_ilp_env_t *env)
469 int edgeidx = ARR_LEN(env->ilpedges);
471 snprintf(name, sizeof(name), "edge%d", edgeidx);
475 edge.ilpvar = lpp_add_var_default(env->lpp, name, lpp_binary, execfreq, 1.0);
477 ARR_APP1(ilp_edge_t, env->ilpedges, edge);
481 static void collect_egde_frequency_ilp(ir_node *block, void *data)
483 blocksched_ilp_env_t *env = data;
484 ir_graph *irg = env->env.irg;
485 ir_node *startblock = get_irg_start_block(irg);
490 blocksched_ilp_entry_t *entry;
492 snprintf(name, sizeof(name), "block_out_constr_%ld", get_irn_node_nr(block));
493 out_count = get_irn_n_edges_kind(block, EDGE_KIND_BLOCK);
495 entry = obstack_alloc(env->env.obst, sizeof(entry[0]));
496 entry->block = block;
499 entry->out_cst = lpp_add_cst_uniq(env->lpp, name, lpp_greater, out_count - 1);
500 set_irn_link(block, entry);
502 if (block == startblock)
505 arity = get_irn_arity(block);
507 double execfreq = get_block_execfreq(env->env.execfreqs, block);
508 add_ilp_edge(block, 0, execfreq, env);
512 int *edgenums = alloca(sizeof(edgenums[0]) * arity);
514 snprintf(name, sizeof(name), "block_in_constr_%ld", get_irn_node_nr(block));
515 cst = lpp_add_cst_uniq(env->lpp, name, lpp_greater, arity - 1);
517 for (i = 0; i < arity; ++i) {
521 ir_node *pred_block = get_Block_cfgpred_block(block, i);
523 execfreq = get_block_execfreq(env->env.execfreqs, pred_block);
524 edgenum = add_ilp_edge(block, i, execfreq, env);
525 edge = &env->ilpedges[edgenum];
526 lpp_set_factor_fast(env->lpp, cst, edge->ilpvar, 1.0);
532 static void coalesce_blocks_ilp(blocksched_ilp_env_t *env)
535 int edge_count = ARR_LEN(env->ilpedges);
537 /* complete out constraints */
538 for(i = 0; i < edge_count; ++i) {
539 const ilp_edge_t *edge = &env->ilpedges[i];
540 ir_node *block = edge->block;
542 blocksched_ilp_entry_t *entry;
544 /* the block might have been removed already... */
545 if (is_Bad(get_Block_cfgpred(block, 0)))
548 pred = get_Block_cfgpred_block(block, edge->pos);
549 entry = get_irn_link(pred);
551 DBG((dbg, LEVEL_1, "Adding out cst to %+F from %+F,%d\n",
552 pred, block, edge->pos));
553 lpp_set_factor_fast(env->lpp, entry->out_cst, edge->ilpvar, 1.0);
560 lpp_dump(env->lpp, "lpp.out");
561 snprintf(fname, sizeof(fname), "lpp_%s.plain", get_irg_dump_name(env->env.irg));
562 f = fopen(fname, "w");
563 lpp_dump_plain(env->lpp, f);
568 //lpp_solve_net(env->lpp, main_env->options->ilp_server, main_env->options->ilp_solver);
569 lpp_solve_net(env->lpp, "i44pc52", "cplex");
570 assert(lpp_is_sol_valid(env->lpp));
572 /* Apply results to edges */
573 for (i = 0; i < edge_count; ++i) {
574 const ilp_edge_t *edge = &env->ilpedges[i];
575 ir_node *block = edge->block;
578 blocksched_entry_t *entry;
579 blocksched_entry_t *pred_entry;
581 /* the block might have been removed already... */
582 if (is_Bad(get_Block_cfgpred(block, 0)))
585 is_jump = (int)lpp_get_var_sol(env->lpp, edge->ilpvar);
589 pred = get_Block_cfgpred_block(block, edge->pos);
590 entry = get_irn_link(block);
591 pred_entry = get_irn_link(pred);
593 assert(entry->prev == NULL && pred_entry->next == NULL);
594 entry->prev = pred_entry;
595 pred_entry->next = entry;
599 static ir_node **create_block_schedule_ilp(ir_graph *irg, ir_exec_freq *execfreqs)
601 blocksched_ilp_env_t env;
603 blocksched_entry_t *start_entry;
604 ir_node **block_list;
609 env.env.obst = &obst;
610 env.env.execfreqs = execfreqs;
611 env.env.worklist = NULL;
612 env.env.blockcount = 0;
613 env.ilpedges = NEW_ARR_F(ilp_edge_t, 0);
615 env.lpp = new_lpp("blockschedule", lpp_minimize);
616 lpp_set_time_limit(env.lpp, 20);
617 lpp_set_log(env.lpp, stdout);
619 irg_block_walk_graph(irg, collect_egde_frequency_ilp, NULL, &env);
621 (void)be_remove_empty_blocks(irg);
622 coalesce_blocks_ilp(&env);
624 start_entry = finish_block_schedule(&env.env);
625 block_list = create_blocksched_array(&env.env, start_entry, env.env.blockcount, get_irg_obstack(irg));
627 DEL_ARR_F(env.ilpedges);
629 obstack_free(&obst, NULL);
633 #endif /* WITH_ILP */
637 * | ____|_ _| |_| __ )| __ )
638 * | _| \ \/ / __| _ \| _ \
639 * | |___ > <| |_| |_) | |_) |
640 * |_____/_/\_\\__|____/|____/
644 /** A simple forward single linked list. */
646 ir_node *start; /**< start of the list */
647 ir_node *end; /**< last block in the list */
648 unsigned n_blks; /**< number of blocks in the list */
651 static void add_block(anchor *list, ir_node *block) {
652 if (list->start == NULL) {
656 set_irn_link(list->end, block);
663 static void create_block_list(ir_node *leader_block, anchor *list) {
665 const ir_edge_t *edge;
666 ir_node *block = NULL;
667 ir_extblk *extbb = get_Block_extbb(leader_block);
669 if (extbb_visited(extbb))
671 mark_extbb_visited(extbb);
673 for (i = 0; i < get_extbb_n_blocks(extbb); ++i) {
674 block = get_extbb_block(extbb, i);
675 add_block(list, block);
678 assert(block != NULL);
680 /* pick successor extbbs */
681 foreach_block_succ(block, edge) {
682 ir_node *succ = get_edge_src_irn(edge);
683 create_block_list(succ, list);
686 for (i = 0; i < get_extbb_n_blocks(extbb) - 1; ++i) {
687 block = get_extbb_block(extbb, i);
689 foreach_block_succ(block, edge) {
690 ir_node *succ = get_edge_src_irn(edge);
691 create_block_list(succ, list);
696 void compute_extbb_execfreqs(ir_graph *irg, ir_exec_freq *execfreqs);
699 * Calculates a block schedule. The schedule is stored as a linked
700 * list starting at the start_block of the irg.
702 static ir_node **create_extbb_block_schedule(ir_graph *irg, ir_exec_freq *execfreqs)
705 ir_node **blk_list, *b, *n;
708 /* schedule extended basic blocks */
709 compute_extbb_execfreqs(irg, execfreqs);
710 //compute_extbb(irg);
716 set_using_irn_link(irg);
717 set_using_visited(irg);
718 inc_irg_block_visited(irg);
720 create_block_list(get_irg_start_block(irg), &list);
722 /** create an array, so we can go forward and backward */
723 blk_list = NEW_ARR_D(ir_node *, irg->obst,list.n_blks);
725 for (i = 0, b = list.start; b; b = n, ++i) {
730 clear_using_irn_link(irg);
731 clear_using_visited(irg);
739 * | |\/| |/ _` | | '_ \
740 * | | | | (_| | | | | |
741 * |_| |_|\__,_|_|_| |_|
744 void be_init_blocksched(void)
746 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
747 lc_opt_entry_t *blocksched_grp = lc_opt_get_grp(be_grp, "blocksched");
749 lc_opt_add_table(blocksched_grp, be_blocksched_options);
751 FIRM_DBG_REGISTER(dbg, "firm.be.blocksched");
754 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_blocksched);
756 ir_node **be_create_block_schedule(ir_graph *irg, ir_exec_freq *execfreqs)
759 case BLOCKSCHED_GREEDY:
760 case BLOCKSCHED_NAIV:
761 return create_block_schedule_greedy(irg, execfreqs);
762 case BLOCKSCHED_EXTBB:
763 return create_extbb_block_schedule(irg, execfreqs);
766 return create_block_schedule_ilp(irg, execfreqs);
767 #endif /* WITH_ILP */
770 assert(0 && "unknown blocksched algo");