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 {
123 struct obstack *obst;
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;
524 blocksched_entry_t *start_entry;
525 ir_node **block_list;
531 env.edges = NEW_ARR_F(edge_t, 0);
535 assure_loopinfo(irg);
537 // collect edge execution frequencies
538 irg_block_walk_graph(irg, collect_egde_frequency, NULL, &env);
540 (void)be_remove_empty_blocks(irg);
542 if (algo != BLOCKSCHED_NAIV)
543 coalesce_blocks(&env);
545 start_entry = finish_block_schedule(&env);
546 block_list = create_blocksched_array(&env, start_entry, env.blockcount,
547 be_get_be_obst(irg));
549 DEL_ARR_F(env.edges);
550 obstack_free(&obst, NULL);
564 typedef struct ilp_edge_t {
565 ir_node *block; /**< source block */
566 int pos; /**< number of cfg predecessor (target) */
570 typedef struct blocksched_ilp_env_t {
571 blocksched_env_t env;
572 ilp_edge_t *ilpedges;
574 } blocksched_ilp_env_t;
576 typedef struct blocksched_ilp_entry_t {
578 struct blocksched_entry_t *next;
579 struct blocksched_entry_t *prev;
582 } blocksched_ilp_entry_t;
584 static int add_ilp_edge(ir_node *block, int pos, double execfreq, blocksched_ilp_env_t *env)
588 int edgeidx = ARR_LEN(env->ilpedges);
590 snprintf(name, sizeof(name), "edge%d", edgeidx);
594 edge.ilpvar = lpp_add_var_default(env->lpp, name, lpp_binary, execfreq, 1.0);
596 ARR_APP1(ilp_edge_t, env->ilpedges, edge);
600 static void collect_egde_frequency_ilp(ir_node *block, void *data)
602 blocksched_ilp_env_t *env = (blocksched_ilp_env_t*)data;
603 ir_graph *irg = env->env.irg;
604 ir_node *startblock = get_irg_start_block(irg);
608 blocksched_ilp_entry_t *entry;
610 snprintf(name, sizeof(name), "block_out_constr_%ld", get_irn_node_nr(block));
611 out_count = get_irn_n_edges_kind(block, EDGE_KIND_BLOCK);
613 entry = OALLOC(env->env.obst, blocksched_ilp_entry_t);
614 entry->block = block;
617 entry->out_cst = lpp_add_cst_uniq(env->lpp, name, lpp_greater_equal, out_count - 1);
618 set_irn_link(block, entry);
620 if (block == startblock)
623 arity = get_irn_arity(block);
625 double execfreq = get_block_execfreq(block);
626 add_ilp_edge(block, 0, execfreq, env);
632 snprintf(name, sizeof(name), "block_in_constr_%ld", get_irn_node_nr(block));
633 cst_idx = lpp_add_cst_uniq(env->lpp, name, lpp_greater_equal, arity - 1);
635 for (i = 0; i < arity; ++i) {
639 ir_node *pred_block = get_Block_cfgpred_block(block, i);
641 execfreq = get_block_execfreq(pred_block);
642 edgenum = add_ilp_edge(block, i, execfreq, env);
643 edge = &env->ilpedges[edgenum];
644 lpp_set_factor_fast(env->lpp, cst_idx, edge->ilpvar, 1.0);
649 static blocksched_ilp_entry_t *get_blocksched_ilp_entry(const ir_node *block)
651 return (blocksched_ilp_entry_t*)get_irn_link(block);
654 static void coalesce_blocks_ilp(blocksched_ilp_env_t *env)
656 int edge_count = ARR_LEN(env->ilpedges);
659 /* complete out constraints */
660 for (i = 0; i < edge_count; ++i) {
661 const ilp_edge_t *edge = &env->ilpedges[i];
662 ir_node *block = edge->block;
664 blocksched_ilp_entry_t *entry;
666 /* the block might have been removed already... */
667 if (is_Bad(get_Block_cfgpred(block, 0)))
670 pred = get_Block_cfgpred_block(block, edge->pos);
671 entry = get_blocksched_ilp_entry(pred);
673 DB((dbg, LEVEL_1, "Adding out cst to %+F from %+F,%d\n",
674 pred, block, edge->pos));
675 lpp_set_factor_fast(env->lpp, entry->out_cst, edge->ilpvar, 1.0);
678 lpp_solve_net(env->lpp, be_options.ilp_server, be_options.ilp_solver);
679 assert(lpp_is_sol_valid(env->lpp));
681 /* Apply results to edges */
682 for (i = 0; i < edge_count; ++i) {
683 const ilp_edge_t *edge = &env->ilpedges[i];
684 ir_node *block = edge->block;
687 blocksched_entry_t *entry;
688 blocksched_entry_t *pred_entry;
690 /* the block might have been removed already... */
691 if (is_Bad(get_Block_cfgpred(block, 0)))
694 is_jump = (int)lpp_get_var_sol(env->lpp, edge->ilpvar);
698 pred = get_Block_cfgpred_block(block, edge->pos);
699 entry = get_blocksched_entry(block);
700 pred_entry = get_blocksched_entry(pred);
702 assert(entry->prev == NULL && pred_entry->next == NULL);
703 entry->prev = pred_entry;
704 pred_entry->next = entry;
708 static ir_node **create_block_schedule_ilp(ir_graph *irg)
710 blocksched_ilp_env_t env;
712 blocksched_entry_t *start_entry;
713 ir_node **block_list;
718 env.env.obst = &obst;
719 env.env.worklist = NULL;
720 env.env.blockcount = 0;
721 env.ilpedges = NEW_ARR_F(ilp_edge_t, 0);
723 env.lpp = lpp_new("blockschedule", lpp_minimize);
724 lpp_set_time_limit(env.lpp, 20);
725 lpp_set_log(env.lpp, stdout);
727 irg_block_walk_graph(irg, collect_egde_frequency_ilp, NULL, &env);
729 (void)be_remove_empty_blocks(irg);
730 coalesce_blocks_ilp(&env);
732 start_entry = finish_block_schedule(&env.env);
733 block_list = create_blocksched_array(&env.env, start_entry,
735 be_get_be_obst(irg));
737 DEL_ARR_F(env.ilpedges);
739 obstack_free(&obst, NULL);
747 * | |\/| |/ _` | | '_ \
748 * | | | | (_| | | | | |
749 * |_| |_|\__,_|_|_| |_|
752 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_blocksched)
753 void be_init_blocksched(void)
755 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
757 lc_opt_add_table(be_grp, be_blocksched_options);
759 FIRM_DBG_REGISTER(dbg, "firm.be.blocksched");
762 ir_node **be_create_block_schedule(ir_graph *irg)
765 case BLOCKSCHED_GREEDY:
766 case BLOCKSCHED_NAIV:
767 return create_block_schedule_greedy(irg);
769 return create_block_schedule_ilp(irg);
772 panic("unknown blocksched algo");