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"
52 #include "besched_t.h"
56 #include "bechordal_t.h"
57 #include "bejavacoal.h"
58 #include "benodesets.h"
59 #include "bespilloptions.h"
60 #include "bestatevent.h"
61 #include "bessaconstr.h"
63 #include "beintlive_t.h"
66 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
68 #define REMAT_COST_INFINITE 1000
70 typedef struct reloader_t reloader_t;
73 ir_node *can_spill_after;
75 ir_node *rematted_node;
76 int remat_cost_delta; /** costs needed for rematerialization,
77 compared to placing a reload */
80 typedef struct spill_t spill_t;
83 ir_node *before; /**< spill has to be placed before this node (or earlier) */
87 typedef struct spill_info_t spill_info_t;
89 ir_node *to_spill; /**< the value that should get spilled */
90 reloader_t *reloaders; /**< list of places where the value should get
92 spill_t *spills; /**< list of latest places where spill must be
94 double spill_costs; /**< costs needed for spilling the value */
95 const arch_register_class_t *reload_cls; /** the register class in which the
96 reload should be placed */
100 const arch_env_t *arch_env;
104 int spill_cost; /**< the cost of a single spill node */
105 int reload_cost; /**< the cost of a reload node */
106 set *spills; /**< all spill_info_t's, which must be
108 ir_nodeset_t mem_phis; /**< set of all spilled phis. */
109 ir_exec_freq *exec_freq;
111 #ifdef FIRM_STATISTICS
112 unsigned spill_count;
113 unsigned reload_count;
114 unsigned remat_count;
115 unsigned spilled_phi_count;
120 * Compare two spill infos.
122 static int cmp_spillinfo(const void *x, const void *y, size_t size)
124 const spill_info_t *xx = x;
125 const spill_info_t *yy = y;
128 return xx->to_spill != yy->to_spill;
132 * Returns spill info for a specific value (the value that is to be spilled)
134 static spill_info_t *get_spillinfo(const spill_env_t *env, ir_node *value)
136 spill_info_t info, *res;
137 int hash = nodeset_hash(value);
139 info.to_spill = value;
140 res = set_find(env->spills, &info, sizeof(info), hash);
143 info.reloaders = NULL;
145 info.spill_costs = -1;
146 info.reload_cls = NULL;
147 res = set_insert(env->spills, &info, sizeof(info), hash);
153 spill_env_t *be_new_spill_env(be_irg_t *birg)
155 const arch_env_t *arch_env = birg->main_env->arch_env;
157 spill_env_t *env = xmalloc(sizeof(env[0]));
158 env->spills = new_set(cmp_spillinfo, 1024);
159 env->irg = be_get_birg_irg(birg);
161 env->arch_env = arch_env;
162 ir_nodeset_init(&env->mem_phis);
163 env->spill_cost = arch_env->isa->spill_cost;
164 env->reload_cost = arch_env->isa->reload_cost;
165 env->exec_freq = be_get_birg_exec_freq(birg);
166 obstack_init(&env->obst);
168 #ifdef FIRM_STATISTICS
169 env->spill_count = 0;
170 env->reload_count = 0;
171 env->remat_count = 0;
172 env->spilled_phi_count = 0;
178 void be_delete_spill_env(spill_env_t *env)
180 del_set(env->spills);
181 ir_nodeset_destroy(&env->mem_phis);
182 obstack_free(&env->obst, NULL);
188 * | _ \| | __ _ ___ ___ | _ \ ___| | ___ __ _ __| |___
189 * | |_) | |/ _` |/ __/ _ \ | |_) / _ \ |/ _ \ / _` |/ _` / __|
190 * | __/| | (_| | (_| __/ | _ < __/ | (_) | (_| | (_| \__ \
191 * |_| |_|\__,_|\___\___| |_| \_\___|_|\___/ \__,_|\__,_|___/
195 void be_add_spill(spill_env_t *env, ir_node *to_spill, ir_node *before)
197 spill_info_t *spill_info = get_spillinfo(env, to_spill);
202 DB((dbg, LEVEL_1, "Add spill of %+F before %+F\n", to_spill, before));
204 /* spills that are dominated by others are not needed */
206 s = spill_info->spills;
207 for( ; s != NULL; s = s->next) {
208 /* no need to add this spill if it is dominated by another */
209 if(value_dominates(s->before, before)) {
210 DB((dbg, LEVEL_1, "...dominated by %+F, not added\n", s->before));
213 /* remove spills that we dominate */
214 if(value_dominates(before, s->before)) {
215 DB((dbg, LEVEL_1, "...remove old spill at %+F\n", s->before));
217 last->next = s->next;
219 spill_info->spills = s->next;
226 spill = obstack_alloc(&env->obst, sizeof(spill[0]));
227 spill->before = before;
228 spill->next = spill_info->spills;
231 spill_info->spills = spill;
234 void be_add_remat(spill_env_t *env, ir_node *to_spill, ir_node *before,
235 ir_node *rematted_node)
237 spill_info_t *spill_info;
238 reloader_t *reloader;
240 spill_info = get_spillinfo(env, to_spill);
242 /* add the remat information */
243 reloader = obstack_alloc(&env->obst, sizeof(reloader[0]));
244 reloader->next = spill_info->reloaders;
245 reloader->reloader = before;
246 reloader->rematted_node = rematted_node;
247 reloader->remat_cost_delta = 0; /* We will never have a cost win over a
248 reload since we're not even allowed to
251 spill_info->reloaders = reloader;
253 DBG((dbg, LEVEL_1, "creating spillinfo for %+F, will be rematerialized before %+F\n",
257 void be_add_reload2(spill_env_t *env, ir_node *to_spill, ir_node *before,
258 ir_node *can_spill_after, const arch_register_class_t *reload_cls,
264 info = get_spillinfo(env, to_spill);
266 if (is_Phi(to_spill)) {
269 /* create spillinfos for the phi arguments */
270 for (i = 0, arity = get_irn_arity(to_spill); i < arity; ++i) {
271 ir_node *arg = get_irn_n(to_spill, i);
272 get_spillinfo(env, arg);
276 assert(!is_Proj(before) && !be_is_Keep(before));
278 /* put reload into list */
279 rel = obstack_alloc(&env->obst, sizeof(rel[0]));
280 rel->next = info->reloaders;
281 rel->reloader = before;
282 rel->rematted_node = NULL;
283 rel->can_spill_after = can_spill_after;
284 rel->remat_cost_delta = allow_remat ? 0 : REMAT_COST_INFINITE;
286 info->reloaders = rel;
287 assert(info->reload_cls == NULL || info->reload_cls == reload_cls);
288 info->reload_cls = reload_cls;
290 DBG((dbg, LEVEL_1, "creating spillinfo for %+F, will be reloaded before %+F, may%s be rematerialized\n",
291 to_spill, before, allow_remat ? "" : " not"));
294 void be_add_reload(spill_env_t *senv, ir_node *to_spill, ir_node *before,
295 const arch_register_class_t *reload_cls, int allow_remat)
297 be_add_reload2(senv, to_spill, before, to_spill, reload_cls, allow_remat);
301 ir_node *be_get_end_of_block_insertion_point(const ir_node *block)
303 ir_node *last = sched_last(block);
305 /* we might have keeps behind the jump... */
306 while(be_is_Keep(last)) {
307 last = sched_prev(last);
308 assert(!sched_is_end(last));
311 assert(is_cfop(last));
313 /* add the reload before the (cond-)jump */
318 * Returns the point at which you can insert a node that should be executed
319 * before block @p block when coming from pred @p pos.
321 static ir_node *get_block_insertion_point(ir_node *block, int pos)
325 /* simply add the reload to the beginning of the block if we only have 1
326 * predecessor. We don't need to check for phis as there can't be any in a
327 * block with only 1 pred. */
328 if(get_Block_n_cfgpreds(block) == 1) {
329 assert(!is_Phi(sched_first(block)));
330 return sched_first(block);
333 /* We have to reload the value in pred-block */
334 predblock = get_Block_cfgpred_block(block, pos);
335 return be_get_end_of_block_insertion_point(predblock);
338 void be_add_reload_at_end(spill_env_t *env, ir_node *to_spill,
339 const ir_node *block,
340 const arch_register_class_t *reload_cls,
343 ir_node *before = be_get_end_of_block_insertion_point(block);
344 be_add_reload(env, to_spill, before, reload_cls, allow_remat);
347 void be_add_reload_on_edge(spill_env_t *env, ir_node *to_spill, ir_node *block,
348 int pos, const arch_register_class_t *reload_cls,
351 ir_node *before = get_block_insertion_point(block, pos);
352 be_add_reload(env, to_spill, before, reload_cls, allow_remat);
355 void be_spill_phi(spill_env_t *env, ir_node *node)
361 assert(is_Phi(node));
363 ir_nodeset_insert(&env->mem_phis, node);
365 /* create spills for the phi arguments */
366 block = get_nodes_block(node);
367 spill = get_spillinfo(env, node);
368 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
369 ir_node *arg = get_irn_n(node, i);
370 ir_node *pred_block = get_Block_cfgpred_block(block, i);
371 ir_node *insert = be_get_end_of_block_insertion_point(pred_block);
372 //get_spillinfo(env, arg);
373 be_add_spill(env, arg, insert);
379 * / ___|_ __ ___ __ _| |_ ___ / ___| _ __ (_) | |___
380 * | | | '__/ _ \/ _` | __/ _ \ \___ \| '_ \| | | / __|
381 * | |___| | | __/ (_| | || __/ ___) | |_) | | | \__ \
382 * \____|_| \___|\__,_|\__\___| |____/| .__/|_|_|_|___/
386 static ir_node *skip_keeps_phis(ir_node *node)
388 node = sched_next(node);
389 while(is_Phi(node) || be_is_Keep(node)) {
390 node = sched_next(node);
395 static void determine_spill_costs(spill_env_t *env, spill_info_t *spillinfo);
400 * @param senv the spill environment
401 * @param irn the node that should be spilled
402 * @param ctx_irn an user of the spilled node
404 * @return a be_Spill node
406 static void spill_irn(spill_env_t *env, spill_info_t *spillinfo)
408 ir_node *to_spill = spillinfo->to_spill;
411 /* determine_spill_costs must have been run before */
412 assert(spillinfo->spill_costs >= 0);
414 /* some backends have virtual noreg/unknown nodes that are not scheduled
415 * and simply always available. */
416 if(!sched_is_scheduled(to_spill)) {
417 /* override spillinfos or create a new one */
418 spillinfo->spills->spill = new_NoMem();
419 DB((dbg, LEVEL_1, "don't spill %+F use NoMem\n", to_spill));
423 DBG((dbg, LEVEL_1, "spilling %+F ... ", to_spill));
424 spill = spillinfo->spills;
425 for( ; spill != NULL; spill = spill->next) {
426 ir_node *block = get_block(spill->before);
427 ir_node *before = spill->before;
429 /* place all spills before the reloads (as we can't guarantee the
430 * same order as the be_add_spill and be_add_reload calls */
431 while(be_is_Reload(sched_prev(before))) {
432 before = sched_prev(before);
435 spill->spill = be_spill(env->arch_env, block, to_spill);
436 sched_add_before(before, spill->spill);
437 DB((dbg, LEVEL_1, "\t%+F before %+F,", spill->spill, before));
438 #ifdef FIRM_STATISTICS
442 DBG((dbg, LEVEL_1, "\n"));
445 static void spill_node(spill_env_t *env, spill_info_t *spillinfo);
448 * If the first usage of a Phi result would be out of memory
449 * there is no sense in allocating a register for it.
450 * Thus we spill it and all its operands to the same spill slot.
451 * Therefore the phi/dataB becomes a phi/Memory
453 * @param senv the spill environment
454 * @param phi the Phi node that should be spilled
455 * @param ctx_irn an user of the spilled node
457 static void spill_phi(spill_env_t *env, spill_info_t *spillinfo)
459 ir_graph *irg = env->irg;
460 ir_node *phi = spillinfo->to_spill;
461 ir_node *block = get_nodes_block(phi);
469 assert(!get_opt_cse());
470 DBG((dbg, LEVEL_1, "spilling Phi %+F:\n", phi));
472 /* build a new PhiM */
473 arity = get_irn_arity(phi);
474 ins = alloca(sizeof(ir_node*) * arity);
475 unknown = new_r_Unknown(irg, mode_M);
476 for(i = 0; i < arity; ++i) {
480 /* override replace spills... */
481 spill = obstack_alloc(&env->obst, sizeof(spill[0]));
482 spill->before = skip_keeps_phis(phi);
483 spill->spill = new_r_Phi(irg, block, arity, ins, mode_M);
486 spillinfo->spills = spill;
487 #ifdef FIRM_STATISTICS
488 env->spilled_phi_count++;
491 for(i = 0; i < arity; ++i) {
492 ir_node *arg = get_irn_n(phi, i);
493 spill_info_t *arg_info = get_spillinfo(env, arg);
495 determine_spill_costs(env, arg_info);
496 spill_node(env, arg_info);
498 set_irn_n(spill->spill, i, arg_info->spills->spill);
500 DBG((dbg, LEVEL_1, "... done spilling Phi %+F, created PhiM %+F\n", phi,
507 * @param senv the spill environment
508 * @param to_spill the node that should be spilled
510 static void spill_node(spill_env_t *env, spill_info_t *spillinfo)
514 /* node is already spilled */
515 if(spillinfo->spills != NULL && spillinfo->spills->spill != NULL)
518 to_spill = spillinfo->to_spill;
520 if (is_Phi(to_spill) && ir_nodeset_contains(&env->mem_phis, to_spill)) {
521 spill_phi(env, spillinfo);
523 spill_irn(env, spillinfo);
530 * | _ \ ___ _ __ ___ __ _| |_ ___ _ __(_) __ _| (_)_______
531 * | |_) / _ \ '_ ` _ \ / _` | __/ _ \ '__| |/ _` | | |_ / _ \
532 * | _ < __/ | | | | | (_| | || __/ | | | (_| | | |/ / __/
533 * |_| \_\___|_| |_| |_|\__,_|\__\___|_| |_|\__,_|_|_/___\___|
538 * Tests whether value @p arg is available before node @p reloader
539 * @returns 1 if value is available, 0 otherwise
541 static int is_value_available(spill_env_t *env, const ir_node *arg,
542 const ir_node *reloader)
544 if(is_Unknown(arg) || arg == new_NoMem())
550 if(arg == get_irg_frame(env->irg))
553 /* hack for now (happens when command should be inserted at end of block) */
554 if(is_Block(reloader)) {
559 * Ignore registers are always available
561 if(arch_irn_is(env->arch_env, arg, ignore)) {
565 /* the following test does not work while spilling,
566 * because the liveness info is not adapted yet to the effects of the
567 * additional spills/reloads.
570 /* we want to remat before the insn reloader
571 * thus an arguments is alive if
572 * - it interferes with the reloaders result
573 * - or it is (last-) used by reloader itself
575 if (values_interfere(env->birg->lv, reloader, arg)) {
579 arity = get_irn_arity(reloader);
580 for (i = 0; i < arity; ++i) {
581 ir_node *rel_arg = get_irn_n(reloader, i);
591 * Checks whether the node can principally be rematerialized
593 static int is_remat_node(spill_env_t *env, const ir_node *node)
595 const arch_env_t *arch_env = env->arch_env;
597 assert(!be_is_Spill(node));
599 if(arch_irn_is(arch_env, node, rematerializable))
606 * Check if a node is rematerializable. This tests for the following conditions:
608 * - The node itself is rematerializable
609 * - All arguments of the node are available or also rematerialisable
610 * - The costs for the rematerialisation operation is less or equal a limit
612 * Returns the costs needed for rematerialisation or something
613 * >= REMAT_COST_INFINITE if remat is not possible.
615 static int check_remat_conditions_costs(spill_env_t *env,
616 const ir_node *spilled, const ir_node *reloader, int parentcosts)
622 if(!is_remat_node(env, spilled))
623 return REMAT_COST_INFINITE;
625 if(be_is_Reload(spilled)) {
628 costs += arch_get_op_estimated_cost(env->arch_env, spilled);
630 if(parentcosts + costs >= env->reload_cost + env->spill_cost) {
631 return REMAT_COST_INFINITE;
633 if(arch_irn_is(env->arch_env, spilled, modify_flags)) {
634 return REMAT_COST_INFINITE;
638 for(i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
639 ir_node *arg = get_irn_n(spilled, i);
641 if(is_value_available(env, arg, reloader))
644 /* we have to rematerialize the argument as well */
646 /* we only support rematerializing 1 argument at the moment,
647 * so that we don't have to care about register pressure
649 return REMAT_COST_INFINITE;
653 costs += check_remat_conditions_costs(env, arg, reloader,
654 parentcosts + costs);
655 if(parentcosts + costs >= env->reload_cost + env->spill_cost)
656 return REMAT_COST_INFINITE;
663 * Re-materialize a node.
665 * @param senv the spill environment
666 * @param spilled the node that was spilled
667 * @param reloader a irn that requires a reload
669 static ir_node *do_remat(spill_env_t *env, ir_node *spilled, ir_node *reloader)
676 if(is_Block(reloader)) {
679 bl = get_nodes_block(reloader);
682 ins = alloca(get_irn_arity(spilled) * sizeof(ins[0]));
683 for(i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
684 ir_node *arg = get_irn_n(spilled, i);
686 if(is_value_available(env, arg, reloader)) {
689 ins[i] = do_remat(env, arg, reloader);
690 #ifdef FIRM_STATISTICS
691 /* don't count the recursive call as remat */
697 /* create a copy of the node */
698 res = new_ir_node(get_irn_dbg_info(spilled), env->irg, bl,
699 get_irn_op(spilled), get_irn_mode(spilled),
700 get_irn_arity(spilled), ins);
701 copy_node_attr(spilled, res);
702 new_backedge_info(res);
704 DBG((dbg, LEVEL_1, "Insert remat %+F of %+F before reloader %+F\n", res, spilled, reloader));
706 if (! is_Proj(res)) {
707 /* insert in schedule */
709 sched_add_before(reloader, res);
710 #ifdef FIRM_STATISTICS
718 double be_get_spill_costs(spill_env_t *env, ir_node *to_spill, ir_node *before)
720 ir_node *block = get_nodes_block(before);
721 double freq = get_block_execfreq(env->exec_freq, block);
724 return env->spill_cost * freq;
727 double be_get_reload_costs(spill_env_t *env, ir_node *to_spill, ir_node *before)
729 ir_node *block = get_nodes_block(before);
730 double freq = get_block_execfreq(env->exec_freq, block);
733 /* is the node rematerializable? */
734 int costs = check_remat_conditions_costs(env, to_spill, before, 0);
735 if(costs < env->reload_cost)
739 return env->reload_cost * freq;
742 int be_is_rematerializable(spill_env_t *env, const ir_node *to_remat,
743 const ir_node *before)
745 return check_remat_conditions_costs(env, to_remat, before, 0) < REMAT_COST_INFINITE;
748 double be_get_reload_costs_on_edge(spill_env_t *env, ir_node *to_spill,
749 ir_node *block, int pos)
751 ir_node *before = get_block_insertion_point(block, pos);
752 return be_get_reload_costs(env, to_spill, before);
757 * |_ _|_ __ ___ ___ _ __| |_ | _ \ ___| | ___ __ _ __| |___
758 * | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ |/ _ \ / _` |/ _` / __|
759 * | || | | \__ \ __/ | | |_ | _ < __/ | (_) | (_| | (_| \__ \
760 * |___|_| |_|___/\___|_| \__| |_| \_\___|_|\___/ \__,_|\__,_|___/
765 * analyzes how to best spill a node and determine costs for that
767 static void determine_spill_costs(spill_env_t *env, spill_info_t *spillinfo)
769 ir_node *to_spill = spillinfo->to_spill;
770 ir_node *spill_block;
772 double spill_execfreq;
774 /* already calculated? */
775 if(spillinfo->spill_costs >= 0)
778 assert(! arch_irn_is(env->arch_env, to_spill, dont_spill));
779 assert(!be_is_Reload(to_spill));
781 /* some backends have virtual noreg/unknown nodes that are not scheduled
782 * and simply always available.
783 * TODO: this is kinda hairy, the NoMem is correct for an Unknown as Phi
784 * predecessor (of a PhiM) but this test might match other things too...
786 if(!sched_is_scheduled(to_spill)) {
787 /* override spillinfos or create a new one */
788 spill_t *spill = obstack_alloc(&env->obst, sizeof(spill[0]));
789 spill->before = NULL;
791 spill->spill = new_NoMem();
793 spillinfo->spills = spill;
794 spillinfo->spill_costs = 0;
796 DB((dbg, LEVEL_1, "don't spill %+F use NoMem\n", to_spill));
800 spill_block = get_nodes_block(to_spill);
801 spill_execfreq = get_block_execfreq(env->exec_freq, spill_block);
803 if (is_Phi(to_spill) && ir_nodeset_contains(&env->mem_phis, to_spill)) {
804 /* TODO calculate correct costs...
805 * (though we can't remat this node anyway so no big problem) */
806 spillinfo->spill_costs = env->spill_cost * spill_execfreq;
810 if(spillinfo->spills != NULL) {
812 double spills_execfreq;
814 /* calculate sum of executaion frequencies of individual spills */
816 s = spillinfo->spills;
817 for( ; s != NULL; s = s->next) {
818 ir_node *spill_block = s->before;
821 if(!is_Block(spill_block)) {
822 spill_block = get_nodes_block(spill_block);
824 freq = get_block_execfreq(env->exec_freq, spill_block);
826 spills_execfreq += freq;
829 DB((dbg, LEVEL_1, "%+F: latespillcosts %f after def: %f\n", to_spill,
830 spills_execfreq * env->spill_cost,
831 spill_execfreq * env->spill_cost));
833 /* multi-/latespill is advantageous -> return*/
834 if(spills_execfreq < spill_execfreq) {
835 DB((dbg, LEVEL_1, "use latespills for %+F\n", to_spill));
836 spillinfo->spill_costs = spills_execfreq * env->spill_cost;
841 /* override spillinfos or create a new one */
842 spill = obstack_alloc(&env->obst, sizeof(spill[0]));
843 spill->before = skip_keeps_phis(to_spill);
847 spillinfo->spills = spill;
848 spillinfo->spill_costs = spill_execfreq * env->spill_cost;
849 DB((dbg, LEVEL_1, "spill %+F after definition\n", to_spill));
852 void be_insert_spills_reloads(spill_env_t *env)
854 const arch_env_t *arch_env = env->arch_env;
855 const ir_exec_freq *exec_freq = env->exec_freq;
857 ir_nodeset_iterator_t iter;
860 /* create all phi-ms first, this is needed so, that phis, hanging on
861 spilled phis work correctly */
862 foreach_ir_nodeset(&env->mem_phis, node, iter) {
863 spill_info_t *info = get_spillinfo(env, node);
864 spill_node(env, info);
867 /* process each spilled node */
868 for (si = set_first(env->spills); si; si = set_next(env->spills)) {
870 ir_node *to_spill = si->to_spill;
871 ir_mode *mode = get_irn_mode(to_spill);
872 ir_node **copies = NEW_ARR_F(ir_node*, 0);
873 double all_remat_costs = 0; /** costs when we would remat all nodes */
876 DBG((dbg, LEVEL_1, "\nhandling all reloaders of %+F:\n", to_spill));
878 determine_spill_costs(env, si);
880 /* determine possibility of rematerialisations */
882 /* calculate cost savings for each indivial value when it would
883 be rematted instead of reloaded */
884 for (rld = si->reloaders; rld != NULL; rld = rld->next) {
887 int remat_cost_delta;
889 ir_node *reloader = rld->reloader;
891 if(rld->rematted_node != NULL) {
892 DBG((dbg, LEVEL_2, "\tforced remat %+F before %+F\n",
893 rld->rematted_node, reloader));
896 if(rld->remat_cost_delta >= REMAT_COST_INFINITE) {
897 DBG((dbg, LEVEL_2, "\treload before %+F is forbidden\n",
899 all_remat_costs = REMAT_COST_INFINITE;
903 remat_cost = check_remat_conditions_costs(env, to_spill,
905 if(remat_cost >= REMAT_COST_INFINITE) {
906 DBG((dbg, LEVEL_2, "\tremat before %+F not possible\n",
908 rld->remat_cost_delta = REMAT_COST_INFINITE;
909 all_remat_costs = REMAT_COST_INFINITE;
913 remat_cost_delta = remat_cost - env->reload_cost;
914 rld->remat_cost_delta = remat_cost_delta;
915 block = is_Block(reloader) ? reloader : get_nodes_block(reloader);
916 freq = get_block_execfreq(exec_freq, block);
917 all_remat_costs += remat_cost_delta * freq;
918 DBG((dbg, LEVEL_2, "\tremat costs delta before %+F: "
919 "%d (rel %f)\n", reloader, remat_cost_delta,
920 remat_cost_delta * freq));
922 if(all_remat_costs < REMAT_COST_INFINITE) {
923 /* we don't need the costs for the spill if we can remat
925 all_remat_costs -= si->spill_costs;
927 DBG((dbg, LEVEL_2, "\tspill costs %d (rel %f)\n",
928 env->spill_cost, si->spill_costs));
931 if(all_remat_costs < 0) {
932 DBG((dbg, LEVEL_1, "\nforcing remats of all reloaders (%f)\n",
938 /* go through all reloads for this spill */
939 for (rld = si->reloaders; rld != NULL; rld = rld->next) {
940 ir_node *copy; /* a reload is a "copy" of the original value */
942 if (rld->rematted_node != NULL) {
943 copy = rld->rematted_node;
944 sched_add_before(rld->reloader, copy);
945 } else if (be_do_remats &&
946 (force_remat || rld->remat_cost_delta < 0)) {
947 copy = do_remat(env, to_spill, rld->reloader);
949 /* make sure we have a spill */
952 /* create a reload, use the first spill for now SSA
953 * reconstruction for memory comes below */
954 assert(si->spills != NULL);
955 copy = be_reload(arch_env, si->reload_cls, rld->reloader, mode,
957 #ifdef FIRM_STATISTICS
962 DBG((dbg, LEVEL_1, " %+F of %+F before %+F\n",
963 copy, to_spill, rld->reloader));
964 ARR_APP1(ir_node*, copies, copy);
967 /* if we had any reloads or remats, then we need to reconstruct the
968 * SSA form for the spilled value */
969 if (ARR_LEN(copies) > 0) {
970 be_ssa_construction_env_t senv;
971 /* be_lv_t *lv = be_get_birg_liveness(env->birg); */
973 be_ssa_construction_init(&senv, env->birg);
974 be_ssa_construction_add_copy(&senv, to_spill);
975 be_ssa_construction_add_copies(&senv, copies, ARR_LEN(copies));
976 be_ssa_construction_fix_users(&senv, to_spill);
979 /* no need to enable this as long as we invalidate liveness
980 after this function... */
981 be_ssa_construction_update_liveness_phis(&senv);
982 be_liveness_update(to_spill);
983 len = ARR_LEN(copies);
984 for(i = 0; i < len; ++i) {
985 be_liveness_update(lv, copies[i]);
988 be_ssa_construction_destroy(&senv);
990 /* need to reconstruct SSA form if we had multiple spills */
991 if (si->spills != NULL && si->spills->next != NULL) {
995 be_ssa_construction_env_t senv;
997 be_ssa_construction_init(&senv, env->birg);
999 for( ; spill != NULL; spill = spill->next) {
1000 /* maybe we rematerialized the value and need no spill */
1001 if(spill->spill == NULL)
1003 be_ssa_construction_add_copy(&senv, spill->spill);
1006 if(spill_count > 1) {
1007 /* all reloads are attached to the first spill, fix them now */
1008 be_ssa_construction_fix_users(&senv, si->spills->spill);
1011 be_ssa_construction_destroy(&senv);
1015 si->reloaders = NULL;
1018 stat_ev_dbl("spill_spills", env->spill_count);
1019 stat_ev_dbl("spill_reloads", env->reload_count);
1020 stat_ev_dbl("spill_remats", env->remat_count);
1021 stat_ev_dbl("spill_spilled_phis", env->spilled_phi_count);
1023 /* Matze: In theory be_ssa_construction should take care of the liveness...
1024 * try to disable this again in the future */
1025 be_liveness_invalidate(env->birg->lv);
1027 be_remove_dead_nodes_from_schedule(env->birg);
1030 void be_init_spill(void)
1032 FIRM_DBG_REGISTER(dbg, "firm.be.spill");
1035 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spill);