blob: 9fbdb8b616a9651d0b778769e3f28b1fac885e10 [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 {
284 INTEL_ATOM = 1,
285 INTEL_CORE2,
286 INTEL_COREI7,
287 AMDFAM10H,
288 AMDFAM15H,
289 INTEL_i386,
290 INTEL_i486,
291 INTEL_PENTIUM,
292 INTEL_PENTIUM_PRO,
293 INTEL_PENTIUM_II,
294 INTEL_PENTIUM_III,
295 INTEL_PENTIUM_IV,
296 INTEL_PENTIUM_M,
297 INTEL_CORE_DUO,
298 INTEL_XEONPHI,
299 INTEL_X86_64,
300 INTEL_NOCONA,
301 INTEL_PRESCOTT,
302 AMD_i486,
303 AMDPENTIUM,
304 AMDATHLON,
305 AMDFAM14H,
306 AMDFAM16H,
307 AMDFAM17H,
308 CPU_TYPE_MAX
309};
310
311enum ProcessorSubtypes {
312 INTEL_COREI7_NEHALEM = 1,
313 INTEL_COREI7_WESTMERE,
314 INTEL_COREI7_SANDYBRIDGE,
315 AMDFAM10H_BARCELONA,
316 AMDFAM10H_SHANGHAI,
317 AMDFAM10H_ISTANBUL,
318 AMDFAM15H_BDVER1,
319 AMDFAM15H_BDVER2,
320 INTEL_PENTIUM_MMX,
321 INTEL_CORE2_65,
322 INTEL_CORE2_45,
323 INTEL_COREI7_IVYBRIDGE,
324 INTEL_COREI7_HASWELL,
325 INTEL_COREI7_BROADWELL,
326 INTEL_COREI7_SKYLAKE,
327 INTEL_COREI7_SKYLAKE_AVX512,
328 INTEL_ATOM_BONNELL,
329 INTEL_ATOM_SILVERMONT,
Michael Zuckerman4bcb9c32017-06-29 10:00:33 +0000330 INTEL_ATOM_GOLDMONT,
Simon Pilgrima271c542017-05-03 15:42:29 +0000331 INTEL_KNIGHTS_LANDING,
332 AMDPENTIUM_K6,
333 AMDPENTIUM_K62,
334 AMDPENTIUM_K63,
335 AMDPENTIUM_GEODE,
336 AMDATHLON_TBIRD,
337 AMDATHLON_MP,
338 AMDATHLON_XP,
339 AMDATHLON_K8SSE3,
340 AMDATHLON_OPTERON,
341 AMDATHLON_FX,
342 AMDATHLON_64,
343 AMD_BTVER1,
344 AMD_BTVER2,
345 AMDFAM15H_BDVER3,
346 AMDFAM15H_BDVER4,
347 AMDFAM17H_ZNVER1,
348 CPU_SUBTYPE_MAX
349};
350
351enum ProcessorFeatures {
352 FEATURE_CMOV = 0,
353 FEATURE_MMX,
354 FEATURE_POPCNT,
355 FEATURE_SSE,
356 FEATURE_SSE2,
357 FEATURE_SSE3,
358 FEATURE_SSSE3,
359 FEATURE_SSE4_1,
360 FEATURE_SSE4_2,
361 FEATURE_AVX,
362 FEATURE_AVX2,
363 FEATURE_AVX512,
364 FEATURE_AVX512SAVE,
365 FEATURE_MOVBE,
366 FEATURE_ADX,
367 FEATURE_EM64T
368};
369
370// The check below for i386 was copied from clang's cpuid.h (__get_cpuid_max).
371// Check motivated by bug reports for OpenSSL crashing on CPUs without CPUID
372// support. Consequently, for i386, the presence of CPUID is checked first
373// via the corresponding eflags bit.
374// Removal of cpuid.h header motivated by PR30384
375// Header cpuid.h and method __get_cpuid_max are not used in llvm, clang, openmp
376// or test-suite, but are used in external projects e.g. libstdcxx
377static bool isCpuIdSupported() {
378#if defined(__GNUC__) || defined(__clang__)
379#if defined(__i386__)
380 int __cpuid_supported;
381 __asm__(" pushfl\n"
382 " popl %%eax\n"
383 " movl %%eax,%%ecx\n"
384 " xorl $0x00200000,%%eax\n"
385 " pushl %%eax\n"
386 " popfl\n"
387 " pushfl\n"
388 " popl %%eax\n"
389 " movl $0,%0\n"
390 " cmpl %%eax,%%ecx\n"
391 " je 1f\n"
392 " movl $1,%0\n"
393 "1:"
394 : "=r"(__cpuid_supported)
395 :
396 : "eax", "ecx");
397 if (!__cpuid_supported)
398 return false;
399#endif
400 return true;
401#endif
402 return true;
403}
404
405/// getX86CpuIDAndInfo - Execute the specified cpuid and return the 4 values in
406/// the specified arguments. If we can't run cpuid on the host, return true.
407static bool getX86CpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
408 unsigned *rECX, unsigned *rEDX) {
409#if defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)
410#if defined(__GNUC__) || defined(__clang__)
411#if defined(__x86_64__)
412 // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
413 // FIXME: should we save this for Clang?
414 __asm__("movq\t%%rbx, %%rsi\n\t"
415 "cpuid\n\t"
416 "xchgq\t%%rbx, %%rsi\n\t"
417 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
418 : "a"(value));
419#elif defined(__i386__)
420 __asm__("movl\t%%ebx, %%esi\n\t"
421 "cpuid\n\t"
422 "xchgl\t%%ebx, %%esi\n\t"
423 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
424 : "a"(value));
425#else
426 assert(0 && "This method is defined only for x86.");
427#endif
428#elif defined(_MSC_VER)
429 // The MSVC intrinsic is portable across x86 and x64.
430 int registers[4];
431 __cpuid(registers, value);
432 *rEAX = registers[0];
433 *rEBX = registers[1];
434 *rECX = registers[2];
435 *rEDX = registers[3];
436#endif
437 return false;
438#else
439 return true;
440#endif
441}
442
443/// getX86CpuIDAndInfoEx - Execute the specified cpuid with subleaf and return
444/// the 4 values in the specified arguments. If we can't run cpuid on the host,
445/// return true.
446static bool getX86CpuIDAndInfoEx(unsigned value, unsigned subleaf,
447 unsigned *rEAX, unsigned *rEBX, unsigned *rECX,
448 unsigned *rEDX) {
449#if defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)
450#if defined(__x86_64__) || defined(_M_X64)
451#if defined(__GNUC__) || defined(__clang__)
452 // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually.
453 // FIXME: should we save this for Clang?
454 __asm__("movq\t%%rbx, %%rsi\n\t"
455 "cpuid\n\t"
456 "xchgq\t%%rbx, %%rsi\n\t"
457 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
458 : "a"(value), "c"(subleaf));
459#elif defined(_MSC_VER)
460 int registers[4];
461 __cpuidex(registers, value, subleaf);
462 *rEAX = registers[0];
463 *rEBX = registers[1];
464 *rECX = registers[2];
465 *rEDX = registers[3];
466#endif
467#elif defined(__i386__) || defined(_M_IX86)
468#if defined(__GNUC__) || defined(__clang__)
469 __asm__("movl\t%%ebx, %%esi\n\t"
470 "cpuid\n\t"
471 "xchgl\t%%ebx, %%esi\n\t"
472 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
473 : "a"(value), "c"(subleaf));
474#elif defined(_MSC_VER)
475 __asm {
476 mov eax,value
477 mov ecx,subleaf
478 cpuid
479 mov esi,rEAX
480 mov dword ptr [esi],eax
481 mov esi,rEBX
482 mov dword ptr [esi],ebx
483 mov esi,rECX
484 mov dword ptr [esi],ecx
485 mov esi,rEDX
486 mov dword ptr [esi],edx
487 }
488#endif
489#else
490 assert(0 && "This method is defined only for x86.");
491#endif
492 return false;
493#else
494 return true;
495#endif
496}
497
498static bool getX86XCR0(unsigned *rEAX, unsigned *rEDX) {
499#if defined(__GNUC__) || defined(__clang__)
500 // Check xgetbv; this uses a .byte sequence instead of the instruction
501 // directly because older assemblers do not include support for xgetbv and
502 // there is no easy way to conditionally compile based on the assembler used.
503 __asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(*rEAX), "=d"(*rEDX) : "c"(0));
504 return false;
505#elif defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK)
506 unsigned long long Result = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
507 *rEAX = Result;
508 *rEDX = Result >> 32;
509 return false;
510#else
511 return true;
512#endif
513}
514
515static void detectX86FamilyModel(unsigned EAX, unsigned *Family,
516 unsigned *Model) {
517 *Family = (EAX >> 8) & 0xf; // Bits 8 - 11
518 *Model = (EAX >> 4) & 0xf; // Bits 4 - 7
519 if (*Family == 6 || *Family == 0xf) {
520 if (*Family == 0xf)
521 // Examine extended family ID if family ID is F.
522 *Family += (EAX >> 20) & 0xff; // Bits 20 - 27
523 // Examine extended model ID if family ID is 6 or F.
524 *Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
525 }
526}
527
528static void
Craig Topperc6bbe4b2017-07-08 05:16:14 +0000529getIntelProcessorTypeAndSubtype(unsigned Family, unsigned Model,
530 unsigned Brand_id, unsigned Features,
Simon Pilgrima271c542017-05-03 15:42:29 +0000531 unsigned *Type, unsigned *Subtype) {
532 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;
684 // Check that we really have AVX512
685 if (Features & (1 << FEATURE_AVX512)) {
686 *Subtype = INTEL_COREI7_SKYLAKE_AVX512; // "skylake-avx512"
687 } else {
688 *Subtype = INTEL_COREI7_SKYLAKE; // "skylake"
689 }
690 break;
691
692 case 0x1c: // Most 45 nm Intel Atom processors
693 case 0x26: // 45 nm Atom Lincroft
694 case 0x27: // 32 nm Atom Medfield
695 case 0x35: // 32 nm Atom Midview
696 case 0x36: // 32 nm Atom Midview
697 *Type = INTEL_ATOM;
698 *Subtype = INTEL_ATOM_BONNELL;
699 break; // "bonnell"
700
701 // Atom Silvermont codes from the Intel software optimization guide.
702 case 0x37:
703 case 0x4a:
704 case 0x4d:
705 case 0x5a:
706 case 0x5d:
707 case 0x4c: // really airmont
708 *Type = INTEL_ATOM;
709 *Subtype = INTEL_ATOM_SILVERMONT;
710 break; // "silvermont"
Michael Zuckerman4bcb9c32017-06-29 10:00:33 +0000711 // Goldmont:
712 case 0x5c:
713 case 0x5f:
714 *Type = INTEL_ATOM;
715 *Subtype = INTEL_ATOM_GOLDMONT;
716 break; // "goldmont"
Simon Pilgrima271c542017-05-03 15:42:29 +0000717 case 0x57:
718 *Type = INTEL_XEONPHI; // knl
719 *Subtype = INTEL_KNIGHTS_LANDING;
720 break;
721
722 default: // Unknown family 6 CPU, try to guess.
723 if (Features & (1 << FEATURE_AVX512)) {
724 *Type = INTEL_XEONPHI; // knl
725 *Subtype = INTEL_KNIGHTS_LANDING;
726 break;
727 }
728 if (Features & (1 << FEATURE_ADX)) {
729 *Type = INTEL_COREI7;
730 *Subtype = INTEL_COREI7_BROADWELL;
731 break;
732 }
733 if (Features & (1 << FEATURE_AVX2)) {
734 *Type = INTEL_COREI7;
735 *Subtype = INTEL_COREI7_HASWELL;
736 break;
737 }
738 if (Features & (1 << FEATURE_AVX)) {
739 *Type = INTEL_COREI7;
740 *Subtype = INTEL_COREI7_SANDYBRIDGE;
741 break;
742 }
743 if (Features & (1 << FEATURE_SSE4_2)) {
744 if (Features & (1 << FEATURE_MOVBE)) {
745 *Type = INTEL_ATOM;
746 *Subtype = INTEL_ATOM_SILVERMONT;
747 } else {
748 *Type = INTEL_COREI7;
749 *Subtype = INTEL_COREI7_NEHALEM;
750 }
751 break;
752 }
753 if (Features & (1 << FEATURE_SSE4_1)) {
754 *Type = INTEL_CORE2; // "penryn"
755 *Subtype = INTEL_CORE2_45;
756 break;
757 }
758 if (Features & (1 << FEATURE_SSSE3)) {
759 if (Features & (1 << FEATURE_MOVBE)) {
760 *Type = INTEL_ATOM;
761 *Subtype = INTEL_ATOM_BONNELL; // "bonnell"
762 } else {
763 *Type = INTEL_CORE2; // "core2"
764 *Subtype = INTEL_CORE2_65;
765 }
766 break;
767 }
768 if (Features & (1 << FEATURE_EM64T)) {
769 *Type = INTEL_X86_64;
770 break; // x86-64
771 }
772 if (Features & (1 << FEATURE_SSE2)) {
773 *Type = INTEL_PENTIUM_M;
774 break;
775 }
776 if (Features & (1 << FEATURE_SSE)) {
777 *Type = INTEL_PENTIUM_III;
778 break;
779 }
780 if (Features & (1 << FEATURE_MMX)) {
781 *Type = INTEL_PENTIUM_II;
782 break;
783 }
784 *Type = INTEL_PENTIUM_PRO;
785 break;
786 }
787 break;
788 case 15: {
789 switch (Model) {
790 case 0: // Pentium 4 processor, Intel Xeon processor. All processors are
791 // model 00h and manufactured using the 0.18 micron process.
792 case 1: // Pentium 4 processor, Intel Xeon processor, Intel Xeon
793 // processor MP, and Intel Celeron processor. All processors are
794 // model 01h and manufactured using the 0.18 micron process.
795 case 2: // Pentium 4 processor, Mobile Intel Pentium 4 processor - M,
796 // Intel Xeon processor, Intel Xeon processor MP, Intel Celeron
797 // processor, and Mobile Intel Celeron processor. All processors
798 // are model 02h and manufactured using the 0.13 micron process.
799 *Type =
800 ((Features & (1 << FEATURE_EM64T)) ? INTEL_X86_64 : INTEL_PENTIUM_IV);
801 break;
802
803 case 3: // Pentium 4 processor, Intel Xeon processor, Intel Celeron D
804 // processor. All processors are model 03h and manufactured using
805 // the 90 nm process.
806 case 4: // Pentium 4 processor, Pentium 4 processor Extreme Edition,
807 // Pentium D processor, Intel Xeon processor, Intel Xeon
808 // processor MP, Intel Celeron D processor. All processors are
809 // model 04h and manufactured using the 90 nm process.
810 case 6: // Pentium 4 processor, Pentium D processor, Pentium processor
811 // Extreme Edition, Intel Xeon processor, Intel Xeon processor
812 // MP, Intel Celeron D processor. All processors are model 06h
813 // and manufactured using the 65 nm process.
814 *Type =
815 ((Features & (1 << FEATURE_EM64T)) ? INTEL_NOCONA : INTEL_PRESCOTT);
816 break;
817
818 default:
819 *Type =
820 ((Features & (1 << FEATURE_EM64T)) ? INTEL_X86_64 : INTEL_PENTIUM_IV);
821 break;
822 }
823 break;
824 }
825 default:
826 break; /*"generic"*/
827 }
828}
829
Craig Topperc6bbe4b2017-07-08 05:16:14 +0000830static void getAMDProcessorTypeAndSubtype(unsigned Family,
831 unsigned Model,
832 unsigned Features,
Simon Pilgrima271c542017-05-03 15:42:29 +0000833 unsigned *Type,
834 unsigned *Subtype) {
835 // FIXME: this poorly matches the generated SubtargetFeatureKV table. There
836 // appears to be no way to generate the wide variety of AMD-specific targets
837 // from the information returned from CPUID.
838 switch (Family) {
839 case 4:
840 *Type = AMD_i486;
841 break;
842 case 5:
843 *Type = AMDPENTIUM;
844 switch (Model) {
845 case 6:
846 case 7:
847 *Subtype = AMDPENTIUM_K6;
848 break; // "k6"
849 case 8:
850 *Subtype = AMDPENTIUM_K62;
851 break; // "k6-2"
852 case 9:
853 case 13:
854 *Subtype = AMDPENTIUM_K63;
855 break; // "k6-3"
856 case 10:
857 *Subtype = AMDPENTIUM_GEODE;
858 break; // "geode"
859 }
860 break;
861 case 6:
862 *Type = AMDATHLON;
863 switch (Model) {
864 case 4:
865 *Subtype = AMDATHLON_TBIRD;
866 break; // "athlon-tbird"
867 case 6:
868 case 7:
869 case 8:
870 *Subtype = AMDATHLON_MP;
871 break; // "athlon-mp"
872 case 10:
873 *Subtype = AMDATHLON_XP;
874 break; // "athlon-xp"
875 }
876 break;
877 case 15:
878 *Type = AMDATHLON;
879 if (Features & (1 << FEATURE_SSE3)) {
880 *Subtype = AMDATHLON_K8SSE3;
881 break; // "k8-sse3"
882 }
883 switch (Model) {
884 case 1:
885 *Subtype = AMDATHLON_OPTERON;
886 break; // "opteron"
887 case 5:
888 *Subtype = AMDATHLON_FX;
889 break; // "athlon-fx"; also opteron
890 default:
891 *Subtype = AMDATHLON_64;
892 break; // "athlon64"
893 }
894 break;
895 case 16:
896 *Type = AMDFAM10H; // "amdfam10"
897 switch (Model) {
898 case 2:
899 *Subtype = AMDFAM10H_BARCELONA;
900 break;
901 case 4:
902 *Subtype = AMDFAM10H_SHANGHAI;
903 break;
904 case 8:
905 *Subtype = AMDFAM10H_ISTANBUL;
906 break;
907 }
908 break;
909 case 20:
910 *Type = AMDFAM14H;
911 *Subtype = AMD_BTVER1;
912 break; // "btver1";
913 case 21:
914 *Type = AMDFAM15H;
915 if (!(Features &
916 (1 << FEATURE_AVX))) { // If no AVX support, provide a sane fallback.
917 *Subtype = AMD_BTVER1;
918 break; // "btver1"
919 }
920 if (Model >= 0x50 && Model <= 0x6f) {
921 *Subtype = AMDFAM15H_BDVER4;
922 break; // "bdver4"; 50h-6Fh: Excavator
923 }
924 if (Model >= 0x30 && Model <= 0x3f) {
925 *Subtype = AMDFAM15H_BDVER3;
926 break; // "bdver3"; 30h-3Fh: Steamroller
927 }
928 if (Model >= 0x10 && Model <= 0x1f) {
929 *Subtype = AMDFAM15H_BDVER2;
930 break; // "bdver2"; 10h-1Fh: Piledriver
931 }
932 if (Model <= 0x0f) {
933 *Subtype = AMDFAM15H_BDVER1;
934 break; // "bdver1"; 00h-0Fh: Bulldozer
935 }
936 break;
937 case 22:
938 *Type = AMDFAM16H;
939 if (!(Features &
940 (1 << FEATURE_AVX))) { // If no AVX support provide a sane fallback.
941 *Subtype = AMD_BTVER1;
942 break; // "btver1";
943 }
944 *Subtype = AMD_BTVER2;
945 break; // "btver2"
946 case 23:
947 *Type = AMDFAM17H;
948 if (Features & (1 << FEATURE_ADX)) {
949 *Subtype = AMDFAM17H_ZNVER1;
950 break; // "znver1"
951 }
952 *Subtype = AMD_BTVER1;
953 break;
954 default:
955 break; // "generic"
956 }
957}
958
Craig Topperc6bbe4b2017-07-08 05:16:14 +0000959static unsigned getAvailableFeatures(unsigned ECX, unsigned EDX,
Simon Pilgrima271c542017-05-03 15:42:29 +0000960 unsigned MaxLeaf) {
961 unsigned Features = 0;
Craig Topperc6bbe4b2017-07-08 05:16:14 +0000962 unsigned EAX, EBX;
Simon Pilgrima271c542017-05-03 15:42:29 +0000963 Features |= (((EDX >> 23) & 1) << FEATURE_MMX);
964 Features |= (((EDX >> 25) & 1) << FEATURE_SSE);
965 Features |= (((EDX >> 26) & 1) << FEATURE_SSE2);
966 Features |= (((ECX >> 0) & 1) << FEATURE_SSE3);
967 Features |= (((ECX >> 9) & 1) << FEATURE_SSSE3);
968 Features |= (((ECX >> 19) & 1) << FEATURE_SSE4_1);
969 Features |= (((ECX >> 20) & 1) << FEATURE_SSE4_2);
970 Features |= (((ECX >> 22) & 1) << FEATURE_MOVBE);
971
972 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
973 // indicates that the AVX registers will be saved and restored on context
974 // switch, then we have full AVX support.
975 const unsigned AVXBits = (1 << 27) | (1 << 28);
976 bool HasAVX = ((ECX & AVXBits) == AVXBits) && !getX86XCR0(&EAX, &EDX) &&
977 ((EAX & 0x6) == 0x6);
978 bool HasAVX512Save = HasAVX && ((EAX & 0xe0) == 0xe0);
979 bool HasLeaf7 =
980 MaxLeaf >= 0x7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
981 bool HasADX = HasLeaf7 && ((EBX >> 19) & 1);
982 bool HasAVX2 = HasAVX && HasLeaf7 && (EBX & 0x20);
983 bool HasAVX512 = HasLeaf7 && HasAVX512Save && ((EBX >> 16) & 1);
984 Features |= (HasAVX << FEATURE_AVX);
985 Features |= (HasAVX2 << FEATURE_AVX2);
986 Features |= (HasAVX512 << FEATURE_AVX512);
987 Features |= (HasAVX512Save << FEATURE_AVX512SAVE);
988 Features |= (HasADX << FEATURE_ADX);
989
Craig Topperbb8c7992017-07-08 05:16:13 +0000990 unsigned MaxExtLevel;
991 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
992
993 bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
994 !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
995 if (HasExtLeaf1)
996 Features |= (((EDX >> 29) & 0x1) << FEATURE_EM64T);
997
Simon Pilgrima271c542017-05-03 15:42:29 +0000998 return Features;
999}
1000
1001StringRef sys::getHostCPUName() {
1002 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
1003 unsigned MaxLeaf, Vendor;
1004
1005#if defined(__GNUC__) || defined(__clang__)
1006 //FIXME: include cpuid.h from clang or copy __get_cpuid_max here
1007 // and simplify it to not invoke __cpuid (like cpu_model.c in
1008 // compiler-rt/lib/builtins/cpu_model.c?
1009 // Opting for the second option.
1010 if(!isCpuIdSupported())
1011 return "generic";
1012#endif
Craig Topperbb8c7992017-07-08 05:16:13 +00001013 if (getX86CpuIDAndInfo(0, &MaxLeaf, &Vendor, &ECX, &EDX) || MaxLeaf < 1)
Simon Pilgrima271c542017-05-03 15:42:29 +00001014 return "generic";
Craig Topperbb8c7992017-07-08 05:16:13 +00001015 getX86CpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
Simon Pilgrima271c542017-05-03 15:42:29 +00001016
1017 unsigned Brand_id = EBX & 0xff;
1018 unsigned Family = 0, Model = 0;
1019 unsigned Features = 0;
1020 detectX86FamilyModel(EAX, &Family, &Model);
1021 Features = getAvailableFeatures(ECX, EDX, MaxLeaf);
1022
1023 unsigned Type;
1024 unsigned Subtype;
1025
1026 if (Vendor == SIG_INTEL) {
1027 getIntelProcessorTypeAndSubtype(Family, Model, Brand_id, Features, &Type,
1028 &Subtype);
1029 switch (Type) {
1030 case INTEL_i386:
1031 return "i386";
1032 case INTEL_i486:
1033 return "i486";
1034 case INTEL_PENTIUM:
1035 if (Subtype == INTEL_PENTIUM_MMX)
1036 return "pentium-mmx";
1037 return "pentium";
1038 case INTEL_PENTIUM_PRO:
1039 return "pentiumpro";
1040 case INTEL_PENTIUM_II:
1041 return "pentium2";
1042 case INTEL_PENTIUM_III:
1043 return "pentium3";
1044 case INTEL_PENTIUM_IV:
1045 return "pentium4";
1046 case INTEL_PENTIUM_M:
1047 return "pentium-m";
1048 case INTEL_CORE_DUO:
1049 return "yonah";
1050 case INTEL_CORE2:
1051 switch (Subtype) {
1052 case INTEL_CORE2_65:
1053 return "core2";
1054 case INTEL_CORE2_45:
1055 return "penryn";
1056 default:
1057 return "core2";
1058 }
1059 case INTEL_COREI7:
1060 switch (Subtype) {
1061 case INTEL_COREI7_NEHALEM:
1062 return "nehalem";
1063 case INTEL_COREI7_WESTMERE:
1064 return "westmere";
1065 case INTEL_COREI7_SANDYBRIDGE:
1066 return "sandybridge";
1067 case INTEL_COREI7_IVYBRIDGE:
1068 return "ivybridge";
1069 case INTEL_COREI7_HASWELL:
1070 return "haswell";
1071 case INTEL_COREI7_BROADWELL:
1072 return "broadwell";
1073 case INTEL_COREI7_SKYLAKE:
1074 return "skylake";
1075 case INTEL_COREI7_SKYLAKE_AVX512:
1076 return "skylake-avx512";
1077 default:
1078 return "corei7";
1079 }
1080 case INTEL_ATOM:
1081 switch (Subtype) {
1082 case INTEL_ATOM_BONNELL:
1083 return "bonnell";
Michael Zuckerman4bcb9c32017-06-29 10:00:33 +00001084 case INTEL_ATOM_GOLDMONT:
1085 return "goldmont";
Simon Pilgrima271c542017-05-03 15:42:29 +00001086 case INTEL_ATOM_SILVERMONT:
1087 return "silvermont";
1088 default:
1089 return "atom";
1090 }
1091 case INTEL_XEONPHI:
1092 return "knl"; /*update for more variants added*/
1093 case INTEL_X86_64:
1094 return "x86-64";
1095 case INTEL_NOCONA:
1096 return "nocona";
1097 case INTEL_PRESCOTT:
1098 return "prescott";
1099 default:
1100 return "generic";
1101 }
1102 } else if (Vendor == SIG_AMD) {
1103 getAMDProcessorTypeAndSubtype(Family, Model, Features, &Type, &Subtype);
1104 switch (Type) {
1105 case AMD_i486:
1106 return "i486";
1107 case AMDPENTIUM:
1108 switch (Subtype) {
1109 case AMDPENTIUM_K6:
1110 return "k6";
1111 case AMDPENTIUM_K62:
1112 return "k6-2";
1113 case AMDPENTIUM_K63:
1114 return "k6-3";
1115 case AMDPENTIUM_GEODE:
1116 return "geode";
1117 default:
1118 return "pentium";
1119 }
1120 case AMDATHLON:
1121 switch (Subtype) {
1122 case AMDATHLON_TBIRD:
1123 return "athlon-tbird";
1124 case AMDATHLON_MP:
1125 return "athlon-mp";
1126 case AMDATHLON_XP:
1127 return "athlon-xp";
1128 case AMDATHLON_K8SSE3:
1129 return "k8-sse3";
1130 case AMDATHLON_OPTERON:
1131 return "opteron";
1132 case AMDATHLON_FX:
1133 return "athlon-fx";
1134 case AMDATHLON_64:
1135 return "athlon64";
1136 default:
1137 return "athlon";
1138 }
1139 case AMDFAM10H:
1140 if(Subtype == AMDFAM10H_BARCELONA)
1141 return "barcelona";
1142 return "amdfam10";
1143 case AMDFAM14H:
1144 return "btver1";
1145 case AMDFAM15H:
1146 switch (Subtype) {
1147 case AMDFAM15H_BDVER1:
1148 return "bdver1";
1149 case AMDFAM15H_BDVER2:
1150 return "bdver2";
1151 case AMDFAM15H_BDVER3:
1152 return "bdver3";
1153 case AMDFAM15H_BDVER4:
1154 return "bdver4";
1155 case AMD_BTVER1:
1156 return "btver1";
1157 default:
1158 return "amdfam15";
1159 }
1160 case AMDFAM16H:
1161 switch (Subtype) {
1162 case AMD_BTVER1:
1163 return "btver1";
1164 case AMD_BTVER2:
1165 return "btver2";
1166 default:
1167 return "amdfam16";
1168 }
1169 case AMDFAM17H:
1170 switch (Subtype) {
1171 case AMD_BTVER1:
1172 return "btver1";
1173 case AMDFAM17H_ZNVER1:
1174 return "znver1";
1175 default:
1176 return "amdfam17";
1177 }
1178 default:
1179 return "generic";
1180 }
1181 }
1182 return "generic";
1183}
1184
1185#elif defined(__APPLE__) && (defined(__ppc__) || defined(__powerpc__))
1186StringRef sys::getHostCPUName() {
1187 host_basic_info_data_t hostInfo;
1188 mach_msg_type_number_t infoCount;
1189
1190 infoCount = HOST_BASIC_INFO_COUNT;
1191 host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo,
1192 &infoCount);
1193
1194 if (hostInfo.cpu_type != CPU_TYPE_POWERPC)
1195 return "generic";
1196
1197 switch (hostInfo.cpu_subtype) {
1198 case CPU_SUBTYPE_POWERPC_601:
1199 return "601";
1200 case CPU_SUBTYPE_POWERPC_602:
1201 return "602";
1202 case CPU_SUBTYPE_POWERPC_603:
1203 return "603";
1204 case CPU_SUBTYPE_POWERPC_603e:
1205 return "603e";
1206 case CPU_SUBTYPE_POWERPC_603ev:
1207 return "603ev";
1208 case CPU_SUBTYPE_POWERPC_604:
1209 return "604";
1210 case CPU_SUBTYPE_POWERPC_604e:
1211 return "604e";
1212 case CPU_SUBTYPE_POWERPC_620:
1213 return "620";
1214 case CPU_SUBTYPE_POWERPC_750:
1215 return "750";
1216 case CPU_SUBTYPE_POWERPC_7400:
1217 return "7400";
1218 case CPU_SUBTYPE_POWERPC_7450:
1219 return "7450";
1220 case CPU_SUBTYPE_POWERPC_970:
1221 return "970";
1222 default:;
1223 }
1224
1225 return "generic";
1226}
1227#elif defined(__linux__) && (defined(__ppc__) || defined(__powerpc__))
1228StringRef sys::getHostCPUName() {
1229 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1230 const StringRef& Content = P ? P->getBuffer() : "";
1231 return detail::getHostCPUNameForPowerPC(Content);
1232}
1233#elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1234StringRef sys::getHostCPUName() {
1235 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1236 const StringRef& Content = P ? P->getBuffer() : "";
1237 return detail::getHostCPUNameForARM(Content);
1238}
1239#elif defined(__linux__) && defined(__s390x__)
1240StringRef sys::getHostCPUName() {
1241 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1242 const StringRef& Content = P ? P->getBuffer() : "";
1243 return detail::getHostCPUNameForS390x(Content);
1244}
1245#else
1246StringRef sys::getHostCPUName() { return "generic"; }
1247#endif
1248
1249#if defined(__linux__) && defined(__x86_64__)
1250// On Linux, the number of physical cores can be computed from /proc/cpuinfo,
1251// using the number of unique physical/core id pairs. The following
1252// implementation reads the /proc/cpuinfo format on an x86_64 system.
1253static int computeHostNumPhysicalCores() {
1254 // Read /proc/cpuinfo as a stream (until EOF reached). It cannot be
1255 // mmapped because it appears to have 0 size.
1256 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
1257 llvm::MemoryBuffer::getFileAsStream("/proc/cpuinfo");
1258 if (std::error_code EC = Text.getError()) {
1259 llvm::errs() << "Can't read "
1260 << "/proc/cpuinfo: " << EC.message() << "\n";
1261 return -1;
1262 }
1263 SmallVector<StringRef, 8> strs;
1264 (*Text)->getBuffer().split(strs, "\n", /*MaxSplit=*/-1,
1265 /*KeepEmpty=*/false);
1266 int CurPhysicalId = -1;
1267 int CurCoreId = -1;
1268 SmallSet<std::pair<int, int>, 32> UniqueItems;
1269 for (auto &Line : strs) {
1270 Line = Line.trim();
1271 if (!Line.startswith("physical id") && !Line.startswith("core id"))
1272 continue;
1273 std::pair<StringRef, StringRef> Data = Line.split(':');
1274 auto Name = Data.first.trim();
1275 auto Val = Data.second.trim();
1276 if (Name == "physical id") {
1277 assert(CurPhysicalId == -1 &&
1278 "Expected a core id before seeing another physical id");
1279 Val.getAsInteger(10, CurPhysicalId);
1280 }
1281 if (Name == "core id") {
1282 assert(CurCoreId == -1 &&
1283 "Expected a physical id before seeing another core id");
1284 Val.getAsInteger(10, CurCoreId);
1285 }
1286 if (CurPhysicalId != -1 && CurCoreId != -1) {
1287 UniqueItems.insert(std::make_pair(CurPhysicalId, CurCoreId));
1288 CurPhysicalId = -1;
1289 CurCoreId = -1;
1290 }
1291 }
1292 return UniqueItems.size();
1293}
1294#elif defined(__APPLE__) && defined(__x86_64__)
1295#include <sys/param.h>
1296#include <sys/sysctl.h>
1297
1298// Gets the number of *physical cores* on the machine.
1299static int computeHostNumPhysicalCores() {
1300 uint32_t count;
1301 size_t len = sizeof(count);
1302 sysctlbyname("hw.physicalcpu", &count, &len, NULL, 0);
1303 if (count < 1) {
1304 int nm[2];
1305 nm[0] = CTL_HW;
1306 nm[1] = HW_AVAILCPU;
1307 sysctl(nm, 2, &count, &len, NULL, 0);
1308 if (count < 1)
1309 return -1;
1310 }
1311 return count;
1312}
1313#else
1314// On other systems, return -1 to indicate unknown.
1315static int computeHostNumPhysicalCores() { return -1; }
1316#endif
1317
1318int sys::getHostNumPhysicalCores() {
1319 static int NumCores = computeHostNumPhysicalCores();
1320 return NumCores;
1321}
1322
1323#if defined(__i386__) || defined(_M_IX86) || \
1324 defined(__x86_64__) || defined(_M_X64)
1325bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
1326 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
1327 unsigned MaxLevel;
1328 union {
1329 unsigned u[3];
1330 char c[12];
1331 } text;
1332
1333 if (getX86CpuIDAndInfo(0, &MaxLevel, text.u + 0, text.u + 2, text.u + 1) ||
1334 MaxLevel < 1)
1335 return false;
1336
1337 getX86CpuIDAndInfo(1, &EAX, &EBX, &ECX, &EDX);
1338
1339 Features["cmov"] = (EDX >> 15) & 1;
1340 Features["mmx"] = (EDX >> 23) & 1;
1341 Features["sse"] = (EDX >> 25) & 1;
1342 Features["sse2"] = (EDX >> 26) & 1;
1343 Features["sse3"] = (ECX >> 0) & 1;
1344 Features["ssse3"] = (ECX >> 9) & 1;
1345 Features["sse4.1"] = (ECX >> 19) & 1;
1346 Features["sse4.2"] = (ECX >> 20) & 1;
1347
1348 Features["pclmul"] = (ECX >> 1) & 1;
1349 Features["cx16"] = (ECX >> 13) & 1;
1350 Features["movbe"] = (ECX >> 22) & 1;
1351 Features["popcnt"] = (ECX >> 23) & 1;
1352 Features["aes"] = (ECX >> 25) & 1;
1353 Features["rdrnd"] = (ECX >> 30) & 1;
1354
1355 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
1356 // indicates that the AVX registers will be saved and restored on context
1357 // switch, then we have full AVX support.
1358 bool HasAVXSave = ((ECX >> 27) & 1) && ((ECX >> 28) & 1) &&
1359 !getX86XCR0(&EAX, &EDX) && ((EAX & 0x6) == 0x6);
1360 Features["avx"] = HasAVXSave;
1361 Features["fma"] = HasAVXSave && (ECX >> 12) & 1;
1362 Features["f16c"] = HasAVXSave && (ECX >> 29) & 1;
1363
1364 // Only enable XSAVE if OS has enabled support for saving YMM state.
1365 Features["xsave"] = HasAVXSave && (ECX >> 26) & 1;
1366
1367 // AVX512 requires additional context to be saved by the OS.
1368 bool HasAVX512Save = HasAVXSave && ((EAX & 0xe0) == 0xe0);
1369
1370 unsigned MaxExtLevel;
1371 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
1372
1373 bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
1374 !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
1375 Features["lzcnt"] = HasExtLeaf1 && ((ECX >> 5) & 1);
1376 Features["sse4a"] = HasExtLeaf1 && ((ECX >> 6) & 1);
1377 Features["prfchw"] = HasExtLeaf1 && ((ECX >> 8) & 1);
1378 Features["xop"] = HasExtLeaf1 && ((ECX >> 11) & 1) && HasAVXSave;
Simon Pilgrim99b925b2017-05-03 15:51:39 +00001379 Features["lwp"] = HasExtLeaf1 && ((ECX >> 15) & 1);
Simon Pilgrima271c542017-05-03 15:42:29 +00001380 Features["fma4"] = HasExtLeaf1 && ((ECX >> 16) & 1) && HasAVXSave;
1381 Features["tbm"] = HasExtLeaf1 && ((ECX >> 21) & 1);
1382 Features["mwaitx"] = HasExtLeaf1 && ((ECX >> 29) & 1);
1383
1384 bool HasExtLeaf8 = MaxExtLevel >= 0x80000008 &&
1385 !getX86CpuIDAndInfoEx(0x80000008,0x0, &EAX, &EBX, &ECX, &EDX);
1386 Features["clzero"] = HasExtLeaf8 && ((EBX >> 0) & 1);
1387
1388 bool HasLeaf7 =
1389 MaxLevel >= 7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
1390
1391 // AVX2 is only supported if we have the OS save support from AVX.
1392 Features["avx2"] = HasAVXSave && HasLeaf7 && ((EBX >> 5) & 1);
1393
1394 Features["fsgsbase"] = HasLeaf7 && ((EBX >> 0) & 1);
1395 Features["sgx"] = HasLeaf7 && ((EBX >> 2) & 1);
1396 Features["bmi"] = HasLeaf7 && ((EBX >> 3) & 1);
1397 Features["bmi2"] = HasLeaf7 && ((EBX >> 8) & 1);
1398 Features["rtm"] = HasLeaf7 && ((EBX >> 11) & 1);
1399 Features["rdseed"] = HasLeaf7 && ((EBX >> 18) & 1);
1400 Features["adx"] = HasLeaf7 && ((EBX >> 19) & 1);
1401 Features["clflushopt"] = HasLeaf7 && ((EBX >> 23) & 1);
1402 Features["clwb"] = HasLeaf7 && ((EBX >> 24) & 1);
1403 Features["sha"] = HasLeaf7 && ((EBX >> 29) & 1);
1404
1405 // AVX512 is only supported if the OS supports the context save for it.
1406 Features["avx512f"] = HasLeaf7 && ((EBX >> 16) & 1) && HasAVX512Save;
1407 Features["avx512dq"] = HasLeaf7 && ((EBX >> 17) & 1) && HasAVX512Save;
1408 Features["avx512ifma"] = HasLeaf7 && ((EBX >> 21) & 1) && HasAVX512Save;
1409 Features["avx512pf"] = HasLeaf7 && ((EBX >> 26) & 1) && HasAVX512Save;
1410 Features["avx512er"] = HasLeaf7 && ((EBX >> 27) & 1) && HasAVX512Save;
1411 Features["avx512cd"] = HasLeaf7 && ((EBX >> 28) & 1) && HasAVX512Save;
1412 Features["avx512bw"] = HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save;
1413 Features["avx512vl"] = HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save;
1414
1415 Features["prefetchwt1"] = HasLeaf7 && (ECX & 1);
1416 Features["avx512vbmi"] = HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save;
Oren Ben Simhonf3aab2f2017-05-28 11:26:11 +00001417 Features["avx512vpopcntdq"] = HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save;
Simon Pilgrima271c542017-05-03 15:42:29 +00001418 // Enable protection keys
1419 Features["pku"] = HasLeaf7 && ((ECX >> 4) & 1);
1420
1421 bool HasLeafD = MaxLevel >= 0xd &&
1422 !getX86CpuIDAndInfoEx(0xd, 0x1, &EAX, &EBX, &ECX, &EDX);
1423
1424 // Only enable XSAVE if OS has enabled support for saving YMM state.
1425 Features["xsaveopt"] = HasAVXSave && HasLeafD && ((EAX >> 0) & 1);
1426 Features["xsavec"] = HasAVXSave && HasLeafD && ((EAX >> 1) & 1);
1427 Features["xsaves"] = HasAVXSave && HasLeafD && ((EAX >> 3) & 1);
1428
1429 return true;
1430}
1431#elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1432bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
1433 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1434 if (!P)
1435 return false;
1436
1437 SmallVector<StringRef, 32> Lines;
1438 P->getBuffer().split(Lines, "\n");
1439
1440 SmallVector<StringRef, 32> CPUFeatures;
1441
1442 // Look for the CPU features.
1443 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
1444 if (Lines[I].startswith("Features")) {
1445 Lines[I].split(CPUFeatures, ' ');
1446 break;
1447 }
1448
1449#if defined(__aarch64__)
1450 // Keep track of which crypto features we have seen
1451 enum { CAP_AES = 0x1, CAP_PMULL = 0x2, CAP_SHA1 = 0x4, CAP_SHA2 = 0x8 };
1452 uint32_t crypto = 0;
1453#endif
1454
1455 for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) {
1456 StringRef LLVMFeatureStr = StringSwitch<StringRef>(CPUFeatures[I])
1457#if defined(__aarch64__)
1458 .Case("asimd", "neon")
1459 .Case("fp", "fp-armv8")
1460 .Case("crc32", "crc")
1461#else
1462 .Case("half", "fp16")
1463 .Case("neon", "neon")
1464 .Case("vfpv3", "vfp3")
1465 .Case("vfpv3d16", "d16")
1466 .Case("vfpv4", "vfp4")
1467 .Case("idiva", "hwdiv-arm")
1468 .Case("idivt", "hwdiv")
1469#endif
1470 .Default("");
1471
1472#if defined(__aarch64__)
1473 // We need to check crypto separately since we need all of the crypto
1474 // extensions to enable the subtarget feature
1475 if (CPUFeatures[I] == "aes")
1476 crypto |= CAP_AES;
1477 else if (CPUFeatures[I] == "pmull")
1478 crypto |= CAP_PMULL;
1479 else if (CPUFeatures[I] == "sha1")
1480 crypto |= CAP_SHA1;
1481 else if (CPUFeatures[I] == "sha2")
1482 crypto |= CAP_SHA2;
1483#endif
1484
1485 if (LLVMFeatureStr != "")
1486 Features[LLVMFeatureStr] = true;
1487 }
1488
1489#if defined(__aarch64__)
1490 // If we have all crypto bits we can add the feature
1491 if (crypto == (CAP_AES | CAP_PMULL | CAP_SHA1 | CAP_SHA2))
1492 Features["crypto"] = true;
1493#endif
1494
1495 return true;
1496}
1497#else
1498bool sys::getHostCPUFeatures(StringMap<bool> &Features) { return false; }
1499#endif
1500
1501std::string sys::getProcessTriple() {
Alex Lorenz3803df32017-07-07 09:53:47 +00001502 std::string TargetTripleString = updateTripleOSVersion(LLVM_HOST_TRIPLE);
1503 Triple PT(Triple::normalize(TargetTripleString));
Simon Pilgrima271c542017-05-03 15:42:29 +00001504
1505 if (sizeof(void *) == 8 && PT.isArch32Bit())
1506 PT = PT.get64BitArchVariant();
1507 if (sizeof(void *) == 4 && PT.isArch64Bit())
1508 PT = PT.get32BitArchVariant();
1509
1510 return PT.str();
1511}