Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1 | //===-- Host.cpp - Implement OS Host Concept --------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the operating system Host concept. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Support/Host.h" |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 15 | #include "llvm/Support/TargetParser.h" |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallSet.h" |
| 17 | #include "llvm/ADT/SmallVector.h" |
| 18 | #include "llvm/ADT/StringRef.h" |
| 19 | #include "llvm/ADT/StringSwitch.h" |
| 20 | #include "llvm/ADT/Triple.h" |
| 21 | #include "llvm/Config/config.h" |
| 22 | #include "llvm/Support/Debug.h" |
| 23 | #include "llvm/Support/FileSystem.h" |
| 24 | #include "llvm/Support/MemoryBuffer.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
| 26 | #include <assert.h> |
| 27 | #include <string.h> |
| 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 | |
| 54 | using namespace llvm; |
| 55 | |
| 56 | static std::unique_ptr<llvm::MemoryBuffer> |
| 57 | LLVM_ATTRIBUTE_UNUSED getProcCpuinfoContent() { |
| 58 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text = |
| 59 | llvm::MemoryBuffer::getFileAsStream("/proc/cpuinfo"); |
| 60 | if (std::error_code EC = Text.getError()) { |
| 61 | llvm::errs() << "Can't read " |
| 62 | << "/proc/cpuinfo: " << EC.message() << "\n"; |
| 63 | return nullptr; |
| 64 | } |
| 65 | return std::move(*Text); |
| 66 | } |
| 67 | |
Craig Topper | 8665f59 | 2018-03-07 17:53:16 +0000 | [diff] [blame] | 68 | StringRef sys::detail::getHostCPUNameForPowerPC(StringRef ProcCpuinfoContent) { |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 69 | // Access to the Processor Version Register (PVR) on PowerPC is privileged, |
| 70 | // and so we must use an operating-system interface to determine the current |
| 71 | // processor type. On Linux, this is exposed through the /proc/cpuinfo file. |
| 72 | const char *generic = "generic"; |
| 73 | |
| 74 | // The cpu line is second (after the 'processor: 0' line), so if this |
| 75 | // buffer is too small then something has changed (or is wrong). |
| 76 | StringRef::const_iterator CPUInfoStart = ProcCpuinfoContent.begin(); |
| 77 | StringRef::const_iterator CPUInfoEnd = ProcCpuinfoContent.end(); |
| 78 | |
| 79 | StringRef::const_iterator CIP = CPUInfoStart; |
| 80 | |
| 81 | StringRef::const_iterator CPUStart = 0; |
| 82 | size_t CPULen = 0; |
| 83 | |
| 84 | // We need to find the first line which starts with cpu, spaces, and a colon. |
| 85 | // After the colon, there may be some additional spaces and then the cpu type. |
| 86 | while (CIP < CPUInfoEnd && CPUStart == 0) { |
| 87 | if (CIP < CPUInfoEnd && *CIP == '\n') |
| 88 | ++CIP; |
| 89 | |
| 90 | if (CIP < CPUInfoEnd && *CIP == 'c') { |
| 91 | ++CIP; |
| 92 | if (CIP < CPUInfoEnd && *CIP == 'p') { |
| 93 | ++CIP; |
| 94 | if (CIP < CPUInfoEnd && *CIP == 'u') { |
| 95 | ++CIP; |
| 96 | while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t')) |
| 97 | ++CIP; |
| 98 | |
| 99 | if (CIP < CPUInfoEnd && *CIP == ':') { |
| 100 | ++CIP; |
| 101 | while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t')) |
| 102 | ++CIP; |
| 103 | |
| 104 | if (CIP < CPUInfoEnd) { |
| 105 | CPUStart = CIP; |
| 106 | while (CIP < CPUInfoEnd && (*CIP != ' ' && *CIP != '\t' && |
| 107 | *CIP != ',' && *CIP != '\n')) |
| 108 | ++CIP; |
| 109 | CPULen = CIP - CPUStart; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | if (CPUStart == 0) |
| 117 | while (CIP < CPUInfoEnd && *CIP != '\n') |
| 118 | ++CIP; |
| 119 | } |
| 120 | |
| 121 | if (CPUStart == 0) |
| 122 | return generic; |
| 123 | |
| 124 | return StringSwitch<const char *>(StringRef(CPUStart, CPULen)) |
| 125 | .Case("604e", "604e") |
| 126 | .Case("604", "604") |
| 127 | .Case("7400", "7400") |
| 128 | .Case("7410", "7400") |
| 129 | .Case("7447", "7400") |
| 130 | .Case("7455", "7450") |
| 131 | .Case("G4", "g4") |
| 132 | .Case("POWER4", "970") |
| 133 | .Case("PPC970FX", "970") |
| 134 | .Case("PPC970MP", "970") |
| 135 | .Case("G5", "g5") |
| 136 | .Case("POWER5", "g5") |
| 137 | .Case("A2", "a2") |
| 138 | .Case("POWER6", "pwr6") |
| 139 | .Case("POWER7", "pwr7") |
| 140 | .Case("POWER8", "pwr8") |
| 141 | .Case("POWER8E", "pwr8") |
| 142 | .Case("POWER8NVL", "pwr8") |
| 143 | .Case("POWER9", "pwr9") |
| 144 | .Default(generic); |
| 145 | } |
| 146 | |
Craig Topper | 8665f59 | 2018-03-07 17:53:16 +0000 | [diff] [blame] | 147 | StringRef sys::detail::getHostCPUNameForARM(StringRef ProcCpuinfoContent) { |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 148 | // The cpuid register on arm is not accessible from user space. On Linux, |
| 149 | // it is exposed through the /proc/cpuinfo file. |
| 150 | |
| 151 | // Read 32 lines from /proc/cpuinfo, which should contain the CPU part line |
| 152 | // in all cases. |
| 153 | SmallVector<StringRef, 32> Lines; |
| 154 | ProcCpuinfoContent.split(Lines, "\n"); |
| 155 | |
| 156 | // Look for the CPU implementer line. |
| 157 | StringRef Implementer; |
| 158 | StringRef Hardware; |
| 159 | for (unsigned I = 0, E = Lines.size(); I != E; ++I) { |
| 160 | if (Lines[I].startswith("CPU implementer")) |
| 161 | Implementer = Lines[I].substr(15).ltrim("\t :"); |
| 162 | if (Lines[I].startswith("Hardware")) |
| 163 | Hardware = Lines[I].substr(8).ltrim("\t :"); |
| 164 | } |
| 165 | |
| 166 | if (Implementer == "0x41") { // ARM Ltd. |
| 167 | // MSM8992/8994 may give cpu part for the core that the kernel is running on, |
| 168 | // which is undeterministic and wrong. Always return cortex-a53 for these SoC. |
| 169 | if (Hardware.endswith("MSM8994") || Hardware.endswith("MSM8996")) |
| 170 | return "cortex-a53"; |
| 171 | |
| 172 | |
| 173 | // Look for the CPU part line. |
| 174 | for (unsigned I = 0, E = Lines.size(); I != E; ++I) |
| 175 | if (Lines[I].startswith("CPU part")) |
| 176 | // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The |
| 177 | // values correspond to the "Part number" in the CP15/c0 register. The |
| 178 | // contents are specified in the various processor manuals. |
| 179 | return StringSwitch<const char *>(Lines[I].substr(8).ltrim("\t :")) |
| 180 | .Case("0x926", "arm926ej-s") |
| 181 | .Case("0xb02", "mpcore") |
| 182 | .Case("0xb36", "arm1136j-s") |
| 183 | .Case("0xb56", "arm1156t2-s") |
| 184 | .Case("0xb76", "arm1176jz-s") |
| 185 | .Case("0xc08", "cortex-a8") |
| 186 | .Case("0xc09", "cortex-a9") |
| 187 | .Case("0xc0f", "cortex-a15") |
| 188 | .Case("0xc20", "cortex-m0") |
| 189 | .Case("0xc23", "cortex-m3") |
| 190 | .Case("0xc24", "cortex-m4") |
| 191 | .Case("0xd04", "cortex-a35") |
| 192 | .Case("0xd03", "cortex-a53") |
| 193 | .Case("0xd07", "cortex-a57") |
| 194 | .Case("0xd08", "cortex-a72") |
| 195 | .Case("0xd09", "cortex-a73") |
| 196 | .Default("generic"); |
| 197 | } |
| 198 | |
| 199 | if (Implementer == "0x51") // Qualcomm Technologies, Inc. |
| 200 | // Look for the CPU part line. |
| 201 | for (unsigned I = 0, E = Lines.size(); I != E; ++I) |
| 202 | if (Lines[I].startswith("CPU part")) |
| 203 | // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The |
| 204 | // values correspond to the "Part number" in the CP15/c0 register. The |
| 205 | // contents are specified in the various processor manuals. |
| 206 | return StringSwitch<const char *>(Lines[I].substr(8).ltrim("\t :")) |
| 207 | .Case("0x06f", "krait") // APQ8064 |
| 208 | .Case("0x201", "kryo") |
| 209 | .Case("0x205", "kryo") |
Eli Friedman | bde9fc7 | 2017-09-13 21:48:00 +0000 | [diff] [blame] | 210 | .Case("0x211", "kryo") |
| 211 | .Case("0x800", "cortex-a73") |
| 212 | .Case("0x801", "cortex-a73") |
Balaram Makam | a1e7ecc7 | 2017-09-22 17:46:36 +0000 | [diff] [blame] | 213 | .Case("0xc00", "falkor") |
Chad Rosier | 7107085 | 2017-09-25 14:05:00 +0000 | [diff] [blame] | 214 | .Case("0xc01", "saphira") |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 215 | .Default("generic"); |
| 216 | |
Evandro Menezes | 5d7a9e6 | 2017-12-08 21:09:59 +0000 | [diff] [blame] | 217 | if (Implementer == "0x53") { // Samsung Electronics Co., Ltd. |
| 218 | // The Exynos chips have a convoluted ID scheme that doesn't seem to follow |
| 219 | // any predictive pattern across variants and parts. |
| 220 | unsigned Variant = 0, Part = 0; |
| 221 | |
| 222 | // Look for the CPU variant line, whose value is a 1 digit hexadecimal |
| 223 | // number, corresponding to the Variant bits in the CP15/C0 register. |
| 224 | for (auto I : Lines) |
| 225 | if (I.consume_front("CPU variant")) |
| 226 | I.ltrim("\t :").getAsInteger(0, Variant); |
| 227 | |
| 228 | // Look for the CPU part line, whose value is a 3 digit hexadecimal |
| 229 | // number, corresponding to the PartNum bits in the CP15/C0 register. |
| 230 | for (auto I : Lines) |
| 231 | if (I.consume_front("CPU part")) |
| 232 | I.ltrim("\t :").getAsInteger(0, Part); |
| 233 | |
| 234 | unsigned Exynos = (Variant << 12) | Part; |
| 235 | switch (Exynos) { |
| 236 | default: |
| 237 | // Default by falling through to Exynos M1. |
| 238 | LLVM_FALLTHROUGH; |
| 239 | |
| 240 | case 0x1001: |
| 241 | return "exynos-m1"; |
| 242 | |
| 243 | case 0x4001: |
| 244 | return "exynos-m2"; |
| 245 | } |
| 246 | } |
| 247 | |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 248 | return "generic"; |
| 249 | } |
| 250 | |
Craig Topper | 8665f59 | 2018-03-07 17:53:16 +0000 | [diff] [blame] | 251 | StringRef sys::detail::getHostCPUNameForS390x(StringRef ProcCpuinfoContent) { |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 252 | // STIDP is a privileged operation, so use /proc/cpuinfo instead. |
| 253 | |
| 254 | // The "processor 0:" line comes after a fair amount of other information, |
| 255 | // including a cache breakdown, but this should be plenty. |
| 256 | SmallVector<StringRef, 32> Lines; |
| 257 | ProcCpuinfoContent.split(Lines, "\n"); |
| 258 | |
| 259 | // Look for the CPU features. |
| 260 | SmallVector<StringRef, 32> CPUFeatures; |
| 261 | for (unsigned I = 0, E = Lines.size(); I != E; ++I) |
| 262 | if (Lines[I].startswith("features")) { |
| 263 | size_t Pos = Lines[I].find(":"); |
| 264 | if (Pos != StringRef::npos) { |
| 265 | Lines[I].drop_front(Pos + 1).split(CPUFeatures, ' '); |
| 266 | break; |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // We need to check for the presence of vector support independently of |
| 271 | // the machine type, since we may only use the vector register set when |
| 272 | // supported by the kernel (and hypervisor). |
| 273 | bool HaveVectorSupport = false; |
| 274 | for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) { |
| 275 | if (CPUFeatures[I] == "vx") |
| 276 | HaveVectorSupport = true; |
| 277 | } |
| 278 | |
| 279 | // Now check the processor machine type. |
| 280 | for (unsigned I = 0, E = Lines.size(); I != E; ++I) { |
| 281 | if (Lines[I].startswith("processor ")) { |
| 282 | size_t Pos = Lines[I].find("machine = "); |
| 283 | if (Pos != StringRef::npos) { |
| 284 | Pos += sizeof("machine = ") - 1; |
| 285 | unsigned int Id; |
| 286 | if (!Lines[I].drop_front(Pos).getAsInteger(10, Id)) { |
Ulrich Weigand | 2b3482f | 2017-07-17 17:41:11 +0000 | [diff] [blame] | 287 | if (Id >= 3906 && HaveVectorSupport) |
| 288 | return "z14"; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 289 | if (Id >= 2964 && HaveVectorSupport) |
| 290 | return "z13"; |
| 291 | if (Id >= 2827) |
| 292 | return "zEC12"; |
| 293 | if (Id >= 2817) |
| 294 | return "z196"; |
| 295 | } |
| 296 | } |
| 297 | break; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | return "generic"; |
| 302 | } |
| 303 | |
Yonghong Song | dc1dbf6 | 2017-08-23 04:25:57 +0000 | [diff] [blame] | 304 | StringRef sys::detail::getHostCPUNameForBPF() { |
| 305 | #if !defined(__linux__) || !defined(__x86_64__) |
| 306 | return "generic"; |
| 307 | #else |
| 308 | uint8_t insns[40] __attribute__ ((aligned (8))) = |
| 309 | /* BPF_MOV64_IMM(BPF_REG_0, 0) */ |
| 310 | { 0xb7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 311 | /* BPF_MOV64_IMM(BPF_REG_2, 1) */ |
| 312 | 0xb7, 0x2, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, |
| 313 | /* BPF_JMP_REG(BPF_JLT, BPF_REG_0, BPF_REG_2, 1) */ |
| 314 | 0xad, 0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 315 | /* BPF_MOV64_IMM(BPF_REG_0, 1) */ |
| 316 | 0xb7, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, |
| 317 | /* BPF_EXIT_INSN() */ |
| 318 | 0x95, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }; |
| 319 | |
| 320 | struct bpf_prog_load_attr { |
| 321 | uint32_t prog_type; |
| 322 | uint32_t insn_cnt; |
| 323 | uint64_t insns; |
| 324 | uint64_t license; |
| 325 | uint32_t log_level; |
| 326 | uint32_t log_size; |
| 327 | uint64_t log_buf; |
| 328 | uint32_t kern_version; |
| 329 | uint32_t prog_flags; |
| 330 | } attr = {}; |
| 331 | attr.prog_type = 1; /* BPF_PROG_TYPE_SOCKET_FILTER */ |
| 332 | attr.insn_cnt = 5; |
| 333 | attr.insns = (uint64_t)insns; |
| 334 | attr.license = (uint64_t)"DUMMY"; |
| 335 | |
| 336 | int fd = syscall(321 /* __NR_bpf */, 5 /* BPF_PROG_LOAD */, &attr, sizeof(attr)); |
Yonghong Song | c6d2571 | 2017-08-23 16:24:31 +0000 | [diff] [blame] | 337 | if (fd >= 0) { |
| 338 | close(fd); |
| 339 | return "v2"; |
| 340 | } |
| 341 | return "v1"; |
Yonghong Song | dc1dbf6 | 2017-08-23 04:25:57 +0000 | [diff] [blame] | 342 | #endif |
| 343 | } |
| 344 | |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 345 | #if defined(__i386__) || defined(_M_IX86) || \ |
| 346 | defined(__x86_64__) || defined(_M_X64) |
| 347 | |
| 348 | enum VendorSignatures { |
| 349 | SIG_INTEL = 0x756e6547 /* Genu */, |
| 350 | SIG_AMD = 0x68747541 /* Auth */ |
| 351 | }; |
| 352 | |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 353 | // The check below for i386 was copied from clang's cpuid.h (__get_cpuid_max). |
| 354 | // Check motivated by bug reports for OpenSSL crashing on CPUs without CPUID |
| 355 | // support. Consequently, for i386, the presence of CPUID is checked first |
| 356 | // via the corresponding eflags bit. |
| 357 | // Removal of cpuid.h header motivated by PR30384 |
| 358 | // Header cpuid.h and method __get_cpuid_max are not used in llvm, clang, openmp |
| 359 | // or test-suite, but are used in external projects e.g. libstdcxx |
| 360 | static bool isCpuIdSupported() { |
| 361 | #if defined(__GNUC__) || defined(__clang__) |
| 362 | #if defined(__i386__) |
| 363 | int __cpuid_supported; |
| 364 | __asm__(" pushfl\n" |
| 365 | " popl %%eax\n" |
| 366 | " movl %%eax,%%ecx\n" |
| 367 | " xorl $0x00200000,%%eax\n" |
| 368 | " pushl %%eax\n" |
| 369 | " popfl\n" |
| 370 | " pushfl\n" |
| 371 | " popl %%eax\n" |
| 372 | " movl $0,%0\n" |
| 373 | " cmpl %%eax,%%ecx\n" |
| 374 | " je 1f\n" |
| 375 | " movl $1,%0\n" |
| 376 | "1:" |
| 377 | : "=r"(__cpuid_supported) |
| 378 | : |
| 379 | : "eax", "ecx"); |
| 380 | if (!__cpuid_supported) |
| 381 | return false; |
| 382 | #endif |
| 383 | return true; |
| 384 | #endif |
| 385 | return true; |
| 386 | } |
| 387 | |
| 388 | /// getX86CpuIDAndInfo - Execute the specified cpuid and return the 4 values in |
| 389 | /// the specified arguments. If we can't run cpuid on the host, return true. |
| 390 | static bool getX86CpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX, |
| 391 | unsigned *rECX, unsigned *rEDX) { |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 392 | #if defined(__GNUC__) || defined(__clang__) |
| 393 | #if defined(__x86_64__) |
| 394 | // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually. |
| 395 | // FIXME: should we save this for Clang? |
| 396 | __asm__("movq\t%%rbx, %%rsi\n\t" |
| 397 | "cpuid\n\t" |
| 398 | "xchgq\t%%rbx, %%rsi\n\t" |
| 399 | : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX) |
| 400 | : "a"(value)); |
Craig Topper | 1efd10a | 2017-07-10 06:04:11 +0000 | [diff] [blame] | 401 | return false; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 402 | #elif defined(__i386__) |
| 403 | __asm__("movl\t%%ebx, %%esi\n\t" |
| 404 | "cpuid\n\t" |
| 405 | "xchgl\t%%ebx, %%esi\n\t" |
| 406 | : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX) |
| 407 | : "a"(value)); |
Craig Topper | 1efd10a | 2017-07-10 06:04:11 +0000 | [diff] [blame] | 408 | return false; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 409 | #else |
Craig Topper | 1efd10a | 2017-07-10 06:04:11 +0000 | [diff] [blame] | 410 | return true; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 411 | #endif |
| 412 | #elif defined(_MSC_VER) |
| 413 | // The MSVC intrinsic is portable across x86 and x64. |
| 414 | int registers[4]; |
| 415 | __cpuid(registers, value); |
| 416 | *rEAX = registers[0]; |
| 417 | *rEBX = registers[1]; |
| 418 | *rECX = registers[2]; |
| 419 | *rEDX = registers[3]; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 420 | return false; |
| 421 | #else |
| 422 | return true; |
| 423 | #endif |
| 424 | } |
| 425 | |
| 426 | /// getX86CpuIDAndInfoEx - Execute the specified cpuid with subleaf and return |
| 427 | /// the 4 values in the specified arguments. If we can't run cpuid on the host, |
| 428 | /// return true. |
| 429 | static bool getX86CpuIDAndInfoEx(unsigned value, unsigned subleaf, |
| 430 | unsigned *rEAX, unsigned *rEBX, unsigned *rECX, |
| 431 | unsigned *rEDX) { |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 432 | #if defined(__GNUC__) || defined(__clang__) |
Craig Topper | 828cf30 | 2017-07-17 05:16:16 +0000 | [diff] [blame] | 433 | #if defined(__x86_64__) |
Craig Topper | ada983a | 2017-07-10 06:09:22 +0000 | [diff] [blame] | 434 | // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually. |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 435 | // FIXME: should we save this for Clang? |
| 436 | __asm__("movq\t%%rbx, %%rsi\n\t" |
| 437 | "cpuid\n\t" |
| 438 | "xchgq\t%%rbx, %%rsi\n\t" |
| 439 | : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX) |
| 440 | : "a"(value), "c"(subleaf)); |
Craig Topper | 1efd10a | 2017-07-10 06:04:11 +0000 | [diff] [blame] | 441 | return false; |
Craig Topper | 828cf30 | 2017-07-17 05:16:16 +0000 | [diff] [blame] | 442 | #elif defined(__i386__) |
| 443 | __asm__("movl\t%%ebx, %%esi\n\t" |
| 444 | "cpuid\n\t" |
| 445 | "xchgl\t%%ebx, %%esi\n\t" |
| 446 | : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX) |
| 447 | : "a"(value), "c"(subleaf)); |
| 448 | return false; |
| 449 | #else |
| 450 | return true; |
| 451 | #endif |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 452 | #elif defined(_MSC_VER) |
| 453 | int registers[4]; |
| 454 | __cpuidex(registers, value, subleaf); |
| 455 | *rEAX = registers[0]; |
| 456 | *rEBX = registers[1]; |
| 457 | *rECX = registers[2]; |
| 458 | *rEDX = registers[3]; |
Craig Topper | 1efd10a | 2017-07-10 06:04:11 +0000 | [diff] [blame] | 459 | return false; |
| 460 | #else |
| 461 | return true; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 462 | #endif |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Craig Topper | f3af64e | 2017-07-12 06:49:57 +0000 | [diff] [blame] | 465 | // Read control register 0 (XCR0). Used to detect features such as AVX. |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 466 | static bool getX86XCR0(unsigned *rEAX, unsigned *rEDX) { |
| 467 | #if defined(__GNUC__) || defined(__clang__) |
| 468 | // Check xgetbv; this uses a .byte sequence instead of the instruction |
| 469 | // directly because older assemblers do not include support for xgetbv and |
| 470 | // there is no easy way to conditionally compile based on the assembler used. |
| 471 | __asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(*rEAX), "=d"(*rEDX) : "c"(0)); |
| 472 | return false; |
| 473 | #elif defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK) |
| 474 | unsigned long long Result = _xgetbv(_XCR_XFEATURE_ENABLED_MASK); |
| 475 | *rEAX = Result; |
| 476 | *rEDX = Result >> 32; |
| 477 | return false; |
| 478 | #else |
| 479 | return true; |
| 480 | #endif |
| 481 | } |
| 482 | |
| 483 | static void detectX86FamilyModel(unsigned EAX, unsigned *Family, |
| 484 | unsigned *Model) { |
| 485 | *Family = (EAX >> 8) & 0xf; // Bits 8 - 11 |
| 486 | *Model = (EAX >> 4) & 0xf; // Bits 4 - 7 |
| 487 | if (*Family == 6 || *Family == 0xf) { |
| 488 | if (*Family == 0xf) |
| 489 | // Examine extended family ID if family ID is F. |
| 490 | *Family += (EAX >> 20) & 0xff; // Bits 20 - 27 |
| 491 | // Examine extended model ID if family ID is 6 or F. |
| 492 | *Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19 |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | static void |
Craig Topper | c6bbe4b | 2017-07-08 05:16:14 +0000 | [diff] [blame] | 497 | getIntelProcessorTypeAndSubtype(unsigned Family, unsigned Model, |
| 498 | unsigned Brand_id, unsigned Features, |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 499 | unsigned Features2, unsigned *Type, |
| 500 | unsigned *Subtype) { |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 501 | if (Brand_id != 0) |
| 502 | return; |
| 503 | switch (Family) { |
| 504 | case 3: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 505 | *Type = X86::INTEL_i386; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 506 | break; |
| 507 | case 4: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 508 | *Type = X86::INTEL_i486; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 509 | break; |
| 510 | case 5: |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 511 | if (Features & (1 << X86::FEATURE_MMX)) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 512 | *Type = X86::INTEL_PENTIUM_MMX; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 513 | break; |
| 514 | } |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 515 | *Type = X86::INTEL_PENTIUM; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 516 | break; |
| 517 | case 6: |
| 518 | switch (Model) { |
| 519 | case 0x01: // Pentium Pro processor |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 520 | *Type = X86::INTEL_PENTIUM_PRO; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 521 | break; |
| 522 | case 0x03: // Intel Pentium II OverDrive processor, Pentium II processor, |
| 523 | // model 03 |
| 524 | case 0x05: // Pentium II processor, model 05, Pentium II Xeon processor, |
| 525 | // model 05, and Intel Celeron processor, model 05 |
| 526 | case 0x06: // Celeron processor, model 06 |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 527 | *Type = X86::INTEL_PENTIUM_II; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 528 | break; |
| 529 | case 0x07: // Pentium III processor, model 07, and Pentium III Xeon |
| 530 | // processor, model 07 |
| 531 | case 0x08: // Pentium III processor, model 08, Pentium III Xeon processor, |
| 532 | // model 08, and Celeron processor, model 08 |
| 533 | case 0x0a: // Pentium III Xeon processor, model 0Ah |
| 534 | case 0x0b: // Pentium III processor, model 0Bh |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 535 | *Type = X86::INTEL_PENTIUM_III; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 536 | break; |
| 537 | case 0x09: // Intel Pentium M processor, Intel Celeron M processor model 09. |
| 538 | case 0x0d: // Intel Pentium M processor, Intel Celeron M processor, model |
| 539 | // 0Dh. All processors are manufactured using the 90 nm process. |
| 540 | case 0x15: // Intel EP80579 Integrated Processor and Intel EP80579 |
| 541 | // Integrated Processor with Intel QuickAssist Technology |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 542 | *Type = X86::INTEL_PENTIUM_M; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 543 | break; |
| 544 | case 0x0e: // Intel Core Duo processor, Intel Core Solo processor, model |
| 545 | // 0Eh. All processors are manufactured using the 65 nm process. |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 546 | *Type = X86::INTEL_CORE_DUO; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 547 | break; // yonah |
| 548 | case 0x0f: // Intel Core 2 Duo processor, Intel Core 2 Duo mobile |
| 549 | // processor, Intel Core 2 Quad processor, Intel Core 2 Quad |
| 550 | // mobile processor, Intel Core 2 Extreme processor, Intel |
| 551 | // Pentium Dual-Core processor, Intel Xeon processor, model |
| 552 | // 0Fh. All processors are manufactured using the 65 nm process. |
| 553 | case 0x16: // Intel Celeron processor model 16h. All processors are |
| 554 | // manufactured using the 65 nm process |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 555 | *Type = X86::INTEL_CORE2; // "core2" |
| 556 | *Subtype = X86::INTEL_CORE2_65; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 557 | break; |
| 558 | case 0x17: // Intel Core 2 Extreme processor, Intel Xeon processor, model |
| 559 | // 17h. All processors are manufactured using the 45 nm process. |
| 560 | // |
| 561 | // 45nm: Penryn , Wolfdale, Yorkfield (XE) |
| 562 | case 0x1d: // Intel Xeon processor MP. All processors are manufactured using |
| 563 | // the 45 nm process. |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 564 | *Type = X86::INTEL_CORE2; // "penryn" |
| 565 | *Subtype = X86::INTEL_CORE2_45; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 566 | break; |
| 567 | case 0x1a: // Intel Core i7 processor and Intel Xeon processor. All |
| 568 | // processors are manufactured using the 45 nm process. |
| 569 | case 0x1e: // Intel(R) Core(TM) i7 CPU 870 @ 2.93GHz. |
| 570 | // As found in a Summer 2010 model iMac. |
| 571 | case 0x1f: |
| 572 | case 0x2e: // Nehalem EX |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 573 | *Type = X86::INTEL_COREI7; // "nehalem" |
| 574 | *Subtype = X86::INTEL_COREI7_NEHALEM; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 575 | break; |
| 576 | case 0x25: // Intel Core i7, laptop version. |
| 577 | case 0x2c: // Intel Core i7 processor and Intel Xeon processor. All |
| 578 | // processors are manufactured using the 32 nm process. |
| 579 | case 0x2f: // Westmere EX |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 580 | *Type = X86::INTEL_COREI7; // "westmere" |
| 581 | *Subtype = X86::INTEL_COREI7_WESTMERE; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 582 | break; |
| 583 | case 0x2a: // Intel Core i7 processor. All processors are manufactured |
| 584 | // using the 32 nm process. |
| 585 | case 0x2d: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 586 | *Type = X86::INTEL_COREI7; //"sandybridge" |
| 587 | *Subtype = X86::INTEL_COREI7_SANDYBRIDGE; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 588 | break; |
| 589 | case 0x3a: |
| 590 | case 0x3e: // Ivy Bridge EP |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 591 | *Type = X86::INTEL_COREI7; // "ivybridge" |
| 592 | *Subtype = X86::INTEL_COREI7_IVYBRIDGE; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 593 | break; |
| 594 | |
| 595 | // Haswell: |
| 596 | case 0x3c: |
| 597 | case 0x3f: |
| 598 | case 0x45: |
| 599 | case 0x46: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 600 | *Type = X86::INTEL_COREI7; // "haswell" |
| 601 | *Subtype = X86::INTEL_COREI7_HASWELL; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 602 | break; |
| 603 | |
| 604 | // Broadwell: |
| 605 | case 0x3d: |
| 606 | case 0x47: |
| 607 | case 0x4f: |
| 608 | case 0x56: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 609 | *Type = X86::INTEL_COREI7; // "broadwell" |
| 610 | *Subtype = X86::INTEL_COREI7_BROADWELL; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 611 | break; |
| 612 | |
| 613 | // Skylake: |
| 614 | case 0x4e: // Skylake mobile |
| 615 | case 0x5e: // Skylake desktop |
| 616 | case 0x8e: // Kaby Lake mobile |
| 617 | case 0x9e: // Kaby Lake desktop |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 618 | *Type = X86::INTEL_COREI7; // "skylake" |
| 619 | *Subtype = X86::INTEL_COREI7_SKYLAKE; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 620 | break; |
| 621 | |
| 622 | // Skylake Xeon: |
| 623 | case 0x55: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 624 | *Type = X86::INTEL_COREI7; |
| 625 | *Subtype = X86::INTEL_COREI7_SKYLAKE_AVX512; // "skylake-avx512" |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 626 | break; |
| 627 | |
Craig Topper | 0749186 | 2017-11-15 06:02:42 +0000 | [diff] [blame] | 628 | // Cannonlake: |
| 629 | case 0x66: |
| 630 | *Type = X86::INTEL_COREI7; |
| 631 | *Subtype = X86::INTEL_COREI7_CANNONLAKE; // "cannonlake" |
| 632 | break; |
| 633 | |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 634 | case 0x1c: // Most 45 nm Intel Atom processors |
| 635 | case 0x26: // 45 nm Atom Lincroft |
| 636 | case 0x27: // 32 nm Atom Medfield |
| 637 | case 0x35: // 32 nm Atom Midview |
| 638 | case 0x36: // 32 nm Atom Midview |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 639 | *Type = X86::INTEL_BONNELL; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 640 | break; // "bonnell" |
| 641 | |
| 642 | // Atom Silvermont codes from the Intel software optimization guide. |
| 643 | case 0x37: |
| 644 | case 0x4a: |
| 645 | case 0x4d: |
| 646 | case 0x5a: |
| 647 | case 0x5d: |
| 648 | case 0x4c: // really airmont |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 649 | *Type = X86::INTEL_SILVERMONT; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 650 | break; // "silvermont" |
Michael Zuckerman | 4bcb9c3 | 2017-06-29 10:00:33 +0000 | [diff] [blame] | 651 | // Goldmont: |
Craig Topper | 0dadfe3 | 2017-11-15 06:02:43 +0000 | [diff] [blame] | 652 | case 0x5c: // Apollo Lake |
| 653 | case 0x5f: // Denverton |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 654 | *Type = X86::INTEL_GOLDMONT; |
Michael Zuckerman | 4bcb9c3 | 2017-06-29 10:00:33 +0000 | [diff] [blame] | 655 | break; // "goldmont" |
Gabor Buella | 8f1646b | 2018-04-16 07:47:35 +0000 | [diff] [blame] | 656 | case 0x7a: |
| 657 | *Type = X86::INTEL_GOLDMONT_PLUS; |
| 658 | break; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 659 | case 0x57: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 660 | *Type = X86::INTEL_KNL; // knl |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 661 | break; |
Craig Topper | 5d69291 | 2017-10-13 18:10:17 +0000 | [diff] [blame] | 662 | case 0x85: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 663 | *Type = X86::INTEL_KNM; // knm |
Craig Topper | 5d69291 | 2017-10-13 18:10:17 +0000 | [diff] [blame] | 664 | break; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 665 | |
| 666 | default: // Unknown family 6 CPU, try to guess. |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 667 | if (Features & (1 << X86::FEATURE_AVX512VBMI)) { |
Craig Topper | 0749186 | 2017-11-15 06:02:42 +0000 | [diff] [blame] | 668 | *Type = X86::INTEL_COREI7; |
| 669 | *Subtype = X86::INTEL_COREI7_CANNONLAKE; |
Craig Topper | 4eda756 | 2017-07-27 03:26:52 +0000 | [diff] [blame] | 670 | break; |
| 671 | } |
Craig Topper | 0749186 | 2017-11-15 06:02:42 +0000 | [diff] [blame] | 672 | |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 673 | if (Features & (1 << X86::FEATURE_AVX512VL)) { |
Craig Topper | 0749186 | 2017-11-15 06:02:42 +0000 | [diff] [blame] | 674 | *Type = X86::INTEL_COREI7; |
| 675 | *Subtype = X86::INTEL_COREI7_SKYLAKE_AVX512; |
| 676 | break; |
| 677 | } |
| 678 | |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 679 | if (Features & (1 << X86::FEATURE_AVX512ER)) { |
Craig Topper | 0749186 | 2017-11-15 06:02:42 +0000 | [diff] [blame] | 680 | *Type = X86::INTEL_KNL; // knl |
| 681 | break; |
| 682 | } |
| 683 | |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 684 | if (Features2 & (1 << (X86::FEATURE_CLFLUSHOPT - 32))) { |
| 685 | if (Features2 & (1 << (X86::FEATURE_SHA - 32))) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 686 | *Type = X86::INTEL_GOLDMONT; |
Craig Topper | 4eda756 | 2017-07-27 03:26:52 +0000 | [diff] [blame] | 687 | } else { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 688 | *Type = X86::INTEL_COREI7; |
| 689 | *Subtype = X86::INTEL_COREI7_SKYLAKE; |
Craig Topper | 4eda756 | 2017-07-27 03:26:52 +0000 | [diff] [blame] | 690 | } |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 691 | break; |
| 692 | } |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 693 | if (Features2 & (1 << (X86::FEATURE_ADX - 32))) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 694 | *Type = X86::INTEL_COREI7; |
| 695 | *Subtype = X86::INTEL_COREI7_BROADWELL; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 696 | break; |
| 697 | } |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 698 | if (Features & (1 << X86::FEATURE_AVX2)) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 699 | *Type = X86::INTEL_COREI7; |
| 700 | *Subtype = X86::INTEL_COREI7_HASWELL; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 701 | break; |
| 702 | } |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 703 | if (Features & (1 << X86::FEATURE_AVX)) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 704 | *Type = X86::INTEL_COREI7; |
| 705 | *Subtype = X86::INTEL_COREI7_SANDYBRIDGE; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 706 | break; |
| 707 | } |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 708 | if (Features & (1 << X86::FEATURE_SSE4_2)) { |
| 709 | if (Features2 & (1 << (X86::FEATURE_MOVBE - 32))) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 710 | *Type = X86::INTEL_SILVERMONT; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 711 | } else { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 712 | *Type = X86::INTEL_COREI7; |
| 713 | *Subtype = X86::INTEL_COREI7_NEHALEM; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 714 | } |
| 715 | break; |
| 716 | } |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 717 | if (Features & (1 << X86::FEATURE_SSE4_1)) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 718 | *Type = X86::INTEL_CORE2; // "penryn" |
| 719 | *Subtype = X86::INTEL_CORE2_45; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 720 | break; |
| 721 | } |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 722 | if (Features & (1 << X86::FEATURE_SSSE3)) { |
| 723 | if (Features2 & (1 << (X86::FEATURE_MOVBE - 32))) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 724 | *Type = X86::INTEL_BONNELL; // "bonnell" |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 725 | } else { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 726 | *Type = X86::INTEL_CORE2; // "core2" |
| 727 | *Subtype = X86::INTEL_CORE2_65; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 728 | } |
| 729 | break; |
| 730 | } |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 731 | if (Features2 & (1 << (X86::FEATURE_EM64T - 32))) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 732 | *Type = X86::INTEL_CORE2; // "core2" |
| 733 | *Subtype = X86::INTEL_CORE2_65; |
Craig Topper | a233e16 | 2017-11-02 19:13:32 +0000 | [diff] [blame] | 734 | break; |
| 735 | } |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 736 | if (Features & (1 << X86::FEATURE_SSE3)) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 737 | *Type = X86::INTEL_CORE_DUO; |
Craig Topper | a233e16 | 2017-11-02 19:13:32 +0000 | [diff] [blame] | 738 | break; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 739 | } |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 740 | if (Features & (1 << X86::FEATURE_SSE2)) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 741 | *Type = X86::INTEL_PENTIUM_M; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 742 | break; |
| 743 | } |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 744 | if (Features & (1 << X86::FEATURE_SSE)) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 745 | *Type = X86::INTEL_PENTIUM_III; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 746 | break; |
| 747 | } |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 748 | if (Features & (1 << X86::FEATURE_MMX)) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 749 | *Type = X86::INTEL_PENTIUM_II; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 750 | break; |
| 751 | } |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 752 | *Type = X86::INTEL_PENTIUM_PRO; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 753 | break; |
| 754 | } |
| 755 | break; |
| 756 | case 15: { |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 757 | if (Features2 & (1 << (X86::FEATURE_EM64T - 32))) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 758 | *Type = X86::INTEL_NOCONA; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 759 | break; |
| 760 | } |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 761 | if (Features & (1 << X86::FEATURE_SSE3)) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 762 | *Type = X86::INTEL_PRESCOTT; |
Craig Topper | 1494915 | 2017-11-02 19:13:34 +0000 | [diff] [blame] | 763 | break; |
| 764 | } |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 765 | *Type = X86::INTEL_PENTIUM_IV; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 766 | break; |
| 767 | } |
| 768 | default: |
| 769 | break; /*"generic"*/ |
| 770 | } |
| 771 | } |
| 772 | |
Craig Topper | 2ace153 | 2017-07-08 06:44:34 +0000 | [diff] [blame] | 773 | static void getAMDProcessorTypeAndSubtype(unsigned Family, unsigned Model, |
| 774 | unsigned Features, unsigned *Type, |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 775 | unsigned *Subtype) { |
| 776 | // FIXME: this poorly matches the generated SubtargetFeatureKV table. There |
| 777 | // appears to be no way to generate the wide variety of AMD-specific targets |
| 778 | // from the information returned from CPUID. |
| 779 | switch (Family) { |
| 780 | case 4: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 781 | *Type = X86::AMD_i486; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 782 | break; |
| 783 | case 5: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 784 | *Type = X86::AMDPENTIUM; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 785 | switch (Model) { |
| 786 | case 6: |
| 787 | case 7: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 788 | *Subtype = X86::AMDPENTIUM_K6; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 789 | break; // "k6" |
| 790 | case 8: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 791 | *Subtype = X86::AMDPENTIUM_K62; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 792 | break; // "k6-2" |
| 793 | case 9: |
| 794 | case 13: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 795 | *Subtype = X86::AMDPENTIUM_K63; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 796 | break; // "k6-3" |
| 797 | case 10: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 798 | *Subtype = X86::AMDPENTIUM_GEODE; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 799 | break; // "geode" |
| 800 | } |
| 801 | break; |
| 802 | case 6: |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 803 | if (Features & (1 << X86::FEATURE_SSE)) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 804 | *Type = X86::AMD_ATHLON_XP; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 805 | break; // "athlon-xp" |
| 806 | } |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 807 | *Type = X86::AMD_ATHLON; |
Craig Topper | f3de5eb | 2017-07-13 06:34:10 +0000 | [diff] [blame] | 808 | break; // "athlon" |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 809 | case 15: |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 810 | if (Features & (1 << X86::FEATURE_SSE3)) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 811 | *Type = X86::AMD_K8SSE3; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 812 | break; // "k8-sse3" |
| 813 | } |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 814 | *Type = X86::AMD_K8; |
Craig Topper | f3de5eb | 2017-07-13 06:34:10 +0000 | [diff] [blame] | 815 | break; // "k8" |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 816 | case 16: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 817 | *Type = X86::AMDFAM10H; // "amdfam10" |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 818 | switch (Model) { |
| 819 | case 2: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 820 | *Subtype = X86::AMDFAM10H_BARCELONA; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 821 | break; |
| 822 | case 4: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 823 | *Subtype = X86::AMDFAM10H_SHANGHAI; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 824 | break; |
| 825 | case 8: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 826 | *Subtype = X86::AMDFAM10H_ISTANBUL; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 827 | break; |
| 828 | } |
| 829 | break; |
| 830 | case 20: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 831 | *Type = X86::AMD_BTVER1; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 832 | break; // "btver1"; |
| 833 | case 21: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 834 | *Type = X86::AMDFAM15H; |
Craig Topper | 1f9d3c0 | 2017-07-08 06:44:35 +0000 | [diff] [blame] | 835 | if (Model >= 0x60 && Model <= 0x7f) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 836 | *Subtype = X86::AMDFAM15H_BDVER4; |
Craig Topper | 3db1170 | 2017-07-12 06:49:56 +0000 | [diff] [blame] | 837 | break; // "bdver4"; 60h-7Fh: Excavator |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 838 | } |
| 839 | if (Model >= 0x30 && Model <= 0x3f) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 840 | *Subtype = X86::AMDFAM15H_BDVER3; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 841 | break; // "bdver3"; 30h-3Fh: Steamroller |
| 842 | } |
| 843 | if (Model >= 0x10 && Model <= 0x1f) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 844 | *Subtype = X86::AMDFAM15H_BDVER2; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 845 | break; // "bdver2"; 10h-1Fh: Piledriver |
| 846 | } |
| 847 | if (Model <= 0x0f) { |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 848 | *Subtype = X86::AMDFAM15H_BDVER1; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 849 | break; // "bdver1"; 00h-0Fh: Bulldozer |
| 850 | } |
| 851 | break; |
| 852 | case 22: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 853 | *Type = X86::AMD_BTVER2; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 854 | break; // "btver2" |
| 855 | case 23: |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 856 | *Type = X86::AMDFAM17H; |
| 857 | *Subtype = X86::AMDFAM17H_ZNVER1; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 858 | break; |
| 859 | default: |
| 860 | break; // "generic" |
| 861 | } |
| 862 | } |
| 863 | |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 864 | static void getAvailableFeatures(unsigned ECX, unsigned EDX, unsigned MaxLeaf, |
| 865 | unsigned *FeaturesOut, |
| 866 | unsigned *Features2Out) { |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 867 | unsigned Features = 0; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 868 | unsigned Features2 = 0; |
Craig Topper | c6bbe4b | 2017-07-08 05:16:14 +0000 | [diff] [blame] | 869 | unsigned EAX, EBX; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 870 | |
| 871 | if ((EDX >> 15) & 1) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 872 | Features |= 1 << X86::FEATURE_CMOV; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 873 | if ((EDX >> 23) & 1) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 874 | Features |= 1 << X86::FEATURE_MMX; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 875 | if ((EDX >> 25) & 1) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 876 | Features |= 1 << X86::FEATURE_SSE; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 877 | if ((EDX >> 26) & 1) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 878 | Features |= 1 << X86::FEATURE_SSE2; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 879 | |
| 880 | if ((ECX >> 0) & 1) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 881 | Features |= 1 << X86::FEATURE_SSE3; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 882 | if ((ECX >> 1) & 1) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 883 | Features |= 1 << X86::FEATURE_PCLMUL; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 884 | if ((ECX >> 9) & 1) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 885 | Features |= 1 << X86::FEATURE_SSSE3; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 886 | if ((ECX >> 12) & 1) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 887 | Features |= 1 << X86::FEATURE_FMA; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 888 | if ((ECX >> 19) & 1) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 889 | Features |= 1 << X86::FEATURE_SSE4_1; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 890 | if ((ECX >> 20) & 1) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 891 | Features |= 1 << X86::FEATURE_SSE4_2; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 892 | if ((ECX >> 23) & 1) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 893 | Features |= 1 << X86::FEATURE_POPCNT; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 894 | if ((ECX >> 25) & 1) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 895 | Features |= 1 << X86::FEATURE_AES; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 896 | |
| 897 | if ((ECX >> 22) & 1) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 898 | Features2 |= 1 << (X86::FEATURE_MOVBE - 32); |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 899 | |
| 900 | // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV |
| 901 | // indicates that the AVX registers will be saved and restored on context |
| 902 | // switch, then we have full AVX support. |
| 903 | const unsigned AVXBits = (1 << 27) | (1 << 28); |
| 904 | bool HasAVX = ((ECX & AVXBits) == AVXBits) && !getX86XCR0(&EAX, &EDX) && |
| 905 | ((EAX & 0x6) == 0x6); |
| 906 | bool HasAVX512Save = HasAVX && ((EAX & 0xe0) == 0xe0); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 907 | |
| 908 | if (HasAVX) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 909 | Features |= 1 << X86::FEATURE_AVX; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 910 | |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 911 | bool HasLeaf7 = |
| 912 | MaxLeaf >= 0x7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 913 | |
| 914 | if (HasLeaf7 && ((EBX >> 3) & 1)) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 915 | Features |= 1 << X86::FEATURE_BMI; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 916 | if (HasLeaf7 && ((EBX >> 5) & 1) && HasAVX) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 917 | Features |= 1 << X86::FEATURE_AVX2; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 918 | if (HasLeaf7 && ((EBX >> 9) & 1)) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 919 | Features |= 1 << X86::FEATURE_BMI2; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 920 | if (HasLeaf7 && ((EBX >> 16) & 1) && HasAVX512Save) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 921 | Features |= 1 << X86::FEATURE_AVX512F; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 922 | if (HasLeaf7 && ((EBX >> 17) & 1) && HasAVX512Save) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 923 | Features |= 1 << X86::FEATURE_AVX512DQ; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 924 | if (HasLeaf7 && ((EBX >> 19) & 1)) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 925 | Features2 |= 1 << (X86::FEATURE_ADX - 32); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 926 | if (HasLeaf7 && ((EBX >> 21) & 1) && HasAVX512Save) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 927 | Features |= 1 << X86::FEATURE_AVX512IFMA; |
Craig Topper | 4eda756 | 2017-07-27 03:26:52 +0000 | [diff] [blame] | 928 | if (HasLeaf7 && ((EBX >> 23) & 1)) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 929 | Features2 |= 1 << (X86::FEATURE_CLFLUSHOPT - 32); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 930 | if (HasLeaf7 && ((EBX >> 26) & 1) && HasAVX512Save) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 931 | Features |= 1 << X86::FEATURE_AVX512PF; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 932 | if (HasLeaf7 && ((EBX >> 27) & 1) && HasAVX512Save) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 933 | Features |= 1 << X86::FEATURE_AVX512ER; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 934 | if (HasLeaf7 && ((EBX >> 28) & 1) && HasAVX512Save) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 935 | Features |= 1 << X86::FEATURE_AVX512CD; |
Craig Topper | 4eda756 | 2017-07-27 03:26:52 +0000 | [diff] [blame] | 936 | if (HasLeaf7 && ((EBX >> 29) & 1)) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 937 | Features2 |= 1 << (X86::FEATURE_SHA - 32); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 938 | if (HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 939 | Features |= 1 << X86::FEATURE_AVX512BW; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 940 | if (HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 941 | Features |= 1 << X86::FEATURE_AVX512VL; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 942 | |
| 943 | if (HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 944 | Features |= 1 << X86::FEATURE_AVX512VBMI; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 945 | if (HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 946 | Features |= 1 << X86::FEATURE_AVX512VPOPCNTDQ; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 947 | |
| 948 | if (HasLeaf7 && ((EDX >> 2) & 1) && HasAVX512Save) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 949 | Features |= 1 << X86::FEATURE_AVX5124VNNIW; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 950 | if (HasLeaf7 && ((EDX >> 3) & 1) && HasAVX512Save) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 951 | Features |= 1 << X86::FEATURE_AVX5124FMAPS; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 952 | |
Craig Topper | bb8c799 | 2017-07-08 05:16:13 +0000 | [diff] [blame] | 953 | unsigned MaxExtLevel; |
| 954 | getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX); |
| 955 | |
| 956 | bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 && |
| 957 | !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 958 | if (HasExtLeaf1 && ((ECX >> 6) & 1)) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 959 | Features |= 1 << X86::FEATURE_SSE4_A; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 960 | if (HasExtLeaf1 && ((ECX >> 11) & 1)) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 961 | Features |= 1 << X86::FEATURE_XOP; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 962 | if (HasExtLeaf1 && ((ECX >> 16) & 1)) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 963 | Features |= 1 << X86::FEATURE_FMA4; |
Craig Topper | bb8c799 | 2017-07-08 05:16:13 +0000 | [diff] [blame] | 964 | |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 965 | if (HasExtLeaf1 && ((EDX >> 29) & 1)) |
Craig Topper | 47c8739 | 2017-11-21 23:36:42 +0000 | [diff] [blame] | 966 | Features2 |= 1 << (X86::FEATURE_EM64T - 32); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 967 | |
| 968 | *FeaturesOut = Features; |
| 969 | *Features2Out = Features2; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | StringRef sys::getHostCPUName() { |
| 973 | unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0; |
| 974 | unsigned MaxLeaf, Vendor; |
| 975 | |
| 976 | #if defined(__GNUC__) || defined(__clang__) |
| 977 | //FIXME: include cpuid.h from clang or copy __get_cpuid_max here |
| 978 | // and simplify it to not invoke __cpuid (like cpu_model.c in |
| 979 | // compiler-rt/lib/builtins/cpu_model.c? |
| 980 | // Opting for the second option. |
| 981 | if(!isCpuIdSupported()) |
| 982 | return "generic"; |
| 983 | #endif |
Craig Topper | bb8c799 | 2017-07-08 05:16:13 +0000 | [diff] [blame] | 984 | if (getX86CpuIDAndInfo(0, &MaxLeaf, &Vendor, &ECX, &EDX) || MaxLeaf < 1) |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 985 | return "generic"; |
Craig Topper | bb8c799 | 2017-07-08 05:16:13 +0000 | [diff] [blame] | 986 | getX86CpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX); |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 987 | |
| 988 | unsigned Brand_id = EBX & 0xff; |
| 989 | unsigned Family = 0, Model = 0; |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 990 | unsigned Features = 0, Features2 = 0; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 991 | detectX86FamilyModel(EAX, &Family, &Model); |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 992 | getAvailableFeatures(ECX, EDX, MaxLeaf, &Features, &Features2); |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 993 | |
Craig Topper | 741e7e6 | 2017-11-03 18:02:44 +0000 | [diff] [blame] | 994 | unsigned Type = 0; |
| 995 | unsigned Subtype = 0; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 996 | |
| 997 | if (Vendor == SIG_INTEL) { |
Craig Topper | 3a5d082 | 2017-07-12 06:49:58 +0000 | [diff] [blame] | 998 | getIntelProcessorTypeAndSubtype(Family, Model, Brand_id, Features, |
| 999 | Features2, &Type, &Subtype); |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1000 | } else if (Vendor == SIG_AMD) { |
| 1001 | getAMDProcessorTypeAndSubtype(Family, Model, Features, &Type, &Subtype); |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1002 | } |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 1003 | |
| 1004 | // Check subtypes first since those are more specific. |
| 1005 | #define X86_CPU_SUBTYPE(ARCHNAME, ENUM) \ |
| 1006 | if (Subtype == X86::ENUM) \ |
| 1007 | return ARCHNAME; |
| 1008 | #include "llvm/Support/X86TargetParser.def" |
| 1009 | |
| 1010 | // Now check types. |
Craig Topper | 55ad329 | 2018-03-06 22:45:31 +0000 | [diff] [blame] | 1011 | #define X86_CPU_TYPE(ARCHNAME, ENUM) \ |
Craig Topper | c77d00e | 2017-11-10 17:10:57 +0000 | [diff] [blame] | 1012 | if (Type == X86::ENUM) \ |
| 1013 | return ARCHNAME; |
| 1014 | #include "llvm/Support/X86TargetParser.def" |
| 1015 | |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1016 | return "generic"; |
| 1017 | } |
| 1018 | |
| 1019 | #elif defined(__APPLE__) && (defined(__ppc__) || defined(__powerpc__)) |
| 1020 | StringRef sys::getHostCPUName() { |
| 1021 | host_basic_info_data_t hostInfo; |
| 1022 | mach_msg_type_number_t infoCount; |
| 1023 | |
| 1024 | infoCount = HOST_BASIC_INFO_COUNT; |
| 1025 | host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo, |
| 1026 | &infoCount); |
| 1027 | |
| 1028 | if (hostInfo.cpu_type != CPU_TYPE_POWERPC) |
| 1029 | return "generic"; |
| 1030 | |
| 1031 | switch (hostInfo.cpu_subtype) { |
| 1032 | case CPU_SUBTYPE_POWERPC_601: |
| 1033 | return "601"; |
| 1034 | case CPU_SUBTYPE_POWERPC_602: |
| 1035 | return "602"; |
| 1036 | case CPU_SUBTYPE_POWERPC_603: |
| 1037 | return "603"; |
| 1038 | case CPU_SUBTYPE_POWERPC_603e: |
| 1039 | return "603e"; |
| 1040 | case CPU_SUBTYPE_POWERPC_603ev: |
| 1041 | return "603ev"; |
| 1042 | case CPU_SUBTYPE_POWERPC_604: |
| 1043 | return "604"; |
| 1044 | case CPU_SUBTYPE_POWERPC_604e: |
| 1045 | return "604e"; |
| 1046 | case CPU_SUBTYPE_POWERPC_620: |
| 1047 | return "620"; |
| 1048 | case CPU_SUBTYPE_POWERPC_750: |
| 1049 | return "750"; |
| 1050 | case CPU_SUBTYPE_POWERPC_7400: |
| 1051 | return "7400"; |
| 1052 | case CPU_SUBTYPE_POWERPC_7450: |
| 1053 | return "7450"; |
| 1054 | case CPU_SUBTYPE_POWERPC_970: |
| 1055 | return "970"; |
| 1056 | default:; |
| 1057 | } |
| 1058 | |
| 1059 | return "generic"; |
| 1060 | } |
| 1061 | #elif defined(__linux__) && (defined(__ppc__) || defined(__powerpc__)) |
| 1062 | StringRef sys::getHostCPUName() { |
| 1063 | std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent(); |
Craig Topper | 8665f59 | 2018-03-07 17:53:16 +0000 | [diff] [blame] | 1064 | StringRef Content = P ? P->getBuffer() : ""; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1065 | return detail::getHostCPUNameForPowerPC(Content); |
| 1066 | } |
| 1067 | #elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__)) |
| 1068 | StringRef sys::getHostCPUName() { |
| 1069 | std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent(); |
Craig Topper | 8665f59 | 2018-03-07 17:53:16 +0000 | [diff] [blame] | 1070 | StringRef Content = P ? P->getBuffer() : ""; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1071 | return detail::getHostCPUNameForARM(Content); |
| 1072 | } |
| 1073 | #elif defined(__linux__) && defined(__s390x__) |
| 1074 | StringRef sys::getHostCPUName() { |
| 1075 | std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent(); |
Craig Topper | 8665f59 | 2018-03-07 17:53:16 +0000 | [diff] [blame] | 1076 | StringRef Content = P ? P->getBuffer() : ""; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1077 | return detail::getHostCPUNameForS390x(Content); |
| 1078 | } |
| 1079 | #else |
| 1080 | StringRef sys::getHostCPUName() { return "generic"; } |
| 1081 | #endif |
| 1082 | |
| 1083 | #if defined(__linux__) && defined(__x86_64__) |
| 1084 | // On Linux, the number of physical cores can be computed from /proc/cpuinfo, |
| 1085 | // using the number of unique physical/core id pairs. The following |
| 1086 | // implementation reads the /proc/cpuinfo format on an x86_64 system. |
| 1087 | static int computeHostNumPhysicalCores() { |
| 1088 | // Read /proc/cpuinfo as a stream (until EOF reached). It cannot be |
| 1089 | // mmapped because it appears to have 0 size. |
| 1090 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text = |
| 1091 | llvm::MemoryBuffer::getFileAsStream("/proc/cpuinfo"); |
| 1092 | if (std::error_code EC = Text.getError()) { |
| 1093 | llvm::errs() << "Can't read " |
| 1094 | << "/proc/cpuinfo: " << EC.message() << "\n"; |
| 1095 | return -1; |
| 1096 | } |
| 1097 | SmallVector<StringRef, 8> strs; |
| 1098 | (*Text)->getBuffer().split(strs, "\n", /*MaxSplit=*/-1, |
| 1099 | /*KeepEmpty=*/false); |
| 1100 | int CurPhysicalId = -1; |
| 1101 | int CurCoreId = -1; |
| 1102 | SmallSet<std::pair<int, int>, 32> UniqueItems; |
| 1103 | for (auto &Line : strs) { |
| 1104 | Line = Line.trim(); |
| 1105 | if (!Line.startswith("physical id") && !Line.startswith("core id")) |
| 1106 | continue; |
| 1107 | std::pair<StringRef, StringRef> Data = Line.split(':'); |
| 1108 | auto Name = Data.first.trim(); |
| 1109 | auto Val = Data.second.trim(); |
| 1110 | if (Name == "physical id") { |
| 1111 | assert(CurPhysicalId == -1 && |
| 1112 | "Expected a core id before seeing another physical id"); |
| 1113 | Val.getAsInteger(10, CurPhysicalId); |
| 1114 | } |
| 1115 | if (Name == "core id") { |
| 1116 | assert(CurCoreId == -1 && |
| 1117 | "Expected a physical id before seeing another core id"); |
| 1118 | Val.getAsInteger(10, CurCoreId); |
| 1119 | } |
| 1120 | if (CurPhysicalId != -1 && CurCoreId != -1) { |
| 1121 | UniqueItems.insert(std::make_pair(CurPhysicalId, CurCoreId)); |
| 1122 | CurPhysicalId = -1; |
| 1123 | CurCoreId = -1; |
| 1124 | } |
| 1125 | } |
| 1126 | return UniqueItems.size(); |
| 1127 | } |
| 1128 | #elif defined(__APPLE__) && defined(__x86_64__) |
| 1129 | #include <sys/param.h> |
| 1130 | #include <sys/sysctl.h> |
| 1131 | |
| 1132 | // Gets the number of *physical cores* on the machine. |
| 1133 | static int computeHostNumPhysicalCores() { |
| 1134 | uint32_t count; |
| 1135 | size_t len = sizeof(count); |
| 1136 | sysctlbyname("hw.physicalcpu", &count, &len, NULL, 0); |
| 1137 | if (count < 1) { |
| 1138 | int nm[2]; |
| 1139 | nm[0] = CTL_HW; |
| 1140 | nm[1] = HW_AVAILCPU; |
| 1141 | sysctl(nm, 2, &count, &len, NULL, 0); |
| 1142 | if (count < 1) |
| 1143 | return -1; |
| 1144 | } |
| 1145 | return count; |
| 1146 | } |
| 1147 | #else |
| 1148 | // On other systems, return -1 to indicate unknown. |
| 1149 | static int computeHostNumPhysicalCores() { return -1; } |
| 1150 | #endif |
| 1151 | |
| 1152 | int sys::getHostNumPhysicalCores() { |
| 1153 | static int NumCores = computeHostNumPhysicalCores(); |
| 1154 | return NumCores; |
| 1155 | } |
| 1156 | |
| 1157 | #if defined(__i386__) || defined(_M_IX86) || \ |
| 1158 | defined(__x86_64__) || defined(_M_X64) |
| 1159 | bool sys::getHostCPUFeatures(StringMap<bool> &Features) { |
| 1160 | unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0; |
| 1161 | unsigned MaxLevel; |
| 1162 | union { |
| 1163 | unsigned u[3]; |
| 1164 | char c[12]; |
| 1165 | } text; |
| 1166 | |
| 1167 | if (getX86CpuIDAndInfo(0, &MaxLevel, text.u + 0, text.u + 2, text.u + 1) || |
| 1168 | MaxLevel < 1) |
| 1169 | return false; |
| 1170 | |
| 1171 | getX86CpuIDAndInfo(1, &EAX, &EBX, &ECX, &EDX); |
| 1172 | |
Craig Topper | 1af7e44 | 2017-11-19 23:30:22 +0000 | [diff] [blame] | 1173 | Features["cmov"] = (EDX >> 15) & 1; |
| 1174 | Features["mmx"] = (EDX >> 23) & 1; |
| 1175 | Features["sse"] = (EDX >> 25) & 1; |
| 1176 | Features["sse2"] = (EDX >> 26) & 1; |
| 1177 | |
| 1178 | Features["sse3"] = (ECX >> 0) & 1; |
| 1179 | Features["pclmul"] = (ECX >> 1) & 1; |
| 1180 | Features["ssse3"] = (ECX >> 9) & 1; |
| 1181 | Features["cx16"] = (ECX >> 13) & 1; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1182 | Features["sse4.1"] = (ECX >> 19) & 1; |
| 1183 | Features["sse4.2"] = (ECX >> 20) & 1; |
Craig Topper | 1af7e44 | 2017-11-19 23:30:22 +0000 | [diff] [blame] | 1184 | Features["movbe"] = (ECX >> 22) & 1; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1185 | Features["popcnt"] = (ECX >> 23) & 1; |
Craig Topper | 1af7e44 | 2017-11-19 23:30:22 +0000 | [diff] [blame] | 1186 | Features["aes"] = (ECX >> 25) & 1; |
| 1187 | Features["rdrnd"] = (ECX >> 30) & 1; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1188 | |
| 1189 | // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV |
| 1190 | // indicates that the AVX registers will be saved and restored on context |
| 1191 | // switch, then we have full AVX support. |
| 1192 | bool HasAVXSave = ((ECX >> 27) & 1) && ((ECX >> 28) & 1) && |
| 1193 | !getX86XCR0(&EAX, &EDX) && ((EAX & 0x6) == 0x6); |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1194 | // AVX512 requires additional context to be saved by the OS. |
| 1195 | bool HasAVX512Save = HasAVXSave && ((EAX & 0xe0) == 0xe0); |
| 1196 | |
Craig Topper | 1af7e44 | 2017-11-19 23:30:22 +0000 | [diff] [blame] | 1197 | Features["avx"] = HasAVXSave; |
| 1198 | Features["fma"] = ((ECX >> 12) & 1) && HasAVXSave; |
| 1199 | // Only enable XSAVE if OS has enabled support for saving YMM state. |
| 1200 | Features["xsave"] = ((ECX >> 26) & 1) && HasAVXSave; |
| 1201 | Features["f16c"] = ((ECX >> 29) & 1) && HasAVXSave; |
| 1202 | |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1203 | unsigned MaxExtLevel; |
| 1204 | getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX); |
| 1205 | |
| 1206 | bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 && |
| 1207 | !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX); |
Craig Topper | 8d02be3 | 2018-02-17 16:52:49 +0000 | [diff] [blame] | 1208 | Features["sahf"] = HasExtLeaf1 && ((ECX >> 0) & 1); |
Craig Topper | 1af7e44 | 2017-11-19 23:30:22 +0000 | [diff] [blame] | 1209 | Features["lzcnt"] = HasExtLeaf1 && ((ECX >> 5) & 1); |
| 1210 | Features["sse4a"] = HasExtLeaf1 && ((ECX >> 6) & 1); |
| 1211 | Features["prfchw"] = HasExtLeaf1 && ((ECX >> 8) & 1); |
| 1212 | Features["xop"] = HasExtLeaf1 && ((ECX >> 11) & 1) && HasAVXSave; |
| 1213 | Features["lwp"] = HasExtLeaf1 && ((ECX >> 15) & 1); |
| 1214 | Features["fma4"] = HasExtLeaf1 && ((ECX >> 16) & 1) && HasAVXSave; |
| 1215 | Features["tbm"] = HasExtLeaf1 && ((ECX >> 21) & 1); |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1216 | Features["mwaitx"] = HasExtLeaf1 && ((ECX >> 29) & 1); |
| 1217 | |
Gabor Buella | 2ef36f3 | 2018-04-11 20:01:57 +0000 | [diff] [blame] | 1218 | // Miscellaneous memory related features, detected by |
| 1219 | // using the 0x80000008 leaf of the CPUID instruction |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1220 | bool HasExtLeaf8 = MaxExtLevel >= 0x80000008 && |
Craig Topper | dcd6979 | 2017-11-19 23:49:19 +0000 | [diff] [blame] | 1221 | !getX86CpuIDAndInfo(0x80000008, &EAX, &EBX, &ECX, &EDX); |
Gabor Buella | 2ef36f3 | 2018-04-11 20:01:57 +0000 | [diff] [blame] | 1222 | Features["clzero"] = HasExtLeaf8 && ((EBX >> 0) & 1); |
| 1223 | Features["wbnoinvd"] = HasExtLeaf8 && ((EBX >> 9) & 1); |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1224 | |
| 1225 | bool HasLeaf7 = |
| 1226 | MaxLevel >= 7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX); |
| 1227 | |
Craig Topper | 1af7e44 | 2017-11-19 23:30:22 +0000 | [diff] [blame] | 1228 | Features["fsgsbase"] = HasLeaf7 && ((EBX >> 0) & 1); |
| 1229 | Features["sgx"] = HasLeaf7 && ((EBX >> 2) & 1); |
| 1230 | Features["bmi"] = HasLeaf7 && ((EBX >> 3) & 1); |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1231 | // AVX2 is only supported if we have the OS save support from AVX. |
Craig Topper | 1af7e44 | 2017-11-19 23:30:22 +0000 | [diff] [blame] | 1232 | Features["avx2"] = HasLeaf7 && ((EBX >> 5) & 1) && HasAVXSave; |
| 1233 | Features["bmi2"] = HasLeaf7 && ((EBX >> 8) & 1); |
| 1234 | Features["rtm"] = HasLeaf7 && ((EBX >> 11) & 1); |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1235 | // AVX512 is only supported if the OS supports the context save for it. |
Craig Topper | 1af7e44 | 2017-11-19 23:30:22 +0000 | [diff] [blame] | 1236 | Features["avx512f"] = HasLeaf7 && ((EBX >> 16) & 1) && HasAVX512Save; |
| 1237 | Features["avx512dq"] = HasLeaf7 && ((EBX >> 17) & 1) && HasAVX512Save; |
| 1238 | Features["rdseed"] = HasLeaf7 && ((EBX >> 18) & 1); |
| 1239 | Features["adx"] = HasLeaf7 && ((EBX >> 19) & 1); |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1240 | Features["avx512ifma"] = HasLeaf7 && ((EBX >> 21) & 1) && HasAVX512Save; |
Craig Topper | 1af7e44 | 2017-11-19 23:30:22 +0000 | [diff] [blame] | 1241 | Features["clflushopt"] = HasLeaf7 && ((EBX >> 23) & 1); |
| 1242 | Features["clwb"] = HasLeaf7 && ((EBX >> 24) & 1); |
| 1243 | Features["avx512pf"] = HasLeaf7 && ((EBX >> 26) & 1) && HasAVX512Save; |
| 1244 | Features["avx512er"] = HasLeaf7 && ((EBX >> 27) & 1) && HasAVX512Save; |
| 1245 | Features["avx512cd"] = HasLeaf7 && ((EBX >> 28) & 1) && HasAVX512Save; |
| 1246 | Features["sha"] = HasLeaf7 && ((EBX >> 29) & 1); |
| 1247 | Features["avx512bw"] = HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save; |
| 1248 | Features["avx512vl"] = HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1249 | |
Craig Topper | 1af7e44 | 2017-11-19 23:30:22 +0000 | [diff] [blame] | 1250 | Features["prefetchwt1"] = HasLeaf7 && ((ECX >> 0) & 1); |
| 1251 | Features["avx512vbmi"] = HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save; |
Craig Topper | 9b03f67 | 2017-11-21 18:50:41 +0000 | [diff] [blame] | 1252 | Features["pku"] = HasLeaf7 && ((ECX >> 4) & 1); |
Gabor Buella | 31fa802 | 2018-04-20 18:42:47 +0000 | [diff] [blame] | 1253 | Features["waitpkg"] = HasLeaf7 && ((ECX >> 5) & 1); |
Coby Tayree | 71e37cc | 2017-11-21 09:48:44 +0000 | [diff] [blame] | 1254 | Features["avx512vbmi2"] = HasLeaf7 && ((ECX >> 6) & 1) && HasAVX512Save; |
Oren Ben Simhon | fa582b0 | 2017-11-26 13:02:45 +0000 | [diff] [blame] | 1255 | Features["shstk"] = HasLeaf7 && ((ECX >> 7) & 1); |
Coby Tayree | d8b17be | 2017-11-26 09:36:41 +0000 | [diff] [blame] | 1256 | Features["gfni"] = HasLeaf7 && ((ECX >> 8) & 1); |
Craig Topper | 9b03f67 | 2017-11-21 18:50:41 +0000 | [diff] [blame] | 1257 | Features["vaes"] = HasLeaf7 && ((ECX >> 9) & 1) && HasAVXSave; |
| 1258 | Features["vpclmulqdq"] = HasLeaf7 && ((ECX >> 10) & 1) && HasAVXSave; |
| 1259 | Features["avx512vnni"] = HasLeaf7 && ((ECX >> 11) & 1) && HasAVX512Save; |
| 1260 | Features["avx512bitalg"] = HasLeaf7 && ((ECX >> 12) & 1) && HasAVX512Save; |
Yonghong Song | dc1dbf6 | 2017-08-23 04:25:57 +0000 | [diff] [blame] | 1261 | Features["avx512vpopcntdq"] = HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save; |
Craig Topper | 84b26b9 | 2018-01-18 23:52:31 +0000 | [diff] [blame] | 1262 | Features["rdpid"] = HasLeaf7 && ((ECX >> 22) & 1); |
Gabor Buella | 604be44 | 2018-04-13 07:35:08 +0000 | [diff] [blame] | 1263 | Features["cldemote"] = HasLeaf7 && ((ECX >> 25) & 1); |
Craig Topper | 84b26b9 | 2018-01-18 23:52:31 +0000 | [diff] [blame] | 1264 | |
| 1265 | Features["ibt"] = HasLeaf7 && ((EDX >> 20) & 1); |
Oren Ben Simhon | fa582b0 | 2017-11-26 13:02:45 +0000 | [diff] [blame] | 1266 | |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1267 | bool HasLeafD = MaxLevel >= 0xd && |
| 1268 | !getX86CpuIDAndInfoEx(0xd, 0x1, &EAX, &EBX, &ECX, &EDX); |
| 1269 | |
| 1270 | // Only enable XSAVE if OS has enabled support for saving YMM state. |
Craig Topper | 1af7e44 | 2017-11-19 23:30:22 +0000 | [diff] [blame] | 1271 | Features["xsaveopt"] = HasLeafD && ((EAX >> 0) & 1) && HasAVXSave; |
| 1272 | Features["xsavec"] = HasLeafD && ((EAX >> 1) & 1) && HasAVXSave; |
| 1273 | Features["xsaves"] = HasLeafD && ((EAX >> 3) & 1) && HasAVXSave; |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1274 | |
| 1275 | return true; |
| 1276 | } |
| 1277 | #elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__)) |
| 1278 | bool sys::getHostCPUFeatures(StringMap<bool> &Features) { |
| 1279 | std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent(); |
| 1280 | if (!P) |
| 1281 | return false; |
| 1282 | |
| 1283 | SmallVector<StringRef, 32> Lines; |
| 1284 | P->getBuffer().split(Lines, "\n"); |
| 1285 | |
| 1286 | SmallVector<StringRef, 32> CPUFeatures; |
| 1287 | |
| 1288 | // Look for the CPU features. |
| 1289 | for (unsigned I = 0, E = Lines.size(); I != E; ++I) |
| 1290 | if (Lines[I].startswith("Features")) { |
| 1291 | Lines[I].split(CPUFeatures, ' '); |
| 1292 | break; |
| 1293 | } |
| 1294 | |
| 1295 | #if defined(__aarch64__) |
| 1296 | // Keep track of which crypto features we have seen |
| 1297 | enum { CAP_AES = 0x1, CAP_PMULL = 0x2, CAP_SHA1 = 0x4, CAP_SHA2 = 0x8 }; |
| 1298 | uint32_t crypto = 0; |
| 1299 | #endif |
| 1300 | |
| 1301 | for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) { |
| 1302 | StringRef LLVMFeatureStr = StringSwitch<StringRef>(CPUFeatures[I]) |
| 1303 | #if defined(__aarch64__) |
| 1304 | .Case("asimd", "neon") |
| 1305 | .Case("fp", "fp-armv8") |
| 1306 | .Case("crc32", "crc") |
| 1307 | #else |
| 1308 | .Case("half", "fp16") |
| 1309 | .Case("neon", "neon") |
| 1310 | .Case("vfpv3", "vfp3") |
| 1311 | .Case("vfpv3d16", "d16") |
| 1312 | .Case("vfpv4", "vfp4") |
| 1313 | .Case("idiva", "hwdiv-arm") |
| 1314 | .Case("idivt", "hwdiv") |
| 1315 | #endif |
| 1316 | .Default(""); |
| 1317 | |
| 1318 | #if defined(__aarch64__) |
| 1319 | // We need to check crypto separately since we need all of the crypto |
| 1320 | // extensions to enable the subtarget feature |
| 1321 | if (CPUFeatures[I] == "aes") |
| 1322 | crypto |= CAP_AES; |
| 1323 | else if (CPUFeatures[I] == "pmull") |
| 1324 | crypto |= CAP_PMULL; |
| 1325 | else if (CPUFeatures[I] == "sha1") |
| 1326 | crypto |= CAP_SHA1; |
| 1327 | else if (CPUFeatures[I] == "sha2") |
| 1328 | crypto |= CAP_SHA2; |
| 1329 | #endif |
| 1330 | |
| 1331 | if (LLVMFeatureStr != "") |
| 1332 | Features[LLVMFeatureStr] = true; |
| 1333 | } |
| 1334 | |
| 1335 | #if defined(__aarch64__) |
| 1336 | // If we have all crypto bits we can add the feature |
| 1337 | if (crypto == (CAP_AES | CAP_PMULL | CAP_SHA1 | CAP_SHA2)) |
| 1338 | Features["crypto"] = true; |
| 1339 | #endif |
| 1340 | |
| 1341 | return true; |
| 1342 | } |
| 1343 | #else |
| 1344 | bool sys::getHostCPUFeatures(StringMap<bool> &Features) { return false; } |
| 1345 | #endif |
| 1346 | |
| 1347 | std::string sys::getProcessTriple() { |
Alex Lorenz | 3803df3 | 2017-07-07 09:53:47 +0000 | [diff] [blame] | 1348 | std::string TargetTripleString = updateTripleOSVersion(LLVM_HOST_TRIPLE); |
| 1349 | Triple PT(Triple::normalize(TargetTripleString)); |
Simon Pilgrim | a271c54 | 2017-05-03 15:42:29 +0000 | [diff] [blame] | 1350 | |
| 1351 | if (sizeof(void *) == 8 && PT.isArch32Bit()) |
| 1352 | PT = PT.get64BitArchVariant(); |
| 1353 | if (sizeof(void *) == 4 && PT.isArch64Bit()) |
| 1354 | PT = PT.get32BitArchVariant(); |
| 1355 | |
| 1356 | return PT.str(); |
| 1357 | } |