ia32: Change the command line switch "nooptcc" to "optcc".
[libfirm] / ir / be / ia32 / ia32_architecture.c
1 /*
2  * Copyright (C) 1995-2010 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       ia32 architecture variants
23  * @author      Michael Beck, Matthias Braun
24  */
25 #include "config.h"
26
27 #include <stdbool.h>
28 #include "lc_opts.h"
29 #include "lc_opts_enum.h"
30
31 #include "irtools.h"
32
33 #include "bearch_ia32_t.h"
34 #include "ia32_architecture.h"
35
36 #undef NATIVE_X86
37
38 #ifdef _MSC_VER
39 #if defined(_M_IX86) || defined(_M_X64)
40 #include <intrin.h>
41 #define NATIVE_X86
42 #endif
43 #else
44 #if defined(__i386__) || defined(__x86_64__)
45 #define NATIVE_X86
46 #endif
47 #endif
48
49 ia32_code_gen_config_t  ia32_cg_config;
50
51 /**
52  * CPU architectures and features.
53  */
54 typedef enum cpu_arch_features {
55         arch_generic32        = 0x00000001, /**< no specific architecture */
56
57         arch_i386             = 0x00000002, /**< i386 architecture */
58         arch_i486             = 0x00000004, /**< i486 architecture */
59         arch_pentium          = 0x00000008, /**< Pentium architecture */
60         arch_ppro             = 0x00000010, /**< PentiumPro architecture */
61         arch_netburst         = 0x00000020, /**< Netburst architecture */
62         arch_nocona           = 0x00000040, /**< Nocona architecture */
63         arch_core2            = 0x00000080, /**< Core2 architecture */
64         arch_atom             = 0x00000100, /**< Atom architecture */
65
66         arch_k6               = 0x00000200, /**< k6 architecture */
67         arch_geode            = 0x00000400, /**< Geode architecture */
68         arch_athlon           = 0x00000800, /**< Athlon architecture */
69         arch_k8               = 0x00001000, /**< K8/Opteron architecture */
70         arch_k10              = 0x00002000, /**< K10/Barcelona architecture */
71
72         arch_mask             = 0x00003FFF,
73
74         arch_athlon_plus      = arch_athlon | arch_k8 | arch_k10,
75         arch_all_amd          = arch_k6 | arch_geode | arch_athlon_plus,
76
77         arch_feature_mmx      = 0x00004000, /**< MMX instructions */
78         arch_feature_cmov     = 0x00008000, /**< cmov instructions */
79         arch_feature_p6_insn  = 0x00010000, /**< PentiumPro instructions */
80         arch_feature_sse1     = 0x00020000, /**< SSE1 instructions */
81         arch_feature_sse2     = 0x00040000, /**< SSE2 instructions */
82         arch_feature_sse3     = 0x00080000, /**< SSE3 instructions */
83         arch_feature_ssse3    = 0x00100000, /**< SSSE3 instructions */
84         arch_feature_3DNow    = 0x00200000, /**< 3DNow! instructions */
85         arch_feature_3DNowE   = 0x00400000, /**< Enhanced 3DNow! instructions */
86         arch_feature_64bit    = 0x00800000, /**< x86_64 support */
87         arch_feature_sse4_1   = 0x01000000, /**< SSE4.1 instructions */
88         arch_feature_sse4_2   = 0x02000000, /**< SSE4.2 instructions */
89         arch_feature_sse4a    = 0x04000000, /**< SSE4a instructions */
90         arch_feature_popcnt   = 0x08000000, /**< popcnt instruction */
91
92         arch_mmx_insn     = arch_feature_mmx,                         /**< MMX instructions */
93         arch_sse1_insn    = arch_feature_sse1   | arch_mmx_insn,      /**< SSE1 instructions, include MMX */
94         arch_sse2_insn    = arch_feature_sse2   | arch_sse1_insn,     /**< SSE2 instructions, include SSE1 */
95         arch_sse3_insn    = arch_feature_sse3   | arch_sse2_insn,     /**< SSE3 instructions, include SSE2 */
96         arch_ssse3_insn   = arch_feature_ssse3  | arch_sse3_insn,     /**< SSSE3 instructions, include SSE3 */
97         arch_sse4_1_insn  = arch_feature_sse4_1 | arch_ssse3_insn,    /**< SSE4.1 instructions, include SSSE3 */
98         arch_sse4_2_insn  = arch_feature_sse4_2 | arch_sse4_1_insn,   /**< SSE4.2 instructions, include SSE4.1 */
99         arch_sse4a_insn   = arch_feature_sse4a  | arch_ssse3_insn,    /**< SSE4a instructions, include SSSE3 */
100
101         arch_3DNow_insn   = arch_feature_3DNow  | arch_feature_mmx,   /**< 3DNow! instructions, including MMX */
102         arch_3DNowE_insn  = arch_feature_3DNowE | arch_3DNow_insn,    /**< Enhanced 3DNow! instructions */
103         arch_64bit_insn   = arch_feature_64bit  | arch_sse2_insn,     /**< x86_64 support, includes SSE2 */
104
105         cpu_generic             = arch_generic32,
106
107         /* intel CPUs */
108         cpu_i386                = arch_i386,
109         cpu_i486                = arch_i486,
110         cpu_pentium             = arch_pentium,
111         cpu_pentium_mmx         = arch_pentium | arch_mmx_insn,
112         cpu_pentium_pro_generic = arch_ppro | arch_feature_p6_insn,
113         cpu_pentium_pro         = arch_ppro | arch_feature_cmov | arch_feature_p6_insn,
114         cpu_pentium_2           = arch_ppro | arch_feature_cmov | arch_feature_p6_insn | arch_mmx_insn,
115         cpu_pentium_3           = arch_ppro | arch_feature_cmov | arch_feature_p6_insn | arch_sse1_insn,
116         cpu_pentium_m           = arch_ppro | arch_feature_cmov | arch_feature_p6_insn | arch_sse2_insn,
117         cpu_netburst_generic    = arch_netburst | arch_feature_p6_insn,
118         cpu_pentium_4           = arch_netburst | arch_feature_cmov | arch_feature_p6_insn | arch_sse2_insn,
119         cpu_prescott            = arch_nocona | arch_feature_cmov | arch_feature_p6_insn | arch_sse3_insn,
120         cpu_nocona              = arch_nocona | arch_feature_cmov | arch_feature_p6_insn | arch_64bit_insn | arch_sse3_insn,
121         cpu_core2_generic       = arch_core2 | arch_feature_p6_insn,
122         cpu_core2               = arch_core2 | arch_feature_cmov | arch_feature_p6_insn | arch_64bit_insn | arch_ssse3_insn,
123         cpu_penryn              = arch_core2 | arch_feature_cmov | arch_feature_p6_insn | arch_64bit_insn | arch_sse4_1_insn,
124         cpu_atom_generic        = arch_atom | arch_feature_p6_insn,
125         cpu_atom                = arch_atom | arch_feature_cmov | arch_feature_p6_insn | arch_ssse3_insn,
126
127         /* AMD CPUs */
128         cpu_k6_generic     = arch_k6,
129         cpu_k6             = arch_k6 | arch_mmx_insn,
130         cpu_k6_PLUS        = arch_k6 | arch_3DNow_insn,
131         cpu_geode_generic  = arch_geode,
132         cpu_geode          = arch_geode  | arch_sse1_insn | arch_3DNowE_insn,
133         cpu_athlon_generic = arch_athlon | arch_feature_p6_insn,
134         cpu_athlon_old     = arch_athlon | arch_3DNowE_insn | arch_feature_cmov | arch_feature_p6_insn,
135         cpu_athlon         = arch_athlon | arch_sse1_insn | arch_3DNowE_insn | arch_feature_cmov | arch_feature_p6_insn,
136         cpu_athlon64       = arch_athlon | arch_sse2_insn | arch_3DNowE_insn | arch_feature_cmov | arch_feature_p6_insn | arch_64bit_insn,
137         cpu_k8_generic     = arch_k8  | arch_feature_p6_insn,
138         cpu_k8             = arch_k8  | arch_3DNowE_insn | arch_feature_cmov | arch_feature_p6_insn | arch_64bit_insn,
139         cpu_k8_sse3        = arch_k8  | arch_3DNowE_insn | arch_feature_cmov | arch_feature_p6_insn | arch_64bit_insn | arch_sse3_insn,
140         cpu_k10_generic    = arch_k10 | arch_feature_p6_insn,
141         cpu_k10            = arch_k10 | arch_3DNowE_insn | arch_feature_cmov | arch_feature_p6_insn | arch_feature_popcnt | arch_64bit_insn | arch_sse4a_insn,
142
143         /* other CPUs */
144         cpu_winchip_c6  = arch_i486 | arch_feature_mmx,
145         cpu_winchip2    = arch_i486 | arch_feature_mmx | arch_feature_3DNow,
146         cpu_c3          = arch_i486 | arch_feature_mmx | arch_feature_3DNow,
147         cpu_c3_2        = arch_ppro | arch_feature_cmov | arch_feature_p6_insn | arch_sse1_insn, /* really no 3DNow! */
148
149         cpu_autodetect  = 0,
150 } cpu_arch_features;
151 ENUM_BITSET(cpu_arch_features)
152
153 static int               opt_size             = 0;
154 static int               emit_machcode        = 0;
155 static cpu_arch_features arch                 = cpu_generic;
156 static cpu_arch_features opt_arch             = cpu_generic;
157 static int               fpu_arch             = 0;
158 static int               opt_cc               = 1;
159 static int               opt_unsafe_floatconv = 0;
160
161 /* instruction set architectures. */
162 static const lc_opt_enum_int_items_t arch_items[] = {
163         { "i386",         cpu_i386 },
164         { "i486",         cpu_i486 },
165         { "i586",         cpu_pentium },
166         { "pentium",      cpu_pentium },
167         { "pentium-mmx",  cpu_pentium_mmx },
168         { "i686",         cpu_pentium_pro },
169         { "pentiumpro",   cpu_pentium_pro },
170         { "pentium2",     cpu_pentium_2 },
171         { "p2",           cpu_pentium_2 },
172         { "pentium3",     cpu_pentium_3 },
173         { "pentium3m",    cpu_pentium_3 },
174         { "p3",           cpu_pentium_3 },
175         { "pentium-m",    cpu_pentium_m },
176         { "pm",           cpu_pentium_m },
177         { "pentium4",     cpu_pentium_4 },
178         { "pentium4m",    cpu_pentium_4 },
179         { "p4",           cpu_pentium_4 },
180         { "prescott",     cpu_prescott },
181         { "nocona",       cpu_nocona },
182         { "merom",        cpu_core2 },
183         { "core2",        cpu_core2 },
184         { "penryn",       cpu_penryn },
185         { "atom",         cpu_atom },
186
187         { "k6",           cpu_k6 },
188         { "k6-2",         cpu_k6_PLUS },
189         { "k6-3",         cpu_k6_PLUS },
190         { "geode",        cpu_geode },
191         { "athlon",       cpu_athlon_old },
192         { "athlon-tbird", cpu_athlon },
193         { "athlon-4",     cpu_athlon },
194         { "athlon-xp",    cpu_athlon },
195         { "athlon-mp",    cpu_athlon },
196         { "athlon64",     cpu_athlon64 },
197         { "k8",           cpu_k8 },
198         { "opteron",      cpu_k8 },
199         { "athlon-fx",    cpu_k8 },
200         { "k8-sse3",      cpu_k8_sse3 },
201         { "opteron-sse3", cpu_k8_sse3 },
202         { "k10",          cpu_k10 },
203         { "barcelona",    cpu_k10 },
204         { "amdfam10",     cpu_k10 },
205
206         { "winchip-c6",   cpu_winchip_c6, },
207         { "winchip2",     cpu_winchip2 },
208         { "c3",           cpu_c3 },
209         { "c3-2",         cpu_c3_2 },
210
211         { "generic",      cpu_generic },
212         { "generic32",    cpu_generic },
213
214 #ifdef NATIVE_X86
215         { "native",       cpu_autodetect },
216 #endif
217
218         { NULL,           0 }
219 };
220
221 static lc_opt_enum_int_var_t arch_var = {
222         (int*) &arch, arch_items
223 };
224
225 static lc_opt_enum_int_var_t opt_arch_var = {
226         (int*) &opt_arch, arch_items
227 };
228
229 static const lc_opt_enum_int_items_t fp_unit_items[] = {
230         { "x87" ,      IA32_FPU_ARCH_X87 },
231         { "sse2",      IA32_FPU_ARCH_SSE2 },
232         { "softfloat", IA32_FPU_ARCH_SOFTFLOAT },
233         { NULL,        IA32_FPU_ARCH_NONE }
234 };
235
236 static lc_opt_enum_int_var_t fp_unit_var = {
237         &fpu_arch, fp_unit_items
238 };
239
240 static const lc_opt_table_entry_t ia32_architecture_options[] = {
241         LC_OPT_ENT_BOOL    ("size",             "optimize for size",                                  &opt_size),
242         LC_OPT_ENT_ENUM_INT("arch",             "select the instruction architecture",                &arch_var),
243         LC_OPT_ENT_ENUM_INT("opt",              "optimize for instruction architecture",              &opt_arch_var),
244         LC_OPT_ENT_ENUM_INT("fpunit",           "select the floating point unit",                     &fp_unit_var),
245         LC_OPT_ENT_BOOL    ("optcc",            "optimize calling convention",                        &opt_cc),
246         LC_OPT_ENT_BOOL    ("unsafe_floatconv", "do unsafe floating point controlword optimisations", &opt_unsafe_floatconv),
247         LC_OPT_ENT_BOOL    ("machcode",         "output machine code instead of assembler",           &emit_machcode),
248         LC_OPT_LAST
249 };
250
251 typedef struct insn_const {
252         int      add_cost;                 /**< cost of an add instruction */
253         int      lea_cost;                 /**< cost of a lea instruction */
254         int      const_shf_cost;           /**< cost of a constant shift instruction */
255         int      cost_mul_start;           /**< starting cost of a multiply instruction */
256         int      cost_mul_bit;             /**< cost of multiply for every set bit */
257         unsigned function_alignment;       /**< logarithm for alignment of function labels */
258         unsigned label_alignment;          /**< logarithm for alignment of loops labels */
259         unsigned label_alignment_max_skip; /**< maximum skip for alignment of loops labels */
260 } insn_const;
261
262 /* costs for optimizing for size */
263 static const insn_const size_cost = {
264         2,   /* cost of an add instruction */
265         3,   /* cost of a lea instruction */
266         3,   /* cost of a constant shift instruction */
267         4,   /* starting cost of a multiply instruction */
268         0,   /* cost of multiply for every set bit */
269         0,   /* logarithm for alignment of function labels */
270         0,   /* logarithm for alignment of loops labels */
271         0,   /* maximum skip for alignment of loops labels */
272 };
273
274 /* costs for the i386 */
275 static const insn_const i386_cost = {
276         1,   /* cost of an add instruction */
277         1,   /* cost of a lea instruction */
278         3,   /* cost of a constant shift instruction */
279         9,   /* starting cost of a multiply instruction */
280         1,   /* cost of multiply for every set bit */
281         2,   /* logarithm for alignment of function labels */
282         2,   /* logarithm for alignment of loops labels */
283         3,   /* maximum skip for alignment of loops labels */
284 };
285
286 /* costs for the i486 */
287 static const insn_const i486_cost = {
288         1,   /* cost of an add instruction */
289         1,   /* cost of a lea instruction */
290         2,   /* cost of a constant shift instruction */
291         12,  /* starting cost of a multiply instruction */
292         1,   /* cost of multiply for every set bit */
293         4,   /* logarithm for alignment of function labels */
294         4,   /* logarithm for alignment of loops labels */
295         15,  /* maximum skip for alignment of loops labels */
296 };
297
298 /* costs for the Pentium */
299 static const insn_const pentium_cost = {
300         1,   /* cost of an add instruction */
301         1,   /* cost of a lea instruction */
302         1,   /* cost of a constant shift instruction */
303         11,  /* starting cost of a multiply instruction */
304         0,   /* cost of multiply for every set bit */
305         4,   /* logarithm for alignment of function labels */
306         4,   /* logarithm for alignment of loops labels */
307         7,   /* maximum skip for alignment of loops labels */
308 };
309
310 /* costs for the Pentium Pro */
311 static const insn_const pentiumpro_cost = {
312         1,   /* cost of an add instruction */
313         1,   /* cost of a lea instruction */
314         1,   /* cost of a constant shift instruction */
315         4,   /* starting cost of a multiply instruction */
316         0,   /* cost of multiply for every set bit */
317         4,   /* logarithm for alignment of function labels */
318         4,   /* logarithm for alignment of loops labels */
319         10,  /* maximum skip for alignment of loops labels */
320 };
321
322 /* costs for the K6 */
323 static const insn_const k6_cost = {
324         1,   /* cost of an add instruction */
325         2,   /* cost of a lea instruction */
326         1,   /* cost of a constant shift instruction */
327         3,   /* starting cost of a multiply instruction */
328         0,   /* cost of multiply for every set bit */
329         5,   /* logarithm for alignment of function labels */
330         5,   /* logarithm for alignment of loops labels */
331         7,   /* maximum skip for alignment of loops labels */
332 };
333
334 /* costs for the Geode */
335 static const insn_const geode_cost = {
336         1,   /* cost of an add instruction */
337         1,   /* cost of a lea instruction */
338         1,   /* cost of a constant shift instruction */
339         7,   /* starting cost of a multiply instruction */
340         0,   /* cost of multiply for every set bit */
341         0,   /* logarithm for alignment of function labels */
342         0,   /* logarithm for alignment of loops labels */
343         0,   /* maximum skip for alignment of loops labels */
344 };
345
346 /* costs for the Athlon */
347 static const insn_const athlon_cost = {
348         1,   /* cost of an add instruction */
349         2,   /* cost of a lea instruction */
350         1,   /* cost of a constant shift instruction */
351         5,   /* starting cost of a multiply instruction */
352         0,   /* cost of multiply for every set bit */
353         4,   /* logarithm for alignment of function labels */
354         4,   /* logarithm for alignment of loops labels */
355         7,   /* maximum skip for alignment of loops labels */
356 };
357
358 /* costs for the Opteron/K8 */
359 static const insn_const k8_cost = {
360         1,   /* cost of an add instruction */
361         2,   /* cost of a lea instruction */
362         1,   /* cost of a constant shift instruction */
363         3,   /* starting cost of a multiply instruction */
364         0,   /* cost of multiply for every set bit */
365         4,   /* logarithm for alignment of function labels */
366         4,   /* logarithm for alignment of loops labels */
367         7,   /* maximum skip for alignment of loops labels */
368 };
369
370 /* costs for the K10 */
371 static const insn_const k10_cost = {
372         1,   /* cost of an add instruction */
373         2,   /* cost of a lea instruction */
374         1,   /* cost of a constant shift instruction */
375         3,   /* starting cost of a multiply instruction */
376         0,   /* cost of multiply for every set bit */
377         5,   /* logarithm for alignment of function labels */
378         5,   /* logarithm for alignment of loops labels */
379         7,   /* maximum skip for alignment of loops labels */
380 };
381
382 /* costs for the Pentium 4 */
383 static const insn_const netburst_cost = {
384         1,   /* cost of an add instruction */
385         3,   /* cost of a lea instruction */
386         4,   /* cost of a constant shift instruction */
387         15,  /* starting cost of a multiply instruction */
388         0,   /* cost of multiply for every set bit */
389         4,   /* logarithm for alignment of function labels */
390         4,   /* logarithm for alignment of loops labels */
391         7,   /* maximum skip for alignment of loops labels */
392 };
393
394 /* costs for the Nocona and Core */
395 static const insn_const nocona_cost = {
396         1,   /* cost of an add instruction */
397         1,   /* cost of a lea instruction */
398         1,   /* cost of a constant shift instruction */
399         10,  /* starting cost of a multiply instruction */
400         0,   /* cost of multiply for every set bit */
401         4,   /* logarithm for alignment of function labels */
402         4,   /* logarithm for alignment of loops labels */
403         7,   /* maximum skip for alignment of loops labels */
404 };
405
406 /* costs for the Core2 */
407 static const insn_const core2_cost = {
408         1,   /* cost of an add instruction */
409         1,   /* cost of a lea instruction */
410         1,   /* cost of a constant shift instruction */
411         3,   /* starting cost of a multiply instruction */
412         0,   /* cost of multiply for every set bit */
413         4,   /* logarithm for alignment of function labels */
414         4,   /* logarithm for alignment of loops labels */
415         10,  /* maximum skip for alignment of loops labels */
416 };
417
418 /* costs for the generic32 */
419 static const insn_const generic32_cost = {
420         1,   /* cost of an add instruction */
421         2,   /* cost of a lea instruction */
422         1,   /* cost of a constant shift instruction */
423         4,   /* starting cost of a multiply instruction */
424         0,   /* cost of multiply for every set bit */
425         4,   /* logarithm for alignment of function labels */
426         4,   /* logarithm for alignment of loops labels */
427         7,   /* maximum skip for alignment of loops labels */
428 };
429
430 static const insn_const *arch_costs = &generic32_cost;
431
432 static void set_arch_costs(void)
433 {
434         if (opt_size) {
435                 arch_costs = &size_cost;
436                 return;
437         }
438         switch (opt_arch & arch_mask) {
439         case arch_i386:      arch_costs = &i386_cost;       break;
440         case arch_i486:      arch_costs = &i486_cost;       break;
441         case arch_pentium:   arch_costs = &pentium_cost;    break;
442         case arch_ppro:      arch_costs = &pentiumpro_cost; break;
443         case arch_netburst:  arch_costs = &netburst_cost;   break;
444         case arch_nocona:    arch_costs = &nocona_cost;     break;
445         case arch_core2:     arch_costs = &core2_cost;      break;
446         case arch_k6:        arch_costs = &k6_cost;         break;
447         case arch_geode:     arch_costs = &geode_cost;      break;
448         case arch_athlon:    arch_costs = &athlon_cost;     break;
449         case arch_k8:        arch_costs = &k8_cost;         break;
450         case arch_k10:       arch_costs = &k10_cost;        break;
451         default:
452         case arch_generic32: arch_costs = &generic32_cost;  break;
453         }
454 }
455
456 /* Evaluate the costs of an instruction. */
457 int ia32_evaluate_insn(insn_kind kind, const ir_mode *mode, ir_tarval *tv)
458 {
459         int cost;
460
461         switch (kind) {
462         case MUL:
463                 cost = arch_costs->cost_mul_start;
464                 if (arch_costs->cost_mul_bit > 0) {
465                         char *bitstr = get_tarval_bitpattern(tv);
466                         int i;
467
468                         for (i = 0; bitstr[i] != '\0'; ++i) {
469                                 if (bitstr[i] == '1') {
470                                         cost += arch_costs->cost_mul_bit;
471                                 }
472                         }
473                         free(bitstr);
474                 }
475                 if (get_mode_size_bits(mode) <= 32)
476                         return cost;
477                 /* 64bit mul supported, approx 4times of a 32bit mul*/
478                 return 4 * cost;
479         case LEA:
480                 /* lea is only supported for 32 bit */
481                 if (get_mode_size_bits(mode) <= 32)
482                         return arch_costs->lea_cost;
483                 /* in 64bit mode, the Lea cost are at wort 2 shifts and one add */
484                 return 2 * arch_costs->add_cost + 2 * (2 * arch_costs->const_shf_cost);
485         case ADD:
486         case SUB:
487                 if (get_mode_size_bits(mode) <= 32)
488                         return arch_costs->add_cost;
489                 /* 64bit add/sub supported, double the cost */
490                 return 2 * arch_costs->add_cost;
491         case SHIFT:
492                 if (get_mode_size_bits(mode) <= 32)
493                         return arch_costs->const_shf_cost;
494                 /* 64bit shift supported, double the cost */
495                 return 2 * arch_costs->const_shf_cost;
496         case ZERO:
497                 return arch_costs->add_cost;
498         default:
499                 return 1;
500         }
501 }
502
503 /* auto detection code only works if we're on an x86 cpu obviously */
504 #ifdef NATIVE_X86
505 typedef struct x86_cpu_info_t {
506         unsigned char cpu_stepping;
507         unsigned char cpu_model;
508         unsigned char cpu_family;
509         unsigned char cpu_type;
510         unsigned char cpu_ext_model;
511         unsigned char cpu_ext_family;
512         unsigned      edx_features;
513         unsigned      ecx_features;
514         unsigned      add_features;
515 } x86_cpu_info_t;
516
517 enum {
518         CPUID_FEAT_ECX_SSE3      = 1 << 0,
519         CPUID_FEAT_ECX_PCLMUL    = 1 << 1,
520         CPUID_FEAT_ECX_DTES64    = 1 << 2,
521         CPUID_FEAT_ECX_MONITOR   = 1 << 3,
522         CPUID_FEAT_ECX_DS_CPL    = 1 << 4,
523         CPUID_FEAT_ECX_VMX       = 1 << 5,
524         CPUID_FEAT_ECX_SMX       = 1 << 6,
525         CPUID_FEAT_ECX_EST       = 1 << 7,
526         CPUID_FEAT_ECX_TM2       = 1 << 8,
527         CPUID_FEAT_ECX_SSSE3     = 1 << 9,
528         CPUID_FEAT_ECX_CID       = 1 << 10,
529         CPUID_FEAT_ECX_FMA       = 1 << 12,
530         CPUID_FEAT_ECX_CX16      = 1 << 13,
531         CPUID_FEAT_ECX_ETPRD     = 1 << 14,
532         CPUID_FEAT_ECX_PDCM      = 1 << 15,
533         CPUID_FEAT_ECX_DCA       = 1 << 18,
534         CPUID_FEAT_ECX_SSE4_1    = 1 << 19,
535         CPUID_FEAT_ECX_SSE4_2    = 1 << 20,
536         CPUID_FEAT_ECX_x2APIC    = 1 << 21,
537         CPUID_FEAT_ECX_MOVBE     = 1 << 22,
538         CPUID_FEAT_ECX_POPCNT    = 1 << 23,
539         CPUID_FEAT_ECX_AES       = 1 << 25,
540         CPUID_FEAT_ECX_XSAVE     = 1 << 26,
541         CPUID_FEAT_ECX_OSXSAVE   = 1 << 27,
542         CPUID_FEAT_ECX_AVX       = 1 << 28,
543
544         CPUID_FEAT_EDX_FPU       = 1 << 0,
545         CPUID_FEAT_EDX_VME       = 1 << 1,
546         CPUID_FEAT_EDX_DE        = 1 << 2,
547         CPUID_FEAT_EDX_PSE       = 1 << 3,
548         CPUID_FEAT_EDX_TSC       = 1 << 4,
549         CPUID_FEAT_EDX_MSR       = 1 << 5,
550         CPUID_FEAT_EDX_PAE       = 1 << 6,
551         CPUID_FEAT_EDX_MCE       = 1 << 7,
552         CPUID_FEAT_EDX_CX8       = 1 << 8,
553         CPUID_FEAT_EDX_APIC      = 1 << 9,
554         CPUID_FEAT_EDX_SEP       = 1 << 11,
555         CPUID_FEAT_EDX_MTRR      = 1 << 12,
556         CPUID_FEAT_EDX_PGE       = 1 << 13,
557         CPUID_FEAT_EDX_MCA       = 1 << 14,
558         CPUID_FEAT_EDX_CMOV      = 1 << 15,
559         CPUID_FEAT_EDX_PAT       = 1 << 16,
560         CPUID_FEAT_EDX_PSE36     = 1 << 17,
561         CPUID_FEAT_EDX_PSN       = 1 << 18,
562         CPUID_FEAT_EDX_CLF       = 1 << 19,
563         CPUID_FEAT_EDX_DTES      = 1 << 21,
564         CPUID_FEAT_EDX_ACPI      = 1 << 22,
565         CPUID_FEAT_EDX_MMX       = 1 << 23,
566         CPUID_FEAT_EDX_FXSR      = 1 << 24,
567         CPUID_FEAT_EDX_SSE       = 1 << 25,
568         CPUID_FEAT_EDX_SSE2      = 1 << 26,
569         CPUID_FEAT_EDX_SS        = 1 << 27,
570         CPUID_FEAT_EDX_HTT       = 1 << 28,
571         CPUID_FEAT_EDX_TM1       = 1 << 29,
572         CPUID_FEAT_EDX_IA64      = 1 << 30,
573         CPUID_FEAT_EDX_PBE       = 1 << 31
574 };
575
576 static cpu_arch_features auto_detect_Intel(x86_cpu_info_t const *info)
577 {
578         cpu_arch_features auto_arch = cpu_generic;
579
580         unsigned family = info->cpu_ext_family + info->cpu_family;
581         unsigned model  = (info->cpu_ext_model << 4) | info->cpu_model;
582
583         switch (family) {
584         case 4:
585                 auto_arch = cpu_i486;
586                 break;
587         case 5:
588                 auto_arch = cpu_pentium;
589                 break;
590         case 6:
591                 switch (model) {
592                 case 0x01: /* PentiumPro */
593                 case 0x03: /* Pentium II Model 3 */
594                 case 0x05: /* Pentium II Model 5 */
595                 case 0x06: /* Celeron Model 6 */
596                 case 0x07: /* Pentium III Model 7 */
597                 case 0x08: /* Pentium III Model 8 */
598                 case 0x09: /* Pentium M Model 9 */
599                 case 0x0A: /* Pentium III Model 0A */
600                 case 0x0B: /* Pentium III Model 0B */
601                 case 0x0D: /* Pentium M Model 0D */
602                 case 0x0E: /* Core Model 0E */
603                         auto_arch = cpu_pentium_pro_generic;
604                         break;
605                 case 0x0F: /* Core2 Model 0F */
606                 case 0x15: /* Intel EP80579 */
607                 case 0x16: /* Celeron Model 16 */
608                 case 0x17: /* Core2 Model 17 */
609                         auto_arch = cpu_core2_generic;
610                         break;
611                 default:
612                         /* unknown */
613                         break;
614                 }
615                 break;
616         case 15:
617                 switch (model) {
618                 case 0x00: /* Pentium 4 Model 00 */
619                 case 0x01: /* Pentium 4 Model 01 */
620                 case 0x02: /* Pentium 4 Model 02 */
621                 case 0x03: /* Pentium 4 Model 03 */
622                 case 0x04: /* Pentium 4 Model 04 */
623                 case 0x06: /* Pentium 4 Model 06 */
624                         auto_arch = cpu_netburst_generic;
625                         break;
626                 case 0x1A: /* Core i7 */
627                         auto_arch = cpu_core2_generic;
628                         break;
629                 case 0x1C: /* Atom */
630                         auto_arch = cpu_atom_generic;
631                         break;
632                 case 0x1D: /* Xeon MP */
633                         auto_arch = cpu_core2_generic;
634                         break;
635                 default:
636                         /* unknown */
637                         break;
638                 }
639                 break;
640         default:
641                 /* unknown */
642                 break;
643         }
644
645         return auto_arch;
646 }
647
648 static cpu_arch_features auto_detect_AMD(x86_cpu_info_t const *info)
649 {
650         cpu_arch_features auto_arch = cpu_generic;
651
652         unsigned family, model;
653
654         if (info->cpu_family == 0x0F) {
655                 family = info->cpu_ext_family + info->cpu_family;
656                 model  = (info->cpu_ext_model << 4) | info->cpu_model;
657         } else {
658                 family = info->cpu_family;
659                 model  = info->cpu_model;
660         }
661
662         switch (family) {
663         case 0x04:
664                 auto_arch = cpu_i486;
665                 break;
666         case 0x05:
667                 switch (model) {
668                 case 0x00: /* K5 Model 0 */
669                 case 0x01: /* K5 Model 1 */
670                 case 0x02: /* K5 Model 2 */
671                 case 0x03: /* K5 Model 3 */
672                         auto_arch = cpu_pentium;
673                         break;
674                 case 0x06: /* K6 Model 6 */
675                 case 0x07: /* K6 Model 7 */
676                 case 0x08: /* K6-2 Model 8 */
677                 case 0x09: /* K6-III Model 9 */
678                 case 0x0D: /* K6-2+ or K6-III+ */
679                         auto_arch = cpu_k6_generic;
680                         break;
681                 case 0x0A: /* Geode LX */
682                         auto_arch = cpu_geode_generic;
683                         break;
684                 default:
685                         /* unknown K6 */
686                         auto_arch = cpu_k6_generic;
687                         break;
688                 }
689                 break;
690         case 0x06:
691                 switch (model) {
692                 case 0x01: /* Athlon Model 1 */
693                 case 0x02: /* Athlon Model 2 */
694                 case 0x03: /* Duron Model 3 */
695                 case 0x04: /* Athlon Model 4 */
696                 case 0x06: /* Athlon MP/Mobile Athlon Model 6 */
697                 case 0x07: /* Mobile Duron Model 7 */
698                 case 0x08: /* Athlon (TH/AP core) including Geode NX */
699                 case 0x0A: /* Athlon (BT core) */
700                 default:   /* unknown K7 */
701                         auto_arch = cpu_athlon_generic;
702                         break;
703                 }
704                 break;
705         case 0x0F:
706                 auto_arch = cpu_k8_generic;
707                 break;
708         case 0x10:
709         case 0x11: /* AMD Family 11h */
710         case 0x12: /* AMD Family 12h */
711         case 0x14: /* AMD Family 14h */
712         case 0x15: /* AMD Family 15h */
713                 auto_arch = cpu_k10_generic;
714                 break;
715         default:
716                 /* unknown */
717                 break;
718         }
719
720         return auto_arch;
721 }
722
723 typedef union {
724         struct {
725         unsigned eax;
726         unsigned ebx;
727         unsigned ecx;
728         unsigned edx;
729         } r;
730         int bulk[4];
731 } cpuid_registers;
732
733 static void x86_cpuid(cpuid_registers *regs, unsigned level)
734 {
735 #if defined(__GNUC__)
736 #       if defined(__PIC__) && !defined(__amd64) // GCC cannot handle EBX in PIC
737         __asm (
738                 "pushl %%ebx\n\t"
739                 "cpuid\n\t"
740                 "movl %%ebx, %1\n\t"
741                 "popl %%ebx"
742         : "=a" (regs->r.eax), "=r" (regs->r.ebx), "=c" (regs->r.ecx), "=d" (regs->r.edx)
743         : "a" (level)
744         );
745 #       else
746         __asm ("cpuid\n\t"
747         : "=a" (regs->r.eax), "=b" (regs->r.ebx), "=c" (regs->r.ecx), "=d" (regs->r.edx)
748         : "a" (level)
749         );
750 #       endif
751 #elif defined(_MSC_VER)
752         __cpuid(regs->bulk, level);
753 #else
754 #       error CPUID is missing
755 #endif
756 }
757
758 static bool x86_toogle_cpuid(void)
759 {
760         unsigned eflags_before = 0;
761         unsigned eflags_after = 0;
762
763 #if defined(__GNUC__)
764 #ifdef __i386__
765         /* If bit 21 of the EFLAGS register can be changed, the cpuid instruction is available */
766         __asm__(
767                 "pushf\n\t"
768                 "popl %0\n\t"
769                 "movl %0, %1\n\t"
770                 "xorl $0x00200000, %1\n\t"
771                 "pushl %1\n\t"
772                 "popf\n\t"
773                 "pushf\n\t"
774                 "popl %1"
775                 : "=r" (eflags_before), "=r" (eflags_after) :: "cc"
776                 );
777 #else
778         eflags_after = 0x00200000;
779 #endif
780 #elif defined(_MSC_VER)
781 #if defined(_M_IX86)
782         __asm {
783                 pushfd
784                 pop eax
785                 mov eflags_before, eax
786                 xor eax, 0x00200000
787                 push eax
788                 popfd
789                 pushfd
790                 pop eax
791                 mov eflags_after, eax
792         }
793 #else
794         eflags_after = 0x00200000;
795 #endif
796 #endif
797         return (eflags_before ^ eflags_after) & 0x00200000;
798 }
799
800 static void autodetect_arch(void)
801 {
802         cpu_arch_features auto_arch = cpu_generic;
803
804         /* We use the cpuid instruction to detect the CPU features */
805         if (x86_toogle_cpuid()) {
806                 cpuid_registers   regs;
807                 char              vendorid[13];
808                 x86_cpu_info_t    cpu_info;
809
810                 /* get vendor ID */
811                 x86_cpuid(&regs, 0);
812                 memcpy(&vendorid[0], &regs.r.ebx, 4);
813                 memcpy(&vendorid[4], &regs.r.edx, 4);
814                 memcpy(&vendorid[8], &regs.r.ecx, 4);
815                 vendorid[12] = '\0';
816
817                 /* get processor info and feature bits */
818                 x86_cpuid(&regs, 1);
819
820                 cpu_info.cpu_stepping   = (regs.r.eax >>  0) & 0x0F;
821                 cpu_info.cpu_model      = (regs.r.eax >>  4) & 0x0F;
822                 cpu_info.cpu_family     = (regs.r.eax >>  8) & 0x0F;
823                 cpu_info.cpu_type       = (regs.r.eax >> 12) & 0x03;
824                 cpu_info.cpu_ext_model  = (regs.r.eax >> 16) & 0x0F;
825                 cpu_info.cpu_ext_family = (regs.r.eax >> 20) & 0xFF;
826                 cpu_info.edx_features   = regs.r.edx;
827                 cpu_info.ecx_features   = regs.r.ecx;
828                 cpu_info.add_features   = regs.r.ebx;
829
830                 if        (0 == strcmp(vendorid, "GenuineIntel")) {
831                         auto_arch = auto_detect_Intel(&cpu_info);
832                 } else if (0 == strcmp(vendorid, "AuthenticAMD")) {
833                         auto_arch = auto_detect_AMD(&cpu_info);
834                 } else if (0 == strcmp(vendorid, "Geode by NSC")) {
835                         auto_arch = cpu_geode_generic;
836                 }
837
838                 if (cpu_info.edx_features & CPUID_FEAT_EDX_CMOV)
839                         auto_arch |= arch_feature_cmov;
840                 if (cpu_info.edx_features & CPUID_FEAT_EDX_MMX)
841                         auto_arch |= arch_feature_mmx;
842                 if (cpu_info.edx_features & CPUID_FEAT_EDX_SSE)
843                         auto_arch |= arch_feature_sse1;
844                 if (cpu_info.edx_features & CPUID_FEAT_EDX_SSE2)
845                         auto_arch |= arch_feature_sse2;
846
847                 if (cpu_info.ecx_features & CPUID_FEAT_ECX_SSE3)
848                         auto_arch |= arch_feature_sse3;
849                 if (cpu_info.ecx_features & CPUID_FEAT_ECX_SSSE3)
850                         auto_arch |= arch_feature_ssse3;
851                 if (cpu_info.ecx_features & CPUID_FEAT_ECX_SSE4_1)
852                         auto_arch |= arch_feature_sse4_1;
853                 if (cpu_info.ecx_features & CPUID_FEAT_ECX_SSE4_2)
854                         auto_arch |= arch_feature_sse4_2;
855                 if (cpu_info.ecx_features & CPUID_FEAT_ECX_POPCNT)
856                         auto_arch |= arch_feature_popcnt;
857         }
858
859         arch     = auto_arch;
860         opt_arch = auto_arch;
861 }
862 #endif  /* NATIVE_X86 */
863
864 static bool flags(cpu_arch_features features, cpu_arch_features flags)
865 {
866         return (features & flags) != 0;
867 }
868
869 void ia32_setup_cg_config(void)
870 {
871         ia32_code_gen_config_t *const c = &ia32_cg_config;
872         memset(c, 0, sizeof(*c));
873
874         set_arch_costs();
875
876 #ifdef NATIVE_X86
877         if (arch == cpu_autodetect)
878                 autodetect_arch();
879 #endif
880
881         c->optimize_size        = opt_size != 0;
882         /* on newer intel cpus mov, pop is often faster than leave although it has a
883          * longer opcode */
884         c->use_leave            = flags(opt_arch, arch_i386 | arch_all_amd | arch_core2) || opt_size;
885         /* P4s don't like inc/decs because they only partially write the flags
886          * register which produces false dependencies */
887         c->use_incdec           = !flags(opt_arch, arch_netburst | arch_nocona | arch_core2 | arch_geode) || opt_size;
888         c->use_softfloat        = (fpu_arch & IA32_FPU_ARCH_SOFTFLOAT) != 0;
889         c->use_sse2             = (fpu_arch & IA32_FPU_ARCH_SSE2) != 0 && flags(arch, arch_feature_sse2);
890         c->use_ffreep           = flags(opt_arch, arch_athlon_plus);
891         c->use_femms            = flags(opt_arch, arch_athlon_plus) &&
892                 flags(arch, arch_feature_mmx | arch_all_amd);
893         c->use_fucomi           = flags(arch, arch_feature_p6_insn);
894         c->use_cmov             = flags(arch, arch_feature_cmov);
895         c->use_modeD_moves      = flags(opt_arch, arch_generic32 | arch_athlon_plus | arch_netburst | arch_nocona | arch_core2 | arch_ppro | arch_geode);
896         c->use_add_esp_4        = flags(opt_arch, arch_generic32 | arch_athlon_plus | arch_netburst | arch_nocona | arch_core2 |             arch_geode)                         && !opt_size;
897         c->use_add_esp_8        = flags(opt_arch, arch_generic32 | arch_athlon_plus | arch_netburst | arch_nocona | arch_core2 | arch_ppro | arch_geode | arch_i386 | arch_i486) && !opt_size;
898         c->use_sub_esp_4        = flags(opt_arch, arch_generic32 | arch_athlon_plus | arch_netburst | arch_nocona | arch_core2 | arch_ppro)                                      && !opt_size;
899         c->use_sub_esp_8        = flags(opt_arch, arch_generic32 | arch_athlon_plus | arch_netburst | arch_nocona | arch_core2 | arch_ppro |              arch_i386 | arch_i486) && !opt_size;
900         c->use_imul_mem_imm32   = !flags(opt_arch, arch_k8 | arch_k10) || opt_size;
901         c->use_pxor             = flags(opt_arch, arch_netburst);
902         c->use_mov_0            = flags(opt_arch, arch_k6) && !opt_size;
903         c->use_short_sex_eax    = !flags(opt_arch, arch_k6) && !opt_size;
904         c->use_pad_return       = flags(opt_arch, arch_athlon_plus) && !opt_size;
905         c->use_bt               = flags(opt_arch, arch_core2 | arch_athlon_plus) || opt_size;
906         c->use_fisttp           = flags(opt_arch & arch, arch_feature_sse3);
907         c->use_sse_prefetch     = flags(arch, (arch_feature_3DNowE | arch_feature_sse1));
908         c->use_3dnow_prefetch   = flags(arch, arch_feature_3DNow);
909         c->use_popcnt           = flags(arch, arch_feature_popcnt);
910         c->use_bswap            = (arch & arch_mask) >= arch_i486;
911         c->optimize_cc          = opt_cc;
912         c->use_unsafe_floatconv = opt_unsafe_floatconv;
913         c->emit_machcode        = emit_machcode;
914
915         c->function_alignment       = arch_costs->function_alignment;
916         c->label_alignment          = arch_costs->label_alignment;
917         c->label_alignment_max_skip = arch_costs->label_alignment_max_skip;
918
919         c->label_alignment_factor =
920                 flags(opt_arch, arch_i386 | arch_i486) || opt_size ? 0 :
921                 opt_arch & arch_all_amd                            ? 3 :
922                 2;
923 }
924
925 void ia32_init_architecture(void)
926 {
927         lc_opt_entry_t *be_grp, *ia32_grp;
928
929         memset(&ia32_cg_config, 0, sizeof(ia32_cg_config));
930
931         be_grp   = lc_opt_get_grp(firm_opt_get_root(), "be");
932         ia32_grp = lc_opt_get_grp(be_grp, "ia32");
933
934         lc_opt_add_table(ia32_grp, ia32_architecture_options);
935 }