1 /* Get CPU type and Features for x86 processors.
2 Copyright (C) 2012-2015 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 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
28 #include "auto-target.h"
30 #ifdef HAVE_INIT_PRIORITY
31 #define CONSTRUCTOR_PRIORITY (101)
33 #define CONSTRUCTOR_PRIORITY
36 int __cpu_indicator_init (void)
37 __attribute__ ((constructor CONSTRUCTOR_PRIORITY
));
39 /* Processor Vendor and Models. */
49 /* Any new types or subtypes have to be inserted at the end. */
65 enum processor_subtypes
67 INTEL_COREI7_NEHALEM
= 1,
68 INTEL_COREI7_WESTMERE
,
69 INTEL_COREI7_SANDYBRIDGE
,
77 INTEL_COREI7_IVYBRIDGE
,
79 INTEL_COREI7_BROADWELL
,
83 /* ISA Features supported. */
85 enum processor_features
109 struct __processor_model
111 unsigned int __cpu_vendor
;
112 unsigned int __cpu_type
;
113 unsigned int __cpu_subtype
;
114 unsigned int __cpu_features
[1];
118 /* Get the specific type of AMD CPU. */
121 get_amd_cpu (unsigned int family
, unsigned int model
)
125 /* AMD Family 10h. */
127 __cpu_model
.__cpu_type
= AMDFAM10H
;
132 __cpu_model
.__cpu_subtype
= AMDFAM10H_BARCELONA
;
136 __cpu_model
.__cpu_subtype
= AMDFAM10H_SHANGHAI
;
140 __cpu_model
.__cpu_subtype
= AMDFAM10H_ISTANBUL
;
146 /* AMD Family 14h "btver1". */
148 __cpu_model
.__cpu_type
= AMD_BTVER1
;
150 /* AMD Family 15h "Bulldozer". */
152 __cpu_model
.__cpu_type
= AMDFAM15H
;
153 /* Bulldozer version 1. */
155 __cpu_model
.__cpu_subtype
= AMDFAM15H_BDVER1
;
156 /* Bulldozer version 2 "Piledriver" */
157 if (model
>= 0x10 && model
<= 0x2f)
158 __cpu_model
.__cpu_subtype
= AMDFAM15H_BDVER2
;
159 /* Bulldozer version 3 "Steamroller" */
160 if (model
>= 0x30 && model
<= 0x4f)
161 __cpu_model
.__cpu_subtype
= AMDFAM15H_BDVER3
;
163 /* AMD Family 16h "btver2" */
165 __cpu_model
.__cpu_type
= AMD_BTVER2
;
172 /* Get the specific type of Intel CPU. */
175 get_intel_cpu (unsigned int family
, unsigned int model
, unsigned int brand_id
)
177 /* Parse family and model only if brand ID is 0. */
191 __cpu_model
.__cpu_type
= INTEL_BONNELL
;
199 __cpu_model
.__cpu_type
= INTEL_SILVERMONT
;
202 /* Knights Landing. */
203 __cpu_model
.__cpu_type
= INTEL_KNL
;
210 __cpu_model
.__cpu_type
= INTEL_COREI7
;
211 __cpu_model
.__cpu_subtype
= INTEL_COREI7_NEHALEM
;
217 __cpu_model
.__cpu_type
= INTEL_COREI7
;
218 __cpu_model
.__cpu_subtype
= INTEL_COREI7_WESTMERE
;
223 __cpu_model
.__cpu_type
= INTEL_COREI7
;
224 __cpu_model
.__cpu_subtype
= INTEL_COREI7_SANDYBRIDGE
;
229 __cpu_model
.__cpu_type
= INTEL_COREI7
;
230 __cpu_model
.__cpu_subtype
= INTEL_COREI7_IVYBRIDGE
;
237 __cpu_model
.__cpu_type
= INTEL_COREI7
;
238 __cpu_model
.__cpu_subtype
= INTEL_COREI7_HASWELL
;
245 __cpu_model
.__cpu_type
= INTEL_COREI7
;
246 __cpu_model
.__cpu_subtype
= INTEL_COREI7_BROADWELL
;
253 __cpu_model
.__cpu_type
= INTEL_CORE2
;
260 /* We have no idea. */
266 /* ECX and EDX are output of CPUID at level one. MAX_CPUID_LEVEL is
267 the max possible level of CPUID insn. */
269 get_available_features (unsigned int ecx
, unsigned int edx
,
272 unsigned int features
= 0;
275 features
|= (1 << FEATURE_CMOV
);
277 features
|= (1 << FEATURE_MMX
);
279 features
|= (1 << FEATURE_SSE
);
281 features
|= (1 << FEATURE_SSE2
);
282 if (ecx
& bit_POPCNT
)
283 features
|= (1 << FEATURE_POPCNT
);
285 features
|= (1 << FEATURE_AES
);
286 if (ecx
& bit_PCLMUL
)
287 features
|= (1 << FEATURE_PCLMUL
);
289 features
|= (1 << FEATURE_SSE3
);
291 features
|= (1 << FEATURE_SSSE3
);
292 if (ecx
& bit_SSE4_1
)
293 features
|= (1 << FEATURE_SSE4_1
);
294 if (ecx
& bit_SSE4_2
)
295 features
|= (1 << FEATURE_SSE4_2
);
297 features
|= (1 << FEATURE_AVX
);
299 features
|= (1 << FEATURE_FMA
);
301 /* Get Advanced Features at level 7 (eax = 7, ecx = 0). */
302 if (max_cpuid_level
>= 7)
304 unsigned int eax
, ebx
, ecx
, edx
;
305 __cpuid_count (7, 0, eax
, ebx
, ecx
, edx
);
307 features
|= (1 << FEATURE_BMI
);
309 features
|= (1 << FEATURE_AVX2
);
311 features
|= (1 << FEATURE_BMI2
);
312 if (ebx
& bit_AVX512F
)
313 features
|= (1 << FEATURE_AVX512F
);
316 unsigned int ext_level
;
317 unsigned int eax
, ebx
;
318 /* Check cpuid level of extended features. */
319 __cpuid (0x80000000, ext_level
, ebx
, ecx
, edx
);
321 if (ext_level
> 0x80000000)
323 __cpuid (0x80000001, eax
, ebx
, ecx
, edx
);
326 features
|= (1 << FEATURE_SSE4_A
);
328 features
|= (1 << FEATURE_FMA4
);
330 features
|= (1 << FEATURE_XOP
);
333 __cpu_model
.__cpu_features
[0] = features
;
336 /* A noinline function calling __get_cpuid. Having many calls to
337 cpuid in one function in 32-bit mode causes GCC to complain:
338 "can't find a register in class CLOBBERED_REGS". This is
339 related to PR rtl-optimization 44174. */
341 static int __attribute__ ((noinline
))
342 __get_cpuid_output (unsigned int __level
,
343 unsigned int *__eax
, unsigned int *__ebx
,
344 unsigned int *__ecx
, unsigned int *__edx
)
346 return __get_cpuid (__level
, __eax
, __ebx
, __ecx
, __edx
);
350 /* A constructor function that is sets __cpu_model and __cpu_features with
351 the right values. This needs to run only once. This constructor is
352 given the highest priority and it should run before constructors without
353 the priority set. However, it still runs after ifunc initializers and
354 needs to be called explicitly there. */
356 int __attribute__ ((constructor CONSTRUCTOR_PRIORITY
))
357 __cpu_indicator_init (void)
359 unsigned int eax
, ebx
, ecx
, edx
;
363 unsigned int model
, family
, brand_id
;
364 unsigned int extended_model
, extended_family
;
366 /* This function needs to run just once. */
367 if (__cpu_model
.__cpu_vendor
)
370 /* Assume cpuid insn present. Run in level 0 to get vendor id. */
371 if (!__get_cpuid_output (0, &eax
, &ebx
, &ecx
, &edx
))
373 __cpu_model
.__cpu_vendor
= VENDOR_OTHER
;
382 __cpu_model
.__cpu_vendor
= VENDOR_OTHER
;
386 if (!__get_cpuid_output (1, &eax
, &ebx
, &ecx
, &edx
))
388 __cpu_model
.__cpu_vendor
= VENDOR_OTHER
;
392 model
= (eax
>> 4) & 0x0f;
393 family
= (eax
>> 8) & 0x0f;
394 brand_id
= ebx
& 0xff;
395 extended_model
= (eax
>> 12) & 0xf0;
396 extended_family
= (eax
>> 20) & 0xff;
398 if (vendor
== signature_INTEL_ebx
)
400 /* Adjust model and family for Intel CPUS. */
403 family
+= extended_family
;
404 model
+= extended_model
;
406 else if (family
== 0x06)
407 model
+= extended_model
;
410 get_intel_cpu (family
, model
, brand_id
);
411 /* Find available features. */
412 get_available_features (ecx
, edx
, max_level
);
413 __cpu_model
.__cpu_vendor
= VENDOR_INTEL
;
415 else if (vendor
== signature_AMD_ebx
)
417 /* Adjust model and family for AMD CPUS. */
420 family
+= extended_family
;
421 model
+= (extended_model
<< 4);
425 get_amd_cpu (family
, model
);
426 /* Find available features. */
427 get_available_features (ecx
, edx
, max_level
);
428 __cpu_model
.__cpu_vendor
= VENDOR_AMD
;
431 __cpu_model
.__cpu_vendor
= VENDOR_OTHER
;
433 gcc_assert (__cpu_model
.__cpu_vendor
< VENDOR_MAX
);
434 gcc_assert (__cpu_model
.__cpu_type
< CPU_TYPE_MAX
);
435 gcc_assert (__cpu_model
.__cpu_subtype
< CPU_SUBTYPE_MAX
);
440 #if defined SHARED && defined USE_ELF_SYMVER
441 __asm__ (".symver __cpu_indicator_init, __cpu_indicator_init@GCC_4.8.0");
442 __asm__ (".symver __cpu_model, __cpu_model@GCC_4.8.0");