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