2 * Copyright (C) 1995-2008 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 Register pressure node selector.
23 * @author Sebastian Hack
31 #include "iredges_t.h"
36 #include "belistsched.h"
40 typedef struct usage_stats_t {
42 struct usage_stats_t *next;
44 int uses_in_block; /**< Number of uses inside the current block. */
45 int already_consumed; /**< Number of insns using this value already
50 const list_sched_selector_t *vtab;
51 } reg_pressure_main_env_t;
55 const reg_pressure_main_env_t *main_env;
57 ir_nodeset_t already_scheduled;
58 } reg_pressure_selector_env_t;
63 * Ugly global variable for the compare function
64 * since qsort(3) does not pass an extra pointer.
66 static ir_node *curr_bl = NULL;
68 static int cmp_usage(const void *a, const void *b)
70 struct trivial_sched_env *env;
75 res = is_live_end(env->curr_bl, a) - is_live_end(env->curr_bl, b);
78 * One of them is live at the end of the block.
79 * Then, that one shall be scheduled at after the other
89 static inline usage_stats_t *get_or_set_usage_stats(reg_pressure_selector_env_t *env, ir_node *irn)
91 usage_stats_t *us = (usage_stats_t*)get_irn_link(irn);
94 us = OALLOC(&env->obst, usage_stats_t);
96 us->already_consumed = 0;
97 us->max_hops = INT_MAX;
100 set_irn_link(irn, us);
106 static inline usage_stats_t *get_usage_stats(ir_node *irn)
108 usage_stats_t *us = (usage_stats_t*)get_irn_link(irn);
109 assert(us && "This node must have usage stats");
113 static int max_hops_walker(reg_pressure_selector_env_t *env, ir_node *irn, ir_node *curr_bl, int depth, unsigned visited_nr)
115 ir_node *bl = get_nodes_block(irn);
117 * If the reached node is not in the block desired,
118 * return the value passed for this situation.
120 if (get_nodes_block(irn) != bl)
121 return block_dominates(bl, curr_bl) ? 0 : INT_MAX;
124 * If the node is in the current block but not
125 * yet scheduled, we keep on searching from that node.
127 if (!ir_nodeset_contains(&env->already_scheduled, irn)) {
130 for (i = 0, n = get_irn_ins_or_deps(irn); i < n; ++i) {
131 ir_node *operand = get_irn_in_or_dep(irn, i);
133 if (get_irn_visited(operand) < visited_nr) {
136 set_irn_visited(operand, visited_nr);
137 tmp = max_hops_walker(env, operand, bl, depth + 1, visited_nr);
146 * If the node is in the current block and scheduled, return
147 * the depth which indicates the number of steps to the
148 * region of scheduled nodes.
153 static int compute_max_hops(reg_pressure_selector_env_t *env, ir_node *irn)
155 ir_node *bl = get_nodes_block(irn);
156 ir_graph *irg = get_irn_irg(bl);
159 const ir_edge_t *edge;
161 foreach_out_edge(irn, edge) {
162 ir_node *user = get_edge_src_irn(edge);
163 unsigned visited_nr = get_irg_visited(irg) + 1;
166 set_irg_visited(irg, visited_nr);
167 max_hops = max_hops_walker(env, user, irn, 0, visited_nr);
168 res = MAX(res, max_hops);
174 static void *reg_pressure_graph_init(const list_sched_selector_t *vtab, ir_graph *irg)
176 reg_pressure_main_env_t *main_env = XMALLOC(reg_pressure_main_env_t);
178 main_env->vtab = vtab;
179 irg_walk_graph(irg, firm_clear_link, NULL, NULL);
184 static void *reg_pressure_block_init(void *graph_env, ir_node *bl)
187 reg_pressure_selector_env_t *env = XMALLOC(reg_pressure_selector_env_t);
189 obstack_init(&env->obst);
190 ir_nodeset_init(&env->already_scheduled);
192 env->main_env = (reg_pressure_main_env_t*)graph_env;
195 * Collect usage statistics.
197 sched_foreach(bl, irn) {
198 if (to_appear_in_schedule(irn)) {
201 for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
202 //ir_node *op = get_irn_n(irn, i);
203 if (to_appear_in_schedule(irn)) {
204 usage_stats_t *us = get_or_set_usage_stats(env, irn);
205 #if 0 /* Liveness is not computed here! */
206 if (is_live_end(bl, op))
207 us->uses_in_block = 99999;
219 static void reg_pressure_block_free(void *block_env)
221 reg_pressure_selector_env_t *env = (reg_pressure_selector_env_t*)block_env;
224 for (us = env->root; us; us = us->next)
225 set_irn_link(us->irn, NULL);
227 obstack_free(&env->obst, NULL);
228 ir_nodeset_destroy(&env->already_scheduled);
232 static int get_result_hops_sum(reg_pressure_selector_env_t *env, ir_node *irn)
235 if (get_irn_mode(irn) == mode_T) {
236 const ir_edge_t *edge;
238 foreach_out_edge(irn, edge)
239 res += get_result_hops_sum(env, get_edge_src_irn(edge));
242 else if (mode_is_data(get_irn_mode(irn)))
243 res = compute_max_hops(env, irn);
249 static inline int reg_pr_costs(reg_pressure_selector_env_t *env, ir_node *irn)
254 for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
255 ir_node *op = get_irn_n(irn, i);
257 if (to_appear_in_schedule(op))
258 sum += compute_max_hops(env, op);
261 sum += get_result_hops_sum(env, irn);
266 static ir_node *reg_pressure_select(void *block_env, ir_nodeset_t *ready_set,
267 ir_nodeset_t *live_set)
269 ir_nodeset_iterator_t iter;
270 reg_pressure_selector_env_t *env = (reg_pressure_selector_env_t*)block_env;
271 ir_node *irn, *res = NULL;
272 int curr_cost = INT_MAX;
275 assert(ir_nodeset_size(ready_set) > 0);
277 ir_nodeset_iterator_init(&iter, ready_set);
278 while ( (irn = ir_nodeset_iterator_next(&iter)) != NULL) {
280 Ignore branch instructions for the time being.
281 They should only be scheduled if there is nothing else.
284 int costs = reg_pr_costs(env, irn);
285 if (costs <= curr_cost) {
293 There was no result so we only saw a branch.
298 ir_nodeset_iterator_init(&iter, ready_set);
299 res = ir_nodeset_iterator_next(&iter);
301 assert(res && "There must be a node scheduled.");
304 ir_nodeset_insert(&env->already_scheduled, res);
308 const list_sched_selector_t reg_pressure_selector = {
309 reg_pressure_graph_init,
310 reg_pressure_block_init,
312 NULL, /* node_ready */
313 NULL, /* node_selected */
316 reg_pressure_block_free,