be: get rid of arch_irn_classify callback
[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 "lc_opts.h"
28 #include "lc_opts_enum.h"
29
30 #include "irtools.h"
31
32 #include "bearch_ia32_t.h"
33 #include "ia32_architecture.h"
34
35 #undef NATIVE_X86
36
37 #ifdef _MSC_VER
38 #if defined(_M_IX86) || defined(_M_X64)
39 #include <intrin.h>
40 #define NATIVE_X86
41 #endif
42 #else
43 #if defined(__i386__) || defined(__x86_64__)
44 #define NATIVE_X86
45 #endif
46 #endif
47
48 ia32_code_gen_config_t  ia32_cg_config;
49
50 /**
51  * CPU architectures and features.
52  */
53 enum cpu_arch_features {
54         arch_generic32        = 0x00000001, /**< no specific architecture */
55
56         arch_i386             = 0x00000002, /**< i386 architecture */
57         arch_i486             = 0x00000004, /**< i486 architecture */
58         arch_pentium          = 0x00000008, /**< Pentium architecture */
59         arch_ppro             = 0x00000010, /**< PentiumPro architecture */
60         arch_netburst         = 0x00000020, /**< Netburst architecture */
61         arch_nocona           = 0x00000040, /**< Nocona architecture */
62         arch_core2            = 0x00000080, /**< Core2 architecture */
63         arch_atom             = 0x00000100, /**< Atom architecture */
64
65         arch_k6               = 0x00000200, /**< k6 architecture */
66         arch_geode            = 0x00000400, /**< Geode architecture */
67         arch_athlon           = 0x00000800, /**< Athlon architecture */
68         arch_k8               = 0x00001000, /**< K8/Opteron architecture */
69         arch_k10              = 0x00002000, /**< K10/Barcelona architecture */
70
71         arch_mask             = 0x00003FFF,
72
73         arch_athlon_plus      = arch_athlon | arch_k8 | arch_k10,
74         arch_all_amd          = arch_k6 | arch_geode | arch_athlon_plus,
75
76         arch_feature_mmx      = 0x00004000, /**< MMX instructions */
77         arch_feature_cmov     = 0x00008000, /**< cmov instructions */
78         arch_feature_p6_insn  = 0x00010000, /**< PentiumPro instructions */
79         arch_feature_sse1     = 0x00020000, /**< SSE1 instructions */
80         arch_feature_sse2     = 0x00040000, /**< SSE2 instructions */
81         arch_feature_sse3     = 0x00080000, /**< SSE3 instructions */
82         arch_feature_ssse3    = 0x00100000, /**< SSSE3 instructions */
83         arch_feature_3DNow    = 0x00200000, /**< 3DNow! instructions */
84         arch_feature_3DNowE   = 0x00400000, /**< Enhanced 3DNow! instructions */
85         arch_feature_64bit    = 0x00800000, /**< x86_64 support */
86         arch_feature_sse4_1   = 0x01000000, /**< SSE4.1 instructions */
87         arch_feature_sse4_2   = 0x02000000, /**< SSE4.2 instructions */
88         arch_feature_sse4a    = 0x04000000, /**< SSE4a instructions */
89         arch_feature_popcnt   = 0x08000000, /**< popcnt instruction */
90
91         arch_mmx_insn     = arch_feature_mmx,                         /**< MMX instructions */
92         arch_sse1_insn    = arch_feature_sse1   | arch_mmx_insn,      /**< SSE1 instructions, include MMX */
93         arch_sse2_insn    = arch_feature_sse2   | arch_sse1_insn,     /**< SSE2 instructions, include SSE1 */
94         arch_sse3_insn    = arch_feature_sse3   | arch_sse2_insn,     /**< SSE3 instructions, include SSE2 */
95         arch_ssse3_insn   = arch_feature_ssse3  | arch_sse3_insn,     /**< SSSE3 instructions, include SSE3 */
96         arch_sse4_1_insn  = arch_feature_sse4_1 | arch_ssse3_insn,    /**< SSE4.1 instructions, include SSSE3 */
97         arch_sse4_2_insn  = arch_feature_sse4_2 | arch_sse4_1_insn,   /**< SSE4.2 instructions, include SSE4.1 */
98         arch_sse4a_insn   = arch_feature_sse4a  | arch_ssse3_insn,    /**< SSE4a instructions, include SSSE3 */
99
100         arch_3DNow_insn   = arch_feature_3DNow | arch_feature_mmx,    /**< 3DNow! instructions, including MMX */
101         arch_3DNowE_insn  = arch_feature_3DNowE | arch_3DNow_insn,    /**< Enhanced 3DNow! instructions */
102         arch_64bit_insn   = arch_feature_64bit  | arch_sse2_insn,     /**< x86_64 support, includes SSE2 */
103 };
104
105 #define FLAGS(x, f) (((x) & (f)) != 0)
106
107 /**
108  * CPU's.
109  */
110 typedef enum cpu_support {
111         cpu_generic     = arch_generic32,
112
113         /* intel CPUs */
114         cpu_i386        = arch_i386,
115         cpu_i486        = arch_i486,
116         cpu_pentium     = arch_pentium,
117         cpu_pentium_mmx = arch_pentium | arch_mmx_insn,
118         cpu_pentium_pro = arch_ppro | arch_feature_cmov | arch_feature_p6_insn,
119         cpu_pentium_2   = arch_ppro | arch_feature_cmov | arch_feature_p6_insn | arch_mmx_insn,
120         cpu_pentium_3   = arch_ppro | arch_feature_cmov | arch_feature_p6_insn | arch_sse1_insn,
121         cpu_pentium_m   = arch_ppro | arch_feature_cmov | arch_feature_p6_insn | arch_sse2_insn,
122         cpu_pentium_4   = arch_netburst | arch_feature_cmov | arch_feature_p6_insn | arch_sse2_insn,
123         cpu_prescott    = arch_nocona | arch_feature_cmov | arch_feature_p6_insn | arch_sse3_insn,
124         cpu_nocona      = arch_nocona | arch_feature_cmov | arch_feature_p6_insn | arch_64bit_insn | arch_sse3_insn,
125         cpu_core2       = arch_core2 | arch_feature_cmov | arch_feature_p6_insn | arch_64bit_insn | arch_ssse3_insn,
126         cpu_penryn      = arch_core2 | arch_feature_cmov | arch_feature_p6_insn | arch_64bit_insn | arch_sse4_1_insn,
127
128         /* AMD CPUs */
129         cpu_k6          = arch_k6 | arch_mmx_insn,
130         cpu_k6_PLUS     = arch_k6 | arch_3DNow_insn,
131         cpu_geode       = arch_geode  | arch_sse1_insn | arch_3DNowE_insn,
132         cpu_athlon_old  = arch_athlon | arch_3DNowE_insn | arch_feature_cmov | arch_feature_p6_insn,
133         cpu_athlon      = arch_athlon | arch_sse1_insn | arch_3DNowE_insn | arch_feature_cmov | arch_feature_p6_insn,
134         cpu_athlon64    = arch_athlon | arch_sse2_insn | arch_3DNowE_insn | arch_feature_cmov | arch_feature_p6_insn | arch_64bit_insn,
135         cpu_k8          = arch_k8  | arch_3DNowE_insn | arch_feature_cmov | arch_feature_p6_insn | arch_64bit_insn,
136         cpu_k8_sse3     = arch_k8  | arch_3DNowE_insn | arch_feature_cmov | arch_feature_p6_insn | arch_64bit_insn | arch_sse3_insn,
137         cpu_k10         = arch_k10 | arch_3DNowE_insn | arch_feature_cmov | arch_feature_p6_insn | arch_feature_popcnt | arch_64bit_insn | arch_sse4a_insn,
138
139         /* other CPUs */
140         cpu_winchip_c6  = arch_i486 | arch_feature_mmx,
141         cpu_winchip2    = arch_i486 | arch_feature_mmx | arch_feature_3DNow,
142         cpu_c3          = arch_i486 | arch_feature_mmx | arch_feature_3DNow,
143         cpu_c3_2        = arch_ppro | arch_feature_cmov | arch_feature_p6_insn | arch_sse1_insn, /* really no 3DNow! */
144
145         cpu_autodetect  = 0,
146 } cpu_support;
147
148 static int         opt_size             = 0;
149 static int         emit_machcode        = 0;
150 static cpu_support arch                 = cpu_generic;
151 static cpu_support opt_arch             = cpu_generic;
152 static int         fpu_arch             = 0;
153 static int         opt_cc               = 1;
154 static int         opt_unsafe_floatconv = 0;
155
156 /* instruction set architectures. */
157 static const lc_opt_enum_int_items_t arch_items[] = {
158         { "i386",         cpu_i386 },
159         { "i486",         cpu_i486 },
160         { "i586",         cpu_pentium },
161         { "pentium",      cpu_pentium },
162         { "pentium-mmx",  cpu_pentium_mmx },
163         { "i686",         cpu_pentium_pro },
164         { "pentiumpro",   cpu_pentium_pro },
165         { "pentium2",     cpu_pentium_2 },
166         { "p2",           cpu_pentium_2 },
167         { "pentium3",     cpu_pentium_3 },
168         { "pentium3m",    cpu_pentium_3 },
169         { "p3",           cpu_pentium_3 },
170         { "pentium-m",    cpu_pentium_m },
171         { "pm",           cpu_pentium_m },
172         { "pentium4",     cpu_pentium_4 },
173         { "pentium4m",    cpu_pentium_4 },
174         { "p4",           cpu_pentium_4 },
175         { "prescott",     cpu_prescott },
176         { "nocona",       cpu_nocona },
177         { "merom",        cpu_core2 },
178         { "core2",        cpu_core2 },
179         { "penryn",       cpu_penryn },
180
181         { "k6",           cpu_k6 },
182         { "k6-2",         cpu_k6_PLUS },
183         { "k6-3",         cpu_k6_PLUS },
184         { "geode",        cpu_geode },
185         { "athlon",       cpu_athlon_old },
186         { "athlon-tbird", cpu_athlon },
187         { "athlon-4",     cpu_athlon },
188         { "athlon-xp",    cpu_athlon },
189         { "athlon-mp",    cpu_athlon },
190         { "athlon64",     cpu_athlon64 },
191         { "k8",           cpu_k8 },
192         { "opteron",      cpu_k8 },
193         { "athlon-fx",    cpu_k8 },
194         { "k8-sse3",      cpu_k8_sse3 },
195         { "opteron-sse3", cpu_k8_sse3 },
196         { "k10",          cpu_k10 },
197         { "barcelona",    cpu_k10 },
198         { "amdfam10",     cpu_k10 },
199
200         { "winchip-c6",   cpu_winchip_c6, },
201         { "winchip2",     cpu_winchip2 },
202         { "c3",           cpu_c3 },
203         { "c3-2",         cpu_c3_2 },
204
205         { "generic",      cpu_generic },
206         { "generic32",    cpu_generic },
207
208 #ifdef NATIVE_X86
209         { "native",       cpu_autodetect },
210 #endif
211
212         { NULL,           0 }
213 };
214
215 static lc_opt_enum_int_var_t arch_var = {
216         (int*) &arch, arch_items
217 };
218
219 static lc_opt_enum_int_var_t opt_arch_var = {
220         (int*) &opt_arch, arch_items
221 };
222
223 static const lc_opt_enum_int_items_t fp_unit_items[] = {
224         { "x87" ,      IA32_FPU_ARCH_X87 },
225         { "sse2",      IA32_FPU_ARCH_SSE2 },
226         { "softfloat", IA32_FPU_ARCH_SOFTFLOAT },
227         { NULL,        IA32_FPU_ARCH_NONE }
228 };
229
230 static lc_opt_enum_int_var_t fp_unit_var = {
231         &fpu_arch, fp_unit_items
232 };
233
234 static const lc_opt_table_entry_t ia32_architecture_options[] = {
235         LC_OPT_ENT_BOOL    ("size",             "optimize for size",                                  &opt_size),
236         LC_OPT_ENT_ENUM_INT("arch",             "select the instruction architecture",                &arch_var),
237         LC_OPT_ENT_ENUM_INT("opt",              "optimize for instruction architecture",              &opt_arch_var),
238         LC_OPT_ENT_ENUM_INT("fpunit",           "select the floating point unit",                     &fp_unit_var),
239         LC_OPT_ENT_NEGBOOL ("nooptcc",          "do not optimize calling convention",                 &opt_cc),
240         LC_OPT_ENT_BOOL    ("unsafe_floatconv", "do unsafe floating point controlword optimisations", &opt_unsafe_floatconv),
241         LC_OPT_ENT_BOOL    ("machcode",         "output machine code instead of assembler",           &emit_machcode),
242         LC_OPT_LAST
243 };
244
245 typedef struct insn_const {
246         int      add_cost;                 /**< cost of an add instruction */
247         int      lea_cost;                 /**< cost of a lea instruction */
248         int      const_shf_cost;           /**< cost of a constant shift instruction */
249         int      cost_mul_start;           /**< starting cost of a multiply instruction */
250         int      cost_mul_bit;             /**< cost of multiply for every set bit */
251         unsigned function_alignment;       /**< logarithm for alignment of function labels */
252         unsigned label_alignment;          /**< logarithm for alignment of loops labels */
253         unsigned label_alignment_max_skip; /**< maximum skip for alignment of loops labels */
254 } insn_const;
255
256 /* costs for optimizing for size */
257 static const insn_const size_cost = {
258         2,   /* cost of an add instruction */
259         3,   /* cost of a lea instruction */
260         3,   /* cost of a constant shift instruction */
261         4,   /* starting cost of a multiply instruction */
262         0,   /* cost of multiply for every set bit */
263         0,   /* logarithm for alignment of function labels */
264         0,   /* logarithm for alignment of loops labels */
265         0,   /* maximum skip for alignment of loops labels */
266 };
267
268 /* costs for the i386 */
269 static const insn_const i386_cost = {
270         1,   /* cost of an add instruction */
271         1,   /* cost of a lea instruction */
272         3,   /* cost of a constant shift instruction */
273         9,   /* starting cost of a multiply instruction */
274         1,   /* cost of multiply for every set bit */
275         2,   /* logarithm for alignment of function labels */
276         2,   /* logarithm for alignment of loops labels */
277         3,   /* maximum skip for alignment of loops labels */
278 };
279
280 /* costs for the i486 */
281 static const insn_const i486_cost = {
282         1,   /* cost of an add instruction */
283         1,   /* cost of a lea instruction */
284         2,   /* cost of a constant shift instruction */
285         12,  /* starting cost of a multiply instruction */
286         1,   /* cost of multiply for every set bit */
287         4,   /* logarithm for alignment of function labels */
288         4,   /* logarithm for alignment of loops labels */
289         15,  /* maximum skip for alignment of loops labels */
290 };
291
292 /* costs for the Pentium */
293 static const insn_const pentium_cost = {
294         1,   /* cost of an add instruction */
295         1,   /* cost of a lea instruction */
296         1,   /* cost of a constant shift instruction */
297         11,  /* starting cost of a multiply instruction */
298         0,   /* cost of multiply for every set bit */
299         4,   /* logarithm for alignment of function labels */
300         4,   /* logarithm for alignment of loops labels */
301         7,   /* maximum skip for alignment of loops labels */
302 };
303
304 /* costs for the Pentium Pro */
305 static const insn_const pentiumpro_cost = {
306         1,   /* cost of an add instruction */
307         1,   /* cost of a lea instruction */
308         1,   /* cost of a constant shift instruction */
309         4,   /* starting cost of a multiply instruction */
310         0,   /* cost of multiply for every set bit */
311         4,   /* logarithm for alignment of function labels */
312         4,   /* logarithm for alignment of loops labels */
313         10,  /* maximum skip for alignment of loops labels */
314 };
315
316 /* costs for the K6 */
317 static const insn_const k6_cost = {
318         1,   /* cost of an add instruction */
319         2,   /* cost of a lea instruction */
320         1,   /* cost of a constant shift instruction */
321         3,   /* starting cost of a multiply instruction */
322         0,   /* cost of multiply for every set bit */
323         5,   /* logarithm for alignment of function labels */
324         5,   /* logarithm for alignment of loops labels */
325         7,   /* maximum skip for alignment of loops labels */
326 };
327
328 /* costs for the Geode */
329 static const insn_const geode_cost = {
330         1,   /* cost of an add instruction */
331         1,   /* cost of a lea instruction */
332         1,   /* cost of a constant shift instruction */
333         7,   /* starting cost of a multiply instruction */
334         0,   /* cost of multiply for every set bit */
335         0,   /* logarithm for alignment of function labels */
336         0,   /* logarithm for alignment of loops labels */
337         0,   /* maximum skip for alignment of loops labels */
338 };
339
340 /* costs for the Athlon */
341 static const insn_const athlon_cost = {
342         1,   /* cost of an add instruction */
343         2,   /* cost of a lea instruction */
344         1,   /* cost of a constant shift instruction */
345         5,   /* starting cost of a multiply instruction */
346         0,   /* cost of multiply for every set bit */
347         4,   /* logarithm for alignment of function labels */
348         4,   /* logarithm for alignment of loops labels */
349         7,   /* maximum skip for alignment of loops labels */
350 };
351
352 /* costs for the Opteron/K8 */
353 static const insn_const k8_cost = {
354         1,   /* cost of an add instruction */
355         2,   /* cost of a lea instruction */
356         1,   /* cost of a constant shift instruction */
357         3,   /* starting cost of a multiply instruction */
358         0,   /* cost of multiply for every set bit */
359 #if 0 /* TEST */
360         4,   /* logarithm for alignment of function labels */
361         4,   /* logarithm for alignment of loops labels */
362         7,   /* maximum skip for alignment of loops labels */
363 #else
364         0,
365         0,
366         0
367 #endif
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_support auto_detect_Intel(x86_cpu_info_t const *info)
577 {
578         cpu_support auto_arch = cpu_generic;
579
580         unsigned family = (info->cpu_ext_family << 4) | info->cpu_family;
581         unsigned model  = (info->cpu_ext_model  << 4) | info->cpu_model;
582
583         switch (family) {
584         case 4:
585                 auto_arch = arch_i486;
586                 break;
587         case 5:
588                 auto_arch = arch_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                         auto_arch = arch_ppro | arch_feature_p6_insn;
603                         break;
604                 case 0x0E: /* Core Model 0E */
605                         auto_arch = arch_ppro | arch_feature_p6_insn;
606                         break;
607                 case 0x0F: /* Core2 Model 0F */
608                 case 0x15: /* Intel EP80579 */
609                 case 0x16: /* Celeron Model 16 */
610                 case 0x17: /* Core2 Model 17 */
611                         auto_arch = arch_core2 | arch_feature_p6_insn;
612                         break;
613                 default:
614                         /* unknown */
615                         break;
616                 }
617                 break;
618         case 15:
619                 switch (model) {
620                 case 0x00: /* Pentium 4 Model 00 */
621                 case 0x01: /* Pentium 4 Model 01 */
622                 case 0x02: /* Pentium 4 Model 02 */
623                 case 0x03: /* Pentium 4 Model 03 */
624                 case 0x04: /* Pentium 4 Model 04 */
625                 case 0x06: /* Pentium 4 Model 06 */
626                         auto_arch = arch_netburst | arch_feature_p6_insn;
627                         break;
628                 case 0x1A: /* Core i7 */
629                         auto_arch = arch_core2 | arch_feature_p6_insn;
630                         break;
631                 case 0x1C: /* Atom */
632                         auto_arch = arch_atom;
633                         break;
634                 case 0x1D: /* Xeon MP */
635                         auto_arch = arch_core2 | arch_feature_p6_insn;
636                         break;
637                 default:
638                         /* unknown */
639                         break;
640                 }
641                 break;
642         default:
643                 /* unknown */
644                 break;
645         }
646
647         return auto_arch;
648 }
649
650 static cpu_support auto_detect_AMD(x86_cpu_info_t const *info) {
651         cpu_support auto_arch = cpu_generic;
652
653         unsigned family, model;
654
655         if (info->cpu_family == 0x0F) {
656                 family = (info->cpu_ext_family << 4) | info->cpu_family;
657                 model  = (info->cpu_ext_model  << 4) | info->cpu_model;
658         } else {
659                 family = info->cpu_family;
660                 model  = info->cpu_model;
661         }
662
663         switch (family) {
664         case 0x04:
665                 auto_arch = arch_i486;
666                 break;
667         case 0x05:
668                 switch (model) {
669                 case 0x00: /* K5 Model 0 */
670                 case 0x01: /* K5 Model 1 */
671                 case 0x02: /* K5 Model 2 */
672                 case 0x03: /* K5 Model 3 */
673                         auto_arch = arch_pentium;
674                         break;
675                 case 0x06: /* K6 Model 6 */
676                 case 0x07: /* K6 Model 7 */
677                 case 0x08: /* K6-2 Model 8 */
678                 case 0x09: /* K6-III Model 9 */
679                 case 0x0D: /* K6-2+ or K6-III+ */
680                         auto_arch = arch_k6;
681                         break;
682                 case 0x0A: /* Geode LX */
683                         auto_arch = arch_geode;
684                         break;
685                 default:
686                         /* unknown K6 */
687                         auto_arch = arch_k6;
688                         break;
689                 }
690                 break;
691         case 0x06:
692                 switch (model) {
693                 case 0x01: /* Athlon Model 1 */
694                 case 0x02: /* Athlon Model 2 */
695                 case 0x03: /* Duron Model 3 */
696                 case 0x04: /* Athlon Model 4 */
697                 case 0x06: /* Athlon MP/Mobile Athlon Model 6 */
698                 case 0x07: /* Mobile Duron Model 7 */
699                 case 0x08: /* Athlon (TH/AP core) including Geode NX */
700                 case 0x0A: /* Athlon (BT core) */
701                         auto_arch = arch_athlon | arch_feature_p6_insn;
702                         break;
703                 default:
704                         /* unknown K7 */
705                         auto_arch = arch_athlon | arch_feature_p6_insn;
706                         break;
707                 }
708                 break;
709         case 0x0F:
710                 auto_arch = arch_k8 | arch_feature_p6_insn;
711                 break;
712         case 0x1F:
713         case 0x2F: /* AMD Family 11h */
714                 auto_arch = arch_k10 | arch_feature_p6_insn;
715                 break;
716         default:
717                 /* unknown */
718                 break;
719         }
720
721         return auto_arch;
722 }
723
724 typedef union {
725         struct {
726         unsigned eax;
727         unsigned ebx;
728         unsigned ecx;
729         unsigned edx;
730         } r;
731         int bulk[4];
732 } cpuid_registers;
733
734 static void x86_cpuid(cpuid_registers *regs, unsigned level)
735 {
736 #if defined(__GNUC__)
737 #       if defined(__PIC__) && !defined(__amd64) // GCC cannot handle EBX in PIC
738         __asm (
739                 "pushl %%ebx\n\t"
740                 "cpuid\n\t"
741                 "movl %%ebx, %1\n\t"
742                 "popl %%ebx"
743         : "=a" (regs->r.eax), "=r" (regs->r.ebx), "=c" (regs->r.ecx), "=d" (regs->r.edx)
744         : "a" (level)
745         );
746 #       else
747         __asm ("cpuid\n\t"
748         : "=a" (regs->r.eax), "=b" (regs->r.ebx), "=c" (regs->r.ecx), "=d" (regs->r.edx)
749         : "a" (level)
750         );
751 #       endif
752 #elif defined(_MSC_VER)
753         __cpuid(regs->bulk, level);
754 #else
755 #       error CPUID is missing
756 #endif
757 }
758
759 static int x86_toogle_cpuid(void)
760 {
761         unsigned eflags_before = 0, 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         /* cpuid always available on 64bit */
779         return true;
780 #endif
781 #elif defined(_MSC_VER)
782 #if defined(_M_IX86)
783         __asm {
784                 pushfd
785                 pop eax
786                 mov eflags_before, eax
787                 xor eax, 0x00200000
788                 push eax
789                 popfd
790                 pushfd
791                 pop eax
792                 mov eflags_after, eax
793         }
794 #else
795         return true;
796 #endif
797 #endif
798         return (eflags_before ^ eflags_after) & 0x00200000;
799 }
800
801 static void autodetect_arch(void)
802 {
803         cpu_support auto_arch = cpu_generic;
804
805         /* We use the cpuid instruction to detect the CPU features */
806         if (x86_toogle_cpuid()) {
807                 cpuid_registers   regs;
808                 char              vendorid[13];
809                 x86_cpu_info_t    cpu_info;
810
811                 /* get vendor ID */
812                 x86_cpuid(&regs, 0);
813                 memcpy(&vendorid[0], &regs.r.ebx, 4);
814                 memcpy(&vendorid[4], &regs.r.edx, 4);
815                 memcpy(&vendorid[8], &regs.r.ecx, 4);
816                 vendorid[12] = '\0';
817
818                 /* get processor info and feature bits */
819                 x86_cpuid(&regs, 1);
820
821                 cpu_info.cpu_stepping   = (regs.r.eax >>  0) & 0x0F;
822                 cpu_info.cpu_model      = (regs.r.eax >>  4) & 0x0F;
823                 cpu_info.cpu_family     = (regs.r.eax >>  8) & 0x0F;
824                 cpu_info.cpu_type       = (regs.r.eax >> 12) & 0x03;
825                 cpu_info.cpu_ext_model  = (regs.r.eax >> 16) & 0x0F;
826                 cpu_info.cpu_ext_family = (regs.r.eax >> 20) & 0xFF;
827                 cpu_info.edx_features   = regs.r.edx;
828                 cpu_info.ecx_features   = regs.r.ecx;
829                 cpu_info.add_features   = regs.r.ebx;
830
831                 if        (0 == strcmp(vendorid, "GenuineIntel")) {
832                         auto_arch = auto_detect_Intel(&cpu_info);
833                 } else if (0 == strcmp(vendorid, "AuthenticAMD")) {
834                         auto_arch = auto_detect_AMD(&cpu_info);
835                 } else if (0 == strcmp(vendorid, "Geode by NSC")) {
836                         auto_arch = arch_geode;
837                 }
838
839                 if (cpu_info.edx_features & CPUID_FEAT_EDX_CMOV)
840                         auto_arch |= arch_feature_cmov;
841                 if (cpu_info.edx_features & CPUID_FEAT_EDX_MMX)
842                         auto_arch |= arch_feature_mmx;
843                 if (cpu_info.edx_features & CPUID_FEAT_EDX_SSE)
844                         auto_arch |= arch_feature_sse1;
845                 if (cpu_info.edx_features & CPUID_FEAT_EDX_SSE2)
846                         auto_arch |= arch_feature_sse2;
847
848                 if (cpu_info.ecx_features & CPUID_FEAT_ECX_SSE3)
849                         auto_arch |= arch_feature_sse3;
850                 if (cpu_info.ecx_features & CPUID_FEAT_ECX_SSSE3)
851                         auto_arch |= arch_feature_ssse3;
852                 if (cpu_info.ecx_features & CPUID_FEAT_ECX_SSE4_1)
853                         auto_arch |= arch_feature_sse4_1;
854                 if (cpu_info.ecx_features & CPUID_FEAT_ECX_SSE4_2)
855                         auto_arch |= arch_feature_sse4_2;
856                 if (cpu_info.ecx_features & CPUID_FEAT_ECX_POPCNT)
857                         auto_arch |= arch_feature_popcnt;
858         }
859
860         arch     = auto_arch;
861         opt_arch = auto_arch;
862 }
863 #endif  /* NATIVE_X86 */
864
865 void ia32_setup_cg_config(void)
866 {
867         ia32_code_gen_config_t *const c = &ia32_cg_config;
868         memset(c, 0, sizeof(*c));
869
870         set_arch_costs();
871
872 #ifdef NATIVE_X86
873         if (arch == cpu_autodetect)
874                 autodetect_arch();
875 #endif
876
877         c->optimize_size        = opt_size != 0;
878         /* on newer intel cpus mov, pop is often faster than leave although it has a
879          * longer opcode */
880         c->use_leave            = FLAGS(opt_arch, arch_i386 | arch_all_amd | arch_core2) || opt_size;
881         /* P4s don't like inc/decs because they only partially write the flags
882          * register which produces false dependencies */
883         c->use_incdec           = !FLAGS(opt_arch, arch_netburst | arch_nocona | arch_core2 | arch_geode) || opt_size;
884         c->use_softfloat        = FLAGS(fpu_arch, IA32_FPU_ARCH_SOFTFLOAT);
885         c->use_sse2             = FLAGS(fpu_arch, IA32_FPU_ARCH_SSE2) && FLAGS(arch, arch_feature_sse2);
886         c->use_ffreep           = FLAGS(opt_arch, arch_athlon_plus);
887         c->use_ftst             = !FLAGS(arch, arch_feature_p6_insn);
888         /* valgrind can't cope with femms yet and the usefulness of the optimization
889          * is questionable anyway */
890 #if 0
891         c->use_femms            = FLAGS(opt_arch, arch_athlon_plus) &&
892                 FLAGS(arch, arch_feature_mmx | arch_all_amd);
893 #else
894         c->use_femms            = 0;
895 #endif
896         c->use_fucomi           = FLAGS(arch, arch_feature_p6_insn);
897         c->use_cmov             = FLAGS(arch, arch_feature_cmov);
898         c->use_modeD_moves      = FLAGS(opt_arch, arch_generic32 | arch_athlon_plus | arch_netburst | arch_nocona | arch_core2 | arch_ppro | arch_geode);
899         c->use_add_esp_4        = FLAGS(opt_arch, arch_generic32 | arch_athlon_plus | arch_netburst | arch_nocona | arch_core2 |             arch_geode)                         && !opt_size;
900         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;
901         c->use_sub_esp_4        = FLAGS(opt_arch, arch_generic32 | arch_athlon_plus | arch_netburst | arch_nocona | arch_core2 | arch_ppro)                                      && !opt_size;
902         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;
903         c->use_imul_mem_imm32   = !FLAGS(opt_arch, arch_k8 | arch_k10) || opt_size;
904         c->use_pxor             = FLAGS(opt_arch, arch_netburst);
905         c->use_mov_0            = FLAGS(opt_arch, arch_k6) && !opt_size;
906         c->use_short_sex_eax    = !FLAGS(opt_arch, arch_k6) && !opt_size;
907         c->use_pad_return       = FLAGS(opt_arch, arch_athlon_plus) && !opt_size;
908         c->use_bt               = FLAGS(opt_arch, arch_core2 | arch_athlon_plus) || opt_size;
909         c->use_fisttp           = FLAGS(opt_arch & arch, arch_feature_sse3);
910         c->use_sse_prefetch     = FLAGS(arch, (arch_feature_3DNowE | arch_feature_sse1));
911         c->use_3dnow_prefetch   = FLAGS(arch, arch_feature_3DNow);
912         c->use_popcnt           = FLAGS(arch, arch_feature_popcnt);
913         c->use_i486             = (arch & arch_mask) >= arch_i486;
914         c->optimize_cc          = opt_cc;
915         c->use_unsafe_floatconv = opt_unsafe_floatconv;
916         c->emit_machcode        = emit_machcode;
917
918         c->function_alignment       = arch_costs->function_alignment;
919         c->label_alignment          = arch_costs->label_alignment;
920         c->label_alignment_max_skip = arch_costs->label_alignment_max_skip;
921
922         c->label_alignment_factor =
923                 FLAGS(opt_arch, arch_i386 | arch_i486) || opt_size ? 0 :
924                 opt_arch & arch_all_amd                            ? 3 :
925                 2;
926 }
927
928 void ia32_init_architecture(void)
929 {
930         lc_opt_entry_t *be_grp, *ia32_grp;
931
932         memset(&ia32_cg_config, 0, sizeof(ia32_cg_config));
933
934         be_grp   = lc_opt_get_grp(firm_opt_get_root(), "be");
935         ia32_grp = lc_opt_get_grp(be_grp, "ia32");
936
937         lc_opt_add_table(ia32_grp, ia32_architecture_options);
938 }