From 08bb312485d8a28679504d97402b70fde8073a66 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Christian=20W=C3=BCrdig?= Date: Tue, 29 Aug 2006 11:55:46 +0000 Subject: [PATCH] added different scheduling node selector modules --- ir/be/beschedregpress.c | 308 +++++++++++++++++++ ir/be/beschedtrace.c | 658 ++++++++++++++++++++++++++++++++++++++++ ir/be/beschedtrivial.c | 61 ++++ 3 files changed, 1027 insertions(+) create mode 100644 ir/be/beschedregpress.c create mode 100644 ir/be/beschedtrace.c create mode 100644 ir/be/beschedtrivial.c diff --git a/ir/be/beschedregpress.c b/ir/be/beschedregpress.c new file mode 100644 index 000000000..dc8c04c47 --- /dev/null +++ b/ir/be/beschedregpress.c @@ -0,0 +1,308 @@ +/** + * Regpressure node selector. + * Originally implemented by Sebastian Hack. + * @author Christian Wuerdig + * @date 29.08.2006 + * @cvs-id $Id$ + */ + +#include + +#include "iredges_t.h" +#include "irgwalk.h" + +#include "besched_t.h" +#include "belistsched.h" +#include "benode_t.h" + + +typedef struct _usage_stats_t { + ir_node *irn; + struct _usage_stats_t *next; + int max_hops; + int uses_in_block; /**< Number of uses inside the current block. */ + int already_consumed; /**< Number of insns using this value already + scheduled. */ +} usage_stats_t; + +typedef struct { + const list_sched_selector_t *vtab; + const arch_env_t *arch_env; +} reg_pressure_main_env_t; + +typedef struct { + struct obstack obst; + const reg_pressure_main_env_t *main_env; + usage_stats_t *root; + nodeset *already_scheduled; +} reg_pressure_selector_env_t; + + +#if 0 +/* +* Ugly global variable for the compare function +* since qsort(3) does not pass an extra pointer. +*/ +static ir_node *curr_bl = NULL; + +static int cmp_usage(const void *a, const void *b) +{ + struct trivial_sched_env *env; + const ir_node *p = a; + const ir_node *q = b; + int res = 0; + + res = is_live_end(env->curr_bl, a) - is_live_end(env->curr_bl, b); + + /* + * One of them is live at the end of the block. + * Then, that one shall be scheduled at after the other + */ + if(res != 0) + return res; + + + return res; +} +#endif + +static INLINE usage_stats_t *get_or_set_usage_stats(reg_pressure_selector_env_t *env, ir_node *irn) +{ + usage_stats_t *us = get_irn_link(irn); + + if(!us) { + us = obstack_alloc(&env->obst, sizeof(us[0])); + us->irn = irn; + us->already_consumed = 0; + us->max_hops = INT_MAX; + us->next = env->root; + env->root = us; + set_irn_link(irn, us); + } + + return us; +} + +static INLINE usage_stats_t *get_usage_stats(ir_node *irn) +{ + usage_stats_t *us = get_irn_link(irn); + assert(us && "This node must have usage stats"); + return us; +} + +static int max_hops_walker(reg_pressure_selector_env_t *env, ir_node *irn, ir_node *curr_bl, int depth, unsigned visited_nr) +{ + ir_node *bl = get_nodes_block(irn); + /* + * If the reached node is not in the block desired, + * return the value passed for this situation. + */ + if(get_nodes_block(irn) != bl) + return block_dominates(bl, curr_bl) ? 0 : INT_MAX; + + /* + * If the node is in the current block but not + * yet scheduled, we keep on searching from that node. + */ + if(!nodeset_find(env->already_scheduled, irn)) { + int i, n; + int res = 0; + for(i = 0, n = get_irn_ins_or_deps(irn); i < n; ++i) { + ir_node *operand = get_irn_in_or_dep(irn, i); + + if(get_irn_visited(operand) < visited_nr) { + int tmp; + + set_irn_visited(operand, visited_nr); + tmp = max_hops_walker(env, operand, bl, depth + 1, visited_nr); + res = MAX(tmp, res); + } + } + + return res; + } + + /* + * If the node is in the current block and scheduled, return + * the depth which indicates the number of steps to the + * region of scheduled nodes. + */ + return depth; +} + +static int compute_max_hops(reg_pressure_selector_env_t *env, ir_node *irn) +{ + ir_node *bl = get_nodes_block(irn); + ir_graph *irg = get_irn_irg(bl); + int res = 0; + + const ir_edge_t *edge; + + foreach_out_edge(irn, edge) { + ir_node *user = get_edge_src_irn(edge); + unsigned visited_nr = get_irg_visited(irg) + 1; + int max_hops; + + set_irg_visited(irg, visited_nr); + max_hops = max_hops_walker(env, user, irn, 0, visited_nr); + res = MAX(res, max_hops); + } + + return res; +} + +static void *reg_pressure_graph_init(const list_sched_selector_t *vtab, const arch_env_t *arch_env, ir_graph *irg) +{ + reg_pressure_main_env_t *main_env = xmalloc(sizeof(main_env[0])); + + main_env->arch_env = arch_env; + main_env->vtab = vtab; + irg_walk_graph(irg, firm_clear_link, NULL, NULL); + + return main_env; +} + +static INLINE int must_appear_in_schedule(const list_sched_selector_t *sel, void *block_env, const ir_node *irn) +{ + int res = -1; + + if(sel->to_appear_in_schedule) + res = sel->to_appear_in_schedule(block_env, irn); + + return res >= 0 ? res : (to_appear_in_schedule(irn) || be_is_Keep(irn) || be_is_CopyKeep(irn) || be_is_RegParams(irn)); +} + +static void *reg_pressure_block_init(void *graph_env, ir_node *bl) +{ + ir_node *irn; + reg_pressure_selector_env_t *env = xmalloc(sizeof(env[0])); + + obstack_init(&env->obst); + env->already_scheduled = new_nodeset(32); + env->root = NULL; + env->main_env = graph_env; + + /* + * Collect usage statistics. + */ + sched_foreach(bl, irn) { + if(must_appear_in_schedule(env->main_env->vtab, env, irn)) { + int i, n; + + for(i = 0, n = get_irn_arity(irn); i < n; ++i) { + //ir_node *op = get_irn_n(irn, i); + if(must_appear_in_schedule(env->main_env->vtab, env, irn)) { + usage_stats_t *us = get_or_set_usage_stats(env, irn); +#if 0 /* Liveness is not computed here! */ + if(is_live_end(bl, op)) + us->uses_in_block = 99999; + else +#endif + us->uses_in_block++; + } + } + } + } + + return env; +} + +static void reg_pressure_block_free(void *block_env) +{ + reg_pressure_selector_env_t *env = block_env; + usage_stats_t *us; + + for(us = env->root; us; us = us->next) + set_irn_link(us->irn, NULL); + + obstack_free(&env->obst, NULL); + del_nodeset(env->already_scheduled); + free(env); +} + +static int get_result_hops_sum(reg_pressure_selector_env_t *env, ir_node *irn) +{ + int res = 0; + if(get_irn_mode(irn) == mode_T) { + const ir_edge_t *edge; + + foreach_out_edge(irn, edge) + res += get_result_hops_sum(env, get_edge_src_irn(edge)); + } + + else if(mode_is_data(get_irn_mode(irn))) + res = compute_max_hops(env, irn); + + + return res; +} + +static INLINE int reg_pr_costs(reg_pressure_selector_env_t *env, ir_node *irn) +{ + int i, n; + int sum = 0; + + for(i = 0, n = get_irn_arity(irn); i < n; ++i) { + ir_node *op = get_irn_n(irn, i); + + if(must_appear_in_schedule(env->main_env->vtab, env, op)) + sum += compute_max_hops(env, op); + } + + sum += get_result_hops_sum(env, irn); + + return sum; +} + +static ir_node *reg_pressure_select(void *block_env, nodeset *ready_set, nodeset *live_set) +{ + reg_pressure_selector_env_t *env = block_env; + ir_node *irn, *res = NULL; + int curr_cost = INT_MAX; + + assert(nodeset_count(ready_set) > 0); + + for (irn = nodeset_first(ready_set); irn; irn = nodeset_next(ready_set)) { + /* + Ignore branch instructions for the time being. + They should only be scheduled if there is nothing else. + */ + if (! arch_irn_class_is(env->main_env->arch_env, irn, branch)) { + int costs = reg_pr_costs(env, irn); + if (costs <= curr_cost) { + res = irn; + curr_cost = costs; + } + } + } + + /* + There was no result so we only saw a branch. + Take it and finish. + */ + + if(!res) { + res = nodeset_first(ready_set); + nodeset_break(ready_set); + + assert(res && "There must be a node scheduled."); + } + + nodeset_insert(env->already_scheduled, res); + return res; +} + +static const list_sched_selector_t reg_pressure_selector_struct = { + reg_pressure_graph_init, + reg_pressure_block_init, + reg_pressure_select, + NULL, /* to_appear_in_schedule */ + NULL, /* node_ready */ + NULL, /* node_selected */ + NULL, /* exectime */ + NULL, /* latency */ + reg_pressure_block_free, + free +}; + +const list_sched_selector_t *reg_pressure_selector = ®_pressure_selector_struct; diff --git a/ir/be/beschedtrace.c b/ir/be/beschedtrace.c new file mode 100644 index 000000000..c95777e7f --- /dev/null +++ b/ir/be/beschedtrace.c @@ -0,0 +1,658 @@ +/** + * Implements a trace scheduler as presented in Muchnik[TM]. + * Originally implemented by Michael Beck. + * @author Christian Wuerdig + * @date 28.08.2006 + * @cvs-id $Id$ + */ + +#include + +#include "iredges_t.h" + +#include "besched_t.h" +#include "belistsched.h" +#include "benode_t.h" + +/* we need a special mark */ +static char _mark; +#define MARK &_mark + +typedef struct _trace_irn { + sched_timestep_t delay; /**< The delay for this node if already calculated, else 0. */ + sched_timestep_t etime; /**< The earliest time of this node. */ + unsigned num_user; /**< The number real users (mode datab) of this node */ + int reg_diff; /**< The difference of num(out registers) - num(in registers) */ + int preorder; /**< The pre-order position */ + unsigned critical_path_len; /**< The weighted length of the longest critical path */ + unsigned is_root : 1; /**< is a root node of a block */ +} trace_irn_t; + +typedef struct _trace_env { + trace_irn_t *sched_info; /**< trace scheduling information about the nodes */ + const arch_env_t *arch_env; /**< the arch environment */ + sched_timestep_t curr_time; /**< current time of the scheduler */ + void *selector_env; /**< the backend selector environment */ + const list_sched_selector_t *selector; /**< the actual backend selector */ + DEBUG_ONLY(firm_dbg_module_t *dbg;) +} trace_env_t; + +/** + * Returns non-zero if the node is a root node + */ +static INLINE unsigned is_root_node(trace_env_t *env, ir_node *n) +{ + int idx = get_irn_idx(n); + + assert(idx < ARR_LEN(env->sched_info)); + return env->sched_info[idx].is_root; +} + +/** + * Mark a node as root node + */ +static INLINE void mark_root_node(trace_env_t *env, ir_node *n) +{ + int idx = get_irn_idx(n); + + assert(idx < ARR_LEN(env->sched_info)); + env->sched_info[idx].is_root = 1; +} + +/** + * Get the current delay. + */ +static INLINE sched_timestep_t get_irn_delay(trace_env_t *env, ir_node *n) { + int idx = get_irn_idx(n); + + assert(idx < ARR_LEN(env->sched_info)); + return env->sched_info[idx].delay; +} + +/** + * Set the current delay. + */ +static INLINE void set_irn_delay(trace_env_t *env, ir_node *n, sched_timestep_t delay) { + int idx = get_irn_idx(n); + + assert(idx < ARR_LEN(env->sched_info)); + env->sched_info[idx].delay = delay; +} + +/** + * Get the current etime. + */ +static INLINE sched_timestep_t get_irn_etime(trace_env_t *env, ir_node *n) { + int idx = get_irn_idx(n); + + assert(idx < ARR_LEN(env->sched_info)); + return env->sched_info[idx].etime; +} + +/** + * Set the current etime. + */ +static INLINE void set_irn_etime(trace_env_t *env, ir_node *n, sched_timestep_t etime) { + int idx = get_irn_idx(n); + + assert(idx < ARR_LEN(env->sched_info)); + env->sched_info[idx].etime = etime; +} + +/** + * Get the number of users. + */ +static INLINE unsigned get_irn_num_user(trace_env_t *env, ir_node *n) { + int idx = get_irn_idx(n); + + assert(idx < ARR_LEN(env->sched_info)); + return env->sched_info[idx].num_user; +} + +/** + * Set the number of users. + */ +static INLINE void set_irn_num_user(trace_env_t *env, ir_node *n, unsigned num_user) { + int idx = get_irn_idx(n); + + assert(idx < ARR_LEN(env->sched_info)); + env->sched_info[idx].num_user = num_user; +} + +/** + * Get the register difference. + */ +static INLINE int get_irn_reg_diff(trace_env_t *env, ir_node *n) { + int idx = get_irn_idx(n); + + assert(idx < ARR_LEN(env->sched_info)); + return env->sched_info[idx].reg_diff; +} + +/** + * Set the register difference. + */ +static INLINE void set_irn_reg_diff(trace_env_t *env, ir_node *n, int reg_diff) { + int idx = get_irn_idx(n); + + assert(idx < ARR_LEN(env->sched_info)); + env->sched_info[idx].reg_diff = reg_diff; +} + +/** + * Get the pre-order position. + */ +static INLINE int get_irn_preorder(trace_env_t *env, ir_node *n) { + int idx = get_irn_idx(n); + + assert(idx < ARR_LEN(env->sched_info)); + return env->sched_info[idx].preorder; +} + +/** + * Set the pre-order position. + */ +static INLINE void set_irn_preorder(trace_env_t *env, ir_node *n, int pos) { + int idx = get_irn_idx(n); + + assert(idx < ARR_LEN(env->sched_info)); + env->sched_info[idx].preorder = pos; +} + +/** + * Get the pre-order position. + */ +static INLINE unsigned get_irn_critical_path_len(trace_env_t *env, ir_node *n) { + int idx = get_irn_idx(n); + + assert(idx < ARR_LEN(env->sched_info)); + return env->sched_info[idx].critical_path_len; +} + +/** + * Set the pre-order position. + */ +static INLINE void set_irn_critical_path_len(trace_env_t *env, ir_node *n, unsigned len) { + int idx = get_irn_idx(n); + + assert(idx < ARR_LEN(env->sched_info)); + env->sched_info[idx].critical_path_len = len; +} + +/** + * returns the exec-time for node n. + */ +static sched_timestep_t exectime(trace_env_t *env, ir_node *n) { + if (be_is_Keep(n) || is_Proj(n)) + return 0; + if (env->selector->exectime) + return env->selector->exectime(env->selector_env, n); + return 1; +} + +/** + * Calculates the latency for between two ops + */ +static sched_timestep_t latency(trace_env_t *env, ir_node *pred, int pred_cycle, ir_node *curr, int curr_cycle) { + /* a Keep hides a root */ + if (be_is_Keep(curr)) + return exectime(env, pred); + + /* Proj's are executed immediately */ + if (is_Proj(curr)) + return 0; + + /* predecessors Proj's must be skipped */ + if (is_Proj(pred)) + pred = get_Proj_pred(pred); + + if (env->selector->latency) + return env->selector->latency(env->selector_env, pred, pred_cycle, curr, curr_cycle); + return 1; +} + +/** + * Returns the number of users of a node having mode datab. + */ +static int get_num_successors(ir_node *irn) { + int sum = 0; + const ir_edge_t *edge; + + if (get_irn_mode(irn) == mode_T) { + /* for mode_T nodes: count the users of all Projs */ + foreach_out_edge(irn, edge) { + ir_node *proj = get_edge_src_irn(edge); + ir_mode *mode = get_irn_mode(proj); + + if (mode == mode_T) + sum += get_num_successors(proj); + else if (mode_is_datab(mode)) + sum += get_irn_n_edges(proj); + } + } + else { + /* do not count keep-alive edges */ + foreach_out_edge(irn, edge) { + if (get_irn_opcode(get_edge_src_irn(edge)) != iro_End) + sum++; + } + } + + return sum; +} + +/** + * Returns the difference of regs_output - regs_input; + */ +static int get_reg_difference(trace_env_t *env, ir_node *irn) { + int num_out = 0; + int num_in = 0; + int i; + + if (get_irn_mode(irn) == mode_T) { + /* mode_T nodes: num out regs == num Projs with mode datab */ + const ir_edge_t *edge; + foreach_out_edge(irn, edge) { + ir_node *proj = get_edge_src_irn(edge); + if (mode_is_datab(get_irn_mode(proj))) + num_out++; + } + } + else + num_out = 1; + + /* num in regs: number of ins with mode datab and not ignore */ + for (i = get_irn_arity(irn) - 1; i >= 0; i--) { + ir_node *in = get_irn_n(irn, i); + if (mode_is_datab(get_irn_mode(in)) && ! arch_irn_is(env->arch_env, in, ignore)) + num_in++; + } + + return num_out - num_in; +} + +/** + * descent into a dag and create a pre-order list. + */ +static void descent(ir_node *root, ir_node *block, ir_node **list, trace_env_t *env, unsigned path_len) { + int i; + + if (! is_Phi(root)) { + path_len += exectime(env, root); + if (get_irn_critical_path_len(env, root) < path_len) { + set_irn_critical_path_len(env, root, path_len); + } + + /* Phi nodes always leave the block */ + for (i = get_irn_arity(root) - 1; i >= 0; --i) { + ir_node *pred = get_irn_n(root, i); + + DBG((env->dbg, LEVEL_3, " node %+F\n", pred)); + /* Blocks may happen as predecessors of End nodes */ + if (is_Block(pred)) + continue; + + /* already seen nodes are not marked */ + if (get_irn_link(pred) != MARK) + continue; + + /* don't leave our block */ + if (get_nodes_block(pred) != block) + continue; + + /* calculate number of users (needed for heuristic) */ + set_irn_num_user(env, root, get_num_successors(root)); + + /* calculate register difference (needed for heuristic) */ + set_irn_reg_diff(env, root, get_reg_difference(env, root)); + + set_irn_link(pred, NULL); + + descent(pred, block, list, env, path_len); + } + } + set_irn_link(root, *list); + *list = root; +} + +/** + * Returns non-zero if root is a root in the block block. + */ +static int is_root(ir_node *root, ir_node *block) { + const ir_edge_t *edge; + + foreach_out_edge(root, edge) { + ir_node *succ = get_edge_src_irn(edge); + + if (is_Block(succ)) + continue; + /* Phi nodes are always in "another block */ + if (is_Phi(succ)) + continue; + if (get_nodes_block(succ) == block) + return 0; + } + return 1; +} + +/** + * Performs initial block calculations for trace scheduling. + */ +static void trace_preprocess_block(trace_env_t *env, ir_node *block) { + ir_node *root = NULL, *preord = NULL; + ir_node *curr, *irn; + int cur_pos; + const ir_edge_t *edge; + + /* First step: Find the root set. */ + foreach_out_edge(block, edge) { + ir_node *succ = get_edge_src_irn(edge); + + if (is_root(succ, block)) { + mark_root_node(env, succ); + set_irn_link(succ, root); + root = succ; + } + else + set_irn_link(succ, MARK); + } + + /* Second step: calculate the pre-order list. */ + preord = NULL; + for (curr = root; curr; curr = irn) { + irn = get_irn_link(curr); + DBG((env->dbg, LEVEL_2, " DAG root %+F\n", curr)); + descent(curr, block, &preord, env, 0); + } + root = preord; + + /* Third step: calculate the Delay. Note that our + * list is now in pre-order, starting at root + */ + for (cur_pos = 0, curr = root; curr; curr = get_irn_link(curr), cur_pos++) { + sched_timestep_t d; + + if (arch_irn_class_is(env->arch_env, curr, branch)) { + /* assure, that branches can be executed last */ + d = 0; + } + else { + if (is_root_node(env, curr)) + d = exectime(env, curr); + else { + d = 0; + foreach_out_edge(curr, edge) { + ir_node *n = get_edge_src_irn(edge); + + if (get_nodes_block(n) == block) { + sched_timestep_t ld; + + ld = latency(env, curr, 1, n, 0) + get_irn_delay(env, n); + d = ld > d ? ld : d; + } + } + } + } + set_irn_delay(env, curr, d); + DB((env->dbg, LEVEL_2, "\t%+F delay %u\n", curr, d)); + + /* set the etime of all nodes to 0 */ + set_irn_etime(env, curr, 0); + + set_irn_preorder(env, curr, cur_pos); + } +} + +/** + * This functions gets called after a node finally has been made ready. + */ +static void trace_node_ready(trace_env_t *env, ir_node *irn, ir_node *pred) { + sched_timestep_t etime_p, etime; + + etime = env->curr_time; + if (pred) { + etime_p = get_irn_etime(env, pred); + etime += latency(env, pred, 1, irn, 0); + etime = etime_p > etime ? etime_p : etime; + } + + set_irn_etime(env, irn, etime); + DB((env->dbg, LEVEL_2, "\tset etime of %+F to %u\n", irn, etime)); +} + +/** + * Update the current time after irn has been selected. + */ +static void trace_update_time(trace_env_t *env, ir_node *irn) { + if (is_Phi(irn) || get_irn_opcode(irn) == iro_Start) { + env->curr_time += get_irn_etime(env, irn); + } + else { + env->curr_time += exectime(env, irn); + } +} + +/** + * Allocates memory and initializes trace scheduling environment. + * @param birg The backend irg object + * @return The environment + */ +static trace_env_t *trace_init(const arch_env_t *arch_env, ir_graph *irg) { + trace_env_t *env = xcalloc(1, sizeof(*env)); + int nn = get_irg_last_idx(irg); + + env->arch_env = arch_env; + env->curr_time = 0; + env->sched_info = NEW_ARR_F(trace_irn_t, nn); + FIRM_DBG_REGISTER(env->dbg, "firm.be.sched.trace"); + + memset(env->sched_info, 0, nn * sizeof(*(env->sched_info))); + + return env; +} + +/** + * Frees all memory allocated for trace scheduling environment. + * @param env The environment + */ +static void trace_free(trace_env_t *env) { + DEL_ARR_F(env->sched_info); + free(env); +} + +/** + * Simple selector. Just assure that jumps are scheduled last. + */ +static ir_node *basic_selection(const arch_env_t *arch_env, nodeset *ready_set) { + ir_node *irn = NULL; + + /* assure that branches and constants are executed last */ + for (irn = nodeset_first(ready_set); irn; irn = nodeset_next(ready_set)) { + if (! arch_irn_class_is(arch_env, irn, branch)) { + nodeset_break(ready_set); + return irn; + } + } + + /* at last: schedule branches */ + irn = nodeset_first(ready_set); + nodeset_break(ready_set); + + return irn; +} + +/** +* The muchnik selector. +*/ +static ir_node *muchnik_select(void *block_env, nodeset *ready_set, nodeset *live_set) +{ + trace_env_t *env = block_env; + nodeset *mcands, *ecands; + sched_timestep_t max_delay = 0; + ir_node *irn; + + /* calculate the max delay of all candidates */ + foreach_nodeset(ready_set, irn) { + sched_timestep_t d = get_irn_delay(env, irn); + + max_delay = d > max_delay ? d : max_delay; + } + + mcands = new_nodeset(8); + ecands = new_nodeset(8); + + /* build mcands and ecands */ + foreach_nodeset(ready_set, irn) { + if (get_irn_delay(env, irn) == max_delay) { + nodeset_insert(mcands, irn); + if (get_irn_etime(env, irn) <= env->curr_time) + nodeset_insert(ecands, irn); + } + } + + /* select a node */ + if (nodeset_count(mcands) == 1) { + irn = nodeset_first(mcands); + DB((env->dbg, LEVEL_3, "\tirn = %+F, mcand = 1, max_delay = %u\n", irn, max_delay)); + } + else { + int cnt = nodeset_count(ecands); + if (cnt == 1) { + irn = nodeset_first(ecands); + + if (arch_irn_class_is(env->arch_env, irn, branch)) { + /* BEWARE: don't select a JUMP if others are still possible */ + goto force_mcands; + } + DB((env->dbg, LEVEL_3, "\tirn = %+F, ecand = 1, max_delay = %u\n", irn, max_delay)); + } + else if (cnt > 1) { + DB((env->dbg, LEVEL_3, "\tecand = %d, max_delay = %u\n", cnt, max_delay)); + irn = basic_selection(env->arch_env, ecands); + } + else { +force_mcands: + DB((env->dbg, LEVEL_3, "\tmcand = %d\n", nodeset_count(mcands))); + irn = basic_selection(env->arch_env, mcands); + } + } + + return irn; +} + +static void *muchnik_init_graph(const list_sched_selector_t *vtab, const arch_env_t *arch_env, ir_graph *irg) +{ + trace_env_t *env = trace_init(arch_env, irg); + env->selector = vtab; + env->selector_env = env; + return (void *)env; +} + +static void *muchnik_init_block(void *graph_env, ir_node *bl) +{ + trace_preprocess_block(graph_env, bl); + return graph_env; +} + +static const list_sched_selector_t muchnik_selector_struct = { + muchnik_init_graph, + muchnik_init_block, + muchnik_select, + NULL, /* to_appear_in_schedule */ + trace_node_ready, /* node_ready */ + trace_update_time, /* node_selected */ + NULL, /* exectime */ + NULL, /* latency */ + NULL, /* finish_block */ + trace_free /* finish_graph */ +}; + +const list_sched_selector_t *muchnik_selector = &muchnik_selector_struct; + +/** + * Execute the heuristic function. + */ +static ir_node *heuristic_select(void *block_env, nodeset *ns, nodeset *lv) +{ + trace_env_t *trace_env = block_env; + ir_node *irn, *cand = NULL; + int max_prio = INT_MIN; + int cur_prio = INT_MIN; + int cur_pressure = nodeset_count(lv); + int reg_fact, cand_reg_fact; + + /* prefer instructions which can be scheduled early */ +#define PRIO_TIME 16 + /* prefer instructions with lots of successors */ +#define PRIO_NUMSUCCS 8 + /* prefer instructions with long critical path */ +#define PRIO_LEVEL 12 + /* prefer instructions coming early in preorder */ +#define PRIO_PREORD 8 + /* weight of current register pressure */ +#define PRIO_CUR_PRESS 20 + /* weight of register pressure difference */ +#define PRIO_CHG_PRESS 8 + + /* priority based selection, heuristic inspired by mueller diss */ + foreach_nodeset(ns, irn) { + /* make sure that branches are scheduled last */ + if (! arch_irn_class_is(trace_env->arch_env, irn, branch)) { + int rdiff = get_irn_reg_diff(trace_env, irn); + int sign = rdiff < 0; + int chg = (rdiff < 0 ? -rdiff : rdiff) << PRIO_CHG_PRESS; + + reg_fact = chg << cur_pressure; + if (reg_fact < chg) + reg_fact = INT_MAX - 2; + reg_fact = sign ? -reg_fact : reg_fact; + + cur_prio = (get_irn_critical_path_len(trace_env, irn) << PRIO_LEVEL) + //- (get_irn_delay(trace_env, irn) << PRIO_LEVEL) + + (get_irn_num_user(trace_env, irn) << PRIO_NUMSUCCS) + - (get_irn_etime(trace_env, irn) << PRIO_TIME) + //- ((get_irn_reg_diff(trace_env, irn) >> PRIO_CHG_PRESS) << ((cur_pressure >> PRIO_CUR_PRESS) - 3)) + - reg_fact + + (get_irn_preorder(trace_env, irn) << PRIO_PREORD); /* high preorder means early schedule */ + if (cur_prio > max_prio) { + cand = irn; + max_prio = cur_prio; + cand_reg_fact = reg_fact; + } + + DBG((trace_env->dbg, LEVEL_4, "checked NODE %+F\n", irn)); + DBG((trace_env->dbg, LEVEL_4, "\tpriority: %d\n", cur_prio)); + 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)); + DBG((trace_env->dbg, LEVEL_4, "\tdelay: %d (%d)\n", get_irn_delay(trace_env, irn), get_irn_delay(trace_env, irn) << PRIO_LEVEL)); + 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)); + 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))); + DBG((trace_env->dbg, LEVEL_4, "\tpreorder: %d (%d)\n", get_irn_preorder(trace_env, irn), get_irn_preorder(trace_env, irn) << PRIO_PREORD)); + DBG((trace_env->dbg, LEVEL_4, "\treg diff: %d (%d)\n", get_irn_reg_diff(trace_env, irn), 0 - cand_reg_fact)); + DBG((trace_env->dbg, LEVEL_4, "\tpressure: %d\n", cur_pressure)); + } + } + + if (cand) { + DBG((trace_env->dbg, LEVEL_4, "heuristic selected %+F:\n", cand)); + } + else { + cand = basic_selection(trace_env->arch_env, ns); + } + + return cand; +} + +static const list_sched_selector_t heuristic_selector_struct = { + muchnik_init_graph, + muchnik_init_block, + heuristic_select, + NULL, /* to_appear_in_schedule */ + trace_node_ready, /* node_ready */ + trace_update_time, /* node_selected */ + NULL, /* exectime */ + NULL, /* latency */ + NULL, /* finish_block */ + trace_free /* finish_graph */ +}; + +const list_sched_selector_t *heuristic_selector = &heuristic_selector_struct; diff --git a/ir/be/beschedtrivial.c b/ir/be/beschedtrivial.c new file mode 100644 index 000000000..2a4f6adaf --- /dev/null +++ b/ir/be/beschedtrivial.c @@ -0,0 +1,61 @@ +/** + * Trivial node selector. + * @author Christian Wuerdig + * @date 29.08.2006 + * @cvs-id $Id$ + */ + +#include + +#include "besched_t.h" +#include "belistsched.h" + +/** + * The trivial selector: + * Just assure that branches are executed last, otherwise select + * the first node ready. + */ +static ir_node *trivial_select(void *block_env, nodeset *ready_set, nodeset *live_set) +{ + const arch_env_t *arch_env = block_env; + ir_node *irn = NULL; + + /* assure that branches and constants are executed last */ + for (irn = nodeset_first(ready_set); irn; irn = nodeset_next(ready_set)) { + if (! arch_irn_class_is(arch_env, irn, branch)) { + nodeset_break(ready_set); + return irn; + } + } + + /* at last: schedule branches */ + irn = nodeset_first(ready_set); + nodeset_break(ready_set); + + return irn; +} + +static void *trivial_init_graph(const list_sched_selector_t *vtab, const arch_env_t *arch_env, ir_graph *irg) +{ + return (void *)arch_env; +} + +static void *trivial_init_block(void *graph_env, ir_node *bl) +{ + return graph_env; +} + +static const list_sched_selector_t trivial_selector_struct = { + trivial_init_graph, + trivial_init_block, + trivial_select, + NULL, /* to_appear_in_schedule */ + NULL, /* node_ready */ + NULL, /* node_selected */ + NULL, /* exectime */ + NULL, /* latency */ + NULL, /* finish_block */ + NULL /* finish_graph */ +}; + +const list_sched_selector_t *trivial_selector = &trivial_selector_struct; -- 2.20.1