2 * Scheduling algorithms.
3 * Just a simple list scheduling algorithm is here.
5 * @author Sebastian Hack
25 #include "iredges_t.h"
30 #include "irprintf_t.h"
36 #include "besched_t.h"
39 #include "belistsched.h"
40 #include "beschedmris.h"
41 #include "beschedrss.h"
45 #include <libcore/lc_opts.h>
46 #include <libcore/lc_opts_enum.h>
48 #define BE_SCHED_NODE(irn) (be_is_Keep(irn) || be_is_CopyKeep(irn) || be_is_RegParams(irn))
51 BE_SCHED_SELECT_TRIVIAL = 0,
52 BE_SCHED_SELECT_REGPRESS = 1,
53 BE_SCHED_SELECT_MUCHNIK = 2,
54 BE_SCHED_SELECT_HEUR = 3,
55 BE_SCHED_SELECT_HMUCHNIK = 4,
56 BE_SCHED_SELECT_RANDOM = 5
60 BE_SCHED_PREP_NONE = 0,
61 BE_SCHED_PREP_MRIS = 2,
65 typedef struct _list_sched_options_t {
66 int select; /**< the node selector */
67 int prep; /**< schedule preparation */
68 } list_sched_options_t;
70 static list_sched_options_t list_sched_options = {
71 BE_SCHED_SELECT_HEUR, /* mueller heuristic selector */
72 BE_SCHED_PREP_NONE, /* no scheduling preparation */
75 /* schedule selector options. */
76 static const lc_opt_enum_int_items_t sched_select_items[] = {
77 { "trivial", BE_SCHED_SELECT_TRIVIAL },
78 { "random", BE_SCHED_SELECT_RANDOM },
79 { "regpress", BE_SCHED_SELECT_REGPRESS },
80 { "muchnik", BE_SCHED_SELECT_MUCHNIK },
81 { "heur", BE_SCHED_SELECT_HEUR },
82 { "hmuchnik", BE_SCHED_SELECT_HMUCHNIK },
86 /* schedule preparation options. */
87 static const lc_opt_enum_int_items_t sched_prep_items[] = {
88 { "none", BE_SCHED_PREP_NONE },
89 { "mris", BE_SCHED_PREP_MRIS },
90 { "rss", BE_SCHED_PREP_RSS },
94 static lc_opt_enum_int_var_t sched_select_var = {
95 &list_sched_options.select, sched_select_items
98 static lc_opt_enum_int_var_t sched_prep_var = {
99 &list_sched_options.prep, sched_prep_items
102 static const lc_opt_table_entry_t list_sched_option_table[] = {
103 LC_OPT_ENT_ENUM_PTR("prep", "schedule preparation", &sched_prep_var),
104 LC_OPT_ENT_ENUM_PTR("select", "node selector", &sched_select_var),
109 * All scheduling info needed per node.
111 typedef struct _sched_irn_t {
112 unsigned num_not_sched_user; /**< The number of not yet scheduled users of this node */
113 unsigned already_sched : 1; /**< Set if this node is already scheduled */
117 * Scheduling environment for the whole graph.
119 typedef struct _sched_env_t {
120 sched_irn_t *sched_info; /**< scheduling info per node */
121 const list_sched_selector_t *selector; /**< The node selector. */
122 const arch_env_t *arch_env; /**< The architecture environment. */
123 const ir_graph *irg; /**< The graph to schedule. */
124 void *selector_env; /**< A pointer to give to the selector. */
128 * Environment for a block scheduler.
130 typedef struct _block_sched_env_t {
131 sched_irn_t *sched_info; /**< scheduling info per node, copied from the global scheduler object */
132 nodeset *cands; /**< the set of candidates */
133 ir_node *block; /**< the current block */
134 sched_env_t *sched_env; /**< the scheduler environment */
135 nodeset *live; /**< simple liveness during scheduling */
136 const list_sched_selector_t *selector;
137 void *selector_block_env;
138 DEBUG_ONLY(firm_dbg_module_t *dbg;)
142 * Returns non-zero if the node is already scheduled
144 static INLINE int is_already_scheduled(block_sched_env_t *env, ir_node *n)
146 int idx = get_irn_idx(n);
148 assert(idx < ARR_LEN(env->sched_info));
149 return env->sched_info[idx].already_sched;
153 * Mark a node as already scheduled
155 static INLINE void mark_already_scheduled(block_sched_env_t *env, ir_node *n)
157 int idx = get_irn_idx(n);
159 assert(idx < ARR_LEN(env->sched_info));
160 env->sched_info[idx].already_sched = 1;
164 * Try to put a node in the ready set.
165 * @param env The block scheduler environment.
166 * @param pred The previous scheduled node.
167 * @param irn The node to make ready.
168 * @return 1, if the node could be made ready, 0 else.
170 static INLINE int make_ready(block_sched_env_t *env, ir_node *pred, ir_node *irn)
174 /* Blocks cannot be scheduled. */
175 if (is_Block(irn) || get_irn_n_edges(irn) == 0)
179 * Check, if the given ir node is in a different block as the
180 * currently scheduled one. If that is so, don't make the node ready.
182 if (env->block != get_nodes_block(irn))
185 for (i = 0, n = get_irn_ins_or_deps(irn); i < n; ++i) {
186 ir_node *op = get_irn_in_or_dep(irn, i);
188 /* if irn is an End we have keep-alives and op might be a block, skip that */
190 assert(get_irn_op(irn) == op_End);
194 /* If the operand is local to the scheduled block and not yet
195 * scheduled, this nodes cannot be made ready, so exit. */
196 if (! is_already_scheduled(env, op) && get_nodes_block(op) == env->block)
200 nodeset_insert(env->cands, irn);
202 /* Notify selector about the ready node. */
203 if (env->selector->node_ready)
204 env->selector->node_ready(env->selector_block_env, irn, pred);
206 DB((env->dbg, LEVEL_2, "\tmaking ready: %+F\n", irn));
212 * Try, to make all users of a node ready.
213 * In fact, a usage node can only be made ready, if all its operands
214 * have already been scheduled yet. This is checked by make_ready().
215 * @param env The block schedule environment.
216 * @param irn The node, which usages (successors) are to be made ready.
218 static INLINE void make_users_ready(block_sched_env_t *env, ir_node *irn)
220 const ir_edge_t *edge;
222 foreach_out_edge(irn, edge) {
223 ir_node *user = get_edge_src_irn(edge);
225 make_ready(env, irn, user);
228 foreach_out_edge_kind(irn, edge, EDGE_KIND_DEP) {
229 ir_node *user = get_edge_src_irn(edge);
231 make_ready(env, irn, user);
236 * Returns the number of not yet schedules users.
238 static INLINE int get_irn_not_sched_user(block_sched_env_t *env, ir_node *n) {
239 int idx = get_irn_idx(n);
241 assert(idx < ARR_LEN(env->sched_info));
242 return env->sched_info[idx].num_not_sched_user;
246 * Sets the number of not yet schedules users.
248 static INLINE void set_irn_not_sched_user(block_sched_env_t *env, ir_node *n, int num) {
249 int idx = get_irn_idx(n);
251 assert(idx < ARR_LEN(env->sched_info));
252 env->sched_info[idx].num_not_sched_user = num;
256 * Add @p num to the number of not yet schedules users and returns the result.
258 static INLINE int add_irn_not_sched_user(block_sched_env_t *env, ir_node *n, int num) {
259 int idx = get_irn_idx(n);
261 assert(idx < ARR_LEN(env->sched_info));
262 env->sched_info[idx].num_not_sched_user += num;
263 return env->sched_info[idx].num_not_sched_user;
267 * Returns the number of users of a node having mode datab.
269 static int get_num_successors(ir_node *irn) {
271 const ir_edge_t *edge;
273 if (get_irn_mode(irn) == mode_T) {
274 /* for mode_T nodes: count the users of all Projs */
275 foreach_out_edge(irn, edge) {
276 ir_node *proj = get_edge_src_irn(edge);
277 ir_mode *mode = get_irn_mode(proj);
280 sum += get_num_successors(proj);
281 else if (mode_is_datab(mode))
282 sum += get_irn_n_edges(proj);
286 /* do not count keep-alive edges */
287 foreach_out_edge(irn, edge) {
288 if (get_irn_opcode(get_edge_src_irn(edge)) != iro_End)
297 * Adds irn to @p live, updates all inputs that this user is scheduled
298 * and counts all of it's non scheduled users.
300 static void update_sched_liveness(block_sched_env_t *env, ir_node *irn) {
307 for (i = get_irn_ins_or_deps(irn) - 1; i >= 0; --i) {
308 ir_node *in = get_irn_in_or_dep(irn, i);
310 /* if in is a proj: update predecessor */
312 in = get_Proj_pred(in);
314 /* if in is still in the live set: reduce number of users by one */
315 if (nodeset_find(env->live, in)) {
316 if (add_irn_not_sched_user(env, in, -1) <= 0)
317 nodeset_remove(env->live, in);
322 get_num_successors returns the number of all users. This includes
323 users in different blocks as well. As the each block is scheduled separately
324 the liveness info of those users will not be updated and so these
325 users will keep up the register pressure as it is desired.
327 i = get_num_successors(irn);
329 set_irn_not_sched_user(env, irn, i);
330 nodeset_insert(env->live, irn);
334 static INLINE int must_appear_in_schedule(const list_sched_selector_t *sel, void *block_env, const ir_node *irn)
338 if (get_irn_n_edges(irn) < 1)
341 if (sel->to_appear_in_schedule)
342 res = sel->to_appear_in_schedule(block_env, irn);
344 return res >= 0 ? res : ((to_appear_in_schedule(irn) || BE_SCHED_NODE(irn)) && ! is_Unknown(irn));
348 * Append an instruction to a schedule.
349 * @param env The block scheduling environment.
350 * @param irn The node to add to the schedule.
351 * @return The given node.
353 static ir_node *add_to_sched(block_sched_env_t *env, ir_node *irn)
355 /* If the node consumes/produces data, it is appended to the schedule
356 * list, otherwise, it is not put into the list */
357 if (must_appear_in_schedule(env->selector, env->selector_block_env, irn)) {
358 update_sched_liveness(env, irn);
359 sched_add_before(env->block, irn);
361 DBG((env->dbg, LEVEL_2, "\tadding %+F\n", irn));
364 /* notify the selector about the finally selected node. */
365 if (env->selector->node_selected)
366 env->selector->node_selected(env->selector_block_env, irn);
368 /* Insert the node in the set of all already scheduled nodes. */
369 mark_already_scheduled(env, irn);
371 /* Remove the node from the ready set */
372 if(nodeset_find(env->cands, irn))
373 nodeset_remove(env->cands, irn);
379 * Add the proj nodes of a tuple-mode irn to the schedule immediately
380 * after the tuple-moded irn. By pinning the projs after the irn, no
381 * other nodes can create a new lifetime between the tuple-moded irn and
382 * one of its projs. This should render a realistic image of a
383 * tuple-moded irn, which in fact models a node which defines multiple
386 * @param irn The tuple-moded irn.
388 static void add_tuple_projs(block_sched_env_t *env, ir_node *irn)
390 const ir_edge_t *edge;
392 assert(get_irn_mode(irn) == mode_T && "Mode of node must be tuple");
398 /* non-proj nodes can have dependency edges to tuple nodes. */
399 foreach_out_edge_kind(irn, edge, EDGE_KIND_DEP) {
400 ir_node *out = get_edge_src_irn(edge);
401 make_ready(env, irn, out);
404 /* schedule the normal projs */
405 foreach_out_edge(irn, edge) {
406 ir_node *out = get_edge_src_irn(edge);
408 assert(is_Proj(out) && "successor of a modeT node must be a proj");
410 if (get_irn_mode(out) == mode_T)
411 add_tuple_projs(env, out);
413 add_to_sched(env, out);
414 make_users_ready(env, out);
420 * Perform list scheduling on a block.
422 * Note, that the caller must compute a linked list of nodes in the block
423 * using the link field before calling this function.
425 * Also the outs must have been computed.
427 * @param block The block node.
428 * @param env Scheduling environment.
430 static void list_sched_block(ir_node *block, void *env_ptr)
432 sched_env_t *env = env_ptr;
433 const list_sched_selector_t *selector = env->selector;
434 ir_node *start_node = get_irg_start(get_irn_irg(block));
436 block_sched_env_t be;
437 const ir_edge_t *edge;
441 /* Initialize the block's list head that will hold the schedule. */
442 sched_init_block(block);
444 /* Initialize the block scheduling environment */
445 be.sched_info = env->sched_info;
447 be.cands = new_nodeset(get_irn_n_edges(block));
448 be.live = new_nodeset(get_irn_n_edges(block));
449 be.selector = selector;
451 FIRM_DBG_REGISTER(be.dbg, "firm.be.sched");
453 DBG((be.dbg, LEVEL_1, "scheduling %+F\n", block));
455 if (selector->init_block)
456 be.selector_block_env = selector->init_block(env->selector_env, block);
458 /* Then one can add all nodes are ready to the set. */
459 foreach_out_edge(block, edge) {
460 ir_node *irn = get_edge_src_irn(edge);
462 /* Skip the end node because of keepalive edges. */
463 if (get_irn_opcode(irn) == iro_End)
466 if (get_irn_n_edges(irn) == 0)
471 Phi functions are scheduled immediately, since they only
472 transfer data flow from the predecessors to this block.
474 add_to_sched(&be, irn);
475 make_users_ready(&be, irn);
477 else if (irn == start_node) {
478 /* The start block will be scheduled as the first node */
479 add_to_sched(&be, irn);
480 add_tuple_projs(&be, irn);
483 /* Other nodes must have all operands in other blocks to be made
487 /* Check, if the operands of a node are not local to this block */
488 for (j = 0, m = get_irn_ins_or_deps(irn); j < m; ++j) {
489 ir_node *operand = get_irn_in_or_dep(irn, j);
491 if (get_nodes_block(operand) == block) {
496 /* live in values increase register pressure */
497 update_sched_liveness(&be, operand);
501 /* Make the node ready, if all operands live in a foreign block */
503 DBG((be.dbg, LEVEL_2, "\timmediately ready: %+F\n", irn));
504 make_ready(&be, NULL, irn);
509 /* Iterate over all remaining nodes */
510 while (nodeset_count(be.cands) > 0) {
511 /* collect statistics about amount of ready nodes */
512 be_do_stat_sched_ready(block, be.cands);
514 /* Keeps must be scheduled immediatly */
515 foreach_nodeset(be.cands, irn) {
516 if (be_is_Keep(irn) || be_is_CopyKeep(irn) || is_Sync(irn)) {
517 nodeset_break(be.cands);
523 /* Keeps must be immediately scheduled */
524 irn = be.selector->select(be.selector_block_env, be.cands, be.live);
527 DB((be.dbg, LEVEL_2, "\tpicked node %+F\n", irn));
529 /* Add the node to the schedule. */
530 add_to_sched(&be, irn);
532 if (get_irn_mode(irn) == mode_T)
533 add_tuple_projs(&be, irn);
535 make_users_ready(&be, irn);
537 /* remove the scheduled node from the ready list. */
538 if (nodeset_find(be.cands, irn))
539 nodeset_remove(be.cands, irn);
542 if (selector->finish_block)
543 selector->finish_block(be.selector_block_env);
545 del_nodeset(be.cands);
546 del_nodeset(be.live);
549 /* List schedule a graph. */
550 void list_sched(const be_irg_t *birg, be_options_t *be_opts)
552 const arch_env_t *arch_env = birg->main_env->arch_env;
553 ir_graph *irg = birg->irg;
557 mris_env_t *mris = NULL;
558 list_sched_selector_t sel;
560 /* Select a scheduler based on backend options */
561 switch (list_sched_options.select) {
562 case BE_SCHED_SELECT_TRIVIAL:
563 memcpy(&sel, trivial_selector, sizeof(sel));
565 case BE_SCHED_SELECT_RANDOM:
566 memcpy(&sel, random_selector, sizeof(sel));
568 case BE_SCHED_SELECT_REGPRESS:
569 memcpy(&sel, reg_pressure_selector, sizeof(sel));
571 case BE_SCHED_SELECT_MUCHNIK:
572 memcpy(&sel, muchnik_selector, sizeof(sel));
574 case BE_SCHED_SELECT_HEUR:
575 memcpy(&sel, heuristic_selector, sizeof(sel));
577 case BE_SCHED_SELECT_HMUCHNIK:
579 memcpy(&sel, trivial_selector, sizeof(sel));
582 /* Assure, that the out edges are computed */
583 edges_deactivate(birg->irg);
584 edges_activate(birg->irg);
586 switch (list_sched_options.prep) {
587 case BE_SCHED_PREP_MRIS:
588 mris = be_sched_mris_preprocess(birg);
590 case BE_SCHED_PREP_RSS:
591 rss_schedule_preparation(birg);
597 num_nodes = get_irg_last_idx(irg);
599 /* initialize environment for list scheduler */
600 memset(&env, 0, sizeof(env));
601 env.selector = arch_env->isa->impl->get_list_sched_selector(arch_env->isa, &sel);
602 env.arch_env = arch_env;
604 env.sched_info = NEW_ARR_F(sched_irn_t, num_nodes);
606 memset(env.sched_info, 0, num_nodes * sizeof(env.sched_info[0]));
608 if (env.selector->init_graph)
609 env.selector_env = env.selector->init_graph(env.selector, arch_env, irg);
611 /* Schedule each single block. */
612 irg_block_walk_graph(irg, list_sched_block, NULL, &env);
614 if (env.selector->finish_graph)
615 env.selector->finish_graph(env.selector_env);
617 if (list_sched_options.prep == BE_SCHED_PREP_MRIS)
618 be_sched_mris_free(mris);
620 DEL_ARR_F(env.sched_info);
623 /* List schedule a block. */
624 void list_sched_single_block(const be_irg_t *birg, ir_node *block, be_options_t *be_opts)
626 const arch_env_t *arch_env = birg->main_env->arch_env;
627 ir_graph *irg = birg->irg;
631 list_sched_selector_t sel;
633 /* Select a scheduler based on backend options */
634 switch (list_sched_options.select) {
635 case BE_SCHED_SELECT_TRIVIAL:
636 memcpy(&sel, trivial_selector, sizeof(sel));
638 case BE_SCHED_SELECT_RANDOM:
639 memcpy(&sel, random_selector, sizeof(sel));
641 case BE_SCHED_SELECT_REGPRESS:
642 memcpy(&sel, reg_pressure_selector, sizeof(sel));
644 case BE_SCHED_SELECT_MUCHNIK:
645 memcpy(&sel, muchnik_selector, sizeof(sel));
647 case BE_SCHED_SELECT_HEUR:
648 memcpy(&sel, heuristic_selector, sizeof(sel));
650 case BE_SCHED_SELECT_HMUCHNIK:
652 memcpy(&sel, trivial_selector, sizeof(sel));
655 /* Assure, that the out edges are computed */
656 edges_deactivate(birg->irg);
657 edges_activate(birg->irg);
659 num_nodes = get_irg_last_idx(irg);
661 /* initialize environment for list scheduler */
662 memset(&env, 0, sizeof(env));
663 env.selector = arch_env->isa->impl->get_list_sched_selector(arch_env->isa, &sel);
664 env.arch_env = arch_env;
666 env.sched_info = NEW_ARR_F(sched_irn_t, num_nodes);
668 memset(env.sched_info, 0, num_nodes * sizeof(env.sched_info[0]));
670 if (env.selector->init_graph)
671 env.selector_env = env.selector->init_graph(env.selector, arch_env, irg);
673 /* Schedule block. */
674 list_sched_block(block, &env);
676 if (env.selector->finish_graph)
677 env.selector->finish_graph(env.selector_env);
679 DEL_ARR_F(env.sched_info);
683 * Register list scheduler options.
685 void be_init_listsched(void) {
686 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
687 lc_opt_entry_t *sched_grp = lc_opt_get_grp(be_grp, "listsched");
689 lc_opt_add_table(sched_grp, list_sched_option_table);
692 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_listsched);