1 //===-- Host.cpp - Implement OS Host Concept --------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the operating system Host concept.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/Host.h"
14 #include "llvm/Support/TargetParser.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/llvm-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"
28 // Include the platform-specific parts of this class.
30 #include "Unix/Host.inc"
33 #include "Windows/Host.inc"
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>
45 #define DEBUG_TYPE "host-detection"
47 //===----------------------------------------------------------------------===//
49 // Implementations of the CPU detection routines
51 //===----------------------------------------------------------------------===//
55 static 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";
64 return std::move(*Text
);
67 StringRef
sys::detail::getHostCPUNameForPowerPC(StringRef ProcCpuinfoContent
) {
68 // Access to the Processor Version Register (PVR) on PowerPC is privileged,
69 // and so we must use an operating-system interface to determine the current
70 // processor type. On Linux, this is exposed through the /proc/cpuinfo file.
71 const char *generic
= "generic";
73 // The cpu line is second (after the 'processor: 0' line), so if this
74 // buffer is too small then something has changed (or is wrong).
75 StringRef::const_iterator CPUInfoStart
= ProcCpuinfoContent
.begin();
76 StringRef::const_iterator CPUInfoEnd
= ProcCpuinfoContent
.end();
78 StringRef::const_iterator CIP
= CPUInfoStart
;
80 StringRef::const_iterator CPUStart
= 0;
83 // We need to find the first line which starts with cpu, spaces, and a colon.
84 // After the colon, there may be some additional spaces and then the cpu type.
85 while (CIP
< CPUInfoEnd
&& CPUStart
== 0) {
86 if (CIP
< CPUInfoEnd
&& *CIP
== '\n')
89 if (CIP
< CPUInfoEnd
&& *CIP
== 'c') {
91 if (CIP
< CPUInfoEnd
&& *CIP
== 'p') {
93 if (CIP
< CPUInfoEnd
&& *CIP
== 'u') {
95 while (CIP
< CPUInfoEnd
&& (*CIP
== ' ' || *CIP
== '\t'))
98 if (CIP
< CPUInfoEnd
&& *CIP
== ':') {
100 while (CIP
< CPUInfoEnd
&& (*CIP
== ' ' || *CIP
== '\t'))
103 if (CIP
< CPUInfoEnd
) {
105 while (CIP
< CPUInfoEnd
&& (*CIP
!= ' ' && *CIP
!= '\t' &&
106 *CIP
!= ',' && *CIP
!= '\n'))
108 CPULen
= CIP
- CPUStart
;
116 while (CIP
< CPUInfoEnd
&& *CIP
!= '\n')
123 return StringSwitch
<const char *>(StringRef(CPUStart
, CPULen
))
124 .Case("604e", "604e")
126 .Case("7400", "7400")
127 .Case("7410", "7400")
128 .Case("7447", "7400")
129 .Case("7455", "7450")
131 .Case("POWER4", "970")
132 .Case("PPC970FX", "970")
133 .Case("PPC970MP", "970")
135 .Case("POWER5", "g5")
137 .Case("POWER6", "pwr6")
138 .Case("POWER7", "pwr7")
139 .Case("POWER8", "pwr8")
140 .Case("POWER8E", "pwr8")
141 .Case("POWER8NVL", "pwr8")
142 .Case("POWER9", "pwr9")
146 StringRef
sys::detail::getHostCPUNameForARM(StringRef ProcCpuinfoContent
) {
147 // The cpuid register on arm is not accessible from user space. On Linux,
148 // it is exposed through the /proc/cpuinfo file.
150 // Read 32 lines from /proc/cpuinfo, which should contain the CPU part line
152 SmallVector
<StringRef
, 32> Lines
;
153 ProcCpuinfoContent
.split(Lines
, "\n");
155 // Look for the CPU implementer line.
156 StringRef Implementer
;
158 for (unsigned I
= 0, E
= Lines
.size(); I
!= E
; ++I
) {
159 if (Lines
[I
].startswith("CPU implementer"))
160 Implementer
= Lines
[I
].substr(15).ltrim("\t :");
161 if (Lines
[I
].startswith("Hardware"))
162 Hardware
= Lines
[I
].substr(8).ltrim("\t :");
165 if (Implementer
== "0x41") { // ARM Ltd.
166 // MSM8992/8994 may give cpu part for the core that the kernel is running on,
167 // which is undeterministic and wrong. Always return cortex-a53 for these SoC.
168 if (Hardware
.endswith("MSM8994") || Hardware
.endswith("MSM8996"))
172 // Look for the CPU part line.
173 for (unsigned I
= 0, E
= Lines
.size(); I
!= E
; ++I
)
174 if (Lines
[I
].startswith("CPU part"))
175 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
176 // values correspond to the "Part number" in the CP15/c0 register. The
177 // contents are specified in the various processor manuals.
178 return StringSwitch
<const char *>(Lines
[I
].substr(8).ltrim("\t :"))
179 .Case("0x926", "arm926ej-s")
180 .Case("0xb02", "mpcore")
181 .Case("0xb36", "arm1136j-s")
182 .Case("0xb56", "arm1156t2-s")
183 .Case("0xb76", "arm1176jz-s")
184 .Case("0xc08", "cortex-a8")
185 .Case("0xc09", "cortex-a9")
186 .Case("0xc0f", "cortex-a15")
187 .Case("0xc20", "cortex-m0")
188 .Case("0xc23", "cortex-m3")
189 .Case("0xc24", "cortex-m4")
190 .Case("0xd04", "cortex-a35")
191 .Case("0xd03", "cortex-a53")
192 .Case("0xd07", "cortex-a57")
193 .Case("0xd08", "cortex-a72")
194 .Case("0xd09", "cortex-a73")
198 if (Implementer
== "0x42" || Implementer
== "0x43") { // Broadcom | Cavium.
199 for (unsigned I
= 0, E
= Lines
.size(); I
!= E
; ++I
) {
200 if (Lines
[I
].startswith("CPU part")) {
201 return StringSwitch
<const char *>(Lines
[I
].substr(8).ltrim("\t :"))
202 .Case("0x516", "thunderx2t99")
203 .Case("0x0516", "thunderx2t99")
204 .Case("0xaf", "thunderx2t99")
205 .Case("0x0af", "thunderx2t99")
206 .Case("0xa1", "thunderxt88")
207 .Case("0x0a1", "thunderxt88")
213 if (Implementer
== "0x48") // HiSilicon Technologies, Inc.
214 // Look for the CPU part line.
215 for (unsigned I
= 0, E
= Lines
.size(); I
!= E
; ++I
)
216 if (Lines
[I
].startswith("CPU part"))
217 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
218 // values correspond to the "Part number" in the CP15/c0 register. The
219 // contents are specified in the various processor manuals.
220 return StringSwitch
<const char *>(Lines
[I
].substr(8).ltrim("\t :"))
221 .Case("0xd01", "tsv110")
224 if (Implementer
== "0x51") // Qualcomm Technologies, Inc.
225 // Look for the CPU part line.
226 for (unsigned I
= 0, E
= Lines
.size(); I
!= E
; ++I
)
227 if (Lines
[I
].startswith("CPU part"))
228 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
229 // values correspond to the "Part number" in the CP15/c0 register. The
230 // contents are specified in the various processor manuals.
231 return StringSwitch
<const char *>(Lines
[I
].substr(8).ltrim("\t :"))
232 .Case("0x06f", "krait") // APQ8064
233 .Case("0x201", "kryo")
234 .Case("0x205", "kryo")
235 .Case("0x211", "kryo")
236 .Case("0x800", "cortex-a73")
237 .Case("0x801", "cortex-a73")
238 .Case("0xc00", "falkor")
239 .Case("0xc01", "saphira")
242 if (Implementer
== "0x53") { // Samsung Electronics Co., Ltd.
243 // The Exynos chips have a convoluted ID scheme that doesn't seem to follow
244 // any predictive pattern across variants and parts.
245 unsigned Variant
= 0, Part
= 0;
247 // Look for the CPU variant line, whose value is a 1 digit hexadecimal
248 // number, corresponding to the Variant bits in the CP15/C0 register.
250 if (I
.consume_front("CPU variant"))
251 I
.ltrim("\t :").getAsInteger(0, Variant
);
253 // Look for the CPU part line, whose value is a 3 digit hexadecimal
254 // number, corresponding to the PartNum bits in the CP15/C0 register.
256 if (I
.consume_front("CPU part"))
257 I
.ltrim("\t :").getAsInteger(0, Part
);
259 unsigned Exynos
= (Variant
<< 12) | Part
;
262 // Default by falling through to Exynos M1.
276 StringRef
sys::detail::getHostCPUNameForS390x(StringRef ProcCpuinfoContent
) {
277 // STIDP is a privileged operation, so use /proc/cpuinfo instead.
279 // The "processor 0:" line comes after a fair amount of other information,
280 // including a cache breakdown, but this should be plenty.
281 SmallVector
<StringRef
, 32> Lines
;
282 ProcCpuinfoContent
.split(Lines
, "\n");
284 // Look for the CPU features.
285 SmallVector
<StringRef
, 32> CPUFeatures
;
286 for (unsigned I
= 0, E
= Lines
.size(); I
!= E
; ++I
)
287 if (Lines
[I
].startswith("features")) {
288 size_t Pos
= Lines
[I
].find(":");
289 if (Pos
!= StringRef::npos
) {
290 Lines
[I
].drop_front(Pos
+ 1).split(CPUFeatures
, ' ');
295 // We need to check for the presence of vector support independently of
296 // the machine type, since we may only use the vector register set when
297 // supported by the kernel (and hypervisor).
298 bool HaveVectorSupport
= false;
299 for (unsigned I
= 0, E
= CPUFeatures
.size(); I
!= E
; ++I
) {
300 if (CPUFeatures
[I
] == "vx")
301 HaveVectorSupport
= true;
304 // Now check the processor machine type.
305 for (unsigned I
= 0, E
= Lines
.size(); I
!= E
; ++I
) {
306 if (Lines
[I
].startswith("processor ")) {
307 size_t Pos
= Lines
[I
].find("machine = ");
308 if (Pos
!= StringRef::npos
) {
309 Pos
+= sizeof("machine = ") - 1;
311 if (!Lines
[I
].drop_front(Pos
).getAsInteger(10, Id
)) {
312 if (Id
>= 3906 && HaveVectorSupport
)
314 if (Id
>= 2964 && HaveVectorSupport
)
329 StringRef
sys::detail::getHostCPUNameForBPF() {
330 #if !defined(__linux__) || !defined(__x86_64__)
333 uint8_t v3_insns
[40] __attribute__ ((aligned (8))) =
334 /* BPF_MOV64_IMM(BPF_REG_0, 0) */
335 { 0xb7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
336 /* BPF_MOV64_IMM(BPF_REG_2, 1) */
337 0xb7, 0x2, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
338 /* BPF_JMP32_REG(BPF_JLT, BPF_REG_0, BPF_REG_2, 1) */
339 0xae, 0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0,
340 /* BPF_MOV64_IMM(BPF_REG_0, 1) */
341 0xb7, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
342 /* BPF_EXIT_INSN() */
343 0x95, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
345 uint8_t v2_insns
[40] __attribute__ ((aligned (8))) =
346 /* BPF_MOV64_IMM(BPF_REG_0, 0) */
347 { 0xb7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
348 /* BPF_MOV64_IMM(BPF_REG_2, 1) */
349 0xb7, 0x2, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
350 /* BPF_JMP_REG(BPF_JLT, BPF_REG_0, BPF_REG_2, 1) */
351 0xad, 0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0,
352 /* BPF_MOV64_IMM(BPF_REG_0, 1) */
353 0xb7, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
354 /* BPF_EXIT_INSN() */
355 0x95, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
357 struct bpf_prog_load_attr
{
365 uint32_t kern_version
;
368 attr
.prog_type
= 1; /* BPF_PROG_TYPE_SOCKET_FILTER */
370 attr
.insns
= (uint64_t)v3_insns
;
371 attr
.license
= (uint64_t)"DUMMY";
373 int fd
= syscall(321 /* __NR_bpf */, 5 /* BPF_PROG_LOAD */, &attr
,
380 /* Clear the whole attr in case its content changed by syscall. */
381 memset(&attr
, 0, sizeof(attr
));
382 attr
.prog_type
= 1; /* BPF_PROG_TYPE_SOCKET_FILTER */
384 attr
.insns
= (uint64_t)v2_insns
;
385 attr
.license
= (uint64_t)"DUMMY";
386 fd
= syscall(321 /* __NR_bpf */, 5 /* BPF_PROG_LOAD */, &attr
, sizeof(attr
));
395 #if defined(__i386__) || defined(_M_IX86) || \
396 defined(__x86_64__) || defined(_M_X64)
398 enum VendorSignatures
{
399 SIG_INTEL
= 0x756e6547 /* Genu */,
400 SIG_AMD
= 0x68747541 /* Auth */
403 // The check below for i386 was copied from clang's cpuid.h (__get_cpuid_max).
404 // Check motivated by bug reports for OpenSSL crashing on CPUs without CPUID
405 // support. Consequently, for i386, the presence of CPUID is checked first
406 // via the corresponding eflags bit.
407 // Removal of cpuid.h header motivated by PR30384
408 // Header cpuid.h and method __get_cpuid_max are not used in llvm, clang, openmp
409 // or test-suite, but are used in external projects e.g. libstdcxx
410 static bool isCpuIdSupported() {
411 #if defined(__GNUC__) || defined(__clang__)
412 #if defined(__i386__)
413 int __cpuid_supported
;
416 " movl %%eax,%%ecx\n"
417 " xorl $0x00200000,%%eax\n"
423 " cmpl %%eax,%%ecx\n"
427 : "=r"(__cpuid_supported
)
430 if (!__cpuid_supported
)
438 /// getX86CpuIDAndInfo - Execute the specified cpuid and return the 4 values in
439 /// the specified arguments. If we can't run cpuid on the host, return true.
440 static bool getX86CpuIDAndInfo(unsigned value
, unsigned *rEAX
, unsigned *rEBX
,
441 unsigned *rECX
, unsigned *rEDX
) {
442 #if defined(__GNUC__) || defined(__clang__)
443 #if defined(__x86_64__)
444 // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
445 // FIXME: should we save this for Clang?
446 __asm__("movq\t%%rbx, %%rsi\n\t"
448 "xchgq\t%%rbx, %%rsi\n\t"
449 : "=a"(*rEAX
), "=S"(*rEBX
), "=c"(*rECX
), "=d"(*rEDX
)
452 #elif defined(__i386__)
453 __asm__("movl\t%%ebx, %%esi\n\t"
455 "xchgl\t%%ebx, %%esi\n\t"
456 : "=a"(*rEAX
), "=S"(*rEBX
), "=c"(*rECX
), "=d"(*rEDX
)
462 #elif defined(_MSC_VER)
463 // The MSVC intrinsic is portable across x86 and x64.
465 __cpuid(registers
, value
);
466 *rEAX
= registers
[0];
467 *rEBX
= registers
[1];
468 *rECX
= registers
[2];
469 *rEDX
= registers
[3];
476 /// getX86CpuIDAndInfoEx - Execute the specified cpuid with subleaf and return
477 /// the 4 values in the specified arguments. If we can't run cpuid on the host,
479 static bool getX86CpuIDAndInfoEx(unsigned value
, unsigned subleaf
,
480 unsigned *rEAX
, unsigned *rEBX
, unsigned *rECX
,
482 #if defined(__GNUC__) || defined(__clang__)
483 #if defined(__x86_64__)
484 // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
485 // FIXME: should we save this for Clang?
486 __asm__("movq\t%%rbx, %%rsi\n\t"
488 "xchgq\t%%rbx, %%rsi\n\t"
489 : "=a"(*rEAX
), "=S"(*rEBX
), "=c"(*rECX
), "=d"(*rEDX
)
490 : "a"(value
), "c"(subleaf
));
492 #elif defined(__i386__)
493 __asm__("movl\t%%ebx, %%esi\n\t"
495 "xchgl\t%%ebx, %%esi\n\t"
496 : "=a"(*rEAX
), "=S"(*rEBX
), "=c"(*rECX
), "=d"(*rEDX
)
497 : "a"(value
), "c"(subleaf
));
502 #elif defined(_MSC_VER)
504 __cpuidex(registers
, value
, subleaf
);
505 *rEAX
= registers
[0];
506 *rEBX
= registers
[1];
507 *rECX
= registers
[2];
508 *rEDX
= registers
[3];
515 // Read control register 0 (XCR0). Used to detect features such as AVX.
516 static bool getX86XCR0(unsigned *rEAX
, unsigned *rEDX
) {
517 #if defined(__GNUC__) || defined(__clang__)
518 // Check xgetbv; this uses a .byte sequence instead of the instruction
519 // directly because older assemblers do not include support for xgetbv and
520 // there is no easy way to conditionally compile based on the assembler used.
521 __asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(*rEAX
), "=d"(*rEDX
) : "c"(0));
523 #elif defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK)
524 unsigned long long Result
= _xgetbv(_XCR_XFEATURE_ENABLED_MASK
);
526 *rEDX
= Result
>> 32;
533 static void detectX86FamilyModel(unsigned EAX
, unsigned *Family
,
535 *Family
= (EAX
>> 8) & 0xf; // Bits 8 - 11
536 *Model
= (EAX
>> 4) & 0xf; // Bits 4 - 7
537 if (*Family
== 6 || *Family
== 0xf) {
539 // Examine extended family ID if family ID is F.
540 *Family
+= (EAX
>> 20) & 0xff; // Bits 20 - 27
541 // Examine extended model ID if family ID is 6 or F.
542 *Model
+= ((EAX
>> 16) & 0xf) << 4; // Bits 16 - 19
547 getIntelProcessorTypeAndSubtype(unsigned Family
, unsigned Model
,
548 unsigned Brand_id
, unsigned Features
,
549 unsigned Features2
, unsigned Features3
,
550 unsigned *Type
, unsigned *Subtype
) {
555 *Type
= X86::INTEL_i386
;
558 *Type
= X86::INTEL_i486
;
561 if (Features
& (1 << X86::FEATURE_MMX
)) {
562 *Type
= X86::INTEL_PENTIUM_MMX
;
565 *Type
= X86::INTEL_PENTIUM
;
569 case 0x01: // Pentium Pro processor
570 *Type
= X86::INTEL_PENTIUM_PRO
;
572 case 0x03: // Intel Pentium II OverDrive processor, Pentium II processor,
574 case 0x05: // Pentium II processor, model 05, Pentium II Xeon processor,
575 // model 05, and Intel Celeron processor, model 05
576 case 0x06: // Celeron processor, model 06
577 *Type
= X86::INTEL_PENTIUM_II
;
579 case 0x07: // Pentium III processor, model 07, and Pentium III Xeon
580 // processor, model 07
581 case 0x08: // Pentium III processor, model 08, Pentium III Xeon processor,
582 // model 08, and Celeron processor, model 08
583 case 0x0a: // Pentium III Xeon processor, model 0Ah
584 case 0x0b: // Pentium III processor, model 0Bh
585 *Type
= X86::INTEL_PENTIUM_III
;
587 case 0x09: // Intel Pentium M processor, Intel Celeron M processor model 09.
588 case 0x0d: // Intel Pentium M processor, Intel Celeron M processor, model
589 // 0Dh. All processors are manufactured using the 90 nm process.
590 case 0x15: // Intel EP80579 Integrated Processor and Intel EP80579
591 // Integrated Processor with Intel QuickAssist Technology
592 *Type
= X86::INTEL_PENTIUM_M
;
594 case 0x0e: // Intel Core Duo processor, Intel Core Solo processor, model
595 // 0Eh. All processors are manufactured using the 65 nm process.
596 *Type
= X86::INTEL_CORE_DUO
;
598 case 0x0f: // Intel Core 2 Duo processor, Intel Core 2 Duo mobile
599 // processor, Intel Core 2 Quad processor, Intel Core 2 Quad
600 // mobile processor, Intel Core 2 Extreme processor, Intel
601 // Pentium Dual-Core processor, Intel Xeon processor, model
602 // 0Fh. All processors are manufactured using the 65 nm process.
603 case 0x16: // Intel Celeron processor model 16h. All processors are
604 // manufactured using the 65 nm process
605 *Type
= X86::INTEL_CORE2
; // "core2"
606 *Subtype
= X86::INTEL_CORE2_65
;
608 case 0x17: // Intel Core 2 Extreme processor, Intel Xeon processor, model
609 // 17h. All processors are manufactured using the 45 nm process.
611 // 45nm: Penryn , Wolfdale, Yorkfield (XE)
612 case 0x1d: // Intel Xeon processor MP. All processors are manufactured using
613 // the 45 nm process.
614 *Type
= X86::INTEL_CORE2
; // "penryn"
615 *Subtype
= X86::INTEL_CORE2_45
;
617 case 0x1a: // Intel Core i7 processor and Intel Xeon processor. All
618 // processors are manufactured using the 45 nm process.
619 case 0x1e: // Intel(R) Core(TM) i7 CPU 870 @ 2.93GHz.
620 // As found in a Summer 2010 model iMac.
622 case 0x2e: // Nehalem EX
623 *Type
= X86::INTEL_COREI7
; // "nehalem"
624 *Subtype
= X86::INTEL_COREI7_NEHALEM
;
626 case 0x25: // Intel Core i7, laptop version.
627 case 0x2c: // Intel Core i7 processor and Intel Xeon processor. All
628 // processors are manufactured using the 32 nm process.
629 case 0x2f: // Westmere EX
630 *Type
= X86::INTEL_COREI7
; // "westmere"
631 *Subtype
= X86::INTEL_COREI7_WESTMERE
;
633 case 0x2a: // Intel Core i7 processor. All processors are manufactured
634 // using the 32 nm process.
636 *Type
= X86::INTEL_COREI7
; //"sandybridge"
637 *Subtype
= X86::INTEL_COREI7_SANDYBRIDGE
;
640 case 0x3e: // Ivy Bridge EP
641 *Type
= X86::INTEL_COREI7
; // "ivybridge"
642 *Subtype
= X86::INTEL_COREI7_IVYBRIDGE
;
650 *Type
= X86::INTEL_COREI7
; // "haswell"
651 *Subtype
= X86::INTEL_COREI7_HASWELL
;
659 *Type
= X86::INTEL_COREI7
; // "broadwell"
660 *Subtype
= X86::INTEL_COREI7_BROADWELL
;
664 case 0x4e: // Skylake mobile
665 case 0x5e: // Skylake desktop
666 case 0x8e: // Kaby Lake mobile
667 case 0x9e: // Kaby Lake desktop
668 *Type
= X86::INTEL_COREI7
; // "skylake"
669 *Subtype
= X86::INTEL_COREI7_SKYLAKE
;
674 *Type
= X86::INTEL_COREI7
;
675 *Subtype
= X86::INTEL_COREI7_SKYLAKE_AVX512
; // "skylake-avx512"
680 *Type
= X86::INTEL_COREI7
;
681 *Subtype
= X86::INTEL_COREI7_CANNONLAKE
; // "cannonlake"
684 case 0x1c: // Most 45 nm Intel Atom processors
685 case 0x26: // 45 nm Atom Lincroft
686 case 0x27: // 32 nm Atom Medfield
687 case 0x35: // 32 nm Atom Midview
688 case 0x36: // 32 nm Atom Midview
689 *Type
= X86::INTEL_BONNELL
;
692 // Atom Silvermont codes from the Intel software optimization guide.
698 case 0x4c: // really airmont
699 *Type
= X86::INTEL_SILVERMONT
;
700 break; // "silvermont"
702 case 0x5c: // Apollo Lake
703 case 0x5f: // Denverton
704 *Type
= X86::INTEL_GOLDMONT
;
707 *Type
= X86::INTEL_GOLDMONT_PLUS
;
710 *Type
= X86::INTEL_KNL
; // knl
713 *Type
= X86::INTEL_KNM
; // knm
716 default: // Unknown family 6 CPU, try to guess.
717 if (Features
& (1 << X86::FEATURE_AVX512VBMI2
)) {
718 *Type
= X86::INTEL_COREI7
;
719 *Subtype
= X86::INTEL_COREI7_ICELAKE_CLIENT
;
723 if (Features
& (1 << X86::FEATURE_AVX512VBMI
)) {
724 *Type
= X86::INTEL_COREI7
;
725 *Subtype
= X86::INTEL_COREI7_CANNONLAKE
;
729 if (Features2
& (1 << (X86::FEATURE_AVX512VNNI
- 32))) {
730 *Type
= X86::INTEL_COREI7
;
731 *Subtype
= X86::INTEL_COREI7_CASCADELAKE
;
735 if (Features
& (1 << X86::FEATURE_AVX512VL
)) {
736 *Type
= X86::INTEL_COREI7
;
737 *Subtype
= X86::INTEL_COREI7_SKYLAKE_AVX512
;
741 if (Features
& (1 << X86::FEATURE_AVX512ER
)) {
742 *Type
= X86::INTEL_KNL
; // knl
746 if (Features3
& (1 << (X86::FEATURE_CLFLUSHOPT
- 64))) {
747 if (Features3
& (1 << (X86::FEATURE_SHA
- 64))) {
748 *Type
= X86::INTEL_GOLDMONT
;
750 *Type
= X86::INTEL_COREI7
;
751 *Subtype
= X86::INTEL_COREI7_SKYLAKE
;
755 if (Features3
& (1 << (X86::FEATURE_ADX
- 64))) {
756 *Type
= X86::INTEL_COREI7
;
757 *Subtype
= X86::INTEL_COREI7_BROADWELL
;
760 if (Features
& (1 << X86::FEATURE_AVX2
)) {
761 *Type
= X86::INTEL_COREI7
;
762 *Subtype
= X86::INTEL_COREI7_HASWELL
;
765 if (Features
& (1 << X86::FEATURE_AVX
)) {
766 *Type
= X86::INTEL_COREI7
;
767 *Subtype
= X86::INTEL_COREI7_SANDYBRIDGE
;
770 if (Features
& (1 << X86::FEATURE_SSE4_2
)) {
771 if (Features3
& (1 << (X86::FEATURE_MOVBE
- 64))) {
772 *Type
= X86::INTEL_SILVERMONT
;
774 *Type
= X86::INTEL_COREI7
;
775 *Subtype
= X86::INTEL_COREI7_NEHALEM
;
779 if (Features
& (1 << X86::FEATURE_SSE4_1
)) {
780 *Type
= X86::INTEL_CORE2
; // "penryn"
781 *Subtype
= X86::INTEL_CORE2_45
;
784 if (Features
& (1 << X86::FEATURE_SSSE3
)) {
785 if (Features3
& (1 << (X86::FEATURE_MOVBE
- 64))) {
786 *Type
= X86::INTEL_BONNELL
; // "bonnell"
788 *Type
= X86::INTEL_CORE2
; // "core2"
789 *Subtype
= X86::INTEL_CORE2_65
;
793 if (Features3
& (1 << (X86::FEATURE_EM64T
- 64))) {
794 *Type
= X86::INTEL_CORE2
; // "core2"
795 *Subtype
= X86::INTEL_CORE2_65
;
798 if (Features
& (1 << X86::FEATURE_SSE3
)) {
799 *Type
= X86::INTEL_CORE_DUO
;
802 if (Features
& (1 << X86::FEATURE_SSE2
)) {
803 *Type
= X86::INTEL_PENTIUM_M
;
806 if (Features
& (1 << X86::FEATURE_SSE
)) {
807 *Type
= X86::INTEL_PENTIUM_III
;
810 if (Features
& (1 << X86::FEATURE_MMX
)) {
811 *Type
= X86::INTEL_PENTIUM_II
;
814 *Type
= X86::INTEL_PENTIUM_PRO
;
819 if (Features3
& (1 << (X86::FEATURE_EM64T
- 64))) {
820 *Type
= X86::INTEL_NOCONA
;
823 if (Features
& (1 << X86::FEATURE_SSE3
)) {
824 *Type
= X86::INTEL_PRESCOTT
;
827 *Type
= X86::INTEL_PENTIUM_IV
;
835 static void getAMDProcessorTypeAndSubtype(unsigned Family
, unsigned Model
,
836 unsigned Features
, unsigned *Type
,
838 // FIXME: this poorly matches the generated SubtargetFeatureKV table. There
839 // appears to be no way to generate the wide variety of AMD-specific targets
840 // from the information returned from CPUID.
843 *Type
= X86::AMD_i486
;
846 *Type
= X86::AMDPENTIUM
;
850 *Subtype
= X86::AMDPENTIUM_K6
;
853 *Subtype
= X86::AMDPENTIUM_K62
;
857 *Subtype
= X86::AMDPENTIUM_K63
;
860 *Subtype
= X86::AMDPENTIUM_GEODE
;
865 if (Features
& (1 << X86::FEATURE_SSE
)) {
866 *Type
= X86::AMD_ATHLON_XP
;
867 break; // "athlon-xp"
869 *Type
= X86::AMD_ATHLON
;
872 if (Features
& (1 << X86::FEATURE_SSE3
)) {
873 *Type
= X86::AMD_K8SSE3
;
879 *Type
= X86::AMDFAM10H
; // "amdfam10"
882 *Subtype
= X86::AMDFAM10H_BARCELONA
;
885 *Subtype
= X86::AMDFAM10H_SHANGHAI
;
888 *Subtype
= X86::AMDFAM10H_ISTANBUL
;
893 *Type
= X86::AMD_BTVER1
;
896 *Type
= X86::AMDFAM15H
;
897 if (Model
>= 0x60 && Model
<= 0x7f) {
898 *Subtype
= X86::AMDFAM15H_BDVER4
;
899 break; // "bdver4"; 60h-7Fh: Excavator
901 if (Model
>= 0x30 && Model
<= 0x3f) {
902 *Subtype
= X86::AMDFAM15H_BDVER3
;
903 break; // "bdver3"; 30h-3Fh: Steamroller
905 if ((Model
>= 0x10 && Model
<= 0x1f) || Model
== 0x02) {
906 *Subtype
= X86::AMDFAM15H_BDVER2
;
907 break; // "bdver2"; 02h, 10h-1Fh: Piledriver
910 *Subtype
= X86::AMDFAM15H_BDVER1
;
911 break; // "bdver1"; 00h-0Fh: Bulldozer
915 *Type
= X86::AMD_BTVER2
;
918 *Type
= X86::AMDFAM17H
;
919 if (Model
>= 0x30 && Model
<= 0x3f) {
920 *Subtype
= X86::AMDFAM17H_ZNVER2
;
921 break; // "znver2"; 30h-3fh: Zen2
924 *Subtype
= X86::AMDFAM17H_ZNVER1
;
925 break; // "znver1"; 00h-0Fh: Zen1
933 static void getAvailableFeatures(unsigned ECX
, unsigned EDX
, unsigned MaxLeaf
,
934 unsigned *FeaturesOut
, unsigned *Features2Out
,
935 unsigned *Features3Out
) {
936 unsigned Features
= 0;
937 unsigned Features2
= 0;
938 unsigned Features3
= 0;
941 auto setFeature
= [&](unsigned F
) {
943 Features
|= 1U << (F
& 0x1f);
945 Features2
|= 1U << ((F
- 32) & 0x1f);
947 Features3
|= 1U << ((F
- 64) & 0x1f);
949 llvm_unreachable("Unexpected FeatureBit");
953 setFeature(X86::FEATURE_CMOV
);
955 setFeature(X86::FEATURE_MMX
);
957 setFeature(X86::FEATURE_SSE
);
959 setFeature(X86::FEATURE_SSE2
);
962 setFeature(X86::FEATURE_SSE3
);
964 setFeature(X86::FEATURE_PCLMUL
);
966 setFeature(X86::FEATURE_SSSE3
);
968 setFeature(X86::FEATURE_FMA
);
970 setFeature(X86::FEATURE_SSE4_1
);
972 setFeature(X86::FEATURE_SSE4_2
);
974 setFeature(X86::FEATURE_POPCNT
);
976 setFeature(X86::FEATURE_AES
);
979 setFeature(X86::FEATURE_MOVBE
);
981 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
982 // indicates that the AVX registers will be saved and restored on context
983 // switch, then we have full AVX support.
984 const unsigned AVXBits
= (1 << 27) | (1 << 28);
985 bool HasAVX
= ((ECX
& AVXBits
) == AVXBits
) && !getX86XCR0(&EAX
, &EDX
) &&
986 ((EAX
& 0x6) == 0x6);
987 bool HasAVX512Save
= HasAVX
&& ((EAX
& 0xe0) == 0xe0);
990 setFeature(X86::FEATURE_AVX
);
993 MaxLeaf
>= 0x7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX
, &EBX
, &ECX
, &EDX
);
995 if (HasLeaf7
&& ((EBX
>> 3) & 1))
996 setFeature(X86::FEATURE_BMI
);
997 if (HasLeaf7
&& ((EBX
>> 5) & 1) && HasAVX
)
998 setFeature(X86::FEATURE_AVX2
);
999 if (HasLeaf7
&& ((EBX
>> 9) & 1))
1000 setFeature(X86::FEATURE_BMI2
);
1001 if (HasLeaf7
&& ((EBX
>> 16) & 1) && HasAVX512Save
)
1002 setFeature(X86::FEATURE_AVX512F
);
1003 if (HasLeaf7
&& ((EBX
>> 17) & 1) && HasAVX512Save
)
1004 setFeature(X86::FEATURE_AVX512DQ
);
1005 if (HasLeaf7
&& ((EBX
>> 19) & 1))
1006 setFeature(X86::FEATURE_ADX
);
1007 if (HasLeaf7
&& ((EBX
>> 21) & 1) && HasAVX512Save
)
1008 setFeature(X86::FEATURE_AVX512IFMA
);
1009 if (HasLeaf7
&& ((EBX
>> 23) & 1))
1010 setFeature(X86::FEATURE_CLFLUSHOPT
);
1011 if (HasLeaf7
&& ((EBX
>> 26) & 1) && HasAVX512Save
)
1012 setFeature(X86::FEATURE_AVX512PF
);
1013 if (HasLeaf7
&& ((EBX
>> 27) & 1) && HasAVX512Save
)
1014 setFeature(X86::FEATURE_AVX512ER
);
1015 if (HasLeaf7
&& ((EBX
>> 28) & 1) && HasAVX512Save
)
1016 setFeature(X86::FEATURE_AVX512CD
);
1017 if (HasLeaf7
&& ((EBX
>> 29) & 1))
1018 setFeature(X86::FEATURE_SHA
);
1019 if (HasLeaf7
&& ((EBX
>> 30) & 1) && HasAVX512Save
)
1020 setFeature(X86::FEATURE_AVX512BW
);
1021 if (HasLeaf7
&& ((EBX
>> 31) & 1) && HasAVX512Save
)
1022 setFeature(X86::FEATURE_AVX512VL
);
1024 if (HasLeaf7
&& ((ECX
>> 1) & 1) && HasAVX512Save
)
1025 setFeature(X86::FEATURE_AVX512VBMI
);
1026 if (HasLeaf7
&& ((ECX
>> 6) & 1) && HasAVX512Save
)
1027 setFeature(X86::FEATURE_AVX512VBMI2
);
1028 if (HasLeaf7
&& ((ECX
>> 8) & 1))
1029 setFeature(X86::FEATURE_GFNI
);
1030 if (HasLeaf7
&& ((ECX
>> 10) & 1) && HasAVX
)
1031 setFeature(X86::FEATURE_VPCLMULQDQ
);
1032 if (HasLeaf7
&& ((ECX
>> 11) & 1) && HasAVX512Save
)
1033 setFeature(X86::FEATURE_AVX512VNNI
);
1034 if (HasLeaf7
&& ((ECX
>> 12) & 1) && HasAVX512Save
)
1035 setFeature(X86::FEATURE_AVX512BITALG
);
1036 if (HasLeaf7
&& ((ECX
>> 14) & 1) && HasAVX512Save
)
1037 setFeature(X86::FEATURE_AVX512VPOPCNTDQ
);
1039 if (HasLeaf7
&& ((EDX
>> 2) & 1) && HasAVX512Save
)
1040 setFeature(X86::FEATURE_AVX5124VNNIW
);
1041 if (HasLeaf7
&& ((EDX
>> 3) & 1) && HasAVX512Save
)
1042 setFeature(X86::FEATURE_AVX5124FMAPS
);
1044 unsigned MaxExtLevel
;
1045 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel
, &EBX
, &ECX
, &EDX
);
1047 bool HasExtLeaf1
= MaxExtLevel
>= 0x80000001 &&
1048 !getX86CpuIDAndInfo(0x80000001, &EAX
, &EBX
, &ECX
, &EDX
);
1049 if (HasExtLeaf1
&& ((ECX
>> 6) & 1))
1050 setFeature(X86::FEATURE_SSE4_A
);
1051 if (HasExtLeaf1
&& ((ECX
>> 11) & 1))
1052 setFeature(X86::FEATURE_XOP
);
1053 if (HasExtLeaf1
&& ((ECX
>> 16) & 1))
1054 setFeature(X86::FEATURE_FMA4
);
1056 if (HasExtLeaf1
&& ((EDX
>> 29) & 1))
1057 setFeature(X86::FEATURE_EM64T
);
1059 *FeaturesOut
= Features
;
1060 *Features2Out
= Features2
;
1061 *Features3Out
= Features3
;
1064 StringRef
sys::getHostCPUName() {
1065 unsigned EAX
= 0, EBX
= 0, ECX
= 0, EDX
= 0;
1066 unsigned MaxLeaf
, Vendor
;
1068 #if defined(__GNUC__) || defined(__clang__)
1069 //FIXME: include cpuid.h from clang or copy __get_cpuid_max here
1070 // and simplify it to not invoke __cpuid (like cpu_model.c in
1071 // compiler-rt/lib/builtins/cpu_model.c?
1072 // Opting for the second option.
1073 if(!isCpuIdSupported())
1076 if (getX86CpuIDAndInfo(0, &MaxLeaf
, &Vendor
, &ECX
, &EDX
) || MaxLeaf
< 1)
1078 getX86CpuIDAndInfo(0x1, &EAX
, &EBX
, &ECX
, &EDX
);
1080 unsigned Brand_id
= EBX
& 0xff;
1081 unsigned Family
= 0, Model
= 0;
1082 unsigned Features
= 0, Features2
= 0, Features3
= 0;
1083 detectX86FamilyModel(EAX
, &Family
, &Model
);
1084 getAvailableFeatures(ECX
, EDX
, MaxLeaf
, &Features
, &Features2
, &Features3
);
1087 unsigned Subtype
= 0;
1089 if (Vendor
== SIG_INTEL
) {
1090 getIntelProcessorTypeAndSubtype(Family
, Model
, Brand_id
, Features
,
1091 Features2
, Features3
, &Type
, &Subtype
);
1092 } else if (Vendor
== SIG_AMD
) {
1093 getAMDProcessorTypeAndSubtype(Family
, Model
, Features
, &Type
, &Subtype
);
1096 // Check subtypes first since those are more specific.
1097 #define X86_CPU_SUBTYPE(ARCHNAME, ENUM) \
1098 if (Subtype == X86::ENUM) \
1100 #include "llvm/Support/X86TargetParser.def"
1103 #define X86_CPU_TYPE(ARCHNAME, ENUM) \
1104 if (Type == X86::ENUM) \
1106 #include "llvm/Support/X86TargetParser.def"
1111 #elif defined(__APPLE__) && (defined(__ppc__) || defined(__powerpc__))
1112 StringRef
sys::getHostCPUName() {
1113 host_basic_info_data_t hostInfo
;
1114 mach_msg_type_number_t infoCount
;
1116 infoCount
= HOST_BASIC_INFO_COUNT
;
1117 mach_port_t hostPort
= mach_host_self();
1118 host_info(hostPort
, HOST_BASIC_INFO
, (host_info_t
)&hostInfo
,
1120 mach_port_deallocate(mach_task_self(), hostPort
);
1122 if (hostInfo
.cpu_type
!= CPU_TYPE_POWERPC
)
1125 switch (hostInfo
.cpu_subtype
) {
1126 case CPU_SUBTYPE_POWERPC_601
:
1128 case CPU_SUBTYPE_POWERPC_602
:
1130 case CPU_SUBTYPE_POWERPC_603
:
1132 case CPU_SUBTYPE_POWERPC_603e
:
1134 case CPU_SUBTYPE_POWERPC_603ev
:
1136 case CPU_SUBTYPE_POWERPC_604
:
1138 case CPU_SUBTYPE_POWERPC_604e
:
1140 case CPU_SUBTYPE_POWERPC_620
:
1142 case CPU_SUBTYPE_POWERPC_750
:
1144 case CPU_SUBTYPE_POWERPC_7400
:
1146 case CPU_SUBTYPE_POWERPC_7450
:
1148 case CPU_SUBTYPE_POWERPC_970
:
1155 #elif defined(__linux__) && (defined(__ppc__) || defined(__powerpc__))
1156 StringRef
sys::getHostCPUName() {
1157 std::unique_ptr
<llvm::MemoryBuffer
> P
= getProcCpuinfoContent();
1158 StringRef Content
= P
? P
->getBuffer() : "";
1159 return detail::getHostCPUNameForPowerPC(Content
);
1161 #elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1162 StringRef
sys::getHostCPUName() {
1163 std::unique_ptr
<llvm::MemoryBuffer
> P
= getProcCpuinfoContent();
1164 StringRef Content
= P
? P
->getBuffer() : "";
1165 return detail::getHostCPUNameForARM(Content
);
1167 #elif defined(__linux__) && defined(__s390x__)
1168 StringRef
sys::getHostCPUName() {
1169 std::unique_ptr
<llvm::MemoryBuffer
> P
= getProcCpuinfoContent();
1170 StringRef Content
= P
? P
->getBuffer() : "";
1171 return detail::getHostCPUNameForS390x(Content
);
1174 StringRef
sys::getHostCPUName() { return "generic"; }
1177 #if defined(__linux__) && defined(__x86_64__)
1178 // On Linux, the number of physical cores can be computed from /proc/cpuinfo,
1179 // using the number of unique physical/core id pairs. The following
1180 // implementation reads the /proc/cpuinfo format on an x86_64 system.
1181 static int computeHostNumPhysicalCores() {
1182 // Read /proc/cpuinfo as a stream (until EOF reached). It cannot be
1183 // mmapped because it appears to have 0 size.
1184 llvm::ErrorOr
<std::unique_ptr
<llvm::MemoryBuffer
>> Text
=
1185 llvm::MemoryBuffer::getFileAsStream("/proc/cpuinfo");
1186 if (std::error_code EC
= Text
.getError()) {
1187 llvm::errs() << "Can't read "
1188 << "/proc/cpuinfo: " << EC
.message() << "\n";
1191 SmallVector
<StringRef
, 8> strs
;
1192 (*Text
)->getBuffer().split(strs
, "\n", /*MaxSplit=*/-1,
1193 /*KeepEmpty=*/false);
1194 int CurPhysicalId
= -1;
1196 SmallSet
<std::pair
<int, int>, 32> UniqueItems
;
1197 for (auto &Line
: strs
) {
1199 if (!Line
.startswith("physical id") && !Line
.startswith("core id"))
1201 std::pair
<StringRef
, StringRef
> Data
= Line
.split(':');
1202 auto Name
= Data
.first
.trim();
1203 auto Val
= Data
.second
.trim();
1204 if (Name
== "physical id") {
1205 assert(CurPhysicalId
== -1 &&
1206 "Expected a core id before seeing another physical id");
1207 Val
.getAsInteger(10, CurPhysicalId
);
1209 if (Name
== "core id") {
1210 assert(CurCoreId
== -1 &&
1211 "Expected a physical id before seeing another core id");
1212 Val
.getAsInteger(10, CurCoreId
);
1214 if (CurPhysicalId
!= -1 && CurCoreId
!= -1) {
1215 UniqueItems
.insert(std::make_pair(CurPhysicalId
, CurCoreId
));
1220 return UniqueItems
.size();
1222 #elif defined(__APPLE__) && defined(__x86_64__)
1223 #include <sys/param.h>
1224 #include <sys/sysctl.h>
1226 // Gets the number of *physical cores* on the machine.
1227 static int computeHostNumPhysicalCores() {
1229 size_t len
= sizeof(count
);
1230 sysctlbyname("hw.physicalcpu", &count
, &len
, NULL
, 0);
1234 nm
[1] = HW_AVAILCPU
;
1235 sysctl(nm
, 2, &count
, &len
, NULL
, 0);
1242 // On other systems, return -1 to indicate unknown.
1243 static int computeHostNumPhysicalCores() { return -1; }
1246 int sys::getHostNumPhysicalCores() {
1247 static int NumCores
= computeHostNumPhysicalCores();
1251 #if defined(__i386__) || defined(_M_IX86) || \
1252 defined(__x86_64__) || defined(_M_X64)
1253 bool sys::getHostCPUFeatures(StringMap
<bool> &Features
) {
1254 unsigned EAX
= 0, EBX
= 0, ECX
= 0, EDX
= 0;
1261 if (getX86CpuIDAndInfo(0, &MaxLevel
, text
.u
+ 0, text
.u
+ 2, text
.u
+ 1) ||
1265 getX86CpuIDAndInfo(1, &EAX
, &EBX
, &ECX
, &EDX
);
1267 Features
["cx8"] = (EDX
>> 8) & 1;
1268 Features
["cmov"] = (EDX
>> 15) & 1;
1269 Features
["mmx"] = (EDX
>> 23) & 1;
1270 Features
["fxsr"] = (EDX
>> 24) & 1;
1271 Features
["sse"] = (EDX
>> 25) & 1;
1272 Features
["sse2"] = (EDX
>> 26) & 1;
1274 Features
["sse3"] = (ECX
>> 0) & 1;
1275 Features
["pclmul"] = (ECX
>> 1) & 1;
1276 Features
["ssse3"] = (ECX
>> 9) & 1;
1277 Features
["cx16"] = (ECX
>> 13) & 1;
1278 Features
["sse4.1"] = (ECX
>> 19) & 1;
1279 Features
["sse4.2"] = (ECX
>> 20) & 1;
1280 Features
["movbe"] = (ECX
>> 22) & 1;
1281 Features
["popcnt"] = (ECX
>> 23) & 1;
1282 Features
["aes"] = (ECX
>> 25) & 1;
1283 Features
["rdrnd"] = (ECX
>> 30) & 1;
1285 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
1286 // indicates that the AVX registers will be saved and restored on context
1287 // switch, then we have full AVX support.
1288 bool HasAVXSave
= ((ECX
>> 27) & 1) && ((ECX
>> 28) & 1) &&
1289 !getX86XCR0(&EAX
, &EDX
) && ((EAX
& 0x6) == 0x6);
1290 // AVX512 requires additional context to be saved by the OS.
1291 bool HasAVX512Save
= HasAVXSave
&& ((EAX
& 0xe0) == 0xe0);
1293 Features
["avx"] = HasAVXSave
;
1294 Features
["fma"] = ((ECX
>> 12) & 1) && HasAVXSave
;
1295 // Only enable XSAVE if OS has enabled support for saving YMM state.
1296 Features
["xsave"] = ((ECX
>> 26) & 1) && HasAVXSave
;
1297 Features
["f16c"] = ((ECX
>> 29) & 1) && HasAVXSave
;
1299 unsigned MaxExtLevel
;
1300 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel
, &EBX
, &ECX
, &EDX
);
1302 bool HasExtLeaf1
= MaxExtLevel
>= 0x80000001 &&
1303 !getX86CpuIDAndInfo(0x80000001, &EAX
, &EBX
, &ECX
, &EDX
);
1304 Features
["sahf"] = HasExtLeaf1
&& ((ECX
>> 0) & 1);
1305 Features
["lzcnt"] = HasExtLeaf1
&& ((ECX
>> 5) & 1);
1306 Features
["sse4a"] = HasExtLeaf1
&& ((ECX
>> 6) & 1);
1307 Features
["prfchw"] = HasExtLeaf1
&& ((ECX
>> 8) & 1);
1308 Features
["xop"] = HasExtLeaf1
&& ((ECX
>> 11) & 1) && HasAVXSave
;
1309 Features
["lwp"] = HasExtLeaf1
&& ((ECX
>> 15) & 1);
1310 Features
["fma4"] = HasExtLeaf1
&& ((ECX
>> 16) & 1) && HasAVXSave
;
1311 Features
["tbm"] = HasExtLeaf1
&& ((ECX
>> 21) & 1);
1312 Features
["mwaitx"] = HasExtLeaf1
&& ((ECX
>> 29) & 1);
1314 Features
["64bit"] = HasExtLeaf1
&& ((EDX
>> 29) & 1);
1316 // Miscellaneous memory related features, detected by
1317 // using the 0x80000008 leaf of the CPUID instruction
1318 bool HasExtLeaf8
= MaxExtLevel
>= 0x80000008 &&
1319 !getX86CpuIDAndInfo(0x80000008, &EAX
, &EBX
, &ECX
, &EDX
);
1320 Features
["clzero"] = HasExtLeaf8
&& ((EBX
>> 0) & 1);
1321 Features
["wbnoinvd"] = HasExtLeaf8
&& ((EBX
>> 9) & 1);
1324 MaxLevel
>= 7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX
, &EBX
, &ECX
, &EDX
);
1326 Features
["fsgsbase"] = HasLeaf7
&& ((EBX
>> 0) & 1);
1327 Features
["sgx"] = HasLeaf7
&& ((EBX
>> 2) & 1);
1328 Features
["bmi"] = HasLeaf7
&& ((EBX
>> 3) & 1);
1329 // AVX2 is only supported if we have the OS save support from AVX.
1330 Features
["avx2"] = HasLeaf7
&& ((EBX
>> 5) & 1) && HasAVXSave
;
1331 Features
["bmi2"] = HasLeaf7
&& ((EBX
>> 8) & 1);
1332 Features
["invpcid"] = HasLeaf7
&& ((EBX
>> 10) & 1);
1333 Features
["rtm"] = HasLeaf7
&& ((EBX
>> 11) & 1);
1334 Features
["mpx"] = HasLeaf7
&& ((EBX
>> 14) & 1);
1335 // AVX512 is only supported if the OS supports the context save for it.
1336 Features
["avx512f"] = HasLeaf7
&& ((EBX
>> 16) & 1) && HasAVX512Save
;
1337 Features
["avx512dq"] = HasLeaf7
&& ((EBX
>> 17) & 1) && HasAVX512Save
;
1338 Features
["rdseed"] = HasLeaf7
&& ((EBX
>> 18) & 1);
1339 Features
["adx"] = HasLeaf7
&& ((EBX
>> 19) & 1);
1340 Features
["avx512ifma"] = HasLeaf7
&& ((EBX
>> 21) & 1) && HasAVX512Save
;
1341 Features
["clflushopt"] = HasLeaf7
&& ((EBX
>> 23) & 1);
1342 Features
["clwb"] = HasLeaf7
&& ((EBX
>> 24) & 1);
1343 Features
["avx512pf"] = HasLeaf7
&& ((EBX
>> 26) & 1) && HasAVX512Save
;
1344 Features
["avx512er"] = HasLeaf7
&& ((EBX
>> 27) & 1) && HasAVX512Save
;
1345 Features
["avx512cd"] = HasLeaf7
&& ((EBX
>> 28) & 1) && HasAVX512Save
;
1346 Features
["sha"] = HasLeaf7
&& ((EBX
>> 29) & 1);
1347 Features
["avx512bw"] = HasLeaf7
&& ((EBX
>> 30) & 1) && HasAVX512Save
;
1348 Features
["avx512vl"] = HasLeaf7
&& ((EBX
>> 31) & 1) && HasAVX512Save
;
1350 Features
["prefetchwt1"] = HasLeaf7
&& ((ECX
>> 0) & 1);
1351 Features
["avx512vbmi"] = HasLeaf7
&& ((ECX
>> 1) & 1) && HasAVX512Save
;
1352 Features
["pku"] = HasLeaf7
&& ((ECX
>> 4) & 1);
1353 Features
["waitpkg"] = HasLeaf7
&& ((ECX
>> 5) & 1);
1354 Features
["avx512vbmi2"] = HasLeaf7
&& ((ECX
>> 6) & 1) && HasAVX512Save
;
1355 Features
["shstk"] = HasLeaf7
&& ((ECX
>> 7) & 1);
1356 Features
["gfni"] = HasLeaf7
&& ((ECX
>> 8) & 1);
1357 Features
["vaes"] = HasLeaf7
&& ((ECX
>> 9) & 1) && HasAVXSave
;
1358 Features
["vpclmulqdq"] = HasLeaf7
&& ((ECX
>> 10) & 1) && HasAVXSave
;
1359 Features
["avx512vnni"] = HasLeaf7
&& ((ECX
>> 11) & 1) && HasAVX512Save
;
1360 Features
["avx512bitalg"] = HasLeaf7
&& ((ECX
>> 12) & 1) && HasAVX512Save
;
1361 Features
["avx512vpopcntdq"] = HasLeaf7
&& ((ECX
>> 14) & 1) && HasAVX512Save
;
1362 Features
["rdpid"] = HasLeaf7
&& ((ECX
>> 22) & 1);
1363 Features
["cldemote"] = HasLeaf7
&& ((ECX
>> 25) & 1);
1364 Features
["movdiri"] = HasLeaf7
&& ((ECX
>> 27) & 1);
1365 Features
["movdir64b"] = HasLeaf7
&& ((ECX
>> 28) & 1);
1367 // There are two CPUID leafs which information associated with the pconfig
1369 // EAX=0x7, ECX=0x0 indicates the availability of the instruction (via the 18th
1370 // bit of EDX), while the EAX=0x1b leaf returns information on the
1371 // availability of specific pconfig leafs.
1372 // The target feature here only refers to the the first of these two.
1373 // Users might need to check for the availability of specific pconfig
1374 // leaves using cpuid, since that information is ignored while
1375 // detecting features using the "-march=native" flag.
1376 // For more info, see X86 ISA docs.
1377 Features
["pconfig"] = HasLeaf7
&& ((EDX
>> 18) & 1);
1379 bool HasLeafD
= MaxLevel
>= 0xd &&
1380 !getX86CpuIDAndInfoEx(0xd, 0x1, &EAX
, &EBX
, &ECX
, &EDX
);
1382 // Only enable XSAVE if OS has enabled support for saving YMM state.
1383 Features
["xsaveopt"] = HasLeafD
&& ((EAX
>> 0) & 1) && HasAVXSave
;
1384 Features
["xsavec"] = HasLeafD
&& ((EAX
>> 1) & 1) && HasAVXSave
;
1385 Features
["xsaves"] = HasLeafD
&& ((EAX
>> 3) & 1) && HasAVXSave
;
1387 bool HasLeaf14
= MaxLevel
>= 0x14 &&
1388 !getX86CpuIDAndInfoEx(0x14, 0x0, &EAX
, &EBX
, &ECX
, &EDX
);
1390 Features
["ptwrite"] = HasLeaf14
&& ((EBX
>> 4) & 1);
1394 #elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1395 bool sys::getHostCPUFeatures(StringMap
<bool> &Features
) {
1396 std::unique_ptr
<llvm::MemoryBuffer
> P
= getProcCpuinfoContent();
1400 SmallVector
<StringRef
, 32> Lines
;
1401 P
->getBuffer().split(Lines
, "\n");
1403 SmallVector
<StringRef
, 32> CPUFeatures
;
1405 // Look for the CPU features.
1406 for (unsigned I
= 0, E
= Lines
.size(); I
!= E
; ++I
)
1407 if (Lines
[I
].startswith("Features")) {
1408 Lines
[I
].split(CPUFeatures
, ' ');
1412 #if defined(__aarch64__)
1413 // Keep track of which crypto features we have seen
1414 enum { CAP_AES
= 0x1, CAP_PMULL
= 0x2, CAP_SHA1
= 0x4, CAP_SHA2
= 0x8 };
1415 uint32_t crypto
= 0;
1418 for (unsigned I
= 0, E
= CPUFeatures
.size(); I
!= E
; ++I
) {
1419 StringRef LLVMFeatureStr
= StringSwitch
<StringRef
>(CPUFeatures
[I
])
1420 #if defined(__aarch64__)
1421 .Case("asimd", "neon")
1422 .Case("fp", "fp-armv8")
1423 .Case("crc32", "crc")
1425 .Case("half", "fp16")
1426 .Case("neon", "neon")
1427 .Case("vfpv3", "vfp3")
1428 .Case("vfpv3d16", "d16")
1429 .Case("vfpv4", "vfp4")
1430 .Case("idiva", "hwdiv-arm")
1431 .Case("idivt", "hwdiv")
1435 #if defined(__aarch64__)
1436 // We need to check crypto separately since we need all of the crypto
1437 // extensions to enable the subtarget feature
1438 if (CPUFeatures
[I
] == "aes")
1440 else if (CPUFeatures
[I
] == "pmull")
1441 crypto
|= CAP_PMULL
;
1442 else if (CPUFeatures
[I
] == "sha1")
1444 else if (CPUFeatures
[I
] == "sha2")
1448 if (LLVMFeatureStr
!= "")
1449 Features
[LLVMFeatureStr
] = true;
1452 #if defined(__aarch64__)
1453 // If we have all crypto bits we can add the feature
1454 if (crypto
== (CAP_AES
| CAP_PMULL
| CAP_SHA1
| CAP_SHA2
))
1455 Features
["crypto"] = true;
1461 bool sys::getHostCPUFeatures(StringMap
<bool> &Features
) { return false; }
1464 std::string
sys::getProcessTriple() {
1465 std::string TargetTripleString
= updateTripleOSVersion(LLVM_HOST_TRIPLE
);
1466 Triple
PT(Triple::normalize(TargetTripleString
));
1468 if (sizeof(void *) == 8 && PT
.isArch32Bit())
1469 PT
= PT
.get64BitArchVariant();
1470 if (sizeof(void *) == 4 && PT
.isArch64Bit())
1471 PT
= PT
.get32BitArchVariant();