1 /* Get CPU type and Features for x86 processors.
2 Copyright (C) 2012-2013 Free Software Foundation, Inc.
3 Contributed by Sriraman Tallam (tmsriram@google.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "auto-target.h"
25 #ifdef HAVE_INIT_PRIORITY
26 #define CONSTRUCTOR_PRIORITY (101)
28 #define CONSTRUCTOR_PRIORITY
31 int __cpu_indicator_init (void)
32 __attribute__ ((constructor CONSTRUCTOR_PRIORITY
));
34 enum vendor_signatures
36 SIG_INTEL
= 0x756e6547 /* Genu */,
37 SIG_AMD
= 0x68747541 /* Auth */
40 /* Processor Vendor and Models. */
50 /* Any new types or subtypes have to be inserted at the end. */
63 enum processor_subtypes
65 INTEL_COREI7_NEHALEM
= 1,
66 INTEL_COREI7_WESTMERE
,
67 INTEL_COREI7_SANDYBRIDGE
,
76 /* ISA Features supported. */
78 enum processor_features
93 struct __processor_model
95 unsigned int __cpu_vendor
;
96 unsigned int __cpu_type
;
97 unsigned int __cpu_subtype
;
98 unsigned int __cpu_features
[1];
102 /* Get the specific type of AMD CPU. */
105 get_amd_cpu (unsigned int family
, unsigned int model
)
109 /* AMD Family 10h. */
115 __cpu_model
.__cpu_type
= AMDFAM10H
;
116 __cpu_model
.__cpu_subtype
= AMDFAM10H_BARCELONA
;
120 __cpu_model
.__cpu_type
= AMDFAM10H
;
121 __cpu_model
.__cpu_subtype
= AMDFAM10H_SHANGHAI
;
125 __cpu_model
.__cpu_type
= AMDFAM10H
;
126 __cpu_model
.__cpu_subtype
= AMDFAM10H_ISTANBUL
;
132 /* AMD Family 15h. */
134 __cpu_model
.__cpu_type
= AMDFAM15H
;
135 /* Bulldozer version 1. */
137 __cpu_model
.__cpu_subtype
= AMDFAM15H_BDVER1
;
138 /* Bulldozer version 2. */
139 if (model
>= 0x10 && model
<= 0x1f)
140 __cpu_model
.__cpu_subtype
= AMDFAM15H_BDVER2
;
147 /* Get the specific type of Intel CPU. */
150 get_intel_cpu (unsigned int family
, unsigned int model
, unsigned int brand_id
)
152 /* Parse family and model only if brand ID is 0. */
166 __cpu_model
.__cpu_type
= INTEL_ATOM
;
173 __cpu_model
.__cpu_type
= INTEL_COREI7
;
174 __cpu_model
.__cpu_subtype
= INTEL_COREI7_NEHALEM
;
180 __cpu_model
.__cpu_type
= INTEL_COREI7
;
181 __cpu_model
.__cpu_subtype
= INTEL_COREI7_WESTMERE
;
186 __cpu_model
.__cpu_type
= INTEL_COREI7
;
187 __cpu_model
.__cpu_subtype
= INTEL_COREI7_SANDYBRIDGE
;
194 __cpu_model
.__cpu_type
= INTEL_CORE2
;
201 /* We have no idea. */
207 /* ECX and EDX are output of CPUID at level one. MAX_CPUID_LEVEL is
208 the max possible level of CPUID insn. */
210 get_available_features (unsigned int ecx
, unsigned int edx
,
213 unsigned int features
= 0;
216 features
|= (1 << FEATURE_CMOV
);
218 features
|= (1 << FEATURE_MMX
);
220 features
|= (1 << FEATURE_SSE
);
222 features
|= (1 << FEATURE_SSE2
);
223 if (ecx
& bit_POPCNT
)
224 features
|= (1 << FEATURE_POPCNT
);
226 features
|= (1 << FEATURE_SSE3
);
228 features
|= (1 << FEATURE_SSSE3
);
229 if (ecx
& bit_SSE4_1
)
230 features
|= (1 << FEATURE_SSE4_1
);
231 if (ecx
& bit_SSE4_2
)
232 features
|= (1 << FEATURE_SSE4_2
);
234 features
|= (1 << FEATURE_AVX
);
236 /* Get Advanced Features at level 7 (eax = 7, ecx = 0). */
237 if (max_cpuid_level
>= 7)
239 unsigned int eax
, ebx
, ecx
, edx
;
240 __cpuid_count (7, 0, eax
, ebx
, ecx
, edx
);
242 features
|= (1 << FEATURE_AVX2
);
245 __cpu_model
.__cpu_features
[0] = features
;
248 /* A noinline function calling __get_cpuid. Having many calls to
249 cpuid in one function in 32-bit mode causes GCC to complain:
250 "can't find a register in class CLOBBERED_REGS". This is
251 related to PR rtl-optimization 44174. */
253 static int __attribute__ ((noinline
))
254 __get_cpuid_output (unsigned int __level
,
255 unsigned int *__eax
, unsigned int *__ebx
,
256 unsigned int *__ecx
, unsigned int *__edx
)
258 return __get_cpuid (__level
, __eax
, __ebx
, __ecx
, __edx
);
262 /* A constructor function that is sets __cpu_model and __cpu_features with
263 the right values. This needs to run only once. This constructor is
264 given the highest priority and it should run before constructors without
265 the priority set. However, it still runs after ifunc initializers and
266 needs to be called explicitly there. */
268 int __attribute__ ((constructor CONSTRUCTOR_PRIORITY
))
269 __cpu_indicator_init (void)
271 unsigned int eax
, ebx
, ecx
, edx
;
275 unsigned int model
, family
, brand_id
;
276 unsigned int extended_model
, extended_family
;
278 /* This function needs to run just once. */
279 if (__cpu_model
.__cpu_vendor
)
282 /* Assume cpuid insn present. Run in level 0 to get vendor id. */
283 if (!__get_cpuid_output (0, &eax
, &ebx
, &ecx
, &edx
))
285 __cpu_model
.__cpu_vendor
= VENDOR_OTHER
;
294 __cpu_model
.__cpu_vendor
= VENDOR_OTHER
;
298 if (!__get_cpuid_output (1, &eax
, &ebx
, &ecx
, &edx
))
300 __cpu_model
.__cpu_vendor
= VENDOR_OTHER
;
304 model
= (eax
>> 4) & 0x0f;
305 family
= (eax
>> 8) & 0x0f;
306 brand_id
= ebx
& 0xff;
307 extended_model
= (eax
>> 12) & 0xf0;
308 extended_family
= (eax
>> 20) & 0xff;
310 if (vendor
== SIG_INTEL
)
312 /* Adjust model and family for Intel CPUS. */
315 family
+= extended_family
;
316 model
+= extended_model
;
318 else if (family
== 0x06)
319 model
+= extended_model
;
322 get_intel_cpu (family
, model
, brand_id
);
323 /* Find available features. */
324 get_available_features (ecx
, edx
, max_level
);
325 __cpu_model
.__cpu_vendor
= VENDOR_INTEL
;
327 else if (vendor
== SIG_AMD
)
329 /* Adjust model and family for AMD CPUS. */
332 family
+= extended_family
;
333 model
+= (extended_model
<< 4);
337 get_amd_cpu (family
, model
);
338 /* Find available features. */
339 get_available_features (ecx
, edx
, max_level
);
340 __cpu_model
.__cpu_vendor
= VENDOR_AMD
;
343 __cpu_model
.__cpu_vendor
= VENDOR_OTHER
;
345 gcc_assert (__cpu_model
.__cpu_vendor
< VENDOR_MAX
);
346 gcc_assert (__cpu_model
.__cpu_type
< CPU_TYPE_MAX
);
347 gcc_assert (__cpu_model
.__cpu_subtype
< CPU_SUBTYPE_MAX
);