fixed syntax of fpa ldf/stf instructions
[libfirm] / ir / be / ia32 / ia32_fpu.c
1 /*
2  * Copyright (C) 1995-2007 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 "gen_ia32_regalloc_if.h"
38
39 #include "ircons.h"
40 #include "irgwalk.h"
41 #include "tv.h"
42 #include "array.h"
43
44 #include "../beirgmod.h"
45 #include "../bearch_t.h"
46 #include "../besched.h"
47 #include "../beabi.h"
48 #include "../benode_t.h"
49 #include "../bestate.h"
50 #include "../beutil.h"
51 #include "../bessaconstr.h"
52 #include "../beirg_t.h"
53
54 static ir_node *create_fpu_mode_spill(void *env, ir_node *state, int force,
55                                       ir_node *after)
56 {
57         ia32_code_gen_t *cg = env;
58         ir_node *spill = NULL;
59
60         if(force == 1 || !is_ia32_ChangeCW(state)) {
61                 ir_graph *irg = get_irn_irg(state);
62                 ir_node *block = get_nodes_block(state);
63                 ir_node *noreg = ia32_new_NoReg_gp(cg);
64                 ir_node *nomem = new_NoMem();
65                 ir_node *frame = get_irg_frame(irg);
66
67                 spill = new_rd_ia32_FnstCW(NULL, irg, block, frame, noreg, state,
68                                            nomem);
69                 set_ia32_am_support(spill, ia32_am_Dest);
70                 set_ia32_op_type(spill, ia32_AddrModeD);
71                 set_ia32_am_flavour(spill, ia32_B);
72                 set_ia32_ls_mode(spill, ia32_reg_classes[CLASS_ia32_fp_cw].mode);
73                 set_ia32_use_frame(spill);
74
75                 sched_add_after(after, spill);
76         }
77
78         return spill;
79 }
80
81 static ir_node *create_fpu_mode_reload(void *env, ir_node *state,
82                                        ir_node *spill, ir_node *before,
83                                        ir_node *last_state)
84 {
85         ia32_code_gen_t *cg = env;
86         ir_graph *irg = get_irn_irg(state);
87         ir_node *block = get_nodes_block(before);
88         ir_node *frame = get_irg_frame(irg);
89         ir_node *noreg = ia32_new_NoReg_gp(cg);
90         ir_node *reload = NULL;
91
92         if(spill != NULL) {
93                 reload = new_rd_ia32_FldCW(NULL, irg, block, frame, noreg, spill);
94                 set_ia32_am_support(reload, ia32_am_Source);
95                 set_ia32_op_type(reload, ia32_AddrModeS);
96                 set_ia32_am_flavour(reload, ia32_B);
97                 set_ia32_ls_mode(reload, ia32_reg_classes[CLASS_ia32_fp_cw].mode);
98                 set_ia32_use_frame(reload);
99                 arch_set_irn_register(cg->arch_env, reload, &ia32_fp_cw_regs[REG_FPCW]);
100
101                 sched_add_before(before, reload);
102         } else {
103                 ir_mode *lsmode = ia32_reg_classes[CLASS_ia32_fp_cw].mode;
104                 ir_node *nomem = new_NoMem();
105                 ir_node *cwstore, *load, *load_res, *or, *store, *fldcw;
106
107                 assert(last_state != NULL);
108                 cwstore = new_rd_ia32_FnstCW(NULL, irg, block, frame, noreg, last_state,
109                                              nomem);
110                 set_ia32_am_support(cwstore, ia32_am_Dest);
111                 set_ia32_op_type(cwstore, ia32_AddrModeD);
112                 set_ia32_am_flavour(cwstore, ia32_B);
113                 set_ia32_ls_mode(cwstore, lsmode);
114                 set_ia32_use_frame(cwstore);
115                 sched_add_before(before, cwstore);
116
117                 load = new_rd_ia32_Load(NULL, irg, block, frame, noreg, cwstore);
118                 set_ia32_am_support(load, ia32_am_Source);
119                 set_ia32_op_type(load, ia32_AddrModeS);
120                 set_ia32_am_flavour(load, ia32_B);
121                 set_ia32_ls_mode(load, lsmode);
122                 set_ia32_use_frame(load);
123                 sched_add_before(before, load);
124
125                 load_res = new_r_Proj(irg, block, load, mode_Iu, pn_ia32_Load_res);
126 #ifdef SCHEDULE_PROJS
127                 sched_add_before(before, load_res);
128 #endif
129
130                 /* TODO: make the actual mode configurable in ChangeCW... */
131                 or = new_rd_ia32_Or(NULL, irg, block, noreg, noreg, load_res, noreg,
132                                     nomem);
133                 set_ia32_Immop_tarval(or, new_tarval_from_long(3072, mode_Iu));
134                 sched_add_before(before, or);
135
136                 store = new_rd_ia32_Store(NULL, irg, block, frame, noreg, or, nomem);
137                 set_ia32_am_support(store, ia32_am_Dest);
138                 set_ia32_op_type(store, ia32_AddrModeD);
139                 set_ia32_am_flavour(store, ia32_B);
140                 set_ia32_ls_mode(store, lsmode);
141                 set_ia32_use_frame(store);
142                 sched_add_before(before, store);
143
144                 fldcw = new_rd_ia32_FldCW(NULL, irg, block, frame, noreg, store);
145                 set_ia32_am_support(fldcw, ia32_am_Source);
146                 set_ia32_op_type(fldcw, ia32_AddrModeS);
147                 set_ia32_am_flavour(fldcw, ia32_B);
148                 set_ia32_ls_mode(fldcw, lsmode);
149                 set_ia32_use_frame(fldcw);
150                 arch_set_irn_register(cg->arch_env, fldcw, &ia32_fp_cw_regs[REG_FPCW]);
151                 sched_add_before(before, fldcw);
152
153                 reload = fldcw;
154         }
155
156         return reload;
157 }
158
159 typedef struct collect_fpu_mode_nodes_env_t {
160         const arch_env_t *arch_env;
161         ir_node         **state_nodes;
162 } collect_fpu_mode_nodes_env_t;
163
164 static
165 void collect_fpu_mode_nodes_walker(ir_node *node, void *data)
166 {
167         collect_fpu_mode_nodes_env_t *env = data;
168
169         const arch_register_t *reg = arch_get_irn_register(env->arch_env, node);
170         if(reg == &ia32_fp_cw_regs[REG_FPCW] && !is_ia32_ChangeCW(node)) {
171                 ARR_APP1(ir_node*, env->state_nodes, node);
172         }
173 }
174
175 static
176 void rewire_fpu_mode_nodes(be_irg_t *birg)
177 {
178         collect_fpu_mode_nodes_env_t env;
179         be_ssa_construction_env_t senv;
180         const arch_register_t *reg = &ia32_fp_cw_regs[REG_FPCW];
181         ir_graph *irg = be_get_birg_irg(birg);
182         ir_node *initial_value;
183         ir_node **phis;
184         be_lv_t *lv = be_get_birg_liveness(birg);
185         int i, len;
186
187         /* do ssa construction for the fpu modes */
188         env.arch_env = be_get_birg_arch_env(birg);
189         env.state_nodes = NEW_ARR_F(ir_node*, 0);
190         irg_walk_graph(irg, collect_fpu_mode_nodes_walker, NULL, &env);
191
192         initial_value = be_abi_get_ignore_irn(birg->abi, reg);
193
194         /* nothing needs to be done, in fact we must not continue as for endless
195          * loops noone is using the initial_value and it will point to a bad node
196          * now
197          */
198         if(ARR_LEN(env.state_nodes) == 0) {
199                 DEL_ARR_F(env.state_nodes);
200                 return;
201         }
202
203         be_ssa_construction_init(&senv, birg);
204         be_ssa_construction_add_copies(&senv, env.state_nodes,
205                                        ARR_LEN(env.state_nodes));
206         be_ssa_construction_fix_users(&senv, initial_value);
207
208         if(lv != NULL) {
209                 be_ssa_construction_update_liveness_phis(&senv, lv);
210                 be_liveness_update(lv, initial_value);
211                 len = ARR_LEN(env.state_nodes);
212                 for(i = 0; i < len; ++i) {
213                         be_liveness_update(lv, env.state_nodes[i]);
214                 }
215         } else {
216                 be_liveness_invalidate(birg->lv);
217         }
218
219         /* set registers for the phis */
220         phis = be_ssa_construction_get_new_phis(&senv);
221         len = ARR_LEN(phis);
222         for(i = 0; i < len; ++i) {
223                 ir_node *phi = phis[i];
224                 be_set_phi_flags(env.arch_env, phi, arch_irn_flags_ignore);
225                 arch_set_irn_register(env.arch_env, phi, reg);
226         }
227         be_ssa_construction_destroy(&senv);
228         DEL_ARR_F(env.state_nodes);
229
230         be_liveness_invalidate(be_get_birg_liveness(birg));
231 }
232
233 void ia32_setup_fpu_mode(ia32_code_gen_t *cg)
234 {
235         /* do ssa construction for the fpu modes */
236         rewire_fpu_mode_nodes(cg->birg);
237
238         /* ensure correct fpu mode for operations */
239         be_assure_state(cg->birg, &ia32_fp_cw_regs[REG_FPCW],
240                         cg, create_fpu_mode_spill, create_fpu_mode_reload);
241 }