Start block isn't a special case anymore (and now get the old node nr).
[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 "adt/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         be_irg_t                    *birg;
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 be_irg_t *birg = env->birg;
62         be_lv_t *lv          = birg->lv;
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          * 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
74          */
75         for (i = 0; i < arity; ++i) {
76                 ir_node *op = get_irn_n(node, i);
77                 ir_node *copy;
78                 const arch_register_t *reg;
79                 const arch_register_req_t *req;
80
81                 req = arch_get_register_req(node, i);
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 (arch_register_type_is(reg, joker)
91                                 || !arch_register_type_is(reg, ignore))
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(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));
104         }
105
106         /* insert copies for nodes that occur constrained more than once. */
107         for (i = 0; i < arity; ++i) {
108                 ir_node                   *in;
109                 ir_node                   *copy;
110                 const arch_register_req_t *req;
111
112                 req = arch_get_register_req(node, i);
113                 if (req->cls != cls)
114                         continue;
115
116                 if (! (req->type & arch_register_req_type_limited))
117                         continue;
118
119                 in = get_irn_n(node, i);
120                 if (!arch_irn_consider_in_reg_alloc(cls, in))
121                         continue;
122
123                 for (i2 = i + 1; i2 < arity; ++i2) {
124                         ir_node *in2;
125                         const arch_register_req_t *req2;
126
127                         req2 = arch_get_register_req(node, i2);
128                         if (req2->cls != cls)
129                                 continue;
130                         if (! (req2->type & arch_register_req_type_limited))
131                                 continue;
132
133                         in2 = get_irn_n(node, i2);
134                         if (in2 != in)
135                                 continue;
136
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))
140                                 continue;
141
142 #if 0
143                         /* Matze: looks fishy to me disabled it for now */
144                         if (be_is_Copy(get_irn_n(insn->irn, a_op->pos)))
145                                 continue;
146 #endif
147
148                         copy = be_new_Copy(cls, block, in);
149                         stat_ev_int("constr_copy", 1);
150
151                         sched_add_before(node, copy);
152                         set_irn_n(node, i2, copy);
153                         DBG((dbg, LEVEL_3,
154                              "inserting multiple constr copy %+F for %+F pos %d\n",
155                              copy, node, i2));
156                 }
157         }
158
159         /* collect all registers occurring in out constraints. */
160         if (get_irn_mode(node) == mode_T) {
161                 const ir_edge_t *edge;
162
163                 foreach_out_edge(node, edge) {
164                         ir_node                   *proj = get_edge_src_irn(edge);
165                         const arch_register_req_t *req  = arch_get_register_req_out(proj);
166                         if (! (req->type & arch_register_req_type_limited))
167                                 continue;
168
169                         if (def_constr == NULL) {
170                                 rbitset_alloca(def_constr, cls->n_regs);
171                         }
172                         rbitset_or(def_constr, req->limited, cls->n_regs);
173                 }
174         } else {
175                 const arch_register_req_t *req = arch_get_register_req_out(node);
176                 if (req->type & arch_register_req_type_limited) {
177                         rbitset_alloca(def_constr, cls->n_regs);
178                         rbitset_or(def_constr, req->limited, cls->n_regs);
179                 }
180         }
181
182         /* no output constraints => we're good */
183         if (def_constr == NULL) {
184                 return;
185         }
186
187         /*
188          * insert copies for all constrained arguments living through the node
189          * and being constrained to a register which also occurs in out constraints.
190          */
191         rbitset_alloca(tmp, cls->n_regs);
192         for (i = 0; i < arity; ++i) {
193                 const arch_register_req_t *req;
194                 ir_node                   *in;
195                 ir_node                   *copy;
196
197                 /*
198                  * Check, if
199                  * 1) the operand is constrained.
200                  * 2) lives through the node.
201                  * 3) is constrained to a register occurring in out constraints.
202                  */
203                 req = arch_get_register_req(node, i);
204                 if (req->cls != cls)
205                         continue;
206                 if (!(req->type & arch_register_req_type_limited))
207                         continue;
208
209                 in = get_irn_n(node, i);
210                 if (!arch_irn_consider_in_reg_alloc(cls, in))
211                         continue;
212                 if (!be_values_interfere(lv, node, in))
213                         continue;
214
215                 rbitset_copy(tmp, req->limited, cls->n_regs);
216                 rbitset_and(tmp, def_constr, cls->n_regs);
217
218                 if (rbitset_is_empty(tmp, cls->n_regs))
219                         continue;
220
221                 /*
222                  * only create the copy if the operand is no copy.
223                  * this is necessary since the assure constraints phase inserts
224                  * Copies and Keeps for operands which must be different from the
225                  * results. Additional copies here would destroy this.
226                  */
227                 if (be_is_Copy(in))
228                         continue;
229
230                 copy = be_new_Copy(cls, block, in);
231                 sched_add_before(node, copy);
232                 set_irn_n(node, i, copy);
233                 DBG((dbg, LEVEL_3, "inserting constr copy %+F for %+F pos %d\n",
234                      copy, node, i));
235                 be_liveness_update(lv, in);
236         }
237 }
238
239 static void pre_spill_prepare_constr_walker(ir_node *block, void *data)
240 {
241         be_pre_spill_env_t *env = data;
242         ir_node *node;
243         sched_foreach(block, node) {
244                 prepare_constr_insn(env, node);
245         }
246 }
247
248 void be_pre_spill_prepare_constr(be_irg_t *birg,
249                                  const arch_register_class_t *cls)
250 {
251         ir_graph *irg = birg->irg;
252         be_pre_spill_env_t env;
253         memset(&env, 0, sizeof(env));
254         env.birg = birg;
255         env.cls  = cls;
256         irg_block_walk_graph(irg, pre_spill_prepare_constr_walker, NULL, &env);
257 }
258
259
260
261 int be_coalesce_spill_slots = 1;
262 int be_do_remats = 1;
263
264 static const lc_opt_table_entry_t be_spill_options[] = {
265         LC_OPT_ENT_BOOL ("coalesce_slots", "coalesce the spill slots", &be_coalesce_spill_slots),
266         LC_OPT_ENT_BOOL ("remat", "try to rematerialize values instead of reloading", &be_do_remats),
267         LC_OPT_LAST
268 };
269
270 static be_module_list_entry_t *spillers = NULL;
271 static const be_spiller_t *selected_spiller = NULL;
272
273 void be_register_spiller(const char *name, be_spiller_t *spiller)
274 {
275         if (selected_spiller == NULL)
276                 selected_spiller = spiller;
277         be_add_module_to_list(&spillers, name, spiller);
278 }
279
280 void be_do_spill(be_irg_t *birg, const arch_register_class_t *cls)
281 {
282         assert(selected_spiller != NULL);
283
284         selected_spiller->spill(birg, cls);
285 }
286
287 void be_init_spilloptions(void)
288 {
289         lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
290         lc_opt_entry_t *spill_grp = lc_opt_get_grp(be_grp, "spill");
291
292         lc_opt_add_table(spill_grp, be_spill_options);
293         be_add_module_list_opt(spill_grp, "spiller", "spill algorithm",
294                                &spillers, (void**) &selected_spiller);
295
296         FIRM_DBG_REGISTER(dbg, "firm.be.spillprepare");
297 }
298
299 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spilloptions);