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