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