#include "irnode_t.h"
#include "ircons_t.h"
#include "irloop_t.h"
-#include "phiclass.h"
+#include "phiclass_t.h"
#include "iredges.h"
#include "execfreq.h"
//#define KEEPALIVE_SPILLS
//#define KEEPALIVE_RELOADS
#define GOODWIN_REDUCTION
-#define NO_MEMCOPIES
+//#define NO_MEMCOPIES
#define SOLVE
//#define SOLVE_LOCAL
#define LPP_SERVER "i44pc52"
#define LPP_SOLVER "cplex"
-#define COST_LOAD 10
+#define COST_LOAD 8
#define COST_STORE 50
#define COST_REMAT 1
+#define LOOP_WEIGHT 12
+
#define ILP_TIMEOUT 120
#define ILP_UNDEF -1
}
static double
-execution_frequency(const spill_ilp_t * si, const ir_node * irn)
+execution_frequency(const ir_node * irn)
{
#define FUDGE 0.001
- if(si->execfreqs) {
- if(is_Block(irn)) {
- return get_block_execfreq(si->execfreqs, irn) + FUDGE;
- } else {
- return get_block_execfreq(si->execfreqs, get_nodes_block(irn)) + FUDGE;
- }
+#ifndef EXECFREQ_LOOPDEPH
+ if(is_Block(irn)) {
+ return get_block_execfreq(irn) + FUDGE;
} else {
- if(is_Block(irn))
- return exp(get_loop_depth(get_irn_loop(irn)) * log(10)) + FUDGE;
- else
- return exp(get_loop_depth(get_irn_loop(get_nodes_block(irn))) * log(10)) + FUDGE;
+ return get_block_execfreq(get_nodes_block(irn)) + FUDGE;
}
+#else
+ if(is_Block(irn))
+ return exp(get_loop_depth(get_irn_loop(irn)) * log(10)) + FUDGE;
+ else
+ return exp(get_loop_depth(get_irn_loop(get_nodes_block(irn))) * log(10)) + FUDGE;
+#endif
}
static double
bb = is_Block(pos)?pos:get_nodes_block(pos);
copy = exact_copy(irn);
+
+ _set_phi_class(copy, NULL);
set_nodes_block(copy, bb);
sched_put_before(si, pos, copy);
bb = is_Block(pos)?pos:get_nodes_block(pos);
copy = exact_copy(irn);
+
+ _set_phi_class(copy, NULL);
set_nodes_block(copy, bb);
sched_put_after(pos, copy);
op->is_remat = 1;
op->attr.remat.remat = remat;
op->attr.remat.pre = 0;
- op->attr.remat.ilp = lpp_add_var(si->lpp, buf, lpp_binary, remat->cost*execution_frequency(si, pos));
+ op->attr.remat.ilp = lpp_add_var(si->lpp, buf, lpp_binary, remat->cost*execution_frequency(pos));
set_irn_link(copy, op);
pset_insert_ptr(si->all_possible_remats, copy);
op->is_remat = 1;
op->attr.remat.remat = remat;
op->attr.remat.pre = 1;
- op->attr.remat.ilp = lpp_add_var(si->lpp, buf, lpp_binary, remat->cost*execution_frequency(si, pos));
+ op->attr.remat.ilp = lpp_add_var(si->lpp, buf, lpp_binary, remat->cost*execution_frequency(pos));
set_irn_link(copy, op);
pset_insert_ptr(si->all_possible_remats, copy);
query.irn = irn;
spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
- spill_cost = is_Unknown(irn)?0.0001:COST_STORE*execution_frequency(si, bb);
+ spill_cost = is_Unknown(irn)?0.0001:COST_STORE*execution_frequency(bb);
ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", irn, bb);
spill->reg_out = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
ilp_cst_t rel_cst;
ir_snprintf(buf, sizeof(buf), "reload_%N_%N", bb, irn);
- reload = lpp_add_var(si->lpp, buf, lpp_binary, COST_LOAD*execution_frequency(si, bb));
+ reload = lpp_add_var(si->lpp, buf, lpp_binary, COST_LOAD*execution_frequency(bb));
set_insert_keyval(spill_bb->reloads, irn, INT_TO_PTR(reload));
/* reload <= mem_out */
query.irn = irn;
spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
- spill_cost = is_Unknown(irn)?0.0001:COST_STORE*execution_frequency(si, bb);
+ spill_cost = is_Unknown(irn)?0.0001:COST_STORE*execution_frequency(bb);
ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", irn, bb);
spill->reg_out = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
spill->spill = lpp_add_var(si->lpp, buf, lpp_binary, spill_cost);
ir_snprintf(buf, sizeof(buf), "reload_%N_%N", bb, irn);
- reload = lpp_add_var(si->lpp, buf, lpp_binary, COST_LOAD*execution_frequency(si, bb));
+ reload = lpp_add_var(si->lpp, buf, lpp_binary, COST_LOAD*execution_frequency(bb));
set_insert_keyval(spill_bb->reloads, irn, INT_TO_PTR(reload));
/* reload <= mem_out */
query.irn = irn;
spill = set_find(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
if(!spill) {
- double spill_cost = is_Unknown(irn)?0.0001:COST_STORE*execution_frequency(si, bb);
+ double spill_cost = is_Unknown(irn)?0.0001:COST_STORE*execution_frequency(bb);
spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
}
}
- ir_snprintf(buf, sizeof(buf), "copyreq_%N_%N", block, to_copy);
+ ir_snprintf(buf, sizeof(buf), "copyreg_%N_%N", block, to_copy);
cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
/* copy - reg_out - copyreg <= 0 */
/* only for values in L\U (TODO and D?), the others are handled with post_use */
if(!pset_find_ptr(used, remat_arg)) {
- /* remat <= live_rang(remat_arg) */
+ /* remat <= live_range(remat_arg) */
ir_snprintf(buf, sizeof(buf), "req_remat2_%N_arg_%N", tmp, remat_arg);
cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
assert(spill);
ir_snprintf(buf, sizeof(buf), "reload_%N_%N", arg, irn);
- op->attr.live_range.args.reloads[i] = lpp_add_var(si->lpp, buf, lpp_binary, COST_LOAD*execution_frequency(si, bb));
+ op->attr.live_range.args.reloads[i] = lpp_add_var(si->lpp, buf, lpp_binary, COST_LOAD*execution_frequency(bb));
/* reload <= mem_out */
ir_snprintf(buf, sizeof(buf), "req_reload_%N_%N", arg, irn);
const ir_node *arg2 = get_irn_n(spill->irn, m);
if(arg==arg2) {
- freq += execution_frequency(si, get_Block_cfgpred_block(bb, m));
+ freq += execution_frequency(get_Block_cfgpred_block(bb, m));
}
}
}
-
-static void
-memcopyinsertor(spill_ilp_t * si)
-{
- /* weise Spillkontexte zu. Sorge bei Phis dafuer, dass gleiche
- * Kontexte zusammenfliessen (Operanden und Ergebnis hat gleichen
- * Kontext)
- */
-
-
-
-
-
-}
-
-
-
-
static INLINE int
is_zero(double x)
{
return 1;
}
+static int
+sched_block_attr_hook(FILE *F, ir_node *node, ir_node *local)
+{
+ if(is_Block(node)) {
+ fprintf(F, " info3:\"execfreq %g\"", execution_frequency(node));
+ }
+
+ return 0;
+}
+
static void
dump_ir_block_graph_sched_pressure(ir_graph *irg, const char *suffix)
{
- DUMP_NODE_EDGE_FUNC old = get_dump_node_edge_hook();
+ DUMP_NODE_EDGE_FUNC old_edge_hook = get_dump_node_edge_hook();
dump_consts_local(0);
+ set_dump_node_vcgattr_hook(sched_block_attr_hook);
set_dump_node_edge_hook(sched_pressure_edge_hook);
dump_ir_block_graph(irg, suffix);
- set_dump_node_edge_hook(old);
+ set_dump_node_vcgattr_hook(NULL);
+ set_dump_node_edge_hook(old_edge_hook);
}
static void
get_spills_for_value(spill_ilp_t * si, ir_node * value)
{
pset *spills = pset_new_ptr_default();
-// pset *visited = pset_new_ptr_default();
-// collect_spills(si, value, spills, visited);
-// del_pset(visited);
ir_node *next;
defs_t *defs;
ir_node *next;
defs_t *defs;
+ pset_insert_ptr(remats, value);
defs = set_find_def(si->values, value);
if(defs && defs->remats) {
return spill;
}
-static ir_node *
-insert_mem_copy(spill_ilp_t * si, const ir_node * bb, const ir_node * arg)
-{
- ir_node *prev = sched_block_last_noncf(si, bb);
- ir_node *insert_pos = sched_next(prev);
- op_t *prev_op = get_irn_link(prev);
- pset *remats = get_remats_for_value(si, arg);
- ir_node *spill;
- const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
-
- /* start from end of block and search a position for memcopy (spill) until block's last op begins */
- while(be_is_Spill(prev)) {
- prev = sched_prev(prev);
- }
-
- prev_op = get_irn_link(prev);
-
- while(!sched_is_end(prev) && !is_Phi(prev)
- && prev_op->is_remat && prev_op->attr.remat.pre) {
-
- insert_pos = prev;
-
- if(pset_find_ptr(remats, prev)) {
- insert_pos = sched_next(insert_pos);
- break;
- }
-
- do {
- prev = sched_prev(prev);
- } while(be_is_Spill(prev));
-
- prev_op = get_irn_link(prev);
- }
- insert_pos = sched_prev(insert_pos);
-
- DBG((si->dbg, LEVEL_2, "\t inserting mem copy for value %+F after %+F\n", arg, insert_pos));
-
- spill = be_spill2(arch_env, arg, insert_pos, arg);
-
- del_pset(remats);
-
- return spill;
-}
-
/**
* @param before The Phi node which has to be spilled
*/
NEW_ARR_A(ir_node*, ins, get_irn_arity(phi));
-#ifndef NO_MEMCOPIES
- for(n=get_irn_arity(phi)-1; n>=0; --n) {
- ir_node *arg = get_irn_n(phi, n);
- ir_node *bb = get_Block_cfgpred_block(get_nodes_block(phi), n);
- lpp_name_t *name = si->lpp->vars[op->attr.live_range.args.copies[n]];
-
- if(!is_zero(name->value)) {
- ins[n] = insert_mem_copy(si, bb, arg);
- } else {
- ins[n] = si->m_unknown;
- }
- }
-#else
for(n=get_irn_arity(phi)-1; n>=0; --n) {
ins[n] = si->m_unknown;
}
-#endif
mem_phi = new_r_Phi(si->chordal_env->irg, get_nodes_block(phi), get_irn_arity(phi), ins, mode_M);
defs->remats = remat;
}
-#if 0
-static void
-collect_spills(spill_ilp_t * si, ir_node * value, pset * spills, pset * visited)
-{
- ir_node *next;
- defs_t *defs;
-
- defs = set_find_def(si->values, value);
-
- if(defs && defs->spills) {
- for(next = defs->spills; next; next = get_irn_link(next)) {
- pset_insert_ptr(spills, next);
- }
- } else if (is_Phi(value)) {
- /* recursion */
- if(!pset_find_ptr(visited, value)) {
- int i,
- n;
-
- pset_insert_ptr(visited, value);
- for(i=0, n=get_irn_arity(value); i<n; ++i) {
- ir_node *arg = get_irn_n(value, i);
-
- collect_spills(si, arg, spills, visited);
- }
- }
- } else {
-// assert(0 && "Phi operand not spilled");
- }
-}
-#endif
/**
* Add reload before operation and add to list of defs
DBG((si->dbg, LEVEL_3, "\t inserting reload for value %+F before %+F\n", value, after));
defs = set_find_def(si->values, value);
- /* get a spill of this value */
-#if 0
- if((!defs || !defs->spills) && is_Phi(value)) {
- pset *spills;
-
- spills = get_spills_for_value(si, value);
- spill = pset_first(spills);
- del_pset(spills);
-
- if(!defs) {
- defs = set_insert_def(si->values, value);
- }
- defs->spills = spill;
- set_irn_link(spill, NULL);
- } else {
- spill = defs->spills;
- }
-#endif
spill = defs->spills;
assert(spill && "no spill placed before reload");
del_pset(spills_to_do);
}
+static ir_node *
+insert_mem_copy(spill_ilp_t * si, const ir_node * bb, const 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;
+
+ /* find last definition of arg value in block */
+ ir_node *next;
+ defs_t *defs;
+ int last = 0;
+
+ defs = set_find_def(si->values, value);
+
+ if(defs && defs->remats) {
+ for(next = defs->remats; next; next = get_irn_link(next)) {
+ if(get_nodes_block(next) == bb && sched_get_time_step(next) > last) {
+ last = sched_get_time_step(next);
+ insert_pos = next;
+ }
+ }
+ }
+
+ if(get_nodes_block(value) == bb && sched_get_time_step(value) > last) {
+ last = sched_get_time_step(value);
+ insert_pos = value;
+ }
+
+ DBG((si->dbg, LEVEL_2, "\t inserting mem copy for value %+F after %+F\n", value, insert_pos));
+
+ spill = be_spill2(arch_env, is_Block(insert_pos)?value:insert_pos, insert_pos, value);
+
+ return spill;
+}
static void
phim_fixer(spill_ilp_t *si) {
set_foreach(si->values, defs) {
const ir_node *phi = defs->value;
- ir_node *phi_m = NULL;
- ir_node *next = defs->spills;
- int n;
+ op_t *op = get_irn_link(phi);
+ ir_node *phi_m = NULL;
+ ir_node *next = defs->spills;
+ int n;
if(!is_Phi(phi)) continue;
/* get a spill of this value */
ir_node *spill = val_defs->spills;
- assert(spill && "no spill placed before PhiM");
- if(is_Unknown(arg))
- set_irn_n(phi_m, n, spill);
+#ifndef NO_MEMCOPIES
+ ir_node *pred = get_Block_cfgpred_block(get_nodes_block(phi), n);
+ lpp_name_t *name = si->lpp->vars[op->attr.live_range.args.copies[n]];
+
+ if(!is_zero(name->value)) {
+ spill = insert_mem_copy(si, pred, value);
+ } else {
+ assert(spill && "no spill placed before PhiM");
+ }
+#else
+ assert(spill && "no spill placed before PhiM");
+#endif
+ set_irn_n(phi_m, n, spill);
}
}
}
prev_op = get_irn_link(prev);
}
-// /* insert reload before pre-remats */
-// while(!sched_is_end(prev) && !be_is_Reload(prev) //FIXME && !be_is_Spill(prev)
-// && !is_Phi(prev) && prev_op->is_remat && prev_op->attr.remat.pre) {
-// insert_pos = prev;
-//
-// prev = sched_prev(insert_pos);
-// prev_op = get_irn_link(prev);
-// }
reload = insert_reload(si, irn, insert_pos);
if(!lc_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)));
+ DBG((kh->si->dbg, LEVEL_1, "\t SUBOPTIMAL! %+F IS UNUSED (cost: %g)\n", irn, get_cost(kh->si, irn)*execution_frequency(bb)));
#if 0
assert(lpp_get_sol_state(kh->si->lpp) != lpp_optimal && "optimal solution is suboptimal?");
#endif
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);
- phim_fixer(si);
irg_block_walk_graph(si->chordal_env->irg, walker_reload_placer, NULL, si);
+ phim_fixer(si);
/* clean the remat info! there are still back-edges leading there! */
clean_remat_info(si);
/* a and b are only interesting if they are in the same phi class */
if(get_phi_class(a) == get_phi_class(b)) {
if(values_interfere_in_block(bb, a, b)) {
- ir_fprintf(stderr, "Spills interfere in %+F: %+F, %+F\n", bb, a, b);
+ ir_fprintf(stderr, "$$ Spills interfere in %+F: %+F, %+F \t$$\n", bb, a, b);
}
}
}
si.spills = pset_new_ptr_default();
si.inverse_ops = pset_new_ptr_default();
#ifndef EXECFREQ_LOOPDEPH
- si.execfreqs = compute_execfreq(chordal_env->irg);
+ compute_execfreq(chordal_env->irg, LOOP_WEIGHT);
#else
si.execfreqs = NULL;
#endif
// move reloads upwards
be_liveness(chordal_env->irg);
- //irg_block_walk_graph(chordal_env->irg, walker_pressure_annotator, NULL, &si);
- //move_reloads_upward(&si);
+ irg_block_walk_graph(chordal_env->irg, walker_pressure_annotator, NULL, &si);
+ move_reloads_upward(&si);
#ifndef NO_MEMCOPIES
verify_phiclasses(&si);
del_pset(si.all_possible_remats);
del_pset(si.spills);
#ifndef EXECFREQ_LOOPDEPH
- free_execfreq(si.execfreqs);
+ free_execfreq();
#endif
free_lpp(si.lpp);
obstack_free(&obst, NULL);