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 Naive spilling algorithm
23 * @author Matthias Braun
25 * @version $Id: bespillbelady.c 13913 2007-05-18 12:48:56Z matze $
27 * This implements a naive spilling algorithm. It is designed to produce similar
28 * effects to the spill decisions produced by traditional graph coloring
29 * register allocators that spill while they are coloring the graph.
31 * This spiller walks over all blocks and looks for places with too high
32 * register pressure where it spills the values that are cheapest to spill.
33 * Spilling in this context means placing a spill instruction behind the
34 * definition of the value and a reload before each usage.
40 #include "irnodeset.h"
43 #include "iredges_t.h"
48 #include "bespillutil.h"
57 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
59 static spill_env_t *spill_env;
61 static const arch_register_class_t *cls;
62 static const be_lv_t *lv;
63 static bitset_t *spilled_nodes;
65 typedef struct spill_candidate_t spill_candidate_t;
66 struct spill_candidate_t {
71 static int compare_spill_candidates_desc(const void *d1, const void *d2)
73 const spill_candidate_t *c1 = d1;
74 const spill_candidate_t *c2 = d2;
76 return (int) (c1->costs - c2->costs);
79 static double get_spill_costs(ir_node *node)
81 const ir_edge_t *edge;
82 ir_node *spill_place = skip_Proj(node);
83 double costs = be_get_spill_costs(spill_env, node,
86 foreach_out_edge(node, edge) {
87 ir_node *use = get_edge_src_irn(edge);
89 /* keeps should be directly below the node */
90 if (be_is_Keep(use)) {
95 int in = get_edge_src_pos(edge);
96 ir_node *block = get_nodes_block(use);
98 costs += be_get_reload_costs_on_edge(spill_env, node, block, in);
100 costs += be_get_reload_costs(spill_env, node, use);
104 /* TODO cache costs? */
110 * spills a node by placing a reload before each usage
112 static void spill_node(ir_node *node)
114 const ir_edge_t *edge;
116 DBG((dbg, LEVEL_3, "\tspilling %+F\n", node));
118 foreach_out_edge(node, edge) {
119 ir_node *use = get_edge_src_irn(edge);
126 int in = get_edge_src_pos(edge);
127 ir_node *block = get_nodes_block(use);
129 be_add_reload_on_edge(spill_env, node, block, in, cls, 1);
131 be_add_reload(spill_env, node, use, cls, 1);
135 bitset_set(spilled_nodes, get_irn_idx(node));
139 * spill @p n nodes from a nodeset. Removes the nodes from the nodeset and
140 * sets the spilled bits in spilled_nodes.
142 static void do_spilling(ir_nodeset_t *live_nodes, ir_node *node)
144 size_t n_live_nodes = ir_nodeset_size(live_nodes);
145 size_t values_defined = 0;
146 size_t free_regs_needed = 0;
147 spill_candidate_t *candidates;
148 ir_nodeset_iterator_t iter;
155 be_foreach_definition(node, cls, value,
159 /* we need registers for the non-live argument values */
160 arity = get_irn_arity(node);
161 for (i = 0; i < arity; ++i) {
162 ir_node *pred = get_irn_n(node, i);
163 if (arch_irn_consider_in_reg_alloc(cls, pred)
164 && !ir_nodeset_contains(live_nodes, pred)) {
169 /* we can reuse all reloaded values for the defined values, but we might
170 need even more registers */
171 if (values_defined > free_regs_needed)
172 free_regs_needed = values_defined;
174 spills_needed = (n_live_nodes + free_regs_needed) - n_regs;
175 if (spills_needed <= 0)
177 DBG((dbg, LEVEL_2, "\tspills needed after %+F: %d\n", node, spills_needed));
179 candidates = ALLOCAN(spill_candidate_t, n_live_nodes);
181 /* construct array with spill candidates and calculate their costs */
183 foreach_ir_nodeset(live_nodes, n, iter) {
184 spill_candidate_t *candidate = & candidates[i];
186 assert(!bitset_is_set(spilled_nodes, get_irn_idx(n)));
189 candidate->costs = get_spill_costs(n);
192 assert(i == n_live_nodes);
194 /* sort spill candidates */
195 qsort(candidates, n_live_nodes, sizeof(candidates[0]),
196 compare_spill_candidates_desc);
198 /* spill cheapest ones */
200 while (spills_needed > 0) {
201 spill_candidate_t *candidate;
205 if (cand_idx >= n_live_nodes) {
206 panic("can't spill enough values for node %+F", node);
210 candidate = &candidates[cand_idx];
211 cand_node = candidate->node;
214 if (arch_irn_is(skip_Proj_const(cand_node), dont_spill))
217 /* make sure the node is not an argument of the instruction */
219 for (i = 0; i < arity; ++i) {
220 ir_node *in = get_irn_n(node, i);
221 if (in == cand_node) {
230 spill_node(cand_node);
231 ir_nodeset_remove(live_nodes, cand_node);
237 * removes all values from the nodeset that are defined by node
239 static void remove_defs(ir_node *node, ir_nodeset_t *nodeset)
242 /* You should better break out of your loop when hitting the first phi
244 assert(!is_Phi(node) && "liveness_transfer produces invalid results for phi nodes");
246 be_foreach_definition(node, cls, value,
247 ir_nodeset_remove(nodeset, value);
251 static void add_uses(ir_node *node, ir_nodeset_t *nodeset)
255 arity = get_irn_arity(node);
256 for (i = 0; i < arity; ++i) {
257 ir_node *op = get_irn_n(node, i);
259 if (arch_irn_consider_in_reg_alloc(cls, op) &&
260 !bitset_is_set(spilled_nodes, get_irn_idx(op))) {
261 ir_nodeset_insert(nodeset, op);
266 static __attribute__((unused))
267 void print_nodeset(ir_nodeset_t *nodeset)
269 ir_nodeset_iterator_t iter;
272 foreach_ir_nodeset(nodeset, node, iter) {
273 ir_fprintf(stderr, "%+F ", node);
275 fprintf(stderr, "\n");
279 * make sure register pressure in a block is always equal or below the number
280 * of available registers
282 static void spill_block(ir_node *block, void *data)
284 ir_nodeset_t live_nodes;
285 ir_nodeset_iterator_t iter;
287 int n_phi_values_spilled;
289 int phi_spills_needed;
292 DBG((dbg, LEVEL_1, "spilling block %+F\n", block));
294 /* construct set of live nodes at end of block */
295 ir_nodeset_init(&live_nodes);
296 be_liveness_end_of_block(lv, cls, block, &live_nodes);
298 /* remove already spilled nodes from liveset */
299 foreach_ir_nodeset(&live_nodes, node, iter) {
300 DBG((dbg, LEVEL_2, "\t%+F is live-end... ", node));
301 if (bitset_is_set(spilled_nodes, get_irn_idx(node))) {
302 DBG((dbg, LEVEL_2, "but spilled; removing.\n"));
303 ir_nodeset_remove_iterator(&live_nodes, &iter);
305 DBG((dbg, LEVEL_2, "keeping.\n"));
309 /* walk schedule backwards and spill until register pressure is fine at
311 sched_foreach_reverse(block, node) {
315 remove_defs(node, &live_nodes);
316 do_spilling(&live_nodes, node);
317 add_uses(node, &live_nodes);
320 /* until now only the values of some phis have been spilled the phis itself
321 * are still there and occupy registers, so we need to count them and might
322 * have to spill some of them.
324 n_phi_values_spilled = 0;
325 sched_foreach(block, node) {
329 if (bitset_is_set(spilled_nodes, get_irn_idx(node))) {
330 ++n_phi_values_spilled;
334 /* calculate how many of the phis need to be spilled */
335 regpressure = ir_nodeset_size(&live_nodes) + n_phi_values_spilled;
336 phi_spills_needed = regpressure - n_regs;
337 DBG((dbg, LEVEL_3, "Regpressure before phis: %d phispills: %d\n",
338 regpressure, phi_spills_needed));
340 /* spill as many phis as needed */
341 /* TODO: we should really estimate costs of the phi spill as well...
342 * and preferably spill phis with lower costs... */
343 sched_foreach(block, node) {
346 if (phi_spills_needed <= 0)
349 if (bitset_is_set(spilled_nodes, get_irn_idx(node))) {
350 be_spill_phi(spill_env, node);
354 assert(phi_spills_needed <= 0);
356 ir_nodeset_destroy(&live_nodes);
359 static void be_spill_daemel(ir_graph *irg, const arch_register_class_t *new_cls)
361 n_regs = new_cls->n_regs - be_put_ignore_regs(irg, new_cls, NULL);
366 be_liveness_assure_sets(be_assure_liveness(irg));
368 spill_env = be_new_spill_env(irg);
370 lv = be_get_irg_liveness(irg);
371 spilled_nodes = bitset_malloc(get_irg_last_idx(irg));
373 DBG((dbg, LEVEL_1, "*** RegClass %s\n", cls->name));
375 irg_block_walk_graph(irg, spill_block, NULL, NULL);
377 bitset_free(spilled_nodes);
378 spilled_nodes = NULL;
380 be_insert_spills_reloads(spill_env);
382 be_delete_spill_env(spill_env);
386 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_daemelspill);
387 void be_init_daemelspill(void)
389 static be_spiller_t daemel_spiller = {
393 be_register_spiller("daemel", &daemel_spiller);
394 FIRM_DBG_REGISTER(dbg, "firm.be.spilldaemel");