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