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"
17 #include "irbackedge_t.h"
26 #include "unionfind.h"
30 #include "besched_t.h"
34 #include "bechordal_t.h"
35 #include "bejavacoal.h"
36 #include "benodesets.h"
37 #include "bespilloptions.h"
38 #include "bestatevent.h"
40 // only rematerialise when costs are less than REMAT_COST_LIMIT
41 // TODO determine a good value here...
42 #define REMAT_COST_LIMIT 10
44 typedef struct _reloader_t reloader_t;
51 typedef struct _spill_info_t {
52 /** the value that should get spilled */
53 ir_node *spilled_node;
54 /** list of places where the value should get reloaded */
55 reloader_t *reloaders;
57 /** the spill node, or a PhiM node */
59 /** if we had the value of a phi spilled before but not the phi itself then
60 * this field contains the spill for the phi value */
65 const arch_register_class_t *cls;
66 const arch_env_t *arch_env;
67 const be_chordal_env_t *chordal_env;
69 int spill_cost; /**< the cost of a single spill node */
70 int reload_cost; /**< the cost of a reload node */
71 set *spills; /**< all spill_info_t's, which must be placed */
72 pset *mem_phis; /**< set of all special spilled phis. allocated and freed separately */
74 DEBUG_ONLY(firm_dbg_module_t *dbg;)
78 * Compare two spill infos.
80 static int cmp_spillinfo(const void *x, const void *y, size_t size) {
81 const spill_info_t *xx = x;
82 const spill_info_t *yy = y;
83 return xx->spilled_node != yy->spilled_node;
87 * Returns spill info for a specific value (returns NULL if the info doesn't
90 static spill_info_t *find_spillinfo(const spill_env_t *env, ir_node *value) {
92 int hash = nodeset_hash(value);
94 info.spilled_node = value;
96 return set_find(env->spills, &info, sizeof(info), hash);
100 * Returns spill info for a specific value (the value that is to be spilled)
102 static spill_info_t *get_spillinfo(const spill_env_t *env, ir_node *value) {
103 spill_info_t info, *res;
104 int hash = nodeset_hash(value);
106 info.spilled_node = value;
107 res = set_find(env->spills, &info, sizeof(info), hash);
110 info.reloaders = NULL;
112 info.old_spill = NULL;
113 res = set_insert(env->spills, &info, sizeof(info), hash);
120 /* Sets the debug module of a spill environment. */
121 void be_set_spill_env_dbg_module(spill_env_t *env, firm_dbg_module_t *dbg) {
126 /* Creates a new spill environment. */
127 spill_env_t *be_new_spill_env(const be_chordal_env_t *chordal_env) {
128 spill_env_t *env = xmalloc(sizeof(env[0]));
129 env->spills = new_set(cmp_spillinfo, 1024);
130 env->cls = chordal_env->cls;
131 env->chordal_env = chordal_env;
132 env->arch_env = env->chordal_env->birg->main_env->arch_env;
133 env->mem_phis = pset_new_ptr_default();
134 // TODO, ask backend about costs...
136 env->reload_cost = 5;
137 obstack_init(&env->obst);
141 /* Deletes a spill environment. */
142 void be_delete_spill_env(spill_env_t *env) {
143 del_set(env->spills);
144 del_pset(env->mem_phis);
145 obstack_free(&env->obst, NULL);
151 * | _ \| | __ _ ___ ___ | _ \ ___| | ___ __ _ __| |___
152 * | |_) | |/ _` |/ __/ _ \ | |_) / _ \ |/ _ \ / _` |/ _` / __|
153 * | __/| | (_| | (_| __/ | _ < __/ | (_) | (_| | (_| \__ \
154 * |_| |_|\__,_|\___\___| |_| \_\___|_|\___/ \__,_|\__,_|___/
158 void be_add_reload(spill_env_t *env, ir_node *to_spill, ir_node *before) {
162 assert(sched_is_scheduled(before));
163 assert(arch_irn_consider_in_reg_alloc(env->arch_env, env->cls, to_spill));
165 info = get_spillinfo(env, to_spill);
167 if(is_Phi(to_spill)) {
169 // create spillinfos for the phi arguments
170 for(i = 0, arity = get_irn_arity(to_spill); i < arity; ++i) {
171 ir_node *arg = get_irn_n(to_spill, i);
172 get_spillinfo(env, arg);
176 rel = obstack_alloc(&env->obst, sizeof(rel[0]));
177 rel->reloader = before;
178 rel->next = info->reloaders;
179 info->reloaders = rel;
182 static ir_node *get_reload_insertion_point(ir_node *block, int pos) {
183 ir_node *predblock, *last;
185 /* simply add the reload to the beginning of the block if we only have 1 predecessor
186 * (we don't need to check for phis as there can't be any in a block with only 1 pred)
188 if(get_Block_n_cfgpreds(block) == 1) {
189 assert(!is_Phi(sched_first(block)));
190 return sched_first(block);
193 /* We have to reload the value in pred-block */
194 predblock = get_Block_cfgpred_block(block, pos);
195 last = sched_last(predblock);
197 /* we might have projs and keepanys behind the jump... */
198 while(is_Proj(last) || be_is_Keep(last)) {
199 last = sched_prev(last);
200 assert(!sched_is_end(last));
204 ir_graph *irg = get_irn_irg(block);
205 ir_node *startblock = get_irg_start_block(irg);
207 last = sched_next(last);
208 // last node must be a cfop, only exception is the start block
209 assert(last == startblock);
212 // add the reload before the (cond-)jump
216 void be_add_reload_on_edge(spill_env_t *env, ir_node *to_spill, ir_node *block, int pos) {
217 ir_node *before = get_reload_insertion_point(block, pos);
218 be_add_reload(env, to_spill, before);
221 void be_spill_phi(spill_env_t *env, ir_node *node) {
225 assert(is_Phi(node));
227 pset_insert_ptr(env->mem_phis, node);
229 // create spillinfos for the phi arguments
230 spill = get_spillinfo(env, node);
231 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
232 ir_node *arg = get_irn_n(node, i);
233 get_spillinfo(env, arg);
236 // if we had a spill for the phi value before, then remove this spill from
237 // schedule, as we will remove it in the insert spill/reload phase
238 if(spill->spill != NULL && !is_Phi(spill->spill)) {
239 spill->old_spill = spill->spill;
246 * / ___|_ __ ___ __ _| |_ ___ / ___| _ __ (_) | |___
247 * | | | '__/ _ \/ _` | __/ _ \ \___ \| '_ \| | | / __|
248 * | |___| | | __/ (_| | || __/ ___) | |_) | | | \__ \
249 * \____|_| \___|\__,_|\__\___| |____/| .__/|_|_|_|___/
254 * Schedules a node after an instruction. (That is the place after all projs and phis
255 * that are scheduled after the instruction)
256 * This function also skips phi nodes at the beginning of a block
258 static void sched_add_after_insn(ir_node *sched_after, ir_node *node) {
259 ir_node *next = sched_next(sched_after);
260 while(is_Proj(next) || is_Phi(next)) {
261 next = sched_next(next);
263 assert(next != NULL);
265 if(sched_is_end(next)) {
266 sched_add_after(sched_last(get_nodes_block(sched_after)), node);
268 sched_add_before(next, node);
275 * @param senv the spill environment
276 * @param irn the node that should be spilled
277 * @param ctx_irn an user of the spilled node
279 * @return a be_Spill node
281 static void spill_irn(spill_env_t *env, spill_info_t *spillinfo) {
282 ir_node *to_spill = spillinfo->spilled_node;
284 DBG((env->dbg, LEVEL_1, "%+F\n", to_spill));
286 /* Trying to spill an already spilled value, no need for a new spill
287 * node then, we can simply connect to the same one for this reload
289 * (although rematerialization code should handle most of these cases
290 * this can still happen when spilling Phis)
292 if(be_is_Reload(to_spill)) {
293 spillinfo->spill = get_irn_n(to_spill, be_pos_Reload_mem);
297 if (arch_irn_is(env->arch_env, to_spill, dont_spill)) {
298 if (env->chordal_env->opts->vrfy_option == BE_CH_VRFY_WARN)
299 ir_fprintf(stderr, "Verify warning: spilling 'dont_spill' node %+F\n", to_spill);
300 else if (env->chordal_env->opts->vrfy_option == BE_CH_VRFY_ASSERT)
301 assert(0 && "Attempt to spill a node marked 'dont_spill'");
304 spillinfo->spill = be_spill(env->arch_env, to_spill);
305 sched_add_after_insn(to_spill, spillinfo->spill);
308 static void spill_node(spill_env_t *env, spill_info_t *spillinfo);
311 * If the first usage of a Phi result would be out of memory
312 * there is no sense in allocating a register for it.
313 * Thus we spill it and all its operands to the same spill slot.
314 * Therefore the phi/dataB becomes a phi/Memory
316 * @param senv the spill environment
317 * @param phi the Phi node that should be spilled
318 * @param ctx_irn an user of the spilled node
320 static void spill_phi(spill_env_t *env, spill_info_t *spillinfo) {
321 ir_node *phi = spillinfo->spilled_node;
323 int arity = get_irn_arity(phi);
324 ir_node *block = get_nodes_block(phi);
329 /* build a new PhiM */
330 ins = alloca(sizeof(ir_node*) * arity);
331 for(i = 0; i < arity; ++i) {
332 ins[i] = get_irg_bad(env->chordal_env->irg);
334 spillinfo->spill = new_r_Phi(env->chordal_env->irg, block, arity, ins, mode_M);
336 for(i = 0; i < arity; ++i) {
337 ir_node *arg = get_irn_n(phi, i);
338 spill_info_t *arg_info = get_spillinfo(env, arg);
340 spill_node(env, arg_info);
342 set_irn_n(spillinfo->spill, i, arg_info->spill);
345 // rewire reloads from old_spill to phi
346 if(spillinfo->old_spill != NULL) {
347 const ir_edge_t *edge, *next;
348 foreach_out_edge_safe(spillinfo->old_spill, edge, next) {
349 ir_node* reload = get_edge_src_irn(edge);
350 assert(be_is_Reload(reload) || is_Phi(reload));
351 set_irn_n(reload, get_edge_src_pos(edge), spillinfo->spill);
353 spillinfo->old_spill = NULL;
360 * @param senv the spill environment
361 * @param to_spill the node that should be spilled
363 static void spill_node(spill_env_t *env, spill_info_t *spillinfo) {
366 // the node should be tagged for spilling already...
367 if(spillinfo->spill != NULL)
370 to_spill = spillinfo->spilled_node;
371 if (is_Phi(to_spill) && pset_find_ptr(env->mem_phis, spillinfo->spilled_node)) {
372 spill_phi(env, spillinfo);
374 spill_irn(env, spillinfo);
381 * | _ \ ___ _ __ ___ __ _| |_ ___ _ __(_) __ _| (_)_______
382 * | |_) / _ \ '_ ` _ \ / _` | __/ _ \ '__| |/ _` | | |_ / _ \
383 * | _ < __/ | | | | | (_| | || __/ | | | (_| | | |/ / __/
384 * |_| \_\___|_| |_| |_|\__,_|\__\___|_| |_|\__,_|_|_/___\___|
389 * Tests whether value @p arg is available before node @p reloader
390 * @returns 1 if value is available, 0 otherwise
392 static int is_value_available(spill_env_t *env, ir_node *arg, ir_node *reloader) {
393 if(is_Unknown(arg) || arg == new_NoMem())
399 if(arg == get_irg_frame(env->chordal_env->irg))
402 /* the following test does not work while spilling,
403 * because the liveness info is not adapted yet to the effects of the
404 * additional spills/reloads.
406 * So we can only do this test for ignore registers (of our register class)
408 if(arch_get_irn_reg_class(env->arch_env, arg, -1) == env->chordal_env->cls
409 && arch_irn_is(env->arch_env, arg, ignore)) {
412 /* we want to remat before the insn reloader
413 * thus an arguments is alive if
414 * - it interferes with the reloaders result
415 * - or it is (last-) used by reloader itself
417 if (values_interfere(env->chordal_env->lv, reloader, arg)) {
421 arity = get_irn_arity(reloader);
422 for (i = 0; i < arity; ++i) {
423 ir_node *rel_arg = get_irn_n(reloader, i);
433 * Checks whether the node can principally be rematerialized
435 static int is_remat_node(spill_env_t *env, ir_node *node) {
436 const arch_env_t *arch_env = env->arch_env;
438 assert(!be_is_Spill(node));
440 if(arch_irn_is(arch_env, node, rematerializable)) {
444 if(be_is_StackParam(node))
451 * Check if a node is rematerializable. This tests for the following conditions:
453 * - The node itself is rematerializable
454 * - All arguments of the node are available or also rematerialisable
455 * - The costs for the rematerialisation operation is less or equal a limit
457 * Returns the costs needed for rematerialisation or something
458 * > REMAT_COST_LIMIT if remat is not possible.
460 static int check_remat_conditions_costs(spill_env_t *env, ir_node *spilled, ir_node *reloader, int parentcosts) {
465 if(!is_remat_node(env, spilled))
466 return REMAT_COST_LIMIT;
468 if(be_is_Reload(spilled)) {
471 costs += arch_get_op_estimated_cost(env->arch_env, spilled);
473 if(parentcosts + costs >= REMAT_COST_LIMIT) {
474 return REMAT_COST_LIMIT;
478 for(i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
479 ir_node *arg = get_irn_n(spilled, i);
481 if(is_value_available(env, arg, reloader))
484 // we have to rematerialize the argument as well...
486 /* we only support rematerializing 1 argument at the moment,
487 * so that we don't have to care about register pressure
489 return REMAT_COST_LIMIT;
493 costs += check_remat_conditions_costs(env, arg, reloader, parentcosts + costs);
494 if(parentcosts + costs >= REMAT_COST_LIMIT)
495 return REMAT_COST_LIMIT;
501 static int check_remat_conditions(spill_env_t *env, ir_node *spilled, ir_node *reloader) {
502 int costs = check_remat_conditions_costs(env, spilled, reloader, 0);
504 return costs < REMAT_COST_LIMIT;
508 * Re-materialize a node.
510 * @param senv the spill environment
511 * @param spilled the node that was spilled
512 * @param reloader a irn that requires a reload
514 static ir_node *do_remat(spill_env_t *env, ir_node *spilled, ir_node *reloader) {
517 ir_node *bl = get_nodes_block(reloader);
520 ins = alloca(get_irn_arity(spilled) * sizeof(ins[0]));
521 for(i = 0, arity = get_irn_arity(spilled); i < arity; ++i) {
522 ir_node *arg = get_irn_n(spilled, i);
524 if(is_value_available(env, arg, reloader)) {
527 ins[i] = do_remat(env, arg, reloader);
531 /* create a copy of the node */
532 res = new_ir_node(get_irn_dbg_info(spilled), env->chordal_env->irg, bl,
534 get_irn_mode(spilled),
535 get_irn_arity(spilled),
537 copy_node_attr(spilled, res);
538 new_backedge_info(res);
540 DBG((env->dbg, LEVEL_1, "Insert remat %+F before reloader %+F\n", res, reloader));
542 /* insert in schedule */
543 assert(!is_Block(reloader));
544 sched_add_before(reloader, res);
549 int be_get_reload_costs(spill_env_t *env, ir_node *to_spill, ir_node *before) {
550 spill_info_t *spill_info;
553 // is the node rematerializable?
554 int costs = check_remat_conditions_costs(env, to_spill, before, 0);
555 if(costs < REMAT_COST_LIMIT)
559 // do we already have a spill?
560 spill_info = find_spillinfo(env, to_spill);
561 if(spill_info != NULL && spill_info->spill != NULL)
562 return env->reload_cost;
564 return env->spill_cost + env->reload_cost;
567 int be_get_reload_costs_on_edge(spill_env_t *env, ir_node *to_spill, ir_node *block, int pos) {
568 ir_node *before = get_reload_insertion_point(block, pos);
569 return be_get_reload_costs(env, to_spill, before);
574 * |_ _|_ __ ___ ___ _ __| |_ | _ \ ___| | ___ __ _ __| |___
575 * | || '_ \/ __|/ _ \ '__| __| | |_) / _ \ |/ _ \ / _` |/ _` / __|
576 * | || | | \__ \ __/ | | |_ | _ < __/ | (_) | (_| | (_| \__ \
577 * |___|_| |_|___/\___|_| \__| |_| \_\___|_|\___/ \__,_|\__,_|___/
581 void be_insert_spills_reloads(spill_env_t *env) {
582 const arch_env_t *arch_env = env->arch_env;
588 /* process each spilled node */
589 for(si = set_first(env->spills); si; si = set_next(env->spills)) {
591 ir_mode *mode = get_irn_mode(si->spilled_node);
592 pset *values = pset_new_ptr(16);
594 /* go through all reloads for this spill */
595 for(rld = si->reloaders; rld; rld = rld->next) {
598 if (be_do_remats && check_remat_conditions(env, si->spilled_node, rld->reloader)) {
599 new_val = do_remat(env, si->spilled_node, rld->reloader);
602 /* make sure we have a spill */
603 if(si->spill == NULL) {
608 /* create a reload */
609 new_val = be_reload(arch_env, env->cls, rld->reloader, mode, si->spill);
613 DBG((env->dbg, LEVEL_1, " %+F of %+F before %+F\n", new_val, si->spilled_node, rld->reloader));
614 pset_insert_ptr(values, new_val);
617 if(pset_count(values) > 0) {
618 /* introduce copies, rewire the uses */
619 pset_insert_ptr(values, si->spilled_node);
620 be_ssa_constr_set_ignore(env->chordal_env->dom_front, env->chordal_env->lv, values, env->mem_phis);
625 si->reloaders = NULL;
628 if(be_stat_ev_is_active()) {
629 be_stat_ev("spill_spills", spills);
630 be_stat_ev("spill_reloads", reloads);
631 be_stat_ev("spill_remats", remats);
634 be_remove_dead_nodes_from_schedule(env->chordal_env->irg);
635 be_liveness_recompute(env->chordal_env->lv);