X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fbe%2Fbespillmorgan.c;h=1ebce396b9f9bc0caae5f2ae9e5c23b78c78031c;hb=8cb8fe7d9d69a9bd06365823d5d5f68d051f6db0;hp=815dd09a6fc50b7c74f93da8ef1b38aa642a35a9;hpb=18ccb2b773fc0e6e0c5aa8688926524ab749951f;p=libfirm diff --git a/ir/be/bespillmorgan.c b/ir/be/bespillmorgan.c index 815dd09a6..1ebce396b 100644 --- a/ir/be/bespillmorgan.c +++ b/ir/be/bespillmorgan.c @@ -1,43 +1,65 @@ /* - * Author: Matthias Braun - * Date: 05.05.2006 - * Copyright: (c) Universitaet Karlsruhe - * License: This file is protected by GPL - GNU GENERAL PUBLIC LICENSE. + * Copyright (C) 1995-2007 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 Morgans spill algorithm. + * @author Matthias Braun + * @date 05.05.2006 + * @version $Id$ */ #ifdef HAVE_CONFIG_H #include "config.h" #endif -#include "bespillmorgan.h" - -#include "bechordal_t.h" -#include "bespill.h" -#include "belive_t.h" -#include "beabi.h" #include "irgwalk.h" -#include "besched.h" -#include "beutil.h" #include "irloop_t.h" #include "irgraph_t.h" #include "irprintf.h" -#include "obstack.h" +#include "obst.h" +#include "bespillmorgan.h" +#include "bechordal_t.h" +#include "bespill.h" +#include "belive_t.h" +#include "beabi.h" #include "bespillbelady.h" #include "beverify.h" #include "benodesets.h" +#include "bespilloptions.h" +#include "besched.h" +#include "beutil.h" +#include "bemodule.h" +#include "beirg_t.h" #define DBG_LIVE 1 #define DBG_LOOPANA 2 #define DBG_PRESSURE 4 #define DBG_SPILLS 8 +#define DBG_CHOOSE 16 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;) typedef struct morgan_env { - const be_chordal_env_t *cenv; const arch_env_t *arch; const arch_register_class_t *cls; ir_graph *irg; + const be_lv_t *lv; struct obstack obst; /** maximum safe register pressure */ int registers_available; @@ -61,7 +83,7 @@ typedef struct loop_attr { bitset_t *livethrough_unused; } loop_attr_t; -typedef struct block_attr { +typedef struct morgan_block_attr { const ir_node *block; /** set of all values that are live in the block but not used in the block */ bitset_t *livethrough_unused; @@ -134,7 +156,7 @@ static INLINE block_attr_t *get_block_attr(morgan_env_t *env, const ir_node *blo res = set_find(env->block_attr_set, &b_attr, sizeof(b_attr), hash); if(res == NULL) { - b_attr.livethrough_unused = bitset_obstack_alloc(&env->obst, get_irg_last_idx(env->irg)); + b_attr.livethrough_unused = NULL; res = set_insert(env->block_attr_set, &b_attr, sizeof(b_attr), hash); } @@ -151,10 +173,10 @@ static INLINE int consider_for_spilling(const arch_env_t *env, const arch_regist } /** - * Determine edges going out of a loop (= edges that go to a block that is not inside - * the loop or one of its subloops) + * Determine edges going out of a loop (= edges that go to a block that is not + * inside the loop or one of its subloops) */ -static INLINE void construct_loop_edges(ir_node* block, void* data) { +static INLINE void construct_loop_edges(ir_node *block, void *data) { morgan_env_t *env = data; int n_cfgpreds = get_Block_n_cfgpreds(block); int i; @@ -236,25 +258,54 @@ static void show_nodebitset(ir_graph* irg, const bitset_t* bitset) { } #endif -/** - * Construct the livethrough unused set for a block - */ -static bitset_t *construct_block_livethrough_unused(morgan_env_t* env, const ir_node* block) { - block_attr_t *block_attr = get_block_attr(env, block); - ir_node *node; +static INLINE void init_livethrough_unuseds(block_attr_t *attr, morgan_env_t *env) { + const ir_node *block; int i; + const be_lv_t *lv = env->lv; + + if(attr->livethrough_unused != NULL) + return; + + block = attr->block; + + attr->livethrough_unused = bitset_obstack_alloc(&env->obst, get_irg_last_idx(env->irg)); - DBG((dbg, DBG_LIVE, "Processing block %d\n", get_irn_node_nr(block))); // copy all live-outs into the livethrough_unused set - be_lv_foreach(env->cenv->lv, block, be_lv_state_in | be_lv_state_out, i) { - ir_node *irn = be_lv_get_irn(env->cenv->lv, block, i); + be_lv_foreach(lv, block, be_lv_state_in | be_lv_state_out, i) { + ir_node *irn = be_lv_get_irn(lv, block, i); int node_idx; if(!consider_for_spilling(env->arch, env->cls, irn)) continue; node_idx = get_irn_idx(irn); - bitset_set(block_attr->livethrough_unused, node_idx); + bitset_set(attr->livethrough_unused, node_idx); + } +} + +/** + * Construct the livethrough unused set for a block + */ +static void construct_block_livethrough_unused(ir_node *block, void *data) { + morgan_env_t* env = data; + block_attr_t *block_attr = get_block_attr(env, block); + ir_node *node; + int n_cfgpreds; + block_attr_t **pred_attrs = NULL; + int i; + + init_livethrough_unuseds(block_attr, env); + + DBG((dbg, DBG_LIVE, "Processing block %d\n", get_irn_node_nr(block))); + + n_cfgpreds = get_Block_n_cfgpreds(block); + if(n_cfgpreds > 1) { + pred_attrs = alloca(sizeof(pred_attrs[0]) * n_cfgpreds); + for(i = 0; i < n_cfgpreds; ++i) { + ir_node *pred_block = get_Block_cfgpred_block(block, i); + pred_attrs[i] = get_block_attr(env, pred_block); + init_livethrough_unuseds(pred_attrs[i], env); + } } /* @@ -264,13 +315,24 @@ static bitset_t *construct_block_livethrough_unused(morgan_env_t* env, const ir_ sched_foreach(block, node) { int i, arity; - for(i = 0, arity = get_irn_arity(node); i < arity; ++i) { - int idx = get_irn_idx(get_irn_n(node, i)); - bitset_clear(block_attr->livethrough_unused, idx); + // phis are really uses in the pred block + if(is_Phi(node)) { + int j; + for(j = 0; j < n_cfgpreds; ++j) { + ir_node *used_value = get_Phi_pred(node, j); + int idx = get_irn_idx(used_value); + block_attr_t *pred_attr = pred_attrs[j]; + + bitset_clear(pred_attr->livethrough_unused, idx); + } + } else { + // mark all used values as used + for(i = 0, arity = get_irn_arity(node); i < arity; ++i) { + int idx = get_irn_idx(get_irn_n(node, i)); + bitset_clear(block_attr->livethrough_unused, idx); + } } } - - return block_attr->livethrough_unused; } /** @@ -286,9 +348,13 @@ static bitset_t *construct_loop_livethrough_unused(morgan_env_t *env, const ir_l loop_element elem = get_loop_element(loop, i); switch (*elem.kind) { case k_ir_node: { - bitset_t *livethrough_block_unused; + ir_node *block = elem.node; + block_attr_t *block_attr = get_block_attr(env, block); + bitset_t *livethrough_block_unused = block_attr->livethrough_unused; + assert(is_Block(elem.node)); - livethrough_block_unused = construct_block_livethrough_unused(env, elem.node); + assert(livethrough_block_unused != NULL); + if(i == 0) { bitset_copy(loop_attr->livethrough_unused, livethrough_block_unused); } else { @@ -338,13 +404,68 @@ static bitset_t *construct_loop_livethrough_unused(morgan_env_t *env, const ir_l /*---------------------------------------------------------------------------*/ +typedef struct _spillcandidate_t { + ir_node *node; + int cost; +} spillcandidate_t; + +static int compare_spillcandidates(const void *d1, const void *d2) { + const spillcandidate_t *cand1 = d1; + const spillcandidate_t *cand2 = d2; + + return cand1->cost - cand2->cost; +} + +static void spill_values(morgan_env_t *env, const loop_attr_t *loop_attr, int spills) { + const bitset_t *cand_bitset = loop_attr->livethrough_unused; + int candidatecount = bitset_popcnt(cand_bitset); + spillcandidate_t *candidates; + int i, c; + loop_edge_t *edge; + + assert(spills <= candidatecount); + + candidates = alloca(sizeof(candidates[0]) * candidatecount); + + DBG((dbg, DBG_CHOOSE, "Candidates for loop %d\n", get_loop_loop_nr(loop_attr->loop))); + // build candidiatelist + c = 0; + bitset_foreach(cand_bitset, i) { + ir_node *node = get_idx_irn(env->irg, i); + candidates[c].node = node; + candidates[c].cost = 0; + + for(edge = set_first(loop_attr->out_edges); edge != NULL; edge = set_next(loop_attr->out_edges)) { + candidates[c].cost += be_get_reload_costs_on_edge(env->senv, node, edge->block, edge->pos); + } + DBG((dbg, DBG_CHOOSE, "%+F has costs %d\n", node, candidates[c].cost)); + + c++; + } + assert(c == candidatecount); + + // sort list + qsort(candidates, candidatecount, sizeof(candidates[0]), compare_spillcandidates); + + // spill values + for(i = 0; i < spills; ++i) { + ir_node *to_spill = candidates[i].node; + DBG((dbg, DBG_CHOOSE, "Spilling %+F ", to_spill)); + + for(edge = set_first(loop_attr->out_edges); edge != NULL; edge = set_next(loop_attr->out_edges)) { + be_add_reload_on_edge(env->senv, to_spill, edge->block, edge->pos, env->cls, 1); + } + } +} + static int reduce_register_pressure_in_block(morgan_env_t *env, const ir_node* block, int loop_unused_spills_possible) { ir_node *node; int max_pressure; int loop_unused_spills_needed; pset *live_nodes = pset_new_ptr_default(); + const be_lv_t *lv = env->lv; - be_liveness_end_of_block(env->cenv->lv, env->arch, env->cls, block, live_nodes); + be_liveness_end_of_block(lv, env->arch, env->cls, block, live_nodes); max_pressure = pset_count(live_nodes); DBG((dbg, DBG_LIVE, "Reduce pressure to %d In Block %+F:\n", env->registers_available, block)); @@ -365,8 +486,6 @@ static int reduce_register_pressure_in_block(morgan_env_t *env, const ir_node* b } del_pset(live_nodes); - DBG((dbg, DBG_PRESSURE, "\tMax Pressure in %+F: %d\n", block, max_pressure)); - loop_unused_spills_needed = max_pressure - env->registers_available; if(loop_unused_spills_needed < 0) { @@ -375,7 +494,8 @@ static int reduce_register_pressure_in_block(morgan_env_t *env, const ir_node* b loop_unused_spills_needed = loop_unused_spills_possible; } - DBG((dbg, DBG_PRESSURE, "Unused spills for Block %+F needed: %d\n", block, loop_unused_spills_needed)); + DBG((dbg, DBG_PRESSURE, "Block %+F: max-pressure %d spills possible: %d spills used: %d\n", + block, max_pressure, loop_unused_spills_possible, loop_unused_spills_needed)); return loop_unused_spills_needed; } @@ -421,8 +541,7 @@ static int reduce_register_pressure_in_loop(morgan_env_t *env, const ir_loop *lo } /* calculate number of spills needed in outer loop and spill - * unused livethrough nodes around this loop - */ + * unused livethrough nodes around this loop */ if(spills_needed > outer_spills_possible) { int spills_to_place; outer_spills_needed = outer_spills_possible; @@ -433,21 +552,7 @@ static int reduce_register_pressure_in_loop(morgan_env_t *env, const ir_loop *lo DBG((dbg, DBG_SPILLS, "%d values unused in loop %d, spilling %d\n", spills_possible - outer_spills_possible, loop->loop_nr, spills_to_place)); - bitset_foreach(loop_attr->livethrough_unused, i) { - loop_edge_t *edge; - ir_node *to_spill = get_idx_irn(env->irg, i); - - DBG((dbg, DBG_SPILLS, "Spilling node %+F around loop %d\n", to_spill, loop->loop_nr)); - - for(edge = set_first(loop_attr->out_edges); edge != NULL; edge = set_next(loop_attr->out_edges)) { - be_add_reload_on_edge(env->senv, to_spill, edge->block, edge->pos); - } - - spills_to_place--; - if(spills_to_place <= 0) { - break; - } - } + spill_values(env, loop_attr, spills_to_place); } else { outer_spills_needed = spills_needed; } @@ -455,64 +560,74 @@ static int reduce_register_pressure_in_loop(morgan_env_t *env, const ir_loop *lo return outer_spills_needed; } -void be_spill_morgan(be_chordal_env_t *chordal_env) { +void be_spill_morgan(be_irg_t *birg, const arch_register_class_t *cls) { + ir_graph *irg = be_get_birg_irg(birg); morgan_env_t env; - FIRM_DBG_REGISTER(dbg, "ir.be.spillmorgan"); - //firm_dbg_set_mask(dbg, DBG_SPILLS | DBG_LOOPANA); + be_assure_liveness(birg); - env.cenv = chordal_env; - env.arch = chordal_env->birg->main_env->arch_env; - env.irg = chordal_env->irg; - env.cls = chordal_env->cls; - env.senv = be_new_spill_env(chordal_env); + env.arch = birg->main_env->arch_env; + env.irg = irg; + env.cls = cls; + env.lv = be_get_birg_liveness(birg); + env.senv = be_new_spill_env(birg); DEBUG_ONLY(be_set_spill_env_dbg_module(env.senv, dbg);) obstack_init(&env.obst); - env.registers_available = env.cls->n_regs - be_put_ignore_regs(chordal_env->birg, env.cls, NULL); + env.registers_available = env.cls->n_regs - be_put_ignore_regs(birg, env.cls, NULL); - env.loop_attr_set = new_set(loop_attr_cmp, 5); + env.loop_attr_set = new_set(loop_attr_cmp, 5); env.block_attr_set = new_set(block_attr_cmp, 20); - /*-- Part1: Analysis --*/ - be_liveness_recompute(chordal_env->lv); + /* -- Part1: Analysis -- */ /* construct control flow loop tree */ - construct_cf_backedges(chordal_env->irg); + if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) { + construct_cf_backedges(irg); + } /* construct loop out edges and livethrough_unused sets for loops and blocks */ - irg_block_walk_graph(chordal_env->irg, NULL, construct_loop_edges, &env); - construct_loop_livethrough_unused(&env, get_irg_loop(env.irg)); + irg_block_walk_graph(irg, construct_block_livethrough_unused, construct_loop_edges, &env); + construct_loop_livethrough_unused(&env, get_irg_loop(irg)); - /*-- Part2: Transformation --*/ + /* -- Part2: Transformation -- */ /* spill unused livethrough values around loops and blocks where * the pressure is too high */ - reduce_register_pressure_in_loop(&env, get_irg_loop(env.irg), 0); + reduce_register_pressure_in_loop(&env, get_irg_loop(irg), 0); /* Insert real spill/reload nodes and fix usages */ be_insert_spills_reloads(env.senv); /* Verify the result */ - if (chordal_env->opts->vrfy_option == BE_CH_VRFY_WARN) { - be_verify_schedule(env.irg); - } else if (chordal_env->opts->vrfy_option == BE_CH_VRFY_ASSERT) { - assert(be_verify_schedule(env.irg)); + if (birg->main_env->options->vrfy_option == BE_VRFY_WARN) { + be_verify_schedule(birg); + } else if (birg->main_env->options->vrfy_option == BE_VRFY_ASSERT) { + assert(be_verify_schedule(birg)); } - if (chordal_env->opts->dump_flags & BE_CH_DUMP_SPILL) - be_dump(env.irg, "-spillmorgan", dump_ir_block_graph_sched); - /* cleanup */ free_loop_edges(&env); del_set(env.loop_attr_set); del_set(env.block_attr_set); /* fix the remaining places with too high register pressure with beladies algorithm */ - be_spill_belady_spill_env(chordal_env, env.senv); + be_spill_belady_spill_env(birg, cls, env.senv); be_delete_spill_env(env.senv); obstack_free(&env.obst, NULL); } + +void be_init_spillmorgan(void) +{ + static be_spiller_t morgan_spiller = { + be_spill_morgan + }; + + be_register_spiller("morgan", &morgan_spiller); + FIRM_DBG_REGISTER(dbg, "ir.be.spillmorgan"); +} + +BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillmorgan);