2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Naive spilling algorithm
9 * @author Matthias Braun
12 * This implements a naive spilling algorithm. It is designed to produce
13 * similar effects to the spill decisions produced by traditional graph
14 * coloring register allocators that spill while they are coloring the graph.
16 * This spiller walks over all blocks and looks for places with too high
17 * register pressure where it spills the values that are cheapest to spill.
18 * Spilling in this context means placing a spill instruction behind the
19 * definition of the value and a reload before each usage.
25 #include "irnodeset.h"
28 #include "iredges_t.h"
33 #include "bespillutil.h"
41 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
43 static spill_env_t *spill_env;
44 static unsigned n_regs;
45 static const arch_register_class_t *cls;
46 static const be_lv_t *lv;
47 static bitset_t *spilled_nodes;
49 typedef struct spill_candidate_t spill_candidate_t;
50 struct spill_candidate_t {
55 static int compare_spill_candidates_desc(const void *d1, const void *d2)
57 const spill_candidate_t *c1 = (const spill_candidate_t*)d1;
58 const spill_candidate_t *c2 = (const spill_candidate_t*)d2;
60 return (int) (c1->costs - c2->costs);
63 static double get_spill_costs(ir_node *node)
65 ir_node *spill_place = skip_Proj(node);
66 double costs = be_get_spill_costs(spill_env, node, spill_place);
68 foreach_out_edge(node, edge) {
69 ir_node *use = get_edge_src_irn(edge);
71 /* keeps should be directly below the node */
72 if (be_is_Keep(use)) {
77 int in = get_edge_src_pos(edge);
78 ir_node *block = get_nodes_block(use);
80 costs += be_get_reload_costs_on_edge(spill_env, node, block, in);
82 costs += be_get_reload_costs(spill_env, node, use);
90 * spills a node by placing a reload before each usage
92 static void spill_node(ir_node *node)
94 DBG((dbg, LEVEL_3, "\tspilling %+F\n", node));
96 foreach_out_edge(node, edge) {
97 ir_node *use = get_edge_src_irn(edge);
104 int in = get_edge_src_pos(edge);
105 ir_node *block = get_nodes_block(use);
107 be_add_reload_on_edge(spill_env, node, block, in, cls, 1);
109 be_add_reload(spill_env, node, use, cls, 1);
113 bitset_set(spilled_nodes, get_irn_idx(node));
116 static unsigned get_value_width(const ir_node *node)
118 const arch_register_req_t *req = arch_get_irn_register_req(node);
123 * spill @p n nodes from a nodeset. Removes the nodes from the nodeset and
124 * sets the spilled bits in spilled_nodes.
126 static void do_spilling(ir_nodeset_t *live_nodes, ir_node *node)
128 size_t values_defined = 0;
129 be_foreach_definition(node, cls, value, req,
131 assert(req->width >= 1);
132 values_defined += req->width;
135 /* we need registers for the non-live argument values */
136 size_t free_regs_needed = 0;
137 be_foreach_use(node, cls, in_req_, use, pred_req_,
138 if (!ir_nodeset_contains(live_nodes, use)) {
139 free_regs_needed += get_value_width(use);
143 /* we can reuse all reloaded values for the defined values, but we might
144 * need even more registers */
145 if (values_defined > free_regs_needed)
146 free_regs_needed = values_defined;
148 size_t n_live_nodes = ir_nodeset_size(live_nodes);
149 int spills_needed = (n_live_nodes + free_regs_needed) - n_regs;
150 if (spills_needed <= 0)
152 DBG((dbg, LEVEL_2, "\tspills needed after %+F: %d\n", node, spills_needed));
154 spill_candidate_t *candidates = ALLOCAN(spill_candidate_t, n_live_nodes);
156 /* construct array with spill candidates and calculate their costs */
158 foreach_ir_nodeset(live_nodes, n, iter) {
159 spill_candidate_t *candidate = & candidates[c];
161 assert(!bitset_is_set(spilled_nodes, get_irn_idx(n)));
164 candidate->costs = get_spill_costs(n);
167 assert(c == n_live_nodes);
169 /* sort spill candidates */
170 qsort(candidates, n_live_nodes, sizeof(candidates[0]),
171 compare_spill_candidates_desc);
173 /* spill cheapest ones */
175 while (spills_needed > 0) {
176 if (cand_idx >= n_live_nodes) {
177 panic("can't spill enough values for node %+F", node);
180 spill_candidate_t *candidate = &candidates[cand_idx];
181 ir_node *cand_node = candidate->node;
184 if (arch_irn_is(skip_Proj_const(cand_node), dont_spill))
187 /* make sure the node is not an argument of the instruction */
189 int arity = get_irn_arity(node);
190 for (int i = 0; i < arity; ++i) {
191 ir_node *in = get_irn_n(node, i);
192 if (in == cand_node) {
200 spill_node(cand_node);
201 ir_nodeset_remove(live_nodes, cand_node);
202 spills_needed -= get_value_width(cand_node);
207 * removes all values from the nodeset that are defined by node
209 static void remove_defs(ir_node *node, ir_nodeset_t *nodeset)
211 /* You must break out of your loop when hitting the first phi function. */
212 assert(!is_Phi(node));
214 be_foreach_definition(node, cls, value, req,
215 ir_nodeset_remove(nodeset, value);
219 static void add_uses(ir_node *node, ir_nodeset_t *nodeset)
221 int arity = get_irn_arity(node);
222 for (int i = 0; i < arity; ++i) {
223 ir_node *op = get_irn_n(node, i);
225 if (arch_irn_consider_in_reg_alloc(cls, op) &&
226 !bitset_is_set(spilled_nodes, get_irn_idx(op))) {
227 ir_nodeset_insert(nodeset, op);
232 static __attribute__((unused))
233 void print_nodeset(ir_nodeset_t *nodeset)
235 foreach_ir_nodeset(nodeset, node, iter) {
236 ir_fprintf(stderr, "%+F ", node);
238 fprintf(stderr, "\n");
242 * make sure register pressure in a block is always equal or below the number
243 * of available registers
245 static void spill_block(ir_node *block, void *data)
248 DBG((dbg, LEVEL_1, "spilling block %+F\n", block));
250 /* construct set of live nodes at end of block */
251 ir_nodeset_t live_nodes;
252 ir_nodeset_init(&live_nodes);
253 be_liveness_end_of_block(lv, cls, block, &live_nodes);
255 /* remove already spilled nodes from liveset */
256 foreach_ir_nodeset(&live_nodes, node, iter) {
257 DBG((dbg, LEVEL_2, "\t%+F is live-end... ", node));
258 if (bitset_is_set(spilled_nodes, get_irn_idx(node))) {
259 DBG((dbg, LEVEL_2, "but spilled; removing.\n"));
260 ir_nodeset_remove_iterator(&live_nodes, &iter);
262 DBG((dbg, LEVEL_2, "keeping.\n"));
266 /* walk schedule backwards and spill until register pressure is fine at
268 sched_foreach_reverse(block, node) {
272 remove_defs(node, &live_nodes);
273 do_spilling(&live_nodes, node);
274 add_uses(node, &live_nodes);
277 /* until now only the values of some phis have been spilled the phis itself
278 * are still there and occupy registers, so we need to count them and might
279 * have to spill some of them. */
280 int n_phi_values_spilled = 0;
281 sched_foreach(block, node) {
285 if (bitset_is_set(spilled_nodes, get_irn_idx(node))) {
286 n_phi_values_spilled += get_value_width(node);
290 int live_nodes_pressure = 0;
291 foreach_ir_nodeset(&live_nodes, node, iter) {
292 live_nodes_pressure += get_value_width(node);
295 /* calculate how many of the phis need to be spilled */
296 int regpressure = live_nodes_pressure + n_phi_values_spilled;
297 int phi_spills_needed = regpressure - n_regs;
298 DBG((dbg, LEVEL_3, "Regpressure before phis: %d phispills: %d\n",
299 regpressure, phi_spills_needed));
301 /* spill as many phis as needed */
302 /* TODO: we should really estimate costs of the phi spill as well...
303 * and preferably spill phis with lower costs... */
304 sched_foreach(block, node) {
307 if (phi_spills_needed <= 0)
310 if (!bitset_is_set(spilled_nodes, get_irn_idx(node)))
313 be_spill_phi(spill_env, node);
314 phi_spills_needed -= get_value_width(node);
316 assert(phi_spills_needed <= 0);
318 ir_nodeset_destroy(&live_nodes);
321 static void be_spill_daemel(ir_graph *irg, const arch_register_class_t *new_cls)
323 n_regs = be_get_n_allocatable_regs(irg, new_cls);
327 be_assure_live_sets(irg);
329 spill_env = be_new_spill_env(irg);
331 lv = be_get_irg_liveness(irg);
332 spilled_nodes = bitset_malloc(get_irg_last_idx(irg));
334 DBG((dbg, LEVEL_1, "*** RegClass %s\n", cls->name));
336 irg_block_walk_graph(irg, spill_block, NULL, NULL);
338 bitset_free(spilled_nodes);
340 be_insert_spills_reloads(spill_env);
341 be_delete_spill_env(spill_env);
344 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_daemelspill)
345 void be_init_daemelspill(void)
347 static be_spiller_t daemel_spiller = {
351 be_register_spiller("daemel", &daemel_spiller);
352 FIRM_DBG_REGISTER(dbg, "firm.be.spilldaemel");