2 * Copyright (C) 1995-2011 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 Implements a trace scheduler as presented in Muchnik[TM].
23 * @author Michael Beck
31 #include "iredges_t.h"
34 #include "belistsched.h"
39 /* we need a special mark */
43 typedef struct trace_irn {
44 sched_timestep_t delay; /**< The delay for this node if already calculated, else 0. */
45 sched_timestep_t etime; /**< The earliest time of this node. */
46 unsigned num_user; /**< The number real users (mode datab) of this node */
47 int reg_diff; /**< The difference of num(out registers) - num(in registers) */
48 int preorder; /**< The pre-order position */
49 unsigned critical_path_len; /**< The weighted length of the longest critical path */
50 unsigned is_root : 1; /**< is a root node of a block */
53 typedef struct trace_env {
54 trace_irn_t *sched_info; /**< trace scheduling information about the nodes */
55 sched_timestep_t curr_time; /**< current time of the scheduler */
56 be_lv_t *liveness; /**< The liveness for the irg */
57 DEBUG_ONLY(firm_dbg_module_t *dbg;)
61 * Returns a random node from a nodeset
63 static ir_node *get_nodeset_node(const ir_nodeset_t *nodeset)
65 ir_nodeset_iterator_t iter;
67 ir_nodeset_iterator_init(&iter, nodeset);
68 return ir_nodeset_iterator_next(&iter);
72 * Returns non-zero if the node is a root node
74 static inline unsigned is_root_node(trace_env_t *env, ir_node *n)
76 unsigned const idx = get_irn_idx(n);
78 assert(idx < ARR_LEN(env->sched_info));
79 return env->sched_info[idx].is_root;
83 * Mark a node as root node
85 static inline void mark_root_node(trace_env_t *env, ir_node *n)
87 unsigned const idx = get_irn_idx(n);
89 assert(idx < ARR_LEN(env->sched_info));
90 env->sched_info[idx].is_root = 1;
94 * Get the current delay.
96 static inline sched_timestep_t get_irn_delay(trace_env_t *env, ir_node *n)
98 unsigned const idx = get_irn_idx(n);
100 assert(idx < ARR_LEN(env->sched_info));
101 return env->sched_info[idx].delay;
105 * Set the current delay.
107 static inline void set_irn_delay(trace_env_t *env, ir_node *n, sched_timestep_t delay)
109 unsigned const idx = get_irn_idx(n);
111 assert(idx < ARR_LEN(env->sched_info));
112 env->sched_info[idx].delay = delay;
116 * Get the current etime.
118 static inline sched_timestep_t get_irn_etime(trace_env_t *env, ir_node *n)
120 unsigned const idx = get_irn_idx(n);
122 assert(idx < ARR_LEN(env->sched_info));
123 return env->sched_info[idx].etime;
127 * Set the current etime.
129 static inline void set_irn_etime(trace_env_t *env, ir_node *n, sched_timestep_t etime)
131 unsigned const idx = get_irn_idx(n);
133 assert(idx < ARR_LEN(env->sched_info));
134 env->sched_info[idx].etime = etime;
138 * Get the number of users.
140 static inline unsigned get_irn_num_user(trace_env_t *env, ir_node *n)
142 unsigned const idx = get_irn_idx(n);
144 assert(idx < ARR_LEN(env->sched_info));
145 return env->sched_info[idx].num_user;
149 * Set the number of users.
151 static inline void set_irn_num_user(trace_env_t *env, ir_node *n, unsigned num_user)
153 unsigned const idx = get_irn_idx(n);
155 assert(idx < ARR_LEN(env->sched_info));
156 env->sched_info[idx].num_user = num_user;
160 * Get the register difference.
162 static inline int get_irn_reg_diff(trace_env_t *env, ir_node *n)
164 unsigned const idx = get_irn_idx(n);
166 assert(idx < ARR_LEN(env->sched_info));
167 return env->sched_info[idx].reg_diff;
171 * Set the register difference.
173 static inline void set_irn_reg_diff(trace_env_t *env, ir_node *n, int reg_diff)
175 unsigned const idx = get_irn_idx(n);
177 assert(idx < ARR_LEN(env->sched_info));
178 env->sched_info[idx].reg_diff = reg_diff;
182 * Get the pre-order position.
184 static inline int get_irn_preorder(trace_env_t *env, ir_node *n)
186 unsigned const idx = get_irn_idx(n);
188 assert(idx < ARR_LEN(env->sched_info));
189 return env->sched_info[idx].preorder;
193 * Set the pre-order position.
195 static inline void set_irn_preorder(trace_env_t *env, ir_node *n, int pos)
197 unsigned const idx = get_irn_idx(n);
199 assert(idx < ARR_LEN(env->sched_info));
200 env->sched_info[idx].preorder = pos;
204 * Get the pre-order position.
206 static inline unsigned get_irn_critical_path_len(trace_env_t *env, ir_node *n)
208 unsigned const idx = get_irn_idx(n);
210 assert(idx < ARR_LEN(env->sched_info));
211 return env->sched_info[idx].critical_path_len;
215 * Set the pre-order position.
217 static inline void set_irn_critical_path_len(trace_env_t *env, ir_node *n, unsigned len)
219 unsigned const idx = get_irn_idx(n);
221 assert(idx < ARR_LEN(env->sched_info));
222 env->sched_info[idx].critical_path_len = len;
226 * returns the exec-time for node n.
228 static sched_timestep_t exectime(trace_env_t *env, ir_node *n)
231 if (be_is_Keep(n) || is_Proj(n))
234 if (env->selector->exectime)
235 return env->selector->exectime(env->selector_env, n);
241 * Calculates the latency for between two ops
243 static sched_timestep_t latency(trace_env_t *env, ir_node *pred, int pred_cycle, ir_node *curr, int curr_cycle)
247 /* a Keep hides a root */
248 if (be_is_Keep(curr))
249 return exectime(env, pred);
251 /* Proj's are executed immediately */
255 /* predecessors Proj's must be skipped */
257 pred = get_Proj_pred(pred);
260 if (env->selector->latency)
261 return env->selector->latency(env->selector_env, pred, pred_cycle, curr, curr_cycle);
268 * Returns the number of users of a node having mode datab.
270 static int get_num_successors(ir_node *irn)
273 const ir_edge_t *edge;
275 if (get_irn_mode(irn) == mode_T) {
276 /* for mode_T nodes: count the users of all Projs */
277 foreach_out_edge(irn, edge) {
278 ir_node *proj = get_edge_src_irn(edge);
279 ir_mode *mode = get_irn_mode(proj);
282 sum += get_num_successors(proj);
283 else if (mode_is_datab(mode))
284 sum += get_irn_n_edges(proj);
288 /* do not count keep-alive edges */
289 foreach_out_edge(irn, edge) {
290 if (get_irn_opcode(get_edge_src_irn(edge)) != iro_End)
299 * Returns the difference of regs_output - regs_input;
301 static int get_reg_difference(trace_env_t *env, ir_node *irn)
306 ir_node *block = get_nodes_block(irn);
308 if (be_is_Call(irn)) {
309 /* we want calls preferred */
313 if (get_irn_mode(irn) == mode_T) {
314 /* mode_T nodes: num out regs == num Projs with mode datab */
315 const ir_edge_t *edge;
316 foreach_out_edge(irn, edge) {
317 ir_node *proj = get_edge_src_irn(edge);
318 if (mode_is_datab(get_irn_mode(proj)))
325 /* num in regs: number of ins with mode datab and not ignore */
326 for (i = get_irn_arity(irn) - 1; i >= 0; i--) {
327 ir_node *in = get_irn_n(irn, i);
329 if (!mode_is_datab(get_irn_mode(in)))
332 if (arch_irn_is_ignore(in))
335 if (be_is_live_end(env->liveness, block, in))
341 return num_out - num_in;
345 * descent into a dag and create a pre-order list.
347 static void descent(ir_node *root, ir_node *block, ir_node **list, trace_env_t *env, unsigned path_len)
351 if (! is_Phi(root)) {
352 path_len += exectime(env, root);
353 if (get_irn_critical_path_len(env, root) < path_len) {
354 set_irn_critical_path_len(env, root, path_len);
356 /* calculate number of users (needed for heuristic) */
357 set_irn_num_user(env, root, get_num_successors(root));
359 /* calculate register difference (needed for heuristic) */
360 set_irn_reg_diff(env, root, get_reg_difference(env, root));
362 /* Phi nodes always leave the block */
363 for (i = get_irn_arity(root) - 1; i >= 0; --i) {
364 ir_node *pred = get_irn_n(root, i);
366 DBG((env->dbg, LEVEL_3, " node %+F\n", pred));
368 /* Blocks may happen as predecessors of End nodes */
372 /* already seen nodes are not marked */
373 if (get_irn_link(pred) != MARK)
376 /* don't leave our block */
377 if (get_nodes_block(pred) != block)
380 set_irn_link(pred, NULL);
382 descent(pred, block, list, env, path_len);
385 set_irn_link(root, *list);
390 * Returns non-zero if root is a root in the block block.
392 static int is_root(ir_node *root, ir_node *block)
394 const ir_edge_t *edge;
396 foreach_out_edge(root, edge) {
397 ir_node *succ = get_edge_src_irn(edge);
401 /* Phi nodes are always in "another block */
404 if (get_nodes_block(succ) == block)
411 * Performs initial block calculations for trace scheduling.
413 static void trace_preprocess_block(trace_env_t *env, ir_node *block)
415 ir_node *root = NULL, *preord = NULL;
418 const ir_edge_t *edge;
420 /* First step: Find the root set. */
421 foreach_out_edge(block, edge) {
422 ir_node *succ = get_edge_src_irn(edge);
424 if (is_Anchor(succ)) {
425 /* ignore a keep alive edge */
428 if (is_root(succ, block)) {
429 mark_root_node(env, succ);
430 set_irn_link(succ, root);
434 set_irn_link(succ, MARK);
437 /* Second step: calculate the pre-order list. */
439 for (curr = root; curr; curr = irn) {
440 irn = (ir_node*)get_irn_link(curr);
441 DBG((env->dbg, LEVEL_2, " DAG root %+F\n", curr));
442 descent(curr, block, &preord, env, 0);
446 /* Third step: calculate the Delay. Note that our
447 * list is now in pre-order, starting at root
449 for (cur_pos = 0, curr = root; curr; curr = (ir_node*)get_irn_link(curr), cur_pos++) {
453 /* assure, that branches can be executed last */
457 if (is_root_node(env, curr))
458 d = exectime(env, curr);
461 foreach_out_edge(curr, edge) {
462 ir_node *n = get_edge_src_irn(edge);
464 if (get_nodes_block(n) == block) {
467 ld = latency(env, curr, 1, n, 0) + get_irn_delay(env, n);
473 set_irn_delay(env, curr, d);
474 DB((env->dbg, LEVEL_2, "\t%+F delay %u\n", curr, d));
476 /* set the etime of all nodes to 0 */
477 set_irn_etime(env, curr, 0);
479 set_irn_preorder(env, curr, cur_pos);
484 * This functions gets called after a node finally has been made ready.
486 static void trace_node_ready(void *data, ir_node *irn, ir_node *pred)
488 trace_env_t *env = (trace_env_t*)data;
489 sched_timestep_t etime_p, etime;
491 etime = env->curr_time;
493 etime_p = get_irn_etime(env, pred);
494 etime += latency(env, pred, 1, irn, 0);
495 etime = etime_p > etime ? etime_p : etime;
498 set_irn_etime(env, irn, etime);
499 DB((env->dbg, LEVEL_2, "\tset etime of %+F to %u\n", irn, etime));
503 * Update the current time after irn has been selected.
505 static void trace_update_time(void *data, ir_node *irn)
507 trace_env_t *env = (trace_env_t*)data;
508 if (is_Phi(irn) || get_irn_opcode(irn) == beo_Start) {
509 env->curr_time += get_irn_etime(env, irn);
512 env->curr_time += exectime(env, irn);
517 * Allocates memory and initializes trace scheduling environment.
518 * @param irg The backend irg object
519 * @return The environment
521 static trace_env_t *trace_init(ir_graph *irg)
523 trace_env_t *env = XMALLOCZ(trace_env_t);
524 int nn = get_irg_last_idx(irg);
527 env->sched_info = NEW_ARR_F(trace_irn_t, nn);
528 env->liveness = be_liveness(irg);
529 FIRM_DBG_REGISTER(env->dbg, "firm.be.sched.trace");
531 be_liveness_assure_chk(env->liveness);
532 memset(env->sched_info, 0, nn * sizeof(*(env->sched_info)));
538 * Frees all memory allocated for trace scheduling environment.
539 * @param env The environment
541 static void trace_free(void *data)
543 trace_env_t *env = (trace_env_t*)data;
544 be_liveness_free(env->liveness);
545 DEL_ARR_F(env->sched_info);
550 * Simple selector. Just assure that jumps are scheduled last.
552 static ir_node *basic_selection(ir_nodeset_t *ready_set)
555 ir_nodeset_iterator_t iter;
557 /* assure that branches and constants are executed last */
558 foreach_ir_nodeset(ready_set, irn, iter) {
564 /* at last: schedule branches */
565 irn = get_nodeset_node(ready_set);
571 * The muchnik selector.
573 static ir_node *muchnik_select(void *block_env, ir_nodeset_t *ready_set)
575 trace_env_t *env = (trace_env_t*)block_env;
576 ir_nodeset_t mcands, ecands;
577 ir_nodeset_iterator_t iter;
578 sched_timestep_t max_delay = 0;
581 /* calculate the max delay of all candidates */
582 foreach_ir_nodeset(ready_set, irn, iter) {
583 sched_timestep_t d = get_irn_delay(env, irn);
585 max_delay = d > max_delay ? d : max_delay;
588 ir_nodeset_init_size(&mcands, 8);
589 ir_nodeset_init_size(&ecands, 8);
591 /* build mcands and ecands */
592 foreach_ir_nodeset(ready_set, irn, iter) {
593 if (get_irn_delay(env, irn) == max_delay) {
594 ir_nodeset_insert(&mcands, irn);
595 if (get_irn_etime(env, irn) <= env->curr_time)
596 ir_nodeset_insert(&ecands, irn);
601 if (ir_nodeset_size(&mcands) == 1) {
602 irn = get_nodeset_node(&mcands);
603 DB((env->dbg, LEVEL_3, "\tirn = %+F, mcand = 1, max_delay = %u\n", irn, max_delay));
606 size_t cnt = ir_nodeset_size(&ecands);
608 irn = get_nodeset_node(&ecands);
611 /* BEWARE: don't select a JUMP if others are still possible */
614 DB((env->dbg, LEVEL_3, "\tirn = %+F, ecand = 1, max_delay = %u\n", irn, max_delay));
617 DB((env->dbg, LEVEL_3, "\tecand = %zu, max_delay = %u\n", cnt, max_delay));
618 irn = basic_selection(&ecands);
622 DB((env->dbg, LEVEL_3, "\tmcand = %zu\n", ir_nodeset_size(&mcands)));
623 irn = basic_selection(&mcands);
630 static void *muchnik_init_graph(ir_graph *irg)
632 trace_env_t *env = trace_init(irg);
636 static void *muchnik_init_block(void *graph_env, ir_node *bl)
638 trace_env_t *env = (trace_env_t*) graph_env;
639 trace_preprocess_block(env, bl);
643 static void sched_muchnik(ir_graph *irg)
645 static const list_sched_selector_t muchnik_selector = {
649 trace_node_ready, /* node_ready */
650 trace_update_time, /* node_selected */
651 NULL, /* finish_block */
652 trace_free /* finish_graph */
654 be_list_sched_graph(irg, &muchnik_selector);
658 * Execute the heuristic function.
660 static ir_node *heuristic_select(void *block_env, ir_nodeset_t *ns)
662 trace_env_t *trace_env = (trace_env_t*)block_env;
663 ir_node *irn, *cand = NULL;
664 int max_prio = INT_MIN;
665 int cur_prio = INT_MIN;
667 ir_nodeset_iterator_t iter;
668 /* Note: register pressure calculation needs an overhaul, you need correct
669 * tracking for each register class indidually and weight by each class
670 int cur_pressure = ir_nodeset_size(lv); */
671 int cur_pressure = 1;
673 /* prefer instructions which can be scheduled early */
675 /* prefer instructions with lots of successors */
676 #define PRIO_NUMSUCCS 8
677 /* prefer instructions with long critical path */
678 #define PRIO_LEVEL 12
679 /* prefer instructions coming early in preorder */
680 #define PRIO_PREORD 8
681 /* weight of current register pressure */
682 #define PRIO_CUR_PRESS 20
683 /* weight of register pressure difference */
684 #define PRIO_CHG_PRESS 8
686 /* priority based selection, heuristic inspired by mueller diss */
687 foreach_ir_nodeset(ns, irn, iter) {
688 /* make sure that branches are scheduled last */
690 int rdiff = get_irn_reg_diff(trace_env, irn);
691 int sign = rdiff < 0;
692 int chg = (rdiff < 0 ? -rdiff : rdiff) << PRIO_CHG_PRESS;
694 reg_fact = chg * cur_pressure;
696 reg_fact = INT_MAX - 2;
697 reg_fact = sign ? -reg_fact : reg_fact;
699 cur_prio = (get_irn_critical_path_len(trace_env, irn) << PRIO_LEVEL)
700 //- (get_irn_delay(trace_env, irn) << PRIO_LEVEL)
701 + (get_irn_num_user(trace_env, irn) << PRIO_NUMSUCCS)
702 - (get_irn_etime(trace_env, irn) << PRIO_TIME)
703 //- ((get_irn_reg_diff(trace_env, irn) >> PRIO_CHG_PRESS) << ((cur_pressure >> PRIO_CUR_PRESS) - 3))
705 + (get_irn_preorder(trace_env, irn) << PRIO_PREORD); /* high preorder means early schedule */
706 if (cur_prio > max_prio) {
711 DBG((trace_env->dbg, LEVEL_4, "checked NODE %+F\n", irn));
712 DBG((trace_env->dbg, LEVEL_4, "\tpriority: %d\n", cur_prio));
713 DBG((trace_env->dbg, LEVEL_4, "\tpath len: %d (%d)\n", get_irn_critical_path_len(trace_env, irn), get_irn_critical_path_len(trace_env, irn) << PRIO_LEVEL));
714 DBG((trace_env->dbg, LEVEL_4, "\tdelay: %d (%d)\n", get_irn_delay(trace_env, irn), get_irn_delay(trace_env, irn) << PRIO_LEVEL));
715 DBG((trace_env->dbg, LEVEL_4, "\t#user: %d (%d)\n", get_irn_num_user(trace_env, irn), get_irn_num_user(trace_env, irn) << PRIO_NUMSUCCS));
716 DBG((trace_env->dbg, LEVEL_4, "\tetime: %d (%d)\n", get_irn_etime(trace_env, irn), 0 - (get_irn_etime(trace_env, irn) << PRIO_TIME)));
717 DBG((trace_env->dbg, LEVEL_4, "\tpreorder: %d (%d)\n", get_irn_preorder(trace_env, irn), get_irn_preorder(trace_env, irn) << PRIO_PREORD));
718 DBG((trace_env->dbg, LEVEL_4, "\treg diff: %d (%d)\n", get_irn_reg_diff(trace_env, irn), 0 - reg_fact));
719 DBG((trace_env->dbg, LEVEL_4, "\tpressure: %d\n", cur_pressure));
724 DBG((trace_env->dbg, LEVEL_4, "heuristic selected %+F:\n", cand));
727 cand = basic_selection(ns);
733 static void sched_heuristic(ir_graph *irg)
735 static const list_sched_selector_t heuristic_selector = {
739 trace_node_ready, /* node_ready */
740 trace_update_time, /* node_selected */
741 NULL, /* finish_block */
742 trace_free /* finish_graph */
744 be_list_sched_graph(irg, &heuristic_selector);
747 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched_trace)
748 void be_init_sched_trace(void)
750 be_register_scheduler("heur", sched_heuristic);
751 be_register_scheduler("muchnik", sched_muchnik);