2 * Author: Matthias Braun, Christoph Mallon
4 * Copyright: (c) Universitaet Karlsruhe
5 * License: This file is protected by GPL - GNU GENERAL PUBLIC LICENSE.
12 #include "beblocksched.h"
21 #include "irgraph_t.h"
28 #include <libcore/lc_opts.h>
29 #include <libcore/lc_opts_enum.h>
30 #include <libcore/lc_timing.h>
35 #include <lpp/lpp_net.h>
38 typedef enum _blocksched_algos_t {
39 BLOCKSCHED_NAIV, BLOCKSCHED_EXTBB, BLOCKSCHED_GREEDY, BLOCKSCHED_ILP
42 static int algo = BLOCKSCHED_GREEDY;
44 static const lc_opt_enum_int_items_t blockschedalgo_items[] = {
45 { "naiv", BLOCKSCHED_NAIV },
46 { "extbb", BLOCKSCHED_EXTBB },
47 { "greedy", BLOCKSCHED_GREEDY },
49 { "ilp", BLOCKSCHED_ILP },
54 static lc_opt_enum_int_var_t algo_var = {
55 &algo, blockschedalgo_items
58 static const lc_opt_table_entry_t be_blocksched_options[] = {
59 LC_OPT_ENT_ENUM_INT ("algo", "the block scheduling algorithm", &algo_var),
65 * / ___|_ __ ___ ___ __| |_ _
66 * | | _| '__/ _ \/ _ \/ _` | | | |
67 * | |_| | | | __/ __/ (_| | |_| |
68 * \____|_| \___|\___|\__,_|\__, |
72 typedef struct _blocksched_entry_t {
74 struct _blocksched_entry_t *next;
75 struct _blocksched_entry_t *prev;
78 typedef struct _edge_t {
82 int highest_execfreq; /**< flag that indicates wether this edge is the edge with the highest
83 execfreq pointing away from this block */
86 typedef struct _blocksched_env_t {
89 ir_exec_freq *execfreqs;
95 static void collect_egde_frequency(ir_node *block, void *data)
97 blocksched_env_t *env = data;
98 ir_graph *irg = env->irg;
99 ir_node *startblock = get_irg_start_block(irg);
102 blocksched_entry_t *entry;
104 entry = obstack_alloc(env->obst, sizeof(entry[0]));
105 entry->block = block;
108 set_irn_link(block, entry);
110 if(block == startblock)
113 arity = get_irn_arity(block);
117 edge.execfreq = get_block_execfreq(env->execfreqs, block);
118 edge.highest_execfreq = 1;
119 ARR_APP1(edge_t, env->edges, edge);
122 double highest_execfreq = -1;
123 int highest_edge_num;
126 for(i = 0; i < arity; ++i) {
129 ir_node *pred_block = get_Block_cfgpred_block(block, i);
130 execfreq = get_block_execfreq(env->execfreqs, pred_block);
133 edge.execfreq = execfreq;
134 edge.highest_execfreq = 0;
135 ARR_APP1(edge_t, env->edges, edge);
136 if(execfreq > highest_execfreq) {
137 highest_execfreq = execfreq;
138 highest_edge_num = ARR_LEN(env->edges) - 1;
142 env->edges[highest_edge_num].highest_execfreq = 1;
146 static int cmp_edges(const void *d1, const void *d2)
148 const edge_t *e1 = d1;
149 const edge_t *e2 = d2;
150 return e2->execfreq - e1->execfreq;
153 static void coalesce_blocks(blocksched_env_t *env)
156 int edge_count = ARR_LEN(env->edges);
158 // run1: only look at jumps
159 for(i = 0; i < edge_count; ++i) {
160 const edge_t *edge = & env->edges[i];
161 ir_node *block = edge->block;
163 blocksched_entry_t *entry, *pred_entry;
165 // the block might have been removed already...
166 if(is_Bad(get_Block_cfgpred(block, 0)))
169 if(!edge->highest_execfreq)
172 pred_block = get_Block_cfgpred_block(block, edge->pos);
173 entry = get_irn_link(block);
174 pred_entry = get_irn_link(pred_block);
176 if(pred_entry->next != NULL || entry->prev != NULL)
178 // only coalesce jumps
179 if(get_block_succ_next(pred_block, get_block_succ_first(pred_block)) != NULL)
182 // schedule the 2 blocks behind each other
183 ir_fprintf(stderr, "Coalesce (Jump) %+F -> %+F (%.3g)\n",
184 pred_entry->block, entry->block, edge->execfreq);
185 pred_entry->next = entry;
186 entry->prev = pred_entry;
189 // run2: remaining edges
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 pred_block = get_Block_cfgpred_block(block, edge->pos);
201 entry = get_irn_link(block);
202 pred_entry = get_irn_link(pred_block);
204 if(pred_entry->next != NULL || entry->prev != NULL)
207 // schedule the 2 blocks behind each other
208 ir_fprintf(stderr, "Coalesce (CondJump) %+F -> %+F (%.3g)\n",
209 pred_entry->block, entry->block, edge->execfreq);
210 pred_entry->next = entry;
211 entry->prev = pred_entry;
215 static void pick_block_successor(blocksched_entry_t *entry, blocksched_env_t *env)
217 ir_node *block = entry->block;
218 blocksched_entry_t *succ_entry;
219 const ir_edge_t *edge;
220 double best_succ_execfreq;
221 ir_node *succ = NULL;
223 if(irn_visited(block))
226 mark_irn_visited(block);
228 ir_fprintf(stderr, "Pick succ of %+F\n", block);
230 // put all successors into the worklist
231 foreach_block_succ(block, edge) {
232 ir_node *succ_block = get_edge_src_irn(edge);
234 if(irn_visited(succ_block))
237 // we only need to put the first of a series of already connected
238 // blocks into the worklist
239 succ_entry = get_irn_link(succ_block);
240 while(succ_entry->prev != NULL) {
242 if(succ_entry->prev->block == succ_block) {
243 succ_entry->prev->next = NULL;
244 succ_entry->prev = NULL;
247 succ_entry = succ_entry->prev;
250 if(irn_visited(succ_entry->block))
253 ir_fprintf(stderr, "Put %+F into worklist\n", succ_entry->block);
254 pdeq_putr(env->worklist, succ_entry->block);
257 if(entry->next != NULL) {
258 pick_block_successor(entry->next, env);
262 fprintf(stderr, "deciding...\n");
263 best_succ_execfreq = -1;
264 /* no successor yet: pick the successor block with the highest execution
265 * frequency which has no predecessor yet
267 foreach_block_succ(block, edge) {
268 ir_node *succ_block = get_edge_src_irn(edge);
270 if(irn_visited(succ_block))
273 succ_entry = get_irn_link(succ_block);
274 if(succ_entry->prev != NULL)
277 double execfreq = get_block_execfreq(env->execfreqs, succ_block);
278 if(execfreq > best_succ_execfreq) {
279 best_succ_execfreq = execfreq;
285 fprintf(stderr, "pick from worklist\n");
288 if(pdeq_empty(env->worklist)) {
289 fprintf(stderr, "worklist empty\n");
292 succ = pdeq_getl(env->worklist);
293 } while(irn_visited(succ));
296 succ_entry = get_irn_link(succ);
297 entry->next = succ_entry;
298 succ_entry->prev = entry;
300 pick_block_successor(succ_entry, env);
303 static blocksched_entry_t *finish_block_schedule(blocksched_env_t *env)
305 ir_graph *irg = env->irg;
306 ir_node *startblock = get_irg_start_block(irg);
307 blocksched_entry_t *entry = get_irn_link(startblock);
309 inc_irg_visited(irg);
311 env->worklist = new_pdeq();
312 pick_block_successor(entry, env);
313 assert(pdeq_empty(env->worklist));
314 del_pdeq(env->worklist);
319 static ir_node **create_blocksched_array(blocksched_entry_t *first, int count,
320 struct obstack* obst) {
322 ir_node **block_list;
323 blocksched_entry_t *entry;
325 block_list = NEW_ARR_D(ir_node *, obst, count);
326 fprintf(stderr, "Blockschedule:\n");
327 for(entry = first; entry != NULL; entry = entry->next) {
329 block_list[i++] = entry->block;
330 ir_fprintf(stderr, "\t%+F\n", entry->block);
337 static ir_node **create_block_schedule_greedy(ir_graph *irg, ir_exec_freq *execfreqs)
339 blocksched_env_t env;
341 blocksched_entry_t *start_entry;
342 ir_node **block_list;
348 env.execfreqs = execfreqs;
349 env.edges = NEW_ARR_F(edge_t, 0);
353 // collect edge execution frequencies
354 irg_block_walk_graph(irg, collect_egde_frequency, NULL, &env);
356 // sort interblock edges by execution frequency
357 qsort(env.edges, ARR_LEN(env.edges), sizeof(env.edges[0]), cmp_edges);
359 be_remove_empty_blocks(irg);
361 if(algo != BLOCKSCHED_NAIV)
362 coalesce_blocks(&env);
364 start_entry = finish_block_schedule(&env);
366 block_list = create_blocksched_array(start_entry, env.blockcount, get_irg_obstack(irg));
368 DEL_ARR_F(env.edges);
369 obstack_free(&obst, NULL);
384 typedef struct _ilp_edge_t {
390 typedef struct _blocksched_ilp_env_t {
391 blocksched_env_t env;
392 ilp_edge_t *ilpedges;
394 } blocksched_ilp_env_t;
396 typedef struct _blocksched_ilp_entry_t {
398 struct _blocksched_entry_t *next;
399 struct _blocksched_entry_t *prev;
402 } blocksched_ilp_entry_t;
404 static int add_ilp_edge(ir_node *block, int pos, double execfreq, blocksched_ilp_env_t *env)
408 int edgeidx = ARR_LEN(env->ilpedges);
410 snprintf(name, sizeof(name), "edge%d", edgeidx);
414 edge.ilpvar = lpp_add_var_default(env->lpp, name, lpp_binary, execfreq, 1.0);
416 ARR_APP1(ilp_edge_t, env->ilpedges, edge);
420 static void collect_egde_frequency_ilp(ir_node *block, void *data)
422 blocksched_ilp_env_t *env = data;
423 ir_graph *irg = env->env.irg;
424 ir_node *startblock = get_irg_start_block(irg);
426 blocksched_ilp_entry_t *entry;
431 snprintf(name, sizeof(name), "block_out_constr_%ld", get_irn_node_nr(block));
432 out_count = get_irn_n_edges_kind(block, EDGE_KIND_BLOCK);
434 entry = obstack_alloc(env->env.obst, sizeof(entry[0]));
435 entry->block = block;
438 entry->out_cst = lpp_add_cst_uniq(env->lpp, name, lpp_greater, out_count - 1);
439 set_irn_link(block, entry);
441 if(block == startblock)
444 arity = get_irn_arity(block);
446 double execfreq = get_block_execfreq(env->env.execfreqs, block);
447 add_ilp_edge(block, 0, execfreq, env);
450 int *edgenums = alloca(sizeof(edgenums[0]) * arity);
452 snprintf(name, sizeof(name), "block_in_constr_%ld", get_irn_node_nr(block));
453 cst = lpp_add_cst_uniq(env->lpp, name, lpp_greater, arity - 1);
455 for(i = 0; i < arity; ++i) {
460 ir_node *pred_block = get_Block_cfgpred_block(block, i);
461 execfreq = get_block_execfreq(env->env.execfreqs, pred_block);
463 edgenum = add_ilp_edge(block, i, execfreq, env);
464 edge = & env->ilpedges[edgenum];
465 lpp_set_factor_fast(env->lpp, cst, edge->ilpvar, 1.0);
471 static void coalesce_blocks_ilp(blocksched_ilp_env_t *env)
474 int edge_count = ARR_LEN(env->ilpedges);
478 /* complete out constraints */
479 for(i = 0; i < edge_count; ++i) {
480 const ilp_edge_t *edge = & env->ilpedges[i];
481 ir_node *block = edge->block;
483 blocksched_ilp_entry_t *entry;
485 // the block might have been removed already...
486 if(is_Bad(get_Block_cfgpred(block, 0)))
489 pred = get_Block_cfgpred_block(block, edge->pos);
490 entry = get_irn_link(pred);
492 ir_printf("Adding out cst to %+F from %+F,%d\n",
493 pred, block, edge->pos);
494 lpp_set_factor_fast(env->lpp, entry->out_cst, edge->ilpvar, 1.0);
497 lpp_dump(env->lpp, "lpp.out");
498 snprintf(fname, sizeof(fname), "lpp_%s.plain", get_irg_dump_name(env->env.irg));
499 f = fopen(fname, "w");
500 lpp_dump_plain(env->lpp, f);
502 //lpp_solve_net(env->lpp, main_env->options->ilp_server, main_env->options->ilp_solver);
503 lpp_solve_net(env->lpp, "i44pc52", "cplex");
504 assert(lpp_is_sol_valid(env->lpp));
506 /* Apply results to edges */
507 for(i = 0; i < edge_count; ++i) {
508 const ilp_edge_t *edge = & env->ilpedges[i];
509 ir_node *block = edge->block;
512 blocksched_entry_t *entry;
513 blocksched_entry_t *pred_entry;
515 // the block might have been removed already...
516 if(is_Bad(get_Block_cfgpred(block, 0)))
519 is_jump = lpp_get_var_sol(env->lpp, edge->ilpvar);
523 pred = get_Block_cfgpred_block(block, edge->pos);
524 entry = get_irn_link(block);
525 pred_entry = get_irn_link(pred);
527 assert(entry->prev == NULL && pred_entry->next == NULL);
528 entry->prev = pred_entry;
529 pred_entry->next = entry;
533 static ir_node **create_block_schedule_ilp(ir_graph *irg, ir_exec_freq *execfreqs)
535 blocksched_ilp_env_t env;
537 blocksched_entry_t *start_entry;
538 ir_node **block_list;
543 env.env.obst = &obst;
544 env.env.execfreqs = execfreqs;
545 env.env.worklist = NULL;
546 env.env.blockcount = 0;
547 env.ilpedges = NEW_ARR_F(ilp_edge_t, 0);
549 env.lpp = new_lpp("blockschedule", lpp_minimize);
550 lpp_set_time_limit(env.lpp, 20);
551 lpp_set_log(env.lpp, stdout);
553 irg_block_walk_graph(irg, collect_egde_frequency_ilp, NULL, &env);
555 be_remove_empty_blocks(irg);
557 coalesce_blocks_ilp(&env);
559 start_entry = finish_block_schedule(&env.env);
561 block_list = create_blocksched_array(start_entry, env.env.blockcount, get_irg_obstack(irg));
563 DEL_ARR_F(env.ilpedges);
565 obstack_free(&obst, NULL);
573 * | ____|_ _| |_| __ )| __ )
574 * | _| \ \/ / __| _ \| _ \
575 * | |___ > <| |_| |_) | |_) |
576 * |_____/_/\_\\__|____/|____/
580 /** A simple forward single linked list. */
582 ir_node *start; /**< start of the list */
583 ir_node *end; /**< last block in the list */
584 unsigned n_blks; /**< number of blocks in the list */
587 static void add_block(anchor *list, ir_node *block) {
588 if(list->start == NULL) {
592 set_irn_link(list->end, block);
599 static void create_block_list(ir_node *leader_block, anchor *list) {
601 ir_node *block = NULL;
602 const ir_edge_t *edge;
604 ir_extblk *extbb = get_Block_extbb(leader_block);
605 if(extbb_visited(extbb))
607 mark_extbb_visited(extbb);
609 for(i = 0; i < get_extbb_n_blocks(extbb); ++i) {
610 block = get_extbb_block(extbb, i);
611 add_block(list, block);
614 assert(block != NULL);
616 // pick successor extbbs
617 foreach_block_succ(block, edge) {
618 ir_node *succ = get_edge_src_irn(edge);
620 create_block_list(succ, list);
623 for(i = 0; i < get_extbb_n_blocks(extbb) - 1; ++i) {
624 block = get_extbb_block(extbb, i);
625 foreach_block_succ(block, edge) {
626 ir_node *succ = get_edge_src_irn(edge);
628 create_block_list(succ, list);
633 void compute_extbb_execfreqs(ir_graph *irg, ir_exec_freq *execfreqs);
636 * Calculates a block schedule. The schedule is stored as a linked
637 * list starting at the start_block of the irg.
639 static ir_node **create_extbb_block_schedule(ir_graph *irg, ir_exec_freq *execfreqs)
642 ir_node **blk_list, *b, *n;
645 /* schedule extended basic blocks */
646 compute_extbb_execfreqs(irg, execfreqs);
647 //compute_extbb(irg);
652 inc_irg_block_visited(irg);
653 create_block_list(get_irg_start_block(irg), &list);
655 /** create an array, so we can go forward and backward */
656 blk_list = NEW_ARR_D(ir_node *, irg->obst,list.n_blks);
658 for (i = 0, b = list.start; b; b = n, ++i) {
669 * | |\/| |/ _` | | '_ \
670 * | | | | (_| | | | | |
671 * |_| |_|\__,_|_|_| |_|
676 void be_block_schedule_register_options(lc_opt_entry_t *grp)
678 static int run_once = 0;
679 lc_opt_entry_t *blocksched_grp;
684 blocksched_grp = lc_opt_get_grp(grp, "blocksched");
686 lc_opt_add_table(blocksched_grp, be_blocksched_options);
690 ir_node **be_create_block_schedule(ir_graph *irg, ir_exec_freq *execfreqs)
693 case BLOCKSCHED_GREEDY:
694 case BLOCKSCHED_NAIV:
695 return create_block_schedule_greedy(irg, execfreqs);
696 case BLOCKSCHED_EXTBB:
697 return create_extbb_block_schedule(irg, execfreqs);
700 return create_block_schedule_ilp(irg, execfreqs);
704 assert(0 && "unknown blocksched algo");