blob: 542ebe9e5370816b2d091a3ff4622448c2eff90b [file] [log] [blame]
Simon Pilgrima271c542017-05-03 15:42:29 +00001//===-- Host.cpp - Implement OS Host Concept --------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Simon Pilgrima271c542017-05-03 15:42:29 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the operating system Host concept.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Support/Host.h"
Craig Topperc77d00e2017-11-10 17:10:57 +000014#include "llvm/Support/TargetParser.h"
Simon Pilgrima271c542017-05-03 15:42:29 +000015#include "llvm/ADT/SmallSet.h"
16#include "llvm/ADT/SmallVector.h"
17#include "llvm/ADT/StringRef.h"
18#include "llvm/ADT/StringSwitch.h"
19#include "llvm/ADT/Triple.h"
Nico Weber432a3882018-04-30 14:59:11 +000020#include "llvm/Config/llvm-config.h"
Simon Pilgrima271c542017-05-03 15:42:29 +000021#include "llvm/Support/Debug.h"
22#include "llvm/Support/FileSystem.h"
23#include "llvm/Support/MemoryBuffer.h"
24#include "llvm/Support/raw_ostream.h"
25#include <assert.h>
26#include <string.h>
27
28// Include the platform-specific parts of this class.
29#ifdef LLVM_ON_UNIX
30#include "Unix/Host.inc"
31#endif
Nico Weber712e8d22018-04-29 00:45:03 +000032#ifdef _WIN32
Simon Pilgrima271c542017-05-03 15:42:29 +000033#include "Windows/Host.inc"
34#endif
35#ifdef _MSC_VER
36#include <intrin.h>
37#endif
38#if defined(__APPLE__) && (defined(__ppc__) || defined(__powerpc__))
39#include <mach/host_info.h>
40#include <mach/mach.h>
41#include <mach/mach_host.h>
42#include <mach/machine.h>
43#endif
44
45#define DEBUG_TYPE "host-detection"
46
47//===----------------------------------------------------------------------===//
48//
49// Implementations of the CPU detection routines
50//
51//===----------------------------------------------------------------------===//
52
53using namespace llvm;
54
55static std::unique_ptr<llvm::MemoryBuffer>
56 LLVM_ATTRIBUTE_UNUSED getProcCpuinfoContent() {
57 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
58 llvm::MemoryBuffer::getFileAsStream("/proc/cpuinfo");
59 if (std::error_code EC = Text.getError()) {
60 llvm::errs() << "Can't read "
61 << "/proc/cpuinfo: " << EC.message() << "\n";
62 return nullptr;
63 }
64 return std::move(*Text);
65}
66
Craig Topper8665f592018-03-07 17:53:16 +000067StringRef sys::detail::getHostCPUNameForPowerPC(StringRef ProcCpuinfoContent) {
Simon Pilgrima271c542017-05-03 15:42:29 +000068 // Access to the Processor Version Register (PVR) on PowerPC is privileged,
69 // and so we must use an operating-system interface to determine the current
70 // processor type. On Linux, this is exposed through the /proc/cpuinfo file.
71 const char *generic = "generic";
72
73 // The cpu line is second (after the 'processor: 0' line), so if this
74 // buffer is too small then something has changed (or is wrong).
75 StringRef::const_iterator CPUInfoStart = ProcCpuinfoContent.begin();
76 StringRef::const_iterator CPUInfoEnd = ProcCpuinfoContent.end();
77
78 StringRef::const_iterator CIP = CPUInfoStart;
79
80 StringRef::const_iterator CPUStart = 0;
81 size_t CPULen = 0;
82
83 // We need to find the first line which starts with cpu, spaces, and a colon.
84 // After the colon, there may be some additional spaces and then the cpu type.
85 while (CIP < CPUInfoEnd && CPUStart == 0) {
86 if (CIP < CPUInfoEnd && *CIP == '\n')
87 ++CIP;
88
89 if (CIP < CPUInfoEnd && *CIP == 'c') {
90 ++CIP;
91 if (CIP < CPUInfoEnd && *CIP == 'p') {
92 ++CIP;
93 if (CIP < CPUInfoEnd && *CIP == 'u') {
94 ++CIP;
95 while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t'))
96 ++CIP;
97
98 if (CIP < CPUInfoEnd && *CIP == ':') {
99 ++CIP;
100 while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t'))
101 ++CIP;
102
103 if (CIP < CPUInfoEnd) {
104 CPUStart = CIP;
105 while (CIP < CPUInfoEnd && (*CIP != ' ' && *CIP != '\t' &&
106 *CIP != ',' && *CIP != '\n'))
107 ++CIP;
108 CPULen = CIP - CPUStart;
109 }
110 }
111 }
112 }
113 }
114
115 if (CPUStart == 0)
116 while (CIP < CPUInfoEnd && *CIP != '\n')
117 ++CIP;
118 }
119
120 if (CPUStart == 0)
121 return generic;
122
123 return StringSwitch<const char *>(StringRef(CPUStart, CPULen))
124 .Case("604e", "604e")
125 .Case("604", "604")
126 .Case("7400", "7400")
127 .Case("7410", "7400")
128 .Case("7447", "7400")
129 .Case("7455", "7450")
130 .Case("G4", "g4")
131 .Case("POWER4", "970")
132 .Case("PPC970FX", "970")
133 .Case("PPC970MP", "970")
134 .Case("G5", "g5")
135 .Case("POWER5", "g5")
136 .Case("A2", "a2")
137 .Case("POWER6", "pwr6")
138 .Case("POWER7", "pwr7")
139 .Case("POWER8", "pwr8")
140 .Case("POWER8E", "pwr8")
141 .Case("POWER8NVL", "pwr8")
142 .Case("POWER9", "pwr9")
143 .Default(generic);
144}
145
Craig Topper8665f592018-03-07 17:53:16 +0000146StringRef sys::detail::getHostCPUNameForARM(StringRef ProcCpuinfoContent) {
Simon Pilgrima271c542017-05-03 15:42:29 +0000147 // The cpuid register on arm is not accessible from user space. On Linux,
148 // it is exposed through the /proc/cpuinfo file.
149
150 // Read 32 lines from /proc/cpuinfo, which should contain the CPU part line
151 // in all cases.
152 SmallVector<StringRef, 32> Lines;
153 ProcCpuinfoContent.split(Lines, "\n");
154
155 // Look for the CPU implementer line.
156 StringRef Implementer;
157 StringRef Hardware;
158 for (unsigned I = 0, E = Lines.size(); I != E; ++I) {
159 if (Lines[I].startswith("CPU implementer"))
160 Implementer = Lines[I].substr(15).ltrim("\t :");
161 if (Lines[I].startswith("Hardware"))
162 Hardware = Lines[I].substr(8).ltrim("\t :");
163 }
164
165 if (Implementer == "0x41") { // ARM Ltd.
166 // MSM8992/8994 may give cpu part for the core that the kernel is running on,
167 // which is undeterministic and wrong. Always return cortex-a53 for these SoC.
168 if (Hardware.endswith("MSM8994") || Hardware.endswith("MSM8996"))
169 return "cortex-a53";
170
171
172 // Look for the CPU part line.
173 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
174 if (Lines[I].startswith("CPU part"))
175 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
176 // values correspond to the "Part number" in the CP15/c0 register. The
177 // contents are specified in the various processor manuals.
178 return StringSwitch<const char *>(Lines[I].substr(8).ltrim("\t :"))
179 .Case("0x926", "arm926ej-s")
180 .Case("0xb02", "mpcore")
181 .Case("0xb36", "arm1136j-s")
182 .Case("0xb56", "arm1156t2-s")
183 .Case("0xb76", "arm1176jz-s")
184 .Case("0xc08", "cortex-a8")
185 .Case("0xc09", "cortex-a9")
186 .Case("0xc0f", "cortex-a15")
187 .Case("0xc20", "cortex-m0")
188 .Case("0xc23", "cortex-m3")
189 .Case("0xc24", "cortex-m4")
190 .Case("0xd04", "cortex-a35")
191 .Case("0xd03", "cortex-a53")
192 .Case("0xd07", "cortex-a57")
193 .Case("0xd08", "cortex-a72")
194 .Case("0xd09", "cortex-a73")
195 .Default("generic");
196 }
197
Joel Jones0a6c0002018-10-05 22:23:21 +0000198 if (Implementer == "0x42" || Implementer == "0x43") { // Broadcom | Cavium.
199 for (unsigned I = 0, E = Lines.size(); I != E; ++I) {
200 if (Lines[I].startswith("CPU part")) {
201 return StringSwitch<const char *>(Lines[I].substr(8).ltrim("\t :"))
202 .Case("0x516", "thunderx2t99")
203 .Case("0x0516", "thunderx2t99")
204 .Case("0xaf", "thunderx2t99")
205 .Case("0x0af", "thunderx2t99")
206 .Case("0xa1", "thunderxt88")
207 .Case("0x0a1", "thunderxt88")
208 .Default("generic");
209 }
210 }
211 }
212
Bryan Chan12355392018-11-09 19:32:08 +0000213 if (Implementer == "0x48") // HiSilicon Technologies, Inc.
214 // Look for the CPU part line.
215 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
216 if (Lines[I].startswith("CPU part"))
217 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
218 // values correspond to the "Part number" in the CP15/c0 register. The
219 // contents are specified in the various processor manuals.
220 return StringSwitch<const char *>(Lines[I].substr(8).ltrim("\t :"))
221 .Case("0xd01", "tsv110")
222 .Default("generic");
223
Simon Pilgrima271c542017-05-03 15:42:29 +0000224 if (Implementer == "0x51") // Qualcomm Technologies, Inc.
225 // Look for the CPU part line.
226 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
227 if (Lines[I].startswith("CPU part"))
228 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
229 // values correspond to the "Part number" in the CP15/c0 register. The
230 // contents are specified in the various processor manuals.
231 return StringSwitch<const char *>(Lines[I].substr(8).ltrim("\t :"))
232 .Case("0x06f", "krait") // APQ8064
233 .Case("0x201", "kryo")
234 .Case("0x205", "kryo")
Eli Friedmanbde9fc72017-09-13 21:48:00 +0000235 .Case("0x211", "kryo")
236 .Case("0x800", "cortex-a73")
237 .Case("0x801", "cortex-a73")
Balaram Makama1e7ecc72017-09-22 17:46:36 +0000238 .Case("0xc00", "falkor")
Chad Rosier71070852017-09-25 14:05:00 +0000239 .Case("0xc01", "saphira")
Simon Pilgrima271c542017-05-03 15:42:29 +0000240 .Default("generic");
241
Evandro Menezes5d7a9e62017-12-08 21:09:59 +0000242 if (Implementer == "0x53") { // Samsung Electronics Co., Ltd.
243 // The Exynos chips have a convoluted ID scheme that doesn't seem to follow
244 // any predictive pattern across variants and parts.
245 unsigned Variant = 0, Part = 0;
246
247 // Look for the CPU variant line, whose value is a 1 digit hexadecimal
248 // number, corresponding to the Variant bits in the CP15/C0 register.
249 for (auto I : Lines)
250 if (I.consume_front("CPU variant"))
251 I.ltrim("\t :").getAsInteger(0, Variant);
252
253 // Look for the CPU part line, whose value is a 3 digit hexadecimal
254 // number, corresponding to the PartNum bits in the CP15/C0 register.
255 for (auto I : Lines)
256 if (I.consume_front("CPU part"))
257 I.ltrim("\t :").getAsInteger(0, Part);
258
259 unsigned Exynos = (Variant << 12) | Part;
260 switch (Exynos) {
261 default:
262 // Default by falling through to Exynos M1.
263 LLVM_FALLTHROUGH;
264
265 case 0x1001:
266 return "exynos-m1";
267
268 case 0x4001:
269 return "exynos-m2";
270 }
271 }
272
Simon Pilgrima271c542017-05-03 15:42:29 +0000273 return "generic";
274}
275
Craig Topper8665f592018-03-07 17:53:16 +0000276StringRef sys::detail::getHostCPUNameForS390x(StringRef ProcCpuinfoContent) {
Simon Pilgrima271c542017-05-03 15:42:29 +0000277 // STIDP is a privileged operation, so use /proc/cpuinfo instead.
278
279 // The "processor 0:" line comes after a fair amount of other information,
280 // including a cache breakdown, but this should be plenty.
281 SmallVector<StringRef, 32> Lines;
282 ProcCpuinfoContent.split(Lines, "\n");
283
284 // Look for the CPU features.
285 SmallVector<StringRef, 32> CPUFeatures;
286 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
287 if (Lines[I].startswith("features")) {
288 size_t Pos = Lines[I].find(":");
289 if (Pos != StringRef::npos) {
290 Lines[I].drop_front(Pos + 1).split(CPUFeatures, ' ');
291 break;
292 }
293 }
294
295 // We need to check for the presence of vector support independently of
296 // the machine type, since we may only use the vector register set when
297 // supported by the kernel (and hypervisor).
298 bool HaveVectorSupport = false;
299 for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) {
300 if (CPUFeatures[I] == "vx")
301 HaveVectorSupport = true;
302 }
303
304 // Now check the processor machine type.
305 for (unsigned I = 0, E = Lines.size(); I != E; ++I) {
306 if (Lines[I].startswith("processor ")) {
307 size_t Pos = Lines[I].find("machine = ");
308 if (Pos != StringRef::npos) {
309 Pos += sizeof("machine = ") - 1;
310 unsigned int Id;
311 if (!Lines[I].drop_front(Pos).getAsInteger(10, Id)) {
Ulrich Weigand2b3482f2017-07-17 17:41:11 +0000312 if (Id >= 3906 && HaveVectorSupport)
313 return "z14";
Simon Pilgrima271c542017-05-03 15:42:29 +0000314 if (Id >= 2964 && HaveVectorSupport)
315 return "z13";
316 if (Id >= 2827)
317 return "zEC12";
318 if (Id >= 2817)
319 return "z196";
320 }
321 }
322 break;
323 }
324 }
325
326 return "generic";
327}
328
Yonghong Songdc1dbf62017-08-23 04:25:57 +0000329StringRef sys::detail::getHostCPUNameForBPF() {
330#if !defined(__linux__) || !defined(__x86_64__)
331 return "generic";
332#else
Jiong Wang66b18e52019-02-07 10:43:09 +0000333 uint8_t v3_insns[40] __attribute__ ((aligned (8))) =
334 /* BPF_MOV64_IMM(BPF_REG_0, 0) */
335 { 0xb7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
336 /* BPF_MOV64_IMM(BPF_REG_2, 1) */
337 0xb7, 0x2, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
338 /* BPF_JMP32_REG(BPF_JLT, BPF_REG_0, BPF_REG_2, 1) */
339 0xae, 0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0,
340 /* BPF_MOV64_IMM(BPF_REG_0, 1) */
341 0xb7, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
342 /* BPF_EXIT_INSN() */
343 0x95, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
344
345 uint8_t v2_insns[40] __attribute__ ((aligned (8))) =
Yonghong Songdc1dbf62017-08-23 04:25:57 +0000346 /* BPF_MOV64_IMM(BPF_REG_0, 0) */
347 { 0xb7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
348 /* BPF_MOV64_IMM(BPF_REG_2, 1) */
349 0xb7, 0x2, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
350 /* BPF_JMP_REG(BPF_JLT, BPF_REG_0, BPF_REG_2, 1) */
351 0xad, 0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0,
352 /* BPF_MOV64_IMM(BPF_REG_0, 1) */
353 0xb7, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
354 /* BPF_EXIT_INSN() */
355 0x95, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
356
357 struct bpf_prog_load_attr {
358 uint32_t prog_type;
359 uint32_t insn_cnt;
360 uint64_t insns;
361 uint64_t license;
362 uint32_t log_level;
363 uint32_t log_size;
364 uint64_t log_buf;
365 uint32_t kern_version;
366 uint32_t prog_flags;
367 } attr = {};
368 attr.prog_type = 1; /* BPF_PROG_TYPE_SOCKET_FILTER */
369 attr.insn_cnt = 5;
Jiong Wang66b18e52019-02-07 10:43:09 +0000370 attr.insns = (uint64_t)v3_insns;
Yonghong Songdc1dbf62017-08-23 04:25:57 +0000371 attr.license = (uint64_t)"DUMMY";
372
Jiong Wang66b18e52019-02-07 10:43:09 +0000373 int fd = syscall(321 /* __NR_bpf */, 5 /* BPF_PROG_LOAD */, &attr,
374 sizeof(attr));
375 if (fd >= 0) {
376 close(fd);
377 return "v3";
378 }
379
380 /* Clear the whole attr in case its content changed by syscall. */
381 memset(&attr, 0, sizeof(attr));
382 attr.prog_type = 1; /* BPF_PROG_TYPE_SOCKET_FILTER */
383 attr.insn_cnt = 5;
384 attr.insns = (uint64_t)v2_insns;
385 attr.license = (uint64_t)"DUMMY";
386 fd = syscall(321 /* __NR_bpf */, 5 /* BPF_PROG_LOAD */, &attr, sizeof(attr));
Yonghong Songc6d25712017-08-23 16:24:31 +0000387 if (fd >= 0) {
388 close(fd);
389 return "v2";
390 }
391 return "v1";
Yonghong Songdc1dbf62017-08-23 04:25:57 +0000392#endif
393}
394
Simon Pilgrima271c542017-05-03 15:42:29 +0000395#if defined(__i386__) || defined(_M_IX86) || \
396 defined(__x86_64__) || defined(_M_X64)
397
398enum VendorSignatures {
399 SIG_INTEL = 0x756e6547 /* Genu */,
400 SIG_AMD = 0x68747541 /* Auth */
401};
402
Simon Pilgrima271c542017-05-03 15:42:29 +0000403// The check below for i386 was copied from clang's cpuid.h (__get_cpuid_max).
404// Check motivated by bug reports for OpenSSL crashing on CPUs without CPUID
405// support. Consequently, for i386, the presence of CPUID is checked first
406// via the corresponding eflags bit.
407// Removal of cpuid.h header motivated by PR30384
408// Header cpuid.h and method __get_cpuid_max are not used in llvm, clang, openmp
409// or test-suite, but are used in external projects e.g. libstdcxx
410static bool isCpuIdSupported() {
411#if defined(__GNUC__) || defined(__clang__)
412#if defined(__i386__)
413 int __cpuid_supported;
414 __asm__(" pushfl\n"
415 " popl %%eax\n"
416 " movl %%eax,%%ecx\n"
417 " xorl $0x00200000,%%eax\n"
418 " pushl %%eax\n"
419 " popfl\n"
420 " pushfl\n"
421 " popl %%eax\n"
422 " movl $0,%0\n"
423 " cmpl %%eax,%%ecx\n"
424 " je 1f\n"
425 " movl $1,%0\n"
426 "1:"
427 : "=r"(__cpuid_supported)
428 :
429 : "eax", "ecx");
430 if (!__cpuid_supported)
431 return false;
432#endif
433 return true;
434#endif
435 return true;
436}
437
438/// getX86CpuIDAndInfo - Execute the specified cpuid and return the 4 values in
439/// the specified arguments. If we can't run cpuid on the host, return true.
440static bool getX86CpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
441 unsigned *rECX, unsigned *rEDX) {
Simon Pilgrima271c542017-05-03 15:42:29 +0000442#if defined(__GNUC__) || defined(__clang__)
443#if defined(__x86_64__)
444 // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
445 // FIXME: should we save this for Clang?
446 __asm__("movq\t%%rbx, %%rsi\n\t"
447 "cpuid\n\t"
448 "xchgq\t%%rbx, %%rsi\n\t"
449 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
450 : "a"(value));
Craig Topper1efd10a2017-07-10 06:04:11 +0000451 return false;
Simon Pilgrima271c542017-05-03 15:42:29 +0000452#elif defined(__i386__)
453 __asm__("movl\t%%ebx, %%esi\n\t"
454 "cpuid\n\t"
455 "xchgl\t%%ebx, %%esi\n\t"
456 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
457 : "a"(value));
Craig Topper1efd10a2017-07-10 06:04:11 +0000458 return false;
Simon Pilgrima271c542017-05-03 15:42:29 +0000459#else
Craig Topper1efd10a2017-07-10 06:04:11 +0000460 return true;
Simon Pilgrima271c542017-05-03 15:42:29 +0000461#endif
462#elif defined(_MSC_VER)
463 // The MSVC intrinsic is portable across x86 and x64.
464 int registers[4];
465 __cpuid(registers, value);
466 *rEAX = registers[0];
467 *rEBX = registers[1];
468 *rECX = registers[2];
469 *rEDX = registers[3];
Simon Pilgrima271c542017-05-03 15:42:29 +0000470 return false;
471#else
472 return true;
473#endif
474}
475
476/// getX86CpuIDAndInfoEx - Execute the specified cpuid with subleaf and return
477/// the 4 values in the specified arguments. If we can't run cpuid on the host,
478/// return true.
479static bool getX86CpuIDAndInfoEx(unsigned value, unsigned subleaf,
480 unsigned *rEAX, unsigned *rEBX, unsigned *rECX,
481 unsigned *rEDX) {
Simon Pilgrima271c542017-05-03 15:42:29 +0000482#if defined(__GNUC__) || defined(__clang__)
Craig Topper828cf302017-07-17 05:16:16 +0000483#if defined(__x86_64__)
Craig Topperada983a2017-07-10 06:09:22 +0000484 // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
Simon Pilgrima271c542017-05-03 15:42:29 +0000485 // FIXME: should we save this for Clang?
486 __asm__("movq\t%%rbx, %%rsi\n\t"
487 "cpuid\n\t"
488 "xchgq\t%%rbx, %%rsi\n\t"
489 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
490 : "a"(value), "c"(subleaf));
Craig Topper1efd10a2017-07-10 06:04:11 +0000491 return false;
Craig Topper828cf302017-07-17 05:16:16 +0000492#elif defined(__i386__)
493 __asm__("movl\t%%ebx, %%esi\n\t"
494 "cpuid\n\t"
495 "xchgl\t%%ebx, %%esi\n\t"
496 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
497 : "a"(value), "c"(subleaf));
498 return false;
499#else
500 return true;
501#endif
Simon Pilgrima271c542017-05-03 15:42:29 +0000502#elif defined(_MSC_VER)
503 int registers[4];
504 __cpuidex(registers, value, subleaf);
505 *rEAX = registers[0];
506 *rEBX = registers[1];
507 *rECX = registers[2];
508 *rEDX = registers[3];
Craig Topper1efd10a2017-07-10 06:04:11 +0000509 return false;
510#else
511 return true;
Simon Pilgrima271c542017-05-03 15:42:29 +0000512#endif
Simon Pilgrima271c542017-05-03 15:42:29 +0000513}
514
Craig Topperf3af64e2017-07-12 06:49:57 +0000515// Read control register 0 (XCR0). Used to detect features such as AVX.
Simon Pilgrima271c542017-05-03 15:42:29 +0000516static bool getX86XCR0(unsigned *rEAX, unsigned *rEDX) {
517#if defined(__GNUC__) || defined(__clang__)
518 // Check xgetbv; this uses a .byte sequence instead of the instruction
519 // directly because older assemblers do not include support for xgetbv and
520 // there is no easy way to conditionally compile based on the assembler used.
521 __asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(*rEAX), "=d"(*rEDX) : "c"(0));
522 return false;
523#elif defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK)
524 unsigned long long Result = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
525 *rEAX = Result;
526 *rEDX = Result >> 32;
527 return false;
528#else
529 return true;
530#endif
531}
532
533static void detectX86FamilyModel(unsigned EAX, unsigned *Family,
534 unsigned *Model) {
535 *Family = (EAX >> 8) & 0xf; // Bits 8 - 11
536 *Model = (EAX >> 4) & 0xf; // Bits 4 - 7
537 if (*Family == 6 || *Family == 0xf) {
538 if (*Family == 0xf)
539 // Examine extended family ID if family ID is F.
540 *Family += (EAX >> 20) & 0xff; // Bits 20 - 27
541 // Examine extended model ID if family ID is 6 or F.
542 *Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
543 }
544}
545
546static void
Craig Topperc6bbe4b2017-07-08 05:16:14 +0000547getIntelProcessorTypeAndSubtype(unsigned Family, unsigned Model,
548 unsigned Brand_id, unsigned Features,
Craig Topper0aca35d2018-10-20 03:51:43 +0000549 unsigned Features2, unsigned Features3,
550 unsigned *Type, unsigned *Subtype) {
Simon Pilgrima271c542017-05-03 15:42:29 +0000551 if (Brand_id != 0)
552 return;
553 switch (Family) {
554 case 3:
Craig Topperc77d00e2017-11-10 17:10:57 +0000555 *Type = X86::INTEL_i386;
Simon Pilgrima271c542017-05-03 15:42:29 +0000556 break;
557 case 4:
Craig Topperc77d00e2017-11-10 17:10:57 +0000558 *Type = X86::INTEL_i486;
Simon Pilgrima271c542017-05-03 15:42:29 +0000559 break;
560 case 5:
Craig Topper47c87392017-11-21 23:36:42 +0000561 if (Features & (1 << X86::FEATURE_MMX)) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000562 *Type = X86::INTEL_PENTIUM_MMX;
Simon Pilgrima271c542017-05-03 15:42:29 +0000563 break;
564 }
Craig Topperc77d00e2017-11-10 17:10:57 +0000565 *Type = X86::INTEL_PENTIUM;
Simon Pilgrima271c542017-05-03 15:42:29 +0000566 break;
567 case 6:
568 switch (Model) {
569 case 0x01: // Pentium Pro processor
Craig Topperc77d00e2017-11-10 17:10:57 +0000570 *Type = X86::INTEL_PENTIUM_PRO;
Simon Pilgrima271c542017-05-03 15:42:29 +0000571 break;
572 case 0x03: // Intel Pentium II OverDrive processor, Pentium II processor,
573 // model 03
574 case 0x05: // Pentium II processor, model 05, Pentium II Xeon processor,
575 // model 05, and Intel Celeron processor, model 05
576 case 0x06: // Celeron processor, model 06
Craig Topperc77d00e2017-11-10 17:10:57 +0000577 *Type = X86::INTEL_PENTIUM_II;
Simon Pilgrima271c542017-05-03 15:42:29 +0000578 break;
579 case 0x07: // Pentium III processor, model 07, and Pentium III Xeon
580 // processor, model 07
581 case 0x08: // Pentium III processor, model 08, Pentium III Xeon processor,
582 // model 08, and Celeron processor, model 08
583 case 0x0a: // Pentium III Xeon processor, model 0Ah
584 case 0x0b: // Pentium III processor, model 0Bh
Craig Topperc77d00e2017-11-10 17:10:57 +0000585 *Type = X86::INTEL_PENTIUM_III;
Simon Pilgrima271c542017-05-03 15:42:29 +0000586 break;
587 case 0x09: // Intel Pentium M processor, Intel Celeron M processor model 09.
588 case 0x0d: // Intel Pentium M processor, Intel Celeron M processor, model
589 // 0Dh. All processors are manufactured using the 90 nm process.
590 case 0x15: // Intel EP80579 Integrated Processor and Intel EP80579
591 // Integrated Processor with Intel QuickAssist Technology
Craig Topperc77d00e2017-11-10 17:10:57 +0000592 *Type = X86::INTEL_PENTIUM_M;
Simon Pilgrima271c542017-05-03 15:42:29 +0000593 break;
594 case 0x0e: // Intel Core Duo processor, Intel Core Solo processor, model
595 // 0Eh. All processors are manufactured using the 65 nm process.
Craig Topperc77d00e2017-11-10 17:10:57 +0000596 *Type = X86::INTEL_CORE_DUO;
Simon Pilgrima271c542017-05-03 15:42:29 +0000597 break; // yonah
598 case 0x0f: // Intel Core 2 Duo processor, Intel Core 2 Duo mobile
599 // processor, Intel Core 2 Quad processor, Intel Core 2 Quad
600 // mobile processor, Intel Core 2 Extreme processor, Intel
601 // Pentium Dual-Core processor, Intel Xeon processor, model
602 // 0Fh. All processors are manufactured using the 65 nm process.
603 case 0x16: // Intel Celeron processor model 16h. All processors are
604 // manufactured using the 65 nm process
Craig Topperc77d00e2017-11-10 17:10:57 +0000605 *Type = X86::INTEL_CORE2; // "core2"
606 *Subtype = X86::INTEL_CORE2_65;
Simon Pilgrima271c542017-05-03 15:42:29 +0000607 break;
608 case 0x17: // Intel Core 2 Extreme processor, Intel Xeon processor, model
609 // 17h. All processors are manufactured using the 45 nm process.
610 //
611 // 45nm: Penryn , Wolfdale, Yorkfield (XE)
612 case 0x1d: // Intel Xeon processor MP. All processors are manufactured using
613 // the 45 nm process.
Craig Topperc77d00e2017-11-10 17:10:57 +0000614 *Type = X86::INTEL_CORE2; // "penryn"
615 *Subtype = X86::INTEL_CORE2_45;
Simon Pilgrima271c542017-05-03 15:42:29 +0000616 break;
617 case 0x1a: // Intel Core i7 processor and Intel Xeon processor. All
618 // processors are manufactured using the 45 nm process.
619 case 0x1e: // Intel(R) Core(TM) i7 CPU 870 @ 2.93GHz.
620 // As found in a Summer 2010 model iMac.
621 case 0x1f:
622 case 0x2e: // Nehalem EX
Craig Topperc77d00e2017-11-10 17:10:57 +0000623 *Type = X86::INTEL_COREI7; // "nehalem"
624 *Subtype = X86::INTEL_COREI7_NEHALEM;
Simon Pilgrima271c542017-05-03 15:42:29 +0000625 break;
626 case 0x25: // Intel Core i7, laptop version.
627 case 0x2c: // Intel Core i7 processor and Intel Xeon processor. All
628 // processors are manufactured using the 32 nm process.
629 case 0x2f: // Westmere EX
Craig Topperc77d00e2017-11-10 17:10:57 +0000630 *Type = X86::INTEL_COREI7; // "westmere"
631 *Subtype = X86::INTEL_COREI7_WESTMERE;
Simon Pilgrima271c542017-05-03 15:42:29 +0000632 break;
633 case 0x2a: // Intel Core i7 processor. All processors are manufactured
634 // using the 32 nm process.
635 case 0x2d:
Craig Topperc77d00e2017-11-10 17:10:57 +0000636 *Type = X86::INTEL_COREI7; //"sandybridge"
637 *Subtype = X86::INTEL_COREI7_SANDYBRIDGE;
Simon Pilgrima271c542017-05-03 15:42:29 +0000638 break;
639 case 0x3a:
640 case 0x3e: // Ivy Bridge EP
Craig Topperc77d00e2017-11-10 17:10:57 +0000641 *Type = X86::INTEL_COREI7; // "ivybridge"
642 *Subtype = X86::INTEL_COREI7_IVYBRIDGE;
Simon Pilgrima271c542017-05-03 15:42:29 +0000643 break;
644
645 // Haswell:
646 case 0x3c:
647 case 0x3f:
648 case 0x45:
649 case 0x46:
Craig Topperc77d00e2017-11-10 17:10:57 +0000650 *Type = X86::INTEL_COREI7; // "haswell"
651 *Subtype = X86::INTEL_COREI7_HASWELL;
Simon Pilgrima271c542017-05-03 15:42:29 +0000652 break;
653
654 // Broadwell:
655 case 0x3d:
656 case 0x47:
657 case 0x4f:
658 case 0x56:
Craig Topperc77d00e2017-11-10 17:10:57 +0000659 *Type = X86::INTEL_COREI7; // "broadwell"
660 *Subtype = X86::INTEL_COREI7_BROADWELL;
Simon Pilgrima271c542017-05-03 15:42:29 +0000661 break;
662
663 // Skylake:
664 case 0x4e: // Skylake mobile
665 case 0x5e: // Skylake desktop
666 case 0x8e: // Kaby Lake mobile
667 case 0x9e: // Kaby Lake desktop
Craig Topperc77d00e2017-11-10 17:10:57 +0000668 *Type = X86::INTEL_COREI7; // "skylake"
669 *Subtype = X86::INTEL_COREI7_SKYLAKE;
Simon Pilgrima271c542017-05-03 15:42:29 +0000670 break;
671
672 // Skylake Xeon:
673 case 0x55:
Craig Topperc77d00e2017-11-10 17:10:57 +0000674 *Type = X86::INTEL_COREI7;
675 *Subtype = X86::INTEL_COREI7_SKYLAKE_AVX512; // "skylake-avx512"
Simon Pilgrima271c542017-05-03 15:42:29 +0000676 break;
677
Craig Topper07491862017-11-15 06:02:42 +0000678 // Cannonlake:
679 case 0x66:
680 *Type = X86::INTEL_COREI7;
681 *Subtype = X86::INTEL_COREI7_CANNONLAKE; // "cannonlake"
682 break;
683
Craig Toppercac6b762019-05-20 16:58:23 +0000684 // Icelake:
Craig Topper2f1895e2019-05-22 19:51:35 +0000685 case 0x7d:
Craig Toppercac6b762019-05-20 16:58:23 +0000686 case 0x7e:
687 *Type = X86::INTEL_COREI7;
688 *Subtype = X86::INTEL_COREI7_ICELAKE_CLIENT; // "icelake-client"
689 break;
690
Craig Topper2f1895e2019-05-22 19:51:35 +0000691 // Icelake Xeon:
692 case 0x6a:
693 case 0x6c:
694 *Type = X86::INTEL_COREI7;
695 *Subtype = X86::INTEL_COREI7_ICELAKE_SERVER; // "icelake-server"
696 break;
697
Simon Pilgrima271c542017-05-03 15:42:29 +0000698 case 0x1c: // Most 45 nm Intel Atom processors
699 case 0x26: // 45 nm Atom Lincroft
700 case 0x27: // 32 nm Atom Medfield
701 case 0x35: // 32 nm Atom Midview
702 case 0x36: // 32 nm Atom Midview
Craig Topperc77d00e2017-11-10 17:10:57 +0000703 *Type = X86::INTEL_BONNELL;
Simon Pilgrima271c542017-05-03 15:42:29 +0000704 break; // "bonnell"
705
706 // Atom Silvermont codes from the Intel software optimization guide.
707 case 0x37:
708 case 0x4a:
709 case 0x4d:
710 case 0x5a:
711 case 0x5d:
712 case 0x4c: // really airmont
Craig Topperc77d00e2017-11-10 17:10:57 +0000713 *Type = X86::INTEL_SILVERMONT;
Simon Pilgrima271c542017-05-03 15:42:29 +0000714 break; // "silvermont"
Michael Zuckerman4bcb9c32017-06-29 10:00:33 +0000715 // Goldmont:
Craig Topper0dadfe32017-11-15 06:02:43 +0000716 case 0x5c: // Apollo Lake
717 case 0x5f: // Denverton
Craig Topperc77d00e2017-11-10 17:10:57 +0000718 *Type = X86::INTEL_GOLDMONT;
Michael Zuckerman4bcb9c32017-06-29 10:00:33 +0000719 break; // "goldmont"
Gabor Buella8f1646b2018-04-16 07:47:35 +0000720 case 0x7a:
721 *Type = X86::INTEL_GOLDMONT_PLUS;
722 break;
Craig Toppercac6b762019-05-20 16:58:23 +0000723 case 0x86:
724 *Type = X86::INTEL_TREMONT;
725 break;
Simon Pilgrima271c542017-05-03 15:42:29 +0000726 case 0x57:
Craig Topperc77d00e2017-11-10 17:10:57 +0000727 *Type = X86::INTEL_KNL; // knl
Simon Pilgrima271c542017-05-03 15:42:29 +0000728 break;
Craig Topper5d692912017-10-13 18:10:17 +0000729 case 0x85:
Craig Topperc77d00e2017-11-10 17:10:57 +0000730 *Type = X86::INTEL_KNM; // knm
Craig Topper5d692912017-10-13 18:10:17 +0000731 break;
Simon Pilgrima271c542017-05-03 15:42:29 +0000732
733 default: // Unknown family 6 CPU, try to guess.
Craig Topperaa3f2492018-11-15 18:11:52 +0000734 if (Features & (1 << X86::FEATURE_AVX512VBMI2)) {
735 *Type = X86::INTEL_COREI7;
736 *Subtype = X86::INTEL_COREI7_ICELAKE_CLIENT;
737 break;
738 }
739
Craig Topper47c87392017-11-21 23:36:42 +0000740 if (Features & (1 << X86::FEATURE_AVX512VBMI)) {
Craig Topper07491862017-11-15 06:02:42 +0000741 *Type = X86::INTEL_COREI7;
742 *Subtype = X86::INTEL_COREI7_CANNONLAKE;
Craig Topper4eda7562017-07-27 03:26:52 +0000743 break;
744 }
Craig Topper07491862017-11-15 06:02:42 +0000745
Craig Topper5fb34b52018-11-27 18:05:00 +0000746 if (Features2 & (1 << (X86::FEATURE_AVX512VNNI - 32))) {
747 *Type = X86::INTEL_COREI7;
748 *Subtype = X86::INTEL_COREI7_CASCADELAKE;
749 break;
750 }
751
Craig Topper47c87392017-11-21 23:36:42 +0000752 if (Features & (1 << X86::FEATURE_AVX512VL)) {
Craig Topper07491862017-11-15 06:02:42 +0000753 *Type = X86::INTEL_COREI7;
754 *Subtype = X86::INTEL_COREI7_SKYLAKE_AVX512;
755 break;
756 }
757
Craig Topper47c87392017-11-21 23:36:42 +0000758 if (Features & (1 << X86::FEATURE_AVX512ER)) {
Craig Topper07491862017-11-15 06:02:42 +0000759 *Type = X86::INTEL_KNL; // knl
760 break;
761 }
762
Craig Topper0aca35d2018-10-20 03:51:43 +0000763 if (Features3 & (1 << (X86::FEATURE_CLFLUSHOPT - 64))) {
764 if (Features3 & (1 << (X86::FEATURE_SHA - 64))) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000765 *Type = X86::INTEL_GOLDMONT;
Craig Topper4eda7562017-07-27 03:26:52 +0000766 } else {
Craig Topperc77d00e2017-11-10 17:10:57 +0000767 *Type = X86::INTEL_COREI7;
768 *Subtype = X86::INTEL_COREI7_SKYLAKE;
Craig Topper4eda7562017-07-27 03:26:52 +0000769 }
Simon Pilgrima271c542017-05-03 15:42:29 +0000770 break;
771 }
Craig Topper0aca35d2018-10-20 03:51:43 +0000772 if (Features3 & (1 << (X86::FEATURE_ADX - 64))) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000773 *Type = X86::INTEL_COREI7;
774 *Subtype = X86::INTEL_COREI7_BROADWELL;
Simon Pilgrima271c542017-05-03 15:42:29 +0000775 break;
776 }
Craig Topper47c87392017-11-21 23:36:42 +0000777 if (Features & (1 << X86::FEATURE_AVX2)) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000778 *Type = X86::INTEL_COREI7;
779 *Subtype = X86::INTEL_COREI7_HASWELL;
Simon Pilgrima271c542017-05-03 15:42:29 +0000780 break;
781 }
Craig Topper47c87392017-11-21 23:36:42 +0000782 if (Features & (1 << X86::FEATURE_AVX)) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000783 *Type = X86::INTEL_COREI7;
784 *Subtype = X86::INTEL_COREI7_SANDYBRIDGE;
Simon Pilgrima271c542017-05-03 15:42:29 +0000785 break;
786 }
Craig Topper47c87392017-11-21 23:36:42 +0000787 if (Features & (1 << X86::FEATURE_SSE4_2)) {
Craig Topper0aca35d2018-10-20 03:51:43 +0000788 if (Features3 & (1 << (X86::FEATURE_MOVBE - 64))) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000789 *Type = X86::INTEL_SILVERMONT;
Simon Pilgrima271c542017-05-03 15:42:29 +0000790 } else {
Craig Topperc77d00e2017-11-10 17:10:57 +0000791 *Type = X86::INTEL_COREI7;
792 *Subtype = X86::INTEL_COREI7_NEHALEM;
Simon Pilgrima271c542017-05-03 15:42:29 +0000793 }
794 break;
795 }
Craig Topper47c87392017-11-21 23:36:42 +0000796 if (Features & (1 << X86::FEATURE_SSE4_1)) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000797 *Type = X86::INTEL_CORE2; // "penryn"
798 *Subtype = X86::INTEL_CORE2_45;
Simon Pilgrima271c542017-05-03 15:42:29 +0000799 break;
800 }
Craig Topper47c87392017-11-21 23:36:42 +0000801 if (Features & (1 << X86::FEATURE_SSSE3)) {
Craig Topper0aca35d2018-10-20 03:51:43 +0000802 if (Features3 & (1 << (X86::FEATURE_MOVBE - 64))) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000803 *Type = X86::INTEL_BONNELL; // "bonnell"
Simon Pilgrima271c542017-05-03 15:42:29 +0000804 } else {
Craig Topperc77d00e2017-11-10 17:10:57 +0000805 *Type = X86::INTEL_CORE2; // "core2"
806 *Subtype = X86::INTEL_CORE2_65;
Simon Pilgrima271c542017-05-03 15:42:29 +0000807 }
808 break;
809 }
Craig Topper0aca35d2018-10-20 03:51:43 +0000810 if (Features3 & (1 << (X86::FEATURE_EM64T - 64))) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000811 *Type = X86::INTEL_CORE2; // "core2"
812 *Subtype = X86::INTEL_CORE2_65;
Craig Toppera233e162017-11-02 19:13:32 +0000813 break;
814 }
Craig Topper47c87392017-11-21 23:36:42 +0000815 if (Features & (1 << X86::FEATURE_SSE3)) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000816 *Type = X86::INTEL_CORE_DUO;
Craig Toppera233e162017-11-02 19:13:32 +0000817 break;
Simon Pilgrima271c542017-05-03 15:42:29 +0000818 }
Craig Topper47c87392017-11-21 23:36:42 +0000819 if (Features & (1 << X86::FEATURE_SSE2)) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000820 *Type = X86::INTEL_PENTIUM_M;
Simon Pilgrima271c542017-05-03 15:42:29 +0000821 break;
822 }
Craig Topper47c87392017-11-21 23:36:42 +0000823 if (Features & (1 << X86::FEATURE_SSE)) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000824 *Type = X86::INTEL_PENTIUM_III;
Simon Pilgrima271c542017-05-03 15:42:29 +0000825 break;
826 }
Craig Topper47c87392017-11-21 23:36:42 +0000827 if (Features & (1 << X86::FEATURE_MMX)) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000828 *Type = X86::INTEL_PENTIUM_II;
Simon Pilgrima271c542017-05-03 15:42:29 +0000829 break;
830 }
Craig Topperc77d00e2017-11-10 17:10:57 +0000831 *Type = X86::INTEL_PENTIUM_PRO;
Simon Pilgrima271c542017-05-03 15:42:29 +0000832 break;
833 }
834 break;
835 case 15: {
Craig Topper0aca35d2018-10-20 03:51:43 +0000836 if (Features3 & (1 << (X86::FEATURE_EM64T - 64))) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000837 *Type = X86::INTEL_NOCONA;
Simon Pilgrima271c542017-05-03 15:42:29 +0000838 break;
839 }
Craig Topper47c87392017-11-21 23:36:42 +0000840 if (Features & (1 << X86::FEATURE_SSE3)) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000841 *Type = X86::INTEL_PRESCOTT;
Craig Topper14949152017-11-02 19:13:34 +0000842 break;
843 }
Craig Topperc77d00e2017-11-10 17:10:57 +0000844 *Type = X86::INTEL_PENTIUM_IV;
Simon Pilgrima271c542017-05-03 15:42:29 +0000845 break;
846 }
847 default:
848 break; /*"generic"*/
849 }
850}
851
Craig Topper2ace1532017-07-08 06:44:34 +0000852static void getAMDProcessorTypeAndSubtype(unsigned Family, unsigned Model,
853 unsigned Features, unsigned *Type,
Simon Pilgrima271c542017-05-03 15:42:29 +0000854 unsigned *Subtype) {
855 // FIXME: this poorly matches the generated SubtargetFeatureKV table. There
856 // appears to be no way to generate the wide variety of AMD-specific targets
857 // from the information returned from CPUID.
858 switch (Family) {
859 case 4:
Craig Topperc77d00e2017-11-10 17:10:57 +0000860 *Type = X86::AMD_i486;
Simon Pilgrima271c542017-05-03 15:42:29 +0000861 break;
862 case 5:
Craig Topperc77d00e2017-11-10 17:10:57 +0000863 *Type = X86::AMDPENTIUM;
Simon Pilgrima271c542017-05-03 15:42:29 +0000864 switch (Model) {
865 case 6:
866 case 7:
Craig Topperc77d00e2017-11-10 17:10:57 +0000867 *Subtype = X86::AMDPENTIUM_K6;
Simon Pilgrima271c542017-05-03 15:42:29 +0000868 break; // "k6"
869 case 8:
Craig Topperc77d00e2017-11-10 17:10:57 +0000870 *Subtype = X86::AMDPENTIUM_K62;
Simon Pilgrima271c542017-05-03 15:42:29 +0000871 break; // "k6-2"
872 case 9:
873 case 13:
Craig Topperc77d00e2017-11-10 17:10:57 +0000874 *Subtype = X86::AMDPENTIUM_K63;
Simon Pilgrima271c542017-05-03 15:42:29 +0000875 break; // "k6-3"
876 case 10:
Craig Topperc77d00e2017-11-10 17:10:57 +0000877 *Subtype = X86::AMDPENTIUM_GEODE;
Simon Pilgrima271c542017-05-03 15:42:29 +0000878 break; // "geode"
879 }
880 break;
881 case 6:
Craig Topper47c87392017-11-21 23:36:42 +0000882 if (Features & (1 << X86::FEATURE_SSE)) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000883 *Type = X86::AMD_ATHLON_XP;
Simon Pilgrima271c542017-05-03 15:42:29 +0000884 break; // "athlon-xp"
885 }
Craig Topperc77d00e2017-11-10 17:10:57 +0000886 *Type = X86::AMD_ATHLON;
Craig Topperf3de5eb2017-07-13 06:34:10 +0000887 break; // "athlon"
Simon Pilgrima271c542017-05-03 15:42:29 +0000888 case 15:
Craig Topper47c87392017-11-21 23:36:42 +0000889 if (Features & (1 << X86::FEATURE_SSE3)) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000890 *Type = X86::AMD_K8SSE3;
Simon Pilgrima271c542017-05-03 15:42:29 +0000891 break; // "k8-sse3"
892 }
Craig Topperc77d00e2017-11-10 17:10:57 +0000893 *Type = X86::AMD_K8;
Craig Topperf3de5eb2017-07-13 06:34:10 +0000894 break; // "k8"
Simon Pilgrima271c542017-05-03 15:42:29 +0000895 case 16:
Craig Topperc77d00e2017-11-10 17:10:57 +0000896 *Type = X86::AMDFAM10H; // "amdfam10"
Simon Pilgrima271c542017-05-03 15:42:29 +0000897 switch (Model) {
898 case 2:
Craig Topperc77d00e2017-11-10 17:10:57 +0000899 *Subtype = X86::AMDFAM10H_BARCELONA;
Simon Pilgrima271c542017-05-03 15:42:29 +0000900 break;
901 case 4:
Craig Topperc77d00e2017-11-10 17:10:57 +0000902 *Subtype = X86::AMDFAM10H_SHANGHAI;
Simon Pilgrima271c542017-05-03 15:42:29 +0000903 break;
904 case 8:
Craig Topperc77d00e2017-11-10 17:10:57 +0000905 *Subtype = X86::AMDFAM10H_ISTANBUL;
Simon Pilgrima271c542017-05-03 15:42:29 +0000906 break;
907 }
908 break;
909 case 20:
Craig Topperc77d00e2017-11-10 17:10:57 +0000910 *Type = X86::AMD_BTVER1;
Simon Pilgrima271c542017-05-03 15:42:29 +0000911 break; // "btver1";
912 case 21:
Craig Topperc77d00e2017-11-10 17:10:57 +0000913 *Type = X86::AMDFAM15H;
Craig Topper1f9d3c02017-07-08 06:44:35 +0000914 if (Model >= 0x60 && Model <= 0x7f) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000915 *Subtype = X86::AMDFAM15H_BDVER4;
Craig Topper3db11702017-07-12 06:49:56 +0000916 break; // "bdver4"; 60h-7Fh: Excavator
Simon Pilgrima271c542017-05-03 15:42:29 +0000917 }
918 if (Model >= 0x30 && Model <= 0x3f) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000919 *Subtype = X86::AMDFAM15H_BDVER3;
Simon Pilgrima271c542017-05-03 15:42:29 +0000920 break; // "bdver3"; 30h-3Fh: Steamroller
921 }
Roman Lebedevbc1a9242018-05-01 18:39:31 +0000922 if ((Model >= 0x10 && Model <= 0x1f) || Model == 0x02) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000923 *Subtype = X86::AMDFAM15H_BDVER2;
Roman Lebedevbc1a9242018-05-01 18:39:31 +0000924 break; // "bdver2"; 02h, 10h-1Fh: Piledriver
Simon Pilgrima271c542017-05-03 15:42:29 +0000925 }
926 if (Model <= 0x0f) {
Craig Topperc77d00e2017-11-10 17:10:57 +0000927 *Subtype = X86::AMDFAM15H_BDVER1;
Simon Pilgrima271c542017-05-03 15:42:29 +0000928 break; // "bdver1"; 00h-0Fh: Bulldozer
929 }
930 break;
931 case 22:
Craig Topperc77d00e2017-11-10 17:10:57 +0000932 *Type = X86::AMD_BTVER2;
Simon Pilgrima271c542017-05-03 15:42:29 +0000933 break; // "btver2"
934 case 23:
Craig Topperc77d00e2017-11-10 17:10:57 +0000935 *Type = X86::AMDFAM17H;
Ganesh Gopalasubramaniane172d7002019-02-26 16:55:10 +0000936 if (Model >= 0x30 && Model <= 0x3f) {
937 *Subtype = X86::AMDFAM17H_ZNVER2;
938 break; // "znver2"; 30h-3fh: Zen2
939 }
940 if (Model <= 0x0f) {
941 *Subtype = X86::AMDFAM17H_ZNVER1;
942 break; // "znver1"; 00h-0Fh: Zen1
943 }
Simon Pilgrima271c542017-05-03 15:42:29 +0000944 break;
945 default:
946 break; // "generic"
947 }
948}
949
Craig Topper3a5d0822017-07-12 06:49:58 +0000950static void getAvailableFeatures(unsigned ECX, unsigned EDX, unsigned MaxLeaf,
Craig Topper0aca35d2018-10-20 03:51:43 +0000951 unsigned *FeaturesOut, unsigned *Features2Out,
952 unsigned *Features3Out) {
Simon Pilgrima271c542017-05-03 15:42:29 +0000953 unsigned Features = 0;
Craig Topper3a5d0822017-07-12 06:49:58 +0000954 unsigned Features2 = 0;
Craig Topper0aca35d2018-10-20 03:51:43 +0000955 unsigned Features3 = 0;
Craig Topperc6bbe4b2017-07-08 05:16:14 +0000956 unsigned EAX, EBX;
Craig Topper3a5d0822017-07-12 06:49:58 +0000957
Simon Pilgrima7d1a7c2018-10-20 13:16:31 +0000958 auto setFeature = [&](unsigned F) {
959 if (F < 32)
Craig Topper28659f52018-11-24 20:26:11 +0000960 Features |= 1U << (F & 0x1f);
Simon Pilgrima7d1a7c2018-10-20 13:16:31 +0000961 else if (F < 64)
Craig Topper28659f52018-11-24 20:26:11 +0000962 Features2 |= 1U << ((F - 32) & 0x1f);
Simon Pilgrima7d1a7c2018-10-20 13:16:31 +0000963 else if (F < 96)
Craig Topper28659f52018-11-24 20:26:11 +0000964 Features3 |= 1U << ((F - 64) & 0x1f);
Simon Pilgrima7d1a7c2018-10-20 13:16:31 +0000965 else
966 llvm_unreachable("Unexpected FeatureBit");
967 };
Craig Topper0aca35d2018-10-20 03:51:43 +0000968
Craig Topper3a5d0822017-07-12 06:49:58 +0000969 if ((EDX >> 15) & 1)
Craig Topper0aca35d2018-10-20 03:51:43 +0000970 setFeature(X86::FEATURE_CMOV);
Craig Topper3a5d0822017-07-12 06:49:58 +0000971 if ((EDX >> 23) & 1)
Craig Topper0aca35d2018-10-20 03:51:43 +0000972 setFeature(X86::FEATURE_MMX);
Craig Topper3a5d0822017-07-12 06:49:58 +0000973 if ((EDX >> 25) & 1)
Craig Topper0aca35d2018-10-20 03:51:43 +0000974 setFeature(X86::FEATURE_SSE);
Craig Topper3a5d0822017-07-12 06:49:58 +0000975 if ((EDX >> 26) & 1)
Craig Topper0aca35d2018-10-20 03:51:43 +0000976 setFeature(X86::FEATURE_SSE2);
Craig Topper3a5d0822017-07-12 06:49:58 +0000977
978 if ((ECX >> 0) & 1)
Craig Topper0aca35d2018-10-20 03:51:43 +0000979 setFeature(X86::FEATURE_SSE3);
Craig Topper3a5d0822017-07-12 06:49:58 +0000980 if ((ECX >> 1) & 1)
Craig Topper0aca35d2018-10-20 03:51:43 +0000981 setFeature(X86::FEATURE_PCLMUL);
Craig Topper3a5d0822017-07-12 06:49:58 +0000982 if ((ECX >> 9) & 1)
Craig Topper0aca35d2018-10-20 03:51:43 +0000983 setFeature(X86::FEATURE_SSSE3);
Craig Topper3a5d0822017-07-12 06:49:58 +0000984 if ((ECX >> 12) & 1)
Craig Topper0aca35d2018-10-20 03:51:43 +0000985 setFeature(X86::FEATURE_FMA);
Craig Topper3a5d0822017-07-12 06:49:58 +0000986 if ((ECX >> 19) & 1)
Craig Topper0aca35d2018-10-20 03:51:43 +0000987 setFeature(X86::FEATURE_SSE4_1);
Craig Topper3a5d0822017-07-12 06:49:58 +0000988 if ((ECX >> 20) & 1)
Craig Topper0aca35d2018-10-20 03:51:43 +0000989 setFeature(X86::FEATURE_SSE4_2);
Craig Topper3a5d0822017-07-12 06:49:58 +0000990 if ((ECX >> 23) & 1)
Craig Topper0aca35d2018-10-20 03:51:43 +0000991 setFeature(X86::FEATURE_POPCNT);
Craig Topper3a5d0822017-07-12 06:49:58 +0000992 if ((ECX >> 25) & 1)
Craig Topper0aca35d2018-10-20 03:51:43 +0000993 setFeature(X86::FEATURE_AES);
Craig Topper3a5d0822017-07-12 06:49:58 +0000994
995 if ((ECX >> 22) & 1)
Craig Topper0aca35d2018-10-20 03:51:43 +0000996 setFeature(X86::FEATURE_MOVBE);
Simon Pilgrima271c542017-05-03 15:42:29 +0000997
998 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
999 // indicates that the AVX registers will be saved and restored on context
1000 // switch, then we have full AVX support.
1001 const unsigned AVXBits = (1 << 27) | (1 << 28);
1002 bool HasAVX = ((ECX & AVXBits) == AVXBits) && !getX86XCR0(&EAX, &EDX) &&
1003 ((EAX & 0x6) == 0x6);
1004 bool HasAVX512Save = HasAVX && ((EAX & 0xe0) == 0xe0);
Craig Topper3a5d0822017-07-12 06:49:58 +00001005
1006 if (HasAVX)
Craig Topper0aca35d2018-10-20 03:51:43 +00001007 setFeature(X86::FEATURE_AVX);
Craig Topper3a5d0822017-07-12 06:49:58 +00001008
Simon Pilgrima271c542017-05-03 15:42:29 +00001009 bool HasLeaf7 =
1010 MaxLeaf >= 0x7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
Craig Topper3a5d0822017-07-12 06:49:58 +00001011
1012 if (HasLeaf7 && ((EBX >> 3) & 1))
Craig Topper0aca35d2018-10-20 03:51:43 +00001013 setFeature(X86::FEATURE_BMI);
Craig Topper3a5d0822017-07-12 06:49:58 +00001014 if (HasLeaf7 && ((EBX >> 5) & 1) && HasAVX)
Craig Topper0aca35d2018-10-20 03:51:43 +00001015 setFeature(X86::FEATURE_AVX2);
Craig Topper3a5d0822017-07-12 06:49:58 +00001016 if (HasLeaf7 && ((EBX >> 9) & 1))
Craig Topper0aca35d2018-10-20 03:51:43 +00001017 setFeature(X86::FEATURE_BMI2);
Craig Topper3a5d0822017-07-12 06:49:58 +00001018 if (HasLeaf7 && ((EBX >> 16) & 1) && HasAVX512Save)
Craig Topper0aca35d2018-10-20 03:51:43 +00001019 setFeature(X86::FEATURE_AVX512F);
Craig Topper3a5d0822017-07-12 06:49:58 +00001020 if (HasLeaf7 && ((EBX >> 17) & 1) && HasAVX512Save)
Craig Topper0aca35d2018-10-20 03:51:43 +00001021 setFeature(X86::FEATURE_AVX512DQ);
Craig Topper3a5d0822017-07-12 06:49:58 +00001022 if (HasLeaf7 && ((EBX >> 19) & 1))
Craig Topper0aca35d2018-10-20 03:51:43 +00001023 setFeature(X86::FEATURE_ADX);
Craig Topper3a5d0822017-07-12 06:49:58 +00001024 if (HasLeaf7 && ((EBX >> 21) & 1) && HasAVX512Save)
Craig Topper0aca35d2018-10-20 03:51:43 +00001025 setFeature(X86::FEATURE_AVX512IFMA);
Craig Topper4eda7562017-07-27 03:26:52 +00001026 if (HasLeaf7 && ((EBX >> 23) & 1))
Craig Topper0aca35d2018-10-20 03:51:43 +00001027 setFeature(X86::FEATURE_CLFLUSHOPT);
Craig Topper3a5d0822017-07-12 06:49:58 +00001028 if (HasLeaf7 && ((EBX >> 26) & 1) && HasAVX512Save)
Craig Topper0aca35d2018-10-20 03:51:43 +00001029 setFeature(X86::FEATURE_AVX512PF);
Craig Topper3a5d0822017-07-12 06:49:58 +00001030 if (HasLeaf7 && ((EBX >> 27) & 1) && HasAVX512Save)
Craig Topper0aca35d2018-10-20 03:51:43 +00001031 setFeature(X86::FEATURE_AVX512ER);
Craig Topper3a5d0822017-07-12 06:49:58 +00001032 if (HasLeaf7 && ((EBX >> 28) & 1) && HasAVX512Save)
Craig Topper0aca35d2018-10-20 03:51:43 +00001033 setFeature(X86::FEATURE_AVX512CD);
Craig Topper4eda7562017-07-27 03:26:52 +00001034 if (HasLeaf7 && ((EBX >> 29) & 1))
Craig Topper0aca35d2018-10-20 03:51:43 +00001035 setFeature(X86::FEATURE_SHA);
Craig Topper3a5d0822017-07-12 06:49:58 +00001036 if (HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save)
Craig Topper0aca35d2018-10-20 03:51:43 +00001037 setFeature(X86::FEATURE_AVX512BW);
Craig Topper3a5d0822017-07-12 06:49:58 +00001038 if (HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save)
Craig Topper0aca35d2018-10-20 03:51:43 +00001039 setFeature(X86::FEATURE_AVX512VL);
Craig Topper3a5d0822017-07-12 06:49:58 +00001040
1041 if (HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save)
Craig Topper0aca35d2018-10-20 03:51:43 +00001042 setFeature(X86::FEATURE_AVX512VBMI);
1043 if (HasLeaf7 && ((ECX >> 6) & 1) && HasAVX512Save)
1044 setFeature(X86::FEATURE_AVX512VBMI2);
1045 if (HasLeaf7 && ((ECX >> 8) & 1))
1046 setFeature(X86::FEATURE_GFNI);
1047 if (HasLeaf7 && ((ECX >> 10) & 1) && HasAVX)
1048 setFeature(X86::FEATURE_VPCLMULQDQ);
1049 if (HasLeaf7 && ((ECX >> 11) & 1) && HasAVX512Save)
1050 setFeature(X86::FEATURE_AVX512VNNI);
1051 if (HasLeaf7 && ((ECX >> 12) & 1) && HasAVX512Save)
1052 setFeature(X86::FEATURE_AVX512BITALG);
Craig Topper3a5d0822017-07-12 06:49:58 +00001053 if (HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save)
Craig Topper0aca35d2018-10-20 03:51:43 +00001054 setFeature(X86::FEATURE_AVX512VPOPCNTDQ);
Craig Topper3a5d0822017-07-12 06:49:58 +00001055
1056 if (HasLeaf7 && ((EDX >> 2) & 1) && HasAVX512Save)
Craig Topper0aca35d2018-10-20 03:51:43 +00001057 setFeature(X86::FEATURE_AVX5124VNNIW);
Craig Topper3a5d0822017-07-12 06:49:58 +00001058 if (HasLeaf7 && ((EDX >> 3) & 1) && HasAVX512Save)
Craig Topper0aca35d2018-10-20 03:51:43 +00001059 setFeature(X86::FEATURE_AVX5124FMAPS);
Simon Pilgrima271c542017-05-03 15:42:29 +00001060
Craig Topperbb8c7992017-07-08 05:16:13 +00001061 unsigned MaxExtLevel;
1062 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
1063
1064 bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
1065 !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
Craig Topper3a5d0822017-07-12 06:49:58 +00001066 if (HasExtLeaf1 && ((ECX >> 6) & 1))
Craig Topper0aca35d2018-10-20 03:51:43 +00001067 setFeature(X86::FEATURE_SSE4_A);
Craig Topper3a5d0822017-07-12 06:49:58 +00001068 if (HasExtLeaf1 && ((ECX >> 11) & 1))
Craig Topper0aca35d2018-10-20 03:51:43 +00001069 setFeature(X86::FEATURE_XOP);
Craig Topper3a5d0822017-07-12 06:49:58 +00001070 if (HasExtLeaf1 && ((ECX >> 16) & 1))
Craig Topper0aca35d2018-10-20 03:51:43 +00001071 setFeature(X86::FEATURE_FMA4);
Craig Topperbb8c7992017-07-08 05:16:13 +00001072
Craig Topper3a5d0822017-07-12 06:49:58 +00001073 if (HasExtLeaf1 && ((EDX >> 29) & 1))
Craig Topper0aca35d2018-10-20 03:51:43 +00001074 setFeature(X86::FEATURE_EM64T);
Craig Topper3a5d0822017-07-12 06:49:58 +00001075
1076 *FeaturesOut = Features;
1077 *Features2Out = Features2;
Craig Topper0aca35d2018-10-20 03:51:43 +00001078 *Features3Out = Features3;
Simon Pilgrima271c542017-05-03 15:42:29 +00001079}
1080
1081StringRef sys::getHostCPUName() {
1082 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
1083 unsigned MaxLeaf, Vendor;
1084
1085#if defined(__GNUC__) || defined(__clang__)
1086 //FIXME: include cpuid.h from clang or copy __get_cpuid_max here
1087 // and simplify it to not invoke __cpuid (like cpu_model.c in
1088 // compiler-rt/lib/builtins/cpu_model.c?
1089 // Opting for the second option.
1090 if(!isCpuIdSupported())
1091 return "generic";
1092#endif
Craig Topperbb8c7992017-07-08 05:16:13 +00001093 if (getX86CpuIDAndInfo(0, &MaxLeaf, &Vendor, &ECX, &EDX) || MaxLeaf < 1)
Simon Pilgrima271c542017-05-03 15:42:29 +00001094 return "generic";
Craig Topperbb8c7992017-07-08 05:16:13 +00001095 getX86CpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
Simon Pilgrima271c542017-05-03 15:42:29 +00001096
1097 unsigned Brand_id = EBX & 0xff;
1098 unsigned Family = 0, Model = 0;
Craig Topper0aca35d2018-10-20 03:51:43 +00001099 unsigned Features = 0, Features2 = 0, Features3 = 0;
Simon Pilgrima271c542017-05-03 15:42:29 +00001100 detectX86FamilyModel(EAX, &Family, &Model);
Craig Topper0aca35d2018-10-20 03:51:43 +00001101 getAvailableFeatures(ECX, EDX, MaxLeaf, &Features, &Features2, &Features3);
Simon Pilgrima271c542017-05-03 15:42:29 +00001102
Craig Topper741e7e62017-11-03 18:02:44 +00001103 unsigned Type = 0;
1104 unsigned Subtype = 0;
Simon Pilgrima271c542017-05-03 15:42:29 +00001105
1106 if (Vendor == SIG_INTEL) {
Craig Topper3a5d0822017-07-12 06:49:58 +00001107 getIntelProcessorTypeAndSubtype(Family, Model, Brand_id, Features,
Craig Topper0aca35d2018-10-20 03:51:43 +00001108 Features2, Features3, &Type, &Subtype);
Simon Pilgrima271c542017-05-03 15:42:29 +00001109 } else if (Vendor == SIG_AMD) {
1110 getAMDProcessorTypeAndSubtype(Family, Model, Features, &Type, &Subtype);
Simon Pilgrima271c542017-05-03 15:42:29 +00001111 }
Craig Topperc77d00e2017-11-10 17:10:57 +00001112
1113 // Check subtypes first since those are more specific.
1114#define X86_CPU_SUBTYPE(ARCHNAME, ENUM) \
1115 if (Subtype == X86::ENUM) \
1116 return ARCHNAME;
1117#include "llvm/Support/X86TargetParser.def"
1118
1119 // Now check types.
Craig Topper55ad3292018-03-06 22:45:31 +00001120#define X86_CPU_TYPE(ARCHNAME, ENUM) \
Craig Topperc77d00e2017-11-10 17:10:57 +00001121 if (Type == X86::ENUM) \
1122 return ARCHNAME;
1123#include "llvm/Support/X86TargetParser.def"
1124
Simon Pilgrima271c542017-05-03 15:42:29 +00001125 return "generic";
1126}
1127
1128#elif defined(__APPLE__) && (defined(__ppc__) || defined(__powerpc__))
1129StringRef sys::getHostCPUName() {
1130 host_basic_info_data_t hostInfo;
1131 mach_msg_type_number_t infoCount;
1132
1133 infoCount = HOST_BASIC_INFO_COUNT;
Kristina Brooks51ae9342018-09-04 10:54:09 +00001134 mach_port_t hostPort = mach_host_self();
1135 host_info(hostPort, HOST_BASIC_INFO, (host_info_t)&hostInfo,
Simon Pilgrima271c542017-05-03 15:42:29 +00001136 &infoCount);
Kristina Brooks51ae9342018-09-04 10:54:09 +00001137 mach_port_deallocate(mach_task_self(), hostPort);
Simon Pilgrima271c542017-05-03 15:42:29 +00001138
1139 if (hostInfo.cpu_type != CPU_TYPE_POWERPC)
1140 return "generic";
1141
1142 switch (hostInfo.cpu_subtype) {
1143 case CPU_SUBTYPE_POWERPC_601:
1144 return "601";
1145 case CPU_SUBTYPE_POWERPC_602:
1146 return "602";
1147 case CPU_SUBTYPE_POWERPC_603:
1148 return "603";
1149 case CPU_SUBTYPE_POWERPC_603e:
1150 return "603e";
1151 case CPU_SUBTYPE_POWERPC_603ev:
1152 return "603ev";
1153 case CPU_SUBTYPE_POWERPC_604:
1154 return "604";
1155 case CPU_SUBTYPE_POWERPC_604e:
1156 return "604e";
1157 case CPU_SUBTYPE_POWERPC_620:
1158 return "620";
1159 case CPU_SUBTYPE_POWERPC_750:
1160 return "750";
1161 case CPU_SUBTYPE_POWERPC_7400:
1162 return "7400";
1163 case CPU_SUBTYPE_POWERPC_7450:
1164 return "7450";
1165 case CPU_SUBTYPE_POWERPC_970:
1166 return "970";
1167 default:;
1168 }
1169
1170 return "generic";
1171}
1172#elif defined(__linux__) && (defined(__ppc__) || defined(__powerpc__))
1173StringRef sys::getHostCPUName() {
1174 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
Craig Topper8665f592018-03-07 17:53:16 +00001175 StringRef Content = P ? P->getBuffer() : "";
Simon Pilgrima271c542017-05-03 15:42:29 +00001176 return detail::getHostCPUNameForPowerPC(Content);
1177}
1178#elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1179StringRef sys::getHostCPUName() {
1180 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
Craig Topper8665f592018-03-07 17:53:16 +00001181 StringRef Content = P ? P->getBuffer() : "";
Simon Pilgrima271c542017-05-03 15:42:29 +00001182 return detail::getHostCPUNameForARM(Content);
1183}
1184#elif defined(__linux__) && defined(__s390x__)
1185StringRef sys::getHostCPUName() {
1186 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
Craig Topper8665f592018-03-07 17:53:16 +00001187 StringRef Content = P ? P->getBuffer() : "";
Simon Pilgrima271c542017-05-03 15:42:29 +00001188 return detail::getHostCPUNameForS390x(Content);
1189}
1190#else
1191StringRef sys::getHostCPUName() { return "generic"; }
1192#endif
1193
1194#if defined(__linux__) && defined(__x86_64__)
1195// On Linux, the number of physical cores can be computed from /proc/cpuinfo,
1196// using the number of unique physical/core id pairs. The following
1197// implementation reads the /proc/cpuinfo format on an x86_64 system.
1198static int computeHostNumPhysicalCores() {
1199 // Read /proc/cpuinfo as a stream (until EOF reached). It cannot be
1200 // mmapped because it appears to have 0 size.
1201 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
1202 llvm::MemoryBuffer::getFileAsStream("/proc/cpuinfo");
1203 if (std::error_code EC = Text.getError()) {
1204 llvm::errs() << "Can't read "
1205 << "/proc/cpuinfo: " << EC.message() << "\n";
1206 return -1;
1207 }
1208 SmallVector<StringRef, 8> strs;
1209 (*Text)->getBuffer().split(strs, "\n", /*MaxSplit=*/-1,
1210 /*KeepEmpty=*/false);
1211 int CurPhysicalId = -1;
1212 int CurCoreId = -1;
1213 SmallSet<std::pair<int, int>, 32> UniqueItems;
1214 for (auto &Line : strs) {
1215 Line = Line.trim();
1216 if (!Line.startswith("physical id") && !Line.startswith("core id"))
1217 continue;
1218 std::pair<StringRef, StringRef> Data = Line.split(':');
1219 auto Name = Data.first.trim();
1220 auto Val = Data.second.trim();
1221 if (Name == "physical id") {
1222 assert(CurPhysicalId == -1 &&
1223 "Expected a core id before seeing another physical id");
1224 Val.getAsInteger(10, CurPhysicalId);
1225 }
1226 if (Name == "core id") {
1227 assert(CurCoreId == -1 &&
1228 "Expected a physical id before seeing another core id");
1229 Val.getAsInteger(10, CurCoreId);
1230 }
1231 if (CurPhysicalId != -1 && CurCoreId != -1) {
1232 UniqueItems.insert(std::make_pair(CurPhysicalId, CurCoreId));
1233 CurPhysicalId = -1;
1234 CurCoreId = -1;
1235 }
1236 }
1237 return UniqueItems.size();
1238}
1239#elif defined(__APPLE__) && defined(__x86_64__)
1240#include <sys/param.h>
1241#include <sys/sysctl.h>
1242
1243// Gets the number of *physical cores* on the machine.
1244static int computeHostNumPhysicalCores() {
1245 uint32_t count;
1246 size_t len = sizeof(count);
1247 sysctlbyname("hw.physicalcpu", &count, &len, NULL, 0);
1248 if (count < 1) {
1249 int nm[2];
1250 nm[0] = CTL_HW;
1251 nm[1] = HW_AVAILCPU;
1252 sysctl(nm, 2, &count, &len, NULL, 0);
1253 if (count < 1)
1254 return -1;
1255 }
1256 return count;
1257}
1258#else
1259// On other systems, return -1 to indicate unknown.
1260static int computeHostNumPhysicalCores() { return -1; }
1261#endif
1262
1263int sys::getHostNumPhysicalCores() {
1264 static int NumCores = computeHostNumPhysicalCores();
1265 return NumCores;
1266}
1267
1268#if defined(__i386__) || defined(_M_IX86) || \
1269 defined(__x86_64__) || defined(_M_X64)
1270bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
1271 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
1272 unsigned MaxLevel;
1273 union {
1274 unsigned u[3];
1275 char c[12];
1276 } text;
1277
1278 if (getX86CpuIDAndInfo(0, &MaxLevel, text.u + 0, text.u + 2, text.u + 1) ||
1279 MaxLevel < 1)
1280 return false;
1281
1282 getX86CpuIDAndInfo(1, &EAX, &EBX, &ECX, &EDX);
1283
Craig Topper8d464032019-03-20 23:35:49 +00001284 Features["cx8"] = (EDX >> 8) & 1;
Craig Topper1af7e442017-11-19 23:30:22 +00001285 Features["cmov"] = (EDX >> 15) & 1;
1286 Features["mmx"] = (EDX >> 23) & 1;
Craig Topper6829ca92019-02-13 18:21:36 +00001287 Features["fxsr"] = (EDX >> 24) & 1;
Craig Topper1af7e442017-11-19 23:30:22 +00001288 Features["sse"] = (EDX >> 25) & 1;
1289 Features["sse2"] = (EDX >> 26) & 1;
1290
1291 Features["sse3"] = (ECX >> 0) & 1;
1292 Features["pclmul"] = (ECX >> 1) & 1;
1293 Features["ssse3"] = (ECX >> 9) & 1;
1294 Features["cx16"] = (ECX >> 13) & 1;
Simon Pilgrima271c542017-05-03 15:42:29 +00001295 Features["sse4.1"] = (ECX >> 19) & 1;
1296 Features["sse4.2"] = (ECX >> 20) & 1;
Craig Topper1af7e442017-11-19 23:30:22 +00001297 Features["movbe"] = (ECX >> 22) & 1;
Simon Pilgrima271c542017-05-03 15:42:29 +00001298 Features["popcnt"] = (ECX >> 23) & 1;
Craig Topper1af7e442017-11-19 23:30:22 +00001299 Features["aes"] = (ECX >> 25) & 1;
1300 Features["rdrnd"] = (ECX >> 30) & 1;
Simon Pilgrima271c542017-05-03 15:42:29 +00001301
1302 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
1303 // indicates that the AVX registers will be saved and restored on context
1304 // switch, then we have full AVX support.
1305 bool HasAVXSave = ((ECX >> 27) & 1) && ((ECX >> 28) & 1) &&
1306 !getX86XCR0(&EAX, &EDX) && ((EAX & 0x6) == 0x6);
Simon Pilgrima271c542017-05-03 15:42:29 +00001307 // AVX512 requires additional context to be saved by the OS.
1308 bool HasAVX512Save = HasAVXSave && ((EAX & 0xe0) == 0xe0);
1309
Craig Topper1af7e442017-11-19 23:30:22 +00001310 Features["avx"] = HasAVXSave;
1311 Features["fma"] = ((ECX >> 12) & 1) && HasAVXSave;
1312 // Only enable XSAVE if OS has enabled support for saving YMM state.
1313 Features["xsave"] = ((ECX >> 26) & 1) && HasAVXSave;
1314 Features["f16c"] = ((ECX >> 29) & 1) && HasAVXSave;
1315
Simon Pilgrima271c542017-05-03 15:42:29 +00001316 unsigned MaxExtLevel;
1317 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
1318
1319 bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
1320 !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
Craig Topper8d02be32018-02-17 16:52:49 +00001321 Features["sahf"] = HasExtLeaf1 && ((ECX >> 0) & 1);
Craig Topper1af7e442017-11-19 23:30:22 +00001322 Features["lzcnt"] = HasExtLeaf1 && ((ECX >> 5) & 1);
1323 Features["sse4a"] = HasExtLeaf1 && ((ECX >> 6) & 1);
1324 Features["prfchw"] = HasExtLeaf1 && ((ECX >> 8) & 1);
1325 Features["xop"] = HasExtLeaf1 && ((ECX >> 11) & 1) && HasAVXSave;
1326 Features["lwp"] = HasExtLeaf1 && ((ECX >> 15) & 1);
1327 Features["fma4"] = HasExtLeaf1 && ((ECX >> 16) & 1) && HasAVXSave;
1328 Features["tbm"] = HasExtLeaf1 && ((ECX >> 21) & 1);
Simon Pilgrima271c542017-05-03 15:42:29 +00001329 Features["mwaitx"] = HasExtLeaf1 && ((ECX >> 29) & 1);
1330
Craig Topper6cdab202018-09-24 18:55:41 +00001331 Features["64bit"] = HasExtLeaf1 && ((EDX >> 29) & 1);
1332
Gabor Buella2ef36f32018-04-11 20:01:57 +00001333 // Miscellaneous memory related features, detected by
1334 // using the 0x80000008 leaf of the CPUID instruction
Simon Pilgrima271c542017-05-03 15:42:29 +00001335 bool HasExtLeaf8 = MaxExtLevel >= 0x80000008 &&
Craig Topperdcd69792017-11-19 23:49:19 +00001336 !getX86CpuIDAndInfo(0x80000008, &EAX, &EBX, &ECX, &EDX);
Gabor Buella2ef36f32018-04-11 20:01:57 +00001337 Features["clzero"] = HasExtLeaf8 && ((EBX >> 0) & 1);
1338 Features["wbnoinvd"] = HasExtLeaf8 && ((EBX >> 9) & 1);
Simon Pilgrima271c542017-05-03 15:42:29 +00001339
1340 bool HasLeaf7 =
1341 MaxLevel >= 7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
1342
Craig Topper1af7e442017-11-19 23:30:22 +00001343 Features["fsgsbase"] = HasLeaf7 && ((EBX >> 0) & 1);
1344 Features["sgx"] = HasLeaf7 && ((EBX >> 2) & 1);
1345 Features["bmi"] = HasLeaf7 && ((EBX >> 3) & 1);
Simon Pilgrima271c542017-05-03 15:42:29 +00001346 // AVX2 is only supported if we have the OS save support from AVX.
Craig Topper1af7e442017-11-19 23:30:22 +00001347 Features["avx2"] = HasLeaf7 && ((EBX >> 5) & 1) && HasAVXSave;
1348 Features["bmi2"] = HasLeaf7 && ((EBX >> 8) & 1);
Gabor Buellad2f1ab12018-05-25 06:32:05 +00001349 Features["invpcid"] = HasLeaf7 && ((EBX >> 10) & 1);
Craig Topper1af7e442017-11-19 23:30:22 +00001350 Features["rtm"] = HasLeaf7 && ((EBX >> 11) & 1);
Craig Topperfa533f22019-02-13 20:12:41 +00001351 Features["mpx"] = HasLeaf7 && ((EBX >> 14) & 1);
Simon Pilgrima271c542017-05-03 15:42:29 +00001352 // AVX512 is only supported if the OS supports the context save for it.
Craig Topper1af7e442017-11-19 23:30:22 +00001353 Features["avx512f"] = HasLeaf7 && ((EBX >> 16) & 1) && HasAVX512Save;
1354 Features["avx512dq"] = HasLeaf7 && ((EBX >> 17) & 1) && HasAVX512Save;
1355 Features["rdseed"] = HasLeaf7 && ((EBX >> 18) & 1);
1356 Features["adx"] = HasLeaf7 && ((EBX >> 19) & 1);
Simon Pilgrima271c542017-05-03 15:42:29 +00001357 Features["avx512ifma"] = HasLeaf7 && ((EBX >> 21) & 1) && HasAVX512Save;
Craig Topper1af7e442017-11-19 23:30:22 +00001358 Features["clflushopt"] = HasLeaf7 && ((EBX >> 23) & 1);
1359 Features["clwb"] = HasLeaf7 && ((EBX >> 24) & 1);
1360 Features["avx512pf"] = HasLeaf7 && ((EBX >> 26) & 1) && HasAVX512Save;
1361 Features["avx512er"] = HasLeaf7 && ((EBX >> 27) & 1) && HasAVX512Save;
1362 Features["avx512cd"] = HasLeaf7 && ((EBX >> 28) & 1) && HasAVX512Save;
1363 Features["sha"] = HasLeaf7 && ((EBX >> 29) & 1);
1364 Features["avx512bw"] = HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save;
1365 Features["avx512vl"] = HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save;
Simon Pilgrima271c542017-05-03 15:42:29 +00001366
Craig Topper1af7e442017-11-19 23:30:22 +00001367 Features["prefetchwt1"] = HasLeaf7 && ((ECX >> 0) & 1);
1368 Features["avx512vbmi"] = HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save;
Craig Topper9b03f672017-11-21 18:50:41 +00001369 Features["pku"] = HasLeaf7 && ((ECX >> 4) & 1);
Gabor Buella31fa8022018-04-20 18:42:47 +00001370 Features["waitpkg"] = HasLeaf7 && ((ECX >> 5) & 1);
Coby Tayree71e37cc2017-11-21 09:48:44 +00001371 Features["avx512vbmi2"] = HasLeaf7 && ((ECX >> 6) & 1) && HasAVX512Save;
Oren Ben Simhonfa582b02017-11-26 13:02:45 +00001372 Features["shstk"] = HasLeaf7 && ((ECX >> 7) & 1);
Coby Tayreed8b17be2017-11-26 09:36:41 +00001373 Features["gfni"] = HasLeaf7 && ((ECX >> 8) & 1);
Craig Topper9b03f672017-11-21 18:50:41 +00001374 Features["vaes"] = HasLeaf7 && ((ECX >> 9) & 1) && HasAVXSave;
1375 Features["vpclmulqdq"] = HasLeaf7 && ((ECX >> 10) & 1) && HasAVXSave;
1376 Features["avx512vnni"] = HasLeaf7 && ((ECX >> 11) & 1) && HasAVX512Save;
1377 Features["avx512bitalg"] = HasLeaf7 && ((ECX >> 12) & 1) && HasAVX512Save;
Yonghong Songdc1dbf62017-08-23 04:25:57 +00001378 Features["avx512vpopcntdq"] = HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save;
Craig Topper84b26b92018-01-18 23:52:31 +00001379 Features["rdpid"] = HasLeaf7 && ((ECX >> 22) & 1);
Gabor Buella604be442018-04-13 07:35:08 +00001380 Features["cldemote"] = HasLeaf7 && ((ECX >> 25) & 1);
Gabor Buellac8ded042018-05-01 10:01:16 +00001381 Features["movdiri"] = HasLeaf7 && ((ECX >> 27) & 1);
1382 Features["movdir64b"] = HasLeaf7 && ((ECX >> 28) & 1);
Craig Topper84b26b92018-01-18 23:52:31 +00001383
Gabor Buella2b5e9602018-05-08 06:47:36 +00001384 // There are two CPUID leafs which information associated with the pconfig
1385 // instruction:
1386 // EAX=0x7, ECX=0x0 indicates the availability of the instruction (via the 18th
1387 // bit of EDX), while the EAX=0x1b leaf returns information on the
1388 // availability of specific pconfig leafs.
1389 // The target feature here only refers to the the first of these two.
1390 // Users might need to check for the availability of specific pconfig
1391 // leaves using cpuid, since that information is ignored while
1392 // detecting features using the "-march=native" flag.
1393 // For more info, see X86 ISA docs.
1394 Features["pconfig"] = HasLeaf7 && ((EDX >> 18) & 1);
Luo, Yuankebeec41c2019-05-06 08:22:37 +00001395 bool HasLeaf7Subleaf1 =
1396 MaxLevel >= 7 && !getX86CpuIDAndInfoEx(0x7, 0x1, &EAX, &EBX, &ECX, &EDX);
1397 Features["avx512bf16"] = HasLeaf7Subleaf1 && ((EAX >> 5) & 1) && HasAVX512Save;
Gabor Buella2b5e9602018-05-08 06:47:36 +00001398
Simon Pilgrima271c542017-05-03 15:42:29 +00001399 bool HasLeafD = MaxLevel >= 0xd &&
1400 !getX86CpuIDAndInfoEx(0xd, 0x1, &EAX, &EBX, &ECX, &EDX);
1401
1402 // Only enable XSAVE if OS has enabled support for saving YMM state.
Craig Topper1af7e442017-11-19 23:30:22 +00001403 Features["xsaveopt"] = HasLeafD && ((EAX >> 0) & 1) && HasAVXSave;
1404 Features["xsavec"] = HasLeafD && ((EAX >> 1) & 1) && HasAVXSave;
1405 Features["xsaves"] = HasLeafD && ((EAX >> 3) & 1) && HasAVXSave;
Simon Pilgrima271c542017-05-03 15:42:29 +00001406
Gabor Buellaa832b222018-05-10 07:26:05 +00001407 bool HasLeaf14 = MaxLevel >= 0x14 &&
1408 !getX86CpuIDAndInfoEx(0x14, 0x0, &EAX, &EBX, &ECX, &EDX);
1409
1410 Features["ptwrite"] = HasLeaf14 && ((EBX >> 4) & 1);
1411
Simon Pilgrima271c542017-05-03 15:42:29 +00001412 return true;
1413}
1414#elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1415bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
1416 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1417 if (!P)
1418 return false;
1419
1420 SmallVector<StringRef, 32> Lines;
1421 P->getBuffer().split(Lines, "\n");
1422
1423 SmallVector<StringRef, 32> CPUFeatures;
1424
1425 // Look for the CPU features.
1426 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
1427 if (Lines[I].startswith("Features")) {
1428 Lines[I].split(CPUFeatures, ' ');
1429 break;
1430 }
1431
1432#if defined(__aarch64__)
1433 // Keep track of which crypto features we have seen
1434 enum { CAP_AES = 0x1, CAP_PMULL = 0x2, CAP_SHA1 = 0x4, CAP_SHA2 = 0x8 };
1435 uint32_t crypto = 0;
1436#endif
1437
1438 for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) {
1439 StringRef LLVMFeatureStr = StringSwitch<StringRef>(CPUFeatures[I])
1440#if defined(__aarch64__)
1441 .Case("asimd", "neon")
1442 .Case("fp", "fp-armv8")
1443 .Case("crc32", "crc")
1444#else
1445 .Case("half", "fp16")
1446 .Case("neon", "neon")
1447 .Case("vfpv3", "vfp3")
1448 .Case("vfpv3d16", "d16")
1449 .Case("vfpv4", "vfp4")
1450 .Case("idiva", "hwdiv-arm")
1451 .Case("idivt", "hwdiv")
1452#endif
1453 .Default("");
1454
1455#if defined(__aarch64__)
1456 // We need to check crypto separately since we need all of the crypto
1457 // extensions to enable the subtarget feature
1458 if (CPUFeatures[I] == "aes")
1459 crypto |= CAP_AES;
1460 else if (CPUFeatures[I] == "pmull")
1461 crypto |= CAP_PMULL;
1462 else if (CPUFeatures[I] == "sha1")
1463 crypto |= CAP_SHA1;
1464 else if (CPUFeatures[I] == "sha2")
1465 crypto |= CAP_SHA2;
1466#endif
1467
1468 if (LLVMFeatureStr != "")
1469 Features[LLVMFeatureStr] = true;
1470 }
1471
1472#if defined(__aarch64__)
1473 // If we have all crypto bits we can add the feature
1474 if (crypto == (CAP_AES | CAP_PMULL | CAP_SHA1 | CAP_SHA2))
1475 Features["crypto"] = true;
1476#endif
1477
1478 return true;
1479}
1480#else
1481bool sys::getHostCPUFeatures(StringMap<bool> &Features) { return false; }
1482#endif
1483
1484std::string sys::getProcessTriple() {
Alex Lorenz3803df32017-07-07 09:53:47 +00001485 std::string TargetTripleString = updateTripleOSVersion(LLVM_HOST_TRIPLE);
1486 Triple PT(Triple::normalize(TargetTripleString));
Simon Pilgrima271c542017-05-03 15:42:29 +00001487
1488 if (sizeof(void *) == 8 && PT.isArch32Bit())
1489 PT = PT.get64BitArchVariant();
1490 if (sizeof(void *) == 4 && PT.isArch64Bit())
1491 PT = PT.get32BitArchVariant();
1492
1493 return PT.str();
1494}