2 * Author: Daniel Grund, Sebastian Hack, Matthias Braun
4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
16 #include "iredges_t.h"
25 #include "unionfind.h"
29 #include "besched_t.h"
33 #include "bechordal_t.h"
34 #include "bejavacoal.h"
35 #include "benodesets.h"
37 // only rematerialise when costs are less than REMAT_COST_LIMIT
38 // TODO determine a good value here...
39 #define REMAT_COST_LIMIT 20
41 typedef struct _reloader_t reloader_t;
48 typedef struct _spill_info_t {
49 /** the value that should get spilled */
50 ir_node *spilled_node;
51 /** list of places where the value should get reloaded */
52 reloader_t *reloaders;
54 /** the spill node, or a PhiM node */
56 /** if we had the value of a phi spilled before but not the phi itself then
57 * this field contains the spill for the phi value */
62 const arch_register_class_t *cls;
63 const arch_env_t *arch_env;
64 const be_chordal_env_t *chordal_env;
66 set *spills; /**< all spill_info_t's, which must be placed */
67 pset *mem_phis; /**< set of all special spilled phis. allocated and freed separately */
69 DEBUG_ONLY(firm_dbg_module_t *dbg;)
73 * Compare two spill infos.
75 static int cmp_spillinfo(const void *x, const void *y, size_t size) {
76 const spill_info_t *xx = x;
77 const spill_info_t *yy = y;
78 return xx->spilled_node != yy->spilled_node;
82 * Returns spill info for a specific value (the value that is to be spilled)
84 static spill_info_t *get_spillinfo(const spill_env_t *env, ir_node *value) {
85 spill_info_t info, *res;
86 int hash = nodeset_hash(value);
88 info.spilled_node = value;
89 res = set_find(env->spills, &info, sizeof(info), hash);
92 info.reloaders = NULL;
94 info.old_spill = NULL;
95 res = set_insert(env->spills, &info, sizeof(info), hash);
102 /* Sets the debug module of a spill environment. */
103 void be_set_spill_env_dbg_module(spill_env_t *env, firm_dbg_module_t *dbg) {
108 /* Creates a new spill environment. */
109 spill_env_t *be_new_spill_env(const be_chordal_env_t *chordal_env) {
110 spill_env_t *env = xmalloc(sizeof(env[0]));
111 env->spills = new_set(cmp_spillinfo, 1024);
112 env->cls = chordal_env->cls;
113 env->chordal_env = chordal_env;
114 env->arch_env = env->chordal_env->birg->main_env->arch_env;
115 env->mem_phis = pset_new_ptr_default();
116 obstack_init(&env->obst);
120 /* Deletes a spill environment. */
121 void be_delete_spill_env(spill_env_t *env) {
122 del_set(env->spills);
123 del_pset(env->mem_phis);
124 obstack_free(&env->obst, NULL);
130 * | _ \| | __ _ ___ ___ | _ \ ___| | ___ __ _ __| |___
131 * | |_) | |/ _` |/ __/ _ \ | |_) / _ \ |/ _ \ / _` |/ _` / __|
132 * | __/| | (_| | (_| __/ | _ < __/ | (_) | (_| | (_| \__ \
133 * |_| |_|\__,_|\___\___| |_| \_\___|_|\___/ \__,_|\__,_|___/
137 void be_add_reload(spill_env_t *env, ir_node *to_spill, ir_node *before) {
141 assert(sched_is_scheduled(before));
142 assert(arch_irn_consider_in_reg_alloc(env->arch_env, env->cls, to_spill));
144 info = get_spillinfo(env, to_spill);
146 if(is_Phi(to_spill)) {
148 // create spillinfos for the phi arguments
149 for(i = 0, arity = get_irn_arity(to_spill); i < arity; ++i) {
150 ir_node *arg = get_irn_n(to_spill, i);
151 get_spillinfo(env, arg);
155 rel = obstack_alloc(&env->obst, sizeof(rel[0]));
156 rel->reloader = before;
157 rel->next = info->reloaders;
158 info->reloaders = rel;
161 void be_add_reload_on_edge(spill_env_t *env, ir_node *to_spill, ir_node *block, int pos) {
162 ir_node *predblock, *last;
164 /* simply add the reload to the beginning of the block if we only have 1 predecessor
165 * (we don't need to check for phis as there can't be any in a block with only 1 pred)
167 if(get_Block_n_cfgpreds(block) == 1) {
168 assert(!is_Phi(sched_first(block)));
169 be_add_reload(env, to_spill, sched_first(block));
173 /* We have to reload the value in pred-block */
174 predblock = get_Block_cfgpred_block(block, pos);
175 last = sched_last(predblock);
177 /* we might have projs and keepanys behind the jump... */
178 while(is_Proj(last) || be_is_Keep(last)) {
179 last = sched_prev(last);
180 assert(!sched_is_end(last));
182 assert(is_cfop(last));
184 // add the reload before the (cond-)jump
185 be_add_reload(env, to_spill, last);
188 void be_spill_phi(spill_env_t *env, ir_node *node) {
192 assert(is_Phi(node));
194 pset_insert_ptr(env->mem_phis, node);
196 // create spillinfos for the phi arguments
197 spill = get_spillinfo(env, node);
198 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
199 ir_node *arg = get_irn_n(node, i);
200 get_spillinfo(env, arg);
203 // if we had a spill for the phi value before, then remove this spill from
204 // schedule, as we will remove it in the insert spill/reload phase
205 if(spill->spill != NULL && !is_Phi(spill->spill)) {
206 spill->old_spill = spill->spill;
213 * / ___|_ __ ___ __ _| |_ ___ / ___| _ __ (_) | |___
214 * | | | '__/ _ \/ _` | __/ _ \ \___ \| '_ \| | | / __|
215 * | |___| | | __/ (_| | || __/ ___) | |_) | | | \__ \
216 * \____|_| \___|\__,_|\__\___| |____/| .__/|_|_|_|___/
221 * Schedules a node after an instruction. (That is the place after all projs and phis
222 * that are scheduled after the instruction)
223 * This function also skips phi nodes at the beginning of a block
225 static void sched_add_after_insn(ir_node *sched_after, ir_node *node) {
226 ir_node *next = sched_next(sched_after);
227 while(is_Proj(next) || is_Phi(next)) {
228 next = sched_next(next);
230 assert(next != NULL);
232 if(sched_is_end(next)) {
233 sched_add_after(sched_last(get_nodes_block(sched_after)), node);
235 sched_add_before(next, node);
242 * @param senv the spill environment
243 * @param irn the node that should be spilled
244 * @param ctx_irn an user of the spilled node
246 * @return a be_Spill node
248 static void spill_irn(spill_env_t *env, spill_info_t *spillinfo) {
249 ir_node *to_spill = spillinfo->spilled_node;
251 DBG((env->dbg, LEVEL_1, "%+F\n", to_spill));
253 /* Trying to spill an already spilled value, no need for a new spill
254 * node then, we can simply connect to the same one for this reload
256 * (although rematerialization code should handle most of these cases
257 * this can still happen when spilling Phis)
259 if(be_is_Reload(to_spill)) {
260 spillinfo->spill = get_irn_n(to_spill, be_pos_Reload_mem);
264 if (arch_irn_is(env->arch_env, to_spill, dont_spill)) {
265 if (env->chordal_env->opts->vrfy_option == BE_CH_VRFY_WARN)
266 ir_fprintf(stderr, "Verify warning: spilling 'dont_spill' node %+F\n", to_spill);
267 else if (env->chordal_env->opts->vrfy_option == BE_CH_VRFY_ASSERT)
268 assert(0 && "Attempt to spill a node marked 'dont_spill'");
271 spillinfo->spill = be_spill(env->arch_env, to_spill);
272 sched_add_after_insn(to_spill, spillinfo->spill);
275 static void spill_node(spill_env_t *env, spill_info_t *spillinfo);
278 * If the first usage of a Phi result would be out of memory
279 * there is no sense in allocating a register for it.
280 * Thus we spill it and all its operands to the same spill slot.
281 * Therefore the phi/dataB becomes a phi/Memory
283 * @param senv the spill environment
284 * @param phi the Phi node that should be spilled
285 * @param ctx_irn an user of the spilled node
287 static void spill_phi(spill_env_t *env, spill_info_t *spillinfo) {
288 ir_node *phi = spillinfo->spilled_node;
290 int arity = get_irn_arity(phi);
291 ir_node *block = get_nodes_block(phi);
296 /* build a new PhiM */
297 ins = alloca(sizeof(ir_node*) * arity);
298 for(i = 0; i < arity; ++i) {
299 ins[i] = get_irg_bad(env->chordal_env->irg);
301 spillinfo->spill = new_r_Phi(env->chordal_env->irg, block, arity, ins, mode_M);
303 for(i = 0; i < arity; ++i) {
304 ir_node *arg = get_irn_n(phi, i);
305 spill_info_t *arg_info = get_spillinfo(env, arg);
307 spill_node(env, arg_info);
309 set_irn_n(spillinfo->spill, i, arg_info->spill);
312 // rewire reloads from old_spill to phi
313 if(spillinfo->old_spill != NULL) {
314 const ir_edge_t *edge, *next;
315 foreach_out_edge_safe(spillinfo->old_spill, edge, next) {
316 ir_node* reload = get_edge_src_irn(edge);
317 assert(be_is_Reload(reload) || is_Phi(reload));
318 set_irn_n(reload, get_edge_src_pos(edge), spillinfo->spill);
320 spillinfo->old_spill = NULL;
327 * @param senv the spill environment
328 * @param to_spill the node that should be spilled
330 static void spill_node(spill_env_t *env, spill_info_t *spillinfo) {
333 // the node should be tagged for spilling already...
334 if(spillinfo->spill != NULL)
337 to_spill = spillinfo->spilled_node;
338 if (is_Phi(to_spill) && pset_find_ptr(env->mem_phis, spillinfo->spilled_node)) {
339 spill_phi(env, spillinfo);
341 spill_irn(env, spillinfo);
348 * | _ \ ___ _ __ ___ __ _| |_ ___ _ __(_) __ _| (_)_______
349 * | |_) / _ \ '_ ` _ \ / _` | __/ _ \ '__| |/ _` | | |_ / _ \
350 * | _ < __/ | | | | | (_| | || __/ | | | (_| | | |/ / __/
351 * |_| \_\___|_| |_| |_|\__,_|\__\___|_| |_|\__,_|_|_/___\___|
356 * Tests whether value @p arg is available before node @p reloader
357 * @returns 1 if value is available, 0 otherwise
359 static int is_value_available(spill_env_t *env, ir_node *arg, ir_node *reloader) {
360 if(is_Unknown(arg) || arg == new_NoMem())
366 if(arg == get_irg_frame(env->chordal_env->irg))
369 /* the following test does not work while spilling,
370 * because the liveness info is not adapted yet to the effects of the
371 * additional spills/reloads.
373 * So we can only do this test for ignore registers (of our register class)
375 if(arch_get_irn_reg_class(env->arch_env, arg, -1) == env->chordal_env->cls
376 && arch_irn_is(env->arch_env, arg, ignore)) {
379 /* we want to remat before the insn reloader
380 * thus an arguments is alive if
381 * - it interferes with the reloaders result
382 * - or it is (last-) used by reloader itself
384 if (values_interfere(env->chordal_env->lv, reloader, arg)) {
388 arity = get_irn_arity(reloader);
389 for (i = 0; i < arity; ++i) {
390 ir_node *rel_arg = get_irn_n(reloader, i);
400 * Checks whether the node can principally be rematerialized
402 static int is_remat_node(spill_env_t *env, ir_node *node) {
403 const arch_env_t *arch_env = env->arch_env;
405 assert(!be_is_Spill(node));
407 if(arch_irn_is(arch_env, node, rematerializable)) {
411 if(be_is_StackParam(node))
418 * Check if a node is rematerializable. This tests for the following conditions:
420 * - The node itself is rematerializable
421 * - All arguments of the node are available or also rematerialisable
422 * - The costs for the rematerialisation operation is less or equal a limit
424 * Returns the costs needed for rematerialisation or something
425 * > REMAT_COST_LIMIT if remat is not possible.
427 static int check_remat_conditions_costs(spill_env_t *env, ir_node *spilled, ir_node *reloader, int parentcosts) {
432 if(!is_remat_node(env, spilled))
433 return REMAT_COST_LIMIT;
435 if(be_is_Reload(spilled)) {
438 costs += arch_get_op_estimated_cost(env->arch_env, spilled);
440 if(parentcosts + costs >= REMAT_COST_LIMIT) {
441 return REMAT_COST_LIMIT;
445 for(i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
446 ir_node *arg = get_irn_n(spilled, i);
448 if(is_value_available(env, arg, reloader))
451 // we have to rematerialize the argument as well...
453 /* we only support rematerializing 1 argument at the moment,
454 * so that we don't have to care about register pressure
456 return REMAT_COST_LIMIT;
460 costs += check_remat_conditions_costs(env, arg, reloader, parentcosts + costs);
461 if(parentcosts + costs >= REMAT_COST_LIMIT)
462 return REMAT_COST_LIMIT;
468 static int check_remat_conditions(spill_env_t *env, ir_node *spilled, ir_node *reloader) {
469 int costs = check_remat_conditions_costs(env, spilled, reloader, 1);
471 return costs < REMAT_COST_LIMIT;
475 * Re-materialize a node.
477 * @param senv the spill environment
478 * @param spilled the node that was spilled
479 * @param reloader a irn that requires a reload
481 static ir_node *do_remat(spill_env_t *env, ir_node *spilled, ir_node *reloader) {
484 ir_node *bl = get_nodes_block(reloader);
487 ins = alloca(get_irn_arity(spilled) * sizeof(ins[0]));
488 for(i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
489 ir_node *arg = get_irn_n(spilled, i);
491 if(is_value_available(env, arg, reloader)) {
494 ins[i] = do_remat(env, arg, reloader);
498 /* create a copy of the node */
499 res = new_ir_node(get_irn_dbg_info(spilled), env->chordal_env->irg, bl,
501 get_irn_mode(spilled),
502 get_irn_arity(spilled),
504 copy_node_attr(spilled, res);
506 DBG((env->dbg, LEVEL_1, "Insert remat %+F before reloader %+F\n", res, reloader));
508 /* insert in schedule */
509 assert(!is_Block(reloader));
510 sched_add_before(reloader, res);
517 * |_ _|_ __ ___ ___ _ __| |_ | _ \ ___| | ___ __ _ __| |___
518 * | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ |/ _ \ / _` |/ _` / __|
519 * | || | | \__ \ __/ | | |_ | _ < __/ | (_) | (_| | (_| \__ \
520 * |___|_| |_|___/\___|_| \__| |_| \_\___|_|\___/ \__,_|\__,_|___/
524 void be_insert_spills_reloads(spill_env_t *env) {
525 const arch_env_t *arch_env = env->arch_env;
528 /* process each spilled node */
529 DBG((env->dbg, LEVEL_1, "Insert spills and reloads:\n"));
530 for(si = set_first(env->spills); si; si = set_next(env->spills)) {
532 ir_mode *mode = get_irn_mode(si->spilled_node);
533 pset *values = pset_new_ptr(16);
535 /* go through all reloads for this spill */
536 for(rld = si->reloaders; rld; rld = rld->next) {
539 if (check_remat_conditions(env, si->spilled_node, rld->reloader)) {
540 new_val = do_remat(env, si->spilled_node, rld->reloader);
542 /* make sure we have a spill */
546 new_val = be_reload(arch_env, env->cls, rld->reloader, mode, si->spill);
549 DBG((env->dbg, LEVEL_1, " %+F of %+F before %+F\n", new_val, si->spilled_node, rld->reloader));
550 pset_insert_ptr(values, new_val);
553 if(pset_count(values) > 0) {
554 /* introduce copies, rewire the uses */
555 pset_insert_ptr(values, si->spilled_node);
556 be_ssa_constr_set_ignore(env->chordal_env->dom_front, env->chordal_env->lv, values, env->mem_phis);
561 si->reloaders = NULL;
564 be_remove_dead_nodes_from_schedule(env->chordal_env->irg);
565 be_liveness_recompute(env->chordal_env->lv);