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"
41 #include "irgraph_t.h"
52 #include <libcore/lc_opts.h>
53 #include <libcore/lc_opts_enum.h>
54 #include <libcore/lc_timing.h>
58 #include <lpp/lpp_net.h>
61 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
63 typedef enum _blocksched_algos_t {
64 BLOCKSCHED_NAIV, BLOCKSCHED_EXTBB, BLOCKSCHED_GREEDY, BLOCKSCHED_ILP
67 static int algo = BLOCKSCHED_GREEDY;
69 static const lc_opt_enum_int_items_t blockschedalgo_items[] = {
70 { "naiv", BLOCKSCHED_NAIV },
71 { "extbb", BLOCKSCHED_EXTBB },
72 { "greedy", BLOCKSCHED_GREEDY },
74 { "ilp", BLOCKSCHED_ILP },
79 static lc_opt_enum_int_var_t algo_var = {
80 &algo, blockschedalgo_items
83 static const lc_opt_table_entry_t be_blocksched_options[] = {
84 LC_OPT_ENT_ENUM_INT ("algo", "the block scheduling algorithm", &algo_var),
90 * / ___|_ __ ___ ___ __| |_ _
91 * | | _| '__/ _ \/ _ \/ _` | | | |
92 * | |_| | | | __/ __/ (_| | |_| |
93 * \____|_| \___|\___|\__,_|\__, |
97 typedef struct _blocksched_entry_t {
99 struct _blocksched_entry_t *next;
100 struct _blocksched_entry_t *prev;
101 } blocksched_entry_t;
103 typedef struct _edge_t {
104 ir_node *block; /**< source block */
105 int pos; /**< number of cfg predecessor (target) */
106 double execfreq; /**< the frequency */
107 int highest_execfreq; /**< flag that indicates whether this edge is the edge with the highest
108 execfreq pointing away from this block */
111 typedef struct _blocksched_env_t {
113 struct obstack *obst;
114 ir_exec_freq *execfreqs;
121 * Collect cfg frequencies of all edges between blocks.
122 * Also determines edge with highest frequency.
124 static void collect_egde_frequency(ir_node *block, void *data)
126 blocksched_env_t *env = data;
129 blocksched_entry_t *entry;
131 entry = obstack_alloc(env->obst, sizeof(entry[0]));
132 entry->block = block;
135 set_irn_link(block, entry);
137 if (block == get_irg_start_block(env->irg))
140 arity = get_Block_n_cfgpreds(block);
145 edge.execfreq = get_block_execfreq(env->execfreqs, block);
146 edge.highest_execfreq = 1;
147 ARR_APP1(edge_t, env->edges, edge);
150 double highest_execfreq = -1.0;
151 int highest_edge_num = -1;
154 for (i = 0; i < arity; ++i) {
156 ir_node *pred_block = get_Block_cfgpred_block(block, i);
158 execfreq = get_block_execfreq(env->execfreqs, pred_block);
161 edge.execfreq = execfreq;
162 edge.highest_execfreq = 0;
163 ARR_APP1(edge_t, env->edges, edge);
165 if (execfreq > highest_execfreq) {
166 highest_execfreq = execfreq;
167 highest_edge_num = ARR_LEN(env->edges) - 1;
171 if(highest_edge_num >= 0)
172 env->edges[highest_edge_num].highest_execfreq = 1;
176 static int cmp_edges(const void *d1, const void *d2)
178 const edge_t *e1 = d1;
179 const edge_t *e2 = d2;
181 return QSORT_CMP(e2->execfreq, e1->execfreq);
184 static void coalesce_blocks(blocksched_env_t *env)
187 int edge_count = ARR_LEN(env->edges);
189 /* run1: only look at jumps */
190 for (i = 0; i < edge_count; ++i) {
191 const edge_t *edge = &env->edges[i];
192 ir_node *block = edge->block;
194 blocksched_entry_t *entry, *pred_entry;
196 /* the block might have been removed already... */
197 if (is_Bad(get_Block_cfgpred(block, 0)))
200 /* only check edge with highest frequency */
201 if (! edge->highest_execfreq)
204 pred_block = get_Block_cfgpred_block(block, edge->pos);
205 entry = get_irn_link(block);
206 pred_entry = get_irn_link(pred_block);
208 if (pred_entry->next != NULL || entry->prev != NULL)
211 /* only coalesce jumps */
212 if (get_block_succ_next(pred_block, get_block_succ_first(pred_block)) != NULL)
215 /* schedule the 2 blocks behind each other */
216 DBG((dbg, LEVEL_1, "Coalesce (Jump) %+F -> %+F (%.3g)\n",
217 pred_entry->block, entry->block, edge->execfreq));
218 pred_entry->next = entry;
219 entry->prev = pred_entry;
222 /* run2: remaining edges */
223 for (i = 0; i < edge_count; ++i) {
224 const edge_t *edge = &env->edges[i];
225 ir_node *block = edge->block;
227 blocksched_entry_t *entry, *pred_entry;
229 /* the block might have been removed already... */
230 if (is_Bad(get_Block_cfgpred(block, 0)))
233 pred_block = get_Block_cfgpred_block(block, edge->pos);
234 entry = get_irn_link(block);
235 pred_entry = get_irn_link(pred_block);
237 /* is 1 of the blocks already attached to another block? */
238 if (pred_entry->next != NULL || entry->prev != NULL)
241 /* schedule the 2 blocks behind each other */
242 DBG((dbg, LEVEL_1, "Coalesce (CondJump) %+F -> %+F (%.3g)\n",
243 pred_entry->block, entry->block, edge->execfreq));
244 pred_entry->next = entry;
245 entry->prev = pred_entry;
249 static void pick_block_successor(blocksched_entry_t *entry, blocksched_env_t *env)
251 ir_node *block = entry->block;
252 ir_node *succ = NULL;
253 blocksched_entry_t *succ_entry;
254 const ir_edge_t *edge;
255 double best_succ_execfreq;
257 if (irn_visited(block))
261 mark_irn_visited(block);
263 DBG((dbg, LEVEL_1, "Pick succ of %+F\n", block));
265 /* put all successors into the worklist */
266 foreach_block_succ(block, edge) {
267 ir_node *succ_block = get_edge_src_irn(edge);
269 if (irn_visited(succ_block))
272 /* we only need to put the first of a series of already connected
273 * blocks into the worklist */
274 succ_entry = get_irn_link(succ_block);
275 while (succ_entry->prev != NULL) {
276 /* break cycles... */
277 if (succ_entry->prev->block == succ_block) {
278 succ_entry->prev->next = NULL;
279 succ_entry->prev = NULL;
282 succ_entry = succ_entry->prev;
285 if (irn_visited(succ_entry->block))
288 DBG((dbg, LEVEL_1, "Put %+F into worklist\n", succ_entry->block));
289 pdeq_putr(env->worklist, succ_entry->block);
292 if (entry->next != NULL) {
293 pick_block_successor(entry->next, env);
297 DBG((dbg, LEVEL_1, "deciding...\n"));
298 best_succ_execfreq = -1;
300 /* no successor yet: pick the successor block with the highest execution
301 * frequency which has no predecessor yet */
303 foreach_block_succ(block, edge) {
304 ir_node *succ_block = get_edge_src_irn(edge);
307 if (irn_visited(succ_block))
310 succ_entry = get_irn_link(succ_block);
311 if (succ_entry->prev != NULL)
314 execfreq = get_block_execfreq(env->execfreqs, succ_block);
315 if (execfreq > best_succ_execfreq) {
316 best_succ_execfreq = execfreq;
322 DBG((dbg, LEVEL_1, "pick from worklist\n"));
325 if (pdeq_empty(env->worklist)) {
326 DBG((dbg, LEVEL_1, "worklist empty\n"));
329 succ = pdeq_getl(env->worklist);
330 } while (irn_visited(succ));
333 succ_entry = get_irn_link(succ);
334 entry->next = succ_entry;
335 succ_entry->prev = entry;
337 pick_block_successor(succ_entry, env);
340 static blocksched_entry_t *finish_block_schedule(blocksched_env_t *env)
342 ir_graph *irg = env->irg;
343 ir_node *startblock = get_irg_start_block(irg);
344 blocksched_entry_t *entry = get_irn_link(startblock);
346 set_using_visited(irg);
347 inc_irg_visited(irg);
349 env->worklist = new_pdeq();
350 pick_block_successor(entry, env);
351 assert(pdeq_empty(env->worklist));
352 del_pdeq(env->worklist);
354 clear_using_visited(irg);
359 static ir_node **create_blocksched_array(blocksched_env_t *env, blocksched_entry_t *first,
360 int count, struct obstack* obst)
363 ir_node **block_list;
364 blocksched_entry_t *entry;
367 block_list = NEW_ARR_D(ir_node *, obst, count);
368 DBG((dbg, LEVEL_1, "Blockschedule:\n"));
370 for (entry = first; entry != NULL; entry = entry->next) {
372 block_list[i++] = entry->block;
373 DBG((dbg, LEVEL_1, "\t%+F\n", entry->block));
380 static ir_node **create_block_schedule_greedy(ir_graph *irg, ir_exec_freq *execfreqs)
382 blocksched_env_t env;
384 blocksched_entry_t *start_entry;
385 ir_node **block_list;
391 env.execfreqs = execfreqs;
392 env.edges = NEW_ARR_F(edge_t, 0);
396 // collect edge execution frequencies
397 irg_block_walk_graph(irg, collect_egde_frequency, NULL, &env);
399 // sort interblock edges by execution frequency
400 qsort(env.edges, ARR_LEN(env.edges), sizeof(env.edges[0]), cmp_edges);
402 (void)be_remove_empty_blocks(irg);
404 if (algo != BLOCKSCHED_NAIV)
405 coalesce_blocks(&env);
407 start_entry = finish_block_schedule(&env);
408 block_list = create_blocksched_array(&env, start_entry, env.blockcount, get_irg_obstack(irg));
410 DEL_ARR_F(env.edges);
411 obstack_free(&obst, NULL);
426 typedef struct _ilp_edge_t {
427 ir_node *block; /**< source block */
428 int pos; /**< number of cfg predecessor (target) */
432 typedef struct _blocksched_ilp_env_t {
433 blocksched_env_t env;
434 ilp_edge_t *ilpedges;
436 } blocksched_ilp_env_t;
438 typedef struct _blocksched_ilp_entry_t {
440 struct _blocksched_entry_t *next;
441 struct _blocksched_entry_t *prev;
444 } blocksched_ilp_entry_t;
446 static int add_ilp_edge(ir_node *block, int pos, double execfreq, blocksched_ilp_env_t *env)
450 int edgeidx = ARR_LEN(env->ilpedges);
452 snprintf(name, sizeof(name), "edge%d", edgeidx);
456 edge.ilpvar = lpp_add_var_default(env->lpp, name, lpp_binary, execfreq, 1.0);
458 ARR_APP1(ilp_edge_t, env->ilpedges, edge);
462 static void collect_egde_frequency_ilp(ir_node *block, void *data)
464 blocksched_ilp_env_t *env = data;
465 ir_graph *irg = env->env.irg;
466 ir_node *startblock = get_irg_start_block(irg);
471 blocksched_ilp_entry_t *entry;
473 snprintf(name, sizeof(name), "block_out_constr_%ld", get_irn_node_nr(block));
474 out_count = get_irn_n_edges_kind(block, EDGE_KIND_BLOCK);
476 entry = obstack_alloc(env->env.obst, sizeof(entry[0]));
477 entry->block = block;
480 entry->out_cst = lpp_add_cst_uniq(env->lpp, name, lpp_greater, out_count - 1);
481 set_irn_link(block, entry);
483 if (block == startblock)
486 arity = get_irn_arity(block);
488 double execfreq = get_block_execfreq(env->env.execfreqs, block);
489 add_ilp_edge(block, 0, execfreq, env);
493 int *edgenums = alloca(sizeof(edgenums[0]) * arity);
495 snprintf(name, sizeof(name), "block_in_constr_%ld", get_irn_node_nr(block));
496 cst = lpp_add_cst_uniq(env->lpp, name, lpp_greater, arity - 1);
498 for (i = 0; i < arity; ++i) {
502 ir_node *pred_block = get_Block_cfgpred_block(block, i);
504 execfreq = get_block_execfreq(env->env.execfreqs, pred_block);
505 edgenum = add_ilp_edge(block, i, execfreq, env);
506 edge = &env->ilpedges[edgenum];
507 lpp_set_factor_fast(env->lpp, cst, edge->ilpvar, 1.0);
513 static void coalesce_blocks_ilp(blocksched_ilp_env_t *env)
516 int edge_count = ARR_LEN(env->ilpedges);
518 /* complete out constraints */
519 for(i = 0; i < edge_count; ++i) {
520 const ilp_edge_t *edge = &env->ilpedges[i];
521 ir_node *block = edge->block;
523 blocksched_ilp_entry_t *entry;
525 /* the block might have been removed already... */
526 if (is_Bad(get_Block_cfgpred(block, 0)))
529 pred = get_Block_cfgpred_block(block, edge->pos);
530 entry = get_irn_link(pred);
532 DBG((dbg, LEVEL_1, "Adding out cst to %+F from %+F,%d\n",
533 pred, block, edge->pos));
534 lpp_set_factor_fast(env->lpp, entry->out_cst, edge->ilpvar, 1.0);
541 lpp_dump(env->lpp, "lpp.out");
542 snprintf(fname, sizeof(fname), "lpp_%s.plain", get_irg_dump_name(env->env.irg));
543 f = fopen(fname, "w");
544 lpp_dump_plain(env->lpp, f);
549 //lpp_solve_net(env->lpp, main_env->options->ilp_server, main_env->options->ilp_solver);
550 lpp_solve_net(env->lpp, "i44pc52", "cplex");
551 assert(lpp_is_sol_valid(env->lpp));
553 /* Apply results to edges */
554 for (i = 0; i < edge_count; ++i) {
555 const ilp_edge_t *edge = &env->ilpedges[i];
556 ir_node *block = edge->block;
559 blocksched_entry_t *entry;
560 blocksched_entry_t *pred_entry;
562 /* the block might have been removed already... */
563 if (is_Bad(get_Block_cfgpred(block, 0)))
566 is_jump = (int)lpp_get_var_sol(env->lpp, edge->ilpvar);
570 pred = get_Block_cfgpred_block(block, edge->pos);
571 entry = get_irn_link(block);
572 pred_entry = get_irn_link(pred);
574 assert(entry->prev == NULL && pred_entry->next == NULL);
575 entry->prev = pred_entry;
576 pred_entry->next = entry;
580 static ir_node **create_block_schedule_ilp(ir_graph *irg, ir_exec_freq *execfreqs)
582 blocksched_ilp_env_t env;
584 blocksched_entry_t *start_entry;
585 ir_node **block_list;
590 env.env.obst = &obst;
591 env.env.execfreqs = execfreqs;
592 env.env.worklist = NULL;
593 env.env.blockcount = 0;
594 env.ilpedges = NEW_ARR_F(ilp_edge_t, 0);
596 env.lpp = new_lpp("blockschedule", lpp_minimize);
597 lpp_set_time_limit(env.lpp, 20);
598 lpp_set_log(env.lpp, stdout);
600 irg_block_walk_graph(irg, collect_egde_frequency_ilp, NULL, &env);
602 (void)be_remove_empty_blocks(irg);
603 coalesce_blocks_ilp(&env);
605 start_entry = finish_block_schedule(&env.env);
606 block_list = create_blocksched_array(&env.env, start_entry, env.env.blockcount, get_irg_obstack(irg));
608 DEL_ARR_F(env.ilpedges);
610 obstack_free(&obst, NULL);
614 #endif /* WITH_ILP */
618 * | ____|_ _| |_| __ )| __ )
619 * | _| \ \/ / __| _ \| _ \
620 * | |___ > <| |_| |_) | |_) |
621 * |_____/_/\_\\__|____/|____/
625 /** A simple forward single linked list. */
627 ir_node *start; /**< start of the list */
628 ir_node *end; /**< last block in the list */
629 unsigned n_blks; /**< number of blocks in the list */
632 static void add_block(anchor *list, ir_node *block) {
633 if (list->start == NULL) {
637 set_irn_link(list->end, block);
644 static void create_block_list(ir_node *leader_block, anchor *list) {
646 const ir_edge_t *edge;
647 ir_node *block = NULL;
648 ir_extblk *extbb = get_Block_extbb(leader_block);
650 if (extbb_visited(extbb))
652 mark_extbb_visited(extbb);
654 for (i = 0; i < get_extbb_n_blocks(extbb); ++i) {
655 block = get_extbb_block(extbb, i);
656 add_block(list, block);
659 assert(block != NULL);
661 /* pick successor extbbs */
662 foreach_block_succ(block, edge) {
663 ir_node *succ = get_edge_src_irn(edge);
664 create_block_list(succ, list);
667 for (i = 0; i < get_extbb_n_blocks(extbb) - 1; ++i) {
668 block = get_extbb_block(extbb, i);
670 foreach_block_succ(block, edge) {
671 ir_node *succ = get_edge_src_irn(edge);
672 create_block_list(succ, list);
677 void compute_extbb_execfreqs(ir_graph *irg, ir_exec_freq *execfreqs);
680 * Calculates a block schedule. The schedule is stored as a linked
681 * list starting at the start_block of the irg.
683 static ir_node **create_extbb_block_schedule(ir_graph *irg, ir_exec_freq *execfreqs)
686 ir_node **blk_list, *b, *n;
689 /* schedule extended basic blocks */
690 compute_extbb_execfreqs(irg, execfreqs);
691 //compute_extbb(irg);
697 set_using_irn_link(irg);
698 set_using_visited(irg);
699 inc_irg_block_visited(irg);
701 create_block_list(get_irg_start_block(irg), &list);
703 /** create an array, so we can go forward and backward */
704 blk_list = NEW_ARR_D(ir_node *, irg->obst,list.n_blks);
706 for (i = 0, b = list.start; b; b = n, ++i) {
711 clear_using_irn_link(irg);
712 clear_using_visited(irg);
720 * | |\/| |/ _` | | '_ \
721 * | | | | (_| | | | | |
722 * |_| |_|\__,_|_|_| |_|
725 void be_init_blocksched(void)
727 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
728 lc_opt_entry_t *blocksched_grp = lc_opt_get_grp(be_grp, "blocksched");
730 lc_opt_add_table(blocksched_grp, be_blocksched_options);
732 FIRM_DBG_REGISTER(dbg, "firm.be.blocksched");
735 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_blocksched);
737 ir_node **be_create_block_schedule(ir_graph *irg, ir_exec_freq *execfreqs)
740 case BLOCKSCHED_GREEDY:
741 case BLOCKSCHED_NAIV:
742 return create_block_schedule_greedy(irg, execfreqs);
743 case BLOCKSCHED_EXTBB:
744 return create_extbb_block_schedule(irg, execfreqs);
747 return create_block_schedule_ilp(irg, execfreqs);
748 #endif /* WITH_ILP */
751 assert(0 && "unknown blocksched algo");