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