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