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