1 /* Subroutines for the gcc driver.
2 Copyright (C) 2006, 2007 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to
18 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
23 #include "coretypes.h"
27 const char *host_detect_local_cpu (int argc, const char **argv);
30 #define cpuid(num,a,b,c,d) \
31 asm volatile ("xchgl %%ebx, %1; cpuid; xchgl %%ebx, %1" \
32 : "=a" (a), "=r" (b), "=c" (c), "=d" (d) \
35 #define bit_CMPXCHG8B (1 << 8)
36 #define bit_CMOV (1 << 15)
37 #define bit_MMX (1 << 23)
38 #define bit_SSE (1 << 25)
39 #define bit_SSE2 (1 << 26)
41 #define bit_SSE3 (1 << 0)
42 #define bit_SSSE3 (1 << 9)
43 #define bit_SSE4a (1 << 6)
44 #define bit_CMPXCHG16B (1 << 13)
46 #define bit_3DNOW (1 << 31)
47 #define bit_3DNOWP (1 << 30)
48 #define bit_LM (1 << 29)
50 /* This will be called by the spec parser in gcc.c when it sees
51 a %:local_cpu_detect(args) construct. Currently it will be called
52 with either "arch" or "tune" as argument depending on if -march=native
53 or -mtune=native is to be substituted.
55 It returns a string containing new command line parameters to be
56 put at the place of the above two options, depending on what CPU
57 this is executed. E.g. "-march=k8" on an AMD64 machine
60 ARGC and ARGV are set depending on the actual arguments given
62 const char *host_detect_local_cpu (int argc, const char **argv)
64 const char *cpu = NULL;
65 enum processor_type processor = PROCESSOR_I386;
66 unsigned int eax, ebx, ecx, edx;
67 unsigned int max_level;
69 unsigned int ext_level;
70 unsigned char has_mmx = 0, has_3dnow = 0, has_3dnowp = 0, has_sse = 0;
71 unsigned char has_sse2 = 0, has_sse3 = 0, has_ssse3 = 0, has_cmov = 0;
72 unsigned char has_longmode = 0, has_cmpxchg8b = 0, has_sse4a = 0;
73 unsigned char is_amd = 0;
74 unsigned int family = 0;
80 arch = strcmp (argv[0], "arch") == 0;
81 if (!arch && strcmp (argv[0], "tune"))
85 /* See if we can use cpuid. */
86 asm volatile ("pushfl; pushfl; popl %0; movl %0,%1; xorl %2,%0;"
87 "pushl %0; popfl; pushfl; popl %0; popfl"
88 : "=&r" (eax), "=&r" (ebx)
91 if (((eax ^ ebx) & 0x00200000) == 0)
95 processor = PROCESSOR_PENTIUM;
97 /* Check the highest input value for eax. */
98 cpuid (0, eax, ebx, ecx, edx);
100 /* We only look at the first four characters. */
105 cpuid (1, eax, ebx, ecx, edx);
106 has_cmpxchg8b = !!(edx & bit_CMPXCHG8B);
107 has_cmov = !!(edx & bit_CMOV);
108 has_mmx = !!(edx & bit_MMX);
109 has_sse = !!(edx & bit_SSE);
110 has_sse2 = !!(edx & bit_SSE2);
111 has_sse3 = !!(ecx & bit_SSE3);
112 has_ssse3 = !!(ecx & bit_SSSE3);
113 /* We don't care for extended family. */
114 family = (eax >> 8) & ~(1 << 4);
116 cpuid (0x80000000, eax, ebx, ecx, edx);
118 if (ext_level >= 0x80000000)
120 cpuid (0x80000001, eax, ebx, ecx, edx);
121 has_3dnow = !!(edx & bit_3DNOW);
122 has_3dnowp = !!(edx & bit_3DNOWP);
123 has_longmode = !!(edx & bit_LM);
124 has_sse4a = !!(ecx & bit_SSE4a);
127 is_amd = vendor == *(unsigned int*)"Auth";
132 processor = PROCESSOR_K6;
134 processor = PROCESSOR_ATHLON;
135 if (has_sse2 || has_longmode)
136 processor = PROCESSOR_K8;
138 processor = PROCESSOR_AMDFAM10;
145 /* Default is PROCESSOR_PENTIUM. */
148 processor = PROCESSOR_PENTIUMPRO;
151 processor = PROCESSOR_PENTIUM4;
154 /* We have no idea. Use something reasonable. */
172 else if (has_cmpxchg8b)
192 case PROCESSOR_PENTIUM:
198 case PROCESSOR_PENTIUMPRO:
205 /* It is Core 2 Duo. */
210 /* It is Core Duo. */
216 /* It is Pentium M. */
221 /* It is Pentium III. */
226 /* It is Pentium II. */
231 /* Default to Pentium Pro. */
237 /* For -mtune, we default to -mtune=generic. */
241 case PROCESSOR_GEODE:
250 case PROCESSOR_ATHLON:
256 case PROCESSOR_PENTIUM4:
270 case PROCESSOR_NOCONA:
273 case PROCESSOR_AMDFAM10:
276 case PROCESSOR_GENERIC32:
277 case PROCESSOR_GENERIC64:
286 return concat ("-m", argv[0], "=", cpu, NULL);
289 /* If we aren't compiling with GCC we just provide a minimal
291 const char *host_detect_local_cpu (int argc, const char **argv)
299 arch = strcmp (argv[0], "arch") == 0;
300 if (!arch && strcmp (argv[0], "tune"))
305 /* FIXME: i386 is wrong for 64bit compiler. How can we tell if
306 we are generating 64bit or 32bit code? */
312 return concat ("-m", argv[0], "=", cpu, NULL);
314 #endif /* GCC_VERSION */