1 //===-- Host.cpp - Implement OS Host Concept --------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the operating system Host concept.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/Host.h"
15 #include "llvm/Support/TargetParser.h"
16 #include "llvm/ADT/SmallSet.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/StringSwitch.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/Config/config.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/FileSystem.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/raw_ostream.h"
29 // Include the platform-specific parts of this class.
31 #include "Unix/Host.inc"
34 #include "Windows/Host.inc"
39 #if defined(__APPLE__) && (defined(__ppc__) || defined(__powerpc__))
40 #include <mach/host_info.h>
41 #include <mach/mach.h>
42 #include <mach/mach_host.h>
43 #include <mach/machine.h>
46 #define DEBUG_TYPE "host-detection"
48 //===----------------------------------------------------------------------===//
50 // Implementations of the CPU detection routines
52 //===----------------------------------------------------------------------===//
56 static std::unique_ptr
<llvm::MemoryBuffer
>
57 LLVM_ATTRIBUTE_UNUSED
getProcCpuinfoContent() {
58 llvm::ErrorOr
<std::unique_ptr
<llvm::MemoryBuffer
>> Text
=
59 llvm::MemoryBuffer::getFileAsStream("/proc/cpuinfo");
60 if (std::error_code EC
= Text
.getError()) {
61 llvm::errs() << "Can't read "
62 << "/proc/cpuinfo: " << EC
.message() << "\n";
65 return std::move(*Text
);
68 StringRef
sys::detail::getHostCPUNameForPowerPC(
69 const StringRef
&ProcCpuinfoContent
) {
70 // Access to the Processor Version Register (PVR) on PowerPC is privileged,
71 // and so we must use an operating-system interface to determine the current
72 // processor type. On Linux, this is exposed through the /proc/cpuinfo file.
73 const char *generic
= "generic";
75 // The cpu line is second (after the 'processor: 0' line), so if this
76 // buffer is too small then something has changed (or is wrong).
77 StringRef::const_iterator CPUInfoStart
= ProcCpuinfoContent
.begin();
78 StringRef::const_iterator CPUInfoEnd
= ProcCpuinfoContent
.end();
80 StringRef::const_iterator CIP
= CPUInfoStart
;
82 StringRef::const_iterator CPUStart
= 0;
85 // We need to find the first line which starts with cpu, spaces, and a colon.
86 // After the colon, there may be some additional spaces and then the cpu type.
87 while (CIP
< CPUInfoEnd
&& CPUStart
== 0) {
88 if (CIP
< CPUInfoEnd
&& *CIP
== '\n')
91 if (CIP
< CPUInfoEnd
&& *CIP
== 'c') {
93 if (CIP
< CPUInfoEnd
&& *CIP
== 'p') {
95 if (CIP
< CPUInfoEnd
&& *CIP
== 'u') {
97 while (CIP
< CPUInfoEnd
&& (*CIP
== ' ' || *CIP
== '\t'))
100 if (CIP
< CPUInfoEnd
&& *CIP
== ':') {
102 while (CIP
< CPUInfoEnd
&& (*CIP
== ' ' || *CIP
== '\t'))
105 if (CIP
< CPUInfoEnd
) {
107 while (CIP
< CPUInfoEnd
&& (*CIP
!= ' ' && *CIP
!= '\t' &&
108 *CIP
!= ',' && *CIP
!= '\n'))
110 CPULen
= CIP
- CPUStart
;
118 while (CIP
< CPUInfoEnd
&& *CIP
!= '\n')
125 return StringSwitch
<const char *>(StringRef(CPUStart
, CPULen
))
126 .Case("604e", "604e")
128 .Case("7400", "7400")
129 .Case("7410", "7400")
130 .Case("7447", "7400")
131 .Case("7455", "7450")
133 .Case("POWER4", "970")
134 .Case("PPC970FX", "970")
135 .Case("PPC970MP", "970")
137 .Case("POWER5", "g5")
139 .Case("POWER6", "pwr6")
140 .Case("POWER7", "pwr7")
141 .Case("POWER8", "pwr8")
142 .Case("POWER8E", "pwr8")
143 .Case("POWER8NVL", "pwr8")
144 .Case("POWER9", "pwr9")
148 StringRef
sys::detail::getHostCPUNameForARM(
149 const StringRef
&ProcCpuinfoContent
) {
150 // The cpuid register on arm is not accessible from user space. On Linux,
151 // it is exposed through the /proc/cpuinfo file.
153 // Read 32 lines from /proc/cpuinfo, which should contain the CPU part line
155 SmallVector
<StringRef
, 32> Lines
;
156 ProcCpuinfoContent
.split(Lines
, "\n");
158 // Look for the CPU implementer line.
159 StringRef Implementer
;
161 for (unsigned I
= 0, E
= Lines
.size(); I
!= E
; ++I
) {
162 if (Lines
[I
].startswith("CPU implementer"))
163 Implementer
= Lines
[I
].substr(15).ltrim("\t :");
164 if (Lines
[I
].startswith("Hardware"))
165 Hardware
= Lines
[I
].substr(8).ltrim("\t :");
168 if (Implementer
== "0x41") { // ARM Ltd.
169 // MSM8992/8994 may give cpu part for the core that the kernel is running on,
170 // which is undeterministic and wrong. Always return cortex-a53 for these SoC.
171 if (Hardware
.endswith("MSM8994") || Hardware
.endswith("MSM8996"))
175 // Look for the CPU part line.
176 for (unsigned I
= 0, E
= Lines
.size(); I
!= E
; ++I
)
177 if (Lines
[I
].startswith("CPU part"))
178 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
179 // values correspond to the "Part number" in the CP15/c0 register. The
180 // contents are specified in the various processor manuals.
181 return StringSwitch
<const char *>(Lines
[I
].substr(8).ltrim("\t :"))
182 .Case("0x926", "arm926ej-s")
183 .Case("0xb02", "mpcore")
184 .Case("0xb36", "arm1136j-s")
185 .Case("0xb56", "arm1156t2-s")
186 .Case("0xb76", "arm1176jz-s")
187 .Case("0xc08", "cortex-a8")
188 .Case("0xc09", "cortex-a9")
189 .Case("0xc0f", "cortex-a15")
190 .Case("0xc20", "cortex-m0")
191 .Case("0xc23", "cortex-m3")
192 .Case("0xc24", "cortex-m4")
193 .Case("0xd04", "cortex-a35")
194 .Case("0xd03", "cortex-a53")
195 .Case("0xd07", "cortex-a57")
196 .Case("0xd08", "cortex-a72")
197 .Case("0xd09", "cortex-a73")
201 if (Implementer
== "0x51") // Qualcomm Technologies, Inc.
202 // Look for the CPU part line.
203 for (unsigned I
= 0, E
= Lines
.size(); I
!= E
; ++I
)
204 if (Lines
[I
].startswith("CPU part"))
205 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
206 // values correspond to the "Part number" in the CP15/c0 register. The
207 // contents are specified in the various processor manuals.
208 return StringSwitch
<const char *>(Lines
[I
].substr(8).ltrim("\t :"))
209 .Case("0x06f", "krait") // APQ8064
210 .Case("0x201", "kryo")
211 .Case("0x205", "kryo")
212 .Case("0x211", "kryo")
213 .Case("0x800", "cortex-a73")
214 .Case("0x801", "cortex-a73")
215 .Case("0xc00", "falkor")
216 .Case("0xc01", "saphira")
222 StringRef
sys::detail::getHostCPUNameForS390x(
223 const StringRef
&ProcCpuinfoContent
) {
224 // STIDP is a privileged operation, so use /proc/cpuinfo instead.
226 // The "processor 0:" line comes after a fair amount of other information,
227 // including a cache breakdown, but this should be plenty.
228 SmallVector
<StringRef
, 32> Lines
;
229 ProcCpuinfoContent
.split(Lines
, "\n");
231 // Look for the CPU features.
232 SmallVector
<StringRef
, 32> CPUFeatures
;
233 for (unsigned I
= 0, E
= Lines
.size(); I
!= E
; ++I
)
234 if (Lines
[I
].startswith("features")) {
235 size_t Pos
= Lines
[I
].find(":");
236 if (Pos
!= StringRef::npos
) {
237 Lines
[I
].drop_front(Pos
+ 1).split(CPUFeatures
, ' ');
242 // We need to check for the presence of vector support independently of
243 // the machine type, since we may only use the vector register set when
244 // supported by the kernel (and hypervisor).
245 bool HaveVectorSupport
= false;
246 for (unsigned I
= 0, E
= CPUFeatures
.size(); I
!= E
; ++I
) {
247 if (CPUFeatures
[I
] == "vx")
248 HaveVectorSupport
= true;
251 // Now check the processor machine type.
252 for (unsigned I
= 0, E
= Lines
.size(); I
!= E
; ++I
) {
253 if (Lines
[I
].startswith("processor ")) {
254 size_t Pos
= Lines
[I
].find("machine = ");
255 if (Pos
!= StringRef::npos
) {
256 Pos
+= sizeof("machine = ") - 1;
258 if (!Lines
[I
].drop_front(Pos
).getAsInteger(10, Id
)) {
259 if (Id
>= 3906 && HaveVectorSupport
)
261 if (Id
>= 2964 && HaveVectorSupport
)
276 StringRef
sys::detail::getHostCPUNameForBPF() {
277 #if !defined(__linux__) || !defined(__x86_64__)
280 uint8_t insns
[40] __attribute__ ((aligned (8))) =
281 /* BPF_MOV64_IMM(BPF_REG_0, 0) */
282 { 0xb7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
283 /* BPF_MOV64_IMM(BPF_REG_2, 1) */
284 0xb7, 0x2, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
285 /* BPF_JMP_REG(BPF_JLT, BPF_REG_0, BPF_REG_2, 1) */
286 0xad, 0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0,
287 /* BPF_MOV64_IMM(BPF_REG_0, 1) */
288 0xb7, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
289 /* BPF_EXIT_INSN() */
290 0x95, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
292 struct bpf_prog_load_attr
{
300 uint32_t kern_version
;
303 attr
.prog_type
= 1; /* BPF_PROG_TYPE_SOCKET_FILTER */
305 attr
.insns
= (uint64_t)insns
;
306 attr
.license
= (uint64_t)"DUMMY";
308 int fd
= syscall(321 /* __NR_bpf */, 5 /* BPF_PROG_LOAD */, &attr
, sizeof(attr
));
317 #if defined(__i386__) || defined(_M_IX86) || \
318 defined(__x86_64__) || defined(_M_X64)
320 enum VendorSignatures
{
321 SIG_INTEL
= 0x756e6547 /* Genu */,
322 SIG_AMD
= 0x68747541 /* Auth */
325 // This should be kept in sync with libcc/compiler-rt as it should be used
326 // by clang as a proxy for what's in libgcc/compiler-rt.
327 enum ProcessorFeatures
{
356 FEATURE_AVX5124VNNIW
,
357 FEATURE_AVX5124FMAPS
,
358 FEATURE_AVX512VPOPCNTDQ
,
359 // One bit free here.
360 // Features below here are not in libgcc/compiler-rt.
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
375 static bool isCpuIdSupported() {
376 #if defined(__GNUC__) || defined(__clang__)
377 #if defined(__i386__)
378 int __cpuid_supported
;
381 " movl %%eax,%%ecx\n"
382 " xorl $0x00200000,%%eax\n"
388 " cmpl %%eax,%%ecx\n"
392 : "=r"(__cpuid_supported
)
395 if (!__cpuid_supported
)
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.
405 static bool getX86CpuIDAndInfo(unsigned value
, unsigned *rEAX
, unsigned *rEBX
,
406 unsigned *rECX
, unsigned *rEDX
) {
407 #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"
413 "xchgq\t%%rbx, %%rsi\n\t"
414 : "=a"(*rEAX
), "=S"(*rEBX
), "=c"(*rECX
), "=d"(*rEDX
)
417 #elif defined(__i386__)
418 __asm__("movl\t%%ebx, %%esi\n\t"
420 "xchgl\t%%ebx, %%esi\n\t"
421 : "=a"(*rEAX
), "=S"(*rEBX
), "=c"(*rECX
), "=d"(*rEDX
)
427 #elif defined(_MSC_VER)
428 // The MSVC intrinsic is portable across x86 and x64.
430 __cpuid(registers
, value
);
431 *rEAX
= registers
[0];
432 *rEBX
= registers
[1];
433 *rECX
= registers
[2];
434 *rEDX
= registers
[3];
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,
444 static bool getX86CpuIDAndInfoEx(unsigned value
, unsigned subleaf
,
445 unsigned *rEAX
, unsigned *rEBX
, unsigned *rECX
,
447 #if defined(__GNUC__) || defined(__clang__)
448 #if defined(__x86_64__)
449 // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
450 // FIXME: should we save this for Clang?
451 __asm__("movq\t%%rbx, %%rsi\n\t"
453 "xchgq\t%%rbx, %%rsi\n\t"
454 : "=a"(*rEAX
), "=S"(*rEBX
), "=c"(*rECX
), "=d"(*rEDX
)
455 : "a"(value
), "c"(subleaf
));
457 #elif defined(__i386__)
458 __asm__("movl\t%%ebx, %%esi\n\t"
460 "xchgl\t%%ebx, %%esi\n\t"
461 : "=a"(*rEAX
), "=S"(*rEBX
), "=c"(*rECX
), "=d"(*rEDX
)
462 : "a"(value
), "c"(subleaf
));
467 #elif defined(_MSC_VER)
469 __cpuidex(registers
, value
, subleaf
);
470 *rEAX
= registers
[0];
471 *rEBX
= registers
[1];
472 *rECX
= registers
[2];
473 *rEDX
= registers
[3];
480 // Read control register 0 (XCR0). Used to detect features such as AVX.
481 static bool getX86XCR0(unsigned *rEAX
, unsigned *rEDX
) {
482 #if defined(__GNUC__) || defined(__clang__)
483 // Check xgetbv; this uses a .byte sequence instead of the instruction
484 // directly because older assemblers do not include support for xgetbv and
485 // there is no easy way to conditionally compile based on the assembler used.
486 __asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(*rEAX
), "=d"(*rEDX
) : "c"(0));
488 #elif defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK)
489 unsigned long long Result
= _xgetbv(_XCR_XFEATURE_ENABLED_MASK
);
491 *rEDX
= Result
>> 32;
498 static void detectX86FamilyModel(unsigned EAX
, unsigned *Family
,
500 *Family
= (EAX
>> 8) & 0xf; // Bits 8 - 11
501 *Model
= (EAX
>> 4) & 0xf; // Bits 4 - 7
502 if (*Family
== 6 || *Family
== 0xf) {
504 // Examine extended family ID if family ID is F.
505 *Family
+= (EAX
>> 20) & 0xff; // Bits 20 - 27
506 // Examine extended model ID if family ID is 6 or F.
507 *Model
+= ((EAX
>> 16) & 0xf) << 4; // Bits 16 - 19
512 getIntelProcessorTypeAndSubtype(unsigned Family
, unsigned Model
,
513 unsigned Brand_id
, unsigned Features
,
514 unsigned Features2
, unsigned *Type
,
520 *Type
= X86::INTEL_i386
;
523 *Type
= X86::INTEL_i486
;
526 if (Features
& (1 << FEATURE_MMX
)) {
527 *Type
= X86::INTEL_PENTIUM_MMX
;
530 *Type
= X86::INTEL_PENTIUM
;
534 case 0x01: // Pentium Pro processor
535 *Type
= X86::INTEL_PENTIUM_PRO
;
537 case 0x03: // Intel Pentium II OverDrive processor, Pentium II processor,
539 case 0x05: // Pentium II processor, model 05, Pentium II Xeon processor,
540 // model 05, and Intel Celeron processor, model 05
541 case 0x06: // Celeron processor, model 06
542 *Type
= X86::INTEL_PENTIUM_II
;
544 case 0x07: // Pentium III processor, model 07, and Pentium III Xeon
545 // processor, model 07
546 case 0x08: // Pentium III processor, model 08, Pentium III Xeon processor,
547 // model 08, and Celeron processor, model 08
548 case 0x0a: // Pentium III Xeon processor, model 0Ah
549 case 0x0b: // Pentium III processor, model 0Bh
550 *Type
= X86::INTEL_PENTIUM_III
;
552 case 0x09: // Intel Pentium M processor, Intel Celeron M processor model 09.
553 case 0x0d: // Intel Pentium M processor, Intel Celeron M processor, model
554 // 0Dh. All processors are manufactured using the 90 nm process.
555 case 0x15: // Intel EP80579 Integrated Processor and Intel EP80579
556 // Integrated Processor with Intel QuickAssist Technology
557 *Type
= X86::INTEL_PENTIUM_M
;
559 case 0x0e: // Intel Core Duo processor, Intel Core Solo processor, model
560 // 0Eh. All processors are manufactured using the 65 nm process.
561 *Type
= X86::INTEL_CORE_DUO
;
563 case 0x0f: // Intel Core 2 Duo processor, Intel Core 2 Duo mobile
564 // processor, Intel Core 2 Quad processor, Intel Core 2 Quad
565 // mobile processor, Intel Core 2 Extreme processor, Intel
566 // Pentium Dual-Core processor, Intel Xeon processor, model
567 // 0Fh. All processors are manufactured using the 65 nm process.
568 case 0x16: // Intel Celeron processor model 16h. All processors are
569 // manufactured using the 65 nm process
570 *Type
= X86::INTEL_CORE2
; // "core2"
571 *Subtype
= X86::INTEL_CORE2_65
;
573 case 0x17: // Intel Core 2 Extreme processor, Intel Xeon processor, model
574 // 17h. All processors are manufactured using the 45 nm process.
576 // 45nm: Penryn , Wolfdale, Yorkfield (XE)
577 case 0x1d: // Intel Xeon processor MP. All processors are manufactured using
578 // the 45 nm process.
579 *Type
= X86::INTEL_CORE2
; // "penryn"
580 *Subtype
= X86::INTEL_CORE2_45
;
582 case 0x1a: // Intel Core i7 processor and Intel Xeon processor. All
583 // processors are manufactured using the 45 nm process.
584 case 0x1e: // Intel(R) Core(TM) i7 CPU 870 @ 2.93GHz.
585 // As found in a Summer 2010 model iMac.
587 case 0x2e: // Nehalem EX
588 *Type
= X86::INTEL_COREI7
; // "nehalem"
589 *Subtype
= X86::INTEL_COREI7_NEHALEM
;
591 case 0x25: // Intel Core i7, laptop version.
592 case 0x2c: // Intel Core i7 processor and Intel Xeon processor. All
593 // processors are manufactured using the 32 nm process.
594 case 0x2f: // Westmere EX
595 *Type
= X86::INTEL_COREI7
; // "westmere"
596 *Subtype
= X86::INTEL_COREI7_WESTMERE
;
598 case 0x2a: // Intel Core i7 processor. All processors are manufactured
599 // using the 32 nm process.
601 *Type
= X86::INTEL_COREI7
; //"sandybridge"
602 *Subtype
= X86::INTEL_COREI7_SANDYBRIDGE
;
605 case 0x3e: // Ivy Bridge EP
606 *Type
= X86::INTEL_COREI7
; // "ivybridge"
607 *Subtype
= X86::INTEL_COREI7_IVYBRIDGE
;
615 *Type
= X86::INTEL_COREI7
; // "haswell"
616 *Subtype
= X86::INTEL_COREI7_HASWELL
;
624 *Type
= X86::INTEL_COREI7
; // "broadwell"
625 *Subtype
= X86::INTEL_COREI7_BROADWELL
;
629 case 0x4e: // Skylake mobile
630 case 0x5e: // Skylake desktop
631 case 0x8e: // Kaby Lake mobile
632 case 0x9e: // Kaby Lake desktop
633 *Type
= X86::INTEL_COREI7
; // "skylake"
634 *Subtype
= X86::INTEL_COREI7_SKYLAKE
;
639 *Type
= X86::INTEL_COREI7
;
640 *Subtype
= X86::INTEL_COREI7_SKYLAKE_AVX512
; // "skylake-avx512"
645 *Type
= X86::INTEL_COREI7
;
646 *Subtype
= X86::INTEL_COREI7_CANNONLAKE
; // "cannonlake"
649 case 0x1c: // Most 45 nm Intel Atom processors
650 case 0x26: // 45 nm Atom Lincroft
651 case 0x27: // 32 nm Atom Medfield
652 case 0x35: // 32 nm Atom Midview
653 case 0x36: // 32 nm Atom Midview
654 *Type
= X86::INTEL_BONNELL
;
657 // Atom Silvermont codes from the Intel software optimization guide.
663 case 0x4c: // really airmont
664 *Type
= X86::INTEL_SILVERMONT
;
665 break; // "silvermont"
667 case 0x5c: // Apollo Lake
668 case 0x5f: // Denverton
669 case 0x7a: // Gemini Lake
670 *Type
= X86::INTEL_GOLDMONT
;
673 *Type
= X86::INTEL_KNL
; // knl
676 *Type
= X86::INTEL_KNM
; // knm
679 default: // Unknown family 6 CPU, try to guess.
680 if (Features
& (1 << FEATURE_AVX512VBMI
)) {
681 *Type
= X86::INTEL_COREI7
;
682 *Subtype
= X86::INTEL_COREI7_CANNONLAKE
;
686 if (Features
& (1 << FEATURE_AVX512VL
)) {
687 *Type
= X86::INTEL_COREI7
;
688 *Subtype
= X86::INTEL_COREI7_SKYLAKE_AVX512
;
692 if (Features
& (1 << FEATURE_AVX512ER
)) {
693 *Type
= X86::INTEL_KNL
; // knl
697 if (Features2
& (1 << (FEATURE_CLFLUSHOPT
- 32))) {
698 if (Features2
& (1 << (FEATURE_SHA
- 32))) {
699 *Type
= X86::INTEL_GOLDMONT
;
701 *Type
= X86::INTEL_COREI7
;
702 *Subtype
= X86::INTEL_COREI7_SKYLAKE
;
706 if (Features2
& (1 << (FEATURE_ADX
- 32))) {
707 *Type
= X86::INTEL_COREI7
;
708 *Subtype
= X86::INTEL_COREI7_BROADWELL
;
711 if (Features
& (1 << FEATURE_AVX2
)) {
712 *Type
= X86::INTEL_COREI7
;
713 *Subtype
= X86::INTEL_COREI7_HASWELL
;
716 if (Features
& (1 << FEATURE_AVX
)) {
717 *Type
= X86::INTEL_COREI7
;
718 *Subtype
= X86::INTEL_COREI7_SANDYBRIDGE
;
721 if (Features
& (1 << FEATURE_SSE4_2
)) {
722 if (Features2
& (1 << (FEATURE_MOVBE
- 32))) {
723 *Type
= X86::INTEL_SILVERMONT
;
725 *Type
= X86::INTEL_COREI7
;
726 *Subtype
= X86::INTEL_COREI7_NEHALEM
;
730 if (Features
& (1 << FEATURE_SSE4_1
)) {
731 *Type
= X86::INTEL_CORE2
; // "penryn"
732 *Subtype
= X86::INTEL_CORE2_45
;
735 if (Features
& (1 << FEATURE_SSSE3
)) {
736 if (Features2
& (1 << (FEATURE_MOVBE
- 32))) {
737 *Type
= X86::INTEL_BONNELL
; // "bonnell"
739 *Type
= X86::INTEL_CORE2
; // "core2"
740 *Subtype
= X86::INTEL_CORE2_65
;
744 if (Features2
& (1 << (FEATURE_EM64T
- 32))) {
745 *Type
= X86::INTEL_CORE2
; // "core2"
746 *Subtype
= X86::INTEL_CORE2_65
;
749 if (Features
& (1 << FEATURE_SSE3
)) {
750 *Type
= X86::INTEL_CORE_DUO
;
753 if (Features
& (1 << FEATURE_SSE2
)) {
754 *Type
= X86::INTEL_PENTIUM_M
;
757 if (Features
& (1 << FEATURE_SSE
)) {
758 *Type
= X86::INTEL_PENTIUM_III
;
761 if (Features
& (1 << FEATURE_MMX
)) {
762 *Type
= X86::INTEL_PENTIUM_II
;
765 *Type
= X86::INTEL_PENTIUM_PRO
;
770 if (Features2
& (1 << (FEATURE_EM64T
- 32))) {
771 *Type
= X86::INTEL_NOCONA
;
774 if (Features
& (1 << FEATURE_SSE3
)) {
775 *Type
= X86::INTEL_PRESCOTT
;
778 *Type
= X86::INTEL_PENTIUM_IV
;
786 static void getAMDProcessorTypeAndSubtype(unsigned Family
, unsigned Model
,
787 unsigned Features
, unsigned *Type
,
789 // FIXME: this poorly matches the generated SubtargetFeatureKV table. There
790 // appears to be no way to generate the wide variety of AMD-specific targets
791 // from the information returned from CPUID.
794 *Type
= X86::AMD_i486
;
797 *Type
= X86::AMDPENTIUM
;
801 *Subtype
= X86::AMDPENTIUM_K6
;
804 *Subtype
= X86::AMDPENTIUM_K62
;
808 *Subtype
= X86::AMDPENTIUM_K63
;
811 *Subtype
= X86::AMDPENTIUM_GEODE
;
816 if (Features
& (1 << FEATURE_SSE
)) {
817 *Type
= X86::AMD_ATHLON_XP
;
818 break; // "athlon-xp"
820 *Type
= X86::AMD_ATHLON
;
823 if (Features
& (1 << FEATURE_SSE3
)) {
824 *Type
= X86::AMD_K8SSE3
;
830 *Type
= X86::AMDFAM10H
; // "amdfam10"
833 *Subtype
= X86::AMDFAM10H_BARCELONA
;
836 *Subtype
= X86::AMDFAM10H_SHANGHAI
;
839 *Subtype
= X86::AMDFAM10H_ISTANBUL
;
844 *Type
= X86::AMD_BTVER1
;
847 *Type
= X86::AMDFAM15H
;
848 if (Model
>= 0x60 && Model
<= 0x7f) {
849 *Subtype
= X86::AMDFAM15H_BDVER4
;
850 break; // "bdver4"; 60h-7Fh: Excavator
852 if (Model
>= 0x30 && Model
<= 0x3f) {
853 *Subtype
= X86::AMDFAM15H_BDVER3
;
854 break; // "bdver3"; 30h-3Fh: Steamroller
856 if (Model
>= 0x10 && Model
<= 0x1f) {
857 *Subtype
= X86::AMDFAM15H_BDVER2
;
858 break; // "bdver2"; 10h-1Fh: Piledriver
861 *Subtype
= X86::AMDFAM15H_BDVER1
;
862 break; // "bdver1"; 00h-0Fh: Bulldozer
866 *Type
= X86::AMD_BTVER2
;
869 *Type
= X86::AMDFAM17H
;
870 *Subtype
= X86::AMDFAM17H_ZNVER1
;
877 static void getAvailableFeatures(unsigned ECX
, unsigned EDX
, unsigned MaxLeaf
,
878 unsigned *FeaturesOut
,
879 unsigned *Features2Out
) {
880 unsigned Features
= 0;
881 unsigned Features2
= 0;
885 Features
|= 1 << FEATURE_CMOV
;
887 Features
|= 1 << FEATURE_MMX
;
889 Features
|= 1 << FEATURE_SSE
;
891 Features
|= 1 << FEATURE_SSE2
;
894 Features
|= 1 << FEATURE_SSE3
;
896 Features
|= 1 << FEATURE_PCLMUL
;
898 Features
|= 1 << FEATURE_SSSE3
;
900 Features
|= 1 << FEATURE_FMA
;
902 Features
|= 1 << FEATURE_SSE4_1
;
904 Features
|= 1 << FEATURE_SSE4_2
;
906 Features
|= 1 << FEATURE_POPCNT
;
908 Features
|= 1 << FEATURE_AES
;
911 Features2
|= 1 << (FEATURE_MOVBE
- 32);
913 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
914 // indicates that the AVX registers will be saved and restored on context
915 // switch, then we have full AVX support.
916 const unsigned AVXBits
= (1 << 27) | (1 << 28);
917 bool HasAVX
= ((ECX
& AVXBits
) == AVXBits
) && !getX86XCR0(&EAX
, &EDX
) &&
918 ((EAX
& 0x6) == 0x6);
919 bool HasAVX512Save
= HasAVX
&& ((EAX
& 0xe0) == 0xe0);
922 Features
|= 1 << FEATURE_AVX
;
925 MaxLeaf
>= 0x7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX
, &EBX
, &ECX
, &EDX
);
927 if (HasLeaf7
&& ((EBX
>> 3) & 1))
928 Features
|= 1 << FEATURE_BMI
;
929 if (HasLeaf7
&& ((EBX
>> 5) & 1) && HasAVX
)
930 Features
|= 1 << FEATURE_AVX2
;
931 if (HasLeaf7
&& ((EBX
>> 9) & 1))
932 Features
|= 1 << FEATURE_BMI2
;
933 if (HasLeaf7
&& ((EBX
>> 16) & 1) && HasAVX512Save
)
934 Features
|= 1 << FEATURE_AVX512F
;
935 if (HasLeaf7
&& ((EBX
>> 17) & 1) && HasAVX512Save
)
936 Features
|= 1 << FEATURE_AVX512DQ
;
937 if (HasLeaf7
&& ((EBX
>> 19) & 1))
938 Features2
|= 1 << (FEATURE_ADX
- 32);
939 if (HasLeaf7
&& ((EBX
>> 21) & 1) && HasAVX512Save
)
940 Features
|= 1 << FEATURE_AVX512IFMA
;
941 if (HasLeaf7
&& ((EBX
>> 23) & 1))
942 Features2
|= 1 << (FEATURE_CLFLUSHOPT
- 32);
943 if (HasLeaf7
&& ((EBX
>> 26) & 1) && HasAVX512Save
)
944 Features
|= 1 << FEATURE_AVX512PF
;
945 if (HasLeaf7
&& ((EBX
>> 27) & 1) && HasAVX512Save
)
946 Features
|= 1 << FEATURE_AVX512ER
;
947 if (HasLeaf7
&& ((EBX
>> 28) & 1) && HasAVX512Save
)
948 Features
|= 1 << FEATURE_AVX512CD
;
949 if (HasLeaf7
&& ((EBX
>> 29) & 1))
950 Features2
|= 1 << (FEATURE_SHA
- 32);
951 if (HasLeaf7
&& ((EBX
>> 30) & 1) && HasAVX512Save
)
952 Features
|= 1 << FEATURE_AVX512BW
;
953 if (HasLeaf7
&& ((EBX
>> 31) & 1) && HasAVX512Save
)
954 Features
|= 1 << FEATURE_AVX512VL
;
956 if (HasLeaf7
&& ((ECX
>> 1) & 1) && HasAVX512Save
)
957 Features
|= 1 << FEATURE_AVX512VBMI
;
958 if (HasLeaf7
&& ((ECX
>> 14) & 1) && HasAVX512Save
)
959 Features
|= 1 << FEATURE_AVX512VPOPCNTDQ
;
961 if (HasLeaf7
&& ((EDX
>> 2) & 1) && HasAVX512Save
)
962 Features
|= 1 << FEATURE_AVX5124VNNIW
;
963 if (HasLeaf7
&& ((EDX
>> 3) & 1) && HasAVX512Save
)
964 Features
|= 1 << FEATURE_AVX5124FMAPS
;
966 unsigned MaxExtLevel
;
967 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel
, &EBX
, &ECX
, &EDX
);
969 bool HasExtLeaf1
= MaxExtLevel
>= 0x80000001 &&
970 !getX86CpuIDAndInfo(0x80000001, &EAX
, &EBX
, &ECX
, &EDX
);
971 if (HasExtLeaf1
&& ((ECX
>> 6) & 1))
972 Features
|= 1 << FEATURE_SSE4_A
;
973 if (HasExtLeaf1
&& ((ECX
>> 11) & 1))
974 Features
|= 1 << FEATURE_XOP
;
975 if (HasExtLeaf1
&& ((ECX
>> 16) & 1))
976 Features
|= 1 << FEATURE_FMA4
;
978 if (HasExtLeaf1
&& ((EDX
>> 29) & 1))
979 Features2
|= 1 << (FEATURE_EM64T
- 32);
981 *FeaturesOut
= Features
;
982 *Features2Out
= Features2
;
985 StringRef
sys::getHostCPUName() {
986 unsigned EAX
= 0, EBX
= 0, ECX
= 0, EDX
= 0;
987 unsigned MaxLeaf
, Vendor
;
989 #if defined(__GNUC__) || defined(__clang__)
990 //FIXME: include cpuid.h from clang or copy __get_cpuid_max here
991 // and simplify it to not invoke __cpuid (like cpu_model.c in
992 // compiler-rt/lib/builtins/cpu_model.c?
993 // Opting for the second option.
994 if(!isCpuIdSupported())
997 if (getX86CpuIDAndInfo(0, &MaxLeaf
, &Vendor
, &ECX
, &EDX
) || MaxLeaf
< 1)
999 getX86CpuIDAndInfo(0x1, &EAX
, &EBX
, &ECX
, &EDX
);
1001 unsigned Brand_id
= EBX
& 0xff;
1002 unsigned Family
= 0, Model
= 0;
1003 unsigned Features
= 0, Features2
= 0;
1004 detectX86FamilyModel(EAX
, &Family
, &Model
);
1005 getAvailableFeatures(ECX
, EDX
, MaxLeaf
, &Features
, &Features2
);
1008 unsigned Subtype
= 0;
1010 if (Vendor
== SIG_INTEL
) {
1011 getIntelProcessorTypeAndSubtype(Family
, Model
, Brand_id
, Features
,
1012 Features2
, &Type
, &Subtype
);
1013 } else if (Vendor
== SIG_AMD
) {
1014 getAMDProcessorTypeAndSubtype(Family
, Model
, Features
, &Type
, &Subtype
);
1017 // Check subtypes first since those are more specific.
1018 #define X86_CPU_SUBTYPE(ARCHNAME, ENUM) \
1019 if (Subtype == X86::ENUM) \
1021 #include "llvm/Support/X86TargetParser.def"
1024 #define X86_CPU_SUBTYPE(ARCHNAME, ENUM) \
1025 if (Type == X86::ENUM) \
1027 #include "llvm/Support/X86TargetParser.def"
1032 #elif defined(__APPLE__) && (defined(__ppc__) || defined(__powerpc__))
1033 StringRef
sys::getHostCPUName() {
1034 host_basic_info_data_t hostInfo
;
1035 mach_msg_type_number_t infoCount
;
1037 infoCount
= HOST_BASIC_INFO_COUNT
;
1038 host_info(mach_host_self(), HOST_BASIC_INFO
, (host_info_t
)&hostInfo
,
1041 if (hostInfo
.cpu_type
!= CPU_TYPE_POWERPC
)
1044 switch (hostInfo
.cpu_subtype
) {
1045 case CPU_SUBTYPE_POWERPC_601
:
1047 case CPU_SUBTYPE_POWERPC_602
:
1049 case CPU_SUBTYPE_POWERPC_603
:
1051 case CPU_SUBTYPE_POWERPC_603e
:
1053 case CPU_SUBTYPE_POWERPC_603ev
:
1055 case CPU_SUBTYPE_POWERPC_604
:
1057 case CPU_SUBTYPE_POWERPC_604e
:
1059 case CPU_SUBTYPE_POWERPC_620
:
1061 case CPU_SUBTYPE_POWERPC_750
:
1063 case CPU_SUBTYPE_POWERPC_7400
:
1065 case CPU_SUBTYPE_POWERPC_7450
:
1067 case CPU_SUBTYPE_POWERPC_970
:
1074 #elif defined(__linux__) && (defined(__ppc__) || defined(__powerpc__))
1075 StringRef
sys::getHostCPUName() {
1076 std::unique_ptr
<llvm::MemoryBuffer
> P
= getProcCpuinfoContent();
1077 const StringRef
& Content
= P
? P
->getBuffer() : "";
1078 return detail::getHostCPUNameForPowerPC(Content
);
1080 #elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1081 StringRef
sys::getHostCPUName() {
1082 std::unique_ptr
<llvm::MemoryBuffer
> P
= getProcCpuinfoContent();
1083 const StringRef
& Content
= P
? P
->getBuffer() : "";
1084 return detail::getHostCPUNameForARM(Content
);
1086 #elif defined(__linux__) && defined(__s390x__)
1087 StringRef
sys::getHostCPUName() {
1088 std::unique_ptr
<llvm::MemoryBuffer
> P
= getProcCpuinfoContent();
1089 const StringRef
& Content
= P
? P
->getBuffer() : "";
1090 return detail::getHostCPUNameForS390x(Content
);
1093 StringRef
sys::getHostCPUName() { return "generic"; }
1096 #if defined(__linux__) && defined(__x86_64__)
1097 // On Linux, the number of physical cores can be computed from /proc/cpuinfo,
1098 // using the number of unique physical/core id pairs. The following
1099 // implementation reads the /proc/cpuinfo format on an x86_64 system.
1100 static int computeHostNumPhysicalCores() {
1101 // Read /proc/cpuinfo as a stream (until EOF reached). It cannot be
1102 // mmapped because it appears to have 0 size.
1103 llvm::ErrorOr
<std::unique_ptr
<llvm::MemoryBuffer
>> Text
=
1104 llvm::MemoryBuffer::getFileAsStream("/proc/cpuinfo");
1105 if (std::error_code EC
= Text
.getError()) {
1106 llvm::errs() << "Can't read "
1107 << "/proc/cpuinfo: " << EC
.message() << "\n";
1110 SmallVector
<StringRef
, 8> strs
;
1111 (*Text
)->getBuffer().split(strs
, "\n", /*MaxSplit=*/-1,
1112 /*KeepEmpty=*/false);
1113 int CurPhysicalId
= -1;
1115 SmallSet
<std::pair
<int, int>, 32> UniqueItems
;
1116 for (auto &Line
: strs
) {
1118 if (!Line
.startswith("physical id") && !Line
.startswith("core id"))
1120 std::pair
<StringRef
, StringRef
> Data
= Line
.split(':');
1121 auto Name
= Data
.first
.trim();
1122 auto Val
= Data
.second
.trim();
1123 if (Name
== "physical id") {
1124 assert(CurPhysicalId
== -1 &&
1125 "Expected a core id before seeing another physical id");
1126 Val
.getAsInteger(10, CurPhysicalId
);
1128 if (Name
== "core id") {
1129 assert(CurCoreId
== -1 &&
1130 "Expected a physical id before seeing another core id");
1131 Val
.getAsInteger(10, CurCoreId
);
1133 if (CurPhysicalId
!= -1 && CurCoreId
!= -1) {
1134 UniqueItems
.insert(std::make_pair(CurPhysicalId
, CurCoreId
));
1139 return UniqueItems
.size();
1141 #elif defined(__APPLE__) && defined(__x86_64__)
1142 #include <sys/param.h>
1143 #include <sys/sysctl.h>
1145 // Gets the number of *physical cores* on the machine.
1146 static int computeHostNumPhysicalCores() {
1148 size_t len
= sizeof(count
);
1149 sysctlbyname("hw.physicalcpu", &count
, &len
, NULL
, 0);
1153 nm
[1] = HW_AVAILCPU
;
1154 sysctl(nm
, 2, &count
, &len
, NULL
, 0);
1161 // On other systems, return -1 to indicate unknown.
1162 static int computeHostNumPhysicalCores() { return -1; }
1165 int sys::getHostNumPhysicalCores() {
1166 static int NumCores
= computeHostNumPhysicalCores();
1170 #if defined(__i386__) || defined(_M_IX86) || \
1171 defined(__x86_64__) || defined(_M_X64)
1172 bool sys::getHostCPUFeatures(StringMap
<bool> &Features
) {
1173 unsigned EAX
= 0, EBX
= 0, ECX
= 0, EDX
= 0;
1180 if (getX86CpuIDAndInfo(0, &MaxLevel
, text
.u
+ 0, text
.u
+ 2, text
.u
+ 1) ||
1184 getX86CpuIDAndInfo(1, &EAX
, &EBX
, &ECX
, &EDX
);
1186 Features
["cmov"] = (EDX
>> 15) & 1;
1187 Features
["mmx"] = (EDX
>> 23) & 1;
1188 Features
["sse"] = (EDX
>> 25) & 1;
1189 Features
["sse2"] = (EDX
>> 26) & 1;
1191 Features
["sse3"] = (ECX
>> 0) & 1;
1192 Features
["pclmul"] = (ECX
>> 1) & 1;
1193 Features
["ssse3"] = (ECX
>> 9) & 1;
1194 Features
["cx16"] = (ECX
>> 13) & 1;
1195 Features
["sse4.1"] = (ECX
>> 19) & 1;
1196 Features
["sse4.2"] = (ECX
>> 20) & 1;
1197 Features
["movbe"] = (ECX
>> 22) & 1;
1198 Features
["popcnt"] = (ECX
>> 23) & 1;
1199 Features
["aes"] = (ECX
>> 25) & 1;
1200 Features
["rdrnd"] = (ECX
>> 30) & 1;
1202 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
1203 // indicates that the AVX registers will be saved and restored on context
1204 // switch, then we have full AVX support.
1205 bool HasAVXSave
= ((ECX
>> 27) & 1) && ((ECX
>> 28) & 1) &&
1206 !getX86XCR0(&EAX
, &EDX
) && ((EAX
& 0x6) == 0x6);
1207 // AVX512 requires additional context to be saved by the OS.
1208 bool HasAVX512Save
= HasAVXSave
&& ((EAX
& 0xe0) == 0xe0);
1210 Features
["avx"] = HasAVXSave
;
1211 Features
["fma"] = ((ECX
>> 12) & 1) && HasAVXSave
;
1212 // Only enable XSAVE if OS has enabled support for saving YMM state.
1213 Features
["xsave"] = ((ECX
>> 26) & 1) && HasAVXSave
;
1214 Features
["f16c"] = ((ECX
>> 29) & 1) && HasAVXSave
;
1216 unsigned MaxExtLevel
;
1217 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel
, &EBX
, &ECX
, &EDX
);
1219 bool HasExtLeaf1
= MaxExtLevel
>= 0x80000001 &&
1220 !getX86CpuIDAndInfo(0x80000001, &EAX
, &EBX
, &ECX
, &EDX
);
1221 Features
["lzcnt"] = HasExtLeaf1
&& ((ECX
>> 5) & 1);
1222 Features
["sse4a"] = HasExtLeaf1
&& ((ECX
>> 6) & 1);
1223 Features
["prfchw"] = HasExtLeaf1
&& ((ECX
>> 8) & 1);
1224 Features
["xop"] = HasExtLeaf1
&& ((ECX
>> 11) & 1) && HasAVXSave
;
1225 Features
["lwp"] = HasExtLeaf1
&& ((ECX
>> 15) & 1);
1226 Features
["fma4"] = HasExtLeaf1
&& ((ECX
>> 16) & 1) && HasAVXSave
;
1227 Features
["tbm"] = HasExtLeaf1
&& ((ECX
>> 21) & 1);
1228 Features
["mwaitx"] = HasExtLeaf1
&& ((ECX
>> 29) & 1);
1230 bool HasExtLeaf8
= MaxExtLevel
>= 0x80000008 &&
1231 !getX86CpuIDAndInfo(0x80000008, &EAX
, &EBX
, &ECX
, &EDX
);
1232 Features
["clzero"] = HasExtLeaf8
&& ((EBX
>> 0) & 1);
1235 MaxLevel
>= 7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX
, &EBX
, &ECX
, &EDX
);
1237 Features
["fsgsbase"] = HasLeaf7
&& ((EBX
>> 0) & 1);
1238 Features
["sgx"] = HasLeaf7
&& ((EBX
>> 2) & 1);
1239 Features
["bmi"] = HasLeaf7
&& ((EBX
>> 3) & 1);
1240 // AVX2 is only supported if we have the OS save support from AVX.
1241 Features
["avx2"] = HasLeaf7
&& ((EBX
>> 5) & 1) && HasAVXSave
;
1242 Features
["bmi2"] = HasLeaf7
&& ((EBX
>> 8) & 1);
1243 Features
["rtm"] = HasLeaf7
&& ((EBX
>> 11) & 1);
1244 // AVX512 is only supported if the OS supports the context save for it.
1245 Features
["avx512f"] = HasLeaf7
&& ((EBX
>> 16) & 1) && HasAVX512Save
;
1246 Features
["avx512dq"] = HasLeaf7
&& ((EBX
>> 17) & 1) && HasAVX512Save
;
1247 Features
["rdseed"] = HasLeaf7
&& ((EBX
>> 18) & 1);
1248 Features
["adx"] = HasLeaf7
&& ((EBX
>> 19) & 1);
1249 Features
["avx512ifma"] = HasLeaf7
&& ((EBX
>> 21) & 1) && HasAVX512Save
;
1250 Features
["clflushopt"] = HasLeaf7
&& ((EBX
>> 23) & 1);
1251 Features
["clwb"] = HasLeaf7
&& ((EBX
>> 24) & 1);
1252 Features
["avx512pf"] = HasLeaf7
&& ((EBX
>> 26) & 1) && HasAVX512Save
;
1253 Features
["avx512er"] = HasLeaf7
&& ((EBX
>> 27) & 1) && HasAVX512Save
;
1254 Features
["avx512cd"] = HasLeaf7
&& ((EBX
>> 28) & 1) && HasAVX512Save
;
1255 Features
["sha"] = HasLeaf7
&& ((EBX
>> 29) & 1);
1256 Features
["avx512bw"] = HasLeaf7
&& ((EBX
>> 30) & 1) && HasAVX512Save
;
1257 Features
["avx512vl"] = HasLeaf7
&& ((EBX
>> 31) & 1) && HasAVX512Save
;
1259 Features
["prefetchwt1"] = HasLeaf7
&& ((ECX
>> 0) & 1);
1260 Features
["avx512vbmi"] = HasLeaf7
&& ((ECX
>> 1) & 1) && HasAVX512Save
;
1261 Features
["pku"] = HasLeaf7
&& ((ECX
>> 4) & 1);
1262 Features
["avx512vbmi2"] = HasLeaf7
&& ((ECX
>> 6) & 1) && HasAVX512Save
;
1263 Features
["vaes"] = HasLeaf7
&& ((ECX
>> 9) & 1) && HasAVXSave
;
1264 Features
["vpclmulqdq"] = HasLeaf7
&& ((ECX
>> 10) & 1) && HasAVXSave
;
1265 Features
["avx512vnni"] = HasLeaf7
&& ((ECX
>> 11) & 1) && HasAVX512Save
;
1266 Features
["avx512bitalg"] = HasLeaf7
&& ((ECX
>> 12) & 1) && HasAVX512Save
;
1267 Features
["avx512vpopcntdq"] = HasLeaf7
&& ((ECX
>> 14) & 1) && HasAVX512Save
;
1269 bool HasLeafD
= MaxLevel
>= 0xd &&
1270 !getX86CpuIDAndInfoEx(0xd, 0x1, &EAX
, &EBX
, &ECX
, &EDX
);
1272 // Only enable XSAVE if OS has enabled support for saving YMM state.
1273 Features
["xsaveopt"] = HasLeafD
&& ((EAX
>> 0) & 1) && HasAVXSave
;
1274 Features
["xsavec"] = HasLeafD
&& ((EAX
>> 1) & 1) && HasAVXSave
;
1275 Features
["xsaves"] = HasLeafD
&& ((EAX
>> 3) & 1) && HasAVXSave
;
1279 #elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1280 bool sys::getHostCPUFeatures(StringMap
<bool> &Features
) {
1281 std::unique_ptr
<llvm::MemoryBuffer
> P
= getProcCpuinfoContent();
1285 SmallVector
<StringRef
, 32> Lines
;
1286 P
->getBuffer().split(Lines
, "\n");
1288 SmallVector
<StringRef
, 32> CPUFeatures
;
1290 // Look for the CPU features.
1291 for (unsigned I
= 0, E
= Lines
.size(); I
!= E
; ++I
)
1292 if (Lines
[I
].startswith("Features")) {
1293 Lines
[I
].split(CPUFeatures
, ' ');
1297 #if defined(__aarch64__)
1298 // Keep track of which crypto features we have seen
1299 enum { CAP_AES
= 0x1, CAP_PMULL
= 0x2, CAP_SHA1
= 0x4, CAP_SHA2
= 0x8 };
1300 uint32_t crypto
= 0;
1303 for (unsigned I
= 0, E
= CPUFeatures
.size(); I
!= E
; ++I
) {
1304 StringRef LLVMFeatureStr
= StringSwitch
<StringRef
>(CPUFeatures
[I
])
1305 #if defined(__aarch64__)
1306 .Case("asimd", "neon")
1307 .Case("fp", "fp-armv8")
1308 .Case("crc32", "crc")
1310 .Case("half", "fp16")
1311 .Case("neon", "neon")
1312 .Case("vfpv3", "vfp3")
1313 .Case("vfpv3d16", "d16")
1314 .Case("vfpv4", "vfp4")
1315 .Case("idiva", "hwdiv-arm")
1316 .Case("idivt", "hwdiv")
1320 #if defined(__aarch64__)
1321 // We need to check crypto separately since we need all of the crypto
1322 // extensions to enable the subtarget feature
1323 if (CPUFeatures
[I
] == "aes")
1325 else if (CPUFeatures
[I
] == "pmull")
1326 crypto
|= CAP_PMULL
;
1327 else if (CPUFeatures
[I
] == "sha1")
1329 else if (CPUFeatures
[I
] == "sha2")
1333 if (LLVMFeatureStr
!= "")
1334 Features
[LLVMFeatureStr
] = true;
1337 #if defined(__aarch64__)
1338 // If we have all crypto bits we can add the feature
1339 if (crypto
== (CAP_AES
| CAP_PMULL
| CAP_SHA1
| CAP_SHA2
))
1340 Features
["crypto"] = true;
1346 bool sys::getHostCPUFeatures(StringMap
<bool> &Features
) { return false; }
1349 std::string
sys::getProcessTriple() {
1350 std::string TargetTripleString
= updateTripleOSVersion(LLVM_HOST_TRIPLE
);
1351 Triple
PT(Triple::normalize(TargetTripleString
));
1353 if (sizeof(void *) == 8 && PT
.isArch32Bit())
1354 PT
= PT
.get64BitArchVariant();
1355 if (sizeof(void *) == 4 && PT
.isArch64Bit())
1356 PT
= PT
.get32BitArchVariant();