besched: Add and use sched_foreach_reverse_safe().
[libfirm] / ir / be / ia32 / bearch_ia32.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief       This is the main ia32 firm backend driver.
9  * @author      Christian Wuerdig
10  */
11 #include "config.h"
12
13 #include "lc_opts.h"
14 #include "lc_opts_enum.h"
15
16 #include <math.h>
17
18 #include "irarch.h"
19 #include "irgwalk.h"
20 #include "irprog.h"
21 #include "irprintf.h"
22 #include "iredges_t.h"
23 #include "ircons.h"
24 #include "irflag.h"
25 #include "irgmod.h"
26 #include "irgopt.h"
27 #include "irgopt.h"
28 #include "irdump.h"
29 #include "pdeq.h"
30 #include "pset.h"
31 #include "debug.h"
32 #include "error.h"
33 #include "xmalloc.h"
34 #include "irtools.h"
35 #include "iroptimize.h"
36 #include "instrument.h"
37 #include "iropt_t.h"
38 #include "lower_dw.h"
39 #include "lower_calls.h"
40 #include "lower_mode_b.h"
41 #include "lower_softfloat.h"
42 #include "firmstat_t.h"
43
44 #include "beabi.h"
45 #include "benode.h"
46 #include "belower.h"
47 #include "besched.h"
48 #include "be.h"
49 #include "be_t.h"
50 #include "beirgmod.h"
51 #include "beblocksched.h"
52 #include "bespillutil.h"
53 #include "bespillslots.h"
54 #include "bemodule.h"
55 #include "begnuas.h"
56 #include "bestate.h"
57 #include "beflags.h"
58 #include "betranshlp.h"
59 #include "belistsched.h"
60 #include "beabihelper.h"
61 #include "bestack.h"
62
63 #include "bearch_ia32_t.h"
64
65 #include "ia32_new_nodes.h"
66 #include "gen_ia32_regalloc_if.h"
67 #include "ia32_common_transform.h"
68 #include "ia32_transform.h"
69 #include "ia32_emitter.h"
70 #include "ia32_optimize.h"
71 #include "ia32_x87.h"
72 #include "ia32_dbg_stat.h"
73 #include "ia32_finish.h"
74 #include "ia32_fpu.h"
75 #include "ia32_architecture.h"
76
77 #ifdef FIRM_GRGEN_BE
78 #include "ia32_pbqp_transform.h"
79
80 transformer_t be_transformer = TRANSFORMER_DEFAULT;
81 #endif
82
83 ir_mode *ia32_mode_fpcw;
84 ir_mode *ia32_mode_E;
85 ir_type *ia32_type_E;
86
87 /** The current omit-fp state */
88 static ir_type *omit_fp_between_type   = NULL;
89 static ir_type *between_type           = NULL;
90 static ir_entity *old_bp_ent           = NULL;
91 static ir_entity *ret_addr_ent         = NULL;
92 static ir_entity *omit_fp_ret_addr_ent = NULL;
93
94 /**
95  * The environment for the intrinsic mapping.
96  */
97 static ia32_intrinsic_env_t intrinsic_env = {
98         NULL,    /* entity for __divdi3 library call */
99         NULL,    /* entity for __moddi3 library call */
100         NULL,    /* entity for __udivdi3 library call */
101         NULL,    /* entity for __umoddi3 library call */
102 };
103
104
105 typedef ir_node *(*create_const_node_func) (dbg_info *dbgi, ir_node *block);
106
107 /**
108  * Used to create per-graph unique pseudo nodes.
109  */
110 static inline ir_node *create_const(ir_graph *irg, ir_node **place,
111                                     create_const_node_func func,
112                                     const arch_register_t* reg)
113 {
114         ir_node *block, *res;
115
116         if (*place != NULL)
117                 return *place;
118
119         block = get_irg_start_block(irg);
120         res = func(NULL, block);
121         arch_set_irn_register(res, reg);
122         *place = res;
123
124         return res;
125 }
126
127 /* Creates the unique per irg GP NoReg node. */
128 ir_node *ia32_new_NoReg_gp(ir_graph *irg)
129 {
130         ia32_irg_data_t *irg_data = ia32_get_irg_data(irg);
131         return create_const(irg, &irg_data->noreg_gp, new_bd_ia32_NoReg_GP,
132                             &ia32_registers[REG_GP_NOREG]);
133 }
134
135 ir_node *ia32_new_NoReg_fp(ir_graph *irg)
136 {
137         ia32_irg_data_t *irg_data = ia32_get_irg_data(irg);
138         return create_const(irg, &irg_data->noreg_fp, new_bd_ia32_NoReg_FP,
139                             &ia32_registers[REG_FP_NOREG]);
140 }
141
142 ir_node *ia32_new_NoReg_xmm(ir_graph *irg)
143 {
144         ia32_irg_data_t *irg_data = ia32_get_irg_data(irg);
145         return create_const(irg, &irg_data->noreg_xmm, new_bd_ia32_NoReg_XMM,
146                             &ia32_registers[REG_XMM_NOREG]);
147 }
148
149 ir_node *ia32_new_Fpu_truncate(ir_graph *irg)
150 {
151         ia32_irg_data_t *irg_data = ia32_get_irg_data(irg);
152         return create_const(irg, &irg_data->fpu_trunc_mode, new_bd_ia32_ChangeCW,
153                         &ia32_registers[REG_FPCW]);
154 }
155
156
157 /**
158  * Returns the admissible noreg register node for input register pos of node irn.
159  */
160 static ir_node *ia32_get_admissible_noreg(ir_node *irn, int pos)
161 {
162         ir_graph                  *irg = get_irn_irg(irn);
163         const arch_register_req_t *req = arch_get_irn_register_req_in(irn, pos);
164
165         assert(req != NULL && "Missing register requirements");
166         if (req->cls == &ia32_reg_classes[CLASS_ia32_gp])
167                 return ia32_new_NoReg_gp(irg);
168
169         if (ia32_cg_config.use_sse2) {
170                 return ia32_new_NoReg_xmm(irg);
171         } else {
172                 return ia32_new_NoReg_fp(irg);
173         }
174 }
175
176 static ir_entity *ia32_get_frame_entity(const ir_node *irn)
177 {
178         return is_ia32_irn(irn) ? get_ia32_frame_ent(irn) : NULL;
179 }
180
181 static void ia32_set_frame_entity(ir_node *node, ir_entity *entity)
182 {
183         if (is_be_node(node))
184                 be_node_set_frame_entity(node, entity);
185         else
186                 set_ia32_frame_ent(node, entity);
187 }
188
189 static void ia32_set_frame_offset(ir_node *irn, int bias)
190 {
191         if (get_ia32_frame_ent(irn) == NULL)
192                 return;
193
194         if (is_ia32_Pop(irn) || is_ia32_PopMem(irn)) {
195                 ir_graph          *irg     = get_irn_irg(irn);
196                 be_stack_layout_t *layout  = be_get_irg_stack_layout(irg);
197                 if (layout->sp_relative) {
198                         /* Pop nodes modify the stack pointer before calculating the
199                          * destination address, so fix this here
200                          */
201                         bias -= 4;
202                 }
203         }
204         add_ia32_am_offs_int(irn, bias);
205 }
206
207 static int ia32_get_sp_bias(const ir_node *node)
208 {
209         if (is_ia32_Call(node))
210                 return -(int)get_ia32_call_attr_const(node)->pop;
211
212         if (is_ia32_Push(node))
213                 return 4;
214
215         if (is_ia32_Pop(node) || is_ia32_PopMem(node))
216                 return -4;
217
218         if (is_ia32_Leave(node) || is_ia32_CopyEbpEsp(node)) {
219                 return SP_BIAS_RESET;
220         }
221
222         return 0;
223 }
224
225 /**
226  * Build the between type and entities if not already build.
227  */
228 static void ia32_build_between_type(void)
229 {
230 #define IDENT(s) new_id_from_chars(s, sizeof(s)-1)
231         if (between_type == NULL) {
232                 ir_type *old_bp_type   = new_type_primitive(mode_Iu);
233                 ir_type *ret_addr_type = new_type_primitive(mode_Iu);
234
235                 between_type           = new_type_struct(IDENT("ia32_between_type"));
236                 old_bp_ent             = new_entity(between_type, IDENT("old_bp"), old_bp_type);
237                 ret_addr_ent           = new_entity(between_type, IDENT("ret_addr"), ret_addr_type);
238
239                 set_entity_offset(old_bp_ent, 0);
240                 set_entity_offset(ret_addr_ent, get_type_size_bytes(old_bp_type));
241                 set_type_size_bytes(between_type, get_type_size_bytes(old_bp_type) + get_type_size_bytes(ret_addr_type));
242                 set_type_state(between_type, layout_fixed);
243
244                 omit_fp_between_type = new_type_struct(IDENT("ia32_between_type_omit_fp"));
245                 omit_fp_ret_addr_ent = new_entity(omit_fp_between_type, IDENT("ret_addr"), ret_addr_type);
246
247                 set_entity_offset(omit_fp_ret_addr_ent, 0);
248                 set_type_size_bytes(omit_fp_between_type, get_type_size_bytes(ret_addr_type));
249                 set_type_state(omit_fp_between_type, layout_fixed);
250         }
251 #undef IDENT
252 }
253
254 /**
255  * Produces the type which sits between the stack args and the locals on the stack.
256  * it will contain the return address and space to store the old base pointer.
257  * @return The Firm type modeling the ABI between type.
258  */
259 static ir_type *ia32_abi_get_between_type(ir_graph *irg)
260 {
261         const be_stack_layout_t *layout = be_get_irg_stack_layout(irg);
262         ia32_build_between_type();
263         return layout->sp_relative ? omit_fp_between_type : between_type;
264 }
265
266 /**
267  * Return the stack entity that contains the return address.
268  */
269 ir_entity *ia32_get_return_address_entity(ir_graph *irg)
270 {
271         const be_stack_layout_t *layout = be_get_irg_stack_layout(irg);
272         ia32_build_between_type();
273         return layout->sp_relative ? omit_fp_ret_addr_ent : ret_addr_ent;
274 }
275
276 /**
277  * Return the stack entity that contains the frame address.
278  */
279 ir_entity *ia32_get_frame_address_entity(ir_graph *irg)
280 {
281         const be_stack_layout_t *layout = be_get_irg_stack_layout(irg);
282         ia32_build_between_type();
283         return layout->sp_relative ? NULL : old_bp_ent;
284 }
285
286 /**
287  * Get the estimated cycle count for @p irn.
288  *
289  * @param self The this pointer.
290  * @param irn  The node.
291  *
292  * @return     The estimated cycle count for this operation
293  */
294 static int ia32_get_op_estimated_cost(const ir_node *irn)
295 {
296         int            cost;
297         ia32_op_type_t op_tp;
298
299         if (is_Proj(irn))
300                 return 0;
301         if (!is_ia32_irn(irn))
302                 return 0;
303
304         assert(is_ia32_irn(irn));
305
306         cost  = get_ia32_latency(irn);
307         op_tp = get_ia32_op_type(irn);
308
309         if (is_ia32_CopyB(irn)) {
310                 cost = 250;
311         }
312         else if (is_ia32_CopyB_i(irn)) {
313                 int size = get_ia32_copyb_size(irn);
314                 cost     = 20 + (int)ceil((4/3) * size);
315         }
316         /* in case of address mode operations add additional cycles */
317         else if (op_tp == ia32_AddrModeD || op_tp == ia32_AddrModeS) {
318                 /*
319                         In case of stack access and access to fixed addresses add 5 cycles
320                         (we assume they are in cache), other memory operations cost 20
321                         cycles.
322                 */
323                 if (is_ia32_use_frame(irn) || (
324                     is_ia32_NoReg_GP(get_irn_n(irn, n_ia32_base)) &&
325                     is_ia32_NoReg_GP(get_irn_n(irn, n_ia32_index))
326                     )) {
327                         cost += 5;
328                 } else {
329                         cost += 20;
330                 }
331         }
332
333         return cost;
334 }
335
336 static ir_mode *get_spill_mode_mode(const ir_mode *mode)
337 {
338         if (mode_is_float(mode))
339                 return mode_D;
340
341         return mode_Iu;
342 }
343
344 /**
345  * Get the mode that should be used for spilling value node
346  */
347 static ir_mode *get_spill_mode(const ir_node *node)
348 {
349         ir_mode *mode = get_irn_mode(node);
350         return get_spill_mode_mode(mode);
351 }
352
353 /**
354  * Checks whether an addressmode reload for a node with mode mode is compatible
355  * with a spillslot of mode spill_mode
356  */
357 static int ia32_is_spillmode_compatible(const ir_mode *mode, const ir_mode *spillmode)
358 {
359         return !mode_is_float(mode) || mode == spillmode;
360 }
361
362 /**
363  * Check if irn can load its operand at position i from memory (source addressmode).
364  * @param irn    The irn to be checked
365  * @param i      The operands position
366  * @return Non-Zero if operand can be loaded
367  */
368 static int ia32_possible_memory_operand(const ir_node *irn, unsigned int i)
369 {
370         ir_node       *op        = get_irn_n(irn, i);
371         const ir_mode *mode      = get_irn_mode(op);
372         const ir_mode *spillmode = get_spill_mode(op);
373
374         if (!is_ia32_irn(irn)                              ||  /* must be an ia32 irn */
375             get_ia32_op_type(irn) != ia32_Normal           ||  /* must not already be a addressmode irn */
376             !ia32_is_spillmode_compatible(mode, spillmode) ||
377             is_ia32_use_frame(irn))                            /* must not already use frame */
378                 return 0;
379
380         switch (get_ia32_am_support(irn)) {
381                 case ia32_am_none:
382                         return 0;
383
384                 case ia32_am_unary:
385                         if (i != n_ia32_unary_op)
386                                 return 0;
387                         break;
388
389                 case ia32_am_binary:
390                         switch (i) {
391                                 case n_ia32_binary_left: {
392                                         if (!is_ia32_commutative(irn))
393                                                 return 0;
394
395                                         /* we can't swap left/right for limited registers
396                                          * (As this (currently) breaks constraint handling copies) */
397                                         arch_register_req_t const *const req = arch_get_irn_register_req_in(irn, n_ia32_binary_left);
398                                         if (arch_register_req_is(req, limited))
399                                                 return 0;
400                                         break;
401                                 }
402
403                                 case n_ia32_binary_right:
404                                         break;
405
406                                 default:
407                                         return 0;
408                         }
409                         break;
410
411                 default:
412                         panic("Unknown AM type");
413         }
414
415         /* HACK: must not already use "real" memory.
416          * This can happen for Call and Div */
417         if (!is_NoMem(get_irn_n(irn, n_ia32_mem)))
418                 return 0;
419
420         return 1;
421 }
422
423 static void ia32_perform_memory_operand(ir_node *irn, ir_node *spill,
424                                         unsigned int i)
425 {
426         ir_mode *load_mode;
427         ir_mode *dest_op_mode;
428
429         assert(ia32_possible_memory_operand(irn, i) && "Cannot perform memory operand change");
430
431         set_ia32_op_type(irn, ia32_AddrModeS);
432
433         load_mode    = get_irn_mode(get_irn_n(irn, i));
434         dest_op_mode = get_ia32_ls_mode(irn);
435         if (get_mode_size_bits(load_mode) <= get_mode_size_bits(dest_op_mode)) {
436                 set_ia32_ls_mode(irn, load_mode);
437         }
438         set_ia32_use_frame(irn);
439         set_ia32_need_stackent(irn);
440
441         if (i == n_ia32_binary_left                    &&
442             get_ia32_am_support(irn) == ia32_am_binary &&
443             /* immediates are only allowed on the right side */
444             !is_ia32_Immediate(get_irn_n(irn, n_ia32_binary_right))) {
445                 ia32_swap_left_right(irn);
446                 i = n_ia32_binary_right;
447         }
448
449         assert(is_NoMem(get_irn_n(irn, n_ia32_mem)));
450
451         set_irn_n(irn, n_ia32_base, get_irg_frame(get_irn_irg(irn)));
452         set_irn_n(irn, n_ia32_mem,  spill);
453         set_irn_n(irn, i,           ia32_get_admissible_noreg(irn, i));
454         set_ia32_is_reload(irn);
455 }
456
457 static const be_abi_callbacks_t ia32_abi_callbacks = {
458         ia32_abi_get_between_type,
459 };
460
461 /* register allocator interface */
462 static const arch_irn_ops_t ia32_irn_ops = {
463         ia32_get_frame_entity,
464         ia32_set_frame_offset,
465         ia32_get_sp_bias,
466         ia32_get_op_estimated_cost,
467         ia32_possible_memory_operand,
468         ia32_perform_memory_operand,
469 };
470
471 static int gprof = 0;
472
473 static void ia32_before_abi(ir_graph *irg)
474 {
475         if (gprof) {
476                 static ir_entity *mcount = NULL;
477                 if (mcount == NULL) {
478                         ir_type *tp = new_type_method(0, 0);
479                         ident   *id = new_id_from_str("mcount");
480                         mcount = new_entity(get_glob_type(), id, tp);
481                         /* FIXME: enter the right ld_ident here */
482                         set_entity_ld_ident(mcount, get_entity_ident(mcount));
483                         set_entity_visibility(mcount, ir_visibility_external);
484                 }
485                 instrument_initcall(irg, mcount);
486         }
487 }
488
489 /**
490  * Transforms the standard firm graph into
491  * an ia32 firm graph
492  */
493 static void ia32_prepare_graph(ir_graph *irg)
494 {
495         ia32_irg_data_t *irg_data = ia32_get_irg_data(irg);
496
497 #ifdef FIRM_GRGEN_BE
498         switch (be_transformer) {
499         case TRANSFORMER_DEFAULT:
500                 /* transform remaining nodes into assembler instructions */
501                 ia32_transform_graph(irg);
502                 break;
503
504         case TRANSFORMER_PBQP:
505         case TRANSFORMER_RAND:
506                 /* transform nodes into assembler instructions by PBQP magic */
507                 ia32_transform_graph_by_pbqp(irg);
508                 break;
509
510         default:
511                 panic("invalid transformer");
512         }
513 #else
514         ia32_transform_graph(irg);
515 #endif
516
517         /* do local optimizations (mainly CSE) */
518         optimize_graph_df(irg);
519         /* backend code expects that outedges are always enabled */
520         assure_edges(irg);
521
522         if (irg_data->dump)
523                 dump_ir_graph(irg, "transformed");
524
525         /* optimize address mode */
526         ia32_optimize_graph(irg);
527
528         /* do code placement, to optimize the position of constants */
529         place_code(irg);
530         /* backend code expects that outedges are always enabled */
531         assure_edges(irg);
532
533         if (irg_data->dump)
534                 dump_ir_graph(irg, "place");
535 }
536
537 ir_node *ia32_turn_back_am(ir_node *node)
538 {
539         dbg_info *dbgi  = get_irn_dbg_info(node);
540         ir_graph *irg   = get_irn_irg(node);
541         ir_node  *block = get_nodes_block(node);
542         ir_node  *base  = get_irn_n(node, n_ia32_base);
543         ir_node  *idx   = get_irn_n(node, n_ia32_index);
544         ir_node  *mem   = get_irn_n(node, n_ia32_mem);
545         ir_node  *noreg;
546
547         ir_node  *load     = new_bd_ia32_Load(dbgi, block, base, idx, mem);
548         ir_node  *load_res = new_rd_Proj(dbgi, load, mode_Iu, pn_ia32_Load_res);
549
550         ia32_copy_am_attrs(load, node);
551         if (is_ia32_is_reload(node))
552                 set_ia32_is_reload(load);
553         set_irn_n(node, n_ia32_mem, get_irg_no_mem(irg));
554
555         switch (get_ia32_am_support(node)) {
556                 case ia32_am_unary:
557                         set_irn_n(node, n_ia32_unary_op, load_res);
558                         break;
559
560                 case ia32_am_binary:
561                         if (is_ia32_Immediate(get_irn_n(node, n_ia32_binary_right))) {
562                                 set_irn_n(node, n_ia32_binary_left, load_res);
563                         } else {
564                                 set_irn_n(node, n_ia32_binary_right, load_res);
565                         }
566                         break;
567
568                 default:
569                         panic("Unknown AM type");
570         }
571         noreg = ia32_new_NoReg_gp(irg);
572         set_irn_n(node, n_ia32_base,  noreg);
573         set_irn_n(node, n_ia32_index, noreg);
574         set_ia32_am_offs_int(node, 0);
575         set_ia32_am_sc(node, NULL);
576         set_ia32_am_scale(node, 0);
577         clear_ia32_am_sc_sign(node);
578
579         /* rewire mem-proj */
580         if (get_irn_mode(node) == mode_T) {
581                 foreach_out_edge(node, edge) {
582                         ir_node *out = get_edge_src_irn(edge);
583                         if (get_irn_mode(out) == mode_M) {
584                                 set_Proj_pred(out, load);
585                                 set_Proj_proj(out, pn_ia32_Load_M);
586                                 break;
587                         }
588                 }
589         }
590
591         set_ia32_op_type(node, ia32_Normal);
592         if (sched_is_scheduled(node))
593                 sched_add_before(node, load);
594
595         return load_res;
596 }
597
598 static ir_node *flags_remat(ir_node *node, ir_node *after)
599 {
600         /* we should turn back source address mode when rematerializing nodes */
601         ia32_op_type_t type;
602         ir_node        *block;
603         ir_node        *copy;
604
605         if (is_Block(after)) {
606                 block = after;
607         } else {
608                 block = get_nodes_block(after);
609         }
610
611         type = get_ia32_op_type(node);
612         switch (type) {
613                 case ia32_AddrModeS:
614                         ia32_turn_back_am(node);
615                         break;
616
617                 case ia32_AddrModeD:
618                         /* TODO implement this later... */
619                         panic("found DestAM with flag user %+F this should not happen", node);
620
621                 default: assert(type == ia32_Normal); break;
622         }
623
624         copy = exact_copy(node);
625         set_nodes_block(copy, block);
626         sched_add_after(after, copy);
627
628         return copy;
629 }
630
631 /**
632  * Called before the register allocator.
633  */
634 static void ia32_before_ra(ir_graph *irg)
635 {
636         /* setup fpu rounding modes */
637         ia32_setup_fpu_mode(irg);
638
639         /* fixup flags */
640         be_sched_fix_flags(irg, &ia32_reg_classes[CLASS_ia32_flags],
641                            &flags_remat, NULL);
642
643         be_add_missing_keeps(irg);
644 }
645
646
647 /**
648  * Transforms a be_Reload into a ia32 Load.
649  */
650 static void transform_to_Load(ir_node *node)
651 {
652         ir_graph *irg        = get_irn_irg(node);
653         dbg_info *dbgi       = get_irn_dbg_info(node);
654         ir_node *block       = get_nodes_block(node);
655         ir_entity *ent       = be_get_frame_entity(node);
656         ir_mode *mode        = get_irn_mode(node);
657         ir_mode *spillmode   = get_spill_mode(node);
658         ir_node *noreg       = ia32_new_NoReg_gp(irg);
659         ir_node *ptr         = get_irg_frame(irg);
660         ir_node *mem         = get_irn_n(node, n_be_Reload_mem);
661         ir_node *new_op, *proj;
662         const arch_register_t *reg;
663
664         if (mode_is_float(spillmode)) {
665                 if (ia32_cg_config.use_sse2)
666                         new_op = new_bd_ia32_xLoad(dbgi, block, ptr, noreg, mem, spillmode);
667                 else
668                         new_op = new_bd_ia32_fld(dbgi, block, ptr, noreg, mem, spillmode);
669         }
670         else if (get_mode_size_bits(spillmode) == 128) {
671                 /* Reload 128 bit SSE registers */
672                 new_op = new_bd_ia32_xxLoad(dbgi, block, ptr, noreg, mem);
673         }
674         else
675                 new_op = new_bd_ia32_Load(dbgi, block, ptr, noreg, mem);
676
677         set_ia32_op_type(new_op, ia32_AddrModeS);
678         set_ia32_ls_mode(new_op, spillmode);
679         set_ia32_frame_ent(new_op, ent);
680         set_ia32_use_frame(new_op);
681         set_ia32_is_reload(new_op);
682
683         DBG_OPT_RELOAD2LD(node, new_op);
684
685         proj = new_rd_Proj(dbgi, new_op, mode, pn_ia32_Load_res);
686
687         sched_replace(node, new_op);
688
689         /* copy the register from the old node to the new Load */
690         reg = arch_get_irn_register(node);
691         arch_set_irn_register(proj, reg);
692
693         SET_IA32_ORIG_NODE(new_op, node);
694
695         exchange(node, proj);
696 }
697
698 /**
699  * Transforms a be_Spill node into a ia32 Store.
700  */
701 static void transform_to_Store(ir_node *node)
702 {
703         ir_graph *irg  = get_irn_irg(node);
704         dbg_info *dbgi = get_irn_dbg_info(node);
705         ir_node *block = get_nodes_block(node);
706         ir_entity *ent = be_get_frame_entity(node);
707         const ir_node *spillval = get_irn_n(node, n_be_Spill_val);
708         ir_mode *mode  = get_spill_mode(spillval);
709         ir_node *noreg = ia32_new_NoReg_gp(irg);
710         ir_node *nomem = get_irg_no_mem(irg);
711         ir_node *ptr   = get_irg_frame(irg);
712         ir_node *val   = get_irn_n(node, n_be_Spill_val);
713         ir_node *res;
714         ir_node *store;
715
716         if (mode_is_float(mode)) {
717                 if (ia32_cg_config.use_sse2) {
718                         store = new_bd_ia32_xStore(dbgi, block, ptr, noreg, nomem, val);
719                         res   = new_r_Proj(store, mode_M, pn_ia32_xStore_M);
720                 } else {
721                         store = new_bd_ia32_fst(dbgi, block, ptr, noreg, nomem, val, mode);
722                         res   = new_r_Proj(store, mode_M, pn_ia32_fst_M);
723                 }
724         } else if (get_mode_size_bits(mode) == 128) {
725                 /* Spill 128 bit SSE registers */
726                 store = new_bd_ia32_xxStore(dbgi, block, ptr, noreg, nomem, val);
727                 res   = new_r_Proj(store, mode_M, pn_ia32_xxStore_M);
728         } else {
729                 store = get_mode_size_bits(mode) == 8
730                         ? new_bd_ia32_Store_8bit(dbgi, block, ptr, noreg, nomem, val)
731                         : new_bd_ia32_Store     (dbgi, block, ptr, noreg, nomem, val);
732                 res   = new_r_Proj(store, mode_M, pn_ia32_Store_M);
733         }
734
735         set_ia32_op_type(store, ia32_AddrModeD);
736         set_ia32_ls_mode(store, mode);
737         set_ia32_frame_ent(store, ent);
738         set_ia32_use_frame(store);
739         set_ia32_is_spill(store);
740         SET_IA32_ORIG_NODE(store, node);
741         DBG_OPT_SPILL2ST(node, store);
742
743         sched_replace(node, store);
744         exchange(node, res);
745 }
746
747 static ir_node *create_push(ir_node *node, ir_node *schedpoint, ir_node *sp, ir_node *mem, ir_entity *ent)
748 {
749         dbg_info *dbgi  = get_irn_dbg_info(node);
750         ir_node  *block = get_nodes_block(node);
751         ir_graph *irg   = get_irn_irg(node);
752         ir_node  *noreg = ia32_new_NoReg_gp(irg);
753         ir_node  *frame = get_irg_frame(irg);
754
755         ir_node *push = new_bd_ia32_Push(dbgi, block, frame, noreg, mem, noreg, sp);
756
757         set_ia32_frame_ent(push, ent);
758         set_ia32_use_frame(push);
759         set_ia32_op_type(push, ia32_AddrModeS);
760         set_ia32_ls_mode(push, mode_Is);
761         set_ia32_is_spill(push);
762
763         sched_add_before(schedpoint, push);
764         return push;
765 }
766
767 static ir_node *create_pop(ir_node *node, ir_node *schedpoint, ir_node *sp, ir_entity *ent)
768 {
769         dbg_info *dbgi  = get_irn_dbg_info(node);
770         ir_node  *block = get_nodes_block(node);
771         ir_graph *irg   = get_irn_irg(node);
772         ir_node  *noreg = ia32_new_NoReg_gp(irg);
773         ir_node  *frame = get_irg_frame(irg);
774
775         ir_node *pop = new_bd_ia32_PopMem(dbgi, block, frame, noreg,
776                                           get_irg_no_mem(irg), sp);
777
778         set_ia32_frame_ent(pop, ent);
779         set_ia32_use_frame(pop);
780         set_ia32_op_type(pop, ia32_AddrModeD);
781         set_ia32_ls_mode(pop, mode_Is);
782         set_ia32_is_reload(pop);
783
784         sched_add_before(schedpoint, pop);
785
786         return pop;
787 }
788
789 static ir_node* create_spproj(ir_node *node, ir_node *pred, int pos)
790 {
791         dbg_info *dbgi   = get_irn_dbg_info(node);
792         ir_mode  *spmode = mode_Iu;
793         const arch_register_t *spreg = &ia32_registers[REG_ESP];
794         ir_node *sp;
795
796         sp = new_rd_Proj(dbgi, pred, spmode, pos);
797         arch_set_irn_register(sp, spreg);
798
799         return sp;
800 }
801
802 /**
803  * Transform MemPerm, currently we do this the ugly way and produce
804  * push/pop into/from memory cascades. This is possible without using
805  * any registers.
806  */
807 static void transform_MemPerm(ir_node *node)
808 {
809         ir_node  *block = get_nodes_block(node);
810         ir_graph *irg   = get_irn_irg(node);
811         ir_node  *sp    = be_get_initial_reg_value(irg, &ia32_registers[REG_ESP]);
812         int       arity = be_get_MemPerm_entity_arity(node);
813         ir_node **pops  = ALLOCAN(ir_node*, arity);
814         ir_node  *in[1];
815         ir_node  *keep;
816         int       i;
817
818         /* create Pushs */
819         for (i = 0; i < arity; ++i) {
820                 ir_entity *inent = be_get_MemPerm_in_entity(node, i);
821                 ir_entity *outent = be_get_MemPerm_out_entity(node, i);
822                 ir_type *enttype = get_entity_type(inent);
823                 unsigned entsize = get_type_size_bytes(enttype);
824                 unsigned entsize2 = get_type_size_bytes(get_entity_type(outent));
825                 ir_node *mem = get_irn_n(node, i + 1);
826                 ir_node *push;
827
828                 /* work around cases where entities have different sizes */
829                 if (entsize2 < entsize)
830                         entsize = entsize2;
831                 assert( (entsize == 4 || entsize == 8) && "spillslot on x86 should be 32 or 64 bit");
832
833                 push = create_push(node, node, sp, mem, inent);
834                 sp = create_spproj(node, push, pn_ia32_Push_stack);
835                 if (entsize == 8) {
836                         /* add another push after the first one */
837                         push = create_push(node, node, sp, mem, inent);
838                         add_ia32_am_offs_int(push, 4);
839                         sp = create_spproj(node, push, pn_ia32_Push_stack);
840                 }
841
842                 set_irn_n(node, i, new_r_Bad(irg, mode_X));
843         }
844
845         /* create pops */
846         for (i = arity - 1; i >= 0; --i) {
847                 ir_entity *inent = be_get_MemPerm_in_entity(node, i);
848                 ir_entity *outent = be_get_MemPerm_out_entity(node, i);
849                 ir_type *enttype = get_entity_type(outent);
850                 unsigned entsize = get_type_size_bytes(enttype);
851                 unsigned entsize2 = get_type_size_bytes(get_entity_type(inent));
852                 ir_node *pop;
853
854                 /* work around cases where entities have different sizes */
855                 if (entsize2 < entsize)
856                         entsize = entsize2;
857                 assert( (entsize == 4 || entsize == 8) && "spillslot on x86 should be 32 or 64 bit");
858
859                 pop = create_pop(node, node, sp, outent);
860                 sp = create_spproj(node, pop, pn_ia32_Pop_stack);
861                 if (entsize == 8) {
862                         add_ia32_am_offs_int(pop, 4);
863
864                         /* add another pop after the first one */
865                         pop = create_pop(node, node, sp, outent);
866                         sp = create_spproj(node, pop, pn_ia32_Pop_stack);
867                 }
868
869                 pops[i] = pop;
870         }
871
872         in[0] = sp;
873         keep  = be_new_Keep(block, 1, in);
874         sched_replace(node, keep);
875
876         /* exchange memprojs */
877         foreach_out_edge_safe(node, edge) {
878                 ir_node *proj = get_edge_src_irn(edge);
879                 int p = get_Proj_proj(proj);
880
881                 assert(p < arity);
882
883                 set_Proj_pred(proj, pops[p]);
884                 set_Proj_proj(proj, pn_ia32_Pop_M);
885         }
886
887         /* remove memperm */
888         kill_node(node);
889 }
890
891 /**
892  * Block-Walker: Calls the transform functions Spill and Reload.
893  */
894 static void ia32_after_ra_walker(ir_node *block, void *env)
895 {
896         (void) env;
897
898         /* beware: the schedule is changed here */
899         sched_foreach_reverse_safe(block, node) {
900                 if (be_is_Reload(node)) {
901                         transform_to_Load(node);
902                 } else if (be_is_Spill(node)) {
903                         transform_to_Store(node);
904                 } else if (be_is_MemPerm(node)) {
905                         transform_MemPerm(node);
906                 }
907         }
908 }
909
910 /**
911  * Collects nodes that need frame entities assigned.
912  */
913 static void ia32_collect_frame_entity_nodes(ir_node *node, void *data)
914 {
915         be_fec_env_t  *env = (be_fec_env_t*)data;
916         const ir_mode *mode;
917         int            align;
918
919         if (be_is_Reload(node) && be_get_frame_entity(node) == NULL) {
920                 mode  = get_spill_mode_mode(get_irn_mode(node));
921                 align = get_mode_size_bytes(mode);
922         } else if (is_ia32_irn(node)         &&
923                         get_ia32_frame_ent(node) == NULL &&
924                         is_ia32_use_frame(node)) {
925                 if (is_ia32_need_stackent(node))
926                         goto need_stackent;
927
928                 switch (get_ia32_irn_opcode(node)) {
929 need_stackent:
930                         case iro_ia32_Load: {
931                                 const ia32_attr_t *attr = get_ia32_attr_const(node);
932
933                                 if (attr->data.need_32bit_stackent) {
934                                         mode = mode_Is;
935                                 } else if (attr->data.need_64bit_stackent) {
936                                         mode = mode_Ls;
937                                 } else {
938                                         mode = get_ia32_ls_mode(node);
939                                         if (is_ia32_is_reload(node))
940                                                 mode = get_spill_mode_mode(mode);
941                                 }
942                                 align = get_mode_size_bytes(mode);
943                                 break;
944                         }
945
946                         case iro_ia32_fild:
947                         case iro_ia32_fld:
948                         case iro_ia32_xLoad: {
949                                 mode  = get_ia32_ls_mode(node);
950                                 align = 4;
951                                 break;
952                         }
953
954                         case iro_ia32_FldCW: {
955                                 /* although 2 byte would be enough 4 byte performs best */
956                                 mode  = mode_Iu;
957                                 align = 4;
958                                 break;
959                         }
960
961                         default:
962 #ifndef NDEBUG
963                                 panic("unexpected frame user while collection frame entity nodes");
964
965                         case iro_ia32_FnstCW:
966                         case iro_ia32_Store:
967                         case iro_ia32_fst:
968                         case iro_ia32_fist:
969                         case iro_ia32_fisttp:
970                         case iro_ia32_xStore:
971                         case iro_ia32_xStoreSimple:
972 #endif
973                                 return;
974                 }
975         } else {
976                 return;
977         }
978         be_node_needs_frame_entity(env, node, mode, align);
979 }
980
981 static int determine_ebp_input(ir_node *ret)
982 {
983         const arch_register_t *bp = &ia32_registers[REG_EBP];
984         int   arity               = get_irn_arity(ret);
985         int   i;
986
987         for (i = 0; i < arity; ++i) {
988                 ir_node *input = get_irn_n(ret, i);
989                 if (arch_get_irn_register(input) == bp)
990                         return i;
991         }
992         panic("no ebp input found at %+F", ret);
993 }
994
995 static void introduce_epilog(ir_node *ret)
996 {
997         const arch_register_t *sp         = &ia32_registers[REG_ESP];
998         const arch_register_t *bp         = &ia32_registers[REG_EBP];
999         ir_graph              *irg        = get_irn_irg(ret);
1000         ir_type               *frame_type = get_irg_frame_type(irg);
1001         unsigned               frame_size = get_type_size_bytes(frame_type);
1002         be_stack_layout_t     *layout     = be_get_irg_stack_layout(irg);
1003         ir_node               *block      = get_nodes_block(ret);
1004         ir_node               *first_sp   = get_irn_n(ret, n_be_Return_sp);
1005         ir_node               *curr_sp    = first_sp;
1006         ir_mode               *mode_gp    = ia32_reg_classes[CLASS_ia32_gp].mode;
1007
1008         if (!layout->sp_relative) {
1009                 int      n_ebp   = determine_ebp_input(ret);
1010                 ir_node *curr_bp = get_irn_n(ret, n_ebp);
1011                 if (ia32_cg_config.use_leave) {
1012                         ir_node *leave = new_bd_ia32_Leave(NULL, block, curr_bp);
1013                         curr_bp        = new_r_Proj(leave, mode_gp, pn_ia32_Leave_frame);
1014                         curr_sp        = new_r_Proj(leave, mode_gp, pn_ia32_Leave_stack);
1015                         arch_set_irn_register(curr_bp, bp);
1016                         arch_set_irn_register(curr_sp, sp);
1017                         sched_add_before(ret, leave);
1018                 } else {
1019                         ir_node *pop;
1020                         ir_node *curr_mem = get_irn_n(ret, n_be_Return_mem);
1021                         /* copy ebp to esp */
1022                         curr_sp = new_bd_ia32_CopyEbpEsp(NULL, block, curr_bp);
1023                         arch_set_irn_register(curr_sp, sp);
1024                         sched_add_before(ret, curr_sp);
1025
1026                         /* pop ebp */
1027                         pop      = new_bd_ia32_PopEbp(NULL, block, curr_mem, curr_sp);
1028                         curr_bp  = new_r_Proj(pop, mode_gp, pn_ia32_PopEbp_res);
1029                         curr_sp  = new_r_Proj(pop, mode_gp, pn_ia32_PopEbp_stack);
1030                         curr_mem = new_r_Proj(pop, mode_M, pn_ia32_Pop_M);
1031                         arch_set_irn_register(curr_bp, bp);
1032                         arch_set_irn_register(curr_sp, sp);
1033                         sched_add_before(ret, pop);
1034
1035                         set_irn_n(ret, n_be_Return_mem, curr_mem);
1036                 }
1037                 set_irn_n(ret, n_ebp, curr_bp);
1038         } else {
1039                 ir_node *incsp = be_new_IncSP(sp, block, curr_sp, -(int)frame_size, 0);
1040                 sched_add_before(ret, incsp);
1041                 curr_sp = incsp;
1042         }
1043         set_irn_n(ret, n_be_Return_sp, curr_sp);
1044
1045         /* keep verifier happy... */
1046         if (get_irn_n_edges(first_sp) == 0 && is_Proj(first_sp)) {
1047                 kill_node(first_sp);
1048         }
1049 }
1050
1051 /**
1052  * put the Prolog code at the beginning, epilog code before each return
1053  */
1054 static void introduce_prolog_epilog(ir_graph *irg)
1055 {
1056         const arch_register_t *sp         = &ia32_registers[REG_ESP];
1057         const arch_register_t *bp         = &ia32_registers[REG_EBP];
1058         ir_node               *start      = get_irg_start(irg);
1059         ir_node               *block      = get_nodes_block(start);
1060         ir_type               *frame_type = get_irg_frame_type(irg);
1061         unsigned               frame_size = get_type_size_bytes(frame_type);
1062         be_stack_layout_t     *layout     = be_get_irg_stack_layout(irg);
1063         ir_node               *initial_sp = be_get_initial_reg_value(irg, sp);
1064         ir_node               *curr_sp    = initial_sp;
1065         ir_mode               *mode_gp    = mode_Iu;
1066
1067         if (!layout->sp_relative) {
1068                 /* push ebp */
1069                 ir_node *mem        = get_irg_initial_mem(irg);
1070                 ir_node *noreg      = ia32_new_NoReg_gp(irg);
1071                 ir_node *initial_bp = be_get_initial_reg_value(irg, bp);
1072                 ir_node *push       = new_bd_ia32_Push(NULL, block, noreg, noreg, mem, initial_bp, initial_sp);
1073                 ir_node *incsp;
1074
1075                 curr_sp = new_r_Proj(push, mode_gp, pn_ia32_Push_stack);
1076                 arch_set_irn_register(curr_sp, sp);
1077                 sched_add_after(start, push);
1078
1079                 /* move esp to ebp */
1080                 ir_node *const curr_bp = be_new_Copy(block, curr_sp);
1081                 sched_add_after(push, curr_bp);
1082                 be_set_constr_single_reg_out(curr_bp, 0, bp, arch_register_req_type_ignore);
1083                 curr_sp = be_new_CopyKeep_single(block, curr_sp, curr_bp);
1084                 sched_add_after(curr_bp, curr_sp);
1085                 be_set_constr_single_reg_out(curr_sp, 0, sp, arch_register_req_type_produces_sp);
1086                 edges_reroute_except(initial_bp, curr_bp, push);
1087
1088                 incsp = be_new_IncSP(sp, block, curr_sp, frame_size, 0);
1089                 edges_reroute_except(initial_sp, incsp, push);
1090                 sched_add_after(curr_sp, incsp);
1091
1092                 /* make sure the initial IncSP is really used by someone */
1093                 if (get_irn_n_edges(incsp) <= 1) {
1094                         ir_node *in[] = { incsp };
1095                         ir_node *keep = be_new_Keep(block, 1, in);
1096                         sched_add_after(incsp, keep);
1097                 }
1098
1099                 layout->initial_bias = -4;
1100         } else {
1101                 ir_node *const incsp = be_new_IncSP(sp, block, initial_sp, frame_size, 0);
1102                 edges_reroute_except(initial_sp, incsp, incsp);
1103                 sched_add_after(start, incsp);
1104         }
1105
1106         /* introduce epilog for every return node */
1107         {
1108                 ir_node *end_block = get_irg_end_block(irg);
1109                 int      arity     = get_irn_arity(end_block);
1110                 int      i;
1111
1112                 for (i = 0; i < arity; ++i) {
1113                         ir_node *ret = get_irn_n(end_block, i);
1114                         assert(be_is_Return(ret));
1115                         introduce_epilog(ret);
1116                 }
1117         }
1118 }
1119
1120 /**
1121  * Last touchups for the graph before emit: x87 simulation to replace the
1122  * virtual with real x87 instructions, creating a block schedule and peephole
1123  * optimisations.
1124  */
1125 static void ia32_finish_graph(ir_graph *irg)
1126 {
1127         ia32_irg_data_t   *irg_data     = ia32_get_irg_data(irg);
1128         be_stack_layout_t *stack_layout = be_get_irg_stack_layout(irg);
1129         bool               at_begin     = stack_layout->sp_relative ? true : false;
1130         be_fec_env_t      *fec_env      = be_new_frame_entity_coalescer(irg);
1131
1132         /* create and coalesce frame entities */
1133         irg_walk_graph(irg, NULL, ia32_collect_frame_entity_nodes, fec_env);
1134         be_assign_entities(fec_env, ia32_set_frame_entity, at_begin);
1135         be_free_frame_entity_coalescer(fec_env);
1136
1137         irg_block_walk_graph(irg, NULL, ia32_after_ra_walker, NULL);
1138
1139         introduce_prolog_epilog(irg);
1140
1141         /* fix stack entity offsets */
1142         be_abi_fix_stack_nodes(irg);
1143         be_abi_fix_stack_bias(irg);
1144
1145         /* fix 2-address code constraints */
1146         ia32_finish_irg(irg);
1147
1148         /* we might have to rewrite x87 virtual registers */
1149         if (irg_data->do_x87_sim) {
1150                 ia32_x87_simulate_graph(irg);
1151         }
1152
1153         /* do peephole optimisations */
1154         ia32_peephole_optimization(irg);
1155
1156         be_remove_dead_nodes_from_schedule(irg);
1157
1158         /* create block schedule, this also removes empty blocks which might
1159          * produce critical edges */
1160         irg_data->blk_sched = be_create_block_schedule(irg);
1161 }
1162
1163 /**
1164  * Emits the code, closes the output file and frees
1165  * the code generator interface.
1166  */
1167 static void ia32_emit(ir_graph *irg)
1168 {
1169         if (ia32_cg_config.emit_machcode) {
1170                 ia32_gen_binary_routine(irg);
1171         } else {
1172                 ia32_gen_routine(irg);
1173         }
1174 }
1175
1176 /**
1177  * Returns the node representing the PIC base.
1178  */
1179 static ir_node *ia32_get_pic_base(ir_graph *irg)
1180 {
1181         ia32_irg_data_t *irg_data = ia32_get_irg_data(irg);
1182         ir_node         *block;
1183         ir_node         *get_eip = irg_data->get_eip;
1184         if (get_eip != NULL)
1185                 return get_eip;
1186
1187         block             = get_irg_start_block(irg);
1188         get_eip           = new_bd_ia32_GetEIP(NULL, block);
1189         irg_data->get_eip = get_eip;
1190
1191         return get_eip;
1192 }
1193
1194 /**
1195  * Initializes a IA32 code generator.
1196  */
1197 static void ia32_init_graph(ir_graph *irg)
1198 {
1199         struct obstack  *obst     = be_get_be_obst(irg);
1200         ia32_irg_data_t *irg_data = OALLOCZ(obst, ia32_irg_data_t);
1201
1202         irg_data->dump = (be_options.dump_flags & DUMP_BE) ? 1 : 0;
1203
1204         if (gprof) {
1205                 /* Linux gprof implementation needs base pointer */
1206                 be_options.omit_fp = 0;
1207         }
1208
1209         be_birg_from_irg(irg)->isa_link = irg_data;
1210 }
1211
1212 static const tarval_mode_info mo_integer = {
1213         TVO_HEX,
1214         "0x",
1215         NULL,
1216 };
1217
1218 /*
1219  * set the tarval output mode of all integer modes to decimal
1220  */
1221 static void set_tarval_output_modes(void)
1222 {
1223         size_t i;
1224
1225         for (i = ir_get_n_modes(); i > 0;) {
1226                 ir_mode *mode = ir_get_mode(--i);
1227
1228                 if (mode_is_int(mode))
1229                         set_tarval_mode_output_option(mode, &mo_integer);
1230         }
1231 }
1232
1233 extern const arch_isa_if_t ia32_isa_if;
1234
1235 static void init_asm_constraints(void)
1236 {
1237         be_init_default_asm_constraint_flags();
1238
1239         asm_constraint_flags['a'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1240         asm_constraint_flags['b'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1241         asm_constraint_flags['c'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1242         asm_constraint_flags['d'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1243         asm_constraint_flags['D'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1244         asm_constraint_flags['S'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1245         asm_constraint_flags['Q'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1246         asm_constraint_flags['q'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1247         asm_constraint_flags['A'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1248         asm_constraint_flags['l'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1249         asm_constraint_flags['R'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1250         asm_constraint_flags['r'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1251         asm_constraint_flags['p'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1252         asm_constraint_flags['f'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1253         asm_constraint_flags['t'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1254         asm_constraint_flags['u'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1255         asm_constraint_flags['Y'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1256         asm_constraint_flags['X'] = ASM_CONSTRAINT_FLAG_SUPPORTS_REGISTER;
1257         asm_constraint_flags['n'] = ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE;
1258         asm_constraint_flags['g'] = ASM_CONSTRAINT_FLAG_SUPPORTS_IMMEDIATE;
1259
1260         /* no support for autodecrement/autoincrement */
1261         asm_constraint_flags['<'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
1262         asm_constraint_flags['>'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
1263         /* no float consts */
1264         asm_constraint_flags['E'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
1265         asm_constraint_flags['F'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
1266         /* makes no sense on x86 */
1267         asm_constraint_flags['s'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
1268         /* no support for sse consts yet */
1269         asm_constraint_flags['C'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
1270         /* no support for x87 consts yet */
1271         asm_constraint_flags['G'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
1272         /* no support for mmx registers yet */
1273         asm_constraint_flags['y'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
1274         /* not available in 32bit mode */
1275         asm_constraint_flags['Z'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
1276         asm_constraint_flags['e'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
1277
1278         /* no code yet to determine register class needed... */
1279         asm_constraint_flags['X'] = ASM_CONSTRAINT_FLAG_NO_SUPPORT;
1280 }
1281
1282 /**
1283  * Check if Mux(sel, mux_true, mux_false) would represent a Max or Min operation
1284  */
1285 static bool mux_is_float_min_max(ir_node *sel, ir_node *mux_true,
1286                                  ir_node *mux_false)
1287 {
1288         ir_node    *cmp_l;
1289         ir_node    *cmp_r;
1290         ir_relation relation;
1291
1292         if (!is_Cmp(sel))
1293                 return false;
1294
1295         cmp_l = get_Cmp_left(sel);
1296         cmp_r = get_Cmp_right(sel);
1297         if (!mode_is_float(get_irn_mode(cmp_l)))
1298                 return false;
1299
1300         /* check for min/max. They're defined as (C-Semantik):
1301          *  min(a, b) = a < b ? a : b
1302          *  or min(a, b) = a <= b ? a : b
1303          *  max(a, b) = a > b ? a : b
1304          *  or max(a, b) = a >= b ? a : b
1305          * (Note we only handle float min/max here)
1306          */
1307         relation = get_Cmp_relation(sel);
1308         switch (relation) {
1309         case ir_relation_greater_equal:
1310         case ir_relation_greater:
1311                 /* this is a max */
1312                 if (cmp_l == mux_true && cmp_r == mux_false)
1313                         return true;
1314                 break;
1315         case ir_relation_less_equal:
1316         case ir_relation_less:
1317                 /* this is a min */
1318                 if (cmp_l == mux_true && cmp_r == mux_false)
1319                         return true;
1320                 break;
1321         case ir_relation_unordered_greater_equal:
1322         case ir_relation_unordered_greater:
1323                 /* this is a min */
1324                 if (cmp_l == mux_false && cmp_r == mux_true)
1325                         return true;
1326                 break;
1327         case ir_relation_unordered_less_equal:
1328         case ir_relation_unordered_less:
1329                 /* this is a max */
1330                 if (cmp_l == mux_false && cmp_r == mux_true)
1331                         return true;
1332                 break;
1333
1334         default:
1335                 break;
1336         }
1337
1338         return false;
1339 }
1340
1341 static bool mux_is_set(ir_node *sel, ir_node *mux_true, ir_node *mux_false)
1342 {
1343         ir_mode *mode = get_irn_mode(mux_true);
1344         (void) sel;
1345
1346         if (!mode_is_int(mode) && !mode_is_reference(mode)
1347                         && mode != mode_b)
1348                 return false;
1349
1350         if (is_Const(mux_true) && is_Const(mux_false)) {
1351                 /* we can create a set plus up two 3 instructions for any combination
1352                  * of constants */
1353                 return true;
1354         }
1355
1356         return false;
1357 }
1358
1359 static bool mux_is_float_const_const(ir_node *sel, ir_node *mux_true,
1360                                      ir_node *mux_false)
1361 {
1362         (void) sel;
1363
1364         if (!mode_is_float(get_irn_mode(mux_true)))
1365                 return false;
1366
1367         return is_Const(mux_true) && is_Const(mux_false);
1368 }
1369
1370 static bool mux_is_doz(ir_node *sel, ir_node *mux_true, ir_node *mux_false)
1371 {
1372         ir_node    *cmp_left;
1373         ir_node    *cmp_right;
1374         ir_node    *sub_left;
1375         ir_node    *sub_right;
1376         ir_mode    *mode;
1377         ir_relation relation;
1378
1379         if (!is_Cmp(sel))
1380                 return false;
1381
1382         mode = get_irn_mode(mux_true);
1383         if (mode_is_signed(mode) || mode_is_float(mode))
1384                 return false;
1385
1386         relation  = get_Cmp_relation(sel);
1387         cmp_left  = get_Cmp_left(sel);
1388         cmp_right = get_Cmp_right(sel);
1389
1390         /* "move" zero constant to false input */
1391         if (is_Const(mux_true) && is_Const_null(mux_true)) {
1392                 ir_node *tmp = mux_false;
1393                 mux_false = mux_true;
1394                 mux_true  = tmp;
1395                 relation = get_negated_relation(relation);
1396         }
1397         if (!is_Const(mux_false) || !is_Const_null(mux_false))
1398                 return false;
1399         if (!is_Sub(mux_true))
1400                 return false;
1401         sub_left  = get_Sub_left(mux_true);
1402         sub_right = get_Sub_right(mux_true);
1403
1404         /* Mux(a >=u b, 0, a-b) */
1405         if ((relation & ir_relation_greater)
1406                         && sub_left == cmp_left && sub_right == cmp_right)
1407                 return true;
1408         /* Mux(a <=u b, 0, b-a) */
1409         if ((relation & ir_relation_less)
1410                         && sub_left == cmp_right && sub_right == cmp_left)
1411                 return true;
1412
1413         return false;
1414 }
1415
1416 static int ia32_is_mux_allowed(ir_node *sel, ir_node *mux_false,
1417                                ir_node *mux_true)
1418 {
1419         ir_mode *mode;
1420
1421         /* middleend can handle some things */
1422         if (ir_is_optimizable_mux(sel, mux_false, mux_true))
1423                 return true;
1424         /* we can handle Set for all modes and compares */
1425         if (mux_is_set(sel, mux_true, mux_false))
1426                 return true;
1427         /* SSE has own min/max operations */
1428         if (ia32_cg_config.use_sse2
1429                         && mux_is_float_min_max(sel, mux_true, mux_false))
1430                 return true;
1431         /* we can handle Mux(?, Const[f], Const[f]) */
1432         if (mux_is_float_const_const(sel, mux_true, mux_false)) {
1433 #ifdef FIRM_GRGEN_BE
1434                 /* well, some code selectors can't handle it */
1435                 if (be_transformer != TRANSFORMER_PBQP
1436                                 || be_transformer != TRANSFORMER_RAND)
1437                         return true;
1438 #else
1439                 return true;
1440 #endif
1441         }
1442
1443         /* no support for 64bit inputs to cmov */
1444         mode = get_irn_mode(mux_true);
1445         if (get_mode_size_bits(mode) > 32)
1446                 return false;
1447         /* we can handle Abs for all modes and compares (except 64bit) */
1448         if (ir_mux_is_abs(sel, mux_false, mux_true) != 0)
1449                 return true;
1450         /* we can't handle MuxF yet */
1451         if (mode_is_float(mode))
1452                 return false;
1453
1454         if (mux_is_doz(sel, mux_true, mux_false))
1455                 return true;
1456
1457         /* Check Cmp before the node */
1458         if (is_Cmp(sel)) {
1459                 ir_mode *cmp_mode = get_irn_mode(get_Cmp_left(sel));
1460
1461                 /* we can't handle 64bit compares */
1462                 if (get_mode_size_bits(cmp_mode) > 32)
1463                         return false;
1464
1465                 /* we can't handle float compares */
1466                 if (mode_is_float(cmp_mode))
1467                         return false;
1468         }
1469
1470         /* did we disable cmov generation? */
1471         if (!ia32_cg_config.use_cmov)
1472                 return false;
1473
1474         /* we can use a cmov */
1475         return true;
1476 }
1477
1478 /**
1479  * Create the trampoline code.
1480  */
1481 static ir_node *ia32_create_trampoline_fkt(ir_node *block, ir_node *mem, ir_node *trampoline, ir_node *env, ir_node *callee)
1482 {
1483         ir_graph *const irg  = get_irn_irg(block);
1484         ir_node  *      p    = trampoline;
1485         ir_mode  *const mode = get_irn_mode(p);
1486         ir_node  *const one  = new_r_Const(irg, get_mode_one(mode_Iu));
1487         ir_node  *const four = new_r_Const_long(irg, mode_Iu, 4);
1488         ir_node  *      st;
1489
1490         /* mov  ecx,<env> */
1491         st  = new_r_Store(block, mem, p, new_r_Const_long(irg, mode_Bu, 0xb9), cons_none);
1492         mem = new_r_Proj(st, mode_M, pn_Store_M);
1493         p   = new_r_Add(block, p, one, mode);
1494         st  = new_r_Store(block, mem, p, env, cons_none);
1495         mem = new_r_Proj(st, mode_M, pn_Store_M);
1496         p   = new_r_Add(block, p, four, mode);
1497         /* jmp  <callee> */
1498         st  = new_r_Store(block, mem, p, new_r_Const_long(irg, mode_Bu, 0xe9), cons_none);
1499         mem = new_r_Proj(st, mode_M, pn_Store_M);
1500         p   = new_r_Add(block, p, one, mode);
1501         st  = new_r_Store(block, mem, p, callee, cons_none);
1502         mem = new_r_Proj(st, mode_M, pn_Store_M);
1503
1504         return mem;
1505 }
1506
1507 static const ir_settings_arch_dep_t ia32_arch_dep = {
1508         1,                   /* also use subs */
1509         4,                   /* maximum shifts */
1510         63,                  /* maximum shift amount */
1511         ia32_evaluate_insn,  /* evaluate the instruction sequence */
1512
1513         1,  /* allow Mulhs */
1514         1,  /* allow Mulus */
1515         32, /* Mulh allowed up to 32 bit */
1516 };
1517 static backend_params ia32_backend_params = {
1518         1,     /* support inline assembly */
1519         1,     /* support Rotl nodes */
1520         0,     /* little endian */
1521         1,     /* modulo shift efficient */
1522         0,     /* non-modulo shift not efficient */
1523         &ia32_arch_dep, /* will be set later */
1524         ia32_is_mux_allowed,
1525         32,    /* machine_size */
1526         NULL,  /* float arithmetic mode, will be set below */
1527         NULL,  /* long long type */
1528         NULL,  /* unsigned long long type */
1529         NULL,  /* long double type */
1530         12,    /* size of trampoline code */
1531         4,     /* alignment of trampoline code */
1532         ia32_create_trampoline_fkt,
1533         4      /* alignment of stack parameter */
1534 };
1535
1536 /**
1537  * Initializes the backend ISA.
1538  */
1539 static void ia32_init(void)
1540 {
1541         ir_mode *mode_long_long;
1542         ir_mode *mode_unsigned_long_long;
1543         ir_type *type_long_long;
1544         ir_type *type_unsigned_long_long;
1545
1546         ia32_setup_cg_config();
1547
1548         init_asm_constraints();
1549
1550         ia32_mode_fpcw = new_int_mode("Fpcw", irma_twos_complement, 16, 0, 0);
1551
1552         /* note mantissa is 64bit but with explicitely encoded 1 so the really
1553          * usable part as counted by firm is only 63 bits */
1554         ia32_mode_E = new_float_mode("E", irma_x86_extended_float, 15, 63);
1555         ia32_type_E = new_type_primitive(ia32_mode_E);
1556         set_type_size_bytes(ia32_type_E, 12);
1557         set_type_alignment_bytes(ia32_type_E, 4);
1558
1559         mode_long_long = new_int_mode("long long", irma_twos_complement, 64, 1, 64);
1560         type_long_long = new_type_primitive(mode_long_long);
1561         mode_unsigned_long_long
1562                 = new_int_mode("unsigned long long", irma_twos_complement, 64, 0, 64);
1563         type_unsigned_long_long = new_type_primitive(mode_unsigned_long_long);
1564
1565         ia32_backend_params.type_long_long          = type_long_long;
1566         ia32_backend_params.type_unsigned_long_long = type_unsigned_long_long;
1567
1568         if (ia32_cg_config.use_sse2 || ia32_cg_config.use_softfloat) {
1569                 ia32_backend_params.mode_float_arithmetic = NULL;
1570                 ia32_backend_params.type_long_double = NULL;
1571         } else {
1572                 ia32_backend_params.mode_float_arithmetic = ia32_mode_E;
1573                 ia32_backend_params.type_long_double      = ia32_type_E;
1574         }
1575
1576         ia32_register_init();
1577         obstack_init(&opcodes_obst);
1578         ia32_create_opcodes(&ia32_irn_ops);
1579 }
1580
1581 static void ia32_finish(void)
1582 {
1583         if (between_type != NULL) {
1584                 free_type(between_type);
1585                 between_type = NULL;
1586         }
1587         ia32_free_opcodes();
1588         obstack_free(&opcodes_obst, NULL);
1589 }
1590
1591 /**
1592  * The template that generates a new ISA object.
1593  * Note that this template can be changed by command line
1594  * arguments.
1595  */
1596 static ia32_isa_t ia32_isa_template = {
1597         {
1598                 &ia32_isa_if,             /* isa interface implementation */
1599                 N_IA32_REGISTERS,
1600                 ia32_registers,
1601                 N_IA32_CLASSES,
1602                 ia32_reg_classes,
1603                 &ia32_registers[REG_ESP], /* stack pointer register */
1604                 &ia32_registers[REG_EBP], /* base pointer register */
1605                 2,                        /* power of two stack alignment, 2^2 == 4 */
1606                 7,                        /* costs for a spill instruction */
1607                 5,                        /* costs for a reload instruction */
1608                 false,                    /* no custom abi handling */
1609         },
1610         NULL,                       /* tv_ents */
1611         IA32_FPU_ARCH_X87,          /* FPU architecture */
1612 };
1613
1614 static arch_env_t *ia32_begin_codegeneration(void)
1615 {
1616         ia32_isa_t *isa = XMALLOC(ia32_isa_t);
1617
1618         set_tarval_output_modes();
1619
1620         *isa        = ia32_isa_template;
1621         isa->tv_ent = pmap_create();
1622
1623         return &isa->base;
1624 }
1625
1626 /**
1627  * Closes the output file and frees the ISA structure.
1628  */
1629 static void ia32_end_codegeneration(void *self)
1630 {
1631         ia32_isa_t *isa = (ia32_isa_t*)self;
1632         pmap_destroy(isa->tv_ent);
1633         free(self);
1634 }
1635
1636 /**
1637  * Returns the register for parameter nr.
1638  */
1639 static const arch_register_t *ia32_get_RegParam_reg(unsigned cc, unsigned nr,
1640                                                     const ir_mode *mode)
1641 {
1642         static const arch_register_t *gpreg_param_reg_fastcall[] = {
1643                 &ia32_registers[REG_ECX],
1644                 &ia32_registers[REG_EDX],
1645                 NULL
1646         };
1647         static const unsigned MAXNUM_GPREG_ARGS = 3;
1648
1649         static const arch_register_t *gpreg_param_reg_regparam[] = {
1650                 &ia32_registers[REG_EAX],
1651                 &ia32_registers[REG_EDX],
1652                 &ia32_registers[REG_ECX]
1653         };
1654
1655         static const arch_register_t *gpreg_param_reg_this[] = {
1656                 &ia32_registers[REG_ECX],
1657                 NULL,
1658                 NULL
1659         };
1660
1661         static const arch_register_t *fpreg_sse_param_reg_std[] = {
1662                 &ia32_registers[REG_XMM0],
1663                 &ia32_registers[REG_XMM1],
1664                 &ia32_registers[REG_XMM2],
1665                 &ia32_registers[REG_XMM3],
1666                 &ia32_registers[REG_XMM4],
1667                 &ia32_registers[REG_XMM5],
1668                 &ia32_registers[REG_XMM6],
1669                 &ia32_registers[REG_XMM7]
1670         };
1671
1672         static const arch_register_t *fpreg_sse_param_reg_this[] = {
1673                 NULL,  /* in case of a "this" pointer, the first parameter must not be a float */
1674         };
1675         static const unsigned MAXNUM_SSE_ARGS = 8;
1676
1677         if ((cc & cc_this_call) && nr == 0)
1678                 return gpreg_param_reg_this[0];
1679
1680         if (! (cc & cc_reg_param))
1681                 return NULL;
1682
1683         if (mode_is_float(mode)) {
1684                 if (!ia32_cg_config.use_sse2 || (cc & cc_fpreg_param) == 0)
1685                         return NULL;
1686                 if (nr >= MAXNUM_SSE_ARGS)
1687                         return NULL;
1688
1689                 if (cc & cc_this_call) {
1690                         return fpreg_sse_param_reg_this[nr];
1691                 }
1692                 return fpreg_sse_param_reg_std[nr];
1693         } else if (mode_is_int(mode) || mode_is_reference(mode)) {
1694                 unsigned num_regparam;
1695
1696                 if (get_mode_size_bits(mode) > 32)
1697                         return NULL;
1698
1699                 if (nr >= MAXNUM_GPREG_ARGS)
1700                         return NULL;
1701
1702                 if (cc & cc_this_call) {
1703                         return gpreg_param_reg_this[nr];
1704                 }
1705                 num_regparam = cc & ~cc_bits;
1706                 if (num_regparam == 0) {
1707                         /* default fastcall */
1708                         return gpreg_param_reg_fastcall[nr];
1709                 }
1710                 if (nr < num_regparam)
1711                         return gpreg_param_reg_regparam[nr];
1712                 return NULL;
1713         }
1714
1715         panic("unknown argument mode");
1716 }
1717
1718 /**
1719  * Get the ABI restrictions for procedure calls.
1720  */
1721 static void ia32_get_call_abi(ir_type *method_type, be_abi_call_t *abi)
1722 {
1723         unsigned  cc;
1724         int       n, i, regnum;
1725         int                 pop_amount = 0;
1726         be_abi_call_flags_t call_flags = be_abi_call_get_flags(abi);
1727
1728         /* set abi flags for calls */
1729         /* call_flags.try_omit_fp                 not changed: can handle both settings */
1730         call_flags.call_has_imm = false;  /* No call immediate, we handle this by ourselves */
1731
1732         /* set parameter passing style */
1733         be_abi_call_set_flags(abi, call_flags, &ia32_abi_callbacks);
1734
1735         cc = get_method_calling_convention(method_type);
1736         if (get_method_variadicity(method_type) == variadicity_variadic) {
1737                 /* pass all parameters of a variadic function on the stack */
1738                 cc = cc_cdecl_set | (cc & cc_this_call);
1739         } else {
1740                 if (get_method_additional_properties(method_type) & mtp_property_private &&
1741                     ia32_cg_config.optimize_cc) {
1742                         /* set the fast calling conventions (allowing up to 3) */
1743                         cc = SET_FASTCALL(cc) | 3;
1744                 }
1745         }
1746
1747         /* we have to pop the shadow parameter ourself for compound calls */
1748         if ( (get_method_calling_convention(method_type) & cc_compound_ret)
1749                         && !(cc & cc_reg_param)) {
1750                 pop_amount += get_mode_size_bytes(mode_P_data);
1751         }
1752
1753         n = get_method_n_params(method_type);
1754         for (i = regnum = 0; i < n; i++) {
1755                 const arch_register_t *reg  = NULL;
1756                 ir_type               *tp   = get_method_param_type(method_type, i);
1757                 ir_mode               *mode = get_type_mode(tp);
1758
1759                 if (mode != NULL) {
1760                         reg  = ia32_get_RegParam_reg(cc, regnum, mode);
1761                 }
1762                 if (reg != NULL) {
1763                         be_abi_call_param_reg(abi, i, reg, ABI_CONTEXT_BOTH);
1764                         ++regnum;
1765                 } else {
1766                         /* Micro optimisation: if the mode is shorter than 4 bytes, load 4 bytes.
1767                          * movl has a shorter opcode than mov[sz][bw]l */
1768                         ir_mode *load_mode = mode;
1769
1770                         if (mode != NULL) {
1771                                 unsigned size = get_mode_size_bytes(mode);
1772
1773                                 if (cc & cc_callee_clear_stk) {
1774                                         pop_amount += (size + 3U) & ~3U;
1775                                 }
1776
1777                                 if (size < 4) load_mode = mode_Iu;
1778                         }
1779
1780                         be_abi_call_param_stack(abi, i, load_mode, 4, 0, 0, ABI_CONTEXT_BOTH);
1781                 }
1782         }
1783
1784         be_abi_call_set_pop(abi, pop_amount);
1785
1786         /* set return registers */
1787         n = get_method_n_ress(method_type);
1788
1789         assert(n <= 2 && "more than two results not supported");
1790
1791         /* In case of 64bit returns, we will have two 32bit values */
1792         if (n == 2) {
1793                 ir_type *tp   = get_method_res_type(method_type, 0);
1794                 ir_mode *mode = get_type_mode(tp);
1795
1796                 assert(!mode_is_float(mode) && "two FP results not supported");
1797
1798                 tp   = get_method_res_type(method_type, 1);
1799                 mode = get_type_mode(tp);
1800
1801                 assert(!mode_is_float(mode) && "mixed INT, FP results not supported");
1802
1803                 be_abi_call_res_reg(abi, 0, &ia32_registers[REG_EAX], ABI_CONTEXT_BOTH);
1804                 be_abi_call_res_reg(abi, 1, &ia32_registers[REG_EDX], ABI_CONTEXT_BOTH);
1805         }
1806         else if (n == 1) {
1807                 ir_type *tp   = get_method_res_type(method_type, 0);
1808                 ir_mode *mode = get_type_mode(tp);
1809                 const arch_register_t *reg;
1810                 assert(is_atomic_type(tp));
1811
1812                 reg = mode_is_float(mode) ? &ia32_registers[REG_ST0] : &ia32_registers[REG_EAX];
1813
1814                 be_abi_call_res_reg(abi, 0, reg, ABI_CONTEXT_BOTH);
1815         }
1816 }
1817
1818 static void ia32_mark_remat(ir_node *node)
1819 {
1820         if (is_ia32_irn(node)) {
1821                 set_ia32_is_remat(node);
1822         }
1823 }
1824
1825 static asm_constraint_flags_t ia32_parse_asm_constraint(const char **c)
1826 {
1827         (void) c;
1828
1829         /* we already added all our simple flags to the flags modifier list in
1830          * init, so this flag we don't know. */
1831         return ASM_CONSTRAINT_FLAG_INVALID;
1832 }
1833
1834 static int ia32_is_valid_clobber(const char *clobber)
1835 {
1836         return ia32_get_clobber_register(clobber) != NULL;
1837 }
1838
1839 static void ia32_lower_for_target(void)
1840 {
1841         ir_mode *mode_gp = ia32_reg_classes[CLASS_ia32_gp].mode;
1842         size_t i, n_irgs = get_irp_n_irgs();
1843
1844         /* perform doubleword lowering */
1845         lwrdw_param_t lower_dw_params = {
1846                 1,  /* little endian */
1847                 64, /* doubleword size */
1848                 ia32_create_intrinsic_fkt,
1849                 &intrinsic_env,
1850         };
1851
1852         /* lower compound param handling
1853          * Note: we lower compound arguments ourself, since on ia32 we don't
1854          * have hidden parameters but know where to find the structs on the stack.
1855          * (This also forces us to always allocate space for the compound arguments
1856          *  on the callframe and we can't just use an arbitrary position on the
1857          *  stackframe)
1858          */
1859         lower_calls_with_compounds(LF_RETURN_HIDDEN | LF_DONT_LOWER_ARGUMENTS);
1860
1861         /* replace floating point operations by function calls */
1862         if (ia32_cg_config.use_softfloat) {
1863                 lower_floating_point();
1864         }
1865
1866         for (i = 0; i < n_irgs; ++i) {
1867                 ir_graph *irg = get_irp_irg(i);
1868                 /* break up switches with wide ranges */
1869                 lower_switch(irg, 4, 256, mode_gp);
1870         }
1871
1872         ir_prepare_dw_lowering(&lower_dw_params);
1873         ir_lower_dw_ops();
1874
1875         for (i = 0; i < n_irgs; ++i) {
1876                 ir_graph *irg = get_irp_irg(i);
1877                 /* lower for mode_b stuff */
1878                 ir_lower_mode_b(irg, mode_Iu);
1879         }
1880
1881         for (i = 0; i < n_irgs; ++i) {
1882                 ir_graph *irg = get_irp_irg(i);
1883                 /* Turn all small CopyBs into loads/stores, keep medium-sized CopyBs,
1884                  * so we can generate rep movs later, and turn all big CopyBs into
1885                  * memcpy calls. */
1886                 lower_CopyB(irg, 64, 8193, true);
1887         }
1888 }
1889
1890 /**
1891  * Returns the libFirm configuration parameter for this backend.
1892  */
1893 static const backend_params *ia32_get_libfirm_params(void)
1894 {
1895         return &ia32_backend_params;
1896 }
1897
1898 /**
1899  * Check if the given register is callee or caller save.
1900  */
1901 static int ia32_register_saved_by(const arch_register_t *reg, int callee)
1902 {
1903         switch (reg->global_index) {
1904         case REG_EBX:
1905         case REG_EBP:
1906         case REG_ESI:
1907         case REG_EDI:
1908         case REG_FPCW:
1909                 return callee;
1910
1911         case REG_EAX:
1912         case REG_ECX:
1913         case REG_EDX:
1914         case REG_ST0:
1915         case REG_ST1:
1916         case REG_ST2:
1917         case REG_ST3:
1918         case REG_ST4:
1919         case REG_ST5:
1920         case REG_ST6:
1921         case REG_ST7:
1922         case REG_XMM0:
1923         case REG_XMM1:
1924         case REG_XMM2:
1925         case REG_XMM3:
1926         case REG_XMM4:
1927         case REG_XMM5:
1928         case REG_XMM6:
1929         case REG_XMM7:
1930                 return !callee;
1931
1932         default:
1933                 return 0;
1934         }
1935 }
1936
1937 static const lc_opt_enum_int_items_t gas_items[] = {
1938         { "elf",   OBJECT_FILE_FORMAT_ELF    },
1939         { "mingw", OBJECT_FILE_FORMAT_COFF   },
1940         { "macho", OBJECT_FILE_FORMAT_MACH_O },
1941         { NULL,    0 }
1942 };
1943
1944 static lc_opt_enum_int_var_t gas_var = {
1945         (int*) &be_gas_object_file_format, gas_items
1946 };
1947
1948 #ifdef FIRM_GRGEN_BE
1949 static const lc_opt_enum_int_items_t transformer_items[] = {
1950         { "default", TRANSFORMER_DEFAULT },
1951         { "pbqp",    TRANSFORMER_PBQP    },
1952         { "random",  TRANSFORMER_RAND    },
1953         { NULL,      0                   }
1954 };
1955
1956 static lc_opt_enum_int_var_t transformer_var = {
1957         (int*)&be_transformer, transformer_items
1958 };
1959 #endif
1960
1961 static const lc_opt_table_entry_t ia32_options[] = {
1962         LC_OPT_ENT_ENUM_INT("gasmode", "set the GAS compatibility mode", &gas_var),
1963 #ifdef FIRM_GRGEN_BE
1964         LC_OPT_ENT_ENUM_INT("transformer", "the transformer used for code selection", &transformer_var),
1965 #endif
1966         LC_OPT_ENT_INT ("stackalign", "set power of two stack alignment for calls",
1967                         &ia32_isa_template.base.stack_alignment),
1968         LC_OPT_ENT_BOOL("gprof",      "create gprof profiling code",                                    &gprof),
1969         LC_OPT_LAST
1970 };
1971
1972 const arch_isa_if_t ia32_isa_if = {
1973         ia32_init,
1974         ia32_finish,
1975         ia32_get_libfirm_params,
1976         ia32_lower_for_target,
1977         ia32_parse_asm_constraint,
1978         ia32_is_valid_clobber,
1979
1980         ia32_begin_codegeneration,
1981         ia32_end_codegeneration,
1982         ia32_init_graph,
1983         ia32_get_call_abi,
1984         ia32_mark_remat,
1985         ia32_get_pic_base,   /* return node used as base in pic code addresses */
1986         be_new_spill,
1987         be_new_reload,
1988         ia32_register_saved_by,
1989
1990         ia32_handle_intrinsics,
1991         ia32_before_abi,     /* before abi introduce hook */
1992         ia32_prepare_graph,
1993         ia32_before_ra,      /* before register allocation hook */
1994         ia32_finish_graph,   /* called before codegen */
1995         ia32_emit,           /* emit && done */
1996 };
1997
1998 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_arch_ia32)
1999 void be_init_arch_ia32(void)
2000 {
2001         lc_opt_entry_t *be_grp   = lc_opt_get_grp(firm_opt_get_root(), "be");
2002         lc_opt_entry_t *ia32_grp = lc_opt_get_grp(be_grp, "ia32");
2003
2004         lc_opt_add_table(ia32_grp, ia32_options);
2005         be_register_isa_if("ia32", &ia32_isa_if);
2006
2007         ia32_init_emitter();
2008         ia32_init_finish();
2009         ia32_init_optimize();
2010         ia32_init_transform();
2011         ia32_init_x87();
2012         ia32_init_architecture();
2013 }