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 wether 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;
366 block_list = NEW_ARR_D(ir_node *, obst, count);
367 DBG((dbg, LEVEL_1, "Blockschedule:\n"));
369 for (entry = first; entry != NULL; entry = entry->next) {
371 block_list[i++] = entry->block;
372 DBG((dbg, LEVEL_1, "\t%+F\n", entry->block));
379 static ir_node **create_block_schedule_greedy(ir_graph *irg, ir_exec_freq *execfreqs)
381 blocksched_env_t env;
383 blocksched_entry_t *start_entry;
384 ir_node **block_list;
390 env.execfreqs = execfreqs;
391 env.edges = NEW_ARR_F(edge_t, 0);
395 // collect edge execution frequencies
396 irg_block_walk_graph(irg, collect_egde_frequency, NULL, &env);
398 // sort interblock edges by execution frequency
399 qsort(env.edges, ARR_LEN(env.edges), sizeof(env.edges[0]), cmp_edges);
401 (void)be_remove_empty_blocks(irg);
403 if (algo != BLOCKSCHED_NAIV)
404 coalesce_blocks(&env);
406 start_entry = finish_block_schedule(&env);
407 block_list = create_blocksched_array(&env, start_entry, env.blockcount, get_irg_obstack(irg));
409 DEL_ARR_F(env.edges);
410 obstack_free(&obst, NULL);
425 typedef struct _ilp_edge_t {
426 ir_node *block; /**< source block */
427 int pos; /**< number of cfg predecessor (target) */
431 typedef struct _blocksched_ilp_env_t {
432 blocksched_env_t env;
433 ilp_edge_t *ilpedges;
435 } blocksched_ilp_env_t;
437 typedef struct _blocksched_ilp_entry_t {
439 struct _blocksched_entry_t *next;
440 struct _blocksched_entry_t *prev;
443 } blocksched_ilp_entry_t;
445 static int add_ilp_edge(ir_node *block, int pos, double execfreq, blocksched_ilp_env_t *env)
449 int edgeidx = ARR_LEN(env->ilpedges);
451 snprintf(name, sizeof(name), "edge%d", edgeidx);
455 edge.ilpvar = lpp_add_var_default(env->lpp, name, lpp_binary, execfreq, 1.0);
457 ARR_APP1(ilp_edge_t, env->ilpedges, edge);
461 static void collect_egde_frequency_ilp(ir_node *block, void *data)
463 blocksched_ilp_env_t *env = data;
464 ir_graph *irg = env->env.irg;
465 ir_node *startblock = get_irg_start_block(irg);
470 blocksched_ilp_entry_t *entry;
472 snprintf(name, sizeof(name), "block_out_constr_%ld", get_irn_node_nr(block));
473 out_count = get_irn_n_edges_kind(block, EDGE_KIND_BLOCK);
475 entry = obstack_alloc(env->env.obst, sizeof(entry[0]));
476 entry->block = block;
479 entry->out_cst = lpp_add_cst_uniq(env->lpp, name, lpp_greater, out_count - 1);
480 set_irn_link(block, entry);
482 if (block == startblock)
485 arity = get_irn_arity(block);
487 double execfreq = get_block_execfreq(env->env.execfreqs, block);
488 add_ilp_edge(block, 0, execfreq, env);
492 int *edgenums = alloca(sizeof(edgenums[0]) * arity);
494 snprintf(name, sizeof(name), "block_in_constr_%ld", get_irn_node_nr(block));
495 cst = lpp_add_cst_uniq(env->lpp, name, lpp_greater, arity - 1);
497 for (i = 0; i < arity; ++i) {
501 ir_node *pred_block = get_Block_cfgpred_block(block, i);
503 execfreq = get_block_execfreq(env->env.execfreqs, pred_block);
504 edgenum = add_ilp_edge(block, i, execfreq, env);
505 edge = &env->ilpedges[edgenum];
506 lpp_set_factor_fast(env->lpp, cst, edge->ilpvar, 1.0);
512 static void coalesce_blocks_ilp(blocksched_ilp_env_t *env)
515 int edge_count = ARR_LEN(env->ilpedges);
517 /* complete out constraints */
518 for(i = 0; i < edge_count; ++i) {
519 const ilp_edge_t *edge = &env->ilpedges[i];
520 ir_node *block = edge->block;
522 blocksched_ilp_entry_t *entry;
524 /* the block might have been removed already... */
525 if (is_Bad(get_Block_cfgpred(block, 0)))
528 pred = get_Block_cfgpred_block(block, edge->pos);
529 entry = get_irn_link(pred);
531 DBG((dbg, LEVEL_1, "Adding out cst to %+F from %+F,%d\n",
532 pred, block, edge->pos));
533 lpp_set_factor_fast(env->lpp, entry->out_cst, edge->ilpvar, 1.0);
540 lpp_dump(env->lpp, "lpp.out");
541 snprintf(fname, sizeof(fname), "lpp_%s.plain", get_irg_dump_name(env->env.irg));
542 f = fopen(fname, "w");
543 lpp_dump_plain(env->lpp, f);
548 //lpp_solve_net(env->lpp, main_env->options->ilp_server, main_env->options->ilp_solver);
549 lpp_solve_net(env->lpp, "i44pc52", "cplex");
550 assert(lpp_is_sol_valid(env->lpp));
552 /* Apply results to edges */
553 for (i = 0; i < edge_count; ++i) {
554 const ilp_edge_t *edge = &env->ilpedges[i];
555 ir_node *block = edge->block;
558 blocksched_entry_t *entry;
559 blocksched_entry_t *pred_entry;
561 /* the block might have been removed already... */
562 if (is_Bad(get_Block_cfgpred(block, 0)))
565 is_jump = (int)lpp_get_var_sol(env->lpp, edge->ilpvar);
569 pred = get_Block_cfgpred_block(block, edge->pos);
570 entry = get_irn_link(block);
571 pred_entry = get_irn_link(pred);
573 assert(entry->prev == NULL && pred_entry->next == NULL);
574 entry->prev = pred_entry;
575 pred_entry->next = entry;
579 static ir_node **create_block_schedule_ilp(ir_graph *irg, ir_exec_freq *execfreqs)
581 blocksched_ilp_env_t env;
583 blocksched_entry_t *start_entry;
584 ir_node **block_list;
589 env.env.obst = &obst;
590 env.env.execfreqs = execfreqs;
591 env.env.worklist = NULL;
592 env.env.blockcount = 0;
593 env.ilpedges = NEW_ARR_F(ilp_edge_t, 0);
595 env.lpp = new_lpp("blockschedule", lpp_minimize);
596 lpp_set_time_limit(env.lpp, 20);
597 lpp_set_log(env.lpp, stdout);
599 irg_block_walk_graph(irg, collect_egde_frequency_ilp, NULL, &env);
601 (void)be_remove_empty_blocks(irg);
602 coalesce_blocks_ilp(&env);
604 start_entry = finish_block_schedule(&env.env);
605 block_list = create_blocksched_array(&env.env, start_entry, env.env.blockcount, get_irg_obstack(irg));
607 DEL_ARR_F(env.ilpedges);
609 obstack_free(&obst, NULL);
613 #endif /* WITH_ILP */
617 * | ____|_ _| |_| __ )| __ )
618 * | _| \ \/ / __| _ \| _ \
619 * | |___ > <| |_| |_) | |_) |
620 * |_____/_/\_\\__|____/|____/
624 /** A simple forward single linked list. */
626 ir_node *start; /**< start of the list */
627 ir_node *end; /**< last block in the list */
628 unsigned n_blks; /**< number of blocks in the list */
631 static void add_block(anchor *list, ir_node *block) {
632 if (list->start == NULL) {
636 set_irn_link(list->end, block);
643 static void create_block_list(ir_node *leader_block, anchor *list) {
645 const ir_edge_t *edge;
646 ir_node *block = NULL;
647 ir_extblk *extbb = get_Block_extbb(leader_block);
649 if (extbb_visited(extbb))
651 mark_extbb_visited(extbb);
653 for (i = 0; i < get_extbb_n_blocks(extbb); ++i) {
654 block = get_extbb_block(extbb, i);
655 add_block(list, block);
658 assert(block != NULL);
660 /* pick successor extbbs */
661 foreach_block_succ(block, edge) {
662 ir_node *succ = get_edge_src_irn(edge);
663 create_block_list(succ, list);
666 for (i = 0; i < get_extbb_n_blocks(extbb) - 1; ++i) {
667 block = get_extbb_block(extbb, i);
669 foreach_block_succ(block, edge) {
670 ir_node *succ = get_edge_src_irn(edge);
671 create_block_list(succ, list);
676 void compute_extbb_execfreqs(ir_graph *irg, ir_exec_freq *execfreqs);
679 * Calculates a block schedule. The schedule is stored as a linked
680 * list starting at the start_block of the irg.
682 static ir_node **create_extbb_block_schedule(ir_graph *irg, ir_exec_freq *execfreqs)
685 ir_node **blk_list, *b, *n;
688 /* schedule extended basic blocks */
689 compute_extbb_execfreqs(irg, execfreqs);
690 //compute_extbb(irg);
696 set_using_irn_link(irg);
697 set_using_visited(irg);
698 inc_irg_block_visited(irg);
700 create_block_list(get_irg_start_block(irg), &list);
702 /** create an array, so we can go forward and backward */
703 blk_list = NEW_ARR_D(ir_node *, irg->obst,list.n_blks);
705 for (i = 0, b = list.start; b; b = n, ++i) {
710 clear_using_irn_link(irg);
711 clear_using_visited(irg);
719 * | |\/| |/ _` | | '_ \
720 * | | | | (_| | | | | |
721 * |_| |_|\__,_|_|_| |_|
724 void be_init_blocksched(void)
726 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
727 lc_opt_entry_t *blocksched_grp = lc_opt_get_grp(be_grp, "blocksched");
729 lc_opt_add_table(blocksched_grp, be_blocksched_options);
731 FIRM_DBG_REGISTER(dbg, "firm.be.blocksched");
734 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_blocksched);
736 ir_node **be_create_block_schedule(ir_graph *irg, ir_exec_freq *execfreqs)
739 case BLOCKSCHED_GREEDY:
740 case BLOCKSCHED_NAIV:
741 return create_block_schedule_greedy(irg, execfreqs);
742 case BLOCKSCHED_EXTBB:
743 return create_extbb_block_schedule(irg, execfreqs);
746 return create_block_schedule_ilp(irg, execfreqs);
747 #endif /* WITH_ILP */
750 assert(0 && "unknown blocksched algo");