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