blob: 00230d1eb7096b0ca61862d3d2d8388fa88cae99 [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,
339 AMDATHLON_TBIRD,
340 AMDATHLON_MP,
341 AMDATHLON_XP,
342 AMDATHLON_K8SSE3,
343 AMDATHLON_OPTERON,
344 AMDATHLON_FX,
345 AMDATHLON_64,
Simon Pilgrima271c542017-05-03 15:42:29 +0000346 CPU_SUBTYPE_MAX
347};
348
349enum ProcessorFeatures {
350 FEATURE_CMOV = 0,
351 FEATURE_MMX,
352 FEATURE_POPCNT,
353 FEATURE_SSE,
354 FEATURE_SSE2,
355 FEATURE_SSE3,
356 FEATURE_SSSE3,
357 FEATURE_SSE4_1,
358 FEATURE_SSE4_2,
359 FEATURE_AVX,
360 FEATURE_AVX2,
361 FEATURE_AVX512,
362 FEATURE_AVX512SAVE,
363 FEATURE_MOVBE,
364 FEATURE_ADX,
365 FEATURE_EM64T
366};
367
368// The check below for i386 was copied from clang's cpuid.h (__get_cpuid_max).
369// Check motivated by bug reports for OpenSSL crashing on CPUs without CPUID
370// support. Consequently, for i386, the presence of CPUID is checked first
371// via the corresponding eflags bit.
372// Removal of cpuid.h header motivated by PR30384
373// Header cpuid.h and method __get_cpuid_max are not used in llvm, clang, openmp
374// or test-suite, but are used in external projects e.g. libstdcxx
375static bool isCpuIdSupported() {
376#if defined(__GNUC__) || defined(__clang__)
377#if defined(__i386__)
378 int __cpuid_supported;
379 __asm__(" pushfl\n"
380 " popl %%eax\n"
381 " movl %%eax,%%ecx\n"
382 " xorl $0x00200000,%%eax\n"
383 " pushl %%eax\n"
384 " popfl\n"
385 " pushfl\n"
386 " popl %%eax\n"
387 " movl $0,%0\n"
388 " cmpl %%eax,%%ecx\n"
389 " je 1f\n"
390 " movl $1,%0\n"
391 "1:"
392 : "=r"(__cpuid_supported)
393 :
394 : "eax", "ecx");
395 if (!__cpuid_supported)
396 return false;
397#endif
398 return true;
399#endif
400 return true;
401}
402
403/// getX86CpuIDAndInfo - Execute the specified cpuid and return the 4 values in
404/// the specified arguments. If we can't run cpuid on the host, return true.
405static bool getX86CpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
406 unsigned *rECX, unsigned *rEDX) {
Simon Pilgrima271c542017-05-03 15:42:29 +0000407#if defined(__GNUC__) || defined(__clang__)
408#if defined(__x86_64__)
409 // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
410 // FIXME: should we save this for Clang?
411 __asm__("movq\t%%rbx, %%rsi\n\t"
412 "cpuid\n\t"
413 "xchgq\t%%rbx, %%rsi\n\t"
414 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
415 : "a"(value));
Craig Topper1efd10a2017-07-10 06:04:11 +0000416 return false;
Simon Pilgrima271c542017-05-03 15:42:29 +0000417#elif defined(__i386__)
418 __asm__("movl\t%%ebx, %%esi\n\t"
419 "cpuid\n\t"
420 "xchgl\t%%ebx, %%esi\n\t"
421 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
422 : "a"(value));
Craig Topper1efd10a2017-07-10 06:04:11 +0000423 return false;
Simon Pilgrima271c542017-05-03 15:42:29 +0000424#else
Craig Topper1efd10a2017-07-10 06:04:11 +0000425 return true;
Simon Pilgrima271c542017-05-03 15:42:29 +0000426#endif
427#elif defined(_MSC_VER)
428 // The MSVC intrinsic is portable across x86 and x64.
429 int registers[4];
430 __cpuid(registers, value);
431 *rEAX = registers[0];
432 *rEBX = registers[1];
433 *rECX = registers[2];
434 *rEDX = registers[3];
Simon Pilgrima271c542017-05-03 15:42:29 +0000435 return false;
436#else
437 return true;
438#endif
439}
440
441/// getX86CpuIDAndInfoEx - Execute the specified cpuid with subleaf and return
442/// the 4 values in the specified arguments. If we can't run cpuid on the host,
443/// return true.
444static bool getX86CpuIDAndInfoEx(unsigned value, unsigned subleaf,
445 unsigned *rEAX, unsigned *rEBX, unsigned *rECX,
446 unsigned *rEDX) {
Simon Pilgrima271c542017-05-03 15:42:29 +0000447#if defined(__x86_64__) || defined(_M_X64)
448#if defined(__GNUC__) || defined(__clang__)
Craig Topperada983a2017-07-10 06:09:22 +0000449 // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
Simon Pilgrima271c542017-05-03 15:42:29 +0000450 // FIXME: should we save this for Clang?
451 __asm__("movq\t%%rbx, %%rsi\n\t"
452 "cpuid\n\t"
453 "xchgq\t%%rbx, %%rsi\n\t"
454 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
455 : "a"(value), "c"(subleaf));
Craig Topper1efd10a2017-07-10 06:04:11 +0000456 return false;
Simon Pilgrima271c542017-05-03 15:42:29 +0000457#elif defined(_MSC_VER)
458 int registers[4];
459 __cpuidex(registers, value, subleaf);
460 *rEAX = registers[0];
461 *rEBX = registers[1];
462 *rECX = registers[2];
463 *rEDX = registers[3];
Craig Topper1efd10a2017-07-10 06:04:11 +0000464 return false;
465#else
466 return true;
Simon Pilgrima271c542017-05-03 15:42:29 +0000467#endif
468#elif defined(__i386__) || defined(_M_IX86)
469#if defined(__GNUC__) || defined(__clang__)
470 __asm__("movl\t%%ebx, %%esi\n\t"
471 "cpuid\n\t"
472 "xchgl\t%%ebx, %%esi\n\t"
473 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
474 : "a"(value), "c"(subleaf));
Craig Topper1efd10a2017-07-10 06:04:11 +0000475 return false;
Simon Pilgrima271c542017-05-03 15:42:29 +0000476#elif defined(_MSC_VER)
477 __asm {
478 mov eax,value
479 mov ecx,subleaf
480 cpuid
481 mov esi,rEAX
482 mov dword ptr [esi],eax
483 mov esi,rEBX
484 mov dword ptr [esi],ebx
485 mov esi,rECX
486 mov dword ptr [esi],ecx
487 mov esi,rEDX
488 mov dword ptr [esi],edx
489 }
Simon Pilgrima271c542017-05-03 15:42:29 +0000490 return false;
491#else
492 return true;
493#endif
Craig Topper1efd10a2017-07-10 06:04:11 +0000494#else
495 return true;
496#endif
Simon Pilgrima271c542017-05-03 15:42:29 +0000497}
498
Craig Topperf3af64e2017-07-12 06:49:57 +0000499// Read control register 0 (XCR0). Used to detect features such as AVX.
Simon Pilgrima271c542017-05-03 15:42:29 +0000500static bool getX86XCR0(unsigned *rEAX, unsigned *rEDX) {
501#if defined(__GNUC__) || defined(__clang__)
502 // Check xgetbv; this uses a .byte sequence instead of the instruction
503 // directly because older assemblers do not include support for xgetbv and
504 // there is no easy way to conditionally compile based on the assembler used.
505 __asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(*rEAX), "=d"(*rEDX) : "c"(0));
506 return false;
507#elif defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK)
508 unsigned long long Result = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
509 *rEAX = Result;
510 *rEDX = Result >> 32;
511 return false;
512#else
513 return true;
514#endif
515}
516
517static void detectX86FamilyModel(unsigned EAX, unsigned *Family,
518 unsigned *Model) {
519 *Family = (EAX >> 8) & 0xf; // Bits 8 - 11
520 *Model = (EAX >> 4) & 0xf; // Bits 4 - 7
521 if (*Family == 6 || *Family == 0xf) {
522 if (*Family == 0xf)
523 // Examine extended family ID if family ID is F.
524 *Family += (EAX >> 20) & 0xff; // Bits 20 - 27
525 // Examine extended model ID if family ID is 6 or F.
526 *Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
527 }
528}
529
530static void
Craig Topperc6bbe4b2017-07-08 05:16:14 +0000531getIntelProcessorTypeAndSubtype(unsigned Family, unsigned Model,
532 unsigned Brand_id, unsigned Features,
Simon Pilgrima271c542017-05-03 15:42:29 +0000533 unsigned *Type, unsigned *Subtype) {
534 if (Brand_id != 0)
535 return;
536 switch (Family) {
537 case 3:
538 *Type = INTEL_i386;
539 break;
540 case 4:
541 switch (Model) {
542 case 0: // Intel486 DX processors
543 case 1: // Intel486 DX processors
544 case 2: // Intel486 SX processors
545 case 3: // Intel487 processors, IntelDX2 OverDrive processors,
546 // IntelDX2 processors
547 case 4: // Intel486 SL processor
548 case 5: // IntelSX2 processors
549 case 7: // Write-Back Enhanced IntelDX2 processors
550 case 8: // IntelDX4 OverDrive processors, IntelDX4 processors
551 default:
552 *Type = INTEL_i486;
553 break;
554 }
555 break;
556 case 5:
557 switch (Model) {
558 case 1: // Pentium OverDrive processor for Pentium processor (60, 66),
559 // Pentium processors (60, 66)
560 case 2: // Pentium OverDrive processor for Pentium processor (75, 90,
561 // 100, 120, 133), Pentium processors (75, 90, 100, 120, 133,
562 // 150, 166, 200)
563 case 3: // Pentium OverDrive processors for Intel486 processor-based
564 // systems
565 *Type = INTEL_PENTIUM;
566 break;
567 case 4: // Pentium OverDrive processor with MMX technology for Pentium
568 // processor (75, 90, 100, 120, 133), Pentium processor with
569 // MMX technology (166, 200)
570 *Type = INTEL_PENTIUM;
571 *Subtype = INTEL_PENTIUM_MMX;
572 break;
573 default:
574 *Type = INTEL_PENTIUM;
575 break;
576 }
577 break;
578 case 6:
579 switch (Model) {
580 case 0x01: // Pentium Pro processor
581 *Type = INTEL_PENTIUM_PRO;
582 break;
583 case 0x03: // Intel Pentium II OverDrive processor, Pentium II processor,
584 // model 03
585 case 0x05: // Pentium II processor, model 05, Pentium II Xeon processor,
586 // model 05, and Intel Celeron processor, model 05
587 case 0x06: // Celeron processor, model 06
588 *Type = INTEL_PENTIUM_II;
589 break;
590 case 0x07: // Pentium III processor, model 07, and Pentium III Xeon
591 // processor, model 07
592 case 0x08: // Pentium III processor, model 08, Pentium III Xeon processor,
593 // model 08, and Celeron processor, model 08
594 case 0x0a: // Pentium III Xeon processor, model 0Ah
595 case 0x0b: // Pentium III processor, model 0Bh
596 *Type = INTEL_PENTIUM_III;
597 break;
598 case 0x09: // Intel Pentium M processor, Intel Celeron M processor model 09.
599 case 0x0d: // Intel Pentium M processor, Intel Celeron M processor, model
600 // 0Dh. All processors are manufactured using the 90 nm process.
601 case 0x15: // Intel EP80579 Integrated Processor and Intel EP80579
602 // Integrated Processor with Intel QuickAssist Technology
603 *Type = INTEL_PENTIUM_M;
604 break;
605 case 0x0e: // Intel Core Duo processor, Intel Core Solo processor, model
606 // 0Eh. All processors are manufactured using the 65 nm process.
607 *Type = INTEL_CORE_DUO;
608 break; // yonah
609 case 0x0f: // Intel Core 2 Duo processor, Intel Core 2 Duo mobile
610 // processor, Intel Core 2 Quad processor, Intel Core 2 Quad
611 // mobile processor, Intel Core 2 Extreme processor, Intel
612 // Pentium Dual-Core processor, Intel Xeon processor, model
613 // 0Fh. All processors are manufactured using the 65 nm process.
614 case 0x16: // Intel Celeron processor model 16h. All processors are
615 // manufactured using the 65 nm process
616 *Type = INTEL_CORE2; // "core2"
617 *Subtype = INTEL_CORE2_65;
618 break;
619 case 0x17: // Intel Core 2 Extreme processor, Intel Xeon processor, model
620 // 17h. All processors are manufactured using the 45 nm process.
621 //
622 // 45nm: Penryn , Wolfdale, Yorkfield (XE)
623 case 0x1d: // Intel Xeon processor MP. All processors are manufactured using
624 // the 45 nm process.
625 *Type = INTEL_CORE2; // "penryn"
626 *Subtype = INTEL_CORE2_45;
627 break;
628 case 0x1a: // Intel Core i7 processor and Intel Xeon processor. All
629 // processors are manufactured using the 45 nm process.
630 case 0x1e: // Intel(R) Core(TM) i7 CPU 870 @ 2.93GHz.
631 // As found in a Summer 2010 model iMac.
632 case 0x1f:
633 case 0x2e: // Nehalem EX
634 *Type = INTEL_COREI7; // "nehalem"
635 *Subtype = INTEL_COREI7_NEHALEM;
636 break;
637 case 0x25: // Intel Core i7, laptop version.
638 case 0x2c: // Intel Core i7 processor and Intel Xeon processor. All
639 // processors are manufactured using the 32 nm process.
640 case 0x2f: // Westmere EX
641 *Type = INTEL_COREI7; // "westmere"
642 *Subtype = INTEL_COREI7_WESTMERE;
643 break;
644 case 0x2a: // Intel Core i7 processor. All processors are manufactured
645 // using the 32 nm process.
646 case 0x2d:
647 *Type = INTEL_COREI7; //"sandybridge"
648 *Subtype = INTEL_COREI7_SANDYBRIDGE;
649 break;
650 case 0x3a:
651 case 0x3e: // Ivy Bridge EP
652 *Type = INTEL_COREI7; // "ivybridge"
653 *Subtype = INTEL_COREI7_IVYBRIDGE;
654 break;
655
656 // Haswell:
657 case 0x3c:
658 case 0x3f:
659 case 0x45:
660 case 0x46:
661 *Type = INTEL_COREI7; // "haswell"
662 *Subtype = INTEL_COREI7_HASWELL;
663 break;
664
665 // Broadwell:
666 case 0x3d:
667 case 0x47:
668 case 0x4f:
669 case 0x56:
670 *Type = INTEL_COREI7; // "broadwell"
671 *Subtype = INTEL_COREI7_BROADWELL;
672 break;
673
674 // Skylake:
675 case 0x4e: // Skylake mobile
676 case 0x5e: // Skylake desktop
677 case 0x8e: // Kaby Lake mobile
678 case 0x9e: // Kaby Lake desktop
679 *Type = INTEL_COREI7; // "skylake"
680 *Subtype = INTEL_COREI7_SKYLAKE;
681 break;
682
683 // Skylake Xeon:
684 case 0x55:
685 *Type = INTEL_COREI7;
Craig Topper52cec382017-07-09 07:26:14 +0000686 *Subtype = INTEL_COREI7_SKYLAKE_AVX512; // "skylake-avx512"
Simon Pilgrima271c542017-05-03 15:42:29 +0000687 break;
688
689 case 0x1c: // Most 45 nm Intel Atom processors
690 case 0x26: // 45 nm Atom Lincroft
691 case 0x27: // 32 nm Atom Medfield
692 case 0x35: // 32 nm Atom Midview
693 case 0x36: // 32 nm Atom Midview
Craig Topperf3af64e2017-07-12 06:49:57 +0000694 *Type = INTEL_BONNELL;
Simon Pilgrima271c542017-05-03 15:42:29 +0000695 break; // "bonnell"
696
697 // Atom Silvermont codes from the Intel software optimization guide.
698 case 0x37:
699 case 0x4a:
700 case 0x4d:
701 case 0x5a:
702 case 0x5d:
703 case 0x4c: // really airmont
Craig Topperf3af64e2017-07-12 06:49:57 +0000704 *Type = INTEL_SILVERMONT;
Simon Pilgrima271c542017-05-03 15:42:29 +0000705 break; // "silvermont"
Michael Zuckerman4bcb9c32017-06-29 10:00:33 +0000706 // Goldmont:
707 case 0x5c:
708 case 0x5f:
Craig Topperf3af64e2017-07-12 06:49:57 +0000709 *Type = INTEL_GOLDMONT;
Michael Zuckerman4bcb9c32017-06-29 10:00:33 +0000710 break; // "goldmont"
Simon Pilgrima271c542017-05-03 15:42:29 +0000711 case 0x57:
Craig Topperf3af64e2017-07-12 06:49:57 +0000712 *Type = INTEL_KNL; // knl
Simon Pilgrima271c542017-05-03 15:42:29 +0000713 break;
714
715 default: // Unknown family 6 CPU, try to guess.
716 if (Features & (1 << FEATURE_AVX512)) {
Craig Topperf3af64e2017-07-12 06:49:57 +0000717 *Type = INTEL_KNL; // knl
Simon Pilgrima271c542017-05-03 15:42:29 +0000718 break;
719 }
720 if (Features & (1 << FEATURE_ADX)) {
721 *Type = INTEL_COREI7;
722 *Subtype = INTEL_COREI7_BROADWELL;
723 break;
724 }
725 if (Features & (1 << FEATURE_AVX2)) {
726 *Type = INTEL_COREI7;
727 *Subtype = INTEL_COREI7_HASWELL;
728 break;
729 }
730 if (Features & (1 << FEATURE_AVX)) {
731 *Type = INTEL_COREI7;
732 *Subtype = INTEL_COREI7_SANDYBRIDGE;
733 break;
734 }
735 if (Features & (1 << FEATURE_SSE4_2)) {
736 if (Features & (1 << FEATURE_MOVBE)) {
Craig Topperf3af64e2017-07-12 06:49:57 +0000737 *Type = INTEL_SILVERMONT;
Simon Pilgrima271c542017-05-03 15:42:29 +0000738 } else {
739 *Type = INTEL_COREI7;
740 *Subtype = INTEL_COREI7_NEHALEM;
741 }
742 break;
743 }
744 if (Features & (1 << FEATURE_SSE4_1)) {
745 *Type = INTEL_CORE2; // "penryn"
746 *Subtype = INTEL_CORE2_45;
747 break;
748 }
749 if (Features & (1 << FEATURE_SSSE3)) {
750 if (Features & (1 << FEATURE_MOVBE)) {
Craig Topperf3af64e2017-07-12 06:49:57 +0000751 *Type = INTEL_BONNELL; // "bonnell"
Simon Pilgrima271c542017-05-03 15:42:29 +0000752 } else {
753 *Type = INTEL_CORE2; // "core2"
754 *Subtype = INTEL_CORE2_65;
755 }
756 break;
757 }
758 if (Features & (1 << FEATURE_EM64T)) {
759 *Type = INTEL_X86_64;
760 break; // x86-64
761 }
762 if (Features & (1 << FEATURE_SSE2)) {
763 *Type = INTEL_PENTIUM_M;
764 break;
765 }
766 if (Features & (1 << FEATURE_SSE)) {
767 *Type = INTEL_PENTIUM_III;
768 break;
769 }
770 if (Features & (1 << FEATURE_MMX)) {
771 *Type = INTEL_PENTIUM_II;
772 break;
773 }
774 *Type = INTEL_PENTIUM_PRO;
775 break;
776 }
777 break;
778 case 15: {
779 switch (Model) {
780 case 0: // Pentium 4 processor, Intel Xeon processor. All processors are
781 // model 00h and manufactured using the 0.18 micron process.
782 case 1: // Pentium 4 processor, Intel Xeon processor, Intel Xeon
783 // processor MP, and Intel Celeron processor. All processors are
784 // model 01h and manufactured using the 0.18 micron process.
785 case 2: // Pentium 4 processor, Mobile Intel Pentium 4 processor - M,
786 // Intel Xeon processor, Intel Xeon processor MP, Intel Celeron
787 // processor, and Mobile Intel Celeron processor. All processors
788 // are model 02h and manufactured using the 0.13 micron process.
789 *Type =
790 ((Features & (1 << FEATURE_EM64T)) ? INTEL_X86_64 : INTEL_PENTIUM_IV);
791 break;
792
793 case 3: // Pentium 4 processor, Intel Xeon processor, Intel Celeron D
794 // processor. All processors are model 03h and manufactured using
795 // the 90 nm process.
796 case 4: // Pentium 4 processor, Pentium 4 processor Extreme Edition,
797 // Pentium D processor, Intel Xeon processor, Intel Xeon
798 // processor MP, Intel Celeron D processor. All processors are
799 // model 04h and manufactured using the 90 nm process.
800 case 6: // Pentium 4 processor, Pentium D processor, Pentium processor
801 // Extreme Edition, Intel Xeon processor, Intel Xeon processor
802 // MP, Intel Celeron D processor. All processors are model 06h
803 // and manufactured using the 65 nm process.
804 *Type =
805 ((Features & (1 << FEATURE_EM64T)) ? INTEL_NOCONA : INTEL_PRESCOTT);
806 break;
807
808 default:
809 *Type =
810 ((Features & (1 << FEATURE_EM64T)) ? INTEL_X86_64 : INTEL_PENTIUM_IV);
811 break;
812 }
813 break;
814 }
815 default:
816 break; /*"generic"*/
817 }
818}
819
Craig Topper2ace1532017-07-08 06:44:34 +0000820static void getAMDProcessorTypeAndSubtype(unsigned Family, unsigned Model,
821 unsigned Features, unsigned *Type,
Simon Pilgrima271c542017-05-03 15:42:29 +0000822 unsigned *Subtype) {
823 // FIXME: this poorly matches the generated SubtargetFeatureKV table. There
824 // appears to be no way to generate the wide variety of AMD-specific targets
825 // from the information returned from CPUID.
826 switch (Family) {
827 case 4:
828 *Type = AMD_i486;
829 break;
830 case 5:
831 *Type = AMDPENTIUM;
832 switch (Model) {
833 case 6:
834 case 7:
835 *Subtype = AMDPENTIUM_K6;
836 break; // "k6"
837 case 8:
838 *Subtype = AMDPENTIUM_K62;
839 break; // "k6-2"
840 case 9:
841 case 13:
842 *Subtype = AMDPENTIUM_K63;
843 break; // "k6-3"
844 case 10:
845 *Subtype = AMDPENTIUM_GEODE;
846 break; // "geode"
847 }
848 break;
849 case 6:
850 *Type = AMDATHLON;
851 switch (Model) {
852 case 4:
853 *Subtype = AMDATHLON_TBIRD;
854 break; // "athlon-tbird"
855 case 6:
856 case 7:
857 case 8:
858 *Subtype = AMDATHLON_MP;
859 break; // "athlon-mp"
860 case 10:
861 *Subtype = AMDATHLON_XP;
862 break; // "athlon-xp"
863 }
864 break;
865 case 15:
866 *Type = AMDATHLON;
867 if (Features & (1 << FEATURE_SSE3)) {
868 *Subtype = AMDATHLON_K8SSE3;
869 break; // "k8-sse3"
870 }
871 switch (Model) {
872 case 1:
873 *Subtype = AMDATHLON_OPTERON;
874 break; // "opteron"
875 case 5:
876 *Subtype = AMDATHLON_FX;
877 break; // "athlon-fx"; also opteron
878 default:
879 *Subtype = AMDATHLON_64;
880 break; // "athlon64"
881 }
882 break;
883 case 16:
884 *Type = AMDFAM10H; // "amdfam10"
885 switch (Model) {
886 case 2:
887 *Subtype = AMDFAM10H_BARCELONA;
888 break;
889 case 4:
890 *Subtype = AMDFAM10H_SHANGHAI;
891 break;
892 case 8:
893 *Subtype = AMDFAM10H_ISTANBUL;
894 break;
895 }
896 break;
897 case 20:
Craig Topperf3af64e2017-07-12 06:49:57 +0000898 *Type = AMD_BTVER1;
Simon Pilgrima271c542017-05-03 15:42:29 +0000899 break; // "btver1";
900 case 21:
901 *Type = AMDFAM15H;
Craig Topper1f9d3c02017-07-08 06:44:35 +0000902 if (Model >= 0x60 && Model <= 0x7f) {
Simon Pilgrima271c542017-05-03 15:42:29 +0000903 *Subtype = AMDFAM15H_BDVER4;
Craig Topper3db11702017-07-12 06:49:56 +0000904 break; // "bdver4"; 60h-7Fh: Excavator
Simon Pilgrima271c542017-05-03 15:42:29 +0000905 }
906 if (Model >= 0x30 && Model <= 0x3f) {
907 *Subtype = AMDFAM15H_BDVER3;
908 break; // "bdver3"; 30h-3Fh: Steamroller
909 }
910 if (Model >= 0x10 && Model <= 0x1f) {
911 *Subtype = AMDFAM15H_BDVER2;
912 break; // "bdver2"; 10h-1Fh: Piledriver
913 }
914 if (Model <= 0x0f) {
915 *Subtype = AMDFAM15H_BDVER1;
916 break; // "bdver1"; 00h-0Fh: Bulldozer
917 }
918 break;
919 case 22:
Craig Topperf3af64e2017-07-12 06:49:57 +0000920 *Type = AMD_BTVER2;
Simon Pilgrima271c542017-05-03 15:42:29 +0000921 break; // "btver2"
922 case 23:
923 *Type = AMDFAM17H;
Craig Topperffe672d2017-07-08 06:44:36 +0000924 *Subtype = AMDFAM17H_ZNVER1;
Simon Pilgrima271c542017-05-03 15:42:29 +0000925 break;
926 default:
927 break; // "generic"
928 }
929}
930
Craig Topperc6bbe4b2017-07-08 05:16:14 +0000931static unsigned getAvailableFeatures(unsigned ECX, unsigned EDX,
Simon Pilgrima271c542017-05-03 15:42:29 +0000932 unsigned MaxLeaf) {
933 unsigned Features = 0;
Craig Topperc6bbe4b2017-07-08 05:16:14 +0000934 unsigned EAX, EBX;
Simon Pilgrima271c542017-05-03 15:42:29 +0000935 Features |= (((EDX >> 23) & 1) << FEATURE_MMX);
936 Features |= (((EDX >> 25) & 1) << FEATURE_SSE);
937 Features |= (((EDX >> 26) & 1) << FEATURE_SSE2);
938 Features |= (((ECX >> 0) & 1) << FEATURE_SSE3);
939 Features |= (((ECX >> 9) & 1) << FEATURE_SSSE3);
940 Features |= (((ECX >> 19) & 1) << FEATURE_SSE4_1);
941 Features |= (((ECX >> 20) & 1) << FEATURE_SSE4_2);
942 Features |= (((ECX >> 22) & 1) << FEATURE_MOVBE);
943
944 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
945 // indicates that the AVX registers will be saved and restored on context
946 // switch, then we have full AVX support.
947 const unsigned AVXBits = (1 << 27) | (1 << 28);
948 bool HasAVX = ((ECX & AVXBits) == AVXBits) && !getX86XCR0(&EAX, &EDX) &&
949 ((EAX & 0x6) == 0x6);
950 bool HasAVX512Save = HasAVX && ((EAX & 0xe0) == 0xe0);
951 bool HasLeaf7 =
952 MaxLeaf >= 0x7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
953 bool HasADX = HasLeaf7 && ((EBX >> 19) & 1);
954 bool HasAVX2 = HasAVX && HasLeaf7 && (EBX & 0x20);
955 bool HasAVX512 = HasLeaf7 && HasAVX512Save && ((EBX >> 16) & 1);
956 Features |= (HasAVX << FEATURE_AVX);
957 Features |= (HasAVX2 << FEATURE_AVX2);
958 Features |= (HasAVX512 << FEATURE_AVX512);
959 Features |= (HasAVX512Save << FEATURE_AVX512SAVE);
960 Features |= (HasADX << FEATURE_ADX);
961
Craig Topperbb8c7992017-07-08 05:16:13 +0000962 unsigned MaxExtLevel;
963 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
964
965 bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
966 !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
967 if (HasExtLeaf1)
968 Features |= (((EDX >> 29) & 0x1) << FEATURE_EM64T);
969
Simon Pilgrima271c542017-05-03 15:42:29 +0000970 return Features;
971}
972
973StringRef sys::getHostCPUName() {
974 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
975 unsigned MaxLeaf, Vendor;
976
977#if defined(__GNUC__) || defined(__clang__)
978 //FIXME: include cpuid.h from clang or copy __get_cpuid_max here
979 // and simplify it to not invoke __cpuid (like cpu_model.c in
980 // compiler-rt/lib/builtins/cpu_model.c?
981 // Opting for the second option.
982 if(!isCpuIdSupported())
983 return "generic";
984#endif
Craig Topperbb8c7992017-07-08 05:16:13 +0000985 if (getX86CpuIDAndInfo(0, &MaxLeaf, &Vendor, &ECX, &EDX) || MaxLeaf < 1)
Simon Pilgrima271c542017-05-03 15:42:29 +0000986 return "generic";
Craig Topperbb8c7992017-07-08 05:16:13 +0000987 getX86CpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
Simon Pilgrima271c542017-05-03 15:42:29 +0000988
989 unsigned Brand_id = EBX & 0xff;
990 unsigned Family = 0, Model = 0;
991 unsigned Features = 0;
992 detectX86FamilyModel(EAX, &Family, &Model);
993 Features = getAvailableFeatures(ECX, EDX, MaxLeaf);
994
995 unsigned Type;
996 unsigned Subtype;
997
998 if (Vendor == SIG_INTEL) {
999 getIntelProcessorTypeAndSubtype(Family, Model, Brand_id, Features, &Type,
1000 &Subtype);
1001 switch (Type) {
1002 case INTEL_i386:
1003 return "i386";
1004 case INTEL_i486:
1005 return "i486";
1006 case INTEL_PENTIUM:
1007 if (Subtype == INTEL_PENTIUM_MMX)
1008 return "pentium-mmx";
1009 return "pentium";
1010 case INTEL_PENTIUM_PRO:
1011 return "pentiumpro";
1012 case INTEL_PENTIUM_II:
1013 return "pentium2";
1014 case INTEL_PENTIUM_III:
1015 return "pentium3";
1016 case INTEL_PENTIUM_IV:
1017 return "pentium4";
1018 case INTEL_PENTIUM_M:
1019 return "pentium-m";
1020 case INTEL_CORE_DUO:
1021 return "yonah";
1022 case INTEL_CORE2:
1023 switch (Subtype) {
1024 case INTEL_CORE2_65:
1025 return "core2";
1026 case INTEL_CORE2_45:
1027 return "penryn";
1028 default:
Craig Topper3db11702017-07-12 06:49:56 +00001029 llvm_unreachable("Unexpected subtype!");
Simon Pilgrima271c542017-05-03 15:42:29 +00001030 }
1031 case INTEL_COREI7:
1032 switch (Subtype) {
1033 case INTEL_COREI7_NEHALEM:
1034 return "nehalem";
1035 case INTEL_COREI7_WESTMERE:
1036 return "westmere";
1037 case INTEL_COREI7_SANDYBRIDGE:
1038 return "sandybridge";
1039 case INTEL_COREI7_IVYBRIDGE:
1040 return "ivybridge";
1041 case INTEL_COREI7_HASWELL:
1042 return "haswell";
1043 case INTEL_COREI7_BROADWELL:
1044 return "broadwell";
1045 case INTEL_COREI7_SKYLAKE:
1046 return "skylake";
1047 case INTEL_COREI7_SKYLAKE_AVX512:
1048 return "skylake-avx512";
1049 default:
Craig Topper3db11702017-07-12 06:49:56 +00001050 llvm_unreachable("Unexpected subtype!");
Simon Pilgrima271c542017-05-03 15:42:29 +00001051 }
Craig Topperf3af64e2017-07-12 06:49:57 +00001052 case INTEL_BONNELL:
1053 return "bonnell";
1054 case INTEL_SILVERMONT:
1055 return "silvermont";
1056 case INTEL_GOLDMONT:
1057 return "goldmont";
1058 case INTEL_KNL:
1059 return "knl";
Simon Pilgrima271c542017-05-03 15:42:29 +00001060 case INTEL_X86_64:
1061 return "x86-64";
1062 case INTEL_NOCONA:
1063 return "nocona";
1064 case INTEL_PRESCOTT:
1065 return "prescott";
1066 default:
Craig Topper3db11702017-07-12 06:49:56 +00001067 break;
Simon Pilgrima271c542017-05-03 15:42:29 +00001068 }
1069 } else if (Vendor == SIG_AMD) {
1070 getAMDProcessorTypeAndSubtype(Family, Model, Features, &Type, &Subtype);
1071 switch (Type) {
1072 case AMD_i486:
1073 return "i486";
1074 case AMDPENTIUM:
1075 switch (Subtype) {
1076 case AMDPENTIUM_K6:
1077 return "k6";
1078 case AMDPENTIUM_K62:
1079 return "k6-2";
1080 case AMDPENTIUM_K63:
1081 return "k6-3";
1082 case AMDPENTIUM_GEODE:
1083 return "geode";
1084 default:
1085 return "pentium";
1086 }
1087 case AMDATHLON:
1088 switch (Subtype) {
1089 case AMDATHLON_TBIRD:
1090 return "athlon-tbird";
1091 case AMDATHLON_MP:
1092 return "athlon-mp";
1093 case AMDATHLON_XP:
1094 return "athlon-xp";
1095 case AMDATHLON_K8SSE3:
1096 return "k8-sse3";
1097 case AMDATHLON_OPTERON:
1098 return "opteron";
1099 case AMDATHLON_FX:
1100 return "athlon-fx";
1101 case AMDATHLON_64:
1102 return "athlon64";
1103 default:
Craig Topper3db11702017-07-12 06:49:56 +00001104 llvm_unreachable("Unexpected subtype!");
Simon Pilgrima271c542017-05-03 15:42:29 +00001105 }
1106 case AMDFAM10H:
Simon Pilgrima271c542017-05-03 15:42:29 +00001107 return "amdfam10";
Craig Topperf3af64e2017-07-12 06:49:57 +00001108 case AMD_BTVER1:
Simon Pilgrima271c542017-05-03 15:42:29 +00001109 return "btver1";
1110 case AMDFAM15H:
1111 switch (Subtype) {
Craig Topper3db11702017-07-12 06:49:56 +00001112 default: // There are gaps in the subtype detection.
Simon Pilgrima271c542017-05-03 15:42:29 +00001113 case AMDFAM15H_BDVER1:
1114 return "bdver1";
1115 case AMDFAM15H_BDVER2:
1116 return "bdver2";
1117 case AMDFAM15H_BDVER3:
1118 return "bdver3";
1119 case AMDFAM15H_BDVER4:
1120 return "bdver4";
Simon Pilgrima271c542017-05-03 15:42:29 +00001121 }
Craig Topperf3af64e2017-07-12 06:49:57 +00001122 case AMD_BTVER2:
Craig Topper3db11702017-07-12 06:49:56 +00001123 return "btver2";
Simon Pilgrima271c542017-05-03 15:42:29 +00001124 case AMDFAM17H:
Craig Topper3db11702017-07-12 06:49:56 +00001125 return "znver1";
Simon Pilgrima271c542017-05-03 15:42:29 +00001126 default:
Craig Topper3db11702017-07-12 06:49:56 +00001127 break;
Simon Pilgrima271c542017-05-03 15:42:29 +00001128 }
1129 }
1130 return "generic";
1131}
1132
1133#elif defined(__APPLE__) && (defined(__ppc__) || defined(__powerpc__))
1134StringRef sys::getHostCPUName() {
1135 host_basic_info_data_t hostInfo;
1136 mach_msg_type_number_t infoCount;
1137
1138 infoCount = HOST_BASIC_INFO_COUNT;
1139 host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo,
1140 &infoCount);
1141
1142 if (hostInfo.cpu_type != CPU_TYPE_POWERPC)
1143 return "generic";
1144
1145 switch (hostInfo.cpu_subtype) {
1146 case CPU_SUBTYPE_POWERPC_601:
1147 return "601";
1148 case CPU_SUBTYPE_POWERPC_602:
1149 return "602";
1150 case CPU_SUBTYPE_POWERPC_603:
1151 return "603";
1152 case CPU_SUBTYPE_POWERPC_603e:
1153 return "603e";
1154 case CPU_SUBTYPE_POWERPC_603ev:
1155 return "603ev";
1156 case CPU_SUBTYPE_POWERPC_604:
1157 return "604";
1158 case CPU_SUBTYPE_POWERPC_604e:
1159 return "604e";
1160 case CPU_SUBTYPE_POWERPC_620:
1161 return "620";
1162 case CPU_SUBTYPE_POWERPC_750:
1163 return "750";
1164 case CPU_SUBTYPE_POWERPC_7400:
1165 return "7400";
1166 case CPU_SUBTYPE_POWERPC_7450:
1167 return "7450";
1168 case CPU_SUBTYPE_POWERPC_970:
1169 return "970";
1170 default:;
1171 }
1172
1173 return "generic";
1174}
1175#elif defined(__linux__) && (defined(__ppc__) || defined(__powerpc__))
1176StringRef sys::getHostCPUName() {
1177 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1178 const StringRef& Content = P ? P->getBuffer() : "";
1179 return detail::getHostCPUNameForPowerPC(Content);
1180}
1181#elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1182StringRef sys::getHostCPUName() {
1183 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1184 const StringRef& Content = P ? P->getBuffer() : "";
1185 return detail::getHostCPUNameForARM(Content);
1186}
1187#elif defined(__linux__) && defined(__s390x__)
1188StringRef sys::getHostCPUName() {
1189 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1190 const StringRef& Content = P ? P->getBuffer() : "";
1191 return detail::getHostCPUNameForS390x(Content);
1192}
1193#else
1194StringRef sys::getHostCPUName() { return "generic"; }
1195#endif
1196
1197#if defined(__linux__) && defined(__x86_64__)
1198// On Linux, the number of physical cores can be computed from /proc/cpuinfo,
1199// using the number of unique physical/core id pairs. The following
1200// implementation reads the /proc/cpuinfo format on an x86_64 system.
1201static int computeHostNumPhysicalCores() {
1202 // Read /proc/cpuinfo as a stream (until EOF reached). It cannot be
1203 // mmapped because it appears to have 0 size.
1204 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
1205 llvm::MemoryBuffer::getFileAsStream("/proc/cpuinfo");
1206 if (std::error_code EC = Text.getError()) {
1207 llvm::errs() << "Can't read "
1208 << "/proc/cpuinfo: " << EC.message() << "\n";
1209 return -1;
1210 }
1211 SmallVector<StringRef, 8> strs;
1212 (*Text)->getBuffer().split(strs, "\n", /*MaxSplit=*/-1,
1213 /*KeepEmpty=*/false);
1214 int CurPhysicalId = -1;
1215 int CurCoreId = -1;
1216 SmallSet<std::pair<int, int>, 32> UniqueItems;
1217 for (auto &Line : strs) {
1218 Line = Line.trim();
1219 if (!Line.startswith("physical id") && !Line.startswith("core id"))
1220 continue;
1221 std::pair<StringRef, StringRef> Data = Line.split(':');
1222 auto Name = Data.first.trim();
1223 auto Val = Data.second.trim();
1224 if (Name == "physical id") {
1225 assert(CurPhysicalId == -1 &&
1226 "Expected a core id before seeing another physical id");
1227 Val.getAsInteger(10, CurPhysicalId);
1228 }
1229 if (Name == "core id") {
1230 assert(CurCoreId == -1 &&
1231 "Expected a physical id before seeing another core id");
1232 Val.getAsInteger(10, CurCoreId);
1233 }
1234 if (CurPhysicalId != -1 && CurCoreId != -1) {
1235 UniqueItems.insert(std::make_pair(CurPhysicalId, CurCoreId));
1236 CurPhysicalId = -1;
1237 CurCoreId = -1;
1238 }
1239 }
1240 return UniqueItems.size();
1241}
1242#elif defined(__APPLE__) && defined(__x86_64__)
1243#include <sys/param.h>
1244#include <sys/sysctl.h>
1245
1246// Gets the number of *physical cores* on the machine.
1247static int computeHostNumPhysicalCores() {
1248 uint32_t count;
1249 size_t len = sizeof(count);
1250 sysctlbyname("hw.physicalcpu", &count, &len, NULL, 0);
1251 if (count < 1) {
1252 int nm[2];
1253 nm[0] = CTL_HW;
1254 nm[1] = HW_AVAILCPU;
1255 sysctl(nm, 2, &count, &len, NULL, 0);
1256 if (count < 1)
1257 return -1;
1258 }
1259 return count;
1260}
1261#else
1262// On other systems, return -1 to indicate unknown.
1263static int computeHostNumPhysicalCores() { return -1; }
1264#endif
1265
1266int sys::getHostNumPhysicalCores() {
1267 static int NumCores = computeHostNumPhysicalCores();
1268 return NumCores;
1269}
1270
1271#if defined(__i386__) || defined(_M_IX86) || \
1272 defined(__x86_64__) || defined(_M_X64)
1273bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
1274 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
1275 unsigned MaxLevel;
1276 union {
1277 unsigned u[3];
1278 char c[12];
1279 } text;
1280
1281 if (getX86CpuIDAndInfo(0, &MaxLevel, text.u + 0, text.u + 2, text.u + 1) ||
1282 MaxLevel < 1)
1283 return false;
1284
1285 getX86CpuIDAndInfo(1, &EAX, &EBX, &ECX, &EDX);
1286
1287 Features["cmov"] = (EDX >> 15) & 1;
1288 Features["mmx"] = (EDX >> 23) & 1;
1289 Features["sse"] = (EDX >> 25) & 1;
1290 Features["sse2"] = (EDX >> 26) & 1;
1291 Features["sse3"] = (ECX >> 0) & 1;
1292 Features["ssse3"] = (ECX >> 9) & 1;
1293 Features["sse4.1"] = (ECX >> 19) & 1;
1294 Features["sse4.2"] = (ECX >> 20) & 1;
1295
1296 Features["pclmul"] = (ECX >> 1) & 1;
1297 Features["cx16"] = (ECX >> 13) & 1;
1298 Features["movbe"] = (ECX >> 22) & 1;
1299 Features["popcnt"] = (ECX >> 23) & 1;
1300 Features["aes"] = (ECX >> 25) & 1;
1301 Features["rdrnd"] = (ECX >> 30) & 1;
1302
1303 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
1304 // indicates that the AVX registers will be saved and restored on context
1305 // switch, then we have full AVX support.
1306 bool HasAVXSave = ((ECX >> 27) & 1) && ((ECX >> 28) & 1) &&
1307 !getX86XCR0(&EAX, &EDX) && ((EAX & 0x6) == 0x6);
1308 Features["avx"] = HasAVXSave;
1309 Features["fma"] = HasAVXSave && (ECX >> 12) & 1;
1310 Features["f16c"] = HasAVXSave && (ECX >> 29) & 1;
1311
1312 // Only enable XSAVE if OS has enabled support for saving YMM state.
1313 Features["xsave"] = HasAVXSave && (ECX >> 26) & 1;
1314
1315 // AVX512 requires additional context to be saved by the OS.
1316 bool HasAVX512Save = HasAVXSave && ((EAX & 0xe0) == 0xe0);
1317
1318 unsigned MaxExtLevel;
1319 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
1320
1321 bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
1322 !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
1323 Features["lzcnt"] = HasExtLeaf1 && ((ECX >> 5) & 1);
1324 Features["sse4a"] = HasExtLeaf1 && ((ECX >> 6) & 1);
1325 Features["prfchw"] = HasExtLeaf1 && ((ECX >> 8) & 1);
1326 Features["xop"] = HasExtLeaf1 && ((ECX >> 11) & 1) && HasAVXSave;
Simon Pilgrim99b925b2017-05-03 15:51:39 +00001327 Features["lwp"] = HasExtLeaf1 && ((ECX >> 15) & 1);
Simon Pilgrima271c542017-05-03 15:42:29 +00001328 Features["fma4"] = HasExtLeaf1 && ((ECX >> 16) & 1) && HasAVXSave;
1329 Features["tbm"] = HasExtLeaf1 && ((ECX >> 21) & 1);
1330 Features["mwaitx"] = HasExtLeaf1 && ((ECX >> 29) & 1);
1331
1332 bool HasExtLeaf8 = MaxExtLevel >= 0x80000008 &&
1333 !getX86CpuIDAndInfoEx(0x80000008,0x0, &EAX, &EBX, &ECX, &EDX);
1334 Features["clzero"] = HasExtLeaf8 && ((EBX >> 0) & 1);
1335
1336 bool HasLeaf7 =
1337 MaxLevel >= 7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
1338
1339 // AVX2 is only supported if we have the OS save support from AVX.
1340 Features["avx2"] = HasAVXSave && HasLeaf7 && ((EBX >> 5) & 1);
1341
1342 Features["fsgsbase"] = HasLeaf7 && ((EBX >> 0) & 1);
1343 Features["sgx"] = HasLeaf7 && ((EBX >> 2) & 1);
1344 Features["bmi"] = HasLeaf7 && ((EBX >> 3) & 1);
1345 Features["bmi2"] = HasLeaf7 && ((EBX >> 8) & 1);
1346 Features["rtm"] = HasLeaf7 && ((EBX >> 11) & 1);
1347 Features["rdseed"] = HasLeaf7 && ((EBX >> 18) & 1);
1348 Features["adx"] = HasLeaf7 && ((EBX >> 19) & 1);
1349 Features["clflushopt"] = HasLeaf7 && ((EBX >> 23) & 1);
1350 Features["clwb"] = HasLeaf7 && ((EBX >> 24) & 1);
1351 Features["sha"] = HasLeaf7 && ((EBX >> 29) & 1);
1352
1353 // AVX512 is only supported if the OS supports the context save for it.
1354 Features["avx512f"] = HasLeaf7 && ((EBX >> 16) & 1) && HasAVX512Save;
1355 Features["avx512dq"] = HasLeaf7 && ((EBX >> 17) & 1) && HasAVX512Save;
1356 Features["avx512ifma"] = HasLeaf7 && ((EBX >> 21) & 1) && HasAVX512Save;
1357 Features["avx512pf"] = HasLeaf7 && ((EBX >> 26) & 1) && HasAVX512Save;
1358 Features["avx512er"] = HasLeaf7 && ((EBX >> 27) & 1) && HasAVX512Save;
1359 Features["avx512cd"] = HasLeaf7 && ((EBX >> 28) & 1) && HasAVX512Save;
1360 Features["avx512bw"] = HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save;
1361 Features["avx512vl"] = HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save;
1362
1363 Features["prefetchwt1"] = HasLeaf7 && (ECX & 1);
1364 Features["avx512vbmi"] = HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save;
Oren Ben Simhonf3aab2f2017-05-28 11:26:11 +00001365 Features["avx512vpopcntdq"] = HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save;
Simon Pilgrima271c542017-05-03 15:42:29 +00001366 // Enable protection keys
1367 Features["pku"] = HasLeaf7 && ((ECX >> 4) & 1);
1368
1369 bool HasLeafD = MaxLevel >= 0xd &&
1370 !getX86CpuIDAndInfoEx(0xd, 0x1, &EAX, &EBX, &ECX, &EDX);
1371
1372 // Only enable XSAVE if OS has enabled support for saving YMM state.
1373 Features["xsaveopt"] = HasAVXSave && HasLeafD && ((EAX >> 0) & 1);
1374 Features["xsavec"] = HasAVXSave && HasLeafD && ((EAX >> 1) & 1);
1375 Features["xsaves"] = HasAVXSave && HasLeafD && ((EAX >> 3) & 1);
1376
1377 return true;
1378}
1379#elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1380bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
1381 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1382 if (!P)
1383 return false;
1384
1385 SmallVector<StringRef, 32> Lines;
1386 P->getBuffer().split(Lines, "\n");
1387
1388 SmallVector<StringRef, 32> CPUFeatures;
1389
1390 // Look for the CPU features.
1391 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
1392 if (Lines[I].startswith("Features")) {
1393 Lines[I].split(CPUFeatures, ' ');
1394 break;
1395 }
1396
1397#if defined(__aarch64__)
1398 // Keep track of which crypto features we have seen
1399 enum { CAP_AES = 0x1, CAP_PMULL = 0x2, CAP_SHA1 = 0x4, CAP_SHA2 = 0x8 };
1400 uint32_t crypto = 0;
1401#endif
1402
1403 for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) {
1404 StringRef LLVMFeatureStr = StringSwitch<StringRef>(CPUFeatures[I])
1405#if defined(__aarch64__)
1406 .Case("asimd", "neon")
1407 .Case("fp", "fp-armv8")
1408 .Case("crc32", "crc")
1409#else
1410 .Case("half", "fp16")
1411 .Case("neon", "neon")
1412 .Case("vfpv3", "vfp3")
1413 .Case("vfpv3d16", "d16")
1414 .Case("vfpv4", "vfp4")
1415 .Case("idiva", "hwdiv-arm")
1416 .Case("idivt", "hwdiv")
1417#endif
1418 .Default("");
1419
1420#if defined(__aarch64__)
1421 // We need to check crypto separately since we need all of the crypto
1422 // extensions to enable the subtarget feature
1423 if (CPUFeatures[I] == "aes")
1424 crypto |= CAP_AES;
1425 else if (CPUFeatures[I] == "pmull")
1426 crypto |= CAP_PMULL;
1427 else if (CPUFeatures[I] == "sha1")
1428 crypto |= CAP_SHA1;
1429 else if (CPUFeatures[I] == "sha2")
1430 crypto |= CAP_SHA2;
1431#endif
1432
1433 if (LLVMFeatureStr != "")
1434 Features[LLVMFeatureStr] = true;
1435 }
1436
1437#if defined(__aarch64__)
1438 // If we have all crypto bits we can add the feature
1439 if (crypto == (CAP_AES | CAP_PMULL | CAP_SHA1 | CAP_SHA2))
1440 Features["crypto"] = true;
1441#endif
1442
1443 return true;
1444}
1445#else
1446bool sys::getHostCPUFeatures(StringMap<bool> &Features) { return false; }
1447#endif
1448
1449std::string sys::getProcessTriple() {
Alex Lorenz3803df32017-07-07 09:53:47 +00001450 std::string TargetTripleString = updateTripleOSVersion(LLVM_HOST_TRIPLE);
1451 Triple PT(Triple::normalize(TargetTripleString));
Simon Pilgrima271c542017-05-03 15:42:29 +00001452
1453 if (sizeof(void *) == 8 && PT.isArch32Bit())
1454 PT = PT.get64BitArchVariant();
1455 if (sizeof(void *) == 4 && PT.isArch64Bit())
1456 PT = PT.get32BitArchVariant();
1457
1458 return PT.str();
1459}