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