ia32: Inline get_ia32_orig_node() into its only caller.
[libfirm] / ir / be / bespill.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Spill module selection; Preparation steps
23  * @author      Matthias Braun
24  * @date        29.09.2005
25  */
26 #include "config.h"
27
28 #include "irtools.h"
29 #include "debug.h"
30 #include "iredges_t.h"
31 #include "raw_bitset.h"
32 #include "statev_t.h"
33 #include "irgwalk.h"
34
35 #include "bespill.h"
36 #include "bemodule.h"
37 #include "be.h"
38 #include "belive_t.h"
39 #include "beirg.h"
40 #include "bearch.h"
41 #include "benode.h"
42 #include "besched.h"
43 #include "bera.h"
44 #include "beintlive_t.h"
45
46 #include "lc_opts.h"
47 #include "lc_opts_enum.h"
48
49 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
50
51 typedef struct be_pre_spill_env_t {
52         ir_graph                    *irg;
53         const arch_register_class_t *cls;
54 } be_pre_spill_env_t;
55
56 static void prepare_constr_insn(be_pre_spill_env_t *env, ir_node *node)
57 {
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);
65
66         int i, i2;
67
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
73          */
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;
78                 ir_node                   *copy;
79
80                 if (req->cls != cls)
81                         continue;
82                 reg = arch_get_irn_register(op);
83                 if (reg == NULL)
84                         continue;
85
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))
89                         continue;
90
91                 if (!arch_register_req_is(req, limited))
92                         continue;
93                 if (rbitset_is_set(req->limited, reg->index))
94                         continue;
95
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",
101                      copy, node, i));
102         }
103
104         /* insert copies for nodes that occur constrained more than once. */
105         for (i = 0; i < arity; ++i) {
106                 ir_node                   *in;
107                 ir_node                   *copy;
108                 const arch_register_req_t *req;
109
110                 req = arch_get_irn_register_req_in(node, i);
111                 if (req->cls != cls)
112                         continue;
113
114                 if (!arch_register_req_is(req, limited))
115                         continue;
116
117                 in = get_irn_n(node, i);
118                 if (!arch_irn_consider_in_reg_alloc(cls, in))
119                         continue;
120
121                 for (i2 = i + 1; i2 < arity; ++i2) {
122                         ir_node *in2;
123                         const arch_register_req_t *req2;
124
125                         req2 = arch_get_irn_register_req_in(node, i2);
126                         if (req2->cls != cls)
127                                 continue;
128                         if (!arch_register_req_is(req2, limited))
129                                 continue;
130
131                         in2 = get_irn_n(node, i2);
132                         if (in2 != in)
133                                 continue;
134
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))
138                                 continue;
139
140                         copy = be_new_Copy(block, in);
141                         stat_ev_int("constr_copy", 1);
142
143                         sched_add_before(node, copy);
144                         set_irn_n(node, i2, copy);
145                         DBG((dbg, LEVEL_3,
146                              "inserting multiple constr copy %+F for %+F pos %d\n",
147                              copy, node, i2));
148                 }
149         }
150
151         /* collect all registers occurring in out constraints. */
152         be_foreach_definition(node, cls, def,
153                 (void)def;
154                 if (!arch_register_req_is(req_, limited))
155                         continue;
156                 if (def_constr == NULL) {
157                         def_constr = rbitset_alloca(cls->n_regs);
158                 }
159                 rbitset_or(def_constr, req_->limited, cls->n_regs);
160         );
161
162         /* no output constraints => we're good */
163         if (def_constr == NULL) {
164                 return;
165         }
166
167         /*
168          * insert copies for all constrained arguments living through the node
169          * and being constrained to a register which also occurs in out constraints.
170          */
171         unsigned *const tmp = rbitset_alloca(cls->n_regs);
172         for (i = 0; i < arity; ++i) {
173                 const arch_register_req_t *req;
174                 ir_node                   *in;
175                 ir_node                   *copy;
176
177                 /*
178                  * Check, if
179                  * 1) the operand is constrained.
180                  * 2) lives through the node.
181                  * 3) is constrained to a register occurring in out constraints.
182                  */
183                 req = arch_get_irn_register_req_in(node, i);
184                 if (req->cls != cls)
185                         continue;
186                 if (!arch_register_req_is(req, limited))
187                         continue;
188
189                 in = get_irn_n(node, i);
190                 if (!arch_irn_consider_in_reg_alloc(cls, in))
191                         continue;
192                 if (!be_values_interfere(lv, node, in))
193                         continue;
194
195                 rbitset_copy(tmp, req->limited, cls->n_regs);
196                 rbitset_and(tmp, def_constr, cls->n_regs);
197
198                 if (rbitset_is_empty(tmp, cls->n_regs))
199                         continue;
200
201                 /*
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.
206                  */
207                 if (be_is_Copy(in))
208                         continue;
209
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",
214                      copy, node, i));
215                 be_liveness_update(lv, in);
216         }
217 }
218
219 static void pre_spill_prepare_constr_walker(ir_node *block, void *data)
220 {
221         be_pre_spill_env_t *env = (be_pre_spill_env_t*)data;
222         sched_foreach(block, node) {
223                 prepare_constr_insn(env, node);
224         }
225 }
226
227 void be_pre_spill_prepare_constr(ir_graph *irg,
228                                  const arch_register_class_t *cls)
229 {
230         be_pre_spill_env_t env;
231         memset(&env, 0, sizeof(env));
232         env.irg = irg;
233         env.cls = cls;
234
235         be_assure_live_sets(irg);
236
237         irg_block_walk_graph(irg, pre_spill_prepare_constr_walker, NULL, &env);
238 }
239
240
241
242 int be_coalesce_spill_slots = 1;
243 int be_do_remats = 1;
244
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),
248         LC_OPT_LAST
249 };
250
251 static be_module_list_entry_t *spillers = NULL;
252 static const be_spiller_t *selected_spiller = NULL;
253
254 void be_register_spiller(const char *name, be_spiller_t *spiller)
255 {
256         if (selected_spiller == NULL)
257                 selected_spiller = spiller;
258         be_add_module_to_list(&spillers, name, spiller);
259 }
260
261 void be_do_spill(ir_graph *irg, const arch_register_class_t *cls)
262 {
263         assert(selected_spiller != NULL);
264
265         selected_spiller->spill(irg, cls);
266 }
267
268 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spilloptions)
269 void be_init_spilloptions(void)
270 {
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");
273
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);
277
278         FIRM_DBG_REGISTER(dbg, "firm.be.spillprepare");
279 }