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"
61 #include "beintlive_t.h"
65 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
67 #define REMAT_COST_INFINITE 1000
69 typedef struct reloader_t reloader_t;
72 ir_node *can_spill_after;
74 ir_node *rematted_node;
75 int remat_cost_delta; /** costs needed for rematerialization,
76 compared to placing a reload */
79 typedef struct spill_t spill_t;
82 ir_node *after; /**< spill has to be placed after this node (or earlier) */
86 typedef struct spill_info_t spill_info_t;
88 ir_node *to_spill; /**< the value that should get spilled */
89 reloader_t *reloaders; /**< list of places where the value should get
91 spill_t *spills; /**< list of latest places where spill must be
93 double spill_costs; /**< costs needed for spilling the value */
94 const arch_register_class_t *reload_cls; /** the register class in which the
95 reload should be placed */
99 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 unsigned spill_count;
110 unsigned reload_count;
111 unsigned remat_count;
112 unsigned spilled_phi_count;
116 * Compare two spill infos.
118 static int cmp_spillinfo(const void *x, const void *y, size_t size)
120 const spill_info_t *xx = (const spill_info_t*)x;
121 const spill_info_t *yy = (const spill_info_t*)y;
124 return xx->to_spill != yy->to_spill;
128 * Returns spill info for a specific value (the value that is to be spilled)
130 static spill_info_t *get_spillinfo(const spill_env_t *env, ir_node *value)
132 spill_info_t info, *res;
133 int hash = hash_irn(value);
135 info.to_spill = value;
136 res = (spill_info_t*)set_find(env->spills, &info, sizeof(info), hash);
139 info.reloaders = NULL;
141 info.spill_costs = -1;
142 info.reload_cls = NULL;
143 res = (spill_info_t*)set_insert(env->spills, &info, sizeof(info), hash);
149 spill_env_t *be_new_spill_env(ir_graph *irg)
151 const arch_env_t *arch_env = be_get_irg_arch_env(irg);
153 spill_env_t *env = XMALLOC(spill_env_t);
154 env->spills = new_set(cmp_spillinfo, 1024);
156 env->arch_env = arch_env;
157 ir_nodeset_init(&env->mem_phis);
158 env->spill_cost = arch_env->spill_cost;
159 env->reload_cost = arch_env->reload_cost;
160 env->exec_freq = be_get_irg_exec_freq(irg);
161 obstack_init(&env->obst);
163 env->spill_count = 0;
164 env->reload_count = 0;
165 env->remat_count = 0;
166 env->spilled_phi_count = 0;
171 void be_delete_spill_env(spill_env_t *env)
173 del_set(env->spills);
174 ir_nodeset_destroy(&env->mem_phis);
175 obstack_free(&env->obst, NULL);
181 * | _ \| | __ _ ___ ___ | _ \ ___| | ___ __ _ __| |___
182 * | |_) | |/ _` |/ __/ _ \ | |_) / _ \ |/ _ \ / _` |/ _` / __|
183 * | __/| | (_| | (_| __/ | _ < __/ | (_) | (_| | (_| \__ \
184 * |_| |_|\__,_|\___\___| |_| \_\___|_|\___/ \__,_|\__,_|___/
188 void be_add_spill(spill_env_t *env, ir_node *to_spill, ir_node *after)
190 spill_info_t *spill_info = get_spillinfo(env, to_spill);
195 assert(!arch_irn_is(skip_Proj_const(to_spill), dont_spill));
196 DB((dbg, LEVEL_1, "Add spill of %+F after %+F\n", to_spill, after));
198 /* Just for safety make sure that we do not insert the spill in front of a phi */
199 assert(!is_Phi(sched_next(after)));
201 /* spills that are dominated by others are not needed */
203 s = spill_info->spills;
204 for ( ; s != NULL; s = s->next) {
205 /* no need to add this spill if it is dominated by another */
206 if (value_dominates(s->after, after)) {
207 DB((dbg, LEVEL_1, "...dominated by %+F, not added\n", s->after));
210 /* remove spills that we dominate */
211 if (value_dominates(after, s->after)) {
212 DB((dbg, LEVEL_1, "...remove old spill at %+F\n", s->after));
214 last->next = s->next;
216 spill_info->spills = s->next;
223 spill = OALLOC(&env->obst, spill_t);
224 spill->after = after;
225 spill->next = spill_info->spills;
228 spill_info->spills = spill;
231 void be_add_reload2(spill_env_t *env, ir_node *to_spill, ir_node *before,
232 ir_node *can_spill_after, const arch_register_class_t *reload_cls,
238 assert(!arch_irn_is(skip_Proj_const(to_spill), dont_spill));
240 info = get_spillinfo(env, to_spill);
242 if (is_Phi(to_spill)) {
245 /* create spillinfos for the phi arguments */
246 for (i = 0, arity = get_irn_arity(to_spill); i < arity; ++i) {
247 ir_node *arg = get_irn_n(to_spill, i);
248 get_spillinfo(env, arg);
252 assert(!is_Proj(before) && !be_is_Keep(before));
254 /* put reload into list */
255 rel = OALLOC(&env->obst, reloader_t);
256 rel->next = info->reloaders;
257 rel->reloader = before;
258 rel->rematted_node = NULL;
259 rel->can_spill_after = can_spill_after;
260 rel->remat_cost_delta = allow_remat ? 0 : REMAT_COST_INFINITE;
262 info->reloaders = rel;
263 assert(info->reload_cls == NULL || info->reload_cls == reload_cls);
264 info->reload_cls = reload_cls;
266 DBG((dbg, LEVEL_1, "creating spillinfo for %+F, will be reloaded before %+F, may%s be rematerialized\n",
267 to_spill, before, allow_remat ? "" : " not"));
270 void be_add_reload(spill_env_t *senv, ir_node *to_spill, ir_node *before,
271 const arch_register_class_t *reload_cls, int allow_remat)
273 be_add_reload2(senv, to_spill, before, to_spill, reload_cls, allow_remat);
277 ir_node *be_get_end_of_block_insertion_point(const ir_node *block)
279 ir_node *last = sched_last(block);
281 /* we might have keeps behind the jump... */
282 while (be_is_Keep(last)) {
283 last = sched_prev(last);
284 assert(!sched_is_end(last));
287 assert(is_cfop(last));
289 /* add the reload before the (cond-)jump */
294 * determine final spill position: it should be after all phis, keep nodes
295 * and behind nodes marked as prolog
297 static ir_node *determine_spill_point(ir_node *node)
299 node = skip_Proj(node);
301 ir_node *next = sched_next(node);
302 if (!is_Phi(next) && !be_is_Keep(next) && !be_is_CopyKeep(next))
310 * Returns the point at which you can insert a node that should be executed
311 * before block @p block when coming from pred @p pos.
313 static ir_node *get_block_insertion_point(ir_node *block, int pos)
317 /* simply add the reload to the beginning of the block if we only have 1
318 * predecessor. We don't need to check for phis as there can't be any in a
319 * block with only 1 pred. */
320 if (get_Block_n_cfgpreds(block) == 1) {
321 assert(!is_Phi(sched_first(block)));
322 return sched_first(block);
325 /* We have to reload the value in pred-block */
326 predblock = get_Block_cfgpred_block(block, pos);
327 return be_get_end_of_block_insertion_point(predblock);
330 void be_add_reload_at_end(spill_env_t *env, ir_node *to_spill,
331 const ir_node *block,
332 const arch_register_class_t *reload_cls,
335 ir_node *before = be_get_end_of_block_insertion_point(block);
336 be_add_reload(env, to_spill, before, reload_cls, allow_remat);
339 void be_add_reload_on_edge(spill_env_t *env, ir_node *to_spill, ir_node *block,
340 int pos, const arch_register_class_t *reload_cls,
343 ir_node *before = get_block_insertion_point(block, pos);
344 be_add_reload(env, to_spill, before, reload_cls, allow_remat);
347 void be_spill_phi(spill_env_t *env, ir_node *node)
352 assert(is_Phi(node));
354 ir_nodeset_insert(&env->mem_phis, node);
356 /* create spills for the phi arguments */
357 block = get_nodes_block(node);
358 for (i = 0, arity = get_irn_arity(node); i < arity; ++i) {
359 ir_node *arg = get_irn_n(node, i);
362 /* some backends have virtual noreg/unknown nodes that are not scheduled
363 * and simply always available. */
364 if (!sched_is_scheduled(arg)) {
365 ir_node *pred_block = get_Block_cfgpred_block(block, i);
366 insert = be_get_end_of_block_insertion_point(pred_block);
367 insert = sched_prev(insert);
369 insert = determine_spill_point(arg);
372 be_add_spill(env, arg, insert);
378 * / ___|_ __ ___ __ _| |_ ___ / ___| _ __ (_) | |___
379 * | | | '__/ _ \/ _` | __/ _ \ \___ \| '_ \| | | / __|
380 * | |___| | | __/ (_| | || __/ ___) | |_) | | | \__ \
381 * \____|_| \___|\__,_|\__\___| |____/| .__/|_|_|_|___/
385 static void determine_spill_costs(spill_env_t *env, spill_info_t *spillinfo);
390 * @param senv the spill environment
391 * @param irn the node that should be spilled
392 * @param ctx_irn an user of the spilled node
394 * @return a be_Spill node
396 static void spill_irn(spill_env_t *env, spill_info_t *spillinfo)
398 ir_node *to_spill = spillinfo->to_spill;
399 const ir_node *insn = skip_Proj_const(to_spill);
402 /* determine_spill_costs must have been run before */
403 assert(spillinfo->spill_costs >= 0);
405 /* some backends have virtual noreg/unknown nodes that are not scheduled
406 * and simply always available. */
407 if (!sched_is_scheduled(insn)) {
408 /* override spillinfos or create a new one */
409 ir_graph *irg = get_irn_irg(to_spill);
410 spillinfo->spills->spill = get_irg_no_mem(irg);
411 DB((dbg, LEVEL_1, "don't spill %+F use NoMem\n", to_spill));
415 DBG((dbg, LEVEL_1, "spilling %+F ... \n", to_spill));
416 spill = spillinfo->spills;
417 for ( ; spill != NULL; spill = spill->next) {
418 ir_node *after = spill->after;
419 after = determine_spill_point(after);
421 spill->spill = arch_env_new_spill(env->arch_env, to_spill, after);
422 DB((dbg, LEVEL_1, "\t%+F after %+F\n", spill->spill, after));
425 DBG((dbg, LEVEL_1, "\n"));
428 static void spill_node(spill_env_t *env, spill_info_t *spillinfo);
431 * If the first usage of a Phi result would be out of memory
432 * there is no sense in allocating a register for it.
433 * Thus we spill it and all its operands to the same spill slot.
434 * Therefore the phi/dataB becomes a phi/Memory
436 * @param senv the spill environment
437 * @param phi the Phi node that should be spilled
438 * @param ctx_irn an user of the spilled node
440 static void spill_phi(spill_env_t *env, spill_info_t *spillinfo)
442 ir_graph *irg = env->irg;
443 ir_node *phi = spillinfo->to_spill;
444 ir_node *block = get_nodes_block(phi);
452 assert(!get_opt_cse());
453 DBG((dbg, LEVEL_1, "spilling Phi %+F:\n", phi));
455 /* build a new PhiM */
456 arity = get_irn_arity(phi);
457 ins = ALLOCAN(ir_node*, arity);
458 unknown = new_r_Unknown(irg, mode_M);
459 for (i = 0; i < arity; ++i) {
463 /* override or replace spills list... */
464 spill = OALLOC(&env->obst, spill_t);
465 spill->after = determine_spill_point(phi);
466 spill->spill = be_new_Phi(block, arity, ins, mode_M, NULL);
468 sched_add_after(block, spill->spill);
470 spillinfo->spills = spill;
471 env->spilled_phi_count++;
473 for (i = 0; i < arity; ++i) {
474 ir_node *arg = get_irn_n(phi, i);
475 spill_info_t *arg_info = get_spillinfo(env, arg);
477 determine_spill_costs(env, arg_info);
478 spill_node(env, arg_info);
480 set_irn_n(spill->spill, i, arg_info->spills->spill);
482 DBG((dbg, LEVEL_1, "... done spilling Phi %+F, created PhiM %+F\n", phi,
489 * @param senv the spill environment
490 * @param to_spill the node that should be spilled
492 static void spill_node(spill_env_t *env, spill_info_t *spillinfo)
496 /* node is already spilled */
497 if (spillinfo->spills != NULL && spillinfo->spills->spill != NULL)
500 to_spill = spillinfo->to_spill;
502 if (is_Phi(to_spill) && ir_nodeset_contains(&env->mem_phis, to_spill)) {
503 spill_phi(env, spillinfo);
505 spill_irn(env, spillinfo);
512 * | _ \ ___ _ __ ___ __ _| |_ ___ _ __(_) __ _| (_)_______
513 * | |_) / _ \ '_ ` _ \ / _` | __/ _ \ '__| |/ _` | | |_ / _ \
514 * | _ < __/ | | | | | (_| | || __/ | | | (_| | | |/ / __/
515 * |_| \_\___|_| |_| |_|\__,_|\__\___|_| |_|\__,_|_|_/___\___|
520 * Tests whether value @p arg is available before node @p reloader
521 * @returns 1 if value is available, 0 otherwise
523 static int is_value_available(spill_env_t *env, const ir_node *arg,
524 const ir_node *reloader)
526 if (is_Unknown(arg) || is_NoMem(arg))
529 if (be_is_Spill(skip_Proj_const(arg)))
532 if (arg == get_irg_frame(env->irg))
537 if (get_irn_mode(arg) == mode_T)
541 * Ignore registers are always available
543 if (arch_irn_is_ignore(arg))
550 * Check if a node is rematerializable. This tests for the following conditions:
552 * - The node itself is rematerializable
553 * - All arguments of the node are available or also rematerialisable
554 * - The costs for the rematerialisation operation is less or equal a limit
556 * Returns the costs needed for rematerialisation or something
557 * >= REMAT_COST_INFINITE if remat is not possible.
559 static int check_remat_conditions_costs(spill_env_t *env,
560 const ir_node *spilled, const ir_node *reloader, int parentcosts)
565 const ir_node *insn = skip_Proj_const(spilled);
567 assert(!be_is_Spill(insn));
568 if (!arch_irn_is(insn, rematerializable))
569 return REMAT_COST_INFINITE;
571 if (be_is_Reload(insn)) {
574 costs += arch_get_op_estimated_cost(insn);
576 if (parentcosts + costs >= env->reload_cost + env->spill_cost) {
577 return REMAT_COST_INFINITE;
579 /* never rematerialize a node which modifies the flags.
580 * (would be better to test whether the flags are actually live at point
583 if (arch_irn_is(insn, modify_flags)) {
584 return REMAT_COST_INFINITE;
588 for (i = 0, arity = get_irn_arity(insn); i < arity; ++i) {
589 ir_node *arg = get_irn_n(insn, i);
591 if (is_value_available(env, arg, reloader))
594 /* we have to rematerialize the argument as well */
597 /* we only support rematerializing 1 argument at the moment,
598 * as multiple arguments could increase register pressure */
599 return REMAT_COST_INFINITE;
602 costs += check_remat_conditions_costs(env, arg, reloader,
603 parentcosts + costs);
604 if (parentcosts + costs >= env->reload_cost + env->spill_cost)
605 return REMAT_COST_INFINITE;
612 * Re-materialize a node.
614 * @param env the spill environment
615 * @param spilled the node that was spilled
616 * @param reloader a irn that requires a reload
618 static ir_node *do_remat(spill_env_t *env, ir_node *spilled, ir_node *reloader)
625 if (is_Block(reloader)) {
628 bl = get_nodes_block(reloader);
631 ins = ALLOCAN(ir_node*, get_irn_arity(spilled));
632 for (i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
633 ir_node *arg = get_irn_n(spilled, i);
635 if (is_value_available(env, arg, reloader)) {
638 ins[i] = do_remat(env, arg, reloader);
639 /* don't count the argument rematerialization as an extra remat */
644 /* create a copy of the node */
645 res = new_ir_node(get_irn_dbg_info(spilled), env->irg, bl,
646 get_irn_op(spilled), get_irn_mode(spilled),
647 get_irn_arity(spilled), ins);
648 copy_node_attr(env->irg, spilled, res);
649 arch_env_mark_remat(env->arch_env, res);
651 DBG((dbg, LEVEL_1, "Insert remat %+F of %+F before reloader %+F\n", res, spilled, reloader));
653 if (! is_Proj(res)) {
654 /* insert in schedule */
656 sched_add_before(reloader, res);
663 double be_get_spill_costs(spill_env_t *env, ir_node *to_spill, ir_node *before)
665 ir_node *block = get_nodes_block(before);
666 double freq = get_block_execfreq(env->exec_freq, block);
669 return env->spill_cost * freq;
672 unsigned be_get_reload_costs_no_weight(spill_env_t *env, const ir_node *to_spill,
673 const ir_node *before)
676 /* is the node rematerializable? */
677 unsigned costs = check_remat_conditions_costs(env, to_spill, before, 0);
678 if (costs < (unsigned) env->reload_cost)
682 return env->reload_cost;
685 double be_get_reload_costs(spill_env_t *env, ir_node *to_spill, ir_node *before)
687 ir_node *block = get_nodes_block(before);
688 double freq = get_block_execfreq(env->exec_freq, block);
691 /* is the node rematerializable? */
692 int costs = check_remat_conditions_costs(env, to_spill, before, 0);
693 if (costs < env->reload_cost)
697 return env->reload_cost * freq;
700 int be_is_rematerializable(spill_env_t *env, const ir_node *to_remat,
701 const ir_node *before)
703 return check_remat_conditions_costs(env, to_remat, before, 0) < REMAT_COST_INFINITE;
706 double be_get_reload_costs_on_edge(spill_env_t *env, ir_node *to_spill,
707 ir_node *block, int pos)
709 ir_node *before = get_block_insertion_point(block, pos);
710 return be_get_reload_costs(env, to_spill, before);
713 ir_node *be_new_spill(ir_node *value, ir_node *after)
715 ir_graph *irg = get_irn_irg(value);
716 ir_node *frame = get_irg_frame(irg);
717 const arch_register_class_t *cls = arch_get_irn_reg_class(value);
718 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(frame);
719 ir_node *block = get_block(after);
721 = be_new_Spill(cls, cls_frame, block, frame, value);
723 sched_add_after(after, spill);
727 ir_node *be_new_reload(ir_node *value, ir_node *spill, ir_node *before)
729 ir_graph *irg = get_irn_irg(value);
730 ir_node *frame = get_irg_frame(irg);
731 ir_node *block = get_block(before);
732 const arch_register_class_t *cls = arch_get_irn_reg_class(value);
733 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(frame);
734 ir_mode *mode = get_irn_mode(value);
737 assert(be_is_Spill(spill) || is_Phi(spill));
738 assert(get_irn_mode(spill) == mode_M);
740 reload = be_new_Reload(cls, cls_frame, block, frame, spill, mode);
741 sched_add_before(before, reload);
748 * |_ _|_ __ ___ ___ _ __| |_ | _ \ ___| | ___ __ _ __| |___
749 * | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ |/ _ \ / _` |/ _` / __|
750 * | || | | \__ \ __/ | | |_ | _ < __/ | (_) | (_| | (_| \__ \
751 * |___|_| |_|___/\___|_| \__| |_| \_\___|_|\___/ \__,_|\__,_|___/
756 * analyzes how to best spill a node and determine costs for that
758 static void determine_spill_costs(spill_env_t *env, spill_info_t *spillinfo)
760 ir_node *to_spill = spillinfo->to_spill;
761 const ir_node *insn = skip_Proj_const(to_spill);
762 ir_node *spill_block;
764 double spill_execfreq;
766 /* already calculated? */
767 if (spillinfo->spill_costs >= 0)
770 assert(!arch_irn_is(insn, dont_spill));
771 assert(!be_is_Reload(insn));
773 /* some backends have virtual noreg/unknown nodes that are not scheduled
774 * and simply always available.
775 * TODO: this is kinda hairy, the NoMem is correct for an Unknown as Phi
776 * predecessor (of a PhiM) but this test might match other things too...
778 if (!sched_is_scheduled(insn)) {
779 ir_graph *irg = get_irn_irg(to_spill);
780 /* override spillinfos or create a new one */
781 spill_t *spill = OALLOC(&env->obst, spill_t);
784 spill->spill = get_irg_no_mem(irg);
786 spillinfo->spills = spill;
787 spillinfo->spill_costs = 0;
789 DB((dbg, LEVEL_1, "don't spill %+F use NoMem\n", to_spill));
793 spill_block = get_nodes_block(insn);
794 spill_execfreq = get_block_execfreq(env->exec_freq, spill_block);
796 if (is_Phi(to_spill) && ir_nodeset_contains(&env->mem_phis, to_spill)) {
797 /* TODO calculate correct costs...
798 * (though we can't remat this node anyway so no big problem) */
799 spillinfo->spill_costs = env->spill_cost * spill_execfreq;
803 if (spillinfo->spills != NULL) {
805 double spills_execfreq;
807 /* calculate sum of execution frequencies of individual spills */
809 s = spillinfo->spills;
810 for ( ; s != NULL; s = s->next) {
811 ir_node *spill_block = get_block(s->after);
812 double freq = get_block_execfreq(env->exec_freq, spill_block);
814 spills_execfreq += freq;
817 DB((dbg, LEVEL_1, "%+F: latespillcosts %f after def: %f\n", to_spill,
818 spills_execfreq * env->spill_cost,
819 spill_execfreq * env->spill_cost));
821 /* multi-/latespill is advantageous -> return*/
822 if (spills_execfreq < spill_execfreq) {
823 DB((dbg, LEVEL_1, "use latespills for %+F\n", to_spill));
824 spillinfo->spill_costs = spills_execfreq * env->spill_cost;
829 /* override spillinfos or create a new one */
830 spill = OALLOC(&env->obst, spill_t);
831 spill->after = determine_spill_point(to_spill);
835 spillinfo->spills = spill;
836 spillinfo->spill_costs = spill_execfreq * env->spill_cost;
837 DB((dbg, LEVEL_1, "spill %+F after definition\n", to_spill));
840 void make_spill_locations_dominate_irn(spill_env_t *env, ir_node *irn)
842 const spill_info_t *si = get_spillinfo(env, irn);
843 ir_node *start_block = get_irg_start_block(get_irn_irg(irn));
844 int n_blocks = get_Block_dom_max_subtree_pre_num(start_block);
845 bitset_t *reloads = bitset_alloca(n_blocks);
852 /* Fill the bitset with the dominance pre-order numbers
853 * of the blocks the reloads are located in. */
854 for (r = si->reloaders; r != NULL; r = r->next) {
855 ir_node *bl = get_nodes_block(r->reloader);
856 bitset_set(reloads, get_Block_dom_tree_pre_num(bl));
859 /* Now, cancel out all the blocks that are dominated by each spill.
860 * If the bitset is not empty after that, we have reloads that are
861 * not dominated by any spill. */
862 for (s = si->spills; s != NULL; s = s->next) {
863 ir_node *bl = get_nodes_block(s->after);
864 int start = get_Block_dom_tree_pre_num(bl);
865 int end = get_Block_dom_max_subtree_pre_num(bl);
867 bitset_clear_range(reloads, start, end);
870 if (!bitset_is_empty(reloads))
871 be_add_spill(env, si->to_spill, si->to_spill);
874 void be_insert_spills_reloads(spill_env_t *env)
876 const ir_exec_freq *exec_freq = env->exec_freq;
878 ir_nodeset_iterator_t iter;
881 be_timer_push(T_RA_SPILL_APPLY);
883 /* create all phi-ms first, this is needed so, that phis, hanging on
884 spilled phis work correctly */
885 foreach_ir_nodeset(&env->mem_phis, node, iter) {
886 spill_info_t *info = get_spillinfo(env, node);
887 spill_node(env, info);
890 /* process each spilled node */
891 foreach_set(env->spills, spill_info_t*, si) {
892 ir_node *to_spill = si->to_spill;
893 ir_node **copies = NEW_ARR_F(ir_node*, 0);
894 double all_remat_costs = 0; /** costs when we would remat all nodes */
895 bool force_remat = false;
898 DBG((dbg, LEVEL_1, "\nhandling all reloaders of %+F:\n", to_spill));
900 determine_spill_costs(env, si);
902 /* determine possibility of rematerialisations */
904 /* calculate cost savings for each indivial value when it would
905 be rematted instead of reloaded */
906 for (rld = si->reloaders; rld != NULL; rld = rld->next) {
909 int remat_cost_delta;
911 ir_node *reloader = rld->reloader;
913 if (rld->rematted_node != NULL) {
914 DBG((dbg, LEVEL_2, "\tforced remat %+F before %+F\n",
915 rld->rematted_node, reloader));
918 if (rld->remat_cost_delta >= REMAT_COST_INFINITE) {
919 DBG((dbg, LEVEL_2, "\treload before %+F is forbidden\n",
921 all_remat_costs = REMAT_COST_INFINITE;
925 remat_cost = check_remat_conditions_costs(env, to_spill,
927 if (remat_cost >= REMAT_COST_INFINITE) {
928 DBG((dbg, LEVEL_2, "\tremat before %+F not possible\n",
930 rld->remat_cost_delta = REMAT_COST_INFINITE;
931 all_remat_costs = REMAT_COST_INFINITE;
935 remat_cost_delta = remat_cost - env->reload_cost;
936 rld->remat_cost_delta = remat_cost_delta;
937 block = is_Block(reloader) ? reloader : get_nodes_block(reloader);
938 freq = get_block_execfreq(exec_freq, block);
939 all_remat_costs += remat_cost_delta * freq;
940 DBG((dbg, LEVEL_2, "\tremat costs delta before %+F: "
941 "%d (rel %f)\n", reloader, remat_cost_delta,
942 remat_cost_delta * freq));
944 if (all_remat_costs < REMAT_COST_INFINITE) {
945 /* we don't need the costs for the spill if we can remat
947 all_remat_costs -= si->spill_costs;
949 DBG((dbg, LEVEL_2, "\tspill costs %d (rel %f)\n",
950 env->spill_cost, si->spill_costs));
953 if (all_remat_costs < 0) {
954 DBG((dbg, LEVEL_1, "\nforcing remats of all reloaders (%f)\n",
960 /* go through all reloads for this spill */
961 for (rld = si->reloaders; rld != NULL; rld = rld->next) {
962 ir_node *copy; /* a reload is a "copy" of the original value */
964 if (rld->rematted_node != NULL) {
965 copy = rld->rematted_node;
966 sched_add_before(rld->reloader, copy);
967 } else if (be_do_remats &&
968 (force_remat || rld->remat_cost_delta < 0)) {
969 copy = do_remat(env, to_spill, rld->reloader);
971 /* make sure we have a spill */
974 /* create a reload, use the first spill for now SSA
975 * reconstruction for memory comes below */
976 assert(si->spills != NULL);
977 copy = arch_env_new_reload(env->arch_env, si->to_spill,
978 si->spills->spill, rld->reloader);
982 DBG((dbg, LEVEL_1, " %+F of %+F before %+F\n",
983 copy, to_spill, rld->reloader));
984 ARR_APP1(ir_node*, copies, copy);
987 /* if we had any reloads or remats, then we need to reconstruct the
988 * SSA form for the spilled value */
989 if (ARR_LEN(copies) > 0) {
990 be_ssa_construction_env_t senv;
991 /* be_lv_t *lv = be_get_irg_liveness(env->irg); */
993 be_ssa_construction_init(&senv, env->irg);
994 be_ssa_construction_add_copy(&senv, to_spill);
995 be_ssa_construction_add_copies(&senv, copies, ARR_LEN(copies));
996 be_ssa_construction_fix_users(&senv, to_spill);
999 /* no need to enable this as long as we invalidate liveness
1000 after this function... */
1001 be_ssa_construction_update_liveness_phis(&senv);
1002 be_liveness_update(to_spill);
1003 len = ARR_LEN(copies);
1004 for (i = 0; i < len; ++i) {
1005 be_liveness_update(lv, copies[i]);
1008 be_ssa_construction_destroy(&senv);
1010 /* need to reconstruct SSA form if we had multiple spills */
1011 if (si->spills != NULL && si->spills->next != NULL) {
1013 int spill_count = 0;
1015 be_ssa_construction_env_t senv;
1017 be_ssa_construction_init(&senv, env->irg);
1019 for ( ; spill != NULL; spill = spill->next) {
1020 /* maybe we rematerialized the value and need no spill */
1021 if (spill->spill == NULL)
1023 be_ssa_construction_add_copy(&senv, spill->spill);
1026 if (spill_count > 1) {
1027 /* all reloads are attached to the first spill, fix them now */
1028 be_ssa_construction_fix_users(&senv, si->spills->spill);
1031 be_ssa_construction_destroy(&senv);
1035 si->reloaders = NULL;
1038 stat_ev_dbl("spill_spills", env->spill_count);
1039 stat_ev_dbl("spill_reloads", env->reload_count);
1040 stat_ev_dbl("spill_remats", env->remat_count);
1041 stat_ev_dbl("spill_spilled_phis", env->spilled_phi_count);
1043 /* Matze: In theory be_ssa_construction should take care of the liveness...
1044 * try to disable this again in the future */
1045 be_liveness_invalidate(be_get_irg_liveness(env->irg));
1047 be_remove_dead_nodes_from_schedule(env->irg);
1049 be_timer_pop(T_RA_SPILL_APPLY);
1052 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spill)
1053 void be_init_spill(void)
1055 FIRM_DBG_REGISTER(dbg, "firm.be.spill");