- Spillslot coalescing now collects all nodes that have class spill/reload
[libfirm] / ir / be / bespill.c
1 /*
2  * Author:      Daniel Grund, Sebastian Hack, Matthias Braun
3  * Date:                29.09.2005
4  * Copyright:   (c) Universitaet Karlsruhe
5  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
6  */
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #include <stdlib.h>
12
13 #include "pset.h"
14 #include "irnode_t.h"
15 #include "ircons_t.h"
16 #include "iredges_t.h"
17 #include "ident_t.h"
18 #include "type_t.h"
19 #include "entity_t.h"
20 #include "debug.h"
21 #include "irgwalk.h"
22 #include "array.h"
23 #include "pdeq.h"
24 #include "unionfind.h"
25 #include "execfreq.h"
26
27 #include "belive_t.h"
28 #include "besched_t.h"
29 #include "bespill.h"
30 #include "belive_t.h"
31 #include "benode_t.h"
32 #include "bechordal_t.h"
33 #include "bejavacoal.h"
34
35 // only rematerialise when costs are less than REMAT_COST_LIMIT
36 // TODO determine a good value here...
37 #define REMAT_COST_LIMIT        80
38
39 typedef struct _reloader_t reloader_t;
40
41 struct _reloader_t {
42         reloader_t *next;
43         ir_node *reloader;
44 };
45
46 typedef struct _spill_info_t {
47         ir_node *spilled_node;
48         reloader_t *reloaders;
49
50         ir_node *spill;
51 } spill_info_t;
52
53 struct _spill_env_t {
54         const arch_register_class_t *cls;
55         const arch_env_t *arch_env;
56         const be_chordal_env_t *chordal_env;
57         struct obstack obst;
58         set *spills;                            /**< all spill_info_t's, which must be placed */
59         pset *mem_phis;                         /**< set of all special spilled phis. allocated and freed separately */
60
61         DEBUG_ONLY(firm_dbg_module_t *dbg;)
62 };
63
64 /**
65  * Compare two spill infos.
66  */
67 static int cmp_spillinfo(const void *x, const void *y, size_t size) {
68         const spill_info_t *xx = x;
69         const spill_info_t *yy = y;
70         return xx->spilled_node != yy->spilled_node;
71 }
72
73 /**
74  * Returns spill info for a specific value (the value that is to be spilled)
75  */
76 static spill_info_t *get_spillinfo(const spill_env_t *env, ir_node *value) {
77         spill_info_t info, *res;
78         int hash = HASH_PTR(value);
79
80         info.spilled_node = value;
81         res = set_find(env->spills, &info, sizeof(info), hash);
82
83         if (res == NULL) {
84                 info.reloaders = NULL;
85                 info.spill = NULL;
86                 res = set_insert(env->spills, &info, sizeof(info), hash);
87         }
88
89         return res;
90 }
91
92 DEBUG_ONLY(
93 /* Sets the debug module of a spill environment. */
94 void be_set_spill_env_dbg_module(spill_env_t *env, firm_dbg_module_t *dbg) {
95         env->dbg = dbg;
96 }
97 )
98
99 /* Creates a new spill environment. */
100 spill_env_t *be_new_spill_env(const be_chordal_env_t *chordal_env) {
101         spill_env_t *env        = xmalloc(sizeof(env[0]));
102         env->spills                     = new_set(cmp_spillinfo, 1024);
103         env->cls                        = chordal_env->cls;
104         env->chordal_env        = chordal_env;
105         env->arch_env       = env->chordal_env->birg->main_env->arch_env;
106         env->mem_phis           = pset_new_ptr_default();
107         obstack_init(&env->obst);
108         return env;
109 }
110
111 /* Deletes a spill environment. */
112 void be_delete_spill_env(spill_env_t *env) {
113         del_set(env->spills);
114         del_pset(env->mem_phis);
115         obstack_free(&env->obst, NULL);
116         free(env);
117 }
118
119 /**
120  *  ____  _                  ____      _                 _
121  * |  _ \| | __ _  ___ ___  |  _ \ ___| | ___   __ _  __| |___
122  * | |_) | |/ _` |/ __/ _ \ | |_) / _ \ |/ _ \ / _` |/ _` / __|
123  * |  __/| | (_| | (_|  __/ |  _ <  __/ | (_) | (_| | (_| \__ \
124  * |_|   |_|\__,_|\___\___| |_| \_\___|_|\___/ \__,_|\__,_|___/
125  *
126  */
127
128 void be_add_reload(spill_env_t *env, ir_node *to_spill, ir_node *before) {
129         spill_info_t *info;
130         reloader_t *rel;
131
132         assert(sched_is_scheduled(before));
133         assert(arch_irn_consider_in_reg_alloc(env->arch_env, env->cls, to_spill));
134
135         info = get_spillinfo(env, to_spill);
136
137         if(is_Phi(to_spill)) {
138                 int i, arity;
139                 // create spillinfos for the phi arguments
140                 for(i = 0, arity = get_irn_arity(to_spill); i < arity; ++i) {
141                         ir_node *arg = get_irn_n(to_spill, i);
142                         get_spillinfo(env, arg);
143                 }
144         }
145
146         rel           = obstack_alloc(&env->obst, sizeof(rel[0]));
147         rel->reloader = before;
148         rel->next     = info->reloaders;
149         info->reloaders = rel;
150         be_liveness_add_missing(env->chordal_env->lv);
151 }
152
153 void be_add_reload_on_edge(spill_env_t *env, ir_node *to_spill, ir_node *block, int pos) {
154         ir_node *predblock, *last;
155
156         /* simply add the reload to the beginning of the block if we only have 1 predecessor
157          * (we don't need to check for phis as there can't be any in a block with only 1 pred)
158          */
159         if(get_Block_n_cfgpreds(block) == 1) {
160                 assert(!is_Phi(sched_first(block)));
161                 be_add_reload(env, to_spill, sched_first(block));
162                 return;
163         }
164
165         /* We have to reload the value in pred-block */
166         predblock = get_Block_cfgpred_block(block, pos);
167         last = sched_last(predblock);
168
169         /* we might have projs and keepanys behind the jump... */
170         while(is_Proj(last) || be_is_Keep(last)) {
171                 last = sched_prev(last);
172                 assert(!sched_is_end(last));
173         }
174         assert(is_cfop(last));
175
176         // add the reload before the (cond-)jump
177         be_add_reload(env, to_spill, last);
178 }
179
180 void be_spill_phi(spill_env_t *env, ir_node *node) {
181         int i, arity;
182
183         assert(is_Phi(node));
184
185         pset_insert_ptr(env->mem_phis, node);
186
187         // create spillinfos for the phi arguments
188         get_spillinfo(env, node);
189         for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
190                 ir_node *arg = get_irn_n(node, i);
191                 get_spillinfo(env, arg);
192         }
193 }
194
195 /*
196  *   ____                _         ____        _ _ _
197  *  / ___|_ __ ___  __ _| |_ ___  / ___| _ __ (_) | |___
198  * | |   | '__/ _ \/ _` | __/ _ \ \___ \| '_ \| | | / __|
199  * | |___| | |  __/ (_| | ||  __/  ___) | |_) | | | \__ \
200  *  \____|_|  \___|\__,_|\__\___| |____/| .__/|_|_|_|___/
201  *                                      |_|
202  */
203
204 /**
205  * Schedules a node after an instruction. (That is the place after all projs and phis
206  * that are scheduled after the instruction)
207  * This function also skips phi nodes at the beginning of a block
208  */
209 static void sched_add_after_insn(ir_node *sched_after, ir_node *node) {
210         ir_node *next = sched_next(sched_after);
211         while(is_Proj(next) || is_Phi(next)) {
212                 next = sched_next(next);
213         }
214         assert(next != NULL);
215
216         if(sched_is_end(next)) {
217                 sched_add_after(sched_last(get_nodes_block(sched_after)), node);
218         } else {
219                 sched_add_before(next, node);
220         }
221 }
222
223 /**
224  * Creates a spill.
225  *
226  * @param senv      the spill environment
227  * @param irn       the node that should be spilled
228  * @param ctx_irn   an user of the spilled node
229  *
230  * @return a be_Spill node
231  */
232 static void spill_irn(spill_env_t *env, spill_info_t *spillinfo) {
233         ir_node *to_spill = spillinfo->spilled_node;
234
235         DBG((env->dbg, LEVEL_1, "%+F\n", to_spill));
236
237         /* Trying to spill an already spilled value, no need for a new spill
238          * node then, we can simply connect to the same one for this reload
239          *
240          * (although rematerialisation code should handle most of these cases
241          * this can still happen when spilling Phis)
242          */
243         if(be_is_Reload(to_spill)) {
244                 spillinfo->spill = get_irn_n(to_spill, be_pos_Reload_mem);
245                 return;
246         }
247
248         spillinfo->spill = be_spill(env->arch_env, to_spill);
249         sched_add_after_insn(to_spill, spillinfo->spill);
250 }
251
252 static void spill_node(spill_env_t *env, spill_info_t *spillinfo);
253
254 /**
255  * If the first usage of a Phi result would be out of memory
256  * there is no sense in allocating a register for it.
257  * Thus we spill it and all its operands to the same spill slot.
258  * Therefore the phi/dataB becomes a phi/Memory
259  *
260  * @param senv      the spill environment
261  * @param phi       the Phi node that should be spilled
262  * @param ctx_irn   an user of the spilled node
263  */
264 static void spill_phi(spill_env_t *env, spill_info_t *spillinfo) {
265         ir_node *phi = spillinfo->spilled_node;
266         int i;
267         int arity = get_irn_arity(phi);
268         ir_node     *block    = get_nodes_block(phi);
269         ir_node     **ins;
270
271         assert(is_Phi(phi));
272
273         /* build a new PhiM */
274         ins = alloca(sizeof(ir_node*) * arity);
275         for(i = 0; i < arity; ++i) {
276                 ins[i] = get_irg_bad(env->chordal_env->irg);
277         }
278         spillinfo->spill = new_r_Phi(env->chordal_env->irg, block, arity, ins, mode_M);
279
280         for(i = 0; i < arity; ++i) {
281                 ir_node *arg = get_irn_n(phi, i);
282                 spill_info_t *arg_info = get_spillinfo(env, arg);
283
284                 spill_node(env, arg_info);
285
286                 set_irn_n(spillinfo->spill, i, arg_info->spill);
287         }
288 }
289
290 /**
291  * Spill a node.
292  *
293  * @param senv      the spill environment
294  * @param to_spill  the node that should be spilled
295  */
296 static void spill_node(spill_env_t *env, spill_info_t *spillinfo) {
297         ir_node *to_spill;
298
299         // the node should be tagged for spilling already...
300         if(spillinfo->spill != NULL)
301                 return;
302
303         to_spill = spillinfo->spilled_node;
304         if (is_Phi(to_spill) && pset_find_ptr(env->mem_phis, spillinfo->spilled_node)) {
305                 spill_phi(env, spillinfo);
306         } else {
307                 spill_irn(env, spillinfo);
308         }
309 }
310
311 /*
312  *
313  *  ____                      _            _       _ _
314  * |  _ \ ___ _ __ ___   __ _| |_ ___ _ __(_) __ _| (_)_______
315  * | |_) / _ \ '_ ` _ \ / _` | __/ _ \ '__| |/ _` | | |_  / _ \
316  * |  _ <  __/ | | | | | (_| | ||  __/ |  | | (_| | | |/ /  __/
317  * |_| \_\___|_| |_| |_|\__,_|\__\___|_|  |_|\__,_|_|_/___\___|
318  *
319  */
320
321 /**
322  * Tests whether value @p arg is available before node @p reloader
323  * @returns 1 if value is available, 0 otherwise
324  */
325 static int is_value_available(spill_env_t *env, ir_node *arg, ir_node *reloader) {
326         if(is_Unknown(arg) || arg == new_NoMem())
327                 return 1;
328
329         if(be_is_Spill(arg))
330                 return 1;
331
332         if(arg == get_irg_frame(env->chordal_env->irg))
333                 return 1;
334
335         /* the following test does not work while spilling,
336          * because the liveness info is not adapted yet to the effects of the
337          * additional spills/reloads.
338          *
339          * So we can only do this test for ignore registers (of our register class)
340          */
341         if(arch_get_irn_reg_class(env->arch_env, arg, -1) == env->chordal_env->cls
342            && arch_irn_is(env->arch_env, arg, ignore)) {
343                 int i, arity;
344
345                 /* we want to remat before the insn reloader
346                  * thus an arguments is alive if
347                  *   - it interferes with the reloaders result
348                  *   - or it is (last-) used by reloader itself
349                  */
350                 if (values_interfere(env->chordal_env->lv, reloader, arg)) {
351                         return 1;
352                 }
353
354                 arity = get_irn_arity(reloader);
355                 for (i = 0; i < arity; ++i) {
356                         ir_node *rel_arg = get_irn_n(reloader, i);
357                         if (rel_arg == arg)
358                                 return 1;
359                 }
360         }
361
362         return 0;
363 }
364
365 /**
366  * Checks whether the node can principally be rematerialized
367  */
368 static int is_remat_node(spill_env_t *env, ir_node *node) {
369         const arch_env_t *arch_env = env->arch_env;
370
371         assert(!be_is_Spill(node));
372
373         if(be_is_Reload(node))
374                 return 1;
375
376         // TODO why does arch_irn_is say rematerializable anyway?
377         if(be_is_Barrier(node))
378                 return 0;
379
380         if(arch_irn_is(arch_env, node, rematerializable))
381                 return 1;
382
383         if(be_is_StackParam(node))
384                 return 1;
385
386         return 0;
387 }
388
389 /**
390  * Check if a node is rematerializable. This tests for the following conditions:
391  *
392  * - The node itself is rematerializable
393  * - All arguments of the node are available or also rematerialisable
394  * - The costs for the rematerialisation operation is less or equal a limit
395  *
396  * Returns the costs needed for rematerialisation or something
397  * > REMAT_COST_LIMIT if remat is not possible.
398  */
399 static int check_remat_conditions_costs(spill_env_t *env, ir_node *spilled, ir_node *reloader, int parentcosts) {
400         int i, arity;
401         int argremats;
402         int costs = 0;
403
404         if(!is_remat_node(env, spilled))
405                 return REMAT_COST_LIMIT;
406
407         if(be_is_Reload(spilled)) {
408                 costs += 2;
409         } else {
410                 costs += arch_get_op_estimated_cost(env->arch_env, spilled);
411         }
412         if(parentcosts + costs >= REMAT_COST_LIMIT)
413                 return REMAT_COST_LIMIT;
414
415         argremats = 0;
416         for(i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
417                 ir_node *arg = get_irn_n(spilled, i);
418
419                 if(is_value_available(env, arg, reloader))
420                         continue;
421
422                 // we have to rematerialize the argument as well...
423                 if(argremats >= 1) {
424                         /* we only support rematerializing 1 argument at the moment,
425                          * so that we don't have to care about register pressure
426                          */
427                         return REMAT_COST_LIMIT;
428                 }
429                 argremats++;
430
431                 // TODO can we get more accurate costs than +1?
432                 costs += check_remat_conditions_costs(env, arg, reloader, parentcosts + costs);
433                 if(parentcosts + costs >= REMAT_COST_LIMIT)
434                         return REMAT_COST_LIMIT;
435         }
436
437         return costs;
438 }
439
440 static int check_remat_conditions(spill_env_t *env, ir_node *spilled, ir_node *reloader) {
441         int costs = check_remat_conditions_costs(env, spilled, reloader, 1);
442
443         return costs < REMAT_COST_LIMIT;
444 }
445
446 /**
447  * Re-materialize a node.
448  *
449  * @param senv      the spill environment
450  * @param spilled   the node that was spilled
451  * @param reloader  a irn that requires a reload
452  */
453 static ir_node *do_remat(spill_env_t *env, ir_node *spilled, ir_node *reloader) {
454         int i, arity;
455         ir_node *res;
456         ir_node *bl = get_nodes_block(reloader);
457         ir_node **ins;
458
459         ins = alloca(get_irn_arity(spilled) * sizeof(ins[0]));
460         for(i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
461                 ir_node *arg = get_irn_n(spilled, i);
462
463                 if(is_value_available(env, arg, reloader)) {
464                         ins[i] = arg;
465                 } else {
466                         ins[i] = do_remat(env, arg, reloader);
467                 }
468         }
469
470         /* create a copy of the node */
471         res = new_ir_node(get_irn_dbg_info(spilled), env->chordal_env->irg, bl,
472                 get_irn_op(spilled),
473                 get_irn_mode(spilled),
474                 get_irn_arity(spilled),
475                 ins);
476         copy_node_attr(spilled, res);
477
478         DBG((env->dbg, LEVEL_1, "Insert remat %+F before reloader %+F\n", res, reloader));
479
480         /* insert in schedule */
481         assert(!is_Block(reloader));
482         sched_add_before(reloader, res);
483
484         return res;
485 }
486
487 /*
488  *  ___                     _     ____      _                 _
489  * |_ _|_ __  ___  ___ _ __| |_  |  _ \ ___| | ___   __ _  __| |___
490  *  | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ |/ _ \ / _` |/ _` / __|
491  *  | || | | \__ \  __/ |  | |_  |  _ <  __/ | (_) | (_| | (_| \__ \
492  * |___|_| |_|___/\___|_|   \__| |_| \_\___|_|\___/ \__,_|\__,_|___/
493  *
494  */
495
496 void be_insert_spills_reloads(spill_env_t *env) {
497         const arch_env_t *arch_env = env->arch_env;
498         spill_info_t *si;
499
500         /* process each spilled node */
501         DBG((env->dbg, LEVEL_1, "Insert spills and reloads:\n"));
502         for(si = set_first(env->spills); si; si = set_next(env->spills)) {
503                 reloader_t *rld;
504                 ir_mode *mode = get_irn_mode(si->spilled_node);
505                 pset *values = pset_new_ptr(16);
506
507                 /* go through all reloads for this spill */
508                 for(rld = si->reloaders; rld; rld = rld->next) {
509                         ir_node *new_val;
510
511                         if (check_remat_conditions(env, si->spilled_node, rld->reloader)) {
512                                 new_val = do_remat(env, si->spilled_node, rld->reloader);
513                         } else {
514                                 /* make sure we have a spill */
515                                 spill_node(env, si);
516
517                                 /* do a reload */
518                                 new_val = be_reload(arch_env, env->cls, rld->reloader, mode, si->spill);
519                         }
520
521                         DBG((env->dbg, LEVEL_1, " %+F of %+F before %+F\n", new_val, si->spilled_node, rld->reloader));
522                         pset_insert_ptr(values, new_val);
523                 }
524
525                 if(pset_count(values) > 0) {
526                         /* introduce copies, rewire the uses */
527                         pset_insert_ptr(values, si->spilled_node);
528                         be_ssa_constr_set_ignore(env->chordal_env->dom_front, env->chordal_env->lv, values, env->mem_phis);
529                 }
530
531                 del_pset(values);
532         }
533
534         // reloads are placed now, but we might reuse the spill environment for further spilling decisions
535         del_set(env->spills);
536         env->spills = new_set(cmp_spillinfo, 1024);
537 }