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
37 #include "iredges_t.h"
38 #include "irbackedge_t.h"
48 #include "irnodeset.h"
53 #include "besched_t.h"
57 #include "bechordal_t.h"
58 #include "bejavacoal.h"
59 #include "bespilloptions.h"
60 #include "bestatevent.h"
61 #include "bessaconstr.h"
63 #include "beintlive_t.h"
67 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
69 #define REMAT_COST_INFINITE 1000
71 typedef struct reloader_t reloader_t;
74 ir_node *can_spill_after;
76 ir_node *rematted_node;
77 int remat_cost_delta; /** costs needed for rematerialization,
78 compared to placing a reload */
81 typedef struct spill_t spill_t;
84 ir_node *after; /**< spill has to be placed after this node (or earlier) */
88 typedef struct spill_info_t spill_info_t;
90 ir_node *to_spill; /**< the value that should get spilled */
91 reloader_t *reloaders; /**< list of places where the value should get
93 spill_t *spills; /**< list of latest places where spill must be
95 double spill_costs; /**< costs needed for spilling the value */
96 const arch_register_class_t *reload_cls; /** the register class in which the
97 reload should be placed */
101 const arch_env_t *arch_env;
105 int spill_cost; /**< the cost of a single spill node */
106 int reload_cost; /**< the cost of a reload node */
107 set *spills; /**< all spill_info_t's, which must be
109 ir_nodeset_t mem_phis; /**< set of all spilled phis. */
110 ir_exec_freq *exec_freq;
112 #ifdef FIRM_STATISTICS
113 unsigned spill_count;
114 unsigned reload_count;
115 unsigned remat_count;
116 unsigned spilled_phi_count;
121 * Compare two spill infos.
123 static int cmp_spillinfo(const void *x, const void *y, size_t size)
125 const spill_info_t *xx = x;
126 const spill_info_t *yy = y;
129 return xx->to_spill != yy->to_spill;
133 * Returns spill info for a specific value (the value that is to be spilled)
135 static spill_info_t *get_spillinfo(const spill_env_t *env, ir_node *value)
137 spill_info_t info, *res;
138 int hash = hash_irn(value);
140 info.to_spill = value;
141 res = set_find(env->spills, &info, sizeof(info), hash);
144 info.reloaders = NULL;
146 info.spill_costs = -1;
147 info.reload_cls = NULL;
148 res = set_insert(env->spills, &info, sizeof(info), hash);
154 spill_env_t *be_new_spill_env(be_irg_t *birg)
156 const arch_env_t *arch_env = birg->main_env->arch_env;
158 spill_env_t *env = XMALLOC(spill_env_t);
159 env->spills = new_set(cmp_spillinfo, 1024);
160 env->irg = be_get_birg_irg(birg);
162 env->arch_env = arch_env;
163 ir_nodeset_init(&env->mem_phis);
164 env->spill_cost = arch_env->spill_cost;
165 env->reload_cost = arch_env->reload_cost;
166 env->exec_freq = be_get_birg_exec_freq(birg);
167 obstack_init(&env->obst);
169 #ifdef FIRM_STATISTICS
170 env->spill_count = 0;
171 env->reload_count = 0;
172 env->remat_count = 0;
173 env->spilled_phi_count = 0;
179 void be_delete_spill_env(spill_env_t *env)
181 del_set(env->spills);
182 ir_nodeset_destroy(&env->mem_phis);
183 obstack_free(&env->obst, NULL);
189 * | _ \| | __ _ ___ ___ | _ \ ___| | ___ __ _ __| |___
190 * | |_) | |/ _` |/ __/ _ \ | |_) / _ \ |/ _ \ / _` |/ _` / __|
191 * | __/| | (_| | (_| __/ | _ < __/ | (_) | (_| | (_| \__ \
192 * |_| |_|\__,_|\___\___| |_| \_\___|_|\___/ \__,_|\__,_|___/
196 void be_add_spill(spill_env_t *env, ir_node *to_spill, ir_node *after)
198 spill_info_t *spill_info = get_spillinfo(env, to_spill);
203 assert(! arch_irn_is(env->arch_env, to_spill, dont_spill));
204 DB((dbg, LEVEL_1, "Add spill of %+F after %+F\n", to_spill, after));
206 /* Just for safety make sure that we do not insert the spill in front of a phi */
207 assert(!is_Phi(sched_next(after)));
209 /* spills that are dominated by others are not needed */
211 s = spill_info->spills;
212 for( ; s != NULL; s = s->next) {
213 /* no need to add this spill if it is dominated by another */
214 if(value_dominates(s->after, after)) {
215 DB((dbg, LEVEL_1, "...dominated by %+F, not added\n", s->after));
218 /* remove spills that we dominate */
219 if(value_dominates(after, s->after)) {
220 DB((dbg, LEVEL_1, "...remove old spill at %+F\n", s->after));
222 last->next = s->next;
224 spill_info->spills = s->next;
231 spill = obstack_alloc(&env->obst, sizeof(spill[0]));
232 spill->after = after;
233 spill->next = spill_info->spills;
236 spill_info->spills = spill;
239 void be_add_remat(spill_env_t *env, ir_node *to_spill, ir_node *before,
240 ir_node *rematted_node)
242 spill_info_t *spill_info;
243 reloader_t *reloader;
245 spill_info = get_spillinfo(env, to_spill);
247 /* add the remat information */
248 reloader = obstack_alloc(&env->obst, sizeof(reloader[0]));
249 reloader->next = spill_info->reloaders;
250 reloader->reloader = before;
251 reloader->rematted_node = rematted_node;
252 reloader->remat_cost_delta = 0; /* We will never have a cost win over a
253 reload since we're not even allowed to
256 spill_info->reloaders = reloader;
258 DBG((dbg, LEVEL_1, "creating spillinfo for %+F, will be rematerialized before %+F\n",
262 void be_add_reload2(spill_env_t *env, ir_node *to_spill, ir_node *before,
263 ir_node *can_spill_after, const arch_register_class_t *reload_cls,
269 assert(! arch_irn_is(env->arch_env, to_spill, dont_spill));
271 info = get_spillinfo(env, to_spill);
273 if (is_Phi(to_spill)) {
276 /* create spillinfos for the phi arguments */
277 for (i = 0, arity = get_irn_arity(to_spill); i < arity; ++i) {
278 ir_node *arg = get_irn_n(to_spill, i);
279 get_spillinfo(env, arg);
283 assert(!is_Proj(before) && !be_is_Keep(before));
285 /* put reload into list */
286 rel = obstack_alloc(&env->obst, sizeof(rel[0]));
287 rel->next = info->reloaders;
288 rel->reloader = before;
289 rel->rematted_node = NULL;
290 rel->can_spill_after = can_spill_after;
291 rel->remat_cost_delta = allow_remat ? 0 : REMAT_COST_INFINITE;
293 info->reloaders = rel;
294 assert(info->reload_cls == NULL || info->reload_cls == reload_cls);
295 info->reload_cls = reload_cls;
297 DBG((dbg, LEVEL_1, "creating spillinfo for %+F, will be reloaded before %+F, may%s be rematerialized\n",
298 to_spill, before, allow_remat ? "" : " not"));
301 void be_add_reload(spill_env_t *senv, ir_node *to_spill, ir_node *before,
302 const arch_register_class_t *reload_cls, int allow_remat)
304 be_add_reload2(senv, to_spill, before, to_spill, reload_cls, allow_remat);
308 ir_node *be_get_end_of_block_insertion_point(const ir_node *block)
310 ir_node *last = sched_last(block);
312 /* we might have keeps behind the jump... */
313 while (be_is_Keep(last)) {
314 last = sched_prev(last);
315 assert(!sched_is_end(last));
318 assert(is_cfop(last));
320 /* add the reload before the (cond-)jump */
324 static ir_node *skip_keeps_phis(ir_node *node)
327 ir_node *next = sched_next(node);
328 if(!is_Phi(next) && !be_is_Keep(next))
336 * Returns the point at which you can insert a node that should be executed
337 * before block @p block when coming from pred @p pos.
339 static ir_node *get_block_insertion_point(ir_node *block, int pos)
343 /* simply add the reload to the beginning of the block if we only have 1
344 * predecessor. We don't need to check for phis as there can't be any in a
345 * block with only 1 pred. */
346 if(get_Block_n_cfgpreds(block) == 1) {
347 assert(!is_Phi(sched_first(block)));
348 return sched_first(block);
351 /* We have to reload the value in pred-block */
352 predblock = get_Block_cfgpred_block(block, pos);
353 return be_get_end_of_block_insertion_point(predblock);
356 void be_add_reload_at_end(spill_env_t *env, ir_node *to_spill,
357 const ir_node *block,
358 const arch_register_class_t *reload_cls,
361 ir_node *before = be_get_end_of_block_insertion_point(block);
362 be_add_reload(env, to_spill, before, reload_cls, allow_remat);
365 void be_add_reload_on_edge(spill_env_t *env, ir_node *to_spill, ir_node *block,
366 int pos, const arch_register_class_t *reload_cls,
369 ir_node *before = get_block_insertion_point(block, pos);
370 be_add_reload(env, to_spill, before, reload_cls, allow_remat);
373 void be_spill_phi(spill_env_t *env, ir_node *node)
379 assert(is_Phi(node));
381 ir_nodeset_insert(&env->mem_phis, node);
383 /* create spills for the phi arguments */
384 block = get_nodes_block(node);
385 spill = get_spillinfo(env, node);
386 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
387 ir_node *arg = get_irn_n(node, i);
389 //get_spillinfo(env, arg);
391 /* some backends have virtual noreg/unknown nodes that are not scheduled
392 * and simply always available. */
393 if(!sched_is_scheduled(arg)) {
394 ir_node *pred_block = get_Block_cfgpred_block(block, i);
395 insert = be_get_end_of_block_insertion_point(pred_block);
396 insert = sched_prev(insert);
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 *after = spill->after;
446 ir_node *block = get_block(after);
448 after = skip_keeps_phis(after);
450 spill->spill = be_spill(env->arch_env, block, to_spill);
451 sched_add_after(after, spill->spill);
452 DB((dbg, LEVEL_1, "\t%+F after %+F\n", spill->spill, after));
453 #ifdef FIRM_STATISTICS
457 DBG((dbg, LEVEL_1, "\n"));
460 static void spill_node(spill_env_t *env, spill_info_t *spillinfo);
463 * If the first usage of a Phi result would be out of memory
464 * there is no sense in allocating a register for it.
465 * Thus we spill it and all its operands to the same spill slot.
466 * Therefore the phi/dataB becomes a phi/Memory
468 * @param senv the spill environment
469 * @param phi the Phi node that should be spilled
470 * @param ctx_irn an user of the spilled node
472 static void spill_phi(spill_env_t *env, spill_info_t *spillinfo)
474 ir_graph *irg = env->irg;
475 ir_node *phi = spillinfo->to_spill;
476 ir_node *block = get_nodes_block(phi);
484 assert(!get_opt_cse());
485 DBG((dbg, LEVEL_1, "spilling Phi %+F:\n", phi));
487 /* build a new PhiM */
488 arity = get_irn_arity(phi);
489 ins = alloca(sizeof(ir_node*) * arity);
490 unknown = new_r_Unknown(irg, mode_M);
491 for(i = 0; i < arity; ++i) {
495 /* override or replace spills list... */
496 spill = obstack_alloc(&env->obst, sizeof(spill[0]));
497 spill->after = skip_keeps_phis(phi);
498 spill->spill = new_r_Phi(irg, block, arity, ins, mode_M);
501 spillinfo->spills = spill;
502 #ifdef FIRM_STATISTICS
503 env->spilled_phi_count++;
506 for(i = 0; i < arity; ++i) {
507 ir_node *arg = get_irn_n(phi, i);
508 spill_info_t *arg_info = get_spillinfo(env, arg);
510 determine_spill_costs(env, arg_info);
511 spill_node(env, arg_info);
513 set_irn_n(spill->spill, i, arg_info->spills->spill);
515 DBG((dbg, LEVEL_1, "... done spilling Phi %+F, created PhiM %+F\n", phi,
522 * @param senv the spill environment
523 * @param to_spill the node that should be spilled
525 static void spill_node(spill_env_t *env, spill_info_t *spillinfo)
529 /* node is already spilled */
530 if(spillinfo->spills != NULL && spillinfo->spills->spill != NULL)
533 to_spill = spillinfo->to_spill;
535 if (is_Phi(to_spill) && ir_nodeset_contains(&env->mem_phis, to_spill)) {
536 spill_phi(env, spillinfo);
538 spill_irn(env, spillinfo);
545 * | _ \ ___ _ __ ___ __ _| |_ ___ _ __(_) __ _| (_)_______
546 * | |_) / _ \ '_ ` _ \ / _` | __/ _ \ '__| |/ _` | | |_ / _ \
547 * | _ < __/ | | | | | (_| | || __/ | | | (_| | | |/ / __/
548 * |_| \_\___|_| |_| |_|\__,_|\__\___|_| |_|\__,_|_|_/___\___|
553 * Tests whether value @p arg is available before node @p reloader
554 * @returns 1 if value is available, 0 otherwise
556 static int is_value_available(spill_env_t *env, const ir_node *arg,
557 const ir_node *reloader)
559 if(is_Unknown(arg) || arg == new_NoMem())
565 if(arg == get_irg_frame(env->irg))
568 /* hack for now (happens when command should be inserted at end of block) */
569 if(is_Block(reloader)) {
574 * Ignore registers are always available
576 if(arch_irn_is(env->arch_env, arg, ignore)) {
580 /* the following test does not work while spilling,
581 * because the liveness info is not adapted yet to the effects of the
582 * additional spills/reloads.
585 /* we want to remat before the insn reloader
586 * thus an arguments is alive if
587 * - it interferes with the reloaders result
588 * - or it is (last-) used by reloader itself
590 if (values_interfere(env->birg->lv, reloader, arg)) {
594 arity = get_irn_arity(reloader);
595 for (i = 0; i < arity; ++i) {
596 ir_node *rel_arg = get_irn_n(reloader, i);
606 * Checks whether the node can principally be rematerialized
608 static int is_remat_node(spill_env_t *env, const ir_node *node)
610 const arch_env_t *arch_env = env->arch_env;
612 assert(!be_is_Spill(node));
614 if(arch_irn_is(arch_env, node, rematerializable))
621 * Check if a node is rematerializable. This tests for the following conditions:
623 * - The node itself is rematerializable
624 * - All arguments of the node are available or also rematerialisable
625 * - The costs for the rematerialisation operation is less or equal a limit
627 * Returns the costs needed for rematerialisation or something
628 * >= REMAT_COST_INFINITE if remat is not possible.
630 static int check_remat_conditions_costs(spill_env_t *env,
631 const ir_node *spilled, const ir_node *reloader, int parentcosts)
637 if(!is_remat_node(env, spilled))
638 return REMAT_COST_INFINITE;
640 if(be_is_Reload(spilled)) {
643 costs += arch_get_op_estimated_cost(env->arch_env, spilled);
645 if(parentcosts + costs >= env->reload_cost + env->spill_cost) {
646 return REMAT_COST_INFINITE;
648 if(arch_irn_is(env->arch_env, spilled, modify_flags)) {
649 return REMAT_COST_INFINITE;
653 for(i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
654 ir_node *arg = get_irn_n(spilled, i);
656 if(is_value_available(env, arg, reloader))
659 /* we have to rematerialize the argument as well */
661 /* we only support rematerializing 1 argument at the moment,
662 * so that we don't have to care about register pressure
664 return REMAT_COST_INFINITE;
668 costs += check_remat_conditions_costs(env, arg, reloader,
669 parentcosts + costs);
670 if(parentcosts + costs >= env->reload_cost + env->spill_cost)
671 return REMAT_COST_INFINITE;
678 * Re-materialize a node.
680 * @param senv the spill environment
681 * @param spilled the node that was spilled
682 * @param reloader a irn that requires a reload
684 static ir_node *do_remat(spill_env_t *env, ir_node *spilled, ir_node *reloader)
691 if(is_Block(reloader)) {
694 bl = get_nodes_block(reloader);
697 ins = alloca(get_irn_arity(spilled) * sizeof(ins[0]));
698 for(i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
699 ir_node *arg = get_irn_n(spilled, i);
701 if(is_value_available(env, arg, reloader)) {
704 ins[i] = do_remat(env, arg, reloader);
705 #ifdef FIRM_STATISTICS
706 /* don't count the recursive call as remat */
712 /* create a copy of the node */
713 res = new_ir_node(get_irn_dbg_info(spilled), env->irg, bl,
714 get_irn_op(spilled), get_irn_mode(spilled),
715 get_irn_arity(spilled), ins);
716 copy_node_attr(spilled, res);
717 arch_env_mark_remat(env->arch_env, res);
718 new_backedge_info(res);
720 DBG((dbg, LEVEL_1, "Insert remat %+F of %+F before reloader %+F\n", res, spilled, reloader));
722 if (! is_Proj(res)) {
723 /* insert in schedule */
725 sched_add_before(reloader, res);
726 #ifdef FIRM_STATISTICS
734 double be_get_spill_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 return env->spill_cost * freq;
743 unsigned be_get_reload_costs_no_weight(spill_env_t *env, const ir_node *to_spill,
744 const ir_node *before)
747 /* is the node rematerializable? */
748 unsigned costs = check_remat_conditions_costs(env, to_spill, before, 0);
749 if(costs < (unsigned) env->reload_cost)
753 return env->reload_cost;
756 double be_get_reload_costs(spill_env_t *env, ir_node *to_spill, ir_node *before)
758 ir_node *block = get_nodes_block(before);
759 double freq = get_block_execfreq(env->exec_freq, block);
762 /* is the node rematerializable? */
763 int costs = check_remat_conditions_costs(env, to_spill, before, 0);
764 if(costs < env->reload_cost)
768 return env->reload_cost * freq;
771 int be_is_rematerializable(spill_env_t *env, const ir_node *to_remat,
772 const ir_node *before)
774 return check_remat_conditions_costs(env, to_remat, before, 0) < REMAT_COST_INFINITE;
777 double be_get_reload_costs_on_edge(spill_env_t *env, ir_node *to_spill,
778 ir_node *block, int pos)
780 ir_node *before = get_block_insertion_point(block, pos);
781 return be_get_reload_costs(env, to_spill, before);
786 * |_ _|_ __ ___ ___ _ __| |_ | _ \ ___| | ___ __ _ __| |___
787 * | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ |/ _ \ / _` |/ _` / __|
788 * | || | | \__ \ __/ | | |_ | _ < __/ | (_) | (_| | (_| \__ \
789 * |___|_| |_|___/\___|_| \__| |_| \_\___|_|\___/ \__,_|\__,_|___/
794 * analyzes how to best spill a node and determine costs for that
796 static void determine_spill_costs(spill_env_t *env, spill_info_t *spillinfo)
798 ir_node *to_spill = spillinfo->to_spill;
799 ir_node *spill_block;
801 double spill_execfreq;
803 /* already calculated? */
804 if(spillinfo->spill_costs >= 0)
807 assert(! arch_irn_is(env->arch_env, to_spill, dont_spill));
808 assert(!be_is_Reload(to_spill));
810 /* some backends have virtual noreg/unknown nodes that are not scheduled
811 * and simply always available.
812 * TODO: this is kinda hairy, the NoMem is correct for an Unknown as Phi
813 * predecessor (of a PhiM) but this test might match other things too...
815 if(!sched_is_scheduled(to_spill)) {
816 /* override spillinfos or create a new one */
817 spill_t *spill = obstack_alloc(&env->obst, sizeof(spill[0]));
820 spill->spill = new_NoMem();
822 spillinfo->spills = spill;
823 spillinfo->spill_costs = 0;
825 DB((dbg, LEVEL_1, "don't spill %+F use NoMem\n", to_spill));
829 spill_block = get_nodes_block(to_spill);
830 spill_execfreq = get_block_execfreq(env->exec_freq, spill_block);
832 if (is_Phi(to_spill) && ir_nodeset_contains(&env->mem_phis, to_spill)) {
833 /* TODO calculate correct costs...
834 * (though we can't remat this node anyway so no big problem) */
835 spillinfo->spill_costs = env->spill_cost * spill_execfreq;
839 if(spillinfo->spills != NULL) {
841 double spills_execfreq;
843 /* calculate sum of executaion frequencies of individual spills */
845 s = spillinfo->spills;
846 for( ; s != NULL; s = s->next) {
847 ir_node *spill_block = get_block(s->after);
848 double freq = get_block_execfreq(env->exec_freq, spill_block);
850 spills_execfreq += freq;
853 DB((dbg, LEVEL_1, "%+F: latespillcosts %f after def: %f\n", to_spill,
854 spills_execfreq * env->spill_cost,
855 spill_execfreq * env->spill_cost));
857 /* multi-/latespill is advantageous -> return*/
858 if(spills_execfreq < spill_execfreq) {
859 DB((dbg, LEVEL_1, "use latespills for %+F\n", to_spill));
860 spillinfo->spill_costs = spills_execfreq * env->spill_cost;
865 /* override spillinfos or create a new one */
866 spill = obstack_alloc(&env->obst, sizeof(spill[0]));
867 spill->after = skip_keeps_phis(to_spill);
871 spillinfo->spills = spill;
872 spillinfo->spill_costs = spill_execfreq * env->spill_cost;
873 DB((dbg, LEVEL_1, "spill %+F after definition\n", to_spill));
876 void make_spill_locations_dominate_irn(spill_env_t *env, ir_node *irn)
878 const spill_info_t *si = get_spillinfo(env, irn);
879 ir_node *start_block = get_irg_start_block(get_irn_irg(irn));
880 int n_blocks = get_Block_dom_max_subtree_pre_num(start_block);
881 bitset_t *reloads = bitset_alloca(n_blocks);
888 /* Fill the bitset with the dominance pre-order numbers
889 * of the blocks the reloads are located in. */
890 for (r = si->reloaders; r != NULL; r = r->next) {
891 ir_node *bl = get_nodes_block(r->reloader);
892 bitset_set(reloads, get_Block_dom_tree_pre_num(bl));
895 /* Now, cancel out all the blocks that are dominated by each spill.
896 * If the bitset is not empty after that, we have reloads that are
897 * not dominated by any spill. */
898 for (s = si->spills; s != NULL; s = s->next) {
899 ir_node *bl = get_nodes_block(s->after);
900 int start = get_Block_dom_tree_pre_num(bl);
901 int end = get_Block_dom_max_subtree_pre_num(bl);
903 bitset_clear_range(reloads, start, end);
906 if (!bitset_is_empty(reloads))
907 be_add_spill(env, si->to_spill, si->to_spill);
910 void be_insert_spills_reloads(spill_env_t *env)
912 const arch_env_t *arch_env = env->arch_env;
913 const ir_exec_freq *exec_freq = env->exec_freq;
915 ir_nodeset_iterator_t iter;
918 BE_TIMER_PUSH(t_ra_spill_apply);
920 /* create all phi-ms first, this is needed so, that phis, hanging on
921 spilled phis work correctly */
922 foreach_ir_nodeset(&env->mem_phis, node, iter) {
923 spill_info_t *info = get_spillinfo(env, node);
924 spill_node(env, info);
927 /* process each spilled node */
928 for (si = set_first(env->spills); si; si = set_next(env->spills)) {
930 ir_node *to_spill = si->to_spill;
931 ir_mode *mode = get_irn_mode(to_spill);
932 ir_node **copies = NEW_ARR_F(ir_node*, 0);
933 double all_remat_costs = 0; /** costs when we would remat all nodes */
936 DBG((dbg, LEVEL_1, "\nhandling all reloaders of %+F:\n", to_spill));
938 determine_spill_costs(env, si);
940 /* determine possibility of rematerialisations */
942 /* calculate cost savings for each indivial value when it would
943 be rematted instead of reloaded */
944 for (rld = si->reloaders; rld != NULL; rld = rld->next) {
947 int remat_cost_delta;
949 ir_node *reloader = rld->reloader;
951 if(rld->rematted_node != NULL) {
952 DBG((dbg, LEVEL_2, "\tforced remat %+F before %+F\n",
953 rld->rematted_node, reloader));
956 if(rld->remat_cost_delta >= REMAT_COST_INFINITE) {
957 DBG((dbg, LEVEL_2, "\treload before %+F is forbidden\n",
959 all_remat_costs = REMAT_COST_INFINITE;
963 remat_cost = check_remat_conditions_costs(env, to_spill,
965 if(remat_cost >= REMAT_COST_INFINITE) {
966 DBG((dbg, LEVEL_2, "\tremat before %+F not possible\n",
968 rld->remat_cost_delta = REMAT_COST_INFINITE;
969 all_remat_costs = REMAT_COST_INFINITE;
973 remat_cost_delta = remat_cost - env->reload_cost;
974 rld->remat_cost_delta = remat_cost_delta;
975 block = is_Block(reloader) ? reloader : get_nodes_block(reloader);
976 freq = get_block_execfreq(exec_freq, block);
977 all_remat_costs += remat_cost_delta * freq;
978 DBG((dbg, LEVEL_2, "\tremat costs delta before %+F: "
979 "%d (rel %f)\n", reloader, remat_cost_delta,
980 remat_cost_delta * freq));
982 if(all_remat_costs < REMAT_COST_INFINITE) {
983 /* we don't need the costs for the spill if we can remat
985 all_remat_costs -= si->spill_costs;
987 DBG((dbg, LEVEL_2, "\tspill costs %d (rel %f)\n",
988 env->spill_cost, si->spill_costs));
991 if(all_remat_costs < 0) {
992 DBG((dbg, LEVEL_1, "\nforcing remats of all reloaders (%f)\n",
998 /* go through all reloads for this spill */
999 for (rld = si->reloaders; rld != NULL; rld = rld->next) {
1000 ir_node *copy; /* a reload is a "copy" of the original value */
1002 if (rld->rematted_node != NULL) {
1003 copy = rld->rematted_node;
1004 sched_add_before(rld->reloader, copy);
1005 } else if (be_do_remats &&
1006 (force_remat || rld->remat_cost_delta < 0)) {
1007 copy = do_remat(env, to_spill, rld->reloader);
1009 /* make sure we have a spill */
1010 spill_node(env, si);
1012 /* create a reload, use the first spill for now SSA
1013 * reconstruction for memory comes below */
1014 assert(si->spills != NULL);
1015 copy = be_reload(arch_env, si->reload_cls, rld->reloader, mode,
1017 #ifdef FIRM_STATISTICS
1018 env->reload_count++;
1022 DBG((dbg, LEVEL_1, " %+F of %+F before %+F\n",
1023 copy, to_spill, rld->reloader));
1024 ARR_APP1(ir_node*, copies, copy);
1027 /* if we had any reloads or remats, then we need to reconstruct the
1028 * SSA form for the spilled value */
1029 if (ARR_LEN(copies) > 0) {
1030 be_ssa_construction_env_t senv;
1031 /* be_lv_t *lv = be_get_birg_liveness(env->birg); */
1033 be_ssa_construction_init(&senv, env->birg);
1034 be_ssa_construction_add_copy(&senv, to_spill);
1035 be_ssa_construction_add_copies(&senv, copies, ARR_LEN(copies));
1036 be_ssa_construction_fix_users(&senv, to_spill);
1039 /* no need to enable this as long as we invalidate liveness
1040 after this function... */
1041 be_ssa_construction_update_liveness_phis(&senv);
1042 be_liveness_update(to_spill);
1043 len = ARR_LEN(copies);
1044 for(i = 0; i < len; ++i) {
1045 be_liveness_update(lv, copies[i]);
1048 be_ssa_construction_destroy(&senv);
1050 /* need to reconstruct SSA form if we had multiple spills */
1051 if (si->spills != NULL && si->spills->next != NULL) {
1053 int spill_count = 0;
1055 be_ssa_construction_env_t senv;
1057 be_ssa_construction_init(&senv, env->birg);
1059 for( ; spill != NULL; spill = spill->next) {
1060 /* maybe we rematerialized the value and need no spill */
1061 if(spill->spill == NULL)
1063 be_ssa_construction_add_copy(&senv, spill->spill);
1066 if(spill_count > 1) {
1067 /* all reloads are attached to the first spill, fix them now */
1068 be_ssa_construction_fix_users(&senv, si->spills->spill);
1071 be_ssa_construction_destroy(&senv);
1075 si->reloaders = NULL;
1078 stat_ev_dbl("spill_spills", env->spill_count);
1079 stat_ev_dbl("spill_reloads", env->reload_count);
1080 stat_ev_dbl("spill_remats", env->remat_count);
1081 stat_ev_dbl("spill_spilled_phis", env->spilled_phi_count);
1083 /* Matze: In theory be_ssa_construction should take care of the liveness...
1084 * try to disable this again in the future */
1085 be_liveness_invalidate(env->birg->lv);
1087 be_remove_dead_nodes_from_schedule(env->birg);
1089 BE_TIMER_POP(t_ra_spill_apply);
1092 void be_init_spill(void)
1094 FIRM_DBG_REGISTER(dbg, "firm.be.spill");
1097 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spill);