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