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 Block-scheduling strategies.
23 * @author Matthias Braun, Christoph Mallon
31 #include "beblocksched.h"
40 #include "irgraph_t.h"
50 #include <libcore/lc_opts.h>
51 #include <libcore/lc_opts_enum.h>
52 #include <libcore/lc_timing.h>
56 #include <lpp/lpp_net.h>
59 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
61 typedef enum _blocksched_algos_t {
62 BLOCKSCHED_NAIV, BLOCKSCHED_EXTBB, BLOCKSCHED_GREEDY, BLOCKSCHED_ILP
65 static int algo = BLOCKSCHED_GREEDY;
67 static const lc_opt_enum_int_items_t blockschedalgo_items[] = {
68 { "naiv", BLOCKSCHED_NAIV },
69 { "extbb", BLOCKSCHED_EXTBB },
70 { "greedy", BLOCKSCHED_GREEDY },
72 { "ilp", BLOCKSCHED_ILP },
77 static lc_opt_enum_int_var_t algo_var = {
78 &algo, blockschedalgo_items
81 static const lc_opt_table_entry_t be_blocksched_options[] = {
82 LC_OPT_ENT_ENUM_INT ("algo", "the block scheduling algorithm", &algo_var),
88 * / ___|_ __ ___ ___ __| |_ _
89 * | | _| '__/ _ \/ _ \/ _` | | | |
90 * | |_| | | | __/ __/ (_| | |_| |
91 * \____|_| \___|\___|\__,_|\__, |
95 typedef struct _blocksched_entry_t {
97 struct _blocksched_entry_t *next;
98 struct _blocksched_entry_t *prev;
101 typedef struct _edge_t {
102 ir_node *block; /**< source block */
103 int pos; /**< number of cfg predecessor (target) */
104 double execfreq; /**< the frequency */
105 int highest_execfreq; /**< flag that indicates wether this edge is the edge with the highest
106 execfreq pointing away from this block */
109 typedef struct _blocksched_env_t {
111 struct obstack *obst;
112 ir_exec_freq *execfreqs;
119 * Collect cfg frequencies of all edges between blocks.
120 * Also determines edge with highest frequency.
122 static void collect_egde_frequency(ir_node *block, void *data)
124 blocksched_env_t *env = data;
127 blocksched_entry_t *entry;
129 entry = obstack_alloc(env->obst, sizeof(entry[0]));
130 entry->block = block;
133 set_irn_link(block, entry);
135 if (block == get_irg_start_block(env->irg))
138 arity = get_irn_arity(block);
143 edge.execfreq = get_block_execfreq(env->execfreqs, block);
144 edge.highest_execfreq = 1;
145 ARR_APP1(edge_t, env->edges, edge);
148 double highest_execfreq = -1.0;
149 int highest_edge_num = -1;
152 for (i = 0; i < arity; ++i) {
154 ir_node *pred_block = get_Block_cfgpred_block(block, i);
156 execfreq = get_block_execfreq(env->execfreqs, pred_block);
159 edge.execfreq = execfreq;
160 edge.highest_execfreq = 0;
161 ARR_APP1(edge_t, env->edges, edge);
163 if (execfreq > highest_execfreq) {
164 highest_execfreq = execfreq;
165 highest_edge_num = ARR_LEN(env->edges) - 1;
169 if(highest_edge_num >= 0)
170 env->edges[highest_edge_num].highest_execfreq = 1;
174 static int cmp_edges(const void *d1, const void *d2)
176 const edge_t *e1 = d1;
177 const edge_t *e2 = d2;
179 return QSORT_CMP(e2->execfreq, e1->execfreq);
182 static void coalesce_blocks(blocksched_env_t *env)
185 int edge_count = ARR_LEN(env->edges);
187 /* run1: only look at jumps */
188 for (i = 0; i < edge_count; ++i) {
189 const edge_t *edge = &env->edges[i];
190 ir_node *block = edge->block;
192 blocksched_entry_t *entry, *pred_entry;
194 /* the block might have been removed already... */
195 if (is_Bad(get_Block_cfgpred(block, 0)))
198 /* only check edge with highest frequency */
199 if (! edge->highest_execfreq)
202 pred_block = get_Block_cfgpred_block(block, edge->pos);
203 entry = get_irn_link(block);
204 pred_entry = get_irn_link(pred_block);
206 if (pred_entry->next != NULL || entry->prev != NULL)
209 /* only coalesce jumps */
210 if (get_block_succ_next(pred_block, get_block_succ_first(pred_block)) != NULL)
213 /* schedule the 2 blocks behind each other */
214 DBG((dbg, LEVEL_1, "Coalesce (Jump) %+F -> %+F (%.3g)\n",
215 pred_entry->block, entry->block, edge->execfreq));
216 pred_entry->next = entry;
217 entry->prev = pred_entry;
220 /* run2: remaining edges */
221 for (i = 0; i < edge_count; ++i) {
222 const edge_t *edge = &env->edges[i];
223 ir_node *block = edge->block;
225 blocksched_entry_t *entry, *pred_entry;
227 /* the block might have been removed already... */
228 if (is_Bad(get_Block_cfgpred(block, 0)))
231 pred_block = get_Block_cfgpred_block(block, edge->pos);
232 entry = get_irn_link(block);
233 pred_entry = get_irn_link(pred_block);
235 /* is 1 of the blocks already attached to another block? */
236 if (pred_entry->next != NULL || entry->prev != NULL)
239 /* schedule the 2 blocks behind each other */
240 DBG((dbg, LEVEL_1, "Coalesce (CondJump) %+F -> %+F (%.3g)\n",
241 pred_entry->block, entry->block, edge->execfreq));
242 pred_entry->next = entry;
243 entry->prev = pred_entry;
247 static void pick_block_successor(blocksched_entry_t *entry, blocksched_env_t *env)
249 ir_node *block = entry->block;
250 ir_node *succ = NULL;
251 blocksched_entry_t *succ_entry;
252 const ir_edge_t *edge;
253 double best_succ_execfreq;
255 if (irn_visited(block))
259 mark_irn_visited(block);
261 DBG((dbg, LEVEL_1, "Pick succ of %+F\n", block));
263 /* put all successors into the worklist */
264 foreach_block_succ(block, edge) {
265 ir_node *succ_block = get_edge_src_irn(edge);
267 if (irn_visited(succ_block))
270 /* we only need to put the first of a series of already connected
271 * blocks into the worklist */
272 succ_entry = get_irn_link(succ_block);
273 while (succ_entry->prev != NULL) {
274 /* break cycles... */
275 if (succ_entry->prev->block == succ_block) {
276 succ_entry->prev->next = NULL;
277 succ_entry->prev = NULL;
280 succ_entry = succ_entry->prev;
283 if (irn_visited(succ_entry->block))
286 DBG((dbg, LEVEL_1, "Put %+F into worklist\n", succ_entry->block));
287 pdeq_putr(env->worklist, succ_entry->block);
290 if (entry->next != NULL) {
291 pick_block_successor(entry->next, env);
295 DBG((dbg, LEVEL_1, "deciding...\n"));
296 best_succ_execfreq = -1;
298 /* no successor yet: pick the successor block with the highest execution
299 * frequency which has no predecessor yet */
301 foreach_block_succ(block, edge) {
302 ir_node *succ_block = get_edge_src_irn(edge);
305 if (irn_visited(succ_block))
308 succ_entry = get_irn_link(succ_block);
309 if (succ_entry->prev != NULL)
312 execfreq = get_block_execfreq(env->execfreqs, succ_block);
313 if (execfreq > best_succ_execfreq) {
314 best_succ_execfreq = execfreq;
320 DBG((dbg, LEVEL_1, "pick from worklist\n"));
323 if (pdeq_empty(env->worklist)) {
324 DBG((dbg, LEVEL_1, "worklist empty\n"));
327 succ = pdeq_getl(env->worklist);
328 } while (irn_visited(succ));
331 succ_entry = get_irn_link(succ);
332 entry->next = succ_entry;
333 succ_entry->prev = entry;
335 pick_block_successor(succ_entry, env);
338 static blocksched_entry_t *finish_block_schedule(blocksched_env_t *env)
340 ir_graph *irg = env->irg;
341 ir_node *startblock = get_irg_start_block(irg);
342 blocksched_entry_t *entry = get_irn_link(startblock);
344 set_using_visited(irg);
345 inc_irg_visited(irg);
347 env->worklist = new_pdeq();
348 pick_block_successor(entry, env);
349 assert(pdeq_empty(env->worklist));
350 del_pdeq(env->worklist);
352 clear_using_visited(irg);
357 static ir_node **create_blocksched_array(blocksched_env_t *env, blocksched_entry_t *first,
358 int count, struct obstack* obst)
361 ir_node **block_list;
362 blocksched_entry_t *entry;
364 block_list = NEW_ARR_D(ir_node *, obst, count);
365 DBG((dbg, LEVEL_1, "Blockschedule:\n"));
367 for (entry = first; entry != NULL; entry = entry->next) {
369 block_list[i++] = entry->block;
370 DBG((dbg, LEVEL_1, "\t%+F\n", entry->block));
377 static ir_node **create_block_schedule_greedy(ir_graph *irg, ir_exec_freq *execfreqs)
379 blocksched_env_t env;
381 blocksched_entry_t *start_entry;
382 ir_node **block_list;
388 env.execfreqs = execfreqs;
389 env.edges = NEW_ARR_F(edge_t, 0);
393 // collect edge execution frequencies
394 irg_block_walk_graph(irg, collect_egde_frequency, NULL, &env);
396 // sort interblock edges by execution frequency
397 qsort(env.edges, ARR_LEN(env.edges), sizeof(env.edges[0]), cmp_edges);
399 (void)be_remove_empty_blocks(irg);
401 if (algo != BLOCKSCHED_NAIV)
402 coalesce_blocks(&env);
404 start_entry = finish_block_schedule(&env);
405 block_list = create_blocksched_array(&env, start_entry, env.blockcount, get_irg_obstack(irg));
407 DEL_ARR_F(env.edges);
408 obstack_free(&obst, NULL);
423 typedef struct _ilp_edge_t {
424 ir_node *block; /**< source block */
425 int pos; /**< number of cfg predecessor (target) */
429 typedef struct _blocksched_ilp_env_t {
430 blocksched_env_t env;
431 ilp_edge_t *ilpedges;
433 } blocksched_ilp_env_t;
435 typedef struct _blocksched_ilp_entry_t {
437 struct _blocksched_entry_t *next;
438 struct _blocksched_entry_t *prev;
441 } blocksched_ilp_entry_t;
443 static int add_ilp_edge(ir_node *block, int pos, double execfreq, blocksched_ilp_env_t *env)
447 int edgeidx = ARR_LEN(env->ilpedges);
449 snprintf(name, sizeof(name), "edge%d", edgeidx);
453 edge.ilpvar = lpp_add_var_default(env->lpp, name, lpp_binary, execfreq, 1.0);
455 ARR_APP1(ilp_edge_t, env->ilpedges, edge);
459 static void collect_egde_frequency_ilp(ir_node *block, void *data)
461 blocksched_ilp_env_t *env = data;
462 ir_graph *irg = env->env.irg;
463 ir_node *startblock = get_irg_start_block(irg);
468 blocksched_ilp_entry_t *entry;
470 snprintf(name, sizeof(name), "block_out_constr_%ld", get_irn_node_nr(block));
471 out_count = get_irn_n_edges_kind(block, EDGE_KIND_BLOCK);
473 entry = obstack_alloc(env->env.obst, sizeof(entry[0]));
474 entry->block = block;
477 entry->out_cst = lpp_add_cst_uniq(env->lpp, name, lpp_greater, out_count - 1);
478 set_irn_link(block, entry);
480 if (block == startblock)
483 arity = get_irn_arity(block);
485 double execfreq = get_block_execfreq(env->env.execfreqs, block);
486 add_ilp_edge(block, 0, execfreq, env);
490 int *edgenums = alloca(sizeof(edgenums[0]) * arity);
492 snprintf(name, sizeof(name), "block_in_constr_%ld", get_irn_node_nr(block));
493 cst = lpp_add_cst_uniq(env->lpp, name, lpp_greater, arity - 1);
495 for (i = 0; i < arity; ++i) {
499 ir_node *pred_block = get_Block_cfgpred_block(block, i);
501 execfreq = get_block_execfreq(env->env.execfreqs, pred_block);
502 edgenum = add_ilp_edge(block, i, execfreq, env);
503 edge = &env->ilpedges[edgenum];
504 lpp_set_factor_fast(env->lpp, cst, edge->ilpvar, 1.0);
510 static void coalesce_blocks_ilp(blocksched_ilp_env_t *env)
513 int edge_count = ARR_LEN(env->ilpedges);
515 /* complete out constraints */
516 for(i = 0; i < edge_count; ++i) {
517 const ilp_edge_t *edge = &env->ilpedges[i];
518 ir_node *block = edge->block;
520 blocksched_ilp_entry_t *entry;
522 /* the block might have been removed already... */
523 if (is_Bad(get_Block_cfgpred(block, 0)))
526 pred = get_Block_cfgpred_block(block, edge->pos);
527 entry = get_irn_link(pred);
529 DBG((dbg, LEVEL_1, "Adding out cst to %+F from %+F,%d\n",
530 pred, block, edge->pos));
531 lpp_set_factor_fast(env->lpp, entry->out_cst, edge->ilpvar, 1.0);
538 lpp_dump(env->lpp, "lpp.out");
539 snprintf(fname, sizeof(fname), "lpp_%s.plain", get_irg_dump_name(env->env.irg));
540 f = fopen(fname, "w");
541 lpp_dump_plain(env->lpp, f);
546 //lpp_solve_net(env->lpp, main_env->options->ilp_server, main_env->options->ilp_solver);
547 lpp_solve_net(env->lpp, "i44pc52", "cplex");
548 assert(lpp_is_sol_valid(env->lpp));
550 /* Apply results to edges */
551 for (i = 0; i < edge_count; ++i) {
552 const ilp_edge_t *edge = &env->ilpedges[i];
553 ir_node *block = edge->block;
556 blocksched_entry_t *entry;
557 blocksched_entry_t *pred_entry;
559 /* the block might have been removed already... */
560 if (is_Bad(get_Block_cfgpred(block, 0)))
563 is_jump = (int)lpp_get_var_sol(env->lpp, edge->ilpvar);
567 pred = get_Block_cfgpred_block(block, edge->pos);
568 entry = get_irn_link(block);
569 pred_entry = get_irn_link(pred);
571 assert(entry->prev == NULL && pred_entry->next == NULL);
572 entry->prev = pred_entry;
573 pred_entry->next = entry;
577 static ir_node **create_block_schedule_ilp(ir_graph *irg, ir_exec_freq *execfreqs)
579 blocksched_ilp_env_t env;
581 blocksched_entry_t *start_entry;
582 ir_node **block_list;
587 env.env.obst = &obst;
588 env.env.execfreqs = execfreqs;
589 env.env.worklist = NULL;
590 env.env.blockcount = 0;
591 env.ilpedges = NEW_ARR_F(ilp_edge_t, 0);
593 env.lpp = new_lpp("blockschedule", lpp_minimize);
594 lpp_set_time_limit(env.lpp, 20);
595 lpp_set_log(env.lpp, stdout);
597 irg_block_walk_graph(irg, collect_egde_frequency_ilp, NULL, &env);
599 (void)be_remove_empty_blocks(irg);
600 coalesce_blocks_ilp(&env);
602 start_entry = finish_block_schedule(&env.env);
603 block_list = create_blocksched_array(&env.env, start_entry, env.env.blockcount, get_irg_obstack(irg));
605 DEL_ARR_F(env.ilpedges);
607 obstack_free(&obst, NULL);
611 #endif /* WITH_ILP */
615 * | ____|_ _| |_| __ )| __ )
616 * | _| \ \/ / __| _ \| _ \
617 * | |___ > <| |_| |_) | |_) |
618 * |_____/_/\_\\__|____/|____/
622 /** A simple forward single linked list. */
624 ir_node *start; /**< start of the list */
625 ir_node *end; /**< last block in the list */
626 unsigned n_blks; /**< number of blocks in the list */
629 static void add_block(anchor *list, ir_node *block) {
630 if (list->start == NULL) {
634 set_irn_link(list->end, block);
641 static void create_block_list(ir_node *leader_block, anchor *list) {
643 const ir_edge_t *edge;
644 ir_node *block = NULL;
645 ir_extblk *extbb = get_Block_extbb(leader_block);
647 if (extbb_visited(extbb))
649 mark_extbb_visited(extbb);
651 for (i = 0; i < get_extbb_n_blocks(extbb); ++i) {
652 block = get_extbb_block(extbb, i);
653 add_block(list, block);
656 assert(block != NULL);
658 /* pick successor extbbs */
659 foreach_block_succ(block, edge) {
660 ir_node *succ = get_edge_src_irn(edge);
661 create_block_list(succ, list);
664 for (i = 0; i < get_extbb_n_blocks(extbb) - 1; ++i) {
665 block = get_extbb_block(extbb, i);
667 foreach_block_succ(block, edge) {
668 ir_node *succ = get_edge_src_irn(edge);
669 create_block_list(succ, list);
674 void compute_extbb_execfreqs(ir_graph *irg, ir_exec_freq *execfreqs);
677 * Calculates a block schedule. The schedule is stored as a linked
678 * list starting at the start_block of the irg.
680 static ir_node **create_extbb_block_schedule(ir_graph *irg, ir_exec_freq *execfreqs)
683 ir_node **blk_list, *b, *n;
686 /* schedule extended basic blocks */
687 compute_extbb_execfreqs(irg, execfreqs);
688 //compute_extbb(irg);
694 set_using_irn_link(irg);
695 set_using_visited(irg);
696 inc_irg_block_visited(irg);
698 create_block_list(get_irg_start_block(irg), &list);
700 /** create an array, so we can go forward and backward */
701 blk_list = NEW_ARR_D(ir_node *, irg->obst,list.n_blks);
703 for (i = 0, b = list.start; b; b = n, ++i) {
708 clear_using_irn_link(irg);
709 clear_using_visited(irg);
717 * | |\/| |/ _` | | '_ \
718 * | | | | (_| | | | | |
719 * |_| |_|\__,_|_|_| |_|
722 void be_init_blocksched(void)
724 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
725 lc_opt_entry_t *blocksched_grp = lc_opt_get_grp(be_grp, "blocksched");
727 lc_opt_add_table(blocksched_grp, be_blocksched_options);
729 FIRM_DBG_REGISTER(dbg, "firm.be.blocksched");
732 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_blocksched);
734 ir_node **be_create_block_schedule(ir_graph *irg, ir_exec_freq *execfreqs)
737 case BLOCKSCHED_GREEDY:
738 case BLOCKSCHED_NAIV:
739 return create_block_schedule_greedy(irg, execfreqs);
740 case BLOCKSCHED_EXTBB:
741 return create_extbb_block_schedule(irg, execfreqs);
744 return create_block_schedule_ilp(irg, execfreqs);
745 #endif /* WITH_ILP */
748 assert(0 && "unknown blocksched algo");