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
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_generic = arch_ppro | arch_feature_p6_insn,
120 cpu_pentium_pro = arch_ppro | arch_feature_cmov | arch_feature_p6_insn,
121 cpu_pentium_2 = arch_ppro | arch_feature_cmov | arch_feature_p6_insn | arch_mmx_insn,
122 cpu_pentium_3 = arch_ppro | arch_feature_cmov | arch_feature_p6_insn | arch_sse1_insn,
123 cpu_pentium_m = arch_ppro | arch_feature_cmov | arch_feature_p6_insn | arch_sse2_insn,
124 cpu_netburst_generic = arch_netburst | arch_feature_p6_insn,
125 cpu_pentium_4 = arch_netburst | arch_feature_cmov | arch_feature_p6_insn | arch_sse2_insn,
126 cpu_prescott = arch_nocona | arch_feature_cmov | arch_feature_p6_insn | arch_sse3_insn,
127 cpu_nocona = arch_nocona | arch_feature_cmov | arch_feature_p6_insn | arch_64bit_insn | arch_sse3_insn,
128 cpu_core2_generic = arch_core2 | arch_feature_p6_insn,
129 cpu_core2 = arch_core2 | arch_feature_cmov | arch_feature_p6_insn | arch_64bit_insn | arch_ssse3_insn,
130 cpu_penryn = arch_core2 | arch_feature_cmov | arch_feature_p6_insn | arch_64bit_insn | arch_sse4_1_insn,
131 cpu_atom_generic = arch_atom | arch_feature_p6_insn,
132 cpu_atom = arch_atom | arch_feature_cmov | arch_feature_p6_insn | arch_ssse3_insn,
135 cpu_k6_generic = arch_k6,
136 cpu_k6 = arch_k6 | arch_mmx_insn,
137 cpu_k6_PLUS = arch_k6 | arch_3DNow_insn,
138 cpu_geode_generic = arch_geode,
139 cpu_geode = arch_geode | arch_sse1_insn | arch_3DNowE_insn,
140 cpu_athlon_generic = arch_athlon | arch_feature_p6_insn,
141 cpu_athlon_old = arch_athlon | arch_3DNowE_insn | arch_feature_cmov | arch_feature_p6_insn,
142 cpu_athlon = arch_athlon | arch_sse1_insn | arch_3DNowE_insn | arch_feature_cmov | arch_feature_p6_insn,
143 cpu_athlon64 = arch_athlon | arch_sse2_insn | arch_3DNowE_insn | arch_feature_cmov | arch_feature_p6_insn | arch_64bit_insn,
144 cpu_k8_generic = arch_k8 | arch_feature_p6_insn,
145 cpu_k8 = arch_k8 | arch_3DNowE_insn | arch_feature_cmov | arch_feature_p6_insn | arch_64bit_insn,
146 cpu_k8_sse3 = arch_k8 | arch_3DNowE_insn | arch_feature_cmov | arch_feature_p6_insn | arch_64bit_insn | arch_sse3_insn,
147 cpu_k10_generic = arch_k10 | arch_feature_p6_insn,
148 cpu_k10 = arch_k10 | arch_3DNowE_insn | arch_feature_cmov | arch_feature_p6_insn | arch_feature_popcnt | arch_64bit_insn | arch_sse4a_insn,
151 cpu_winchip_c6 = arch_i486 | arch_feature_mmx,
152 cpu_winchip2 = arch_i486 | arch_feature_mmx | arch_feature_3DNow,
153 cpu_c3 = arch_i486 | arch_feature_mmx | arch_feature_3DNow,
154 cpu_c3_2 = arch_ppro | arch_feature_cmov | arch_feature_p6_insn | arch_sse1_insn, /* really no 3DNow! */
159 static int opt_size = 0;
160 static int emit_machcode = 0;
161 static cpu_support arch = cpu_generic;
162 static cpu_support opt_arch = cpu_generic;
163 static int fpu_arch = 0;
164 static int opt_cc = 1;
165 static int opt_unsafe_floatconv = 0;
167 /* instruction set architectures. */
168 static const lc_opt_enum_int_items_t arch_items[] = {
169 { "i386", cpu_i386 },
170 { "i486", cpu_i486 },
171 { "i586", cpu_pentium },
172 { "pentium", cpu_pentium },
173 { "pentium-mmx", cpu_pentium_mmx },
174 { "i686", cpu_pentium_pro },
175 { "pentiumpro", cpu_pentium_pro },
176 { "pentium2", cpu_pentium_2 },
177 { "p2", cpu_pentium_2 },
178 { "pentium3", cpu_pentium_3 },
179 { "pentium3m", cpu_pentium_3 },
180 { "p3", cpu_pentium_3 },
181 { "pentium-m", cpu_pentium_m },
182 { "pm", cpu_pentium_m },
183 { "pentium4", cpu_pentium_4 },
184 { "pentium4m", cpu_pentium_4 },
185 { "p4", cpu_pentium_4 },
186 { "prescott", cpu_prescott },
187 { "nocona", cpu_nocona },
188 { "merom", cpu_core2 },
189 { "core2", cpu_core2 },
190 { "penryn", cpu_penryn },
191 { "atom", cpu_atom },
194 { "k6-2", cpu_k6_PLUS },
195 { "k6-3", cpu_k6_PLUS },
196 { "geode", cpu_geode },
197 { "athlon", cpu_athlon_old },
198 { "athlon-tbird", cpu_athlon },
199 { "athlon-4", cpu_athlon },
200 { "athlon-xp", cpu_athlon },
201 { "athlon-mp", cpu_athlon },
202 { "athlon64", cpu_athlon64 },
204 { "opteron", cpu_k8 },
205 { "athlon-fx", cpu_k8 },
206 { "k8-sse3", cpu_k8_sse3 },
207 { "opteron-sse3", cpu_k8_sse3 },
209 { "barcelona", cpu_k10 },
210 { "amdfam10", cpu_k10 },
212 { "winchip-c6", cpu_winchip_c6, },
213 { "winchip2", cpu_winchip2 },
215 { "c3-2", cpu_c3_2 },
217 { "generic", cpu_generic },
218 { "generic32", cpu_generic },
221 { "native", cpu_autodetect },
227 static lc_opt_enum_int_var_t arch_var = {
228 (int*) &arch, arch_items
231 static lc_opt_enum_int_var_t opt_arch_var = {
232 (int*) &opt_arch, arch_items
235 static const lc_opt_enum_int_items_t fp_unit_items[] = {
236 { "x87" , IA32_FPU_ARCH_X87 },
237 { "sse2", IA32_FPU_ARCH_SSE2 },
238 { "softfloat", IA32_FPU_ARCH_SOFTFLOAT },
239 { NULL, IA32_FPU_ARCH_NONE }
242 static lc_opt_enum_int_var_t fp_unit_var = {
243 &fpu_arch, fp_unit_items
246 static const lc_opt_table_entry_t ia32_architecture_options[] = {
247 LC_OPT_ENT_BOOL ("size", "optimize for size", &opt_size),
248 LC_OPT_ENT_ENUM_INT("arch", "select the instruction architecture", &arch_var),
249 LC_OPT_ENT_ENUM_INT("opt", "optimize for instruction architecture", &opt_arch_var),
250 LC_OPT_ENT_ENUM_INT("fpunit", "select the floating point unit", &fp_unit_var),
251 LC_OPT_ENT_NEGBOOL ("nooptcc", "do not optimize calling convention", &opt_cc),
252 LC_OPT_ENT_BOOL ("unsafe_floatconv", "do unsafe floating point controlword optimisations", &opt_unsafe_floatconv),
253 LC_OPT_ENT_BOOL ("machcode", "output machine code instead of assembler", &emit_machcode),
257 typedef struct insn_const {
258 int add_cost; /**< cost of an add instruction */
259 int lea_cost; /**< cost of a lea instruction */
260 int const_shf_cost; /**< cost of a constant shift instruction */
261 int cost_mul_start; /**< starting cost of a multiply instruction */
262 int cost_mul_bit; /**< cost of multiply for every set bit */
263 unsigned function_alignment; /**< logarithm for alignment of function labels */
264 unsigned label_alignment; /**< logarithm for alignment of loops labels */
265 unsigned label_alignment_max_skip; /**< maximum skip for alignment of loops labels */
268 /* costs for optimizing for size */
269 static const insn_const size_cost = {
270 2, /* cost of an add instruction */
271 3, /* cost of a lea instruction */
272 3, /* cost of a constant shift instruction */
273 4, /* starting cost of a multiply instruction */
274 0, /* cost of multiply for every set bit */
275 0, /* logarithm for alignment of function labels */
276 0, /* logarithm for alignment of loops labels */
277 0, /* maximum skip for alignment of loops labels */
280 /* costs for the i386 */
281 static const insn_const i386_cost = {
282 1, /* cost of an add instruction */
283 1, /* cost of a lea instruction */
284 3, /* cost of a constant shift instruction */
285 9, /* starting cost of a multiply instruction */
286 1, /* cost of multiply for every set bit */
287 2, /* logarithm for alignment of function labels */
288 2, /* logarithm for alignment of loops labels */
289 3, /* maximum skip for alignment of loops labels */
292 /* costs for the i486 */
293 static const insn_const i486_cost = {
294 1, /* cost of an add instruction */
295 1, /* cost of a lea instruction */
296 2, /* cost of a constant shift instruction */
297 12, /* starting cost of a multiply instruction */
298 1, /* cost of multiply for every set bit */
299 4, /* logarithm for alignment of function labels */
300 4, /* logarithm for alignment of loops labels */
301 15, /* maximum skip for alignment of loops labels */
304 /* costs for the Pentium */
305 static const insn_const pentium_cost = {
306 1, /* cost of an add instruction */
307 1, /* cost of a lea instruction */
308 1, /* cost of a constant shift instruction */
309 11, /* starting cost of a multiply instruction */
310 0, /* cost of multiply for every set bit */
311 4, /* logarithm for alignment of function labels */
312 4, /* logarithm for alignment of loops labels */
313 7, /* maximum skip for alignment of loops labels */
316 /* costs for the Pentium Pro */
317 static const insn_const pentiumpro_cost = {
318 1, /* cost of an add instruction */
319 1, /* cost of a lea instruction */
320 1, /* cost of a constant shift instruction */
321 4, /* starting cost of a multiply instruction */
322 0, /* cost of multiply for every set bit */
323 4, /* logarithm for alignment of function labels */
324 4, /* logarithm for alignment of loops labels */
325 10, /* maximum skip for alignment of loops labels */
328 /* costs for the K6 */
329 static const insn_const k6_cost = {
330 1, /* cost of an add instruction */
331 2, /* cost of a lea instruction */
332 1, /* cost of a constant shift instruction */
333 3, /* starting cost of a multiply instruction */
334 0, /* cost of multiply for every set bit */
335 5, /* logarithm for alignment of function labels */
336 5, /* logarithm for alignment of loops labels */
337 7, /* maximum skip for alignment of loops labels */
340 /* costs for the Geode */
341 static const insn_const geode_cost = {
342 1, /* cost of an add instruction */
343 1, /* cost of a lea instruction */
344 1, /* cost of a constant shift instruction */
345 7, /* starting cost of a multiply instruction */
346 0, /* cost of multiply for every set bit */
347 0, /* logarithm for alignment of function labels */
348 0, /* logarithm for alignment of loops labels */
349 0, /* maximum skip for alignment of loops labels */
352 /* costs for the Athlon */
353 static const insn_const athlon_cost = {
354 1, /* cost of an add instruction */
355 2, /* cost of a lea instruction */
356 1, /* cost of a constant shift instruction */
357 5, /* starting cost of a multiply instruction */
358 0, /* cost of multiply for every set bit */
359 4, /* logarithm for alignment of function labels */
360 4, /* logarithm for alignment of loops labels */
361 7, /* maximum skip for alignment of loops labels */
364 /* costs for the Opteron/K8 */
365 static const insn_const k8_cost = {
366 1, /* cost of an add instruction */
367 2, /* cost of a lea instruction */
368 1, /* cost of a constant shift instruction */
369 3, /* starting cost of a multiply instruction */
370 0, /* cost of multiply for every set bit */
372 4, /* logarithm for alignment of function labels */
373 4, /* logarithm for alignment of loops labels */
374 7, /* maximum skip for alignment of loops labels */
382 /* costs for the K10 */
383 static const insn_const k10_cost = {
384 1, /* cost of an add instruction */
385 2, /* cost of a lea instruction */
386 1, /* cost of a constant shift instruction */
387 3, /* starting cost of a multiply instruction */
388 0, /* cost of multiply for every set bit */
389 5, /* logarithm for alignment of function labels */
390 5, /* logarithm for alignment of loops labels */
391 7, /* maximum skip for alignment of loops labels */
394 /* costs for the Pentium 4 */
395 static const insn_const netburst_cost = {
396 1, /* cost of an add instruction */
397 3, /* cost of a lea instruction */
398 4, /* cost of a constant shift instruction */
399 15, /* starting cost of a multiply instruction */
400 0, /* cost of multiply for every set bit */
401 4, /* logarithm for alignment of function labels */
402 4, /* logarithm for alignment of loops labels */
403 7, /* maximum skip for alignment of loops labels */
406 /* costs for the Nocona and Core */
407 static const insn_const nocona_cost = {
408 1, /* cost of an add instruction */
409 1, /* cost of a lea instruction */
410 1, /* cost of a constant shift instruction */
411 10, /* starting cost of a multiply instruction */
412 0, /* cost of multiply for every set bit */
413 4, /* logarithm for alignment of function labels */
414 4, /* logarithm for alignment of loops labels */
415 7, /* maximum skip for alignment of loops labels */
418 /* costs for the Core2 */
419 static const insn_const core2_cost = {
420 1, /* cost of an add instruction */
421 1, /* cost of a lea instruction */
422 1, /* cost of a constant shift instruction */
423 3, /* starting cost of a multiply instruction */
424 0, /* cost of multiply for every set bit */
425 4, /* logarithm for alignment of function labels */
426 4, /* logarithm for alignment of loops labels */
427 10, /* maximum skip for alignment of loops labels */
430 /* costs for the generic32 */
431 static const insn_const generic32_cost = {
432 1, /* cost of an add instruction */
433 2, /* cost of a lea instruction */
434 1, /* cost of a constant shift instruction */
435 4, /* starting cost of a multiply instruction */
436 0, /* cost of multiply for every set bit */
437 4, /* logarithm for alignment of function labels */
438 4, /* logarithm for alignment of loops labels */
439 7, /* maximum skip for alignment of loops labels */
442 static const insn_const *arch_costs = &generic32_cost;
444 static void set_arch_costs(void)
447 arch_costs = &size_cost;
450 switch (opt_arch & arch_mask) {
451 case arch_i386: arch_costs = &i386_cost; break;
452 case arch_i486: arch_costs = &i486_cost; break;
453 case arch_pentium: arch_costs = &pentium_cost; break;
454 case arch_ppro: arch_costs = &pentiumpro_cost; break;
455 case arch_netburst: arch_costs = &netburst_cost; break;
456 case arch_nocona: arch_costs = &nocona_cost; break;
457 case arch_core2: arch_costs = &core2_cost; break;
458 case arch_k6: arch_costs = &k6_cost; break;
459 case arch_geode: arch_costs = &geode_cost; break;
460 case arch_athlon: arch_costs = &athlon_cost; break;
461 case arch_k8: arch_costs = &k8_cost; break;
462 case arch_k10: arch_costs = &k10_cost; break;
464 case arch_generic32: arch_costs = &generic32_cost; break;
468 /* Evaluate the costs of an instruction. */
469 int ia32_evaluate_insn(insn_kind kind, const ir_mode *mode, ir_tarval *tv)
475 cost = arch_costs->cost_mul_start;
476 if (arch_costs->cost_mul_bit > 0) {
477 char *bitstr = get_tarval_bitpattern(tv);
480 for (i = 0; bitstr[i] != '\0'; ++i) {
481 if (bitstr[i] == '1') {
482 cost += arch_costs->cost_mul_bit;
487 if (get_mode_size_bits(mode) <= 32)
489 /* 64bit mul supported, approx 4times of a 32bit mul*/
492 /* lea is only supported for 32 bit */
493 if (get_mode_size_bits(mode) <= 32)
494 return arch_costs->lea_cost;
495 /* in 64bit mode, the Lea cost are at wort 2 shifts and one add */
496 return 2 * arch_costs->add_cost + 2 * (2 * arch_costs->const_shf_cost);
499 if (get_mode_size_bits(mode) <= 32)
500 return arch_costs->add_cost;
501 /* 64bit add/sub supported, double the cost */
502 return 2 * arch_costs->add_cost;
504 if (get_mode_size_bits(mode) <= 32)
505 return arch_costs->const_shf_cost;
506 /* 64bit shift supported, double the cost */
507 return 2 * arch_costs->const_shf_cost;
509 return arch_costs->add_cost;
515 /* auto detection code only works if we're on an x86 cpu obviously */
517 typedef struct x86_cpu_info_t {
518 unsigned char cpu_stepping;
519 unsigned char cpu_model;
520 unsigned char cpu_family;
521 unsigned char cpu_type;
522 unsigned char cpu_ext_model;
523 unsigned char cpu_ext_family;
524 unsigned edx_features;
525 unsigned ecx_features;
526 unsigned add_features;
530 CPUID_FEAT_ECX_SSE3 = 1 << 0,
531 CPUID_FEAT_ECX_PCLMUL = 1 << 1,
532 CPUID_FEAT_ECX_DTES64 = 1 << 2,
533 CPUID_FEAT_ECX_MONITOR = 1 << 3,
534 CPUID_FEAT_ECX_DS_CPL = 1 << 4,
535 CPUID_FEAT_ECX_VMX = 1 << 5,
536 CPUID_FEAT_ECX_SMX = 1 << 6,
537 CPUID_FEAT_ECX_EST = 1 << 7,
538 CPUID_FEAT_ECX_TM2 = 1 << 8,
539 CPUID_FEAT_ECX_SSSE3 = 1 << 9,
540 CPUID_FEAT_ECX_CID = 1 << 10,
541 CPUID_FEAT_ECX_FMA = 1 << 12,
542 CPUID_FEAT_ECX_CX16 = 1 << 13,
543 CPUID_FEAT_ECX_ETPRD = 1 << 14,
544 CPUID_FEAT_ECX_PDCM = 1 << 15,
545 CPUID_FEAT_ECX_DCA = 1 << 18,
546 CPUID_FEAT_ECX_SSE4_1 = 1 << 19,
547 CPUID_FEAT_ECX_SSE4_2 = 1 << 20,
548 CPUID_FEAT_ECX_x2APIC = 1 << 21,
549 CPUID_FEAT_ECX_MOVBE = 1 << 22,
550 CPUID_FEAT_ECX_POPCNT = 1 << 23,
551 CPUID_FEAT_ECX_AES = 1 << 25,
552 CPUID_FEAT_ECX_XSAVE = 1 << 26,
553 CPUID_FEAT_ECX_OSXSAVE = 1 << 27,
554 CPUID_FEAT_ECX_AVX = 1 << 28,
556 CPUID_FEAT_EDX_FPU = 1 << 0,
557 CPUID_FEAT_EDX_VME = 1 << 1,
558 CPUID_FEAT_EDX_DE = 1 << 2,
559 CPUID_FEAT_EDX_PSE = 1 << 3,
560 CPUID_FEAT_EDX_TSC = 1 << 4,
561 CPUID_FEAT_EDX_MSR = 1 << 5,
562 CPUID_FEAT_EDX_PAE = 1 << 6,
563 CPUID_FEAT_EDX_MCE = 1 << 7,
564 CPUID_FEAT_EDX_CX8 = 1 << 8,
565 CPUID_FEAT_EDX_APIC = 1 << 9,
566 CPUID_FEAT_EDX_SEP = 1 << 11,
567 CPUID_FEAT_EDX_MTRR = 1 << 12,
568 CPUID_FEAT_EDX_PGE = 1 << 13,
569 CPUID_FEAT_EDX_MCA = 1 << 14,
570 CPUID_FEAT_EDX_CMOV = 1 << 15,
571 CPUID_FEAT_EDX_PAT = 1 << 16,
572 CPUID_FEAT_EDX_PSE36 = 1 << 17,
573 CPUID_FEAT_EDX_PSN = 1 << 18,
574 CPUID_FEAT_EDX_CLF = 1 << 19,
575 CPUID_FEAT_EDX_DTES = 1 << 21,
576 CPUID_FEAT_EDX_ACPI = 1 << 22,
577 CPUID_FEAT_EDX_MMX = 1 << 23,
578 CPUID_FEAT_EDX_FXSR = 1 << 24,
579 CPUID_FEAT_EDX_SSE = 1 << 25,
580 CPUID_FEAT_EDX_SSE2 = 1 << 26,
581 CPUID_FEAT_EDX_SS = 1 << 27,
582 CPUID_FEAT_EDX_HTT = 1 << 28,
583 CPUID_FEAT_EDX_TM1 = 1 << 29,
584 CPUID_FEAT_EDX_IA64 = 1 << 30,
585 CPUID_FEAT_EDX_PBE = 1 << 31
588 static cpu_support auto_detect_Intel(x86_cpu_info_t const *info)
590 cpu_support auto_arch = cpu_generic;
592 unsigned family = (info->cpu_ext_family << 4) | info->cpu_family;
593 unsigned model = (info->cpu_ext_model << 4) | info->cpu_model;
597 auto_arch = cpu_i486;
600 auto_arch = cpu_pentium;
604 case 0x01: /* PentiumPro */
605 case 0x03: /* Pentium II Model 3 */
606 case 0x05: /* Pentium II Model 5 */
607 case 0x06: /* Celeron Model 6 */
608 case 0x07: /* Pentium III Model 7 */
609 case 0x08: /* Pentium III Model 8 */
610 case 0x09: /* Pentium M Model 9 */
611 case 0x0A: /* Pentium III Model 0A */
612 case 0x0B: /* Pentium III Model 0B */
613 case 0x0D: /* Pentium M Model 0D */
614 case 0x0E: /* Core Model 0E */
615 auto_arch = cpu_pentium_pro_generic;
617 case 0x0F: /* Core2 Model 0F */
618 case 0x15: /* Intel EP80579 */
619 case 0x16: /* Celeron Model 16 */
620 case 0x17: /* Core2 Model 17 */
621 auto_arch = cpu_core2_generic;
630 case 0x00: /* Pentium 4 Model 00 */
631 case 0x01: /* Pentium 4 Model 01 */
632 case 0x02: /* Pentium 4 Model 02 */
633 case 0x03: /* Pentium 4 Model 03 */
634 case 0x04: /* Pentium 4 Model 04 */
635 case 0x06: /* Pentium 4 Model 06 */
636 auto_arch = cpu_netburst_generic;
638 case 0x1A: /* Core i7 */
639 auto_arch = cpu_core2_generic;
641 case 0x1C: /* Atom */
642 auto_arch = cpu_atom_generic;
644 case 0x1D: /* Xeon MP */
645 auto_arch = cpu_core2_generic;
660 static cpu_support auto_detect_AMD(x86_cpu_info_t const *info) {
661 cpu_support auto_arch = cpu_generic;
663 unsigned family, model;
665 if (info->cpu_family == 0x0F) {
666 family = (info->cpu_ext_family << 4) | info->cpu_family;
667 model = (info->cpu_ext_model << 4) | info->cpu_model;
669 family = info->cpu_family;
670 model = info->cpu_model;
675 auto_arch = cpu_i486;
679 case 0x00: /* K5 Model 0 */
680 case 0x01: /* K5 Model 1 */
681 case 0x02: /* K5 Model 2 */
682 case 0x03: /* K5 Model 3 */
683 auto_arch = cpu_pentium;
685 case 0x06: /* K6 Model 6 */
686 case 0x07: /* K6 Model 7 */
687 case 0x08: /* K6-2 Model 8 */
688 case 0x09: /* K6-III Model 9 */
689 case 0x0D: /* K6-2+ or K6-III+ */
690 auto_arch = cpu_k6_generic;
692 case 0x0A: /* Geode LX */
693 auto_arch = cpu_geode_generic;
697 auto_arch = cpu_k6_generic;
703 case 0x01: /* Athlon Model 1 */
704 case 0x02: /* Athlon Model 2 */
705 case 0x03: /* Duron Model 3 */
706 case 0x04: /* Athlon Model 4 */
707 case 0x06: /* Athlon MP/Mobile Athlon Model 6 */
708 case 0x07: /* Mobile Duron Model 7 */
709 case 0x08: /* Athlon (TH/AP core) including Geode NX */
710 case 0x0A: /* Athlon (BT core) */
711 default: /* unknown K7 */
712 auto_arch = cpu_athlon_generic;
717 auto_arch = cpu_k8_generic;
720 case 0x2F: /* AMD Family 11h */
721 auto_arch = cpu_k10_generic;
741 static void x86_cpuid(cpuid_registers *regs, unsigned level)
743 #if defined(__GNUC__)
744 # if defined(__PIC__) && !defined(__amd64) // GCC cannot handle EBX in PIC
750 : "=a" (regs->r.eax), "=r" (regs->r.ebx), "=c" (regs->r.ecx), "=d" (regs->r.edx)
755 : "=a" (regs->r.eax), "=b" (regs->r.ebx), "=c" (regs->r.ecx), "=d" (regs->r.edx)
759 #elif defined(_MSC_VER)
760 __cpuid(regs->bulk, level);
762 # error CPUID is missing
766 static bool x86_toogle_cpuid(void)
768 unsigned eflags_before = 0;
769 unsigned eflags_after = 0;
771 #if defined(__GNUC__)
773 /* If bit 21 of the EFLAGS register can be changed, the cpuid instruction is available */
778 "xorl $0x00200000, %1\n\t"
783 : "=r" (eflags_before), "=r" (eflags_after) :: "cc"
786 eflags_after = 0x00200000;
788 #elif defined(_MSC_VER)
793 mov eflags_before, eax
799 mov eflags_after, eax
802 eflags_after = 0x00200000;
805 return (eflags_before ^ eflags_after) & 0x00200000;
808 static void autodetect_arch(void)
810 cpu_support auto_arch = cpu_generic;
812 /* We use the cpuid instruction to detect the CPU features */
813 if (x86_toogle_cpuid()) {
814 cpuid_registers regs;
816 x86_cpu_info_t cpu_info;
820 memcpy(&vendorid[0], ®s.r.ebx, 4);
821 memcpy(&vendorid[4], ®s.r.edx, 4);
822 memcpy(&vendorid[8], ®s.r.ecx, 4);
825 /* get processor info and feature bits */
828 cpu_info.cpu_stepping = (regs.r.eax >> 0) & 0x0F;
829 cpu_info.cpu_model = (regs.r.eax >> 4) & 0x0F;
830 cpu_info.cpu_family = (regs.r.eax >> 8) & 0x0F;
831 cpu_info.cpu_type = (regs.r.eax >> 12) & 0x03;
832 cpu_info.cpu_ext_model = (regs.r.eax >> 16) & 0x0F;
833 cpu_info.cpu_ext_family = (regs.r.eax >> 20) & 0xFF;
834 cpu_info.edx_features = regs.r.edx;
835 cpu_info.ecx_features = regs.r.ecx;
836 cpu_info.add_features = regs.r.ebx;
838 if (0 == strcmp(vendorid, "GenuineIntel")) {
839 auto_arch = auto_detect_Intel(&cpu_info);
840 } else if (0 == strcmp(vendorid, "AuthenticAMD")) {
841 auto_arch = auto_detect_AMD(&cpu_info);
842 } else if (0 == strcmp(vendorid, "Geode by NSC")) {
843 auto_arch = cpu_geode_generic;
846 if (cpu_info.edx_features & CPUID_FEAT_EDX_CMOV)
847 auto_arch |= arch_feature_cmov;
848 if (cpu_info.edx_features & CPUID_FEAT_EDX_MMX)
849 auto_arch |= arch_feature_mmx;
850 if (cpu_info.edx_features & CPUID_FEAT_EDX_SSE)
851 auto_arch |= arch_feature_sse1;
852 if (cpu_info.edx_features & CPUID_FEAT_EDX_SSE2)
853 auto_arch |= arch_feature_sse2;
855 if (cpu_info.ecx_features & CPUID_FEAT_ECX_SSE3)
856 auto_arch |= arch_feature_sse3;
857 if (cpu_info.ecx_features & CPUID_FEAT_ECX_SSSE3)
858 auto_arch |= arch_feature_ssse3;
859 if (cpu_info.ecx_features & CPUID_FEAT_ECX_SSE4_1)
860 auto_arch |= arch_feature_sse4_1;
861 if (cpu_info.ecx_features & CPUID_FEAT_ECX_SSE4_2)
862 auto_arch |= arch_feature_sse4_2;
863 if (cpu_info.ecx_features & CPUID_FEAT_ECX_POPCNT)
864 auto_arch |= arch_feature_popcnt;
868 opt_arch = auto_arch;
870 #endif /* NATIVE_X86 */
872 void ia32_setup_cg_config(void)
874 ia32_code_gen_config_t *const c = &ia32_cg_config;
875 memset(c, 0, sizeof(*c));
880 if (arch == cpu_autodetect)
884 c->optimize_size = opt_size != 0;
885 /* on newer intel cpus mov, pop is often faster than leave although it has a
887 c->use_leave = FLAGS(opt_arch, arch_i386 | arch_all_amd | arch_core2) || opt_size;
888 /* P4s don't like inc/decs because they only partially write the flags
889 * register which produces false dependencies */
890 c->use_incdec = !FLAGS(opt_arch, arch_netburst | arch_nocona | arch_core2 | arch_geode) || opt_size;
891 c->use_softfloat = FLAGS(fpu_arch, IA32_FPU_ARCH_SOFTFLOAT);
892 c->use_sse2 = FLAGS(fpu_arch, IA32_FPU_ARCH_SSE2) && FLAGS(arch, arch_feature_sse2);
893 c->use_ffreep = FLAGS(opt_arch, arch_athlon_plus);
894 /* valgrind can't cope with femms yet and the usefulness of the optimization
895 * is questionable anyway */
897 c->use_femms = FLAGS(opt_arch, arch_athlon_plus) &&
898 FLAGS(arch, arch_feature_mmx | arch_all_amd);
902 c->use_fucomi = FLAGS(arch, arch_feature_p6_insn);
903 c->use_cmov = FLAGS(arch, arch_feature_cmov);
904 c->use_modeD_moves = FLAGS(opt_arch, arch_generic32 | arch_athlon_plus | arch_netburst | arch_nocona | arch_core2 | arch_ppro | arch_geode);
905 c->use_add_esp_4 = FLAGS(opt_arch, arch_generic32 | arch_athlon_plus | arch_netburst | arch_nocona | arch_core2 | arch_geode) && !opt_size;
906 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;
907 c->use_sub_esp_4 = FLAGS(opt_arch, arch_generic32 | arch_athlon_plus | arch_netburst | arch_nocona | arch_core2 | arch_ppro) && !opt_size;
908 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;
909 c->use_imul_mem_imm32 = !FLAGS(opt_arch, arch_k8 | arch_k10) || opt_size;
910 c->use_pxor = FLAGS(opt_arch, arch_netburst);
911 c->use_mov_0 = FLAGS(opt_arch, arch_k6) && !opt_size;
912 c->use_short_sex_eax = !FLAGS(opt_arch, arch_k6) && !opt_size;
913 c->use_pad_return = FLAGS(opt_arch, arch_athlon_plus) && !opt_size;
914 c->use_bt = FLAGS(opt_arch, arch_core2 | arch_athlon_plus) || opt_size;
915 c->use_fisttp = FLAGS(opt_arch & arch, arch_feature_sse3);
916 c->use_sse_prefetch = FLAGS(arch, (arch_feature_3DNowE | arch_feature_sse1));
917 c->use_3dnow_prefetch = FLAGS(arch, arch_feature_3DNow);
918 c->use_popcnt = FLAGS(arch, arch_feature_popcnt);
919 c->use_bswap = (arch & arch_mask) >= arch_i486;
920 c->optimize_cc = opt_cc;
921 c->use_unsafe_floatconv = opt_unsafe_floatconv;
922 c->emit_machcode = emit_machcode;
924 c->function_alignment = arch_costs->function_alignment;
925 c->label_alignment = arch_costs->label_alignment;
926 c->label_alignment_max_skip = arch_costs->label_alignment_max_skip;
928 c->label_alignment_factor =
929 FLAGS(opt_arch, arch_i386 | arch_i486) || opt_size ? 0 :
930 opt_arch & arch_all_amd ? 3 :
934 void ia32_init_architecture(void)
936 lc_opt_entry_t *be_grp, *ia32_grp;
938 memset(&ia32_cg_config, 0, sizeof(ia32_cg_config));
940 be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
941 ia32_grp = lc_opt_get_grp(be_grp, "ia32");
943 lc_opt_add_table(ia32_grp, ia32_architecture_options);