bearch: Disallow passing Projs to get_irn_ops().
[libfirm] / ir / be / bespilldaemel.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief       Naive spilling algorithm
9  * @author      Matthias Braun
10  * @date        20.09.2005
11  * @brief
12  *   This implements a naive spilling algorithm. It is designed to produce
13  *   similar effects to the spill decisions produced by traditional graph
14  *   coloring register allocators that spill while they are coloring the graph.
15  *
16  *   This spiller walks over all blocks and looks for places with too high
17  *   register pressure where it spills the values that are cheapest to spill.
18  *   Spilling in this context means placing a spill instruction behind the
19  *   definition of the value and a reload before each usage.
20  */
21 #include "config.h"
22
23 #include "debug.h"
24
25 #include "irnodeset.h"
26 #include "irgwalk.h"
27 #include "irprintf.h"
28 #include "iredges_t.h"
29 #include "error.h"
30
31 #include "beirg.h"
32 #include "bespill.h"
33 #include "bespillutil.h"
34 #include "bemodule.h"
35 #include "besched.h"
36 #include "bearch.h"
37 #include "be_t.h"
38 #include "benode.h"
39 #include "belive.h"
40
41 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
42
43 static spill_env_t                 *spill_env;
44 static unsigned                     n_regs;
45 static const arch_register_class_t *cls;
46 static const be_lv_t               *lv;
47 static bitset_t                    *spilled_nodes;
48
49 typedef struct spill_candidate_t spill_candidate_t;
50 struct spill_candidate_t {
51         double   costs;
52         ir_node *node;
53 };
54
55 static int compare_spill_candidates_desc(const void *d1, const void *d2)
56 {
57         const spill_candidate_t *c1 = (const spill_candidate_t*)d1;
58         const spill_candidate_t *c2 = (const spill_candidate_t*)d2;
59
60         return (int) (c1->costs - c2->costs);
61 }
62
63 static double get_spill_costs(ir_node *node)
64 {
65         ir_node *spill_place = skip_Proj(node);
66         double   costs       = be_get_spill_costs(spill_env, node, spill_place);
67
68         foreach_out_edge(node, edge) {
69                 ir_node *use = get_edge_src_irn(edge);
70
71                 /* keeps should be directly below the node */
72                 if (be_is_Keep(use)) {
73                         continue;
74                 }
75
76                 if (is_Phi(use)) {
77                         int      in    = get_edge_src_pos(edge);
78                         ir_node *block = get_nodes_block(use);
79
80                         costs += be_get_reload_costs_on_edge(spill_env, node, block, in);
81                 } else {
82                         costs += be_get_reload_costs(spill_env, node, use);
83                 }
84         }
85
86         return costs;
87 }
88
89 /**
90  * spills a node by placing a reload before each usage
91  */
92 static void spill_node(ir_node *node)
93 {
94         DBG((dbg, LEVEL_3, "\tspilling %+F\n", node));
95
96         foreach_out_edge(node, edge) {
97                 ir_node *use = get_edge_src_irn(edge);
98                 if (is_Anchor(use))
99                         continue;
100                 if (be_is_Keep(use))
101                         continue;
102
103                 if (is_Phi(use)) {
104                         int      in    = get_edge_src_pos(edge);
105                         ir_node *block = get_nodes_block(use);
106
107                         be_add_reload_on_edge(spill_env, node, block, in, cls, 1);
108                 } else {
109                         be_add_reload(spill_env, node, use, cls, 1);
110                 }
111         }
112
113         bitset_set(spilled_nodes, get_irn_idx(node));
114 }
115
116 static unsigned get_value_width(const ir_node *node)
117 {
118         const arch_register_req_t *req = arch_get_irn_register_req(node);
119         return req->width;
120 }
121
122 /**
123  * spill @p n nodes from a nodeset. Removes the nodes from the nodeset and
124  * sets the spilled bits in spilled_nodes.
125  */
126 static void do_spilling(ir_nodeset_t *live_nodes, ir_node *node)
127 {
128         size_t values_defined = 0;
129         be_foreach_definition(node, cls, value, req,
130                 (void)value;
131                 assert(req->width >= 1);
132                 values_defined += req->width;
133         );
134
135         /* we need registers for the non-live argument values */
136         size_t free_regs_needed = 0;
137         be_foreach_use(node, cls, in_req_, use, pred_req_,
138                 if (!ir_nodeset_contains(live_nodes, use)) {
139                         free_regs_needed += get_value_width(use);
140                 }
141         );
142
143         /* we can reuse all reloaded values for the defined values, but we might
144          * need even more registers */
145         if (values_defined > free_regs_needed)
146                 free_regs_needed = values_defined;
147
148         size_t n_live_nodes  = ir_nodeset_size(live_nodes);
149         int    spills_needed = (n_live_nodes + free_regs_needed) - n_regs;
150         if (spills_needed <= 0)
151                 return;
152         DBG((dbg, LEVEL_2, "\tspills needed after %+F: %d\n", node, spills_needed));
153
154         spill_candidate_t *candidates = ALLOCAN(spill_candidate_t, n_live_nodes);
155
156         /* construct array with spill candidates and calculate their costs */
157         size_t c = 0;
158         foreach_ir_nodeset(live_nodes, n, iter) {
159                 spill_candidate_t *candidate = & candidates[c];
160
161                 assert(!bitset_is_set(spilled_nodes, get_irn_idx(n)));
162
163                 candidate->node  = n;
164                 candidate->costs = get_spill_costs(n);
165                 ++c;
166         }
167         assert(c == n_live_nodes);
168
169         /* sort spill candidates */
170         qsort(candidates, n_live_nodes, sizeof(candidates[0]),
171               compare_spill_candidates_desc);
172
173         /* spill cheapest ones */
174         size_t cand_idx = 0;
175         while (spills_needed > 0) {
176                 if (cand_idx >= n_live_nodes) {
177                         panic("can't spill enough values for node %+F", node);
178                 }
179
180                 spill_candidate_t *candidate = &candidates[cand_idx];
181                 ir_node           *cand_node = candidate->node;
182                 ++cand_idx;
183
184                 if (arch_irn_is(skip_Proj_const(cand_node), dont_spill))
185                         continue;
186
187                 /* make sure the node is not an argument of the instruction */
188                 bool is_use = false;
189                 int arity = get_irn_arity(node);
190                 for (int i = 0; i < arity; ++i) {
191                         ir_node *in = get_irn_n(node, i);
192                         if (in == cand_node) {
193                                 is_use = true;
194                                 break;
195                         }
196                 }
197                 if (is_use)
198                         continue;
199
200                 spill_node(cand_node);
201                 ir_nodeset_remove(live_nodes, cand_node);
202                 spills_needed -= get_value_width(cand_node);
203         }
204 }
205
206 /**
207  * removes all values from the nodeset that are defined by node
208  */
209 static void remove_defs(ir_node *node, ir_nodeset_t *nodeset)
210 {
211         /* You must break out of your loop when hitting the first phi function. */
212         assert(!is_Phi(node));
213
214         be_foreach_definition(node, cls, value, req,
215                 ir_nodeset_remove(nodeset, value);
216         );
217 }
218
219 static void add_uses(ir_node *node, ir_nodeset_t *nodeset)
220 {
221         int arity = get_irn_arity(node);
222         for (int i = 0; i < arity; ++i) {
223                 ir_node *op = get_irn_n(node, i);
224
225                 if (arch_irn_consider_in_reg_alloc(cls, op) &&
226                                 !bitset_is_set(spilled_nodes, get_irn_idx(op))) {
227                         ir_nodeset_insert(nodeset, op);
228                 }
229         }
230 }
231
232 static __attribute__((unused))
233 void print_nodeset(ir_nodeset_t *nodeset)
234 {
235         foreach_ir_nodeset(nodeset, node, iter) {
236                 ir_fprintf(stderr, "%+F ", node);
237         }
238         fprintf(stderr, "\n");
239 }
240
241 /**
242  * make sure register pressure in a block is always equal or below the number
243  * of available registers
244  */
245 static void spill_block(ir_node *block, void *data)
246 {
247         (void) data;
248         DBG((dbg, LEVEL_1, "spilling block %+F\n", block));
249
250         /* construct set of live nodes at end of block */
251         ir_nodeset_t live_nodes;
252         ir_nodeset_init(&live_nodes);
253         be_liveness_end_of_block(lv, cls, block, &live_nodes);
254
255         /* remove already spilled nodes from liveset */
256         foreach_ir_nodeset(&live_nodes, node, iter) {
257                 DBG((dbg, LEVEL_2, "\t%+F is live-end... ", node));
258                 if (bitset_is_set(spilled_nodes, get_irn_idx(node))) {
259                         DBG((dbg, LEVEL_2, "but spilled; removing.\n"));
260                         ir_nodeset_remove_iterator(&live_nodes, &iter);
261                 } else {
262                         DBG((dbg, LEVEL_2, "keeping.\n"));
263                 }
264         }
265
266         /* walk schedule backwards and spill until register pressure is fine at
267          * each node */
268         sched_foreach_reverse(block, node) {
269                 if (is_Phi(node))
270                         break;
271
272                 remove_defs(node, &live_nodes);
273                 do_spilling(&live_nodes, node);
274                 add_uses(node, &live_nodes);
275         }
276
277         /* until now only the values of some phis have been spilled the phis itself
278          * are still there and occupy registers, so we need to count them and might
279          * have to spill some of them. */
280         int n_phi_values_spilled = 0;
281         sched_foreach(block, node) {
282                 if (!is_Phi(node))
283                         break;
284
285                 if (bitset_is_set(spilled_nodes, get_irn_idx(node))) {
286                         n_phi_values_spilled += get_value_width(node);
287                 }
288         }
289
290         int live_nodes_pressure = 0;
291         foreach_ir_nodeset(&live_nodes, node, iter) {
292                 live_nodes_pressure += get_value_width(node);
293         }
294
295         /* calculate how many of the phis need to be spilled */
296         int regpressure       = live_nodes_pressure + n_phi_values_spilled;
297         int phi_spills_needed = regpressure - n_regs;
298         DBG((dbg, LEVEL_3, "Regpressure before phis: %d phispills: %d\n",
299              regpressure, phi_spills_needed));
300
301         /* spill as many phis as needed */
302         /* TODO: we should really estimate costs of the phi spill as well...
303          * and preferably spill phis with lower costs... */
304         sched_foreach(block, node) {
305                 if (!is_Phi(node))
306                         break;
307                 if (phi_spills_needed <= 0)
308                         break;
309
310                 if (!bitset_is_set(spilled_nodes, get_irn_idx(node)))
311                         continue;
312
313                 be_spill_phi(spill_env, node);
314                 phi_spills_needed -= get_value_width(node);
315         }
316         assert(phi_spills_needed <= 0);
317
318         ir_nodeset_destroy(&live_nodes);
319 }
320
321 static void be_spill_daemel(ir_graph *irg, const arch_register_class_t *new_cls)
322 {
323         n_regs = be_get_n_allocatable_regs(irg, new_cls);
324         if (n_regs == 0)
325                 return;
326
327         be_assure_live_sets(irg);
328
329         spill_env     = be_new_spill_env(irg);
330         cls           = new_cls;
331         lv            = be_get_irg_liveness(irg);
332         spilled_nodes = bitset_malloc(get_irg_last_idx(irg));
333
334         DBG((dbg, LEVEL_1, "*** RegClass %s\n", cls->name));
335
336         irg_block_walk_graph(irg, spill_block, NULL, NULL);
337
338         bitset_free(spilled_nodes);
339
340         be_insert_spills_reloads(spill_env);
341         be_delete_spill_env(spill_env);
342 }
343
344 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_daemelspill)
345 void be_init_daemelspill(void)
346 {
347         static be_spiller_t daemel_spiller = {
348                 be_spill_daemel
349         };
350
351         be_register_spiller("daemel", &daemel_spiller);
352         FIRM_DBG_REGISTER(dbg, "firm.be.spilldaemel");
353 }