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