2 * Copyright (C) 1995-2008 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"
40 ia32_code_gen_config_t ia32_cg_config;
43 * CPU architectures and features.
45 enum cpu_arch_features {
46 arch_generic32 = 0x00000001, /**< no specific architecture */
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 */
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 */
63 arch_mask = 0x00003FFF,
65 arch_athlon_plus = arch_athlon | arch_k8 | arch_k10,
66 arch_all_amd = arch_k6 | arch_geode | arch_athlon_plus,
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 */
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 */
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 */
95 #define FLAGS(x, f) (((x) & (f)) != 0)
100 typedef enum cpu_support {
101 cpu_generic = arch_generic32,
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,
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,
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! */
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;
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 },
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 },
182 { "opteron", cpu_k8 },
183 { "athlon-fx", cpu_k8 },
184 { "k8-sse3", cpu_k8_sse3 },
185 { "opteron-sse3", cpu_k8_sse3 },
187 { "barcelona", cpu_k10 },
188 { "amdfam10", cpu_k10 },
190 { "winchip-c6", cpu_winchip_c6, },
191 { "winchip2", cpu_winchip2 },
193 { "c3-2", cpu_c3_2 },
195 { "generic", cpu_generic },
196 { "generic32", cpu_generic },
198 { "native", cpu_autodetect },
202 static lc_opt_enum_int_var_t arch_var = {
203 (int*) &arch, arch_items
206 static lc_opt_enum_int_var_t opt_arch_var = {
207 (int*) &opt_arch, arch_items
210 static const lc_opt_enum_int_items_t fp_unit_items[] = {
216 static lc_opt_enum_int_var_t fp_unit_var = {
217 &use_sse2, fp_unit_items
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",
224 LC_OPT_ENT_ENUM_INT("opt", "optimize for instruction architecture",
226 LC_OPT_ENT_ENUM_INT("fpunit", "select the floating point unit",
228 LC_OPT_ENT_NEGBIT("nooptcc", "do not optimize calling convention",
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",
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
422 static const insn_const *arch_costs = &generic32_cost;
424 static void set_arch_costs(void)
427 arch_costs = &size_cost;
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;
444 case arch_generic32: arch_costs = &generic32_cost; break;
448 /* Evaluate the costs of an instruction. */
449 int ia32_evaluate_insn(insn_kind kind, const ir_mode *mode, ir_tarval *tv)
455 cost = arch_costs->cost_mul_start;
456 if (arch_costs->cost_mul_bit > 0) {
457 char *bitstr = get_tarval_bitpattern(tv);
460 for (i = 0; bitstr[i] != '\0'; ++i) {
461 if (bitstr[i] == '1') {
462 cost += arch_costs->cost_mul_bit;
467 if (get_mode_size_bits(mode) <= 32)
469 /* 64bit mul supported, approx 4times of a 32bit mul*/
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);
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;
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;
489 return arch_costs->add_cost;
495 /* autodetection code only works if we're on an x86 cpu obviously */
497 typedef struct cpu_info_t {
498 unsigned char cpu_stepping;
499 unsigned char cpu_model;
500 unsigned char cpu_family;
501 unsigned char cpu_type;
502 unsigned char cpu_ext_model;
503 unsigned char cpu_ext_family;
504 unsigned edx_features;
505 unsigned ecx_features;
506 unsigned add_features;
509 static cpu_support auto_detect_Intel(cpu_info_t const *info)
511 cpu_support auto_arch = cpu_generic;
513 unsigned family = (info->cpu_ext_family << 4) | info->cpu_family;
514 unsigned model = (info->cpu_ext_model << 4) | info->cpu_model;
518 auto_arch = arch_i486;
521 auto_arch = arch_pentium;
525 case 0x01: /* PentiumPro */
526 case 0x03: /* Pentium II Model 3 */
527 case 0x05: /* Pentium II Model 5 */
528 case 0x06: /* Celeron Model 6 */
529 case 0x07: /* Pentium III Model 7 */
530 case 0x08: /* Pentium III Model 8 */
531 case 0x09: /* Pentium M Model 9 */
532 case 0x0A: /* Pentium III Model 0A */
533 case 0x0B: /* Pentium III Model 0B */
534 case 0x0D: /* Pentium M Model 0D */
535 auto_arch = arch_ppro;
537 case 0x0E: /* Core Model 0E */
538 auto_arch = arch_ppro;
540 case 0x0F: /* Core2 Model 0F */
541 case 0x15: /* Intel EP80579 */
542 case 0x16: /* Celeron Model 16 */
543 case 0x17: /* Core2 Model 17 */
544 auto_arch = arch_core2;
553 case 0x00: /* Pentium 4 Model 00 */
554 case 0x01: /* Pentium 4 Model 01 */
555 case 0x02: /* Pentium 4 Model 02 */
556 case 0x03: /* Pentium 4 Model 03 */
557 case 0x04: /* Pentium 4 Model 04 */
558 case 0x06: /* Pentium 4 Model 06 */
559 auto_arch = arch_netburst;
561 case 0x1A: /* Core i7 */
562 auto_arch = arch_core2;
564 case 0x1C: /* Atom */
565 auto_arch = arch_atom;
567 case 0x1D: /* Xeon MP */
568 auto_arch = arch_core2;
580 if (info->edx_features & (1<<23)) auto_arch |= arch_feature_mmx;
581 if (info->edx_features & (1<<25)) auto_arch |= arch_feature_sse1;
582 if (info->edx_features & (1<<26)) auto_arch |= arch_feature_sse2;
584 if (info->ecx_features & (1<< 0)) auto_arch |= arch_feature_sse3;
585 if (info->ecx_features & (1<< 9)) auto_arch |= arch_feature_ssse3;
586 if (info->ecx_features & (1<<19)) auto_arch |= arch_feature_sse4_1;
587 if (info->ecx_features & (1<<20)) auto_arch |= arch_feature_sse4_2;
592 static cpu_support auto_detect_AMD(cpu_info_t const *info) {
593 cpu_support auto_arch = cpu_generic;
595 unsigned family, model;
597 if (info->cpu_family == 0x0F) {
598 family = (info->cpu_ext_family << 4) | info->cpu_family;
599 model = (info->cpu_ext_model << 4) | info->cpu_model;
601 family = info->cpu_family;
602 model = info->cpu_model;
607 auto_arch = arch_i486;
610 case 0x06: // actually, 6 means K7 family
618 auto_arch = arch_k10;
625 if (info->edx_features & (1<<23)) auto_arch |= arch_feature_mmx;
626 if (info->edx_features & (1<<25)) auto_arch |= arch_feature_sse1;
627 if (info->edx_features & (1<<26)) auto_arch |= arch_feature_sse2;
629 if (info->ecx_features & (1<< 0)) auto_arch |= arch_feature_sse3;
630 if (info->ecx_features & (1<< 9)) auto_arch |= arch_feature_ssse3;
631 if (info->ecx_features & (1<<19)) auto_arch |= arch_feature_sse4_1;
632 if (info->ecx_features & (1<<20)) auto_arch |= arch_feature_sse4_2;
647 static void x86_cpuid(cpuid_registers *regs, unsigned level)
649 #if defined(__GNUC__)
650 /* 32bit requires ebx to be saved, and it doesn't hurt on 64 bit */
651 __asm ("pushl %%ebx\n\t"
655 : "=a" (regs->r.eax), "=r" (regs->r.ebx), "=c" (regs->r.ecx), "=d" (regs->r.edx)
658 #elif defined(_MSC_VER)
659 __cpuid(regs->bulk, level);
663 static int x86_toogle_cpuid(void)
665 unsigned eflags_before = 0, eflags_after = 0;
667 #if defined(__GNUC__)
669 /* If bit 21 of the EFLAGS register can be changed, the cpuid instruction is available */
674 "xorl $0x00200000, %1\n\t"
679 : "=r" (eflags_before), "=r" (eflags_after) :: "cc"
682 /* cpuid always available on 64bit */
685 #elif defined(_MSC_VER)
690 mov eflags_before, eax
696 mov eflags_after, eax
702 return (eflags_before ^ eflags_after) & 0x00200000;
705 static void autodetect_arch(void)
707 cpu_support auto_arch = cpu_generic;
709 /* We use the cpuid instruction to detect the CPU features */
710 if (x86_toogle_cpuid()) {
711 cpuid_registers regs;
712 unsigned highest_level;
714 struct cpu_info_t cpu_info;
718 highest_level = regs.r.eax;
719 memcpy(&vendorid[0], ®s.r.ebx, 4);
720 memcpy(&vendorid[4], ®s.r.edx, 4);
721 memcpy(&vendorid[8], ®s.r.ecx, 4);
724 /* get processor info and feature bits */
727 cpu_info.cpu_stepping = (regs.r.eax >> 0) & 0x0F;
728 cpu_info.cpu_model = (regs.r.eax >> 4) & 0x0F;
729 cpu_info.cpu_family = (regs.r.eax >> 8) & 0x0F;
730 cpu_info.cpu_type = (regs.r.eax >> 12) & 0x03;
731 cpu_info.cpu_ext_model = (regs.r.eax >> 16) & 0x0F;
732 cpu_info.cpu_ext_family = (regs.r.eax >> 20) & 0xFF;
733 cpu_info.edx_features = regs.r.edx;
734 cpu_info.ecx_features = regs.r.ecx;
735 cpu_info.add_features = regs.r.ebx;
737 if (0 == strcmp(vendorid, "GenuineIntel")) {
738 auto_arch = auto_detect_Intel(&cpu_info);
739 } else if (0 == strcmp(vendorid, "AuthenticAMD")) {
740 auto_arch = auto_detect_AMD(&cpu_info);
745 opt_arch = auto_arch;
748 static void autodetect_arch(void)
750 panic("architecture autodetection only possible when compiling on target architecture");
754 void ia32_setup_cg_config(void)
756 ia32_code_gen_config_t *const c = &ia32_cg_config;
757 memset(c, 0, sizeof(*c));
764 c->optimize_size = opt_size != 0;
765 /* on newer intel cpus mov, pop is often faster than leave although it has a
767 c->use_leave = FLAGS(opt_arch, arch_i386 | arch_all_amd | arch_core2) || opt_size;
768 /* P4s don't like inc/decs because they only partially write the flags
769 * register which produces false dependencies */
770 c->use_incdec = !FLAGS(opt_arch, arch_netburst | arch_nocona | arch_core2 | arch_geode) || opt_size;
771 c->use_sse2 = use_sse2 && FLAGS(arch, arch_feature_sse2);
772 c->use_ffreep = FLAGS(opt_arch, arch_athlon_plus);
773 c->use_ftst = !FLAGS(arch, arch_feature_p6_insn);
774 /* valgrind can't cope with femms yet and the usefulness of the optimization
775 * is questionable anyway */
777 c->use_femms = FLAGS(opt_arch, arch_athlon_plus) &&
778 FLAGS(arch, arch_feature_mmx | arch_all_amd);
782 c->use_fucomi = FLAGS(arch, arch_feature_p6_insn);
783 c->use_cmov = FLAGS(arch, arch_feature_p6_insn);
784 c->use_modeD_moves = FLAGS(opt_arch, arch_generic32 | arch_athlon_plus | arch_netburst | arch_nocona | arch_core2 | arch_ppro | arch_geode);
785 c->use_add_esp_4 = FLAGS(opt_arch, arch_generic32 | arch_athlon_plus | arch_netburst | arch_nocona | arch_core2 | arch_geode) && !opt_size;
786 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;
787 c->use_sub_esp_4 = FLAGS(opt_arch, arch_generic32 | arch_athlon_plus | arch_netburst | arch_nocona | arch_core2 | arch_ppro) && !opt_size;
788 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;
789 c->use_imul_mem_imm32 = !FLAGS(opt_arch, arch_k8 | arch_k10) || opt_size;
790 c->use_pxor = FLAGS(opt_arch, arch_netburst);
791 c->use_mov_0 = FLAGS(opt_arch, arch_k6) && !opt_size;
792 c->use_short_sex_eax = !FLAGS(opt_arch, arch_k6) && !opt_size;
793 c->use_pad_return = FLAGS(opt_arch, arch_athlon_plus | arch_core2 | arch_generic32) && !opt_size;
794 c->use_bt = FLAGS(opt_arch, arch_core2 | arch_athlon_plus) || opt_size;
795 c->use_fisttp = FLAGS(opt_arch & arch, arch_feature_sse3);
796 c->use_sse_prefetch = FLAGS(arch, (arch_feature_3DNowE | arch_feature_sse1));
797 c->use_3dnow_prefetch = FLAGS(arch, arch_feature_3DNow);
798 c->use_popcnt = FLAGS(arch, (arch_feature_sse4_2 | arch_feature_sse4a));
799 c->use_i486 = (arch & arch_mask) >= arch_i486;
800 c->optimize_cc = opt_cc;
801 c->use_unsafe_floatconv = opt_unsafe_floatconv;
802 c->emit_machcode = emit_machcode;
804 c->function_alignment = arch_costs->function_alignment;
805 c->label_alignment = arch_costs->label_alignment;
806 c->label_alignment_max_skip = arch_costs->label_alignment_max_skip;
808 c->label_alignment_factor =
809 FLAGS(opt_arch, arch_i386 | arch_i486) || opt_size ? 0 :
810 opt_arch & arch_all_amd ? 3 :
814 void ia32_init_architecture(void)
816 lc_opt_entry_t *be_grp, *ia32_grp;
818 memset(&ia32_cg_config, 0, sizeof(ia32_cg_config));
820 be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
821 ia32_grp = lc_opt_get_grp(be_grp, "ia32");
823 lc_opt_add_table(ia32_grp, ia32_architecture_options);