From 44d51ff21a4a48494fd8e42dd10f5b489b4fca78 Mon Sep 17 00:00:00 2001 From: Matthias Braun Date: Wed, 23 Jul 2008 17:58:28 +0000 Subject: [PATCH] belady fixes/cleanups [r20634] --- ir/be/bespill.c | 85 +++++++++----------- ir/be/bespill.h | 2 +- ir/be/bespillbelady.c | 175 ++++++++++++++++++++++++++++------------- ir/be/bespillbelady2.c | 2 +- ir/be/beverify.c | 10 +-- 5 files changed, 163 insertions(+), 111 deletions(-) diff --git a/ir/be/bespill.c b/ir/be/bespill.c index 78ff1482b..edebe354f 100644 --- a/ir/be/bespill.c +++ b/ir/be/bespill.c @@ -29,6 +29,7 @@ #endif #include +#include #include "pset.h" #include "irnode_t.h" @@ -80,7 +81,7 @@ struct reloader_t { typedef struct spill_t spill_t; struct spill_t { spill_t *next; - ir_node *before; /**< spill has to be placed before this node (or earlier) */ + ir_node *after; /**< spill has to be placed after this node (or earlier) */ ir_node *spill; }; @@ -107,8 +108,6 @@ struct spill_env_t { placed */ ir_nodeset_t mem_phis; /**< set of all spilled phis. */ ir_exec_freq *exec_freq; - unsigned new_nodes_idx; /**< all old nodes idx is smaller than - this */ #ifdef FIRM_STATISTICS unsigned spill_count; @@ -194,32 +193,31 @@ void be_delete_spill_env(spill_env_t *env) * */ -void be_add_spill(spill_env_t *env, ir_node *to_spill, ir_node *before) +void be_add_spill(spill_env_t *env, ir_node *to_spill, ir_node *after) { -#if 1 spill_info_t *spill_info = get_spillinfo(env, to_spill); spill_t *spill; spill_t *s; spill_t *last; assert(! arch_irn_is(env->arch_env, to_spill, dont_spill)); - DB((dbg, LEVEL_1, "Add spill of %+F before %+F\n", to_spill, before)); + DB((dbg, LEVEL_1, "Add spill of %+F after %+F\n", to_spill, after)); /* Just for safety make sure that we do not insert the spill in front of a phi */ - for (; is_Phi(before); before = sched_next(before)); + assert(!is_Phi(sched_next(after))); /* spills that are dominated by others are not needed */ last = NULL; s = spill_info->spills; for( ; s != NULL; s = s->next) { /* no need to add this spill if it is dominated by another */ - if(value_dominates(s->before, before)) { - DB((dbg, LEVEL_1, "...dominated by %+F, not added\n", s->before)); + if(value_dominates(s->after, after)) { + DB((dbg, LEVEL_1, "...dominated by %+F, not added\n", s->after)); return; } /* remove spills that we dominate */ - if(value_dominates(before, s->before)) { - DB((dbg, LEVEL_1, "...remove old spill at %+F\n", s->before)); + if(value_dominates(after, s->after)) { + DB((dbg, LEVEL_1, "...remove old spill at %+F\n", s->after)); if(last != NULL) { last->next = s->next; } else { @@ -231,12 +229,11 @@ void be_add_spill(spill_env_t *env, ir_node *to_spill, ir_node *before) } spill = obstack_alloc(&env->obst, sizeof(spill[0])); - spill->before = before; + spill->after = after; spill->next = spill_info->spills; spill->spill = NULL; spill_info->spills = spill; -#endif } void be_add_remat(spill_env_t *env, ir_node *to_spill, ir_node *before, @@ -326,9 +323,11 @@ ir_node *be_get_end_of_block_insertion_point(const ir_node *block) static ir_node *skip_keeps_phis(ir_node *node) { - node = sched_next(node); - while(is_Phi(node) || be_is_Keep(node)) { - node = sched_next(node); + while(true) { + ir_node *next = sched_next(node); + if(!is_Phi(next) && !be_is_Keep(next)) + break; + node = next; } return node; } @@ -385,7 +384,7 @@ void be_spill_phi(spill_env_t *env, ir_node *node) block = get_nodes_block(node); spill = get_spillinfo(env, node); for(i = 0, arity = get_irn_arity(node); i < arity; ++i) { - ir_node *arg = get_irn_n(node, i); + ir_node *arg = get_irn_n(node, i); ir_node *insert; //get_spillinfo(env, arg); @@ -394,6 +393,7 @@ void be_spill_phi(spill_env_t *env, ir_node *node) if(!sched_is_scheduled(arg)) { ir_node *pred_block = get_Block_cfgpred_block(block, i); insert = be_get_end_of_block_insertion_point(pred_block); + insert = sched_prev(insert); } else { insert = skip_keeps_phis(arg); } @@ -442,19 +442,14 @@ static void spill_irn(spill_env_t *env, spill_info_t *spillinfo) DBG((dbg, LEVEL_1, "spilling %+F ... \n", to_spill)); spill = spillinfo->spills; for( ; spill != NULL; spill = spill->next) { - ir_node *block = get_block(spill->before); - ir_node *before = spill->before; - - /* place all spills before the reloads (as we can't guarantee the - * same order as the be_add_spill and be_add_reload calls. - * Also make sure that we do not run into Phis when going up. */ - while(get_irn_idx(sched_prev(before)) > env->new_nodes_idx && !is_Phi(sched_prev(before))) { - before = sched_prev(before); - } + ir_node *after = spill->after; + ir_node *block = get_block(after); + + after = skip_keeps_phis(after); - spill->spill = be_spill(env->arch_env, block, to_spill); - sched_add_before(before, spill->spill); - DB((dbg, LEVEL_1, "\t%+F before %+F\n", spill->spill, before)); + spill->spill = be_spill(env->arch_env, block, to_spill); + sched_add_after(after, spill->spill); + DB((dbg, LEVEL_1, "\t%+F after %+F\n", spill->spill, after)); #ifdef FIRM_STATISTICS env->spill_count++; #endif @@ -499,7 +494,7 @@ static void spill_phi(spill_env_t *env, spill_info_t *spillinfo) /* override or replace spills list... */ spill = obstack_alloc(&env->obst, sizeof(spill[0])); - spill->before = skip_keeps_phis(phi); + spill->after = skip_keeps_phis(phi); spill->spill = new_r_Phi(irg, block, arity, ins, mode_M); spill->next = NULL; @@ -819,9 +814,9 @@ static void determine_spill_costs(spill_env_t *env, spill_info_t *spillinfo) if(!sched_is_scheduled(to_spill)) { /* override spillinfos or create a new one */ spill_t *spill = obstack_alloc(&env->obst, sizeof(spill[0])); - spill->before = NULL; - spill->next = NULL; - spill->spill = new_NoMem(); + spill->after = NULL; + spill->next = NULL; + spill->spill = new_NoMem(); spillinfo->spills = spill; spillinfo->spill_costs = 0; @@ -848,13 +843,8 @@ static void determine_spill_costs(spill_env_t *env, spill_info_t *spillinfo) spills_execfreq = 0; s = spillinfo->spills; for( ; s != NULL; s = s->next) { - ir_node *spill_block = s->before; - double freq; - - if(!is_Block(spill_block)) { - spill_block = get_nodes_block(spill_block); - } - freq = get_block_execfreq(env->exec_freq, spill_block); + ir_node *spill_block = get_block(s->after); + double freq = get_block_execfreq(env->exec_freq, spill_block); spills_execfreq += freq; } @@ -872,10 +862,10 @@ static void determine_spill_costs(spill_env_t *env, spill_info_t *spillinfo) } /* override spillinfos or create a new one */ - spill = obstack_alloc(&env->obst, sizeof(spill[0])); - spill->before = skip_keeps_phis(to_spill); - spill->next = NULL; - spill->spill = NULL; + spill = obstack_alloc(&env->obst, sizeof(spill[0])); + spill->after = skip_keeps_phis(to_spill); + spill->next = NULL; + spill->spill = NULL; spillinfo->spills = spill; spillinfo->spill_costs = spill_execfreq * env->spill_cost; @@ -905,7 +895,7 @@ void make_spill_locations_dominate_irn(spill_env_t *env, ir_node *irn) * If the bitset is not empty after that, we have reloads that are * not dominated by any spill. */ for (s = si->spills; s != NULL; s = s->next) { - ir_node *bl = get_nodes_block(s->before); + ir_node *bl = get_nodes_block(s->after); int start = get_Block_dom_tree_pre_num(bl); int end = get_Block_dom_max_subtree_pre_num(bl); @@ -913,12 +903,11 @@ void make_spill_locations_dominate_irn(spill_env_t *env, ir_node *irn) } if (!bitset_is_empty(reloads)) - be_add_spill(env, si->to_spill, sched_next(si->to_spill)); + be_add_spill(env, si->to_spill, si->to_spill); } void be_insert_spills_reloads(spill_env_t *env) { - ir_graph *irg = env->irg; const arch_env_t *arch_env = env->arch_env; const ir_exec_freq *exec_freq = env->exec_freq; spill_info_t *si; @@ -927,8 +916,6 @@ void be_insert_spills_reloads(spill_env_t *env) BE_TIMER_PUSH(t_ra_spill_apply); - env->new_nodes_idx = get_irg_last_idx(irg); - /* create all phi-ms first, this is needed so, that phis, hanging on spilled phis work correctly */ foreach_ir_nodeset(&env->mem_phis, node, iter) { diff --git a/ir/be/bespill.h b/ir/be/bespill.h index e44a4634a..018f52f1e 100644 --- a/ir/be/bespill.h +++ b/ir/be/bespill.h @@ -54,7 +54,7 @@ ir_node *be_get_end_of_block_insertion_point(const ir_node *block); /** * Marks a point until which a node must be spilled. */ -void be_add_spill(spill_env_t *senv, ir_node *to_spill, ir_node *before); +void be_add_spill(spill_env_t *senv, ir_node *to_spill, ir_node *after); /** * Inserts a new entry into the list of reloads to place (the real nodes will diff --git a/ir/be/bespillbelady.c b/ir/be/bespillbelady.c index f9f9dbca7..eaf93d036 100644 --- a/ir/be/bespillbelady.c +++ b/ir/be/bespillbelady.c @@ -220,16 +220,17 @@ static INLINE void workset_remove(workset_t *workset, ir_node *val) } } -static INLINE int workset_contains(const workset_t *ws, const ir_node *val) +static INLINE const loc_t *workset_contains(const workset_t *ws, + const ir_node *val) { int i; - for(i=0; ilen; ++i) { + for (i = 0; i < ws->len; ++i) { if (ws->vals[i].node == val) - return 1; + return &ws->vals[i]; } - return 0; + return NULL; } /** @@ -307,6 +308,7 @@ static INLINE unsigned get_distance(ir_node *from, unsigned from_step, static void displace(workset_t *new_vals, int is_usage) { ir_node **to_insert = alloca(n_regs * sizeof(to_insert[0])); + bool *spilled = alloca(n_regs * sizeof(spilled[0])); ir_node *val; int i; int len; @@ -317,6 +319,8 @@ static void displace(workset_t *new_vals, int is_usage) /* 1. Identify the number of needed slots and the values to reload */ demand = 0; workset_foreach(new_vals, val, iter) { + bool reloaded = false; + /* mark value as used */ if (is_usage) ir_nodeset_insert(&used, val); @@ -326,6 +330,7 @@ static void displace(workset_t *new_vals, int is_usage) if (is_usage) { DB((dbg, DBG_SPILL, "Reload %+F before %+F\n", val, instr)); be_add_reload(senv, val, instr, cls, 1); + reloaded = true; } } else { DB((dbg, DBG_DECIDE, " %+F already in workset\n", val)); @@ -334,7 +339,9 @@ static void displace(workset_t *new_vals, int is_usage) * spilled */ workset_remove(ws, val); } - to_insert[demand++] = val; + spilled[demand] = reloaded; + to_insert[demand] = val; + ++demand; } /* 2. Make room for at least 'demand' slots */ @@ -344,8 +351,10 @@ static void displace(workset_t *new_vals, int is_usage) /* Only make more free room if we do not have enough */ if (spills_needed > 0) { +#ifndef PLACE_SPILLS ir_node *curr_bb = get_nodes_block(instr); workset_t *ws_start = get_block_info(curr_bb)->start_workset; +#endif DB((dbg, DBG_DECIDE, " disposing %d values\n", spills_needed)); @@ -359,9 +368,6 @@ static void displace(workset_t *new_vals, int is_usage) /* sort entries by increasing nextuse-distance*/ workset_sort(ws); - /* Logic for not needed live-ins: If a value is disposed - * before its first usage, remove it from start workset - * We don't do this for phis though */ for (i = len - spills_needed; i < len; ++i) { ir_node *val = ws->vals[i].node; @@ -370,14 +376,20 @@ static void displace(workset_t *new_vals, int is_usage) #ifdef PLACE_SPILLS if(!USES_IS_INFINITE(ws->vals[i].time) && !ws->vals[i].spilled) { - be_add_spill(senv, val, instr); + ir_node *after_pos = sched_prev(instr); + be_add_spill(senv, val, after_pos); } #endif +#ifndef PLACE_SPILLS + /* Logic for not needed live-ins: If a value is disposed + * before its first use, remove it from start workset + * We don't do this for phis though */ if (!is_Phi(val) && ! ir_nodeset_contains(&used, val)) { workset_remove(ws_start, val); DB((dbg, DBG_DECIDE, " (and removing %+F from start workset)\n", val)); } +#endif } /* kill the last 'demand' entries in the array */ @@ -388,7 +400,7 @@ static void displace(workset_t *new_vals, int is_usage) for (i = 0; i < demand; ++i) { ir_node *val = to_insert[i]; - workset_insert(ws, val, false); + workset_insert(ws, val, spilled[i]); } } @@ -579,9 +591,8 @@ static void decide_start_workset(const ir_node *block) DEL_ARR_F(starters); /* determine spill status of the values: If there's 1 pred block (which - * is no backedge) where the value is reloaded then we must set it to - * reloaded here. We place spills in all pred where the value was not yet - * reloaded to be sure we have a spill on each path */ + * is no backedge) where the value is spilled then we must set it to + * spilled here. */ arity = get_irn_arity(block); pred_worksets = alloca(sizeof(pred_worksets[0]) * arity); for(i = 0; i < arity; ++i) { @@ -626,49 +637,87 @@ static void decide_start_workset(const ir_node *block) if (l->spilled) { spilled = true; - goto determined_spill; } break; } - if (p >= p_len) { - spilled = true; - goto determined_spill; - } } -determined_spill: - if (spilled) { - for (n = 0; n < arity; ++n) { - workset_t *pred_workset = pred_worksets[n]; - int p_len; - int p; + loc->spilled = spilled; + } +} + +#if 0 +static void decide_start_workset2(const ir_node *block) +{ + int arity; + workset_t **pred_worksets; + int p; + int len; + + /* check if all predecessors are known */ + arity = get_irn_arity(block); + pred_worksets = alloca(sizeof(pred_worksets[0]) * arity); + for (i = 0; i < arity; ++i) { + ir_node *pred_block = get_Block_cfgpred_block(block, i); + block_info_t *pred_info = get_block_info(pred_block); + + if (pred_info == NULL) { + /* not all predecessors known, use decide_start_workset */ + decide_start_workset(block); + return; + } + + pred_worksets[i] = pred_info->end_workset; + } + + /* take values live in all pred blocks */ + len = workset_get_length(pred_workset[0]); + for (p = 0; p < p_len; ++p) { + const loc_t *l = &pred_workset[0]->vals[p]; + ir_node *value; + bool spilled = false; + + if (USES_IS_INFINITE(l->time)) + continue; - if (pred_workset == NULL) + /* value available in all preds? */ + value = l->node; + for (i = 0; i < arity; ++i) { + bool found = false; + workset_t p_workset = &pred_worksets[i]; + int p_len = workset_get_length(p_workset); + int p_i; + + for (p_i = 0; p_i < p_len; ++p_i) { + const loc_t *p_l = &p_workset->vals[p_i]; + if (p_l->node != value) continue; - p_len = workset_get_length(pred_workset); - for (p = 0; p < p_len; ++p) { - loc_t *l = &pred_workset->vals[p]; - - if (l->node != value) - continue; - - if (!l->spilled) { - ir_node *pred_block = get_Block_cfgpred_block(block, n); - ir_node *insert_point - = be_get_end_of_block_insertion_point(pred_block); - DB((dbg, DBG_SPILL, "Spill %+F before %+F\n", node, - insert_point)); - be_add_spill(senv, value, insert_point); - } - break; - } + found = true; + if (p_l->spilled) + spilled = true; + break; } + + if (!found) + break; + } + + /* it was available in all preds, TODO: insert spills... */ + if (i >= arity) { + workset_insert(ws, value, spilled); } - loc->spilled = spilled; } + + + /* Copy the best ones from starters to start workset */ + ws_count = MIN(ARR_LEN(starters), n_regs); + workset_clear(ws); + workset_bulk_fill(ws, ws_count, starters); } +#endif + /** * For the given block @p block, decide for each values * whether it is used from a register or is reloaded @@ -855,23 +904,27 @@ static void fix_block_borders(ir_node *block, void *data) ir_node *insert_point; if (arity > 1) { insert_point = be_get_end_of_block_insertion_point(pred); + insert_point = sched_prev(insert_point); } else { - insert_point = sched_first(block); - assert(!is_Phi(insert_point)); + insert_point = block; } - DB((dbg, DBG_SPILL, "Spill %+F before %+F\n", node, + DB((dbg, DBG_SPILL, "Spill %+F after %+F\n", node, insert_point)); be_add_spill(senv, node, insert_point); } #endif } - /* reload missing values in predecessors */ + /* reload missing values in predecessors, add missing spills */ workset_foreach(start_workset, node, iter) { + const loc_t *l = &start_workset->vals[iter]; + const loc_t *pred_loc; + /* if node is a phi of the current block we reload * the corresponding argument, else node itself */ - if(is_Phi(node) && block == get_nodes_block(node)) { + if(is_Phi(node) && get_nodes_block(node) == block) { node = get_irn_n(node, i); + assert(!l->spilled); /* we might have unknowns as argument for the phi */ if(!arch_irn_consider_in_reg_alloc(arch_env, cls, node)) @@ -879,13 +932,25 @@ static void fix_block_borders(ir_node *block, void *data) } /* check if node is in a register at end of pred */ - if(workset_contains(pred_end_workset, node)) - continue; - - /* node is not in memory at the end of pred -> reload it */ - DB((dbg, DBG_FIX, " reload %+F\n", node)); - DB((dbg, DBG_SPILL, "Reload %+F before %+F,%d\n", node, block, i)); - be_add_reload_on_edge(senv, node, block, i, cls, 1); + pred_loc = workset_contains(pred_end_workset, node); + if (pred_loc != NULL) { +#ifdef PLACE_SPILLS + /* we might have to spill value on this path */ + if (!pred_loc->spilled && l->spilled) { + ir_node *insert_point + = be_get_end_of_block_insertion_point(pred); + insert_point = sched_prev(insert_point); + DB((dbg, DBG_SPILL, "Spill %+F after %+F\n", node, + insert_point)); + be_add_spill(senv, node, insert_point); + } +#endif + } else { + /* node is not in register at the end of pred -> reload it */ + DB((dbg, DBG_FIX, " reload %+F\n", node)); + DB((dbg, DBG_SPILL, "Reload %+F before %+F,%d\n", node, block, i)); + be_add_reload_on_edge(senv, node, block, i, cls, 1); + } } } } diff --git a/ir/be/bespillbelady2.c b/ir/be/bespillbelady2.c index f8fefd414..b48b8dc8d 100644 --- a/ir/be/bespillbelady2.c +++ b/ir/be/bespillbelady2.c @@ -1328,7 +1328,7 @@ static void optimize_variable(global_end_state_t *ges, bring_in_t *br) DBG((dbg, DBG_GLOBAL, "\t-> use blocked. local reload: %+F, try spill at: %+F\n", br->first_use, better_spill_loc)); be_add_reload(env->senv, irn, br->first_use, env->cls, 1); - be_add_spill(env->senv, irn, sched_next(better_spill_loc)); + be_add_spill(env->senv, irn, better_spill_loc); ir_nodeset_insert(env->extra_spilled, irn); } diff --git a/ir/be/beverify.c b/ir/be/beverify.c index ec2af73ef..eb37d272f 100644 --- a/ir/be/beverify.c +++ b/ir/be/beverify.c @@ -158,7 +158,7 @@ typedef struct be_verify_schedule_env_t_ { static void verify_schedule_walker(ir_node *block, void *data) { be_verify_schedule_env_t *env = (be_verify_schedule_env_t*) data; ir_node *node; - int non_phi_found = 0; + ir_node *non_phi_found = NULL; int cfchange_found = 0; /* TODO ask arch about delay branches */ int delay_branches = 0; @@ -200,13 +200,13 @@ static void verify_schedule_walker(ir_node *block, void *data) { /* Check that phis come before any other node */ if (is_Phi(node)) { - if (non_phi_found) { - ir_fprintf(stderr, "Verify Warning: Phi node %+F scheduled after non-Phi nodes in block %+F (%s)\n", - node, block, get_irg_dump_name(env->irg)); + if (non_phi_found != NULL) { + ir_fprintf(stderr, "Verify Warning: Phi node %+F scheduled after non-Phi nodes (for example %+F) in block %+F (%s)\n", + node, non_phi_found, block, get_irg_dump_name(env->irg)); env->problem_found = 1; } } else { - non_phi_found = 1; + non_phi_found = node; } /* Check for control flow changing nodes */ -- 2.20.1