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