sieve is not needed anymore
[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  * CVS-Id:      $Id$
7  */
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11
12 #include <stdlib.h>
13
14 #include "pset.h"
15 #include "irnode_t.h"
16 #include "ircons_t.h"
17 #include "iredges_t.h"
18 #include "irbackedge_t.h"
19 #include "irprintf.h"
20 #include "ident_t.h"
21 #include "type_t.h"
22 #include "entity_t.h"
23 #include "debug.h"
24 #include "irgwalk.h"
25 #include "array.h"
26 #include "pdeq.h"
27 #include "unionfind.h"
28 #include "execfreq.h"
29
30 #include "belive_t.h"
31 #include "besched_t.h"
32 #include "bespill.h"
33 #include "belive_t.h"
34 #include "benode_t.h"
35 #include "bechordal_t.h"
36 #include "bejavacoal.h"
37 #include "benodesets.h"
38 #include "bespilloptions.h"
39 #include "bestatevent.h"
40
41 // only rematerialise when costs are less than REMAT_COST_LIMIT
42 // TODO determine a good value here...
43 #define REMAT_COST_LIMIT        10
44
45 typedef struct _reloader_t reloader_t;
46
47 struct _reloader_t {
48         reloader_t *next;
49         ir_node    *reloader;
50         ir_node    *rematted_node;
51         int        allow_remat;     /**< the node may be rematted instead of reloaded if global remat option is on */
52 };
53
54 typedef struct _spill_info_t {
55         /** the value that should get spilled */
56         ir_node *spilled_node;
57         /** list of places where the value should get reloaded */
58         reloader_t *reloaders;
59
60         /** the spill node, or a PhiM node */
61         ir_node *spill;
62         /** if we had the value of a phi spilled before but not the phi itself then
63          * this field contains the spill for the phi value */
64         ir_node *old_spill;
65
66         /** the register class in which the reload should be placed */
67         const arch_register_class_t *reload_cls;
68 } spill_info_t;
69
70 struct _spill_env_t {
71         const arch_env_t *arch_env;
72         ir_graph *irg;
73         struct obstack obst;
74         be_irg_t *birg;
75         int spill_cost;     /**< the cost of a single spill node */
76         int reload_cost;    /**< the cost of a reload node */
77         set *spills;        /**< all spill_info_t's, which must be placed */
78         pset *mem_phis;     /**< set of all special spilled phis. allocated and freed separately */
79
80         DEBUG_ONLY(firm_dbg_module_t *dbg;)
81 };
82
83 /**
84  * Compare two spill infos.
85  */
86 static int cmp_spillinfo(const void *x, const void *y, size_t size) {
87         const spill_info_t *xx = x;
88         const spill_info_t *yy = y;
89         return xx->spilled_node != yy->spilled_node;
90 }
91
92 /**
93  * Returns spill info for a specific value (returns NULL if the info doesn't
94  * exist yet)
95  */
96 static spill_info_t *find_spillinfo(const spill_env_t *env, ir_node *value) {
97         spill_info_t info;
98         int hash = nodeset_hash(value);
99
100         info.spilled_node = value;
101
102         return set_find(env->spills, &info, sizeof(info), hash);
103 }
104
105 /**
106  * Returns spill info for a specific value (the value that is to be spilled)
107  */
108 static spill_info_t *get_spillinfo(const spill_env_t *env, ir_node *value) {
109         spill_info_t info, *res;
110         int hash = nodeset_hash(value);
111
112         info.spilled_node = value;
113         res = set_find(env->spills, &info, sizeof(info), hash);
114
115         if (res == NULL) {
116                 info.reloaders = NULL;
117                 info.spill = NULL;
118                 info.old_spill = NULL;
119                 info.reload_cls = NULL;
120                 res = set_insert(env->spills, &info, sizeof(info), hash);
121         }
122
123         return res;
124 }
125
126 DEBUG_ONLY(
127 /* Sets the debug module of a spill environment. */
128 void be_set_spill_env_dbg_module(spill_env_t *env, firm_dbg_module_t *dbg) {
129         env->dbg = dbg;
130 }
131 )
132
133 /* Creates a new spill environment. */
134 spill_env_t *be_new_spill_env(be_irg_t *birg) {
135         spill_env_t *env        = xmalloc(sizeof(env[0]));
136         env->spills                     = new_set(cmp_spillinfo, 1024);
137         env->irg            = be_get_birg_irg(birg);
138         env->birg           = birg;
139         env->arch_env       = birg->main_env->arch_env;
140         env->mem_phis           = pset_new_ptr_default();
141         // TODO, ask backend about costs..., or even ask backend whether we should
142         // rematerialize...
143         env->spill_cost     = 8;
144         env->reload_cost    = 5;
145         obstack_init(&env->obst);
146         return env;
147 }
148
149 /* Deletes a spill environment. */
150 void be_delete_spill_env(spill_env_t *env) {
151         del_set(env->spills);
152         del_pset(env->mem_phis);
153         obstack_free(&env->obst, NULL);
154         free(env);
155 }
156
157 /*
158  *  ____  _                  ____      _                 _
159  * |  _ \| | __ _  ___ ___  |  _ \ ___| | ___   __ _  __| |___
160  * | |_) | |/ _` |/ __/ _ \ | |_) / _ \ |/ _ \ / _` |/ _` / __|
161  * |  __/| | (_| | (_|  __/ |  _ <  __/ | (_) | (_| | (_| \__ \
162  * |_|   |_|\__,_|\___\___| |_| \_\___|_|\___/ \__,_|\__,_|___/
163  *
164  */
165
166 void be_add_remat(spill_env_t *env, ir_node *to_spill, ir_node *before, ir_node *rematted_node) {
167         spill_info_t *spill_info;
168         reloader_t *reloader;
169
170         spill_info = get_spillinfo(env, to_spill);
171
172         /* add the remat information */
173         reloader                = obstack_alloc(&env->obst, sizeof(reloader[0]));
174         reloader->next          = spill_info->reloaders;
175         reloader->reloader      = before;
176         reloader->rematted_node = rematted_node;
177         reloader->allow_remat   = 1;
178
179         spill_info->reloaders  = reloader;
180
181         DBG((env->dbg, LEVEL_1, "creating spillinfo for %+F, will be rematerialized before %+F\n",
182                 to_spill, before));
183 }
184
185 void be_add_reload(spill_env_t *env, ir_node *to_spill, ir_node *before,
186                 const arch_register_class_t *reload_cls, int allow_remat)
187 {
188         spill_info_t *info;
189         reloader_t *rel;
190
191         info = get_spillinfo(env, to_spill);
192
193         if (is_Phi(to_spill)) {
194                 int i, arity;
195
196                 /* create spillinfos for the phi arguments */
197                 for (i = 0, arity = get_irn_arity(to_spill); i < arity; ++i) {
198                         ir_node *arg = get_irn_n(to_spill, i);
199                         get_spillinfo(env, arg);
200                 }
201
202 #if 1
203                 // hackery... sometimes the morgan algo spilled the value of a phi,
204                 // the belady algo decides later to spill the whole phi, then sees the
205                 // spill node and adds a reload for that spill node, problem is the
206                 // reload gets attach to that same spill (and is totally unnecessary)
207                 if (info->old_spill != NULL &&
208                         (before == info->old_spill || value_dominates(before, info->old_spill)))
209                 {
210                         printf("spilledphi hack was needed...\n");
211                         before = sched_next(info->old_spill);
212                 }
213 #endif
214         }
215
216         /* put reload into list */
217         rel                = obstack_alloc(&env->obst, sizeof(rel[0]));
218         rel->next          = info->reloaders;
219         rel->reloader      = before;
220         rel->rematted_node = NULL;
221         rel->allow_remat   = allow_remat;
222
223         info->reloaders  = rel;
224         assert(info->reload_cls == NULL || info->reload_cls == reload_cls);
225         info->reload_cls = reload_cls;
226
227         DBG((env->dbg, LEVEL_1, "creating spillinfo for %+F, will be reloaded before %+F, may%s be rematerialized\n",
228                 to_spill, before, allow_remat ? "" : " not"));
229 }
230
231 static ir_node *get_reload_insertion_point(ir_node *block, int pos) {
232         ir_node *predblock, *last;
233
234         /* simply add the reload to the beginning of the block if we only have 1 predecessor
235          * (we don't need to check for phis as there can't be any in a block with only 1 pred)
236          */
237         if(get_Block_n_cfgpreds(block) == 1) {
238                 assert(!is_Phi(sched_first(block)));
239                 return sched_first(block);
240         }
241
242         /* We have to reload the value in pred-block */
243         predblock = get_Block_cfgpred_block(block, pos);
244         last = sched_last(predblock);
245
246         /* we might have projs and keepanys behind the jump... */
247         while(is_Proj(last) || be_is_Keep(last)) {
248                 last = sched_prev(last);
249                 assert(!sched_is_end(last));
250         }
251
252         if(!is_cfop(last)) {
253                 last = sched_next(last);
254                 // last node must be a cfop, only exception is the start block
255                 assert(last     == get_irg_start_block(get_irn_irg(block)));
256         }
257
258         // add the reload before the (cond-)jump
259         return last;
260 }
261
262 void be_add_reload_on_edge(spill_env_t *env, ir_node *to_spill, ir_node *block, int pos,
263                 const arch_register_class_t *reload_cls, int allow_remat)
264 {
265         ir_node *before = get_reload_insertion_point(block, pos);
266         be_add_reload(env, to_spill, before, reload_cls, allow_remat);
267 }
268
269 void be_spill_phi(spill_env_t *env, ir_node *node) {
270         spill_info_t* spill;
271         int i, arity;
272
273         assert(is_Phi(node));
274
275         pset_insert_ptr(env->mem_phis, node);
276
277         // create spillinfos for the phi arguments
278         spill = get_spillinfo(env, node);
279         for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
280                 ir_node *arg = get_irn_n(node, i);
281                 get_spillinfo(env, arg);
282         }
283
284         // if we had a spill for the phi value before, then remove this spill from
285         // schedule, as we will remove it in the insert spill/reload phase
286         if(spill->spill != NULL && !is_Phi(spill->spill)) {
287                 assert(spill->old_spill == NULL);
288                 spill->old_spill = spill->spill;
289                 spill->spill = NULL;
290         }
291 }
292
293 /*
294  *   ____                _         ____        _ _ _
295  *  / ___|_ __ ___  __ _| |_ ___  / ___| _ __ (_) | |___
296  * | |   | '__/ _ \/ _` | __/ _ \ \___ \| '_ \| | | / __|
297  * | |___| | |  __/ (_| | ||  __/  ___) | |_) | | | \__ \
298  *  \____|_|  \___|\__,_|\__\___| |____/| .__/|_|_|_|___/
299  *                                      |_|
300  */
301
302 /**
303  * Schedules a node after an instruction. (That is the place after all projs and phis
304  * that are scheduled after the instruction)
305  * This function also skips phi nodes at the beginning of a block
306  */
307 static void sched_add_after_insn(ir_node *sched_after, ir_node *node) {
308         ir_node *next = sched_next(sched_after);
309         while(is_Proj(next) || is_Phi(next)) {
310                 next = sched_next(next);
311         }
312         assert(next != NULL);
313
314         if(sched_is_end(next)) {
315                 sched_add_after(sched_last(get_nodes_block(sched_after)), node);
316         } else {
317                 sched_add_before(next, node);
318         }
319 }
320
321 /**
322  * Creates a spill.
323  *
324  * @param senv      the spill environment
325  * @param irn       the node that should be spilled
326  * @param ctx_irn   an user of the spilled node
327  *
328  * @return a be_Spill node
329  */
330 static void spill_irn(spill_env_t *env, spill_info_t *spillinfo) {
331         ir_node *to_spill = spillinfo->spilled_node;
332
333         DBG((env->dbg, LEVEL_1, "spilling %+F ... ", to_spill));
334
335         /* Trying to spill an already spilled value, no need for a new spill
336          * node then, we can simply connect to the same one for this reload
337          *
338          * (although rematerialization code should handle most of these cases
339          * this can still happen when spilling Phis)
340          */
341         if(be_is_Reload(to_spill)) {
342                 spillinfo->spill = get_irn_n(to_spill, be_pos_Reload_mem);
343                 DB((env->dbg, LEVEL_1, "skip reload, using existing spill %+F\n", spillinfo->spill));
344                 return;
345         }
346
347         if (arch_irn_is(env->arch_env, to_spill, dont_spill)) {
348                 assert(0 && "Attempt to spill a node marked 'dont_spill'");
349         }
350
351         spillinfo->spill = be_spill(env->arch_env, to_spill);
352         DB((env->dbg, LEVEL_1, "add spill %+F after %+F\n", spillinfo->spill, to_spill));
353         sched_add_after_insn(to_spill, spillinfo->spill);
354 }
355
356 static void spill_node(spill_env_t *env, spill_info_t *spillinfo);
357
358 /**
359  * If the first usage of a Phi result would be out of memory
360  * there is no sense in allocating a register for it.
361  * Thus we spill it and all its operands to the same spill slot.
362  * Therefore the phi/dataB becomes a phi/Memory
363  *
364  * @param senv      the spill environment
365  * @param phi       the Phi node that should be spilled
366  * @param ctx_irn   an user of the spilled node
367  */
368 static void spill_phi(spill_env_t *env, spill_info_t *spillinfo) {
369         ir_node *phi = spillinfo->spilled_node;
370         int i;
371         int arity = get_irn_arity(phi);
372         ir_node     *block    = get_nodes_block(phi);
373         ir_node     **ins;
374
375         assert(is_Phi(phi));
376
377         DBG((env->dbg, LEVEL_1, "spilling Phi %+F:\n", phi));
378         /* build a new PhiM */
379         ins = alloca(sizeof(ir_node*) * arity);
380         for(i = 0; i < arity; ++i) {
381                 ins[i] = get_irg_bad(env->irg);
382         }
383         spillinfo->spill = new_r_Phi(env->irg, block, arity, ins, mode_M);
384
385         for(i = 0; i < arity; ++i) {
386                 ir_node *arg = get_irn_n(phi, i);
387                 spill_info_t *arg_info = get_spillinfo(env, arg);
388
389                 spill_node(env, arg_info);
390
391                 set_irn_n(spillinfo->spill, i, arg_info->spill);
392         }
393         DBG((env->dbg, LEVEL_1, "... done spilling Phi %+F\n", phi));
394
395         // rewire reloads from old_spill to phi
396         if (spillinfo->old_spill != NULL) {
397                 const ir_edge_t *edge, *next;
398                 ir_node *old_spill = spillinfo->old_spill;
399
400                 DBG((env->dbg, LEVEL_1, "old spill found, rewiring reloads:\n"));
401
402                 foreach_out_edge_safe(old_spill, edge, next) {
403                         ir_node *reload = get_edge_src_irn(edge);
404                         int     pos     = get_edge_src_pos(edge);
405
406                         DBG((env->dbg, LEVEL_1, "\tset input %d of %+F to %+F\n", pos, reload, spillinfo->spill));
407
408                         assert(be_is_Reload(reload) || is_Phi(reload));
409                         set_irn_n(reload, pos, spillinfo->spill);
410                 }
411                 DBG((env->dbg, LEVEL_1, "\tset input of %+F to BAD\n", old_spill));
412                 set_irn_n(old_spill, be_pos_Spill_val, new_Bad());
413                 //sched_remove(old_spill);
414                 spillinfo->old_spill = NULL;
415         }
416 }
417
418 /**
419  * Spill a node.
420  *
421  * @param senv      the spill environment
422  * @param to_spill  the node that should be spilled
423  */
424 static void spill_node(spill_env_t *env, spill_info_t *spillinfo) {
425         ir_node *to_spill;
426
427         // the node should be tagged for spilling already...
428         if(spillinfo->spill != NULL)
429                 return;
430
431         to_spill = spillinfo->spilled_node;
432         assert(sched_is_scheduled(to_spill) && "Node to be spilled must be scheduled!");
433         if (is_Phi(to_spill) && pset_find_ptr(env->mem_phis, spillinfo->spilled_node)) {
434                 spill_phi(env, spillinfo);
435         } else {
436                 spill_irn(env, spillinfo);
437         }
438 }
439
440 /*
441  *
442  *  ____                      _            _       _ _
443  * |  _ \ ___ _ __ ___   __ _| |_ ___ _ __(_) __ _| (_)_______
444  * | |_) / _ \ '_ ` _ \ / _` | __/ _ \ '__| |/ _` | | |_  / _ \
445  * |  _ <  __/ | | | | | (_| | ||  __/ |  | | (_| | | |/ /  __/
446  * |_| \_\___|_| |_| |_|\__,_|\__\___|_|  |_|\__,_|_|_/___\___|
447  *
448  */
449
450 /**
451  * Tests whether value @p arg is available before node @p reloader
452  * @returns 1 if value is available, 0 otherwise
453  */
454 static int is_value_available(spill_env_t *env, ir_node *arg, ir_node *reloader) {
455         if(is_Unknown(arg) || arg == new_NoMem())
456                 return 1;
457
458         if(be_is_Spill(arg))
459                 return 1;
460
461         if(arg == get_irg_frame(env->irg))
462                 return 1;
463
464         // hack for now (happens when command should be inserted at end of block)
465         if(is_Block(reloader)) {
466                 return 0;
467         }
468
469         /*
470          * Ignore registers are always available
471          */
472         if(arch_irn_is(env->arch_env, arg, ignore)) {
473                 return 1;
474         }
475
476 #if 0
477         /* the following test does not work while spilling,
478          * because the liveness info is not adapted yet to the effects of the
479          * additional spills/reloads.
480          */
481
482         /* we want to remat before the insn reloader
483          * thus an arguments is alive if
484          *   - it interferes with the reloaders result
485          *   - or it is (last-) used by reloader itself
486          */
487         if (values_interfere(env->birg->lv, reloader, arg)) {
488                 return 1;
489         }
490
491         arity = get_irn_arity(reloader);
492         for (i = 0; i < arity; ++i) {
493                 ir_node *rel_arg = get_irn_n(reloader, i);
494                 if (rel_arg == arg)
495                         return 1;
496         }
497 #endif
498
499         return 0;
500 }
501
502 /**
503  * Checks whether the node can principally be rematerialized
504  */
505 static int is_remat_node(spill_env_t *env, ir_node *node) {
506         const arch_env_t *arch_env = env->arch_env;
507
508         assert(!be_is_Spill(node));
509
510         if(arch_irn_is(arch_env, node, rematerializable)) {
511                 return 1;
512         }
513
514         if(be_is_StackParam(node))
515                 return 1;
516
517         return 0;
518 }
519
520 /**
521  * Check if a node is rematerializable. This tests for the following conditions:
522  *
523  * - The node itself is rematerializable
524  * - All arguments of the node are available or also rematerialisable
525  * - The costs for the rematerialisation operation is less or equal a limit
526  *
527  * Returns the costs needed for rematerialisation or something
528  * > REMAT_COST_LIMIT if remat is not possible.
529  */
530 static int check_remat_conditions_costs(spill_env_t *env, ir_node *spilled, ir_node *reloader, int parentcosts) {
531         int i, arity;
532         int argremats;
533         int costs = 0;
534
535         if(!is_remat_node(env, spilled))
536                 return REMAT_COST_LIMIT;
537
538         if(be_is_Reload(spilled)) {
539                 costs += 2;
540         } else {
541                 costs += arch_get_op_estimated_cost(env->arch_env, spilled);
542         }
543         if(parentcosts + costs >= REMAT_COST_LIMIT) {
544                 return REMAT_COST_LIMIT;
545         }
546
547         argremats = 0;
548         for(i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
549                 ir_node *arg = get_irn_n(spilled, i);
550
551                 if(is_value_available(env, arg, reloader))
552                         continue;
553
554                 // we have to rematerialize the argument as well...
555                 if(argremats >= 1) {
556                         /* we only support rematerializing 1 argument at the moment,
557                          * so that we don't have to care about register pressure
558                          */
559                         return REMAT_COST_LIMIT;
560                 }
561                 argremats++;
562
563                 costs += check_remat_conditions_costs(env, arg, reloader, parentcosts + costs);
564                 if(parentcosts + costs >= REMAT_COST_LIMIT)
565                         return REMAT_COST_LIMIT;
566         }
567
568         return costs;
569 }
570
571 static int check_remat_conditions(spill_env_t *env, ir_node *spilled, ir_node *reloader) {
572         int costs = check_remat_conditions_costs(env, spilled, reloader, 0);
573
574         return costs < REMAT_COST_LIMIT;
575 }
576
577 /**
578  * Re-materialize a node.
579  *
580  * @param senv      the spill environment
581  * @param spilled   the node that was spilled
582  * @param reloader  a irn that requires a reload
583  */
584 static ir_node *do_remat(spill_env_t *env, ir_node *spilled, ir_node *reloader) {
585         int i, arity;
586         ir_node *res;
587         ir_node *bl;
588         ir_node **ins;
589
590         if(is_Block(reloader)) {
591                 bl = reloader;
592         } else {
593                 bl = get_nodes_block(reloader);
594         }
595
596         ins = alloca(get_irn_arity(spilled) * sizeof(ins[0]));
597         for(i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
598                 ir_node *arg = get_irn_n(spilled, i);
599
600                 if(is_value_available(env, arg, reloader)) {
601                         ins[i] = arg;
602                 } else {
603                         ins[i] = do_remat(env, arg, reloader);
604                 }
605         }
606
607         /* create a copy of the node */
608         res = new_ir_node(get_irn_dbg_info(spilled), env->irg, bl,
609                 get_irn_op(spilled),
610                 get_irn_mode(spilled),
611                 get_irn_arity(spilled),
612                 ins);
613         copy_node_attr(spilled, res);
614         new_backedge_info(res);
615         sched_reset(res);
616
617         DBG((env->dbg, LEVEL_1, "Insert remat %+F of %+F before reloader %+F\n", res, spilled, reloader));
618
619         /* insert in schedule */
620         sched_add_before(reloader, res);
621
622         return res;
623 }
624
625 int be_get_reload_costs(spill_env_t *env, ir_node *to_spill, ir_node *before) {
626         spill_info_t *spill_info;
627
628         if(be_do_remats) {
629                 // is the node rematerializable?
630                 int costs = check_remat_conditions_costs(env, to_spill, before, 0);
631                 if(costs < REMAT_COST_LIMIT)
632                         return costs;
633         }
634
635         // do we already have a spill?
636         spill_info = find_spillinfo(env, to_spill);
637         if(spill_info != NULL && spill_info->spill != NULL)
638                 return env->reload_cost;
639
640         return env->spill_cost + env->reload_cost;
641 }
642
643 int be_get_reload_costs_on_edge(spill_env_t *env, ir_node *to_spill, ir_node *block, int pos) {
644         ir_node *before = get_reload_insertion_point(block, pos);
645         return be_get_reload_costs(env, to_spill, before);
646 }
647
648 /*
649  *  ___                     _     ____      _                 _
650  * |_ _|_ __  ___  ___ _ __| |_  |  _ \ ___| | ___   __ _  __| |___
651  *  | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ |/ _ \ / _` |/ _` / __|
652  *  | || | | \__ \  __/ |  | |_  |  _ <  __/ | (_) | (_| | (_| \__ \
653  * |___|_| |_|___/\___|_|   \__| |_| \_\___|_|\___/ \__,_|\__,_|___/
654  *
655  */
656
657 void be_insert_spills_reloads(spill_env_t *env) {
658         const arch_env_t *arch_env = env->arch_env;
659         int              remats    = 0;
660         int              reloads   = 0;
661         int              spills    = 0;
662         spill_info_t     *si;
663
664         /* process each spilled node */
665         for (si = set_first(env->spills); si; si = set_next(env->spills)) {
666                 reloader_t *rld;
667                 ir_mode    *mode   = get_irn_mode(si->spilled_node);
668                 pset       *values = pset_new_ptr(16);
669
670                 DBG((env->dbg, LEVEL_1, "\nhandling all reloaders of %+F:\n", si->spilled_node));
671
672                 /* go through all reloads for this spill */
673                 for (rld = si->reloaders; rld; rld = rld->next) {
674                         ir_node *new_val;
675
676                         if (rld->rematted_node != NULL) {
677                                 new_val = rld->rematted_node;
678                                 remats++;
679                                 sched_add_before(rld->reloader, new_val);
680                         }
681                         else if (be_do_remats && rld->allow_remat && check_remat_conditions(env, si->spilled_node, rld->reloader)) {
682                                 new_val = do_remat(env, si->spilled_node, rld->reloader);
683                                 remats++;
684                         }
685                         else {
686                                 /* make sure we have a spill */
687                                 if (si->spill == NULL) {
688                                         spill_node(env, si);
689                                         spills++;
690                                 }
691
692                                 /* create a reload */
693                                 new_val = be_reload(arch_env, si->reload_cls, rld->reloader, mode, si->spill);
694                                 reloads++;
695                         }
696
697                         DBG((env->dbg, LEVEL_1, " %+F of %+F before %+F\n", new_val, si->spilled_node, rld->reloader));
698                         pset_insert_ptr(values, new_val);
699                 }
700
701                 if (pset_count(values) > 0) {
702                         /* introduce copies, rewire the uses */
703                         pset_insert_ptr(values, si->spilled_node);
704                         be_ssa_constr_set_ignore(env->birg->dom_front, env->birg->lv, values, env->mem_phis);
705                 }
706
707                 del_pset(values);
708
709                 si->reloaders = NULL;
710         }
711
712 #ifdef FIRM_STATISTICS
713         if (be_stat_ev_is_active()) {
714                 be_stat_ev("spill_spills", spills);
715                 be_stat_ev("spill_reloads", reloads);
716                 be_stat_ev("spill_remats", remats);
717         }
718 #endif /* FIRM_STATISTICS */
719
720         be_remove_dead_nodes_from_schedule(env->irg);
721         //be_liveness_recompute(env->birg->lv);
722         be_invalidate_liveness(env->birg);
723 }