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