2 * Copyright (C) 1995-2008 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
35 #include "iredges_t.h"
36 #include "irbackedge_t.h"
46 #include "irnodeset.h"
53 #include "bespillutil.h"
56 #include "bechordal_t.h"
57 #include "bestatevent.h"
58 #include "bessaconstr.h"
60 #include "beintlive_t.h"
64 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
66 #define REMAT_COST_INFINITE 1000
68 typedef struct reloader_t reloader_t;
71 ir_node *can_spill_after;
73 ir_node *rematted_node;
74 int remat_cost_delta; /** costs needed for rematerialization,
75 compared to placing a reload */
78 typedef struct spill_t spill_t;
81 ir_node *after; /**< spill has to be placed after this node (or earlier) */
85 typedef struct spill_info_t spill_info_t;
87 ir_node *to_spill; /**< the value that should get spilled */
88 reloader_t *reloaders; /**< list of places where the value should get
90 spill_t *spills; /**< list of latest places where spill must be
92 double spill_costs; /**< costs needed for spilling the value */
93 const arch_register_class_t *reload_cls; /** the register class in which the
94 reload should be placed */
98 const arch_env_t *arch_env;
102 int spill_cost; /**< the cost of a single spill node */
103 int reload_cost; /**< the cost of a reload node */
104 set *spills; /**< all spill_info_t's, which must be
106 ir_nodeset_t mem_phis; /**< set of all spilled phis. */
107 ir_exec_freq *exec_freq;
109 #ifdef FIRM_STATISTICS
110 unsigned spill_count;
111 unsigned reload_count;
112 unsigned remat_count;
113 unsigned spilled_phi_count;
118 * Compare two spill infos.
120 static int cmp_spillinfo(const void *x, const void *y, size_t size)
122 const spill_info_t *xx = x;
123 const spill_info_t *yy = y;
126 return xx->to_spill != yy->to_spill;
130 * Returns spill info for a specific value (the value that is to be spilled)
132 static spill_info_t *get_spillinfo(const spill_env_t *env, ir_node *value)
134 spill_info_t info, *res;
135 int hash = hash_irn(value);
137 info.to_spill = value;
138 res = set_find(env->spills, &info, sizeof(info), hash);
141 info.reloaders = NULL;
143 info.spill_costs = -1;
144 info.reload_cls = NULL;
145 res = set_insert(env->spills, &info, sizeof(info), hash);
151 spill_env_t *be_new_spill_env(be_irg_t *birg)
153 const arch_env_t *arch_env = birg->main_env->arch_env;
155 spill_env_t *env = XMALLOC(spill_env_t);
156 env->spills = new_set(cmp_spillinfo, 1024);
157 env->irg = be_get_birg_irg(birg);
159 env->arch_env = arch_env;
160 ir_nodeset_init(&env->mem_phis);
161 env->spill_cost = arch_env->spill_cost;
162 env->reload_cost = arch_env->reload_cost;
163 env->exec_freq = be_get_birg_exec_freq(birg);
164 obstack_init(&env->obst);
166 #ifdef FIRM_STATISTICS
167 env->spill_count = 0;
168 env->reload_count = 0;
169 env->remat_count = 0;
170 env->spilled_phi_count = 0;
176 void be_delete_spill_env(spill_env_t *env)
178 del_set(env->spills);
179 ir_nodeset_destroy(&env->mem_phis);
180 obstack_free(&env->obst, NULL);
186 * | _ \| | __ _ ___ ___ | _ \ ___| | ___ __ _ __| |___
187 * | |_) | |/ _` |/ __/ _ \ | |_) / _ \ |/ _ \ / _` |/ _` / __|
188 * | __/| | (_| | (_| __/ | _ < __/ | (_) | (_| | (_| \__ \
189 * |_| |_|\__,_|\___\___| |_| \_\___|_|\___/ \__,_|\__,_|___/
193 void be_add_spill(spill_env_t *env, ir_node *to_spill, ir_node *after)
195 spill_info_t *spill_info = get_spillinfo(env, to_spill);
200 assert(!arch_irn_is(skip_Proj_const(to_spill), dont_spill));
201 DB((dbg, LEVEL_1, "Add spill of %+F after %+F\n", to_spill, after));
203 /* Just for safety make sure that we do not insert the spill in front of a phi */
204 assert(!is_Phi(sched_next(after)));
206 /* spills that are dominated by others are not needed */
208 s = spill_info->spills;
209 for( ; s != NULL; s = s->next) {
210 /* no need to add this spill if it is dominated by another */
211 if(value_dominates(s->after, after)) {
212 DB((dbg, LEVEL_1, "...dominated by %+F, not added\n", s->after));
215 /* remove spills that we dominate */
216 if(value_dominates(after, s->after)) {
217 DB((dbg, LEVEL_1, "...remove old spill at %+F\n", s->after));
219 last->next = s->next;
221 spill_info->spills = s->next;
228 spill = obstack_alloc(&env->obst, sizeof(spill[0]));
229 spill->after = after;
230 spill->next = spill_info->spills;
233 spill_info->spills = spill;
236 void be_add_remat(spill_env_t *env, ir_node *to_spill, ir_node *before,
237 ir_node *rematted_node)
239 spill_info_t *spill_info;
240 reloader_t *reloader;
242 spill_info = get_spillinfo(env, to_spill);
244 /* add the remat information */
245 reloader = obstack_alloc(&env->obst, sizeof(reloader[0]));
246 reloader->next = spill_info->reloaders;
247 reloader->reloader = before;
248 reloader->rematted_node = rematted_node;
249 reloader->remat_cost_delta = 0; /* We will never have a cost win over a
250 reload since we're not even allowed to
253 spill_info->reloaders = reloader;
255 DBG((dbg, LEVEL_1, "creating spillinfo for %+F, will be rematerialized before %+F\n",
259 void be_add_reload2(spill_env_t *env, ir_node *to_spill, ir_node *before,
260 ir_node *can_spill_after, const arch_register_class_t *reload_cls,
266 assert(!arch_irn_is(skip_Proj_const(to_spill), dont_spill));
268 info = get_spillinfo(env, to_spill);
270 if (is_Phi(to_spill)) {
273 /* create spillinfos for the phi arguments */
274 for (i = 0, arity = get_irn_arity(to_spill); i < arity; ++i) {
275 ir_node *arg = get_irn_n(to_spill, i);
276 get_spillinfo(env, arg);
280 assert(!is_Proj(before) && !be_is_Keep(before));
282 /* put reload into list */
283 rel = obstack_alloc(&env->obst, sizeof(rel[0]));
284 rel->next = info->reloaders;
285 rel->reloader = before;
286 rel->rematted_node = NULL;
287 rel->can_spill_after = can_spill_after;
288 rel->remat_cost_delta = allow_remat ? 0 : REMAT_COST_INFINITE;
290 info->reloaders = rel;
291 assert(info->reload_cls == NULL || info->reload_cls == reload_cls);
292 info->reload_cls = reload_cls;
294 DBG((dbg, LEVEL_1, "creating spillinfo for %+F, will be reloaded before %+F, may%s be rematerialized\n",
295 to_spill, before, allow_remat ? "" : " not"));
298 void be_add_reload(spill_env_t *senv, ir_node *to_spill, ir_node *before,
299 const arch_register_class_t *reload_cls, int allow_remat)
301 be_add_reload2(senv, to_spill, before, to_spill, reload_cls, allow_remat);
305 ir_node *be_get_end_of_block_insertion_point(const ir_node *block)
307 ir_node *last = sched_last(block);
309 /* we might have keeps behind the jump... */
310 while (be_is_Keep(last)) {
311 last = sched_prev(last);
312 assert(!sched_is_end(last));
315 assert(is_cfop(last));
317 /* add the reload before the (cond-)jump */
321 static ir_node *skip_keeps_phis(ir_node *node)
324 ir_node *next = sched_next(node);
325 if(!is_Phi(next) && !be_is_Keep(next))
333 * Returns the point at which you can insert a node that should be executed
334 * before block @p block when coming from pred @p pos.
336 static ir_node *get_block_insertion_point(ir_node *block, int pos)
340 /* simply add the reload to the beginning of the block if we only have 1
341 * predecessor. We don't need to check for phis as there can't be any in a
342 * block with only 1 pred. */
343 if(get_Block_n_cfgpreds(block) == 1) {
344 assert(!is_Phi(sched_first(block)));
345 return sched_first(block);
348 /* We have to reload the value in pred-block */
349 predblock = get_Block_cfgpred_block(block, pos);
350 return be_get_end_of_block_insertion_point(predblock);
353 void be_add_reload_at_end(spill_env_t *env, ir_node *to_spill,
354 const ir_node *block,
355 const arch_register_class_t *reload_cls,
358 ir_node *before = be_get_end_of_block_insertion_point(block);
359 be_add_reload(env, to_spill, before, reload_cls, allow_remat);
362 void be_add_reload_on_edge(spill_env_t *env, ir_node *to_spill, ir_node *block,
363 int pos, const arch_register_class_t *reload_cls,
366 ir_node *before = get_block_insertion_point(block, pos);
367 be_add_reload(env, to_spill, before, reload_cls, allow_remat);
370 void be_spill_phi(spill_env_t *env, ir_node *node)
376 assert(is_Phi(node));
378 ir_nodeset_insert(&env->mem_phis, node);
380 /* create spills for the phi arguments */
381 block = get_nodes_block(node);
382 spill = get_spillinfo(env, node);
383 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
384 ir_node *arg = get_irn_n(node, i);
386 //get_spillinfo(env, arg);
388 /* some backends have virtual noreg/unknown nodes that are not scheduled
389 * and simply always available. */
390 if(!sched_is_scheduled(arg)) {
391 ir_node *pred_block = get_Block_cfgpred_block(block, i);
392 insert = be_get_end_of_block_insertion_point(pred_block);
393 insert = sched_prev(insert);
395 insert = skip_keeps_phis(arg);
398 be_add_spill(env, arg, insert);
404 * / ___|_ __ ___ __ _| |_ ___ / ___| _ __ (_) | |___
405 * | | | '__/ _ \/ _` | __/ _ \ \___ \| '_ \| | | / __|
406 * | |___| | | __/ (_| | || __/ ___) | |_) | | | \__ \
407 * \____|_| \___|\__,_|\__\___| |____/| .__/|_|_|_|___/
411 static void determine_spill_costs(spill_env_t *env, spill_info_t *spillinfo);
416 * @param senv the spill environment
417 * @param irn the node that should be spilled
418 * @param ctx_irn an user of the spilled node
420 * @return a be_Spill node
422 static void spill_irn(spill_env_t *env, spill_info_t *spillinfo)
424 ir_node *to_spill = spillinfo->to_spill;
427 /* determine_spill_costs must have been run before */
428 assert(spillinfo->spill_costs >= 0);
430 /* some backends have virtual noreg/unknown nodes that are not scheduled
431 * and simply always available. */
432 if(!sched_is_scheduled(to_spill)) {
433 /* override spillinfos or create a new one */
434 spillinfo->spills->spill = new_NoMem();
435 DB((dbg, LEVEL_1, "don't spill %+F use NoMem\n", to_spill));
439 DBG((dbg, LEVEL_1, "spilling %+F ... \n", to_spill));
440 spill = spillinfo->spills;
441 for( ; spill != NULL; spill = spill->next) {
442 ir_node *after = spill->after;
443 ir_node *block = get_block(after);
445 after = skip_keeps_phis(after);
447 spill->spill = be_spill(block, to_spill);
448 sched_add_after(skip_Proj(after), spill->spill);
449 DB((dbg, LEVEL_1, "\t%+F after %+F\n", spill->spill, after));
450 #ifdef FIRM_STATISTICS
454 DBG((dbg, LEVEL_1, "\n"));
457 static void spill_node(spill_env_t *env, spill_info_t *spillinfo);
460 * If the first usage of a Phi result would be out of memory
461 * there is no sense in allocating a register for it.
462 * Thus we spill it and all its operands to the same spill slot.
463 * Therefore the phi/dataB becomes a phi/Memory
465 * @param senv the spill environment
466 * @param phi the Phi node that should be spilled
467 * @param ctx_irn an user of the spilled node
469 static void spill_phi(spill_env_t *env, spill_info_t *spillinfo)
471 ir_graph *irg = env->irg;
472 ir_node *phi = spillinfo->to_spill;
473 ir_node *block = get_nodes_block(phi);
481 assert(!get_opt_cse());
482 DBG((dbg, LEVEL_1, "spilling Phi %+F:\n", phi));
484 /* build a new PhiM */
485 arity = get_irn_arity(phi);
486 ins = ALLOCAN(ir_node*, arity);
487 unknown = new_r_Unknown(irg, mode_M);
488 for(i = 0; i < arity; ++i) {
492 /* override or replace spills list... */
493 spill = obstack_alloc(&env->obst, sizeof(spill[0]));
494 spill->after = skip_keeps_phis(phi);
495 spill->spill = new_r_Phi(block, arity, ins, mode_M);
498 spillinfo->spills = spill;
499 #ifdef FIRM_STATISTICS
500 env->spilled_phi_count++;
503 for(i = 0; i < arity; ++i) {
504 ir_node *arg = get_irn_n(phi, i);
505 spill_info_t *arg_info = get_spillinfo(env, arg);
507 determine_spill_costs(env, arg_info);
508 spill_node(env, arg_info);
510 set_irn_n(spill->spill, i, arg_info->spills->spill);
512 DBG((dbg, LEVEL_1, "... done spilling Phi %+F, created PhiM %+F\n", phi,
519 * @param senv the spill environment
520 * @param to_spill the node that should be spilled
522 static void spill_node(spill_env_t *env, spill_info_t *spillinfo)
526 /* node is already spilled */
527 if(spillinfo->spills != NULL && spillinfo->spills->spill != NULL)
530 to_spill = spillinfo->to_spill;
532 if (is_Phi(to_spill) && ir_nodeset_contains(&env->mem_phis, to_spill)) {
533 spill_phi(env, spillinfo);
535 spill_irn(env, spillinfo);
542 * | _ \ ___ _ __ ___ __ _| |_ ___ _ __(_) __ _| (_)_______
543 * | |_) / _ \ '_ ` _ \ / _` | __/ _ \ '__| |/ _` | | |_ / _ \
544 * | _ < __/ | | | | | (_| | || __/ | | | (_| | | |/ / __/
545 * |_| \_\___|_| |_| |_|\__,_|\__\___|_| |_|\__,_|_|_/___\___|
550 * Tests whether value @p arg is available before node @p reloader
551 * @returns 1 if value is available, 0 otherwise
553 static int is_value_available(spill_env_t *env, const ir_node *arg,
554 const ir_node *reloader)
556 if(is_Unknown(arg) || arg == new_NoMem())
562 if(arg == get_irg_frame(env->irg))
566 /* hack for now (happens when command should be inserted at end of block) */
567 if(is_Block(reloader))
574 * Ignore registers are always available
576 if (arch_irn_is_ignore(arg))
583 * Checks whether the node can principally be rematerialized
585 static int is_remat_node(const ir_node *node)
587 assert(!be_is_Spill(node));
588 return arch_irn_is(skip_Proj_const(node), rematerializable);
592 * Check if a node is rematerializable. This tests for the following conditions:
594 * - The node itself is rematerializable
595 * - All arguments of the node are available or also rematerialisable
596 * - The costs for the rematerialisation operation is less or equal a limit
598 * Returns the costs needed for rematerialisation or something
599 * >= REMAT_COST_INFINITE if remat is not possible.
601 static int check_remat_conditions_costs(spill_env_t *env,
602 const ir_node *spilled, const ir_node *reloader, int parentcosts)
608 if (!is_remat_node(spilled))
609 return REMAT_COST_INFINITE;
611 if(be_is_Reload(spilled)) {
614 costs += arch_get_op_estimated_cost(spilled);
616 if(parentcosts + costs >= env->reload_cost + env->spill_cost) {
617 return REMAT_COST_INFINITE;
619 /* never rematerialize a node which modifies the flags.
620 * (would be better to test wether the flags are actually live at point
623 if (arch_irn_is(skip_Proj_const(spilled), modify_flags)) {
624 return REMAT_COST_INFINITE;
628 for(i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
629 ir_node *arg = get_irn_n(spilled, i);
631 if(is_value_available(env, arg, reloader))
634 /* we have to rematerialize the argument as well */
636 /* we only support rematerializing 1 argument at the moment,
637 * so that we don't have to care about register pressure
639 return REMAT_COST_INFINITE;
643 costs += check_remat_conditions_costs(env, arg, reloader,
644 parentcosts + costs);
645 if(parentcosts + costs >= env->reload_cost + env->spill_cost)
646 return REMAT_COST_INFINITE;
653 * Re-materialize a node.
655 * @param senv the spill environment
656 * @param spilled the node that was spilled
657 * @param reloader a irn that requires a reload
659 static ir_node *do_remat(spill_env_t *env, ir_node *spilled, ir_node *reloader)
666 if(is_Block(reloader)) {
669 bl = get_nodes_block(reloader);
672 ins = ALLOCAN(ir_node*, get_irn_arity(spilled));
673 for(i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
674 ir_node *arg = get_irn_n(spilled, i);
676 if(is_value_available(env, arg, reloader)) {
679 ins[i] = do_remat(env, arg, reloader);
680 #ifdef FIRM_STATISTICS
681 /* don't count the recursive call as remat */
687 /* create a copy of the node */
688 res = new_ir_node(get_irn_dbg_info(spilled), env->irg, bl,
689 get_irn_op(spilled), get_irn_mode(spilled),
690 get_irn_arity(spilled), ins);
691 copy_node_attr(spilled, res);
692 arch_env_mark_remat(env->arch_env, res);
693 new_backedge_info(res);
695 DBG((dbg, LEVEL_1, "Insert remat %+F of %+F before reloader %+F\n", res, spilled, reloader));
697 if (! is_Proj(res)) {
698 /* insert in schedule */
700 sched_add_before(reloader, res);
701 #ifdef FIRM_STATISTICS
709 double be_get_spill_costs(spill_env_t *env, ir_node *to_spill, ir_node *before)
711 ir_node *block = get_nodes_block(before);
712 double freq = get_block_execfreq(env->exec_freq, block);
715 return env->spill_cost * freq;
718 unsigned be_get_reload_costs_no_weight(spill_env_t *env, const ir_node *to_spill,
719 const ir_node *before)
722 /* is the node rematerializable? */
723 unsigned costs = check_remat_conditions_costs(env, to_spill, before, 0);
724 if(costs < (unsigned) env->reload_cost)
728 return env->reload_cost;
731 double be_get_reload_costs(spill_env_t *env, ir_node *to_spill, ir_node *before)
733 ir_node *block = get_nodes_block(before);
734 double freq = get_block_execfreq(env->exec_freq, block);
737 /* is the node rematerializable? */
738 int costs = check_remat_conditions_costs(env, to_spill, before, 0);
739 if(costs < env->reload_cost)
743 return env->reload_cost * freq;
746 int be_is_rematerializable(spill_env_t *env, const ir_node *to_remat,
747 const ir_node *before)
749 return check_remat_conditions_costs(env, to_remat, before, 0) < REMAT_COST_INFINITE;
752 double be_get_reload_costs_on_edge(spill_env_t *env, ir_node *to_spill,
753 ir_node *block, int pos)
755 ir_node *before = get_block_insertion_point(block, pos);
756 return be_get_reload_costs(env, to_spill, before);
761 * |_ _|_ __ ___ ___ _ __| |_ | _ \ ___| | ___ __ _ __| |___
762 * | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ |/ _ \ / _` |/ _` / __|
763 * | || | | \__ \ __/ | | |_ | _ < __/ | (_) | (_| | (_| \__ \
764 * |___|_| |_|___/\___|_| \__| |_| \_\___|_|\___/ \__,_|\__,_|___/
769 * analyzes how to best spill a node and determine costs for that
771 static void determine_spill_costs(spill_env_t *env, spill_info_t *spillinfo)
773 ir_node *to_spill = spillinfo->to_spill;
774 ir_node *spill_block;
776 double spill_execfreq;
778 /* already calculated? */
779 if(spillinfo->spill_costs >= 0)
782 assert(!arch_irn_is(skip_Proj_const(to_spill), dont_spill));
783 assert(!be_is_Reload(to_spill));
785 /* some backends have virtual noreg/unknown nodes that are not scheduled
786 * and simply always available.
787 * TODO: this is kinda hairy, the NoMem is correct for an Unknown as Phi
788 * predecessor (of a PhiM) but this test might match other things too...
790 if(!sched_is_scheduled(to_spill)) {
791 /* override spillinfos or create a new one */
792 spill_t *spill = obstack_alloc(&env->obst, sizeof(spill[0]));
795 spill->spill = new_NoMem();
797 spillinfo->spills = spill;
798 spillinfo->spill_costs = 0;
800 DB((dbg, LEVEL_1, "don't spill %+F use NoMem\n", to_spill));
804 spill_block = get_nodes_block(to_spill);
805 spill_execfreq = get_block_execfreq(env->exec_freq, spill_block);
807 if (is_Phi(to_spill) && ir_nodeset_contains(&env->mem_phis, to_spill)) {
808 /* TODO calculate correct costs...
809 * (though we can't remat this node anyway so no big problem) */
810 spillinfo->spill_costs = env->spill_cost * spill_execfreq;
814 if(spillinfo->spills != NULL) {
816 double spills_execfreq;
818 /* calculate sum of execution frequencies of individual spills */
820 s = spillinfo->spills;
821 for( ; s != NULL; s = s->next) {
822 ir_node *spill_block = get_block(s->after);
823 double freq = get_block_execfreq(env->exec_freq, spill_block);
825 spills_execfreq += freq;
828 DB((dbg, LEVEL_1, "%+F: latespillcosts %f after def: %f\n", to_spill,
829 spills_execfreq * env->spill_cost,
830 spill_execfreq * env->spill_cost));
832 /* multi-/latespill is advantageous -> return*/
833 if(spills_execfreq < spill_execfreq) {
834 DB((dbg, LEVEL_1, "use latespills for %+F\n", to_spill));
835 spillinfo->spill_costs = spills_execfreq * env->spill_cost;
840 /* override spillinfos or create a new one */
841 spill = obstack_alloc(&env->obst, sizeof(spill[0]));
842 spill->after = skip_keeps_phis(to_spill);
846 spillinfo->spills = spill;
847 spillinfo->spill_costs = spill_execfreq * env->spill_cost;
848 DB((dbg, LEVEL_1, "spill %+F after definition\n", to_spill));
851 void make_spill_locations_dominate_irn(spill_env_t *env, ir_node *irn)
853 const spill_info_t *si = get_spillinfo(env, irn);
854 ir_node *start_block = get_irg_start_block(get_irn_irg(irn));
855 int n_blocks = get_Block_dom_max_subtree_pre_num(start_block);
856 bitset_t *reloads = bitset_alloca(n_blocks);
863 /* Fill the bitset with the dominance pre-order numbers
864 * of the blocks the reloads are located in. */
865 for (r = si->reloaders; r != NULL; r = r->next) {
866 ir_node *bl = get_nodes_block(r->reloader);
867 bitset_set(reloads, get_Block_dom_tree_pre_num(bl));
870 /* Now, cancel out all the blocks that are dominated by each spill.
871 * If the bitset is not empty after that, we have reloads that are
872 * not dominated by any spill. */
873 for (s = si->spills; s != NULL; s = s->next) {
874 ir_node *bl = get_nodes_block(s->after);
875 int start = get_Block_dom_tree_pre_num(bl);
876 int end = get_Block_dom_max_subtree_pre_num(bl);
878 bitset_clear_range(reloads, start, end);
881 if (!bitset_is_empty(reloads))
882 be_add_spill(env, si->to_spill, si->to_spill);
885 void be_insert_spills_reloads(spill_env_t *env)
887 const ir_exec_freq *exec_freq = env->exec_freq;
889 ir_nodeset_iterator_t iter;
892 BE_TIMER_PUSH(t_ra_spill_apply);
894 /* create all phi-ms first, this is needed so, that phis, hanging on
895 spilled phis work correctly */
896 foreach_ir_nodeset(&env->mem_phis, node, iter) {
897 spill_info_t *info = get_spillinfo(env, node);
898 spill_node(env, info);
901 /* process each spilled node */
902 for (si = set_first(env->spills); si; si = set_next(env->spills)) {
904 ir_node *to_spill = si->to_spill;
905 ir_mode *mode = get_irn_mode(to_spill);
906 ir_node **copies = NEW_ARR_F(ir_node*, 0);
907 double all_remat_costs = 0; /** costs when we would remat all nodes */
910 DBG((dbg, LEVEL_1, "\nhandling all reloaders of %+F:\n", to_spill));
912 determine_spill_costs(env, si);
914 /* determine possibility of rematerialisations */
916 /* calculate cost savings for each indivial value when it would
917 be rematted instead of reloaded */
918 for (rld = si->reloaders; rld != NULL; rld = rld->next) {
921 int remat_cost_delta;
923 ir_node *reloader = rld->reloader;
925 if(rld->rematted_node != NULL) {
926 DBG((dbg, LEVEL_2, "\tforced remat %+F before %+F\n",
927 rld->rematted_node, reloader));
930 if(rld->remat_cost_delta >= REMAT_COST_INFINITE) {
931 DBG((dbg, LEVEL_2, "\treload before %+F is forbidden\n",
933 all_remat_costs = REMAT_COST_INFINITE;
937 remat_cost = check_remat_conditions_costs(env, to_spill,
939 if(remat_cost >= REMAT_COST_INFINITE) {
940 DBG((dbg, LEVEL_2, "\tremat before %+F not possible\n",
942 rld->remat_cost_delta = REMAT_COST_INFINITE;
943 all_remat_costs = REMAT_COST_INFINITE;
947 remat_cost_delta = remat_cost - env->reload_cost;
948 rld->remat_cost_delta = remat_cost_delta;
949 block = is_Block(reloader) ? reloader : get_nodes_block(reloader);
950 freq = get_block_execfreq(exec_freq, block);
951 all_remat_costs += remat_cost_delta * freq;
952 DBG((dbg, LEVEL_2, "\tremat costs delta before %+F: "
953 "%d (rel %f)\n", reloader, remat_cost_delta,
954 remat_cost_delta * freq));
956 if(all_remat_costs < REMAT_COST_INFINITE) {
957 /* we don't need the costs for the spill if we can remat
959 all_remat_costs -= si->spill_costs;
961 DBG((dbg, LEVEL_2, "\tspill costs %d (rel %f)\n",
962 env->spill_cost, si->spill_costs));
965 if(all_remat_costs < 0) {
966 DBG((dbg, LEVEL_1, "\nforcing remats of all reloaders (%f)\n",
972 /* go through all reloads for this spill */
973 for (rld = si->reloaders; rld != NULL; rld = rld->next) {
974 ir_node *copy; /* a reload is a "copy" of the original value */
976 if (rld->rematted_node != NULL) {
977 copy = rld->rematted_node;
978 sched_add_before(rld->reloader, copy);
979 } else if (be_do_remats &&
980 (force_remat || rld->remat_cost_delta < 0)) {
981 copy = do_remat(env, to_spill, rld->reloader);
983 /* make sure we have a spill */
986 /* create a reload, use the first spill for now SSA
987 * reconstruction for memory comes below */
988 assert(si->spills != NULL);
989 copy = be_reload(si->reload_cls, rld->reloader, mode,
991 #ifdef FIRM_STATISTICS
996 DBG((dbg, LEVEL_1, " %+F of %+F before %+F\n",
997 copy, to_spill, rld->reloader));
998 ARR_APP1(ir_node*, copies, copy);
1001 /* if we had any reloads or remats, then we need to reconstruct the
1002 * SSA form for the spilled value */
1003 if (ARR_LEN(copies) > 0) {
1004 be_ssa_construction_env_t senv;
1005 /* be_lv_t *lv = be_get_birg_liveness(env->birg); */
1007 be_ssa_construction_init(&senv, env->birg);
1008 be_ssa_construction_add_copy(&senv, to_spill);
1009 be_ssa_construction_add_copies(&senv, copies, ARR_LEN(copies));
1010 be_ssa_construction_fix_users(&senv, to_spill);
1013 /* no need to enable this as long as we invalidate liveness
1014 after this function... */
1015 be_ssa_construction_update_liveness_phis(&senv);
1016 be_liveness_update(to_spill);
1017 len = ARR_LEN(copies);
1018 for(i = 0; i < len; ++i) {
1019 be_liveness_update(lv, copies[i]);
1022 be_ssa_construction_destroy(&senv);
1024 /* need to reconstruct SSA form if we had multiple spills */
1025 if (si->spills != NULL && si->spills->next != NULL) {
1027 int spill_count = 0;
1029 be_ssa_construction_env_t senv;
1031 be_ssa_construction_init(&senv, env->birg);
1033 for( ; spill != NULL; spill = spill->next) {
1034 /* maybe we rematerialized the value and need no spill */
1035 if(spill->spill == NULL)
1037 be_ssa_construction_add_copy(&senv, spill->spill);
1040 if(spill_count > 1) {
1041 /* all reloads are attached to the first spill, fix them now */
1042 be_ssa_construction_fix_users(&senv, si->spills->spill);
1045 be_ssa_construction_destroy(&senv);
1049 si->reloaders = NULL;
1052 stat_ev_dbl("spill_spills", env->spill_count);
1053 stat_ev_dbl("spill_reloads", env->reload_count);
1054 stat_ev_dbl("spill_remats", env->remat_count);
1055 stat_ev_dbl("spill_spilled_phis", env->spilled_phi_count);
1057 /* Matze: In theory be_ssa_construction should take care of the liveness...
1058 * try to disable this again in the future */
1059 be_liveness_invalidate(env->birg->lv);
1061 be_remove_dead_nodes_from_schedule(env->birg);
1063 BE_TIMER_POP(t_ra_spill_apply);
1066 void be_init_spill(void)
1068 FIRM_DBG_REGISTER(dbg, "firm.be.spill");
1071 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spill);