2 * Copyright (C) 1995-2007 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 Naiv spilling algorithm
23 * @author Matthias Braun
25 * @version $Id: bespillbelady.c 13913 2007-05-18 12:48:56Z matze $
27 * This implements a naiv spilling algorithm. It is design 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.
42 #include "irnodeset.h"
45 #include "iredges_t.h"
49 #include "bespilloptions.h"
58 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
60 typedef struct daemel_env_t daemel_env_t;
62 spill_env_t *spill_env;
64 const arch_env_t *arch_env;
65 const arch_register_class_t *cls;
67 bitset_t *spilled_nodes;
70 typedef struct spill_candidate_t spill_candidate_t;
71 struct spill_candidate_t {
77 int compare_spill_candidates_desc(const void *d1, const void *d2)
79 const spill_candidate_t *c1 = d1;
80 const spill_candidate_t *c2 = d2;
82 return (int) (c1->costs - c2->costs);
86 double get_spill_costs(daemel_env_t *env, ir_node *node)
88 const ir_edge_t *edge;
89 spill_env_t *spill_env = env->spill_env;
90 double costs = be_get_spill_costs(spill_env, node, node);
92 foreach_out_edge(node, edge) {
93 ir_node *use = get_edge_src_irn(edge);
96 int in = get_edge_src_pos(edge);
97 ir_node *block = get_nodes_block(use);
99 costs += be_get_reload_costs_on_edge(spill_env, node, block, in);
101 costs += be_get_reload_costs(spill_env, node, use);
109 * spills a node by placing a reload before each usage
112 void spill_node(daemel_env_t *env, ir_node *node)
114 const ir_edge_t *edge;
115 spill_env_t *spill_env = env->spill_env;
116 const arch_register_class_t *cls = env->cls;
118 DBG((dbg, LEVEL_3, "\tspilling %+F\n", node));
120 foreach_out_edge(node, edge) {
121 ir_node *use = get_edge_src_irn(edge);
124 int in = get_edge_src_pos(edge);
125 ir_node *block = get_nodes_block(use);
127 be_add_reload_on_edge(spill_env, node, block, in, cls, 1);
128 } else if(!be_is_Keep(use)) {
129 be_add_reload(spill_env, node, use, cls, 1);
133 bitset_set(env->spilled_nodes, get_irn_idx(node));
137 * spill @p n nodes from a nodeset. Removes the nodes from the nodeset and
138 * sets the spilled bits in env->spilled_nodes.
141 void do_spilling(daemel_env_t *env, ir_nodeset_t *live_nodes, ir_node *node)
143 size_t node_count = ir_nodeset_size(live_nodes);
144 size_t additional_defines = 0;
145 size_t reload_values = 0;
146 int registers = env->n_regs;
147 const arch_env_t *arch_env = env->arch_env;
148 const arch_register_class_t *cls = env->cls;
149 spill_candidate_t *candidates;
150 ir_nodeset_iterator_t iter;
155 const bitset_t *spilled_nodes = env->spilled_nodes;
157 /* mode_T nodes define several values at once. Count them */
158 if(get_irn_mode(node) == mode_T) {
159 const ir_edge_t *edge;
161 foreach_out_edge(node, edge) {
162 const ir_node *proj = get_edge_src_irn(edge);
164 if(arch_irn_consider_in_reg_alloc(arch_env, cls, proj)) {
165 ++additional_defines;
169 if(bitset_is_set(spilled_nodes, get_irn_idx(node)))
170 ++additional_defines;
172 /* we need registers for the non-live argument values */
173 arity = get_irn_arity(node);
174 for(i = 0; i < arity; ++i) {
175 ir_node *pred = get_irn_n(node, i);
176 if(arch_irn_consider_in_reg_alloc(arch_env, cls, pred)
177 && !ir_nodeset_contains(live_nodes, pred)) {
182 if(reload_values > additional_defines)
183 additional_defines = reload_values;
185 spills_needed = (node_count + additional_defines) - registers;
186 if(spills_needed <= 0)
188 DBG((dbg, LEVEL_2, "\tspills needed after %+F: %d\n", node, spills_needed));
190 candidates = xmalloc(node_count * sizeof(candidates[0]));
192 /* construct array with spill candidates and calculate their costs */
194 foreach_ir_nodeset(live_nodes, n, iter) {
195 spill_candidate_t *candidate = & candidates[i];
197 assert(!bitset_is_set(spilled_nodes, get_irn_idx(n)));
200 candidate->costs = get_spill_costs(env, n);
203 assert(i == node_count);
205 /* sort spill candidates */
206 qsort(candidates, node_count, sizeof(candidates[0]),
207 compare_spill_candidates_desc);
209 /* spill cheapest ones */
211 while(spills_needed > 0) {
212 spill_candidate_t *candidate;
216 if (cand_idx >= node_count) {
217 panic("can't spill enough values for node %+F\n", node);
221 candidate = &candidates[cand_idx];
222 cand_node = candidate->node;
225 if(arch_irn_is(arch_env, cand_node, dont_spill))
228 /* make sure the node is not an argument of the instruction */
230 for (i = 0; i < arity; ++i) {
231 ir_node *in = get_irn_n(node, i);
232 if(in == cand_node) {
241 spill_node(env, cand_node);
242 ir_nodeset_remove(live_nodes, cand_node);
250 * similar to be_liveness_transfer.
251 * custom liveness transfer function, that doesn't place already spilled values
252 * into the liveness set
255 void liveness_transfer_remove_defs(daemel_env_t *env, ir_node *node,
256 ir_nodeset_t *nodeset)
258 const arch_register_class_t *cls = env->cls;
259 const arch_env_t *arch_env = env->arch_env;
261 /* You should better break out of your loop when hitting the first phi
263 assert(!is_Phi(node) && "liveness_transfer produces invalid results for phi nodes");
265 if (get_irn_mode(node) == mode_T) {
266 const ir_edge_t *edge;
268 foreach_out_edge(node, edge) {
269 const ir_node *proj = get_edge_src_irn(edge);
271 if (arch_irn_consider_in_reg_alloc(arch_env, cls, proj)) {
272 ir_nodeset_remove(nodeset, proj);
277 if(arch_irn_consider_in_reg_alloc(arch_env, cls, node)) {
278 ir_nodeset_remove(nodeset, node);
282 static void liveness_transfer_add_uses(daemel_env_t *env, ir_node *node,
283 ir_nodeset_t *nodeset)
286 const arch_register_class_t *cls = env->cls;
287 const arch_env_t *arch_env = env->arch_env;
288 const bitset_t *bitset = env->spilled_nodes;
291 arity = get_irn_arity(node);
292 for(i = 0; i < arity; ++i) {
293 ir_node *op = get_irn_n(node, i);
295 if(arch_irn_consider_in_reg_alloc(arch_env, cls, op)
296 && !bitset_is_set(bitset, get_irn_idx(op))) {
297 ir_nodeset_insert(nodeset, op);
302 static __attribute__((unused))
303 void print_nodeset(ir_nodeset_t *nodeset)
305 ir_nodeset_iterator_t iter;
308 foreach_ir_nodeset(nodeset, node, iter) {
309 ir_fprintf(stderr, "%+F ", node);
311 fprintf(stderr, "\n");
315 * make sure register pressure in a block is always equal or below the number
316 * of available registers
319 void spill_block(ir_node *block, void *data)
321 daemel_env_t *env = data;
322 const arch_env_t *arch_env = env->arch_env;
323 const arch_register_class_t *cls = env->cls;
324 const be_lv_t *lv = env->lv;
325 ir_nodeset_t live_nodes;
326 ir_nodeset_iterator_t iter;
328 bitset_t *spilled_nodes = env->spilled_nodes;
329 int phi_count, spilled_phis, regpressure, phi_spills_needed;
331 DBG((dbg, LEVEL_1, "spilling block %+F\n", block));
333 ir_nodeset_init(&live_nodes);
334 be_liveness_end_of_block(lv, arch_env, cls, block, &live_nodes);
336 foreach_ir_nodeset(&live_nodes, node, iter) {
337 DBG((dbg, LEVEL_2, "\t%+F is live-end... ", node));
338 if(bitset_is_set(spilled_nodes, get_irn_idx(node))) {
339 DBG((dbg, LEVEL_2, "but spilled; removing.\n"));
340 ir_nodeset_remove_iterator(&live_nodes, &iter);
342 DBG((dbg, LEVEL_2, "keeping.\n"));
346 sched_foreach_reverse(block, node) {
350 if(be_is_Keep(node)) {
351 /* remove defs should never do something for keep nodes, but we
352 * leave it here for consistency */
353 liveness_transfer_remove_defs(env, node, &live_nodes);
354 liveness_transfer_add_uses(env, node, &live_nodes);
358 liveness_transfer_remove_defs(env, node, &live_nodes);
359 do_spilling(env, &live_nodes, node);
360 liveness_transfer_add_uses(env, node, &live_nodes);
365 sched_foreach(block, node) {
370 if(bitset_is_set(spilled_nodes, get_irn_idx(node))) {
374 regpressure = ir_nodeset_size(&live_nodes) + spilled_phis;
375 phi_spills_needed = regpressure - env->n_regs;
376 DBG((dbg, LEVEL_3, "Regpressure before phis: %d phispills: %d\n",
377 regpressure, phi_spills_needed));
378 sched_foreach(block, node) {
381 if(phi_spills_needed <= 0)
384 if(bitset_is_set(spilled_nodes, get_irn_idx(node))) {
385 be_spill_phi(env->spill_env, node);
389 assert(phi_spills_needed <= 0);
391 ir_nodeset_destroy(&live_nodes);
394 void be_spill_daemel(be_irg_t *birg, const arch_register_class_t *cls)
397 ir_graph *irg = be_get_birg_irg(birg);
398 int n_regs = cls->n_regs - be_put_ignore_regs(birg, cls, NULL);
403 be_liveness_assure_sets(be_assure_liveness(birg));
405 env.spill_env = be_new_spill_env(birg);
407 env.arch_env = be_get_birg_arch_env(birg);
409 env.lv = be_get_birg_liveness(birg);
410 env.spilled_nodes = bitset_malloc(get_irg_last_idx(irg));
412 DBG((dbg, LEVEL_1, "*** RegClass %s\n", cls->name));
414 irg_block_walk_graph(irg, spill_block, NULL, &env);
416 bitset_free(env.spilled_nodes);
418 be_insert_spills_reloads(env.spill_env);
420 be_delete_spill_env(env.spill_env);
423 void be_init_daemelspill(void)
425 static be_spiller_t daemel_spiller = {
429 be_register_spiller("daemel", &daemel_spiller);
430 FIRM_DBG_REGISTER(dbg, "ir.be.spilldaemel");
433 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_doedelspill);