2 * Copyright (C) 1995-2007 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
33 #include "iredges_t.h"
35 #include "besched_t.h"
36 #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 const arch_env_t *arch_env; /**< the arch environment */
56 sched_timestep_t curr_time; /**< current time of the scheduler */
57 void *selector_env; /**< the backend selector environment */
58 const list_sched_selector_t *selector; /**< the actual backend selector */
59 be_lv_t *liveness; /**< The liveness for the irg */
60 DEBUG_ONLY(firm_dbg_module_t *dbg;)
64 * Returns a random node from a nodeset
66 static ir_node *get_nodeset_node(const ir_nodeset_t *nodeset)
68 ir_nodeset_iterator_t iter;
70 ir_nodeset_iterator_init(&iter, nodeset);
71 return ir_nodeset_iterator_next(&iter);
75 * Returns non-zero if the node is a root node
77 static INLINE unsigned is_root_node(trace_env_t *env, ir_node *n)
79 int idx = get_irn_idx(n);
81 assert(idx < ARR_LEN(env->sched_info));
82 return env->sched_info[idx].is_root;
86 * Mark a node as root node
88 static INLINE void mark_root_node(trace_env_t *env, ir_node *n)
90 int idx = get_irn_idx(n);
92 assert(idx < ARR_LEN(env->sched_info));
93 env->sched_info[idx].is_root = 1;
97 * Get the current delay.
99 static INLINE sched_timestep_t get_irn_delay(trace_env_t *env, ir_node *n) {
100 int idx = get_irn_idx(n);
102 assert(idx < ARR_LEN(env->sched_info));
103 return env->sched_info[idx].delay;
107 * Set the current delay.
109 static INLINE void set_irn_delay(trace_env_t *env, ir_node *n, sched_timestep_t delay) {
110 int idx = get_irn_idx(n);
112 assert(idx < ARR_LEN(env->sched_info));
113 env->sched_info[idx].delay = delay;
117 * Get the current etime.
119 static INLINE sched_timestep_t get_irn_etime(trace_env_t *env, ir_node *n) {
120 int 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) {
130 int idx = get_irn_idx(n);
132 assert(idx < ARR_LEN(env->sched_info));
133 env->sched_info[idx].etime = etime;
137 * Get the number of users.
139 static INLINE unsigned get_irn_num_user(trace_env_t *env, ir_node *n) {
140 int idx = get_irn_idx(n);
142 assert(idx < ARR_LEN(env->sched_info));
143 return env->sched_info[idx].num_user;
147 * Set the number of users.
149 static INLINE void set_irn_num_user(trace_env_t *env, ir_node *n, unsigned num_user) {
150 int idx = get_irn_idx(n);
152 assert(idx < ARR_LEN(env->sched_info));
153 env->sched_info[idx].num_user = num_user;
157 * Get the register difference.
159 static INLINE int get_irn_reg_diff(trace_env_t *env, ir_node *n) {
160 int idx = get_irn_idx(n);
162 assert(idx < ARR_LEN(env->sched_info));
163 return env->sched_info[idx].reg_diff;
167 * Set the register difference.
169 static INLINE void set_irn_reg_diff(trace_env_t *env, ir_node *n, int reg_diff) {
170 int idx = get_irn_idx(n);
172 assert(idx < ARR_LEN(env->sched_info));
173 env->sched_info[idx].reg_diff = reg_diff;
177 * Get the pre-order position.
179 static INLINE int get_irn_preorder(trace_env_t *env, ir_node *n) {
180 int idx = get_irn_idx(n);
182 assert(idx < ARR_LEN(env->sched_info));
183 return env->sched_info[idx].preorder;
187 * Set the pre-order position.
189 static INLINE void set_irn_preorder(trace_env_t *env, ir_node *n, int pos) {
190 int idx = get_irn_idx(n);
192 assert(idx < ARR_LEN(env->sched_info));
193 env->sched_info[idx].preorder = pos;
197 * Get the pre-order position.
199 static INLINE unsigned get_irn_critical_path_len(trace_env_t *env, ir_node *n) {
200 int idx = get_irn_idx(n);
202 assert(idx < ARR_LEN(env->sched_info));
203 return env->sched_info[idx].critical_path_len;
207 * Set the pre-order position.
209 static INLINE void set_irn_critical_path_len(trace_env_t *env, ir_node *n, unsigned len) {
210 int idx = get_irn_idx(n);
212 assert(idx < ARR_LEN(env->sched_info));
213 env->sched_info[idx].critical_path_len = len;
217 * returns the exec-time for node n.
219 static sched_timestep_t exectime(trace_env_t *env, ir_node *n) {
220 if (be_is_Keep(n) || is_Proj(n))
222 if (env->selector->exectime)
223 return env->selector->exectime(env->selector_env, n);
228 * Calculates the latency for between two ops
230 static sched_timestep_t latency(trace_env_t *env, ir_node *pred, int pred_cycle, ir_node *curr, int curr_cycle) {
231 /* a Keep hides a root */
232 if (be_is_Keep(curr))
233 return exectime(env, pred);
235 /* Proj's are executed immediately */
239 /* predecessors Proj's must be skipped */
241 pred = get_Proj_pred(pred);
243 if (env->selector->latency)
244 return env->selector->latency(env->selector_env, pred, pred_cycle, curr, curr_cycle);
249 * Returns the number of users of a node having mode datab.
251 static int get_num_successors(ir_node *irn) {
253 const ir_edge_t *edge;
255 if (get_irn_mode(irn) == mode_T) {
256 /* for mode_T nodes: count the users of all Projs */
257 foreach_out_edge(irn, edge) {
258 ir_node *proj = get_edge_src_irn(edge);
259 ir_mode *mode = get_irn_mode(proj);
262 sum += get_num_successors(proj);
263 else if (mode_is_datab(mode))
264 sum += get_irn_n_edges(proj);
268 /* do not count keep-alive edges */
269 foreach_out_edge(irn, edge) {
270 if (get_irn_opcode(get_edge_src_irn(edge)) != iro_End)
279 * Returns the difference of regs_output - regs_input;
281 static int get_reg_difference(trace_env_t *env, ir_node *irn) {
285 ir_node *block = get_nodes_block(irn);
287 if (be_is_Call(irn)) {
288 /* we want calls prefered */
292 if (get_irn_mode(irn) == mode_T) {
293 /* mode_T nodes: num out regs == num Projs with mode datab */
294 const ir_edge_t *edge;
295 foreach_out_edge(irn, edge) {
296 ir_node *proj = get_edge_src_irn(edge);
297 if (mode_is_datab(get_irn_mode(proj)))
304 /* num in regs: number of ins with mode datab and not ignore */
305 for (i = get_irn_arity(irn) - 1; i >= 0; i--) {
306 ir_node *in = get_irn_n(irn, i);
308 if (! be_is_live_end(env->liveness, block, in) && /* if the value lives outside of block: do not count */
309 mode_is_datab(get_irn_mode(in)) && /* must be data node */
310 ! arch_irn_is(env->arch_env, in, ignore)) /* ignore "ignore" nodes :) */
314 return num_out - num_in;
318 * descent into a dag and create a pre-order list.
320 static void descent(ir_node *root, ir_node *block, ir_node **list, trace_env_t *env, unsigned path_len) {
323 if (! is_Phi(root)) {
324 path_len += exectime(env, root);
325 if (get_irn_critical_path_len(env, root) < path_len) {
326 set_irn_critical_path_len(env, root, path_len);
328 /* calculate number of users (needed for heuristic) */
329 set_irn_num_user(env, root, get_num_successors(root));
331 /* calculate register difference (needed for heuristic) */
332 set_irn_reg_diff(env, root, get_reg_difference(env, root));
334 /* Phi nodes always leave the block */
335 for (i = get_irn_arity(root) - 1; i >= 0; --i) {
336 ir_node *pred = get_irn_n(root, i);
338 DBG((env->dbg, LEVEL_3, " node %+F\n", pred));
340 /* Blocks may happen as predecessors of End nodes */
344 /* already seen nodes are not marked */
345 if (get_irn_link(pred) != MARK)
348 /* don't leave our block */
349 if (get_nodes_block(pred) != block)
352 set_irn_link(pred, NULL);
354 descent(pred, block, list, env, path_len);
357 set_irn_link(root, *list);
362 * Returns non-zero if root is a root in the block block.
364 static int is_root(ir_node *root, ir_node *block) {
365 const ir_edge_t *edge;
367 foreach_out_edge(root, edge) {
368 ir_node *succ = get_edge_src_irn(edge);
372 /* Phi nodes are always in "another block */
375 if (get_nodes_block(succ) == block)
382 * Performs initial block calculations for trace scheduling.
384 static void trace_preprocess_block(trace_env_t *env, ir_node *block) {
385 ir_node *root = NULL, *preord = NULL;
388 const ir_edge_t *edge;
390 /* First step: Find the root set. */
391 foreach_out_edge(block, edge) {
392 ir_node *succ = get_edge_src_irn(edge);
394 if (is_root(succ, block)) {
395 mark_root_node(env, succ);
396 set_irn_link(succ, root);
400 set_irn_link(succ, MARK);
403 /* Second step: calculate the pre-order list. */
405 for (curr = root; curr; curr = irn) {
406 irn = get_irn_link(curr);
407 DBG((env->dbg, LEVEL_2, " DAG root %+F\n", curr));
408 descent(curr, block, &preord, env, 0);
412 /* Third step: calculate the Delay. Note that our
413 * list is now in pre-order, starting at root
415 for (cur_pos = 0, curr = root; curr; curr = get_irn_link(curr), cur_pos++) {
418 if (arch_irn_class_is(env->arch_env, curr, branch)) {
419 /* assure, that branches can be executed last */
423 if (is_root_node(env, curr))
424 d = exectime(env, curr);
427 foreach_out_edge(curr, edge) {
428 ir_node *n = get_edge_src_irn(edge);
430 if (get_nodes_block(n) == block) {
433 ld = latency(env, curr, 1, n, 0) + get_irn_delay(env, n);
439 set_irn_delay(env, curr, d);
440 DB((env->dbg, LEVEL_2, "\t%+F delay %u\n", curr, d));
442 /* set the etime of all nodes to 0 */
443 set_irn_etime(env, curr, 0);
445 set_irn_preorder(env, curr, cur_pos);
450 * This functions gets called after a node finally has been made ready.
452 static void trace_node_ready(void *data, ir_node *irn, ir_node *pred) {
453 trace_env_t *env = data;
454 sched_timestep_t etime_p, etime;
456 etime = env->curr_time;
458 etime_p = get_irn_etime(env, pred);
459 etime += latency(env, pred, 1, irn, 0);
460 etime = etime_p > etime ? etime_p : etime;
463 set_irn_etime(env, irn, etime);
464 DB((env->dbg, LEVEL_2, "\tset etime of %+F to %u\n", irn, etime));
468 * Update the current time after irn has been selected.
470 static void trace_update_time(void *data, ir_node *irn) {
471 trace_env_t *env = data;
472 if (is_Phi(irn) || get_irn_opcode(irn) == iro_Start) {
473 env->curr_time += get_irn_etime(env, irn);
476 env->curr_time += exectime(env, irn);
481 * Allocates memory and initializes trace scheduling environment.
482 * @param birg The backend irg object
483 * @return The environment
485 static trace_env_t *trace_init(const arch_env_t *arch_env, ir_graph *irg) {
486 trace_env_t *env = xcalloc(1, sizeof(*env));
487 int nn = get_irg_last_idx(irg);
489 env->arch_env = arch_env;
491 env->sched_info = NEW_ARR_F(trace_irn_t, nn);
492 env->liveness = be_liveness(irg);
493 FIRM_DBG_REGISTER(env->dbg, "firm.be.sched.trace");
495 memset(env->sched_info, 0, nn * sizeof(*(env->sched_info)));
501 * Frees all memory allocated for trace scheduling environment.
502 * @param env The environment
504 static void trace_free(void *data) {
505 trace_env_t *env = data;
506 be_liveness_free(env->liveness);
507 DEL_ARR_F(env->sched_info);
512 * Simple selector. Just assure that jumps are scheduled last.
514 static ir_node *basic_selection(const arch_env_t *arch_env, ir_nodeset_t *ready_set) {
516 ir_nodeset_iterator_t iter;
518 /* assure that branches and constants are executed last */
519 foreach_ir_nodeset(ready_set, irn, iter) {
520 if (! arch_irn_class_is(arch_env, irn, branch)) {
525 /* at last: schedule branches */
526 irn = get_nodeset_node(ready_set);
532 * The muchnik selector.
534 static ir_node *muchnik_select(void *block_env, ir_nodeset_t *ready_set, ir_nodeset_t *live_set)
536 trace_env_t *env = block_env;
537 ir_nodeset_t mcands, ecands;
538 ir_nodeset_iterator_t iter;
539 sched_timestep_t max_delay = 0;
542 /* calculate the max delay of all candidates */
543 foreach_ir_nodeset(ready_set, irn, iter) {
544 sched_timestep_t d = get_irn_delay(env, irn);
546 max_delay = d > max_delay ? d : max_delay;
549 ir_nodeset_init_size(&mcands, 8);
550 ir_nodeset_init_size(&ecands, 8);
552 /* build mcands and ecands */
553 foreach_ir_nodeset(ready_set, irn, iter) {
554 if (get_irn_delay(env, irn) == max_delay) {
555 ir_nodeset_insert(&mcands, irn);
556 if (get_irn_etime(env, irn) <= env->curr_time)
557 ir_nodeset_insert(&ecands, irn);
562 if (ir_nodeset_size(&mcands) == 1) {
563 irn = get_nodeset_node(&mcands);
564 DB((env->dbg, LEVEL_3, "\tirn = %+F, mcand = 1, max_delay = %u\n", irn, max_delay));
567 int cnt = ir_nodeset_size(&ecands);
569 irn = get_nodeset_node(&ecands);
571 if (arch_irn_class_is(env->arch_env, irn, branch)) {
572 /* BEWARE: don't select a JUMP if others are still possible */
575 DB((env->dbg, LEVEL_3, "\tirn = %+F, ecand = 1, max_delay = %u\n", irn, max_delay));
578 DB((env->dbg, LEVEL_3, "\tecand = %d, max_delay = %u\n", cnt, max_delay));
579 irn = basic_selection(env->arch_env, &ecands);
583 DB((env->dbg, LEVEL_3, "\tmcand = %d\n", ir_nodeset_size(&mcands)));
584 irn = basic_selection(env->arch_env, &mcands);
591 static void *muchnik_init_graph(const list_sched_selector_t *vtab, const arch_env_t *arch_env, ir_graph *irg)
593 trace_env_t *env = trace_init(arch_env, irg);
594 env->selector = vtab;
595 env->selector_env = (void*) arch_env;
599 static void *muchnik_init_block(void *graph_env, ir_node *bl)
601 trace_preprocess_block(graph_env, bl);
605 static const list_sched_selector_t muchnik_selector_struct = {
609 NULL, /* to_appear_in_schedule */
610 trace_node_ready, /* node_ready */
611 trace_update_time, /* node_selected */
614 NULL, /* finish_block */
615 trace_free /* finish_graph */
618 const list_sched_selector_t *muchnik_selector = &muchnik_selector_struct;
621 * Execute the heuristic function.
623 static ir_node *heuristic_select(void *block_env, ir_nodeset_t *ns, ir_nodeset_t *lv)
625 trace_env_t *trace_env = block_env;
626 ir_node *irn, *cand = NULL;
627 int max_prio = INT_MIN;
628 int cur_prio = INT_MIN;
629 int cur_pressure = ir_nodeset_size(lv);
630 int reg_fact, cand_reg_fact;
631 ir_nodeset_iterator_t iter;
633 /* prefer instructions which can be scheduled early */
635 /* prefer instructions with lots of successors */
636 #define PRIO_NUMSUCCS 8
637 /* prefer instructions with long critical path */
638 #define PRIO_LEVEL 12
639 /* prefer instructions coming early in preorder */
640 #define PRIO_PREORD 8
641 /* weight of current register pressure */
642 #define PRIO_CUR_PRESS 20
643 /* weight of register pressure difference */
644 #define PRIO_CHG_PRESS 8
646 /* priority based selection, heuristic inspired by mueller diss */
647 foreach_ir_nodeset(ns, irn, iter) {
648 /* make sure that branches are scheduled last */
649 if (! arch_irn_class_is(trace_env->arch_env, irn, branch)) {
650 int rdiff = get_irn_reg_diff(trace_env, irn);
651 int sign = rdiff < 0;
652 int chg = (rdiff < 0 ? -rdiff : rdiff) << PRIO_CHG_PRESS;
654 //reg_fact = chg << cur_pressure;
655 reg_fact = chg * cur_pressure;
657 reg_fact = INT_MAX - 2;
658 reg_fact = sign ? -reg_fact : reg_fact;
660 cur_prio = (get_irn_critical_path_len(trace_env, irn) << PRIO_LEVEL)
661 //- (get_irn_delay(trace_env, irn) << PRIO_LEVEL)
662 + (get_irn_num_user(trace_env, irn) << PRIO_NUMSUCCS)
663 - (get_irn_etime(trace_env, irn) << PRIO_TIME)
664 //- ((get_irn_reg_diff(trace_env, irn) >> PRIO_CHG_PRESS) << ((cur_pressure >> PRIO_CUR_PRESS) - 3))
666 + (get_irn_preorder(trace_env, irn) << PRIO_PREORD); /* high preorder means early schedule */
667 if (cur_prio > max_prio) {
670 cand_reg_fact = reg_fact;
673 DBG((trace_env->dbg, LEVEL_4, "checked NODE %+F\n", irn));
674 DBG((trace_env->dbg, LEVEL_4, "\tpriority: %d\n", cur_prio));
675 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));
676 DBG((trace_env->dbg, LEVEL_4, "\tdelay: %d (%d)\n", get_irn_delay(trace_env, irn), get_irn_delay(trace_env, irn) << PRIO_LEVEL));
677 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));
678 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)));
679 DBG((trace_env->dbg, LEVEL_4, "\tpreorder: %d (%d)\n", get_irn_preorder(trace_env, irn), get_irn_preorder(trace_env, irn) << PRIO_PREORD));
680 DBG((trace_env->dbg, LEVEL_4, "\treg diff: %d (%d)\n", get_irn_reg_diff(trace_env, irn), 0 - reg_fact));
681 DBG((trace_env->dbg, LEVEL_4, "\tpressure: %d\n", cur_pressure));
686 DBG((trace_env->dbg, LEVEL_4, "heuristic selected %+F:\n", cand));
689 cand = basic_selection(trace_env->arch_env, ns);
695 static const list_sched_selector_t heuristic_selector_struct = {
699 NULL, /* to_appear_in_schedule */
700 trace_node_ready, /* node_ready */
701 trace_update_time, /* node_selected */
704 NULL, /* finish_block */
705 trace_free /* finish_graph */
708 const list_sched_selector_t *heuristic_selector = &heuristic_selector_struct;