[X86] Sort bits in getHostCPUFeatures again.
[llvm-complete.git] / lib / Support / Host.cpp
blob3870a5876b7854d8c84f93237031abc38f6935c5
1 //===-- 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.
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"
26 #include <assert.h>
27 #include <string.h>
29 // Include the platform-specific parts of this class.
30 #ifdef LLVM_ON_UNIX
31 #include "Unix/Host.inc"
32 #endif
33 #ifdef LLVM_ON_WIN32
34 #include "Windows/Host.inc"
35 #endif
36 #ifdef _MSC_VER
37 #include <intrin.h>
38 #endif
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>
44 #endif
46 #define DEBUG_TYPE "host-detection"
48 //===----------------------------------------------------------------------===//
50 // Implementations of the CPU detection routines
52 //===----------------------------------------------------------------------===//
54 using namespace llvm;
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";
63 return nullptr;
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;
83 size_t CPULen = 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')
89 ++CIP;
91 if (CIP < CPUInfoEnd && *CIP == 'c') {
92 ++CIP;
93 if (CIP < CPUInfoEnd && *CIP == 'p') {
94 ++CIP;
95 if (CIP < CPUInfoEnd && *CIP == 'u') {
96 ++CIP;
97 while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t'))
98 ++CIP;
100 if (CIP < CPUInfoEnd && *CIP == ':') {
101 ++CIP;
102 while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t'))
103 ++CIP;
105 if (CIP < CPUInfoEnd) {
106 CPUStart = CIP;
107 while (CIP < CPUInfoEnd && (*CIP != ' ' && *CIP != '\t' &&
108 *CIP != ',' && *CIP != '\n'))
109 ++CIP;
110 CPULen = CIP - CPUStart;
117 if (CPUStart == 0)
118 while (CIP < CPUInfoEnd && *CIP != '\n')
119 ++CIP;
122 if (CPUStart == 0)
123 return generic;
125 return StringSwitch<const char *>(StringRef(CPUStart, CPULen))
126 .Case("604e", "604e")
127 .Case("604", "604")
128 .Case("7400", "7400")
129 .Case("7410", "7400")
130 .Case("7447", "7400")
131 .Case("7455", "7450")
132 .Case("G4", "g4")
133 .Case("POWER4", "970")
134 .Case("PPC970FX", "970")
135 .Case("PPC970MP", "970")
136 .Case("G5", "g5")
137 .Case("POWER5", "g5")
138 .Case("A2", "a2")
139 .Case("POWER6", "pwr6")
140 .Case("POWER7", "pwr7")
141 .Case("POWER8", "pwr8")
142 .Case("POWER8E", "pwr8")
143 .Case("POWER8NVL", "pwr8")
144 .Case("POWER9", "pwr9")
145 .Default(generic);
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
154 // in all cases.
155 SmallVector<StringRef, 32> Lines;
156 ProcCpuinfoContent.split(Lines, "\n");
158 // Look for the CPU implementer line.
159 StringRef Implementer;
160 StringRef Hardware;
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"))
172 return "cortex-a53";
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")
198 .Default("generic");
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")
217 .Default("generic");
219 return "generic";
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, ' ');
238 break;
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;
257 unsigned int Id;
258 if (!Lines[I].drop_front(Pos).getAsInteger(10, Id)) {
259 if (Id >= 3906 && HaveVectorSupport)
260 return "z14";
261 if (Id >= 2964 && HaveVectorSupport)
262 return "z13";
263 if (Id >= 2827)
264 return "zEC12";
265 if (Id >= 2817)
266 return "z196";
269 break;
273 return "generic";
276 StringRef sys::detail::getHostCPUNameForBPF() {
277 #if !defined(__linux__) || !defined(__x86_64__)
278 return "generic";
279 #else
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 {
293 uint32_t prog_type;
294 uint32_t insn_cnt;
295 uint64_t insns;
296 uint64_t license;
297 uint32_t log_level;
298 uint32_t log_size;
299 uint64_t log_buf;
300 uint32_t kern_version;
301 uint32_t prog_flags;
302 } attr = {};
303 attr.prog_type = 1; /* BPF_PROG_TYPE_SOCKET_FILTER */
304 attr.insn_cnt = 5;
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));
309 if (fd >= 0) {
310 close(fd);
311 return "v2";
313 return "v1";
314 #endif
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 {
328 FEATURE_CMOV = 0,
329 FEATURE_MMX,
330 FEATURE_POPCNT,
331 FEATURE_SSE,
332 FEATURE_SSE2,
333 FEATURE_SSE3,
334 FEATURE_SSSE3,
335 FEATURE_SSE4_1,
336 FEATURE_SSE4_2,
337 FEATURE_AVX,
338 FEATURE_AVX2,
339 FEATURE_SSE4_A,
340 FEATURE_FMA4,
341 FEATURE_XOP,
342 FEATURE_FMA,
343 FEATURE_AVX512F,
344 FEATURE_BMI,
345 FEATURE_BMI2,
346 FEATURE_AES,
347 FEATURE_PCLMUL,
348 FEATURE_AVX512VL,
349 FEATURE_AVX512BW,
350 FEATURE_AVX512DQ,
351 FEATURE_AVX512CD,
352 FEATURE_AVX512ER,
353 FEATURE_AVX512PF,
354 FEATURE_AVX512VBMI,
355 FEATURE_AVX512IFMA,
356 FEATURE_AVX5124VNNIW,
357 FEATURE_AVX5124FMAPS,
358 FEATURE_AVX512VPOPCNTDQ,
359 // One bit free here.
360 // Features below here are not in libgcc/compiler-rt.
361 FEATURE_MOVBE = 32,
362 FEATURE_ADX,
363 FEATURE_EM64T,
364 FEATURE_CLFLUSHOPT,
365 FEATURE_SHA,
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;
379 __asm__(" pushfl\n"
380 " popl %%eax\n"
381 " movl %%eax,%%ecx\n"
382 " xorl $0x00200000,%%eax\n"
383 " pushl %%eax\n"
384 " popfl\n"
385 " pushfl\n"
386 " popl %%eax\n"
387 " movl $0,%0\n"
388 " cmpl %%eax,%%ecx\n"
389 " je 1f\n"
390 " movl $1,%0\n"
391 "1:"
392 : "=r"(__cpuid_supported)
394 : "eax", "ecx");
395 if (!__cpuid_supported)
396 return false;
397 #endif
398 return true;
399 #endif
400 return true;
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"
412 "cpuid\n\t"
413 "xchgq\t%%rbx, %%rsi\n\t"
414 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
415 : "a"(value));
416 return false;
417 #elif defined(__i386__)
418 __asm__("movl\t%%ebx, %%esi\n\t"
419 "cpuid\n\t"
420 "xchgl\t%%ebx, %%esi\n\t"
421 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
422 : "a"(value));
423 return false;
424 #else
425 return true;
426 #endif
427 #elif defined(_MSC_VER)
428 // The MSVC intrinsic is portable across x86 and x64.
429 int registers[4];
430 __cpuid(registers, value);
431 *rEAX = registers[0];
432 *rEBX = registers[1];
433 *rECX = registers[2];
434 *rEDX = registers[3];
435 return false;
436 #else
437 return true;
438 #endif
441 /// getX86CpuIDAndInfoEx - Execute the specified cpuid with subleaf and return
442 /// the 4 values in the specified arguments. If we can't run cpuid on the host,
443 /// return true.
444 static bool getX86CpuIDAndInfoEx(unsigned value, unsigned subleaf,
445 unsigned *rEAX, unsigned *rEBX, unsigned *rECX,
446 unsigned *rEDX) {
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"
452 "cpuid\n\t"
453 "xchgq\t%%rbx, %%rsi\n\t"
454 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
455 : "a"(value), "c"(subleaf));
456 return false;
457 #elif defined(__i386__)
458 __asm__("movl\t%%ebx, %%esi\n\t"
459 "cpuid\n\t"
460 "xchgl\t%%ebx, %%esi\n\t"
461 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
462 : "a"(value), "c"(subleaf));
463 return false;
464 #else
465 return true;
466 #endif
467 #elif defined(_MSC_VER)
468 int registers[4];
469 __cpuidex(registers, value, subleaf);
470 *rEAX = registers[0];
471 *rEBX = registers[1];
472 *rECX = registers[2];
473 *rEDX = registers[3];
474 return false;
475 #else
476 return true;
477 #endif
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));
487 return false;
488 #elif defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK)
489 unsigned long long Result = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
490 *rEAX = Result;
491 *rEDX = Result >> 32;
492 return false;
493 #else
494 return true;
495 #endif
498 static void detectX86FamilyModel(unsigned EAX, unsigned *Family,
499 unsigned *Model) {
500 *Family = (EAX >> 8) & 0xf; // Bits 8 - 11
501 *Model = (EAX >> 4) & 0xf; // Bits 4 - 7
502 if (*Family == 6 || *Family == 0xf) {
503 if (*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
511 static void
512 getIntelProcessorTypeAndSubtype(unsigned Family, unsigned Model,
513 unsigned Brand_id, unsigned Features,
514 unsigned Features2, unsigned *Type,
515 unsigned *Subtype) {
516 if (Brand_id != 0)
517 return;
518 switch (Family) {
519 case 3:
520 *Type = X86::INTEL_i386;
521 break;
522 case 4:
523 *Type = X86::INTEL_i486;
524 break;
525 case 5:
526 if (Features & (1 << FEATURE_MMX)) {
527 *Type = X86::INTEL_PENTIUM_MMX;
528 break;
530 *Type = X86::INTEL_PENTIUM;
531 break;
532 case 6:
533 switch (Model) {
534 case 0x01: // Pentium Pro processor
535 *Type = X86::INTEL_PENTIUM_PRO;
536 break;
537 case 0x03: // Intel Pentium II OverDrive processor, Pentium II processor,
538 // model 03
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;
543 break;
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;
551 break;
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;
558 break;
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;
562 break; // yonah
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;
572 break;
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;
581 break;
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.
586 case 0x1f:
587 case 0x2e: // Nehalem EX
588 *Type = X86::INTEL_COREI7; // "nehalem"
589 *Subtype = X86::INTEL_COREI7_NEHALEM;
590 break;
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;
597 break;
598 case 0x2a: // Intel Core i7 processor. All processors are manufactured
599 // using the 32 nm process.
600 case 0x2d:
601 *Type = X86::INTEL_COREI7; //"sandybridge"
602 *Subtype = X86::INTEL_COREI7_SANDYBRIDGE;
603 break;
604 case 0x3a:
605 case 0x3e: // Ivy Bridge EP
606 *Type = X86::INTEL_COREI7; // "ivybridge"
607 *Subtype = X86::INTEL_COREI7_IVYBRIDGE;
608 break;
610 // Haswell:
611 case 0x3c:
612 case 0x3f:
613 case 0x45:
614 case 0x46:
615 *Type = X86::INTEL_COREI7; // "haswell"
616 *Subtype = X86::INTEL_COREI7_HASWELL;
617 break;
619 // Broadwell:
620 case 0x3d:
621 case 0x47:
622 case 0x4f:
623 case 0x56:
624 *Type = X86::INTEL_COREI7; // "broadwell"
625 *Subtype = X86::INTEL_COREI7_BROADWELL;
626 break;
628 // Skylake:
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;
635 break;
637 // Skylake Xeon:
638 case 0x55:
639 *Type = X86::INTEL_COREI7;
640 *Subtype = X86::INTEL_COREI7_SKYLAKE_AVX512; // "skylake-avx512"
641 break;
643 // Cannonlake:
644 case 0x66:
645 *Type = X86::INTEL_COREI7;
646 *Subtype = X86::INTEL_COREI7_CANNONLAKE; // "cannonlake"
647 break;
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;
655 break; // "bonnell"
657 // Atom Silvermont codes from the Intel software optimization guide.
658 case 0x37:
659 case 0x4a:
660 case 0x4d:
661 case 0x5a:
662 case 0x5d:
663 case 0x4c: // really airmont
664 *Type = X86::INTEL_SILVERMONT;
665 break; // "silvermont"
666 // Goldmont:
667 case 0x5c: // Apollo Lake
668 case 0x5f: // Denverton
669 case 0x7a: // Gemini Lake
670 *Type = X86::INTEL_GOLDMONT;
671 break; // "goldmont"
672 case 0x57:
673 *Type = X86::INTEL_KNL; // knl
674 break;
675 case 0x85:
676 *Type = X86::INTEL_KNM; // knm
677 break;
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;
683 break;
686 if (Features & (1 << FEATURE_AVX512VL)) {
687 *Type = X86::INTEL_COREI7;
688 *Subtype = X86::INTEL_COREI7_SKYLAKE_AVX512;
689 break;
692 if (Features & (1 << FEATURE_AVX512ER)) {
693 *Type = X86::INTEL_KNL; // knl
694 break;
697 if (Features2 & (1 << (FEATURE_CLFLUSHOPT - 32))) {
698 if (Features2 & (1 << (FEATURE_SHA - 32))) {
699 *Type = X86::INTEL_GOLDMONT;
700 } else {
701 *Type = X86::INTEL_COREI7;
702 *Subtype = X86::INTEL_COREI7_SKYLAKE;
704 break;
706 if (Features2 & (1 << (FEATURE_ADX - 32))) {
707 *Type = X86::INTEL_COREI7;
708 *Subtype = X86::INTEL_COREI7_BROADWELL;
709 break;
711 if (Features & (1 << FEATURE_AVX2)) {
712 *Type = X86::INTEL_COREI7;
713 *Subtype = X86::INTEL_COREI7_HASWELL;
714 break;
716 if (Features & (1 << FEATURE_AVX)) {
717 *Type = X86::INTEL_COREI7;
718 *Subtype = X86::INTEL_COREI7_SANDYBRIDGE;
719 break;
721 if (Features & (1 << FEATURE_SSE4_2)) {
722 if (Features2 & (1 << (FEATURE_MOVBE - 32))) {
723 *Type = X86::INTEL_SILVERMONT;
724 } else {
725 *Type = X86::INTEL_COREI7;
726 *Subtype = X86::INTEL_COREI7_NEHALEM;
728 break;
730 if (Features & (1 << FEATURE_SSE4_1)) {
731 *Type = X86::INTEL_CORE2; // "penryn"
732 *Subtype = X86::INTEL_CORE2_45;
733 break;
735 if (Features & (1 << FEATURE_SSSE3)) {
736 if (Features2 & (1 << (FEATURE_MOVBE - 32))) {
737 *Type = X86::INTEL_BONNELL; // "bonnell"
738 } else {
739 *Type = X86::INTEL_CORE2; // "core2"
740 *Subtype = X86::INTEL_CORE2_65;
742 break;
744 if (Features2 & (1 << (FEATURE_EM64T - 32))) {
745 *Type = X86::INTEL_CORE2; // "core2"
746 *Subtype = X86::INTEL_CORE2_65;
747 break;
749 if (Features & (1 << FEATURE_SSE3)) {
750 *Type = X86::INTEL_CORE_DUO;
751 break;
753 if (Features & (1 << FEATURE_SSE2)) {
754 *Type = X86::INTEL_PENTIUM_M;
755 break;
757 if (Features & (1 << FEATURE_SSE)) {
758 *Type = X86::INTEL_PENTIUM_III;
759 break;
761 if (Features & (1 << FEATURE_MMX)) {
762 *Type = X86::INTEL_PENTIUM_II;
763 break;
765 *Type = X86::INTEL_PENTIUM_PRO;
766 break;
768 break;
769 case 15: {
770 if (Features2 & (1 << (FEATURE_EM64T - 32))) {
771 *Type = X86::INTEL_NOCONA;
772 break;
774 if (Features & (1 << FEATURE_SSE3)) {
775 *Type = X86::INTEL_PRESCOTT;
776 break;
778 *Type = X86::INTEL_PENTIUM_IV;
779 break;
781 default:
782 break; /*"generic"*/
786 static void getAMDProcessorTypeAndSubtype(unsigned Family, unsigned Model,
787 unsigned Features, unsigned *Type,
788 unsigned *Subtype) {
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.
792 switch (Family) {
793 case 4:
794 *Type = X86::AMD_i486;
795 break;
796 case 5:
797 *Type = X86::AMDPENTIUM;
798 switch (Model) {
799 case 6:
800 case 7:
801 *Subtype = X86::AMDPENTIUM_K6;
802 break; // "k6"
803 case 8:
804 *Subtype = X86::AMDPENTIUM_K62;
805 break; // "k6-2"
806 case 9:
807 case 13:
808 *Subtype = X86::AMDPENTIUM_K63;
809 break; // "k6-3"
810 case 10:
811 *Subtype = X86::AMDPENTIUM_GEODE;
812 break; // "geode"
814 break;
815 case 6:
816 if (Features & (1 << FEATURE_SSE)) {
817 *Type = X86::AMD_ATHLON_XP;
818 break; // "athlon-xp"
820 *Type = X86::AMD_ATHLON;
821 break; // "athlon"
822 case 15:
823 if (Features & (1 << FEATURE_SSE3)) {
824 *Type = X86::AMD_K8SSE3;
825 break; // "k8-sse3"
827 *Type = X86::AMD_K8;
828 break; // "k8"
829 case 16:
830 *Type = X86::AMDFAM10H; // "amdfam10"
831 switch (Model) {
832 case 2:
833 *Subtype = X86::AMDFAM10H_BARCELONA;
834 break;
835 case 4:
836 *Subtype = X86::AMDFAM10H_SHANGHAI;
837 break;
838 case 8:
839 *Subtype = X86::AMDFAM10H_ISTANBUL;
840 break;
842 break;
843 case 20:
844 *Type = X86::AMD_BTVER1;
845 break; // "btver1";
846 case 21:
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
860 if (Model <= 0x0f) {
861 *Subtype = X86::AMDFAM15H_BDVER1;
862 break; // "bdver1"; 00h-0Fh: Bulldozer
864 break;
865 case 22:
866 *Type = X86::AMD_BTVER2;
867 break; // "btver2"
868 case 23:
869 *Type = X86::AMDFAM17H;
870 *Subtype = X86::AMDFAM17H_ZNVER1;
871 break;
872 default:
873 break; // "generic"
877 static void getAvailableFeatures(unsigned ECX, unsigned EDX, unsigned MaxLeaf,
878 unsigned *FeaturesOut,
879 unsigned *Features2Out) {
880 unsigned Features = 0;
881 unsigned Features2 = 0;
882 unsigned EAX, EBX;
884 if ((EDX >> 15) & 1)
885 Features |= 1 << FEATURE_CMOV;
886 if ((EDX >> 23) & 1)
887 Features |= 1 << FEATURE_MMX;
888 if ((EDX >> 25) & 1)
889 Features |= 1 << FEATURE_SSE;
890 if ((EDX >> 26) & 1)
891 Features |= 1 << FEATURE_SSE2;
893 if ((ECX >> 0) & 1)
894 Features |= 1 << FEATURE_SSE3;
895 if ((ECX >> 1) & 1)
896 Features |= 1 << FEATURE_PCLMUL;
897 if ((ECX >> 9) & 1)
898 Features |= 1 << FEATURE_SSSE3;
899 if ((ECX >> 12) & 1)
900 Features |= 1 << FEATURE_FMA;
901 if ((ECX >> 19) & 1)
902 Features |= 1 << FEATURE_SSE4_1;
903 if ((ECX >> 20) & 1)
904 Features |= 1 << FEATURE_SSE4_2;
905 if ((ECX >> 23) & 1)
906 Features |= 1 << FEATURE_POPCNT;
907 if ((ECX >> 25) & 1)
908 Features |= 1 << FEATURE_AES;
910 if ((ECX >> 22) & 1)
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);
921 if (HasAVX)
922 Features |= 1 << FEATURE_AVX;
924 bool HasLeaf7 =
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())
995 return "generic";
996 #endif
997 if (getX86CpuIDAndInfo(0, &MaxLeaf, &Vendor, &ECX, &EDX) || MaxLeaf < 1)
998 return "generic";
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);
1007 unsigned Type = 0;
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) \
1020 return ARCHNAME;
1021 #include "llvm/Support/X86TargetParser.def"
1023 // Now check types.
1024 #define X86_CPU_SUBTYPE(ARCHNAME, ENUM) \
1025 if (Type == X86::ENUM) \
1026 return ARCHNAME;
1027 #include "llvm/Support/X86TargetParser.def"
1029 return "generic";
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,
1039 &infoCount);
1041 if (hostInfo.cpu_type != CPU_TYPE_POWERPC)
1042 return "generic";
1044 switch (hostInfo.cpu_subtype) {
1045 case CPU_SUBTYPE_POWERPC_601:
1046 return "601";
1047 case CPU_SUBTYPE_POWERPC_602:
1048 return "602";
1049 case CPU_SUBTYPE_POWERPC_603:
1050 return "603";
1051 case CPU_SUBTYPE_POWERPC_603e:
1052 return "603e";
1053 case CPU_SUBTYPE_POWERPC_603ev:
1054 return "603ev";
1055 case CPU_SUBTYPE_POWERPC_604:
1056 return "604";
1057 case CPU_SUBTYPE_POWERPC_604e:
1058 return "604e";
1059 case CPU_SUBTYPE_POWERPC_620:
1060 return "620";
1061 case CPU_SUBTYPE_POWERPC_750:
1062 return "750";
1063 case CPU_SUBTYPE_POWERPC_7400:
1064 return "7400";
1065 case CPU_SUBTYPE_POWERPC_7450:
1066 return "7450";
1067 case CPU_SUBTYPE_POWERPC_970:
1068 return "970";
1069 default:;
1072 return "generic";
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);
1092 #else
1093 StringRef sys::getHostCPUName() { return "generic"; }
1094 #endif
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";
1108 return -1;
1110 SmallVector<StringRef, 8> strs;
1111 (*Text)->getBuffer().split(strs, "\n", /*MaxSplit=*/-1,
1112 /*KeepEmpty=*/false);
1113 int CurPhysicalId = -1;
1114 int CurCoreId = -1;
1115 SmallSet<std::pair<int, int>, 32> UniqueItems;
1116 for (auto &Line : strs) {
1117 Line = Line.trim();
1118 if (!Line.startswith("physical id") && !Line.startswith("core id"))
1119 continue;
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));
1135 CurPhysicalId = -1;
1136 CurCoreId = -1;
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() {
1147 uint32_t count;
1148 size_t len = sizeof(count);
1149 sysctlbyname("hw.physicalcpu", &count, &len, NULL, 0);
1150 if (count < 1) {
1151 int nm[2];
1152 nm[0] = CTL_HW;
1153 nm[1] = HW_AVAILCPU;
1154 sysctl(nm, 2, &count, &len, NULL, 0);
1155 if (count < 1)
1156 return -1;
1158 return count;
1160 #else
1161 // On other systems, return -1 to indicate unknown.
1162 static int computeHostNumPhysicalCores() { return -1; }
1163 #endif
1165 int sys::getHostNumPhysicalCores() {
1166 static int NumCores = computeHostNumPhysicalCores();
1167 return NumCores;
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;
1174 unsigned MaxLevel;
1175 union {
1176 unsigned u[3];
1177 char c[12];
1178 } text;
1180 if (getX86CpuIDAndInfo(0, &MaxLevel, text.u + 0, text.u + 2, text.u + 1) ||
1181 MaxLevel < 1)
1182 return false;
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);
1234 bool HasLeaf7 =
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;
1277 return true;
1279 #elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1280 bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
1281 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1282 if (!P)
1283 return false;
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, ' ');
1294 break;
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;
1301 #endif
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")
1309 #else
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")
1317 #endif
1318 .Default("");
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")
1324 crypto |= CAP_AES;
1325 else if (CPUFeatures[I] == "pmull")
1326 crypto |= CAP_PMULL;
1327 else if (CPUFeatures[I] == "sha1")
1328 crypto |= CAP_SHA1;
1329 else if (CPUFeatures[I] == "sha2")
1330 crypto |= CAP_SHA2;
1331 #endif
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;
1341 #endif
1343 return true;
1345 #else
1346 bool sys::getHostCPUFeatures(StringMap<bool> &Features) { return false; }
1347 #endif
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();
1358 return PT.str();