2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Spill module selection; Preparation steps
9 * @author Matthias Braun
16 #include "iredges_t.h"
17 #include "raw_bitset.h"
30 #include "beintlive_t.h"
33 #include "lc_opts_enum.h"
35 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
37 typedef struct be_pre_spill_env_t {
39 const arch_register_class_t *cls;
42 static void prepare_constr_insn(be_pre_spill_env_t *env, ir_node *node)
44 const arch_register_class_t *cls = env->cls;
45 ir_node *block = get_nodes_block(node);
46 const ir_graph *irg = env->irg;
47 be_irg_t *birg = be_birg_from_irg(irg);
48 be_lv_t *lv = be_get_irg_liveness(irg);
49 unsigned *def_constr = NULL;
50 int arity = get_irn_arity(node);
52 /* Insert a copy for constraint inputs attached to a value which can't
53 * fulfill the constraint
54 * (typical example: stack pointer as input to copyb)
55 * TODO: This really just checks precolored registers at the moment and
56 * ignores the general case of not matching in/out constraints
58 for (int i = 0; i < arity; ++i) {
59 ir_node *op = get_irn_n(node, i);
60 const arch_register_req_t *req = arch_get_irn_register_req_in(node, i);
61 const arch_register_t *reg;
66 reg = arch_get_irn_register(op);
70 /* Precolored with an ignore register (which is not virtual). */
71 if (reg->type & arch_register_type_virtual ||
72 rbitset_is_set(birg->allocatable_regs, reg->global_index))
75 if (!arch_register_req_is(req, limited))
77 if (rbitset_is_set(req->limited, reg->index))
80 copy = be_new_Copy(block, op);
81 stat_ev_int("constr_copy", 1);
82 sched_add_before(node, copy);
83 set_irn_n(node, i, copy);
84 DBG((dbg, LEVEL_3, "inserting ignore arg copy %+F for %+F pos %d\n",
88 /* insert copies for nodes that occur constrained more than once. */
89 be_foreach_use(node, cls, req, in, in_req_,
90 if (!arch_register_req_is(req, limited))
93 for (int i2 = i_ + 1; i2 < arity; ++i2) {
95 const arch_register_req_t *req2;
97 req2 = arch_get_irn_register_req_in(node, i2);
100 if (!arch_register_req_is(req2, limited))
103 in2 = get_irn_n(node, i2);
107 /* if the constraint is the same, no copy is necessary
108 * TODO generalise unequal but overlapping constraints */
109 if (rbitsets_equal(req->limited, req2->limited, cls->n_regs))
112 ir_node *copy = be_new_Copy(block, in);
113 stat_ev_int("constr_copy", 1);
115 sched_add_before(node, copy);
116 set_irn_n(node, i2, copy);
118 "inserting multiple constr copy %+F for %+F pos %d\n",
123 /* collect all registers occurring in out constraints. */
124 be_foreach_definition(node, cls, def, req,
126 if (!arch_register_req_is(req, limited))
128 if (def_constr == NULL) {
129 def_constr = rbitset_alloca(cls->n_regs);
131 rbitset_or(def_constr, req->limited, cls->n_regs);
134 /* no output constraints => we're good */
135 if (def_constr == NULL) {
140 * insert copies for all constrained arguments living through the node
141 * and being constrained to a register which also occurs in out constraints.
143 unsigned *const tmp = rbitset_alloca(cls->n_regs);
144 be_foreach_use(node, cls, req, in, in_req_,
146 * 1) the operand is constrained.
147 * 2) lives through the node.
148 * 3) is constrained to a register occurring in out constraints.
150 if (!arch_register_req_is(req, limited))
152 if (!be_values_interfere(lv, node, in))
155 rbitset_copy(tmp, req->limited, cls->n_regs);
156 rbitset_and(tmp, def_constr, cls->n_regs);
158 if (rbitset_is_empty(tmp, cls->n_regs))
162 * only create the copy if the operand is no copy.
163 * this is necessary since the assure constraints phase inserts
164 * Copies and Keeps for operands which must be different from the
165 * results. Additional copies here would destroy this.
170 ir_node *copy = be_new_Copy(block, in);
171 sched_add_before(node, copy);
172 set_irn_n(node, i_, copy);
173 DBG((dbg, LEVEL_3, "inserting constr copy %+F for %+F pos %d\n",
175 be_liveness_update(lv, in);
179 static void pre_spill_prepare_constr_walker(ir_node *block, void *data)
181 be_pre_spill_env_t *env = (be_pre_spill_env_t*)data;
182 sched_foreach(block, node) {
183 prepare_constr_insn(env, node);
187 void be_pre_spill_prepare_constr(ir_graph *irg,
188 const arch_register_class_t *cls)
190 be_pre_spill_env_t env;
191 memset(&env, 0, sizeof(env));
195 be_assure_live_sets(irg);
197 irg_block_walk_graph(irg, pre_spill_prepare_constr_walker, NULL, &env);
202 int be_coalesce_spill_slots = 1;
203 int be_do_remats = 1;
205 static const lc_opt_table_entry_t be_spill_options[] = {
206 LC_OPT_ENT_BOOL ("coalesce_slots", "coalesce the spill slots", &be_coalesce_spill_slots),
207 LC_OPT_ENT_BOOL ("remat", "try to rematerialize values instead of reloading", &be_do_remats),
211 static be_module_list_entry_t *spillers = NULL;
212 static const be_spiller_t *selected_spiller = NULL;
214 void be_register_spiller(const char *name, be_spiller_t *spiller)
216 if (selected_spiller == NULL)
217 selected_spiller = spiller;
218 be_add_module_to_list(&spillers, name, spiller);
221 void be_do_spill(ir_graph *irg, const arch_register_class_t *cls)
223 assert(selected_spiller != NULL);
225 selected_spiller->spill(irg, cls);
228 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spilloptions)
229 void be_init_spilloptions(void)
231 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
232 lc_opt_entry_t *spill_grp = lc_opt_get_grp(be_grp, "spill");
234 lc_opt_add_table(spill_grp, be_spill_options);
235 be_add_module_list_opt(be_grp, "spiller", "spill algorithm",
236 &spillers, (void**) &selected_spiller);
238 FIRM_DBG_REGISTER(dbg, "firm.be.spillprepare");