X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fbe%2Fbespillutil.c;h=1b2023010159485d9ffee893937cb4d4e81bf6ae;hb=3dff5ea08f916551668dc18b449327a8a593bc9f;hp=a8109542e8e792c22288ef3d859d62af896fd386;hpb=1a26f4853c07d1ecd68a097409dd602edfe29eff;p=libfirm diff --git a/ir/be/bespillutil.c b/ir/be/bespillutil.c index a8109542e..1b2023010 100644 --- a/ir/be/bespillutil.c +++ b/ir/be/bespillutil.c @@ -21,8 +21,7 @@ * @file * @brief implementation of the spill/reload placement abstraction layer * @author Daniel Grund, Sebastian Hack, Matthias Braun - * @date 29.09.2005 - * @version $Id$ + * @date 29.09.2005 */ #include "config.h" @@ -54,9 +53,10 @@ #include "belive_t.h" #include "benode.h" #include "bechordal_t.h" -#include "bestatevent.h" +#include "statev_t.h" #include "bessaconstr.h" #include "beirg.h" +#include "beirgmod.h" #include "beintlive_t.h" #include "bemodule.h" #include "be_t.h" @@ -92,26 +92,25 @@ struct spill_info_t { double spill_costs; /**< costs needed for spilling the value */ const arch_register_class_t *reload_cls; /** the register class in which the reload should be placed */ + bool spilled_phi; /* true when the whole Phi has been spilled and + will be replaced with a PhiM. false if only the + value of the Phi gets spilled */ }; struct spill_env_t { const arch_env_t *arch_env; ir_graph *irg; struct obstack obst; - be_irg_t *birg; int spill_cost; /**< the cost of a single spill node */ int reload_cost; /**< the cost of a reload node */ set *spills; /**< all spill_info_t's, which must be placed */ - ir_nodeset_t mem_phis; /**< set of all spilled phis. */ - ir_exec_freq *exec_freq; + spill_info_t **mem_phis; /**< set of all spilled phis. */ -#ifdef FIRM_STATISTICS unsigned spill_count; unsigned reload_count; unsigned remat_count; unsigned spilled_phi_count; -#endif }; /** @@ -119,8 +118,8 @@ struct spill_env_t { */ static int cmp_spillinfo(const void *x, const void *y, size_t size) { - const spill_info_t *xx = x; - const spill_info_t *yy = y; + const spill_info_t *xx = (const spill_info_t*)x; + const spill_info_t *yy = (const spill_info_t*)y; (void) size; return xx->to_spill != yy->to_spill; @@ -135,40 +134,37 @@ static spill_info_t *get_spillinfo(const spill_env_t *env, ir_node *value) int hash = hash_irn(value); info.to_spill = value; - res = set_find(env->spills, &info, sizeof(info), hash); + res = set_find(spill_info_t, env->spills, &info, sizeof(info), hash); if (res == NULL) { info.reloaders = NULL; info.spills = NULL; info.spill_costs = -1; info.reload_cls = NULL; - res = set_insert(env->spills, &info, sizeof(info), hash); + info.spilled_phi = false; + res = set_insert(spill_info_t, env->spills, &info, sizeof(info), hash); } return res; } -spill_env_t *be_new_spill_env(be_irg_t *birg) +spill_env_t *be_new_spill_env(ir_graph *irg) { - const arch_env_t *arch_env = birg->main_env->arch_env; + const arch_env_t *arch_env = be_get_irg_arch_env(irg); spill_env_t *env = XMALLOC(spill_env_t); - env->spills = new_set(cmp_spillinfo, 1024); - env->irg = be_get_birg_irg(birg); - env->birg = birg; + env->spills = new_set(cmp_spillinfo, 1024); + env->irg = irg; env->arch_env = arch_env; - ir_nodeset_init(&env->mem_phis); + env->mem_phis = NEW_ARR_F(spill_info_t*, 0); env->spill_cost = arch_env->spill_cost; env->reload_cost = arch_env->reload_cost; - env->exec_freq = be_get_birg_exec_freq(birg); obstack_init(&env->obst); -#ifdef FIRM_STATISTICS env->spill_count = 0; env->reload_count = 0; env->remat_count = 0; env->spilled_phi_count = 0; -#endif return env; } @@ -176,7 +172,7 @@ spill_env_t *be_new_spill_env(be_irg_t *birg) void be_delete_spill_env(spill_env_t *env) { del_set(env->spills); - ir_nodeset_destroy(&env->mem_phis); + DEL_ARR_F(env->mem_phis); obstack_free(&env->obst, NULL); free(env); } @@ -193,12 +189,11 @@ void be_delete_spill_env(spill_env_t *env) void be_add_spill(spill_env_t *env, ir_node *to_spill, ir_node *after) { spill_info_t *spill_info = get_spillinfo(env, to_spill); - const ir_node *insn = skip_Proj_const(to_spill); spill_t *spill; spill_t *s; spill_t *last; - assert(!arch_irn_is(insn, dont_spill)); + assert(!arch_irn_is(skip_Proj_const(to_spill), dont_spill)); DB((dbg, LEVEL_1, "Add spill of %+F after %+F\n", to_spill, after)); /* Just for safety make sure that we do not insert the spill in front of a phi */ @@ -234,38 +229,14 @@ void be_add_spill(spill_env_t *env, ir_node *to_spill, ir_node *after) spill_info->spills = spill; } -void be_add_remat(spill_env_t *env, ir_node *to_spill, ir_node *before, - ir_node *rematted_node) -{ - spill_info_t *spill_info; - reloader_t *reloader; - - spill_info = get_spillinfo(env, to_spill); - - /* add the remat information */ - reloader = OALLOC(&env->obst, reloader_t); - reloader->next = spill_info->reloaders; - reloader->reloader = before; - reloader->rematted_node = rematted_node; - reloader->remat_cost_delta = 0; /* We will never have a cost win over a - reload since we're not even allowed to - create a reload */ - - spill_info->reloaders = reloader; - - DBG((dbg, LEVEL_1, "creating spillinfo for %+F, will be rematerialized before %+F\n", - to_spill, before)); -} - void be_add_reload2(spill_env_t *env, ir_node *to_spill, ir_node *before, ir_node *can_spill_after, const arch_register_class_t *reload_cls, int allow_remat) { spill_info_t *info; reloader_t *rel; - const ir_node *insn = skip_Proj_const(to_spill); - assert(!arch_irn_is(insn, dont_spill)); + assert(!arch_irn_is(skip_Proj_const(to_spill), dont_spill)); info = get_spillinfo(env, to_spill); @@ -320,8 +291,13 @@ ir_node *be_get_end_of_block_insertion_point(const ir_node *block) return last; } -static ir_node *skip_keeps_phis(ir_node *node) +/** + * determine final spill position: it should be after all phis, keep nodes + * and behind nodes marked as prolog + */ +static ir_node *determine_spill_point(ir_node *node) { + node = skip_Proj(node); while (true) { ir_node *next = sched_next(node); if (!is_Phi(next) && !be_is_Keep(next) && !be_is_CopyKeep(next)) @@ -362,7 +338,7 @@ void be_add_reload_at_end(spill_env_t *env, ir_node *to_spill, } void be_add_reload_on_edge(spill_env_t *env, ir_node *to_spill, ir_node *block, - int pos, const arch_register_class_t *reload_cls, + int pos, const arch_register_class_t *reload_cls, int allow_remat) { ir_node *before = get_block_insertion_point(block, pos); @@ -372,20 +348,20 @@ void be_add_reload_on_edge(spill_env_t *env, ir_node *to_spill, ir_node *block, void be_spill_phi(spill_env_t *env, ir_node *node) { ir_node *block; - spill_info_t* spill; int i, arity; + spill_info_t *info; assert(is_Phi(node)); - ir_nodeset_insert(&env->mem_phis, node); + info = get_spillinfo(env, node); + info->spilled_phi = true; + ARR_APP1(spill_info_t*, env->mem_phis, info); /* create spills for the phi arguments */ block = get_nodes_block(node); - spill = get_spillinfo(env, node); for (i = 0, arity = get_irn_arity(node); i < arity; ++i) { ir_node *arg = get_irn_n(node, i); ir_node *insert; - //get_spillinfo(env, arg); /* some backends have virtual noreg/unknown nodes that are not scheduled * and simply always available. */ @@ -394,7 +370,7 @@ void be_spill_phi(spill_env_t *env, ir_node *node) insert = be_get_end_of_block_insertion_point(pred_block); insert = sched_prev(insert); } else { - insert = skip_keeps_phis(arg); + insert = determine_spill_point(arg); } be_add_spill(env, arg, insert); @@ -434,7 +410,8 @@ static void spill_irn(spill_env_t *env, spill_info_t *spillinfo) * and simply always available. */ if (!sched_is_scheduled(insn)) { /* override spillinfos or create a new one */ - spillinfo->spills->spill = new_NoMem(); + ir_graph *irg = get_irn_irg(to_spill); + spillinfo->spills->spill = get_irg_no_mem(irg); DB((dbg, LEVEL_1, "don't spill %+F use NoMem\n", to_spill)); return; } @@ -443,16 +420,11 @@ static void spill_irn(spill_env_t *env, spill_info_t *spillinfo) spill = spillinfo->spills; for ( ; spill != NULL; spill = spill->next) { ir_node *after = spill->after; - ir_node *block = get_block(after); - - after = skip_keeps_phis(after); + after = determine_spill_point(after); - spill->spill = be_spill(block, to_spill); - sched_add_after(skip_Proj(after), spill->spill); + spill->spill = arch_env_new_spill(env->arch_env, to_spill, after); DB((dbg, LEVEL_1, "\t%+F after %+F\n", spill->spill, after)); -#ifdef FIRM_STATISTICS env->spill_count++; -#endif } DBG((dbg, LEVEL_1, "\n")); } @@ -494,14 +466,13 @@ static void spill_phi(spill_env_t *env, spill_info_t *spillinfo) /* override or replace spills list... */ spill = OALLOC(&env->obst, spill_t); - spill->after = skip_keeps_phis(phi); - spill->spill = new_r_Phi(block, arity, ins, mode_M); + spill->after = determine_spill_point(phi); + spill->spill = be_new_Phi(block, arity, ins, mode_M, arch_no_register_req); spill->next = NULL; + sched_add_after(block, spill->spill); spillinfo->spills = spill; -#ifdef FIRM_STATISTICS env->spilled_phi_count++; -#endif for (i = 0; i < arity; ++i) { ir_node *arg = get_irn_n(phi, i); @@ -524,15 +495,11 @@ static void spill_phi(spill_env_t *env, spill_info_t *spillinfo) */ static void spill_node(spill_env_t *env, spill_info_t *spillinfo) { - ir_node *to_spill; - /* node is already spilled */ if (spillinfo->spills != NULL && spillinfo->spills->spill != NULL) return; - to_spill = spillinfo->to_spill; - - if (is_Phi(to_spill) && ir_nodeset_contains(&env->mem_phis, to_spill)) { + if (spillinfo->spilled_phi) { spill_phi(env, spillinfo); } else { spill_irn(env, spillinfo); @@ -556,7 +523,7 @@ static void spill_node(spill_env_t *env, spill_info_t *spillinfo) static int is_value_available(spill_env_t *env, const ir_node *arg, const ir_node *reloader) { - if (is_Unknown(arg) || arg == new_NoMem()) + if (is_Unknown(arg) || is_NoMem(arg)) return 1; if (be_is_Spill(skip_Proj_const(arg))) @@ -567,13 +534,16 @@ static int is_value_available(spill_env_t *env, const ir_node *arg, (void)reloader; + if (get_irn_mode(arg) == mode_T) + return 0; + /* * Ignore registers are always available */ if (arch_irn_is_ignore(arg)) return 1; - return 0; + return 0; } /** @@ -607,7 +577,7 @@ static int check_remat_conditions_costs(spill_env_t *env, return REMAT_COST_INFINITE; } /* never rematerialize a node which modifies the flags. - * (would be better to test wether the flags are actually live at point + * (would be better to test whether the flags are actually live at point * reloader...) */ if (arch_irn_is(insn, modify_flags)) { @@ -641,7 +611,7 @@ static int check_remat_conditions_costs(spill_env_t *env, /** * Re-materialize a node. * - * @param senv the spill environment + * @param env the spill environment * @param spilled the node that was spilled * @param reloader a irn that requires a reload */ @@ -666,10 +636,8 @@ static ir_node *do_remat(spill_env_t *env, ir_node *spilled, ir_node *reloader) ins[i] = arg; } else { ins[i] = do_remat(env, arg, reloader); -#ifdef FIRM_STATISTICS - /* don't count the recursive call as remat */ - env->remat_count--; -#endif + /* don't count the argument rematerialization as an extra remat */ + --env->remat_count; } } @@ -677,9 +645,8 @@ static ir_node *do_remat(spill_env_t *env, ir_node *spilled, ir_node *reloader) res = new_ir_node(get_irn_dbg_info(spilled), env->irg, bl, get_irn_op(spilled), get_irn_mode(spilled), get_irn_arity(spilled), ins); - copy_node_attr(spilled, res); + copy_node_attr(env->irg, spilled, res); arch_env_mark_remat(env->arch_env, res); - new_backedge_info(res); DBG((dbg, LEVEL_1, "Insert remat %+F of %+F before reloader %+F\n", res, spilled, reloader)); @@ -687,9 +654,7 @@ static ir_node *do_remat(spill_env_t *env, ir_node *spilled, ir_node *reloader) /* insert in schedule */ sched_reset(res); sched_add_before(reloader, res); -#ifdef FIRM_STATISTICS - env->remat_count++; -#endif + ++env->remat_count; } return res; @@ -698,7 +663,7 @@ static ir_node *do_remat(spill_env_t *env, ir_node *spilled, ir_node *reloader) double be_get_spill_costs(spill_env_t *env, ir_node *to_spill, ir_node *before) { ir_node *block = get_nodes_block(before); - double freq = get_block_execfreq(env->exec_freq, block); + double freq = get_block_execfreq(block); (void) to_spill; return env->spill_cost * freq; @@ -719,8 +684,8 @@ unsigned be_get_reload_costs_no_weight(spill_env_t *env, const ir_node *to_spill double be_get_reload_costs(spill_env_t *env, ir_node *to_spill, ir_node *before) { - ir_node *block = get_nodes_block(before); - double freq = get_block_execfreq(env->exec_freq, block); + ir_node *block = get_nodes_block(before); + double freq = get_block_execfreq(block); if (be_do_remats) { /* is the node rematerializable? */ @@ -745,6 +710,39 @@ double be_get_reload_costs_on_edge(spill_env_t *env, ir_node *to_spill, return be_get_reload_costs(env, to_spill, before); } +ir_node *be_new_spill(ir_node *value, ir_node *after) +{ + ir_graph *irg = get_irn_irg(value); + ir_node *frame = get_irg_frame(irg); + const arch_register_class_t *cls = arch_get_irn_reg_class(value); + const arch_register_class_t *cls_frame = arch_get_irn_reg_class(frame); + ir_node *block = get_block(after); + ir_node *spill + = be_new_Spill(cls, cls_frame, block, frame, value); + + sched_add_after(after, spill); + return spill; +} + +ir_node *be_new_reload(ir_node *value, ir_node *spill, ir_node *before) +{ + ir_graph *irg = get_irn_irg(value); + ir_node *frame = get_irg_frame(irg); + ir_node *block = get_block(before); + const arch_register_class_t *cls = arch_get_irn_reg_class(value); + const arch_register_class_t *cls_frame = arch_get_irn_reg_class(frame); + ir_mode *mode = get_irn_mode(value); + ir_node *reload; + + assert(be_is_Spill(spill) || is_Phi(spill)); + assert(get_irn_mode(spill) == mode_M); + + reload = be_new_Reload(cls, cls_frame, block, frame, spill, mode); + sched_add_before(before, reload); + + return reload; +} + /* * ___ _ ____ _ _ * |_ _|_ __ ___ ___ _ __| |_ | _ \ ___| | ___ __ _ __| |___ @@ -778,11 +776,12 @@ static void determine_spill_costs(spill_env_t *env, spill_info_t *spillinfo) * predecessor (of a PhiM) but this test might match other things too... */ if (!sched_is_scheduled(insn)) { + ir_graph *irg = get_irn_irg(to_spill); /* override spillinfos or create a new one */ spill_t *spill = OALLOC(&env->obst, spill_t); spill->after = NULL; spill->next = NULL; - spill->spill = new_NoMem(); + spill->spill = get_irg_no_mem(irg); spillinfo->spills = spill; spillinfo->spill_costs = 0; @@ -792,9 +791,9 @@ static void determine_spill_costs(spill_env_t *env, spill_info_t *spillinfo) } spill_block = get_nodes_block(insn); - spill_execfreq = get_block_execfreq(env->exec_freq, spill_block); + spill_execfreq = get_block_execfreq(spill_block); - if (is_Phi(to_spill) && ir_nodeset_contains(&env->mem_phis, to_spill)) { + if (spillinfo->spilled_phi) { /* TODO calculate correct costs... * (though we can't remat this node anyway so no big problem) */ spillinfo->spill_costs = env->spill_cost * spill_execfreq; @@ -810,7 +809,7 @@ static void determine_spill_costs(spill_env_t *env, spill_info_t *spillinfo) s = spillinfo->spills; for ( ; s != NULL; s = s->next) { ir_node *spill_block = get_block(s->after); - double freq = get_block_execfreq(env->exec_freq, spill_block); + double freq = get_block_execfreq(spill_block); spills_execfreq += freq; } @@ -829,7 +828,7 @@ static void determine_spill_costs(spill_env_t *env, spill_info_t *spillinfo) /* override spillinfos or create a new one */ spill = OALLOC(&env->obst, spill_t); - spill->after = skip_keeps_phis(to_spill); + spill->after = determine_spill_point(to_spill); spill->next = NULL; spill->spill = NULL; @@ -874,28 +873,25 @@ void make_spill_locations_dominate_irn(spill_env_t *env, ir_node *irn) void be_insert_spills_reloads(spill_env_t *env) { - const ir_exec_freq *exec_freq = env->exec_freq; - spill_info_t *si; - ir_nodeset_iterator_t iter; - ir_node *node; + size_t n_mem_phis = ARR_LEN(env->mem_phis); + size_t i; be_timer_push(T_RA_SPILL_APPLY); /* create all phi-ms first, this is needed so, that phis, hanging on spilled phis work correctly */ - foreach_ir_nodeset(&env->mem_phis, node, iter) { - spill_info_t *info = get_spillinfo(env, node); + for (i = 0; i < n_mem_phis; ++i) { + spill_info_t *info = env->mem_phis[i]; spill_node(env, info); } /* process each spilled node */ - for (si = set_first(env->spills); si; si = set_next(env->spills)) { - reloader_t *rld; + foreach_set(env->spills, spill_info_t, si) { ir_node *to_spill = si->to_spill; - ir_mode *mode = get_irn_mode(to_spill); ir_node **copies = NEW_ARR_F(ir_node*, 0); double all_remat_costs = 0; /** costs when we would remat all nodes */ - int force_remat = 0; + bool force_remat = false; + reloader_t *rld; DBG((dbg, LEVEL_1, "\nhandling all reloaders of %+F:\n", to_spill)); @@ -937,7 +933,7 @@ void be_insert_spills_reloads(spill_env_t *env) remat_cost_delta = remat_cost - env->reload_cost; rld->remat_cost_delta = remat_cost_delta; block = is_Block(reloader) ? reloader : get_nodes_block(reloader); - freq = get_block_execfreq(exec_freq, block); + freq = get_block_execfreq(block); all_remat_costs += remat_cost_delta * freq; DBG((dbg, LEVEL_2, "\tremat costs delta before %+F: " "%d (rel %f)\n", reloader, remat_cost_delta, @@ -955,7 +951,7 @@ void be_insert_spills_reloads(spill_env_t *env) if (all_remat_costs < 0) { DBG((dbg, LEVEL_1, "\nforcing remats of all reloaders (%f)\n", all_remat_costs)); - force_remat = 1; + force_remat = true; } } @@ -976,11 +972,9 @@ void be_insert_spills_reloads(spill_env_t *env) /* create a reload, use the first spill for now SSA * reconstruction for memory comes below */ assert(si->spills != NULL); - copy = be_reload(si->reload_cls, rld->reloader, mode, - si->spills->spill); -#ifdef FIRM_STATISTICS + copy = arch_env_new_reload(env->arch_env, si->to_spill, + si->spills->spill, rld->reloader); env->reload_count++; -#endif } DBG((dbg, LEVEL_1, " %+F of %+F before %+F\n", @@ -992,9 +986,9 @@ void be_insert_spills_reloads(spill_env_t *env) * SSA form for the spilled value */ if (ARR_LEN(copies) > 0) { be_ssa_construction_env_t senv; - /* be_lv_t *lv = be_get_birg_liveness(env->birg); */ + /* be_lv_t *lv = be_get_irg_liveness(env->irg); */ - be_ssa_construction_init(&senv, env->birg); + be_ssa_construction_init(&senv, env->irg); be_ssa_construction_add_copy(&senv, to_spill); be_ssa_construction_add_copies(&senv, copies, ARR_LEN(copies)); be_ssa_construction_fix_users(&senv, to_spill); @@ -1018,7 +1012,7 @@ void be_insert_spills_reloads(spill_env_t *env) be_ssa_construction_env_t senv; - be_ssa_construction_init(&senv, env->birg); + be_ssa_construction_init(&senv, env->irg); spill = si->spills; for ( ; spill != NULL; spill = spill->next) { /* maybe we rematerialized the value and need no spill */ @@ -1046,16 +1040,15 @@ void be_insert_spills_reloads(spill_env_t *env) /* Matze: In theory be_ssa_construction should take care of the liveness... * try to disable this again in the future */ - be_liveness_invalidate(env->birg->lv); + be_invalidate_live_sets(env->irg); - be_remove_dead_nodes_from_schedule(env->birg); + be_remove_dead_nodes_from_schedule(env->irg); be_timer_pop(T_RA_SPILL_APPLY); } +BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spill) void be_init_spill(void) { FIRM_DBG_REGISTER(dbg, "firm.be.spill"); } - -BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spill);