blob: 42b71ad206f95664de44391003f1cb7ba96e21ba [file] [log] [blame]
Simon Pilgrima271c542017-05-03 15:42:29 +00001//===-- Host.cpp - Implement OS Host Concept --------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the operating system Host concept.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/Host.h"
15#include "llvm/ADT/SmallSet.h"
16#include "llvm/ADT/SmallVector.h"
17#include "llvm/ADT/StringRef.h"
18#include "llvm/ADT/StringSwitch.h"
19#include "llvm/ADT/Triple.h"
20#include "llvm/Config/config.h"
21#include "llvm/Support/Debug.h"
22#include "llvm/Support/FileSystem.h"
23#include "llvm/Support/MemoryBuffer.h"
24#include "llvm/Support/raw_ostream.h"
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
32#ifdef LLVM_ON_WIN32
33#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
67StringRef sys::detail::getHostCPUNameForPowerPC(
68 const StringRef &ProcCpuinfoContent) {
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
147StringRef sys::detail::getHostCPUNameForARM(
148 const StringRef &ProcCpuinfoContent) {
149 // The cpuid register on arm is not accessible from user space. On Linux,
150 // it is exposed through the /proc/cpuinfo file.
151
152 // Read 32 lines from /proc/cpuinfo, which should contain the CPU part line
153 // in all cases.
154 SmallVector<StringRef, 32> Lines;
155 ProcCpuinfoContent.split(Lines, "\n");
156
157 // Look for the CPU implementer line.
158 StringRef Implementer;
159 StringRef Hardware;
160 for (unsigned I = 0, E = Lines.size(); I != E; ++I) {
161 if (Lines[I].startswith("CPU implementer"))
162 Implementer = Lines[I].substr(15).ltrim("\t :");
163 if (Lines[I].startswith("Hardware"))
164 Hardware = Lines[I].substr(8).ltrim("\t :");
165 }
166
167 if (Implementer == "0x41") { // ARM Ltd.
168 // MSM8992/8994 may give cpu part for the core that the kernel is running on,
169 // which is undeterministic and wrong. Always return cortex-a53 for these SoC.
170 if (Hardware.endswith("MSM8994") || Hardware.endswith("MSM8996"))
171 return "cortex-a53";
172
173
174 // Look for the CPU part line.
175 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
176 if (Lines[I].startswith("CPU part"))
177 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
178 // values correspond to the "Part number" in the CP15/c0 register. The
179 // contents are specified in the various processor manuals.
180 return StringSwitch<const char *>(Lines[I].substr(8).ltrim("\t :"))
181 .Case("0x926", "arm926ej-s")
182 .Case("0xb02", "mpcore")
183 .Case("0xb36", "arm1136j-s")
184 .Case("0xb56", "arm1156t2-s")
185 .Case("0xb76", "arm1176jz-s")
186 .Case("0xc08", "cortex-a8")
187 .Case("0xc09", "cortex-a9")
188 .Case("0xc0f", "cortex-a15")
189 .Case("0xc20", "cortex-m0")
190 .Case("0xc23", "cortex-m3")
191 .Case("0xc24", "cortex-m4")
192 .Case("0xd04", "cortex-a35")
193 .Case("0xd03", "cortex-a53")
194 .Case("0xd07", "cortex-a57")
195 .Case("0xd08", "cortex-a72")
196 .Case("0xd09", "cortex-a73")
197 .Default("generic");
198 }
199
200 if (Implementer == "0x51") // Qualcomm Technologies, Inc.
201 // Look for the CPU part line.
202 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
203 if (Lines[I].startswith("CPU part"))
204 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
205 // values correspond to the "Part number" in the CP15/c0 register. The
206 // contents are specified in the various processor manuals.
207 return StringSwitch<const char *>(Lines[I].substr(8).ltrim("\t :"))
208 .Case("0x06f", "krait") // APQ8064
209 .Case("0x201", "kryo")
210 .Case("0x205", "kryo")
211 .Default("generic");
212
213 return "generic";
214}
215
216StringRef sys::detail::getHostCPUNameForS390x(
217 const StringRef &ProcCpuinfoContent) {
218 // STIDP is a privileged operation, so use /proc/cpuinfo instead.
219
220 // The "processor 0:" line comes after a fair amount of other information,
221 // including a cache breakdown, but this should be plenty.
222 SmallVector<StringRef, 32> Lines;
223 ProcCpuinfoContent.split(Lines, "\n");
224
225 // Look for the CPU features.
226 SmallVector<StringRef, 32> CPUFeatures;
227 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
228 if (Lines[I].startswith("features")) {
229 size_t Pos = Lines[I].find(":");
230 if (Pos != StringRef::npos) {
231 Lines[I].drop_front(Pos + 1).split(CPUFeatures, ' ');
232 break;
233 }
234 }
235
236 // We need to check for the presence of vector support independently of
237 // the machine type, since we may only use the vector register set when
238 // supported by the kernel (and hypervisor).
239 bool HaveVectorSupport = false;
240 for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) {
241 if (CPUFeatures[I] == "vx")
242 HaveVectorSupport = true;
243 }
244
245 // Now check the processor machine type.
246 for (unsigned I = 0, E = Lines.size(); I != E; ++I) {
247 if (Lines[I].startswith("processor ")) {
248 size_t Pos = Lines[I].find("machine = ");
249 if (Pos != StringRef::npos) {
250 Pos += sizeof("machine = ") - 1;
251 unsigned int Id;
252 if (!Lines[I].drop_front(Pos).getAsInteger(10, Id)) {
253 if (Id >= 2964 && HaveVectorSupport)
254 return "z13";
255 if (Id >= 2827)
256 return "zEC12";
257 if (Id >= 2817)
258 return "z196";
259 }
260 }
261 break;
262 }
263 }
264
265 return "generic";
266}
267
268#if defined(__i386__) || defined(_M_IX86) || \
269 defined(__x86_64__) || defined(_M_X64)
270
271enum VendorSignatures {
272 SIG_INTEL = 0x756e6547 /* Genu */,
273 SIG_AMD = 0x68747541 /* Auth */
274};
275
276enum ProcessorVendors {
277 VENDOR_INTEL = 1,
278 VENDOR_AMD,
279 VENDOR_OTHER,
280 VENDOR_MAX
281};
282
283enum ProcessorTypes {
284 INTEL_ATOM = 1,
285 INTEL_CORE2,
286 INTEL_COREI7,
287 AMDFAM10H,
288 AMDFAM15H,
289 INTEL_i386,
290 INTEL_i486,
291 INTEL_PENTIUM,
292 INTEL_PENTIUM_PRO,
293 INTEL_PENTIUM_II,
294 INTEL_PENTIUM_III,
295 INTEL_PENTIUM_IV,
296 INTEL_PENTIUM_M,
297 INTEL_CORE_DUO,
298 INTEL_XEONPHI,
299 INTEL_X86_64,
300 INTEL_NOCONA,
301 INTEL_PRESCOTT,
302 AMD_i486,
303 AMDPENTIUM,
304 AMDATHLON,
305 AMDFAM14H,
306 AMDFAM16H,
307 AMDFAM17H,
308 CPU_TYPE_MAX
309};
310
311enum ProcessorSubtypes {
312 INTEL_COREI7_NEHALEM = 1,
313 INTEL_COREI7_WESTMERE,
314 INTEL_COREI7_SANDYBRIDGE,
315 AMDFAM10H_BARCELONA,
316 AMDFAM10H_SHANGHAI,
317 AMDFAM10H_ISTANBUL,
318 AMDFAM15H_BDVER1,
319 AMDFAM15H_BDVER2,
320 INTEL_PENTIUM_MMX,
321 INTEL_CORE2_65,
322 INTEL_CORE2_45,
323 INTEL_COREI7_IVYBRIDGE,
324 INTEL_COREI7_HASWELL,
325 INTEL_COREI7_BROADWELL,
326 INTEL_COREI7_SKYLAKE,
327 INTEL_COREI7_SKYLAKE_AVX512,
328 INTEL_ATOM_BONNELL,
329 INTEL_ATOM_SILVERMONT,
Michael Zuckerman4bcb9c32017-06-29 10:00:33 +0000330 INTEL_ATOM_GOLDMONT,
Simon Pilgrima271c542017-05-03 15:42:29 +0000331 INTEL_KNIGHTS_LANDING,
332 AMDPENTIUM_K6,
333 AMDPENTIUM_K62,
334 AMDPENTIUM_K63,
335 AMDPENTIUM_GEODE,
336 AMDATHLON_TBIRD,
337 AMDATHLON_MP,
338 AMDATHLON_XP,
339 AMDATHLON_K8SSE3,
340 AMDATHLON_OPTERON,
341 AMDATHLON_FX,
342 AMDATHLON_64,
343 AMD_BTVER1,
344 AMD_BTVER2,
345 AMDFAM15H_BDVER3,
346 AMDFAM15H_BDVER4,
347 AMDFAM17H_ZNVER1,
348 CPU_SUBTYPE_MAX
349};
350
351enum ProcessorFeatures {
352 FEATURE_CMOV = 0,
353 FEATURE_MMX,
354 FEATURE_POPCNT,
355 FEATURE_SSE,
356 FEATURE_SSE2,
357 FEATURE_SSE3,
358 FEATURE_SSSE3,
359 FEATURE_SSE4_1,
360 FEATURE_SSE4_2,
361 FEATURE_AVX,
362 FEATURE_AVX2,
363 FEATURE_AVX512,
364 FEATURE_AVX512SAVE,
365 FEATURE_MOVBE,
366 FEATURE_ADX,
367 FEATURE_EM64T
368};
369
370// The check below for i386 was copied from clang's cpuid.h (__get_cpuid_max).
371// Check motivated by bug reports for OpenSSL crashing on CPUs without CPUID
372// support. Consequently, for i386, the presence of CPUID is checked first
373// via the corresponding eflags bit.
374// Removal of cpuid.h header motivated by PR30384
375// Header cpuid.h and method __get_cpuid_max are not used in llvm, clang, openmp
376// or test-suite, but are used in external projects e.g. libstdcxx
377static bool isCpuIdSupported() {
378#if defined(__GNUC__) || defined(__clang__)
379#if defined(__i386__)
380 int __cpuid_supported;
381 __asm__(" pushfl\n"
382 " popl %%eax\n"
383 " movl %%eax,%%ecx\n"
384 " xorl $0x00200000,%%eax\n"
385 " pushl %%eax\n"
386 " popfl\n"
387 " pushfl\n"
388 " popl %%eax\n"
389 " movl $0,%0\n"
390 " cmpl %%eax,%%ecx\n"
391 " je 1f\n"
392 " movl $1,%0\n"
393 "1:"
394 : "=r"(__cpuid_supported)
395 :
396 : "eax", "ecx");
397 if (!__cpuid_supported)
398 return false;
399#endif
400 return true;
401#endif
402 return true;
403}
404
405/// getX86CpuIDAndInfo - Execute the specified cpuid and return the 4 values in
406/// the specified arguments. If we can't run cpuid on the host, return true.
407static bool getX86CpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
408 unsigned *rECX, unsigned *rEDX) {
409#if defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)
410#if defined(__GNUC__) || defined(__clang__)
411#if defined(__x86_64__)
412 // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
413 // FIXME: should we save this for Clang?
414 __asm__("movq\t%%rbx, %%rsi\n\t"
415 "cpuid\n\t"
416 "xchgq\t%%rbx, %%rsi\n\t"
417 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
418 : "a"(value));
419#elif defined(__i386__)
420 __asm__("movl\t%%ebx, %%esi\n\t"
421 "cpuid\n\t"
422 "xchgl\t%%ebx, %%esi\n\t"
423 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
424 : "a"(value));
425#else
426 assert(0 && "This method is defined only for x86.");
427#endif
428#elif defined(_MSC_VER)
429 // The MSVC intrinsic is portable across x86 and x64.
430 int registers[4];
431 __cpuid(registers, value);
432 *rEAX = registers[0];
433 *rEBX = registers[1];
434 *rECX = registers[2];
435 *rEDX = registers[3];
436#endif
437 return false;
438#else
439 return true;
440#endif
441}
442
443/// getX86CpuIDAndInfoEx - Execute the specified cpuid with subleaf and return
444/// the 4 values in the specified arguments. If we can't run cpuid on the host,
445/// return true.
446static bool getX86CpuIDAndInfoEx(unsigned value, unsigned subleaf,
447 unsigned *rEAX, unsigned *rEBX, unsigned *rECX,
448 unsigned *rEDX) {
449#if defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)
450#if defined(__x86_64__) || defined(_M_X64)
451#if defined(__GNUC__) || defined(__clang__)
452 // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually.
453 // FIXME: should we save this for Clang?
454 __asm__("movq\t%%rbx, %%rsi\n\t"
455 "cpuid\n\t"
456 "xchgq\t%%rbx, %%rsi\n\t"
457 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
458 : "a"(value), "c"(subleaf));
459#elif defined(_MSC_VER)
460 int registers[4];
461 __cpuidex(registers, value, subleaf);
462 *rEAX = registers[0];
463 *rEBX = registers[1];
464 *rECX = registers[2];
465 *rEDX = registers[3];
466#endif
467#elif defined(__i386__) || defined(_M_IX86)
468#if defined(__GNUC__) || defined(__clang__)
469 __asm__("movl\t%%ebx, %%esi\n\t"
470 "cpuid\n\t"
471 "xchgl\t%%ebx, %%esi\n\t"
472 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
473 : "a"(value), "c"(subleaf));
474#elif defined(_MSC_VER)
475 __asm {
476 mov eax,value
477 mov ecx,subleaf
478 cpuid
479 mov esi,rEAX
480 mov dword ptr [esi],eax
481 mov esi,rEBX
482 mov dword ptr [esi],ebx
483 mov esi,rECX
484 mov dword ptr [esi],ecx
485 mov esi,rEDX
486 mov dword ptr [esi],edx
487 }
488#endif
489#else
490 assert(0 && "This method is defined only for x86.");
491#endif
492 return false;
493#else
494 return true;
495#endif
496}
497
498static bool getX86XCR0(unsigned *rEAX, unsigned *rEDX) {
499#if defined(__GNUC__) || defined(__clang__)
500 // Check xgetbv; this uses a .byte sequence instead of the instruction
501 // directly because older assemblers do not include support for xgetbv and
502 // there is no easy way to conditionally compile based on the assembler used.
503 __asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(*rEAX), "=d"(*rEDX) : "c"(0));
504 return false;
505#elif defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK)
506 unsigned long long Result = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
507 *rEAX = Result;
508 *rEDX = Result >> 32;
509 return false;
510#else
511 return true;
512#endif
513}
514
515static void detectX86FamilyModel(unsigned EAX, unsigned *Family,
516 unsigned *Model) {
517 *Family = (EAX >> 8) & 0xf; // Bits 8 - 11
518 *Model = (EAX >> 4) & 0xf; // Bits 4 - 7
519 if (*Family == 6 || *Family == 0xf) {
520 if (*Family == 0xf)
521 // Examine extended family ID if family ID is F.
522 *Family += (EAX >> 20) & 0xff; // Bits 20 - 27
523 // Examine extended model ID if family ID is 6 or F.
524 *Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
525 }
526}
527
528static void
Craig Topperc6bbe4b2017-07-08 05:16:14 +0000529getIntelProcessorTypeAndSubtype(unsigned Family, unsigned Model,
530 unsigned Brand_id, unsigned Features,
Simon Pilgrima271c542017-05-03 15:42:29 +0000531 unsigned *Type, unsigned *Subtype) {
532 if (Brand_id != 0)
533 return;
534 switch (Family) {
535 case 3:
536 *Type = INTEL_i386;
537 break;
538 case 4:
539 switch (Model) {
540 case 0: // Intel486 DX processors
541 case 1: // Intel486 DX processors
542 case 2: // Intel486 SX processors
543 case 3: // Intel487 processors, IntelDX2 OverDrive processors,
544 // IntelDX2 processors
545 case 4: // Intel486 SL processor
546 case 5: // IntelSX2 processors
547 case 7: // Write-Back Enhanced IntelDX2 processors
548 case 8: // IntelDX4 OverDrive processors, IntelDX4 processors
549 default:
550 *Type = INTEL_i486;
551 break;
552 }
553 break;
554 case 5:
555 switch (Model) {
556 case 1: // Pentium OverDrive processor for Pentium processor (60, 66),
557 // Pentium processors (60, 66)
558 case 2: // Pentium OverDrive processor for Pentium processor (75, 90,
559 // 100, 120, 133), Pentium processors (75, 90, 100, 120, 133,
560 // 150, 166, 200)
561 case 3: // Pentium OverDrive processors for Intel486 processor-based
562 // systems
563 *Type = INTEL_PENTIUM;
564 break;
565 case 4: // Pentium OverDrive processor with MMX technology for Pentium
566 // processor (75, 90, 100, 120, 133), Pentium processor with
567 // MMX technology (166, 200)
568 *Type = INTEL_PENTIUM;
569 *Subtype = INTEL_PENTIUM_MMX;
570 break;
571 default:
572 *Type = INTEL_PENTIUM;
573 break;
574 }
575 break;
576 case 6:
577 switch (Model) {
578 case 0x01: // Pentium Pro processor
579 *Type = INTEL_PENTIUM_PRO;
580 break;
581 case 0x03: // Intel Pentium II OverDrive processor, Pentium II processor,
582 // model 03
583 case 0x05: // Pentium II processor, model 05, Pentium II Xeon processor,
584 // model 05, and Intel Celeron processor, model 05
585 case 0x06: // Celeron processor, model 06
586 *Type = INTEL_PENTIUM_II;
587 break;
588 case 0x07: // Pentium III processor, model 07, and Pentium III Xeon
589 // processor, model 07
590 case 0x08: // Pentium III processor, model 08, Pentium III Xeon processor,
591 // model 08, and Celeron processor, model 08
592 case 0x0a: // Pentium III Xeon processor, model 0Ah
593 case 0x0b: // Pentium III processor, model 0Bh
594 *Type = INTEL_PENTIUM_III;
595 break;
596 case 0x09: // Intel Pentium M processor, Intel Celeron M processor model 09.
597 case 0x0d: // Intel Pentium M processor, Intel Celeron M processor, model
598 // 0Dh. All processors are manufactured using the 90 nm process.
599 case 0x15: // Intel EP80579 Integrated Processor and Intel EP80579
600 // Integrated Processor with Intel QuickAssist Technology
601 *Type = INTEL_PENTIUM_M;
602 break;
603 case 0x0e: // Intel Core Duo processor, Intel Core Solo processor, model
604 // 0Eh. All processors are manufactured using the 65 nm process.
605 *Type = INTEL_CORE_DUO;
606 break; // yonah
607 case 0x0f: // Intel Core 2 Duo processor, Intel Core 2 Duo mobile
608 // processor, Intel Core 2 Quad processor, Intel Core 2 Quad
609 // mobile processor, Intel Core 2 Extreme processor, Intel
610 // Pentium Dual-Core processor, Intel Xeon processor, model
611 // 0Fh. All processors are manufactured using the 65 nm process.
612 case 0x16: // Intel Celeron processor model 16h. All processors are
613 // manufactured using the 65 nm process
614 *Type = INTEL_CORE2; // "core2"
615 *Subtype = INTEL_CORE2_65;
616 break;
617 case 0x17: // Intel Core 2 Extreme processor, Intel Xeon processor, model
618 // 17h. All processors are manufactured using the 45 nm process.
619 //
620 // 45nm: Penryn , Wolfdale, Yorkfield (XE)
621 case 0x1d: // Intel Xeon processor MP. All processors are manufactured using
622 // the 45 nm process.
623 *Type = INTEL_CORE2; // "penryn"
624 *Subtype = INTEL_CORE2_45;
625 break;
626 case 0x1a: // Intel Core i7 processor and Intel Xeon processor. All
627 // processors are manufactured using the 45 nm process.
628 case 0x1e: // Intel(R) Core(TM) i7 CPU 870 @ 2.93GHz.
629 // As found in a Summer 2010 model iMac.
630 case 0x1f:
631 case 0x2e: // Nehalem EX
632 *Type = INTEL_COREI7; // "nehalem"
633 *Subtype = INTEL_COREI7_NEHALEM;
634 break;
635 case 0x25: // Intel Core i7, laptop version.
636 case 0x2c: // Intel Core i7 processor and Intel Xeon processor. All
637 // processors are manufactured using the 32 nm process.
638 case 0x2f: // Westmere EX
639 *Type = INTEL_COREI7; // "westmere"
640 *Subtype = INTEL_COREI7_WESTMERE;
641 break;
642 case 0x2a: // Intel Core i7 processor. All processors are manufactured
643 // using the 32 nm process.
644 case 0x2d:
645 *Type = INTEL_COREI7; //"sandybridge"
646 *Subtype = INTEL_COREI7_SANDYBRIDGE;
647 break;
648 case 0x3a:
649 case 0x3e: // Ivy Bridge EP
650 *Type = INTEL_COREI7; // "ivybridge"
651 *Subtype = INTEL_COREI7_IVYBRIDGE;
652 break;
653
654 // Haswell:
655 case 0x3c:
656 case 0x3f:
657 case 0x45:
658 case 0x46:
659 *Type = INTEL_COREI7; // "haswell"
660 *Subtype = INTEL_COREI7_HASWELL;
661 break;
662
663 // Broadwell:
664 case 0x3d:
665 case 0x47:
666 case 0x4f:
667 case 0x56:
668 *Type = INTEL_COREI7; // "broadwell"
669 *Subtype = INTEL_COREI7_BROADWELL;
670 break;
671
672 // Skylake:
673 case 0x4e: // Skylake mobile
674 case 0x5e: // Skylake desktop
675 case 0x8e: // Kaby Lake mobile
676 case 0x9e: // Kaby Lake desktop
677 *Type = INTEL_COREI7; // "skylake"
678 *Subtype = INTEL_COREI7_SKYLAKE;
679 break;
680
681 // Skylake Xeon:
682 case 0x55:
683 *Type = INTEL_COREI7;
684 // Check that we really have AVX512
685 if (Features & (1 << FEATURE_AVX512)) {
686 *Subtype = INTEL_COREI7_SKYLAKE_AVX512; // "skylake-avx512"
687 } else {
688 *Subtype = INTEL_COREI7_SKYLAKE; // "skylake"
689 }
690 break;
691
692 case 0x1c: // Most 45 nm Intel Atom processors
693 case 0x26: // 45 nm Atom Lincroft
694 case 0x27: // 32 nm Atom Medfield
695 case 0x35: // 32 nm Atom Midview
696 case 0x36: // 32 nm Atom Midview
697 *Type = INTEL_ATOM;
698 *Subtype = INTEL_ATOM_BONNELL;
699 break; // "bonnell"
700
701 // Atom Silvermont codes from the Intel software optimization guide.
702 case 0x37:
703 case 0x4a:
704 case 0x4d:
705 case 0x5a:
706 case 0x5d:
707 case 0x4c: // really airmont
708 *Type = INTEL_ATOM;
709 *Subtype = INTEL_ATOM_SILVERMONT;
710 break; // "silvermont"
Michael Zuckerman4bcb9c32017-06-29 10:00:33 +0000711 // Goldmont:
712 case 0x5c:
713 case 0x5f:
714 *Type = INTEL_ATOM;
715 *Subtype = INTEL_ATOM_GOLDMONT;
716 break; // "goldmont"
Simon Pilgrima271c542017-05-03 15:42:29 +0000717 case 0x57:
718 *Type = INTEL_XEONPHI; // knl
719 *Subtype = INTEL_KNIGHTS_LANDING;
720 break;
721
722 default: // Unknown family 6 CPU, try to guess.
723 if (Features & (1 << FEATURE_AVX512)) {
724 *Type = INTEL_XEONPHI; // knl
725 *Subtype = INTEL_KNIGHTS_LANDING;
726 break;
727 }
728 if (Features & (1 << FEATURE_ADX)) {
729 *Type = INTEL_COREI7;
730 *Subtype = INTEL_COREI7_BROADWELL;
731 break;
732 }
733 if (Features & (1 << FEATURE_AVX2)) {
734 *Type = INTEL_COREI7;
735 *Subtype = INTEL_COREI7_HASWELL;
736 break;
737 }
738 if (Features & (1 << FEATURE_AVX)) {
739 *Type = INTEL_COREI7;
740 *Subtype = INTEL_COREI7_SANDYBRIDGE;
741 break;
742 }
743 if (Features & (1 << FEATURE_SSE4_2)) {
744 if (Features & (1 << FEATURE_MOVBE)) {
745 *Type = INTEL_ATOM;
746 *Subtype = INTEL_ATOM_SILVERMONT;
747 } else {
748 *Type = INTEL_COREI7;
749 *Subtype = INTEL_COREI7_NEHALEM;
750 }
751 break;
752 }
753 if (Features & (1 << FEATURE_SSE4_1)) {
754 *Type = INTEL_CORE2; // "penryn"
755 *Subtype = INTEL_CORE2_45;
756 break;
757 }
758 if (Features & (1 << FEATURE_SSSE3)) {
759 if (Features & (1 << FEATURE_MOVBE)) {
760 *Type = INTEL_ATOM;
761 *Subtype = INTEL_ATOM_BONNELL; // "bonnell"
762 } else {
763 *Type = INTEL_CORE2; // "core2"
764 *Subtype = INTEL_CORE2_65;
765 }
766 break;
767 }
768 if (Features & (1 << FEATURE_EM64T)) {
769 *Type = INTEL_X86_64;
770 break; // x86-64
771 }
772 if (Features & (1 << FEATURE_SSE2)) {
773 *Type = INTEL_PENTIUM_M;
774 break;
775 }
776 if (Features & (1 << FEATURE_SSE)) {
777 *Type = INTEL_PENTIUM_III;
778 break;
779 }
780 if (Features & (1 << FEATURE_MMX)) {
781 *Type = INTEL_PENTIUM_II;
782 break;
783 }
784 *Type = INTEL_PENTIUM_PRO;
785 break;
786 }
787 break;
788 case 15: {
789 switch (Model) {
790 case 0: // Pentium 4 processor, Intel Xeon processor. All processors are
791 // model 00h and manufactured using the 0.18 micron process.
792 case 1: // Pentium 4 processor, Intel Xeon processor, Intel Xeon
793 // processor MP, and Intel Celeron processor. All processors are
794 // model 01h and manufactured using the 0.18 micron process.
795 case 2: // Pentium 4 processor, Mobile Intel Pentium 4 processor - M,
796 // Intel Xeon processor, Intel Xeon processor MP, Intel Celeron
797 // processor, and Mobile Intel Celeron processor. All processors
798 // are model 02h and manufactured using the 0.13 micron process.
799 *Type =
800 ((Features & (1 << FEATURE_EM64T)) ? INTEL_X86_64 : INTEL_PENTIUM_IV);
801 break;
802
803 case 3: // Pentium 4 processor, Intel Xeon processor, Intel Celeron D
804 // processor. All processors are model 03h and manufactured using
805 // the 90 nm process.
806 case 4: // Pentium 4 processor, Pentium 4 processor Extreme Edition,
807 // Pentium D processor, Intel Xeon processor, Intel Xeon
808 // processor MP, Intel Celeron D processor. All processors are
809 // model 04h and manufactured using the 90 nm process.
810 case 6: // Pentium 4 processor, Pentium D processor, Pentium processor
811 // Extreme Edition, Intel Xeon processor, Intel Xeon processor
812 // MP, Intel Celeron D processor. All processors are model 06h
813 // and manufactured using the 65 nm process.
814 *Type =
815 ((Features & (1 << FEATURE_EM64T)) ? INTEL_NOCONA : INTEL_PRESCOTT);
816 break;
817
818 default:
819 *Type =
820 ((Features & (1 << FEATURE_EM64T)) ? INTEL_X86_64 : INTEL_PENTIUM_IV);
821 break;
822 }
823 break;
824 }
825 default:
826 break; /*"generic"*/
827 }
828}
829
Craig Topper2ace1532017-07-08 06:44:34 +0000830static void getAMDProcessorTypeAndSubtype(unsigned Family, unsigned Model,
831 unsigned Features, unsigned *Type,
Simon Pilgrima271c542017-05-03 15:42:29 +0000832 unsigned *Subtype) {
833 // FIXME: this poorly matches the generated SubtargetFeatureKV table. There
834 // appears to be no way to generate the wide variety of AMD-specific targets
835 // from the information returned from CPUID.
836 switch (Family) {
837 case 4:
838 *Type = AMD_i486;
839 break;
840 case 5:
841 *Type = AMDPENTIUM;
842 switch (Model) {
843 case 6:
844 case 7:
845 *Subtype = AMDPENTIUM_K6;
846 break; // "k6"
847 case 8:
848 *Subtype = AMDPENTIUM_K62;
849 break; // "k6-2"
850 case 9:
851 case 13:
852 *Subtype = AMDPENTIUM_K63;
853 break; // "k6-3"
854 case 10:
855 *Subtype = AMDPENTIUM_GEODE;
856 break; // "geode"
857 }
858 break;
859 case 6:
860 *Type = AMDATHLON;
861 switch (Model) {
862 case 4:
863 *Subtype = AMDATHLON_TBIRD;
864 break; // "athlon-tbird"
865 case 6:
866 case 7:
867 case 8:
868 *Subtype = AMDATHLON_MP;
869 break; // "athlon-mp"
870 case 10:
871 *Subtype = AMDATHLON_XP;
872 break; // "athlon-xp"
873 }
874 break;
875 case 15:
876 *Type = AMDATHLON;
877 if (Features & (1 << FEATURE_SSE3)) {
878 *Subtype = AMDATHLON_K8SSE3;
879 break; // "k8-sse3"
880 }
881 switch (Model) {
882 case 1:
883 *Subtype = AMDATHLON_OPTERON;
884 break; // "opteron"
885 case 5:
886 *Subtype = AMDATHLON_FX;
887 break; // "athlon-fx"; also opteron
888 default:
889 *Subtype = AMDATHLON_64;
890 break; // "athlon64"
891 }
892 break;
893 case 16:
894 *Type = AMDFAM10H; // "amdfam10"
895 switch (Model) {
896 case 2:
897 *Subtype = AMDFAM10H_BARCELONA;
898 break;
899 case 4:
900 *Subtype = AMDFAM10H_SHANGHAI;
901 break;
902 case 8:
903 *Subtype = AMDFAM10H_ISTANBUL;
904 break;
905 }
906 break;
907 case 20:
908 *Type = AMDFAM14H;
909 *Subtype = AMD_BTVER1;
910 break; // "btver1";
911 case 21:
912 *Type = AMDFAM15H;
913 if (!(Features &
914 (1 << FEATURE_AVX))) { // If no AVX support, provide a sane fallback.
915 *Subtype = AMD_BTVER1;
916 break; // "btver1"
917 }
918 if (Model >= 0x50 && Model <= 0x6f) {
919 *Subtype = AMDFAM15H_BDVER4;
920 break; // "bdver4"; 50h-6Fh: Excavator
921 }
922 if (Model >= 0x30 && Model <= 0x3f) {
923 *Subtype = AMDFAM15H_BDVER3;
924 break; // "bdver3"; 30h-3Fh: Steamroller
925 }
926 if (Model >= 0x10 && Model <= 0x1f) {
927 *Subtype = AMDFAM15H_BDVER2;
928 break; // "bdver2"; 10h-1Fh: Piledriver
929 }
930 if (Model <= 0x0f) {
931 *Subtype = AMDFAM15H_BDVER1;
932 break; // "bdver1"; 00h-0Fh: Bulldozer
933 }
934 break;
935 case 22:
936 *Type = AMDFAM16H;
937 if (!(Features &
938 (1 << FEATURE_AVX))) { // If no AVX support provide a sane fallback.
939 *Subtype = AMD_BTVER1;
940 break; // "btver1";
941 }
942 *Subtype = AMD_BTVER2;
943 break; // "btver2"
944 case 23:
945 *Type = AMDFAM17H;
946 if (Features & (1 << FEATURE_ADX)) {
947 *Subtype = AMDFAM17H_ZNVER1;
948 break; // "znver1"
949 }
950 *Subtype = AMD_BTVER1;
951 break;
952 default:
953 break; // "generic"
954 }
955}
956
Craig Topperc6bbe4b2017-07-08 05:16:14 +0000957static unsigned getAvailableFeatures(unsigned ECX, unsigned EDX,
Simon Pilgrima271c542017-05-03 15:42:29 +0000958 unsigned MaxLeaf) {
959 unsigned Features = 0;
Craig Topperc6bbe4b2017-07-08 05:16:14 +0000960 unsigned EAX, EBX;
Simon Pilgrima271c542017-05-03 15:42:29 +0000961 Features |= (((EDX >> 23) & 1) << FEATURE_MMX);
962 Features |= (((EDX >> 25) & 1) << FEATURE_SSE);
963 Features |= (((EDX >> 26) & 1) << FEATURE_SSE2);
964 Features |= (((ECX >> 0) & 1) << FEATURE_SSE3);
965 Features |= (((ECX >> 9) & 1) << FEATURE_SSSE3);
966 Features |= (((ECX >> 19) & 1) << FEATURE_SSE4_1);
967 Features |= (((ECX >> 20) & 1) << FEATURE_SSE4_2);
968 Features |= (((ECX >> 22) & 1) << FEATURE_MOVBE);
969
970 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
971 // indicates that the AVX registers will be saved and restored on context
972 // switch, then we have full AVX support.
973 const unsigned AVXBits = (1 << 27) | (1 << 28);
974 bool HasAVX = ((ECX & AVXBits) == AVXBits) && !getX86XCR0(&EAX, &EDX) &&
975 ((EAX & 0x6) == 0x6);
976 bool HasAVX512Save = HasAVX && ((EAX & 0xe0) == 0xe0);
977 bool HasLeaf7 =
978 MaxLeaf >= 0x7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
979 bool HasADX = HasLeaf7 && ((EBX >> 19) & 1);
980 bool HasAVX2 = HasAVX && HasLeaf7 && (EBX & 0x20);
981 bool HasAVX512 = HasLeaf7 && HasAVX512Save && ((EBX >> 16) & 1);
982 Features |= (HasAVX << FEATURE_AVX);
983 Features |= (HasAVX2 << FEATURE_AVX2);
984 Features |= (HasAVX512 << FEATURE_AVX512);
985 Features |= (HasAVX512Save << FEATURE_AVX512SAVE);
986 Features |= (HasADX << FEATURE_ADX);
987
Craig Topperbb8c7992017-07-08 05:16:13 +0000988 unsigned MaxExtLevel;
989 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
990
991 bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
992 !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
993 if (HasExtLeaf1)
994 Features |= (((EDX >> 29) & 0x1) << FEATURE_EM64T);
995
Simon Pilgrima271c542017-05-03 15:42:29 +0000996 return Features;
997}
998
999StringRef sys::getHostCPUName() {
1000 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
1001 unsigned MaxLeaf, Vendor;
1002
1003#if defined(__GNUC__) || defined(__clang__)
1004 //FIXME: include cpuid.h from clang or copy __get_cpuid_max here
1005 // and simplify it to not invoke __cpuid (like cpu_model.c in
1006 // compiler-rt/lib/builtins/cpu_model.c?
1007 // Opting for the second option.
1008 if(!isCpuIdSupported())
1009 return "generic";
1010#endif
Craig Topperbb8c7992017-07-08 05:16:13 +00001011 if (getX86CpuIDAndInfo(0, &MaxLeaf, &Vendor, &ECX, &EDX) || MaxLeaf < 1)
Simon Pilgrima271c542017-05-03 15:42:29 +00001012 return "generic";
Craig Topperbb8c7992017-07-08 05:16:13 +00001013 getX86CpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
Simon Pilgrima271c542017-05-03 15:42:29 +00001014
1015 unsigned Brand_id = EBX & 0xff;
1016 unsigned Family = 0, Model = 0;
1017 unsigned Features = 0;
1018 detectX86FamilyModel(EAX, &Family, &Model);
1019 Features = getAvailableFeatures(ECX, EDX, MaxLeaf);
1020
1021 unsigned Type;
1022 unsigned Subtype;
1023
1024 if (Vendor == SIG_INTEL) {
1025 getIntelProcessorTypeAndSubtype(Family, Model, Brand_id, Features, &Type,
1026 &Subtype);
1027 switch (Type) {
1028 case INTEL_i386:
1029 return "i386";
1030 case INTEL_i486:
1031 return "i486";
1032 case INTEL_PENTIUM:
1033 if (Subtype == INTEL_PENTIUM_MMX)
1034 return "pentium-mmx";
1035 return "pentium";
1036 case INTEL_PENTIUM_PRO:
1037 return "pentiumpro";
1038 case INTEL_PENTIUM_II:
1039 return "pentium2";
1040 case INTEL_PENTIUM_III:
1041 return "pentium3";
1042 case INTEL_PENTIUM_IV:
1043 return "pentium4";
1044 case INTEL_PENTIUM_M:
1045 return "pentium-m";
1046 case INTEL_CORE_DUO:
1047 return "yonah";
1048 case INTEL_CORE2:
1049 switch (Subtype) {
1050 case INTEL_CORE2_65:
1051 return "core2";
1052 case INTEL_CORE2_45:
1053 return "penryn";
1054 default:
1055 return "core2";
1056 }
1057 case INTEL_COREI7:
1058 switch (Subtype) {
1059 case INTEL_COREI7_NEHALEM:
1060 return "nehalem";
1061 case INTEL_COREI7_WESTMERE:
1062 return "westmere";
1063 case INTEL_COREI7_SANDYBRIDGE:
1064 return "sandybridge";
1065 case INTEL_COREI7_IVYBRIDGE:
1066 return "ivybridge";
1067 case INTEL_COREI7_HASWELL:
1068 return "haswell";
1069 case INTEL_COREI7_BROADWELL:
1070 return "broadwell";
1071 case INTEL_COREI7_SKYLAKE:
1072 return "skylake";
1073 case INTEL_COREI7_SKYLAKE_AVX512:
1074 return "skylake-avx512";
1075 default:
1076 return "corei7";
1077 }
1078 case INTEL_ATOM:
1079 switch (Subtype) {
1080 case INTEL_ATOM_BONNELL:
1081 return "bonnell";
Michael Zuckerman4bcb9c32017-06-29 10:00:33 +00001082 case INTEL_ATOM_GOLDMONT:
1083 return "goldmont";
Simon Pilgrima271c542017-05-03 15:42:29 +00001084 case INTEL_ATOM_SILVERMONT:
1085 return "silvermont";
1086 default:
1087 return "atom";
1088 }
1089 case INTEL_XEONPHI:
1090 return "knl"; /*update for more variants added*/
1091 case INTEL_X86_64:
1092 return "x86-64";
1093 case INTEL_NOCONA:
1094 return "nocona";
1095 case INTEL_PRESCOTT:
1096 return "prescott";
1097 default:
1098 return "generic";
1099 }
1100 } else if (Vendor == SIG_AMD) {
1101 getAMDProcessorTypeAndSubtype(Family, Model, Features, &Type, &Subtype);
1102 switch (Type) {
1103 case AMD_i486:
1104 return "i486";
1105 case AMDPENTIUM:
1106 switch (Subtype) {
1107 case AMDPENTIUM_K6:
1108 return "k6";
1109 case AMDPENTIUM_K62:
1110 return "k6-2";
1111 case AMDPENTIUM_K63:
1112 return "k6-3";
1113 case AMDPENTIUM_GEODE:
1114 return "geode";
1115 default:
1116 return "pentium";
1117 }
1118 case AMDATHLON:
1119 switch (Subtype) {
1120 case AMDATHLON_TBIRD:
1121 return "athlon-tbird";
1122 case AMDATHLON_MP:
1123 return "athlon-mp";
1124 case AMDATHLON_XP:
1125 return "athlon-xp";
1126 case AMDATHLON_K8SSE3:
1127 return "k8-sse3";
1128 case AMDATHLON_OPTERON:
1129 return "opteron";
1130 case AMDATHLON_FX:
1131 return "athlon-fx";
1132 case AMDATHLON_64:
1133 return "athlon64";
1134 default:
1135 return "athlon";
1136 }
1137 case AMDFAM10H:
1138 if(Subtype == AMDFAM10H_BARCELONA)
1139 return "barcelona";
1140 return "amdfam10";
1141 case AMDFAM14H:
1142 return "btver1";
1143 case AMDFAM15H:
1144 switch (Subtype) {
1145 case AMDFAM15H_BDVER1:
1146 return "bdver1";
1147 case AMDFAM15H_BDVER2:
1148 return "bdver2";
1149 case AMDFAM15H_BDVER3:
1150 return "bdver3";
1151 case AMDFAM15H_BDVER4:
1152 return "bdver4";
1153 case AMD_BTVER1:
1154 return "btver1";
1155 default:
1156 return "amdfam15";
1157 }
1158 case AMDFAM16H:
1159 switch (Subtype) {
1160 case AMD_BTVER1:
1161 return "btver1";
1162 case AMD_BTVER2:
1163 return "btver2";
1164 default:
1165 return "amdfam16";
1166 }
1167 case AMDFAM17H:
1168 switch (Subtype) {
1169 case AMD_BTVER1:
1170 return "btver1";
1171 case AMDFAM17H_ZNVER1:
1172 return "znver1";
1173 default:
1174 return "amdfam17";
1175 }
1176 default:
1177 return "generic";
1178 }
1179 }
1180 return "generic";
1181}
1182
1183#elif defined(__APPLE__) && (defined(__ppc__) || defined(__powerpc__))
1184StringRef sys::getHostCPUName() {
1185 host_basic_info_data_t hostInfo;
1186 mach_msg_type_number_t infoCount;
1187
1188 infoCount = HOST_BASIC_INFO_COUNT;
1189 host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo,
1190 &infoCount);
1191
1192 if (hostInfo.cpu_type != CPU_TYPE_POWERPC)
1193 return "generic";
1194
1195 switch (hostInfo.cpu_subtype) {
1196 case CPU_SUBTYPE_POWERPC_601:
1197 return "601";
1198 case CPU_SUBTYPE_POWERPC_602:
1199 return "602";
1200 case CPU_SUBTYPE_POWERPC_603:
1201 return "603";
1202 case CPU_SUBTYPE_POWERPC_603e:
1203 return "603e";
1204 case CPU_SUBTYPE_POWERPC_603ev:
1205 return "603ev";
1206 case CPU_SUBTYPE_POWERPC_604:
1207 return "604";
1208 case CPU_SUBTYPE_POWERPC_604e:
1209 return "604e";
1210 case CPU_SUBTYPE_POWERPC_620:
1211 return "620";
1212 case CPU_SUBTYPE_POWERPC_750:
1213 return "750";
1214 case CPU_SUBTYPE_POWERPC_7400:
1215 return "7400";
1216 case CPU_SUBTYPE_POWERPC_7450:
1217 return "7450";
1218 case CPU_SUBTYPE_POWERPC_970:
1219 return "970";
1220 default:;
1221 }
1222
1223 return "generic";
1224}
1225#elif defined(__linux__) && (defined(__ppc__) || defined(__powerpc__))
1226StringRef sys::getHostCPUName() {
1227 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1228 const StringRef& Content = P ? P->getBuffer() : "";
1229 return detail::getHostCPUNameForPowerPC(Content);
1230}
1231#elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1232StringRef sys::getHostCPUName() {
1233 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1234 const StringRef& Content = P ? P->getBuffer() : "";
1235 return detail::getHostCPUNameForARM(Content);
1236}
1237#elif defined(__linux__) && defined(__s390x__)
1238StringRef sys::getHostCPUName() {
1239 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1240 const StringRef& Content = P ? P->getBuffer() : "";
1241 return detail::getHostCPUNameForS390x(Content);
1242}
1243#else
1244StringRef sys::getHostCPUName() { return "generic"; }
1245#endif
1246
1247#if defined(__linux__) && defined(__x86_64__)
1248// On Linux, the number of physical cores can be computed from /proc/cpuinfo,
1249// using the number of unique physical/core id pairs. The following
1250// implementation reads the /proc/cpuinfo format on an x86_64 system.
1251static int computeHostNumPhysicalCores() {
1252 // Read /proc/cpuinfo as a stream (until EOF reached). It cannot be
1253 // mmapped because it appears to have 0 size.
1254 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
1255 llvm::MemoryBuffer::getFileAsStream("/proc/cpuinfo");
1256 if (std::error_code EC = Text.getError()) {
1257 llvm::errs() << "Can't read "
1258 << "/proc/cpuinfo: " << EC.message() << "\n";
1259 return -1;
1260 }
1261 SmallVector<StringRef, 8> strs;
1262 (*Text)->getBuffer().split(strs, "\n", /*MaxSplit=*/-1,
1263 /*KeepEmpty=*/false);
1264 int CurPhysicalId = -1;
1265 int CurCoreId = -1;
1266 SmallSet<std::pair<int, int>, 32> UniqueItems;
1267 for (auto &Line : strs) {
1268 Line = Line.trim();
1269 if (!Line.startswith("physical id") && !Line.startswith("core id"))
1270 continue;
1271 std::pair<StringRef, StringRef> Data = Line.split(':');
1272 auto Name = Data.first.trim();
1273 auto Val = Data.second.trim();
1274 if (Name == "physical id") {
1275 assert(CurPhysicalId == -1 &&
1276 "Expected a core id before seeing another physical id");
1277 Val.getAsInteger(10, CurPhysicalId);
1278 }
1279 if (Name == "core id") {
1280 assert(CurCoreId == -1 &&
1281 "Expected a physical id before seeing another core id");
1282 Val.getAsInteger(10, CurCoreId);
1283 }
1284 if (CurPhysicalId != -1 && CurCoreId != -1) {
1285 UniqueItems.insert(std::make_pair(CurPhysicalId, CurCoreId));
1286 CurPhysicalId = -1;
1287 CurCoreId = -1;
1288 }
1289 }
1290 return UniqueItems.size();
1291}
1292#elif defined(__APPLE__) && defined(__x86_64__)
1293#include <sys/param.h>
1294#include <sys/sysctl.h>
1295
1296// Gets the number of *physical cores* on the machine.
1297static int computeHostNumPhysicalCores() {
1298 uint32_t count;
1299 size_t len = sizeof(count);
1300 sysctlbyname("hw.physicalcpu", &count, &len, NULL, 0);
1301 if (count < 1) {
1302 int nm[2];
1303 nm[0] = CTL_HW;
1304 nm[1] = HW_AVAILCPU;
1305 sysctl(nm, 2, &count, &len, NULL, 0);
1306 if (count < 1)
1307 return -1;
1308 }
1309 return count;
1310}
1311#else
1312// On other systems, return -1 to indicate unknown.
1313static int computeHostNumPhysicalCores() { return -1; }
1314#endif
1315
1316int sys::getHostNumPhysicalCores() {
1317 static int NumCores = computeHostNumPhysicalCores();
1318 return NumCores;
1319}
1320
1321#if defined(__i386__) || defined(_M_IX86) || \
1322 defined(__x86_64__) || defined(_M_X64)
1323bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
1324 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
1325 unsigned MaxLevel;
1326 union {
1327 unsigned u[3];
1328 char c[12];
1329 } text;
1330
1331 if (getX86CpuIDAndInfo(0, &MaxLevel, text.u + 0, text.u + 2, text.u + 1) ||
1332 MaxLevel < 1)
1333 return false;
1334
1335 getX86CpuIDAndInfo(1, &EAX, &EBX, &ECX, &EDX);
1336
1337 Features["cmov"] = (EDX >> 15) & 1;
1338 Features["mmx"] = (EDX >> 23) & 1;
1339 Features["sse"] = (EDX >> 25) & 1;
1340 Features["sse2"] = (EDX >> 26) & 1;
1341 Features["sse3"] = (ECX >> 0) & 1;
1342 Features["ssse3"] = (ECX >> 9) & 1;
1343 Features["sse4.1"] = (ECX >> 19) & 1;
1344 Features["sse4.2"] = (ECX >> 20) & 1;
1345
1346 Features["pclmul"] = (ECX >> 1) & 1;
1347 Features["cx16"] = (ECX >> 13) & 1;
1348 Features["movbe"] = (ECX >> 22) & 1;
1349 Features["popcnt"] = (ECX >> 23) & 1;
1350 Features["aes"] = (ECX >> 25) & 1;
1351 Features["rdrnd"] = (ECX >> 30) & 1;
1352
1353 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
1354 // indicates that the AVX registers will be saved and restored on context
1355 // switch, then we have full AVX support.
1356 bool HasAVXSave = ((ECX >> 27) & 1) && ((ECX >> 28) & 1) &&
1357 !getX86XCR0(&EAX, &EDX) && ((EAX & 0x6) == 0x6);
1358 Features["avx"] = HasAVXSave;
1359 Features["fma"] = HasAVXSave && (ECX >> 12) & 1;
1360 Features["f16c"] = HasAVXSave && (ECX >> 29) & 1;
1361
1362 // Only enable XSAVE if OS has enabled support for saving YMM state.
1363 Features["xsave"] = HasAVXSave && (ECX >> 26) & 1;
1364
1365 // AVX512 requires additional context to be saved by the OS.
1366 bool HasAVX512Save = HasAVXSave && ((EAX & 0xe0) == 0xe0);
1367
1368 unsigned MaxExtLevel;
1369 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
1370
1371 bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
1372 !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
1373 Features["lzcnt"] = HasExtLeaf1 && ((ECX >> 5) & 1);
1374 Features["sse4a"] = HasExtLeaf1 && ((ECX >> 6) & 1);
1375 Features["prfchw"] = HasExtLeaf1 && ((ECX >> 8) & 1);
1376 Features["xop"] = HasExtLeaf1 && ((ECX >> 11) & 1) && HasAVXSave;
Simon Pilgrim99b925b2017-05-03 15:51:39 +00001377 Features["lwp"] = HasExtLeaf1 && ((ECX >> 15) & 1);
Simon Pilgrima271c542017-05-03 15:42:29 +00001378 Features["fma4"] = HasExtLeaf1 && ((ECX >> 16) & 1) && HasAVXSave;
1379 Features["tbm"] = HasExtLeaf1 && ((ECX >> 21) & 1);
1380 Features["mwaitx"] = HasExtLeaf1 && ((ECX >> 29) & 1);
1381
1382 bool HasExtLeaf8 = MaxExtLevel >= 0x80000008 &&
1383 !getX86CpuIDAndInfoEx(0x80000008,0x0, &EAX, &EBX, &ECX, &EDX);
1384 Features["clzero"] = HasExtLeaf8 && ((EBX >> 0) & 1);
1385
1386 bool HasLeaf7 =
1387 MaxLevel >= 7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
1388
1389 // AVX2 is only supported if we have the OS save support from AVX.
1390 Features["avx2"] = HasAVXSave && HasLeaf7 && ((EBX >> 5) & 1);
1391
1392 Features["fsgsbase"] = HasLeaf7 && ((EBX >> 0) & 1);
1393 Features["sgx"] = HasLeaf7 && ((EBX >> 2) & 1);
1394 Features["bmi"] = HasLeaf7 && ((EBX >> 3) & 1);
1395 Features["bmi2"] = HasLeaf7 && ((EBX >> 8) & 1);
1396 Features["rtm"] = HasLeaf7 && ((EBX >> 11) & 1);
1397 Features["rdseed"] = HasLeaf7 && ((EBX >> 18) & 1);
1398 Features["adx"] = HasLeaf7 && ((EBX >> 19) & 1);
1399 Features["clflushopt"] = HasLeaf7 && ((EBX >> 23) & 1);
1400 Features["clwb"] = HasLeaf7 && ((EBX >> 24) & 1);
1401 Features["sha"] = HasLeaf7 && ((EBX >> 29) & 1);
1402
1403 // AVX512 is only supported if the OS supports the context save for it.
1404 Features["avx512f"] = HasLeaf7 && ((EBX >> 16) & 1) && HasAVX512Save;
1405 Features["avx512dq"] = HasLeaf7 && ((EBX >> 17) & 1) && HasAVX512Save;
1406 Features["avx512ifma"] = HasLeaf7 && ((EBX >> 21) & 1) && HasAVX512Save;
1407 Features["avx512pf"] = HasLeaf7 && ((EBX >> 26) & 1) && HasAVX512Save;
1408 Features["avx512er"] = HasLeaf7 && ((EBX >> 27) & 1) && HasAVX512Save;
1409 Features["avx512cd"] = HasLeaf7 && ((EBX >> 28) & 1) && HasAVX512Save;
1410 Features["avx512bw"] = HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save;
1411 Features["avx512vl"] = HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save;
1412
1413 Features["prefetchwt1"] = HasLeaf7 && (ECX & 1);
1414 Features["avx512vbmi"] = HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save;
Oren Ben Simhonf3aab2f2017-05-28 11:26:11 +00001415 Features["avx512vpopcntdq"] = HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save;
Simon Pilgrima271c542017-05-03 15:42:29 +00001416 // Enable protection keys
1417 Features["pku"] = HasLeaf7 && ((ECX >> 4) & 1);
1418
1419 bool HasLeafD = MaxLevel >= 0xd &&
1420 !getX86CpuIDAndInfoEx(0xd, 0x1, &EAX, &EBX, &ECX, &EDX);
1421
1422 // Only enable XSAVE if OS has enabled support for saving YMM state.
1423 Features["xsaveopt"] = HasAVXSave && HasLeafD && ((EAX >> 0) & 1);
1424 Features["xsavec"] = HasAVXSave && HasLeafD && ((EAX >> 1) & 1);
1425 Features["xsaves"] = HasAVXSave && HasLeafD && ((EAX >> 3) & 1);
1426
1427 return true;
1428}
1429#elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1430bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
1431 std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1432 if (!P)
1433 return false;
1434
1435 SmallVector<StringRef, 32> Lines;
1436 P->getBuffer().split(Lines, "\n");
1437
1438 SmallVector<StringRef, 32> CPUFeatures;
1439
1440 // Look for the CPU features.
1441 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
1442 if (Lines[I].startswith("Features")) {
1443 Lines[I].split(CPUFeatures, ' ');
1444 break;
1445 }
1446
1447#if defined(__aarch64__)
1448 // Keep track of which crypto features we have seen
1449 enum { CAP_AES = 0x1, CAP_PMULL = 0x2, CAP_SHA1 = 0x4, CAP_SHA2 = 0x8 };
1450 uint32_t crypto = 0;
1451#endif
1452
1453 for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) {
1454 StringRef LLVMFeatureStr = StringSwitch<StringRef>(CPUFeatures[I])
1455#if defined(__aarch64__)
1456 .Case("asimd", "neon")
1457 .Case("fp", "fp-armv8")
1458 .Case("crc32", "crc")
1459#else
1460 .Case("half", "fp16")
1461 .Case("neon", "neon")
1462 .Case("vfpv3", "vfp3")
1463 .Case("vfpv3d16", "d16")
1464 .Case("vfpv4", "vfp4")
1465 .Case("idiva", "hwdiv-arm")
1466 .Case("idivt", "hwdiv")
1467#endif
1468 .Default("");
1469
1470#if defined(__aarch64__)
1471 // We need to check crypto separately since we need all of the crypto
1472 // extensions to enable the subtarget feature
1473 if (CPUFeatures[I] == "aes")
1474 crypto |= CAP_AES;
1475 else if (CPUFeatures[I] == "pmull")
1476 crypto |= CAP_PMULL;
1477 else if (CPUFeatures[I] == "sha1")
1478 crypto |= CAP_SHA1;
1479 else if (CPUFeatures[I] == "sha2")
1480 crypto |= CAP_SHA2;
1481#endif
1482
1483 if (LLVMFeatureStr != "")
1484 Features[LLVMFeatureStr] = true;
1485 }
1486
1487#if defined(__aarch64__)
1488 // If we have all crypto bits we can add the feature
1489 if (crypto == (CAP_AES | CAP_PMULL | CAP_SHA1 | CAP_SHA2))
1490 Features["crypto"] = true;
1491#endif
1492
1493 return true;
1494}
1495#else
1496bool sys::getHostCPUFeatures(StringMap<bool> &Features) { return false; }
1497#endif
1498
1499std::string sys::getProcessTriple() {
Alex Lorenz3803df32017-07-07 09:53:47 +00001500 std::string TargetTripleString = updateTripleOSVersion(LLVM_HOST_TRIPLE);
1501 Triple PT(Triple::normalize(TargetTripleString));
Simon Pilgrima271c542017-05-03 15:42:29 +00001502
1503 if (sizeof(void *) == 8 && PT.isArch32Bit())
1504 PT = PT.get64BitArchVariant();
1505 if (sizeof(void *) == 4 && PT.isArch64Bit())
1506 PT = PT.get32BitArchVariant();
1507
1508 return PT.str();
1509}