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"
36 // only rematerialise when costs are less than REMAT_COST_LIMIT
37 // TODO determine a good value here...
38 #define REMAT_COST_LIMIT 20
40 typedef struct _reloader_t reloader_t;
47 typedef struct _spill_info_t {
48 /** the value that should get spilled */
49 ir_node *spilled_node;
50 /** list of places where the value should get reloaded */
51 reloader_t *reloaders;
53 /** the spill node, or a PhiM node */
55 /** if we had the value of a phi spilled before but not the phi itself then
56 * this field contains the spill for the phi value */
61 const arch_register_class_t *cls;
62 const arch_env_t *arch_env;
63 const be_chordal_env_t *chordal_env;
65 set *spills; /**< all spill_info_t's, which must be placed */
66 pset *mem_phis; /**< set of all special spilled phis. allocated and freed separately */
68 DEBUG_ONLY(firm_dbg_module_t *dbg;)
72 * Compare two spill infos.
74 static int cmp_spillinfo(const void *x, const void *y, size_t size) {
75 const spill_info_t *xx = x;
76 const spill_info_t *yy = y;
77 return xx->spilled_node != yy->spilled_node;
81 * Returns spill info for a specific value (the value that is to be spilled)
83 static spill_info_t *get_spillinfo(const spill_env_t *env, ir_node *value) {
84 spill_info_t info, *res;
85 int hash = HASH_PTR(value);
87 info.spilled_node = value;
88 res = set_find(env->spills, &info, sizeof(info), hash);
91 info.reloaders = NULL;
93 info.old_spill = NULL;
94 res = set_insert(env->spills, &info, sizeof(info), hash);
101 /* Sets the debug module of a spill environment. */
102 void be_set_spill_env_dbg_module(spill_env_t *env, firm_dbg_module_t *dbg) {
107 /* Creates a new spill environment. */
108 spill_env_t *be_new_spill_env(const be_chordal_env_t *chordal_env) {
109 spill_env_t *env = xmalloc(sizeof(env[0]));
110 env->spills = new_set(cmp_spillinfo, 1024);
111 env->cls = chordal_env->cls;
112 env->chordal_env = chordal_env;
113 env->arch_env = env->chordal_env->birg->main_env->arch_env;
114 env->mem_phis = pset_new_ptr_default();
115 obstack_init(&env->obst);
119 /* Deletes a spill environment. */
120 void be_delete_spill_env(spill_env_t *env) {
121 del_set(env->spills);
122 del_pset(env->mem_phis);
123 obstack_free(&env->obst, NULL);
129 * | _ \| | __ _ ___ ___ | _ \ ___| | ___ __ _ __| |___
130 * | |_) | |/ _` |/ __/ _ \ | |_) / _ \ |/ _ \ / _` |/ _` / __|
131 * | __/| | (_| | (_| __/ | _ < __/ | (_) | (_| | (_| \__ \
132 * |_| |_|\__,_|\___\___| |_| \_\___|_|\___/ \__,_|\__,_|___/
136 void be_add_reload(spill_env_t *env, ir_node *to_spill, ir_node *before) {
140 assert(sched_is_scheduled(before));
141 assert(arch_irn_consider_in_reg_alloc(env->arch_env, env->cls, to_spill));
143 info = get_spillinfo(env, to_spill);
145 if(is_Phi(to_spill)) {
147 // create spillinfos for the phi arguments
148 for(i = 0, arity = get_irn_arity(to_spill); i < arity; ++i) {
149 ir_node *arg = get_irn_n(to_spill, i);
150 get_spillinfo(env, arg);
154 rel = obstack_alloc(&env->obst, sizeof(rel[0]));
155 rel->reloader = before;
156 rel->next = info->reloaders;
157 info->reloaders = rel;
160 void be_add_reload_on_edge(spill_env_t *env, ir_node *to_spill, ir_node *block, int pos) {
161 ir_node *predblock, *last;
163 /* simply add the reload to the beginning of the block if we only have 1 predecessor
164 * (we don't need to check for phis as there can't be any in a block with only 1 pred)
166 if(get_Block_n_cfgpreds(block) == 1) {
167 assert(!is_Phi(sched_first(block)));
168 be_add_reload(env, to_spill, sched_first(block));
172 /* We have to reload the value in pred-block */
173 predblock = get_Block_cfgpred_block(block, pos);
174 last = sched_last(predblock);
176 /* we might have projs and keepanys behind the jump... */
177 while(is_Proj(last) || be_is_Keep(last)) {
178 last = sched_prev(last);
179 assert(!sched_is_end(last));
181 assert(is_cfop(last));
183 // add the reload before the (cond-)jump
184 be_add_reload(env, to_spill, last);
187 void be_spill_phi(spill_env_t *env, ir_node *node) {
190 assert(is_Phi(node));
192 pset_insert_ptr(env->mem_phis, node);
194 // create spillinfos for the phi arguments
195 spill_info_t* spill = get_spillinfo(env, node);
196 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
197 ir_node *arg = get_irn_n(node, i);
198 get_spillinfo(env, arg);
201 // if we had a spill for the phi value before, then remove this spill from
202 // schedule, as we will remove it in the insert spill/reload phase
203 if(spill->spill != NULL && !is_Phi(spill->spill)) {
204 //sched_remove(spill->spill);
205 spill->old_spill = spill->spill;
212 * / ___|_ __ ___ __ _| |_ ___ / ___| _ __ (_) | |___
213 * | | | '__/ _ \/ _` | __/ _ \ \___ \| '_ \| | | / __|
214 * | |___| | | __/ (_| | || __/ ___) | |_) | | | \__ \
215 * \____|_| \___|\__,_|\__\___| |____/| .__/|_|_|_|___/
220 * Schedules a node after an instruction. (That is the place after all projs and phis
221 * that are scheduled after the instruction)
222 * This function also skips phi nodes at the beginning of a block
224 static void sched_add_after_insn(ir_node *sched_after, ir_node *node) {
225 ir_node *next = sched_next(sched_after);
226 while(is_Proj(next) || is_Phi(next)) {
227 next = sched_next(next);
229 assert(next != NULL);
231 if(sched_is_end(next)) {
232 sched_add_after(sched_last(get_nodes_block(sched_after)), node);
234 sched_add_before(next, node);
241 * @param senv the spill environment
242 * @param irn the node that should be spilled
243 * @param ctx_irn an user of the spilled node
245 * @return a be_Spill node
247 static void spill_irn(spill_env_t *env, spill_info_t *spillinfo) {
248 ir_node *to_spill = spillinfo->spilled_node;
250 DBG((env->dbg, LEVEL_1, "%+F\n", to_spill));
252 /* Trying to spill an already spilled value, no need for a new spill
253 * node then, we can simply connect to the same one for this reload
255 * (although rematerialization code should handle most of these cases
256 * this can still happen when spilling Phis)
258 if(be_is_Reload(to_spill)) {
259 spillinfo->spill = get_irn_n(to_spill, be_pos_Reload_mem);
263 if (arch_irn_is(env->arch_env, to_spill, dont_spill)) {
264 if (env->chordal_env->opts->vrfy_option == BE_CH_VRFY_WARN)
265 ir_fprintf(stderr, "Verify warning: spilling 'dont_spill' node %+F\n", to_spill);
266 else if (env->chordal_env->opts->vrfy_option == BE_CH_VRFY_ASSERT)
267 assert(0 && "Attempt to spill a node marked 'dont_spill'");
270 spillinfo->spill = be_spill(env->arch_env, to_spill);
271 sched_add_after_insn(to_spill, spillinfo->spill);
274 static void spill_node(spill_env_t *env, spill_info_t *spillinfo);
277 * If the first usage of a Phi result would be out of memory
278 * there is no sense in allocating a register for it.
279 * Thus we spill it and all its operands to the same spill slot.
280 * Therefore the phi/dataB becomes a phi/Memory
282 * @param senv the spill environment
283 * @param phi the Phi node that should be spilled
284 * @param ctx_irn an user of the spilled node
286 static void spill_phi(spill_env_t *env, spill_info_t *spillinfo) {
287 ir_node *phi = spillinfo->spilled_node;
289 int arity = get_irn_arity(phi);
290 ir_node *block = get_nodes_block(phi);
295 /* build a new PhiM */
296 ins = alloca(sizeof(ir_node*) * arity);
297 for(i = 0; i < arity; ++i) {
298 ins[i] = get_irg_bad(env->chordal_env->irg);
300 spillinfo->spill = new_r_Phi(env->chordal_env->irg, block, arity, ins, mode_M);
302 for(i = 0; i < arity; ++i) {
303 ir_node *arg = get_irn_n(phi, i);
304 spill_info_t *arg_info = get_spillinfo(env, arg);
306 spill_node(env, arg_info);
308 set_irn_n(spillinfo->spill, i, arg_info->spill);
311 // rewire reloads from old_spill to phi
312 if(spillinfo->old_spill != NULL) {
313 const ir_edge_t *edge, *next;
314 foreach_out_edge_safe(spillinfo->old_spill, edge, next) {
315 ir_node* reload = get_edge_src_irn(edge);
316 assert(be_is_Reload(reload) || is_Phi(reload));
317 set_irn_n(reload, get_edge_src_pos(edge), spillinfo->spill);
319 spillinfo->old_spill = NULL;
326 * @param senv the spill environment
327 * @param to_spill the node that should be spilled
329 static void spill_node(spill_env_t *env, spill_info_t *spillinfo) {
332 // the node should be tagged for spilling already...
333 if(spillinfo->spill != NULL)
336 to_spill = spillinfo->spilled_node;
337 if (is_Phi(to_spill) && pset_find_ptr(env->mem_phis, spillinfo->spilled_node)) {
338 spill_phi(env, spillinfo);
340 spill_irn(env, spillinfo);
347 * | _ \ ___ _ __ ___ __ _| |_ ___ _ __(_) __ _| (_)_______
348 * | |_) / _ \ '_ ` _ \ / _` | __/ _ \ '__| |/ _` | | |_ / _ \
349 * | _ < __/ | | | | | (_| | || __/ | | | (_| | | |/ / __/
350 * |_| \_\___|_| |_| |_|\__,_|\__\___|_| |_|\__,_|_|_/___\___|
355 * Tests whether value @p arg is available before node @p reloader
356 * @returns 1 if value is available, 0 otherwise
358 static int is_value_available(spill_env_t *env, ir_node *arg, ir_node *reloader) {
359 if(is_Unknown(arg) || arg == new_NoMem())
365 if(arg == get_irg_frame(env->chordal_env->irg))
368 /* the following test does not work while spilling,
369 * because the liveness info is not adapted yet to the effects of the
370 * additional spills/reloads.
372 * So we can only do this test for ignore registers (of our register class)
374 if(arch_get_irn_reg_class(env->arch_env, arg, -1) == env->chordal_env->cls
375 && arch_irn_is(env->arch_env, arg, ignore)) {
378 /* we want to remat before the insn reloader
379 * thus an arguments is alive if
380 * - it interferes with the reloaders result
381 * - or it is (last-) used by reloader itself
383 if (values_interfere(env->chordal_env->lv, reloader, arg)) {
387 arity = get_irn_arity(reloader);
388 for (i = 0; i < arity; ++i) {
389 ir_node *rel_arg = get_irn_n(reloader, i);
399 * Checks whether the node can principally be rematerialized
401 static int is_remat_node(spill_env_t *env, ir_node *node) {
402 const arch_env_t *arch_env = env->arch_env;
404 assert(!be_is_Spill(node));
406 if(arch_irn_is(arch_env, node, rematerializable)) {
410 if(be_is_StackParam(node))
417 * Check if a node is rematerializable. This tests for the following conditions:
419 * - The node itself is rematerializable
420 * - All arguments of the node are available or also rematerialisable
421 * - The costs for the rematerialisation operation is less or equal a limit
423 * Returns the costs needed for rematerialisation or something
424 * > REMAT_COST_LIMIT if remat is not possible.
426 static int check_remat_conditions_costs(spill_env_t *env, ir_node *spilled, ir_node *reloader, int parentcosts) {
431 if(!is_remat_node(env, spilled))
432 return REMAT_COST_LIMIT;
434 if(be_is_Reload(spilled)) {
437 costs += arch_get_op_estimated_cost(env->arch_env, spilled);
439 if(parentcosts + costs >= REMAT_COST_LIMIT) {
440 return REMAT_COST_LIMIT;
444 for(i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
445 ir_node *arg = get_irn_n(spilled, i);
447 if(is_value_available(env, arg, reloader))
450 // we have to rematerialize the argument as well...
452 /* we only support rematerializing 1 argument at the moment,
453 * so that we don't have to care about register pressure
455 return REMAT_COST_LIMIT;
459 costs += check_remat_conditions_costs(env, arg, reloader, parentcosts + costs);
460 if(parentcosts + costs >= REMAT_COST_LIMIT)
461 return REMAT_COST_LIMIT;
467 static int check_remat_conditions(spill_env_t *env, ir_node *spilled, ir_node *reloader) {
468 int costs = check_remat_conditions_costs(env, spilled, reloader, 1);
470 return costs < REMAT_COST_LIMIT;
474 * Re-materialize a node.
476 * @param senv the spill environment
477 * @param spilled the node that was spilled
478 * @param reloader a irn that requires a reload
480 static ir_node *do_remat(spill_env_t *env, ir_node *spilled, ir_node *reloader) {
483 ir_node *bl = get_nodes_block(reloader);
486 ins = alloca(get_irn_arity(spilled) * sizeof(ins[0]));
487 for(i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
488 ir_node *arg = get_irn_n(spilled, i);
490 if(is_value_available(env, arg, reloader)) {
493 ins[i] = do_remat(env, arg, reloader);
497 /* create a copy of the node */
498 res = new_ir_node(get_irn_dbg_info(spilled), env->chordal_env->irg, bl,
500 get_irn_mode(spilled),
501 get_irn_arity(spilled),
503 copy_node_attr(spilled, res);
505 DBG((env->dbg, LEVEL_1, "Insert remat %+F before reloader %+F\n", res, reloader));
507 /* insert in schedule */
508 assert(!is_Block(reloader));
509 sched_add_before(reloader, res);
516 * |_ _|_ __ ___ ___ _ __| |_ | _ \ ___| | ___ __ _ __| |___
517 * | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ |/ _ \ / _` |/ _` / __|
518 * | || | | \__ \ __/ | | |_ | _ < __/ | (_) | (_| | (_| \__ \
519 * |___|_| |_|___/\___|_| \__| |_| \_\___|_|\___/ \__,_|\__,_|___/
523 void be_insert_spills_reloads(spill_env_t *env) {
524 const arch_env_t *arch_env = env->arch_env;
527 /* process each spilled node */
528 DBG((env->dbg, LEVEL_1, "Insert spills and reloads:\n"));
529 for(si = set_first(env->spills); si; si = set_next(env->spills)) {
531 ir_mode *mode = get_irn_mode(si->spilled_node);
532 pset *values = pset_new_ptr(16);
534 /* go through all reloads for this spill */
535 for(rld = si->reloaders; rld; rld = rld->next) {
538 if (check_remat_conditions(env, si->spilled_node, rld->reloader)) {
539 new_val = do_remat(env, si->spilled_node, rld->reloader);
541 /* make sure we have a spill */
545 new_val = be_reload(arch_env, env->cls, rld->reloader, mode, si->spill);
548 DBG((env->dbg, LEVEL_1, " %+F of %+F before %+F\n", new_val, si->spilled_node, rld->reloader));
549 pset_insert_ptr(values, new_val);
552 if(pset_count(values) > 0) {
553 /* introduce copies, rewire the uses */
554 pset_insert_ptr(values, si->spilled_node);
555 be_ssa_constr_set_ignore(env->chordal_env->dom_front, env->chordal_env->lv, values, env->mem_phis);
560 si->reloaders = NULL;
563 be_remove_dead_nodes_from_schedule(env->chordal_env->irg);
564 be_liveness_recompute(env->chordal_env->lv);