2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Spill module selection; Preparation steps
23 * @author Matthias Braun
30 #include "iredges_t.h"
31 #include "raw_bitset.h"
44 #include "beintlive_t.h"
47 #include "lc_opts_enum.h"
49 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
51 typedef struct be_pre_spill_env_t {
53 const arch_register_class_t *cls;
56 static void prepare_constr_insn(be_pre_spill_env_t *env, ir_node *node)
58 const arch_register_class_t *cls = env->cls;
59 ir_node *block = get_nodes_block(node);
60 const ir_graph *irg = env->irg;
61 be_irg_t *birg = be_birg_from_irg(irg);
62 be_lv_t *lv = be_get_irg_liveness(irg);
63 unsigned *def_constr = NULL;
64 int arity = get_irn_arity(node);
68 /* Insert a copy for constraint inputs attached to a value which can't
69 * fulfill the constraint
70 * (typical example: stack pointer as input to copyb)
71 * TODO: This really just checks precolored registers at the moment and
72 * ignores the general case of not matching in/out constraints
74 for (i = 0; i < arity; ++i) {
75 ir_node *op = get_irn_n(node, i);
76 const arch_register_req_t *req = arch_get_irn_register_req_in(node, i);
77 const arch_register_t *reg;
82 reg = arch_get_irn_register(op);
86 /* Precolored with an ignore register (which is not virtual). */
87 if (reg->type & arch_register_type_virtual ||
88 rbitset_is_set(birg->allocatable_regs, reg->global_index))
91 if (!arch_register_req_is(req, limited))
93 if (rbitset_is_set(req->limited, reg->index))
96 copy = be_new_Copy(block, op);
97 stat_ev_int("constr_copy", 1);
98 sched_add_before(node, copy);
99 set_irn_n(node, i, copy);
100 DBG((dbg, LEVEL_3, "inserting ignore arg copy %+F for %+F pos %d\n",
104 /* insert copies for nodes that occur constrained more than once. */
105 for (i = 0; i < arity; ++i) {
108 const arch_register_req_t *req;
110 req = arch_get_irn_register_req_in(node, i);
114 if (!arch_register_req_is(req, limited))
117 in = get_irn_n(node, i);
118 if (!arch_irn_consider_in_reg_alloc(cls, in))
121 for (i2 = i + 1; i2 < arity; ++i2) {
123 const arch_register_req_t *req2;
125 req2 = arch_get_irn_register_req_in(node, i2);
126 if (req2->cls != cls)
128 if (!arch_register_req_is(req2, limited))
131 in2 = get_irn_n(node, i2);
135 /* if the constraint is the same, no copy is necessary
136 * TODO generalise unequal but overlapping constraints */
137 if (rbitsets_equal(req->limited, req2->limited, cls->n_regs))
140 copy = be_new_Copy(block, in);
141 stat_ev_int("constr_copy", 1);
143 sched_add_before(node, copy);
144 set_irn_n(node, i2, copy);
146 "inserting multiple constr copy %+F for %+F pos %d\n",
151 /* collect all registers occurring in out constraints. */
152 be_foreach_definition(node, cls, def,
154 if (!arch_register_req_is(req_, limited))
156 if (def_constr == NULL) {
157 def_constr = rbitset_alloca(cls->n_regs);
159 rbitset_or(def_constr, req_->limited, cls->n_regs);
162 /* no output constraints => we're good */
163 if (def_constr == NULL) {
168 * insert copies for all constrained arguments living through the node
169 * and being constrained to a register which also occurs in out constraints.
171 unsigned *const tmp = rbitset_alloca(cls->n_regs);
172 for (i = 0; i < arity; ++i) {
173 const arch_register_req_t *req;
179 * 1) the operand is constrained.
180 * 2) lives through the node.
181 * 3) is constrained to a register occurring in out constraints.
183 req = arch_get_irn_register_req_in(node, i);
186 if (!arch_register_req_is(req, limited))
189 in = get_irn_n(node, i);
190 if (!arch_irn_consider_in_reg_alloc(cls, in))
192 if (!be_values_interfere(lv, node, in))
195 rbitset_copy(tmp, req->limited, cls->n_regs);
196 rbitset_and(tmp, def_constr, cls->n_regs);
198 if (rbitset_is_empty(tmp, cls->n_regs))
202 * only create the copy if the operand is no copy.
203 * this is necessary since the assure constraints phase inserts
204 * Copies and Keeps for operands which must be different from the
205 * results. Additional copies here would destroy this.
210 copy = be_new_Copy(block, in);
211 sched_add_before(node, copy);
212 set_irn_n(node, i, copy);
213 DBG((dbg, LEVEL_3, "inserting constr copy %+F for %+F pos %d\n",
215 be_liveness_update(lv, in);
219 static void pre_spill_prepare_constr_walker(ir_node *block, void *data)
221 be_pre_spill_env_t *env = (be_pre_spill_env_t*)data;
222 sched_foreach(block, node) {
223 prepare_constr_insn(env, node);
227 void be_pre_spill_prepare_constr(ir_graph *irg,
228 const arch_register_class_t *cls)
230 be_pre_spill_env_t env;
231 memset(&env, 0, sizeof(env));
235 be_assure_live_sets(irg);
237 irg_block_walk_graph(irg, pre_spill_prepare_constr_walker, NULL, &env);
242 int be_coalesce_spill_slots = 1;
243 int be_do_remats = 1;
245 static const lc_opt_table_entry_t be_spill_options[] = {
246 LC_OPT_ENT_BOOL ("coalesce_slots", "coalesce the spill slots", &be_coalesce_spill_slots),
247 LC_OPT_ENT_BOOL ("remat", "try to rematerialize values instead of reloading", &be_do_remats),
251 static be_module_list_entry_t *spillers = NULL;
252 static const be_spiller_t *selected_spiller = NULL;
254 void be_register_spiller(const char *name, be_spiller_t *spiller)
256 if (selected_spiller == NULL)
257 selected_spiller = spiller;
258 be_add_module_to_list(&spillers, name, spiller);
261 void be_do_spill(ir_graph *irg, const arch_register_class_t *cls)
263 assert(selected_spiller != NULL);
265 selected_spiller->spill(irg, cls);
268 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spilloptions)
269 void be_init_spilloptions(void)
271 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
272 lc_opt_entry_t *spill_grp = lc_opt_get_grp(be_grp, "spill");
274 lc_opt_add_table(spill_grp, be_spill_options);
275 be_add_module_list_opt(be_grp, "spiller", "spill algorithm",
276 &spillers, (void**) &selected_spiller);
278 FIRM_DBG_REGISTER(dbg, "firm.be.spillprepare");