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