X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fbe%2Fbespillremat.c;h=2d418f6374c3c7478fec1b7d9f2b95c430bf0787;hb=2669742071b00949c6a5102f39b7df7fd7d3e3fb;hp=2d536f6cd7b803e51d44bb7b218628517bce9c11;hpb=c05c098e0503b0abebc60700fe0d4d7d06200f79;p=libfirm diff --git a/ir/be/bespillremat.c b/ir/be/bespillremat.c index 2d536f6cd..2d418f637 100644 --- a/ir/be/bespillremat.c +++ b/ir/be/bespillremat.c @@ -1,12 +1,28 @@ -/** vim: set sw=4 ts=4: - * @file bespillremat.c - * @date 2006-04-06 - * @author Adam M. Szalkowski & Sebastian Hack +/* + * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved. * - * ILP based spilling & rematerialization + * This file is part of libFirm. * - * Copyright (C) 2006 Universitaet Karlsruhe - * Released under the GPL + * 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 ILP based spilling & rematerialization + * @author Adam M. Szalkowski + * @date 06.04.2006 + * @version $Id$ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -29,39 +45,44 @@ #include "irnode_t.h" #include "ircons_t.h" #include "irloop_t.h" -#include "phiclass_t.h" -#include "iredges.h" +#include "irnodeset.h" +#include "phiclass.h" +#include "iredges_t.h" #include "execfreq.h" #include "irvrfy.h" +#include "irbackedge_t.h" +#include "irprofile.h" #include #include #include #include -//#include -#include #include "be_t.h" +#include "beirg_t.h" #include "belive_t.h" #include "besched_t.h" -#include "beirgmod.h" -#include "bearch.h" +#include "bessaconstr.h" +#include "bearch_t.h" +#include "beintlive_t.h" +#include "beabi.h" #include "benode_t.h" #include "beutil.h" #include "bespillremat.h" #include "bespill.h" #include "bepressurestat.h" - +#include "bespilloptions.h" #include "bechordal_t.h" +#include "bemodule.h" -#ifdef WITH_LIBCORE -#include -#include -#endif /* WITH_LIBCORE */ +#include "lc_opts.h" +#include "lc_opts_enum.h" #define DUMP_PROBLEM 1 #define DUMP_MPS 2 #define DUMP_SOLUTION 4 +#define DUMP_STATS 8 +#define DUMP_PRESSURE 16 #define KEEPALIVE_REMATS 1 #define KEEPALIVE_SPILLS 2 @@ -75,29 +96,30 @@ #define REMATS_NOINVERSE 2 #define REMATS_ALL 3 -static int opt_dump_flags = 0; +static unsigned opt_dump_flags = 0; static int opt_log = 0; -static int opt_keep_alive = 0; +static unsigned opt_keep_alive = 0; static int opt_goodwin = 1; static int opt_memcopies = 1; static int opt_memoperands = 1; static int opt_verify = VERIFY_MEMINTERF; -static int opt_remats = REMATS_ALL; +static unsigned opt_remats = REMATS_ALL; static int opt_repair_schedule = 0; static int opt_no_enlarge_liveness = 0; static int opt_remat_while_live = 1; static int opt_timeout = 300; static double opt_cost_reload = 8.0; static double opt_cost_memoperand = 7.0; -static double opt_cost_spill = 50.0; +static double opt_cost_spill = 15.0; static double opt_cost_remat = 1.0; -#ifdef WITH_LIBCORE static const lc_opt_enum_mask_items_t dump_items[] = { { "problem", DUMP_PROBLEM }, { "mps", DUMP_MPS }, { "solution", DUMP_SOLUTION }, + { "stats", DUMP_STATS }, + { "pressure", DUMP_PRESSURE }, { NULL, 0 } }; @@ -120,7 +142,7 @@ static const lc_opt_enum_mask_items_t remats_items[] = { { "none", REMATS_NONE }, { "briggs", REMATS_BRIGGS }, { "noinverse", REMATS_NOINVERSE }, - { "ALL", REMATS_ALL }, + { "all", REMATS_ALL }, { NULL, 0 } }; @@ -129,17 +151,17 @@ static lc_opt_enum_mask_var_t remats_var = { }; static const lc_opt_table_entry_t options[] = { - LC_OPT_ENT_ENUM_MASK("keepalive", "keep alive remats, spills or reloads", &keep_alive_var), + LC_OPT_ENT_ENUM_MASK("keepalive", "keep alive inserted nodes", &keep_alive_var), LC_OPT_ENT_BOOL ("goodwin", "activate goodwin reduction", &opt_goodwin), LC_OPT_ENT_BOOL ("memcopies", "activate memcopy handling", &opt_memcopies), LC_OPT_ENT_BOOL ("memoperands", "activate memoperands", &opt_memoperands), - LC_OPT_ENT_ENUM_INT ("remats", "type of remats to insert (none, briggs, noinverse or all)",&remats_var), + LC_OPT_ENT_ENUM_INT ("remats", "type of remats to insert", &remats_var), LC_OPT_ENT_BOOL ("repair_schedule", "repair the schedule by rematting once used nodes",&opt_repair_schedule), LC_OPT_ENT_BOOL ("no_enlage_liveness", "do not enlarge liveness of operands of remats",&opt_no_enlarge_liveness), - LC_OPT_ENT_BOOL ("remat_while_live", "remat only values that can be used by real ops", &opt_remat_while_live), + LC_OPT_ENT_BOOL ("remat_while_live", "only remat where rematted value was live", &opt_remat_while_live), - LC_OPT_ENT_ENUM_MASK("dump", "dump ifg before, after or after each cloud", &dump_var), + LC_OPT_ENT_ENUM_MASK("dump", "dump problem, solution or statistical data", &dump_var), LC_OPT_ENT_BOOL ("log", "activate the lpp log", &opt_log), LC_OPT_ENT_INT ("timeout", "ILP solver timeout", &opt_timeout), @@ -147,18 +169,11 @@ static const lc_opt_table_entry_t options[] = { LC_OPT_ENT_DBL ("cost_memoperand", "cost of a memory operand", &opt_cost_memoperand), LC_OPT_ENT_DBL ("cost_spill", "cost of a spill instruction", &opt_cost_spill), LC_OPT_ENT_DBL ("cost_remat", "cost of a rematerialization", &opt_cost_remat), - { NULL } + LC_OPT_LAST }; -void be_spill_remat_register_options(lc_opt_entry_t *grp) -{ - lc_opt_entry_t *my_grp = lc_opt_get_grp(grp, "remat"); - lc_opt_add_table(my_grp, options); -} -#endif - - -//#define EXECFREQ_LOOPDEPH /* compute execution frequency from loop depth only */ +//#define EXECFREQ_LOOPDEPH /* compute execution frequency from loop depth only */ +//#define SCHEDULE_PHIM /* insert phim nodes into schedule */ #define SOLVE //#define SOLVE_LOCAL @@ -166,13 +181,13 @@ void be_spill_remat_register_options(lc_opt_entry_t *grp) #define LPP_SOLVER "cplex" -#define MAX_PATHS 16 +#define MAX_PATHS INT_MAX #define ILP_UNDEF -1 typedef struct _spill_ilp_t { const arch_register_class_t *cls; int n_regs; - const be_chordal_env_t *chordal_env; + be_irg_t *birg; be_lv_t *lv; lpp_t *lpp; struct obstack *obst; @@ -185,6 +200,10 @@ typedef struct _spill_ilp_t { set *interferences; ir_node *m_unknown; set *memoperands; + phi_classes_t *pc; +#ifndef SCHEDULE_PHIM + pset *phims; +#endif DEBUG_ONLY(firm_dbg_module_t * dbg); } spill_ilp_t; @@ -263,7 +282,8 @@ typedef struct _memoperand_t { static INLINE int has_reg_class(const spill_ilp_t * si, const ir_node * irn) { - return chordal_has_class(si->chordal_env, irn); + return arch_irn_consider_in_reg_alloc(si->birg->main_env->arch_env, + si->cls, irn); } #if 0 @@ -284,7 +304,7 @@ static int cmp_remat(const void *a, const void *b) { const remat_t *r = a; - const remat_t *s = a; + const remat_t *s = b; return !(r == s || r->op == s->op); } @@ -294,6 +314,7 @@ cmp_spill(const void *a, const void *b, size_t size) { const spill_t *p = a; const spill_t *q = b; + (void) size; // return !(p->irn == q->irn && p->bb == q->bb); return !(p->irn == q->irn); @@ -304,6 +325,7 @@ cmp_memoperands(const void *a, const void *b, size_t size) { const memoperand_t *p = a; const memoperand_t *q = b; + (void) size; return !(p->irn == q->irn && p->pos == q->pos); } @@ -389,6 +411,7 @@ cmp_remat_info(const void *a, const void *b, size_t size) { const remat_info_t *p = a; const remat_info_t *q = b; + (void) size; return !(p->irn == q->irn); } @@ -398,6 +421,7 @@ cmp_defs(const void *a, const void *b, size_t size) { const defs_t *p = a; const defs_t *q = b; + (void) size; return !(p->value == q->value); } @@ -407,6 +431,7 @@ cmp_keyval(const void *a, const void *b, size_t size) { const keyval_t *p = a; const keyval_t *q = b; + (void) size; return !(p->key == q->key); } @@ -415,8 +440,11 @@ static double execution_frequency(const spill_ilp_t *si, const ir_node * irn) { #define FUDGE 0.001 + if(ir_profile_has_data()) + return ((double)ir_profile_get_block_execcount(get_block_const(irn))) + FUDGE; + #ifndef EXECFREQ_LOOPDEPH - return get_block_execfreq(si->chordal_env->exec_freq, get_block(irn)) + FUDGE; + return get_block_execfreq(si->birg->exec_freq, get_block_const(irn)) + FUDGE; #else if(is_Block(irn)) return exp(get_loop_depth(get_irn_loop(irn)) * log(10)) + FUDGE; @@ -433,7 +461,7 @@ get_cost(const spill_ilp_t * si, const ir_node * irn) } else if(be_is_Reload(irn)){ return opt_cost_reload; } else { - return arch_get_op_estimated_cost(si->chordal_env->birg->main_env->arch_env, irn); + return arch_get_op_estimated_cost(si->birg->main_env->arch_env, irn); } } @@ -444,7 +472,7 @@ static INLINE int is_rematerializable(const spill_ilp_t * si, const ir_node * irn) { int n; - const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env; + const arch_env_t *arch_env = si->birg->main_env->arch_env; int remat = (arch_irn_get_flags(arch_env, irn) & arch_irn_flags_rematerializable) != 0; #if 0 @@ -478,18 +506,18 @@ get_remat_from_op(spill_ilp_t * si, const ir_node * dest_value, const ir_node * const ir_node *proj = NULL; if(is_Proj(dest_value)) { - op = get_irn_n(op, 0); + op = get_Proj_pred(op); proj = dest_value; } if(!is_rematerializable(si, op)) return NULL; - remat = obstack_alloc(si->obst, sizeof(*remat)); - remat->op = op; - remat->cost = get_cost(si, op); - remat->value = dest_value; - remat->proj = proj; + remat = obstack_alloc(si->obst, sizeof(*remat)); + remat->op = op; + remat->cost = (int)get_cost(si, op); + remat->value = dest_value; + remat->proj = proj; remat->inverse = 0; } else { arch_inverse_t inverse; @@ -506,7 +534,7 @@ get_remat_from_op(spill_ilp_t * si, const ir_node * dest_value, const ir_node * DBG((si->dbg, LEVEL_5, "\t requesting inverse op for argument %d of op %+F\n", n, op)); /* else ask the backend to give an inverse op */ - if(arch_get_inverse(si->chordal_env->birg->main_env->arch_env, op, n, &inverse, si->obst)) { + if(arch_get_inverse(si->birg->main_env->arch_env, op, n, &inverse, si->obst)) { int i; DBG((si->dbg, LEVEL_4, "\t backend gave us an inverse op with %d nodes and cost %d\n", inverse.n, inverse.costs)); @@ -525,7 +553,9 @@ get_remat_from_op(spill_ilp_t * si, const ir_node * dest_value, const ir_node * remat->proj = (inverse.n==2)?inverse.nodes[1]:NULL; remat->inverse = 1; - assert(is_Proj(remat->proj)); + // Matze: commented this out, this doesn't seem to be true if + // the inverse is a simple operation with only 1 result... + //assert(is_Proj(remat->proj)); } else { assert(0 && "I can not handle remats with more than 2 nodes"); } @@ -599,10 +629,15 @@ static int get_irn_n_nonignore_args(const spill_ilp_t * si, const ir_node * irn) { int n; - unsigned int ret = 0; + int ret = 0; + + if(is_Proj(irn)) + irn = get_Proj_pred(irn); for(n=get_irn_arity(irn)-1; n>=0; --n) { - if(has_reg_class(si, irn)) ++ret; + const ir_node *arg = get_irn_n(irn, n); + + if(has_reg_class(si, arg)) ++ret; } return ret; @@ -647,6 +682,7 @@ value_is_defined_before(const spill_ilp_t * si, const ir_node * pos, const ir_no ir_node *block; ir_node *def_block = get_nodes_block(val); int ret; + (void) si; if(val == pos) return 0; @@ -676,7 +712,7 @@ value_is_defined_before(const spill_ilp_t * si, const ir_node * pos, const ir_no static INLINE ir_node * sched_block_last_noncf(const spill_ilp_t * si, const ir_node * bb) { - return sched_skip((ir_node*)bb, 0, sched_skip_cf_predicator, (void *) si->chordal_env->birg->main_env->arch_env); + return sched_skip((ir_node*)bb, 0, sched_skip_cf_predicator, (void *) si->birg->main_env->arch_env); } /** @@ -691,6 +727,7 @@ sched_block_first_nonphi(const ir_node * bb) static int sched_skip_proj_predicator(const ir_node * irn, void * data) { + (void) data; return (is_Proj(irn)); } @@ -738,6 +775,7 @@ sched_put_after(ir_node * insert, ir_node * irn) } else { insert = sched_next_op(insert); } + sched_reset(irn); sched_add_before(insert, irn); } @@ -750,9 +788,59 @@ sched_put_before(const spill_ilp_t * si, ir_node * insert, ir_node * irn) insert = sched_next_nonproj(insert, 0); insert = sched_prev(insert); } + sched_reset(irn); sched_add_after(insert, irn); } +static ir_node * +next_post_remat(const ir_node * irn) +{ + op_t *op; + ir_node *next; + + if(is_Block(irn)) { + next = sched_block_first_nonphi(irn); + } else { + next = sched_next_op(irn); + } + + if(sched_is_end(next)) + return NULL; + + op = get_irn_link(next); + if(op->is_remat && !op->attr.remat.pre) { + return next; + } + + return NULL; +} + + +static ir_node * +next_pre_remat(const spill_ilp_t * si, const ir_node * irn) +{ + op_t *op; + ir_node *ret; + + if(is_Block(irn)) { + ret = sched_block_last_noncf(si, irn); + ret = sched_next(ret); + ret = sched_prev_op(ret); + } else { + ret = sched_prev_op(irn); + } + + if(sched_is_end(ret) || is_Phi(ret)) + return NULL; + + op = (op_t*)get_irn_link(ret); + if(op->is_remat && op->attr.remat.pre) { + return ret; + } + + return NULL; +} + /** * Tells you whether a @p remat can be placed before the irn @p pos */ @@ -790,7 +878,7 @@ can_remat_before(const spill_ilp_t * si, const remat_t * remat, const ir_node * if(opt_no_enlarge_liveness) { if(has_reg_class(si, arg) && live) { - res &= pset_find_ptr(live, arg)?1:0; + res &= pset_find_ptr((pset*)live, arg)?1:0; } else { res &= value_is_defined_before(si, pos, arg); } @@ -848,7 +936,7 @@ insert_copy_before(const spill_ilp_t * si, const ir_node * irn, ir_node * pos) bb = is_Block(pos)?pos:get_nodes_block(pos); copy = exact_copy(irn); - _set_phi_class(copy, NULL); + set_phi_class(si->pc, copy, NULL); set_nodes_block(copy, bb); sched_put_before(si, pos, copy); @@ -867,7 +955,7 @@ insert_copy_after(const spill_ilp_t * si, const ir_node * irn, ir_node * pos) bb = is_Block(pos)?pos:get_nodes_block(pos); copy = exact_copy(irn); - _set_phi_class(copy, NULL); + set_phi_class(si->pc, copy, NULL); set_nodes_block(copy, bb); sched_put_after(pos, copy); @@ -884,7 +972,7 @@ insert_remat_after(spill_ilp_t * si, const remat_t * remat, ir_node * pos, const *proj_copy; op_t *op; - DBG((si->dbg, LEVEL_3, "\t >inserting remat %+F\n", remat->op)); + DBG((si->dbg, LEVEL_3, "\t >inserting remat2 %+F\n", remat->op)); copy = insert_copy_after(si, remat->op, pos); @@ -964,9 +1052,18 @@ get_block_n_succs(const ir_node *block) { return edge ? 2 : 1; } +static int +is_start_block(const ir_node * bb) +{ + return get_irg_start_block(get_irn_irg(bb)) == bb; +} + static int is_merge_edge(const ir_node * bb) { + if(is_start_block(bb)) + return 0; + if(opt_goodwin) return get_block_n_succs(bb) == 1; else @@ -976,12 +1073,67 @@ is_merge_edge(const ir_node * bb) static int is_diverge_edge(const ir_node * bb) { + if(is_start_block(bb)) + return 0; + if(opt_goodwin) return get_Block_n_cfgpreds(bb) == 1; else return 1; } +static void +get_live_end(spill_ilp_t * si, ir_node * bb, pset * live) +{ + ir_node *irn; + int i; + + be_lv_foreach(si->lv, bb, be_lv_state_end, i) { + irn = be_lv_get_irn(si->lv, bb, i); + + if (has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) { + pset_insert_ptr(live, irn); + } + } + + irn = sched_last(bb); + + /* all values eaten by control flow operations are also live until the end of the block */ + sched_foreach_reverse(bb, irn) { + int i; + + if(!sched_skip_cf_predicator(irn, si->birg->main_env->arch_env)) break; + + for(i=get_irn_arity(irn)-1; i>=0; --i) { + ir_node *arg = get_irn_n(irn,i); + + if(has_reg_class(si, arg)) { + pset_insert_ptr(live, arg); + } + } + } + /* + * find values that are used by remats at end of block + * and insert them into live set + */ + foreach_pre_remat(si, bb, irn) { + int n; + + for (n=get_irn_arity(irn)-1; n>=0; --n) { + ir_node *remat_arg = get_irn_n(irn, n); + + if(!has_reg_class(si, remat_arg)) continue; + + /* if value is becoming live through use by remat */ + if(!pset_find_ptr(live, remat_arg)) { + DBG((si->dbg, LEVEL_4, " value %+F becoming live through use by remat at end of block %+F\n", remat_arg, irn)); + + pset_insert_ptr(live, remat_arg); + } + } + } +} + static void walker_regclass_copy_insertor(ir_node * irn, void * data) { @@ -995,7 +1147,7 @@ walker_regclass_copy_insertor(ir_node * irn, void * data) ir_node *bb = get_Block_cfgpred_block(get_nodes_block(irn), n); if(!has_reg_class(si, phi_arg)) { - ir_node *copy = be_new_Copy(si->cls, si->chordal_env->irg, bb, phi_arg); + ir_node *copy = be_new_Copy(si->cls, si->birg->irg, bb, phi_arg); ir_node *pos = sched_block_last_noncf(si, bb); op_t *op = obstack_alloc(si->obst, sizeof(*op)); @@ -1012,7 +1164,6 @@ walker_regclass_copy_insertor(ir_node * irn, void * data) } } - /** * Insert (so far unused) remats into the irg to * recompute the potential liveness of all values @@ -1021,13 +1172,18 @@ static void walker_remat_insertor(ir_node * bb, void * data) { spill_ilp_t *si = data; - spill_bb_t *spill_bb; ir_node *irn; int n, i; - pset *live = pset_new_ptr_default(); + pset *live; + pset *post_remats; + remat_t *remat; + + /* skip start block, no remats to do there */ + if(is_start_block(bb)) return; DBG((si->dbg, LEVEL_3, "\t Entering %+F\n\n", bb)); + live = pset_new_ptr_default(); be_lv_foreach(si->lv, bb, be_lv_state_end, i) { ir_node *value = be_lv_get_irn(si->lv, bb, i); @@ -1037,76 +1193,45 @@ walker_remat_insertor(ir_node * bb, void * data) } } - spill_bb = obstack_alloc(si->obst, sizeof(*spill_bb)); - set_irn_link(bb, spill_bb); - irn = sched_last(bb); while(!sched_is_end(irn)) { ir_node *next; - op_t *op; pset *args; ir_node *arg; - pset *remat_args; + pset *used; next = sched_prev(irn); - DBG((si->dbg, LEVEL_5, "\t at %+F (next: %+F)\n", irn, next)); + /* delete defined value from live set */ + if(has_reg_class(si, irn)) { + pset_remove_ptr(live, irn); + } if(is_Phi(irn) || is_Proj(irn)) { - op_t *op; - - if(has_reg_class(si, irn)) { - pset_remove_ptr(live, irn); - } - - op = obstack_alloc(si->obst, sizeof(*op)); - op->is_remat = 0; - op->attr.live_range.args.reloads = NULL; - op->attr.live_range.ilp = ILP_UNDEF; - set_irn_link(irn, op); - irn = next; continue; } - op = obstack_alloc(si->obst, sizeof(*op)); - op->is_remat = 0; - op->attr.live_range.ilp = ILP_UNDEF; - op->attr.live_range.args.reloads = obstack_alloc(si->obst, sizeof(*op->attr.live_range.args.reloads) * get_irn_arity(irn)); - memset(op->attr.live_range.args.reloads, 0xFF, sizeof(*op->attr.live_range.args.reloads) * get_irn_arity(irn)); - set_irn_link(irn, op); - args = pset_new_ptr_default(); + used = pset_new_ptr_default(); - /* collect arguments of op */ + /* collect arguments of op and set args of op already live in epilog */ for (n = get_irn_arity(irn)-1; n>=0; --n) { ir_node *arg = get_irn_n(irn, n); pset_insert_ptr(args, arg); - } - - /* set args of op already live in epilog */ - pset_foreach(args, arg) { if(has_reg_class(si, arg)) { pset_insert_ptr(live, arg); + pset_insert_ptr(used, arg); } } - /* delete defined value from live set */ - if(has_reg_class(si, irn)) { - pset_remove_ptr(live, irn); - } - - - remat_args = pset_new_ptr_default(); /* insert all possible remats before irn */ pset_foreach(args, arg) { remat_info_t *remat_info, query; - remat_t *remat; - /* continue if the operand has the wrong reg class - */ + /* continue if the operand has the wrong reg class */ if(!has_reg_class(si, arg)) continue; @@ -1124,41 +1249,34 @@ walker_remat_insertor(ir_node * bb, void * data) ir_node *remat_irn = NULL; DBG((si->dbg, LEVEL_4, "\t considering remat %+F for arg %+F\n", remat->op, arg)); - if(opt_remat_while_live) { - if(pset_find_ptr(live, remat->value)) { - remat_irn = insert_remat_before(si, remat, irn, live); - } - } else { - remat_irn = insert_remat_before(si, remat, irn, live); - } + remat_irn = insert_remat_before(si, remat, irn, live); + if(remat_irn) { for(n=get_irn_arity(remat_irn)-1; n>=0; --n) { ir_node *remat_arg = get_irn_n(remat_irn, n); - if(!has_reg_class(si, remat_arg)) continue; - - pset_insert_ptr(remat_args, remat_arg); + /* collect args of remats which are not args of op */ + if(has_reg_class(si, remat_arg) && !pset_find_ptr(args, remat_arg)) { + pset_insert_ptr(used, remat_arg); + } } } } } } - /* now we add remat args to op's args because they could also die at this op */ - pset_foreach(args,arg) { - if(pset_find_ptr(remat_args, arg)) { - pset_remove_ptr(remat_args, arg); - } - } - pset_foreach(remat_args,arg) { - pset_insert_ptr(args, arg); + /* do not place post remats after jumps */ + if(sched_skip_cf_predicator(irn, si->birg->main_env->arch_env)) { + del_pset(used); + del_pset(args); + break; } /* insert all possible remats after irn */ - pset_foreach(args, arg) { + post_remats = pset_new_ptr_default(); + pset_foreach(used, arg) { remat_info_t *remat_info, query; - remat_t *remat; /* continue if the operand has the wrong reg class */ if(!has_reg_class(si, arg)) @@ -1173,82 +1291,167 @@ walker_remat_insertor(ir_node * bb, void * data) continue; } - /* do not place post remats after jumps */ - if(sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) continue; - if(remat_info->remats_by_operand) { pset_foreach(remat_info->remats_by_operand, remat) { /* do not insert remats producing the same value as one of the operands */ if(!pset_find_ptr(args, remat->value)) { DBG((si->dbg, LEVEL_4, "\t considering remat %+F with arg %+F\n", remat->op, arg)); - if(opt_remat_while_live) { - if(pset_find_ptr(live, remat->value)) { - insert_remat_after(si, remat, irn, live); - } - } else { - insert_remat_after(si, remat, irn, live); + + /* only remat values that can be used by real ops */ + if(!opt_remat_while_live || pset_find_ptr(live, remat->value)) { + pset_insert_ptr(post_remats, remat); } } } } } + pset_foreach(post_remats, remat) { + insert_remat_after(si, remat, irn, live); + } + del_pset(post_remats); - del_pset(remat_args); + del_pset(used); del_pset(args); irn = next; } - be_lv_foreach(si->lv, bb, be_lv_state_end | be_lv_state_in, i) { - ir_node *value = be_lv_get_irn(si->lv, bb, i); + /* add remats at end if successor has multiple predecessors */ + if(is_merge_edge(bb)) { + pset *live_out = pset_new_ptr_default(); + ir_node *value; - /* add remats at end if successor has multiple predecessors */ - if(is_merge_edge(bb)) { - /* add remats at end of block */ - if (be_is_live_end(si->lv, bb, value) && has_reg_class(si, value)) { - remat_info_t *remat_info, - query; - remat_t *remat; - - query.irn = value; - query.remats = NULL; - query.remats_by_operand = NULL; - remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value)); - - if(remat_info && remat_info->remats) { - pset_foreach(remat_info->remats, remat) { - DBG((si->dbg, LEVEL_4, "\t considering remat %+F at end of block %+F\n", remat->op, bb)); - - insert_remat_before(si, remat, bb, NULL); - } + get_live_end(si, bb, live_out); + + /* add remats at end of block */ + pset_foreach(live_out, value) { + remat_info_t *remat_info, + query; + + query.irn = value; + query.remats = NULL; + query.remats_by_operand = NULL; + remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value)); + + if(remat_info && remat_info->remats) { + pset_foreach(remat_info->remats, remat) { + DBG((si->dbg, LEVEL_4, "\t considering remat %+F at end of block %+F\n", remat->op, bb)); + + insert_remat_before(si, remat, bb, live_out); } } } - if(is_diverge_edge(bb)) { - /* add remat2s at beginning of block */ - if ((be_is_live_in(si->lv, bb, value) || (is_Phi(value) && get_nodes_block(value)==bb)) && has_reg_class(si, value)) { - remat_info_t *remat_info, - query; - remat_t *remat; + del_pset(live_out); + } - query.irn = value; - query.remats = NULL; - query.remats_by_operand = NULL; - remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value)); + if(is_diverge_edge(bb)) { + pset *live_in = pset_new_ptr_default(); + ir_node *value; - if(remat_info && remat_info->remats) { - pset_foreach(remat_info->remats, remat) { - DBG((si->dbg, LEVEL_4, "\t considering remat %+F at beginning of block %+F\n", remat->op, bb)); + be_lv_foreach(si->lv, bb, be_lv_state_in, i) { + value = be_lv_get_irn(si->lv, bb, i); - /* put the remat here if all its args are available */ - insert_remat_after(si, remat, bb, NULL); + if(has_reg_class(si, value)) { + pset_insert_ptr(live_in, value); + } + } + /* add phis to live_in */ + sched_foreach(bb, value) { + if(!is_Phi(value)) break; + + if(has_reg_class(si, value)) { + pset_insert_ptr(live_in, value); + } + } + /* add remat2s at beginning of block */ + post_remats = pset_new_ptr_default(); + pset_foreach(live_in, value) { + remat_info_t *remat_info, + query; + + query.irn = value; + query.remats = NULL; + query.remats_by_operand = NULL; + remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value)); + + if(remat_info && remat_info->remats_by_operand) { + pset_foreach(remat_info->remats_by_operand, remat) { + DBG((si->dbg, LEVEL_4, "\t considering remat2 %+F at beginning of block %+F\n", remat->op, bb)); + + /* put the remat here if all its args are available and result is still live */ + if(!opt_remat_while_live || pset_find_ptr(live_in, remat->value)) { + pset_insert_ptr(post_remats, remat); } } } } + pset_foreach(post_remats, remat) { + insert_remat_after(si, remat, bb, live_in); + } + del_pset(post_remats); + del_pset(live_in); + } +} + +static int +can_be_copied(const ir_node * bb, const ir_node * irn) +{ + const ir_edge_t *edge = get_block_succ_first(bb); + const ir_node *next_bb = edge->src; + int pos = edge->pos; + const ir_node *phi; + + assert(is_merge_edge(bb)); + + sched_foreach(next_bb, phi) { + const ir_node *phi_arg; + + if(!is_Phi(phi)) break; + + phi_arg = get_irn_n(phi, pos); + + if(phi_arg == irn) { + return 1; + } } + return 0; } +/** + * Initialize additional node info + */ +static void +luke_initializer(ir_node * bb, void * data) +{ + spill_ilp_t *si = (spill_ilp_t*)data; + spill_bb_t *spill_bb; + ir_node *irn; + + spill_bb = obstack_alloc(si->obst, sizeof(*spill_bb)); + set_irn_link(bb, spill_bb); + + sched_foreach(bb, irn) { + op_t *op; + + op = obstack_alloc(si->obst, sizeof(*op)); + op->is_remat = 0; + op->attr.live_range.ilp = ILP_UNDEF; + if(is_Phi(irn)) { + if(opt_memcopies) { + op->attr.live_range.args.copies = obstack_alloc(si->obst, sizeof(*op->attr.live_range.args.copies) * get_irn_arity(irn)); + memset(op->attr.live_range.args.copies, 0xFF, sizeof(*op->attr.live_range.args.copies) * get_irn_arity(irn)); + } + } else if(!is_Proj(irn)) { + op->attr.live_range.args.reloads = obstack_alloc(si->obst, sizeof(*op->attr.live_range.args.reloads) * get_irn_arity(irn)); + memset(op->attr.live_range.args.reloads, 0xFF, sizeof(*op->attr.live_range.args.reloads) * get_irn_arity(irn)); + } else { + op->attr.live_range.args.reloads = NULL; + } + set_irn_link(irn, op); + } +} + + /** * Preparation of blocks' ends for Luke Blockwalker(tm)(R) */ @@ -1264,18 +1467,28 @@ luke_endwalker(ir_node * bb, void * data) spill_bb_t *spill_bb = get_irn_link(bb); int i; - live = pset_new_ptr_default(); use_end = pset_new_ptr_default(); be_lv_foreach(si->lv, bb, be_lv_state_end, i) { irn = be_lv_get_irn(si->lv, bb, i); if (has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) { - op_t *op; - pset_insert_ptr(live, irn); - op = get_irn_link(irn); - assert(!op->is_remat); + } + } + /* + * find values that are used by remats at end of block + * and insert them into live set + */ + foreach_pre_remat(si, bb, irn) { + int n; + + for (n=get_irn_arity(irn)-1; n>=0; --n) { + ir_node *remat_arg = get_irn_n(irn, n); + + if(has_reg_class(si, remat_arg)) { + pset_insert_ptr(live, remat_arg); + } } } @@ -1284,7 +1497,7 @@ luke_endwalker(ir_node * bb, void * data) sched_foreach_reverse(bb, irn) { int n; - if(!sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) break; + if(!sched_skip_cf_predicator(irn, si->birg->main_env->arch_env)) break; for (n=get_irn_arity(irn)-1; n>=0; --n) { ir_node *irn_arg = get_irn_n(irn, n); @@ -1342,7 +1555,7 @@ luke_endwalker(ir_node * bb, void * data) ilp_cst_t rel_cst; ir_snprintf(buf, sizeof(buf), "reload_%N_%N", bb, irn); - reload = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_reload*execution_frequency(si, bb), 1.0); + reload = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_reload*execution_frequency(si, bb), can_be_copied(bb, irn)); set_insert_keyval(spill_bb->reloads, irn, INT_TO_PTR(reload)); /* reload <= mem_out */ @@ -1371,8 +1584,6 @@ luke_endwalker(ir_node * bb, void * data) ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", irn, bb); spill->reg_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0); - /* if irn is used at the end of the block, then it is live anyway */ - //lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0); ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb); spill->mem_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0); @@ -1403,55 +1614,7 @@ luke_endwalker(ir_node * bb, void * data) del_pset(use_end); } -static ir_node * -next_post_remat(const ir_node * irn) -{ - op_t *op; - ir_node *next; - - if(is_Block(irn)) { - next = sched_block_first_nonphi(irn); - } else { - next = sched_next_op(irn); - } - - if(sched_is_end(next)) - return NULL; - - op = get_irn_link(next); - if(op->is_remat && !op->attr.remat.pre) { - return next; - } - - return NULL; -} - - -static ir_node * -next_pre_remat(const spill_ilp_t * si, const ir_node * irn) -{ - op_t *op; - ir_node *ret; - - if(is_Block(irn)) { - ret = sched_block_last_noncf(si, irn); - ret = sched_next(ret); - ret = sched_prev_op(ret); - } else { - ret = sched_prev_op(irn); - } - - if(sched_is_end(ret) || is_Phi(ret)) - return NULL; - - op = (op_t*)get_irn_link(ret); - if(op->is_remat && op->attr.remat.pre) { - return ret; - } - - return NULL; -} - +#ifndef NDEBUG /** * Find a remat of value @p value in the epilog of @p pos */ @@ -1479,6 +1642,7 @@ find_post_remat(const ir_node * value, const ir_node * pos) return NULL; } +#endif static spill_t * add_to_spill_bb(spill_ilp_t * si, ir_node * bb, ir_node * irn) @@ -1511,38 +1675,6 @@ add_to_spill_bb(spill_ilp_t * si, ir_node * bb, ir_node * irn) return spill; } -static void -get_live_end(spill_ilp_t * si, ir_node * bb, pset * live) -{ - ir_node *irn; - int i; - - be_lv_foreach(si->lv, bb, be_lv_state_end, i) { - irn = be_lv_get_irn(si->lv, bb, i); - - if (has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) { - pset_insert_ptr(live, irn); - } - } - - irn = sched_last(bb); - - /* all values eaten by control flow operations are also live until the end of the block */ - sched_foreach_reverse(bb, irn) { - int i; - - if(!sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) break; - - for(i=get_irn_arity(irn)-1; i>=0; --i) { - ir_node *arg = get_irn_n(irn,i); - - if(has_reg_class(si, arg)) { - pset_insert_ptr(live, arg); - } - } - } -} - /** * Inserts ILP-constraints and variables for memory copying before the given position */ @@ -1603,7 +1735,6 @@ insert_mem_copy_position(spill_ilp_t * si, pset * live, const ir_node * block) if(!has_reg_class(si, phi)) continue; to_copy = get_irn_n(phi, pos); - to_copy_op = get_irn_link(to_copy); to_copy_spill = set_find_spill(spill_bb->ilp, to_copy); @@ -1658,8 +1789,7 @@ luke_blockwalker(ir_node * bb, void * data) ir_node *tmp; spill_t *spill; pset *defs = pset_new_ptr_default(); - const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env; - + const arch_env_t *arch_env = si->birg->main_env->arch_env; live = pset_new_ptr_default(); @@ -1706,15 +1836,48 @@ luke_blockwalker(ir_node * bb, void * data) lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0); } } - /* maybe we should also assure that reg_out >= live_range etc. */ + ir_snprintf(buf, sizeof(buf), "reg_out2_%N_%N", bb, irn); + cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 0.0); + + /* value may only die at bb end if it is used for a mem copy */ + /* reg_out + \sum copy - reload - remat - live_range >= 0 */ + lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0); + if(reload != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, reload, -1.0); + lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -1.0); + foreach_pre_remat(si, bb, tmp) { + op_t *remat_op = get_irn_link(tmp); + if(remat_op->attr.remat.remat->value == irn) { + lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0); + } + } + if(is_merge_edge(bb)) { + const ir_edge_t *edge = get_block_succ_first(bb); + const ir_node *next_bb = edge->src; + int pos = edge->pos; + const ir_node *phi; + + sched_foreach(next_bb, phi) { + const ir_node *phi_arg; + + if(!is_Phi(phi)) break; + + phi_arg = get_irn_n(phi, pos); + + if(phi_arg == irn) { + op_t *phi_op = get_irn_link(phi); + ilp_var_t copy = phi_op->attr.live_range.args.copies[pos]; + + lpp_set_factor_fast(si->lpp, cst, copy, 1.0); + } + } + } } if(opt_memcopies) insert_mem_copy_position(si, live, bb); /* - * start new live ranges for values used by remats at end of block - * and assure the remat args are available + * assure the remat args are available */ foreach_pre_remat(si, bb, tmp) { op_t *remat_op = get_irn_link(tmp); @@ -1723,29 +1886,22 @@ luke_blockwalker(ir_node * bb, void * data) for (n=get_irn_arity(tmp)-1; n>=0; --n) { ir_node *remat_arg = get_irn_n(tmp, n); op_t *arg_op = get_irn_link(remat_arg); - ilp_var_t prev_lr; if(!has_reg_class(si, remat_arg)) continue; - /* if value is becoming live through use by remat */ - if(!pset_find_ptr(live, remat_arg)) { - ir_snprintf(buf, sizeof(buf), "lr_%N_end%N", remat_arg, bb); - prev_lr = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0); - - arg_op->attr.live_range.ilp = prev_lr; - arg_op->attr.live_range.op = bb; - - DBG((si->dbg, LEVEL_4, " value %+F becoming live through use by remat at end of block %+F\n", remat_arg, tmp)); + spill = set_find_spill(spill_bb->ilp, remat_arg); + assert(spill); - pset_insert_ptr(live, remat_arg); - add_to_spill_bb(si, bb, remat_arg); - } + /* arguments of remats have to be live until the very end of the block + * remat = reg_out(remat_arg) and (reload(remat_arg) or live_range(remat_arg)), + * no remats, they could be in wrong order + */ - /* remat <= live_rang(remat_arg) [ + reload(remat_arg) ] */ ir_snprintf(buf, sizeof(buf), "req_remat_%N_arg_%N", tmp, remat_arg); cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0); - lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0); + lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 3.0); + lpp_set_factor_fast(si->lpp, cst, spill->reg_out, -2.0); lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0); /* use reload placed for this argument */ @@ -1781,7 +1937,7 @@ luke_blockwalker(ir_node * bb, void * data) pset *used; pset *remat_defs; keyval_t *keyval; - ilp_cst_t one_memoperand; + ilp_cst_t one_memoperand = -1; /* iterate only until first phi */ if(is_Phi(irn)) @@ -1790,6 +1946,7 @@ luke_blockwalker(ir_node * bb, void * data) op = get_irn_link(irn); /* skip remats */ if(op->is_remat) continue; + DBG((si->dbg, LEVEL_4, "\t at node %+F\n", irn)); /* collect defined values */ @@ -1809,30 +1966,32 @@ luke_blockwalker(ir_node * bb, void * data) used = pset_new_ptr(pset_count(live) + get_irn_arity(irn)); remat_defs = pset_new_ptr(pset_count(live)); - for (n=get_irn_arity(irn)-1; n>=0; --n) { - ir_node *irn_arg = get_irn_n(irn, n); - if(has_reg_class(si, irn_arg)) { - set_insert_keyval(args, irn_arg, (void*)n); - pset_insert_ptr(used, irn_arg); + if(!is_start_block(bb) || !be_is_Barrier(irn)) { + for (n=get_irn_arity(irn)-1; n>=0; --n) { + ir_node *irn_arg = get_irn_n(irn, n); + if(has_reg_class(si, irn_arg)) { + set_insert_keyval(args, irn_arg, (void*)n); + pset_insert_ptr(used, irn_arg); + } } - } - foreach_post_remat(irn, tmp) { - op_t *remat_op = get_irn_link(tmp); + foreach_post_remat(irn, tmp) { + op_t *remat_op = get_irn_link(tmp); - pset_insert_ptr(remat_defs, remat_op->attr.remat.remat->value); + pset_insert_ptr(remat_defs, remat_op->attr.remat.remat->value); - for (n=get_irn_arity(tmp)-1; n>=0; --n) { - ir_node *remat_arg = get_irn_n(tmp, n); - if(has_reg_class(si, remat_arg)) { - pset_insert_ptr(used, remat_arg); + for (n=get_irn_arity(tmp)-1; n>=0; --n) { + ir_node *remat_arg = get_irn_n(tmp, n); + if(has_reg_class(si, remat_arg)) { + pset_insert_ptr(used, remat_arg); + } } } - } - foreach_pre_remat(si, irn, tmp) { - for (n=get_irn_arity(tmp)-1; n>=0; --n) { - ir_node *remat_arg = get_irn_n(tmp, n); - if(has_reg_class(si, remat_arg)) { - pset_insert_ptr(used, remat_arg); + foreach_pre_remat(si, irn, tmp) { + for (n=get_irn_arity(tmp)-1; n>=0; --n) { + ir_node *remat_arg = get_irn_n(tmp, n); + if(has_reg_class(si, remat_arg)) { + pset_insert_ptr(used, remat_arg); + } } } } @@ -1870,7 +2029,6 @@ luke_blockwalker(ir_node * bb, void * data) } } - // value_op->attr.live_range.ilp != ILP_UNDEF if(pset_find_ptr(live, value) && cst != ILP_UNDEF) { lpp_set_factor_fast(si->lpp, cst, value_op->attr.live_range.ilp, -n_remats); } @@ -2041,7 +2199,26 @@ skip_one_must_die: cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0); lpp_set_factor_fast(si->lpp, cst, post_use, -1.0); lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0); + } + + /* forall post remat which use arg add a similar cst */ + foreach_post_remat(irn, remat) { + int n; + + for (n=get_irn_arity(remat)-1; n>=0; --n) { + ir_node *remat_arg = get_irn_n(remat, n); + op_t *remat_op = get_irn_link(remat); + + if(remat_arg == arg) { + DBG((si->dbg, LEVEL_3, "\t found remat with arg %+F in epilog of %+F\n", arg, irn)); + /* post_use >= remat */ + ir_snprintf(buf, sizeof(buf), "post_use_%N_%N-%d", arg, irn, p++); + cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0); + lpp_set_factor_fast(si->lpp, cst, post_use, -1.0); + lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0); + } + } } /* if value is not an arg of op and not possibly defined by post remat @@ -2063,27 +2240,7 @@ skip_one_must_die: } } - - /* forall post remat which use arg add a similar cst */ - foreach_post_remat(irn, remat) { - int n; - - for (n=get_irn_arity(remat)-1; n>=0; --n) { - ir_node *remat_arg = get_irn_n(remat, n); - op_t *remat_op = get_irn_link(remat); - - if(remat_arg == arg) { - DBG((si->dbg, LEVEL_3, "\t found remat with arg %+F in epilog of %+F\n", arg, irn)); - - ir_snprintf(buf, sizeof(buf), "post_use_%N_%N-%d", arg, irn, p++); - cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0); - lpp_set_factor_fast(si->lpp, cst, post_use, -1.0); - lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0); - } - } - } - - if(opt_memoperands) { + if(opt_memoperands && (!is_start_block(bb) || be_is_Barrier(irn))) { for(n = get_irn_arity(irn)-1; n>=0; --n) { if(get_irn_n(irn, n) == arg && arch_possible_memory_operand(arch_env, irn, n)) { ilp_var_t memoperand; @@ -2097,8 +2254,6 @@ skip_one_must_die: lpp_set_factor_fast(si->lpp, cst, memoperand, 1.0); lpp_set_factor_fast(si->lpp, cst, post_use, 1.0); -// if(arg_op->attr.live_range.ilp != ILP_UNDEF) -// lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0); } } } @@ -2107,18 +2262,71 @@ skip_one_must_die: arg_op->attr.live_range.ilp = prev_lr; arg_op->attr.live_range.op = irn; - /*if(!pset_find_ptr(live, arg)) { - pset_insert_ptr(live, arg); - add_to_spill_bb(si, bb, arg); - }*/ pset_insert_ptr(live, arg); - } /* just to be sure */ check_post = ILP_UNDEF; - + /* allow original defintions to be removed */ + if(opt_repair_schedule) { + pset_foreach(defs, tmp) { + op_t *tmp_op = get_irn_link(tmp); + spill_t *spill = set_find_spill(spill_bb->ilp, tmp); +#if 1 + ilp_var_t delete; + assert(spill); + + ir_snprintf(buf, sizeof(buf), "delete_%N", tmp); + delete = lpp_add_var_default(si->lpp, buf, lpp_binary, -1.0*get_cost(si, irn)*execution_frequency(si, bb), 0.0); + + /* op may not be killed if its first live_range is 1 */ + ir_snprintf(buf, sizeof(buf), "killorig-lr_%N", tmp); + cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0); + lpp_set_factor_fast(si->lpp, cst, delete, 1.0); + lpp_set_factor_fast(si->lpp, cst, tmp_op->attr.live_range.ilp, 1.0); + + /* op may not be killed if it is spilled after the definition */ + ir_snprintf(buf, sizeof(buf), "killorig-spill_%N", tmp); + cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0); + lpp_set_factor_fast(si->lpp, cst, delete, 1.0); + lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0); +#else + ilp_var_t keep; + assert(spill); + + ir_snprintf(buf, sizeof(buf), "keep_%N", tmp); + keep = lpp_add_var_default(si->lpp, buf, lpp_binary, get_cost(si, irn)*execution_frequency(si, bb), 1.0); + + /* op may not be killed if its first live_range is 1 */ + ir_snprintf(buf, sizeof(buf), "killorig-lr_%N", tmp); + cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 0.0); + lpp_set_factor_fast(si->lpp, cst, keep, 1.0); + lpp_set_factor_fast(si->lpp, cst, tmp_op->attr.live_range.ilp, -1.0); + + /* op may not be killed if it is spilled after the definition */ + ir_snprintf(buf, sizeof(buf), "killorig-spill_%N", tmp); + cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 0.0); + lpp_set_factor_fast(si->lpp, cst, keep, 1.0); + lpp_set_factor_fast(si->lpp, cst, spill->spill, -1.0); +#endif + } + } else { +#if 0 + pset_foreach(defs, tmp) { + op_t *tmp_op = get_irn_link(tmp); + spill_t *spill = set_find_spill(spill_bb->ilp, tmp); + assert(spill); + + /* live_range or spill should be 1 + TODO: lr should be live until first use */ + ir_snprintf(buf, sizeof(buf), "nokillorig_%N", tmp); + cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 1.0); + lpp_set_factor_fast(si->lpp, cst, tmp_op->attr.live_range.ilp, 1.0); + lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0); + } +#endif + } /****************** @@ -2135,7 +2343,7 @@ skip_one_must_die: pset_remove_ptr(live, tmp); } - if(opt_memoperands) { + if(opt_memoperands && (!is_start_block(bb) || be_is_Barrier(irn))) { ir_snprintf(buf, sizeof(buf), "one_memoperand_%N", irn); one_memoperand = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0); } @@ -2179,7 +2387,7 @@ skip_one_must_die: } } - if(opt_memoperands) { + if(opt_memoperands && (!is_start_block(bb) || be_is_Barrier(irn))) { n_memoperands = 0; for(n = get_irn_arity(irn)-1; n>=0; --n) { if(get_irn_n(irn, n) == arg) { @@ -2219,9 +2427,7 @@ skip_one_must_die: } } - /* requirements for remats */ - /* start new live ranges for values used by remats */ foreach_pre_remat(si, irn, tmp) { op_t *remat_op = get_irn_link(tmp); int n; @@ -2261,18 +2467,28 @@ skip_one_must_die: assert(has_reg_class(si, tmp)); } +#ifndef NDEBUG for (n=get_irn_arity(irn)-1; n>=0; --n) { ir_node *arg = get_irn_n(irn, n); assert(!find_post_remat(arg, irn) && "there should be no post remat for an argument of an op"); } +#endif del_pset(remat_defs); del_pset(used); del_set(args); del_pset(defs); defs = pset_new_ptr_default(); + + /* skip everything above barrier in start block */ + if(is_start_block(bb) && be_is_Barrier(irn)) { + assert(pset_count(live) == 0); + break; + } + } + del_pset(defs); @@ -2289,7 +2505,6 @@ skip_one_must_die: } /* construct mem_outs for all values */ - set_foreach(spill_bb->ilp, spill) { ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", spill->irn, bb); cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0); @@ -2306,14 +2521,10 @@ skip_one_must_die: spill->mem_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, default_spilled); lpp_set_factor_fast(si->lpp, cst, spill->mem_in, -1.0); - if(is_Phi(spill->irn) && get_nodes_block(spill->irn) == bb) { + if(opt_memcopies && is_Phi(spill->irn) && get_nodes_block(spill->irn) == bb) { int n; op_t *op = get_irn_link(spill->irn); - /* do we have to copy a phi argument? */ - op->attr.live_range.args.copies = obstack_alloc(si->obst, sizeof(*op->attr.live_range.args.copies) * get_irn_arity(spill->irn)); - memset(op->attr.live_range.args.copies, 0xFF, sizeof(*op->attr.live_range.args.copies) * get_irn_arity(spill->irn)); - for(n=get_irn_arity(spill->irn)-1; n>=0; --n) { const ir_node *arg = get_irn_n(spill->irn, n); double freq=0.0; @@ -2357,6 +2568,65 @@ skip_one_must_die: } } + foreach_post_remat(bb, tmp) { + int n; + op_t *remat_op = get_irn_link(tmp); + pset *remat_args = pset_new_ptr(get_irn_arity(tmp)); + ir_node *remat_arg; + + for (n=get_irn_arity(tmp)-1; n>=0; --n) { + remat_arg = get_irn_n(tmp, n); + + if(has_reg_class(si, remat_arg)) { + pset_insert_ptr(remat_args, remat_arg); + } + } + + /* remat + \sum live_range(remat_arg) <= |args| */ + ir_snprintf(buf, sizeof(buf), "one_must_die_%N", tmp); + cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, pset_count(remat_args)); + lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0); + + pset_foreach(remat_args, remat_arg) { + if(pset_find_ptr(live, remat_arg)) { + op_t *remat_arg_op = get_irn_link(remat_arg); + lpp_set_factor_fast(si->lpp, cst, remat_arg_op->attr.live_range.ilp, 1.0); + } + } + del_pset(remat_args); + } + + foreach_post_remat(bb, tmp) { + int n; + + for(n=get_irn_arity(tmp)-1; n>=0; --n) { + ir_node *remat_arg = get_irn_n(tmp, n); + + /* if value is becoming live through use by remat2 */ + if(has_reg_class(si, remat_arg) && !pset_find_ptr(live, remat_arg)) { + op_t *remat_arg_op = get_irn_link(remat_arg); + ilp_cst_t nomem; + + DBG((si->dbg, LEVEL_3, " value %+F becoming live through use by remat2 at bb start %+F\n", remat_arg, tmp)); + + pset_insert_ptr(live, remat_arg); + spill = add_to_spill_bb(si, bb, remat_arg); + remat_arg_op->attr.live_range.ilp = ILP_UNDEF; + + /* we need reg_in and mem_in for this value; they will be referenced later */ + ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N", remat_arg, bb); + spill->reg_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0); + ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N", remat_arg, bb); + spill->mem_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0); + + + /* optimization: all memory stuff should be 0, for we do not want to insert reloads for remats */ + ir_snprintf(buf, sizeof(buf), "nomem_%N_%N", remat_arg, bb); + nomem = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 0.0); + lpp_set_factor_fast(si->lpp, nomem, spill->spill, 1.0); + } + } + } /* L\U is empty at bb start */ /* arg is live throughout epilog if it is reg_in into this block */ @@ -2364,6 +2634,8 @@ skip_one_must_die: /* check the register pressure at the beginning of the block * including remats */ + /* reg_in entspricht post_use */ + ir_snprintf(buf, sizeof(buf), "check_start_%N", bb); cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs); @@ -2385,36 +2657,8 @@ skip_one_must_die: lpp_set_factor_fast(si->lpp, nospill, spill->mem_in, 1.0); lpp_set_factor_fast(si->lpp, nospill, spill->spill, 1.0); - } - foreach_post_remat(bb, irn) { - op_t *remat_op = get_irn_link(irn); - - DBG((si->dbg, LEVEL_4, "\t next post remat: %+F\n", irn)); - assert(remat_op->is_remat && !remat_op->attr.remat.pre); - - lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0); - } - - /* forall post remats add requirements */ - foreach_post_remat(bb, tmp) { - int n; - - for (n=get_irn_arity(tmp)-1; n>=0; --n) { - ir_node *remat_arg = get_irn_n(tmp, n); - op_t *remat_op = get_irn_link(tmp); - - if(!has_reg_class(si, remat_arg)) continue; - - spill = set_find_spill(spill_bb->ilp, remat_arg); - assert(spill); - - /* remat <= reg_in_argument */ - ir_snprintf(buf, sizeof(buf), "req_remat2_%N_%N_arg_%N", tmp, bb, remat_arg); - cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0); - lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0); - lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0); - } - } + } /* post_remats are NOT included in register pressure check because + they do not increase regpressure */ /* mem_in/reg_in for live_in values, especially phis and their arguments */ pset_foreach(live, irn) { @@ -2454,7 +2698,9 @@ skip_one_must_die: assert(spill_p); lpp_set_factor_fast(si->lpp, mem_in, spill_p->mem_out, -1.0); - lpp_set_factor_fast(si->lpp, mem_in, op->attr.live_range.args.copies[n], -1.0); + if(opt_memcopies) + lpp_set_factor_fast(si->lpp, mem_in, op->attr.live_range.args.copies[n], -1.0); + lpp_set_factor_fast(si->lpp, reg_in, spill_p->reg_out, -1.0); } } @@ -2485,36 +2731,91 @@ skip_one_must_die: } } + foreach_post_remat(bb, tmp) { + int n; + + for (n=get_irn_arity(tmp)-1; n>=0; --n) { + ir_node *remat_arg = get_irn_n(tmp, n); + op_t *remat_op = get_irn_link(tmp); + + if(!has_reg_class(si, remat_arg)) continue; + + spill = set_find_spill(spill_bb->ilp, remat_arg); + assert(spill); + + ir_snprintf(buf, sizeof(buf), "req_remat2_%N_%N_arg_%N", tmp, bb, remat_arg); + cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0); + lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0); + lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0); + } + } + + pset_foreach(live, irn) { + const op_t *op = get_irn_link(irn); + const ir_node *remat; + int n_remats = 0; + + cst = ILP_UNDEF; + + foreach_post_remat(bb, remat) { + int n; + + for (n=get_irn_arity(remat)-1; n>=0; --n) { + const ir_node *arg = get_irn_n(remat, n); + + if(arg == irn) { + const op_t *remat_op = get_irn_link(remat); + + if(cst == ILP_UNDEF) { + /* sum remat2s <= 1 + n_remats*live_range */ + ir_snprintf(buf, sizeof(buf), "dying_lr_%N_%N", irn, bb); + cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0); + } + lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0); + ++n_remats; + break; + } + } + } + if(cst != ILP_UNDEF && op->attr.live_range.ilp != ILP_UNDEF) { + lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -n_remats); + } + } + /* first live ranges from reg_ins */ pset_foreach(live, irn) { op_t *op = get_irn_link(irn); - spill = set_find_spill(spill_bb->ilp, irn); - assert(spill && spill->irn == irn); + if(op->attr.live_range.ilp != ILP_UNDEF) { - ir_snprintf(buf, sizeof(buf), "first_lr_%N_%N", irn, bb); - cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0); - lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, 1.0); - lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0); + spill = set_find_spill(spill_bb->ilp, irn); + assert(spill && spill->irn == irn); - foreach_post_remat(bb, tmp) { - op_t *remat_op = get_irn_link(tmp); + ir_snprintf(buf, sizeof(buf), "first_lr_%N_%N", irn, bb); + cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0); + lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, 1.0); + lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0); - if(remat_op->attr.remat.remat->value == irn) { - lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0); + foreach_post_remat(bb, tmp) { + op_t *remat_op = get_irn_link(tmp); + + if(remat_op->attr.remat.remat->value == irn) { + lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0); + } } } } /* walk forward now and compute constraints for placing spills */ /* this must only be done for values that are not defined in this block */ - /* TODO are these values at start of block? if yes, just check whether this is a diverge edge and skip the loop */ pset_foreach(live, irn) { /* * if value is defined in this block we can anways place the spill directly after the def * -> no constraint necessary */ - if(!is_Phi(irn) && get_nodes_block(irn) == bb) continue; + if(!is_Phi(irn) && get_nodes_block(irn) == bb) { + assert(0); + } spill = set_find_spill(spill_bb->ilp, irn); @@ -2576,6 +2877,7 @@ cmp_interference(const void *a, const void *b, size_t size) { const interference_t *p = a; const interference_t *q = b; + (void) size; return !(p->a == q->a && p->b == q->b); } @@ -2618,19 +2920,19 @@ set_insert_interference(spill_ilp_t * si, set * set, ir_node * a, ir_node * b, i return result; } -static int -values_interfere_in_block(const spill_ilp_t * si, const ir_node * bb, const ir_node * a, const ir_node * b) +static +int values_interfere_in_block(const spill_ilp_t *si, const ir_node *bb, const ir_node *a, const ir_node *b) { const ir_edge_t *edge; - if(get_nodes_block(a) != bb && get_nodes_block(b) != bb) { + if (get_nodes_block(a) != bb && get_nodes_block(b) != bb) { /* both values are live in, so they interfere */ return 1; } /* ensure a dominates b */ - if(value_dominates(b,a)) { - const ir_node * t; + if (value_dominates(b, a)) { + const ir_node *t; t = b; b = a; a = t; @@ -2639,14 +2941,15 @@ values_interfere_in_block(const spill_ilp_t * si, const ir_node * bb, const ir_n /* the following code is stolen from bera.c */ - if(be_is_live_end(si->lv, bb, a)) + if (be_is_live_end(si->lv, bb, a)) return 1; foreach_out_edge(a, edge) { const ir_node *user = edge->src; - if(get_nodes_block(user) == bb - && !is_Phi(user) + if (get_nodes_block(user) == bb + && ! is_Phi(user) && b != user + && ! pset_find_ptr(si->inverse_ops, user) && value_dominates(b, user)) return 1; } @@ -2669,21 +2972,20 @@ luke_interferencewalker(ir_node * bb, void * data) /* a is only interesting if it is in my register class and if it is inside a phi class */ - if (has_reg_class(si, a) && get_phi_class(a)) { - if(a_op->is_remat) + if (has_reg_class(si, a) && get_phi_class(si->pc, a)) { + if (a_op->is_remat || pset_find_ptr(si->inverse_ops, a)) continue; - for(l2=_be_lv_next_irn(si->lv, bb, 0xff, l1+1); l2>=0; l2=_be_lv_next_irn(si->lv, bb, 0xff, l2+1)) { - ir_node *b = be_lv_get_irn(si->lv, bb, l2); - op_t *b_op = get_irn_link(b); - + for (l2 = _be_lv_next_irn(si->lv, bb, 0xff, l1 + 1); l2 >= 0; l2 = _be_lv_next_irn(si->lv, bb, 0xff, l2 + 1)) { + ir_node *b = be_lv_get_irn(si->lv, bb, l2); + op_t *b_op = get_irn_link(b); /* a and b are only interesting if they are in the same phi class */ - if(has_reg_class(si, b) && get_phi_class(a) == get_phi_class(b)) { - if(b_op->is_remat) + if (has_reg_class(si, b) && get_phi_class(si->pc, a) == get_phi_class(si->pc, b)) { + if (b_op->is_remat || pset_find_ptr(si->inverse_ops, b)) continue; - if(values_interfere_in_block(si, bb, a, b)) { + if (values_interfere_in_block(si, bb, a, b)) { DBG((si->dbg, LEVEL_4, "\tvalues interfere in %+F: %+F, %+F\n", bb, a, b)); set_insert_interference(si, si->interferences, a, b, bb); } @@ -2758,7 +3060,7 @@ find_copy_path(spill_ilp_t * si, const ir_node * irn, const ir_node * target, il paths += find_copy_path(si, arg, target, any_interfere, copies, visited); pset_remove(copies, INT_TO_PTR(copy), copy); - /*if(paths > MAX_PATHS) { + if(paths > MAX_PATHS) { if(pset_count(copies) == 0) { ilp_cst_t cst; char buf[256]; @@ -2778,7 +3080,7 @@ find_copy_path(spill_ilp_t * si, const ir_node * irn, const ir_node * target, il } } else if(pset_count(copies) == 0) { paths = 0; - }*/ + } } } @@ -2813,7 +3115,7 @@ find_copy_path(spill_ilp_t * si, const ir_node * irn, const ir_node * target, il paths += find_copy_path(si, user, target, any_interfere, copies, visited); pset_remove(copies, INT_TO_PTR(copy), copy); - /*if(paths > MAX_PATHS) { + if(paths > MAX_PATHS) { if(pset_count(copies) == 0) { ilp_cst_t cst; char buf[256]; @@ -2832,7 +3134,7 @@ find_copy_path(spill_ilp_t * si, const ir_node * irn, const ir_node * target, il } } else if(pset_count(copies) == 0) { paths = 0; - }*/ + } } } @@ -2861,26 +3163,21 @@ memcopyhandler(spill_ilp_t * si) char buf[256]; /* teste Speicherwerte auf Interferenz */ - /* analyze phi classes */ - phi_class_compute(si->chordal_env->irg); - DBG((si->dbg, LEVEL_2, "\t calling interferencewalker\n")); - irg_block_walk_graph(si->chordal_env->irg, luke_interferencewalker, NULL, si); + irg_block_walk_graph(si->birg->irg, luke_interferencewalker, NULL, si); /* now lets emit the ILP unequations for the crap */ set_foreach(si->interferences, interference) { irnlist_t *irnlist; - ilp_var_t interfere, - any_interfere; - ilp_cst_t any_interfere_cst, - cst; + ilp_var_t interfere, any_interfere; + ilp_cst_t any_interfere_cst, cst; const ir_node *a = interference->a; const ir_node *b = interference->b; /* any_interf <= \sum interf */ ir_snprintf(buf, sizeof(buf), "interfere_%N_%N", a, b); any_interfere_cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0); - any_interfere = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0); + any_interfere = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0); lpp_set_factor_fast(si->lpp, any_interfere_cst, any_interfere, 1.0); @@ -2950,9 +3247,13 @@ is_zero(double x) return fabs(x) < 0.00001; } +/** + * node attribute hook for changing colors + */ static int mark_remat_nodes_hook(FILE *F, ir_node *n, ir_node *l) { spill_ilp_t *si = get_irg_link(current_ir_graph); + (void) l; if(pset_find_ptr(si->all_possible_remats, n)) { op_t *op = (op_t*)get_irn_link(n); @@ -3066,7 +3367,7 @@ walker_pressure_annotator(ir_node * bb, void * data) static void dump_pressure_graph(spill_ilp_t * si, const char *suffix) { - be_dump(si->chordal_env->irg, suffix, dump_ir_block_graph_sched_pressure); + be_dump(si->birg->irg, suffix, dump_ir_block_graph_sched_pressure); } static void @@ -3088,7 +3389,7 @@ connect_all_remats_with_keep(spill_ilp_t * si) ++pos; } - si->keep = be_new_Keep(si->chordal_env->cls, si->chordal_env->irg, get_irg_end_block(si->chordal_env->irg), n_remats, ins); + si->keep = be_new_Keep(si->cls, si->birg->irg, get_irg_end_block(si->birg->irg), n_remats, ins); obstack_free(si->obst, ins); } @@ -3114,7 +3415,7 @@ connect_all_spills_with_keep(spill_ilp_t * si) ++pos; } - keep = be_new_Keep(si->chordal_env->cls, si->chordal_env->irg, get_irg_end_block(si->chordal_env->irg), n_spills, ins); + keep = be_new_Keep(si->cls, si->birg->irg, get_irg_end_block(si->birg->irg), n_spills, ins); obstack_free(si->obst, ins); } @@ -3123,12 +3424,11 @@ connect_all_spills_with_keep(spill_ilp_t * si) /** insert a spill at an arbitrary position */ ir_node *be_spill2(const arch_env_t *arch_env, ir_node *irn, ir_node *insert) { - ir_node *bl = is_Block(insert)?insert:get_nodes_block(insert); + ir_node *bl = is_Block(insert) ? insert : get_nodes_block(insert); ir_graph *irg = get_irn_irg(bl); - ir_node *frame = get_irg_frame(irg); - ir_node *spill; - ir_node *next; - + ir_node *frame = get_irg_frame(irg); + ir_node *spill; + ir_node *next; const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1); const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1); @@ -3146,7 +3446,7 @@ ir_node *be_spill2(const arch_env_t *arch_env, ir_node *irn, ir_node *insert) * which is its default initialization (see above). */ - if(bl == get_irg_start_block(irg) && sched_get_time_step(frame) >= sched_get_time_step(insert)) + if (bl == get_irg_start_block(irg) && sched_get_time_step(frame) >= sched_get_time_step(insert)) insert = frame; for (next = sched_next(insert); is_Phi(next) || is_Proj(next); next = sched_next(insert)) @@ -3159,7 +3459,7 @@ ir_node *be_spill2(const arch_env_t *arch_env, ir_node *irn, ir_node *insert) static void delete_remat(spill_ilp_t * si, ir_node * remat) { int n; - ir_node *bad = get_irg_bad(si->chordal_env->irg); + ir_node *bad = get_irg_bad(si->birg->irg); sched_remove(remat); @@ -3175,7 +3475,7 @@ clean_remat_info(spill_ilp_t * si) int n; remat_t *remat; remat_info_t *remat_info; - ir_node *bad = get_irg_bad(si->chordal_env->irg); + ir_node *bad = get_irg_bad(si->birg->irg); set_foreach(si->remat_info, remat_info) { if(!remat_info->remats) continue; @@ -3183,11 +3483,17 @@ clean_remat_info(spill_ilp_t * si) pset_foreach(remat_info->remats, remat) { if(remat->proj && get_irn_n_edges(remat->proj) == 0) { + if(sched_is_scheduled(remat->proj)) { + sched_remove((ir_node*)remat->proj); + } set_irn_n((ir_node*)remat->proj, -1, bad); set_irn_n((ir_node*)remat->proj, 0, bad); } if(get_irn_n_edges(remat->op) == 0) { + if(sched_is_scheduled(remat->op)) { + sched_remove((ir_node*)remat->op); + } for (n=get_irn_arity(remat->op)-1; n>=-1; --n) { set_irn_n((ir_node*)remat->op, n, bad); } @@ -3204,12 +3510,9 @@ delete_unnecessary_remats(spill_ilp_t * si) { if(opt_keep_alive & KEEPALIVE_REMATS) { int n; - ir_node *bad = get_irg_bad(si->chordal_env->irg); + ir_node *bad = get_irg_bad(si->birg->irg); if(si->keep) { -// ir_node *end = get_irg_end(si->chordal_env->irg); -// ir_node **keeps; - for (n=get_irn_arity(si->keep)-1; n>=0; --n) { ir_node *keep_arg = get_irn_n(si->keep, n); op_t *arg_op = get_irn_link(keep_arg); @@ -3241,18 +3544,6 @@ delete_unnecessary_remats(spill_ilp_t * si) set_irn_n(si->keep, n, bad); } -#if 0 - for (i = 0, n = get_End_n_keepalives(end); i < n; ++i) { - ir_node *end_arg = get_End_keepalive(end, i); - - if(end_arg != si->keep) { - obstack_grow(si->obst, &end_arg, sizeof(end_arg)); - } - } - keeps = obstack_finish(si->obst); - set_End_keepalives(end, n-1, keeps); - obstack_free(si->obst, keeps); -#endif } else { DBG((si->dbg, LEVEL_2, "\t no remats to delete (none have been inserted)\n")); } @@ -3305,16 +3596,28 @@ get_spills_for_value(spill_ilp_t * si, const ir_node * value) return spills; } +static ir_node * +new_r_PhiM_nokeep(ir_graph * irg, ir_node *block, int arity, ir_node **in) +{ + ir_node *res; + + assert( get_irn_arity(block) == arity ); + + res = new_ir_node(NULL, irg, block, op_Phi, mode_M, arity, in); + res->attr.phi.u.backedge = new_backedge_arr(irg->obst, arity); + + return res; +} + /** * @param before The node after which the spill will be placed in the schedule */ -/* TODO set context properly */ static ir_node * insert_spill(spill_ilp_t * si, ir_node * irn, const ir_node * value, ir_node * before) { defs_t *defs; ir_node *spill; - const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env; + const arch_env_t *arch_env = si->birg->main_env->arch_env; DBG((si->dbg, LEVEL_3, "\t inserting spill for value %+F after %+F\n", irn, before)); @@ -3350,7 +3653,7 @@ insert_mem_phi(spill_ilp_t * si, ir_node * phi) ins[n] = si->m_unknown; } - mem_phi = new_r_Phi(si->chordal_env->irg, get_nodes_block(phi), get_irn_arity(phi), ins, mode_M); + mem_phi = new_r_PhiM_nokeep(si->birg->irg, get_nodes_block(phi), get_irn_arity(phi), ins); defs = set_insert_def(si->values, phi); assert(defs); @@ -3359,7 +3662,11 @@ insert_mem_phi(spill_ilp_t * si, ir_node * phi) set_irn_link(mem_phi, defs->spills); defs->spills = mem_phi; +#ifdef SCHEDULE_PHIM sched_add_after(phi, mem_phi); +#else + pset_insert_ptr(si->phims, mem_phi); +#endif if(opt_keep_alive & KEEPALIVE_SPILLS) pset_insert_ptr(si->spills, mem_phi); @@ -3397,7 +3704,7 @@ insert_reload(spill_ilp_t * si, const ir_node * value, ir_node * after) defs_t *defs; ir_node *reload, *spill; - const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env; + const arch_env_t *arch_env = si->birg->main_env->arch_env; DBG((si->dbg, LEVEL_3, "\t inserting reload for value %+F before %+F\n", value, after)); @@ -3418,10 +3725,9 @@ insert_reload(spill_ilp_t * si, const ir_node * value, ir_node * after) void perform_memory_operand(spill_ilp_t * si, memoperand_t * memoperand) { defs_t *defs; - ir_node *reload; ir_node *value = get_irn_n(memoperand->irn, memoperand->pos); ir_node *spill; - const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env; + const arch_env_t *arch_env = si->birg->main_env->arch_env; DBG((si->dbg, LEVEL_2, "\t inserting memory operand for value %+F at %+F\n", value, memoperand->irn)); @@ -3430,10 +3736,7 @@ void perform_memory_operand(spill_ilp_t * si, memoperand_t * memoperand) spill = defs->spills; assert(spill && "no spill placed before reload"); - reload = be_reload(arch_env, si->cls, memoperand->irn, get_irn_mode(value), spill); - - arch_perform_memory_operand(arch_env, memoperand->irn, reload, memoperand->pos); - sched_remove(reload); + arch_perform_memory_operand(arch_env, memoperand->irn, spill, memoperand->pos); } void insert_memoperands(spill_ilp_t * si) @@ -3527,7 +3830,7 @@ insert_mem_copy(spill_ilp_t * si, ir_node * bb, ir_node * value) { ir_node *insert_pos = bb; ir_node *spill; - const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env; + const arch_env_t *arch_env = si->birg->main_env->arch_env; /* find last definition of arg value in block */ ir_node *next; @@ -3704,6 +4007,7 @@ walker_reload_placer(ir_node * bb, void * data) { reload = insert_reload(si, arg, insert_pos); + assert(reload && "no reload returned"); set_irn_n(irn, n, reload); if(opt_keep_alive & KEEPALIVE_RELOADS) @@ -3721,13 +4025,13 @@ walker_reload_placer(ir_node * bb, void * data) { static void walker_collect_used(ir_node * irn, void * data) { - lc_bitset_t *used = data; + bitset_t *used = data; - lc_bitset_set(used, get_irn_idx(irn)); + bitset_set(used, get_irn_idx(irn)); } struct kill_helper { - lc_bitset_t *used; + bitset_t *used; spill_ilp_t *si; }; @@ -3743,7 +4047,7 @@ walker_kill_unused(ir_node * bb, void * data) ir_node *next = sched_next(irn); int n; - if(!lc_bitset_is_set(kh->used, get_irn_idx(irn))) { + if(!bitset_is_set(kh->used, get_irn_idx(irn))) { if(be_is_Spill(irn) || be_is_Reload(irn)) { DBG((kh->si->dbg, LEVEL_1, "\t SUBOPTIMAL! %+F IS UNUSED (cost: %g)\n", irn, get_cost(kh->si, irn)*execution_frequency(kh->si, bb))); #if 0 @@ -3762,18 +4066,41 @@ walker_kill_unused(ir_node * bb, void * data) } } +#ifndef SCHEDULE_PHIM +static void +kill_unused_phims(spill_ilp_t * si, struct kill_helper * kh) +{ + ir_node *phi; + ir_node *bad = get_irg_bad(si->birg->irg); + int n; + + pset_foreach(si->phims, phi) { + if(!bitset_is_set(kh->used, get_irn_idx(phi))) { + + set_nodes_block(phi, bad); + for (n=get_irn_arity(phi)-1; n>=0; --n) { + set_irn_n(phi, n, bad); + } + } + } +} +#endif + static void kill_all_unused_values_in_schedule(spill_ilp_t * si) { - struct kill_helper kh; + struct kill_helper kh; - kh.used = lc_bitset_malloc(get_irg_last_idx(si->chordal_env->irg)); + kh.used = bitset_malloc(get_irg_last_idx(si->birg->irg)); kh.si = si; - irg_walk_graph(si->chordal_env->irg, walker_collect_used, NULL, kh.used); - irg_block_walk_graph(si->chordal_env->irg, walker_kill_unused, NULL, &kh); + irg_walk_graph(si->birg->irg, walker_collect_used, NULL, kh.used); +#ifndef SCHEDULE_PHIM + kill_unused_phims(si, &kh); +#endif + irg_block_walk_graph(si->birg->irg, walker_kill_unused, NULL, &kh); - lc_bitset_free(kh.used); + bitset_free(kh.used); } void @@ -3787,39 +4114,42 @@ print_irn_pset(pset * p) } void -dump_phi_class(spill_ilp_t * si, pset * phiclass, const char * file) +dump_phi_class(spill_ilp_t *si, ir_node **phiclass, const char * file) { FILE *f = fopen(file, "w"); ir_node *irn; interference_t *interference; + int i; - pset_break(phiclass); set_break(si->interferences); ir_fprintf(f, "digraph phiclass {\n"); - pset_foreach(phiclass, irn) { - if(is_Phi(irn)) - ir_fprintf(f, " %F%N [shape=box]\n",irn,irn); + for (i = ARR_LEN(phiclass) - 1; i >= 0; --i) { + irn = phiclass[i]; + if (is_Phi(irn)) + ir_fprintf(f, " %F%N [shape=box]\n", irn, irn); } - pset_foreach(phiclass, irn) { + for (i = ARR_LEN(phiclass) - 1; i >= 0; --i) { int n; - if(!is_Phi(irn)) continue; + irn = phiclass[i]; + if (! is_Phi(irn)) + continue; - for(n=get_irn_arity(irn)-1; n>=0; --n) { + for (n = get_irn_arity(irn) - 1; n >= 0; --n) { ir_node *arg = get_irn_n(irn, n); - ir_fprintf(f, " %F%N -> %F%N\n",irn,irn,arg,arg); + ir_fprintf(f, " %F%N -> %F%N\n", irn, irn, arg, arg); } } set_foreach(si->interferences, interference) { - const ir_node *a = interference->a; - const ir_node *b = interference->b; - if(get_phi_class(a) == phiclass) { - ir_fprintf(f, " %F%N -> %F%N [color=red,dir=none,style=bold]\n",a,a,b,b); + const ir_node *a = interference->a; + const ir_node *b = interference->b; + if (get_phi_class(si->pc, (ir_node *)a) == phiclass) { + ir_fprintf(f, " %F%N -> %F%N [color=red,dir=none,style=bold]\n", a, a, b, b); } } @@ -3830,11 +4160,11 @@ dump_phi_class(spill_ilp_t * si, pset * phiclass, const char * file) static void rewire_uses(spill_ilp_t * si) { - dom_front_info_t *dfi = be_compute_dominance_frontiers(si->chordal_env->irg); defs_t *defs; - pset *ignore = pset_new_ptr(1); + ir_nodeset_t ignore; - pset_insert_ptr(ignore, get_irg_end(si->chordal_env->irg)); + ir_nodeset_init(&ignore); + ir_nodeset_insert(&ignore, get_irg_end(si->birg->irg)); /* then fix uses of spills */ set_foreach(si->values, defs) { @@ -3857,11 +4187,25 @@ rewire_uses(spill_ilp_t * si) spills = get_spills_for_value(si, defs->value); DBG((si->dbg, LEVEL_2, "\t %d remats, %d reloads, and %d spills for value %+F\n", remats, pset_count(reloads), pset_count(spills), defs->value)); if(pset_count(spills) > 1) { + be_ssa_construction_env_t senv; + ir_node *node; //assert(pset_count(reloads) > 0); // print_irn_pset(spills); // print_irn_pset(reloads); - be_ssa_constr_set_ignore(dfi, si->lv, spills, ignore); + be_ssa_construction_init(&senv, si->birg); + be_ssa_construction_set_ignore_uses(&senv, &ignore); + pset_foreach(spills, node) { + be_ssa_construction_add_copy(&senv, node); + } + pset_foreach(spills, node) { + be_ssa_construction_fix_users(&senv, node); + } + be_ssa_construction_update_liveness_phis(&senv, si->lv); + pset_foreach(spills, node) { + be_liveness_update(si->lv, node); + } + be_ssa_construction_destroy(&senv); } del_pset(reloads); @@ -3870,30 +4214,52 @@ rewire_uses(spill_ilp_t * si) /* first fix uses of remats and reloads */ set_foreach(si->values, defs) { - pset *nodes; const ir_node *next = defs->remats; + int orig_kept = 0; if(next) { - nodes = pset_new_ptr_default(); - pset_insert_ptr(nodes, defs->value); + be_ssa_construction_env_t senv; + + be_ssa_construction_init(&senv, si->birg); + + if(sched_is_scheduled(defs->value)) { + be_ssa_construction_add_copy(&senv, (ir_node*) defs->value); + orig_kept = 1; + } + + next = defs->remats; + while(next) { + be_ssa_construction_add_copy(&senv, (ir_node*) next); + next = get_irn_link(next); + } + + if(sched_is_scheduled(defs->value)) { + be_ssa_construction_fix_users(&senv, (ir_node*) defs->value); + } + next = defs->remats; while(next) { - pset_insert_ptr(nodes, next); + be_ssa_construction_fix_users(&senv, (ir_node*) next); next = get_irn_link(next); } - if(pset_count(nodes) > 1) { - DBG((si->dbg, LEVEL_4, "\t %d new definitions for value %+F\n", pset_count(nodes)-1, defs->value)); - be_ssa_constr_set(dfi, si->lv, nodes); + be_ssa_construction_update_liveness_phis(&senv, si->lv); + if(sched_is_scheduled(defs->value)) { + be_liveness_update(si->lv, (ir_node*) defs->value); + } + + next = defs->remats; + while(next) { + be_liveness_update(si->lv, (ir_node*) next); + next = get_irn_link(next); } - del_pset(nodes); + be_ssa_construction_destroy(&senv); } } + ir_nodeset_destroy(&ignore); // remove_unused_defs(si); - - be_free_dominance_frontiers(dfi); } @@ -3906,9 +4272,9 @@ writeback_results(spill_ilp_t * si) DBG((si->dbg, LEVEL_1, "Applying results\n")); delete_unnecessary_remats(si); - si->m_unknown = new_r_Unknown(si->chordal_env->irg, mode_M); - irg_block_walk_graph(si->chordal_env->irg, walker_spill_placer, NULL, si); - irg_block_walk_graph(si->chordal_env->irg, walker_reload_placer, NULL, si); + si->m_unknown = new_r_Unknown(si->birg->irg, mode_M); + irg_block_walk_graph(si->birg->irg, walker_spill_placer, NULL, si); + irg_block_walk_graph(si->birg->irg, walker_reload_placer, NULL, si); if(opt_memoperands) insert_memoperands(si); phim_fixer(si); @@ -3926,18 +4292,22 @@ writeback_results(spill_ilp_t * si) static int get_n_regs(spill_ilp_t * si) { - int arch_n_regs = arch_register_class_n_regs(si->cls); - int free = 0; - int i; + int arch_n_regs = arch_register_class_n_regs(si->cls); - for(i=0; icls->regs[i], ignore)) { - free++; - } - } + bitset_t *arch_regs = bitset_malloc(arch_n_regs); + bitset_t *abi_regs = bitset_malloc(arch_n_regs); - DBG((si->dbg, LEVEL_1, "\tArchitecture has %d free registers in class %s\n", free, si->cls->name)); - return free; + arch_put_non_ignore_regs(si->birg->main_env->arch_env, si->cls, arch_regs); + be_abi_put_ignore_regs(si->birg->abi, si->cls, abi_regs); + + bitset_andnot(arch_regs, abi_regs); + arch_n_regs = bitset_popcnt(arch_regs); + + bitset_free(arch_regs); + bitset_free(abi_regs); + + DBG((si->dbg, LEVEL_1, "\tArchitecture has %d free registers in class %s\n", arch_n_regs, si->cls->name)); + return arch_n_regs; } static void @@ -3985,7 +4355,7 @@ walker_reload_mover(ir_node * bb, void * data) static void move_reloads_upward(spill_ilp_t * si) { - irg_block_walk_graph(si->chordal_env->irg, walker_reload_mover, NULL, si); + irg_block_walk_graph(si->birg->irg, walker_reload_mover, NULL, si); } @@ -3995,24 +4365,26 @@ move_reloads_upward(spill_ilp_t * si) static void luke_meminterferencechecker(ir_node * bb, void * data) { - spill_ilp_t *si = (spill_ilp_t*)data; - int l1, l2; + spill_ilp_t *si = (spill_ilp_t*)data; + int l1, l2; be_lv_foreach(si->lv, bb, be_lv_state_end | be_lv_state_out | be_lv_state_in, l1) { ir_node *a = be_lv_get_irn(si->lv, bb, l1); - if(!be_is_Spill(a) && (!is_Phi(a) || get_irn_mode(a) != mode_T)) continue; + if (! be_is_Spill(a) && (!is_Phi(a) || get_irn_mode(a) != mode_T)) + continue; /* a is only interesting if it is in my register class and if it is inside a phi class */ - if (has_reg_class(si, a) && get_phi_class(a)) { - for(l2=_be_lv_next_irn(si->lv, bb, 0xff, l1+1); l2>=0; l2=_be_lv_next_irn(si->lv, bb, 0xff, l2+1)) { - ir_node *b = be_lv_get_irn(si->lv, bb, l2); + if (has_reg_class(si, a) && get_phi_class(si->pc, a)) { + for (l2 = _be_lv_next_irn(si->lv, bb, 0xff, l1 + 1); l2 >= 0; l2 = _be_lv_next_irn(si->lv, bb, 0xff, l2 + 1)) { + ir_node *b = be_lv_get_irn(si->lv, bb, l2); - if(!be_is_Spill(b) && (!is_Phi(b) || get_irn_mode(b) != mode_T)) continue; + if (! be_is_Spill(b) && (! is_Phi(b) || get_irn_mode(b) != mode_T)) + continue; /* a and b are only interesting if they are in the same phi class */ - if(has_reg_class(si, b) && get_phi_class(a) == get_phi_class(b)) { - if(values_interfere_in_block(si, bb, a, b)) { + if (has_reg_class(si, b) && get_phi_class(si->pc, a) == get_phi_class(si->pc, b)) { + if (values_interfere_in_block(si, bb, a, b)) { ir_fprintf(stderr, "$$ Spills interfere in %+F: %+F, %+F \t$$\n", bb, a, b); } } @@ -4025,41 +4397,15 @@ static void verify_phiclasses(spill_ilp_t * si) { /* analyze phi classes */ - phi_class_compute(si->chordal_env->irg); + phi_class_free(si->pc); + si->pc = phi_class_new_from_irg(si->birg->irg, 0); DBG((si->dbg, LEVEL_2, "\t calling memory interference checker\n")); - irg_block_walk_graph(si->chordal_env->irg, luke_meminterferencechecker, NULL, si); -} - -static void -walker_spillslotassigner(ir_node * irn, void * data) -{ - void *cls; - - if(!be_is_Spill(irn)) return; - - /* set spill context to phi class if it has one ;) */ - (void) cls; -#if 0 - // Matze: not needed anymore - cls = get_phi_class(irn); - if(cls) - be_set_Spill_context(irn, cls); - else - be_set_Spill_context(irn, irn); -#endif -} - - -static void -assign_spillslots(spill_ilp_t * si) -{ - DBG((si->dbg, LEVEL_2, "\t calling spill slot assigner\n")); - irg_walk_graph(si->chordal_env->irg, walker_spillslotassigner, NULL, si); + irg_block_walk_graph(si->birg->irg, luke_meminterferencechecker, NULL, si); } void -be_spill_remat(const be_chordal_env_t * chordal_env) +be_spill_remat(be_irg_t *birg, const arch_register_class_t *cls) { char buf[256]; char problem_name[256]; @@ -4067,80 +4413,88 @@ be_spill_remat(const be_chordal_env_t * chordal_env) char dump_suffix2[256]; struct obstack obst; spill_ilp_t si; + ir_graph *irg = be_get_birg_irg(birg); - ir_snprintf(problem_name, sizeof(problem_name), "%F_%s", chordal_env->irg, chordal_env->cls->name); - ir_snprintf(dump_suffix, sizeof(dump_suffix), "-%s-remats", chordal_env->cls->name); - ir_snprintf(dump_suffix2, sizeof(dump_suffix2), "-%s-pressure", chordal_env->cls->name); + ir_snprintf(problem_name, sizeof(problem_name), "%F_%s", irg, cls->name); + ir_snprintf(dump_suffix, sizeof(dump_suffix), "-%s-remats", cls->name); + ir_snprintf(dump_suffix2, sizeof(dump_suffix2), "-%s-pressure", cls->name); FIRM_DBG_REGISTER(si.dbg, "firm.be.ra.spillremat"); DBG((si.dbg, LEVEL_1, "\n\n\t\t===== Processing %s =====\n\n", problem_name)); if(opt_verify & VERIFY_DOMINANCE) - be_check_dominance(chordal_env->irg); + be_check_dominance(irg); + + be_assure_dom_front(birg); + be_assure_liveness(birg); obstack_init(&obst); - si.chordal_env = chordal_env; - si.obst = &obst; - si.cls = chordal_env->cls; - si.lpp = new_lpp(problem_name, lpp_minimize); - si.remat_info = new_set(cmp_remat_info, 4096); - si.interferences = new_set(cmp_interference, 32); - si.memoperands = new_set(cmp_memoperands, 128); + si.obst = &obst; + si.birg = birg; + si.cls = cls; + si.lpp = new_lpp(problem_name, lpp_minimize); + si.remat_info = new_set(cmp_remat_info, 4096); + si.interferences = new_set(cmp_interference, 32); + si.memoperands = new_set(cmp_memoperands, 128); si.all_possible_remats = pset_new_ptr_default(); - si.spills = pset_new_ptr_default(); - si.inverse_ops = pset_new_ptr_default(); - si.lv = chordal_env->lv; - si.keep = NULL; - si.n_regs = get_n_regs(&si); + si.spills = pset_new_ptr_default(); + si.inverse_ops = pset_new_ptr_default(); + si.lv = birg->lv; + si.keep = NULL; + si.n_regs = get_n_regs(&si); - set_irg_link(chordal_env->irg, &si); - compute_doms(chordal_env->irg); + set_irg_link(irg, &si); + compute_doms(irg); /* compute phi classes */ -// phi_class_compute(chordal_env->irg); + // phi_class_compute(irg); - be_analyze_regpressure(chordal_env, "-pre"); + if(opt_dump_flags & DUMP_STATS) + be_analyze_regpressure(birg, cls, "-pre"); + + DBG((si.dbg, LEVEL_2, "\t initializing\n")); + irg_block_walk_graph(irg, luke_initializer, NULL, &si); if(opt_remats) { /* collect remats */ DBG((si.dbg, LEVEL_1, "Collecting remats\n")); - irg_walk_graph(chordal_env->irg, walker_remat_collector, NULL, &si); + irg_walk_graph(irg, walker_remat_collector, NULL, &si); } /* insert possible remats */ DBG((si.dbg, LEVEL_1, "Inserting possible remats\n")); - irg_block_walk_graph(chordal_env->irg, walker_remat_insertor, NULL, &si); + irg_block_walk_graph(irg, walker_remat_insertor, NULL, &si); DBG((si.dbg, LEVEL_2, " -> inserted %d possible remats\n", pset_count(si.all_possible_remats))); if(opt_keep_alive & KEEPALIVE_REMATS) { DBG((si.dbg, LEVEL_1, "Connecting remats with keep and dumping\n")); connect_all_remats_with_keep(&si); /* dump graph with inserted remats */ - dump_graph_with_remats(chordal_env->irg, dump_suffix); + dump_graph_with_remats(irg, dump_suffix); } /* insert copies for phi arguments not in my regclass */ - irg_walk_graph(chordal_env->irg, walker_regclass_copy_insertor, NULL, &si); + irg_walk_graph(irg, walker_regclass_copy_insertor, NULL, &si); /* recompute liveness */ DBG((si.dbg, LEVEL_1, "Recomputing liveness\n")); be_liveness_recompute(si.lv); /* build the ILP */ - DBG((si.dbg, LEVEL_1, "\tBuilding ILP\n")); DBG((si.dbg, LEVEL_2, "\t endwalker\n")); - irg_block_walk_graph(chordal_env->irg, luke_endwalker, NULL, &si); + irg_block_walk_graph(irg, luke_endwalker, NULL, &si); DBG((si.dbg, LEVEL_2, "\t blockwalker\n")); - irg_block_walk_graph(chordal_env->irg, luke_blockwalker, NULL, &si); + irg_block_walk_graph(irg, luke_blockwalker, NULL, &si); - if(opt_memcopies) { + si.pc = phi_class_new_from_irg(birg->irg, 0); + if (opt_memcopies) { DBG((si.dbg, LEVEL_2, "\t memcopyhandler\n")); memcopyhandler(&si); } - if(opt_dump_flags & DUMP_PROBLEM) { + if (opt_dump_flags & DUMP_PROBLEM) { FILE *f; ir_snprintf(buf, sizeof(buf), "%s-spillremat.ilp", problem_name); if ((f = fopen(buf, "wt")) != NULL) { @@ -4149,17 +4503,17 @@ be_spill_remat(const be_chordal_env_t * chordal_env) } } - if(opt_dump_flags & DUMP_MPS) { + if (opt_dump_flags & DUMP_MPS) { FILE *f; ir_snprintf(buf, sizeof(buf), "%s-spillremat.mps", problem_name); - if((f = fopen(buf, "wt")) != NULL) { + if ((f = fopen(buf, "wt")) != NULL) { mps_write_mps(si.lpp, s_mps_fixed, f); fclose(f); } ir_snprintf(buf, sizeof(buf), "%s-spillremat.mst", problem_name); - if((f = fopen(buf, "wt")) != NULL) { + if ((f = fopen(buf, "wt")) != NULL) { mps_write_mst(si.lpp, s_mps_fixed, f); fclose(f); } @@ -4182,7 +4536,7 @@ be_spill_remat(const be_chordal_env_t * chordal_env) assert(lpp_is_sol_valid(si.lpp) && "solution of ILP must be valid"); - DBG((si.dbg, LEVEL_1, "\t%s: iterations: %d, solution time: %g, objective function: %g\n", problem_name, si.lpp->iterations, si.lpp->sol_time, is_zero(si.lpp->objval)?0.0:si.lpp->objval)); + DBG((si.dbg, LEVEL_1, "\t%s: iterations: %d, solution time: %g, objective function: %g, best bound: %g\n", problem_name, si.lpp->iterations, si.lpp->sol_time, is_zero(si.lpp->objval)?0.0:si.lpp->objval, is_zero(si.lpp->best_bound)?0.0:si.lpp->best_bound)); if(opt_dump_flags & DUMP_SOLUTION) { FILE *f; @@ -4199,49 +4553,75 @@ be_spill_remat(const be_chordal_env_t * chordal_env) } } +#ifndef SCHEDULE_PHIM + si.phims = pset_new_ptr_default(); +#endif writeback_results(&si); + #endif /* SOLVE */ kill_all_unused_values_in_schedule(&si); +#if !defined(SCHEDULE_PHIM) && defined(SOLVE) + del_pset(si.phims); +#endif + if(opt_keep_alive & (KEEPALIVE_SPILLS | KEEPALIVE_RELOADS)) - be_dump(chordal_env->irg, "-spills-placed", dump_ir_block_graph); + be_dump(irg, "-spills-placed", dump_ir_block_graph); // move reloads upwards be_liveness_recompute(si.lv); - irg_block_walk_graph(chordal_env->irg, walker_pressure_annotator, NULL, &si); + irg_block_walk_graph(irg, walker_pressure_annotator, NULL, &si); move_reloads_upward(&si); if(opt_memcopies) { verify_phiclasses(&si); - assign_spillslots(&si); } - irg_block_walk_graph(chordal_env->irg, walker_pressure_annotator, NULL, &si); + irg_block_walk_graph(irg, walker_pressure_annotator, NULL, &si); - dump_pressure_graph(&si, dump_suffix2); + if(opt_dump_flags & DUMP_PRESSURE) + dump_pressure_graph(&si, dump_suffix2); - be_analyze_regpressure(chordal_env, "-post"); + if(opt_dump_flags & DUMP_STATS) + be_analyze_regpressure(birg, cls, "-post"); if(opt_verify & VERIFY_DOMINANCE) - be_check_dominance(chordal_env->irg); + be_check_dominance(irg); - free_dom(chordal_env->irg); + free_dom(irg); del_set(si.interferences); del_pset(si.inverse_ops); del_pset(si.all_possible_remats); del_set(si.memoperands); del_pset(si.spills); free_lpp(si.lpp); + phi_class_free(si.pc); obstack_free(&obst, NULL); DBG((si.dbg, LEVEL_1, "\tdone.\n")); } +void be_init_spillremat(void) +{ + static be_spiller_t remat_spiller = { + be_spill_remat + }; + lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be"); + lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra"); + lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal"); + lc_opt_entry_t *remat_grp = lc_opt_get_grp(chordal_grp, "remat"); + + be_register_spiller("remat", &remat_spiller); + lc_opt_add_table(remat_grp, options); +} + +BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillremat); + #else /* WITH_ILP */ -static void -only_that_you_can_compile_without_WITH_ILP_defined(void) +static __attribute__((unused)) +void only_that_you_can_compile_without_WITH_ILP_defined(void) { }