2 * Scheduling algorithms.
3 * Just a simple list scheduling algorithm is here.
5 * @author Sebastian Hack
23 #include "iredges_t.h"
28 #include "irprintf_t.h"
31 #include "besched_t.h"
34 #include "belistsched.h"
37 #define MAX(x,y) ((x) > (y) ? (x) : (y))
38 #define MIN(x,y) ((x) < (y) ? (x) : (y))
41 * Scheduling environment for the whole graph.
43 typedef struct _sched_env_t {
44 const ir_graph *irg; /**< The graph to schedule. */
45 const list_sched_selector_t *selector; /**< The node selector. */
46 void *selector_env; /**< A pointer to give to the selector. */
51 * Ugly global variable for the compare function
52 * since qsort(3) does not pass an extra pointer.
54 static ir_node *curr_bl = NULL;
56 static int cmp_usage(const void *a, const void *b)
58 struct trivial_sched_env *env;
63 res = is_live_end(env->curr_bl, a) - is_live_end(env->curr_bl, b);
66 * One of them is live at the end of the block.
67 * Then, that one shall be scheduled at after the other
77 static ir_node *trivial_select(void *block_env, pset *ready_set)
81 res = pset_first(ready_set);
82 pset_break(ready_set);
86 static INLINE int must_appear_in_schedule(const list_sched_selector_t *sel, void *block_env, const ir_node *irn)
90 if(sel->to_appear_in_schedule)
91 res = sel->to_appear_in_schedule(block_env, irn);
93 return res || to_appear_in_schedule(irn) || be_is_Keep(irn);
96 static const list_sched_selector_t trivial_selector_struct = {
105 const list_sched_selector_t *trivial_selector = &trivial_selector_struct;
107 typedef struct _usage_stats_t {
109 struct _usage_stats_t *next;
111 int uses_in_block; /**< Number of uses inside the current block. */
112 int already_consumed; /**< Number of insns using this value already
119 pset *already_scheduled;
120 const list_sched_selector_t *vtab;
121 } reg_pressure_selector_env_t;
123 static INLINE usage_stats_t *get_or_set_usage_stats(reg_pressure_selector_env_t *env, ir_node *irn)
125 usage_stats_t *us = get_irn_link(irn);
128 us = obstack_alloc(&env->obst, sizeof(us[0]));
130 us->already_consumed = 0;
131 us->max_hops = INT_MAX;
132 us->next = env->root;
134 set_irn_link(irn, us);
140 static INLINE usage_stats_t *get_usage_stats(ir_node *irn)
142 usage_stats_t *us = get_irn_link(irn);
143 assert(us && "This node must have usage stats");
147 static int max_hops_walker(ir_node *irn, ir_node *tgt, int depth, unsigned visited_nr)
155 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
156 ir_node *operand = get_irn_n(irn, i);
158 if(get_irn_visited(operand) < visited_nr) {
161 set_irn_visited(operand, visited_nr);
162 tmp = max_hops_walker(operand, tgt, depth + 1, visited_nr);
171 static int compute_max_hops(reg_pressure_selector_env_t *env, ir_node *irn)
173 ir_node *bl = get_nodes_block(irn);
174 ir_graph *irg = get_irn_irg(bl);
177 const ir_edge_t *edge;
179 foreach_out_edge(irn, edge) {
180 ir_node *user = get_edge_src_irn(edge);
182 if(get_nodes_block(user) == bl && !pset_find_ptr(env->already_scheduled, user)) {
183 unsigned visited_nr = get_irg_visited(irg) + 1;
186 set_irg_visited(irg, visited_nr);
187 max_hops = max_hops_walker(user, irn, 0, visited_nr);
188 res = MAX(res, max_hops);
195 static void *reg_pressure_graph_init(const list_sched_selector_t *vtab, const arch_isa_t *isa, ir_graph *irg)
197 irg_walk_graph(irg, firm_clear_link, NULL, NULL);
198 return (void *) vtab;
201 static void *reg_pressure_block_init(void *graph_env, ir_node *bl)
204 reg_pressure_selector_env_t *env = xmalloc(sizeof(env[0]));
206 obstack_init(&env->obst);
207 env->already_scheduled = pset_new_ptr(32);
209 env->vtab = graph_env;
212 * Collect usage statistics.
214 sched_foreach(bl, irn) {
215 if(must_appear_in_schedule(env->vtab, env, irn)) {
218 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
219 ir_node *op = get_irn_n(irn, i);
220 if(must_appear_in_schedule(env->vtab, env, irn)) {
221 usage_stats_t *us = get_or_set_usage_stats(env, irn);
222 if(is_live_end(bl, op))
223 us->uses_in_block = 99999;
234 static void reg_pressure_block_free(void *block_env)
236 reg_pressure_selector_env_t *env = block_env;
239 for(us = env->root; us; us = us->next)
240 set_irn_link(us->irn, NULL);
242 obstack_free(&env->obst, NULL);
243 del_pset(env->already_scheduled);
247 static INLINE int reg_pr_costs(reg_pressure_selector_env_t *env, ir_node *irn)
252 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
253 ir_node *op = get_irn_n(irn, i);
255 if(must_appear_in_schedule(env->vtab, env, op))
256 sum += compute_max_hops(env, op);
262 ir_node *reg_pressure_select(void *block_env, pset *ready_set)
264 reg_pressure_selector_env_t *env = block_env;
265 ir_node *irn, *res = NULL;
266 int curr_cost = INT_MAX;
268 assert(pset_count(ready_set) > 0);
270 for(irn = pset_first(ready_set); irn; irn = pset_next(ready_set)) {
271 int costs = reg_pr_costs(env, irn);
272 if(costs <= curr_cost) {
278 pset_insert_ptr(env->already_scheduled, res);
282 static const list_sched_selector_t reg_pressure_selector_struct = {
283 reg_pressure_graph_init,
284 reg_pressure_block_init,
287 reg_pressure_block_free,
291 const list_sched_selector_t *reg_pressure_selector = ®_pressure_selector_struct;
293 static void list_sched_block(ir_node *block, void *env_ptr);
295 void list_sched(const struct _arch_isa_t *isa, ir_graph *irg)
298 const list_sched_selector_t *selector;
300 memset(&env, 0, sizeof(env));
301 selector = env.selector = isa->impl->get_list_sched_selector(isa);
302 env.selector_env = selector->init_graph ? selector->init_graph(selector, isa, irg) : NULL;
305 /* Assure, that the out edges are computed */
308 /* Schedule each single block. */
309 irg_block_walk_graph(irg, list_sched_block, NULL, &env);
311 if(selector->finish_graph)
312 selector->finish_graph(env.selector_env);
317 * Environment for a block scheduler.
319 typedef struct _block_sched_env_t {
322 pset *already_scheduled;
324 firm_dbg_module_t *dbg;
325 const list_sched_selector_t *selector;
326 void *selector_block_env;
330 * Try to put a node in the ready set.
331 * @param env The block scheduler environment.
332 * @param irn The node to make ready.
333 * @return 1, if the node could be made ready, 0 else.
335 static INLINE int make_ready(block_sched_env_t *env, ir_node *irn)
339 /* Blocks cannot be scheduled. */
344 * Check, if the given ir node is in a different block as the
345 * currently scheduled one. If that is so, don't make the node ready.
347 if(env->block != get_nodes_block(irn))
350 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
351 ir_node *op = get_irn_n(irn, i);
353 /* If the operand is local to the scheduled block and not yet
354 * scheduled, this nodes cannot be made ready, so exit. */
355 if(!pset_find_ptr(env->already_scheduled, op) && get_nodes_block(op) == env->block)
359 DBG((env->dbg, LEVEL_2, "\tmaking ready: %+F\n", irn));
360 pset_insert_ptr(env->ready_set, irn);
366 * Check, if a node is ready in a block schedule.
367 * @param env The block schedule environment.
368 * @param irn The node to check for.
369 * @return 1 if the node was ready, 0 if not.
371 #define is_ready(env,irn) \
372 (pset_find_ptr((env)->ready_set, irn) != NULL)
375 * Check, if a node has already been schedules.
376 * @param env The block schedule environment.
377 * @param irn The node to check for.
378 * @return 1 if the node was already scheduled, 0 if not.
380 #define is_scheduled(env,irn) \
381 (pset_find_ptr((env)->already_scheduled, irn) != NULL)
384 * Try, to make all users of a node ready.
385 * In fact, a usage node can only be made ready, if all its operands
386 * have already been scheduled yet. This is checked my make_ready().
387 * @param env The block schedule environment.
388 * @param irn The node, which usages (successors) are to be made ready.
390 static INLINE void make_users_ready(block_sched_env_t *env, ir_node *irn)
392 const ir_edge_t *edge;
394 foreach_out_edge(irn, edge) {
395 ir_node *user = edge->src;
397 make_ready(env, user);
402 * Compare to nodes using pointer equality.
403 * @param p1 Node one.
404 * @param p2 Node two.
405 * @return 0 if they are identical.
407 static int node_cmp_func(const void *p1, const void *p2)
413 * Append an instruction to a schedule.
414 * @param env The block scheduling environment.
415 * @param irn The node to add to the schedule.
416 * @return The given node.
418 static ir_node *add_to_sched(block_sched_env_t *env, ir_node *irn)
420 /* If the node consumes/produces data, it is appended to the schedule
421 * list, otherwise, it is not put into the list */
422 if(must_appear_in_schedule(env->selector, env->selector_block_env, irn)) {
423 sched_info_t *info = get_irn_sched_info(irn);
424 INIT_LIST_HEAD(&info->list);
426 sched_add_before(env->block, irn);
428 DBG((env->dbg, LEVEL_2, "\tadding %+F\n", irn));
431 /* Insert the node in the set of all already scheduled nodes. */
432 pset_insert_ptr(env->already_scheduled, irn);
434 /* Remove the node from the ready set */
435 if(pset_find_ptr(env->ready_set, irn))
436 pset_remove_ptr(env->ready_set, irn);
442 * Add the proj nodes of a tuple-mode irn to the schedule immediately
443 * after the tuple-moded irn. By pinning the projs after the irn, no
444 * other nodes can create a new lifetime between the tuple-moded irn and
445 * one of its projs. This should render a realistic image of a
446 * tuple-moded irn, which in fact models a node which defines multiple
449 * @param irn The tuple-moded irn.
450 * @param list The schedule list to append all the projs.
451 * @param time The time step to which the irn and all its projs are
453 * @param obst The obstack the scheduling data structures shall be
455 * @param ready_set The ready set of the list scheduler.
456 * @param already_scheduled A set containing all nodes already
459 static void add_tuple_projs(block_sched_env_t *env, ir_node *irn)
461 const ir_edge_t *edge;
463 assert(get_irn_mode(irn) == mode_T && "Mode of node must be tuple");
465 foreach_out_edge(irn, edge) {
466 ir_node *out = edge->src;
468 assert(is_Proj(out) && "successor of a modeT node must be a proj");
470 if(get_irn_mode(out) == mode_T)
471 add_tuple_projs(env, out);
473 add_to_sched(env, out);
474 make_users_ready(env, out);
479 static ir_node *select_node(block_sched_env_t *be)
483 for(irn = pset_first(be->ready_set); irn; irn = pset_next(be->ready_set)) {
484 if(be_is_Keep(irn)) {
485 pset_break(be->ready_set);
490 return be->selector->select(be->selector_block_env, be->ready_set);
494 * Perform list scheduling on a block.
496 * Note, that the caller must compute a linked list of nodes in the block
497 * using the link field before calling this function.
499 * Also the outs must have been computed.
501 * @param block The block node.
502 * @param env Scheduling environment.
504 static void list_sched_block(ir_node *block, void *env_ptr)
506 sched_env_t *env = env_ptr;
507 block_sched_env_t be;
508 const list_sched_selector_t *selector = env->selector;
509 const ir_edge_t *edge;
511 ir_node *start_node = get_irg_start(get_irn_irg(block));
512 ir_node *final_jmp = NULL;
515 sched_info_t *info = get_irn_sched_info(block);
517 /* Initialize the block's list head that will hold the schedule. */
518 INIT_LIST_HEAD(&info->list);
520 /* Initialize the block scheduling environment */
521 be.dbg = firm_dbg_register("firm.be.sched");
524 be.ready_set = new_pset(node_cmp_func, get_irn_n_edges(block));
525 be.already_scheduled = new_pset(node_cmp_func, get_irn_n_edges(block));
526 be.selector = selector;
528 if(selector->init_block)
529 be.selector_block_env = selector->init_block(env->selector_env, block);
531 DBG((be.dbg, LEVEL_1, "scheduling %+F\n", block));
533 /* Then one can add all nodes are ready to the set. */
534 foreach_out_edge(block, edge) {
535 ir_node *irn = get_edge_src_irn(edge);
537 /* Skip the end node because of keepalive edges. */
538 if(get_irn_opcode(irn) == iro_End)
541 /* Phi functions are scheduled immediately, since they only transfer
542 * data flow from the predecessors to this block. */
544 add_to_sched(&be, irn);
545 make_users_ready(&be, irn);
549 else if(irn == start_node) {
550 add_to_sched(&be, irn);
551 add_tuple_projs(&be, irn);
554 else if(get_irn_opcode(irn) == iro_Jmp) {
558 /* Other nodes must have all operands in other blocks to be made
563 /* Check, if the operands of a node are not local to this block */
564 for(j = 0, m = get_irn_arity(irn); j < m; ++j) {
565 ir_node *operand = get_irn_n(irn, j);
567 if(get_nodes_block(operand) == block) {
573 /* Make the node ready, if all operands live in a foreign block */
575 DBG((be.dbg, LEVEL_2, "\timmediately ready: %+F\n", irn));
576 make_ready(&be, irn);
581 /* Increase the time, if some phi functions have been scheduled */
582 be.curr_time += phi_seen;
584 while(pset_count(be.ready_set) > 0) {
585 /* select a node to be scheduled and check if it was ready */
586 irn = select_node(&be);
588 DBG((be.dbg, LEVEL_3, "\tpicked node %+F\n", irn));
590 /* Add the node to the schedule. */
591 add_to_sched(&be, irn);
593 if(get_irn_mode(irn) == mode_T)
594 add_tuple_projs(&be, irn);
596 make_users_ready(&be, irn);
598 /* Increase the time step. */
601 /* remove the scheduled node from the ready list. */
602 if(pset_find_ptr(be.ready_set, irn))
603 pset_remove_ptr(be.ready_set, irn);
606 if(selector->finish_block)
607 selector->finish_block(be.selector_block_env);
610 add_to_sched(&be, final_jmp);
612 del_pset(be.ready_set);
613 del_pset(be.already_scheduled);