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
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 "bespilloptions.h"
59 #include "bestatevent.h"
60 #include "bessaconstr.h"
62 #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;
110 unsigned new_nodes_idx; /**< all old nodes idx is smaller than
113 #ifdef FIRM_STATISTICS
114 unsigned spill_count;
115 unsigned reload_count;
116 unsigned remat_count;
117 unsigned spilled_phi_count;
122 * Compare two spill infos.
124 static int cmp_spillinfo(const void *x, const void *y, size_t size)
126 const spill_info_t *xx = x;
127 const spill_info_t *yy = y;
130 return xx->to_spill != yy->to_spill;
134 * Returns spill info for a specific value (the value that is to be spilled)
136 static spill_info_t *get_spillinfo(const spill_env_t *env, ir_node *value)
138 spill_info_t info, *res;
139 int hash = hash_irn(value);
141 info.to_spill = value;
142 res = set_find(env->spills, &info, sizeof(info), hash);
145 info.reloaders = NULL;
147 info.spill_costs = -1;
148 info.reload_cls = NULL;
149 res = set_insert(env->spills, &info, sizeof(info), hash);
155 spill_env_t *be_new_spill_env(be_irg_t *birg)
157 const arch_env_t *arch_env = birg->main_env->arch_env;
159 spill_env_t *env = xmalloc(sizeof(env[0]));
160 env->spills = new_set(cmp_spillinfo, 1024);
161 env->irg = be_get_birg_irg(birg);
163 env->arch_env = arch_env;
164 ir_nodeset_init(&env->mem_phis);
165 env->spill_cost = arch_env->spill_cost;
166 env->reload_cost = arch_env->reload_cost;
167 env->exec_freq = be_get_birg_exec_freq(birg);
168 obstack_init(&env->obst);
170 #ifdef FIRM_STATISTICS
171 env->spill_count = 0;
172 env->reload_count = 0;
173 env->remat_count = 0;
174 env->spilled_phi_count = 0;
180 void be_delete_spill_env(spill_env_t *env)
182 del_set(env->spills);
183 ir_nodeset_destroy(&env->mem_phis);
184 obstack_free(&env->obst, NULL);
190 * | _ \| | __ _ ___ ___ | _ \ ___| | ___ __ _ __| |___
191 * | |_) | |/ _` |/ __/ _ \ | |_) / _ \ |/ _ \ / _` |/ _` / __|
192 * | __/| | (_| | (_| __/ | _ < __/ | (_) | (_| | (_| \__ \
193 * |_| |_|\__,_|\___\___| |_| \_\___|_|\___/ \__,_|\__,_|___/
197 void be_add_spill(spill_env_t *env, ir_node *to_spill, ir_node *before)
200 spill_info_t *spill_info = get_spillinfo(env, to_spill);
205 assert(! arch_irn_is(env->arch_env, to_spill, dont_spill));
206 DB((dbg, LEVEL_1, "Add spill of %+F before %+F\n", to_spill, before));
208 /* Just for safety make sure that we do not insert the spill in front of a phi */
209 for (; is_Phi(before); before = sched_next(before));
211 /* spills that are dominated by others are not needed */
213 s = spill_info->spills;
214 for( ; s != NULL; s = s->next) {
215 /* no need to add this spill if it is dominated by another */
216 if(value_dominates(s->before, before)) {
217 DB((dbg, LEVEL_1, "...dominated by %+F, not added\n", s->before));
220 /* remove spills that we dominate */
221 if(value_dominates(before, s->before)) {
222 DB((dbg, LEVEL_1, "...remove old spill at %+F\n", s->before));
224 last->next = s->next;
226 spill_info->spills = s->next;
233 spill = obstack_alloc(&env->obst, sizeof(spill[0]));
234 spill->before = before;
235 spill->next = spill_info->spills;
238 spill_info->spills = spill;
242 void be_add_remat(spill_env_t *env, ir_node *to_spill, ir_node *before,
243 ir_node *rematted_node)
245 spill_info_t *spill_info;
246 reloader_t *reloader;
248 spill_info = get_spillinfo(env, to_spill);
250 /* add the remat information */
251 reloader = obstack_alloc(&env->obst, sizeof(reloader[0]));
252 reloader->next = spill_info->reloaders;
253 reloader->reloader = before;
254 reloader->rematted_node = rematted_node;
255 reloader->remat_cost_delta = 0; /* We will never have a cost win over a
256 reload since we're not even allowed to
259 spill_info->reloaders = reloader;
261 DBG((dbg, LEVEL_1, "creating spillinfo for %+F, will be rematerialized before %+F\n",
265 void be_add_reload2(spill_env_t *env, ir_node *to_spill, ir_node *before,
266 ir_node *can_spill_after, const arch_register_class_t *reload_cls,
272 assert(! arch_irn_is(env->arch_env, to_spill, dont_spill));
274 info = get_spillinfo(env, to_spill);
276 if (is_Phi(to_spill)) {
279 /* create spillinfos for the phi arguments */
280 for (i = 0, arity = get_irn_arity(to_spill); i < arity; ++i) {
281 ir_node *arg = get_irn_n(to_spill, i);
282 get_spillinfo(env, arg);
286 assert(!is_Proj(before) && !be_is_Keep(before));
288 /* put reload into list */
289 rel = obstack_alloc(&env->obst, sizeof(rel[0]));
290 rel->next = info->reloaders;
291 rel->reloader = before;
292 rel->rematted_node = NULL;
293 rel->can_spill_after = can_spill_after;
294 rel->remat_cost_delta = allow_remat ? 0 : REMAT_COST_INFINITE;
296 info->reloaders = rel;
297 assert(info->reload_cls == NULL || info->reload_cls == reload_cls);
298 info->reload_cls = reload_cls;
300 DBG((dbg, LEVEL_1, "creating spillinfo for %+F, will be reloaded before %+F, may%s be rematerialized\n",
301 to_spill, before, allow_remat ? "" : " not"));
304 void be_add_reload(spill_env_t *senv, ir_node *to_spill, ir_node *before,
305 const arch_register_class_t *reload_cls, int allow_remat)
307 be_add_reload2(senv, to_spill, before, to_spill, reload_cls, allow_remat);
311 ir_node *be_get_end_of_block_insertion_point(const ir_node *block)
313 ir_node *last = sched_last(block);
315 /* we might have keeps behind the jump... */
316 while(be_is_Keep(last)) {
317 last = sched_prev(last);
318 assert(!sched_is_end(last));
321 assert(is_cfop(last));
323 /* add the reload before the (cond-)jump */
327 static ir_node *skip_keeps_phis(ir_node *node)
329 node = sched_next(node);
330 while(is_Phi(node) || be_is_Keep(node)) {
331 node = sched_next(node);
337 * Returns the point at which you can insert a node that should be executed
338 * before block @p block when coming from pred @p pos.
340 static ir_node *get_block_insertion_point(ir_node *block, int pos)
344 /* simply add the reload to the beginning of the block if we only have 1
345 * predecessor. We don't need to check for phis as there can't be any in a
346 * block with only 1 pred. */
347 if(get_Block_n_cfgpreds(block) == 1) {
348 assert(!is_Phi(sched_first(block)));
349 return sched_first(block);
352 /* We have to reload the value in pred-block */
353 predblock = get_Block_cfgpred_block(block, pos);
354 return be_get_end_of_block_insertion_point(predblock);
357 void be_add_reload_at_end(spill_env_t *env, ir_node *to_spill,
358 const ir_node *block,
359 const arch_register_class_t *reload_cls,
362 ir_node *before = be_get_end_of_block_insertion_point(block);
363 be_add_reload(env, to_spill, before, reload_cls, allow_remat);
366 void be_add_reload_on_edge(spill_env_t *env, ir_node *to_spill, ir_node *block,
367 int pos, const arch_register_class_t *reload_cls,
370 ir_node *before = get_block_insertion_point(block, pos);
371 be_add_reload(env, to_spill, before, reload_cls, allow_remat);
374 void be_spill_phi(spill_env_t *env, ir_node *node)
380 assert(is_Phi(node));
382 ir_nodeset_insert(&env->mem_phis, node);
384 /* create spills for the phi arguments */
385 block = get_nodes_block(node);
386 spill = get_spillinfo(env, node);
387 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
388 ir_node *arg = get_irn_n(node, i);
390 //get_spillinfo(env, arg);
392 /* some backends have virtual noreg/unknown nodes that are not scheduled
393 * and simply always available. */
394 if(!sched_is_scheduled(arg)) {
395 ir_node *pred_block = get_Block_cfgpred_block(block, i);
396 insert = be_get_end_of_block_insertion_point(pred_block);
398 insert = skip_keeps_phis(arg);
401 be_add_spill(env, arg, insert);
407 * / ___|_ __ ___ __ _| |_ ___ / ___| _ __ (_) | |___
408 * | | | '__/ _ \/ _` | __/ _ \ \___ \| '_ \| | | / __|
409 * | |___| | | __/ (_| | || __/ ___) | |_) | | | \__ \
410 * \____|_| \___|\__,_|\__\___| |____/| .__/|_|_|_|___/
414 static void determine_spill_costs(spill_env_t *env, spill_info_t *spillinfo);
419 * @param senv the spill environment
420 * @param irn the node that should be spilled
421 * @param ctx_irn an user of the spilled node
423 * @return a be_Spill node
425 static void spill_irn(spill_env_t *env, spill_info_t *spillinfo)
427 ir_node *to_spill = spillinfo->to_spill;
430 /* determine_spill_costs must have been run before */
431 assert(spillinfo->spill_costs >= 0);
433 /* some backends have virtual noreg/unknown nodes that are not scheduled
434 * and simply always available. */
435 if(!sched_is_scheduled(to_spill)) {
436 /* override spillinfos or create a new one */
437 spillinfo->spills->spill = new_NoMem();
438 DB((dbg, LEVEL_1, "don't spill %+F use NoMem\n", to_spill));
442 DBG((dbg, LEVEL_1, "spilling %+F ... \n", to_spill));
443 spill = spillinfo->spills;
444 for( ; spill != NULL; spill = spill->next) {
445 ir_node *block = get_block(spill->before);
446 ir_node *before = spill->before;
448 /* place all spills before the reloads (as we can't guarantee the
449 * same order as the be_add_spill and be_add_reload calls.
450 * Also make sure that we do not run into Phis when going up. */
451 while(get_irn_idx(sched_prev(before)) > env->new_nodes_idx && !is_Phi(sched_prev(before))) {
452 before = sched_prev(before);
455 spill->spill = be_spill(env->arch_env, block, to_spill);
456 sched_add_before(before, spill->spill);
457 DB((dbg, LEVEL_1, "\t%+F before %+F\n", spill->spill, before));
458 #ifdef FIRM_STATISTICS
462 DBG((dbg, LEVEL_1, "\n"));
465 static void spill_node(spill_env_t *env, spill_info_t *spillinfo);
468 * If the first usage of a Phi result would be out of memory
469 * there is no sense in allocating a register for it.
470 * Thus we spill it and all its operands to the same spill slot.
471 * Therefore the phi/dataB becomes a phi/Memory
473 * @param senv the spill environment
474 * @param phi the Phi node that should be spilled
475 * @param ctx_irn an user of the spilled node
477 static void spill_phi(spill_env_t *env, spill_info_t *spillinfo)
479 ir_graph *irg = env->irg;
480 ir_node *phi = spillinfo->to_spill;
481 ir_node *block = get_nodes_block(phi);
489 assert(!get_opt_cse());
490 DBG((dbg, LEVEL_1, "spilling Phi %+F:\n", phi));
492 /* build a new PhiM */
493 arity = get_irn_arity(phi);
494 ins = alloca(sizeof(ir_node*) * arity);
495 unknown = new_r_Unknown(irg, mode_M);
496 for(i = 0; i < arity; ++i) {
500 /* override or replace spills list... */
501 spill = obstack_alloc(&env->obst, sizeof(spill[0]));
502 spill->before = skip_keeps_phis(phi);
503 spill->spill = new_r_Phi(irg, block, arity, ins, mode_M);
506 spillinfo->spills = spill;
507 #ifdef FIRM_STATISTICS
508 env->spilled_phi_count++;
511 for(i = 0; i < arity; ++i) {
512 ir_node *arg = get_irn_n(phi, i);
513 spill_info_t *arg_info = get_spillinfo(env, arg);
515 determine_spill_costs(env, arg_info);
516 spill_node(env, arg_info);
518 set_irn_n(spill->spill, i, arg_info->spills->spill);
520 DBG((dbg, LEVEL_1, "... done spilling Phi %+F, created PhiM %+F\n", phi,
527 * @param senv the spill environment
528 * @param to_spill the node that should be spilled
530 static void spill_node(spill_env_t *env, spill_info_t *spillinfo)
534 /* node is already spilled */
535 if(spillinfo->spills != NULL && spillinfo->spills->spill != NULL)
538 to_spill = spillinfo->to_spill;
540 if (is_Phi(to_spill) && ir_nodeset_contains(&env->mem_phis, to_spill)) {
541 spill_phi(env, spillinfo);
543 spill_irn(env, spillinfo);
550 * | _ \ ___ _ __ ___ __ _| |_ ___ _ __(_) __ _| (_)_______
551 * | |_) / _ \ '_ ` _ \ / _` | __/ _ \ '__| |/ _` | | |_ / _ \
552 * | _ < __/ | | | | | (_| | || __/ | | | (_| | | |/ / __/
553 * |_| \_\___|_| |_| |_|\__,_|\__\___|_| |_|\__,_|_|_/___\___|
558 * Tests whether value @p arg is available before node @p reloader
559 * @returns 1 if value is available, 0 otherwise
561 static int is_value_available(spill_env_t *env, const ir_node *arg,
562 const ir_node *reloader)
564 if(is_Unknown(arg) || arg == new_NoMem())
570 if(arg == get_irg_frame(env->irg))
573 /* hack for now (happens when command should be inserted at end of block) */
574 if(is_Block(reloader)) {
579 * Ignore registers are always available
581 if(arch_irn_is(env->arch_env, arg, ignore)) {
585 /* the following test does not work while spilling,
586 * because the liveness info is not adapted yet to the effects of the
587 * additional spills/reloads.
590 /* we want to remat before the insn reloader
591 * thus an arguments is alive if
592 * - it interferes with the reloaders result
593 * - or it is (last-) used by reloader itself
595 if (values_interfere(env->birg->lv, reloader, arg)) {
599 arity = get_irn_arity(reloader);
600 for (i = 0; i < arity; ++i) {
601 ir_node *rel_arg = get_irn_n(reloader, i);
611 * Checks whether the node can principally be rematerialized
613 static int is_remat_node(spill_env_t *env, const ir_node *node)
615 const arch_env_t *arch_env = env->arch_env;
617 assert(!be_is_Spill(node));
619 if(arch_irn_is(arch_env, node, rematerializable))
626 * Check if a node is rematerializable. This tests for the following conditions:
628 * - The node itself is rematerializable
629 * - All arguments of the node are available or also rematerialisable
630 * - The costs for the rematerialisation operation is less or equal a limit
632 * Returns the costs needed for rematerialisation or something
633 * >= REMAT_COST_INFINITE if remat is not possible.
635 static int check_remat_conditions_costs(spill_env_t *env,
636 const ir_node *spilled, const ir_node *reloader, int parentcosts)
642 if(!is_remat_node(env, spilled))
643 return REMAT_COST_INFINITE;
645 if(be_is_Reload(spilled)) {
648 costs += arch_get_op_estimated_cost(env->arch_env, spilled);
650 if(parentcosts + costs >= env->reload_cost + env->spill_cost) {
651 return REMAT_COST_INFINITE;
653 if(arch_irn_is(env->arch_env, spilled, modify_flags)) {
654 return REMAT_COST_INFINITE;
658 for(i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
659 ir_node *arg = get_irn_n(spilled, i);
661 if(is_value_available(env, arg, reloader))
664 /* we have to rematerialize the argument as well */
666 /* we only support rematerializing 1 argument at the moment,
667 * so that we don't have to care about register pressure
669 return REMAT_COST_INFINITE;
673 costs += check_remat_conditions_costs(env, arg, reloader,
674 parentcosts + costs);
675 if(parentcosts + costs >= env->reload_cost + env->spill_cost)
676 return REMAT_COST_INFINITE;
683 * Re-materialize a node.
685 * @param senv the spill environment
686 * @param spilled the node that was spilled
687 * @param reloader a irn that requires a reload
689 static ir_node *do_remat(spill_env_t *env, ir_node *spilled, ir_node *reloader)
696 if(is_Block(reloader)) {
699 bl = get_nodes_block(reloader);
702 ins = alloca(get_irn_arity(spilled) * sizeof(ins[0]));
703 for(i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
704 ir_node *arg = get_irn_n(spilled, i);
706 if(is_value_available(env, arg, reloader)) {
709 ins[i] = do_remat(env, arg, reloader);
710 #ifdef FIRM_STATISTICS
711 /* don't count the recursive call as remat */
717 /* create a copy of the node */
718 res = new_ir_node(get_irn_dbg_info(spilled), env->irg, bl,
719 get_irn_op(spilled), get_irn_mode(spilled),
720 get_irn_arity(spilled), ins);
721 copy_node_attr(spilled, res);
722 new_backedge_info(res);
724 DBG((dbg, LEVEL_1, "Insert remat %+F of %+F before reloader %+F\n", res, spilled, reloader));
726 if (! is_Proj(res)) {
727 /* insert in schedule */
729 sched_add_before(reloader, res);
730 #ifdef FIRM_STATISTICS
738 double be_get_spill_costs(spill_env_t *env, ir_node *to_spill, ir_node *before)
740 ir_node *block = get_nodes_block(before);
741 double freq = get_block_execfreq(env->exec_freq, block);
744 return env->spill_cost * freq;
747 unsigned be_get_reload_costs_no_weight(spill_env_t *env, const ir_node *to_spill,
748 const ir_node *before)
751 /* is the node rematerializable? */
752 unsigned costs = check_remat_conditions_costs(env, to_spill, before, 0);
753 if(costs < (unsigned) env->reload_cost)
757 return env->reload_cost;
760 double be_get_reload_costs(spill_env_t *env, ir_node *to_spill, ir_node *before)
762 ir_node *block = get_nodes_block(before);
763 double freq = get_block_execfreq(env->exec_freq, block);
766 /* is the node rematerializable? */
767 int costs = check_remat_conditions_costs(env, to_spill, before, 0);
768 if(costs < env->reload_cost)
772 return env->reload_cost * freq;
775 int be_is_rematerializable(spill_env_t *env, const ir_node *to_remat,
776 const ir_node *before)
778 return check_remat_conditions_costs(env, to_remat, before, 0) < REMAT_COST_INFINITE;
781 double be_get_reload_costs_on_edge(spill_env_t *env, ir_node *to_spill,
782 ir_node *block, int pos)
784 ir_node *before = get_block_insertion_point(block, pos);
785 return be_get_reload_costs(env, to_spill, before);
790 * |_ _|_ __ ___ ___ _ __| |_ | _ \ ___| | ___ __ _ __| |___
791 * | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ |/ _ \ / _` |/ _` / __|
792 * | || | | \__ \ __/ | | |_ | _ < __/ | (_) | (_| | (_| \__ \
793 * |___|_| |_|___/\___|_| \__| |_| \_\___|_|\___/ \__,_|\__,_|___/
798 * analyzes how to best spill a node and determine costs for that
800 static void determine_spill_costs(spill_env_t *env, spill_info_t *spillinfo)
802 ir_node *to_spill = spillinfo->to_spill;
803 ir_node *spill_block;
805 double spill_execfreq;
807 /* already calculated? */
808 if(spillinfo->spill_costs >= 0)
811 assert(! arch_irn_is(env->arch_env, to_spill, dont_spill));
812 assert(!be_is_Reload(to_spill));
814 /* some backends have virtual noreg/unknown nodes that are not scheduled
815 * and simply always available.
816 * TODO: this is kinda hairy, the NoMem is correct for an Unknown as Phi
817 * predecessor (of a PhiM) but this test might match other things too...
819 if(!sched_is_scheduled(to_spill)) {
820 /* override spillinfos or create a new one */
821 spill_t *spill = obstack_alloc(&env->obst, sizeof(spill[0]));
822 spill->before = NULL;
824 spill->spill = new_NoMem();
826 spillinfo->spills = spill;
827 spillinfo->spill_costs = 0;
829 DB((dbg, LEVEL_1, "don't spill %+F use NoMem\n", to_spill));
833 spill_block = get_nodes_block(to_spill);
834 spill_execfreq = get_block_execfreq(env->exec_freq, spill_block);
836 if (is_Phi(to_spill) && ir_nodeset_contains(&env->mem_phis, to_spill)) {
837 /* TODO calculate correct costs...
838 * (though we can't remat this node anyway so no big problem) */
839 spillinfo->spill_costs = env->spill_cost * spill_execfreq;
843 if(spillinfo->spills != NULL) {
845 double spills_execfreq;
847 /* calculate sum of executaion frequencies of individual spills */
849 s = spillinfo->spills;
850 for( ; s != NULL; s = s->next) {
851 ir_node *spill_block = s->before;
854 if(!is_Block(spill_block)) {
855 spill_block = get_nodes_block(spill_block);
857 freq = get_block_execfreq(env->exec_freq, spill_block);
859 spills_execfreq += freq;
862 DB((dbg, LEVEL_1, "%+F: latespillcosts %f after def: %f\n", to_spill,
863 spills_execfreq * env->spill_cost,
864 spill_execfreq * env->spill_cost));
866 /* multi-/latespill is advantageous -> return*/
867 if(spills_execfreq < spill_execfreq) {
868 DB((dbg, LEVEL_1, "use latespills for %+F\n", to_spill));
869 spillinfo->spill_costs = spills_execfreq * env->spill_cost;
874 /* override spillinfos or create a new one */
875 spill = obstack_alloc(&env->obst, sizeof(spill[0]));
876 spill->before = skip_keeps_phis(to_spill);
880 spillinfo->spills = spill;
881 spillinfo->spill_costs = spill_execfreq * env->spill_cost;
882 DB((dbg, LEVEL_1, "spill %+F after definition\n", to_spill));
885 void make_spill_locations_dominate_irn(spill_env_t *env, ir_node *irn)
887 const spill_info_t *si = get_spillinfo(env, irn);
888 ir_node *start_block = get_irg_start_block(get_irn_irg(irn));
889 int n_blocks = get_Block_dom_max_subtree_pre_num(start_block);
890 bitset_t *reloads = bitset_alloca(n_blocks);
897 /* Fill the bitset with the dominance pre-order numbers
898 * of the blocks the reloads are located in. */
899 for (r = si->reloaders; r != NULL; r = r->next) {
900 ir_node *bl = get_nodes_block(r->reloader);
901 bitset_set(reloads, get_Block_dom_tree_pre_num(bl));
904 /* Now, cancel out all the blocks that are dominated by each spill.
905 * If the bitset is not empty after that, we have reloads that are
906 * not dominated by any spill. */
907 for (s = si->spills; s != NULL; s = s->next) {
908 ir_node *bl = get_nodes_block(s->before);
909 int start = get_Block_dom_tree_pre_num(bl);
910 int end = get_Block_dom_max_subtree_pre_num(bl);
912 bitset_clear_range(reloads, start, end);
915 if (!bitset_is_empty(reloads))
916 be_add_spill(env, si->to_spill, sched_next(si->to_spill));
919 void be_insert_spills_reloads(spill_env_t *env)
921 ir_graph *irg = env->irg;
922 const arch_env_t *arch_env = env->arch_env;
923 const ir_exec_freq *exec_freq = env->exec_freq;
925 ir_nodeset_iterator_t iter;
928 BE_TIMER_PUSH(t_ra_spill_apply);
930 env->new_nodes_idx = get_irg_last_idx(irg);
932 /* create all phi-ms first, this is needed so, that phis, hanging on
933 spilled phis work correctly */
934 foreach_ir_nodeset(&env->mem_phis, node, iter) {
935 spill_info_t *info = get_spillinfo(env, node);
936 spill_node(env, info);
939 /* process each spilled node */
940 for (si = set_first(env->spills); si; si = set_next(env->spills)) {
942 ir_node *to_spill = si->to_spill;
943 ir_mode *mode = get_irn_mode(to_spill);
944 ir_node **copies = NEW_ARR_F(ir_node*, 0);
945 double all_remat_costs = 0; /** costs when we would remat all nodes */
948 DBG((dbg, LEVEL_1, "\nhandling all reloaders of %+F:\n", to_spill));
950 determine_spill_costs(env, si);
952 /* determine possibility of rematerialisations */
954 /* calculate cost savings for each indivial value when it would
955 be rematted instead of reloaded */
956 for (rld = si->reloaders; rld != NULL; rld = rld->next) {
959 int remat_cost_delta;
961 ir_node *reloader = rld->reloader;
963 if(rld->rematted_node != NULL) {
964 DBG((dbg, LEVEL_2, "\tforced remat %+F before %+F\n",
965 rld->rematted_node, reloader));
968 if(rld->remat_cost_delta >= REMAT_COST_INFINITE) {
969 DBG((dbg, LEVEL_2, "\treload before %+F is forbidden\n",
971 all_remat_costs = REMAT_COST_INFINITE;
975 remat_cost = check_remat_conditions_costs(env, to_spill,
977 if(remat_cost >= REMAT_COST_INFINITE) {
978 DBG((dbg, LEVEL_2, "\tremat before %+F not possible\n",
980 rld->remat_cost_delta = REMAT_COST_INFINITE;
981 all_remat_costs = REMAT_COST_INFINITE;
985 remat_cost_delta = remat_cost - env->reload_cost;
986 rld->remat_cost_delta = remat_cost_delta;
987 block = is_Block(reloader) ? reloader : get_nodes_block(reloader);
988 freq = get_block_execfreq(exec_freq, block);
989 all_remat_costs += remat_cost_delta * freq;
990 DBG((dbg, LEVEL_2, "\tremat costs delta before %+F: "
991 "%d (rel %f)\n", reloader, remat_cost_delta,
992 remat_cost_delta * freq));
994 if(all_remat_costs < REMAT_COST_INFINITE) {
995 /* we don't need the costs for the spill if we can remat
997 all_remat_costs -= si->spill_costs;
999 DBG((dbg, LEVEL_2, "\tspill costs %d (rel %f)\n",
1000 env->spill_cost, si->spill_costs));
1003 if(all_remat_costs < 0) {
1004 DBG((dbg, LEVEL_1, "\nforcing remats of all reloaders (%f)\n",
1010 /* go through all reloads for this spill */
1011 for (rld = si->reloaders; rld != NULL; rld = rld->next) {
1012 ir_node *copy; /* a reload is a "copy" of the original value */
1014 if (rld->rematted_node != NULL) {
1015 copy = rld->rematted_node;
1016 sched_add_before(rld->reloader, copy);
1017 } else if (be_do_remats &&
1018 (force_remat || rld->remat_cost_delta < 0)) {
1019 copy = do_remat(env, to_spill, rld->reloader);
1021 /* make sure we have a spill */
1022 spill_node(env, si);
1024 /* create a reload, use the first spill for now SSA
1025 * reconstruction for memory comes below */
1026 assert(si->spills != NULL);
1027 copy = be_reload(arch_env, si->reload_cls, rld->reloader, mode,
1029 #ifdef FIRM_STATISTICS
1030 env->reload_count++;
1034 DBG((dbg, LEVEL_1, " %+F of %+F before %+F\n",
1035 copy, to_spill, rld->reloader));
1036 ARR_APP1(ir_node*, copies, copy);
1039 /* if we had any reloads or remats, then we need to reconstruct the
1040 * SSA form for the spilled value */
1041 if (ARR_LEN(copies) > 0) {
1042 be_ssa_construction_env_t senv;
1043 /* be_lv_t *lv = be_get_birg_liveness(env->birg); */
1045 be_ssa_construction_init(&senv, env->birg);
1046 be_ssa_construction_add_copy(&senv, to_spill);
1047 be_ssa_construction_add_copies(&senv, copies, ARR_LEN(copies));
1048 be_ssa_construction_fix_users(&senv, to_spill);
1051 /* no need to enable this as long as we invalidate liveness
1052 after this function... */
1053 be_ssa_construction_update_liveness_phis(&senv);
1054 be_liveness_update(to_spill);
1055 len = ARR_LEN(copies);
1056 for(i = 0; i < len; ++i) {
1057 be_liveness_update(lv, copies[i]);
1060 be_ssa_construction_destroy(&senv);
1062 /* need to reconstruct SSA form if we had multiple spills */
1063 if (si->spills != NULL && si->spills->next != NULL) {
1065 int spill_count = 0;
1067 be_ssa_construction_env_t senv;
1069 be_ssa_construction_init(&senv, env->birg);
1071 for( ; spill != NULL; spill = spill->next) {
1072 /* maybe we rematerialized the value and need no spill */
1073 if(spill->spill == NULL)
1075 be_ssa_construction_add_copy(&senv, spill->spill);
1078 if(spill_count > 1) {
1079 /* all reloads are attached to the first spill, fix them now */
1080 be_ssa_construction_fix_users(&senv, si->spills->spill);
1083 be_ssa_construction_destroy(&senv);
1087 si->reloaders = NULL;
1090 stat_ev_dbl("spill_spills", env->spill_count);
1091 stat_ev_dbl("spill_reloads", env->reload_count);
1092 stat_ev_dbl("spill_remats", env->remat_count);
1093 stat_ev_dbl("spill_spilled_phis", env->spilled_phi_count);
1095 /* Matze: In theory be_ssa_construction should take care of the liveness...
1096 * try to disable this again in the future */
1097 be_liveness_invalidate(env->birg->lv);
1099 be_remove_dead_nodes_from_schedule(env->birg);
1101 BE_TIMER_POP(t_ra_spill_apply);
1104 void be_init_spill(void)
1106 FIRM_DBG_REGISTER(dbg, "firm.be.spill");
1109 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spill);