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