X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fbe%2Fbestate.c;h=5a57bdddbf9154541c8309b688e340c9597d32f0;hb=5fa1d6710012b17fe0c24f3edfb1d8fc066972a1;hp=2ee3738071665fd3e2ac650778815219eccd650c;hpb=1ce363f80e6a204d4011f85813362d9bd1d0e7e4;p=libfirm diff --git a/ir/be/bestate.c b/ir/be/bestate.c index 2ee373807..5a57bdddb 100644 --- a/ir/be/bestate.c +++ b/ir/be/bestate.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved. + * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved. * * This file is part of libFirm. * @@ -25,9 +25,7 @@ * @date 26.03.2007 * @version $Id$ */ -#ifdef HAVE_CONFIG_H #include "config.h" -#endif #include "bestate.h" @@ -43,12 +41,13 @@ #include "irnodemap.h" #include "adt/cpset.h" -#include "bearch_t.h" +#include "bearch.h" #include "beuses.h" -#include "besched_t.h" +#include "besched.h" #include "belive_t.h" #include "bemodule.h" -#include "benode_t.h" +#include "benode.h" +#include "beirgmod.h" #include "bessaconstr.h" DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;) @@ -62,7 +61,6 @@ typedef struct spill_info_t { typedef struct minibelady_env_t { struct obstack obst; - const arch_env_t *arch_env; const arch_register_t *reg; const be_lv_t *lv; void *func_env; @@ -79,11 +77,9 @@ typedef struct block_info_t { ir_node *end_state; } block_info_t; -static INLINE -block_info_t *new_block_info(struct obstack *obst, ir_node *block) +static inline block_info_t *new_block_info(struct obstack *obst, ir_node *block) { - block_info_t *res = obstack_alloc(obst, sizeof(*res)); - memset(res, 0, sizeof(res[0])); + block_info_t *res = OALLOCZ(obst, block_info_t); assert(is_Block(block)); set_irn_link(block, res); @@ -92,18 +88,15 @@ block_info_t *new_block_info(struct obstack *obst, ir_node *block) return res; } -static INLINE -block_info_t *get_block_info(ir_node *block) +static inline block_info_t *get_block_info(ir_node *block) { assert(irn_visited(block)); return (block_info_t*) get_irn_link(block); } -static INLINE -spill_info_t *create_spill_info(minibelady_env_t *env, ir_node *state) +static inline spill_info_t *create_spill_info(minibelady_env_t *env, ir_node *state) { - spill_info_t *spill_info = obstack_alloc(&env->obst, sizeof(spill_info[0])); - memset(spill_info, 0, sizeof(spill_info[0])); + spill_info_t *spill_info = OALLOCZ(&env->obst, spill_info_t); spill_info->value = state; spill_info->reloads = NEW_ARR_F(ir_node*, 0); @@ -116,8 +109,7 @@ spill_info_t *create_spill_info(minibelady_env_t *env, ir_node *state) return spill_info; } -static INLINE -spill_info_t *get_spill_info(minibelady_env_t *env, const ir_node *node) +static inline spill_info_t *get_spill_info(minibelady_env_t *env, const ir_node *node) { spill_info_t *spill_info = (spill_info_t*) ir_nodemap_get(&env->spill_infos, node); @@ -125,26 +117,25 @@ spill_info_t *get_spill_info(minibelady_env_t *env, const ir_node *node) return spill_info; } -static -spill_info_t *create_spill(minibelady_env_t *env, ir_node *state, int force) +static spill_info_t *create_spill(minibelady_env_t *env, ir_node *state, int force) { spill_info_t *spill_info; ir_node *next; ir_node *after; spill_info = get_spill_info(env, state); - if(spill_info == NULL) { + if (spill_info == NULL) { spill_info = create_spill_info(env, state); - } else if(spill_info->spill != NULL) { + } else if (spill_info->spill != NULL) { return spill_info; } - if(sched_is_scheduled(state)) { + if (sched_is_scheduled(state)) { next = state; do { after = next; next = sched_next(after); - } while(is_Proj(next) || is_Phi(next) || be_is_Keep(next)); + } while (is_Proj(next) || is_Phi(next) || be_is_Keep(next)); } else { after = state; } @@ -153,9 +144,8 @@ spill_info_t *create_spill(minibelady_env_t *env, ir_node *state, int force) return spill_info; } -static -void create_reload(minibelady_env_t *env, ir_node *state, ir_node *before, - ir_node *last_state) +static void create_reload(minibelady_env_t *env, ir_node *state, + ir_node *before, ir_node *last_state) { spill_info_t *spill_info = create_spill(env, state, 0); ir_node *spill = spill_info->spill; @@ -166,60 +156,60 @@ void create_reload(minibelady_env_t *env, ir_node *state, ir_node *before, ARR_APP1(ir_node*, spill_info->reloads, reload); } -static -void spill_phi(minibelady_env_t *env, ir_node *phi) +static void spill_phi(minibelady_env_t *env, ir_node *phi) { - ir_graph *irg = get_irn_irg(phi); - ir_node *block = get_nodes_block(phi); - int i, arity = get_irn_arity(phi); - ir_node **in = alloca(arity * sizeof(in[0])); - ir_node *spill_to_kill = NULL; + ir_graph *irg = get_irn_irg(phi); + ir_node *block = get_nodes_block(phi); + int arity = get_irn_arity(phi); + ir_node **in = ALLOCAN(ir_node*, arity); + ir_node *dummy = new_r_Dummy(irg, mode_M); + ir_node *spill_to_kill = NULL; spill_info_t *spill_info; + int i; /* does a spill exist for the phis value? */ spill_info = get_spill_info(env, phi); - if(spill_info != NULL) { + if (spill_info != NULL) { spill_to_kill = spill_info->spill; } else { spill_info = create_spill_info(env, phi); } /* create a new phi-M with bad preds */ - for(i = 0; i < arity; ++i) { - in[i] = new_r_Unknown(irg, mode_M); + for (i = 0; i < arity; ++i) { + in[i] = dummy; } DBG((dbg, LEVEL_2, "\tcreate Phi-M for %+F\n", phi)); /* create a Phi-M */ - spill_info->spill = new_r_Phi(irg, block, arity, in, mode_M); + spill_info->spill = be_new_Phi(block, arity, in, mode_M, NULL); + sched_add_after(block, spill_info->spill); - if(spill_to_kill != NULL) { + if (spill_to_kill != NULL) { exchange(spill_to_kill, spill_info->spill); sched_remove(spill_to_kill); } /* create spills for the phi values */ - for(i = 0; i < arity; ++i) { + for (i = 0; i < arity; ++i) { ir_node *in = get_irn_n(phi, i); spill_info_t *pred_spill = create_spill(env, in, 1); set_irn_n(spill_info->spill, i, pred_spill->spill); } } -static -void belady(minibelady_env_t *env, ir_node *block); +static void belady(minibelady_env_t *env, ir_node *block); /** * Collects all values live-in at block @p block and all phi results in this * block. * Then it adds the best values (at most n_regs) to the blocks start_workset. - * The phis among the remaining values get spilled: Introduce psudo-copies of + * The phis among the remaining values get spilled: Introduce pseudo-copies of * their args to break interference and make it possible to spill them to the * same spill slot. */ -static -block_info_t *compute_block_start_state(minibelady_env_t *env, ir_node *block) +static block_info_t *compute_block_start_state(minibelady_env_t *env, ir_node *block) { block_info_t *block_info; be_next_use_t next_use; @@ -236,13 +226,13 @@ block_info_t *compute_block_start_state(minibelady_env_t *env, ir_node *block) n_cfgpreds = get_Block_n_cfgpreds(block); /* no cfgpred -> no value active */ - if(n_cfgpreds == 0) { + if (n_cfgpreds == 0) { block_info->start_state = NULL; return block_info; } /* for 1 pred only: simply take the the end-state of the pred */ - if(n_cfgpreds == 1) { + if (n_cfgpreds == 1) { ir_node *pred_block = get_Block_cfgpred_block(block, 0); block_info_t *pred_info; @@ -268,23 +258,23 @@ block_info_t *compute_block_start_state(minibelady_env_t *env, ir_node *block) sched_foreach(block, node) { if (!is_Phi(node)) break; - if (arch_get_irn_register(env->arch_env, node) != env->reg) + if (arch_get_irn_register(node) != env->reg) continue; DBG((dbg, LEVEL_2, "\t...checking %+F\n", node)); - next_use = be_get_next_use(env->uses, first, 0, node, 0); + next_use = be_get_next_use(env->uses, first, node, 0); - if(USES_IS_INFINITE(next_use.time)) { + if (USES_IS_INFINITE(next_use.time)) { DBG((dbg, LEVEL_2, "\tnot taken (dead)\n")); continue; } - if(next_use.outermost_loop >= get_loop_depth(loop)) { - if(outer_loop_allowed || next_use.time < best_time) { + if (next_use.outermost_loop >= get_loop_depth(loop)) { + if (outer_loop_allowed || next_use.time < best_time) { DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time, next_use.outermost_loop)); - if(best_starter != NULL) { + if (best_starter != NULL) { /* spill the phi as it is not used */ spill_phi(env, best_starter); } @@ -293,10 +283,10 @@ block_info_t *compute_block_start_state(minibelady_env_t *env, ir_node *block) outer_loop_allowed = 0; } } else { - if(outer_loop_allowed && next_use.time < best_time) { + if (outer_loop_allowed && next_use.time < best_time) { DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time, next_use.outermost_loop)); - if(best_starter != NULL) { + if (best_starter != NULL) { /* spill the phi as it is not used */ spill_phi(env, best_starter); } @@ -305,7 +295,7 @@ block_info_t *compute_block_start_state(minibelady_env_t *env, ir_node *block) } } - if(best_starter != node) { + if (best_starter != node) { /* spill the phi as it is not used */ spill_phi(env, best_starter); } @@ -315,26 +305,26 @@ block_info_t *compute_block_start_state(minibelady_env_t *env, ir_node *block) be_lv_foreach(env->lv, block, be_lv_state_in, i) { node = be_lv_get_irn(env->lv, block, i); - if(!mode_is_data(get_irn_mode(node))) + if (!mode_is_data(get_irn_mode(node))) continue; - if (arch_get_irn_register(env->arch_env, node) != env->reg) + if (arch_get_irn_register(node) != env->reg) continue; DBG((dbg, LEVEL_2, "\t...checking %+F\n", node)); - next_use = be_get_next_use(env->uses, first, 0, node, 0); + next_use = be_get_next_use(env->uses, first, node, 0); - if(USES_IS_INFINITE(next_use.time)) { + if (USES_IS_INFINITE(next_use.time)) { DBG((dbg, LEVEL_2, "\tnot taken (dead)\n")); continue; } - if(next_use.outermost_loop >= get_loop_depth(loop)) { - if(outer_loop_allowed || next_use.time < best_time) { + if (next_use.outermost_loop >= get_loop_depth(loop)) { + if (outer_loop_allowed || next_use.time < best_time) { DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time, next_use.outermost_loop)); - if(best_starter != NULL && is_Phi(best_starter)) { + if (best_starter != NULL && is_Phi(best_starter)) { /* spill the phi as it is not used */ spill_phi(env, best_starter); } @@ -343,10 +333,10 @@ block_info_t *compute_block_start_state(minibelady_env_t *env, ir_node *block) outer_loop_allowed = 0; } } else { - if(outer_loop_allowed && next_use.time < best_time) { + if (outer_loop_allowed && next_use.time < best_time) { DBG((dbg, LEVEL_2, "\ttaken (%u, loop %d)\n", next_use.time, next_use.outermost_loop)); - if(best_starter != NULL && is_Phi(best_starter)) { + if (best_starter != NULL && is_Phi(best_starter)) { /* spill the phi as it is not used */ spill_phi(env, best_starter); } @@ -366,15 +356,14 @@ block_info_t *compute_block_start_state(minibelady_env_t *env, ir_node *block) * whether it is used from a register or is reloaded * before the use. */ -static -void belady(minibelady_env_t *env, ir_node *block) +static void belady(minibelady_env_t *env, ir_node *block) { ir_node *current_state; ir_node *node; block_info_t *block_info; /* Don't do a block twice */ - if(irn_visited(block)) + if (irn_visited(block)) return; /* compute value to start with */ @@ -402,22 +391,22 @@ void belady(minibelady_env_t *env, ir_node *block) /* check which state is desired for the node */ arity = get_irn_arity(node); - for(i = 0; i < arity; ++i) { + for (i = 0; i < arity; ++i) { const arch_register_t *reg; ir_node *in = get_irn_n(node, i); - if(!mode_is_data(get_irn_mode(in))) + if (!mode_is_data(get_irn_mode(in))) continue; - reg = arch_get_irn_register(env->arch_env, in); - if(reg == env->reg) { + reg = arch_get_irn_register(in); + if (reg == env->reg) { assert(need_val == NULL); need_val = in; DBG((dbg, LEVEL_3, "\t... need state %+F\n", need_val)); } } /* create a reload to match state if necessary */ - if(need_val != NULL && need_val != current_state) { + if (need_val != NULL && need_val != current_state) { DBG((dbg, LEVEL_3, "\t... reloading %+F\n", need_val)); create_reload(env, need_val, node, current_state); current_state = need_val; @@ -433,20 +422,19 @@ void belady(minibelady_env_t *env, ir_node *block) const arch_register_t *reg; ir_node *proj = get_edge_src_irn(edge); - if(!mode_is_data(get_irn_mode(proj))) + if (!mode_is_data(get_irn_mode(proj))) continue; - reg = arch_get_irn_register(env->arch_env, proj); - if(reg == env->reg) { + reg = arch_get_irn_register(proj); + if (reg == env->reg) { current_state = proj; DBG((dbg, LEVEL_3, "\t... current_state <- %+F\n", current_state)); } } } else { - if(mode_is_data(get_irn_mode(node))) { - const arch_register_t *reg = - arch_get_irn_register(env->arch_env, node); - if(reg == env->reg) { + if (mode_is_data(get_irn_mode(node))) { + const arch_register_t *reg = arch_get_irn_register(node); + if (reg == env->reg) { current_state = node; DBG((dbg, LEVEL_3, "\t... current_state <- %+F\n", current_state)); } @@ -459,26 +447,24 @@ void belady(minibelady_env_t *env, ir_node *block) DBG((dbg, LEVEL_3, "End value for %+F: %+F\n", block, current_state)); } -static -void belady_walker(ir_node *block, void *data) +static void belady_walker(ir_node *block, void *data) { belady((minibelady_env_t*) data, block); } -static -ir_node *get_end_of_block_insertion_point(ir_node *block) +static ir_node *get_end_of_block_insertion_point(ir_node *block) { ir_node *last = sched_last(block); /* skip Projs and Keep-alikes behind the jump... */ - while(is_Proj(last) || be_is_Keep(last)) { + while (is_Proj(last) || be_is_Keep(last)) { last = sched_prev(last); } - if(!is_cfop(last)) { + if (!is_cfop(last)) { last = sched_next(last); /* last node must be a cfop, only exception is the start block */ - assert(last == get_irg_start_block(get_irn_irg(block))); + assert(last == get_irg_start_block(get_irn_irg(block))); } return last; @@ -487,16 +473,16 @@ ir_node *get_end_of_block_insertion_point(ir_node *block) /** * We must adapt the live-outs to the live-ins at each block-border. */ -static -void fix_block_borders(ir_node *block, void *data) { - minibelady_env_t *env = data; +static void fix_block_borders(ir_node *block, void *data) +{ + minibelady_env_t *env = (minibelady_env_t*)data; ir_graph *irg = get_irn_irg(block); ir_node *startblock = get_irg_start_block(irg); int i; int arity; block_info_t *block_info; - if(block == startblock) + if (block == startblock) return; DBG((dbg, LEVEL_3, "\n")); @@ -513,18 +499,18 @@ void fix_block_borders(ir_node *block, void *data) { block_info_t *pred_info = get_block_info(pred); ir_node *need_state = block_info->start_state; - if(need_state == NULL) + if (need_state == NULL) continue; - if(is_Phi(need_state) && get_nodes_block(need_state) == block) { + if (is_Phi(need_state) && get_nodes_block(need_state) == block) { need_state = get_irn_n(need_state, i); } DBG((dbg, LEVEL_3, " Pred %+F (ends in %+F, we need %+F)\n", pred, pred_info->end_state, need_state)); - if(pred_info->end_state != need_state) { - ir_node *insert_point = get_end_of_block_insertion_point(pred); + if (pred_info->end_state != need_state) { + ir_node *insert_point = get_end_of_block_insertion_point(pred); DBG((dbg, LEVEL_3, " Creating reload for %+F\n", need_state)); @@ -533,35 +519,32 @@ void fix_block_borders(ir_node *block, void *data) { } } -void be_assure_state(be_irg_t *birg, const arch_register_t *reg, void *func_env, +void be_assure_state(ir_graph *irg, const arch_register_t *reg, void *func_env, create_spill_func create_spill, - create_reload_func create_reload) { + create_reload_func create_reload) +{ minibelady_env_t env; - ir_graph *irg = be_get_birg_irg(birg); spill_info_t *info; - be_lv_t *lv = be_assure_liveness(birg); + be_lv_t *lv = be_assure_liveness(irg); be_liveness_assure_sets(lv); - be_assure_dom_front(birg); /* construct control flow loop tree */ - if(! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) { + if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) { construct_cf_backedges(irg); } obstack_init(&env.obst); - env.arch_env = be_get_birg_arch_env(birg); env.reg = reg; env.func_env = func_env; env.create_spill = create_spill; env.create_reload = create_reload; - env.lv = be_get_birg_liveness(birg); + env.lv = be_get_irg_liveness(irg); env.uses = be_begin_uses(irg, env.lv); env.spills = NULL; ir_nodemap_init(&env.spill_infos); assure_doms(irg); - set_using_visited(irg); - set_using_irn_link(irg); + ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED | IR_RESOURCE_IRN_LINK); inc_irg_visited(irg); /* process blocks */ @@ -570,29 +553,28 @@ void be_assure_state(be_irg_t *birg, const arch_register_t *reg, void *func_env, /* fix block end_states that don't match the next blocks start_state */ irg_block_walk_graph(irg, fix_block_borders, NULL, &env); - clear_using_visited(irg); - clear_using_irn_link(irg); + ir_free_resources(irg, IR_RESOURCE_IRN_VISITED | IR_RESOURCE_IRN_LINK); /* reconstruct ssa-form */ info = env.spills; - while(info != NULL) { + while (info != NULL) { be_ssa_construction_env_t senv; - int i, len; + size_t i, len; ir_node **phis; - be_ssa_construction_init(&senv, birg); - if(sched_is_scheduled(info->value)) + be_ssa_construction_init(&senv, irg); + if (sched_is_scheduled(info->value)) be_ssa_construction_add_copy(&senv, info->value); be_ssa_construction_add_copies(&senv, info->reloads, ARR_LEN(info->reloads)); be_ssa_construction_fix_users(&senv, info->value); - if(lv != NULL) { + if (lv != NULL) { be_ssa_construction_update_liveness_phis(&senv, lv); be_liveness_update(lv, info->value); len = ARR_LEN(info->reloads); - for(i = 0; i < len; ++i) { + for (i = 0; i < len; ++i) { ir_node *reload = info->reloads[i]; be_liveness_update(lv, reload); } @@ -602,10 +584,9 @@ void be_assure_state(be_irg_t *birg, const arch_register_t *reg, void *func_env, /* set register requirements for phis */ len = ARR_LEN(phis); - for(i = 0; i < len; ++i) { + for (i = 0; i < len; ++i) { ir_node *phi = phis[i]; - be_set_phi_flags(env.arch_env, phi, arch_irn_flags_ignore); - arch_set_irn_register(env.arch_env, phi, env.reg); + arch_set_irn_register(phi, env.reg); } be_ssa_construction_destroy(&senv); @@ -613,16 +594,15 @@ void be_assure_state(be_irg_t *birg, const arch_register_t *reg, void *func_env, } /* some nodes might be dead now. */ - be_remove_dead_nodes_from_schedule(birg); + be_remove_dead_nodes_from_schedule(irg); ir_nodemap_destroy(&env.spill_infos); be_end_uses(env.uses); obstack_free(&env.obst, NULL); } +BE_REGISTER_MODULE_CONSTRUCTOR(be_init_state); void be_init_state(void) { FIRM_DBG_REGISTER(dbg, "firm.be.state"); } - -BE_REGISTER_MODULE_CONSTRUCTOR(be_init_state);