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
30 #include "iredges_t.h"
33 #include "belistsched.h"
38 /* we need a special mark */
42 typedef struct trace_irn {
43 sched_timestep_t delay; /**< The delay for this node if already calculated, else 0. */
44 sched_timestep_t etime; /**< The earliest time of this node. */
45 unsigned num_user; /**< The number real users (mode datab) of this node */
46 int reg_diff; /**< The difference of num(out registers) - num(in registers) */
47 int preorder; /**< The pre-order position */
48 unsigned critical_path_len; /**< The weighted length of the longest critical path */
49 unsigned is_root : 1; /**< is a root node of a block */
52 typedef struct trace_env {
53 trace_irn_t *sched_info; /**< trace scheduling information about the nodes */
54 sched_timestep_t curr_time; /**< current time of the scheduler */
55 be_lv_t *liveness; /**< The liveness for the irg */
56 DEBUG_ONLY(firm_dbg_module_t *dbg;)
60 * Returns a random node from a nodeset
62 static ir_node *get_nodeset_node(const ir_nodeset_t *nodeset)
64 ir_nodeset_iterator_t iter;
66 ir_nodeset_iterator_init(&iter, nodeset);
67 return ir_nodeset_iterator_next(&iter);
71 * Returns non-zero if the node is a root node
73 static inline unsigned is_root_node(trace_env_t *env, ir_node *n)
75 unsigned const idx = get_irn_idx(n);
77 assert(idx < ARR_LEN(env->sched_info));
78 return env->sched_info[idx].is_root;
82 * Mark a node as root node
84 static inline void mark_root_node(trace_env_t *env, ir_node *n)
86 unsigned const idx = get_irn_idx(n);
88 assert(idx < ARR_LEN(env->sched_info));
89 env->sched_info[idx].is_root = 1;
93 * Get the current delay.
95 static inline sched_timestep_t get_irn_delay(trace_env_t *env, ir_node *n)
97 unsigned const idx = get_irn_idx(n);
99 assert(idx < ARR_LEN(env->sched_info));
100 return env->sched_info[idx].delay;
104 * Set the current delay.
106 static inline void set_irn_delay(trace_env_t *env, ir_node *n, sched_timestep_t delay)
108 unsigned const idx = get_irn_idx(n);
110 assert(idx < ARR_LEN(env->sched_info));
111 env->sched_info[idx].delay = delay;
115 * Get the current etime.
117 static inline sched_timestep_t get_irn_etime(trace_env_t *env, ir_node *n)
119 unsigned const idx = get_irn_idx(n);
121 assert(idx < ARR_LEN(env->sched_info));
122 return env->sched_info[idx].etime;
126 * Set the current etime.
128 static inline void set_irn_etime(trace_env_t *env, ir_node *n, sched_timestep_t etime)
130 unsigned const 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)
141 unsigned const idx = get_irn_idx(n);
143 assert(idx < ARR_LEN(env->sched_info));
144 return env->sched_info[idx].num_user;
148 * Set the number of users.
150 static inline void set_irn_num_user(trace_env_t *env, ir_node *n, unsigned num_user)
152 unsigned const idx = get_irn_idx(n);
154 assert(idx < ARR_LEN(env->sched_info));
155 env->sched_info[idx].num_user = num_user;
159 * Get the register difference.
161 static inline int get_irn_reg_diff(trace_env_t *env, ir_node *n)
163 unsigned const idx = get_irn_idx(n);
165 assert(idx < ARR_LEN(env->sched_info));
166 return env->sched_info[idx].reg_diff;
170 * Set the register difference.
172 static inline void set_irn_reg_diff(trace_env_t *env, ir_node *n, int reg_diff)
174 unsigned const idx = get_irn_idx(n);
176 assert(idx < ARR_LEN(env->sched_info));
177 env->sched_info[idx].reg_diff = reg_diff;
181 * Get the pre-order position.
183 static inline int get_irn_preorder(trace_env_t *env, ir_node *n)
185 unsigned const idx = get_irn_idx(n);
187 assert(idx < ARR_LEN(env->sched_info));
188 return env->sched_info[idx].preorder;
192 * Set the pre-order position.
194 static inline void set_irn_preorder(trace_env_t *env, ir_node *n, int pos)
196 unsigned const idx = get_irn_idx(n);
198 assert(idx < ARR_LEN(env->sched_info));
199 env->sched_info[idx].preorder = pos;
203 * Get the pre-order position.
205 static inline unsigned get_irn_critical_path_len(trace_env_t *env, ir_node *n)
207 unsigned const idx = get_irn_idx(n);
209 assert(idx < ARR_LEN(env->sched_info));
210 return env->sched_info[idx].critical_path_len;
214 * Set the pre-order position.
216 static inline void set_irn_critical_path_len(trace_env_t *env, ir_node *n, unsigned len)
218 unsigned const idx = get_irn_idx(n);
220 assert(idx < ARR_LEN(env->sched_info));
221 env->sched_info[idx].critical_path_len = len;
225 * returns the exec-time for node n.
227 static sched_timestep_t exectime(trace_env_t *env, ir_node *n)
230 if (be_is_Keep(n) || is_Proj(n))
233 if (env->selector->exectime)
234 return env->selector->exectime(env->selector_env, n);
240 * Calculates the latency for between two ops
242 static sched_timestep_t latency(trace_env_t *env, ir_node *pred, int pred_cycle, ir_node *curr, int curr_cycle)
246 /* a Keep hides a root */
247 if (be_is_Keep(curr))
248 return exectime(env, pred);
250 /* Proj's are executed immediately */
254 /* predecessors Proj's must be skipped */
256 pred = get_Proj_pred(pred);
259 if (env->selector->latency)
260 return env->selector->latency(env->selector_env, pred, pred_cycle, curr, curr_cycle);
267 * Returns the number of users of a node having mode datab.
269 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 * Returns the difference of regs_output - regs_input;
300 static int get_reg_difference(trace_env_t *env, ir_node *irn)
305 ir_node *block = get_nodes_block(irn);
307 if (be_is_Call(irn)) {
308 /* we want calls preferred */
312 if (get_irn_mode(irn) == mode_T) {
313 /* mode_T nodes: num out regs == num Projs with mode datab */
314 const ir_edge_t *edge;
315 foreach_out_edge(irn, edge) {
316 ir_node *proj = get_edge_src_irn(edge);
317 if (mode_is_datab(get_irn_mode(proj)))
324 /* num in regs: number of ins with mode datab and not ignore */
325 for (i = get_irn_arity(irn) - 1; i >= 0; i--) {
326 ir_node *in = get_irn_n(irn, i);
328 if (!mode_is_datab(get_irn_mode(in)))
331 if (arch_irn_is_ignore(in))
334 if (be_is_live_end(env->liveness, block, in))
340 return num_out - num_in;
344 * descent into a dag and create a pre-order list.
346 static void descent(ir_node *root, ir_node *block, ir_node **list, trace_env_t *env, unsigned path_len)
350 if (! is_Phi(root)) {
351 path_len += exectime(env, root);
352 if (get_irn_critical_path_len(env, root) < path_len) {
353 set_irn_critical_path_len(env, root, path_len);
355 /* calculate number of users (needed for heuristic) */
356 set_irn_num_user(env, root, get_num_successors(root));
358 /* calculate register difference (needed for heuristic) */
359 set_irn_reg_diff(env, root, get_reg_difference(env, root));
361 /* Phi nodes always leave the block */
362 for (i = get_irn_arity(root) - 1; i >= 0; --i) {
363 ir_node *pred = get_irn_n(root, i);
365 DBG((env->dbg, LEVEL_3, " node %+F\n", pred));
367 /* Blocks may happen as predecessors of End nodes */
371 /* already seen nodes are not marked */
372 if (get_irn_link(pred) != MARK)
375 /* don't leave our block */
376 if (get_nodes_block(pred) != block)
379 set_irn_link(pred, NULL);
381 descent(pred, block, list, env, path_len);
384 set_irn_link(root, *list);
389 * Returns non-zero if root is a root in the block block.
391 static int is_root(ir_node *root, ir_node *block)
393 const ir_edge_t *edge;
395 foreach_out_edge(root, edge) {
396 ir_node *succ = get_edge_src_irn(edge);
400 /* Phi nodes are always in "another block */
403 if (get_nodes_block(succ) == block)
410 * Performs initial block calculations for trace scheduling.
412 static void trace_preprocess_block(trace_env_t *env, ir_node *block)
414 ir_node *root = NULL, *preord = NULL;
417 const ir_edge_t *edge;
419 /* First step: Find the root set. */
420 foreach_out_edge(block, edge) {
421 ir_node *succ = get_edge_src_irn(edge);
423 if (is_Anchor(succ)) {
424 /* ignore a keep alive edge */
427 if (is_root(succ, block)) {
428 mark_root_node(env, succ);
429 set_irn_link(succ, root);
433 set_irn_link(succ, MARK);
436 /* Second step: calculate the pre-order list. */
438 for (curr = root; curr; curr = irn) {
439 irn = (ir_node*)get_irn_link(curr);
440 DBG((env->dbg, LEVEL_2, " DAG root %+F\n", curr));
441 descent(curr, block, &preord, env, 0);
445 /* Third step: calculate the Delay. Note that our
446 * list is now in pre-order, starting at root
448 for (cur_pos = 0, curr = root; curr; curr = (ir_node*)get_irn_link(curr), cur_pos++) {
452 /* assure, that branches can be executed last */
456 if (is_root_node(env, curr))
457 d = exectime(env, curr);
460 foreach_out_edge(curr, edge) {
461 ir_node *n = get_edge_src_irn(edge);
463 if (get_nodes_block(n) == block) {
466 ld = latency(env, curr, 1, n, 0) + get_irn_delay(env, n);
472 set_irn_delay(env, curr, d);
473 DB((env->dbg, LEVEL_2, "\t%+F delay %u\n", curr, d));
475 /* set the etime of all nodes to 0 */
476 set_irn_etime(env, curr, 0);
478 set_irn_preorder(env, curr, cur_pos);
483 * This functions gets called after a node finally has been made ready.
485 static void trace_node_ready(void *data, ir_node *irn, ir_node *pred)
487 trace_env_t *env = (trace_env_t*)data;
488 sched_timestep_t etime_p, etime;
490 etime = env->curr_time;
492 etime_p = get_irn_etime(env, pred);
493 etime += latency(env, pred, 1, irn, 0);
494 etime = etime_p > etime ? etime_p : etime;
497 set_irn_etime(env, irn, etime);
498 DB((env->dbg, LEVEL_2, "\tset etime of %+F to %u\n", irn, etime));
502 * Update the current time after irn has been selected.
504 static void trace_update_time(void *data, ir_node *irn)
506 trace_env_t *env = (trace_env_t*)data;
507 if (is_Phi(irn) || get_irn_opcode(irn) == beo_Start) {
508 env->curr_time += get_irn_etime(env, irn);
511 env->curr_time += exectime(env, irn);
516 * Allocates memory and initializes trace scheduling environment.
517 * @param irg The backend irg object
518 * @return The environment
520 static trace_env_t *trace_init(ir_graph *irg)
522 trace_env_t *env = XMALLOCZ(trace_env_t);
523 int nn = get_irg_last_idx(irg);
526 env->sched_info = NEW_ARR_F(trace_irn_t, nn);
527 env->liveness = be_liveness(irg);
528 FIRM_DBG_REGISTER(env->dbg, "firm.be.sched.trace");
530 be_liveness_assure_chk(env->liveness);
531 memset(env->sched_info, 0, nn * sizeof(*(env->sched_info)));
537 * Frees all memory allocated for trace scheduling environment.
538 * @param env The environment
540 static void trace_free(void *data)
542 trace_env_t *env = (trace_env_t*)data;
543 be_liveness_free(env->liveness);
544 DEL_ARR_F(env->sched_info);
549 * Simple selector. Just assure that jumps are scheduled last.
551 static ir_node *basic_selection(ir_nodeset_t *ready_set)
554 ir_nodeset_iterator_t iter;
556 /* assure that branches and constants are executed last */
557 foreach_ir_nodeset(ready_set, irn, iter) {
563 /* at last: schedule branches */
564 irn = get_nodeset_node(ready_set);
570 * The muchnik selector.
572 static ir_node *muchnik_select(void *block_env, ir_nodeset_t *ready_set)
574 trace_env_t *env = (trace_env_t*)block_env;
575 ir_nodeset_t mcands, ecands;
576 ir_nodeset_iterator_t iter;
577 sched_timestep_t max_delay = 0;
580 /* calculate the max delay of all candidates */
581 foreach_ir_nodeset(ready_set, irn, iter) {
582 sched_timestep_t d = get_irn_delay(env, irn);
584 max_delay = d > max_delay ? d : max_delay;
587 ir_nodeset_init_size(&mcands, 8);
588 ir_nodeset_init_size(&ecands, 8);
590 /* build mcands and ecands */
591 foreach_ir_nodeset(ready_set, irn, iter) {
592 if (get_irn_delay(env, irn) == max_delay) {
593 ir_nodeset_insert(&mcands, irn);
594 if (get_irn_etime(env, irn) <= env->curr_time)
595 ir_nodeset_insert(&ecands, irn);
600 if (ir_nodeset_size(&mcands) == 1) {
601 irn = get_nodeset_node(&mcands);
602 DB((env->dbg, LEVEL_3, "\tirn = %+F, mcand = 1, max_delay = %u\n", irn, max_delay));
605 size_t cnt = ir_nodeset_size(&ecands);
607 irn = get_nodeset_node(&ecands);
610 /* BEWARE: don't select a JUMP if others are still possible */
613 DB((env->dbg, LEVEL_3, "\tirn = %+F, ecand = 1, max_delay = %u\n", irn, max_delay));
616 DB((env->dbg, LEVEL_3, "\tecand = %zu, max_delay = %u\n", cnt, max_delay));
617 irn = basic_selection(&ecands);
621 DB((env->dbg, LEVEL_3, "\tmcand = %zu\n", ir_nodeset_size(&mcands)));
622 irn = basic_selection(&mcands);
629 static void *muchnik_init_graph(ir_graph *irg)
631 trace_env_t *env = trace_init(irg);
635 static void *muchnik_init_block(void *graph_env, ir_node *bl)
637 trace_env_t *env = (trace_env_t*) graph_env;
638 trace_preprocess_block(env, bl);
642 static void sched_muchnik(ir_graph *irg)
644 static const list_sched_selector_t muchnik_selector = {
648 trace_node_ready, /* node_ready */
649 trace_update_time, /* node_selected */
650 NULL, /* finish_block */
651 trace_free /* finish_graph */
653 be_list_sched_graph(irg, &muchnik_selector);
657 * Execute the heuristic function.
659 static ir_node *heuristic_select(void *block_env, ir_nodeset_t *ns)
661 trace_env_t *trace_env = (trace_env_t*)block_env;
662 ir_node *irn, *cand = NULL;
663 int max_prio = INT_MIN;
664 int cur_prio = INT_MIN;
666 ir_nodeset_iterator_t iter;
667 /* Note: register pressure calculation needs an overhaul, you need correct
668 * tracking for each register class indidually and weight by each class
669 int cur_pressure = ir_nodeset_size(lv); */
670 int cur_pressure = 1;
672 /* prefer instructions which can be scheduled early */
674 /* prefer instructions with lots of successors */
675 #define PRIO_NUMSUCCS 8
676 /* prefer instructions with long critical path */
677 #define PRIO_LEVEL 12
678 /* prefer instructions coming early in preorder */
679 #define PRIO_PREORD 8
680 /* weight of current register pressure */
681 #define PRIO_CUR_PRESS 20
682 /* weight of register pressure difference */
683 #define PRIO_CHG_PRESS 8
685 /* priority based selection, heuristic inspired by mueller diss */
686 foreach_ir_nodeset(ns, irn, iter) {
687 /* make sure that branches are scheduled last */
689 int rdiff = get_irn_reg_diff(trace_env, irn);
690 int sign = rdiff < 0;
691 int chg = (rdiff < 0 ? -rdiff : rdiff) << PRIO_CHG_PRESS;
693 reg_fact = chg * cur_pressure;
695 reg_fact = INT_MAX - 2;
696 reg_fact = sign ? -reg_fact : reg_fact;
698 cur_prio = (get_irn_critical_path_len(trace_env, irn) << PRIO_LEVEL)
699 //- (get_irn_delay(trace_env, irn) << PRIO_LEVEL)
700 + (get_irn_num_user(trace_env, irn) << PRIO_NUMSUCCS)
701 - (get_irn_etime(trace_env, irn) << PRIO_TIME)
702 //- ((get_irn_reg_diff(trace_env, irn) >> PRIO_CHG_PRESS) << ((cur_pressure >> PRIO_CUR_PRESS) - 3))
704 + (get_irn_preorder(trace_env, irn) << PRIO_PREORD); /* high preorder means early schedule */
705 if (cur_prio > max_prio) {
710 DBG((trace_env->dbg, LEVEL_4, "checked NODE %+F\n", irn));
711 DBG((trace_env->dbg, LEVEL_4, "\tpriority: %d\n", cur_prio));
712 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));
713 DBG((trace_env->dbg, LEVEL_4, "\tdelay: %d (%d)\n", get_irn_delay(trace_env, irn), get_irn_delay(trace_env, irn) << PRIO_LEVEL));
714 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));
715 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)));
716 DBG((trace_env->dbg, LEVEL_4, "\tpreorder: %d (%d)\n", get_irn_preorder(trace_env, irn), get_irn_preorder(trace_env, irn) << PRIO_PREORD));
717 DBG((trace_env->dbg, LEVEL_4, "\treg diff: %d (%d)\n", get_irn_reg_diff(trace_env, irn), 0 - reg_fact));
718 DBG((trace_env->dbg, LEVEL_4, "\tpressure: %d\n", cur_pressure));
723 DBG((trace_env->dbg, LEVEL_4, "heuristic selected %+F:\n", cand));
726 cand = basic_selection(ns);
732 static void sched_heuristic(ir_graph *irg)
734 static const list_sched_selector_t heuristic_selector = {
738 trace_node_ready, /* node_ready */
739 trace_update_time, /* node_selected */
740 NULL, /* finish_block */
741 trace_free /* finish_graph */
743 be_list_sched_graph(irg, &heuristic_selector);
746 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_sched_trace)
747 void be_init_sched_trace(void)
749 be_register_scheduler("heur", sched_heuristic);
750 be_register_scheduler("muchnik", sched_muchnik);