cleanup: Remove unnecessary #include "beirg.h".
[libfirm] / ir / be / ia32 / ia32_fpu.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   Handles fpu rounding modes
23  * @author  Matthias Braun
24  *
25  * The problem we deal with here is that the x86 ABI says the user can control
26  * the fpu rounding mode, which means that when we do some operations like float
27  * to int conversion which are specified as truncation in the C standard we have
28  * to spill, change and restore the fpu rounding mode between spills.
29  */
30 #include "config.h"
31
32 #include "ia32_fpu.h"
33 #include "ia32_new_nodes.h"
34 #include "ia32_architecture.h"
35 #include "gen_ia32_regalloc_if.h"
36
37 #include "ircons.h"
38 #include "irgwalk.h"
39 #include "tv.h"
40 #include "array.h"
41
42 #include "beirgmod.h"
43 #include "bearch.h"
44 #include "besched.h"
45 #include "beabi.h"
46 #include "benode.h"
47 #include "bestate.h"
48 #include "beutil.h"
49 #include "bessaconstr.h"
50
51 static ir_entity *fpcw_round    = NULL;
52 static ir_entity *fpcw_truncate = NULL;
53
54 static ir_entity *create_ent(int value, const char *name)
55 {
56         ir_mode   *mode = mode_Hu;
57         ir_type   *type = new_type_primitive(mode);
58         ir_type   *glob = get_glob_type();
59         ir_graph  *cnst_irg;
60         ir_entity *ent;
61         ir_node   *cnst;
62         ir_tarval *tv;
63
64         set_type_alignment_bytes(type, 4);
65
66         tv  = new_tarval_from_long(value, mode);
67         ent = new_entity(glob, new_id_from_str(name), type);
68         set_entity_ld_ident(ent, get_entity_ident(ent));
69         set_entity_visibility(ent, ir_visibility_local);
70         add_entity_linkage(ent, IR_LINKAGE_CONSTANT);
71
72         cnst_irg = get_const_code_irg();
73         cnst     = new_r_Const(cnst_irg, tv);
74         set_atomic_ent_value(ent, cnst);
75
76         return ent;
77 }
78
79 static void create_fpcw_entities(void)
80 {
81         fpcw_round    = create_ent(0xc7f, "_fpcw_round");
82         fpcw_truncate = create_ent(0x37f, "_fpcw_truncate");
83 }
84
85 static ir_node *create_fpu_mode_spill(void *env, ir_node *state, int force,
86                                       ir_node *after)
87 {
88         (void) env;
89
90         /* we don't spill the fpcw in unsafe mode */
91         if (ia32_cg_config.use_unsafe_floatconv) {
92                 ir_node *block = get_nodes_block(state);
93                 if (force == 1 || !is_ia32_ChangeCW(state)) {
94                         ir_node *spill = new_bd_ia32_FnstCWNOP(NULL, block, state);
95                         sched_add_after(after, spill);
96                         return spill;
97                 }
98                 return NULL;
99         }
100
101         if (force == 1 || !is_ia32_ChangeCW(state)) {
102                 ir_graph *irg = get_irn_irg(state);
103                 ir_node *block = get_nodes_block(state);
104                 ir_node *noreg = ia32_new_NoReg_gp(irg);
105                 ir_node *nomem = get_irg_no_mem(irg);
106                 ir_node *frame = get_irg_frame(irg);
107                 ir_node *spill
108                         = new_bd_ia32_FnstCW(NULL, block, frame, noreg, nomem, state);
109                 set_ia32_op_type(spill, ia32_AddrModeD);
110                 /* use mode_Iu, as movl has a shorter opcode than movw */
111                 set_ia32_ls_mode(spill, mode_Iu);
112                 set_ia32_use_frame(spill);
113
114                 sched_add_after(skip_Proj(after), spill);
115                 return spill;
116         }
117
118         return NULL;
119 }
120
121 static ir_node *create_fldcw_ent(ir_node *block, ir_entity *entity)
122 {
123         ir_graph *irg   = get_irn_irg(block);
124         ir_node  *nomem = get_irg_no_mem(irg);
125         ir_node  *noreg = ia32_new_NoReg_gp(irg);
126         ir_node  *reload;
127
128         reload = new_bd_ia32_FldCW(NULL, block, noreg, noreg, nomem);
129         set_ia32_op_type(reload, ia32_AddrModeS);
130         set_ia32_ls_mode(reload, ia32_reg_classes[CLASS_ia32_fp_cw].mode);
131         set_ia32_am_sc(reload, entity);
132         set_ia32_use_frame(reload);
133         arch_set_irn_register(reload, &ia32_registers[REG_FPCW]);
134
135         return reload;
136 }
137
138 static ir_node *create_fpu_mode_reload(void *env, ir_node *state,
139                                        ir_node *spill, ir_node *before,
140                                        ir_node *last_state)
141 {
142         ir_graph *irg    = get_irn_irg(state);
143         ir_node  *block  = get_nodes_block(before);
144         ir_node  *frame  = get_irg_frame(irg);
145         ir_node  *noreg  = ia32_new_NoReg_gp(irg);
146         ir_node  *reload = NULL;
147         (void) env;
148
149         if (ia32_cg_config.use_unsafe_floatconv) {
150                 if (fpcw_round == NULL) {
151                         create_fpcw_entities();
152                 }
153                 if (spill != NULL) {
154                         reload = create_fldcw_ent(block, fpcw_round);
155                 } else {
156                         reload = create_fldcw_ent(block, fpcw_truncate);
157                 }
158                 sched_add_before(before, reload);
159                 return reload;
160         }
161
162         if (spill != NULL) {
163                 reload = new_bd_ia32_FldCW(NULL, block, frame, noreg, spill);
164                 set_ia32_op_type(reload, ia32_AddrModeS);
165                 set_ia32_ls_mode(reload, ia32_reg_classes[CLASS_ia32_fp_cw].mode);
166                 set_ia32_use_frame(reload);
167                 arch_set_irn_register(reload, &ia32_registers[REG_FPCW]);
168
169                 sched_add_before(before, reload);
170         } else {
171                 ir_mode *lsmode = ia32_reg_classes[CLASS_ia32_fp_cw].mode;
172                 ir_node *nomem  = get_irg_no_mem(irg);
173                 ir_node *cwstore, *load, *load_res, *orn, *store, *fldcw;
174                 ir_node *store_proj;
175                 ir_node *or_const;
176
177                 assert(last_state != NULL);
178                 cwstore = new_bd_ia32_FnstCW(NULL, block, frame, noreg, nomem,
179                                              last_state);
180                 set_ia32_op_type(cwstore, ia32_AddrModeD);
181                 set_ia32_ls_mode(cwstore, lsmode);
182                 set_ia32_use_frame(cwstore);
183                 sched_add_before(before, cwstore);
184
185                 load = new_bd_ia32_Load(NULL, block, frame, noreg, cwstore);
186                 set_ia32_op_type(load, ia32_AddrModeS);
187                 set_ia32_ls_mode(load, lsmode);
188                 set_ia32_use_frame(load);
189                 sched_add_before(before, load);
190
191                 load_res = new_r_Proj(load, mode_Iu, pn_ia32_Load_res);
192
193                 /* TODO: make the actual mode configurable in ChangeCW... */
194                 or_const = new_bd_ia32_Immediate(NULL, get_irg_start_block(irg),
195                                                  NULL, 0, 0, 3072);
196                 arch_set_irn_register(or_const, &ia32_registers[REG_GP_NOREG]);
197                 orn = new_bd_ia32_Or(NULL, block, noreg, noreg, nomem, load_res,
198                                     or_const);
199                 sched_add_before(before, orn);
200
201                 store = new_bd_ia32_Store(NULL, block, frame, noreg, nomem, orn);
202                 set_ia32_op_type(store, ia32_AddrModeD);
203                 /* use mode_Iu, as movl has a shorter opcode than movw */
204                 set_ia32_ls_mode(store, mode_Iu);
205                 set_ia32_use_frame(store);
206                 store_proj = new_r_Proj(store, mode_M, pn_ia32_Store_M);
207                 sched_add_before(before, store);
208
209                 fldcw = new_bd_ia32_FldCW(NULL, block, frame, noreg, store_proj);
210                 set_ia32_op_type(fldcw, ia32_AddrModeS);
211                 set_ia32_ls_mode(fldcw, lsmode);
212                 set_ia32_use_frame(fldcw);
213                 arch_set_irn_register(fldcw, &ia32_registers[REG_FPCW]);
214                 sched_add_before(before, fldcw);
215
216                 reload = fldcw;
217         }
218
219         return reload;
220 }
221
222 typedef struct collect_fpu_mode_nodes_env_t {
223         ir_node         **state_nodes;
224 } collect_fpu_mode_nodes_env_t;
225
226 static void collect_fpu_mode_nodes_walker(ir_node *node, void *data)
227 {
228         collect_fpu_mode_nodes_env_t *env = (collect_fpu_mode_nodes_env_t*)data;
229         const arch_register_t *reg;
230
231         if (!mode_is_data(get_irn_mode(node)))
232                 return;
233
234         reg = arch_get_irn_register(node);
235         if (reg == &ia32_registers[REG_FPCW] && !is_ia32_ChangeCW(node)) {
236                 ARR_APP1(ir_node*, env->state_nodes, node);
237         }
238 }
239
240 static void rewire_fpu_mode_nodes(ir_graph *irg)
241 {
242         collect_fpu_mode_nodes_env_t env;
243         be_ssa_construction_env_t senv;
244         const arch_register_t *reg = &ia32_registers[REG_FPCW];
245         ir_node *initial_value;
246         ir_node **phis;
247         be_lv_t *lv = be_get_irg_liveness(irg);
248         size_t i, len;
249
250         /* do ssa construction for the fpu modes */
251         env.state_nodes = NEW_ARR_F(ir_node*, 0);
252         irg_walk_graph(irg, collect_fpu_mode_nodes_walker, NULL, &env);
253
254         /* nothing needs to be done, in fact we must not continue as for endless
255          * loops noone is using the initial_value and it will point to a bad node
256          * now
257          */
258         if (ARR_LEN(env.state_nodes) == 0) {
259                 DEL_ARR_F(env.state_nodes);
260                 return;
261         }
262
263         initial_value = be_get_initial_reg_value(irg, reg);
264         be_ssa_construction_init(&senv, irg);
265         be_ssa_construction_add_copies(&senv, env.state_nodes,
266                                        ARR_LEN(env.state_nodes));
267         be_ssa_construction_fix_users(&senv, initial_value);
268
269         if (lv != NULL) {
270                 be_ssa_construction_update_liveness_phis(&senv, lv);
271                 be_liveness_update(lv, initial_value);
272                 len = ARR_LEN(env.state_nodes);
273                 for (i = 0; i < len; ++i) {
274                         be_liveness_update(lv, env.state_nodes[i]);
275                 }
276         } else {
277                 be_invalidate_live_sets(irg);
278         }
279
280         /* set registers for the phis */
281         phis = be_ssa_construction_get_new_phis(&senv);
282         len = ARR_LEN(phis);
283         for (i = 0; i < len; ++i) {
284                 ir_node *phi = phis[i];
285                 arch_set_irn_register(phi, reg);
286         }
287         be_ssa_construction_destroy(&senv);
288         DEL_ARR_F(env.state_nodes);
289
290         be_invalidate_live_sets(irg);
291 }
292
293 void ia32_setup_fpu_mode(ir_graph *irg)
294 {
295         /* do ssa construction for the fpu modes */
296         rewire_fpu_mode_nodes(irg);
297
298         /* ensure correct fpu mode for operations */
299         be_assure_state(irg, &ia32_registers[REG_FPCW],
300                         NULL, create_fpu_mode_spill, create_fpu_mode_reload);
301 }