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
26 * The goals of the greedy (and ILP) algorithm here works by assuming that
27 * we want to change as many jumps to fallthroughs as possible (executed jumps
28 * actually, we have to look at the execution frequencies). The algorithms
29 * do this by collecting execution frequencies of all branches (which is easily
30 * possible when all critical edges are split) then removes critical edges where
31 * possible as we don't need and want them anymore now. The algorithms then try
32 * to change as many edges to fallthroughs as possible, this is done by setting
33 * a next and prev pointers on blocks. The greedy algorithm sorts the edges by
34 * execution frequencies and tries to transform them to fallthroughs in this order
38 #include "beblocksched.h"
48 #include "irgraph_t.h"
62 #include "lc_opts_enum.h"
67 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
69 typedef enum blocksched_algos_t {
70 BLOCKSCHED_NAIV, BLOCKSCHED_GREEDY, BLOCKSCHED_ILP
73 static int algo = BLOCKSCHED_GREEDY;
75 static const lc_opt_enum_int_items_t blockschedalgo_items[] = {
76 { "naiv", BLOCKSCHED_NAIV },
77 { "greedy", BLOCKSCHED_GREEDY },
78 { "ilp", BLOCKSCHED_ILP },
82 static lc_opt_enum_int_var_t algo_var = {
83 &algo, blockschedalgo_items
86 static const lc_opt_table_entry_t be_blocksched_options[] = {
87 LC_OPT_ENT_ENUM_INT ("blockscheduler", "the block scheduling algorithm", &algo_var),
93 * / ___|_ __ ___ ___ __| |_ _
94 * | | _| '__/ _ \/ _ \/ _` | | | |
95 * | |_| | | | __/ __/ (_| | |_| |
96 * \____|_| \___|\___|\__,_|\__, |
100 typedef struct blocksched_entry_t blocksched_entry_t;
101 struct blocksched_entry_t {
103 blocksched_entry_t *next;
104 blocksched_entry_t *prev;
107 typedef struct edge_t edge_t;
109 ir_node *block; /**< source block */
110 int pos; /**< number of cfg predecessor (target) */
111 double execfreq; /**< the frequency */
112 double outedge_penalty_freq; /**< for edges leaving the loop this is the
113 penality when we make them a
115 int highest_execfreq; /**< flag that indicates whether this edge is
116 the edge with the highest execfreq pointing
117 away from this block */
120 typedef struct blocksched_env_t blocksched_env_t;
121 struct blocksched_env_t {
129 static blocksched_entry_t* get_blocksched_entry(const ir_node *block)
131 return (blocksched_entry_t*)get_irn_link(block);
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 = (blocksched_env_t*)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(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(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(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_base(const edge_t *e1, const edge_t *e2)
209 long nr1 = get_irn_node_nr(e1->block);
210 long nr2 = get_irn_node_nr(e2->block);
213 } else if (nr1 > nr2) {
216 if (e1->pos < e2->pos) {
218 } else if (e1->pos > e2->pos) {
226 static int cmp_edges(const void *d1, const void *d2)
228 const edge_t *e1 = (const edge_t*)d1;
229 const edge_t *e2 = (const edge_t*)d2;
230 double freq1 = e1->execfreq;
231 double freq2 = e2->execfreq;
234 } else if (freq1 > freq2) {
237 return cmp_edges_base(e1, e2);
241 static int cmp_edges_outedge_penalty(const void *d1, const void *d2)
243 const edge_t *e1 = (const edge_t*)d1;
244 const edge_t *e2 = (const edge_t*)d2;
245 double pen1 = e1->outedge_penalty_freq;
246 double pen2 = e2->outedge_penalty_freq;
249 } else if (pen1 < pen2) {
252 return cmp_edges_base(e1, e2);
256 static void clear_loop_links(ir_loop *loop)
260 set_loop_link(loop, NULL);
261 n = get_loop_n_elements(loop);
262 for (i = 0; i < n; ++i) {
263 loop_element elem = get_loop_element(loop, i);
264 if (*elem.kind == k_ir_loop) {
265 clear_loop_links(elem.son);
270 static void coalesce_blocks(blocksched_env_t *env)
273 int edge_count = ARR_LEN(env->edges);
274 edge_t *edges = env->edges;
276 /* sort interblock edges by execution frequency */
277 qsort(edges, ARR_LEN(edges), sizeof(edges[0]), cmp_edges);
279 /* run1: only look at jumps */
280 for (i = 0; i < edge_count; ++i) {
281 const edge_t *edge = &edges[i];
282 ir_node *block = edge->block;
285 blocksched_entry_t *entry, *pred_entry;
287 /* only check edge with highest frequency */
288 if (! edge->highest_execfreq)
291 /* the block might have been removed already... */
292 if (is_Bad(get_Block_cfgpred(block, 0)))
295 pred_block = get_Block_cfgpred_block(block, pos);
296 entry = get_blocksched_entry(block);
297 pred_entry = get_blocksched_entry(pred_block);
299 if (pred_entry->next != NULL || entry->prev != NULL)
302 /* only coalesce jumps */
303 if (get_block_succ_next(pred_block, get_block_succ_first(pred_block)) != NULL)
306 /* schedule the 2 blocks behind each other */
307 DB((dbg, LEVEL_1, "Coalesce (Jump) %+F -> %+F (%.3g)\n",
308 pred_entry->block, entry->block, edge->execfreq));
309 pred_entry->next = entry;
310 entry->prev = pred_entry;
313 /* run2: pick loop fallthroughs */
314 clear_loop_links(get_irg_loop(env->irg));
316 qsort(edges, ARR_LEN(edges), sizeof(edges[0]), cmp_edges_outedge_penalty);
317 for (i = 0; i < edge_count; ++i) {
318 const edge_t *edge = &edges[i];
319 ir_node *block = edge->block;
322 blocksched_entry_t *entry, *pred_entry;
326 /* already seen all loop outedges? */
327 if (edge->outedge_penalty_freq == 0)
330 /* the block might have been removed already... */
331 if (is_Bad(get_Block_cfgpred(block, pos)))
334 pred_block = get_Block_cfgpred_block(block, pos);
335 entry = get_blocksched_entry(block);
336 pred_entry = get_blocksched_entry(pred_block);
338 if (pred_entry->next != NULL || entry->prev != NULL)
341 /* we want at most 1 outedge fallthrough per loop */
342 loop = get_irn_loop(pred_block);
343 if (get_loop_link(loop) != NULL)
346 /* schedule the 2 blocks behind each other */
347 DB((dbg, LEVEL_1, "Coalesce (Loop Outedge) %+F -> %+F (%.3g)\n",
348 pred_entry->block, entry->block, edge->execfreq));
349 pred_entry->next = entry;
350 entry->prev = pred_entry;
352 /* all loops left have an outedge now */
353 outer_loop = get_irn_loop(block);
355 /* we set loop link to loop to mark it */
356 set_loop_link(loop, loop);
357 loop = get_loop_outer_loop(loop);
358 } while (loop != outer_loop);
361 /* sort interblock edges by execution frequency */
362 qsort(edges, ARR_LEN(edges), sizeof(edges[0]), cmp_edges);
364 /* run3: remaining edges */
365 for (i = 0; i < edge_count; ++i) {
366 const edge_t *edge = &edges[i];
367 ir_node *block = edge->block;
370 blocksched_entry_t *entry, *pred_entry;
372 /* the block might have been removed already... */
373 if (is_Bad(get_Block_cfgpred(block, pos)))
376 pred_block = get_Block_cfgpred_block(block, pos);
377 entry = get_blocksched_entry(block);
378 pred_entry = get_blocksched_entry(pred_block);
380 /* is 1 of the blocks already attached to another block? */
381 if (pred_entry->next != NULL || entry->prev != NULL)
384 /* schedule the 2 blocks behind each other */
385 DB((dbg, LEVEL_1, "Coalesce (CondJump) %+F -> %+F (%.3g)\n",
386 pred_entry->block, entry->block, edge->execfreq));
387 pred_entry->next = entry;
388 entry->prev = pred_entry;
392 static void pick_block_successor(blocksched_entry_t *entry, blocksched_env_t *env)
394 ir_node *block = entry->block;
395 ir_node *succ = NULL;
396 blocksched_entry_t *succ_entry;
397 double best_succ_execfreq;
399 if (irn_visited_else_mark(block))
404 DB((dbg, LEVEL_1, "Pick succ of %+F\n", block));
406 /* put all successors into the worklist */
407 foreach_block_succ(block, edge) {
408 ir_node *succ_block = get_edge_src_irn(edge);
410 if (irn_visited(succ_block))
413 /* we only need to put the first of a series of already connected
414 * blocks into the worklist */
415 succ_entry = get_blocksched_entry(succ_block);
416 while (succ_entry->prev != NULL) {
417 /* break cycles... */
418 if (succ_entry->prev->block == succ_block) {
419 succ_entry->prev->next = NULL;
420 succ_entry->prev = NULL;
423 succ_entry = succ_entry->prev;
426 if (irn_visited(succ_entry->block))
429 DB((dbg, LEVEL_1, "Put %+F into worklist\n", succ_entry->block));
430 pdeq_putr(env->worklist, succ_entry->block);
433 if (entry->next != NULL) {
434 pick_block_successor(entry->next, env);
438 DB((dbg, LEVEL_1, "deciding...\n"));
439 best_succ_execfreq = -1;
441 /* no successor yet: pick the successor block with the highest execution
442 * frequency which has no predecessor yet */
444 foreach_block_succ(block, edge) {
445 ir_node *succ_block = get_edge_src_irn(edge);
447 if (irn_visited(succ_block))
450 succ_entry = get_blocksched_entry(succ_block);
451 if (succ_entry->prev != NULL)
454 double execfreq = get_block_execfreq(succ_block);
455 if (execfreq > best_succ_execfreq) {
456 best_succ_execfreq = execfreq;
462 DB((dbg, LEVEL_1, "pick from worklist\n"));
465 if (pdeq_empty(env->worklist)) {
466 DB((dbg, LEVEL_1, "worklist empty\n"));
469 succ = (ir_node*)pdeq_getl(env->worklist);
470 } while (irn_visited(succ));
473 succ_entry = get_blocksched_entry(succ);
474 entry->next = succ_entry;
475 succ_entry->prev = entry;
477 pick_block_successor(succ_entry, env);
480 static blocksched_entry_t *finish_block_schedule(blocksched_env_t *env)
482 ir_graph *irg = env->irg;
483 ir_node *startblock = get_irg_start_block(irg);
484 blocksched_entry_t *entry = get_blocksched_entry(startblock);
486 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
487 inc_irg_visited(irg);
489 env->worklist = new_pdeq();
490 pick_block_successor(entry, env);
491 assert(pdeq_empty(env->worklist));
492 del_pdeq(env->worklist);
494 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
499 static ir_node **create_blocksched_array(blocksched_env_t *env, blocksched_entry_t *first,
500 int count, struct obstack* obst)
503 ir_node **block_list;
504 blocksched_entry_t *entry;
507 block_list = NEW_ARR_D(ir_node *, obst, count);
508 DB((dbg, LEVEL_1, "Blockschedule:\n"));
510 for (entry = first; entry != NULL; entry = entry->next) {
512 block_list[i++] = entry->block;
513 DB((dbg, LEVEL_1, "\t%+F\n", entry->block));
520 static ir_node **create_block_schedule_greedy(ir_graph *irg)
522 blocksched_env_t env;
523 blocksched_entry_t *start_entry;
524 ir_node **block_list;
527 env.edges = NEW_ARR_F(edge_t, 0);
530 obstack_init(&env.obst);
532 assure_loopinfo(irg);
534 // collect edge execution frequencies
535 irg_block_walk_graph(irg, collect_egde_frequency, NULL, &env);
537 (void)be_remove_empty_blocks(irg);
539 if (algo != BLOCKSCHED_NAIV)
540 coalesce_blocks(&env);
542 start_entry = finish_block_schedule(&env);
543 block_list = create_blocksched_array(&env, start_entry, env.blockcount,
544 be_get_be_obst(irg));
546 DEL_ARR_F(env.edges);
547 obstack_free(&env.obst, NULL);
561 typedef struct ilp_edge_t {
562 ir_node *block; /**< source block */
563 int pos; /**< number of cfg predecessor (target) */
567 typedef struct blocksched_ilp_env_t {
568 blocksched_env_t env;
569 ilp_edge_t *ilpedges;
571 } blocksched_ilp_env_t;
573 typedef struct blocksched_ilp_entry_t {
575 struct blocksched_entry_t *next;
576 struct blocksched_entry_t *prev;
579 } blocksched_ilp_entry_t;
581 static int add_ilp_edge(ir_node *block, int pos, double execfreq, blocksched_ilp_env_t *env)
585 int edgeidx = ARR_LEN(env->ilpedges);
587 snprintf(name, sizeof(name), "edge%d", edgeidx);
591 edge.ilpvar = lpp_add_var_default(env->lpp, name, lpp_binary, execfreq, 1.0);
593 ARR_APP1(ilp_edge_t, env->ilpedges, edge);
597 static void collect_egde_frequency_ilp(ir_node *block, void *data)
599 blocksched_ilp_env_t *env = (blocksched_ilp_env_t*)data;
600 ir_graph *irg = env->env.irg;
601 ir_node *startblock = get_irg_start_block(irg);
605 blocksched_ilp_entry_t *entry;
607 snprintf(name, sizeof(name), "block_out_constr_%ld", get_irn_node_nr(block));
608 out_count = get_irn_n_edges_kind(block, EDGE_KIND_BLOCK);
610 entry = OALLOC(&env->env.obst, blocksched_ilp_entry_t);
611 entry->block = block;
614 entry->out_cst = lpp_add_cst_uniq(env->lpp, name, lpp_greater_equal, out_count - 1);
615 set_irn_link(block, entry);
617 if (block == startblock)
620 arity = get_irn_arity(block);
622 double execfreq = get_block_execfreq(block);
623 add_ilp_edge(block, 0, execfreq, env);
629 snprintf(name, sizeof(name), "block_in_constr_%ld", get_irn_node_nr(block));
630 cst_idx = lpp_add_cst_uniq(env->lpp, name, lpp_greater_equal, arity - 1);
632 for (i = 0; i < arity; ++i) {
636 ir_node *pred_block = get_Block_cfgpred_block(block, i);
638 execfreq = get_block_execfreq(pred_block);
639 edgenum = add_ilp_edge(block, i, execfreq, env);
640 edge = &env->ilpedges[edgenum];
641 lpp_set_factor_fast(env->lpp, cst_idx, edge->ilpvar, 1.0);
646 static blocksched_ilp_entry_t *get_blocksched_ilp_entry(const ir_node *block)
648 return (blocksched_ilp_entry_t*)get_irn_link(block);
651 static void coalesce_blocks_ilp(blocksched_ilp_env_t *env)
653 int edge_count = ARR_LEN(env->ilpedges);
656 /* complete out constraints */
657 for (i = 0; i < edge_count; ++i) {
658 const ilp_edge_t *edge = &env->ilpedges[i];
659 ir_node *block = edge->block;
661 blocksched_ilp_entry_t *entry;
663 /* the block might have been removed already... */
664 if (is_Bad(get_Block_cfgpred(block, 0)))
667 pred = get_Block_cfgpred_block(block, edge->pos);
668 entry = get_blocksched_ilp_entry(pred);
670 DB((dbg, LEVEL_1, "Adding out cst to %+F from %+F,%d\n",
671 pred, block, edge->pos));
672 lpp_set_factor_fast(env->lpp, entry->out_cst, edge->ilpvar, 1.0);
675 lpp_solve_net(env->lpp, be_options.ilp_server, be_options.ilp_solver);
676 assert(lpp_is_sol_valid(env->lpp));
678 /* Apply results to edges */
679 for (i = 0; i < edge_count; ++i) {
680 const ilp_edge_t *edge = &env->ilpedges[i];
681 ir_node *block = edge->block;
684 blocksched_entry_t *entry;
685 blocksched_entry_t *pred_entry;
687 /* the block might have been removed already... */
688 if (is_Bad(get_Block_cfgpred(block, 0)))
691 is_jump = (int)lpp_get_var_sol(env->lpp, edge->ilpvar);
695 pred = get_Block_cfgpred_block(block, edge->pos);
696 entry = get_blocksched_entry(block);
697 pred_entry = get_blocksched_entry(pred);
699 assert(entry->prev == NULL && pred_entry->next == NULL);
700 entry->prev = pred_entry;
701 pred_entry->next = entry;
705 static ir_node **create_block_schedule_ilp(ir_graph *irg)
707 blocksched_ilp_env_t env;
708 blocksched_entry_t *start_entry;
709 ir_node **block_list;
712 env.env.worklist = NULL;
713 env.env.blockcount = 0;
714 env.ilpedges = NEW_ARR_F(ilp_edge_t, 0);
715 obstack_init(&env.env.obst);
717 env.lpp = lpp_new("blockschedule", lpp_minimize);
718 lpp_set_time_limit(env.lpp, 20);
719 lpp_set_log(env.lpp, stdout);
721 irg_block_walk_graph(irg, collect_egde_frequency_ilp, NULL, &env);
723 (void)be_remove_empty_blocks(irg);
724 coalesce_blocks_ilp(&env);
726 start_entry = finish_block_schedule(&env.env);
727 block_list = create_blocksched_array(&env.env, start_entry,
729 be_get_be_obst(irg));
731 DEL_ARR_F(env.ilpedges);
733 obstack_free(&env.env.obst, NULL);
741 * | |\/| |/ _` | | '_ \
742 * | | | | (_| | | | | |
743 * |_| |_|\__,_|_|_| |_|
746 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_blocksched)
747 void be_init_blocksched(void)
749 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
751 lc_opt_add_table(be_grp, be_blocksched_options);
753 FIRM_DBG_REGISTER(dbg, "firm.be.blocksched");
756 ir_node **be_create_block_schedule(ir_graph *irg)
759 case BLOCKSCHED_GREEDY:
760 case BLOCKSCHED_NAIV:
761 return create_block_schedule_greedy(irg);
763 return create_block_schedule_ilp(irg);
766 panic("unknown blocksched algo");