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