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"
35 #include "besched_t.h"
38 #include "belistsched.h"
39 #include "beschedmris.h"
40 #include "beschedrss.h"
45 #include <libcore/lc_opts.h>
46 #include <libcore/lc_opts_enum.h>
47 #endif /* WITH_LIBCORE */
50 BE_SCHED_SELECT_TRIVIAL = 0,
51 BE_SCHED_SELECT_REGPRESS = 1,
52 BE_SCHED_SELECT_MUCHNIK = 2,
53 BE_SCHED_SELECT_HEUR = 3,
54 BE_SCHED_SELECT_HMUCHNIK = 4,
55 BE_SCHED_SELECT_RANDOM = 5
59 BE_SCHED_PREP_NONE = 0,
60 BE_SCHED_PREP_MRIS = 2,
64 typedef struct _list_sched_options_t {
65 int select; /**< the node selector */
66 int prep; /**< schedule preparation */
67 } list_sched_options_t;
69 static list_sched_options_t list_sched_options = {
70 BE_SCHED_SELECT_HEUR, /* mueller heuristic selector */
71 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),
107 #endif /* WITH_LIBCORE */
110 * All scheduling info needed per node.
112 typedef struct _sched_irn_t {
113 unsigned num_not_sched_user; /**< The number of not yet scheduled users of this node */
114 unsigned already_sched : 1; /**< Set if this node is already scheduled */
118 * Scheduling environment for the whole graph.
120 typedef struct _sched_env_t {
121 sched_irn_t *sched_info; /**< scheduling info per node */
122 const list_sched_selector_t *selector; /**< The node selector. */
123 const arch_env_t *arch_env; /**< The architecture environment. */
124 const ir_graph *irg; /**< The graph to schedule. */
125 void *selector_env; /**< A pointer to give to the selector. */
129 * Environment for a block scheduler.
131 typedef struct _block_sched_env_t {
132 sched_irn_t *sched_info; /**< scheduling info per node, copied from the global scheduler object */
133 nodeset *cands; /**< the set of candidates */
134 ir_node *block; /**< the current block */
135 sched_env_t *sched_env; /**< the scheduler environment */
136 nodeset *live; /**< simple liveness during scheduling */
137 const list_sched_selector_t *selector;
138 void *selector_block_env;
139 DEBUG_ONLY(firm_dbg_module_t *dbg;)
143 * Returns non-zero if the node is already scheduled
145 static INLINE int is_already_scheduled(block_sched_env_t *env, ir_node *n)
147 int idx = get_irn_idx(n);
149 assert(idx < ARR_LEN(env->sched_info));
150 return env->sched_info[idx].already_sched;
154 * Mark a node as already scheduled
156 static INLINE void mark_already_scheduled(block_sched_env_t *env, ir_node *n)
158 int idx = get_irn_idx(n);
160 assert(idx < ARR_LEN(env->sched_info));
161 env->sched_info[idx].already_sched = 1;
165 * Try to put a node in the ready set.
166 * @param env The block scheduler environment.
167 * @param pred The previous scheduled node.
168 * @param irn The node to make ready.
169 * @return 1, if the node could be made ready, 0 else.
171 static INLINE int make_ready(block_sched_env_t *env, ir_node *pred, ir_node *irn)
175 /* Blocks cannot be scheduled. */
176 if (is_Block(irn) || get_irn_n_edges(irn) == 0)
180 * Check, if the given ir node is in a different block as the
181 * currently scheduled one. If that is so, don't make the node ready.
183 if (env->block != get_nodes_block(irn))
186 for (i = 0, n = get_irn_ins_or_deps(irn); i < n; ++i) {
187 ir_node *op = get_irn_in_or_dep(irn, i);
189 /* if irn is an End we have keep-alives and op might be a block, skip that */
191 assert(get_irn_op(irn) == op_End);
195 /* If the operand is local to the scheduled block and not yet
196 * scheduled, this nodes cannot be made ready, so exit. */
197 if (! is_already_scheduled(env, op) && get_nodes_block(op) == env->block)
201 nodeset_insert(env->cands, irn);
203 /* Notify selector about the ready node. */
204 if (env->selector->node_ready)
205 env->selector->node_ready(env->selector_block_env, irn, pred);
207 DB((env->dbg, LEVEL_2, "\tmaking ready: %+F\n", irn));
213 * Try, to make all users of a node ready.
214 * In fact, a usage node can only be made ready, if all its operands
215 * have already been scheduled yet. This is checked my make_ready().
216 * @param env The block schedule environment.
217 * @param irn The node, which usages (successors) are to be made ready.
219 static INLINE void make_users_ready(block_sched_env_t *env, ir_node *irn)
221 const ir_edge_t *edge;
223 foreach_out_edge(irn, edge) {
224 ir_node *user = get_edge_src_irn(edge);
226 make_ready(env, irn, user);
229 foreach_out_edge_kind(irn, edge, EDGE_KIND_DEP) {
230 ir_node *user = get_edge_src_irn(edge);
232 make_ready(env, irn, user);
237 * Returns the number of not yet schedules users.
239 static INLINE int get_irn_not_sched_user(block_sched_env_t *env, ir_node *n) {
240 int idx = get_irn_idx(n);
242 assert(idx < ARR_LEN(env->sched_info));
243 return env->sched_info[idx].num_not_sched_user;
247 * Sets the number of not yet schedules users.
249 static INLINE void set_irn_not_sched_user(block_sched_env_t *env, ir_node *n, int num) {
250 int idx = get_irn_idx(n);
252 assert(idx < ARR_LEN(env->sched_info));
253 env->sched_info[idx].num_not_sched_user = num;
257 * Add @p num to the number of not yet schedules users and returns the result.
259 static INLINE int add_irn_not_sched_user(block_sched_env_t *env, ir_node *n, int num) {
260 int idx = get_irn_idx(n);
262 assert(idx < ARR_LEN(env->sched_info));
263 env->sched_info[idx].num_not_sched_user += num;
264 return env->sched_info[idx].num_not_sched_user;
268 * Returns the number of users of a node having mode datab.
270 static int get_num_successors(ir_node *irn) {
272 const ir_edge_t *edge;
274 if (get_irn_mode(irn) == mode_T) {
275 /* for mode_T nodes: count the users of all Projs */
276 foreach_out_edge(irn, edge) {
277 ir_node *proj = get_edge_src_irn(edge);
278 ir_mode *mode = get_irn_mode(proj);
281 sum += get_num_successors(proj);
282 else if (mode_is_datab(mode))
283 sum += get_irn_n_edges(proj);
287 /* do not count keep-alive edges */
288 foreach_out_edge(irn, edge) {
289 if (get_irn_opcode(get_edge_src_irn(edge)) != iro_End)
298 * Adds irn to @p live, updates all inputs that this user is scheduled
299 * and counts all of it's non scheduled users.
301 static void update_sched_liveness(block_sched_env_t *env, ir_node *irn) {
308 for (i = get_irn_ins_or_deps(irn) - 1; i >= 0; --i) {
309 ir_node *in = get_irn_in_or_dep(irn, i);
311 /* if in is a proj: update predecessor */
313 in = get_Proj_pred(in);
315 /* if in is still in the live set: reduce number of users by one */
316 if (nodeset_find(env->live, in)) {
317 if (add_irn_not_sched_user(env, in, -1) <= 0)
318 nodeset_remove(env->live, in);
323 get_num_successors returns the number of all users. This includes
324 users in different blocks as well. As the each block is scheduled separately
325 the liveness info of those users will not be updated and so these
326 users will keep up the register pressure as it is desired.
328 i = get_num_successors(irn);
330 set_irn_not_sched_user(env, irn, i);
331 nodeset_insert(env->live, irn);
335 static INLINE int must_appear_in_schedule(const list_sched_selector_t *sel, void *block_env, const ir_node *irn)
339 if(sel->to_appear_in_schedule)
340 res = sel->to_appear_in_schedule(block_env, irn);
342 return res >= 0 ? res : (to_appear_in_schedule(irn) || be_is_Keep(irn) || be_is_CopyKeep(irn) || be_is_RegParams(irn));
346 * Append an instruction to a schedule.
347 * @param env The block scheduling environment.
348 * @param irn The node to add to the schedule.
349 * @return The given node.
351 static ir_node *add_to_sched(block_sched_env_t *env, ir_node *irn)
353 /* If the node consumes/produces data, it is appended to the schedule
354 * list, otherwise, it is not put into the list */
355 if (must_appear_in_schedule(env->selector, env->selector_block_env, irn)) {
356 update_sched_liveness(env, irn);
357 sched_add_before(env->block, irn);
359 DBG((env->dbg, LEVEL_2, "\tadding %+F\n", irn));
362 /* notify the selector about the finally selected node. */
363 if (env->selector->node_selected)
364 env->selector->node_selected(env->selector_block_env, irn);
366 /* Insert the node in the set of all already scheduled nodes. */
367 mark_already_scheduled(env, irn);
369 /* Remove the node from the ready set */
370 if(nodeset_find(env->cands, irn))
371 nodeset_remove(env->cands, irn);
377 * Add the proj nodes of a tuple-mode irn to the schedule immediately
378 * after the tuple-moded irn. By pinning the projs after the irn, no
379 * other nodes can create a new lifetime between the tuple-moded irn and
380 * one of its projs. This should render a realistic image of a
381 * tuple-moded irn, which in fact models a node which defines multiple
384 * @param irn The tuple-moded irn.
386 static void add_tuple_projs(block_sched_env_t *env, ir_node *irn)
388 const ir_edge_t *edge;
390 assert(get_irn_mode(irn) == mode_T && "Mode of node must be tuple");
396 /* non-proj nodes can have dependency edges to tuple nodes. */
397 foreach_out_edge_kind(irn, edge, EDGE_KIND_DEP) {
398 ir_node *out = get_edge_src_irn(edge);
399 make_ready(env, irn, out);
402 /* schedule the normal projs */
403 foreach_out_edge(irn, edge) {
404 ir_node *out = get_edge_src_irn(edge);
406 assert(is_Proj(out) && "successor of a modeT node must be a proj");
408 if (get_irn_mode(out) == mode_T)
409 add_tuple_projs(env, out);
411 add_to_sched(env, out);
412 make_users_ready(env, out);
418 * Perform list scheduling on a block.
420 * Note, that the caller must compute a linked list of nodes in the block
421 * using the link field before calling this function.
423 * Also the outs must have been computed.
425 * @param block The block node.
426 * @param env Scheduling environment.
428 static void list_sched_block(ir_node *block, void *env_ptr)
430 sched_env_t *env = env_ptr;
431 const list_sched_selector_t *selector = env->selector;
432 ir_node *start_node = get_irg_start(get_irn_irg(block));
434 block_sched_env_t be;
435 const ir_edge_t *edge;
439 /* Initialize the block's list head that will hold the schedule. */
440 sched_init_block(block);
442 /* Initialize the block scheduling environment */
443 be.sched_info = env->sched_info;
445 be.cands = new_nodeset(get_irn_n_edges(block));
446 be.live = new_nodeset(get_irn_n_edges(block));
447 be.selector = selector;
449 FIRM_DBG_REGISTER(be.dbg, "firm.be.sched");
451 DBG((be.dbg, LEVEL_1, "scheduling %+F\n", block));
453 if (selector->init_block)
454 be.selector_block_env = selector->init_block(env->selector_env, block);
456 /* Then one can add all nodes are ready to the set. */
457 foreach_out_edge(block, edge) {
458 ir_node *irn = get_edge_src_irn(edge);
460 /* Skip the end node because of keepalive edges. */
461 if (get_irn_opcode(irn) == iro_End)
464 if (get_irn_n_edges(irn) == 0)
469 Phi functions are scheduled immediately, since they only
470 transfer data flow from the predecessors to this block.
472 add_to_sched(&be, irn);
473 make_users_ready(&be, irn);
475 else if (irn == start_node) {
476 /* The start block will be scheduled as the first node */
477 add_to_sched(&be, irn);
478 add_tuple_projs(&be, irn);
481 /* Other nodes must have all operands in other blocks to be made
485 /* Check, if the operands of a node are not local to this block */
486 for (j = 0, m = get_irn_ins_or_deps(irn); j < m; ++j) {
487 ir_node *operand = get_irn_in_or_dep(irn, j);
489 if (get_nodes_block(operand) == block) {
494 /* live in values increase register pressure */
495 update_sched_liveness(&be, operand);
499 /* Make the node ready, if all operands live in a foreign block */
501 DBG((be.dbg, LEVEL_2, "\timmediately ready: %+F\n", irn));
502 make_ready(&be, NULL, irn);
507 /* Iterate over all remaining nodes */
508 while (nodeset_count(be.cands) > 0) {
509 /* collect statistics about amount of ready nodes */
510 be_do_stat_sched_ready(block, be.cands);
512 /* Keeps must be scheduled immediatly */
513 foreach_nodeset(be.cands, irn) {
514 if (be_is_Keep(irn) || be_is_CopyKeep(irn) || get_irn_mode(irn) == mode_M) {
515 nodeset_break(be.cands);
521 /* Keeps must be immediately scheduled */
522 irn = be.selector->select(be.selector_block_env, be.cands, be.live);
525 DB((be.dbg, LEVEL_2, "\tpicked node %+F\n", irn));
527 /* Add the node to the schedule. */
528 add_to_sched(&be, irn);
530 if (get_irn_mode(irn) == mode_T)
531 add_tuple_projs(&be, irn);
533 make_users_ready(&be, irn);
535 /* remove the scheduled node from the ready list. */
536 if (nodeset_find(be.cands, irn))
537 nodeset_remove(be.cands, irn);
540 if (selector->finish_block)
541 selector->finish_block(be.selector_block_env);
543 del_nodeset(be.cands);
544 del_nodeset(be.live);
547 /* List schedule a graph. */
548 void list_sched(const be_irg_t *birg, be_options_t *be_opts)
550 const arch_env_t *arch_env = birg->main_env->arch_env;
551 ir_graph *irg = birg->irg;
555 mris_env_t *mris = NULL;
556 list_sched_selector_t sel;
558 /* Select a scheduler based on backend options */
559 switch (list_sched_options.select) {
560 case BE_SCHED_SELECT_TRIVIAL:
561 memcpy(&sel, trivial_selector, sizeof(sel));
563 case BE_SCHED_SELECT_RANDOM:
564 memcpy(&sel, random_selector, sizeof(sel));
566 case BE_SCHED_SELECT_REGPRESS:
567 memcpy(&sel, reg_pressure_selector, sizeof(sel));
569 case BE_SCHED_SELECT_MUCHNIK:
570 memcpy(&sel, muchnik_selector, sizeof(sel));
572 case BE_SCHED_SELECT_HEUR:
573 memcpy(&sel, heuristic_selector, sizeof(sel));
575 case BE_SCHED_SELECT_HMUCHNIK:
577 memcpy(&sel, trivial_selector, sizeof(sel));
580 /* Assure, that the out edges are computed */
581 edges_deactivate(birg->irg);
582 edges_activate(birg->irg);
584 switch (list_sched_options.prep) {
585 case BE_SCHED_PREP_MRIS:
586 mris = be_sched_mris_preprocess(birg);
588 case BE_SCHED_PREP_RSS:
589 rss_schedule_preparation(birg);
595 num_nodes = get_irg_last_idx(irg);
597 /* initialize environment for list scheduler */
598 memset(&env, 0, sizeof(env));
599 env.selector = arch_env->isa->impl->get_list_sched_selector(arch_env->isa, &sel);
600 env.arch_env = arch_env;
602 env.sched_info = NEW_ARR_F(sched_irn_t, num_nodes);
604 memset(env.sched_info, 0, num_nodes * sizeof(env.sched_info[0]));
606 if (env.selector->init_graph)
607 env.selector_env = env.selector->init_graph(env.selector, arch_env, irg);
609 /* Schedule each single block. */
610 irg_block_walk_graph(irg, list_sched_block, NULL, &env);
612 if (env.selector->finish_graph)
613 env.selector->finish_graph(env.selector_env);
615 if (list_sched_options.prep == BE_SCHED_PREP_MRIS)
616 be_sched_mris_free(mris);
618 DEL_ARR_F(env.sched_info);
623 * Register list scheduler options.
625 void list_sched_register_options(lc_opt_entry_t *grp) {
626 static int run_once = 0;
627 lc_opt_entry_t *sched_grp;
631 sched_grp = lc_opt_get_grp(grp, "listsched");
633 lc_opt_add_table(sched_grp, list_sched_option_table);
634 rss_register_options(sched_grp);
637 #endif /* WITH_LIBCORE */