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 Naive spilling algorithm
23 * @author Matthias Braun
26 * This implements a naive spilling algorithm. It is designed to produce
27 * similar effects to the spill decisions produced by traditional graph
28 * coloring register allocators that spill while they are coloring the graph.
30 * This spiller walks over all blocks and looks for places with too high
31 * register pressure where it spills the values that are cheapest to spill.
32 * Spilling in this context means placing a spill instruction behind the
33 * definition of the value and a reload before each usage.
39 #include "irnodeset.h"
42 #include "iredges_t.h"
47 #include "bespillutil.h"
55 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
57 static spill_env_t *spill_env;
58 static unsigned n_regs;
59 static const arch_register_class_t *cls;
60 static const be_lv_t *lv;
61 static bitset_t *spilled_nodes;
63 typedef struct spill_candidate_t spill_candidate_t;
64 struct spill_candidate_t {
69 static int compare_spill_candidates_desc(const void *d1, const void *d2)
71 const spill_candidate_t *c1 = (const spill_candidate_t*)d1;
72 const spill_candidate_t *c2 = (const spill_candidate_t*)d2;
74 return (int) (c1->costs - c2->costs);
77 static double get_spill_costs(ir_node *node)
79 ir_node *spill_place = skip_Proj(node);
80 double costs = be_get_spill_costs(spill_env, node, spill_place);
82 foreach_out_edge(node, edge) {
83 ir_node *use = get_edge_src_irn(edge);
85 /* keeps should be directly below the node */
86 if (be_is_Keep(use)) {
91 int in = get_edge_src_pos(edge);
92 ir_node *block = get_nodes_block(use);
94 costs += be_get_reload_costs_on_edge(spill_env, node, block, in);
96 costs += be_get_reload_costs(spill_env, node, use);
104 * spills a node by placing a reload before each usage
106 static void spill_node(ir_node *node)
108 DBG((dbg, LEVEL_3, "\tspilling %+F\n", node));
110 foreach_out_edge(node, edge) {
111 ir_node *use = get_edge_src_irn(edge);
118 int in = get_edge_src_pos(edge);
119 ir_node *block = get_nodes_block(use);
121 be_add_reload_on_edge(spill_env, node, block, in, cls, 1);
123 be_add_reload(spill_env, node, use, cls, 1);
127 bitset_set(spilled_nodes, get_irn_idx(node));
130 static unsigned get_value_width(const ir_node *node)
132 const arch_register_req_t *req = arch_get_irn_register_req(node);
137 * spill @p n nodes from a nodeset. Removes the nodes from the nodeset and
138 * sets the spilled bits in spilled_nodes.
140 static void do_spilling(ir_nodeset_t *live_nodes, ir_node *node)
142 size_t values_defined = 0;
143 be_foreach_definition(node, cls, value,
145 assert(req_->width >= 1);
146 values_defined += req_->width;
149 /* we need registers for the non-live argument values */
150 size_t free_regs_needed = 0;
151 int arity = get_irn_arity(node);
152 for (int i = 0; i < arity; ++i) {
153 ir_node *pred = get_irn_n(node, i);
154 if (arch_irn_consider_in_reg_alloc(cls, pred)
155 && !ir_nodeset_contains(live_nodes, pred)) {
156 free_regs_needed += get_value_width(pred);
160 /* we can reuse all reloaded values for the defined values, but we might
161 * need even more registers */
162 if (values_defined > free_regs_needed)
163 free_regs_needed = values_defined;
165 size_t n_live_nodes = ir_nodeset_size(live_nodes);
166 int spills_needed = (n_live_nodes + free_regs_needed) - n_regs;
167 if (spills_needed <= 0)
169 DBG((dbg, LEVEL_2, "\tspills needed after %+F: %d\n", node, spills_needed));
171 spill_candidate_t *candidates = ALLOCAN(spill_candidate_t, n_live_nodes);
173 /* construct array with spill candidates and calculate their costs */
175 foreach_ir_nodeset(live_nodes, n, iter) {
176 spill_candidate_t *candidate = & candidates[c];
178 assert(!bitset_is_set(spilled_nodes, get_irn_idx(n)));
181 candidate->costs = get_spill_costs(n);
184 assert(c == n_live_nodes);
186 /* sort spill candidates */
187 qsort(candidates, n_live_nodes, sizeof(candidates[0]),
188 compare_spill_candidates_desc);
190 /* spill cheapest ones */
192 while (spills_needed > 0) {
193 if (cand_idx >= n_live_nodes) {
194 panic("can't spill enough values for node %+F", node);
197 spill_candidate_t *candidate = &candidates[cand_idx];
198 ir_node *cand_node = candidate->node;
201 if (arch_irn_is(skip_Proj_const(cand_node), dont_spill))
204 /* make sure the node is not an argument of the instruction */
206 for (int i = 0; i < arity; ++i) {
207 ir_node *in = get_irn_n(node, i);
208 if (in == cand_node) {
216 spill_node(cand_node);
217 ir_nodeset_remove(live_nodes, cand_node);
218 spills_needed -= get_value_width(cand_node);
223 * removes all values from the nodeset that are defined by node
225 static void remove_defs(ir_node *node, ir_nodeset_t *nodeset)
227 /* You must break out of your loop when hitting the first phi function. */
228 assert(!is_Phi(node));
230 be_foreach_definition(node, cls, value,
231 ir_nodeset_remove(nodeset, value);
235 static void add_uses(ir_node *node, ir_nodeset_t *nodeset)
237 int arity = get_irn_arity(node);
238 for (int i = 0; i < arity; ++i) {
239 ir_node *op = get_irn_n(node, i);
241 if (arch_irn_consider_in_reg_alloc(cls, op) &&
242 !bitset_is_set(spilled_nodes, get_irn_idx(op))) {
243 ir_nodeset_insert(nodeset, op);
248 static __attribute__((unused))
249 void print_nodeset(ir_nodeset_t *nodeset)
251 foreach_ir_nodeset(nodeset, node, iter) {
252 ir_fprintf(stderr, "%+F ", node);
254 fprintf(stderr, "\n");
258 * make sure register pressure in a block is always equal or below the number
259 * of available registers
261 static void spill_block(ir_node *block, void *data)
264 DBG((dbg, LEVEL_1, "spilling block %+F\n", block));
266 /* construct set of live nodes at end of block */
267 ir_nodeset_t live_nodes;
268 ir_nodeset_init(&live_nodes);
269 be_liveness_end_of_block(lv, cls, block, &live_nodes);
271 /* remove already spilled nodes from liveset */
272 foreach_ir_nodeset(&live_nodes, node, iter) {
273 DBG((dbg, LEVEL_2, "\t%+F is live-end... ", node));
274 if (bitset_is_set(spilled_nodes, get_irn_idx(node))) {
275 DBG((dbg, LEVEL_2, "but spilled; removing.\n"));
276 ir_nodeset_remove_iterator(&live_nodes, &iter);
278 DBG((dbg, LEVEL_2, "keeping.\n"));
282 /* walk schedule backwards and spill until register pressure is fine at
284 sched_foreach_reverse(block, node) {
288 remove_defs(node, &live_nodes);
289 do_spilling(&live_nodes, node);
290 add_uses(node, &live_nodes);
293 /* until now only the values of some phis have been spilled the phis itself
294 * are still there and occupy registers, so we need to count them and might
295 * have to spill some of them. */
296 int n_phi_values_spilled = 0;
297 sched_foreach(block, node) {
301 if (bitset_is_set(spilled_nodes, get_irn_idx(node))) {
302 n_phi_values_spilled += get_value_width(node);
306 int live_nodes_pressure = 0;
307 foreach_ir_nodeset(&live_nodes, node, iter) {
308 live_nodes_pressure += get_value_width(node);
311 /* calculate how many of the phis need to be spilled */
312 int regpressure = live_nodes_pressure + n_phi_values_spilled;
313 int phi_spills_needed = regpressure - n_regs;
314 DBG((dbg, LEVEL_3, "Regpressure before phis: %d phispills: %d\n",
315 regpressure, phi_spills_needed));
317 /* spill as many phis as needed */
318 /* TODO: we should really estimate costs of the phi spill as well...
319 * and preferably spill phis with lower costs... */
320 sched_foreach(block, node) {
323 if (phi_spills_needed <= 0)
326 if (!bitset_is_set(spilled_nodes, get_irn_idx(node)))
329 be_spill_phi(spill_env, node);
330 phi_spills_needed -= get_value_width(node);
332 assert(phi_spills_needed <= 0);
334 ir_nodeset_destroy(&live_nodes);
337 static void be_spill_daemel(ir_graph *irg, const arch_register_class_t *new_cls)
339 n_regs = be_get_n_allocatable_regs(irg, new_cls);
343 be_assure_live_sets(irg);
345 spill_env = be_new_spill_env(irg);
347 lv = be_get_irg_liveness(irg);
348 spilled_nodes = bitset_malloc(get_irg_last_idx(irg));
350 DBG((dbg, LEVEL_1, "*** RegClass %s\n", cls->name));
352 irg_block_walk_graph(irg, spill_block, NULL, NULL);
354 bitset_free(spilled_nodes);
356 be_insert_spills_reloads(spill_env);
357 be_delete_spill_env(spill_env);
360 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_daemelspill)
361 void be_init_daemelspill(void)
363 static be_spiller_t daemel_spiller = {
367 be_register_spiller("daemel", &daemel_spiller);
368 FIRM_DBG_REGISTER(dbg, "firm.be.spilldaemel");