/**
* @file
- * @brief Naiv spilling algorithm
+ * @brief Naive spilling algorithm
* @author Matthias Braun
* @date 20.09.2005
* @version $Id: bespillbelady.c 13913 2007-05-18 12:48:56Z matze $
- * @summary
- * This implements a naiv spilling algorithm. It is design to produce similar
+ * @brief
+ * This implements a naive spilling algorithm. It is designed to produce similar
* effects to the spill decisions produced by traditional graph coloring
* register allocators that spill while they are coloring the graph.
*
* Spilling in this context means placing a spill instruction behind the
* definition of the value and a reload before each usage.
*/
-#ifdef HAVE_CONFIG_H
#include "config.h"
-#endif
#include "debug.h"
#include "error.h"
#include "beirg.h"
-#include "bespilloptions.h"
#include "bespill.h"
+#include "bespillutil.h"
#include "bemodule.h"
#include "besched.h"
-#include "bearch_t.h"
+#include "bearch.h"
#include "be_t.h"
-#include "benode_t.h"
+#include "benode.h"
#include "beirg.h"
+#include "belive.h"
DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
static spill_env_t *spill_env;
static int n_regs;
-static const arch_env_t *arch_env;
static const arch_register_class_t *cls;
static const be_lv_t *lv;
static bitset_t *spilled_nodes;
ir_node *use = get_edge_src_irn(edge);
/* keeps should be directly below the node */
- if(be_is_Keep(use)) {
+ if (be_is_Keep(use)) {
continue;
}
- if(is_Phi(use)) {
+ if (is_Phi(use)) {
int in = get_edge_src_pos(edge);
ir_node *block = get_nodes_block(use);
foreach_out_edge(node, edge) {
ir_node *use = get_edge_src_irn(edge);
- if(is_Anchor(use))
+ if (is_Anchor(use))
continue;
- if(be_is_Keep(use))
+ if (be_is_Keep(use))
continue;
- if(is_Phi(use)) {
+ if (is_Phi(use)) {
int in = get_edge_src_pos(edge);
ir_node *block = get_nodes_block(use);
ir_node *n;
/* mode_T nodes define several values at once. Count them */
- if(get_irn_mode(node) == mode_T) {
+ if (get_irn_mode(node) == mode_T) {
const ir_edge_t *edge;
foreach_out_edge(node, edge) {
const ir_node *proj = get_edge_src_irn(edge);
- if(arch_irn_consider_in_reg_alloc(arch_env, cls, proj)) {
+ if (arch_irn_consider_in_reg_alloc(cls, proj)) {
++values_defined;
}
}
- } else if(arch_irn_consider_in_reg_alloc(arch_env, cls, node)) {
+ } else if (arch_irn_consider_in_reg_alloc(cls, node)) {
++values_defined;
}
/* we need registers for the non-live argument values */
arity = get_irn_arity(node);
- for(i = 0; i < arity; ++i) {
+ for (i = 0; i < arity; ++i) {
ir_node *pred = get_irn_n(node, i);
- if(arch_irn_consider_in_reg_alloc(arch_env, cls, pred)
+ if (arch_irn_consider_in_reg_alloc(cls, pred)
&& !ir_nodeset_contains(live_nodes, pred)) {
++free_regs_needed;
}
/* we can reuse all reloaded values for the defined values, but we might
need even more registers */
- if(values_defined > free_regs_needed)
+ if (values_defined > free_regs_needed)
free_regs_needed = values_defined;
spills_needed = (n_live_nodes + free_regs_needed) - n_regs;
- if(spills_needed <= 0)
+ if (spills_needed <= 0)
return;
DBG((dbg, LEVEL_2, "\tspills needed after %+F: %d\n", node, spills_needed));
- candidates = alloca(n_live_nodes * sizeof(candidates[0]));
+ candidates = ALLOCAN(spill_candidate_t, n_live_nodes);
/* construct array with spill candidates and calculate their costs */
i = 0;
/* spill cheapest ones */
cand_idx = 0;
- while(spills_needed > 0) {
+ while (spills_needed > 0) {
spill_candidate_t *candidate;
ir_node *cand_node;
int is_use;
if (cand_idx >= n_live_nodes) {
- panic("can't spill enough values for node %+F\n", node);
+ panic("can't spill enough values for node %+F", node);
}
cand_node = candidate->node;
++cand_idx;
- if(arch_irn_is(arch_env, cand_node, dont_spill))
+ if (arch_irn_is(skip_Proj_const(cand_node), dont_spill))
continue;
/* make sure the node is not an argument of the instruction */
is_use = 0;
for (i = 0; i < arity; ++i) {
ir_node *in = get_irn_n(node, i);
- if(in == cand_node) {
+ if (in == cand_node) {
is_use = 1;
break;
}
}
- if(is_use) {
+ if (is_use) {
continue;
}
foreach_out_edge(node, edge) {
const ir_node *proj = get_edge_src_irn(edge);
- if (arch_irn_consider_in_reg_alloc(arch_env, cls, proj)) {
+ if (arch_irn_consider_in_reg_alloc(cls, proj)) {
ir_nodeset_remove(nodeset, proj);
}
}
}
- if(arch_irn_consider_in_reg_alloc(arch_env, cls, node)) {
- ir_nodeset_remove(nodeset, node);
- }
+ if (arch_irn_consider_in_reg_alloc(cls, node)) {
+ ir_nodeset_remove(nodeset, node);
+ }
}
static void add_uses(ir_node *node, ir_nodeset_t *nodeset)
{
int i, arity;
- arity = get_irn_arity(node);
- for(i = 0; i < arity; ++i) {
- ir_node *op = get_irn_n(node, i);
+ arity = get_irn_arity(node);
+ for (i = 0; i < arity; ++i) {
+ ir_node *op = get_irn_n(node, i);
- if(arch_irn_consider_in_reg_alloc(arch_env, cls, op)
- && !bitset_is_set(spilled_nodes, get_irn_idx(op))) {
- ir_nodeset_insert(nodeset, op);
+ if (arch_irn_consider_in_reg_alloc(cls, op) &&
+ !bitset_is_set(spilled_nodes, get_irn_idx(op))) {
+ ir_nodeset_insert(nodeset, op);
}
- }
+ }
}
static __attribute__((unused))
/* construct set of live nodes at end of block */
ir_nodeset_init(&live_nodes);
- be_liveness_end_of_block(lv, arch_env, cls, block, &live_nodes);
+ be_liveness_end_of_block(lv, cls, block, &live_nodes);
/* remove already spilled nodes from liveset */
foreach_ir_nodeset(&live_nodes, node, iter) {
DBG((dbg, LEVEL_2, "\t%+F is live-end... ", node));
- if(bitset_is_set(spilled_nodes, get_irn_idx(node))) {
+ if (bitset_is_set(spilled_nodes, get_irn_idx(node))) {
DBG((dbg, LEVEL_2, "but spilled; removing.\n"));
ir_nodeset_remove_iterator(&live_nodes, &iter);
} else {
/* walk schedule backwards and spill until register pressure is fine at
* each node */
sched_foreach_reverse(block, node) {
- if(is_Phi(node))
+ if (is_Phi(node))
break;
remove_defs(node, &live_nodes);
*/
n_phi_values_spilled = 0;
sched_foreach(block, node) {
- if(!is_Phi(node))
+ if (!is_Phi(node))
break;
- if(bitset_is_set(spilled_nodes, get_irn_idx(node))) {
+ if (bitset_is_set(spilled_nodes, get_irn_idx(node))) {
++n_phi_values_spilled;
}
}
/* TODO: we should really estimate costs of the phi spill as well...
* and preferably spill phis with lower costs... */
sched_foreach(block, node) {
- if(!is_Phi(node))
+ if (!is_Phi(node))
break;
- if(phi_spills_needed <= 0)
+ if (phi_spills_needed <= 0)
break;
- if(bitset_is_set(spilled_nodes, get_irn_idx(node))) {
+ if (bitset_is_set(spilled_nodes, get_irn_idx(node))) {
be_spill_phi(spill_env, node);
--phi_spills_needed;
}
ir_graph *irg = be_get_birg_irg(birg);
n_regs = new_cls->n_regs - be_put_ignore_regs(birg, new_cls, NULL);
- if(n_regs == 0)
+ if (n_regs == 0)
return;
be_liveness_assure_sets(be_assure_liveness(birg));
spill_env = be_new_spill_env(birg);
- arch_env = be_get_birg_arch_env(birg);
cls = new_cls;
lv = be_get_birg_liveness(birg);
spilled_nodes = bitset_malloc(get_irg_last_idx(irg));