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