BugFixes:
[libfirm] / ir / be / bespillnaive.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Naive spilling algorithm
23  * @author      Matthias Braun
24  * @date        20.09.2005
25  * @version     $Id$
26  * @summary
27  *   This implements a naive 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.
30  *
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.
35  */
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include "debug.h"
41
42 #include "irnodeset.h"
43 #include "irgwalk.h"
44 #include "irprintf.h"
45 #include "iredges_t.h"
46 #include "error.h"
47
48 #include "beirg.h"
49 #include "bespilloptions.h"
50 #include "bespill.h"
51 #include "bemodule.h"
52 #include "besched.h"
53 #include "bearch_t.h"
54 #include "be_t.h"
55 #include "benode_t.h"
56 #include "beirg.h"
57
58 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
59
60 typedef struct daemel_env_t daemel_env_t;
61 struct daemel_env_t {
62         spill_env_t                 *spill_env;
63         int                          n_regs;
64         const arch_env_t            *arch_env;
65         const arch_register_class_t *cls;
66         be_lv_t               *lv;
67         bitset_t                    *spilled_nodes;
68 };
69
70 typedef struct spill_candidate_t spill_candidate_t;
71 struct spill_candidate_t {
72         double   costs;
73         ir_node *node;
74 };
75
76 static
77 int compare_spill_candidates_desc(const void *d1, const void *d2)
78 {
79         const spill_candidate_t *c1 = d1;
80         const spill_candidate_t *c2 = d2;
81
82         return (int) (c1->costs - c2->costs);
83 }
84
85 static
86 double get_spill_costs(daemel_env_t *env, ir_node *node)
87 {
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);
91
92         foreach_out_edge(node, edge) {
93                 ir_node *use = get_edge_src_irn(edge);
94
95                 if(is_Phi(use)) {
96                         int      in         = get_edge_src_pos(edge);
97                         ir_node *block      = get_nodes_block(use);
98
99                         costs += be_get_reload_costs_on_edge(spill_env, node, block, in);
100                 } else {
101                         costs += be_get_reload_costs(spill_env, node, use);
102                 }
103         }
104
105         return costs;
106 }
107
108 /**
109  * spills a node by placing a reload before each usage
110  */
111 static
112 void spill_node(daemel_env_t *env, ir_node *node, ir_nodeset_t *nodes)
113 {
114         const ir_edge_t *edge;
115         spill_env_t     *spill_env       = env->spill_env;
116         const arch_register_class_t *cls = env->cls;
117
118         DBG((dbg, LEVEL_3, "\tspilling %+F\n", node));
119
120         foreach_out_edge(node, edge) {
121                 ir_node *use = get_edge_src_irn(edge);
122
123                 if(is_Phi(use)) {
124                         int      in         = get_edge_src_pos(edge);
125                         ir_node *block      = get_nodes_block(use);
126
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);
130                 }
131         }
132
133         bitset_set(env->spilled_nodes, get_irn_idx(node));
134         ir_nodeset_remove(nodes, node);
135 }
136
137 /**
138  * spill @p n nodes from a nodeset. Removes the nodes from the nodeset and
139  * sets the spilled bits in env->spilled_nodes.
140  */
141 static
142 void do_spilling(daemel_env_t *env, ir_nodeset_t *nodes, ir_node *node,
143                  ir_node *spill_phis_in)
144 {
145         size_t                       node_count         = ir_nodeset_size(nodes);
146         size_t                       additional_defines = 0;
147         int                          registers          = env->n_regs;
148         const arch_env_t            *arch_env           = env->arch_env;
149         const arch_register_class_t *cls                = env->cls;
150         spill_candidate_t           *candidates;
151         ir_nodeset_iterator_t        iter;
152         size_t                       i, arity;
153         int                          spills_needed;
154         int                          cand_idx;
155         ir_node                     *n;
156
157         /* mode_T nodes define several values at once. Count them */
158         if(get_irn_mode(node) == mode_T) {
159                 ir_node *proj  = sched_next(node);
160
161                 while(is_Proj(proj)) {
162                         if(arch_irn_consider_in_reg_alloc(arch_env, cls, proj)) {
163                                 ++additional_defines;
164                         }
165                         proj = sched_next(proj);
166                 }
167         }
168
169         spills_needed = (node_count + additional_defines) - registers;
170         if(spills_needed <= 0)
171                 return;
172         DBG((dbg, LEVEL_2, "\tspills needed after %+F: %d\n", node, spills_needed));
173
174         candidates = malloc(node_count * sizeof(candidates[0]));
175
176         /* construct array with spill candidates and calculate their costs */
177         i = 0;
178         foreach_ir_nodeset(nodes, n, iter) {
179                 spill_candidate_t *candidate = & candidates[i];
180
181                 candidate->node  = n;
182                 candidate->costs = get_spill_costs(env, n);
183                 ++i;
184         }
185         assert(i == node_count);
186
187         /* sort spill candidates */
188         qsort(candidates, node_count, sizeof(candidates[0]),
189               compare_spill_candidates_desc);
190
191         /* spill cheapest ones */
192         cand_idx = 0;
193         arity    = get_irn_arity(node);
194         while(spills_needed > 0) {
195                 spill_candidate_t *candidate = &candidates[cand_idx];
196                 ir_node           *cand_node = candidate->node;
197                 int                is_use;
198                 ++cand_idx;
199
200                 if(cand_idx >= node_count) {
201                         panic("can't spill enough values for node %+F\n", node);
202                 }
203
204                 /* make sure the node is not a use of the instruction */
205                 is_use = 0;
206                 for(i = 0; i < arity; ++i) {
207                         ir_node *in = get_irn_n(node, i);
208                         if(in == cand_node) {
209                                 is_use = 1;
210                                 break;
211                         }
212                 }
213                 if(is_use) {
214                         continue;
215                 }
216
217                 spill_node(env, cand_node, nodes);
218                 --spills_needed;
219                 if(spill_phis_in != NULL && is_Phi(node) &&
220                    get_nodes_block(node) == spill_phis_in) {
221                         DBG((dbg, LEVEL_3, "\tspilled phi\n"));
222                         be_spill_phi(env->spill_env, node);
223                 }
224         }
225
226         free(candidates);
227 }
228
229 /**
230  * similar to be_liveness_transfer.
231  * custom liveness transfer function, that doesn't place already spilled values
232  * into the liveness set
233  */
234 static
235 void liveness_transfer(daemel_env_t *env, ir_node *node, ir_nodeset_t *nodeset)
236 {
237         int i, arity;
238         const arch_register_class_t *cls      = env->cls;
239         const arch_env_t            *arch_env = env->arch_env;
240         const bitset_t              *bitset   = env->spilled_nodes;
241
242         /* You should better break out of your loop when hitting the first phi
243          * function. */
244         assert(!is_Phi(node) && "liveness_transfer produces invalid results for phi nodes");
245
246     if(arch_irn_consider_in_reg_alloc(arch_env, cls, node)) {
247         ir_nodeset_remove(nodeset, node);
248     }
249
250     arity = get_irn_arity(node);
251     for(i = 0; i < arity; ++i) {
252         ir_node *op = get_irn_n(node, i);
253
254         if(arch_irn_consider_in_reg_alloc(arch_env, cls, op)
255                    && !bitset_is_set(bitset, get_irn_idx(op))) {
256             ir_nodeset_insert(nodeset, op);
257                 }
258     }
259 }
260
261 /**
262  * make sure register pressure in a block is always equal or below the number
263  * of available registers
264  */
265 static
266 void spill_block(ir_node *block, void *data)
267 {
268         daemel_env_t                *env           = data;
269         const arch_env_t            *arch_env      = env->arch_env;
270         const arch_register_class_t *cls           = env->cls;
271         const be_lv_t               *lv            = env->lv;
272         ir_nodeset_t                 live_nodes;
273         ir_nodeset_iterator_t        iter;
274         ir_node                     *node;
275         bitset_t                    *spilled_nodes = env->spilled_nodes;
276
277         DBG((dbg, LEVEL_1, "spilling block %+F\n", block));
278
279         ir_nodeset_init(&live_nodes);
280         be_liveness_end_of_block_ir_nodeset(lv, arch_env, cls, block, &live_nodes);
281
282         foreach_ir_nodeset(&live_nodes, node, iter) {
283                 DBG((dbg, LEVEL_2, "\t%+F is live-in... ", node));
284                 if(bitset_is_set(spilled_nodes, get_irn_idx(node))) {
285                         DBG((dbg, LEVEL_2, "but spilled; removing.\n"));
286                 } else {
287                         DBG((dbg, LEVEL_2, "keeping.\n"));
288                 }
289         }
290
291         sched_foreach_reverse(block, node) {
292                 if(is_Phi(node))
293                         break;
294
295                 if(is_Proj(node) || be_is_Keep(node)) {
296                         liveness_transfer(env, node, &live_nodes);
297                         continue;
298                 }
299
300                 do_spilling(env, &live_nodes, node, NULL);
301
302                 liveness_transfer(env, node, &live_nodes);
303         }
304
305         do_spilling(env, &live_nodes, node, block);
306
307         ir_nodeset_destroy(&live_nodes);
308 }
309
310 void be_spill_daemel(be_irg_t *birg, const arch_register_class_t *cls)
311 {
312         daemel_env_t  env;
313         ir_graph     *irg    = be_get_birg_irg(birg);
314         int           n_regs = cls->n_regs - be_put_ignore_regs(birg, cls, NULL);
315
316         if(n_regs == 0)
317                 return;
318
319         be_liveness_assure_sets(be_assure_liveness(birg));
320
321         env.spill_env     = be_new_spill_env(birg);
322         env.n_regs        = n_regs;
323         env.arch_env      = be_get_birg_arch_env(birg);
324         env.cls           = cls;
325         env.lv            = be_get_birg_liveness(birg);
326         env.spilled_nodes = bitset_malloc(get_irg_last_idx(irg));
327
328         irg_block_walk_graph(irg, spill_block, NULL, &env);
329
330         bitset_free(env.spilled_nodes);
331
332         be_insert_spills_reloads(env.spill_env);
333
334         be_delete_spill_env(env.spill_env);
335 }
336
337 void be_init_daemelspill(void)
338 {
339         static be_spiller_t daemel_spiller = {
340                 be_spill_daemel
341         };
342
343         be_register_spiller("daemel", &daemel_spiller);
344         FIRM_DBG_REGISTER(dbg, "ir.be.spilldaemel");
345 }
346
347 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_doedelspill);