2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief implementation of the spill/reload placement abstraction layer
23 * @author Daniel Grund, Sebastian Hack, Matthias Braun
36 #include "iredges_t.h"
37 #include "irbackedge_t.h"
47 #include "irnodeset.h"
51 #include "besched_t.h"
55 #include "bechordal_t.h"
56 #include "bejavacoal.h"
57 #include "benodesets.h"
58 #include "bespilloptions.h"
59 #include "bestatevent.h"
60 #include "bessaconstr.h"
62 #include "beintlive_t.h"
65 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
67 #define REMAT_COST_INFINITE 1000
69 typedef struct reloader_t reloader_t;
72 ir_node *can_spill_after;
74 ir_node *rematted_node;
75 int remat_cost_delta; /** costs needed for rematerialization,
76 compared to placing a reload */
79 typedef struct spill_info_t spill_info_t;
81 ir_node *to_spill; /**< the value that should get spilled */
82 reloader_t *reloaders; /**< list of places where the value should get
84 ir_node *spill; /**< the spill node, or a PhiM node */
85 ir_node *old_spill; /**< if we had the value of a phi spilled before but
86 not the phi itself then this field contains the
87 spill for the phi value */
88 const arch_register_class_t *reload_cls; /** the register class in which the
89 reload should be placed */
93 const arch_env_t *arch_env;
97 int spill_cost; /**< the cost of a single spill node */
98 int reload_cost; /**< the cost of a reload node */
99 set *spills; /**< all spill_info_t's, which must be
101 ir_nodeset_t mem_phis; /**< set of all spilled phis. */
102 ir_exec_freq *exec_freq;
104 #ifdef FIRM_STATISTICS
105 unsigned spill_count;
106 unsigned reload_count;
107 unsigned remat_count;
108 unsigned spilled_phi_count;
113 * Compare two spill infos.
116 int cmp_spillinfo(const void *x, const void *y, size_t size)
118 const spill_info_t *xx = x;
119 const spill_info_t *yy = y;
122 return xx->to_spill != yy->to_spill;
126 * Returns spill info for a specific value (the value that is to be spilled)
129 spill_info_t *get_spillinfo(const spill_env_t *env, ir_node *value)
131 spill_info_t info, *res;
132 int hash = nodeset_hash(value);
134 info.to_spill = value;
135 res = set_find(env->spills, &info, sizeof(info), hash);
138 info.reloaders = NULL;
140 info.old_spill = NULL;
141 info.reload_cls = NULL;
142 res = set_insert(env->spills, &info, sizeof(info), hash);
148 spill_env_t *be_new_spill_env(be_irg_t *birg)
150 const arch_env_t *arch_env = birg->main_env->arch_env;
152 spill_env_t *env = xmalloc(sizeof(env[0]));
153 env->spills = new_set(cmp_spillinfo, 1024);
154 env->irg = be_get_birg_irg(birg);
156 env->arch_env = arch_env;
157 ir_nodeset_init(&env->mem_phis);
158 env->spill_cost = arch_env->isa->spill_cost;
159 env->reload_cost = arch_env->isa->reload_cost;
160 env->exec_freq = be_get_birg_exec_freq(birg);
161 obstack_init(&env->obst);
163 #ifdef FIRM_STATISTICS
164 env->spill_count = 0;
165 env->reload_count = 0;
166 env->remat_count = 0;
167 env->spilled_phi_count = 0;
173 void be_delete_spill_env(spill_env_t *env)
175 del_set(env->spills);
176 ir_nodeset_destroy(&env->mem_phis);
177 obstack_free(&env->obst, NULL);
183 * | _ \| | __ _ ___ ___ | _ \ ___| | ___ __ _ __| |___
184 * | |_) | |/ _` |/ __/ _ \ | |_) / _ \ |/ _ \ / _` |/ _` / __|
185 * | __/| | (_| | (_| __/ | _ < __/ | (_) | (_| | (_| \__ \
186 * |_| |_|\__,_|\___\___| |_| \_\___|_|\___/ \__,_|\__,_|___/
190 void be_add_remat(spill_env_t *env, ir_node *to_spill, ir_node *before,
191 ir_node *rematted_node)
193 spill_info_t *spill_info;
194 reloader_t *reloader;
196 spill_info = get_spillinfo(env, to_spill);
198 /* add the remat information */
199 reloader = obstack_alloc(&env->obst, sizeof(reloader[0]));
200 reloader->next = spill_info->reloaders;
201 reloader->reloader = before;
202 reloader->rematted_node = rematted_node;
203 reloader->remat_cost_delta = 0; /* We will never have a cost win over a
204 reload since we're not even allowed to
207 spill_info->reloaders = reloader;
209 DBG((dbg, LEVEL_1, "creating spillinfo for %+F, will be rematerialized before %+F\n",
213 void be_add_reload2(spill_env_t *env, ir_node *to_spill, ir_node *before, ir_node *can_spill_after,
214 const arch_register_class_t *reload_cls, int allow_remat)
219 info = get_spillinfo(env, to_spill);
221 if (is_Phi(to_spill)) {
224 /* create spillinfos for the phi arguments */
225 for (i = 0, arity = get_irn_arity(to_spill); i < arity; ++i) {
226 ir_node *arg = get_irn_n(to_spill, i);
227 get_spillinfo(env, arg);
231 /* hackery... sometimes the morgan algo spilled the value of a phi,
232 * the belady algo decides later to spill the whole phi, then sees the
233 * spill node and adds a reload for that spill node, problem is the
234 * reload gets attach to that same spill (and is totally unnecessary)
236 if (info->old_spill != NULL &&
237 (before == info->old_spill || value_dominates(before, info->old_spill)))
239 printf("spilledphi hack was needed...\n");
240 before = sched_next(info->old_spill);
245 assert(!is_Proj(before) && !be_is_Keep(before));
247 /* put reload into list */
248 rel = obstack_alloc(&env->obst, sizeof(rel[0]));
249 rel->next = info->reloaders;
250 rel->reloader = before;
251 rel->rematted_node = NULL;
252 rel->can_spill_after = can_spill_after;
253 rel->remat_cost_delta = allow_remat ? 0 : REMAT_COST_INFINITE;
255 info->reloaders = rel;
256 assert(info->reload_cls == NULL || info->reload_cls == reload_cls);
257 info->reload_cls = reload_cls;
259 DBG((dbg, LEVEL_1, "creating spillinfo for %+F, will be reloaded before %+F, may%s be rematerialized\n",
260 to_spill, before, allow_remat ? "" : " not"));
263 void be_add_reload(spill_env_t *senv, ir_node *to_spill, ir_node *before,
264 const arch_register_class_t *reload_cls, int allow_remat)
266 be_add_reload2(senv, to_spill, before, to_spill, reload_cls, allow_remat);
270 ir_node *be_get_end_of_block_insertion_point(const ir_node *block)
272 ir_node *last = sched_last(block);
274 /* we might have projs and keepanys behind the jump... */
275 while(is_Proj(last) || be_is_Keep(last)) {
276 last = sched_prev(last);
277 assert(!sched_is_end(last));
281 last = sched_next(last);
282 /* last node must be a cfop, only exception is the start block */
283 assert(last == get_irg_start_block(get_irn_irg(block)));
286 /* add the reload before the (cond-)jump */
291 * Returns the point at which you can insert a node that should be executed
292 * before block @p block when coming from pred @p pos.
295 ir_node *get_block_insertion_point(ir_node *block, int pos)
299 /* simply add the reload to the beginning of the block if we only have 1
300 * predecessor. We don't need to check for phis as there can't be any in a
301 * block with only 1 pred. */
302 if(get_Block_n_cfgpreds(block) == 1) {
303 assert(!is_Phi(sched_first(block)));
304 return sched_first(block);
307 /* We have to reload the value in pred-block */
308 predblock = get_Block_cfgpred_block(block, pos);
309 return be_get_end_of_block_insertion_point(predblock);
312 void be_add_reload_at_end(spill_env_t *env, ir_node *to_spill, const ir_node *block,
313 const arch_register_class_t *reload_cls,
316 ir_node *before = be_get_end_of_block_insertion_point(block);
317 be_add_reload(env, to_spill, before, reload_cls, allow_remat);
320 void be_add_reload_on_edge(spill_env_t *env, ir_node *to_spill, ir_node *block,
321 int pos, const arch_register_class_t *reload_cls,
324 ir_node *before = get_block_insertion_point(block, pos);
325 be_add_reload(env, to_spill, before, reload_cls, allow_remat);
328 void be_spill_phi(spill_env_t *env, ir_node *node)
333 assert(is_Phi(node));
335 ir_nodeset_insert(&env->mem_phis, node);
337 /* create spillinfos for the phi arguments */
338 spill = get_spillinfo(env, node);
339 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
340 ir_node *arg = get_irn_n(node, i);
341 get_spillinfo(env, arg);
344 /* if we had a spill for the phi value before, then remove this spill from
345 * schedule, as we will remove it in the insert spill/reload phase
347 if(spill->spill != NULL && !is_Phi(spill->spill)) {
348 assert(spill->old_spill == NULL);
349 spill->old_spill = spill->spill;
356 * / ___|_ __ ___ __ _| |_ ___ / ___| _ __ (_) | |___
357 * | | | '__/ _ \/ _` | __/ _ \ \___ \| '_ \| | | / __|
358 * | |___| | | __/ (_| | || __/ ___) | |_) | | | \__ \
359 * \____|_| \___|\__,_|\__\___| |____/| .__/|_|_|_|___/
364 * Schedules a node after an instruction. That is the place after all projs and
365 * phis that are scheduled after the instruction. This function also skips phi
366 * nodes at the beginning of a block
369 void sched_add_after_insn(ir_node *sched_after, ir_node *node)
371 ir_node *next = sched_next(sched_after);
372 while(is_Proj(next) || is_Phi(next) || be_is_Keep(next)) {
373 next = sched_next(next);
375 assert(next != NULL);
377 if(sched_is_end(next)) {
378 sched_add_after(sched_last(get_nodes_block(sched_after)), node);
380 sched_add_before(next, node);
387 * @param senv the spill environment
388 * @param irn the node that should be spilled
389 * @param ctx_irn an user of the spilled node
391 * @return a be_Spill node
394 void spill_irn(spill_env_t *env, spill_info_t *spillinfo)
396 optimization_state_t opt;
397 ir_node *to_spill = spillinfo->to_spill;
399 DBG((dbg, LEVEL_1, "spilling %+F ... ", to_spill));
401 /* Trying to spill an already spilled value, no need for a new spill
402 * node then, we can simply connect to the same one for this reload
404 * Normally reloads get simply rematerialized instead of spilled again; this
405 * can happen annyway when the reload is the pred of a phi to spill)
407 if (be_is_Reload(to_spill)) {
408 spillinfo->spill = get_irn_n(to_spill, be_pos_Reload_mem);
409 DB((dbg, LEVEL_1, "skip reload, using existing spill %+F\n", spillinfo->spill));
413 assert(!(arch_irn_is(env->arch_env, to_spill, dont_spill)
414 && "Attempt to spill a node marked 'dont_spill'"));
416 /* some backends have virtual noreg/unknown nodes that are not scheduled */
417 if(!sched_is_scheduled(to_spill)) {
418 spillinfo->spill = new_NoMem();
424 * We switch on optimizations here to get CSE. This is needed as the STA
425 * backends has some extra spill phases and we want to make use of those
426 * spills instead of creating new ones.
428 save_optimization_state(&opt);
430 spillinfo->spill = be_spill(env->arch_env, to_spill);
431 restore_optimization_state(&opt);
432 if (! sched_is_scheduled(spillinfo->spill)) {
433 DB((dbg, LEVEL_1, "add spill %+F after %+F\n", spillinfo->spill, to_spill));
434 #ifdef FIRM_STATISTICS
437 sched_add_after_insn(to_spill, spillinfo->spill);
439 DB((dbg, LEVEL_1, "re-using spill %+F after %+F\n", spillinfo->spill, to_spill));
444 void spill_node(spill_env_t *env, spill_info_t *spillinfo);
447 * If the first usage of a Phi result would be out of memory
448 * there is no sense in allocating a register for it.
449 * Thus we spill it and all its operands to the same spill slot.
450 * Therefore the phi/dataB becomes a phi/Memory
452 * @param senv the spill environment
453 * @param phi the Phi node that should be spilled
454 * @param ctx_irn an user of the spilled node
457 void spill_phi(spill_env_t *env, spill_info_t *spillinfo)
459 ir_node *phi = spillinfo->to_spill;
461 int arity = get_irn_arity(phi);
462 ir_node *block = get_nodes_block(phi);
467 DBG((dbg, LEVEL_1, "spilling Phi %+F:\n", phi));
468 /* build a new PhiM */
469 ins = alloca(sizeof(ir_node*) * arity);
470 for(i = 0; i < arity; ++i) {
471 ins[i] = get_irg_bad(env->irg);
473 spillinfo->spill = new_r_Phi(env->irg, block, arity, ins, mode_M);
474 #ifdef FIRM_STATISTICS
475 env->spilled_phi_count++;
478 for(i = 0; i < arity; ++i) {
479 ir_node *arg = get_irn_n(phi, i);
480 spill_info_t *arg_info = get_spillinfo(env, arg);
482 spill_node(env, arg_info);
484 set_irn_n(spillinfo->spill, i, arg_info->spill);
486 DBG((dbg, LEVEL_1, "... done spilling Phi %+F, created PhiM %+F\n", phi, spillinfo->spill));
488 /* rewire reloads from old_spill to phi */
489 if (spillinfo->old_spill != NULL) {
490 const ir_edge_t *edge, *next;
491 ir_node *old_spill = spillinfo->old_spill;
493 DBG((dbg, LEVEL_1, "old spill found, rewiring reloads:\n"));
495 foreach_out_edge_safe(old_spill, edge, next) {
496 ir_node *reload = get_edge_src_irn(edge);
497 int pos = get_edge_src_pos(edge);
499 DBG((dbg, LEVEL_1, "\tset input %d of %+F to %+F\n", pos, reload, spillinfo->spill));
501 assert(be_is_Reload(reload) || is_Phi(reload));
502 set_irn_n(reload, pos, spillinfo->spill);
504 DBG((dbg, LEVEL_1, "\tset input of %+F to BAD\n", old_spill));
505 set_irn_n(old_spill, be_pos_Spill_val, new_Bad());
506 /* sched_remove(old_spill); */
507 spillinfo->old_spill = NULL;
514 * @param senv the spill environment
515 * @param to_spill the node that should be spilled
518 void spill_node(spill_env_t *env, spill_info_t *spillinfo)
522 /* the node should be tagged for spilling already... */
523 if(spillinfo->spill != NULL)
526 to_spill = spillinfo->to_spill;
528 if (is_Phi(to_spill) && ir_nodeset_contains(&env->mem_phis, to_spill)) {
529 spill_phi(env, spillinfo);
531 spill_irn(env, spillinfo);
538 * | _ \ ___ _ __ ___ __ _| |_ ___ _ __(_) __ _| (_)_______
539 * | |_) / _ \ '_ ` _ \ / _` | __/ _ \ '__| |/ _` | | |_ / _ \
540 * | _ < __/ | | | | | (_| | || __/ | | | (_| | | |/ / __/
541 * |_| \_\___|_| |_| |_|\__,_|\__\___|_| |_|\__,_|_|_/___\___|
546 * Tests whether value @p arg is available before node @p reloader
547 * @returns 1 if value is available, 0 otherwise
550 int is_value_available(spill_env_t *env, const ir_node *arg, const ir_node *reloader)
552 if(is_Unknown(arg) || arg == new_NoMem())
558 if(arg == get_irg_frame(env->irg))
561 /* hack for now (happens when command should be inserted at end of block) */
562 if(is_Block(reloader)) {
567 * Ignore registers are always available
569 if(arch_irn_is(env->arch_env, arg, ignore)) {
573 /* the following test does not work while spilling,
574 * because the liveness info is not adapted yet to the effects of the
575 * additional spills/reloads.
578 /* we want to remat before the insn reloader
579 * thus an arguments is alive if
580 * - it interferes with the reloaders result
581 * - or it is (last-) used by reloader itself
583 if (values_interfere(env->birg->lv, reloader, arg)) {
587 arity = get_irn_arity(reloader);
588 for (i = 0; i < arity; ++i) {
589 ir_node *rel_arg = get_irn_n(reloader, i);
599 * Checks whether the node can principally be rematerialized
602 int is_remat_node(spill_env_t *env, const ir_node *node)
604 const arch_env_t *arch_env = env->arch_env;
606 assert(!be_is_Spill(node));
608 if(arch_irn_is(arch_env, node, rematerializable))
615 * Check if a node is rematerializable. This tests for the following conditions:
617 * - The node itself is rematerializable
618 * - All arguments of the node are available or also rematerialisable
619 * - The costs for the rematerialisation operation is less or equal a limit
621 * Returns the costs needed for rematerialisation or something
622 * >= REMAT_COST_INFINITE if remat is not possible.
625 int check_remat_conditions_costs(spill_env_t *env, const ir_node *spilled,
626 const ir_node *reloader, int parentcosts)
632 if(!is_remat_node(env, spilled))
633 return REMAT_COST_INFINITE;
635 if(be_is_Reload(spilled)) {
638 costs += arch_get_op_estimated_cost(env->arch_env, spilled);
640 if(parentcosts + costs >= env->reload_cost + env->spill_cost) {
641 return REMAT_COST_INFINITE;
645 for(i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
646 ir_node *arg = get_irn_n(spilled, i);
648 if(is_value_available(env, arg, reloader))
651 /* we have to rematerialize the argument as well */
653 /* we only support rematerializing 1 argument at the moment,
654 * so that we don't have to care about register pressure
656 return REMAT_COST_INFINITE;
660 costs += check_remat_conditions_costs(env, arg, reloader, parentcosts + costs);
661 if(parentcosts + costs >= env->reload_cost + env->spill_cost)
662 return REMAT_COST_INFINITE;
669 * Re-materialize a node.
671 * @param senv the spill environment
672 * @param spilled the node that was spilled
673 * @param reloader a irn that requires a reload
676 ir_node *do_remat(spill_env_t *env, ir_node *spilled, ir_node *reloader)
683 if(is_Block(reloader)) {
686 bl = get_nodes_block(reloader);
689 ins = alloca(get_irn_arity(spilled) * sizeof(ins[0]));
690 for(i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
691 ir_node *arg = get_irn_n(spilled, i);
693 if(is_value_available(env, arg, reloader)) {
696 ins[i] = do_remat(env, arg, reloader);
697 #ifdef FIRM_STATISTICS
698 /* don't count the recursive call as remat */
704 /* create a copy of the node */
705 res = new_ir_node(get_irn_dbg_info(spilled), env->irg, bl,
706 get_irn_op(spilled), get_irn_mode(spilled),
707 get_irn_arity(spilled), ins);
708 copy_node_attr(spilled, res);
709 new_backedge_info(res);
711 DBG((dbg, LEVEL_1, "Insert remat %+F of %+F before reloader %+F\n", res, spilled, reloader));
713 if (! is_Proj(res)) {
714 /* insert in schedule */
716 sched_add_before(reloader, res);
717 #ifdef FIRM_STATISTICS
725 double be_get_spill_costs(spill_env_t *env, ir_node *to_spill, ir_node *after)
727 ir_node *block = get_nodes_block(after);
728 double freq = get_block_execfreq(env->exec_freq, block);
731 return env->spill_cost * freq;
734 double be_get_reload_costs(spill_env_t *env, ir_node *to_spill, ir_node *before)
736 ir_node *block = get_nodes_block(before);
737 double freq = get_block_execfreq(env->exec_freq, block);
740 /* is the node rematerializable? */
741 int costs = check_remat_conditions_costs(env, to_spill, before, 0);
742 if(costs < env->reload_cost)
746 return env->reload_cost * freq;
749 int be_is_rematerializable(spill_env_t *env, const ir_node *to_remat, const ir_node *before)
751 return check_remat_conditions_costs(env, to_remat, before, 0) < REMAT_COST_INFINITE;
754 double be_get_reload_costs_on_edge(spill_env_t *env, ir_node *to_spill,
755 ir_node *block, int pos)
757 ir_node *before = get_block_insertion_point(block, pos);
758 return be_get_reload_costs(env, to_spill, before);
763 * |_ _|_ __ ___ ___ _ __| |_ | _ \ ___| | ___ __ _ __| |___
764 * | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ |/ _ \ / _` |/ _` / __|
765 * | || | | \__ \ __/ | | |_ | _ < __/ | (_) | (_| | (_| \__ \
766 * |___|_| |_|___/\___|_| \__| |_| \_\___|_|\___/ \__,_|\__,_|___/
770 void be_insert_spills_reloads(spill_env_t *env)
772 const arch_env_t *arch_env = env->arch_env;
773 const ir_exec_freq *exec_freq = env->exec_freq;
775 ir_nodeset_iterator_t iter;
778 /* create all phi-ms first, this is needed so, that phis, hanging on
779 spilled phis work correctly */
780 foreach_ir_nodeset(&env->mem_phis, node, iter) {
781 spill_info_t *info = get_spillinfo(env, node);
782 spill_node(env, info);
785 /* process each spilled node */
786 for (si = set_first(env->spills); si; si = set_next(env->spills)) {
788 ir_node *to_spill = si->to_spill;
789 ir_mode *mode = get_irn_mode(to_spill);
790 ir_node **copies = NEW_ARR_F(ir_node*, 0);
791 double all_remat_costs = 0; /** costs when we would remat all nodes */
794 DBG((dbg, LEVEL_1, "\nhandling all reloaders of %+F:\n", to_spill));
796 /* determine possibility of rematerialisations */
798 for (rld = si->reloaders; rld != NULL; rld = rld->next) {
801 int remat_cost_delta;
803 ir_node *reloader = rld->reloader;
805 if(rld->rematted_node != NULL) {
806 DBG((dbg, LEVEL_2, "\tforced remat %+F before %+F\n",
807 rld->rematted_node, reloader));
810 if(rld->remat_cost_delta >= REMAT_COST_INFINITE) {
811 DBG((dbg, LEVEL_2, "\treload before %+F is forbidden\n",
813 all_remat_costs = REMAT_COST_INFINITE;
817 remat_cost = check_remat_conditions_costs(env, to_spill,
819 if(remat_cost >= REMAT_COST_INFINITE) {
820 DBG((dbg, LEVEL_2, "\tremat before %+F not possible\n",
822 rld->remat_cost_delta = REMAT_COST_INFINITE;
823 all_remat_costs = REMAT_COST_INFINITE;
827 remat_cost_delta = remat_cost - env->reload_cost;
828 rld->remat_cost_delta = remat_cost_delta;
829 block = is_Block(reloader) ? reloader : get_nodes_block(reloader);
830 freq = get_block_execfreq(exec_freq, block);
831 all_remat_costs += remat_cost_delta * freq;
832 DBG((dbg, LEVEL_2, "\tremat costs delta before %+F: "
833 "%d (rel %f)\n", reloader, remat_cost_delta,
834 remat_cost_delta * freq));
836 if(all_remat_costs < REMAT_COST_INFINITE) {
837 ir_node *block = get_nodes_block(to_spill);
838 double freq = get_block_execfreq(exec_freq, block);
839 /* we don't need the costs for the spill if we can remat
841 all_remat_costs -= env->spill_cost * freq;
843 DBG((dbg, LEVEL_2, "\tspill costs %d (rel %f)\n",
844 env->spill_cost, env->spill_cost * freq));
847 if(all_remat_costs < 0) {
848 DBG((dbg, LEVEL_1, "\nforcing remats of all reloaders (%f)\n",
854 /* go through all reloads for this spill */
855 for (rld = si->reloaders; rld != NULL; rld = rld->next) {
856 ir_node *copy; /* a reload is a "copy" of the original value */
858 if (rld->rematted_node != NULL) {
859 copy = rld->rematted_node;
860 sched_add_before(rld->reloader, copy);
861 } else if (be_do_remats &&
862 (force_remat || rld->remat_cost_delta < 0)) {
863 copy = do_remat(env, to_spill, rld->reloader);
865 /* make sure we have a spill */
866 if (si->spill == NULL) {
870 /* create a reload */
871 copy = be_reload(arch_env, si->reload_cls, rld->reloader, mode,
873 #ifdef FIRM_STATISTICS
878 DBG((dbg, LEVEL_1, " %+F of %+F before %+F\n",
879 copy, to_spill, rld->reloader));
880 ARR_APP1(ir_node*, copies, copy);
883 /* if we had any reloads or remats, then we need to reconstruct the
884 * SSA form for the spilled value */
885 if (ARR_LEN(copies) > 0) {
886 be_ssa_construction_env_t senv;
887 /* be_lv_t *lv = be_get_birg_liveness(env->birg); */
889 be_ssa_construction_init(&senv, env->birg);
890 be_ssa_construction_add_copy(&senv, to_spill);
891 be_ssa_construction_add_copies(&senv, copies, ARR_LEN(copies));
892 be_ssa_construction_fix_users(&senv, to_spill);
895 /* no need to enable this as long as we invalidate liveness
896 after this function... */
897 be_ssa_construction_update_liveness_phis(&senv);
898 be_liveness_update(to_spill);
899 len = ARR_LEN(copies);
900 for(i = 0; i < len; ++i) {
901 be_liveness_update(lv, copies[i]);
904 be_ssa_construction_destroy(&senv);
908 si->reloaders = NULL;
911 stat_ev_dbl("spill_spills", env->spill_count);
912 stat_ev_dbl("spill_reloads", env->reload_count);
913 stat_ev_dbl("spill_remats", env->remat_count);
914 stat_ev_dbl("spill_spilled_phis", env->spilled_phi_count);
916 /* Matze: In theory be_ssa_construction should take care of the liveness...
917 * try to disable this again in the future */
918 be_liveness_invalidate(env->birg->lv);
920 be_remove_dead_nodes_from_schedule(env->birg);
923 void be_init_spill(void)
925 FIRM_DBG_REGISTER(dbg, "firm.be.spill");
928 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spill);