blob: f80ec6ba72bea09bff2d2d96a95724cddf862d5b [file] [log] [blame]
Simon Pilgrima271c542017-05-03 15:42:29 +00001//===-- Host.cpp - Implement OS Host Concept --------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the operating system Host concept.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/Host.h"
Craig Topperc77d00e2017-11-10 17:10:57 +000015#include "llvm/Support/TargetParser.h"
Simon Pilgrima271c542017-05-03 15:42:29 +000016#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>
28
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
45
46#define DEBUG_TYPE "host-detection"
47
48//===----------------------------------------------------------------------===//
49//
50// Implementations of the CPU detection routines
51//
52//===----------------------------------------------------------------------===//
53
54using namespace llvm;
55
56static 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;
64 }
65 return std::move(*Text);
66}
67
68StringRef 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";
74
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();
79
80 StringRef::const_iterator CIP = CPUInfoStart;
81
82 StringRef::const_iterator CPUStart = 0;
83 size_t CPULen = 0;
84
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;
90
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;
99
100 if (CIP < CPUInfoEnd && *CIP == ':') {
101 ++CIP;
102 while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t'))
103 ++CIP;
104
105 if (CIP < CPUInfoEnd) {
106 CPUStart = CIP;
107 while (CIP < CPUInfoEnd && (*CIP != ' ' && *CIP != '\t' &&
108 *CIP != ',' && *CIP != '\n'))
109 ++CIP;
110 CPULen = CIP - CPUStart;
111 }
112 }
113 }
114 }
115 }
116
117 if (CPUStart == 0)
118 while (CIP < CPUInfoEnd && *CIP != '\n')
119 ++CIP;
120 }
121
122 if (CPUStart == 0)
123 return generic;
124
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);
146}
147
148StringRef 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.
152
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");
157
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 :");
166 }
167
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";
173
174
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");
199 }
200
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")
Eli Friedmanbde9fc72017-09-13 21:48:00 +0000212 .Case("0x211", "kryo")
213 .Case("0x800", "cortex-a73")
214 .Case("0x801", "cortex-a73")
Balaram Makama1e7ecc72017-09-22 17:46:36 +0000215 .Case("0xc00", "falkor")
Chad Rosier71070852017-09-25 14:05:00 +0000216 .Case("0xc01", "saphira")
Simon Pilgrima271c542017-05-03 15:42:29 +0000217 .Default("generic");
218
219 return "generic";
220}
221
222StringRef sys::detail::getHostCPUNameForS390x(
223 const StringRef &ProcCpuinfoContent) {
224 // STIDP is a privileged operation, so use /proc/cpuinfo instead.
225
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");
230
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;
239 }
240 }
241
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;
249 }
250
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)) {
Ulrich Weigand2b3482f2017-07-17 17:41:11 +0000259 if (Id >= 3906 && HaveVectorSupport)
260 return "z14";
Simon Pilgrima271c542017-05-03 15:42:29 +0000261 if (Id >= 2964 && HaveVectorSupport)
262 return "z13";
263 if (Id >= 2827)
264 return "zEC12";
265 if (Id >= 2817)
266 return "z196";
267 }
268 }
269 break;
270 }
271 }
272
273 return "generic";
274}
275
Yonghong Songdc1dbf62017-08-23 04:25:57 +0000276StringRef 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 };
291
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";
307
308 int fd = syscall(321 /* __NR_bpf */, 5 /* BPF_PROG_LOAD */, &attr, sizeof(attr));
Yonghong Songc6d25712017-08-23 16:24:31 +0000309 if (fd >= 0) {
310 close(fd);
311 return "v2";
312 }
313 return "v1";
Yonghong Songdc1dbf62017-08-23 04:25:57 +0000314#endif
315}
316
Simon Pilgrima271c542017-05-03 15:42:29 +0000317#if defined(__i386__) || defined(_M_IX86) || \
318 defined(__x86_64__) || defined(_M_X64)
319
320enum VendorSignatures {
321 SIG_INTEL = 0x756e6547 /* Genu */,
322 SIG_AMD = 0x68747541 /* Auth */
323};
324
Craig Topper4a1ec812017-11-15 20:42:49 +0000325// 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.
Simon Pilgrima271c542017-05-03 15:42:29 +0000327enum 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,
Craig Topper3a5d0822017-07-12 06:49:58 +0000339 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,
Craig Topper4a1ec812017-11-15 20:42:49 +0000359 // One bit free here.
360 // Features below here are not in libgcc/compiler-rt.
Craig Topper3a5d0822017-07-12 06:49:58 +0000361 FEATURE_MOVBE = 32,
Simon Pilgrima271c542017-05-03 15:42:29 +0000362 FEATURE_ADX,
Craig Topper4eda7562017-07-27 03:26:52 +0000363 FEATURE_EM64T,
364 FEATURE_CLFLUSHOPT,
365 FEATURE_SHA,
Simon Pilgrima271c542017-05-03 15:42:29 +0000366};
367
368// The check below for i386 was copied from clang's cpuid.h (__get_cpuid_max).
369// Check motivated by bug reports for OpenSSL crashing on CPUs without CPUID
370// support. Consequently, for i386, the presence of CPUID is checked first
371// via the corresponding eflags bit.
372// Removal of cpuid.h header motivated by PR30384
373// Header cpuid.h and method __get_cpuid_max are not used in llvm, clang, openmp
374// or test-suite, but are used in external projects e.g. libstdcxx
375static bool isCpuIdSupported() {
376#if defined(__GNUC__) || defined(__clang__)
377#if defined(__i386__)
378 int __cpuid_supported;
379 __asm__(" pushfl\n"
380 " popl %%eax\n"
381 " movl %%eax,%%ecx\n"
382 " xorl $0x00200000,%%eax\n"
383 " pushl %%eax\n"
384 " popfl\n"
385 " pushfl\n"
386 " popl %%eax\n"
387 " movl $0,%0\n"
388 " cmpl %%eax,%%ecx\n"
389 " je 1f\n"
390 " movl $1,%0\n"
391 "1:"
392 : "=r"(__cpuid_supported)
393 :
394 : "eax", "ecx");
395 if (!__cpuid_supported)
396 return false;
397#endif
398 return true;
399#endif
400 return true;
401}
402
403/// getX86CpuIDAndInfo - Execute the specified cpuid and return the 4 values in
404/// the specified arguments. If we can't run cpuid on the host, return true.
405static bool getX86CpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
406 unsigned *rECX, unsigned *rEDX) {
Simon Pilgrima271c542017-05-03 15:42:29 +0000407#if defined(__GNUC__) || defined(__clang__)
408#if defined(__x86_64__)
409 // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
410 // FIXME: should we save this for Clang?
411 __asm__("movq\t%%rbx, %%rsi\n\t"
412 "cpuid\n\t"
413 "xchgq\t%%rbx, %%rsi\n\t"
414 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
415 : "a"(value));
Craig Topper1efd10a2017-07-10 06:04:11 +0000416 return false;
Simon Pilgrima271c542017-05-03 15:42:29 +0000417#elif defined(__i386__)
418 __asm__("movl\t%%ebx, %%esi\n\t"
419 "cpuid\n\t"
420 "xchgl\t%%ebx, %%esi\n\t"
421 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
422 : "a"(value));
Craig Topper1efd10a2017-07-10 06:04:11 +0000423 return false;
Simon Pilgrima271c542017-05-03 15:42:29 +0000424#else
Craig Topper1efd10a2017-07-10 06:04:11 +0000425 return true;
Simon Pilgrima271c542017-05-03 15:42:29 +0000426#endif
427#elif defined(_MSC_VER)
428 // The MSVC intrinsic is portable across x86 and x64.
429 int registers[4];
430 __cpuid(registers, value);
431 *rEAX = registers[0];
432 *rEBX = registers[1];
433 *rECX = registers[2];
434 *rEDX = registers[3];
Simon Pilgrima271c542017-05-03 15:42:29 +0000435 return false;
436#else
437 return true;
438#endif
439}
440
441/// getX86CpuIDAndInfoEx - Execute the specified cpuid with subleaf and return
442/// the 4 values in the specified arguments. If we can't run cpuid on the host,
443/// return true.
444static bool getX86CpuIDAndInfoEx(unsigned value, unsigned subleaf,
445 unsigned *rEAX, unsigned *rEBX, unsigned *rECX,
446 unsigned *rEDX) {
Simon Pilgrima271c542017-05-03 15:42:29 +0000447#if defined(__GNUC__) || defined(__clang__)
Craig Topper828cf302017-07-17 05:16:16 +0000448#if defined(__x86_64__)
Craig Topperada983a2017-07-10 06:09:22 +0000449 // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
Simon Pilgrima271c542017-05-03 15:42:29 +0000450 // FIXME: should we save this for Clang?
451 __asm__("movq\t%%rbx, %%rsi\n\t"
452 "cpuid\n\t"
453 "xchgq\t%%rbx, %%rsi\n\t"
454 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
455 : "a"(value), "c"(subleaf));
Craig Topper1efd10a2017-07-10 06:04:11 +0000456 return false;
Craig Topper828cf302017-07-17 05:16:16 +0000457#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
Simon Pilgrima271c542017-05-03 15:42:29 +0000467#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];
Craig Topper1efd10a2017-07-10 06:04:11 +0000474 return false;
475#else
476 return true;
Simon Pilgrima271c542017-05-03 15:42:29 +0000477#endif
Simon Pilgrima271c542017-05-03 15:42:29 +0000478}
479
Craig Topperf3af64e2017-07-12 06:49:57 +0000480// Read control register 0 (XCR0). Used to detect features such as AVX.
Simon Pilgrima271c542017-05-03 15:42:29 +0000481static 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
496}
497
498static 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
508 }
509}
510
511static void
Craig Topperc6bbe4b2017-07-08 05:16:14 +0000512getIntelProcessorTypeAndSubtype(unsigned Family, unsigned Model,
513 unsigned Brand_id, unsigned Features,
Craig Topper3a5d0822017-07-12 06:49:58 +0000514 unsigned Features2, unsigned *Type,
515 unsigned *Subtype) {
Simon Pilgrima271c542017-05-03 15:42:29 +0000516 if (Brand_id != 0)
517 return;
518 switch (Family) {
519 case 3:
Craig Topperc77d00e2017-11-10 17:10:57 +0000520 *Type = X86::INTEL_i386;
Simon Pilgrima271c542017-05-03 15:42:29 +0000521 break;
522 case 4:
Craig Topperc77d00e2017-11-10 17:10:57 +0000523 *Type = X86::INTEL_i486;
Simon Pilgrima271c542017-05-03 15:42:29 +0000524 break;
525 case 5:
Craig Topper094d7912017-11-02 03:32:49 +0000526 if (Features & (1 << FEATURE_MMX)) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000527 *Type = X86::INTEL_PENTIUM_MMX;
Simon Pilgrima271c542017-05-03 15:42:29 +0000528 break;
529 }
Craig Topperc77d00e2017-11-10 17:10:57 +0000530 *Type = X86::INTEL_PENTIUM;
Simon Pilgrima271c542017-05-03 15:42:29 +0000531 break;
532 case 6:
533 switch (Model) {
534 case 0x01: // Pentium Pro processor
Craig Topperc77d00e2017-11-10 17:10:57 +0000535 *Type = X86::INTEL_PENTIUM_PRO;
Simon Pilgrima271c542017-05-03 15:42:29 +0000536 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
Craig Topperc77d00e2017-11-10 17:10:57 +0000542 *Type = X86::INTEL_PENTIUM_II;
Simon Pilgrima271c542017-05-03 15:42:29 +0000543 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
Craig Topperc77d00e2017-11-10 17:10:57 +0000550 *Type = X86::INTEL_PENTIUM_III;
Simon Pilgrima271c542017-05-03 15:42:29 +0000551 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
Craig Topperc77d00e2017-11-10 17:10:57 +0000557 *Type = X86::INTEL_PENTIUM_M;
Simon Pilgrima271c542017-05-03 15:42:29 +0000558 break;
559 case 0x0e: // Intel Core Duo processor, Intel Core Solo processor, model
560 // 0Eh. All processors are manufactured using the 65 nm process.
Craig Topperc77d00e2017-11-10 17:10:57 +0000561 *Type = X86::INTEL_CORE_DUO;
Simon Pilgrima271c542017-05-03 15:42:29 +0000562 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
Craig Topperc77d00e2017-11-10 17:10:57 +0000570 *Type = X86::INTEL_CORE2; // "core2"
571 *Subtype = X86::INTEL_CORE2_65;
Simon Pilgrima271c542017-05-03 15:42:29 +0000572 break;
573 case 0x17: // Intel Core 2 Extreme processor, Intel Xeon processor, model
574 // 17h. All processors are manufactured using the 45 nm process.
575 //
576 // 45nm: Penryn , Wolfdale, Yorkfield (XE)
577 case 0x1d: // Intel Xeon processor MP. All processors are manufactured using
578 // the 45 nm process.
Craig Topperc77d00e2017-11-10 17:10:57 +0000579 *Type = X86::INTEL_CORE2; // "penryn"
580 *Subtype = X86::INTEL_CORE2_45;
Simon Pilgrima271c542017-05-03 15:42:29 +0000581 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
Craig Topperc77d00e2017-11-10 17:10:57 +0000588 *Type = X86::INTEL_COREI7; // "nehalem"
589 *Subtype = X86::INTEL_COREI7_NEHALEM;
Simon Pilgrima271c542017-05-03 15:42:29 +0000590 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
Craig Topperc77d00e2017-11-10 17:10:57 +0000595 *Type = X86::INTEL_COREI7; // "westmere"
596 *Subtype = X86::INTEL_COREI7_WESTMERE;
Simon Pilgrima271c542017-05-03 15:42:29 +0000597 break;
598 case 0x2a: // Intel Core i7 processor. All processors are manufactured
599 // using the 32 nm process.
600 case 0x2d:
Craig Topperc77d00e2017-11-10 17:10:57 +0000601 *Type = X86::INTEL_COREI7; //"sandybridge"
602 *Subtype = X86::INTEL_COREI7_SANDYBRIDGE;
Simon Pilgrima271c542017-05-03 15:42:29 +0000603 break;
604 case 0x3a:
605 case 0x3e: // Ivy Bridge EP
Craig Topperc77d00e2017-11-10 17:10:57 +0000606 *Type = X86::INTEL_COREI7; // "ivybridge"
607 *Subtype = X86::INTEL_COREI7_IVYBRIDGE;
Simon Pilgrima271c542017-05-03 15:42:29 +0000608 break;
609
610 // Haswell:
611 case 0x3c:
612 case 0x3f:
613 case 0x45:
614 case 0x46:
Craig Topperc77d00e2017-11-10 17:10:57 +0000615 *Type = X86::INTEL_COREI7; // "haswell"
616 *Subtype = X86::INTEL_COREI7_HASWELL;
Simon Pilgrima271c542017-05-03 15:42:29 +0000617 break;
618
619 // Broadwell:
620 case 0x3d:
621 case 0x47:
622 case 0x4f:
623 case 0x56:
Craig Topperc77d00e2017-11-10 17:10:57 +0000624 *Type = X86::INTEL_COREI7; // "broadwell"
625 *Subtype = X86::INTEL_COREI7_BROADWELL;
Simon Pilgrima271c542017-05-03 15:42:29 +0000626 break;
627
628 // Skylake:
629 case 0x4e: // Skylake mobile
630 case 0x5e: // Skylake desktop
631 case 0x8e: // Kaby Lake mobile
632 case 0x9e: // Kaby Lake desktop
Craig Topperc77d00e2017-11-10 17:10:57 +0000633 *Type = X86::INTEL_COREI7; // "skylake"
634 *Subtype = X86::INTEL_COREI7_SKYLAKE;
Simon Pilgrima271c542017-05-03 15:42:29 +0000635 break;
636
637 // Skylake Xeon:
638 case 0x55:
Craig Topperc77d00e2017-11-10 17:10:57 +0000639 *Type = X86::INTEL_COREI7;
640 *Subtype = X86::INTEL_COREI7_SKYLAKE_AVX512; // "skylake-avx512"
Simon Pilgrima271c542017-05-03 15:42:29 +0000641 break;
642
Craig Topper07491862017-11-15 06:02:42 +0000643 // Cannonlake:
644 case 0x66:
645 *Type = X86::INTEL_COREI7;
646 *Subtype = X86::INTEL_COREI7_CANNONLAKE; // "cannonlake"
647 break;
648
Simon Pilgrima271c542017-05-03 15:42:29 +0000649 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
Craig Topperc77d00e2017-11-10 17:10:57 +0000654 *Type = X86::INTEL_BONNELL;
Simon Pilgrima271c542017-05-03 15:42:29 +0000655 break; // "bonnell"
656
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
Craig Topperc77d00e2017-11-10 17:10:57 +0000664 *Type = X86::INTEL_SILVERMONT;
Simon Pilgrima271c542017-05-03 15:42:29 +0000665 break; // "silvermont"
Michael Zuckerman4bcb9c32017-06-29 10:00:33 +0000666 // Goldmont:
Craig Topper0dadfe32017-11-15 06:02:43 +0000667 case 0x5c: // Apollo Lake
668 case 0x5f: // Denverton
669 case 0x7a: // Gemini Lake
Craig Topperc77d00e2017-11-10 17:10:57 +0000670 *Type = X86::INTEL_GOLDMONT;
Michael Zuckerman4bcb9c32017-06-29 10:00:33 +0000671 break; // "goldmont"
Simon Pilgrima271c542017-05-03 15:42:29 +0000672 case 0x57:
Craig Topperc77d00e2017-11-10 17:10:57 +0000673 *Type = X86::INTEL_KNL; // knl
Simon Pilgrima271c542017-05-03 15:42:29 +0000674 break;
Craig Topper5d692912017-10-13 18:10:17 +0000675 case 0x85:
Craig Topperc77d00e2017-11-10 17:10:57 +0000676 *Type = X86::INTEL_KNM; // knm
Craig Topper5d692912017-10-13 18:10:17 +0000677 break;
Simon Pilgrima271c542017-05-03 15:42:29 +0000678
679 default: // Unknown family 6 CPU, try to guess.
Craig Topper07491862017-11-15 06:02:42 +0000680 if (Features & (1 << FEATURE_AVX512VBMI)) {
681 *Type = X86::INTEL_COREI7;
682 *Subtype = X86::INTEL_COREI7_CANNONLAKE;
Craig Topper4eda7562017-07-27 03:26:52 +0000683 break;
684 }
Craig Topper07491862017-11-15 06:02:42 +0000685
686 if (Features & (1 << FEATURE_AVX512VL)) {
687 *Type = X86::INTEL_COREI7;
688 *Subtype = X86::INTEL_COREI7_SKYLAKE_AVX512;
689 break;
690 }
691
692 if (Features & (1 << FEATURE_AVX512ER)) {
693 *Type = X86::INTEL_KNL; // knl
694 break;
695 }
696
Craig Topper4eda7562017-07-27 03:26:52 +0000697 if (Features2 & (1 << (FEATURE_CLFLUSHOPT - 32))) {
698 if (Features2 & (1 << (FEATURE_SHA - 32))) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000699 *Type = X86::INTEL_GOLDMONT;
Craig Topper4eda7562017-07-27 03:26:52 +0000700 } else {
Craig Topperc77d00e2017-11-10 17:10:57 +0000701 *Type = X86::INTEL_COREI7;
702 *Subtype = X86::INTEL_COREI7_SKYLAKE;
Craig Topper4eda7562017-07-27 03:26:52 +0000703 }
Simon Pilgrima271c542017-05-03 15:42:29 +0000704 break;
705 }
Craig Topper3a5d0822017-07-12 06:49:58 +0000706 if (Features2 & (1 << (FEATURE_ADX - 32))) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000707 *Type = X86::INTEL_COREI7;
708 *Subtype = X86::INTEL_COREI7_BROADWELL;
Simon Pilgrima271c542017-05-03 15:42:29 +0000709 break;
710 }
711 if (Features & (1 << FEATURE_AVX2)) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000712 *Type = X86::INTEL_COREI7;
713 *Subtype = X86::INTEL_COREI7_HASWELL;
Simon Pilgrima271c542017-05-03 15:42:29 +0000714 break;
715 }
716 if (Features & (1 << FEATURE_AVX)) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000717 *Type = X86::INTEL_COREI7;
718 *Subtype = X86::INTEL_COREI7_SANDYBRIDGE;
Simon Pilgrima271c542017-05-03 15:42:29 +0000719 break;
720 }
721 if (Features & (1 << FEATURE_SSE4_2)) {
Craig Topper3a5d0822017-07-12 06:49:58 +0000722 if (Features2 & (1 << (FEATURE_MOVBE - 32))) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000723 *Type = X86::INTEL_SILVERMONT;
Simon Pilgrima271c542017-05-03 15:42:29 +0000724 } else {
Craig Topperc77d00e2017-11-10 17:10:57 +0000725 *Type = X86::INTEL_COREI7;
726 *Subtype = X86::INTEL_COREI7_NEHALEM;
Simon Pilgrima271c542017-05-03 15:42:29 +0000727 }
728 break;
729 }
730 if (Features & (1 << FEATURE_SSE4_1)) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000731 *Type = X86::INTEL_CORE2; // "penryn"
732 *Subtype = X86::INTEL_CORE2_45;
Simon Pilgrima271c542017-05-03 15:42:29 +0000733 break;
734 }
735 if (Features & (1 << FEATURE_SSSE3)) {
Craig Topper3a5d0822017-07-12 06:49:58 +0000736 if (Features2 & (1 << (FEATURE_MOVBE - 32))) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000737 *Type = X86::INTEL_BONNELL; // "bonnell"
Simon Pilgrima271c542017-05-03 15:42:29 +0000738 } else {
Craig Topperc77d00e2017-11-10 17:10:57 +0000739 *Type = X86::INTEL_CORE2; // "core2"
740 *Subtype = X86::INTEL_CORE2_65;
Simon Pilgrima271c542017-05-03 15:42:29 +0000741 }
742 break;
743 }
Craig Topper3a5d0822017-07-12 06:49:58 +0000744 if (Features2 & (1 << (FEATURE_EM64T - 32))) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000745 *Type = X86::INTEL_CORE2; // "core2"
746 *Subtype = X86::INTEL_CORE2_65;
Craig Toppera233e162017-11-02 19:13:32 +0000747 break;
748 }
749 if (Features & (1 << FEATURE_SSE3)) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000750 *Type = X86::INTEL_CORE_DUO;
Craig Toppera233e162017-11-02 19:13:32 +0000751 break;
Simon Pilgrima271c542017-05-03 15:42:29 +0000752 }
753 if (Features & (1 << FEATURE_SSE2)) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000754 *Type = X86::INTEL_PENTIUM_M;
Simon Pilgrima271c542017-05-03 15:42:29 +0000755 break;
756 }
757 if (Features & (1 << FEATURE_SSE)) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000758 *Type = X86::INTEL_PENTIUM_III;
Simon Pilgrima271c542017-05-03 15:42:29 +0000759 break;
760 }
761 if (Features & (1 << FEATURE_MMX)) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000762 *Type = X86::INTEL_PENTIUM_II;
Simon Pilgrima271c542017-05-03 15:42:29 +0000763 break;
764 }
Craig Topperc77d00e2017-11-10 17:10:57 +0000765 *Type = X86::INTEL_PENTIUM_PRO;
Simon Pilgrima271c542017-05-03 15:42:29 +0000766 break;
767 }
768 break;
769 case 15: {
Craig Topper14949152017-11-02 19:13:34 +0000770 if (Features2 & (1 << (FEATURE_EM64T - 32))) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000771 *Type = X86::INTEL_NOCONA;
Simon Pilgrima271c542017-05-03 15:42:29 +0000772 break;
773 }
Craig Topper14949152017-11-02 19:13:34 +0000774 if (Features & (1 << FEATURE_SSE3)) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000775 *Type = X86::INTEL_PRESCOTT;
Craig Topper14949152017-11-02 19:13:34 +0000776 break;
777 }
Craig Topperc77d00e2017-11-10 17:10:57 +0000778 *Type = X86::INTEL_PENTIUM_IV;
Simon Pilgrima271c542017-05-03 15:42:29 +0000779 break;
780 }
781 default:
782 break; /*"generic"*/
783 }
784}
785
Craig Topper2ace1532017-07-08 06:44:34 +0000786static void getAMDProcessorTypeAndSubtype(unsigned Family, unsigned Model,
787 unsigned Features, unsigned *Type,
Simon Pilgrima271c542017-05-03 15:42:29 +0000788 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:
Craig Topperc77d00e2017-11-10 17:10:57 +0000794 *Type = X86::AMD_i486;
Simon Pilgrima271c542017-05-03 15:42:29 +0000795 break;
796 case 5:
Craig Topperc77d00e2017-11-10 17:10:57 +0000797 *Type = X86::AMDPENTIUM;
Simon Pilgrima271c542017-05-03 15:42:29 +0000798 switch (Model) {
799 case 6:
800 case 7:
Craig Topperc77d00e2017-11-10 17:10:57 +0000801 *Subtype = X86::AMDPENTIUM_K6;
Simon Pilgrima271c542017-05-03 15:42:29 +0000802 break; // "k6"
803 case 8:
Craig Topperc77d00e2017-11-10 17:10:57 +0000804 *Subtype = X86::AMDPENTIUM_K62;
Simon Pilgrima271c542017-05-03 15:42:29 +0000805 break; // "k6-2"
806 case 9:
807 case 13:
Craig Topperc77d00e2017-11-10 17:10:57 +0000808 *Subtype = X86::AMDPENTIUM_K63;
Simon Pilgrima271c542017-05-03 15:42:29 +0000809 break; // "k6-3"
810 case 10:
Craig Topperc77d00e2017-11-10 17:10:57 +0000811 *Subtype = X86::AMDPENTIUM_GEODE;
Simon Pilgrima271c542017-05-03 15:42:29 +0000812 break; // "geode"
813 }
814 break;
815 case 6:
Craig Topperf3de5eb2017-07-13 06:34:10 +0000816 if (Features & (1 << FEATURE_SSE)) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000817 *Type = X86::AMD_ATHLON_XP;
Simon Pilgrima271c542017-05-03 15:42:29 +0000818 break; // "athlon-xp"
819 }
Craig Topperc77d00e2017-11-10 17:10:57 +0000820 *Type = X86::AMD_ATHLON;
Craig Topperf3de5eb2017-07-13 06:34:10 +0000821 break; // "athlon"
Simon Pilgrima271c542017-05-03 15:42:29 +0000822 case 15:
Simon Pilgrima271c542017-05-03 15:42:29 +0000823 if (Features & (1 << FEATURE_SSE3)) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000824 *Type = X86::AMD_K8SSE3;
Simon Pilgrima271c542017-05-03 15:42:29 +0000825 break; // "k8-sse3"
826 }
Craig Topperc77d00e2017-11-10 17:10:57 +0000827 *Type = X86::AMD_K8;
Craig Topperf3de5eb2017-07-13 06:34:10 +0000828 break; // "k8"
Simon Pilgrima271c542017-05-03 15:42:29 +0000829 case 16:
Craig Topperc77d00e2017-11-10 17:10:57 +0000830 *Type = X86::AMDFAM10H; // "amdfam10"
Simon Pilgrima271c542017-05-03 15:42:29 +0000831 switch (Model) {
832 case 2:
Craig Topperc77d00e2017-11-10 17:10:57 +0000833 *Subtype = X86::AMDFAM10H_BARCELONA;
Simon Pilgrima271c542017-05-03 15:42:29 +0000834 break;
835 case 4:
Craig Topperc77d00e2017-11-10 17:10:57 +0000836 *Subtype = X86::AMDFAM10H_SHANGHAI;
Simon Pilgrima271c542017-05-03 15:42:29 +0000837 break;
838 case 8:
Craig Topperc77d00e2017-11-10 17:10:57 +0000839 *Subtype = X86::AMDFAM10H_ISTANBUL;
Simon Pilgrima271c542017-05-03 15:42:29 +0000840 break;
841 }
842 break;
843 case 20:
Craig Topperc77d00e2017-11-10 17:10:57 +0000844 *Type = X86::AMD_BTVER1;
Simon Pilgrima271c542017-05-03 15:42:29 +0000845 break; // "btver1";
846 case 21:
Craig Topperc77d00e2017-11-10 17:10:57 +0000847 *Type = X86::AMDFAM15H;
Craig Topper1f9d3c02017-07-08 06:44:35 +0000848 if (Model >= 0x60 && Model <= 0x7f) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000849 *Subtype = X86::AMDFAM15H_BDVER4;
Craig Topper3db11702017-07-12 06:49:56 +0000850 break; // "bdver4"; 60h-7Fh: Excavator
Simon Pilgrima271c542017-05-03 15:42:29 +0000851 }
852 if (Model >= 0x30 && Model <= 0x3f) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000853 *Subtype = X86::AMDFAM15H_BDVER3;
Simon Pilgrima271c542017-05-03 15:42:29 +0000854 break; // "bdver3"; 30h-3Fh: Steamroller
855 }
856 if (Model >= 0x10 && Model <= 0x1f) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000857 *Subtype = X86::AMDFAM15H_BDVER2;
Simon Pilgrima271c542017-05-03 15:42:29 +0000858 break; // "bdver2"; 10h-1Fh: Piledriver
859 }
860 if (Model <= 0x0f) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000861 *Subtype = X86::AMDFAM15H_BDVER1;
Simon Pilgrima271c542017-05-03 15:42:29 +0000862 break; // "bdver1"; 00h-0Fh: Bulldozer
863 }
864 break;
865 case 22:
Craig Topperc77d00e2017-11-10 17:10:57 +0000866 *Type = X86::AMD_BTVER2;
Simon Pilgrima271c542017-05-03 15:42:29 +0000867 break; // "btver2"
868 case 23:
Craig Topperc77d00e2017-11-10 17:10:57 +0000869 *Type = X86::AMDFAM17H;
870 *Subtype = X86::AMDFAM17H_ZNVER1;
Simon Pilgrima271c542017-05-03 15:42:29 +0000871 break;
872 default:
873 break; // "generic"
874 }
875}
876
Craig Topper3a5d0822017-07-12 06:49:58 +0000877static void getAvailableFeatures(unsigned ECX, unsigned EDX, unsigned MaxLeaf,
878 unsigned *FeaturesOut,
879 unsigned *Features2Out) {
Simon Pilgrima271c542017-05-03 15:42:29 +0000880 unsigned Features = 0;
Craig Topper3a5d0822017-07-12 06:49:58 +0000881 unsigned Features2 = 0;
Craig Topperc6bbe4b2017-07-08 05:16:14 +0000882 unsigned EAX, EBX;
Craig Topper3a5d0822017-07-12 06:49:58 +0000883
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;
892
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;
909
910 if ((ECX >> 22) & 1)
911 Features2 |= 1 << (FEATURE_MOVBE - 32);
Simon Pilgrima271c542017-05-03 15:42:29 +0000912
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);
Craig Topper3a5d0822017-07-12 06:49:58 +0000920
921 if (HasAVX)
922 Features |= 1 << FEATURE_AVX;
923
Simon Pilgrima271c542017-05-03 15:42:29 +0000924 bool HasLeaf7 =
925 MaxLeaf >= 0x7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
Craig Topper3a5d0822017-07-12 06:49:58 +0000926
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;
Craig Topper4eda7562017-07-27 03:26:52 +0000941 if (HasLeaf7 && ((EBX >> 23) & 1))
942 Features2 |= 1 << (FEATURE_CLFLUSHOPT - 32);
Craig Topper3a5d0822017-07-12 06:49:58 +0000943 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;
Craig Topper4eda7562017-07-27 03:26:52 +0000949 if (HasLeaf7 && ((EBX >> 29) & 1))
950 Features2 |= 1 << (FEATURE_SHA - 32);
Craig Topper3a5d0822017-07-12 06:49:58 +0000951 if (HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save)
952 Features |= 1 << FEATURE_AVX512BW;
953 if (HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save)
954 Features |= 1 << FEATURE_AVX512VL;
955
956 if (HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save)
957 Features |= 1 << FEATURE_AVX512VBMI;
958 if (HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save)
959 Features |= 1 << FEATURE_AVX512VPOPCNTDQ;
960
961 if (HasLeaf7 && ((EDX >> 2) & 1) && HasAVX512Save)
962 Features |= 1 << FEATURE_AVX5124VNNIW;
963 if (HasLeaf7 && ((EDX >> 3) & 1) && HasAVX512Save)
964 Features |= 1 << FEATURE_AVX5124FMAPS;
Simon Pilgrima271c542017-05-03 15:42:29 +0000965
Craig Topperbb8c7992017-07-08 05:16:13 +0000966 unsigned MaxExtLevel;
967 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
968
969 bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
970 !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
Craig Topper3a5d0822017-07-12 06:49:58 +0000971 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;
Craig Topperbb8c7992017-07-08 05:16:13 +0000977
Craig Topper3a5d0822017-07-12 06:49:58 +0000978 if (HasExtLeaf1 && ((EDX >> 29) & 1))
979 Features2 |= 1 << (FEATURE_EM64T - 32);
980
981 *FeaturesOut = Features;
982 *Features2Out = Features2;
Simon Pilgrima271c542017-05-03 15:42:29 +0000983}
984
985StringRef sys::getHostCPUName() {
986 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
987 unsigned MaxLeaf, Vendor;
988
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
Craig Topperbb8c7992017-07-08 05:16:13 +0000997 if (getX86CpuIDAndInfo(0, &MaxLeaf, &Vendor, &ECX, &EDX) || MaxLeaf < 1)
Simon Pilgrima271c542017-05-03 15:42:29 +0000998 return "generic";
Craig Topperbb8c7992017-07-08 05:16:13 +0000999 getX86CpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
Simon Pilgrima271c542017-05-03 15:42:29 +00001000
1001 unsigned Brand_id = EBX & 0xff;
1002 unsigned Family = 0, Model = 0;
Craig Topper3a5d0822017-07-12 06:49:58 +00001003 unsigned Features = 0, Features2 = 0;
Simon Pilgrima271c542017-05-03 15:42:29 +00001004 detectX86FamilyModel(EAX, &Family, &Model);
Craig Topper3a5d0822017-07-12 06:49:58 +00001005 getAvailableFeatures(ECX, EDX, MaxLeaf, &Features, &Features2);
Simon Pilgrima271c542017-05-03 15:42:29 +00001006
Craig Topper741e7e62017-11-03 18:02:44 +00001007 unsigned Type = 0;
1008 unsigned Subtype = 0;
Simon Pilgrima271c542017-05-03 15:42:29 +00001009
1010 if (Vendor == SIG_INTEL) {
Craig Topper3a5d0822017-07-12 06:49:58 +00001011 getIntelProcessorTypeAndSubtype(Family, Model, Brand_id, Features,
1012 Features2, &Type, &Subtype);
Simon Pilgrima271c542017-05-03 15:42:29 +00001013 } else if (Vendor == SIG_AMD) {
1014 getAMDProcessorTypeAndSubtype(Family, Model, Features, &Type, &Subtype);
Simon Pilgrima271c542017-05-03 15:42:29 +00001015 }
Craig Topperc77d00e2017-11-10 17:10:57 +00001016
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"
1022
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"
1028
Simon Pilgrima271c542017-05-03 15:42:29 +00001029 return "generic";
1030}
1031
1032#elif defined(__APPLE__) && (defined(__ppc__) || defined(__powerpc__))
1033StringRef sys::getHostCPUName() {
1034 host_basic_info_data_t hostInfo;
1035 mach_msg_type_number_t infoCount;
1036
1037 infoCount = HOST_BASIC_INFO_COUNT;
1038 host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo,
1039 &infoCount);
1040
1041 if (hostInfo.cpu_type != CPU_TYPE_POWERPC)
1042 return "generic";
1043
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:;
1070 }
1071
1072 return "generic";
1073}
1074#elif defined(__linux__) && (defined(__ppc__) || defined(__powerpc__))
1075StringRef sys::getHostCPUName() {
1076 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1077 const StringRef& Content = P ? P->getBuffer() : "";
1078 return detail::getHostCPUNameForPowerPC(Content);
1079}
1080#elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1081StringRef sys::getHostCPUName() {
1082 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1083 const StringRef& Content = P ? P->getBuffer() : "";
1084 return detail::getHostCPUNameForARM(Content);
1085}
1086#elif defined(__linux__) && defined(__s390x__)
1087StringRef sys::getHostCPUName() {
1088 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1089 const StringRef& Content = P ? P->getBuffer() : "";
1090 return detail::getHostCPUNameForS390x(Content);
1091}
1092#else
1093StringRef sys::getHostCPUName() { return "generic"; }
1094#endif
1095
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.
1100static 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;
1109 }
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);
1127 }
1128 if (Name == "core id") {
1129 assert(CurCoreId == -1 &&
1130 "Expected a physical id before seeing another core id");
1131 Val.getAsInteger(10, CurCoreId);
1132 }
1133 if (CurPhysicalId != -1 && CurCoreId != -1) {
1134 UniqueItems.insert(std::make_pair(CurPhysicalId, CurCoreId));
1135 CurPhysicalId = -1;
1136 CurCoreId = -1;
1137 }
1138 }
1139 return UniqueItems.size();
1140}
1141#elif defined(__APPLE__) && defined(__x86_64__)
1142#include <sys/param.h>
1143#include <sys/sysctl.h>
1144
1145// Gets the number of *physical cores* on the machine.
1146static 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;
1157 }
1158 return count;
1159}
1160#else
1161// On other systems, return -1 to indicate unknown.
1162static int computeHostNumPhysicalCores() { return -1; }
1163#endif
1164
1165int sys::getHostNumPhysicalCores() {
1166 static int NumCores = computeHostNumPhysicalCores();
1167 return NumCores;
1168}
1169
1170#if defined(__i386__) || defined(_M_IX86) || \
1171 defined(__x86_64__) || defined(_M_X64)
1172bool 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;
1179
1180 if (getX86CpuIDAndInfo(0, &MaxLevel, text.u + 0, text.u + 2, text.u + 1) ||
1181 MaxLevel < 1)
1182 return false;
1183
1184 getX86CpuIDAndInfo(1, &EAX, &EBX, &ECX, &EDX);
1185
Craig Topper1af7e442017-11-19 23:30:22 +00001186 Features["cmov"] = (EDX >> 15) & 1;
1187 Features["mmx"] = (EDX >> 23) & 1;
1188 Features["sse"] = (EDX >> 25) & 1;
1189 Features["sse2"] = (EDX >> 26) & 1;
1190
1191 Features["sse3"] = (ECX >> 0) & 1;
1192 Features["pclmul"] = (ECX >> 1) & 1;
1193 Features["ssse3"] = (ECX >> 9) & 1;
1194 Features["cx16"] = (ECX >> 13) & 1;
Simon Pilgrima271c542017-05-03 15:42:29 +00001195 Features["sse4.1"] = (ECX >> 19) & 1;
1196 Features["sse4.2"] = (ECX >> 20) & 1;
Craig Topper1af7e442017-11-19 23:30:22 +00001197 Features["movbe"] = (ECX >> 22) & 1;
Simon Pilgrima271c542017-05-03 15:42:29 +00001198 Features["popcnt"] = (ECX >> 23) & 1;
Craig Topper1af7e442017-11-19 23:30:22 +00001199 Features["aes"] = (ECX >> 25) & 1;
1200 Features["rdrnd"] = (ECX >> 30) & 1;
Simon Pilgrima271c542017-05-03 15:42:29 +00001201
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);
Simon Pilgrima271c542017-05-03 15:42:29 +00001207 // AVX512 requires additional context to be saved by the OS.
1208 bool HasAVX512Save = HasAVXSave && ((EAX & 0xe0) == 0xe0);
1209
Craig Topper1af7e442017-11-19 23:30:22 +00001210 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;
1215
Simon Pilgrima271c542017-05-03 15:42:29 +00001216 unsigned MaxExtLevel;
1217 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
1218
1219 bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
1220 !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
Craig Topper1af7e442017-11-19 23:30:22 +00001221 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);
Simon Pilgrima271c542017-05-03 15:42:29 +00001228 Features["mwaitx"] = HasExtLeaf1 && ((ECX >> 29) & 1);
1229
1230 bool HasExtLeaf8 = MaxExtLevel >= 0x80000008 &&
Craig Topperdcd69792017-11-19 23:49:19 +00001231 !getX86CpuIDAndInfo(0x80000008, &EAX, &EBX, &ECX, &EDX);
Simon Pilgrima271c542017-05-03 15:42:29 +00001232 Features["clzero"] = HasExtLeaf8 && ((EBX >> 0) & 1);
1233
1234 bool HasLeaf7 =
1235 MaxLevel >= 7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
1236
Craig Topper1af7e442017-11-19 23:30:22 +00001237 Features["fsgsbase"] = HasLeaf7 && ((EBX >> 0) & 1);
1238 Features["sgx"] = HasLeaf7 && ((EBX >> 2) & 1);
1239 Features["bmi"] = HasLeaf7 && ((EBX >> 3) & 1);
Simon Pilgrima271c542017-05-03 15:42:29 +00001240 // AVX2 is only supported if we have the OS save support from AVX.
Craig Topper1af7e442017-11-19 23:30:22 +00001241 Features["avx2"] = HasLeaf7 && ((EBX >> 5) & 1) && HasAVXSave;
1242 Features["bmi2"] = HasLeaf7 && ((EBX >> 8) & 1);
1243 Features["rtm"] = HasLeaf7 && ((EBX >> 11) & 1);
Simon Pilgrima271c542017-05-03 15:42:29 +00001244 // AVX512 is only supported if the OS supports the context save for it.
Craig Topper1af7e442017-11-19 23:30:22 +00001245 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);
Simon Pilgrima271c542017-05-03 15:42:29 +00001249 Features["avx512ifma"] = HasLeaf7 && ((EBX >> 21) & 1) && HasAVX512Save;
Craig Topper1af7e442017-11-19 23:30:22 +00001250 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;
Simon Pilgrima271c542017-05-03 15:42:29 +00001258
Craig Topper1af7e442017-11-19 23:30:22 +00001259 Features["prefetchwt1"] = HasLeaf7 && ((ECX >> 0) & 1);
1260 Features["avx512vbmi"] = HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save;
Coby Tayree71e37cc2017-11-21 09:48:44 +00001261 Features["avx512vbmi2"] = HasLeaf7 && ((ECX >> 6) & 1) && HasAVX512Save;
Yonghong Songdc1dbf62017-08-23 04:25:57 +00001262 Features["avx512vpopcntdq"] = HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save;
Craig Topper1af7e442017-11-19 23:30:22 +00001263 Features["pku"] = HasLeaf7 && ((ECX >> 4) & 1);
Coby Tayree2a1c02f2017-11-21 09:11:41 +00001264 Features["vaes"] = HasLeaf7 && ((ECX >> 9) & 1) && HasAVXSave;
Simon Pilgrima271c542017-05-03 15:42:29 +00001265
Coby Tayree7ca5e5872017-11-21 09:30:33 +00001266 // VPCLMULQDQ (carry-less multiplication quadword)
1267 Features["vpclmulqdq"] = HasLeaf7 && ((ECX >> 10) & 1) && HasAVXSave;
1268
Simon Pilgrima271c542017-05-03 15:42:29 +00001269 bool HasLeafD = MaxLevel >= 0xd &&
1270 !getX86CpuIDAndInfoEx(0xd, 0x1, &EAX, &EBX, &ECX, &EDX);
1271
1272 // Only enable XSAVE if OS has enabled support for saving YMM state.
Craig Topper1af7e442017-11-19 23:30:22 +00001273 Features["xsaveopt"] = HasLeafD && ((EAX >> 0) & 1) && HasAVXSave;
1274 Features["xsavec"] = HasLeafD && ((EAX >> 1) & 1) && HasAVXSave;
1275 Features["xsaves"] = HasLeafD && ((EAX >> 3) & 1) && HasAVXSave;
Simon Pilgrima271c542017-05-03 15:42:29 +00001276
1277 return true;
1278}
1279#elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1280bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
1281 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1282 if (!P)
1283 return false;
1284
1285 SmallVector<StringRef, 32> Lines;
1286 P->getBuffer().split(Lines, "\n");
1287
1288 SmallVector<StringRef, 32> CPUFeatures;
1289
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;
1295 }
1296
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
1302
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("");
1319
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
1332
1333 if (LLVMFeatureStr != "")
1334 Features[LLVMFeatureStr] = true;
1335 }
1336
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
1342
1343 return true;
1344}
1345#else
1346bool sys::getHostCPUFeatures(StringMap<bool> &Features) { return false; }
1347#endif
1348
1349std::string sys::getProcessTriple() {
Alex Lorenz3803df32017-07-07 09:53:47 +00001350 std::string TargetTripleString = updateTripleOSVersion(LLVM_HOST_TRIPLE);
1351 Triple PT(Triple::normalize(TargetTripleString));
Simon Pilgrima271c542017-05-03 15:42:29 +00001352
1353 if (sizeof(void *) == 8 && PT.isArch32Bit())
1354 PT = PT.get64BitArchVariant();
1355 if (sizeof(void *) == 4 && PT.isArch64Bit())
1356 PT = PT.get32BitArchVariant();
1357
1358 return PT.str();
1359}