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
31 #include "iredges_t.h"
32 #include "adt/raw_bitset.h"
45 #include "beintlive_t.h"
48 #include "lc_opts_enum.h"
50 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
52 typedef struct be_pre_spill_env_t {
54 const arch_register_class_t *cls;
57 static void prepare_constr_insn(be_pre_spill_env_t *env, ir_node *node)
59 const arch_register_class_t *cls = env->cls;
60 ir_node *block = get_nodes_block(node);
61 const be_irg_t *birg = env->birg;
62 be_lv_t *lv = birg->lv;
64 unsigned *def_constr = NULL;
65 int arity = get_irn_arity(node);
69 /* Insert a copy for constraint inputs attached to a value which can't
70 * fullfil the constraint
71 * (typical example: stack pointer as input to copyb)
72 * TODO: This really just checks precolored registers at the moment and
73 * ignore the general case of not matching in/out constraints
75 for (i = 0; i < arity; ++i) {
76 ir_node *op = get_irn_n(node, i);
78 const arch_register_t *reg;
79 const arch_register_req_t *req;
81 req = arch_get_register_req(node, i);
84 reg = arch_get_irn_register(op);
88 /* precolored with an ignore register (which is not a joker like
90 if (arch_register_type_is(reg, joker)
91 || !arch_register_type_is(reg, ignore))
94 if (! (req->type & arch_register_req_type_limited))
96 if (rbitset_is_set(req->limited, reg->index))
99 copy = be_new_Copy(cls, block, op);
100 stat_ev_int("constr_copy", 1);
101 sched_add_before(node, copy);
102 set_irn_n(node, i, copy);
103 DBG((dbg, LEVEL_3, "inserting ignore arg copy %+F for %+F pos %d\n", copy, node, i));
106 /* insert copies for nodes that occur constrained more than once. */
107 for (i = 0; i < arity; ++i) {
110 const arch_register_req_t *req;
112 req = arch_get_register_req(node, i);
116 if (! (req->type & arch_register_req_type_limited))
119 in = get_irn_n(node, i);
120 if (!arch_irn_consider_in_reg_alloc(cls, in))
123 for (i2 = i + 1; i2 < arity; ++i2) {
125 const arch_register_req_t *req2;
127 req2 = arch_get_register_req(node, i2);
128 if (req2->cls != cls)
130 if (! (req2->type & arch_register_req_type_limited))
133 in2 = get_irn_n(node, i2);
137 /* if the constraint is the same, no copy is necessary
138 * TODO generalise unequal but overlapping constraints */
139 if (rbitset_equal(req->limited, req2->limited, cls->n_regs))
142 copy = be_new_Copy(cls, block, in);
143 stat_ev_int("constr_copy", 1);
145 sched_add_before(node, copy);
146 set_irn_n(node, i2, copy);
148 "inserting multiple constr copy %+F for %+F pos %d\n",
153 /* collect all registers occurring in out constraints. */
154 if (get_irn_mode(node) == mode_T) {
155 const ir_edge_t *edge;
157 foreach_out_edge(node, edge) {
158 ir_node *proj = get_edge_src_irn(edge);
159 const arch_register_req_t *req = arch_get_register_req_out(proj);
160 if (! (req->type & arch_register_req_type_limited))
163 if (def_constr == NULL) {
164 rbitset_alloca(def_constr, cls->n_regs);
166 rbitset_or(def_constr, req->limited, cls->n_regs);
169 const arch_register_req_t *req = arch_get_register_req_out(node);
170 if (req->type & arch_register_req_type_limited) {
171 rbitset_alloca(def_constr, cls->n_regs);
172 rbitset_or(def_constr, req->limited, cls->n_regs);
176 /* no output constraints => we're good */
177 if (def_constr == NULL) {
182 * insert copies for all constrained arguments living through the node
183 * and being constrained to a register which also occurs in out constraints.
185 rbitset_alloca(tmp, cls->n_regs);
186 for (i = 0; i < arity; ++i) {
187 const arch_register_req_t *req;
193 * 1) the operand is constrained.
194 * 2) lives through the node.
195 * 3) is constrained to a register occurring in out constraints.
197 req = arch_get_register_req(node, i);
200 if (!(req->type & arch_register_req_type_limited))
203 in = get_irn_n(node, i);
204 if (!arch_irn_consider_in_reg_alloc(cls, in))
206 if (!be_values_interfere(lv, node, in))
209 rbitset_copy(tmp, req->limited, cls->n_regs);
210 rbitset_and(tmp, def_constr, cls->n_regs);
212 if (rbitset_is_empty(tmp, cls->n_regs))
216 * only create the copy if the operand is no copy.
217 * this is necessary since the assure constraints phase inserts
218 * Copies and Keeps for operands which must be different from the
219 * results. Additional copies here would destroy this.
224 copy = be_new_Copy(cls, block, in);
225 sched_add_before(node, copy);
226 set_irn_n(node, i, copy);
227 DBG((dbg, LEVEL_3, "inserting constr copy %+F for %+F pos %d\n",
229 be_liveness_update(lv, in);
233 static void pre_spill_prepare_constr_walker(ir_node *block, void *data)
235 be_pre_spill_env_t *env = data;
237 sched_foreach(block, node) {
238 prepare_constr_insn(env, node);
242 void be_pre_spill_prepare_constr(be_irg_t *birg,
243 const arch_register_class_t *cls)
245 ir_graph *irg = birg->irg;
246 be_pre_spill_env_t env;
247 memset(&env, 0, sizeof(env));
250 irg_block_walk_graph(irg, pre_spill_prepare_constr_walker, NULL, &env);
255 int be_coalesce_spill_slots = 1;
256 int be_do_remats = 1;
258 static const lc_opt_table_entry_t be_spill_options[] = {
259 LC_OPT_ENT_BOOL ("coalesce_slots", "coalesce the spill slots", &be_coalesce_spill_slots),
260 LC_OPT_ENT_BOOL ("remat", "try to rematerialize values instead of reloading", &be_do_remats),
264 static be_module_list_entry_t *spillers = NULL;
265 static const be_spiller_t *selected_spiller = NULL;
267 void be_register_spiller(const char *name, be_spiller_t *spiller)
269 if (selected_spiller == NULL)
270 selected_spiller = spiller;
271 be_add_module_to_list(&spillers, name, spiller);
274 void be_do_spill(be_irg_t *birg, const arch_register_class_t *cls)
276 assert(selected_spiller != NULL);
278 selected_spiller->spill(birg, cls);
281 void be_init_spilloptions(void)
283 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
284 lc_opt_entry_t *spill_grp = lc_opt_get_grp(be_grp, "spill");
286 lc_opt_add_table(spill_grp, be_spill_options);
287 be_add_module_list_opt(spill_grp, "spiller", "spill algorithm",
288 &spillers, (void**) &selected_spiller);
290 FIRM_DBG_REGISTER(dbg, "firm.be.spillprepare");
293 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spilloptions);