Fixed initialization of option tables
[libfirm] / ir / be / ia32 / bearch_ia32.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       This is the main ia32 firm backend driver.
23  * @author      Christian Wuerdig
24  * @version     $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <libcore/lc_opts.h>
31 #include <libcore/lc_opts_enum.h>
32
33 #include <math.h>
34
35 #include "pseudo_irg.h"
36 #include "irgwalk.h"
37 #include "irprog.h"
38 #include "irprintf.h"
39 #include "iredges_t.h"
40 #include "ircons.h"
41 #include "irgmod.h"
42 #include "irgopt.h"
43 #include "irbitset.h"
44 #include "irgopt.h"
45 #include "pdeq.h"
46 #include "pset.h"
47 #include "debug.h"
48 #include "error.h"
49 #include "xmalloc.h"
50
51 #include "../beabi.h"
52 #include "../beirg_t.h"
53 #include "../benode_t.h"
54 #include "../belower.h"
55 #include "../besched_t.h"
56 #include "be.h"
57 #include "../be_t.h"
58 #include "../beirgmod.h"
59 #include "../be_dbgout.h"
60 #include "../beblocksched.h"
61 #include "../bemachine.h"
62 #include "../beilpsched.h"
63 #include "../bespillslots.h"
64 #include "../bemodule.h"
65 #include "../begnuas.h"
66 #include "../bestate.h"
67
68 #include "bearch_ia32_t.h"
69
70 #include "ia32_new_nodes.h"
71 #include "gen_ia32_regalloc_if.h"
72 #include "gen_ia32_machine.h"
73 #include "ia32_transform.h"
74 #include "ia32_emitter.h"
75 #include "ia32_map_regs.h"
76 #include "ia32_optimize.h"
77 #include "ia32_x87.h"
78 #include "ia32_dbg_stat.h"
79 #include "ia32_finish.h"
80 #include "ia32_util.h"
81 #include "ia32_fpu.h"
82
83 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
84
85 /* TODO: ugly */
86 static set *cur_reg_set = NULL;
87
88 ir_mode *mode_fpcw = NULL;
89
90 typedef ir_node *(*create_const_node_func) (dbg_info *dbg, ir_graph *irg, ir_node *block);
91
92 static INLINE ir_node *create_const(ia32_code_gen_t *cg, ir_node **place,
93                                     create_const_node_func func,
94                                     const arch_register_t* reg)
95 {
96         ir_node *block, *res;
97
98         if(*place != NULL)
99                 return *place;
100
101         block = get_irg_start_block(cg->irg);
102         res = func(NULL, cg->irg, block);
103         arch_set_irn_register(cg->arch_env, res, reg);
104         *place = res;
105
106         add_irn_dep(get_irg_end(cg->irg), res);
107         /* add_irn_dep(get_irg_start(cg->irg), res); */
108
109         return res;
110 }
111
112 /* Creates the unique per irg GP NoReg node. */
113 ir_node *ia32_new_NoReg_gp(ia32_code_gen_t *cg) {
114         return create_const(cg, &cg->noreg_gp, new_rd_ia32_NoReg_GP,
115                             &ia32_gp_regs[REG_GP_NOREG]);
116 }
117
118 ir_node *ia32_new_NoReg_vfp(ia32_code_gen_t *cg) {
119         return create_const(cg, &cg->noreg_vfp, new_rd_ia32_NoReg_VFP,
120                             &ia32_vfp_regs[REG_VFP_NOREG]);
121 }
122
123 ir_node *ia32_new_NoReg_xmm(ia32_code_gen_t *cg) {
124         return create_const(cg, &cg->noreg_xmm, new_rd_ia32_NoReg_XMM,
125                             &ia32_xmm_regs[REG_XMM_NOREG]);
126 }
127
128 /* Creates the unique per irg FP NoReg node. */
129 ir_node *ia32_new_NoReg_fp(ia32_code_gen_t *cg) {
130         return USE_SSE2(cg) ? ia32_new_NoReg_xmm(cg) : ia32_new_NoReg_vfp(cg);
131 }
132
133 ir_node *ia32_new_Unknown_gp(ia32_code_gen_t *cg) {
134         return create_const(cg, &cg->unknown_gp, new_rd_ia32_Unknown_GP,
135                             &ia32_gp_regs[REG_GP_UKNWN]);
136 }
137
138 ir_node *ia32_new_Unknown_vfp(ia32_code_gen_t *cg) {
139         return create_const(cg, &cg->unknown_vfp, new_rd_ia32_Unknown_VFP,
140                             &ia32_vfp_regs[REG_VFP_UKNWN]);
141 }
142
143 ir_node *ia32_new_Unknown_xmm(ia32_code_gen_t *cg) {
144         return create_const(cg, &cg->unknown_xmm, new_rd_ia32_Unknown_XMM,
145                             &ia32_xmm_regs[REG_XMM_UKNWN]);
146 }
147
148 ir_node *ia32_new_Fpu_truncate(ia32_code_gen_t *cg) {
149         return create_const(cg, &cg->fpu_trunc_mode, new_rd_ia32_ChangeCW,
150                         &ia32_fp_cw_regs[REG_FPCW]);
151 }
152
153
154 /**
155  * Returns gp_noreg or fp_noreg, depending in input requirements.
156  */
157 ir_node *ia32_get_admissible_noreg(ia32_code_gen_t *cg, ir_node *irn, int pos) {
158         const arch_register_req_t *req;
159
160         req = arch_get_register_req(cg->arch_env, irn, pos);
161         assert(req != NULL && "Missing register requirements");
162         if (req->cls == &ia32_reg_classes[CLASS_ia32_gp])
163                 return ia32_new_NoReg_gp(cg);
164
165         return ia32_new_NoReg_fp(cg);
166 }
167
168 /**************************************************
169  *                         _ _              _  __
170  *                        | | |            (_)/ _|
171  *  _ __ ___  __ _    __ _| | | ___   ___   _| |_
172  * | '__/ _ \/ _` |  / _` | | |/ _ \ / __| | |  _|
173  * | | |  __/ (_| | | (_| | | | (_) | (__  | | |
174  * |_|  \___|\__, |  \__,_|_|_|\___/ \___| |_|_|
175  *            __/ |
176  *           |___/
177  **************************************************/
178
179 /**
180  * Return register requirements for an ia32 node.
181  * If the node returns a tuple (mode_T) then the proj's
182  * will be asked for this information.
183  */
184 static const arch_register_req_t *ia32_get_irn_reg_req(const void *self,
185                                                        const ir_node *node,
186                                                                                                            int pos) {
187         long node_pos = pos == -1 ? 0 : pos;
188         ir_mode *mode     = is_Block(node) ? NULL : get_irn_mode(node);
189         (void) self;
190
191         if (is_Block(node) || mode == mode_X) {
192                 return arch_no_register_req;
193         }
194
195         if (mode == mode_T && pos < 0) {
196                 return arch_no_register_req;
197         }
198
199         if (is_Proj(node)) {
200                 if(mode == mode_M)
201                         return arch_no_register_req;
202
203                 if(pos >= 0) {
204                         return arch_no_register_req;
205                 }
206
207                 node_pos = (pos == -1) ? get_Proj_proj(node) : pos;
208                 node     = skip_Proj_const(node);
209         }
210
211         if (is_ia32_irn(node)) {
212                 const arch_register_req_t *req;
213                 if(pos >= 0)
214                         req = get_ia32_in_req(node, pos);
215                 else
216                         req = get_ia32_out_req(node, node_pos);
217
218                 assert(req != NULL);
219
220                 return req;
221         }
222
223         /* unknowns should be transformed already */
224         assert(!is_Unknown(node));
225
226         return arch_no_register_req;
227 }
228
229 static void ia32_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg) {
230         int                   pos = 0;
231
232         if (get_irn_mode(irn) == mode_X) {
233                 return;
234         }
235
236         if (is_Proj(irn)) {
237                 pos = get_Proj_proj(irn);
238                 irn = skip_Proj(irn);
239         }
240
241         if (is_ia32_irn(irn)) {
242                 const arch_register_t **slots;
243
244                 slots      = get_ia32_slots(irn);
245                 slots[pos] = reg;
246         } else {
247                 ia32_set_firm_reg(irn, reg, cur_reg_set);
248         }
249 }
250
251 static const arch_register_t *ia32_get_irn_reg(const void *self, const ir_node *irn) {
252         int pos = 0;
253         const arch_register_t *reg = NULL;
254
255         if (is_Proj(irn)) {
256
257                 if (get_irn_mode(irn) == mode_X) {
258                         return NULL;
259                 }
260
261                 pos = get_Proj_proj(irn);
262                 irn = skip_Proj_const(irn);
263         }
264
265         if (is_ia32_irn(irn)) {
266                 const arch_register_t **slots;
267                 slots = get_ia32_slots(irn);
268                 reg   = slots[pos];
269         } else {
270                 reg = ia32_get_firm_reg(irn, cur_reg_set);
271         }
272
273         return reg;
274 }
275
276 static arch_irn_class_t ia32_classify(const void *self, const ir_node *irn) {
277         arch_irn_class_t classification = arch_irn_class_normal;
278
279         irn = skip_Proj_const(irn);
280
281         if (is_cfop(irn))
282                 classification |= arch_irn_class_branch;
283
284         if (! is_ia32_irn(irn))
285                 return classification & ~arch_irn_class_normal;
286
287         if (is_ia32_Cnst(irn))
288                 classification |= arch_irn_class_const;
289
290         if (is_ia32_Ld(irn))
291                 classification |= arch_irn_class_load;
292
293         if (is_ia32_St(irn))
294                 classification |= arch_irn_class_store;
295
296         if (is_ia32_need_stackent(irn))
297                 classification |= arch_irn_class_reload;
298
299         return classification;
300 }
301
302 static arch_irn_flags_t ia32_get_flags(const void *self, const ir_node *irn) {
303         arch_irn_flags_t flags = arch_irn_flags_none;
304
305         if (is_Unknown(irn))
306                 return arch_irn_flags_ignore;
307
308         if(is_Proj(irn) && mode_is_datab(get_irn_mode(irn))) {
309                 ir_node *pred = get_Proj_pred(irn);
310
311                 if(is_ia32_irn(pred)) {
312                         flags = get_ia32_out_flags(pred, get_Proj_proj(irn));
313                 }
314
315                 irn = pred;
316         }
317
318         if (is_ia32_irn(irn)) {
319                 flags |= get_ia32_flags(irn);
320         }
321
322         return flags;
323 }
324
325 /**
326  * The IA32 ABI callback object.
327  */
328 typedef struct {
329         be_abi_call_flags_bits_t flags;  /**< The call flags. */
330         const arch_isa_t *isa;           /**< The ISA handle. */
331         const arch_env_t *aenv;          /**< The architecture environment. */
332         ir_graph *irg;                   /**< The associated graph. */
333 } ia32_abi_env_t;
334
335 static ir_entity *ia32_get_frame_entity(const void *self, const ir_node *irn) {
336         return is_ia32_irn(irn) ? get_ia32_frame_ent(irn) : NULL;
337 }
338
339 static void ia32_set_frame_entity(const void *self, ir_node *irn, ir_entity *ent) {
340         set_ia32_frame_ent(irn, ent);
341 }
342
343 static void ia32_set_frame_offset(const void *self, ir_node *irn, int bias) {
344         const ia32_irn_ops_t *ops = self;
345
346         if (get_ia32_frame_ent(irn)) {
347                 ia32_am_flavour_t am_flav;
348
349                 if (is_ia32_Pop(irn)) {
350                         int omit_fp = be_abi_omit_fp(ops->cg->birg->abi);
351                         if (omit_fp) {
352                                 /* Pop nodes modify the stack pointer before calculating the destination
353                                  * address, so fix this here
354                                  */
355                                 bias -= 4;
356                         }
357                 }
358
359                 am_flav  = get_ia32_am_flavour(irn);
360                 am_flav |= ia32_O;
361                 set_ia32_am_flavour(irn, am_flav);
362
363                 add_ia32_am_offs_int(irn, bias);
364         }
365 }
366
367 static int ia32_get_sp_bias(const void *self, const ir_node *irn) {
368         if(is_Proj(irn)) {
369                 long proj = get_Proj_proj(irn);
370                 ir_node *pred = get_Proj_pred(irn);
371
372                 if (is_ia32_Push(pred) && proj == pn_ia32_Push_stack)
373                         return 4;
374                 if (is_ia32_Pop(pred) && proj == pn_ia32_Pop_stack)
375                         return -4;
376         }
377
378         return 0;
379 }
380
381 /**
382  * Put all registers which are saved by the prologue/epilogue in a set.
383  *
384  * @param self  The callback object.
385  * @param s     The result set.
386  */
387 static void ia32_abi_dont_save_regs(void *self, pset *s)
388 {
389         ia32_abi_env_t *env = self;
390         if(env->flags.try_omit_fp)
391                 pset_insert_ptr(s, env->isa->bp);
392 }
393
394 /**
395  * Generate the routine prologue.
396  *
397  * @param self    The callback object.
398  * @param mem     A pointer to the mem node. Update this if you define new memory.
399  * @param reg_map A map mapping all callee_save/ignore/parameter registers to their defining nodes.
400  *
401  * @return        The register which shall be used as a stack frame base.
402  *
403  * All nodes which define registers in @p reg_map must keep @p reg_map current.
404  */
405 static const arch_register_t *ia32_abi_prologue(void *self, ir_node **mem, pmap *reg_map)
406 {
407         ia32_abi_env_t *env = self;
408         const ia32_isa_t *isa     = (ia32_isa_t *)env->isa;
409         ia32_code_gen_t *cg = isa->cg;
410
411         if (! env->flags.try_omit_fp) {
412                 ir_node *bl      = get_irg_start_block(env->irg);
413                 ir_node *curr_sp = be_abi_reg_map_get(reg_map, env->isa->sp);
414                 ir_node *curr_bp = be_abi_reg_map_get(reg_map, env->isa->bp);
415                 ir_node *noreg = ia32_new_NoReg_gp(cg);
416                 ir_node *push;
417
418                 /* ALL nodes representing bp must be set to ignore. */
419                 be_node_set_flags(get_Proj_pred(curr_bp), BE_OUT_POS(get_Proj_proj(curr_bp)), arch_irn_flags_ignore);
420
421                 /* push ebp */
422                 push    = new_rd_ia32_Push(NULL, env->irg, bl, noreg, noreg, curr_bp, curr_sp, *mem);
423                 curr_sp = new_r_Proj(env->irg, bl, push, get_irn_mode(curr_sp), pn_ia32_Push_stack);
424                 *mem    = new_r_Proj(env->irg, bl, push, mode_M, pn_ia32_Push_M);
425
426                 /* the push must have SP out register */
427                 arch_set_irn_register(env->aenv, curr_sp, env->isa->sp);
428                 set_ia32_flags(push, arch_irn_flags_ignore);
429
430                 /* move esp to ebp */
431                 curr_bp  = be_new_Copy(env->isa->bp->reg_class, env->irg, bl, curr_sp);
432                 be_set_constr_single_reg(curr_bp, BE_OUT_POS(0), env->isa->bp);
433                 arch_set_irn_register(env->aenv, curr_bp, env->isa->bp);
434                 be_node_set_flags(curr_bp, BE_OUT_POS(0), arch_irn_flags_ignore);
435
436                 /* beware: the copy must be done before any other sp use */
437                 curr_sp = be_new_CopyKeep_single(env->isa->sp->reg_class, env->irg, bl, curr_sp, curr_bp, get_irn_mode(curr_sp));
438                 be_set_constr_single_reg(curr_sp, BE_OUT_POS(0), env->isa->sp);
439                 arch_set_irn_register(env->aenv, curr_sp, env->isa->sp);
440                 be_node_set_flags(curr_sp, BE_OUT_POS(0), arch_irn_flags_ignore);
441
442                 be_abi_reg_map_set(reg_map, env->isa->sp, curr_sp);
443                 be_abi_reg_map_set(reg_map, env->isa->bp, curr_bp);
444
445                 return env->isa->bp;
446         }
447
448         return env->isa->sp;
449 }
450
451 /**
452  * Generate the routine epilogue.
453  * @param self    The callback object.
454  * @param bl      The block for the epilog
455  * @param mem     A pointer to the mem node. Update this if you define new memory.
456  * @param reg_map A map mapping all callee_save/ignore/parameter registers to their defining nodes.
457  * @return        The register which shall be used as a stack frame base.
458  *
459  * All nodes which define registers in @p reg_map must keep @p reg_map current.
460  */
461 static void ia32_abi_epilogue(void *self, ir_node *bl, ir_node **mem, pmap *reg_map)
462 {
463         ia32_abi_env_t *env     = self;
464         ir_node        *curr_sp = be_abi_reg_map_get(reg_map, env->isa->sp);
465         ir_node        *curr_bp = be_abi_reg_map_get(reg_map, env->isa->bp);
466
467         if (env->flags.try_omit_fp) {
468                 /* simply remove the stack frame here */
469                 curr_sp = be_new_IncSP(env->isa->sp, env->irg, bl, curr_sp, BE_STACK_FRAME_SIZE_SHRINK);
470                 add_irn_dep(curr_sp, *mem);
471         } else {
472                 const ia32_isa_t *isa     = (ia32_isa_t *)env->isa;
473                 ia32_code_gen_t *cg = isa->cg;
474                 ir_mode          *mode_bp = env->isa->bp->reg_class->mode;
475
476                 /* gcc always emits a leave at the end of a routine */
477                 if (1 || ARCH_AMD(isa->opt_arch)) {
478                         ir_node *leave;
479
480                         /* leave */
481                         leave   = new_rd_ia32_Leave(NULL, env->irg, bl, curr_sp, curr_bp);
482                         set_ia32_flags(leave, arch_irn_flags_ignore);
483                         curr_bp = new_r_Proj(current_ir_graph, bl, leave, mode_bp, pn_ia32_Leave_frame);
484                         curr_sp = new_r_Proj(current_ir_graph, bl, leave, get_irn_mode(curr_sp), pn_ia32_Leave_stack);
485                 } else {
486                         ir_node *noreg = ia32_new_NoReg_gp(cg);
487                         ir_node *pop;
488
489                         /* copy ebp to esp */
490                         curr_sp = be_new_SetSP(env->isa->sp, env->irg, bl, curr_sp, curr_bp, *mem);
491
492                         /* pop ebp */
493                         pop     = new_rd_ia32_Pop(NULL, env->irg, bl, noreg, noreg, curr_sp, *mem);
494                         set_ia32_flags(pop, arch_irn_flags_ignore);
495                         curr_bp = new_r_Proj(current_ir_graph, bl, pop, mode_bp, pn_ia32_Pop_res);
496                         curr_sp = new_r_Proj(current_ir_graph, bl, pop, get_irn_mode(curr_sp), pn_ia32_Pop_stack);
497
498                         *mem = new_r_Proj(current_ir_graph, bl, pop, mode_M, pn_ia32_Pop_M);
499                 }
500                 arch_set_irn_register(env->aenv, curr_sp, env->isa->sp);
501                 arch_set_irn_register(env->aenv, curr_bp, env->isa->bp);
502         }
503
504         be_abi_reg_map_set(reg_map, env->isa->sp, curr_sp);
505         be_abi_reg_map_set(reg_map, env->isa->bp, curr_bp);
506 }
507
508 /**
509  * Initialize the callback object.
510  * @param call The call object.
511  * @param aenv The architecture environment.
512  * @param irg  The graph with the method.
513  * @return     Some pointer. This pointer is passed to all other callback functions as self object.
514  */
515 static void *ia32_abi_init(const be_abi_call_t *call, const arch_env_t *aenv, ir_graph *irg)
516 {
517         ia32_abi_env_t *env    = xmalloc(sizeof(env[0]));
518         be_abi_call_flags_t fl = be_abi_call_get_flags(call);
519         env->flags = fl.bits;
520         env->irg   = irg;
521         env->aenv  = aenv;
522         env->isa   = aenv->isa;
523         return env;
524 }
525
526 /**
527  * Destroy the callback object.
528  * @param self The callback object.
529  */
530 static void ia32_abi_done(void *self) {
531         free(self);
532 }
533
534 /**
535  * Produces the type which sits between the stack args and the locals on the stack.
536  * it will contain the return address and space to store the old base pointer.
537  * @return The Firm type modeling the ABI between type.
538  */
539 static ir_type *ia32_abi_get_between_type(void *self)
540 {
541 #define IDENT(s) new_id_from_chars(s, sizeof(s)-1)
542         static ir_type *omit_fp_between_type = NULL;
543         static ir_type *between_type         = NULL;
544
545         ia32_abi_env_t *env = self;
546
547         if (! between_type) {
548                 ir_entity *old_bp_ent;
549                 ir_entity *ret_addr_ent;
550                 ir_entity *omit_fp_ret_addr_ent;
551
552                 ir_type *old_bp_type   = new_type_primitive(IDENT("bp"), mode_Iu);
553                 ir_type *ret_addr_type = new_type_primitive(IDENT("return_addr"), mode_Iu);
554
555                 between_type           = new_type_struct(IDENT("ia32_between_type"));
556                 old_bp_ent             = new_entity(between_type, IDENT("old_bp"), old_bp_type);
557                 ret_addr_ent           = new_entity(between_type, IDENT("ret_addr"), ret_addr_type);
558
559                 set_entity_offset(old_bp_ent, 0);
560                 set_entity_offset(ret_addr_ent, get_type_size_bytes(old_bp_type));
561                 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
562                 set_type_state(between_type, layout_fixed);
563
564                 omit_fp_between_type = new_type_struct(IDENT("ia32_between_type_omit_fp"));
565                 omit_fp_ret_addr_ent = new_entity(omit_fp_between_type, IDENT("ret_addr"), ret_addr_type);
566
567                 set_entity_offset(omit_fp_ret_addr_ent, 0);
568                 set_type_size_bytes(omit_fp_between_type, get_type_size_bytes(ret_addr_type));
569                 set_type_state(omit_fp_between_type, layout_fixed);
570         }
571
572         return env->flags.try_omit_fp ? omit_fp_between_type : between_type;
573 #undef IDENT
574 }
575
576 /**
577  * Get the estimated cycle count for @p irn.
578  *
579  * @param self The this pointer.
580  * @param irn  The node.
581  *
582  * @return     The estimated cycle count for this operation
583  */
584 static int ia32_get_op_estimated_cost(const void *self, const ir_node *irn)
585 {
586         int cost;
587         ia32_op_type_t op_tp;
588         const ia32_irn_ops_t *ops = self;
589
590         if (is_Proj(irn))
591                 return 0;
592         if (!is_ia32_irn(irn))
593                 return 0;
594
595         assert(is_ia32_irn(irn));
596
597         cost  = get_ia32_latency(irn);
598         op_tp = get_ia32_op_type(irn);
599
600         if (is_ia32_CopyB(irn)) {
601                 cost = 250;
602                 if (ARCH_INTEL(ops->cg->arch))
603                         cost += 150;
604         }
605         else if (is_ia32_CopyB_i(irn)) {
606                 int size = get_tarval_long(get_ia32_Immop_tarval(irn));
607                 cost     = 20 + (int)ceil((4/3) * size);
608                 if (ARCH_INTEL(ops->cg->arch))
609                         cost += 150;
610         }
611         /* in case of address mode operations add additional cycles */
612         else if (op_tp == ia32_AddrModeD || op_tp == ia32_AddrModeS) {
613                 /*
614                         In case of stack access add 5 cycles (we assume stack is in cache),
615                         other memory operations cost 20 cycles.
616                 */
617                 cost += is_ia32_use_frame(irn) ? 5 : 20;
618         }
619
620         return cost;
621 }
622
623 /**
624  * Returns the inverse operation if @p irn, recalculating the argument at position @p i.
625  *
626  * @param irn       The original operation
627  * @param i         Index of the argument we want the inverse operation to yield
628  * @param inverse   struct to be filled with the resulting inverse op
629  * @param obstack   The obstack to use for allocation of the returned nodes array
630  * @return          The inverse operation or NULL if operation invertible
631  */
632 static arch_inverse_t *ia32_get_inverse(const void *self, const ir_node *irn, int i, arch_inverse_t *inverse, struct obstack *obst) {
633         ir_graph *irg;
634         ir_mode  *mode;
635         ir_mode  *irn_mode;
636         ir_node  *block, *noreg, *nomem;
637         dbg_info *dbg;
638
639         /* we cannot invert non-ia32 irns */
640         if (! is_ia32_irn(irn))
641                 return NULL;
642
643         /* operand must always be a real operand (not base, index or mem) */
644         if (i != 2 && i != 3)
645                 return NULL;
646
647         /* we don't invert address mode operations */
648         if (get_ia32_op_type(irn) != ia32_Normal)
649                 return NULL;
650
651         irg      = get_irn_irg(irn);
652         block    = get_nodes_block(irn);
653         mode     = get_irn_mode(irn);
654         irn_mode = get_irn_mode(irn);
655         noreg    = get_irn_n(irn, 0);
656         nomem    = new_r_NoMem(irg);
657         dbg      = get_irn_dbg_info(irn);
658
659         /* initialize structure */
660         inverse->nodes = obstack_alloc(obst, 2 * sizeof(inverse->nodes[0]));
661         inverse->costs = 0;
662         inverse->n     = 1;
663
664         switch (get_ia32_irn_opcode(irn)) {
665                 case iro_ia32_Add:
666                         if (get_ia32_immop_type(irn) == ia32_ImmConst) {
667                                 /* we have an add with a const here */
668                                 /* invers == add with negated const */
669                                 inverse->nodes[0] = new_rd_ia32_Add(dbg, irg, block, noreg, noreg, get_irn_n(irn, i), noreg, nomem);
670                                 inverse->costs   += 1;
671                                 copy_ia32_Immop_attr(inverse->nodes[0], (ir_node *)irn);
672                                 set_ia32_Immop_tarval(inverse->nodes[0], tarval_neg(get_ia32_Immop_tarval(irn)));
673                                 set_ia32_commutative(inverse->nodes[0]);
674                         }
675                         else if (get_ia32_immop_type(irn) == ia32_ImmSymConst) {
676                                 /* we have an add with a symconst here */
677                                 /* invers == sub with const */
678                                 inverse->nodes[0] = new_rd_ia32_Sub(dbg, irg, block, noreg, noreg, get_irn_n(irn, i), noreg, nomem);
679                                 inverse->costs   += 2;
680                                 copy_ia32_Immop_attr(inverse->nodes[0], (ir_node *)irn);
681                         }
682                         else {
683                                 /* normal add: inverse == sub */
684                                 inverse->nodes[0] = new_rd_ia32_Sub(dbg, irg, block, noreg, noreg, (ir_node*) irn, get_irn_n(irn, i ^ 1), nomem);
685                                 inverse->costs   += 2;
686                         }
687                         break;
688                 case iro_ia32_Sub:
689                         if (get_ia32_immop_type(irn) != ia32_ImmNone) {
690                                 /* we have a sub with a const/symconst here */
691                                 /* invers == add with this const */
692                                 inverse->nodes[0] = new_rd_ia32_Add(dbg, irg, block, noreg, noreg, get_irn_n(irn, i), noreg, nomem);
693                                 inverse->costs   += (get_ia32_immop_type(irn) == ia32_ImmSymConst) ? 5 : 1;
694                                 copy_ia32_Immop_attr(inverse->nodes[0], (ir_node *)irn);
695                         }
696                         else {
697                                 /* normal sub */
698                                 if (i == 2) {
699                                         inverse->nodes[0] = new_rd_ia32_Add(dbg, irg, block, noreg, noreg, (ir_node*) irn, get_irn_n(irn, 3), nomem);
700                                 }
701                                 else {
702                                         inverse->nodes[0] = new_rd_ia32_Sub(dbg, irg, block, noreg, noreg, get_irn_n(irn, 2), (ir_node*) irn, nomem);
703                                 }
704                                 inverse->costs += 1;
705                         }
706                         break;
707                 case iro_ia32_Xor:
708                         if (get_ia32_immop_type(irn) != ia32_ImmNone) {
709                                 /* xor with const: inverse = xor */
710                                 inverse->nodes[0] = new_rd_ia32_Xor(dbg, irg, block, noreg, noreg, get_irn_n(irn, i), noreg, nomem);
711                                 inverse->costs   += (get_ia32_immop_type(irn) == ia32_ImmSymConst) ? 5 : 1;
712                                 copy_ia32_Immop_attr(inverse->nodes[0], (ir_node *)irn);
713                         }
714                         else {
715                                 /* normal xor */
716                                 inverse->nodes[0] = new_rd_ia32_Xor(dbg, irg, block, noreg, noreg, (ir_node *) irn, get_irn_n(irn, i), nomem);
717                                 inverse->costs   += 1;
718                         }
719                         break;
720                 case iro_ia32_Not: {
721                         inverse->nodes[0] = new_rd_ia32_Not(dbg, irg, block, noreg, noreg, (ir_node*) irn, nomem);
722                         inverse->costs   += 1;
723                         break;
724                 }
725                 case iro_ia32_Neg: {
726                         inverse->nodes[0] = new_rd_ia32_Neg(dbg, irg, block, noreg, noreg, (ir_node*) irn, nomem);
727                         inverse->costs   += 1;
728                         break;
729                 }
730                 default:
731                         /* inverse operation not supported */
732                         return NULL;
733         }
734
735         return inverse;
736 }
737
738 static ir_mode *get_spill_mode_mode(const ir_mode *mode)
739 {
740         if(mode_is_float(mode))
741                 return mode_D;
742
743         return mode_Iu;
744 }
745
746 /**
747  * Get the mode that should be used for spilling value node
748  */
749 static ir_mode *get_spill_mode(const ir_node *node)
750 {
751         ir_mode *mode = get_irn_mode(node);
752         return get_spill_mode_mode(mode);
753 }
754
755 /**
756  * Checks whether an addressmode reload for a node with mode mode is compatible
757  * with a spillslot of mode spill_mode
758  */
759 static int ia32_is_spillmode_compatible(const ir_mode *mode, const ir_mode *spillmode)
760 {
761         if(mode_is_float(mode)) {
762                 return mode == spillmode;
763         } else {
764                 return 1;
765         }
766 }
767
768 /**
769  * Check if irn can load it's operand at position i from memory (source addressmode).
770  * @param self   Pointer to irn ops itself
771  * @param irn    The irn to be checked
772  * @param i      The operands position
773  * @return Non-Zero if operand can be loaded
774  */
775 static int ia32_possible_memory_operand(const void *self, const ir_node *irn, unsigned int i) {
776         ir_node *op = get_irn_n(irn, i);
777         const ir_mode *mode = get_irn_mode(op);
778         const ir_mode *spillmode = get_spill_mode(op);
779
780         if (! is_ia32_irn(irn)                            ||  /* must be an ia32 irn */
781                 get_irn_arity(irn) != 5                       ||  /* must be a binary operation */
782                 get_ia32_op_type(irn) != ia32_Normal          ||  /* must not already be a addressmode irn */
783                 ! (get_ia32_am_support(irn) & ia32_am_Source) ||  /* must be capable of source addressmode */
784                 ! ia32_is_spillmode_compatible(mode, spillmode) ||
785                 (i != 2 && i != 3)                            ||  /* a "real" operand position must be requested */
786                 (i == 2 && ! is_ia32_commutative(irn))        ||  /* if first operand requested irn must be commutative */
787                 is_ia32_use_frame(irn))                           /* must not already use frame */
788                 return 0;
789
790         return 1;
791 }
792
793 static void ia32_perform_memory_operand(const void *self, ir_node *irn, ir_node *spill, unsigned int i) {
794         const ia32_irn_ops_t *ops = self;
795         ia32_code_gen_t      *cg  = ops->cg;
796
797         assert(ia32_possible_memory_operand(self, irn, i) && "Cannot perform memory operand change");
798
799         if (i == 2) {
800                 ir_node *tmp = get_irn_n(irn, 3);
801                 set_irn_n(irn, 3, get_irn_n(irn, 2));
802                 set_irn_n(irn, 2, tmp);
803         }
804
805         set_ia32_am_support(irn, ia32_am_Source);
806         set_ia32_op_type(irn, ia32_AddrModeS);
807         set_ia32_am_flavour(irn, ia32_B);
808         set_ia32_ls_mode(irn, get_irn_mode(get_irn_n(irn, i)));
809         set_ia32_use_frame(irn);
810         set_ia32_need_stackent(irn);
811
812         set_irn_n(irn, 0, get_irg_frame(get_irn_irg(irn)));
813         set_irn_n(irn, 3, ia32_get_admissible_noreg(cg, irn, 3));
814         set_irn_n(irn, 4, spill);
815
816         //FIXME DBG_OPT_AM_S(reload, irn);
817 }
818
819 static const be_abi_callbacks_t ia32_abi_callbacks = {
820         ia32_abi_init,
821         ia32_abi_done,
822         ia32_abi_get_between_type,
823         ia32_abi_dont_save_regs,
824         ia32_abi_prologue,
825         ia32_abi_epilogue
826 };
827
828 /* fill register allocator interface */
829
830 static const arch_irn_ops_if_t ia32_irn_ops_if = {
831         ia32_get_irn_reg_req,
832         ia32_set_irn_reg,
833         ia32_get_irn_reg,
834         ia32_classify,
835         ia32_get_flags,
836         ia32_get_frame_entity,
837         ia32_set_frame_entity,
838         ia32_set_frame_offset,
839         ia32_get_sp_bias,
840         ia32_get_inverse,
841         ia32_get_op_estimated_cost,
842         ia32_possible_memory_operand,
843         ia32_perform_memory_operand,
844 };
845
846 ia32_irn_ops_t ia32_irn_ops = {
847         &ia32_irn_ops_if,
848         NULL
849 };
850
851
852
853 /**************************************************
854  *                _                         _  __
855  *               | |                       (_)/ _|
856  *   ___ ___   __| | ___  __ _  ___ _ __    _| |_
857  *  / __/ _ \ / _` |/ _ \/ _` |/ _ \ '_ \  | |  _|
858  * | (_| (_) | (_| |  __/ (_| |  __/ | | | | | |
859  *  \___\___/ \__,_|\___|\__, |\___|_| |_| |_|_|
860  *                        __/ |
861  *                       |___/
862  **************************************************/
863
864 /**
865  * Transforms the standard firm graph into
866  * an ia32 firm graph
867  */
868 static void ia32_prepare_graph(void *self) {
869         ia32_code_gen_t *cg = self;
870
871         /* transform nodes into assembler instructions */
872         ia32_transform_graph(cg);
873
874         /* do local optimisations (mainly CSE) */
875         local_optimize_graph(cg->irg);
876
877         if (cg->dump)
878                 be_dump(cg->irg, "-transformed", dump_ir_block_graph_sched);
879
880         /* optimize address mode */
881         ia32_optimize_graph(cg);
882
883         if (cg->dump)
884                 be_dump(cg->irg, "-am", dump_ir_block_graph_sched);
885
886         /* do code placement, to optimize the position of constants */
887         place_code(cg->irg);
888
889         if (cg->dump)
890                 be_dump(cg->irg, "-place", dump_ir_block_graph_sched);
891 }
892
893 /**
894  * Dummy functions for hooks we don't need but which must be filled.
895  */
896 static void ia32_before_sched(void *self) {
897 }
898
899 /**
900  * Called before the register allocator.
901  * Calculate a block schedule here. We need it for the x87
902  * simulator and the emitter.
903  */
904 static void ia32_before_ra(void *self) {
905         ia32_code_gen_t *cg              = self;
906
907         /* setup fpu rounding modes */
908         ia32_setup_fpu_mode(cg);
909 }
910
911
912 /**
913  * Transforms a be_Reload into a ia32 Load.
914  */
915 static void transform_to_Load(ia32_code_gen_t *cg, ir_node *node) {
916         ir_graph *irg        = get_irn_irg(node);
917         dbg_info *dbg        = get_irn_dbg_info(node);
918         ir_node *block       = get_nodes_block(node);
919         ir_entity *ent       = be_get_frame_entity(node);
920         ir_mode *mode        = get_irn_mode(node);
921         ir_mode *spillmode   = get_spill_mode(node);
922         ir_node *noreg       = ia32_new_NoReg_gp(cg);
923         ir_node *sched_point = NULL;
924         ir_node *ptr         = get_irg_frame(irg);
925         ir_node *mem         = get_irn_n(node, be_pos_Reload_mem);
926         ir_node *new_op, *proj;
927         const arch_register_t *reg;
928
929         if (sched_is_scheduled(node)) {
930                 sched_point = sched_prev(node);
931         }
932
933         if (mode_is_float(spillmode)) {
934                 if (USE_SSE2(cg))
935                         new_op = new_rd_ia32_xLoad(dbg, irg, block, ptr, noreg, mem);
936                 else
937                         new_op = new_rd_ia32_vfld(dbg, irg, block, ptr, noreg, mem);
938         }
939         else if (get_mode_size_bits(spillmode) == 128) {
940                 // Reload 128 bit sse registers
941                 new_op = new_rd_ia32_xxLoad(dbg, irg, block, ptr, noreg, mem);
942         }
943         else
944                 new_op = new_rd_ia32_Load(dbg, irg, block, ptr, noreg, mem);
945
946         set_ia32_am_support(new_op, ia32_am_Source);
947         set_ia32_op_type(new_op, ia32_AddrModeS);
948         set_ia32_am_flavour(new_op, ia32_B);
949         set_ia32_ls_mode(new_op, spillmode);
950         set_ia32_frame_ent(new_op, ent);
951         set_ia32_use_frame(new_op);
952
953         DBG_OPT_RELOAD2LD(node, new_op);
954
955         proj = new_rd_Proj(dbg, irg, block, new_op, mode, pn_ia32_Load_res);
956
957         if (sched_point) {
958                 sched_add_after(sched_point, new_op);
959                 sched_add_after(new_op, proj);
960
961                 sched_remove(node);
962         }
963
964         /* copy the register from the old node to the new Load */
965         reg = arch_get_irn_register(cg->arch_env, node);
966         arch_set_irn_register(cg->arch_env, new_op, reg);
967
968         SET_IA32_ORIG_NODE(new_op, ia32_get_old_node_name(cg, node));
969
970         exchange(node, proj);
971 }
972
973 /**
974  * Transforms a be_Spill node into a ia32 Store.
975  */
976 static void transform_to_Store(ia32_code_gen_t *cg, ir_node *node) {
977         ir_graph *irg  = get_irn_irg(node);
978         dbg_info *dbg  = get_irn_dbg_info(node);
979         ir_node *block = get_nodes_block(node);
980         ir_entity *ent = be_get_frame_entity(node);
981         const ir_node *spillval = get_irn_n(node, be_pos_Spill_val);
982         ir_mode *mode  = get_spill_mode(spillval);
983         ir_node *noreg = ia32_new_NoReg_gp(cg);
984         ir_node *nomem = new_rd_NoMem(irg);
985         ir_node *ptr   = get_irg_frame(irg);
986         ir_node *val   = get_irn_n(node, be_pos_Spill_val);
987         ir_node *store;
988         ir_node *sched_point = NULL;
989
990         if (sched_is_scheduled(node)) {
991                 sched_point = sched_prev(node);
992         }
993
994         /* No need to spill unknown values... */
995         if(is_ia32_Unknown_GP(val) ||
996                 is_ia32_Unknown_VFP(val) ||
997                 is_ia32_Unknown_XMM(val)) {
998                 store = nomem;
999                 if(sched_point)
1000                         sched_remove(node);
1001
1002                 exchange(node, store);
1003                 return;
1004         }
1005
1006         if (mode_is_float(mode)) {
1007                 if (USE_SSE2(cg))
1008                         store = new_rd_ia32_xStore(dbg, irg, block, ptr, noreg, val, nomem);
1009                 else
1010                         store = new_rd_ia32_vfst(dbg, irg, block, ptr, noreg, val, nomem);
1011         } else if (get_mode_size_bits(mode) == 128) {
1012                 // Spill 128 bit SSE registers
1013                 store = new_rd_ia32_xxStore(dbg, irg, block, ptr, noreg, val, nomem);
1014         } else if (get_mode_size_bits(mode) == 8) {
1015                 store = new_rd_ia32_Store8Bit(dbg, irg, block, ptr, noreg, val, nomem);
1016         } else {
1017                 store = new_rd_ia32_Store(dbg, irg, block, ptr, noreg, val, nomem);
1018         }
1019
1020         set_ia32_am_support(store, ia32_am_Dest);
1021         set_ia32_op_type(store, ia32_AddrModeD);
1022         set_ia32_am_flavour(store, ia32_B);
1023         set_ia32_ls_mode(store, mode);
1024         set_ia32_frame_ent(store, ent);
1025         set_ia32_use_frame(store);
1026         SET_IA32_ORIG_NODE(store, ia32_get_old_node_name(cg, node));
1027         DBG_OPT_SPILL2ST(node, store);
1028
1029         if (sched_point) {
1030                 sched_add_after(sched_point, store);
1031                 sched_remove(node);
1032         }
1033
1034         exchange(node, store);
1035 }
1036
1037 static ir_node *create_push(ia32_code_gen_t *cg, ir_node *node, ir_node *schedpoint, ir_node *sp, ir_node *mem, ir_entity *ent) {
1038         ir_graph *irg = get_irn_irg(node);
1039         dbg_info *dbg = get_irn_dbg_info(node);
1040         ir_node *block = get_nodes_block(node);
1041         ir_node *noreg = ia32_new_NoReg_gp(cg);
1042         ir_node *frame = get_irg_frame(irg);
1043
1044         ir_node *push = new_rd_ia32_Push(dbg, irg, block, frame, noreg, noreg, sp, mem);
1045
1046         set_ia32_frame_ent(push, ent);
1047         set_ia32_use_frame(push);
1048         set_ia32_op_type(push, ia32_AddrModeS);
1049         set_ia32_am_flavour(push, ia32_B);
1050         set_ia32_ls_mode(push, mode_Is);
1051
1052         sched_add_before(schedpoint, push);
1053         return push;
1054 }
1055
1056 static ir_node *create_pop(ia32_code_gen_t *cg, ir_node *node, ir_node *schedpoint, ir_node *sp, ir_entity *ent) {
1057         ir_graph *irg = get_irn_irg(node);
1058         dbg_info *dbg = get_irn_dbg_info(node);
1059         ir_node *block = get_nodes_block(node);
1060         ir_node *noreg = ia32_new_NoReg_gp(cg);
1061         ir_node *frame = get_irg_frame(irg);
1062
1063         ir_node *pop = new_rd_ia32_Pop(dbg, irg, block, frame, noreg, sp, new_NoMem());
1064
1065         set_ia32_frame_ent(pop, ent);
1066         set_ia32_use_frame(pop);
1067         set_ia32_op_type(pop, ia32_AddrModeD);
1068         set_ia32_am_flavour(pop, ia32_am_OB);
1069         set_ia32_ls_mode(pop, mode_Is);
1070
1071         sched_add_before(schedpoint, pop);
1072
1073         return pop;
1074 }
1075
1076 static ir_node* create_spproj(ia32_code_gen_t *cg, ir_node *node, ir_node *pred, int pos, ir_node *schedpoint) {
1077         ir_graph *irg = get_irn_irg(node);
1078         dbg_info *dbg = get_irn_dbg_info(node);
1079         ir_node *block = get_nodes_block(node);
1080         ir_mode *spmode = mode_Iu;
1081         const arch_register_t *spreg = &ia32_gp_regs[REG_ESP];
1082         ir_node *sp;
1083
1084         sp = new_rd_Proj(dbg, irg, block, pred, spmode, pos);
1085         arch_set_irn_register(cg->arch_env, sp, spreg);
1086         sched_add_before(schedpoint, sp);
1087
1088         return sp;
1089 }
1090
1091 /**
1092  * Transform memperm, currently we do this the ugly way and produce
1093  * push/pop into/from memory cascades. This is possible without using
1094  * any registers.
1095  */
1096 static void transform_MemPerm(ia32_code_gen_t *cg, ir_node *node) {
1097         ir_graph *irg = get_irn_irg(node);
1098         ir_node *block = get_nodes_block(node);
1099         ir_node *in[1];
1100         ir_node *keep;
1101         int i, arity;
1102         ir_node *sp = be_abi_get_ignore_irn(cg->birg->abi, &ia32_gp_regs[REG_ESP]);
1103         const ir_edge_t *edge;
1104         const ir_edge_t *next;
1105         ir_node **pops;
1106
1107         arity = be_get_MemPerm_entity_arity(node);
1108         pops = alloca(arity * sizeof(pops[0]));
1109
1110         // create pushs
1111         for(i = 0; i < arity; ++i) {
1112                 ir_entity *inent = be_get_MemPerm_in_entity(node, i);
1113                 ir_entity *outent = be_get_MemPerm_out_entity(node, i);
1114                 ir_type *enttype = get_entity_type(inent);
1115                 int entbits = get_type_size_bits(enttype);
1116                 int entbits2 = get_type_size_bits(get_entity_type(outent));
1117                 ir_node *mem = get_irn_n(node, i + 1);
1118                 ir_node *push;
1119
1120                 /* work around cases where entities have different sizes */
1121                 if(entbits2 < entbits)
1122                         entbits = entbits2;
1123                 assert( (entbits == 32 || entbits == 64) && "spillslot on x86 should be 32 or 64 bit");
1124
1125                 push = create_push(cg, node, node, sp, mem, inent);
1126                 sp = create_spproj(cg, node, push, pn_ia32_Push_stack, node);
1127                 if(entbits == 64) {
1128                         // add another push after the first one
1129                         push = create_push(cg, node, node, sp, mem, inent);
1130                         add_ia32_am_offs_int(push, 4);
1131                         sp = create_spproj(cg, node, push, pn_ia32_Push_stack, node);
1132                 }
1133
1134                 set_irn_n(node, i, new_Bad());
1135         }
1136
1137         // create pops
1138         for(i = arity - 1; i >= 0; --i) {
1139                 ir_entity *inent = be_get_MemPerm_in_entity(node, i);
1140                 ir_entity *outent = be_get_MemPerm_out_entity(node, i);
1141                 ir_type *enttype = get_entity_type(outent);
1142                 int entbits = get_type_size_bits(enttype);
1143                 int entbits2 = get_type_size_bits(get_entity_type(inent));
1144                 ir_node *pop;
1145
1146                 /* work around cases where entities have different sizes */
1147                 if(entbits2 < entbits)
1148                         entbits = entbits2;
1149                 assert( (entbits == 32 || entbits == 64) && "spillslot on x86 should be 32 or 64 bit");
1150
1151                 pop = create_pop(cg, node, node, sp, outent);
1152                 sp = create_spproj(cg, node, pop, pn_ia32_Pop_stack, node);
1153                 if(entbits == 64) {
1154                         add_ia32_am_offs_int(pop, 4);
1155
1156                         // add another pop after the first one
1157                         pop = create_pop(cg, node, node, sp, outent);
1158                         sp = create_spproj(cg, node, pop, pn_ia32_Pop_stack, node);
1159                 }
1160
1161                 pops[i] = pop;
1162         }
1163
1164         in[0] = sp;
1165         keep = be_new_Keep(&ia32_reg_classes[CLASS_ia32_gp], irg, block, 1, in);
1166         sched_add_before(node, keep);
1167
1168         // exchange memprojs
1169         foreach_out_edge_safe(node, edge, next) {
1170                 ir_node *proj = get_edge_src_irn(edge);
1171                 int p = get_Proj_proj(proj);
1172
1173                 assert(p < arity);
1174
1175                 set_Proj_pred(proj, pops[p]);
1176                 set_Proj_proj(proj, pn_ia32_Pop_M);
1177         }
1178
1179         // remove memperm
1180         arity = get_irn_arity(node);
1181         for(i = 0; i < arity; ++i) {
1182                 set_irn_n(node, i, new_Bad());
1183         }
1184         sched_remove(node);
1185 }
1186
1187 /**
1188  * Block-Walker: Calls the transform functions Spill and Reload.
1189  */
1190 static void ia32_after_ra_walker(ir_node *block, void *env) {
1191         ir_node *node, *prev;
1192         ia32_code_gen_t *cg = env;
1193
1194         /* beware: the schedule is changed here */
1195         for (node = sched_last(block); !sched_is_begin(node); node = prev) {
1196                 prev = sched_prev(node);
1197
1198                 if (be_is_Reload(node)) {
1199                         transform_to_Load(cg, node);
1200                 } else if (be_is_Spill(node)) {
1201                         transform_to_Store(cg, node);
1202                 } else if(be_is_MemPerm(node)) {
1203                         transform_MemPerm(cg, node);
1204                 }
1205         }
1206 }
1207
1208 /**
1209  * Collects nodes that need frame entities assigned.
1210  */
1211 static void ia32_collect_frame_entity_nodes(ir_node *node, void *data)
1212 {
1213         be_fec_env_t *env = data;
1214
1215         if (be_is_Reload(node) && be_get_frame_entity(node) == NULL) {
1216                 const ir_mode *mode = get_spill_mode_mode(get_irn_mode(node));
1217                 int align = get_mode_size_bytes(mode);
1218                 be_node_needs_frame_entity(env, node, mode, align);
1219         } else if(is_ia32_irn(node) && get_ia32_frame_ent(node) == NULL
1220                   && is_ia32_use_frame(node)) {
1221                 if (is_ia32_need_stackent(node) || is_ia32_Load(node)) {
1222                         const ir_mode *mode = get_ia32_ls_mode(node);
1223                         int align = get_mode_size_bytes(mode);
1224                         be_node_needs_frame_entity(env, node, mode, align);
1225                 } else if (is_ia32_vfild(node) || is_ia32_xLoad(node)) {
1226                         const ir_mode *mode = get_ia32_ls_mode(node);
1227                         int align = 4;
1228                         be_node_needs_frame_entity(env, node, mode, align);
1229                 } else if(is_ia32_FldCW(node)) {
1230                         const ir_mode *mode = ia32_reg_classes[CLASS_ia32_fp_cw].mode;
1231                         int align = 4;
1232                         be_node_needs_frame_entity(env, node, mode, align);
1233                 } else if (is_ia32_SetST0(node)) {
1234                         const ir_mode *mode = get_ia32_ls_mode(node);
1235                         int align = 4;
1236                         be_node_needs_frame_entity(env, node, mode, align);
1237                 } else {
1238 #ifndef NDEBUG
1239                         if(!is_ia32_St(node) && !is_ia32_xStoreSimple(node)
1240                                         && !is_ia32_vfist(node)
1241                                         && !is_ia32_GetST0(node)
1242                                         && !is_ia32_FnstCW(node)) {
1243                                 assert(0);
1244                         }
1245 #endif
1246                 }
1247         }
1248 }
1249
1250 /**
1251  * We transform Spill and Reload here. This needs to be done before
1252  * stack biasing otherwise we would miss the corrected offset for these nodes.
1253  */
1254 static void ia32_after_ra(void *self) {
1255         ia32_code_gen_t *cg = self;
1256         ir_graph *irg = cg->irg;
1257         be_fec_env_t *fec_env = be_new_frame_entity_coalescer(cg->birg);
1258
1259         /* create and coalesce frame entities */
1260         irg_walk_graph(irg, NULL, ia32_collect_frame_entity_nodes, fec_env);
1261         be_assign_entities(fec_env);
1262         be_free_frame_entity_coalescer(fec_env);
1263
1264         irg_block_walk_graph(irg, NULL, ia32_after_ra_walker, cg);
1265
1266         ia32_finish_irg(irg, cg);
1267 }
1268
1269 /**
1270  * Last touchups for the graph before emit: x87 simulation to replace the
1271  * virtual with real x87 instructions, creating a block schedule and peephole
1272  * optimisations.
1273  */
1274 static void ia32_finish(void *self) {
1275         ia32_code_gen_t *cg = self;
1276         ir_graph        *irg = cg->irg;
1277
1278         /* if we do x87 code generation, rewrite all the virtual instructions and registers */
1279         if (cg->used_fp == fp_x87 || cg->force_sim) {
1280                 x87_simulate_graph(cg->arch_env, cg->birg);
1281         }
1282
1283         /* create block schedule, this also removes empty blocks which might
1284          * produce critical edges */
1285         cg->blk_sched = be_create_block_schedule(irg, cg->birg->exec_freq);
1286
1287         /* do peephole optimisations */
1288         ia32_peephole_optimization(irg, cg);
1289 }
1290
1291 /**
1292  * Emits the code, closes the output file and frees
1293  * the code generator interface.
1294  */
1295 static void ia32_codegen(void *self) {
1296         ia32_code_gen_t *cg = self;
1297         ir_graph        *irg = cg->irg;
1298
1299         ia32_gen_routine(cg, irg);
1300
1301         cur_reg_set = NULL;
1302
1303         /* remove it from the isa */
1304         cg->isa->cg = NULL;
1305
1306         /* de-allocate code generator */
1307         del_set(cg->reg_set);
1308         free(cg);
1309 }
1310
1311 static void *ia32_cg_init(be_irg_t *birg);
1312
1313 static const arch_code_generator_if_t ia32_code_gen_if = {
1314         ia32_cg_init,
1315         NULL,                /* before abi introduce hook */
1316         ia32_prepare_graph,
1317         NULL,                /* spill */
1318         ia32_before_sched,   /* before scheduling hook */
1319         ia32_before_ra,      /* before register allocation hook */
1320         ia32_after_ra,       /* after register allocation hook */
1321         ia32_finish,         /* called before codegen */
1322         ia32_codegen         /* emit && done */
1323 };
1324
1325 /**
1326  * Initializes a IA32 code generator.
1327  */
1328 static void *ia32_cg_init(be_irg_t *birg) {
1329         ia32_isa_t      *isa = (ia32_isa_t *)birg->main_env->arch_env->isa;
1330         ia32_code_gen_t *cg  = xcalloc(1, sizeof(*cg));
1331
1332         cg->impl      = &ia32_code_gen_if;
1333         cg->irg       = birg->irg;
1334         cg->reg_set   = new_set(ia32_cmp_irn_reg_assoc, 1024);
1335         cg->arch_env  = birg->main_env->arch_env;
1336         cg->isa       = isa;
1337         cg->birg      = birg;
1338         cg->blk_sched = NULL;
1339         cg->fp_kind   = isa->fp_kind;
1340         cg->used_fp   = fp_none;
1341         cg->dump      = (birg->main_env->options->dump_flags & DUMP_BE) ? 1 : 0;
1342
1343         /* copy optimizations from isa for easier access */
1344         cg->opt      = isa->opt;
1345         cg->arch     = isa->arch;
1346         cg->opt_arch = isa->opt_arch;
1347
1348         /* enter it */
1349         isa->cg = cg;
1350
1351 #ifndef NDEBUG
1352         if (isa->name_obst) {
1353                 obstack_free(isa->name_obst, NULL);
1354                 obstack_init(isa->name_obst);
1355         }
1356 #endif /* NDEBUG */
1357
1358         cur_reg_set = cg->reg_set;
1359
1360         ia32_irn_ops.cg = cg;
1361
1362         return (arch_code_generator_t *)cg;
1363 }
1364
1365
1366
1367 /*****************************************************************
1368  *  ____             _                  _   _____  _____
1369  * |  _ \           | |                | | |_   _|/ ____|  /\
1370  * | |_) | __ _  ___| | _____ _ __   __| |   | | | (___   /  \
1371  * |  _ < / _` |/ __| |/ / _ \ '_ \ / _` |   | |  \___ \ / /\ \
1372  * | |_) | (_| | (__|   <  __/ | | | (_| |  _| |_ ____) / ____ \
1373  * |____/ \__,_|\___|_|\_\___|_| |_|\__,_| |_____|_____/_/    \_\
1374  *
1375  *****************************************************************/
1376
1377 /**
1378  * Set output modes for GCC
1379  */
1380 static const tarval_mode_info mo_integer = {
1381         TVO_DECIMAL,
1382         NULL,
1383         NULL,
1384 };
1385
1386 /*
1387  * set the tarval output mode of all integer modes to decimal
1388  */
1389 static void set_tarval_output_modes(void)
1390 {
1391         int i;
1392
1393         for (i = get_irp_n_modes() - 1; i >= 0; --i) {
1394                 ir_mode *mode = get_irp_mode(i);
1395
1396                 if (mode_is_int(mode))
1397                         set_tarval_mode_output_option(mode, &mo_integer);
1398         }
1399 }
1400
1401 const arch_isa_if_t ia32_isa_if;
1402
1403 /**
1404  * The template that generates a new ISA object.
1405  * Note that this template can be changed by command line
1406  * arguments.
1407  */
1408 static ia32_isa_t ia32_isa_template = {
1409         {
1410                 &ia32_isa_if,            /* isa interface implementation */
1411                 &ia32_gp_regs[REG_ESP],  /* stack pointer register */
1412                 &ia32_gp_regs[REG_EBP],  /* base pointer register */
1413                 -1,                      /* stack direction */
1414                 NULL,                    /* main environment */
1415                 7,                       /* costs for a spill instruction */
1416                 5,                       /* costs for a reload instruction */
1417         },
1418         { NULL, },                      /* emitter environment */
1419         NULL,                    /* 16bit register names */
1420         NULL,                    /* 8bit register names */
1421         NULL,                    /* 8bit register names high */
1422         NULL,                    /* types */
1423         NULL,                    /* tv_ents */
1424         (0                 |
1425         IA32_OPT_INCDEC    |     /* optimize add 1, sub 1 into inc/dec               default: on */
1426         IA32_OPT_DOAM      |     /* optimize address mode                            default: on */
1427         IA32_OPT_LEA       |     /* optimize for LEAs                                default: on */
1428         IA32_OPT_PLACECNST |     /* place constants immediately before instructions, default: on */
1429         IA32_OPT_IMMOPS    |     /* operations can use immediates,                   default: on */
1430         IA32_OPT_PUSHARGS),      /* create pushs for function argument passing,      default: on */
1431         arch_pentium_4,          /* instruction architecture */
1432         arch_pentium_4,          /* optimize for architecture */
1433         fp_x87,                  /* floating point mode */
1434         NULL,                    /* current code generator */
1435 #ifndef NDEBUG
1436         NULL,                    /* name obstack */
1437         0                        /* name obst size */
1438 #endif
1439 };
1440
1441 /**
1442  * Initializes the backend ISA.
1443  */
1444 static void *ia32_init(FILE *file_handle) {
1445         static int inited = 0;
1446         ia32_isa_t *isa;
1447
1448         if (inited)
1449                 return NULL;
1450         inited = 1;
1451
1452         set_tarval_output_modes();
1453
1454         isa = xmalloc(sizeof(*isa));
1455         memcpy(isa, &ia32_isa_template, sizeof(*isa));
1456
1457         if(mode_fpcw == NULL) {
1458                 mode_fpcw = new_ir_mode("Fpcw", irms_int_number, 16, 0, irma_none, 0);
1459         }
1460
1461         ia32_register_init(isa);
1462         ia32_create_opcodes();
1463         ia32_register_copy_attr_func();
1464
1465         if ((ARCH_INTEL(isa->arch) && isa->arch < arch_pentium_4) ||
1466             (ARCH_AMD(isa->arch) && isa->arch < arch_athlon))
1467                 /* no SSE2 for these cpu's */
1468                 isa->fp_kind = fp_x87;
1469
1470         if (ARCH_INTEL(isa->opt_arch) && isa->opt_arch >= arch_pentium_4) {
1471                 /* Pentium 4 don't like inc and dec instructions */
1472                 isa->opt &= ~IA32_OPT_INCDEC;
1473         }
1474
1475         be_emit_init_env(&isa->emit, file_handle);
1476         isa->regs_16bit     = pmap_create();
1477         isa->regs_8bit      = pmap_create();
1478         isa->regs_8bit_high = pmap_create();
1479         isa->types          = pmap_create();
1480         isa->tv_ent         = pmap_create();
1481         isa->cpu            = ia32_init_machine_description();
1482
1483         ia32_build_16bit_reg_map(isa->regs_16bit);
1484         ia32_build_8bit_reg_map(isa->regs_8bit);
1485         ia32_build_8bit_reg_map_high(isa->regs_8bit_high);
1486
1487 #ifndef NDEBUG
1488         isa->name_obst = xmalloc(sizeof(*isa->name_obst));
1489         obstack_init(isa->name_obst);
1490 #endif /* NDEBUG */
1491
1492         ia32_handle_intrinsics();
1493
1494         /* needed for the debug support */
1495         be_gas_emit_switch_section(&isa->emit, GAS_SECTION_TEXT);
1496         be_emit_cstring(&isa->emit, ".Ltext0:\n");
1497         be_emit_write_line(&isa->emit);
1498
1499         /* we mark referenced global entities, so we can only emit those which
1500          * are actually referenced. (Note: you mustn't use the type visited flag
1501          * elsewhere in the backend)
1502          */
1503         inc_master_type_visited();
1504
1505         return isa;
1506 }
1507
1508
1509
1510 /**
1511  * Closes the output file and frees the ISA structure.
1512  */
1513 static void ia32_done(void *self) {
1514         ia32_isa_t *isa = self;
1515
1516         /* emit now all global declarations */
1517         be_gas_emit_decls(&isa->emit, isa->arch_isa.main_env, 1);
1518
1519         pmap_destroy(isa->regs_16bit);
1520         pmap_destroy(isa->regs_8bit);
1521         pmap_destroy(isa->regs_8bit_high);
1522         pmap_destroy(isa->tv_ent);
1523         pmap_destroy(isa->types);
1524
1525 #ifndef NDEBUG
1526         obstack_free(isa->name_obst, NULL);
1527 #endif /* NDEBUG */
1528
1529         be_emit_destroy_env(&isa->emit);
1530
1531         free(self);
1532 }
1533
1534
1535 /**
1536  * Return the number of register classes for this architecture.
1537  * We report always these:
1538  *  - the general purpose registers
1539  *  - the SSE floating point register set
1540  *  - the virtual floating point registers
1541  *  - the SSE vector register set
1542  */
1543 static int ia32_get_n_reg_class(const void *self) {
1544         return N_CLASSES;
1545 }
1546
1547 /**
1548  * Return the register class for index i.
1549  */
1550 static const arch_register_class_t *ia32_get_reg_class(const void *self, int i)
1551 {
1552         assert(i >= 0 && i < N_CLASSES);
1553         return &ia32_reg_classes[i];
1554 }
1555
1556 /**
1557  * Get the register class which shall be used to store a value of a given mode.
1558  * @param self The this pointer.
1559  * @param mode The mode in question.
1560  * @return A register class which can hold values of the given mode.
1561  */
1562 const arch_register_class_t *ia32_get_reg_class_for_mode(const void *self, const ir_mode *mode) {
1563         const ia32_isa_t *isa = self;
1564         if (mode_is_float(mode)) {
1565                 return USE_SSE2(isa) ? &ia32_reg_classes[CLASS_ia32_xmm] : &ia32_reg_classes[CLASS_ia32_vfp];
1566         }
1567         else
1568                 return &ia32_reg_classes[CLASS_ia32_gp];
1569 }
1570
1571 /**
1572  * Get the ABI restrictions for procedure calls.
1573  * @param self        The this pointer.
1574  * @param method_type The type of the method (procedure) in question.
1575  * @param abi         The abi object to be modified
1576  */
1577 static void ia32_get_call_abi(const void *self, ir_type *method_type, be_abi_call_t *abi) {
1578         const ia32_isa_t *isa = self;
1579         ir_type  *tp;
1580         ir_mode  *mode;
1581         unsigned  cc        = get_method_calling_convention(method_type);
1582         int       n         = get_method_n_params(method_type);
1583         int       i, regnum;
1584         be_abi_call_flags_t call_flags = be_abi_call_get_flags(abi);
1585
1586         unsigned use_push = !IS_P6_ARCH(isa->opt_arch);
1587
1588         /* set abi flags for calls */
1589         call_flags.bits.left_to_right         = 0;  /* always last arg first on stack */
1590         call_flags.bits.store_args_sequential = use_push;
1591         /* call_flags.bits.try_omit_fp                 not changed: can handle both settings */
1592         call_flags.bits.fp_free               = 0;  /* the frame pointer is fixed in IA32 */
1593         call_flags.bits.call_has_imm          = 1;  /* IA32 calls can have immediate address */
1594
1595         /* set parameter passing style */
1596         be_abi_call_set_flags(abi, call_flags, &ia32_abi_callbacks);
1597
1598         for (i = regnum = 0; i < n; i++) {
1599                 const ir_mode         *mode;
1600                 const arch_register_t *reg = NULL;
1601
1602                 tp   = get_method_param_type(method_type, i);
1603                 mode = get_type_mode(tp);
1604                 if (mode != NULL) {
1605                         reg  = ia32_get_RegParam_reg(isa->cg, cc, regnum, mode);
1606                 }
1607                 if (reg != NULL) {
1608                         be_abi_call_param_reg(abi, i, reg);
1609                         ++regnum;
1610                 } else {
1611                         be_abi_call_param_stack(abi, i, 4, 0, 0);
1612                 }
1613         }
1614
1615         /* set return registers */
1616         n = get_method_n_ress(method_type);
1617
1618         assert(n <= 2 && "more than two results not supported");
1619
1620         /* In case of 64bit returns, we will have two 32bit values */
1621         if (n == 2) {
1622                 tp   = get_method_res_type(method_type, 0);
1623                 mode = get_type_mode(tp);
1624
1625                 assert(!mode_is_float(mode) && "two FP results not supported");
1626
1627                 tp   = get_method_res_type(method_type, 1);
1628                 mode = get_type_mode(tp);
1629
1630                 assert(!mode_is_float(mode) && "mixed INT, FP results not supported");
1631
1632                 be_abi_call_res_reg(abi, 0, &ia32_gp_regs[REG_EAX]);
1633                 be_abi_call_res_reg(abi, 1, &ia32_gp_regs[REG_EDX]);
1634         }
1635         else if (n == 1) {
1636                 const arch_register_t *reg;
1637
1638                 tp   = get_method_res_type(method_type, 0);
1639                 assert(is_atomic_type(tp));
1640                 mode = get_type_mode(tp);
1641
1642                 reg = mode_is_float(mode) ? &ia32_vfp_regs[REG_VF0] : &ia32_gp_regs[REG_EAX];
1643
1644                 be_abi_call_res_reg(abi, 0, reg);
1645         }
1646 }
1647
1648
1649 static const void *ia32_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn) {
1650         return &ia32_irn_ops;
1651 }
1652
1653 const arch_irn_handler_t ia32_irn_handler = {
1654         ia32_get_irn_ops
1655 };
1656
1657 const arch_irn_handler_t *ia32_get_irn_handler(const void *self) {
1658         return &ia32_irn_handler;
1659 }
1660
1661 int ia32_to_appear_in_schedule(void *block_env, const ir_node *irn) {
1662         if(!is_ia32_irn(irn)) {
1663                 return -1;
1664         }
1665
1666         if(is_ia32_NoReg_GP(irn) || is_ia32_NoReg_VFP(irn) || is_ia32_NoReg_XMM(irn)
1667                 || is_ia32_Unknown_GP(irn) || is_ia32_Unknown_XMM(irn)
1668                 || is_ia32_Unknown_VFP(irn) || is_ia32_ChangeCW(irn)
1669                 || is_ia32_Immediate(irn))
1670                 return 0;
1671
1672         return 1;
1673 }
1674
1675 /**
1676  * Initializes the code generator interface.
1677  */
1678 static const arch_code_generator_if_t *ia32_get_code_generator_if(void *self) {
1679         return &ia32_code_gen_if;
1680 }
1681
1682 /**
1683  * Returns the estimated execution time of an ia32 irn.
1684  */
1685 static sched_timestep_t ia32_sched_exectime(void *env, const ir_node *irn) {
1686         const arch_env_t *arch_env = env;
1687         return is_ia32_irn(irn) ? ia32_get_op_estimated_cost(arch_get_irn_ops(arch_env, irn), irn) : 1;
1688 }
1689
1690 list_sched_selector_t ia32_sched_selector;
1691
1692 /**
1693  * Returns the reg_pressure scheduler with to_appear_in_schedule() overloaded
1694  */
1695 static const list_sched_selector_t *ia32_get_list_sched_selector(const void *self, list_sched_selector_t *selector) {
1696         memcpy(&ia32_sched_selector, selector, sizeof(ia32_sched_selector));
1697         ia32_sched_selector.exectime              = ia32_sched_exectime;
1698         ia32_sched_selector.to_appear_in_schedule = ia32_to_appear_in_schedule;
1699         return &ia32_sched_selector;
1700 }
1701
1702 static const ilp_sched_selector_t *ia32_get_ilp_sched_selector(const void *self) {
1703         return NULL;
1704 }
1705
1706 /**
1707  * Returns the necessary byte alignment for storing a register of given class.
1708  */
1709 static int ia32_get_reg_class_alignment(const void *self, const arch_register_class_t *cls) {
1710         ir_mode *mode = arch_register_class_mode(cls);
1711         int bytes     = get_mode_size_bytes(mode);
1712
1713         if (mode_is_float(mode) && bytes > 8)
1714                 return 16;
1715         return bytes;
1716 }
1717
1718 static const be_execution_unit_t ***ia32_get_allowed_execution_units(const void *self, const ir_node *irn) {
1719         static const be_execution_unit_t *_allowed_units_BRANCH[] = {
1720                 &ia32_execution_units_BRANCH[IA32_EXECUNIT_TP_BRANCH_BRANCH1],
1721                 &ia32_execution_units_BRANCH[IA32_EXECUNIT_TP_BRANCH_BRANCH2],
1722                 NULL,
1723         };
1724         static const be_execution_unit_t *_allowed_units_GP[] = {
1725                 &ia32_execution_units_GP[IA32_EXECUNIT_TP_GP_GP_EAX],
1726                 &ia32_execution_units_GP[IA32_EXECUNIT_TP_GP_GP_EBX],
1727                 &ia32_execution_units_GP[IA32_EXECUNIT_TP_GP_GP_ECX],
1728                 &ia32_execution_units_GP[IA32_EXECUNIT_TP_GP_GP_EDX],
1729                 &ia32_execution_units_GP[IA32_EXECUNIT_TP_GP_GP_ESI],
1730                 &ia32_execution_units_GP[IA32_EXECUNIT_TP_GP_GP_EDI],
1731                 &ia32_execution_units_GP[IA32_EXECUNIT_TP_GP_GP_EBP],
1732                 NULL,
1733         };
1734         static const be_execution_unit_t *_allowed_units_DUMMY[] = {
1735                 &be_machine_execution_units_DUMMY[0],
1736                 NULL,
1737         };
1738         static const be_execution_unit_t **_units_callret[] = {
1739                 _allowed_units_BRANCH,
1740                 NULL
1741         };
1742         static const be_execution_unit_t **_units_other[] = {
1743                 _allowed_units_GP,
1744                 NULL
1745         };
1746         static const be_execution_unit_t **_units_dummy[] = {
1747                 _allowed_units_DUMMY,
1748                 NULL
1749         };
1750         const be_execution_unit_t ***ret;
1751
1752         if (is_ia32_irn(irn)) {
1753                 ret = get_ia32_exec_units(irn);
1754         }
1755         else if (is_be_node(irn)) {
1756                 if (be_is_Call(irn) || be_is_Return(irn)) {
1757                         ret = _units_callret;
1758                 }
1759                 else if (be_is_Barrier(irn)) {
1760                         ret = _units_dummy;
1761                 }
1762                 else {
1763                          ret = _units_other;
1764                 }
1765         }
1766         else {
1767                 ret = _units_dummy;
1768         }
1769
1770         return ret;
1771 }
1772
1773 /**
1774  * Return the abstract ia32 machine.
1775  */
1776 static const be_machine_t *ia32_get_machine(const void *self) {
1777         const ia32_isa_t *isa = self;
1778         return isa->cpu;
1779 }
1780
1781 /**
1782  * Return irp irgs in the desired order.
1783  */
1784 static ir_graph **ia32_get_irg_list(const void *self, ir_graph ***irg_list) {
1785         return NULL;
1786 }
1787
1788 /**
1789  * Allows or disallows the creation of Psi nodes for the given Phi nodes.
1790  * @return 1 if allowed, 0 otherwise
1791  */
1792 static int ia32_is_psi_allowed(ir_node *sel, ir_node *phi_list, int i, int j)
1793 {
1794         ir_node *cmp, *cmp_a, *phi;
1795         ir_mode *mode;
1796
1797 /* we don't want long long an floating point Psi */
1798 #define IS_BAD_PSI_MODE(mode) (mode_is_float(mode) || get_mode_size_bits(mode) > 32)
1799
1800         if (get_irn_mode(sel) != mode_b)
1801                 return 0;
1802
1803         cmp   = get_Proj_pred(sel);
1804         cmp_a = get_Cmp_left(cmp);
1805         mode  = get_irn_mode(cmp_a);
1806
1807         if (IS_BAD_PSI_MODE(mode))
1808                 return 0;
1809
1810         /* check the Phi nodes */
1811         for (phi = phi_list; phi; phi = get_irn_link(phi)) {
1812                 ir_node *pred_i = get_irn_n(phi, i);
1813                 ir_node *pred_j = get_irn_n(phi, j);
1814                 ir_mode *mode_i = get_irn_mode(pred_i);
1815                 ir_mode *mode_j = get_irn_mode(pred_j);
1816
1817                 if (IS_BAD_PSI_MODE(mode_i) || IS_BAD_PSI_MODE(mode_j))
1818                         return 0;
1819         }
1820
1821 #undef IS_BAD_PSI_MODE
1822
1823         return 1;
1824 }
1825
1826 static ia32_intrinsic_env_t intrinsic_env = {
1827         NULL,    /**< the irg, these entities belong to */
1828         NULL,    /**< entity for first div operand (move into FPU) */
1829         NULL,    /**< entity for second div operand (move into FPU) */
1830         NULL,    /**< entity for converts ll -> d */
1831         NULL,    /**< entity for converts d -> ll */
1832 };
1833
1834 /**
1835  * Returns the libFirm configuration parameter for this backend.
1836  */
1837 static const backend_params *ia32_get_libfirm_params(void) {
1838         static const opt_if_conv_info_t ifconv = {
1839                 4,                    /* maxdepth, doesn't matter for Psi-conversion */
1840                 ia32_is_psi_allowed   /* allows or disallows Psi creation for given selector */
1841         };
1842         static const arch_dep_params_t ad = {
1843                 1,  /* also use subs */
1844                 4,  /* maximum shifts */
1845                 31, /* maximum shift amount */
1846
1847                 1,  /* allow Mulhs */
1848                 1,  /* allow Mulus */
1849                 32  /* Mulh allowed up to 32 bit */
1850         };
1851         static backend_params p = {
1852                 1,     /* need dword lowering */
1853                 1,     /* support inline assembly */
1854                 1,     /* prefer fastcall calling convention */
1855                 NULL,  /* no additional opcodes */
1856                 NULL,  /* will be set later */
1857                 ia32_create_intrinsic_fkt,
1858                 &intrinsic_env,  /* context for ia32_create_intrinsic_fkt */
1859                 NULL,  /* will be set later */
1860         };
1861
1862         p.dep_param    = &ad;
1863         p.if_conv_info = &ifconv;
1864         return &p;
1865 }
1866
1867 /* instruction set architectures. */
1868 static const lc_opt_enum_int_items_t arch_items[] = {
1869         { "386",        arch_i386, },
1870         { "486",        arch_i486, },
1871         { "pentium",    arch_pentium, },
1872         { "586",        arch_pentium, },
1873         { "pentiumpro", arch_pentium_pro, },
1874         { "686",        arch_pentium_pro, },
1875         { "pentiummmx", arch_pentium_mmx, },
1876         { "pentium2",   arch_pentium_2, },
1877         { "p2",         arch_pentium_2, },
1878         { "pentium3",   arch_pentium_3, },
1879         { "p3",         arch_pentium_3, },
1880         { "pentium4",   arch_pentium_4, },
1881         { "p4",         arch_pentium_4, },
1882         { "pentiumm",   arch_pentium_m, },
1883         { "pm",         arch_pentium_m, },
1884         { "core",       arch_core, },
1885         { "k6",         arch_k6, },
1886         { "athlon",     arch_athlon, },
1887         { "athlon64",   arch_athlon_64, },
1888         { "opteron",    arch_opteron, },
1889         { NULL,         0 }
1890 };
1891
1892 static lc_opt_enum_int_var_t arch_var = {
1893         &ia32_isa_template.arch, arch_items
1894 };
1895
1896 static lc_opt_enum_int_var_t opt_arch_var = {
1897         &ia32_isa_template.opt_arch, arch_items
1898 };
1899
1900 static const lc_opt_enum_int_items_t fp_unit_items[] = {
1901         { "x87" ,    fp_x87 },
1902         { "sse2",    fp_sse2 },
1903         { NULL,      0 }
1904 };
1905
1906 static lc_opt_enum_int_var_t fp_unit_var = {
1907         &ia32_isa_template.fp_kind, fp_unit_items
1908 };
1909
1910 static const lc_opt_enum_int_items_t gas_items[] = {
1911         { "normal",  GAS_FLAVOUR_NORMAL },
1912         { "mingw",   GAS_FLAVOUR_MINGW  },
1913         { NULL,      0 }
1914 };
1915
1916 static lc_opt_enum_int_var_t gas_var = {
1917         (int*) &be_gas_flavour, gas_items
1918 };
1919
1920 static const lc_opt_table_entry_t ia32_options[] = {
1921         LC_OPT_ENT_ENUM_INT("arch",      "select the instruction architecture", &arch_var),
1922         LC_OPT_ENT_ENUM_INT("opt",       "optimize for instruction architecture", &opt_arch_var),
1923         LC_OPT_ENT_ENUM_INT("fpunit",    "select the floating point unit", &fp_unit_var),
1924         LC_OPT_ENT_NEGBIT("noaddrmode",  "do not use address mode", &ia32_isa_template.opt, IA32_OPT_DOAM),
1925         LC_OPT_ENT_NEGBIT("nolea",       "do not optimize for LEAs", &ia32_isa_template.opt, IA32_OPT_LEA),
1926         LC_OPT_ENT_NEGBIT("noplacecnst", "do not place constants", &ia32_isa_template.opt, IA32_OPT_PLACECNST),
1927         LC_OPT_ENT_NEGBIT("noimmop",     "no operations with immediates", &ia32_isa_template.opt, IA32_OPT_IMMOPS),
1928         LC_OPT_ENT_NEGBIT("nopushargs",  "do not create pushs for function arguments", &ia32_isa_template.opt, IA32_OPT_PUSHARGS),
1929         LC_OPT_ENT_ENUM_INT("gasmode",   "set the GAS compatibility mode", &gas_var),
1930         { NULL }
1931 };
1932
1933 const arch_isa_if_t ia32_isa_if = {
1934         ia32_init,
1935         ia32_done,
1936         ia32_get_n_reg_class,
1937         ia32_get_reg_class,
1938         ia32_get_reg_class_for_mode,
1939         ia32_get_call_abi,
1940         ia32_get_irn_handler,
1941         ia32_get_code_generator_if,
1942         ia32_get_list_sched_selector,
1943         ia32_get_ilp_sched_selector,
1944         ia32_get_reg_class_alignment,
1945         ia32_get_libfirm_params,
1946         ia32_get_allowed_execution_units,
1947         ia32_get_machine,
1948         ia32_get_irg_list,
1949 };
1950
1951 void ia32_init_emitter(void);
1952 void ia32_init_finish(void);
1953 void ia32_init_optimize(void);
1954 void ia32_init_transform(void);
1955 void ia32_init_x87(void);
1956
1957 void be_init_arch_ia32(void)
1958 {
1959         lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
1960         lc_opt_entry_t *ia32_grp = lc_opt_get_grp(be_grp, "ia32");
1961
1962         lc_opt_add_table(ia32_grp, ia32_options);
1963         be_register_isa_if("ia32", &ia32_isa_if);
1964
1965         FIRM_DBG_REGISTER(dbg, "firm.be.ia32.cg");
1966
1967         ia32_init_emitter();
1968         ia32_init_finish();
1969         ia32_init_optimize();
1970         ia32_init_transform();
1971         ia32_init_x87();
1972 }
1973
1974 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_ia32);