blob: 4318f1d96ecb14e5c843ec5d05117dcc287264e3 [file] [log] [blame]
Simon Pilgrima271c542017-05-03 15:42:29 +00001//===-- Host.cpp - Implement OS Host Concept --------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the operating system Host concept.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/Host.h"
15#include "llvm/ADT/SmallSet.h"
16#include "llvm/ADT/SmallVector.h"
17#include "llvm/ADT/StringRef.h"
18#include "llvm/ADT/StringSwitch.h"
19#include "llvm/ADT/Triple.h"
20#include "llvm/Config/config.h"
21#include "llvm/Support/Debug.h"
22#include "llvm/Support/FileSystem.h"
23#include "llvm/Support/MemoryBuffer.h"
24#include "llvm/Support/raw_ostream.h"
25#include <assert.h>
26#include <string.h>
27
28// Include the platform-specific parts of this class.
29#ifdef LLVM_ON_UNIX
30#include "Unix/Host.inc"
31#endif
32#ifdef LLVM_ON_WIN32
33#include "Windows/Host.inc"
34#endif
35#ifdef _MSC_VER
36#include <intrin.h>
37#endif
38#if defined(__APPLE__) && (defined(__ppc__) || defined(__powerpc__))
39#include <mach/host_info.h>
40#include <mach/mach.h>
41#include <mach/mach_host.h>
42#include <mach/machine.h>
43#endif
44
45#define DEBUG_TYPE "host-detection"
46
47//===----------------------------------------------------------------------===//
48//
49// Implementations of the CPU detection routines
50//
51//===----------------------------------------------------------------------===//
52
53using namespace llvm;
54
55static std::unique_ptr<llvm::MemoryBuffer>
56 LLVM_ATTRIBUTE_UNUSED getProcCpuinfoContent() {
57 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
58 llvm::MemoryBuffer::getFileAsStream("/proc/cpuinfo");
59 if (std::error_code EC = Text.getError()) {
60 llvm::errs() << "Can't read "
61 << "/proc/cpuinfo: " << EC.message() << "\n";
62 return nullptr;
63 }
64 return std::move(*Text);
65}
66
67StringRef sys::detail::getHostCPUNameForPowerPC(
68 const StringRef &ProcCpuinfoContent) {
69 // Access to the Processor Version Register (PVR) on PowerPC is privileged,
70 // and so we must use an operating-system interface to determine the current
71 // processor type. On Linux, this is exposed through the /proc/cpuinfo file.
72 const char *generic = "generic";
73
74 // The cpu line is second (after the 'processor: 0' line), so if this
75 // buffer is too small then something has changed (or is wrong).
76 StringRef::const_iterator CPUInfoStart = ProcCpuinfoContent.begin();
77 StringRef::const_iterator CPUInfoEnd = ProcCpuinfoContent.end();
78
79 StringRef::const_iterator CIP = CPUInfoStart;
80
81 StringRef::const_iterator CPUStart = 0;
82 size_t CPULen = 0;
83
84 // We need to find the first line which starts with cpu, spaces, and a colon.
85 // After the colon, there may be some additional spaces and then the cpu type.
86 while (CIP < CPUInfoEnd && CPUStart == 0) {
87 if (CIP < CPUInfoEnd && *CIP == '\n')
88 ++CIP;
89
90 if (CIP < CPUInfoEnd && *CIP == 'c') {
91 ++CIP;
92 if (CIP < CPUInfoEnd && *CIP == 'p') {
93 ++CIP;
94 if (CIP < CPUInfoEnd && *CIP == 'u') {
95 ++CIP;
96 while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t'))
97 ++CIP;
98
99 if (CIP < CPUInfoEnd && *CIP == ':') {
100 ++CIP;
101 while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t'))
102 ++CIP;
103
104 if (CIP < CPUInfoEnd) {
105 CPUStart = CIP;
106 while (CIP < CPUInfoEnd && (*CIP != ' ' && *CIP != '\t' &&
107 *CIP != ',' && *CIP != '\n'))
108 ++CIP;
109 CPULen = CIP - CPUStart;
110 }
111 }
112 }
113 }
114 }
115
116 if (CPUStart == 0)
117 while (CIP < CPUInfoEnd && *CIP != '\n')
118 ++CIP;
119 }
120
121 if (CPUStart == 0)
122 return generic;
123
124 return StringSwitch<const char *>(StringRef(CPUStart, CPULen))
125 .Case("604e", "604e")
126 .Case("604", "604")
127 .Case("7400", "7400")
128 .Case("7410", "7400")
129 .Case("7447", "7400")
130 .Case("7455", "7450")
131 .Case("G4", "g4")
132 .Case("POWER4", "970")
133 .Case("PPC970FX", "970")
134 .Case("PPC970MP", "970")
135 .Case("G5", "g5")
136 .Case("POWER5", "g5")
137 .Case("A2", "a2")
138 .Case("POWER6", "pwr6")
139 .Case("POWER7", "pwr7")
140 .Case("POWER8", "pwr8")
141 .Case("POWER8E", "pwr8")
142 .Case("POWER8NVL", "pwr8")
143 .Case("POWER9", "pwr9")
144 .Default(generic);
145}
146
147StringRef sys::detail::getHostCPUNameForARM(
148 const StringRef &ProcCpuinfoContent) {
149 // The cpuid register on arm is not accessible from user space. On Linux,
150 // it is exposed through the /proc/cpuinfo file.
151
152 // Read 32 lines from /proc/cpuinfo, which should contain the CPU part line
153 // in all cases.
154 SmallVector<StringRef, 32> Lines;
155 ProcCpuinfoContent.split(Lines, "\n");
156
157 // Look for the CPU implementer line.
158 StringRef Implementer;
159 StringRef Hardware;
160 for (unsigned I = 0, E = Lines.size(); I != E; ++I) {
161 if (Lines[I].startswith("CPU implementer"))
162 Implementer = Lines[I].substr(15).ltrim("\t :");
163 if (Lines[I].startswith("Hardware"))
164 Hardware = Lines[I].substr(8).ltrim("\t :");
165 }
166
167 if (Implementer == "0x41") { // ARM Ltd.
168 // MSM8992/8994 may give cpu part for the core that the kernel is running on,
169 // which is undeterministic and wrong. Always return cortex-a53 for these SoC.
170 if (Hardware.endswith("MSM8994") || Hardware.endswith("MSM8996"))
171 return "cortex-a53";
172
173
174 // Look for the CPU part line.
175 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
176 if (Lines[I].startswith("CPU part"))
177 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
178 // values correspond to the "Part number" in the CP15/c0 register. The
179 // contents are specified in the various processor manuals.
180 return StringSwitch<const char *>(Lines[I].substr(8).ltrim("\t :"))
181 .Case("0x926", "arm926ej-s")
182 .Case("0xb02", "mpcore")
183 .Case("0xb36", "arm1136j-s")
184 .Case("0xb56", "arm1156t2-s")
185 .Case("0xb76", "arm1176jz-s")
186 .Case("0xc08", "cortex-a8")
187 .Case("0xc09", "cortex-a9")
188 .Case("0xc0f", "cortex-a15")
189 .Case("0xc20", "cortex-m0")
190 .Case("0xc23", "cortex-m3")
191 .Case("0xc24", "cortex-m4")
192 .Case("0xd04", "cortex-a35")
193 .Case("0xd03", "cortex-a53")
194 .Case("0xd07", "cortex-a57")
195 .Case("0xd08", "cortex-a72")
196 .Case("0xd09", "cortex-a73")
197 .Default("generic");
198 }
199
200 if (Implementer == "0x51") // Qualcomm Technologies, Inc.
201 // Look for the CPU part line.
202 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
203 if (Lines[I].startswith("CPU part"))
204 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
205 // values correspond to the "Part number" in the CP15/c0 register. The
206 // contents are specified in the various processor manuals.
207 return StringSwitch<const char *>(Lines[I].substr(8).ltrim("\t :"))
208 .Case("0x06f", "krait") // APQ8064
209 .Case("0x201", "kryo")
210 .Case("0x205", "kryo")
211 .Default("generic");
212
213 return "generic";
214}
215
216StringRef sys::detail::getHostCPUNameForS390x(
217 const StringRef &ProcCpuinfoContent) {
218 // STIDP is a privileged operation, so use /proc/cpuinfo instead.
219
220 // The "processor 0:" line comes after a fair amount of other information,
221 // including a cache breakdown, but this should be plenty.
222 SmallVector<StringRef, 32> Lines;
223 ProcCpuinfoContent.split(Lines, "\n");
224
225 // Look for the CPU features.
226 SmallVector<StringRef, 32> CPUFeatures;
227 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
228 if (Lines[I].startswith("features")) {
229 size_t Pos = Lines[I].find(":");
230 if (Pos != StringRef::npos) {
231 Lines[I].drop_front(Pos + 1).split(CPUFeatures, ' ');
232 break;
233 }
234 }
235
236 // We need to check for the presence of vector support independently of
237 // the machine type, since we may only use the vector register set when
238 // supported by the kernel (and hypervisor).
239 bool HaveVectorSupport = false;
240 for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) {
241 if (CPUFeatures[I] == "vx")
242 HaveVectorSupport = true;
243 }
244
245 // Now check the processor machine type.
246 for (unsigned I = 0, E = Lines.size(); I != E; ++I) {
247 if (Lines[I].startswith("processor ")) {
248 size_t Pos = Lines[I].find("machine = ");
249 if (Pos != StringRef::npos) {
250 Pos += sizeof("machine = ") - 1;
251 unsigned int Id;
252 if (!Lines[I].drop_front(Pos).getAsInteger(10, Id)) {
253 if (Id >= 2964 && HaveVectorSupport)
254 return "z13";
255 if (Id >= 2827)
256 return "zEC12";
257 if (Id >= 2817)
258 return "z196";
259 }
260 }
261 break;
262 }
263 }
264
265 return "generic";
266}
267
268#if defined(__i386__) || defined(_M_IX86) || \
269 defined(__x86_64__) || defined(_M_X64)
270
271enum VendorSignatures {
272 SIG_INTEL = 0x756e6547 /* Genu */,
273 SIG_AMD = 0x68747541 /* Auth */
274};
275
276enum ProcessorVendors {
277 VENDOR_INTEL = 1,
278 VENDOR_AMD,
279 VENDOR_OTHER,
280 VENDOR_MAX
281};
282
283enum ProcessorTypes {
Craig Topperf3af64e2017-07-12 06:49:57 +0000284 INTEL_BONNELL = 1,
Simon Pilgrima271c542017-05-03 15:42:29 +0000285 INTEL_CORE2,
286 INTEL_COREI7,
287 AMDFAM10H,
288 AMDFAM15H,
Craig Topperf3af64e2017-07-12 06:49:57 +0000289 INTEL_SILVERMONT,
290 INTEL_KNL,
291 AMD_BTVER1,
292 AMD_BTVER2,
293 AMDFAM17H,
294 // Entries below this are not in libgcc/compiler-rt.
Simon Pilgrima271c542017-05-03 15:42:29 +0000295 INTEL_i386,
296 INTEL_i486,
297 INTEL_PENTIUM,
298 INTEL_PENTIUM_PRO,
299 INTEL_PENTIUM_II,
300 INTEL_PENTIUM_III,
301 INTEL_PENTIUM_IV,
302 INTEL_PENTIUM_M,
303 INTEL_CORE_DUO,
Simon Pilgrima271c542017-05-03 15:42:29 +0000304 INTEL_X86_64,
305 INTEL_NOCONA,
306 INTEL_PRESCOTT,
307 AMD_i486,
308 AMDPENTIUM,
309 AMDATHLON,
Craig Topperf3af64e2017-07-12 06:49:57 +0000310 INTEL_GOLDMONT,
Simon Pilgrima271c542017-05-03 15:42:29 +0000311 CPU_TYPE_MAX
312};
313
314enum ProcessorSubtypes {
315 INTEL_COREI7_NEHALEM = 1,
316 INTEL_COREI7_WESTMERE,
317 INTEL_COREI7_SANDYBRIDGE,
318 AMDFAM10H_BARCELONA,
319 AMDFAM10H_SHANGHAI,
320 AMDFAM10H_ISTANBUL,
321 AMDFAM15H_BDVER1,
322 AMDFAM15H_BDVER2,
Craig Topperf3af64e2017-07-12 06:49:57 +0000323 AMDFAM15H_BDVER3,
324 AMDFAM15H_BDVER4,
325 AMDFAM17H_ZNVER1,
Simon Pilgrima271c542017-05-03 15:42:29 +0000326 INTEL_COREI7_IVYBRIDGE,
327 INTEL_COREI7_HASWELL,
328 INTEL_COREI7_BROADWELL,
329 INTEL_COREI7_SKYLAKE,
330 INTEL_COREI7_SKYLAKE_AVX512,
Craig Topperf3af64e2017-07-12 06:49:57 +0000331 // Entries below this are not in libgcc/compiler-rt.
332 INTEL_PENTIUM_MMX,
333 INTEL_CORE2_65,
334 INTEL_CORE2_45,
Simon Pilgrima271c542017-05-03 15:42:29 +0000335 AMDPENTIUM_K6,
336 AMDPENTIUM_K62,
337 AMDPENTIUM_K63,
338 AMDPENTIUM_GEODE,
Craig Topperf3de5eb2017-07-13 06:34:10 +0000339 AMDATHLON_CLASSIC,
Simon Pilgrima271c542017-05-03 15:42:29 +0000340 AMDATHLON_XP,
Craig Topperf3de5eb2017-07-13 06:34:10 +0000341 AMDATHLON_K8,
Simon Pilgrima271c542017-05-03 15:42:29 +0000342 AMDATHLON_K8SSE3,
Simon Pilgrima271c542017-05-03 15:42:29 +0000343 CPU_SUBTYPE_MAX
344};
345
346enum ProcessorFeatures {
347 FEATURE_CMOV = 0,
348 FEATURE_MMX,
349 FEATURE_POPCNT,
350 FEATURE_SSE,
351 FEATURE_SSE2,
352 FEATURE_SSE3,
353 FEATURE_SSSE3,
354 FEATURE_SSE4_1,
355 FEATURE_SSE4_2,
356 FEATURE_AVX,
357 FEATURE_AVX2,
Craig Topper3a5d0822017-07-12 06:49:58 +0000358 FEATURE_SSE4_A,
359 FEATURE_FMA4,
360 FEATURE_XOP,
361 FEATURE_FMA,
362 FEATURE_AVX512F,
363 FEATURE_BMI,
364 FEATURE_BMI2,
365 FEATURE_AES,
366 FEATURE_PCLMUL,
367 FEATURE_AVX512VL,
368 FEATURE_AVX512BW,
369 FEATURE_AVX512DQ,
370 FEATURE_AVX512CD,
371 FEATURE_AVX512ER,
372 FEATURE_AVX512PF,
373 FEATURE_AVX512VBMI,
374 FEATURE_AVX512IFMA,
375 FEATURE_AVX5124VNNIW,
376 FEATURE_AVX5124FMAPS,
377 FEATURE_AVX512VPOPCNTDQ,
378 // Only one bit free left in the first 32 features.
379 FEATURE_MOVBE = 32,
Simon Pilgrima271c542017-05-03 15:42:29 +0000380 FEATURE_ADX,
381 FEATURE_EM64T
382};
383
384// The check below for i386 was copied from clang's cpuid.h (__get_cpuid_max).
385// Check motivated by bug reports for OpenSSL crashing on CPUs without CPUID
386// support. Consequently, for i386, the presence of CPUID is checked first
387// via the corresponding eflags bit.
388// Removal of cpuid.h header motivated by PR30384
389// Header cpuid.h and method __get_cpuid_max are not used in llvm, clang, openmp
390// or test-suite, but are used in external projects e.g. libstdcxx
391static bool isCpuIdSupported() {
392#if defined(__GNUC__) || defined(__clang__)
393#if defined(__i386__)
394 int __cpuid_supported;
395 __asm__(" pushfl\n"
396 " popl %%eax\n"
397 " movl %%eax,%%ecx\n"
398 " xorl $0x00200000,%%eax\n"
399 " pushl %%eax\n"
400 " popfl\n"
401 " pushfl\n"
402 " popl %%eax\n"
403 " movl $0,%0\n"
404 " cmpl %%eax,%%ecx\n"
405 " je 1f\n"
406 " movl $1,%0\n"
407 "1:"
408 : "=r"(__cpuid_supported)
409 :
410 : "eax", "ecx");
411 if (!__cpuid_supported)
412 return false;
413#endif
414 return true;
415#endif
416 return true;
417}
418
419/// getX86CpuIDAndInfo - Execute the specified cpuid and return the 4 values in
420/// the specified arguments. If we can't run cpuid on the host, return true.
421static bool getX86CpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
422 unsigned *rECX, unsigned *rEDX) {
Simon Pilgrima271c542017-05-03 15:42:29 +0000423#if defined(__GNUC__) || defined(__clang__)
424#if defined(__x86_64__)
425 // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
426 // FIXME: should we save this for Clang?
427 __asm__("movq\t%%rbx, %%rsi\n\t"
428 "cpuid\n\t"
429 "xchgq\t%%rbx, %%rsi\n\t"
430 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
431 : "a"(value));
Craig Topper1efd10a2017-07-10 06:04:11 +0000432 return false;
Simon Pilgrima271c542017-05-03 15:42:29 +0000433#elif defined(__i386__)
434 __asm__("movl\t%%ebx, %%esi\n\t"
435 "cpuid\n\t"
436 "xchgl\t%%ebx, %%esi\n\t"
437 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
438 : "a"(value));
Craig Topper1efd10a2017-07-10 06:04:11 +0000439 return false;
Simon Pilgrima271c542017-05-03 15:42:29 +0000440#else
Craig Topper1efd10a2017-07-10 06:04:11 +0000441 return true;
Simon Pilgrima271c542017-05-03 15:42:29 +0000442#endif
443#elif defined(_MSC_VER)
444 // The MSVC intrinsic is portable across x86 and x64.
445 int registers[4];
446 __cpuid(registers, value);
447 *rEAX = registers[0];
448 *rEBX = registers[1];
449 *rECX = registers[2];
450 *rEDX = registers[3];
Simon Pilgrima271c542017-05-03 15:42:29 +0000451 return false;
452#else
453 return true;
454#endif
455}
456
457/// getX86CpuIDAndInfoEx - Execute the specified cpuid with subleaf and return
458/// the 4 values in the specified arguments. If we can't run cpuid on the host,
459/// return true.
460static bool getX86CpuIDAndInfoEx(unsigned value, unsigned subleaf,
461 unsigned *rEAX, unsigned *rEBX, unsigned *rECX,
462 unsigned *rEDX) {
Simon Pilgrima271c542017-05-03 15:42:29 +0000463#if defined(__GNUC__) || defined(__clang__)
Craig Topper828cf302017-07-17 05:16:16 +0000464#if defined(__x86_64__)
Craig Topperada983a2017-07-10 06:09:22 +0000465 // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
Simon Pilgrima271c542017-05-03 15:42:29 +0000466 // FIXME: should we save this for Clang?
467 __asm__("movq\t%%rbx, %%rsi\n\t"
468 "cpuid\n\t"
469 "xchgq\t%%rbx, %%rsi\n\t"
470 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
471 : "a"(value), "c"(subleaf));
Craig Topper1efd10a2017-07-10 06:04:11 +0000472 return false;
Craig Topper828cf302017-07-17 05:16:16 +0000473#elif defined(__i386__)
474 __asm__("movl\t%%ebx, %%esi\n\t"
475 "cpuid\n\t"
476 "xchgl\t%%ebx, %%esi\n\t"
477 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
478 : "a"(value), "c"(subleaf));
479 return false;
480#else
481 return true;
482#endif
Simon Pilgrima271c542017-05-03 15:42:29 +0000483#elif defined(_MSC_VER)
484 int registers[4];
485 __cpuidex(registers, value, subleaf);
486 *rEAX = registers[0];
487 *rEBX = registers[1];
488 *rECX = registers[2];
489 *rEDX = registers[3];
Craig Topper1efd10a2017-07-10 06:04:11 +0000490 return false;
491#else
492 return true;
Simon Pilgrima271c542017-05-03 15:42:29 +0000493#endif
Simon Pilgrima271c542017-05-03 15:42:29 +0000494}
495
Craig Topperf3af64e2017-07-12 06:49:57 +0000496// Read control register 0 (XCR0). Used to detect features such as AVX.
Simon Pilgrima271c542017-05-03 15:42:29 +0000497static bool getX86XCR0(unsigned *rEAX, unsigned *rEDX) {
498#if defined(__GNUC__) || defined(__clang__)
499 // Check xgetbv; this uses a .byte sequence instead of the instruction
500 // directly because older assemblers do not include support for xgetbv and
501 // there is no easy way to conditionally compile based on the assembler used.
502 __asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(*rEAX), "=d"(*rEDX) : "c"(0));
503 return false;
504#elif defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK)
505 unsigned long long Result = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
506 *rEAX = Result;
507 *rEDX = Result >> 32;
508 return false;
509#else
510 return true;
511#endif
512}
513
514static void detectX86FamilyModel(unsigned EAX, unsigned *Family,
515 unsigned *Model) {
516 *Family = (EAX >> 8) & 0xf; // Bits 8 - 11
517 *Model = (EAX >> 4) & 0xf; // Bits 4 - 7
518 if (*Family == 6 || *Family == 0xf) {
519 if (*Family == 0xf)
520 // Examine extended family ID if family ID is F.
521 *Family += (EAX >> 20) & 0xff; // Bits 20 - 27
522 // Examine extended model ID if family ID is 6 or F.
523 *Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
524 }
525}
526
527static void
Craig Topperc6bbe4b2017-07-08 05:16:14 +0000528getIntelProcessorTypeAndSubtype(unsigned Family, unsigned Model,
529 unsigned Brand_id, unsigned Features,
Craig Topper3a5d0822017-07-12 06:49:58 +0000530 unsigned Features2, unsigned *Type,
531 unsigned *Subtype) {
Simon Pilgrima271c542017-05-03 15:42:29 +0000532 if (Brand_id != 0)
533 return;
534 switch (Family) {
535 case 3:
536 *Type = INTEL_i386;
537 break;
538 case 4:
539 switch (Model) {
540 case 0: // Intel486 DX processors
541 case 1: // Intel486 DX processors
542 case 2: // Intel486 SX processors
543 case 3: // Intel487 processors, IntelDX2 OverDrive processors,
544 // IntelDX2 processors
545 case 4: // Intel486 SL processor
546 case 5: // IntelSX2 processors
547 case 7: // Write-Back Enhanced IntelDX2 processors
548 case 8: // IntelDX4 OverDrive processors, IntelDX4 processors
549 default:
550 *Type = INTEL_i486;
551 break;
552 }
553 break;
554 case 5:
555 switch (Model) {
556 case 1: // Pentium OverDrive processor for Pentium processor (60, 66),
557 // Pentium processors (60, 66)
558 case 2: // Pentium OverDrive processor for Pentium processor (75, 90,
559 // 100, 120, 133), Pentium processors (75, 90, 100, 120, 133,
560 // 150, 166, 200)
561 case 3: // Pentium OverDrive processors for Intel486 processor-based
562 // systems
563 *Type = INTEL_PENTIUM;
564 break;
565 case 4: // Pentium OverDrive processor with MMX technology for Pentium
566 // processor (75, 90, 100, 120, 133), Pentium processor with
567 // MMX technology (166, 200)
568 *Type = INTEL_PENTIUM;
569 *Subtype = INTEL_PENTIUM_MMX;
570 break;
571 default:
572 *Type = INTEL_PENTIUM;
573 break;
574 }
575 break;
576 case 6:
577 switch (Model) {
578 case 0x01: // Pentium Pro processor
579 *Type = INTEL_PENTIUM_PRO;
580 break;
581 case 0x03: // Intel Pentium II OverDrive processor, Pentium II processor,
582 // model 03
583 case 0x05: // Pentium II processor, model 05, Pentium II Xeon processor,
584 // model 05, and Intel Celeron processor, model 05
585 case 0x06: // Celeron processor, model 06
586 *Type = INTEL_PENTIUM_II;
587 break;
588 case 0x07: // Pentium III processor, model 07, and Pentium III Xeon
589 // processor, model 07
590 case 0x08: // Pentium III processor, model 08, Pentium III Xeon processor,
591 // model 08, and Celeron processor, model 08
592 case 0x0a: // Pentium III Xeon processor, model 0Ah
593 case 0x0b: // Pentium III processor, model 0Bh
594 *Type = INTEL_PENTIUM_III;
595 break;
596 case 0x09: // Intel Pentium M processor, Intel Celeron M processor model 09.
597 case 0x0d: // Intel Pentium M processor, Intel Celeron M processor, model
598 // 0Dh. All processors are manufactured using the 90 nm process.
599 case 0x15: // Intel EP80579 Integrated Processor and Intel EP80579
600 // Integrated Processor with Intel QuickAssist Technology
601 *Type = INTEL_PENTIUM_M;
602 break;
603 case 0x0e: // Intel Core Duo processor, Intel Core Solo processor, model
604 // 0Eh. All processors are manufactured using the 65 nm process.
605 *Type = INTEL_CORE_DUO;
606 break; // yonah
607 case 0x0f: // Intel Core 2 Duo processor, Intel Core 2 Duo mobile
608 // processor, Intel Core 2 Quad processor, Intel Core 2 Quad
609 // mobile processor, Intel Core 2 Extreme processor, Intel
610 // Pentium Dual-Core processor, Intel Xeon processor, model
611 // 0Fh. All processors are manufactured using the 65 nm process.
612 case 0x16: // Intel Celeron processor model 16h. All processors are
613 // manufactured using the 65 nm process
614 *Type = INTEL_CORE2; // "core2"
615 *Subtype = INTEL_CORE2_65;
616 break;
617 case 0x17: // Intel Core 2 Extreme processor, Intel Xeon processor, model
618 // 17h. All processors are manufactured using the 45 nm process.
619 //
620 // 45nm: Penryn , Wolfdale, Yorkfield (XE)
621 case 0x1d: // Intel Xeon processor MP. All processors are manufactured using
622 // the 45 nm process.
623 *Type = INTEL_CORE2; // "penryn"
624 *Subtype = INTEL_CORE2_45;
625 break;
626 case 0x1a: // Intel Core i7 processor and Intel Xeon processor. All
627 // processors are manufactured using the 45 nm process.
628 case 0x1e: // Intel(R) Core(TM) i7 CPU 870 @ 2.93GHz.
629 // As found in a Summer 2010 model iMac.
630 case 0x1f:
631 case 0x2e: // Nehalem EX
632 *Type = INTEL_COREI7; // "nehalem"
633 *Subtype = INTEL_COREI7_NEHALEM;
634 break;
635 case 0x25: // Intel Core i7, laptop version.
636 case 0x2c: // Intel Core i7 processor and Intel Xeon processor. All
637 // processors are manufactured using the 32 nm process.
638 case 0x2f: // Westmere EX
639 *Type = INTEL_COREI7; // "westmere"
640 *Subtype = INTEL_COREI7_WESTMERE;
641 break;
642 case 0x2a: // Intel Core i7 processor. All processors are manufactured
643 // using the 32 nm process.
644 case 0x2d:
645 *Type = INTEL_COREI7; //"sandybridge"
646 *Subtype = INTEL_COREI7_SANDYBRIDGE;
647 break;
648 case 0x3a:
649 case 0x3e: // Ivy Bridge EP
650 *Type = INTEL_COREI7; // "ivybridge"
651 *Subtype = INTEL_COREI7_IVYBRIDGE;
652 break;
653
654 // Haswell:
655 case 0x3c:
656 case 0x3f:
657 case 0x45:
658 case 0x46:
659 *Type = INTEL_COREI7; // "haswell"
660 *Subtype = INTEL_COREI7_HASWELL;
661 break;
662
663 // Broadwell:
664 case 0x3d:
665 case 0x47:
666 case 0x4f:
667 case 0x56:
668 *Type = INTEL_COREI7; // "broadwell"
669 *Subtype = INTEL_COREI7_BROADWELL;
670 break;
671
672 // Skylake:
673 case 0x4e: // Skylake mobile
674 case 0x5e: // Skylake desktop
675 case 0x8e: // Kaby Lake mobile
676 case 0x9e: // Kaby Lake desktop
677 *Type = INTEL_COREI7; // "skylake"
678 *Subtype = INTEL_COREI7_SKYLAKE;
679 break;
680
681 // Skylake Xeon:
682 case 0x55:
683 *Type = INTEL_COREI7;
Craig Topper52cec382017-07-09 07:26:14 +0000684 *Subtype = INTEL_COREI7_SKYLAKE_AVX512; // "skylake-avx512"
Simon Pilgrima271c542017-05-03 15:42:29 +0000685 break;
686
687 case 0x1c: // Most 45 nm Intel Atom processors
688 case 0x26: // 45 nm Atom Lincroft
689 case 0x27: // 32 nm Atom Medfield
690 case 0x35: // 32 nm Atom Midview
691 case 0x36: // 32 nm Atom Midview
Craig Topperf3af64e2017-07-12 06:49:57 +0000692 *Type = INTEL_BONNELL;
Simon Pilgrima271c542017-05-03 15:42:29 +0000693 break; // "bonnell"
694
695 // Atom Silvermont codes from the Intel software optimization guide.
696 case 0x37:
697 case 0x4a:
698 case 0x4d:
699 case 0x5a:
700 case 0x5d:
701 case 0x4c: // really airmont
Craig Topperf3af64e2017-07-12 06:49:57 +0000702 *Type = INTEL_SILVERMONT;
Simon Pilgrima271c542017-05-03 15:42:29 +0000703 break; // "silvermont"
Michael Zuckerman4bcb9c32017-06-29 10:00:33 +0000704 // Goldmont:
705 case 0x5c:
706 case 0x5f:
Craig Topperf3af64e2017-07-12 06:49:57 +0000707 *Type = INTEL_GOLDMONT;
Michael Zuckerman4bcb9c32017-06-29 10:00:33 +0000708 break; // "goldmont"
Simon Pilgrima271c542017-05-03 15:42:29 +0000709 case 0x57:
Craig Topperf3af64e2017-07-12 06:49:57 +0000710 *Type = INTEL_KNL; // knl
Simon Pilgrima271c542017-05-03 15:42:29 +0000711 break;
712
713 default: // Unknown family 6 CPU, try to guess.
Craig Topper3a5d0822017-07-12 06:49:58 +0000714 if (Features & (1 << FEATURE_AVX512F)) {
Craig Topperf3af64e2017-07-12 06:49:57 +0000715 *Type = INTEL_KNL; // knl
Simon Pilgrima271c542017-05-03 15:42:29 +0000716 break;
717 }
Craig Topper3a5d0822017-07-12 06:49:58 +0000718 if (Features2 & (1 << (FEATURE_ADX - 32))) {
Simon Pilgrima271c542017-05-03 15:42:29 +0000719 *Type = INTEL_COREI7;
720 *Subtype = INTEL_COREI7_BROADWELL;
721 break;
722 }
723 if (Features & (1 << FEATURE_AVX2)) {
724 *Type = INTEL_COREI7;
725 *Subtype = INTEL_COREI7_HASWELL;
726 break;
727 }
728 if (Features & (1 << FEATURE_AVX)) {
729 *Type = INTEL_COREI7;
730 *Subtype = INTEL_COREI7_SANDYBRIDGE;
731 break;
732 }
733 if (Features & (1 << FEATURE_SSE4_2)) {
Craig Topper3a5d0822017-07-12 06:49:58 +0000734 if (Features2 & (1 << (FEATURE_MOVBE - 32))) {
Craig Topperf3af64e2017-07-12 06:49:57 +0000735 *Type = INTEL_SILVERMONT;
Simon Pilgrima271c542017-05-03 15:42:29 +0000736 } else {
737 *Type = INTEL_COREI7;
738 *Subtype = INTEL_COREI7_NEHALEM;
739 }
740 break;
741 }
742 if (Features & (1 << FEATURE_SSE4_1)) {
743 *Type = INTEL_CORE2; // "penryn"
744 *Subtype = INTEL_CORE2_45;
745 break;
746 }
747 if (Features & (1 << FEATURE_SSSE3)) {
Craig Topper3a5d0822017-07-12 06:49:58 +0000748 if (Features2 & (1 << (FEATURE_MOVBE - 32))) {
Craig Topperf3af64e2017-07-12 06:49:57 +0000749 *Type = INTEL_BONNELL; // "bonnell"
Simon Pilgrima271c542017-05-03 15:42:29 +0000750 } else {
751 *Type = INTEL_CORE2; // "core2"
752 *Subtype = INTEL_CORE2_65;
753 }
754 break;
755 }
Craig Topper3a5d0822017-07-12 06:49:58 +0000756 if (Features2 & (1 << (FEATURE_EM64T - 32))) {
Simon Pilgrima271c542017-05-03 15:42:29 +0000757 *Type = INTEL_X86_64;
758 break; // x86-64
759 }
760 if (Features & (1 << FEATURE_SSE2)) {
761 *Type = INTEL_PENTIUM_M;
762 break;
763 }
764 if (Features & (1 << FEATURE_SSE)) {
765 *Type = INTEL_PENTIUM_III;
766 break;
767 }
768 if (Features & (1 << FEATURE_MMX)) {
769 *Type = INTEL_PENTIUM_II;
770 break;
771 }
772 *Type = INTEL_PENTIUM_PRO;
773 break;
774 }
775 break;
776 case 15: {
777 switch (Model) {
778 case 0: // Pentium 4 processor, Intel Xeon processor. All processors are
779 // model 00h and manufactured using the 0.18 micron process.
780 case 1: // Pentium 4 processor, Intel Xeon processor, Intel Xeon
781 // processor MP, and Intel Celeron processor. All processors are
782 // model 01h and manufactured using the 0.18 micron process.
783 case 2: // Pentium 4 processor, Mobile Intel Pentium 4 processor - M,
784 // Intel Xeon processor, Intel Xeon processor MP, Intel Celeron
785 // processor, and Mobile Intel Celeron processor. All processors
786 // are model 02h and manufactured using the 0.13 micron process.
Craig Topper3a5d0822017-07-12 06:49:58 +0000787 *Type = ((Features2 & (1 << (FEATURE_EM64T - 32))) ? INTEL_X86_64
788 : INTEL_PENTIUM_IV);
Simon Pilgrima271c542017-05-03 15:42:29 +0000789 break;
790
791 case 3: // Pentium 4 processor, Intel Xeon processor, Intel Celeron D
792 // processor. All processors are model 03h and manufactured using
793 // the 90 nm process.
794 case 4: // Pentium 4 processor, Pentium 4 processor Extreme Edition,
795 // Pentium D processor, Intel Xeon processor, Intel Xeon
796 // processor MP, Intel Celeron D processor. All processors are
797 // model 04h and manufactured using the 90 nm process.
798 case 6: // Pentium 4 processor, Pentium D processor, Pentium processor
799 // Extreme Edition, Intel Xeon processor, Intel Xeon processor
800 // MP, Intel Celeron D processor. All processors are model 06h
801 // and manufactured using the 65 nm process.
Craig Topper3a5d0822017-07-12 06:49:58 +0000802 *Type = ((Features2 & (1 << (FEATURE_EM64T - 32))) ? INTEL_NOCONA
803 : INTEL_PRESCOTT);
Simon Pilgrima271c542017-05-03 15:42:29 +0000804 break;
805
806 default:
Craig Topper3a5d0822017-07-12 06:49:58 +0000807 *Type = ((Features2 & (1 << (FEATURE_EM64T - 32))) ? INTEL_X86_64
808 : INTEL_PENTIUM_IV);
Simon Pilgrima271c542017-05-03 15:42:29 +0000809 break;
810 }
811 break;
812 }
813 default:
814 break; /*"generic"*/
815 }
816}
817
Craig Topper2ace1532017-07-08 06:44:34 +0000818static void getAMDProcessorTypeAndSubtype(unsigned Family, unsigned Model,
819 unsigned Features, unsigned *Type,
Simon Pilgrima271c542017-05-03 15:42:29 +0000820 unsigned *Subtype) {
821 // FIXME: this poorly matches the generated SubtargetFeatureKV table. There
822 // appears to be no way to generate the wide variety of AMD-specific targets
823 // from the information returned from CPUID.
824 switch (Family) {
825 case 4:
826 *Type = AMD_i486;
827 break;
828 case 5:
829 *Type = AMDPENTIUM;
830 switch (Model) {
831 case 6:
832 case 7:
833 *Subtype = AMDPENTIUM_K6;
834 break; // "k6"
835 case 8:
836 *Subtype = AMDPENTIUM_K62;
837 break; // "k6-2"
838 case 9:
839 case 13:
840 *Subtype = AMDPENTIUM_K63;
841 break; // "k6-3"
842 case 10:
843 *Subtype = AMDPENTIUM_GEODE;
844 break; // "geode"
845 }
846 break;
847 case 6:
848 *Type = AMDATHLON;
Craig Topperf3de5eb2017-07-13 06:34:10 +0000849 if (Features & (1 << FEATURE_SSE)) {
Simon Pilgrima271c542017-05-03 15:42:29 +0000850 *Subtype = AMDATHLON_XP;
851 break; // "athlon-xp"
852 }
Craig Topperf3de5eb2017-07-13 06:34:10 +0000853 *Subtype = AMDATHLON_CLASSIC;
854 break; // "athlon"
Simon Pilgrima271c542017-05-03 15:42:29 +0000855 case 15:
856 *Type = AMDATHLON;
857 if (Features & (1 << FEATURE_SSE3)) {
858 *Subtype = AMDATHLON_K8SSE3;
859 break; // "k8-sse3"
860 }
Craig Topperf3de5eb2017-07-13 06:34:10 +0000861 *Subtype = AMDATHLON_K8;
862 break; // "k8"
Simon Pilgrima271c542017-05-03 15:42:29 +0000863 case 16:
864 *Type = AMDFAM10H; // "amdfam10"
865 switch (Model) {
866 case 2:
867 *Subtype = AMDFAM10H_BARCELONA;
868 break;
869 case 4:
870 *Subtype = AMDFAM10H_SHANGHAI;
871 break;
872 case 8:
873 *Subtype = AMDFAM10H_ISTANBUL;
874 break;
875 }
876 break;
877 case 20:
Craig Topperf3af64e2017-07-12 06:49:57 +0000878 *Type = AMD_BTVER1;
Simon Pilgrima271c542017-05-03 15:42:29 +0000879 break; // "btver1";
880 case 21:
881 *Type = AMDFAM15H;
Craig Topper1f9d3c02017-07-08 06:44:35 +0000882 if (Model >= 0x60 && Model <= 0x7f) {
Simon Pilgrima271c542017-05-03 15:42:29 +0000883 *Subtype = AMDFAM15H_BDVER4;
Craig Topper3db11702017-07-12 06:49:56 +0000884 break; // "bdver4"; 60h-7Fh: Excavator
Simon Pilgrima271c542017-05-03 15:42:29 +0000885 }
886 if (Model >= 0x30 && Model <= 0x3f) {
887 *Subtype = AMDFAM15H_BDVER3;
888 break; // "bdver3"; 30h-3Fh: Steamroller
889 }
890 if (Model >= 0x10 && Model <= 0x1f) {
891 *Subtype = AMDFAM15H_BDVER2;
892 break; // "bdver2"; 10h-1Fh: Piledriver
893 }
894 if (Model <= 0x0f) {
895 *Subtype = AMDFAM15H_BDVER1;
896 break; // "bdver1"; 00h-0Fh: Bulldozer
897 }
898 break;
899 case 22:
Craig Topperf3af64e2017-07-12 06:49:57 +0000900 *Type = AMD_BTVER2;
Simon Pilgrima271c542017-05-03 15:42:29 +0000901 break; // "btver2"
902 case 23:
903 *Type = AMDFAM17H;
Craig Topperffe672d2017-07-08 06:44:36 +0000904 *Subtype = AMDFAM17H_ZNVER1;
Simon Pilgrima271c542017-05-03 15:42:29 +0000905 break;
906 default:
907 break; // "generic"
908 }
909}
910
Craig Topper3a5d0822017-07-12 06:49:58 +0000911static void getAvailableFeatures(unsigned ECX, unsigned EDX, unsigned MaxLeaf,
912 unsigned *FeaturesOut,
913 unsigned *Features2Out) {
Simon Pilgrima271c542017-05-03 15:42:29 +0000914 unsigned Features = 0;
Craig Topper3a5d0822017-07-12 06:49:58 +0000915 unsigned Features2 = 0;
Craig Topperc6bbe4b2017-07-08 05:16:14 +0000916 unsigned EAX, EBX;
Craig Topper3a5d0822017-07-12 06:49:58 +0000917
918 if ((EDX >> 15) & 1)
919 Features |= 1 << FEATURE_CMOV;
920 if ((EDX >> 23) & 1)
921 Features |= 1 << FEATURE_MMX;
922 if ((EDX >> 25) & 1)
923 Features |= 1 << FEATURE_SSE;
924 if ((EDX >> 26) & 1)
925 Features |= 1 << FEATURE_SSE2;
926
927 if ((ECX >> 0) & 1)
928 Features |= 1 << FEATURE_SSE3;
929 if ((ECX >> 1) & 1)
930 Features |= 1 << FEATURE_PCLMUL;
931 if ((ECX >> 9) & 1)
932 Features |= 1 << FEATURE_SSSE3;
933 if ((ECX >> 12) & 1)
934 Features |= 1 << FEATURE_FMA;
935 if ((ECX >> 19) & 1)
936 Features |= 1 << FEATURE_SSE4_1;
937 if ((ECX >> 20) & 1)
938 Features |= 1 << FEATURE_SSE4_2;
939 if ((ECX >> 23) & 1)
940 Features |= 1 << FEATURE_POPCNT;
941 if ((ECX >> 25) & 1)
942 Features |= 1 << FEATURE_AES;
943
944 if ((ECX >> 22) & 1)
945 Features2 |= 1 << (FEATURE_MOVBE - 32);
Simon Pilgrima271c542017-05-03 15:42:29 +0000946
947 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
948 // indicates that the AVX registers will be saved and restored on context
949 // switch, then we have full AVX support.
950 const unsigned AVXBits = (1 << 27) | (1 << 28);
951 bool HasAVX = ((ECX & AVXBits) == AVXBits) && !getX86XCR0(&EAX, &EDX) &&
952 ((EAX & 0x6) == 0x6);
953 bool HasAVX512Save = HasAVX && ((EAX & 0xe0) == 0xe0);
Craig Topper3a5d0822017-07-12 06:49:58 +0000954
955 if (HasAVX)
956 Features |= 1 << FEATURE_AVX;
957
Simon Pilgrima271c542017-05-03 15:42:29 +0000958 bool HasLeaf7 =
959 MaxLeaf >= 0x7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
Craig Topper3a5d0822017-07-12 06:49:58 +0000960
961 if (HasLeaf7 && ((EBX >> 3) & 1))
962 Features |= 1 << FEATURE_BMI;
963 if (HasLeaf7 && ((EBX >> 5) & 1) && HasAVX)
964 Features |= 1 << FEATURE_AVX2;
965 if (HasLeaf7 && ((EBX >> 9) & 1))
966 Features |= 1 << FEATURE_BMI2;
967 if (HasLeaf7 && ((EBX >> 16) & 1) && HasAVX512Save)
968 Features |= 1 << FEATURE_AVX512F;
969 if (HasLeaf7 && ((EBX >> 17) & 1) && HasAVX512Save)
970 Features |= 1 << FEATURE_AVX512DQ;
971 if (HasLeaf7 && ((EBX >> 19) & 1))
972 Features2 |= 1 << (FEATURE_ADX - 32);
973 if (HasLeaf7 && ((EBX >> 21) & 1) && HasAVX512Save)
974 Features |= 1 << FEATURE_AVX512IFMA;
975 if (HasLeaf7 && ((EBX >> 26) & 1) && HasAVX512Save)
976 Features |= 1 << FEATURE_AVX512PF;
977 if (HasLeaf7 && ((EBX >> 27) & 1) && HasAVX512Save)
978 Features |= 1 << FEATURE_AVX512ER;
979 if (HasLeaf7 && ((EBX >> 28) & 1) && HasAVX512Save)
980 Features |= 1 << FEATURE_AVX512CD;
981 if (HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save)
982 Features |= 1 << FEATURE_AVX512BW;
983 if (HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save)
984 Features |= 1 << FEATURE_AVX512VL;
985
986 if (HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save)
987 Features |= 1 << FEATURE_AVX512VBMI;
988 if (HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save)
989 Features |= 1 << FEATURE_AVX512VPOPCNTDQ;
990
991 if (HasLeaf7 && ((EDX >> 2) & 1) && HasAVX512Save)
992 Features |= 1 << FEATURE_AVX5124VNNIW;
993 if (HasLeaf7 && ((EDX >> 3) & 1) && HasAVX512Save)
994 Features |= 1 << FEATURE_AVX5124FMAPS;
Simon Pilgrima271c542017-05-03 15:42:29 +0000995
Craig Topperbb8c7992017-07-08 05:16:13 +0000996 unsigned MaxExtLevel;
997 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
998
999 bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
1000 !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
Craig Topper3a5d0822017-07-12 06:49:58 +00001001 if (HasExtLeaf1 && ((ECX >> 6) & 1))
1002 Features |= 1 << FEATURE_SSE4_A;
1003 if (HasExtLeaf1 && ((ECX >> 11) & 1))
1004 Features |= 1 << FEATURE_XOP;
1005 if (HasExtLeaf1 && ((ECX >> 16) & 1))
1006 Features |= 1 << FEATURE_FMA4;
Craig Topperbb8c7992017-07-08 05:16:13 +00001007
Craig Topper3a5d0822017-07-12 06:49:58 +00001008 if (HasExtLeaf1 && ((EDX >> 29) & 1))
1009 Features2 |= 1 << (FEATURE_EM64T - 32);
1010
1011 *FeaturesOut = Features;
1012 *Features2Out = Features2;
Simon Pilgrima271c542017-05-03 15:42:29 +00001013}
1014
1015StringRef sys::getHostCPUName() {
1016 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
1017 unsigned MaxLeaf, Vendor;
1018
1019#if defined(__GNUC__) || defined(__clang__)
1020 //FIXME: include cpuid.h from clang or copy __get_cpuid_max here
1021 // and simplify it to not invoke __cpuid (like cpu_model.c in
1022 // compiler-rt/lib/builtins/cpu_model.c?
1023 // Opting for the second option.
1024 if(!isCpuIdSupported())
1025 return "generic";
1026#endif
Craig Topperbb8c7992017-07-08 05:16:13 +00001027 if (getX86CpuIDAndInfo(0, &MaxLeaf, &Vendor, &ECX, &EDX) || MaxLeaf < 1)
Simon Pilgrima271c542017-05-03 15:42:29 +00001028 return "generic";
Craig Topperbb8c7992017-07-08 05:16:13 +00001029 getX86CpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
Simon Pilgrima271c542017-05-03 15:42:29 +00001030
1031 unsigned Brand_id = EBX & 0xff;
1032 unsigned Family = 0, Model = 0;
Craig Topper3a5d0822017-07-12 06:49:58 +00001033 unsigned Features = 0, Features2 = 0;
Simon Pilgrima271c542017-05-03 15:42:29 +00001034 detectX86FamilyModel(EAX, &Family, &Model);
Craig Topper3a5d0822017-07-12 06:49:58 +00001035 getAvailableFeatures(ECX, EDX, MaxLeaf, &Features, &Features2);
Simon Pilgrima271c542017-05-03 15:42:29 +00001036
1037 unsigned Type;
1038 unsigned Subtype;
1039
1040 if (Vendor == SIG_INTEL) {
Craig Topper3a5d0822017-07-12 06:49:58 +00001041 getIntelProcessorTypeAndSubtype(Family, Model, Brand_id, Features,
1042 Features2, &Type, &Subtype);
Simon Pilgrima271c542017-05-03 15:42:29 +00001043 switch (Type) {
1044 case INTEL_i386:
1045 return "i386";
1046 case INTEL_i486:
1047 return "i486";
1048 case INTEL_PENTIUM:
1049 if (Subtype == INTEL_PENTIUM_MMX)
1050 return "pentium-mmx";
1051 return "pentium";
1052 case INTEL_PENTIUM_PRO:
1053 return "pentiumpro";
1054 case INTEL_PENTIUM_II:
1055 return "pentium2";
1056 case INTEL_PENTIUM_III:
1057 return "pentium3";
1058 case INTEL_PENTIUM_IV:
1059 return "pentium4";
1060 case INTEL_PENTIUM_M:
1061 return "pentium-m";
1062 case INTEL_CORE_DUO:
1063 return "yonah";
1064 case INTEL_CORE2:
1065 switch (Subtype) {
1066 case INTEL_CORE2_65:
1067 return "core2";
1068 case INTEL_CORE2_45:
1069 return "penryn";
1070 default:
Craig Topper3db11702017-07-12 06:49:56 +00001071 llvm_unreachable("Unexpected subtype!");
Simon Pilgrima271c542017-05-03 15:42:29 +00001072 }
1073 case INTEL_COREI7:
1074 switch (Subtype) {
1075 case INTEL_COREI7_NEHALEM:
1076 return "nehalem";
1077 case INTEL_COREI7_WESTMERE:
1078 return "westmere";
1079 case INTEL_COREI7_SANDYBRIDGE:
1080 return "sandybridge";
1081 case INTEL_COREI7_IVYBRIDGE:
1082 return "ivybridge";
1083 case INTEL_COREI7_HASWELL:
1084 return "haswell";
1085 case INTEL_COREI7_BROADWELL:
1086 return "broadwell";
1087 case INTEL_COREI7_SKYLAKE:
1088 return "skylake";
1089 case INTEL_COREI7_SKYLAKE_AVX512:
1090 return "skylake-avx512";
1091 default:
Craig Topper3db11702017-07-12 06:49:56 +00001092 llvm_unreachable("Unexpected subtype!");
Simon Pilgrima271c542017-05-03 15:42:29 +00001093 }
Craig Topperf3af64e2017-07-12 06:49:57 +00001094 case INTEL_BONNELL:
1095 return "bonnell";
1096 case INTEL_SILVERMONT:
1097 return "silvermont";
1098 case INTEL_GOLDMONT:
1099 return "goldmont";
1100 case INTEL_KNL:
1101 return "knl";
Simon Pilgrima271c542017-05-03 15:42:29 +00001102 case INTEL_X86_64:
1103 return "x86-64";
1104 case INTEL_NOCONA:
1105 return "nocona";
1106 case INTEL_PRESCOTT:
1107 return "prescott";
1108 default:
Craig Topper3db11702017-07-12 06:49:56 +00001109 break;
Simon Pilgrima271c542017-05-03 15:42:29 +00001110 }
1111 } else if (Vendor == SIG_AMD) {
1112 getAMDProcessorTypeAndSubtype(Family, Model, Features, &Type, &Subtype);
1113 switch (Type) {
1114 case AMD_i486:
1115 return "i486";
1116 case AMDPENTIUM:
1117 switch (Subtype) {
1118 case AMDPENTIUM_K6:
1119 return "k6";
1120 case AMDPENTIUM_K62:
1121 return "k6-2";
1122 case AMDPENTIUM_K63:
1123 return "k6-3";
1124 case AMDPENTIUM_GEODE:
1125 return "geode";
1126 default:
1127 return "pentium";
1128 }
1129 case AMDATHLON:
1130 switch (Subtype) {
Craig Topperf3de5eb2017-07-13 06:34:10 +00001131 case AMDATHLON_CLASSIC:
1132 return "athlon";
Simon Pilgrima271c542017-05-03 15:42:29 +00001133 case AMDATHLON_XP:
1134 return "athlon-xp";
Craig Topperf3de5eb2017-07-13 06:34:10 +00001135 case AMDATHLON_K8:
1136 return "k8";
Simon Pilgrima271c542017-05-03 15:42:29 +00001137 case AMDATHLON_K8SSE3:
1138 return "k8-sse3";
Simon Pilgrima271c542017-05-03 15:42:29 +00001139 default:
Craig Topper3db11702017-07-12 06:49:56 +00001140 llvm_unreachable("Unexpected subtype!");
Simon Pilgrima271c542017-05-03 15:42:29 +00001141 }
1142 case AMDFAM10H:
Simon Pilgrima271c542017-05-03 15:42:29 +00001143 return "amdfam10";
Craig Topperf3af64e2017-07-12 06:49:57 +00001144 case AMD_BTVER1:
Simon Pilgrima271c542017-05-03 15:42:29 +00001145 return "btver1";
1146 case AMDFAM15H:
1147 switch (Subtype) {
Craig Topper3db11702017-07-12 06:49:56 +00001148 default: // There are gaps in the subtype detection.
Simon Pilgrima271c542017-05-03 15:42:29 +00001149 case AMDFAM15H_BDVER1:
1150 return "bdver1";
1151 case AMDFAM15H_BDVER2:
1152 return "bdver2";
1153 case AMDFAM15H_BDVER3:
1154 return "bdver3";
1155 case AMDFAM15H_BDVER4:
1156 return "bdver4";
Simon Pilgrima271c542017-05-03 15:42:29 +00001157 }
Craig Topperf3af64e2017-07-12 06:49:57 +00001158 case AMD_BTVER2:
Craig Topper3db11702017-07-12 06:49:56 +00001159 return "btver2";
Simon Pilgrima271c542017-05-03 15:42:29 +00001160 case AMDFAM17H:
Craig Topper3db11702017-07-12 06:49:56 +00001161 return "znver1";
Simon Pilgrima271c542017-05-03 15:42:29 +00001162 default:
Craig Topper3db11702017-07-12 06:49:56 +00001163 break;
Simon Pilgrima271c542017-05-03 15:42:29 +00001164 }
1165 }
1166 return "generic";
1167}
1168
1169#elif defined(__APPLE__) && (defined(__ppc__) || defined(__powerpc__))
1170StringRef sys::getHostCPUName() {
1171 host_basic_info_data_t hostInfo;
1172 mach_msg_type_number_t infoCount;
1173
1174 infoCount = HOST_BASIC_INFO_COUNT;
1175 host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo,
1176 &infoCount);
1177
1178 if (hostInfo.cpu_type != CPU_TYPE_POWERPC)
1179 return "generic";
1180
1181 switch (hostInfo.cpu_subtype) {
1182 case CPU_SUBTYPE_POWERPC_601:
1183 return "601";
1184 case CPU_SUBTYPE_POWERPC_602:
1185 return "602";
1186 case CPU_SUBTYPE_POWERPC_603:
1187 return "603";
1188 case CPU_SUBTYPE_POWERPC_603e:
1189 return "603e";
1190 case CPU_SUBTYPE_POWERPC_603ev:
1191 return "603ev";
1192 case CPU_SUBTYPE_POWERPC_604:
1193 return "604";
1194 case CPU_SUBTYPE_POWERPC_604e:
1195 return "604e";
1196 case CPU_SUBTYPE_POWERPC_620:
1197 return "620";
1198 case CPU_SUBTYPE_POWERPC_750:
1199 return "750";
1200 case CPU_SUBTYPE_POWERPC_7400:
1201 return "7400";
1202 case CPU_SUBTYPE_POWERPC_7450:
1203 return "7450";
1204 case CPU_SUBTYPE_POWERPC_970:
1205 return "970";
1206 default:;
1207 }
1208
1209 return "generic";
1210}
1211#elif defined(__linux__) && (defined(__ppc__) || defined(__powerpc__))
1212StringRef sys::getHostCPUName() {
1213 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1214 const StringRef& Content = P ? P->getBuffer() : "";
1215 return detail::getHostCPUNameForPowerPC(Content);
1216}
1217#elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1218StringRef sys::getHostCPUName() {
1219 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1220 const StringRef& Content = P ? P->getBuffer() : "";
1221 return detail::getHostCPUNameForARM(Content);
1222}
1223#elif defined(__linux__) && defined(__s390x__)
1224StringRef sys::getHostCPUName() {
1225 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1226 const StringRef& Content = P ? P->getBuffer() : "";
1227 return detail::getHostCPUNameForS390x(Content);
1228}
1229#else
1230StringRef sys::getHostCPUName() { return "generic"; }
1231#endif
1232
1233#if defined(__linux__) && defined(__x86_64__)
1234// On Linux, the number of physical cores can be computed from /proc/cpuinfo,
1235// using the number of unique physical/core id pairs. The following
1236// implementation reads the /proc/cpuinfo format on an x86_64 system.
1237static int computeHostNumPhysicalCores() {
1238 // Read /proc/cpuinfo as a stream (until EOF reached). It cannot be
1239 // mmapped because it appears to have 0 size.
1240 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
1241 llvm::MemoryBuffer::getFileAsStream("/proc/cpuinfo");
1242 if (std::error_code EC = Text.getError()) {
1243 llvm::errs() << "Can't read "
1244 << "/proc/cpuinfo: " << EC.message() << "\n";
1245 return -1;
1246 }
1247 SmallVector<StringRef, 8> strs;
1248 (*Text)->getBuffer().split(strs, "\n", /*MaxSplit=*/-1,
1249 /*KeepEmpty=*/false);
1250 int CurPhysicalId = -1;
1251 int CurCoreId = -1;
1252 SmallSet<std::pair<int, int>, 32> UniqueItems;
1253 for (auto &Line : strs) {
1254 Line = Line.trim();
1255 if (!Line.startswith("physical id") && !Line.startswith("core id"))
1256 continue;
1257 std::pair<StringRef, StringRef> Data = Line.split(':');
1258 auto Name = Data.first.trim();
1259 auto Val = Data.second.trim();
1260 if (Name == "physical id") {
1261 assert(CurPhysicalId == -1 &&
1262 "Expected a core id before seeing another physical id");
1263 Val.getAsInteger(10, CurPhysicalId);
1264 }
1265 if (Name == "core id") {
1266 assert(CurCoreId == -1 &&
1267 "Expected a physical id before seeing another core id");
1268 Val.getAsInteger(10, CurCoreId);
1269 }
1270 if (CurPhysicalId != -1 && CurCoreId != -1) {
1271 UniqueItems.insert(std::make_pair(CurPhysicalId, CurCoreId));
1272 CurPhysicalId = -1;
1273 CurCoreId = -1;
1274 }
1275 }
1276 return UniqueItems.size();
1277}
1278#elif defined(__APPLE__) && defined(__x86_64__)
1279#include <sys/param.h>
1280#include <sys/sysctl.h>
1281
1282// Gets the number of *physical cores* on the machine.
1283static int computeHostNumPhysicalCores() {
1284 uint32_t count;
1285 size_t len = sizeof(count);
1286 sysctlbyname("hw.physicalcpu", &count, &len, NULL, 0);
1287 if (count < 1) {
1288 int nm[2];
1289 nm[0] = CTL_HW;
1290 nm[1] = HW_AVAILCPU;
1291 sysctl(nm, 2, &count, &len, NULL, 0);
1292 if (count < 1)
1293 return -1;
1294 }
1295 return count;
1296}
1297#else
1298// On other systems, return -1 to indicate unknown.
1299static int computeHostNumPhysicalCores() { return -1; }
1300#endif
1301
1302int sys::getHostNumPhysicalCores() {
1303 static int NumCores = computeHostNumPhysicalCores();
1304 return NumCores;
1305}
1306
1307#if defined(__i386__) || defined(_M_IX86) || \
1308 defined(__x86_64__) || defined(_M_X64)
1309bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
1310 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
1311 unsigned MaxLevel;
1312 union {
1313 unsigned u[3];
1314 char c[12];
1315 } text;
1316
1317 if (getX86CpuIDAndInfo(0, &MaxLevel, text.u + 0, text.u + 2, text.u + 1) ||
1318 MaxLevel < 1)
1319 return false;
1320
1321 getX86CpuIDAndInfo(1, &EAX, &EBX, &ECX, &EDX);
1322
1323 Features["cmov"] = (EDX >> 15) & 1;
1324 Features["mmx"] = (EDX >> 23) & 1;
1325 Features["sse"] = (EDX >> 25) & 1;
1326 Features["sse2"] = (EDX >> 26) & 1;
1327 Features["sse3"] = (ECX >> 0) & 1;
1328 Features["ssse3"] = (ECX >> 9) & 1;
1329 Features["sse4.1"] = (ECX >> 19) & 1;
1330 Features["sse4.2"] = (ECX >> 20) & 1;
1331
1332 Features["pclmul"] = (ECX >> 1) & 1;
1333 Features["cx16"] = (ECX >> 13) & 1;
1334 Features["movbe"] = (ECX >> 22) & 1;
1335 Features["popcnt"] = (ECX >> 23) & 1;
1336 Features["aes"] = (ECX >> 25) & 1;
1337 Features["rdrnd"] = (ECX >> 30) & 1;
1338
1339 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
1340 // indicates that the AVX registers will be saved and restored on context
1341 // switch, then we have full AVX support.
1342 bool HasAVXSave = ((ECX >> 27) & 1) && ((ECX >> 28) & 1) &&
1343 !getX86XCR0(&EAX, &EDX) && ((EAX & 0x6) == 0x6);
1344 Features["avx"] = HasAVXSave;
1345 Features["fma"] = HasAVXSave && (ECX >> 12) & 1;
1346 Features["f16c"] = HasAVXSave && (ECX >> 29) & 1;
1347
1348 // Only enable XSAVE if OS has enabled support for saving YMM state.
1349 Features["xsave"] = HasAVXSave && (ECX >> 26) & 1;
1350
1351 // AVX512 requires additional context to be saved by the OS.
1352 bool HasAVX512Save = HasAVXSave && ((EAX & 0xe0) == 0xe0);
1353
1354 unsigned MaxExtLevel;
1355 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
1356
1357 bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
1358 !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
1359 Features["lzcnt"] = HasExtLeaf1 && ((ECX >> 5) & 1);
1360 Features["sse4a"] = HasExtLeaf1 && ((ECX >> 6) & 1);
1361 Features["prfchw"] = HasExtLeaf1 && ((ECX >> 8) & 1);
1362 Features["xop"] = HasExtLeaf1 && ((ECX >> 11) & 1) && HasAVXSave;
Simon Pilgrim99b925b2017-05-03 15:51:39 +00001363 Features["lwp"] = HasExtLeaf1 && ((ECX >> 15) & 1);
Simon Pilgrima271c542017-05-03 15:42:29 +00001364 Features["fma4"] = HasExtLeaf1 && ((ECX >> 16) & 1) && HasAVXSave;
1365 Features["tbm"] = HasExtLeaf1 && ((ECX >> 21) & 1);
1366 Features["mwaitx"] = HasExtLeaf1 && ((ECX >> 29) & 1);
1367
1368 bool HasExtLeaf8 = MaxExtLevel >= 0x80000008 &&
1369 !getX86CpuIDAndInfoEx(0x80000008,0x0, &EAX, &EBX, &ECX, &EDX);
1370 Features["clzero"] = HasExtLeaf8 && ((EBX >> 0) & 1);
1371
1372 bool HasLeaf7 =
1373 MaxLevel >= 7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
1374
1375 // AVX2 is only supported if we have the OS save support from AVX.
1376 Features["avx2"] = HasAVXSave && HasLeaf7 && ((EBX >> 5) & 1);
1377
1378 Features["fsgsbase"] = HasLeaf7 && ((EBX >> 0) & 1);
1379 Features["sgx"] = HasLeaf7 && ((EBX >> 2) & 1);
1380 Features["bmi"] = HasLeaf7 && ((EBX >> 3) & 1);
1381 Features["bmi2"] = HasLeaf7 && ((EBX >> 8) & 1);
1382 Features["rtm"] = HasLeaf7 && ((EBX >> 11) & 1);
1383 Features["rdseed"] = HasLeaf7 && ((EBX >> 18) & 1);
1384 Features["adx"] = HasLeaf7 && ((EBX >> 19) & 1);
1385 Features["clflushopt"] = HasLeaf7 && ((EBX >> 23) & 1);
1386 Features["clwb"] = HasLeaf7 && ((EBX >> 24) & 1);
1387 Features["sha"] = HasLeaf7 && ((EBX >> 29) & 1);
1388
1389 // AVX512 is only supported if the OS supports the context save for it.
1390 Features["avx512f"] = HasLeaf7 && ((EBX >> 16) & 1) && HasAVX512Save;
1391 Features["avx512dq"] = HasLeaf7 && ((EBX >> 17) & 1) && HasAVX512Save;
1392 Features["avx512ifma"] = HasLeaf7 && ((EBX >> 21) & 1) && HasAVX512Save;
1393 Features["avx512pf"] = HasLeaf7 && ((EBX >> 26) & 1) && HasAVX512Save;
1394 Features["avx512er"] = HasLeaf7 && ((EBX >> 27) & 1) && HasAVX512Save;
1395 Features["avx512cd"] = HasLeaf7 && ((EBX >> 28) & 1) && HasAVX512Save;
1396 Features["avx512bw"] = HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save;
1397 Features["avx512vl"] = HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save;
1398
1399 Features["prefetchwt1"] = HasLeaf7 && (ECX & 1);
1400 Features["avx512vbmi"] = HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save;
Oren Ben Simhonf3aab2f2017-05-28 11:26:11 +00001401 Features["avx512vpopcntdq"] = HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save;
Simon Pilgrima271c542017-05-03 15:42:29 +00001402 // Enable protection keys
1403 Features["pku"] = HasLeaf7 && ((ECX >> 4) & 1);
1404
1405 bool HasLeafD = MaxLevel >= 0xd &&
1406 !getX86CpuIDAndInfoEx(0xd, 0x1, &EAX, &EBX, &ECX, &EDX);
1407
1408 // Only enable XSAVE if OS has enabled support for saving YMM state.
1409 Features["xsaveopt"] = HasAVXSave && HasLeafD && ((EAX >> 0) & 1);
1410 Features["xsavec"] = HasAVXSave && HasLeafD && ((EAX >> 1) & 1);
1411 Features["xsaves"] = HasAVXSave && HasLeafD && ((EAX >> 3) & 1);
1412
1413 return true;
1414}
1415#elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1416bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
1417 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1418 if (!P)
1419 return false;
1420
1421 SmallVector<StringRef, 32> Lines;
1422 P->getBuffer().split(Lines, "\n");
1423
1424 SmallVector<StringRef, 32> CPUFeatures;
1425
1426 // Look for the CPU features.
1427 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
1428 if (Lines[I].startswith("Features")) {
1429 Lines[I].split(CPUFeatures, ' ');
1430 break;
1431 }
1432
1433#if defined(__aarch64__)
1434 // Keep track of which crypto features we have seen
1435 enum { CAP_AES = 0x1, CAP_PMULL = 0x2, CAP_SHA1 = 0x4, CAP_SHA2 = 0x8 };
1436 uint32_t crypto = 0;
1437#endif
1438
1439 for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) {
1440 StringRef LLVMFeatureStr = StringSwitch<StringRef>(CPUFeatures[I])
1441#if defined(__aarch64__)
1442 .Case("asimd", "neon")
1443 .Case("fp", "fp-armv8")
1444 .Case("crc32", "crc")
1445#else
1446 .Case("half", "fp16")
1447 .Case("neon", "neon")
1448 .Case("vfpv3", "vfp3")
1449 .Case("vfpv3d16", "d16")
1450 .Case("vfpv4", "vfp4")
1451 .Case("idiva", "hwdiv-arm")
1452 .Case("idivt", "hwdiv")
1453#endif
1454 .Default("");
1455
1456#if defined(__aarch64__)
1457 // We need to check crypto separately since we need all of the crypto
1458 // extensions to enable the subtarget feature
1459 if (CPUFeatures[I] == "aes")
1460 crypto |= CAP_AES;
1461 else if (CPUFeatures[I] == "pmull")
1462 crypto |= CAP_PMULL;
1463 else if (CPUFeatures[I] == "sha1")
1464 crypto |= CAP_SHA1;
1465 else if (CPUFeatures[I] == "sha2")
1466 crypto |= CAP_SHA2;
1467#endif
1468
1469 if (LLVMFeatureStr != "")
1470 Features[LLVMFeatureStr] = true;
1471 }
1472
1473#if defined(__aarch64__)
1474 // If we have all crypto bits we can add the feature
1475 if (crypto == (CAP_AES | CAP_PMULL | CAP_SHA1 | CAP_SHA2))
1476 Features["crypto"] = true;
1477#endif
1478
1479 return true;
1480}
1481#else
1482bool sys::getHostCPUFeatures(StringMap<bool> &Features) { return false; }
1483#endif
1484
1485std::string sys::getProcessTriple() {
Alex Lorenz3803df32017-07-07 09:53:47 +00001486 std::string TargetTripleString = updateTripleOSVersion(LLVM_HOST_TRIPLE);
1487 Triple PT(Triple::normalize(TargetTripleString));
Simon Pilgrima271c542017-05-03 15:42:29 +00001488
1489 if (sizeof(void *) == 8 && PT.isArch32Bit())
1490 PT = PT.get64BitArchVariant();
1491 if (sizeof(void *) == 4 && PT.isArch64Bit())
1492 PT = PT.get32BitArchVariant();
1493
1494 return PT.str();
1495}