2 * Regpressure node selector.
3 * Originally implemented by Sebastian Hack.
4 * @author Christian Wuerdig
11 #include "iredges_t.h"
14 #include "besched_t.h"
15 #include "belistsched.h"
19 typedef struct _usage_stats_t {
21 struct _usage_stats_t *next;
23 int uses_in_block; /**< Number of uses inside the current block. */
24 int already_consumed; /**< Number of insns using this value already
29 const list_sched_selector_t *vtab;
30 const arch_env_t *arch_env;
31 } reg_pressure_main_env_t;
35 const reg_pressure_main_env_t *main_env;
37 nodeset *already_scheduled;
38 } reg_pressure_selector_env_t;
43 * Ugly global variable for the compare function
44 * since qsort(3) does not pass an extra pointer.
46 static ir_node *curr_bl = NULL;
48 static int cmp_usage(const void *a, const void *b)
50 struct trivial_sched_env *env;
55 res = is_live_end(env->curr_bl, a) - is_live_end(env->curr_bl, b);
58 * One of them is live at the end of the block.
59 * Then, that one shall be scheduled at after the other
69 static INLINE usage_stats_t *get_or_set_usage_stats(reg_pressure_selector_env_t *env, ir_node *irn)
71 usage_stats_t *us = get_irn_link(irn);
74 us = obstack_alloc(&env->obst, sizeof(us[0]));
76 us->already_consumed = 0;
77 us->max_hops = INT_MAX;
80 set_irn_link(irn, us);
86 static INLINE usage_stats_t *get_usage_stats(ir_node *irn)
88 usage_stats_t *us = get_irn_link(irn);
89 assert(us && "This node must have usage stats");
93 static int max_hops_walker(reg_pressure_selector_env_t *env, ir_node *irn, ir_node *curr_bl, int depth, unsigned visited_nr)
95 ir_node *bl = get_nodes_block(irn);
97 * If the reached node is not in the block desired,
98 * return the value passed for this situation.
100 if(get_nodes_block(irn) != bl)
101 return block_dominates(bl, curr_bl) ? 0 : INT_MAX;
104 * If the node is in the current block but not
105 * yet scheduled, we keep on searching from that node.
107 if(!nodeset_find(env->already_scheduled, irn)) {
110 for(i = 0, n = get_irn_ins_or_deps(irn); i < n; ++i) {
111 ir_node *operand = get_irn_in_or_dep(irn, i);
113 if(get_irn_visited(operand) < visited_nr) {
116 set_irn_visited(operand, visited_nr);
117 tmp = max_hops_walker(env, operand, bl, depth + 1, visited_nr);
126 * If the node is in the current block and scheduled, return
127 * the depth which indicates the number of steps to the
128 * region of scheduled nodes.
133 static int compute_max_hops(reg_pressure_selector_env_t *env, ir_node *irn)
135 ir_node *bl = get_nodes_block(irn);
136 ir_graph *irg = get_irn_irg(bl);
139 const ir_edge_t *edge;
141 foreach_out_edge(irn, edge) {
142 ir_node *user = get_edge_src_irn(edge);
143 unsigned visited_nr = get_irg_visited(irg) + 1;
146 set_irg_visited(irg, visited_nr);
147 max_hops = max_hops_walker(env, user, irn, 0, visited_nr);
148 res = MAX(res, max_hops);
154 static void *reg_pressure_graph_init(const list_sched_selector_t *vtab, const arch_env_t *arch_env, ir_graph *irg)
156 reg_pressure_main_env_t *main_env = xmalloc(sizeof(main_env[0]));
158 main_env->arch_env = arch_env;
159 main_env->vtab = vtab;
160 irg_walk_graph(irg, firm_clear_link, NULL, NULL);
165 static INLINE int must_appear_in_schedule(const list_sched_selector_t *sel, void *block_env, const ir_node *irn)
169 if(sel->to_appear_in_schedule)
170 res = sel->to_appear_in_schedule(block_env, irn);
172 return res >= 0 ? res : (to_appear_in_schedule(irn) || be_is_Keep(irn) || be_is_CopyKeep(irn) || be_is_RegParams(irn));
175 static void *reg_pressure_block_init(void *graph_env, ir_node *bl)
178 reg_pressure_selector_env_t *env = xmalloc(sizeof(env[0]));
180 obstack_init(&env->obst);
181 env->already_scheduled = new_nodeset(32);
183 env->main_env = graph_env;
186 * Collect usage statistics.
188 sched_foreach(bl, irn) {
189 if(must_appear_in_schedule(env->main_env->vtab, env, irn)) {
192 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
193 //ir_node *op = get_irn_n(irn, i);
194 if(must_appear_in_schedule(env->main_env->vtab, env, irn)) {
195 usage_stats_t *us = get_or_set_usage_stats(env, irn);
196 #if 0 /* Liveness is not computed here! */
197 if(is_live_end(bl, op))
198 us->uses_in_block = 99999;
210 static void reg_pressure_block_free(void *block_env)
212 reg_pressure_selector_env_t *env = block_env;
215 for(us = env->root; us; us = us->next)
216 set_irn_link(us->irn, NULL);
218 obstack_free(&env->obst, NULL);
219 del_nodeset(env->already_scheduled);
223 static int get_result_hops_sum(reg_pressure_selector_env_t *env, ir_node *irn)
226 if(get_irn_mode(irn) == mode_T) {
227 const ir_edge_t *edge;
229 foreach_out_edge(irn, edge)
230 res += get_result_hops_sum(env, get_edge_src_irn(edge));
233 else if(mode_is_data(get_irn_mode(irn)))
234 res = compute_max_hops(env, irn);
240 static INLINE int reg_pr_costs(reg_pressure_selector_env_t *env, ir_node *irn)
245 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
246 ir_node *op = get_irn_n(irn, i);
248 if(must_appear_in_schedule(env->main_env->vtab, env, op))
249 sum += compute_max_hops(env, op);
252 sum += get_result_hops_sum(env, irn);
257 static ir_node *reg_pressure_select(void *block_env, nodeset *ready_set, nodeset *live_set)
259 reg_pressure_selector_env_t *env = block_env;
260 ir_node *irn, *res = NULL;
261 int curr_cost = INT_MAX;
263 assert(nodeset_count(ready_set) > 0);
265 for (irn = nodeset_first(ready_set); irn; irn = nodeset_next(ready_set)) {
267 Ignore branch instructions for the time being.
268 They should only be scheduled if there is nothing else.
270 if (! arch_irn_class_is(env->main_env->arch_env, irn, branch)) {
271 int costs = reg_pr_costs(env, irn);
272 if (costs <= curr_cost) {
280 There was no result so we only saw a branch.
285 res = nodeset_first(ready_set);
286 nodeset_break(ready_set);
288 assert(res && "There must be a node scheduled.");
291 nodeset_insert(env->already_scheduled, res);
295 static const list_sched_selector_t reg_pressure_selector_struct = {
296 reg_pressure_graph_init,
297 reg_pressure_block_init,
299 NULL, /* to_appear_in_schedule */
300 NULL, /* node_ready */
301 NULL, /* node_selected */
304 reg_pressure_block_free,
308 const list_sched_selector_t *reg_pressure_selector = ®_pressure_selector_struct;