X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fbe%2Fbestate.c;h=0daebb2fff4204d6f0b715a7013bf9070f11ac7b;hb=5728d0c2eae6d6f026d1a5435f9f0e4fa67382a2;hp=0c93320d2ca27e2def7ab9a54e34859bebe8b1e2;hpb=863d31d7a5c8210432fef88b30fc3e8353131538;p=libfirm diff --git a/ir/be/bestate.c b/ir/be/bestate.c index 0c93320d2..0daebb2ff 100644 --- a/ir/be/bestate.c +++ b/ir/be/bestate.c @@ -1,3 +1,22 @@ +/* + * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved. + * + * This file is part of libFirm. + * + * This file may be distributed and/or modified under the terms of the + * GNU General Public License version 2 as published by the Free Software + * Foundation and appearing in the file LICENSE.GPL included in the + * packaging of this file. + * + * Licensees holding valid libFirm Professional Edition licenses may use + * this file in accordance with the libFirm Commercial License. + * Agreement provided with the Software. + * + * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE + * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE. + */ + /** * @file * @brief Handles state switching. This is basically the belady spill @@ -5,12 +24,8 @@ * @author Matthias Braun * @date 26.03.2007 * @version $Id$ - * Copyright: (c) Universitaet Karlsruhe - * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE. */ -#ifdef HAVE_CONFIG_H #include "config.h" -#endif #include "bestate.h" @@ -22,14 +37,17 @@ #include "iredges_t.h" #include "ircons_t.h" #include "irgmod.h" +#include "irnodeset.h" +#include "irnodemap.h" +#include "adt/cpset.h" #include "bearch.h" -#include "beuses_t.h" -#include "besched_t.h" +#include "beuses.h" +#include "besched.h" #include "belive_t.h" #include "bemodule.h" -#include "benode_t.h" -#include "beirgmod.h" +#include "benode.h" +#include "bessaconstr.h" DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;) @@ -42,13 +60,13 @@ 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; create_reload_func create_reload; create_spill_func create_spill; spill_info_t *spills; + ir_nodemap_t spill_infos; be_uses_t *uses; /**< env for the next-use magic */ } minibelady_env_t; @@ -58,35 +76,34 @@ typedef struct block_info_t { ir_node *end_state; } block_info_t; -static INLINE +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); mark_irn_visited(block); return res; } -static INLINE +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 *value) +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->value = value; + spill_info_t *spill_info = OALLOCZ(&env->obst, spill_info_t); + spill_info->value = state; spill_info->reloads = NEW_ARR_F(ir_node*, 0); - set_irn_link(value, spill_info); - mark_irn_visited(value); + ir_nodemap_insert(&env->spill_infos, state, spill_info); + //ir_fprintf(stderr, "Insert %+F -> %p\n", state, spill_info); spill_info->next = env->spills; env->spills = spill_info; @@ -94,48 +111,70 @@ spill_info_t *create_spill_info(minibelady_env_t *env, ir_node *value) return spill_info; } +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); + //ir_fprintf(stderr, "Get %+F -> %p\n", node, spill_info); + return spill_info; +} + static -spill_info_t *create_spill(minibelady_env_t *env, ir_node *value, int force) +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) { + spill_info = create_spill_info(env, state); + } else if(spill_info->spill != NULL) { + return spill_info; + } - if(irn_visited(value)) { - spill_info = (spill_info_t*) get_irn_link(value); - if(spill_info->spill != NULL || !force) - return spill_info; + 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)); } else { - spill_info = create_spill_info(env, value); + after = state; } - - spill_info->spill = env->create_spill(env->func_env, value, force); + spill_info->spill = env->create_spill(env->func_env, state, force, after); return spill_info; } static -void create_reload(minibelady_env_t *env, ir_node *value, ir_node *before) +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, value, 0); + spill_info_t *spill_info = create_spill(env, state, 0); ir_node *spill = spill_info->spill; ir_node *reload; - reload = env->create_reload(env->func_env, value, spill, before); + reload = env->create_reload(env->func_env, state, spill, before, + last_state); ARR_APP1(ir_node*, spill_info->reloads, reload); } 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 *spill_to_kill = NULL; spill_info_t *spill_info; + int i; /* does a spill exist for the phis value? */ - if(irn_visited(phi)) { - spill_info = (spill_info_t*) get_irn_link(phi); + spill_info = get_spill_info(env, phi); + if(spill_info != NULL) { spill_to_kill = spill_info->spill; } else { spill_info = create_spill_info(env, phi); @@ -143,13 +182,13 @@ void spill_phi(minibelady_env_t *env, ir_node *phi) /* create a new phi-M with bad preds */ for(i = 0; i < arity; ++i) { - in[i] = new_r_Bad(irg); + in[i] = new_r_Unknown(irg, mode_M); } 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 = new_r_Phi(block, arity, in, mode_M); if(spill_to_kill != NULL) { exchange(spill_to_kill, spill_info->spill); @@ -225,7 +264,7 @@ 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)); @@ -272,7 +311,10 @@ 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 (arch_get_irn_register(env->arch_env, node) != env->reg) + if(!mode_is_data(get_irn_mode(node))) + continue; + + if (arch_get_irn_register(node) != env->reg) continue; DBG((dbg, LEVEL_2, "\t...checking %+F\n", node)); @@ -310,8 +352,6 @@ block_info_t *compute_block_start_state(minibelady_env_t *env, ir_node *block) } } - /* TODO: spill phis */ - block_info->start_state = best_starter; return block_info; @@ -359,9 +399,13 @@ 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) { + const arch_register_t *reg; ir_node *in = get_irn_n(node, i); - const arch_register_t *reg = - arch_get_irn_register(env->arch_env, in); + + if(!mode_is_data(get_irn_mode(in))) + continue; + + reg = arch_get_irn_register(in); if(reg == env->reg) { assert(need_val == NULL); need_val = in; @@ -370,29 +414,37 @@ void belady(minibelady_env_t *env, ir_node *block) } /* create a reload to match state if necessary */ if(need_val != NULL && need_val != current_state) { - create_reload(env, need_val, node); + DBG((dbg, LEVEL_3, "\t... reloading %+F\n", need_val)); + create_reload(env, need_val, node, current_state); + current_state = need_val; } DBG((dbg, LEVEL_3, " ...%+F\n", node)); /* record state changes by the node */ if (get_irn_mode(node) == mode_T) { - ir_node *proj; - for(proj = sched_next(node); is_Proj(proj); - proj = sched_next(proj)) { - const arch_register_t *reg = - arch_get_irn_register(env->arch_env, proj); + const ir_edge_t *edge; + + foreach_out_edge(node, edge) { + const arch_register_t *reg; + ir_node *proj = get_edge_src_irn(edge); + + if(!mode_is_data(get_irn_mode(proj))) + continue; + + 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 { - const arch_register_t *reg = - arch_get_irn_register(env->arch_env, node); - if(reg == env->reg) { - current_state = node; - DBG((dbg, LEVEL_3, "\t... current_state <- %+F\n", current_state)); + 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)); + } } } } @@ -413,14 +465,14 @@ ir_node *get_end_of_block_insertion_point(ir_node *block) { ir_node *last = sched_last(block); - /* skip projs and keepanies behind the jump... */ + /* skip Projs and Keep-alikes behind the jump... */ while(is_Proj(last) || be_is_Keep(last)) { last = sched_prev(last); } if(!is_cfop(last)) { last = sched_next(last); - // last node must be a cfop, only exception is the start block + /* last node must be a cfop, only exception is the start block */ assert(last == get_irg_start_block(get_irn_irg(block))); } @@ -443,25 +495,35 @@ void fix_block_borders(ir_node *block, void *data) { return; DBG((dbg, LEVEL_3, "\n")); - DBG((dbg, LEVEL_3, "Fixing %+F\n", block)); block_info = get_block_info(block); + DBG((dbg, LEVEL_3, "Fixing %+F (needs %+F)\n", block, + block_info->start_state)); + /* process all pred blocks */ arity = get_irn_arity(block); for (i = 0; i < arity; ++i) { - ir_node *pred = get_Block_cfgpred_block(block, i); - block_info_t *pred_info = get_block_info(pred); + ir_node *pred = get_Block_cfgpred_block(block, i); + block_info_t *pred_info = get_block_info(pred); + ir_node *need_state = block_info->start_state; - DBG((dbg, LEVEL_3, " Pred %+F\n", pred)); + if(need_state == NULL) + continue; - if(pred_info->end_state != block_info->start_state && - block_info->start_state != NULL) { - ir_node *need_state = block_info->start_state; - ir_node *insert_point = - get_end_of_block_insertion_point(pred); + if(is_Phi(need_state) && get_nodes_block(need_state) == block) { + need_state = get_irn_n(need_state, i); + } - create_reload(env, need_state, insert_point); + 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); + + + DBG((dbg, LEVEL_3, " Creating reload for %+F\n", need_state)); + create_reload(env, need_state, insert_point, pred_info->end_state); } } } @@ -472,16 +534,15 @@ void be_assure_state(be_irg_t *birg, const arch_register_t *reg, void *func_env, 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_assure_liveness(birg); - be_assure_dom_front(birg); + be_liveness_assure_sets(lv); /* construct control flow loop tree */ if(! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) { construct_cf_backedges(irg); } obstack_init(&env.obst); - env.arch_env = birg->main_env->arch_env; env.reg = reg; env.func_env = func_env; env.create_spill = create_spill; @@ -489,9 +550,10 @@ void be_assure_state(be_irg_t *birg, const arch_register_t *reg, void *func_env, env.lv = be_get_birg_liveness(birg); env.uses = be_begin_uses(irg, env.lv); env.spills = NULL; + ir_nodemap_init(&env.spill_infos); - set_using_visited(irg); - set_using_irn_link(irg); + assure_doms(irg); + ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED | IR_RESOURCE_IRN_LINK); inc_irg_visited(irg); /* process blocks */ @@ -500,35 +562,50 @@ 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) { + be_ssa_construction_env_t senv; int i, len; ir_node **phis; - phis = be_ssa_construction(be_get_birg_dom_front(birg), - be_get_birg_liveness(birg), - info->value, - ARR_LEN(info->reloads), info->reloads, - NULL, 1); + + be_ssa_construction_init(&senv, birg); + 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) { + 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) { + ir_node *reload = info->reloads[i]; + be_liveness_update(lv, reload); + } + } + + phis = be_ssa_construction_get_new_phis(&senv); /* set register requirements for phis */ len = ARR_LEN(phis); 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); } - DEL_ARR_F(phis); + be_ssa_construction_destroy(&senv); info = info->next; } /* some nodes might be dead now. */ - be_remove_dead_nodes_from_schedule(irg); + be_remove_dead_nodes_from_schedule(birg); + ir_nodemap_destroy(&env.spill_infos); be_end_uses(env.uses); obstack_free(&env.obst, NULL); }