don't reload before anchor node
[libfirm] / ir / be / bespilldaemel.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       Naiv spilling algorithm
23  * @author      Matthias Braun
24  * @date        20.09.2005
25  * @version     $Id: bespillbelady.c 13913 2007-05-18 12:48:56Z matze $
26  * @summary
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.
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         const 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)
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                 if(is_Anchor(use))
123                         continue;
124
125                 if(is_Phi(use)) {
126                         int      in         = get_edge_src_pos(edge);
127                         ir_node *block      = get_nodes_block(use);
128
129                         be_add_reload_on_edge(spill_env, node, block, in, cls, 1);
130                 } else if(!be_is_Keep(use)) {
131                         be_add_reload(spill_env, node, use, cls, 1);
132                 }
133         }
134
135         bitset_set(env->spilled_nodes, get_irn_idx(node));
136 }
137
138 /**
139  * spill @p n nodes from a nodeset. Removes the nodes from the nodeset and
140  * sets the spilled bits in env->spilled_nodes.
141  */
142 static
143 void do_spilling(daemel_env_t *env, ir_nodeset_t *live_nodes, ir_node *node)
144 {
145         size_t                       node_count      = ir_nodeset_size(live_nodes);
146         size_t                       additional_defines = 0;
147         size_t                       reload_values      = 0;
148         int                          registers          = env->n_regs;
149         const arch_env_t            *arch_env           = env->arch_env;
150         const arch_register_class_t *cls                = env->cls;
151         spill_candidate_t           *candidates;
152         ir_nodeset_iterator_t        iter;
153         size_t                       i, arity;
154         int                          spills_needed;
155         size_t                       cand_idx;
156         ir_node                     *n;
157         const bitset_t              *spilled_nodes = env->spilled_nodes;
158
159         /* mode_T nodes define several values at once. Count them */
160         if(get_irn_mode(node) == mode_T) {
161                 const ir_edge_t *edge;
162
163                 foreach_out_edge(node, edge) {
164                         const ir_node *proj = get_edge_src_irn(edge);
165
166                         if(arch_irn_consider_in_reg_alloc(arch_env, cls, proj)) {
167                                 ++additional_defines;
168                         }
169                 }
170         }
171         if(bitset_is_set(spilled_nodes, get_irn_idx(node)))
172                 ++additional_defines;
173
174         /* we need registers for the non-live argument values */
175         arity = get_irn_arity(node);
176         for(i = 0; i < arity; ++i) {
177                 ir_node *pred = get_irn_n(node, i);
178                 if(arch_irn_consider_in_reg_alloc(arch_env, cls, pred)
179                                 && !ir_nodeset_contains(live_nodes, pred)) {
180                         ++reload_values;
181                 }
182         }
183
184         if(reload_values > additional_defines)
185                 additional_defines = reload_values;
186
187         spills_needed = (node_count + additional_defines) - registers;
188         if(spills_needed <= 0)
189                 return;
190         DBG((dbg, LEVEL_2, "\tspills needed after %+F: %d\n", node, spills_needed));
191
192         candidates = xmalloc(node_count * sizeof(candidates[0]));
193
194         /* construct array with spill candidates and calculate their costs */
195         i = 0;
196         foreach_ir_nodeset(live_nodes, n, iter) {
197                 spill_candidate_t *candidate = & candidates[i];
198
199                 assert(!bitset_is_set(spilled_nodes, get_irn_idx(n)));
200
201                 candidate->node  = n;
202                 candidate->costs = get_spill_costs(env, n);
203                 ++i;
204         }
205         assert(i == node_count);
206
207         /* sort spill candidates */
208         qsort(candidates, node_count, sizeof(candidates[0]),
209               compare_spill_candidates_desc);
210
211         /* spill cheapest ones */
212         cand_idx = 0;
213         while(spills_needed > 0) {
214                 spill_candidate_t *candidate;
215                 ir_node           *cand_node;
216                 int               is_use;
217
218                 if (cand_idx >= node_count) {
219                         panic("can't spill enough values for node %+F\n", node);
220                 }
221
222
223                 candidate = &candidates[cand_idx];
224                 cand_node = candidate->node;
225                 ++cand_idx;
226
227                 if(arch_irn_is(arch_env, cand_node, dont_spill))
228                         continue;
229
230                 /* make sure the node is not an argument of the instruction */
231                 is_use = 0;
232                 for (i = 0; i < arity; ++i) {
233                         ir_node *in = get_irn_n(node, i);
234                         if(in == cand_node) {
235                                 is_use = 1;
236                                 break;
237                         }
238                 }
239                 if(is_use) {
240                         continue;
241                 }
242
243                 spill_node(env, cand_node);
244                 ir_nodeset_remove(live_nodes, cand_node);
245                 --spills_needed;
246         }
247
248         free(candidates);
249 }
250
251 /**
252  * similar to be_liveness_transfer.
253  * custom liveness transfer function, that doesn't place already spilled values
254  * into the liveness set
255  */
256 static
257 void liveness_transfer_remove_defs(daemel_env_t *env, ir_node *node,
258                                    ir_nodeset_t *nodeset)
259 {
260         const arch_register_class_t *cls      = env->cls;
261         const arch_env_t            *arch_env = env->arch_env;
262
263         /* You should better break out of your loop when hitting the first phi
264          * function. */
265         assert(!is_Phi(node) && "liveness_transfer produces invalid results for phi nodes");
266
267         if (get_irn_mode(node) == mode_T) {
268                 const ir_edge_t *edge;
269
270                 foreach_out_edge(node, edge) {
271                         const ir_node *proj = get_edge_src_irn(edge);
272
273                         if (arch_irn_consider_in_reg_alloc(arch_env, cls, proj)) {
274                                 ir_nodeset_remove(nodeset, proj);
275                         }
276                 }
277         }
278
279     if(arch_irn_consider_in_reg_alloc(arch_env, cls, node)) {
280         ir_nodeset_remove(nodeset, node);
281     }
282 }
283
284 static void liveness_transfer_add_uses(daemel_env_t *env, ir_node *node,
285                                    ir_nodeset_t *nodeset)
286 {
287         int i, arity;
288         const arch_register_class_t *cls      = env->cls;
289         const arch_env_t            *arch_env = env->arch_env;
290         const bitset_t              *bitset   = env->spilled_nodes;
291
292
293     arity = get_irn_arity(node);
294     for(i = 0; i < arity; ++i) {
295         ir_node *op = get_irn_n(node, i);
296
297         if(arch_irn_consider_in_reg_alloc(arch_env, cls, op)
298                    && !bitset_is_set(bitset, get_irn_idx(op))) {
299             ir_nodeset_insert(nodeset, op);
300                 }
301     }
302 }
303
304 static __attribute__((unused))
305 void print_nodeset(ir_nodeset_t *nodeset)
306 {
307         ir_nodeset_iterator_t  iter;
308         ir_node               *node;
309
310         foreach_ir_nodeset(nodeset, node, iter) {
311                 ir_fprintf(stderr, "%+F ", node);
312         }
313         fprintf(stderr, "\n");
314 }
315
316 /**
317  * make sure register pressure in a block is always equal or below the number
318  * of available registers
319  */
320 static
321 void spill_block(ir_node *block, void *data)
322 {
323         daemel_env_t                *env           = data;
324         const arch_env_t            *arch_env      = env->arch_env;
325         const arch_register_class_t *cls           = env->cls;
326         const be_lv_t               *lv            = env->lv;
327         ir_nodeset_t                 live_nodes;
328         ir_nodeset_iterator_t        iter;
329         ir_node                     *node;
330         bitset_t                    *spilled_nodes = env->spilled_nodes;
331         int                          phi_count, spilled_phis, regpressure, phi_spills_needed;
332
333         DBG((dbg, LEVEL_1, "spilling block %+F\n", block));
334
335         ir_nodeset_init(&live_nodes);
336         be_liveness_end_of_block(lv, arch_env, cls, block, &live_nodes);
337
338         foreach_ir_nodeset(&live_nodes, node, iter) {
339                 DBG((dbg, LEVEL_2, "\t%+F is live-end... ", node));
340                 if(bitset_is_set(spilled_nodes, get_irn_idx(node))) {
341                         DBG((dbg, LEVEL_2, "but spilled; removing.\n"));
342                         ir_nodeset_remove_iterator(&live_nodes, &iter);
343                 } else {
344                         DBG((dbg, LEVEL_2, "keeping.\n"));
345                 }
346         }
347
348         sched_foreach_reverse(block, node) {
349                 if(is_Phi(node))
350                         break;
351
352                 if(be_is_Keep(node)) {
353                         /* remove defs should never do something for keep nodes, but we
354                          * leave it here for consistency */
355                         liveness_transfer_remove_defs(env, node, &live_nodes);
356                         liveness_transfer_add_uses(env, node, &live_nodes);
357                         continue;
358                 }
359
360                 liveness_transfer_remove_defs(env, node, &live_nodes);
361                 do_spilling(env, &live_nodes, node);
362                 liveness_transfer_add_uses(env, node, &live_nodes);
363         }
364
365         phi_count = 0;
366         spilled_phis = 0;
367         sched_foreach(block, node) {
368                 if(!is_Phi(node))
369                         break;
370
371                 ++phi_count;
372                 if(bitset_is_set(spilled_nodes, get_irn_idx(node))) {
373                         ++spilled_phis;
374                 }
375         }
376         regpressure       = ir_nodeset_size(&live_nodes) + spilled_phis;
377         phi_spills_needed = regpressure - env->n_regs;
378         DBG((dbg, LEVEL_3, "Regpressure before phis: %d phispills: %d\n",
379              regpressure, phi_spills_needed));
380         sched_foreach(block, node) {
381                 if(!is_Phi(node))
382                         break;
383                 if(phi_spills_needed <= 0)
384                         break;
385
386                 if(bitset_is_set(spilled_nodes, get_irn_idx(node))) {
387                         be_spill_phi(env->spill_env, node);
388                         --phi_spills_needed;
389                 }
390         }
391         assert(phi_spills_needed <= 0);
392
393         ir_nodeset_destroy(&live_nodes);
394 }
395
396 void be_spill_daemel(be_irg_t *birg, const arch_register_class_t *cls)
397 {
398         daemel_env_t  env;
399         ir_graph     *irg    = be_get_birg_irg(birg);
400         int           n_regs = cls->n_regs - be_put_ignore_regs(birg, cls, NULL);
401
402         if(n_regs == 0)
403                 return;
404
405         be_liveness_assure_sets(be_assure_liveness(birg));
406
407         env.spill_env     = be_new_spill_env(birg);
408         env.n_regs        = n_regs;
409         env.arch_env      = be_get_birg_arch_env(birg);
410         env.cls           = cls;
411         env.lv            = be_get_birg_liveness(birg);
412         env.spilled_nodes = bitset_malloc(get_irg_last_idx(irg));
413
414         DBG((dbg, LEVEL_1, "*** RegClass %s\n", cls->name));
415
416         irg_block_walk_graph(irg, spill_block, NULL, &env);
417
418         bitset_free(env.spilled_nodes);
419
420         be_insert_spills_reloads(env.spill_env);
421
422         be_delete_spill_env(env.spill_env);
423 }
424
425 void be_init_daemelspill(void)
426 {
427         static be_spiller_t daemel_spiller = {
428                 be_spill_daemel
429         };
430
431         be_register_spiller("daemel", &daemel_spiller);
432         FIRM_DBG_REGISTER(dbg, "ir.be.spilldaemel");
433 }
434
435 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_doedelspill);