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