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;
154 ir_graph *irg = be_get_birg_irg(birg);
156 spill_env_t *env = XMALLOC(spill_env_t);
157 env->spills = new_set(cmp_spillinfo, 1024);
160 env->arch_env = arch_env;
161 ir_nodeset_init(&env->mem_phis);
162 env->spill_cost = arch_env->spill_cost;
163 env->reload_cost = arch_env->reload_cost;
164 env->exec_freq = be_get_irg_exec_freq(irg);
165 obstack_init(&env->obst);
167 #ifdef FIRM_STATISTICS
168 env->spill_count = 0;
169 env->reload_count = 0;
170 env->remat_count = 0;
171 env->spilled_phi_count = 0;
177 void be_delete_spill_env(spill_env_t *env)
179 del_set(env->spills);
180 ir_nodeset_destroy(&env->mem_phis);
181 obstack_free(&env->obst, NULL);
187 * | _ \| | __ _ ___ ___ | _ \ ___| | ___ __ _ __| |___
188 * | |_) | |/ _` |/ __/ _ \ | |_) / _ \ |/ _ \ / _` |/ _` / __|
189 * | __/| | (_| | (_| __/ | _ < __/ | (_) | (_| | (_| \__ \
190 * |_| |_|\__,_|\___\___| |_| \_\___|_|\___/ \__,_|\__,_|___/
194 void be_add_spill(spill_env_t *env, ir_node *to_spill, ir_node *after)
196 spill_info_t *spill_info = get_spillinfo(env, to_spill);
197 const ir_node *insn = skip_Proj_const(to_spill);
202 assert(!arch_irn_is(insn, dont_spill));
203 DB((dbg, LEVEL_1, "Add spill of %+F after %+F\n", to_spill, after));
205 /* Just for safety make sure that we do not insert the spill in front of a phi */
206 assert(!is_Phi(sched_next(after)));
208 /* spills that are dominated by others are not needed */
210 s = spill_info->spills;
211 for ( ; s != NULL; s = s->next) {
212 /* no need to add this spill if it is dominated by another */
213 if (value_dominates(s->after, after)) {
214 DB((dbg, LEVEL_1, "...dominated by %+F, not added\n", s->after));
217 /* remove spills that we dominate */
218 if (value_dominates(after, s->after)) {
219 DB((dbg, LEVEL_1, "...remove old spill at %+F\n", s->after));
221 last->next = s->next;
223 spill_info->spills = s->next;
230 spill = OALLOC(&env->obst, spill_t);
231 spill->after = after;
232 spill->next = spill_info->spills;
235 spill_info->spills = spill;
238 void be_add_remat(spill_env_t *env, ir_node *to_spill, ir_node *before,
239 ir_node *rematted_node)
241 spill_info_t *spill_info;
242 reloader_t *reloader;
244 spill_info = get_spillinfo(env, to_spill);
246 /* add the remat information */
247 reloader = OALLOC(&env->obst, reloader_t);
248 reloader->next = spill_info->reloaders;
249 reloader->reloader = before;
250 reloader->rematted_node = rematted_node;
251 reloader->remat_cost_delta = 0; /* We will never have a cost win over a
252 reload since we're not even allowed to
255 spill_info->reloaders = reloader;
257 DBG((dbg, LEVEL_1, "creating spillinfo for %+F, will be rematerialized before %+F\n",
261 void be_add_reload2(spill_env_t *env, ir_node *to_spill, ir_node *before,
262 ir_node *can_spill_after, const arch_register_class_t *reload_cls,
267 const ir_node *insn = skip_Proj_const(to_spill);
269 assert(!arch_irn_is(insn, 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 = OALLOC(&env->obst, reloader_t);
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) && !be_is_CopyKeep(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)
378 assert(is_Phi(node));
380 ir_nodeset_insert(&env->mem_phis, node);
382 /* create spills for the phi arguments */
383 block = get_nodes_block(node);
384 for (i = 0, arity = get_irn_arity(node); i < arity; ++i) {
385 ir_node *arg = get_irn_n(node, i);
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;
425 const ir_node *insn = skip_Proj_const(to_spill);
428 /* determine_spill_costs must have been run before */
429 assert(spillinfo->spill_costs >= 0);
431 /* some backends have virtual noreg/unknown nodes that are not scheduled
432 * and simply always available. */
433 if (!sched_is_scheduled(insn)) {
434 /* override spillinfos or create a new one */
435 spillinfo->spills->spill = new_NoMem();
436 DB((dbg, LEVEL_1, "don't spill %+F use NoMem\n", to_spill));
440 DBG((dbg, LEVEL_1, "spilling %+F ... \n", to_spill));
441 spill = spillinfo->spills;
442 for ( ; spill != NULL; spill = spill->next) {
443 ir_node *after = spill->after;
444 ir_node *block = get_block(after);
446 after = skip_keeps_phis(after);
448 spill->spill = be_spill(block, to_spill);
449 sched_add_after(skip_Proj(after), spill->spill);
450 DB((dbg, LEVEL_1, "\t%+F after %+F\n", spill->spill, after));
451 #ifdef FIRM_STATISTICS
455 DBG((dbg, LEVEL_1, "\n"));
458 static void spill_node(spill_env_t *env, spill_info_t *spillinfo);
461 * If the first usage of a Phi result would be out of memory
462 * there is no sense in allocating a register for it.
463 * Thus we spill it and all its operands to the same spill slot.
464 * Therefore the phi/dataB becomes a phi/Memory
466 * @param senv the spill environment
467 * @param phi the Phi node that should be spilled
468 * @param ctx_irn an user of the spilled node
470 static void spill_phi(spill_env_t *env, spill_info_t *spillinfo)
472 ir_graph *irg = env->irg;
473 ir_node *phi = spillinfo->to_spill;
474 ir_node *block = get_nodes_block(phi);
482 assert(!get_opt_cse());
483 DBG((dbg, LEVEL_1, "spilling Phi %+F:\n", phi));
485 /* build a new PhiM */
486 arity = get_irn_arity(phi);
487 ins = ALLOCAN(ir_node*, arity);
488 unknown = new_r_Unknown(irg, mode_M);
489 for (i = 0; i < arity; ++i) {
493 /* override or replace spills list... */
494 spill = OALLOC(&env->obst, spill_t);
495 spill->after = skip_keeps_phis(phi);
496 spill->spill = new_r_Phi(block, arity, ins, mode_M);
499 spillinfo->spills = spill;
500 #ifdef FIRM_STATISTICS
501 env->spilled_phi_count++;
504 for (i = 0; i < arity; ++i) {
505 ir_node *arg = get_irn_n(phi, i);
506 spill_info_t *arg_info = get_spillinfo(env, arg);
508 determine_spill_costs(env, arg_info);
509 spill_node(env, arg_info);
511 set_irn_n(spill->spill, i, arg_info->spills->spill);
513 DBG((dbg, LEVEL_1, "... done spilling Phi %+F, created PhiM %+F\n", phi,
520 * @param senv the spill environment
521 * @param to_spill the node that should be spilled
523 static void spill_node(spill_env_t *env, spill_info_t *spillinfo)
527 /* node is already spilled */
528 if (spillinfo->spills != NULL && spillinfo->spills->spill != NULL)
531 to_spill = spillinfo->to_spill;
533 if (is_Phi(to_spill) && ir_nodeset_contains(&env->mem_phis, to_spill)) {
534 spill_phi(env, spillinfo);
536 spill_irn(env, spillinfo);
543 * | _ \ ___ _ __ ___ __ _| |_ ___ _ __(_) __ _| (_)_______
544 * | |_) / _ \ '_ ` _ \ / _` | __/ _ \ '__| |/ _` | | |_ / _ \
545 * | _ < __/ | | | | | (_| | || __/ | | | (_| | | |/ / __/
546 * |_| \_\___|_| |_| |_|\__,_|\__\___|_| |_|\__,_|_|_/___\___|
551 * Tests whether value @p arg is available before node @p reloader
552 * @returns 1 if value is available, 0 otherwise
554 static int is_value_available(spill_env_t *env, const ir_node *arg,
555 const ir_node *reloader)
557 if (is_Unknown(arg) || arg == new_NoMem())
560 if (be_is_Spill(skip_Proj_const(arg)))
563 if (arg == get_irg_frame(env->irg))
568 if (get_irn_mode(arg) == mode_T)
572 * Ignore registers are always available
574 if (arch_irn_is_ignore(arg))
581 * Check if a node is rematerializable. This tests for the following conditions:
583 * - The node itself is rematerializable
584 * - All arguments of the node are available or also rematerialisable
585 * - The costs for the rematerialisation operation is less or equal a limit
587 * Returns the costs needed for rematerialisation or something
588 * >= REMAT_COST_INFINITE if remat is not possible.
590 static int check_remat_conditions_costs(spill_env_t *env,
591 const ir_node *spilled, const ir_node *reloader, int parentcosts)
596 const ir_node *insn = skip_Proj_const(spilled);
598 assert(!be_is_Spill(insn));
599 if (!arch_irn_is(insn, rematerializable))
600 return REMAT_COST_INFINITE;
602 if (be_is_Reload(insn)) {
605 costs += arch_get_op_estimated_cost(insn);
607 if (parentcosts + costs >= env->reload_cost + env->spill_cost) {
608 return REMAT_COST_INFINITE;
610 /* never rematerialize a node which modifies the flags.
611 * (would be better to test wether the flags are actually live at point
614 if (arch_irn_is(insn, modify_flags)) {
615 return REMAT_COST_INFINITE;
619 for (i = 0, arity = get_irn_arity(insn); i < arity; ++i) {
620 ir_node *arg = get_irn_n(insn, i);
622 if (is_value_available(env, arg, reloader))
625 /* we have to rematerialize the argument as well */
628 /* we only support rematerializing 1 argument at the moment,
629 * as multiple arguments could increase register pressure */
630 return REMAT_COST_INFINITE;
633 costs += check_remat_conditions_costs(env, arg, reloader,
634 parentcosts + costs);
635 if (parentcosts + costs >= env->reload_cost + env->spill_cost)
636 return REMAT_COST_INFINITE;
643 * Re-materialize a node.
645 * @param senv the spill environment
646 * @param spilled the node that was spilled
647 * @param reloader a irn that requires a reload
649 static ir_node *do_remat(spill_env_t *env, ir_node *spilled, ir_node *reloader)
656 if (is_Block(reloader)) {
659 bl = get_nodes_block(reloader);
662 ins = ALLOCAN(ir_node*, get_irn_arity(spilled));
663 for (i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
664 ir_node *arg = get_irn_n(spilled, i);
666 if (is_value_available(env, arg, reloader)) {
669 ins[i] = do_remat(env, arg, reloader);
670 #ifdef FIRM_STATISTICS
671 /* don't count the recursive call as remat */
677 /* create a copy of the node */
678 res = new_ir_node(get_irn_dbg_info(spilled), env->irg, bl,
679 get_irn_op(spilled), get_irn_mode(spilled),
680 get_irn_arity(spilled), ins);
681 copy_node_attr(env->irg, spilled, res);
682 arch_env_mark_remat(env->arch_env, res);
684 DBG((dbg, LEVEL_1, "Insert remat %+F of %+F before reloader %+F\n", res, spilled, reloader));
686 if (! is_Proj(res)) {
687 /* insert in schedule */
689 sched_add_before(reloader, res);
690 #ifdef FIRM_STATISTICS
698 double be_get_spill_costs(spill_env_t *env, ir_node *to_spill, ir_node *before)
700 ir_node *block = get_nodes_block(before);
701 double freq = get_block_execfreq(env->exec_freq, block);
704 return env->spill_cost * freq;
707 unsigned be_get_reload_costs_no_weight(spill_env_t *env, const ir_node *to_spill,
708 const ir_node *before)
711 /* is the node rematerializable? */
712 unsigned costs = check_remat_conditions_costs(env, to_spill, before, 0);
713 if (costs < (unsigned) env->reload_cost)
717 return env->reload_cost;
720 double be_get_reload_costs(spill_env_t *env, ir_node *to_spill, ir_node *before)
722 ir_node *block = get_nodes_block(before);
723 double freq = get_block_execfreq(env->exec_freq, block);
726 /* is the node rematerializable? */
727 int costs = check_remat_conditions_costs(env, to_spill, before, 0);
728 if (costs < env->reload_cost)
732 return env->reload_cost * freq;
735 int be_is_rematerializable(spill_env_t *env, const ir_node *to_remat,
736 const ir_node *before)
738 return check_remat_conditions_costs(env, to_remat, before, 0) < REMAT_COST_INFINITE;
741 double be_get_reload_costs_on_edge(spill_env_t *env, ir_node *to_spill,
742 ir_node *block, int pos)
744 ir_node *before = get_block_insertion_point(block, pos);
745 return be_get_reload_costs(env, to_spill, before);
750 * |_ _|_ __ ___ ___ _ __| |_ | _ \ ___| | ___ __ _ __| |___
751 * | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ |/ _ \ / _` |/ _` / __|
752 * | || | | \__ \ __/ | | |_ | _ < __/ | (_) | (_| | (_| \__ \
753 * |___|_| |_|___/\___|_| \__| |_| \_\___|_|\___/ \__,_|\__,_|___/
758 * analyzes how to best spill a node and determine costs for that
760 static void determine_spill_costs(spill_env_t *env, spill_info_t *spillinfo)
762 ir_node *to_spill = spillinfo->to_spill;
763 const ir_node *insn = skip_Proj_const(to_spill);
764 ir_node *spill_block;
766 double spill_execfreq;
768 /* already calculated? */
769 if (spillinfo->spill_costs >= 0)
772 assert(!arch_irn_is(insn, dont_spill));
773 assert(!be_is_Reload(insn));
775 /* some backends have virtual noreg/unknown nodes that are not scheduled
776 * and simply always available.
777 * TODO: this is kinda hairy, the NoMem is correct for an Unknown as Phi
778 * predecessor (of a PhiM) but this test might match other things too...
780 if (!sched_is_scheduled(insn)) {
781 /* override spillinfos or create a new one */
782 spill_t *spill = OALLOC(&env->obst, spill_t);
785 spill->spill = new_NoMem();
787 spillinfo->spills = spill;
788 spillinfo->spill_costs = 0;
790 DB((dbg, LEVEL_1, "don't spill %+F use NoMem\n", to_spill));
794 spill_block = get_nodes_block(insn);
795 spill_execfreq = get_block_execfreq(env->exec_freq, spill_block);
797 if (is_Phi(to_spill) && ir_nodeset_contains(&env->mem_phis, to_spill)) {
798 /* TODO calculate correct costs...
799 * (though we can't remat this node anyway so no big problem) */
800 spillinfo->spill_costs = env->spill_cost * spill_execfreq;
804 if (spillinfo->spills != NULL) {
806 double spills_execfreq;
808 /* calculate sum of execution frequencies of individual spills */
810 s = spillinfo->spills;
811 for ( ; s != NULL; s = s->next) {
812 ir_node *spill_block = get_block(s->after);
813 double freq = get_block_execfreq(env->exec_freq, spill_block);
815 spills_execfreq += freq;
818 DB((dbg, LEVEL_1, "%+F: latespillcosts %f after def: %f\n", to_spill,
819 spills_execfreq * env->spill_cost,
820 spill_execfreq * env->spill_cost));
822 /* multi-/latespill is advantageous -> return*/
823 if (spills_execfreq < spill_execfreq) {
824 DB((dbg, LEVEL_1, "use latespills for %+F\n", to_spill));
825 spillinfo->spill_costs = spills_execfreq * env->spill_cost;
830 /* override spillinfos or create a new one */
831 spill = OALLOC(&env->obst, spill_t);
832 spill->after = skip_keeps_phis(to_spill);
836 spillinfo->spills = spill;
837 spillinfo->spill_costs = spill_execfreq * env->spill_cost;
838 DB((dbg, LEVEL_1, "spill %+F after definition\n", to_spill));
841 void make_spill_locations_dominate_irn(spill_env_t *env, ir_node *irn)
843 const spill_info_t *si = get_spillinfo(env, irn);
844 ir_node *start_block = get_irg_start_block(get_irn_irg(irn));
845 int n_blocks = get_Block_dom_max_subtree_pre_num(start_block);
846 bitset_t *reloads = bitset_alloca(n_blocks);
853 /* Fill the bitset with the dominance pre-order numbers
854 * of the blocks the reloads are located in. */
855 for (r = si->reloaders; r != NULL; r = r->next) {
856 ir_node *bl = get_nodes_block(r->reloader);
857 bitset_set(reloads, get_Block_dom_tree_pre_num(bl));
860 /* Now, cancel out all the blocks that are dominated by each spill.
861 * If the bitset is not empty after that, we have reloads that are
862 * not dominated by any spill. */
863 for (s = si->spills; s != NULL; s = s->next) {
864 ir_node *bl = get_nodes_block(s->after);
865 int start = get_Block_dom_tree_pre_num(bl);
866 int end = get_Block_dom_max_subtree_pre_num(bl);
868 bitset_clear_range(reloads, start, end);
871 if (!bitset_is_empty(reloads))
872 be_add_spill(env, si->to_spill, si->to_spill);
875 void be_insert_spills_reloads(spill_env_t *env)
877 const ir_exec_freq *exec_freq = env->exec_freq;
879 ir_nodeset_iterator_t iter;
882 be_timer_push(T_RA_SPILL_APPLY);
884 /* create all phi-ms first, this is needed so, that phis, hanging on
885 spilled phis work correctly */
886 foreach_ir_nodeset(&env->mem_phis, node, iter) {
887 spill_info_t *info = get_spillinfo(env, node);
888 spill_node(env, info);
891 /* process each spilled node */
892 for (si = set_first(env->spills); si; si = set_next(env->spills)) {
894 ir_node *to_spill = si->to_spill;
895 ir_mode *mode = get_irn_mode(to_spill);
896 ir_node **copies = NEW_ARR_F(ir_node*, 0);
897 double all_remat_costs = 0; /** costs when we would remat all nodes */
900 DBG((dbg, LEVEL_1, "\nhandling all reloaders of %+F:\n", to_spill));
902 determine_spill_costs(env, si);
904 /* determine possibility of rematerialisations */
906 /* calculate cost savings for each indivial value when it would
907 be rematted instead of reloaded */
908 for (rld = si->reloaders; rld != NULL; rld = rld->next) {
911 int remat_cost_delta;
913 ir_node *reloader = rld->reloader;
915 if (rld->rematted_node != NULL) {
916 DBG((dbg, LEVEL_2, "\tforced remat %+F before %+F\n",
917 rld->rematted_node, reloader));
920 if (rld->remat_cost_delta >= REMAT_COST_INFINITE) {
921 DBG((dbg, LEVEL_2, "\treload before %+F is forbidden\n",
923 all_remat_costs = REMAT_COST_INFINITE;
927 remat_cost = check_remat_conditions_costs(env, to_spill,
929 if (remat_cost >= REMAT_COST_INFINITE) {
930 DBG((dbg, LEVEL_2, "\tremat before %+F not possible\n",
932 rld->remat_cost_delta = REMAT_COST_INFINITE;
933 all_remat_costs = REMAT_COST_INFINITE;
937 remat_cost_delta = remat_cost - env->reload_cost;
938 rld->remat_cost_delta = remat_cost_delta;
939 block = is_Block(reloader) ? reloader : get_nodes_block(reloader);
940 freq = get_block_execfreq(exec_freq, block);
941 all_remat_costs += remat_cost_delta * freq;
942 DBG((dbg, LEVEL_2, "\tremat costs delta before %+F: "
943 "%d (rel %f)\n", reloader, remat_cost_delta,
944 remat_cost_delta * freq));
946 if (all_remat_costs < REMAT_COST_INFINITE) {
947 /* we don't need the costs for the spill if we can remat
949 all_remat_costs -= si->spill_costs;
951 DBG((dbg, LEVEL_2, "\tspill costs %d (rel %f)\n",
952 env->spill_cost, si->spill_costs));
955 if (all_remat_costs < 0) {
956 DBG((dbg, LEVEL_1, "\nforcing remats of all reloaders (%f)\n",
962 /* go through all reloads for this spill */
963 for (rld = si->reloaders; rld != NULL; rld = rld->next) {
964 ir_node *copy; /* a reload is a "copy" of the original value */
966 if (rld->rematted_node != NULL) {
967 copy = rld->rematted_node;
968 sched_add_before(rld->reloader, copy);
969 } else if (be_do_remats &&
970 (force_remat || rld->remat_cost_delta < 0)) {
971 copy = do_remat(env, to_spill, rld->reloader);
973 /* make sure we have a spill */
976 /* create a reload, use the first spill for now SSA
977 * reconstruction for memory comes below */
978 assert(si->spills != NULL);
979 copy = be_reload(si->reload_cls, rld->reloader, mode,
981 #ifdef FIRM_STATISTICS
986 DBG((dbg, LEVEL_1, " %+F of %+F before %+F\n",
987 copy, to_spill, rld->reloader));
988 ARR_APP1(ir_node*, copies, copy);
991 /* if we had any reloads or remats, then we need to reconstruct the
992 * SSA form for the spilled value */
993 if (ARR_LEN(copies) > 0) {
994 be_ssa_construction_env_t senv;
995 /* be_lv_t *lv = be_get_irg_liveness(env->irg); */
997 be_ssa_construction_init(&senv, env->birg);
998 be_ssa_construction_add_copy(&senv, to_spill);
999 be_ssa_construction_add_copies(&senv, copies, ARR_LEN(copies));
1000 be_ssa_construction_fix_users(&senv, to_spill);
1003 /* no need to enable this as long as we invalidate liveness
1004 after this function... */
1005 be_ssa_construction_update_liveness_phis(&senv);
1006 be_liveness_update(to_spill);
1007 len = ARR_LEN(copies);
1008 for (i = 0; i < len; ++i) {
1009 be_liveness_update(lv, copies[i]);
1012 be_ssa_construction_destroy(&senv);
1014 /* need to reconstruct SSA form if we had multiple spills */
1015 if (si->spills != NULL && si->spills->next != NULL) {
1017 int spill_count = 0;
1019 be_ssa_construction_env_t senv;
1021 be_ssa_construction_init(&senv, env->birg);
1023 for ( ; spill != NULL; spill = spill->next) {
1024 /* maybe we rematerialized the value and need no spill */
1025 if (spill->spill == NULL)
1027 be_ssa_construction_add_copy(&senv, spill->spill);
1030 if (spill_count > 1) {
1031 /* all reloads are attached to the first spill, fix them now */
1032 be_ssa_construction_fix_users(&senv, si->spills->spill);
1035 be_ssa_construction_destroy(&senv);
1039 si->reloaders = NULL;
1042 stat_ev_dbl("spill_spills", env->spill_count);
1043 stat_ev_dbl("spill_reloads", env->reload_count);
1044 stat_ev_dbl("spill_remats", env->remat_count);
1045 stat_ev_dbl("spill_spilled_phis", env->spilled_phi_count);
1047 /* Matze: In theory be_ssa_construction should take care of the liveness...
1048 * try to disable this again in the future */
1049 be_liveness_invalidate(env->birg->lv);
1051 be_remove_dead_nodes_from_schedule(env->birg);
1053 be_timer_pop(T_RA_SPILL_APPLY);
1056 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spill);
1057 void be_init_spill(void)
1059 FIRM_DBG_REGISTER(dbg, "firm.be.spill");