beinsn: Do not store, whether insn operands are constrained.
[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 *tmp        = NULL;
64         unsigned *def_constr = NULL;
65         int       arity      = get_irn_arity(node);
66
67         int i, i2;
68
69         /* Insert a copy for constraint inputs attached to a value which can't
70          * fulfill the constraint
71          * (typical example: stack pointer as input to copyb)
72          * TODO: This really just checks precolored registers at the moment and
73          *       ignores the general case of not matching in/out constraints
74          */
75         for (i = 0; i < arity; ++i) {
76                 ir_node                   *op  = get_irn_n(node, i);
77                 const arch_register_req_t *req = arch_get_irn_register_req_in(node, i);
78                 const arch_register_t     *reg;
79                 ir_node                   *copy;
80
81                 if (req->cls != cls)
82                         continue;
83                 reg = arch_get_irn_register(op);
84                 if (reg == NULL)
85                         continue;
86
87                 /* Precolored with an ignore register (which is not virtual). */
88                 if (reg->type & arch_register_type_virtual ||
89                     rbitset_is_set(birg->allocatable_regs, reg->global_index))
90                         continue;
91
92                 if (! (req->type & arch_register_req_type_limited))
93                         continue;
94                 if (rbitset_is_set(req->limited, reg->index))
95                         continue;
96
97                 copy = be_new_Copy(block, op);
98                 stat_ev_int("constr_copy", 1);
99                 sched_add_before(node, copy);
100                 set_irn_n(node, i, copy);
101                 DBG((dbg, LEVEL_3, "inserting ignore arg copy %+F for %+F pos %d\n",
102                      copy, node, i));
103         }
104
105         /* insert copies for nodes that occur constrained more than once. */
106         for (i = 0; i < arity; ++i) {
107                 ir_node                   *in;
108                 ir_node                   *copy;
109                 const arch_register_req_t *req;
110
111                 req = arch_get_irn_register_req_in(node, i);
112                 if (req->cls != cls)
113                         continue;
114
115                 if (! (req->type & arch_register_req_type_limited))
116                         continue;
117
118                 in = get_irn_n(node, i);
119                 if (!arch_irn_consider_in_reg_alloc(cls, in))
120                         continue;
121
122                 for (i2 = i + 1; i2 < arity; ++i2) {
123                         ir_node *in2;
124                         const arch_register_req_t *req2;
125
126                         req2 = arch_get_irn_register_req_in(node, i2);
127                         if (req2->cls != cls)
128                                 continue;
129                         if (! (req2->type & arch_register_req_type_limited))
130                                 continue;
131
132                         in2 = get_irn_n(node, i2);
133                         if (in2 != in)
134                                 continue;
135
136                         /* if the constraint is the same, no copy is necessary
137                          * TODO generalise unequal but overlapping constraints */
138                         if (rbitsets_equal(req->limited, req2->limited, cls->n_regs))
139                                 continue;
140
141                         copy = be_new_Copy(block, in);
142                         stat_ev_int("constr_copy", 1);
143
144                         sched_add_before(node, copy);
145                         set_irn_n(node, i2, copy);
146                         DBG((dbg, LEVEL_3,
147                              "inserting multiple constr copy %+F for %+F pos %d\n",
148                              copy, node, i2));
149                 }
150         }
151
152         /* collect all registers occurring in out constraints. */
153         be_foreach_definition(node, cls, def,
154                 (void)def;
155                 if (! (req_->type & arch_register_req_type_limited))
156                         continue;
157                 if (def_constr == NULL) {
158                         rbitset_alloca(def_constr, cls->n_regs);
159                 }
160                 rbitset_or(def_constr, req_->limited, cls->n_regs);
161         );
162
163         /* no output constraints => we're good */
164         if (def_constr == NULL) {
165                 return;
166         }
167
168         /*
169          * insert copies for all constrained arguments living through the node
170          * and being constrained to a register which also occurs in out constraints.
171          */
172         rbitset_alloca(tmp, cls->n_regs);
173         for (i = 0; i < arity; ++i) {
174                 const arch_register_req_t *req;
175                 ir_node                   *in;
176                 ir_node                   *copy;
177
178                 /*
179                  * Check, if
180                  * 1) the operand is constrained.
181                  * 2) lives through the node.
182                  * 3) is constrained to a register occurring in out constraints.
183                  */
184                 req = arch_get_irn_register_req_in(node, i);
185                 if (req->cls != cls)
186                         continue;
187                 if (!(req->type & arch_register_req_type_limited))
188                         continue;
189
190                 in = get_irn_n(node, i);
191                 if (!arch_irn_consider_in_reg_alloc(cls, in))
192                         continue;
193                 if (!be_values_interfere(lv, node, in))
194                         continue;
195
196                 rbitset_copy(tmp, req->limited, cls->n_regs);
197                 rbitset_and(tmp, def_constr, cls->n_regs);
198
199                 if (rbitset_is_empty(tmp, cls->n_regs))
200                         continue;
201
202                 /*
203                  * only create the copy if the operand is no copy.
204                  * this is necessary since the assure constraints phase inserts
205                  * Copies and Keeps for operands which must be different from the
206                  * results. Additional copies here would destroy this.
207                  */
208                 if (be_is_Copy(in))
209                         continue;
210
211                 copy = be_new_Copy(block, in);
212                 sched_add_before(node, copy);
213                 set_irn_n(node, i, copy);
214                 DBG((dbg, LEVEL_3, "inserting constr copy %+F for %+F pos %d\n",
215                      copy, node, i));
216                 be_liveness_update(lv, in);
217         }
218 }
219
220 static void pre_spill_prepare_constr_walker(ir_node *block, void *data)
221 {
222         be_pre_spill_env_t *env = (be_pre_spill_env_t*)data;
223         sched_foreach(block, node) {
224                 prepare_constr_insn(env, node);
225         }
226 }
227
228 void be_pre_spill_prepare_constr(ir_graph *irg,
229                                  const arch_register_class_t *cls)
230 {
231         be_pre_spill_env_t env;
232         memset(&env, 0, sizeof(env));
233         env.irg = irg;
234         env.cls = cls;
235
236         be_assure_live_sets(irg);
237
238         irg_block_walk_graph(irg, pre_spill_prepare_constr_walker, NULL, &env);
239 }
240
241
242
243 int be_coalesce_spill_slots = 1;
244 int be_do_remats = 1;
245
246 static const lc_opt_table_entry_t be_spill_options[] = {
247         LC_OPT_ENT_BOOL ("coalesce_slots", "coalesce the spill slots", &be_coalesce_spill_slots),
248         LC_OPT_ENT_BOOL ("remat", "try to rematerialize values instead of reloading", &be_do_remats),
249         LC_OPT_LAST
250 };
251
252 static be_module_list_entry_t *spillers = NULL;
253 static const be_spiller_t *selected_spiller = NULL;
254
255 void be_register_spiller(const char *name, be_spiller_t *spiller)
256 {
257         if (selected_spiller == NULL)
258                 selected_spiller = spiller;
259         be_add_module_to_list(&spillers, name, spiller);
260 }
261
262 void be_do_spill(ir_graph *irg, const arch_register_class_t *cls)
263 {
264         assert(selected_spiller != NULL);
265
266         selected_spiller->spill(irg, cls);
267 }
268
269 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spilloptions)
270 void be_init_spilloptions(void)
271 {
272         lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
273         lc_opt_entry_t *spill_grp = lc_opt_get_grp(be_grp, "spill");
274
275         lc_opt_add_table(spill_grp, be_spill_options);
276         be_add_module_list_opt(be_grp, "spiller", "spill algorithm",
277                                &spillers, (void**) &selected_spiller);
278
279         FIRM_DBG_REGISTER(dbg, "firm.be.spillprepare");
280 }