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