2013-06-18 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libgcc / config / i386 / cpuinfo.c
blobb7e64c3d720a8279b188a84de1aabc8c91d90984
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
10 version.
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
15 for more details.
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/>. */
21 #include "cpuid.h"
22 #include "tsystem.h"
23 #include "auto-target.h"
25 #ifdef HAVE_INIT_PRIORITY
26 #define CONSTRUCTOR_PRIORITY (101)
27 #else
28 #define CONSTRUCTOR_PRIORITY
29 #endif
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. */
42 enum processor_vendor
44 VENDOR_INTEL = 1,
45 VENDOR_AMD,
46 VENDOR_OTHER,
47 VENDOR_MAX
50 /* Any new types or subtypes have to be inserted at the end. */
52 enum processor_types
54 INTEL_ATOM = 1,
55 INTEL_CORE2,
56 INTEL_COREI7,
57 AMDFAM10H,
58 AMDFAM15H,
59 INTEL_SLM,
60 CPU_TYPE_MAX
63 enum processor_subtypes
65 INTEL_COREI7_NEHALEM = 1,
66 INTEL_COREI7_WESTMERE,
67 INTEL_COREI7_SANDYBRIDGE,
68 AMDFAM10H_BARCELONA,
69 AMDFAM10H_SHANGHAI,
70 AMDFAM10H_ISTANBUL,
71 AMDFAM15H_BDVER1,
72 AMDFAM15H_BDVER2,
73 CPU_SUBTYPE_MAX
76 /* ISA Features supported. */
78 enum processor_features
80 FEATURE_CMOV = 0,
81 FEATURE_MMX,
82 FEATURE_POPCNT,
83 FEATURE_SSE,
84 FEATURE_SSE2,
85 FEATURE_SSE3,
86 FEATURE_SSSE3,
87 FEATURE_SSE4_1,
88 FEATURE_SSE4_2,
89 FEATURE_AVX,
90 FEATURE_AVX2
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];
99 } __cpu_model;
102 /* Get the specific type of AMD CPU. */
104 static void
105 get_amd_cpu (unsigned int family, unsigned int model)
107 switch (family)
109 /* AMD Family 10h. */
110 case 0x10:
111 switch (model)
113 case 0x2:
114 /* Barcelona. */
115 __cpu_model.__cpu_type = AMDFAM10H;
116 __cpu_model.__cpu_subtype = AMDFAM10H_BARCELONA;
117 break;
118 case 0x4:
119 /* Shanghai. */
120 __cpu_model.__cpu_type = AMDFAM10H;
121 __cpu_model.__cpu_subtype = AMDFAM10H_SHANGHAI;
122 break;
123 case 0x8:
124 /* Istanbul. */
125 __cpu_model.__cpu_type = AMDFAM10H;
126 __cpu_model.__cpu_subtype = AMDFAM10H_ISTANBUL;
127 break;
128 default:
129 break;
131 break;
132 /* AMD Family 15h. */
133 case 0x15:
134 __cpu_model.__cpu_type = AMDFAM15H;
135 /* Bulldozer version 1. */
136 if ( model <= 0xf)
137 __cpu_model.__cpu_subtype = AMDFAM15H_BDVER1;
138 /* Bulldozer version 2. */
139 if (model >= 0x10 && model <= 0x1f)
140 __cpu_model.__cpu_subtype = AMDFAM15H_BDVER2;
141 break;
142 default:
143 break;
147 /* Get the specific type of Intel CPU. */
149 static void
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. */
153 if (brand_id == 0)
155 switch (family)
157 case 0x5:
158 /* Pentium. */
159 break;
160 case 0x6:
161 switch (model)
163 case 0x1c:
164 case 0x26:
165 /* Atom. */
166 __cpu_model.__cpu_type = INTEL_ATOM;
167 break;
168 case 0x1a:
169 case 0x1e:
170 case 0x1f:
171 case 0x2e:
172 /* Nehalem. */
173 __cpu_model.__cpu_type = INTEL_COREI7;
174 __cpu_model.__cpu_subtype = INTEL_COREI7_NEHALEM;
175 break;
176 case 0x25:
177 case 0x2c:
178 case 0x2f:
179 /* Westmere. */
180 __cpu_model.__cpu_type = INTEL_COREI7;
181 __cpu_model.__cpu_subtype = INTEL_COREI7_WESTMERE;
182 break;
183 case 0x2a:
184 case 0x2d:
185 /* Sandy Bridge. */
186 __cpu_model.__cpu_type = INTEL_COREI7;
187 __cpu_model.__cpu_subtype = INTEL_COREI7_SANDYBRIDGE;
188 break;
189 case 0x17:
190 case 0x1d:
191 /* Penryn. */
192 case 0x0f:
193 /* Merom. */
194 __cpu_model.__cpu_type = INTEL_CORE2;
195 break;
196 default:
197 break;
199 break;
200 default:
201 /* We have no idea. */
202 break;
207 /* ECX and EDX are output of CPUID at level one. MAX_CPUID_LEVEL is
208 the max possible level of CPUID insn. */
209 static void
210 get_available_features (unsigned int ecx, unsigned int edx,
211 int max_cpuid_level)
213 unsigned int features = 0;
215 if (edx & bit_CMOV)
216 features |= (1 << FEATURE_CMOV);
217 if (edx & bit_MMX)
218 features |= (1 << FEATURE_MMX);
219 if (edx & bit_SSE)
220 features |= (1 << FEATURE_SSE);
221 if (edx & bit_SSE2)
222 features |= (1 << FEATURE_SSE2);
223 if (ecx & bit_POPCNT)
224 features |= (1 << FEATURE_POPCNT);
225 if (ecx & bit_SSE3)
226 features |= (1 << FEATURE_SSE3);
227 if (ecx & bit_SSSE3)
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);
233 if (ecx & bit_AVX)
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);
241 if (ebx & bit_AVX2)
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;
273 int max_level = 5;
274 unsigned int vendor;
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)
280 return 0;
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;
286 return -1;
289 vendor = ebx;
290 max_level = eax;
292 if (max_level < 1)
294 __cpu_model.__cpu_vendor = VENDOR_OTHER;
295 return -1;
298 if (!__get_cpuid_output (1, &eax, &ebx, &ecx, &edx))
300 __cpu_model.__cpu_vendor = VENDOR_OTHER;
301 return -1;
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. */
313 if (family == 0x0f)
315 family += extended_family;
316 model += extended_model;
318 else if (family == 0x06)
319 model += extended_model;
321 /* Get CPU type. */
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. */
330 if (family == 0x0f)
332 family += extended_family;
333 model += (extended_model << 4);
336 /* Get CPU type. */
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;
342 else
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);
349 return 0;