2 * Author: Daniel Grund, Sebastian Hack, Matthias Braun
4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
17 #include "iredges_t.h"
18 #include "irbackedge_t.h"
27 #include "unionfind.h"
31 #include "besched_t.h"
35 #include "bechordal_t.h"
36 #include "bejavacoal.h"
37 #include "benodesets.h"
38 #include "bespilloptions.h"
39 #include "bestatevent.h"
41 // only rematerialise when costs are less than REMAT_COST_LIMIT
42 // TODO determine a good value here...
43 #define REMAT_COST_LIMIT 10
45 typedef struct _reloader_t reloader_t;
50 ir_node *rematted_node;
51 int allow_remat; /**< the node may be rematted instead of reloaded if global remat option is on */
54 typedef struct _spill_info_t {
55 /** the value that should get spilled */
56 ir_node *spilled_node;
57 /** list of places where the value should get reloaded */
58 reloader_t *reloaders;
60 /** the spill node, or a PhiM node */
62 /** if we had the value of a phi spilled before but not the phi itself then
63 * this field contains the spill for the phi value */
66 /** the register class in which the reload should be placed */
67 const arch_register_class_t *reload_cls;
71 const arch_env_t *arch_env;
75 int spill_cost; /**< the cost of a single spill node */
76 int reload_cost; /**< the cost of a reload node */
77 set *spills; /**< all spill_info_t's, which must be placed */
78 pset *mem_phis; /**< set of all special spilled phis. allocated and freed separately */
80 DEBUG_ONLY(firm_dbg_module_t *dbg;)
84 * Compare two spill infos.
86 static int cmp_spillinfo(const void *x, const void *y, size_t size) {
87 const spill_info_t *xx = x;
88 const spill_info_t *yy = y;
89 return xx->spilled_node != yy->spilled_node;
93 * Returns spill info for a specific value (returns NULL if the info doesn't
96 static spill_info_t *find_spillinfo(const spill_env_t *env, ir_node *value) {
98 int hash = nodeset_hash(value);
100 info.spilled_node = value;
102 return set_find(env->spills, &info, sizeof(info), hash);
106 * Returns spill info for a specific value (the value that is to be spilled)
108 static spill_info_t *get_spillinfo(const spill_env_t *env, ir_node *value) {
109 spill_info_t info, *res;
110 int hash = nodeset_hash(value);
112 info.spilled_node = value;
113 res = set_find(env->spills, &info, sizeof(info), hash);
116 info.reloaders = NULL;
118 info.old_spill = NULL;
119 info.reload_cls = NULL;
120 res = set_insert(env->spills, &info, sizeof(info), hash);
127 /* Sets the debug module of a spill environment. */
128 void be_set_spill_env_dbg_module(spill_env_t *env, firm_dbg_module_t *dbg) {
133 /* Creates a new spill environment. */
134 spill_env_t *be_new_spill_env(be_irg_t *birg) {
135 spill_env_t *env = xmalloc(sizeof(env[0]));
136 env->spills = new_set(cmp_spillinfo, 1024);
137 env->irg = be_get_birg_irg(birg);
139 env->arch_env = birg->main_env->arch_env;
140 env->mem_phis = pset_new_ptr_default();
141 // TODO, ask backend about costs..., or even ask backend whether we should
144 env->reload_cost = 5;
145 obstack_init(&env->obst);
149 /* Deletes a spill environment. */
150 void be_delete_spill_env(spill_env_t *env) {
151 del_set(env->spills);
152 del_pset(env->mem_phis);
153 obstack_free(&env->obst, NULL);
159 * | _ \| | __ _ ___ ___ | _ \ ___| | ___ __ _ __| |___
160 * | |_) | |/ _` |/ __/ _ \ | |_) / _ \ |/ _ \ / _` |/ _` / __|
161 * | __/| | (_| | (_| __/ | _ < __/ | (_) | (_| | (_| \__ \
162 * |_| |_|\__,_|\___\___| |_| \_\___|_|\___/ \__,_|\__,_|___/
166 void be_add_remat(spill_env_t *env, ir_node *to_spill, ir_node *before, ir_node *rematted_node) {
167 spill_info_t *spill_info;
168 reloader_t *reloader;
170 spill_info = get_spillinfo(env, to_spill);
172 /* add the remat information */
173 reloader = obstack_alloc(&env->obst, sizeof(reloader[0]));
174 reloader->next = spill_info->reloaders;
175 reloader->reloader = before;
176 reloader->rematted_node = rematted_node;
177 reloader->allow_remat = 1;
179 spill_info->reloaders = reloader;
181 DBG((env->dbg, LEVEL_1, "creating spillinfo for %+F, will be rematerialized before %+F\n",
185 void be_add_reload(spill_env_t *env, ir_node *to_spill, ir_node *before,
186 const arch_register_class_t *reload_cls, int allow_remat)
191 info = get_spillinfo(env, to_spill);
193 if (is_Phi(to_spill)) {
196 /* create spillinfos for the phi arguments */
197 for (i = 0, arity = get_irn_arity(to_spill); i < arity; ++i) {
198 ir_node *arg = get_irn_n(to_spill, i);
199 get_spillinfo(env, arg);
203 // hackery... sometimes the morgan algo spilled the value of a phi,
204 // the belady algo decides later to spill the whole phi, then sees the
205 // spill node and adds a reload for that spill node, problem is the
206 // reload gets attach to that same spill (and is totally unnecessary)
207 if (info->old_spill != NULL &&
208 (before == info->old_spill || value_dominates(before, info->old_spill)))
210 printf("spilledphi hack was needed...\n");
211 before = sched_next(info->old_spill);
216 /* put reload into list */
217 rel = obstack_alloc(&env->obst, sizeof(rel[0]));
218 rel->next = info->reloaders;
219 rel->reloader = before;
220 rel->rematted_node = NULL;
221 rel->allow_remat = allow_remat;
223 info->reloaders = rel;
224 assert(info->reload_cls == NULL || info->reload_cls == reload_cls);
225 info->reload_cls = reload_cls;
227 DBG((env->dbg, LEVEL_1, "creating spillinfo for %+F, will be reloaded before %+F, may%s be rematerialized\n",
228 to_spill, before, allow_remat ? "" : " not"));
231 static ir_node *get_reload_insertion_point(ir_node *block, int pos) {
232 ir_node *predblock, *last;
234 /* simply add the reload to the beginning of the block if we only have 1 predecessor
235 * (we don't need to check for phis as there can't be any in a block with only 1 pred)
237 if(get_Block_n_cfgpreds(block) == 1) {
238 assert(!is_Phi(sched_first(block)));
239 return sched_first(block);
242 /* We have to reload the value in pred-block */
243 predblock = get_Block_cfgpred_block(block, pos);
244 last = sched_last(predblock);
246 /* we might have projs and keepanys behind the jump... */
247 while(is_Proj(last) || be_is_Keep(last)) {
248 last = sched_prev(last);
249 assert(!sched_is_end(last));
253 ir_graph *irg = get_irn_irg(block);
254 ir_node *startblock = get_irg_start_block(irg);
256 last = sched_next(last);
257 // last node must be a cfop, only exception is the start block
258 assert(last == startblock);
261 // add the reload before the (cond-)jump
265 void be_add_reload_on_edge(spill_env_t *env, ir_node *to_spill, ir_node *block, int pos,
266 const arch_register_class_t *reload_cls, int allow_remat)
268 ir_node *before = get_reload_insertion_point(block, pos);
269 be_add_reload(env, to_spill, before, reload_cls, allow_remat);
272 void be_spill_phi(spill_env_t *env, ir_node *node) {
276 assert(is_Phi(node));
278 pset_insert_ptr(env->mem_phis, node);
280 // create spillinfos for the phi arguments
281 spill = get_spillinfo(env, node);
282 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
283 ir_node *arg = get_irn_n(node, i);
284 get_spillinfo(env, arg);
287 // if we had a spill for the phi value before, then remove this spill from
288 // schedule, as we will remove it in the insert spill/reload phase
289 if(spill->spill != NULL && !is_Phi(spill->spill)) {
290 assert(spill->old_spill == NULL);
291 spill->old_spill = spill->spill;
298 * / ___|_ __ ___ __ _| |_ ___ / ___| _ __ (_) | |___
299 * | | | '__/ _ \/ _` | __/ _ \ \___ \| '_ \| | | / __|
300 * | |___| | | __/ (_| | || __/ ___) | |_) | | | \__ \
301 * \____|_| \___|\__,_|\__\___| |____/| .__/|_|_|_|___/
306 * Schedules a node after an instruction. (That is the place after all projs and phis
307 * that are scheduled after the instruction)
308 * This function also skips phi nodes at the beginning of a block
310 static void sched_add_after_insn(ir_node *sched_after, ir_node *node) {
311 ir_node *next = sched_next(sched_after);
312 while(is_Proj(next) || is_Phi(next)) {
313 next = sched_next(next);
315 assert(next != NULL);
317 if(sched_is_end(next)) {
318 sched_add_after(sched_last(get_nodes_block(sched_after)), node);
320 sched_add_before(next, node);
327 * @param senv the spill environment
328 * @param irn the node that should be spilled
329 * @param ctx_irn an user of the spilled node
331 * @return a be_Spill node
333 static void spill_irn(spill_env_t *env, spill_info_t *spillinfo) {
334 ir_node *to_spill = spillinfo->spilled_node;
336 DBG((env->dbg, LEVEL_1, "%+F\n", to_spill));
338 /* Trying to spill an already spilled value, no need for a new spill
339 * node then, we can simply connect to the same one for this reload
341 * (although rematerialization code should handle most of these cases
342 * this can still happen when spilling Phis)
344 if(be_is_Reload(to_spill)) {
345 spillinfo->spill = get_irn_n(to_spill, be_pos_Reload_mem);
349 if (arch_irn_is(env->arch_env, to_spill, dont_spill)) {
350 assert(0 && "Attempt to spill a node marked 'dont_spill'");
353 spillinfo->spill = be_spill(env->arch_env, to_spill);
354 sched_add_after_insn(to_spill, spillinfo->spill);
357 static void spill_node(spill_env_t *env, spill_info_t *spillinfo);
360 * If the first usage of a Phi result would be out of memory
361 * there is no sense in allocating a register for it.
362 * Thus we spill it and all its operands to the same spill slot.
363 * Therefore the phi/dataB becomes a phi/Memory
365 * @param senv the spill environment
366 * @param phi the Phi node that should be spilled
367 * @param ctx_irn an user of the spilled node
369 static void spill_phi(spill_env_t *env, spill_info_t *spillinfo) {
370 ir_node *phi = spillinfo->spilled_node;
372 int arity = get_irn_arity(phi);
373 ir_node *block = get_nodes_block(phi);
378 /* build a new PhiM */
379 ins = alloca(sizeof(ir_node*) * arity);
380 for(i = 0; i < arity; ++i) {
381 ins[i] = get_irg_bad(env->irg);
383 spillinfo->spill = new_r_Phi(env->irg, block, arity, ins, mode_M);
385 for(i = 0; i < arity; ++i) {
386 ir_node *arg = get_irn_n(phi, i);
387 spill_info_t *arg_info = get_spillinfo(env, arg);
389 spill_node(env, arg_info);
391 set_irn_n(spillinfo->spill, i, arg_info->spill);
394 // rewire reloads from old_spill to phi
395 if(spillinfo->old_spill != NULL) {
396 const ir_edge_t *edge, *next;
397 ir_node *old_spill = spillinfo->old_spill;
399 foreach_out_edge_safe(old_spill, edge, next) {
400 ir_node* reload = get_edge_src_irn(edge);
401 assert(be_is_Reload(reload) || is_Phi(reload));
402 set_irn_n(reload, get_edge_src_pos(edge), spillinfo->spill);
404 set_irn_n(old_spill, be_pos_Spill_val, new_Bad());
405 //sched_remove(old_spill);
406 spillinfo->old_spill = NULL;
413 * @param senv the spill environment
414 * @param to_spill the node that should be spilled
416 static void spill_node(spill_env_t *env, spill_info_t *spillinfo) {
419 // the node should be tagged for spilling already...
420 if(spillinfo->spill != NULL)
423 to_spill = spillinfo->spilled_node;
424 if (is_Phi(to_spill) && pset_find_ptr(env->mem_phis, spillinfo->spilled_node)) {
425 spill_phi(env, spillinfo);
427 spill_irn(env, spillinfo);
434 * | _ \ ___ _ __ ___ __ _| |_ ___ _ __(_) __ _| (_)_______
435 * | |_) / _ \ '_ ` _ \ / _` | __/ _ \ '__| |/ _` | | |_ / _ \
436 * | _ < __/ | | | | | (_| | || __/ | | | (_| | | |/ / __/
437 * |_| \_\___|_| |_| |_|\__,_|\__\___|_| |_|\__,_|_|_/___\___|
442 * Tests whether value @p arg is available before node @p reloader
443 * @returns 1 if value is available, 0 otherwise
445 static int is_value_available(spill_env_t *env, ir_node *arg, ir_node *reloader) {
446 if(is_Unknown(arg) || arg == new_NoMem())
452 if(arg == get_irg_frame(env->irg))
455 // hack for now (happens when command should be inserted at end of block)
456 if(is_Block(reloader)) {
461 * Ignore registers are always available
463 if(arch_irn_is(env->arch_env, arg, ignore)) {
468 /* the following test does not work while spilling,
469 * because the liveness info is not adapted yet to the effects of the
470 * additional spills/reloads.
473 /* we want to remat before the insn reloader
474 * thus an arguments is alive if
475 * - it interferes with the reloaders result
476 * - or it is (last-) used by reloader itself
478 if (values_interfere(env->birg->lv, reloader, arg)) {
482 arity = get_irn_arity(reloader);
483 for (i = 0; i < arity; ++i) {
484 ir_node *rel_arg = get_irn_n(reloader, i);
494 * Checks whether the node can principally be rematerialized
496 static int is_remat_node(spill_env_t *env, ir_node *node) {
497 const arch_env_t *arch_env = env->arch_env;
499 assert(!be_is_Spill(node));
501 if(arch_irn_is(arch_env, node, rematerializable)) {
505 if(be_is_StackParam(node))
512 * Check if a node is rematerializable. This tests for the following conditions:
514 * - The node itself is rematerializable
515 * - All arguments of the node are available or also rematerialisable
516 * - The costs for the rematerialisation operation is less or equal a limit
518 * Returns the costs needed for rematerialisation or something
519 * > REMAT_COST_LIMIT if remat is not possible.
521 static int check_remat_conditions_costs(spill_env_t *env, ir_node *spilled, ir_node *reloader, int parentcosts) {
526 if(!is_remat_node(env, spilled))
527 return REMAT_COST_LIMIT;
529 if(be_is_Reload(spilled)) {
532 costs += arch_get_op_estimated_cost(env->arch_env, spilled);
534 if(parentcosts + costs >= REMAT_COST_LIMIT) {
535 return REMAT_COST_LIMIT;
539 for(i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
540 ir_node *arg = get_irn_n(spilled, i);
542 if(is_value_available(env, arg, reloader))
545 // we have to rematerialize the argument as well...
547 /* we only support rematerializing 1 argument at the moment,
548 * so that we don't have to care about register pressure
550 return REMAT_COST_LIMIT;
554 costs += check_remat_conditions_costs(env, arg, reloader, parentcosts + costs);
555 if(parentcosts + costs >= REMAT_COST_LIMIT)
556 return REMAT_COST_LIMIT;
562 static int check_remat_conditions(spill_env_t *env, ir_node *spilled, ir_node *reloader) {
563 int costs = check_remat_conditions_costs(env, spilled, reloader, 0);
565 return costs < REMAT_COST_LIMIT;
569 * Re-materialize a node.
571 * @param senv the spill environment
572 * @param spilled the node that was spilled
573 * @param reloader a irn that requires a reload
575 static ir_node *do_remat(spill_env_t *env, ir_node *spilled, ir_node *reloader) {
581 if(is_Block(reloader)) {
584 bl = get_nodes_block(reloader);
587 ins = alloca(get_irn_arity(spilled) * sizeof(ins[0]));
588 for(i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
589 ir_node *arg = get_irn_n(spilled, i);
591 if(is_value_available(env, arg, reloader)) {
594 ins[i] = do_remat(env, arg, reloader);
598 /* create a copy of the node */
599 res = new_ir_node(get_irn_dbg_info(spilled), env->irg, bl,
601 get_irn_mode(spilled),
602 get_irn_arity(spilled),
604 copy_node_attr(spilled, res);
605 new_backedge_info(res);
608 DBG((env->dbg, LEVEL_1, "Insert remat %+F of %+F before reloader %+F\n", res, spilled, reloader));
610 /* insert in schedule */
611 sched_add_before(reloader, res);
616 int be_get_reload_costs(spill_env_t *env, ir_node *to_spill, ir_node *before) {
617 spill_info_t *spill_info;
620 // is the node rematerializable?
621 int costs = check_remat_conditions_costs(env, to_spill, before, 0);
622 if(costs < REMAT_COST_LIMIT)
626 // do we already have a spill?
627 spill_info = find_spillinfo(env, to_spill);
628 if(spill_info != NULL && spill_info->spill != NULL)
629 return env->reload_cost;
631 return env->spill_cost + env->reload_cost;
634 int be_get_reload_costs_on_edge(spill_env_t *env, ir_node *to_spill, ir_node *block, int pos) {
635 ir_node *before = get_reload_insertion_point(block, pos);
636 return be_get_reload_costs(env, to_spill, before);
641 * |_ _|_ __ ___ ___ _ __| |_ | _ \ ___| | ___ __ _ __| |___
642 * | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ |/ _ \ / _` |/ _` / __|
643 * | || | | \__ \ __/ | | |_ | _ < __/ | (_) | (_| | (_| \__ \
644 * |___|_| |_|___/\___|_| \__| |_| \_\___|_|\___/ \__,_|\__,_|___/
648 void be_insert_spills_reloads(spill_env_t *env) {
649 const arch_env_t *arch_env = env->arch_env;
655 /* process each spilled node */
656 for (si = set_first(env->spills); si; si = set_next(env->spills)) {
658 ir_mode *mode = get_irn_mode(si->spilled_node);
659 pset *values = pset_new_ptr(16);
661 DBG((env->dbg, LEVEL_1, "\nhandling all reloaders of %+F:\n", si->spilled_node));
663 /* go through all reloads for this spill */
664 for (rld = si->reloaders; rld; rld = rld->next) {
667 if (rld->rematted_node != NULL) {
668 new_val = rld->rematted_node;
670 sched_add_before(rld->reloader, new_val);
672 else if (be_do_remats && rld->allow_remat && check_remat_conditions(env, si->spilled_node, rld->reloader)) {
673 new_val = do_remat(env, si->spilled_node, rld->reloader);
677 /* make sure we have a spill */
678 if (si->spill == NULL) {
683 /* create a reload */
684 new_val = be_reload(arch_env, si->reload_cls, rld->reloader, mode, si->spill);
688 DBG((env->dbg, LEVEL_1, " %+F of %+F before %+F\n", new_val, si->spilled_node, rld->reloader));
689 pset_insert_ptr(values, new_val);
692 if (pset_count(values) > 0) {
693 /* introduce copies, rewire the uses */
694 pset_insert_ptr(values, si->spilled_node);
695 be_ssa_constr_set_ignore(env->birg->dom_front, env->birg->lv, values, env->mem_phis);
700 si->reloaders = NULL;
703 #ifdef FIRM_STATISTICS
704 if (be_stat_ev_is_active()) {
705 be_stat_ev("spill_spills", spills);
706 be_stat_ev("spill_reloads", reloads);
707 be_stat_ev("spill_remats", remats);
709 #endif /* FIRM_STATISTICS */
711 be_remove_dead_nodes_from_schedule(env->irg);
712 //be_liveness_recompute(env->birg->lv);
713 be_invalidate_liveness(env->birg);