2 * Copyright (C) 1995-2010 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
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 $
29 #include "lc_opts_enum.h"
33 #include "bearch_ia32_t.h"
34 #include "ia32_architecture.h"
39 #if defined(_M_IX86) || defined(_M_X64)
44 #if defined(__i386__) || defined(__x86_64__)
49 ia32_code_gen_config_t ia32_cg_config;
52 * CPU architectures and features.
54 enum cpu_arch_features {
55 arch_generic32 = 0x00000001, /**< no specific architecture */
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 */
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 */
72 arch_mask = 0x00003FFF,
74 arch_athlon_plus = arch_athlon | arch_k8 | arch_k10,
75 arch_all_amd = arch_k6 | arch_geode | arch_athlon_plus,
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 */
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 */
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 */
106 #define FLAGS(x, f) (((x) & (f)) != 0)
111 typedef enum cpu_support {
112 cpu_generic = arch_generic32,
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,
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,
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! */
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 use_sse2 = 0;
154 static int opt_cc = 1;
155 static int opt_unsafe_floatconv = 0;
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 },
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 },
193 { "opteron", cpu_k8 },
194 { "athlon-fx", cpu_k8 },
195 { "k8-sse3", cpu_k8_sse3 },
196 { "opteron-sse3", cpu_k8_sse3 },
198 { "barcelona", cpu_k10 },
199 { "amdfam10", cpu_k10 },
201 { "winchip-c6", cpu_winchip_c6, },
202 { "winchip2", cpu_winchip2 },
204 { "c3-2", cpu_c3_2 },
206 { "generic", cpu_generic },
207 { "generic32", cpu_generic },
210 { "native", cpu_autodetect },
216 static lc_opt_enum_int_var_t arch_var = {
217 (int*) &arch, arch_items
220 static lc_opt_enum_int_var_t opt_arch_var = {
221 (int*) &opt_arch, arch_items
224 static const lc_opt_enum_int_items_t fp_unit_items[] = {
230 static lc_opt_enum_int_var_t fp_unit_var = {
231 &use_sse2, fp_unit_items
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",
238 LC_OPT_ENT_ENUM_INT("opt", "optimize for instruction architecture",
240 LC_OPT_ENT_ENUM_INT("fpunit", "select the floating point unit",
242 LC_OPT_ENT_NEGBIT("nooptcc", "do not optimize calling convention",
244 LC_OPT_ENT_BIT("unsafe_floatconv", "do unsafe floating point controlword "
245 "optimisations", &opt_unsafe_floatconv, 1),
246 LC_OPT_ENT_BOOL("machcode", "output machine code instead of assembler",
251 typedef struct insn_const {
252 int add_cost; /**< cost of an add instruction */
253 int lea_cost; /**< cost of a lea instruction */
254 int const_shf_cost; /**< cost of a constant shift instruction */
255 int cost_mul_start; /**< starting cost of a multiply instruction */
256 int cost_mul_bit; /**< cost of multiply for every set bit */
257 unsigned function_alignment; /**< logarithm for alignment of function labels */
258 unsigned label_alignment; /**< logarithm for alignment of loops labels */
259 unsigned label_alignment_max_skip; /**< maximum skip for alignment of loops labels */
262 /* costs for optimizing for size */
263 static const insn_const size_cost = {
264 2, /* cost of an add instruction */
265 3, /* cost of a lea instruction */
266 3, /* cost of a constant shift instruction */
267 4, /* starting cost of a multiply instruction */
268 0, /* cost of multiply for every set bit */
269 0, /* logarithm for alignment of function labels */
270 0, /* logarithm for alignment of loops labels */
271 0, /* maximum skip for alignment of loops labels */
274 /* costs for the i386 */
275 static const insn_const i386_cost = {
276 1, /* cost of an add instruction */
277 1, /* cost of a lea instruction */
278 3, /* cost of a constant shift instruction */
279 9, /* starting cost of a multiply instruction */
280 1, /* cost of multiply for every set bit */
281 2, /* logarithm for alignment of function labels */
282 2, /* logarithm for alignment of loops labels */
283 3, /* maximum skip for alignment of loops labels */
286 /* costs for the i486 */
287 static const insn_const i486_cost = {
288 1, /* cost of an add instruction */
289 1, /* cost of a lea instruction */
290 2, /* cost of a constant shift instruction */
291 12, /* starting cost of a multiply instruction */
292 1, /* cost of multiply for every set bit */
293 4, /* logarithm for alignment of function labels */
294 4, /* logarithm for alignment of loops labels */
295 15, /* maximum skip for alignment of loops labels */
298 /* costs for the Pentium */
299 static const insn_const pentium_cost = {
300 1, /* cost of an add instruction */
301 1, /* cost of a lea instruction */
302 1, /* cost of a constant shift instruction */
303 11, /* starting cost of a multiply instruction */
304 0, /* cost of multiply for every set bit */
305 4, /* logarithm for alignment of function labels */
306 4, /* logarithm for alignment of loops labels */
307 7, /* maximum skip for alignment of loops labels */
310 /* costs for the Pentium Pro */
311 static const insn_const pentiumpro_cost = {
312 1, /* cost of an add instruction */
313 1, /* cost of a lea instruction */
314 1, /* cost of a constant shift instruction */
315 4, /* starting cost of a multiply instruction */
316 0, /* cost of multiply for every set bit */
317 4, /* logarithm for alignment of function labels */
318 4, /* logarithm for alignment of loops labels */
319 10, /* maximum skip for alignment of loops labels */
322 /* costs for the K6 */
323 static const insn_const k6_cost = {
324 1, /* cost of an add instruction */
325 2, /* cost of a lea instruction */
326 1, /* cost of a constant shift instruction */
327 3, /* starting cost of a multiply instruction */
328 0, /* cost of multiply for every set bit */
329 5, /* logarithm for alignment of function labels */
330 5, /* logarithm for alignment of loops labels */
331 7, /* maximum skip for alignment of loops labels */
334 /* costs for the Geode */
335 static const insn_const geode_cost = {
336 1, /* cost of an add instruction */
337 1, /* cost of a lea instruction */
338 1, /* cost of a constant shift instruction */
339 7, /* starting cost of a multiply instruction */
340 0, /* cost of multiply for every set bit */
341 0, /* logarithm for alignment of function labels */
342 0, /* logarithm for alignment of loops labels */
343 0, /* maximum skip for alignment of loops labels */
346 /* costs for the Athlon */
347 static const insn_const athlon_cost = {
348 1, /* cost of an add instruction */
349 2, /* cost of a lea instruction */
350 1, /* cost of a constant shift instruction */
351 5, /* starting cost of a multiply instruction */
352 0, /* cost of multiply for every set bit */
353 4, /* logarithm for alignment of function labels */
354 4, /* logarithm for alignment of loops labels */
355 7, /* maximum skip for alignment of loops labels */
358 /* costs for the Opteron/K8 */
359 static const insn_const k8_cost = {
360 1, /* cost of an add instruction */
361 2, /* cost of a lea instruction */
362 1, /* cost of a constant shift instruction */
363 3, /* starting cost of a multiply instruction */
364 0, /* cost of multiply for every set bit */
366 4, /* logarithm for alignment of function labels */
367 4, /* logarithm for alignment of loops labels */
368 7, /* maximum skip for alignment of loops labels */
376 /* costs for the K10 */
377 static const insn_const k10_cost = {
378 1, /* cost of an add instruction */
379 2, /* cost of a lea instruction */
380 1, /* cost of a constant shift instruction */
381 3, /* starting cost of a multiply instruction */
382 0, /* cost of multiply for every set bit */
383 5, /* logarithm for alignment of function labels */
384 5, /* logarithm for alignment of loops labels */
385 7, /* maximum skip for alignment of loops labels */
388 /* costs for the Pentium 4 */
389 static const insn_const netburst_cost = {
390 1, /* cost of an add instruction */
391 3, /* cost of a lea instruction */
392 4, /* cost of a constant shift instruction */
393 15, /* starting cost of a multiply instruction */
394 0, /* cost of multiply for every set bit */
395 4, /* logarithm for alignment of function labels */
396 4, /* logarithm for alignment of loops labels */
397 7, /* maximum skip for alignment of loops labels */
400 /* costs for the Nocona and Core */
401 static const insn_const nocona_cost = {
402 1, /* cost of an add instruction */
403 1, /* cost of a lea instruction */
404 1, /* cost of a constant shift instruction */
405 10, /* starting cost of a multiply instruction */
406 0, /* cost of multiply for every set bit */
407 4, /* logarithm for alignment of function labels */
408 4, /* logarithm for alignment of loops labels */
409 7, /* maximum skip for alignment of loops labels */
412 /* costs for the Core2 */
413 static const insn_const core2_cost = {
414 1, /* cost of an add instruction */
415 1, /* cost of a lea instruction */
416 1, /* cost of a constant shift instruction */
417 3, /* starting cost of a multiply instruction */
418 0, /* cost of multiply for every set bit */
419 4, /* logarithm for alignment of function labels */
420 4, /* logarithm for alignment of loops labels */
421 10, /* maximum skip for alignment of loops labels */
424 /* costs for the generic32 */
425 static const insn_const generic32_cost = {
426 1, /* cost of an add instruction */
427 2, /* cost of a lea instruction */
428 1, /* cost of a constant shift instruction */
429 4, /* starting cost of a multiply instruction */
430 0, /* cost of multiply for every set bit */
431 4, /* logarithm for alignment of function labels */
432 4, /* logarithm for alignment of loops labels */
433 7, /* maximum skip for alignment of loops labels */
436 static const insn_const *arch_costs = &generic32_cost;
438 static void set_arch_costs(void)
441 arch_costs = &size_cost;
444 switch (opt_arch & arch_mask) {
445 case arch_i386: arch_costs = &i386_cost; break;
446 case arch_i486: arch_costs = &i486_cost; break;
447 case arch_pentium: arch_costs = &pentium_cost; break;
448 case arch_ppro: arch_costs = &pentiumpro_cost; break;
449 case arch_netburst: arch_costs = &netburst_cost; break;
450 case arch_nocona: arch_costs = &nocona_cost; break;
451 case arch_core2: arch_costs = &core2_cost; break;
452 case arch_k6: arch_costs = &k6_cost; break;
453 case arch_geode: arch_costs = &geode_cost; break;
454 case arch_athlon: arch_costs = &athlon_cost; break;
455 case arch_k8: arch_costs = &k8_cost; break;
456 case arch_k10: arch_costs = &k10_cost; break;
458 case arch_generic32: arch_costs = &generic32_cost; break;
462 /* Evaluate the costs of an instruction. */
463 int ia32_evaluate_insn(insn_kind kind, const ir_mode *mode, ir_tarval *tv)
469 cost = arch_costs->cost_mul_start;
470 if (arch_costs->cost_mul_bit > 0) {
471 char *bitstr = get_tarval_bitpattern(tv);
474 for (i = 0; bitstr[i] != '\0'; ++i) {
475 if (bitstr[i] == '1') {
476 cost += arch_costs->cost_mul_bit;
481 if (get_mode_size_bits(mode) <= 32)
483 /* 64bit mul supported, approx 4times of a 32bit mul*/
486 /* lea is only supported for 32 bit */
487 if (get_mode_size_bits(mode) <= 32)
488 return arch_costs->lea_cost;
489 /* in 64bit mode, the Lea cost are at wort 2 shifts and one add */
490 return 2 * arch_costs->add_cost + 2 * (2 * arch_costs->const_shf_cost);
493 if (get_mode_size_bits(mode) <= 32)
494 return arch_costs->add_cost;
495 /* 64bit add/sub supported, double the cost */
496 return 2 * arch_costs->add_cost;
498 if (get_mode_size_bits(mode) <= 32)
499 return arch_costs->const_shf_cost;
500 /* 64bit shift supported, double the cost */
501 return 2 * arch_costs->const_shf_cost;
503 return arch_costs->add_cost;
509 /* auto detection code only works if we're on an x86 cpu obviously */
511 typedef struct x86_cpu_info_t {
512 unsigned char cpu_stepping;
513 unsigned char cpu_model;
514 unsigned char cpu_family;
515 unsigned char cpu_type;
516 unsigned char cpu_ext_model;
517 unsigned char cpu_ext_family;
518 unsigned edx_features;
519 unsigned ecx_features;
520 unsigned add_features;
524 CPUID_FEAT_ECX_SSE3 = 1 << 0,
525 CPUID_FEAT_ECX_PCLMUL = 1 << 1,
526 CPUID_FEAT_ECX_DTES64 = 1 << 2,
527 CPUID_FEAT_ECX_MONITOR = 1 << 3,
528 CPUID_FEAT_ECX_DS_CPL = 1 << 4,
529 CPUID_FEAT_ECX_VMX = 1 << 5,
530 CPUID_FEAT_ECX_SMX = 1 << 6,
531 CPUID_FEAT_ECX_EST = 1 << 7,
532 CPUID_FEAT_ECX_TM2 = 1 << 8,
533 CPUID_FEAT_ECX_SSSE3 = 1 << 9,
534 CPUID_FEAT_ECX_CID = 1 << 10,
535 CPUID_FEAT_ECX_FMA = 1 << 12,
536 CPUID_FEAT_ECX_CX16 = 1 << 13,
537 CPUID_FEAT_ECX_ETPRD = 1 << 14,
538 CPUID_FEAT_ECX_PDCM = 1 << 15,
539 CPUID_FEAT_ECX_DCA = 1 << 18,
540 CPUID_FEAT_ECX_SSE4_1 = 1 << 19,
541 CPUID_FEAT_ECX_SSE4_2 = 1 << 20,
542 CPUID_FEAT_ECX_x2APIC = 1 << 21,
543 CPUID_FEAT_ECX_MOVBE = 1 << 22,
544 CPUID_FEAT_ECX_POPCNT = 1 << 23,
545 CPUID_FEAT_ECX_AES = 1 << 25,
546 CPUID_FEAT_ECX_XSAVE = 1 << 26,
547 CPUID_FEAT_ECX_OSXSAVE = 1 << 27,
548 CPUID_FEAT_ECX_AVX = 1 << 28,
550 CPUID_FEAT_EDX_FPU = 1 << 0,
551 CPUID_FEAT_EDX_VME = 1 << 1,
552 CPUID_FEAT_EDX_DE = 1 << 2,
553 CPUID_FEAT_EDX_PSE = 1 << 3,
554 CPUID_FEAT_EDX_TSC = 1 << 4,
555 CPUID_FEAT_EDX_MSR = 1 << 5,
556 CPUID_FEAT_EDX_PAE = 1 << 6,
557 CPUID_FEAT_EDX_MCE = 1 << 7,
558 CPUID_FEAT_EDX_CX8 = 1 << 8,
559 CPUID_FEAT_EDX_APIC = 1 << 9,
560 CPUID_FEAT_EDX_SEP = 1 << 11,
561 CPUID_FEAT_EDX_MTRR = 1 << 12,
562 CPUID_FEAT_EDX_PGE = 1 << 13,
563 CPUID_FEAT_EDX_MCA = 1 << 14,
564 CPUID_FEAT_EDX_CMOV = 1 << 15,
565 CPUID_FEAT_EDX_PAT = 1 << 16,
566 CPUID_FEAT_EDX_PSE36 = 1 << 17,
567 CPUID_FEAT_EDX_PSN = 1 << 18,
568 CPUID_FEAT_EDX_CLF = 1 << 19,
569 CPUID_FEAT_EDX_DTES = 1 << 21,
570 CPUID_FEAT_EDX_ACPI = 1 << 22,
571 CPUID_FEAT_EDX_MMX = 1 << 23,
572 CPUID_FEAT_EDX_FXSR = 1 << 24,
573 CPUID_FEAT_EDX_SSE = 1 << 25,
574 CPUID_FEAT_EDX_SSE2 = 1 << 26,
575 CPUID_FEAT_EDX_SS = 1 << 27,
576 CPUID_FEAT_EDX_HTT = 1 << 28,
577 CPUID_FEAT_EDX_TM1 = 1 << 29,
578 CPUID_FEAT_EDX_IA64 = 1 << 30,
579 CPUID_FEAT_EDX_PBE = 1 << 31
582 static cpu_support auto_detect_Intel(x86_cpu_info_t const *info)
584 cpu_support auto_arch = cpu_generic;
586 unsigned family = (info->cpu_ext_family << 4) | info->cpu_family;
587 unsigned model = (info->cpu_ext_model << 4) | info->cpu_model;
591 auto_arch = arch_i486;
594 auto_arch = arch_pentium;
598 case 0x01: /* PentiumPro */
599 case 0x03: /* Pentium II Model 3 */
600 case 0x05: /* Pentium II Model 5 */
601 case 0x06: /* Celeron Model 6 */
602 case 0x07: /* Pentium III Model 7 */
603 case 0x08: /* Pentium III Model 8 */
604 case 0x09: /* Pentium M Model 9 */
605 case 0x0A: /* Pentium III Model 0A */
606 case 0x0B: /* Pentium III Model 0B */
607 case 0x0D: /* Pentium M Model 0D */
608 auto_arch = arch_ppro | arch_feature_p6_insn;
610 case 0x0E: /* Core Model 0E */
611 auto_arch = arch_ppro | arch_feature_p6_insn;
613 case 0x0F: /* Core2 Model 0F */
614 case 0x15: /* Intel EP80579 */
615 case 0x16: /* Celeron Model 16 */
616 case 0x17: /* Core2 Model 17 */
617 auto_arch = arch_core2 | arch_feature_p6_insn;
626 case 0x00: /* Pentium 4 Model 00 */
627 case 0x01: /* Pentium 4 Model 01 */
628 case 0x02: /* Pentium 4 Model 02 */
629 case 0x03: /* Pentium 4 Model 03 */
630 case 0x04: /* Pentium 4 Model 04 */
631 case 0x06: /* Pentium 4 Model 06 */
632 auto_arch = arch_netburst | arch_feature_p6_insn;
634 case 0x1A: /* Core i7 */
635 auto_arch = arch_core2 | arch_feature_p6_insn;
637 case 0x1C: /* Atom */
638 auto_arch = arch_atom;
640 case 0x1D: /* Xeon MP */
641 auto_arch = arch_core2 | arch_feature_p6_insn;
656 static cpu_support auto_detect_AMD(x86_cpu_info_t const *info) {
657 cpu_support auto_arch = cpu_generic;
659 unsigned family, model;
661 if (info->cpu_family == 0x0F) {
662 family = (info->cpu_ext_family << 4) | info->cpu_family;
663 model = (info->cpu_ext_model << 4) | info->cpu_model;
665 family = info->cpu_family;
666 model = info->cpu_model;
671 auto_arch = arch_i486;
675 case 0x00: /* K5 Model 0 */
676 case 0x01: /* K5 Model 1 */
677 case 0x02: /* K5 Model 2 */
678 case 0x03: /* K5 Model 3 */
679 auto_arch = arch_pentium;
681 case 0x06: /* K6 Model 6 */
682 case 0x07: /* K6 Model 7 */
683 case 0x08: /* K6-2 Model 8 */
684 case 0x09: /* K6-III Model 9 */
685 case 0x0D: /* K6-2+ or K6-III+ */
688 case 0x0A: /* Geode LX */
689 auto_arch = arch_geode;
699 case 0x01: /* Athlon Model 1 */
700 case 0x02: /* Athlon Model 2 */
701 case 0x03: /* Duron Model 3 */
702 case 0x04: /* Athlon Model 4 */
703 case 0x06: /* Athlon MP/Mobile Athlon Model 6 */
704 case 0x07: /* Mobile Duron Model 7 */
705 case 0x08: /* Athlon (TH/AP core) including Geode NX */
706 case 0x0A: /* Athlon (BT core) */
707 auto_arch = arch_athlon | arch_feature_p6_insn;
711 auto_arch = arch_athlon | arch_feature_p6_insn;
716 auto_arch = arch_k8 | arch_feature_p6_insn;
719 case 0x2F: /* AMD Family 11h */
720 auto_arch = arch_k10 | arch_feature_p6_insn;
740 static void x86_cpuid(cpuid_registers *regs, unsigned level)
742 #if defined(__GNUC__)
743 # if defined(__PIC__) && !defined(__amd64) // GCC cannot handle EBX in PIC
749 : "=a" (regs->r.eax), "=r" (regs->r.ebx), "=c" (regs->r.ecx), "=d" (regs->r.edx)
754 : "=a" (regs->r.eax), "=b" (regs->r.ebx), "=c" (regs->r.ecx), "=d" (regs->r.edx)
758 #elif defined(_MSC_VER)
759 __cpuid(regs->bulk, level);
761 # error CPUID is missing
765 static int x86_toogle_cpuid(void)
767 unsigned eflags_before = 0, eflags_after = 0;
769 #if defined(__GNUC__)
771 /* If bit 21 of the EFLAGS register can be changed, the cpuid instruction is available */
776 "xorl $0x00200000, %1\n\t"
781 : "=r" (eflags_before), "=r" (eflags_after) :: "cc"
784 /* cpuid always available on 64bit */
787 #elif defined(_MSC_VER)
792 mov eflags_before, eax
798 mov eflags_after, eax
804 return (eflags_before ^ eflags_after) & 0x00200000;
807 static void autodetect_arch(void)
809 cpu_support auto_arch = cpu_generic;
811 /* We use the cpuid instruction to detect the CPU features */
812 if (x86_toogle_cpuid()) {
813 cpuid_registers regs;
814 unsigned highest_level;
816 x86_cpu_info_t cpu_info;
820 highest_level = regs.r.eax;
821 memcpy(&vendorid[0], ®s.r.ebx, 4);
822 memcpy(&vendorid[4], ®s.r.edx, 4);
823 memcpy(&vendorid[8], ®s.r.ecx, 4);
826 /* get processor info and feature bits */
829 cpu_info.cpu_stepping = (regs.r.eax >> 0) & 0x0F;
830 cpu_info.cpu_model = (regs.r.eax >> 4) & 0x0F;
831 cpu_info.cpu_family = (regs.r.eax >> 8) & 0x0F;
832 cpu_info.cpu_type = (regs.r.eax >> 12) & 0x03;
833 cpu_info.cpu_ext_model = (regs.r.eax >> 16) & 0x0F;
834 cpu_info.cpu_ext_family = (regs.r.eax >> 20) & 0xFF;
835 cpu_info.edx_features = regs.r.edx;
836 cpu_info.ecx_features = regs.r.ecx;
837 cpu_info.add_features = regs.r.ebx;
839 if (0 == strcmp(vendorid, "GenuineIntel")) {
840 auto_arch = auto_detect_Intel(&cpu_info);
841 } else if (0 == strcmp(vendorid, "AuthenticAMD")) {
842 auto_arch = auto_detect_AMD(&cpu_info);
843 } else if (0 == strcmp(vendorid, "Geode by NSC")) {
844 auto_arch = arch_geode;
847 if (cpu_info.edx_features & CPUID_FEAT_EDX_CMOV)
848 auto_arch |= arch_feature_cmov;
849 if (cpu_info.edx_features & CPUID_FEAT_EDX_MMX)
850 auto_arch |= arch_feature_mmx;
851 if (cpu_info.edx_features & CPUID_FEAT_EDX_SSE)
852 auto_arch |= arch_feature_sse1;
853 if (cpu_info.edx_features & CPUID_FEAT_EDX_SSE2)
854 auto_arch |= arch_feature_sse2;
856 if (cpu_info.ecx_features & CPUID_FEAT_ECX_SSE3)
857 auto_arch |= arch_feature_sse3;
858 if (cpu_info.ecx_features & CPUID_FEAT_ECX_SSSE3)
859 auto_arch |= arch_feature_ssse3;
860 if (cpu_info.ecx_features & CPUID_FEAT_ECX_SSE4_1)
861 auto_arch |= arch_feature_sse4_1;
862 if (cpu_info.ecx_features & CPUID_FEAT_ECX_SSE4_2)
863 auto_arch |= arch_feature_sse4_2;
864 if (cpu_info.ecx_features & CPUID_FEAT_ECX_POPCNT)
865 auto_arch |= arch_feature_popcnt;
869 opt_arch = auto_arch;
871 #endif /* NATIVE_X86 */
873 void ia32_setup_cg_config(void)
875 ia32_code_gen_config_t *const c = &ia32_cg_config;
876 memset(c, 0, sizeof(*c));
881 if (arch == cpu_autodetect)
885 c->optimize_size = opt_size != 0;
886 /* on newer intel cpus mov, pop is often faster than leave although it has a
888 c->use_leave = FLAGS(opt_arch, arch_i386 | arch_all_amd | arch_core2) || opt_size;
889 /* P4s don't like inc/decs because they only partially write the flags
890 * register which produces false dependencies */
891 c->use_incdec = !FLAGS(opt_arch, arch_netburst | arch_nocona | arch_core2 | arch_geode) || opt_size;
892 c->use_sse2 = use_sse2 && FLAGS(arch, arch_feature_sse2);
893 c->use_ffreep = FLAGS(opt_arch, arch_athlon_plus);
894 c->use_ftst = !FLAGS(arch, arch_feature_p6_insn);
895 /* valgrind can't cope with femms yet and the usefulness of the optimization
896 * is questionable anyway */
898 c->use_femms = FLAGS(opt_arch, arch_athlon_plus) &&
899 FLAGS(arch, arch_feature_mmx | arch_all_amd);
903 c->use_fucomi = FLAGS(arch, arch_feature_p6_insn);
904 c->use_cmov = FLAGS(arch, arch_feature_cmov);
905 c->use_modeD_moves = FLAGS(opt_arch, arch_generic32 | arch_athlon_plus | arch_netburst | arch_nocona | arch_core2 | arch_ppro | arch_geode);
906 c->use_add_esp_4 = FLAGS(opt_arch, arch_generic32 | arch_athlon_plus | arch_netburst | arch_nocona | arch_core2 | arch_geode) && !opt_size;
907 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;
908 c->use_sub_esp_4 = FLAGS(opt_arch, arch_generic32 | arch_athlon_plus | arch_netburst | arch_nocona | arch_core2 | arch_ppro) && !opt_size;
909 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;
910 c->use_imul_mem_imm32 = !FLAGS(opt_arch, arch_k8 | arch_k10) || opt_size;
911 c->use_pxor = FLAGS(opt_arch, arch_netburst);
912 c->use_mov_0 = FLAGS(opt_arch, arch_k6) && !opt_size;
913 c->use_short_sex_eax = !FLAGS(opt_arch, arch_k6) && !opt_size;
914 c->use_pad_return = FLAGS(opt_arch, arch_athlon_plus | arch_core2 | arch_generic32) && !opt_size;
915 c->use_bt = FLAGS(opt_arch, arch_core2 | arch_athlon_plus) || opt_size;
916 c->use_fisttp = FLAGS(opt_arch & arch, arch_feature_sse3);
917 c->use_sse_prefetch = FLAGS(arch, (arch_feature_3DNowE | arch_feature_sse1));
918 c->use_3dnow_prefetch = FLAGS(arch, arch_feature_3DNow);
919 c->use_popcnt = FLAGS(arch, arch_feature_popcnt);
920 c->use_i486 = (arch & arch_mask) >= arch_i486;
921 c->optimize_cc = opt_cc;
922 c->use_unsafe_floatconv = opt_unsafe_floatconv;
923 c->emit_machcode = emit_machcode;
925 c->function_alignment = arch_costs->function_alignment;
926 c->label_alignment = arch_costs->label_alignment;
927 c->label_alignment_max_skip = arch_costs->label_alignment_max_skip;
929 c->label_alignment_factor =
930 FLAGS(opt_arch, arch_i386 | arch_i486) || opt_size ? 0 :
931 opt_arch & arch_all_amd ? 3 :
935 void ia32_init_architecture(void)
937 lc_opt_entry_t *be_grp, *ia32_grp;
939 memset(&ia32_cg_config, 0, sizeof(ia32_cg_config));
941 be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
942 ia32_grp = lc_opt_get_grp(be_grp, "ia32");
944 lc_opt_add_table(ia32_grp, ia32_architecture_options);