bd7a7a5a032d76f67e56f6754c717c1f16533f1f
[libfirm] / ir / be / bespilldaemel.c
1 /*
2  * Copyright (C) 1995-2008 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 static spill_env_t                 *spill_env;
61 static int                          n_regs;
62 static const arch_register_class_t *cls;
63 static const be_lv_t               *lv;
64 static bitset_t                    *spilled_nodes;
65
66 typedef struct spill_candidate_t spill_candidate_t;
67 struct spill_candidate_t {
68         double   costs;
69         ir_node *node;
70 };
71
72 static int compare_spill_candidates_desc(const void *d1, const void *d2)
73 {
74         const spill_candidate_t *c1 = d1;
75         const spill_candidate_t *c2 = d2;
76
77         return (int) (c1->costs - c2->costs);
78 }
79
80 static double get_spill_costs(ir_node *node)
81 {
82         const ir_edge_t *edge;
83         ir_node         *spill_place = skip_Proj(node);
84         double           costs       = be_get_spill_costs(spill_env, node,
85                                                           spill_place);
86
87         foreach_out_edge(node, edge) {
88                 ir_node *use = get_edge_src_irn(edge);
89
90                 /* keeps should be directly below the node */
91                 if(be_is_Keep(use)) {
92                         continue;
93                 }
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         /* TODO cache costs? */
106
107         return costs;
108 }
109
110 /**
111  * spills a node by placing a reload before each usage
112  */
113 static void spill_node(ir_node *node)
114 {
115         const ir_edge_t *edge;
116
117         DBG((dbg, LEVEL_3, "\tspilling %+F\n", node));
118
119         foreach_out_edge(node, edge) {
120                 ir_node *use = get_edge_src_irn(edge);
121                 if(is_Anchor(use))
122                         continue;
123                 if(be_is_Keep(use))
124                         continue;
125
126                 if(is_Phi(use)) {
127                         int      in         = get_edge_src_pos(edge);
128                         ir_node *block      = get_nodes_block(use);
129
130                         be_add_reload_on_edge(spill_env, node, block, in, cls, 1);
131                 } else {
132                         be_add_reload(spill_env, node, use, cls, 1);
133                 }
134         }
135
136         bitset_set(spilled_nodes, get_irn_idx(node));
137 }
138
139 /**
140  * spill @p n nodes from a nodeset. Removes the nodes from the nodeset and
141  * sets the spilled bits in spilled_nodes.
142  */
143 static void do_spilling(ir_nodeset_t *live_nodes, ir_node *node)
144 {
145         size_t                 n_live_nodes     = ir_nodeset_size(live_nodes);
146         size_t                 values_defined   = 0;
147         size_t                 free_regs_needed = 0;
148         spill_candidate_t     *candidates;
149         ir_nodeset_iterator_t  iter;
150         size_t                 i, arity;
151         int                    spills_needed;
152         size_t                 cand_idx;
153         ir_node               *n;
154
155         /* mode_T nodes define several values at once. Count them */
156         if(get_irn_mode(node) == mode_T) {
157                 const ir_edge_t *edge;
158
159                 foreach_out_edge(node, edge) {
160                         const ir_node *proj = get_edge_src_irn(edge);
161
162                         if (arch_irn_consider_in_reg_alloc(cls, proj)) {
163                                 ++values_defined;
164                         }
165                 }
166         } else if (arch_irn_consider_in_reg_alloc(cls, node)) {
167                 ++values_defined;
168         }
169
170         /* we need registers for the non-live argument values */
171         arity = get_irn_arity(node);
172         for(i = 0; i < arity; ++i) {
173                 ir_node *pred = get_irn_n(node, i);
174                 if (arch_irn_consider_in_reg_alloc(cls, pred)
175                                 && !ir_nodeset_contains(live_nodes, pred)) {
176                         ++free_regs_needed;
177                 }
178         }
179
180         /* we can reuse all reloaded values for the defined values, but we might
181            need even more registers */
182         if(values_defined > free_regs_needed)
183                 free_regs_needed = values_defined;
184
185         spills_needed = (n_live_nodes + free_regs_needed) - n_regs;
186         if(spills_needed <= 0)
187                 return;
188         DBG((dbg, LEVEL_2, "\tspills needed after %+F: %d\n", node, spills_needed));
189
190         candidates = alloca(n_live_nodes * sizeof(candidates[0]));
191
192         /* construct array with spill candidates and calculate their costs */
193         i = 0;
194         foreach_ir_nodeset(live_nodes, n, iter) {
195                 spill_candidate_t *candidate = & candidates[i];
196
197                 assert(!bitset_is_set(spilled_nodes, get_irn_idx(n)));
198
199                 candidate->node  = n;
200                 candidate->costs = get_spill_costs(n);
201                 ++i;
202         }
203         assert(i == n_live_nodes);
204
205         /* sort spill candidates */
206         qsort(candidates, n_live_nodes, sizeof(candidates[0]),
207               compare_spill_candidates_desc);
208
209         /* spill cheapest ones */
210         cand_idx = 0;
211         while(spills_needed > 0) {
212                 spill_candidate_t *candidate;
213                 ir_node           *cand_node;
214                 int               is_use;
215
216                 if (cand_idx >= n_live_nodes) {
217                         panic("can't spill enough values for node %+F", node);
218                 }
219
220
221                 candidate = &candidates[cand_idx];
222                 cand_node = candidate->node;
223                 ++cand_idx;
224
225                 if (arch_irn_is(cand_node, dont_spill))
226                         continue;
227
228                 /* make sure the node is not an argument of the instruction */
229                 is_use = 0;
230                 for (i = 0; i < arity; ++i) {
231                         ir_node *in = get_irn_n(node, i);
232                         if(in == cand_node) {
233                                 is_use = 1;
234                                 break;
235                         }
236                 }
237                 if(is_use) {
238                         continue;
239                 }
240
241                 spill_node(cand_node);
242                 ir_nodeset_remove(live_nodes, cand_node);
243                 --spills_needed;
244         }
245 }
246
247 /**
248  * removes all values from the nodeset that are defined by node
249  */
250 static void remove_defs(ir_node *node, ir_nodeset_t *nodeset)
251 {
252         /* You should better break out of your loop when hitting the first phi
253          * function. */
254         assert(!is_Phi(node) && "liveness_transfer produces invalid results for phi nodes");
255
256         if (get_irn_mode(node) == mode_T) {
257                 const ir_edge_t *edge;
258
259                 foreach_out_edge(node, edge) {
260                         const ir_node *proj = get_edge_src_irn(edge);
261
262                         if (arch_irn_consider_in_reg_alloc(cls, proj)) {
263                                 ir_nodeset_remove(nodeset, proj);
264                         }
265                 }
266         }
267
268         if (arch_irn_consider_in_reg_alloc(cls, node)) {
269                 ir_nodeset_remove(nodeset, node);
270         }
271 }
272
273 static void add_uses(ir_node *node, ir_nodeset_t *nodeset)
274 {
275         int i, arity;
276
277         arity = get_irn_arity(node);
278         for(i = 0; i < arity; ++i) {
279                 ir_node *op = get_irn_n(node, i);
280
281                 if (arch_irn_consider_in_reg_alloc(cls, op) &&
282                                 !bitset_is_set(spilled_nodes, get_irn_idx(op))) {
283                         ir_nodeset_insert(nodeset, op);
284                 }
285         }
286 }
287
288 static __attribute__((unused))
289 void print_nodeset(ir_nodeset_t *nodeset)
290 {
291         ir_nodeset_iterator_t  iter;
292         ir_node               *node;
293
294         foreach_ir_nodeset(nodeset, node, iter) {
295                 ir_fprintf(stderr, "%+F ", node);
296         }
297         fprintf(stderr, "\n");
298 }
299
300 /**
301  * make sure register pressure in a block is always equal or below the number
302  * of available registers
303  */
304 static void spill_block(ir_node *block, void *data)
305 {
306         ir_nodeset_t           live_nodes;
307         ir_nodeset_iterator_t  iter;
308         ir_node               *node;
309         int                    n_phi_values_spilled;
310         int                    regpressure;
311         int                    phi_spills_needed;
312         (void) data;
313
314         DBG((dbg, LEVEL_1, "spilling block %+F\n", block));
315
316         /* construct set of live nodes at end of block */
317         ir_nodeset_init(&live_nodes);
318         be_liveness_end_of_block(lv, cls, block, &live_nodes);
319
320         /* remove already spilled nodes from liveset */
321         foreach_ir_nodeset(&live_nodes, node, iter) {
322                 DBG((dbg, LEVEL_2, "\t%+F is live-end... ", node));
323                 if(bitset_is_set(spilled_nodes, get_irn_idx(node))) {
324                         DBG((dbg, LEVEL_2, "but spilled; removing.\n"));
325                         ir_nodeset_remove_iterator(&live_nodes, &iter);
326                 } else {
327                         DBG((dbg, LEVEL_2, "keeping.\n"));
328                 }
329         }
330
331         /* walk schedule backwards and spill until register pressure is fine at
332          * each node */
333         sched_foreach_reverse(block, node) {
334                 if(is_Phi(node))
335                         break;
336
337                 remove_defs(node, &live_nodes);
338                 do_spilling(&live_nodes, node);
339                 add_uses(node, &live_nodes);
340         }
341
342         /* until now only the values of some phis have been spilled the phis itself
343          * are still there and occupy registers, so we need to count them and might
344          * have to spill some of them.
345          */
346         n_phi_values_spilled = 0;
347         sched_foreach(block, node) {
348                 if(!is_Phi(node))
349                         break;
350
351                 if(bitset_is_set(spilled_nodes, get_irn_idx(node))) {
352                         ++n_phi_values_spilled;
353                 }
354         }
355
356         /* calculate how many of the phis need to be spilled */
357         regpressure       = ir_nodeset_size(&live_nodes) + n_phi_values_spilled;
358         phi_spills_needed = regpressure - n_regs;
359         DBG((dbg, LEVEL_3, "Regpressure before phis: %d phispills: %d\n",
360              regpressure, phi_spills_needed));
361
362         /* spill as many phis as needed */
363         /* TODO: we should really estimate costs of the phi spill as well...
364          * and preferably spill phis with lower costs... */
365         sched_foreach(block, node) {
366                 if(!is_Phi(node))
367                         break;
368                 if(phi_spills_needed <= 0)
369                         break;
370
371                 if(bitset_is_set(spilled_nodes, get_irn_idx(node))) {
372                         be_spill_phi(spill_env, node);
373                         --phi_spills_needed;
374                 }
375         }
376         assert(phi_spills_needed <= 0);
377
378         ir_nodeset_destroy(&live_nodes);
379 }
380
381 void be_spill_daemel(be_irg_t *birg, const arch_register_class_t *new_cls)
382 {
383         ir_graph     *irg    = be_get_birg_irg(birg);
384         n_regs = new_cls->n_regs - be_put_ignore_regs(birg, new_cls, NULL);
385
386         if(n_regs == 0)
387                 return;
388
389         be_liveness_assure_sets(be_assure_liveness(birg));
390
391         spill_env     = be_new_spill_env(birg);
392         cls           = new_cls;
393         lv            = be_get_birg_liveness(birg);
394         spilled_nodes = bitset_malloc(get_irg_last_idx(irg));
395
396         DBG((dbg, LEVEL_1, "*** RegClass %s\n", cls->name));
397
398         irg_block_walk_graph(irg, spill_block, NULL, NULL);
399
400         bitset_free(spilled_nodes);
401         spilled_nodes = NULL;
402
403         be_insert_spills_reloads(spill_env);
404
405         be_delete_spill_env(spill_env);
406         spill_env = NULL;
407 }
408
409 void be_init_daemelspill(void)
410 {
411         static be_spiller_t daemel_spiller = {
412                 be_spill_daemel
413         };
414
415         be_register_spiller("daemel", &daemel_spiller);
416         FIRM_DBG_REGISTER(dbg, "firm.be.spilldaemel");
417 }
418
419 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_daemelspill);