blob: 689e05093fea3aa9f32edc75c6f57516122d7783 [file] [log] [blame]
Daniel Dunbare52e6bf2008-10-02 01:17:28 +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//
Hans Wennborgcfe341f2014-06-20 01:36:00 +000010// This file implements the operating system Host concept.
Daniel Dunbare52e6bf2008-10-02 01:17:28 +000011//
12//===----------------------------------------------------------------------===//
13
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/Support/Host.h"
Benjamin Kramerefe40282012-06-26 21:36:32 +000015#include "llvm/ADT/SmallVector.h"
Hal Finkel59b0ee82012-06-12 03:03:13 +000016#include "llvm/ADT/StringRef.h"
17#include "llvm/ADT/StringSwitch.h"
Peter Collingbournea51c6ed2013-01-16 17:27:22 +000018#include "llvm/ADT/Triple.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/Config/config.h"
Hal Finkel59b0ee82012-06-12 03:03:13 +000020#include "llvm/Support/Debug.h"
Rafael Espindola97935a92014-12-17 02:32:44 +000021#include "llvm/Support/FileSystem.h"
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000022#include "llvm/Support/raw_ostream.h"
23#include <string.h>
Alina Sbirlea33588b12016-07-20 18:15:29 +000024#include <assert.h>
Daniel Dunbare52e6bf2008-10-02 01:17:28 +000025
26// Include the platform-specific parts of this class.
27#ifdef LLVM_ON_UNIX
28#include "Unix/Host.inc"
29#endif
30#ifdef LLVM_ON_WIN32
Michael J. Spencer447762d2010-11-29 18:16:10 +000031#include "Windows/Host.inc"
Daniel Dunbare52e6bf2008-10-02 01:17:28 +000032#endif
Benjamin Kramer38465062009-11-19 12:17:31 +000033#ifdef _MSC_VER
34#include <intrin.h>
35#endif
Hal Finkel59b0ee82012-06-12 03:03:13 +000036#if defined(__APPLE__) && (defined(__ppc__) || defined(__powerpc__))
Eric Christopher5db9d662016-06-02 21:03:19 +000037#include <mach/host_info.h>
Hal Finkel59b0ee82012-06-12 03:03:13 +000038#include <mach/mach.h>
39#include <mach/mach_host.h>
Hal Finkel59b0ee82012-06-12 03:03:13 +000040#include <mach/machine.h>
41#endif
Daniel Dunbare52e6bf2008-10-02 01:17:28 +000042
Chandler Carruth66f38db2014-04-21 23:58:10 +000043#define DEBUG_TYPE "host-detection"
44
Daniel Dunbar241d01b2009-11-14 10:09:12 +000045//===----------------------------------------------------------------------===//
46//
47// Implementations of the CPU detection routines
48//
49//===----------------------------------------------------------------------===//
50
51using namespace llvm;
52
Rafael Espindola81adfb52014-12-17 02:42:20 +000053#if defined(__linux__)
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000054static ssize_t LLVM_ATTRIBUTE_UNUSED readCpuInfo(void *Buf, size_t Size) {
Rafael Espindola97935a92014-12-17 02:32:44 +000055 // Note: We cannot mmap /proc/cpuinfo here and then process the resulting
56 // memory buffer because the 'file' has 0 size (it can be read from only
57 // as a stream).
58
59 int FD;
60 std::error_code EC = sys::fs::openFileForRead("/proc/cpuinfo", FD);
61 if (EC) {
62 DEBUG(dbgs() << "Unable to open /proc/cpuinfo: " << EC.message() << "\n");
63 return -1;
64 }
65 int Ret = read(FD, Buf, Size);
66 int CloseStatus = close(FD);
67 if (CloseStatus)
68 return -1;
69 return Ret;
70}
Rafael Espindola81adfb52014-12-17 02:42:20 +000071#endif
Rafael Espindola97935a92014-12-17 02:32:44 +000072
Alina Sbirlea33588b12016-07-20 18:15:29 +000073#if defined(__i386__) || defined(_M_IX86) || \
74 defined(__x86_64__) || defined(_M_X64)
Daniel Dunbar241d01b2009-11-14 10:09:12 +000075
Alina Sbirleab86aa172016-07-20 18:54:26 +000076#if defined(__GNUC__) || defined(__clang__)
77#include <cpuid.h>
78#endif
79
Alina Sbirlea76c4a852016-06-06 18:29:59 +000080enum VendorSignatures {
81 SIG_INTEL = 0x756e6547 /* Genu */,
82 SIG_AMD = 0x68747541 /* Auth */
83};
84
85enum ProcessorVendors {
86 VENDOR_INTEL = 1,
87 VENDOR_AMD,
88 VENDOR_OTHER,
89 VENDOR_MAX
90};
91
92enum ProcessorTypes {
93 INTEL_ATOM = 1,
94 INTEL_CORE2,
95 INTEL_COREI7,
96 AMDFAM10H,
97 AMDFAM15H,
98 INTEL_i386,
99 INTEL_i486,
100 INTEL_PENTIUM,
101 INTEL_PENTIUM_PRO,
102 INTEL_PENTIUM_II,
103 INTEL_PENTIUM_III,
104 INTEL_PENTIUM_IV,
105 INTEL_PENTIUM_M,
106 INTEL_CORE_DUO,
107 INTEL_XEONPHI,
108 INTEL_X86_64,
109 INTEL_NOCONA,
110 INTEL_PRESCOTT,
111 AMD_i486,
112 AMDPENTIUM,
113 AMDATHLON,
114 AMDFAM14H,
115 AMDFAM16H,
116 CPU_TYPE_MAX
117};
118
119enum ProcessorSubtypes {
120 INTEL_COREI7_NEHALEM = 1,
121 INTEL_COREI7_WESTMERE,
122 INTEL_COREI7_SANDYBRIDGE,
123 AMDFAM10H_BARCELONA,
124 AMDFAM10H_SHANGHAI,
125 AMDFAM10H_ISTANBUL,
126 AMDFAM15H_BDVER1,
127 AMDFAM15H_BDVER2,
128 INTEL_PENTIUM_MMX,
129 INTEL_CORE2_65,
130 INTEL_CORE2_45,
131 INTEL_COREI7_IVYBRIDGE,
132 INTEL_COREI7_HASWELL,
133 INTEL_COREI7_BROADWELL,
134 INTEL_COREI7_SKYLAKE,
135 INTEL_COREI7_SKYLAKE_AVX512,
136 INTEL_ATOM_BONNELL,
137 INTEL_ATOM_SILVERMONT,
138 INTEL_KNIGHTS_LANDING,
139 AMDPENTIUM_K6,
140 AMDPENTIUM_K62,
141 AMDPENTIUM_K63,
142 AMDPENTIUM_GEODE,
143 AMDATHLON_TBIRD,
144 AMDATHLON_MP,
145 AMDATHLON_XP,
146 AMDATHLON_K8SSE3,
147 AMDATHLON_OPTERON,
148 AMDATHLON_FX,
149 AMDATHLON_64,
150 AMD_BTVER1,
151 AMD_BTVER2,
152 AMDFAM15H_BDVER3,
153 AMDFAM15H_BDVER4,
154 CPU_SUBTYPE_MAX
155};
156
157enum ProcessorFeatures {
158 FEATURE_CMOV = 0,
159 FEATURE_MMX,
160 FEATURE_POPCNT,
161 FEATURE_SSE,
162 FEATURE_SSE2,
163 FEATURE_SSE3,
164 FEATURE_SSSE3,
165 FEATURE_SSE4_1,
166 FEATURE_SSE4_2,
167 FEATURE_AVX,
168 FEATURE_AVX2,
169 FEATURE_AVX512,
170 FEATURE_AVX512SAVE,
171 FEATURE_MOVBE,
172 FEATURE_ADX,
173 FEATURE_EM64T
174};
175
Alina Sbirlea400eb022016-06-03 20:27:50 +0000176/// getX86CpuIDAndInfo - Execute the specified cpuid and return the 4 values in
Eric Christopherd9804bb2016-06-02 21:09:17 +0000177/// the specified arguments. If we can't run cpuid on the host, return true.
Alina Sbirlea400eb022016-06-03 20:27:50 +0000178static bool getX86CpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000179 unsigned *rECX, unsigned *rEDX) {
Alina Sbirlea33588b12016-07-20 18:15:29 +0000180#if defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)
Reid Klecknerbf4f9eb2013-08-16 22:42:42 +0000181#if defined(__GNUC__) || defined(__clang__)
Alina Sbirlea33588b12016-07-20 18:15:29 +0000182#if defined(__x86_64__)
183 // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
184 // FIXME: should we save this for Clang?
185 __asm__("movq\t%%rbx, %%rsi\n\t"
186 "cpuid\n\t"
187 "xchgq\t%%rbx, %%rsi\n\t"
188 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
189 : "a"(value));
190#elif defined(__i386__)
191 __asm__("movl\t%%ebx, %%esi\n\t"
192 "cpuid\n\t"
193 "xchgl\t%%ebx, %%esi\n\t"
194 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
195 : "a"(value));
Eric Christopher5db9d662016-06-02 21:03:19 +0000196#else
Alina Sbirlea33588b12016-07-20 18:15:29 +0000197 assert(0 && "This method is defined only for x86.");
Eric Christopher5db9d662016-06-02 21:03:19 +0000198#endif
Reid Klecknerbf4f9eb2013-08-16 22:42:42 +0000199#elif defined(_MSC_VER)
200 // The MSVC intrinsic is portable across x86 and x64.
201 int registers[4];
202 __cpuid(registers, value);
203 *rEAX = registers[0];
204 *rEBX = registers[1];
205 *rECX = registers[2];
206 *rEDX = registers[3];
Alina Sbirlea33588b12016-07-20 18:15:29 +0000207#endif
Reid Klecknerbf4f9eb2013-08-16 22:42:42 +0000208 return false;
David Blaikieb48ed1a2012-01-17 04:43:56 +0000209#else
Daniel Dunbar241d01b2009-11-14 10:09:12 +0000210 return true;
David Blaikieb48ed1a2012-01-17 04:43:56 +0000211#endif
Daniel Dunbar241d01b2009-11-14 10:09:12 +0000212}
213
Alina Sbirlea400eb022016-06-03 20:27:50 +0000214/// getX86CpuIDAndInfoEx - Execute the specified cpuid with subleaf and return
215/// the 4 values in the specified arguments. If we can't run cpuid on the host,
Tim Northover89ccb612013-11-25 09:52:59 +0000216/// return true.
Alina Sbirlea400eb022016-06-03 20:27:50 +0000217static bool getX86CpuIDAndInfoEx(unsigned value, unsigned subleaf,
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000218 unsigned *rEAX, unsigned *rEBX, unsigned *rECX,
219 unsigned *rEDX) {
Alina Sbirlea33588b12016-07-20 18:15:29 +0000220#if defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)
221#if defined(__x86_64__) || defined(_M_X64)
222#if defined(__GNUC__) || defined(__clang__)
Eric Christopher5db9d662016-06-02 21:03:19 +0000223 // gcc doesn't know cpuid would clobber ebx/rbx. Preseve it manually.
Alina Sbirlea33588b12016-07-20 18:15:29 +0000224 // FIXME: should we save this for Clang?
225 __asm__("movq\t%%rbx, %%rsi\n\t"
226 "cpuid\n\t"
227 "xchgq\t%%rbx, %%rsi\n\t"
228 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
229 : "a"(value), "c"(subleaf));
Eric Christopher5db9d662016-06-02 21:03:19 +0000230#elif defined(_MSC_VER)
231 int registers[4];
232 __cpuidex(registers, value, subleaf);
233 *rEAX = registers[0];
234 *rEBX = registers[1];
235 *rECX = registers[2];
236 *rEDX = registers[3];
Eric Christopher5db9d662016-06-02 21:03:19 +0000237#endif
Alina Sbirlea33588b12016-07-20 18:15:29 +0000238#elif defined(__i386__) || defined(_M_IX86)
239#if defined(__GNUC__) || defined(__clang__)
240 __asm__("movl\t%%ebx, %%esi\n\t"
241 "cpuid\n\t"
242 "xchgl\t%%ebx, %%esi\n\t"
243 : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
244 : "a"(value), "c"(subleaf));
Eric Christopher5db9d662016-06-02 21:03:19 +0000245#elif defined(_MSC_VER)
246 __asm {
Tim Northover89ccb612013-11-25 09:52:59 +0000247 mov eax,value
248 mov ecx,subleaf
249 cpuid
250 mov esi,rEAX
251 mov dword ptr [esi],eax
252 mov esi,rEBX
253 mov dword ptr [esi],ebx
254 mov esi,rECX
255 mov dword ptr [esi],ecx
256 mov esi,rEDX
257 mov dword ptr [esi],edx
Eric Christopher5db9d662016-06-02 21:03:19 +0000258 }
Eric Christopher5db9d662016-06-02 21:03:19 +0000259#endif
Tim Northover89ccb612013-11-25 09:52:59 +0000260#else
Alina Sbirlea33588b12016-07-20 18:15:29 +0000261 assert(0 && "This method is defined only for x86.");
262#endif
263 return false;
264#else
Tim Northover89ccb612013-11-25 09:52:59 +0000265 return true;
266#endif
267}
268
Alina Sbirlea400eb022016-06-03 20:27:50 +0000269static bool getX86XCR0(unsigned *rEAX, unsigned *rEDX) {
Alina Sbirlea33588b12016-07-20 18:15:29 +0000270#if defined(__GNUC__) || defined(__clang__)
Craig Topper7af39d72013-04-22 05:38:01 +0000271 // Check xgetbv; this uses a .byte sequence instead of the instruction
272 // directly because older assemblers do not include support for xgetbv and
273 // there is no easy way to conditionally compile based on the assembler used.
Eric Christopher5db9d662016-06-02 21:03:19 +0000274 __asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(*rEAX), "=d"(*rEDX) : "c"(0));
Craig Topper798a2602015-03-29 01:00:23 +0000275 return false;
Aaron Ballman31c0adc2013-04-23 17:38:44 +0000276#elif defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK)
Craig Topper798a2602015-03-29 01:00:23 +0000277 unsigned long long Result = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
Craig Topper7db49fd2015-03-29 01:07:57 +0000278 *rEAX = Result;
279 *rEDX = Result >> 32;
Craig Topper798a2602015-03-29 01:00:23 +0000280 return false;
Craig Topper7af39d72013-04-22 05:38:01 +0000281#else
Craig Topper798a2602015-03-29 01:00:23 +0000282 return true;
Craig Topper7af39d72013-04-22 05:38:01 +0000283#endif
Aaron Ballman5f7c6802013-04-03 12:25:06 +0000284}
285
Alina Sbirlea400eb022016-06-03 20:27:50 +0000286static void detectX86FamilyModel(unsigned EAX, unsigned *Family,
287 unsigned *Model) {
288 *Family = (EAX >> 8) & 0xf; // Bits 8 - 11
289 *Model = (EAX >> 4) & 0xf; // Bits 4 - 7
290 if (*Family == 6 || *Family == 0xf) {
291 if (*Family == 0xf)
Daniel Dunbar241d01b2009-11-14 10:09:12 +0000292 // Examine extended family ID if family ID is F.
Alina Sbirlea400eb022016-06-03 20:27:50 +0000293 *Family += (EAX >> 20) & 0xff; // Bits 20 - 27
Daniel Dunbar241d01b2009-11-14 10:09:12 +0000294 // Examine extended model ID if family ID is 6 or F.
Alina Sbirlea400eb022016-06-03 20:27:50 +0000295 *Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
Daniel Dunbar241d01b2009-11-14 10:09:12 +0000296 }
297}
Daniel Dunbar241d01b2009-11-14 10:09:12 +0000298
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000299static void
300getIntelProcessorTypeAndSubtype(unsigned int Family, unsigned int Model,
301 unsigned int Brand_id, unsigned int Features,
302 unsigned *Type, unsigned *Subtype) {
303 if (Brand_id != 0)
304 return;
305 switch (Family) {
306 case 3:
307 *Type = INTEL_i386;
308 break;
309 case 4:
310 switch (Model) {
311 case 0: // Intel486 DX processors
312 case 1: // Intel486 DX processors
313 case 2: // Intel486 SX processors
314 case 3: // Intel487 processors, IntelDX2 OverDrive processors,
315 // IntelDX2 processors
316 case 4: // Intel486 SL processor
317 case 5: // IntelSX2 processors
318 case 7: // Write-Back Enhanced IntelDX2 processors
319 case 8: // IntelDX4 OverDrive processors, IntelDX4 processors
320 default:
321 *Type = INTEL_i486;
322 break;
323 }
Alina Sbirlea080241b2016-06-09 00:08:15 +0000324 break;
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000325 case 5:
326 switch (Model) {
327 case 1: // Pentium OverDrive processor for Pentium processor (60, 66),
328 // Pentium processors (60, 66)
329 case 2: // Pentium OverDrive processor for Pentium processor (75, 90,
330 // 100, 120, 133), Pentium processors (75, 90, 100, 120, 133,
331 // 150, 166, 200)
332 case 3: // Pentium OverDrive processors for Intel486 processor-based
333 // systems
334 *Type = INTEL_PENTIUM;
335 break;
336 case 4: // Pentium OverDrive processor with MMX technology for Pentium
337 // processor (75, 90, 100, 120, 133), Pentium processor with
338 // MMX technology (166, 200)
339 *Type = INTEL_PENTIUM;
340 *Subtype = INTEL_PENTIUM_MMX;
341 break;
342 default:
343 *Type = INTEL_PENTIUM;
344 break;
345 }
Alina Sbirlea080241b2016-06-09 00:08:15 +0000346 break;
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000347 case 6:
348 switch (Model) {
349 case 0x01: // Pentium Pro processor
350 *Type = INTEL_PENTIUM_PRO;
351 break;
352 case 0x03: // Intel Pentium II OverDrive processor, Pentium II processor,
353 // model 03
354 case 0x05: // Pentium II processor, model 05, Pentium II Xeon processor,
355 // model 05, and Intel Celeron processor, model 05
356 case 0x06: // Celeron processor, model 06
357 *Type = INTEL_PENTIUM_II;
358 break;
359 case 0x07: // Pentium III processor, model 07, and Pentium III Xeon
360 // processor, model 07
361 case 0x08: // Pentium III processor, model 08, Pentium III Xeon processor,
362 // model 08, and Celeron processor, model 08
363 case 0x0a: // Pentium III Xeon processor, model 0Ah
364 case 0x0b: // Pentium III processor, model 0Bh
365 *Type = INTEL_PENTIUM_III;
366 break;
367 case 0x09: // Intel Pentium M processor, Intel Celeron M processor model 09.
368 case 0x0d: // Intel Pentium M processor, Intel Celeron M processor, model
369 // 0Dh. All processors are manufactured using the 90 nm process.
370 case 0x15: // Intel EP80579 Integrated Processor and Intel EP80579
371 // Integrated Processor with Intel QuickAssist Technology
372 *Type = INTEL_PENTIUM_M;
373 break;
374 case 0x0e: // Intel Core Duo processor, Intel Core Solo processor, model
375 // 0Eh. All processors are manufactured using the 65 nm process.
376 *Type = INTEL_CORE_DUO;
377 break; // yonah
378 case 0x0f: // Intel Core 2 Duo processor, Intel Core 2 Duo mobile
379 // processor, Intel Core 2 Quad processor, Intel Core 2 Quad
380 // mobile processor, Intel Core 2 Extreme processor, Intel
381 // Pentium Dual-Core processor, Intel Xeon processor, model
382 // 0Fh. All processors are manufactured using the 65 nm process.
383 case 0x16: // Intel Celeron processor model 16h. All processors are
384 // manufactured using the 65 nm process
385 *Type = INTEL_CORE2; // "core2"
386 *Subtype = INTEL_CORE2_65;
387 break;
388 case 0x17: // Intel Core 2 Extreme processor, Intel Xeon processor, model
389 // 17h. All processors are manufactured using the 45 nm process.
390 //
391 // 45nm: Penryn , Wolfdale, Yorkfield (XE)
392 case 0x1d: // Intel Xeon processor MP. All processors are manufactured using
393 // the 45 nm process.
394 *Type = INTEL_CORE2; // "penryn"
395 *Subtype = INTEL_CORE2_45;
396 break;
397 case 0x1a: // Intel Core i7 processor and Intel Xeon processor. All
398 // processors are manufactured using the 45 nm process.
399 case 0x1e: // Intel(R) Core(TM) i7 CPU 870 @ 2.93GHz.
400 // As found in a Summer 2010 model iMac.
401 case 0x1f:
402 case 0x2e: // Nehalem EX
403 *Type = INTEL_COREI7; // "nehalem"
404 *Subtype = INTEL_COREI7_NEHALEM;
405 break;
406 case 0x25: // Intel Core i7, laptop version.
407 case 0x2c: // Intel Core i7 processor and Intel Xeon processor. All
408 // processors are manufactured using the 32 nm process.
409 case 0x2f: // Westmere EX
410 *Type = INTEL_COREI7; // "westmere"
411 *Subtype = INTEL_COREI7_WESTMERE;
412 break;
413 case 0x2a: // Intel Core i7 processor. All processors are manufactured
414 // using the 32 nm process.
415 case 0x2d:
416 *Type = INTEL_COREI7; //"sandybridge"
417 *Subtype = INTEL_COREI7_SANDYBRIDGE;
418 break;
419 case 0x3a:
420 case 0x3e: // Ivy Bridge EP
421 *Type = INTEL_COREI7; // "ivybridge"
422 *Subtype = INTEL_COREI7_IVYBRIDGE;
423 break;
Daniel Dunbar241d01b2009-11-14 10:09:12 +0000424
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000425 // Haswell:
426 case 0x3c:
427 case 0x3f:
428 case 0x45:
429 case 0x46:
430 *Type = INTEL_COREI7; // "haswell"
431 *Subtype = INTEL_COREI7_HASWELL;
432 break;
Tim Northover89ccb612013-11-25 09:52:59 +0000433
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000434 // Broadwell:
435 case 0x3d:
436 case 0x47:
437 case 0x4f:
438 case 0x56:
439 *Type = INTEL_COREI7; // "broadwell"
440 *Subtype = INTEL_COREI7_BROADWELL;
441 break;
Tim Northover89ccb612013-11-25 09:52:59 +0000442
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000443 // Skylake:
444 case 0x4e:
445 *Type = INTEL_COREI7; // "skylake-avx512"
446 *Subtype = INTEL_COREI7_SKYLAKE_AVX512;
447 break;
448 case 0x5e:
449 *Type = INTEL_COREI7; // "skylake"
450 *Subtype = INTEL_COREI7_SKYLAKE;
451 break;
452
453 case 0x1c: // Most 45 nm Intel Atom processors
454 case 0x26: // 45 nm Atom Lincroft
455 case 0x27: // 32 nm Atom Medfield
456 case 0x35: // 32 nm Atom Midview
457 case 0x36: // 32 nm Atom Midview
458 *Type = INTEL_ATOM;
459 *Subtype = INTEL_ATOM_BONNELL;
460 break; // "bonnell"
461
462 // Atom Silvermont codes from the Intel software optimization guide.
463 case 0x37:
464 case 0x4a:
465 case 0x4d:
466 case 0x5a:
467 case 0x5d:
468 case 0x4c: // really airmont
469 *Type = INTEL_ATOM;
470 *Subtype = INTEL_ATOM_SILVERMONT;
471 break; // "silvermont"
472
473 case 0x57:
474 *Type = INTEL_XEONPHI; // knl
475 *Subtype = INTEL_KNIGHTS_LANDING;
476 break;
477
478 default: // Unknown family 6 CPU, try to guess.
479 if (Features & (1 << FEATURE_AVX512)) {
480 *Type = INTEL_XEONPHI; // knl
481 *Subtype = INTEL_KNIGHTS_LANDING;
482 break;
483 }
484 if (Features & (1 << FEATURE_ADX)) {
485 *Type = INTEL_COREI7;
486 *Subtype = INTEL_COREI7_BROADWELL;
487 break;
488 }
489 if (Features & (1 << FEATURE_AVX2)) {
490 *Type = INTEL_COREI7;
491 *Subtype = INTEL_COREI7_HASWELL;
492 break;
493 }
494 if (Features & (1 << FEATURE_AVX)) {
495 *Type = INTEL_COREI7;
496 *Subtype = INTEL_COREI7_SANDYBRIDGE;
497 break;
498 }
499 if (Features & (1 << FEATURE_SSE4_2)) {
500 if (Features & (1 << FEATURE_MOVBE)) {
501 *Type = INTEL_ATOM;
502 *Subtype = INTEL_ATOM_SILVERMONT;
503 } else {
504 *Type = INTEL_COREI7;
505 *Subtype = INTEL_COREI7_NEHALEM;
506 }
507 break;
508 }
509 if (Features & (1 << FEATURE_SSE4_1)) {
510 *Type = INTEL_CORE2; // "penryn"
511 *Subtype = INTEL_CORE2_45;
512 break;
513 }
514 if (Features & (1 << FEATURE_SSSE3)) {
515 if (Features & (1 << FEATURE_MOVBE)) {
516 *Type = INTEL_ATOM;
517 *Subtype = INTEL_ATOM_BONNELL; // "bonnell"
518 } else {
519 *Type = INTEL_CORE2; // "core2"
520 *Subtype = INTEL_CORE2_65;
521 }
522 break;
523 }
524 if (Features & (1 << FEATURE_EM64T)) {
525 *Type = INTEL_X86_64;
526 break; // x86-64
527 }
528 if (Features & (1 << FEATURE_SSE2)) {
529 *Type = INTEL_PENTIUM_M;
530 break;
531 }
532 if (Features & (1 << FEATURE_SSE)) {
533 *Type = INTEL_PENTIUM_III;
534 break;
535 }
536 if (Features & (1 << FEATURE_MMX)) {
537 *Type = INTEL_PENTIUM_II;
538 break;
539 }
540 *Type = INTEL_PENTIUM_PRO;
541 break;
542 }
Alina Sbirlea080241b2016-06-09 00:08:15 +0000543 break;
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000544 case 15: {
545 switch (Model) {
546 case 0: // Pentium 4 processor, Intel Xeon processor. All processors are
547 // model 00h and manufactured using the 0.18 micron process.
548 case 1: // Pentium 4 processor, Intel Xeon processor, Intel Xeon
549 // processor MP, and Intel Celeron processor. All processors are
550 // model 01h and manufactured using the 0.18 micron process.
551 case 2: // Pentium 4 processor, Mobile Intel Pentium 4 processor - M,
552 // Intel Xeon processor, Intel Xeon processor MP, Intel Celeron
553 // processor, and Mobile Intel Celeron processor. All processors
554 // are model 02h and manufactured using the 0.13 micron process.
555 *Type =
556 ((Features & (1 << FEATURE_EM64T)) ? INTEL_X86_64 : INTEL_PENTIUM_IV);
557 break;
558
559 case 3: // Pentium 4 processor, Intel Xeon processor, Intel Celeron D
560 // processor. All processors are model 03h and manufactured using
561 // the 90 nm process.
562 case 4: // Pentium 4 processor, Pentium 4 processor Extreme Edition,
563 // Pentium D processor, Intel Xeon processor, Intel Xeon
564 // processor MP, Intel Celeron D processor. All processors are
565 // model 04h and manufactured using the 90 nm process.
566 case 6: // Pentium 4 processor, Pentium D processor, Pentium processor
567 // Extreme Edition, Intel Xeon processor, Intel Xeon processor
568 // MP, Intel Celeron D processor. All processors are model 06h
569 // and manufactured using the 65 nm process.
570 *Type =
571 ((Features & (1 << FEATURE_EM64T)) ? INTEL_NOCONA : INTEL_PRESCOTT);
572 break;
573
574 default:
575 *Type =
576 ((Features & (1 << FEATURE_EM64T)) ? INTEL_X86_64 : INTEL_PENTIUM_IV);
577 break;
578 }
Alina Sbirlea080241b2016-06-09 00:08:15 +0000579 break;
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000580 }
581 default:
582 break; /*"generic"*/
583 }
584}
585
586static void getAMDProcessorTypeAndSubtype(unsigned int Family,
587 unsigned int Model,
588 unsigned int Features,
589 unsigned *Type,
590 unsigned *Subtype) {
591 // FIXME: this poorly matches the generated SubtargetFeatureKV table. There
592 // appears to be no way to generate the wide variety of AMD-specific targets
593 // from the information returned from CPUID.
594 switch (Family) {
595 case 4:
596 *Type = AMD_i486;
Alina Sbirlea080241b2016-06-09 00:08:15 +0000597 break;
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000598 case 5:
599 *Type = AMDPENTIUM;
600 switch (Model) {
601 case 6:
602 case 7:
603 *Subtype = AMDPENTIUM_K6;
604 break; // "k6"
605 case 8:
606 *Subtype = AMDPENTIUM_K62;
607 break; // "k6-2"
608 case 9:
609 case 13:
610 *Subtype = AMDPENTIUM_K63;
611 break; // "k6-3"
612 case 10:
613 *Subtype = AMDPENTIUM_GEODE;
614 break; // "geode"
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000615 }
Alina Sbirlea080241b2016-06-09 00:08:15 +0000616 break;
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000617 case 6:
618 *Type = AMDATHLON;
619 switch (Model) {
620 case 4:
621 *Subtype = AMDATHLON_TBIRD;
622 break; // "athlon-tbird"
623 case 6:
624 case 7:
625 case 8:
626 *Subtype = AMDATHLON_MP;
627 break; // "athlon-mp"
628 case 10:
629 *Subtype = AMDATHLON_XP;
630 break; // "athlon-xp"
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000631 }
Alina Sbirlea080241b2016-06-09 00:08:15 +0000632 break;
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000633 case 15:
634 *Type = AMDATHLON;
635 if (Features & (1 << FEATURE_SSE3)) {
636 *Subtype = AMDATHLON_K8SSE3;
637 break; // "k8-sse3"
638 }
639 switch (Model) {
640 case 1:
641 *Subtype = AMDATHLON_OPTERON;
642 break; // "opteron"
643 case 5:
644 *Subtype = AMDATHLON_FX;
645 break; // "athlon-fx"; also opteron
646 default:
647 *Subtype = AMDATHLON_64;
648 break; // "athlon64"
649 }
Alina Sbirlea080241b2016-06-09 00:08:15 +0000650 break;
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000651 case 16:
652 *Type = AMDFAM10H; // "amdfam10"
653 switch (Model) {
654 case 2:
655 *Subtype = AMDFAM10H_BARCELONA;
656 break;
657 case 4:
658 *Subtype = AMDFAM10H_SHANGHAI;
659 break;
660 case 8:
661 *Subtype = AMDFAM10H_ISTANBUL;
662 break;
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000663 }
Alina Sbirlea080241b2016-06-09 00:08:15 +0000664 break;
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000665 case 20:
666 *Type = AMDFAM14H;
667 *Subtype = AMD_BTVER1;
668 break; // "btver1";
669 case 21:
670 *Type = AMDFAM15H;
671 if (!(Features &
672 (1 << FEATURE_AVX))) { // If no AVX support, provide a sane fallback.
673 *Subtype = AMD_BTVER1;
674 break; // "btver1"
675 }
676 if (Model >= 0x50 && Model <= 0x6f) {
677 *Subtype = AMDFAM15H_BDVER4;
678 break; // "bdver4"; 50h-6Fh: Excavator
679 }
680 if (Model >= 0x30 && Model <= 0x3f) {
681 *Subtype = AMDFAM15H_BDVER3;
682 break; // "bdver3"; 30h-3Fh: Steamroller
683 }
684 if (Model >= 0x10 && Model <= 0x1f) {
685 *Subtype = AMDFAM15H_BDVER2;
686 break; // "bdver2"; 10h-1Fh: Piledriver
687 }
688 if (Model <= 0x0f) {
689 *Subtype = AMDFAM15H_BDVER1;
690 break; // "bdver1"; 00h-0Fh: Bulldozer
691 }
692 break;
693 case 22:
694 *Type = AMDFAM16H;
695 if (!(Features &
696 (1 << FEATURE_AVX))) { // If no AVX support provide a sane fallback.
697 *Subtype = AMD_BTVER1;
698 break; // "btver1";
699 }
700 *Subtype = AMD_BTVER2;
701 break; // "btver2"
702 default:
703 break; // "generic"
704 }
705}
706
Benjamin Kramerb308f8b2016-07-10 16:11:53 +0000707static unsigned getAvailableFeatures(unsigned int ECX, unsigned int EDX,
708 unsigned MaxLeaf) {
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000709 unsigned Features = 0;
710 unsigned int EAX, EBX;
711 Features |= (((EDX >> 23) & 1) << FEATURE_MMX);
712 Features |= (((EDX >> 25) & 1) << FEATURE_SSE);
713 Features |= (((EDX >> 26) & 1) << FEATURE_SSE2);
714 Features |= (((ECX >> 0) & 1) << FEATURE_SSE3);
715 Features |= (((ECX >> 9) & 1) << FEATURE_SSSE3);
716 Features |= (((ECX >> 19) & 1) << FEATURE_SSE4_1);
717 Features |= (((ECX >> 20) & 1) << FEATURE_SSE4_2);
718 Features |= (((ECX >> 22) & 1) << FEATURE_MOVBE);
719
Craig Topper1214bdc2015-03-31 05:42:45 +0000720 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
Aaron Ballman5f7c6802013-04-03 12:25:06 +0000721 // indicates that the AVX registers will be saved and restored on context
722 // switch, then we have full AVX support.
Aaron Ballman5e6d2052013-04-03 18:00:22 +0000723 const unsigned AVXBits = (1 << 27) | (1 << 28);
Alina Sbirlea400eb022016-06-03 20:27:50 +0000724 bool HasAVX = ((ECX & AVXBits) == AVXBits) && !getX86XCR0(&EAX, &EDX) &&
Craig Topper798a2602015-03-29 01:00:23 +0000725 ((EAX & 0x6) == 0x6);
Craig Topper1214bdc2015-03-31 05:42:45 +0000726 bool HasAVX512Save = HasAVX && ((EAX & 0xe0) == 0xe0);
Eric Christopher5db9d662016-06-02 21:03:19 +0000727 bool HasLeaf7 =
Alina Sbirlea400eb022016-06-03 20:27:50 +0000728 MaxLeaf >= 0x7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
Craig Topper1214bdc2015-03-31 05:42:45 +0000729 bool HasADX = HasLeaf7 && ((EBX >> 19) & 1);
730 bool HasAVX2 = HasAVX && HasLeaf7 && (EBX & 0x20);
731 bool HasAVX512 = HasLeaf7 && HasAVX512Save && ((EBX >> 16) & 1);
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000732 Features |= (HasAVX << FEATURE_AVX);
733 Features |= (HasAVX2 << FEATURE_AVX2);
734 Features |= (HasAVX512 << FEATURE_AVX512);
735 Features |= (HasAVX512Save << FEATURE_AVX512SAVE);
736 Features |= (HasADX << FEATURE_ADX);
Craig Topper1214bdc2015-03-31 05:42:45 +0000737
Alina Sbirlea400eb022016-06-03 20:27:50 +0000738 getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000739 Features |= (((EDX >> 29) & 0x1) << FEATURE_EM64T);
740 return Features;
741}
Daniel Dunbar241d01b2009-11-14 10:09:12 +0000742
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000743StringRef sys::getHostCPUName() {
744 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
745 unsigned MaxLeaf, Vendor;
746
Alina Sbirleab86aa172016-07-20 18:54:26 +0000747#if defined(__GNUC__) || defined(__clang__)
Alina Sbirlea33588b12016-07-20 18:15:29 +0000748 //FIXME: include cpuid.h from clang or copy __get_cpuid_max here
749 // and simplify it to not invoke __cpuid (like cpu_model.c in
750 // compiler-rt/lib/builtins/cpu_model.c?
751 if(!__get_cpuid_max(0, &Vendor))
752 return "generic";
Alina Sbirleab86aa172016-07-20 18:54:26 +0000753#endif
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000754 if (getX86CpuIDAndInfo(0, &MaxLeaf, &Vendor, &ECX, &EDX))
755 return "generic";
756 if (getX86CpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX))
757 return "generic";
758
759 unsigned Brand_id = EBX & 0xff;
760 unsigned Family = 0, Model = 0;
761 unsigned Features = 0;
762 detectX86FamilyModel(EAX, &Family, &Model);
763 Features = getAvailableFeatures(ECX, EDX, MaxLeaf);
764
765 unsigned Type;
766 unsigned Subtype;
767
768 if (Vendor == SIG_INTEL) {
769 getIntelProcessorTypeAndSubtype(Family, Model, Brand_id, Features, &Type,
770 &Subtype);
771 switch (Type) {
772 case INTEL_i386:
Daniel Dunbar397235f2009-11-14 21:36:19 +0000773 return "i386";
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000774 case INTEL_i486:
775 return "i486";
776 case INTEL_PENTIUM:
777 if (Subtype == INTEL_PENTIUM_MMX)
Daniel Dunbar397235f2009-11-14 21:36:19 +0000778 return "pentium-mmx";
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000779 return "pentium";
780 case INTEL_PENTIUM_PRO:
781 return "pentiumpro";
782 case INTEL_PENTIUM_II:
783 return "pentium2";
784 case INTEL_PENTIUM_III:
785 return "pentium3";
786 case INTEL_PENTIUM_IV:
787 return "pentium4";
788 case INTEL_PENTIUM_M:
789 return "pentium-m";
790 case INTEL_CORE_DUO:
791 return "yonah";
792 case INTEL_CORE2:
793 switch (Subtype) {
794 case INTEL_CORE2_65:
Daniel Dunbar397235f2009-11-14 21:36:19 +0000795 return "core2";
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000796 case INTEL_CORE2_45:
Craig Topper545b9512015-03-31 06:18:31 +0000797 return "penryn";
Daniel Dunbar397235f2009-11-14 21:36:19 +0000798 default:
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000799 return "core2";
Daniel Dunbar397235f2009-11-14 21:36:19 +0000800 }
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000801 case INTEL_COREI7:
802 switch (Subtype) {
803 case INTEL_COREI7_NEHALEM:
804 return "nehalem";
805 case INTEL_COREI7_WESTMERE:
806 return "westmere";
807 case INTEL_COREI7_SANDYBRIDGE:
808 return "sandybridge";
809 case INTEL_COREI7_IVYBRIDGE:
810 return "ivybridge";
811 case INTEL_COREI7_HASWELL:
812 return "haswell";
813 case INTEL_COREI7_BROADWELL:
814 return "broadwell";
815 case INTEL_COREI7_SKYLAKE:
816 return "skylake";
817 case INTEL_COREI7_SKYLAKE_AVX512:
818 return "skylake-avx512";
819 default:
820 return "corei7";
821 }
822 case INTEL_ATOM:
823 switch (Subtype) {
824 case INTEL_ATOM_BONNELL:
825 return "bonnell";
826 case INTEL_ATOM_SILVERMONT:
827 return "silvermont";
828 default:
829 return "atom";
830 }
831 case INTEL_XEONPHI:
832 return "knl"; /*update for more variants added*/
833 case INTEL_X86_64:
834 return "x86-64";
835 case INTEL_NOCONA:
836 return "nocona";
837 case INTEL_PRESCOTT:
838 return "prescott";
Daniel Dunbar241d01b2009-11-14 10:09:12 +0000839 default:
Benjamin Kramer713fd352009-11-17 17:57:04 +0000840 return "generic";
Daniel Dunbar241d01b2009-11-14 10:09:12 +0000841 }
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000842 } else if (Vendor == SIG_AMD) {
843 getAMDProcessorTypeAndSubtype(Family, Model, Features, &Type, &Subtype);
844 switch (Type) {
845 case AMD_i486:
Eric Christopher5db9d662016-06-02 21:03:19 +0000846 return "i486";
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000847 case AMDPENTIUM:
848 switch (Subtype) {
849 case AMDPENTIUM_K6:
Eric Christopher5db9d662016-06-02 21:03:19 +0000850 return "k6";
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000851 case AMDPENTIUM_K62:
Eric Christopher5db9d662016-06-02 21:03:19 +0000852 return "k6-2";
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000853 case AMDPENTIUM_K63:
Eric Christopher5db9d662016-06-02 21:03:19 +0000854 return "k6-3";
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000855 case AMDPENTIUM_GEODE:
Eric Christopher5db9d662016-06-02 21:03:19 +0000856 return "geode";
857 default:
858 return "pentium";
859 }
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000860 case AMDATHLON:
861 switch (Subtype) {
862 case AMDATHLON_TBIRD:
Eric Christopher5db9d662016-06-02 21:03:19 +0000863 return "athlon-tbird";
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000864 case AMDATHLON_MP:
Eric Christopher5db9d662016-06-02 21:03:19 +0000865 return "athlon-mp";
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000866 case AMDATHLON_XP:
Eric Christopher5db9d662016-06-02 21:03:19 +0000867 return "athlon-xp";
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000868 case AMDATHLON_K8SSE3:
869 return "k8-sse3";
870 case AMDATHLON_OPTERON:
871 return "opteron";
872 case AMDATHLON_FX:
873 return "athlon-fx";
874 case AMDATHLON_64:
875 return "athlon64";
Eric Christopher5db9d662016-06-02 21:03:19 +0000876 default:
877 return "athlon";
878 }
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000879 case AMDFAM10H:
Alina Sbirlead665b412016-06-09 23:04:15 +0000880 if(Subtype == AMDFAM10H_BARCELONA)
881 return "barcelona";
882 return "amdfam10";
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000883 case AMDFAM14H:
Eric Christopher5db9d662016-06-02 21:03:19 +0000884 return "btver1";
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000885 case AMDFAM15H:
886 switch (Subtype) {
887 case AMDFAM15H_BDVER1:
888 return "bdver1";
889 case AMDFAM15H_BDVER2:
890 return "bdver2";
891 case AMDFAM15H_BDVER3:
892 return "bdver3";
893 case AMDFAM15H_BDVER4:
894 return "bdver4";
895 case AMD_BTVER1:
Benjamin Kramer077ae1d2012-01-10 11:50:02 +0000896 return "btver1";
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000897 default:
898 return "amdfam15";
899 }
900 case AMDFAM16H:
901 switch (Subtype) {
902 case AMD_BTVER1:
Eric Christopher5db9d662016-06-02 21:03:19 +0000903 return "btver1";
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000904 case AMD_BTVER2:
905 return "btver2";
906 default:
907 return "amdfam16";
908 }
Daniel Dunbar241d01b2009-11-14 10:09:12 +0000909 default:
Benjamin Kramer713fd352009-11-17 17:57:04 +0000910 return "generic";
Daniel Dunbar241d01b2009-11-14 10:09:12 +0000911 }
Daniel Dunbar241d01b2009-11-14 10:09:12 +0000912 }
Torok Edwin022336a2009-12-14 12:38:18 +0000913 return "generic";
Torok Edwinabdc1c22009-12-13 08:59:40 +0000914}
Alina Sbirlea76c4a852016-06-06 18:29:59 +0000915
Hal Finkel59b0ee82012-06-12 03:03:13 +0000916#elif defined(__APPLE__) && (defined(__ppc__) || defined(__powerpc__))
Rafael Espindola1f58e4d2013-12-12 16:10:48 +0000917StringRef sys::getHostCPUName() {
Hal Finkel59b0ee82012-06-12 03:03:13 +0000918 host_basic_info_data_t hostInfo;
919 mach_msg_type_number_t infoCount;
920
921 infoCount = HOST_BASIC_INFO_COUNT;
Eric Christopher5db9d662016-06-02 21:03:19 +0000922 host_info(mach_host_self(), HOST_BASIC_INFO, (host_info_t)&hostInfo,
Hal Finkel59b0ee82012-06-12 03:03:13 +0000923 &infoCount);
Hal Finkel59b0ee82012-06-12 03:03:13 +0000924
Eric Christopher5db9d662016-06-02 21:03:19 +0000925 if (hostInfo.cpu_type != CPU_TYPE_POWERPC)
926 return "generic";
927
928 switch (hostInfo.cpu_subtype) {
929 case CPU_SUBTYPE_POWERPC_601:
930 return "601";
931 case CPU_SUBTYPE_POWERPC_602:
932 return "602";
933 case CPU_SUBTYPE_POWERPC_603:
934 return "603";
935 case CPU_SUBTYPE_POWERPC_603e:
936 return "603e";
937 case CPU_SUBTYPE_POWERPC_603ev:
938 return "603ev";
939 case CPU_SUBTYPE_POWERPC_604:
940 return "604";
941 case CPU_SUBTYPE_POWERPC_604e:
942 return "604e";
943 case CPU_SUBTYPE_POWERPC_620:
944 return "620";
945 case CPU_SUBTYPE_POWERPC_750:
946 return "750";
947 case CPU_SUBTYPE_POWERPC_7400:
948 return "7400";
949 case CPU_SUBTYPE_POWERPC_7450:
950 return "7450";
951 case CPU_SUBTYPE_POWERPC_970:
952 return "970";
953 default:;
Hal Finkel59b0ee82012-06-12 03:03:13 +0000954 }
Eric Christopher5db9d662016-06-02 21:03:19 +0000955
Hal Finkel59b0ee82012-06-12 03:03:13 +0000956 return "generic";
957}
958#elif defined(__linux__) && (defined(__ppc__) || defined(__powerpc__))
Rafael Espindolab75ea012013-12-12 16:17:40 +0000959StringRef sys::getHostCPUName() {
Hal Finkel59b0ee82012-06-12 03:03:13 +0000960 // Access to the Processor Version Register (PVR) on PowerPC is privileged,
961 // and so we must use an operating-system interface to determine the current
962 // processor type. On Linux, this is exposed through the /proc/cpuinfo file.
963 const char *generic = "generic";
964
Hal Finkel59b0ee82012-06-12 03:03:13 +0000965 // The cpu line is second (after the 'processor: 0' line), so if this
966 // buffer is too small then something has changed (or is wrong).
967 char buffer[1024];
Rafael Espindola97935a92014-12-17 02:32:44 +0000968 ssize_t CPUInfoSize = readCpuInfo(buffer, sizeof(buffer));
969 if (CPUInfoSize == -1)
970 return generic;
Hal Finkel59b0ee82012-06-12 03:03:13 +0000971
972 const char *CPUInfoStart = buffer;
973 const char *CPUInfoEnd = buffer + CPUInfoSize;
974
975 const char *CIP = CPUInfoStart;
976
977 const char *CPUStart = 0;
978 size_t CPULen = 0;
979
980 // We need to find the first line which starts with cpu, spaces, and a colon.
981 // After the colon, there may be some additional spaces and then the cpu type.
982 while (CIP < CPUInfoEnd && CPUStart == 0) {
983 if (CIP < CPUInfoEnd && *CIP == '\n')
984 ++CIP;
985
986 if (CIP < CPUInfoEnd && *CIP == 'c') {
987 ++CIP;
988 if (CIP < CPUInfoEnd && *CIP == 'p') {
989 ++CIP;
990 if (CIP < CPUInfoEnd && *CIP == 'u') {
991 ++CIP;
992 while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t'))
993 ++CIP;
Eric Christopher5db9d662016-06-02 21:03:19 +0000994
Hal Finkel59b0ee82012-06-12 03:03:13 +0000995 if (CIP < CPUInfoEnd && *CIP == ':') {
996 ++CIP;
997 while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t'))
998 ++CIP;
Eric Christopher5db9d662016-06-02 21:03:19 +0000999
Hal Finkel59b0ee82012-06-12 03:03:13 +00001000 if (CIP < CPUInfoEnd) {
1001 CPUStart = CIP;
1002 while (CIP < CPUInfoEnd && (*CIP != ' ' && *CIP != '\t' &&
1003 *CIP != ',' && *CIP != '\n'))
1004 ++CIP;
1005 CPULen = CIP - CPUStart;
1006 }
1007 }
1008 }
1009 }
1010 }
1011
1012 if (CPUStart == 0)
1013 while (CIP < CPUInfoEnd && *CIP != '\n')
1014 ++CIP;
1015 }
1016
1017 if (CPUStart == 0)
1018 return generic;
1019
1020 return StringSwitch<const char *>(StringRef(CPUStart, CPULen))
Eric Christopher5db9d662016-06-02 21:03:19 +00001021 .Case("604e", "604e")
1022 .Case("604", "604")
1023 .Case("7400", "7400")
1024 .Case("7410", "7400")
1025 .Case("7447", "7400")
1026 .Case("7455", "7450")
1027 .Case("G4", "g4")
1028 .Case("POWER4", "970")
1029 .Case("PPC970FX", "970")
1030 .Case("PPC970MP", "970")
1031 .Case("G5", "g5")
1032 .Case("POWER5", "g5")
1033 .Case("A2", "a2")
1034 .Case("POWER6", "pwr6")
1035 .Case("POWER7", "pwr7")
1036 .Case("POWER8", "pwr8")
1037 .Case("POWER8E", "pwr8")
1038 .Case("POWER9", "pwr9")
1039 .Default(generic);
Hal Finkel59b0ee82012-06-12 03:03:13 +00001040}
Benjamin Kramerefe40282012-06-26 21:36:32 +00001041#elif defined(__linux__) && defined(__arm__)
Rafael Espindola1f58e4d2013-12-12 16:10:48 +00001042StringRef sys::getHostCPUName() {
Benjamin Kramerefe40282012-06-26 21:36:32 +00001043 // The cpuid register on arm is not accessible from user space. On Linux,
1044 // it is exposed through the /proc/cpuinfo file.
Benjamin Kramerefe40282012-06-26 21:36:32 +00001045
1046 // Read 1024 bytes from /proc/cpuinfo, which should contain the CPU part line
1047 // in all cases.
1048 char buffer[1024];
Rafael Espindola97935a92014-12-17 02:32:44 +00001049 ssize_t CPUInfoSize = readCpuInfo(buffer, sizeof(buffer));
1050 if (CPUInfoSize == -1)
1051 return "generic";
Benjamin Kramerefe40282012-06-26 21:36:32 +00001052
1053 StringRef Str(buffer, CPUInfoSize);
1054
1055 SmallVector<StringRef, 32> Lines;
1056 Str.split(Lines, "\n");
1057
1058 // Look for the CPU implementer line.
1059 StringRef Implementer;
1060 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
1061 if (Lines[I].startswith("CPU implementer"))
1062 Implementer = Lines[I].substr(15).ltrim("\t :");
1063
1064 if (Implementer == "0x41") // ARM Ltd.
1065 // Look for the CPU part line.
1066 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
1067 if (Lines[I].startswith("CPU part"))
1068 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
1069 // values correspond to the "Part number" in the CP15/c0 register. The
1070 // contents are specified in the various processor manuals.
1071 return StringSwitch<const char *>(Lines[I].substr(8).ltrim("\t :"))
Eric Christopher5db9d662016-06-02 21:03:19 +00001072 .Case("0x926", "arm926ej-s")
1073 .Case("0xb02", "mpcore")
1074 .Case("0xb36", "arm1136j-s")
1075 .Case("0xb56", "arm1156t2-s")
1076 .Case("0xb76", "arm1176jz-s")
1077 .Case("0xc08", "cortex-a8")
1078 .Case("0xc09", "cortex-a9")
1079 .Case("0xc0f", "cortex-a15")
1080 .Case("0xc20", "cortex-m0")
1081 .Case("0xc23", "cortex-m3")
1082 .Case("0xc24", "cortex-m4")
1083 .Default("generic");
Benjamin Kramerefe40282012-06-26 21:36:32 +00001084
Kai Nackeb38bf962013-12-20 09:24:13 +00001085 if (Implementer == "0x51") // Qualcomm Technologies, Inc.
1086 // Look for the CPU part line.
1087 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
1088 if (Lines[I].startswith("CPU part"))
1089 // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
1090 // values correspond to the "Part number" in the CP15/c0 register. The
1091 // contents are specified in the various processor manuals.
1092 return StringSwitch<const char *>(Lines[I].substr(8).ltrim("\t :"))
Eric Christopher5db9d662016-06-02 21:03:19 +00001093 .Case("0x06f", "krait") // APQ8064
1094 .Default("generic");
Kai Nackeb38bf962013-12-20 09:24:13 +00001095
Benjamin Kramerefe40282012-06-26 21:36:32 +00001096 return "generic";
1097}
Richard Sandifordf834ea12013-10-31 12:14:17 +00001098#elif defined(__linux__) && defined(__s390x__)
Rafael Espindola1f58e4d2013-12-12 16:10:48 +00001099StringRef sys::getHostCPUName() {
Richard Sandifordf834ea12013-10-31 12:14:17 +00001100 // STIDP is a privileged operation, so use /proc/cpuinfo instead.
Richard Sandifordf834ea12013-10-31 12:14:17 +00001101
1102 // The "processor 0:" line comes after a fair amount of other information,
1103 // including a cache breakdown, but this should be plenty.
1104 char buffer[2048];
Rafael Espindola97935a92014-12-17 02:32:44 +00001105 ssize_t CPUInfoSize = readCpuInfo(buffer, sizeof(buffer));
1106 if (CPUInfoSize == -1)
1107 return "generic";
Richard Sandifordf834ea12013-10-31 12:14:17 +00001108
1109 StringRef Str(buffer, CPUInfoSize);
1110 SmallVector<StringRef, 32> Lines;
1111 Str.split(Lines, "\n");
Ulrich Weiganda8b04e12015-05-05 19:23:40 +00001112
1113 // Look for the CPU features.
1114 SmallVector<StringRef, 32> CPUFeatures;
1115 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
1116 if (Lines[I].startswith("features")) {
1117 size_t Pos = Lines[I].find(":");
1118 if (Pos != StringRef::npos) {
Chandler Carruthe4405e92015-09-10 06:12:31 +00001119 Lines[I].drop_front(Pos + 1).split(CPUFeatures, ' ');
Ulrich Weiganda8b04e12015-05-05 19:23:40 +00001120 break;
1121 }
1122 }
1123
1124 // We need to check for the presence of vector support independently of
1125 // the machine type, since we may only use the vector register set when
1126 // supported by the kernel (and hypervisor).
1127 bool HaveVectorSupport = false;
1128 for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) {
1129 if (CPUFeatures[I] == "vx")
1130 HaveVectorSupport = true;
1131 }
1132
1133 // Now check the processor machine type.
Richard Sandifordf834ea12013-10-31 12:14:17 +00001134 for (unsigned I = 0, E = Lines.size(); I != E; ++I) {
1135 if (Lines[I].startswith("processor ")) {
1136 size_t Pos = Lines[I].find("machine = ");
1137 if (Pos != StringRef::npos) {
1138 Pos += sizeof("machine = ") - 1;
1139 unsigned int Id;
1140 if (!Lines[I].drop_front(Pos).getAsInteger(10, Id)) {
Ulrich Weiganda8b04e12015-05-05 19:23:40 +00001141 if (Id >= 2964 && HaveVectorSupport)
1142 return "z13";
Richard Sandifordf834ea12013-10-31 12:14:17 +00001143 if (Id >= 2827)
1144 return "zEC12";
1145 if (Id >= 2817)
1146 return "z196";
1147 }
1148 }
1149 break;
1150 }
1151 }
Eric Christopher5db9d662016-06-02 21:03:19 +00001152
Richard Sandifordf834ea12013-10-31 12:14:17 +00001153 return "generic";
1154}
Torok Edwinabdc1c22009-12-13 08:59:40 +00001155#else
Eric Christopher5db9d662016-06-02 21:03:19 +00001156StringRef sys::getHostCPUName() { return "generic"; }
Torok Edwinabdc1c22009-12-13 08:59:40 +00001157#endif
Xerxes Ranby17dc3a02010-01-19 21:26:05 +00001158
Alina Sbirlea33588b12016-07-20 18:15:29 +00001159#if defined(__i386__) || defined(_M_IX86) || \
1160 defined(__x86_64__) || defined(_M_X64)
Craig Topper798a2602015-03-29 01:00:23 +00001161bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
1162 unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
1163 unsigned MaxLevel;
1164 union {
1165 unsigned u[3];
Eric Christopher5db9d662016-06-02 21:03:19 +00001166 char c[12];
Craig Topper798a2602015-03-29 01:00:23 +00001167 } text;
1168
Alina Sbirlea400eb022016-06-03 20:27:50 +00001169 if (getX86CpuIDAndInfo(0, &MaxLevel, text.u + 0, text.u + 2, text.u + 1) ||
Craig Topper798a2602015-03-29 01:00:23 +00001170 MaxLevel < 1)
1171 return false;
1172
Alina Sbirlea400eb022016-06-03 20:27:50 +00001173 getX86CpuIDAndInfo(1, &EAX, &EBX, &ECX, &EDX);
Craig Topper798a2602015-03-29 01:00:23 +00001174
Eric Christopher5db9d662016-06-02 21:03:19 +00001175 Features["cmov"] = (EDX >> 15) & 1;
1176 Features["mmx"] = (EDX >> 23) & 1;
1177 Features["sse"] = (EDX >> 25) & 1;
1178 Features["sse2"] = (EDX >> 26) & 1;
1179 Features["sse3"] = (ECX >> 0) & 1;
1180 Features["ssse3"] = (ECX >> 9) & 1;
Craig Topper798a2602015-03-29 01:00:23 +00001181 Features["sse4.1"] = (ECX >> 19) & 1;
1182 Features["sse4.2"] = (ECX >> 20) & 1;
1183
Eric Christopher5db9d662016-06-02 21:03:19 +00001184 Features["pclmul"] = (ECX >> 1) & 1;
1185 Features["cx16"] = (ECX >> 13) & 1;
1186 Features["movbe"] = (ECX >> 22) & 1;
Craig Topper798a2602015-03-29 01:00:23 +00001187 Features["popcnt"] = (ECX >> 23) & 1;
Eric Christopher5db9d662016-06-02 21:03:19 +00001188 Features["aes"] = (ECX >> 25) & 1;
1189 Features["rdrnd"] = (ECX >> 30) & 1;
Craig Topper798a2602015-03-29 01:00:23 +00001190
1191 // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
1192 // indicates that the AVX registers will be saved and restored on context
1193 // switch, then we have full AVX support.
Craig Topperb84b1262015-10-14 05:37:42 +00001194 bool HasAVXSave = ((ECX >> 27) & 1) && ((ECX >> 28) & 1) &&
Alina Sbirlea400eb022016-06-03 20:27:50 +00001195 !getX86XCR0(&EAX, &EDX) && ((EAX & 0x6) == 0x6);
Eric Christopher5db9d662016-06-02 21:03:19 +00001196 Features["avx"] = HasAVXSave;
1197 Features["fma"] = HasAVXSave && (ECX >> 12) & 1;
1198 Features["f16c"] = HasAVXSave && (ECX >> 29) & 1;
Craig Topperb84b1262015-10-14 05:37:42 +00001199
1200 // Only enable XSAVE if OS has enabled support for saving YMM state.
Eric Christopher5db9d662016-06-02 21:03:19 +00001201 Features["xsave"] = HasAVXSave && (ECX >> 26) & 1;
Craig Topper798a2602015-03-29 01:00:23 +00001202
1203 // AVX512 requires additional context to be saved by the OS.
Craig Topperb84b1262015-10-14 05:37:42 +00001204 bool HasAVX512Save = HasAVXSave && ((EAX & 0xe0) == 0xe0);
Craig Topper798a2602015-03-29 01:00:23 +00001205
1206 unsigned MaxExtLevel;
Alina Sbirlea400eb022016-06-03 20:27:50 +00001207 getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
Craig Topper798a2602015-03-29 01:00:23 +00001208
1209 bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
Alina Sbirlea400eb022016-06-03 20:27:50 +00001210 !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
Eric Christopher5db9d662016-06-02 21:03:19 +00001211 Features["lzcnt"] = HasExtLeaf1 && ((ECX >> 5) & 1);
1212 Features["sse4a"] = HasExtLeaf1 && ((ECX >> 6) & 1);
1213 Features["prfchw"] = HasExtLeaf1 && ((ECX >> 8) & 1);
1214 Features["xop"] = HasExtLeaf1 && ((ECX >> 11) & 1) && HasAVXSave;
1215 Features["fma4"] = HasExtLeaf1 && ((ECX >> 16) & 1) && HasAVXSave;
1216 Features["tbm"] = HasExtLeaf1 && ((ECX >> 21) & 1);
Ashutosh Nema348af9c2016-05-18 11:59:12 +00001217 Features["mwaitx"] = HasExtLeaf1 && ((ECX >> 29) & 1);
Craig Topper798a2602015-03-29 01:00:23 +00001218
Eric Christopher5db9d662016-06-02 21:03:19 +00001219 bool HasLeaf7 =
Alina Sbirlea400eb022016-06-03 20:27:50 +00001220 MaxLevel >= 7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
Craig Topper798a2602015-03-29 01:00:23 +00001221
1222 // AVX2 is only supported if we have the OS save support from AVX.
Eric Christopher5db9d662016-06-02 21:03:19 +00001223 Features["avx2"] = HasAVXSave && HasLeaf7 && ((EBX >> 5) & 1);
Craig Topper798a2602015-03-29 01:00:23 +00001224
Eric Christopher5db9d662016-06-02 21:03:19 +00001225 Features["fsgsbase"] = HasLeaf7 && ((EBX >> 0) & 1);
1226 Features["sgx"] = HasLeaf7 && ((EBX >> 2) & 1);
1227 Features["bmi"] = HasLeaf7 && ((EBX >> 3) & 1);
1228 Features["hle"] = HasLeaf7 && ((EBX >> 4) & 1);
1229 Features["bmi2"] = HasLeaf7 && ((EBX >> 8) & 1);
1230 Features["invpcid"] = HasLeaf7 && ((EBX >> 10) & 1);
1231 Features["rtm"] = HasLeaf7 && ((EBX >> 11) & 1);
1232 Features["rdseed"] = HasLeaf7 && ((EBX >> 18) & 1);
1233 Features["adx"] = HasLeaf7 && ((EBX >> 19) & 1);
1234 Features["smap"] = HasLeaf7 && ((EBX >> 20) & 1);
1235 Features["pcommit"] = HasLeaf7 && ((EBX >> 22) & 1);
Elena Demikhovsky29cde352016-01-24 10:41:28 +00001236 Features["clflushopt"] = HasLeaf7 && ((EBX >> 23) & 1);
Eric Christopher5db9d662016-06-02 21:03:19 +00001237 Features["clwb"] = HasLeaf7 && ((EBX >> 24) & 1);
1238 Features["sha"] = HasLeaf7 && ((EBX >> 29) & 1);
Craig Topper798a2602015-03-29 01:00:23 +00001239
1240 // AVX512 is only supported if the OS supports the context save for it.
Eric Christopher5db9d662016-06-02 21:03:19 +00001241 Features["avx512f"] = HasLeaf7 && ((EBX >> 16) & 1) && HasAVX512Save;
Craig Topper798a2602015-03-29 01:00:23 +00001242 Features["avx512dq"] = HasLeaf7 && ((EBX >> 17) & 1) && HasAVX512Save;
Elena Demikhovsky29cde352016-01-24 10:41:28 +00001243 Features["avx512ifma"] = HasLeaf7 && ((EBX >> 21) & 1) && HasAVX512Save;
Craig Topper798a2602015-03-29 01:00:23 +00001244 Features["avx512pf"] = HasLeaf7 && ((EBX >> 26) & 1) && HasAVX512Save;
1245 Features["avx512er"] = HasLeaf7 && ((EBX >> 27) & 1) && HasAVX512Save;
1246 Features["avx512cd"] = HasLeaf7 && ((EBX >> 28) & 1) && HasAVX512Save;
1247 Features["avx512bw"] = HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save;
1248 Features["avx512vl"] = HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save;
Elena Demikhovsky29cde352016-01-24 10:41:28 +00001249
1250 Features["prefetchwt1"] = HasLeaf7 && (ECX & 1);
Eric Christopher5db9d662016-06-02 21:03:19 +00001251 Features["avx512vbmi"] = HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save;
Elena Demikhovsky29cde352016-01-24 10:41:28 +00001252 // Enable protection keys
Eric Christopher5db9d662016-06-02 21:03:19 +00001253 Features["pku"] = HasLeaf7 && ((ECX >> 4) & 1);
Craig Topper798a2602015-03-29 01:00:23 +00001254
Amjad Aboud1db6d7a2015-10-12 11:47:46 +00001255 bool HasLeafD = MaxLevel >= 0xd &&
Alina Sbirlea400eb022016-06-03 20:27:50 +00001256 !getX86CpuIDAndInfoEx(0xd, 0x1, &EAX, &EBX, &ECX, &EDX);
Amjad Aboud1db6d7a2015-10-12 11:47:46 +00001257
Craig Topperb84b1262015-10-14 05:37:42 +00001258 // Only enable XSAVE if OS has enabled support for saving YMM state.
1259 Features["xsaveopt"] = HasAVXSave && HasLeafD && ((EAX >> 0) & 1);
Eric Christopher5db9d662016-06-02 21:03:19 +00001260 Features["xsavec"] = HasAVXSave && HasLeafD && ((EAX >> 1) & 1);
1261 Features["xsaves"] = HasAVXSave && HasLeafD && ((EAX >> 3) & 1);
Amjad Aboud1db6d7a2015-10-12 11:47:46 +00001262
Craig Topper798a2602015-03-29 01:00:23 +00001263 return true;
1264}
1265#elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
Hao Liu10be3b22012-12-13 02:40:20 +00001266bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
Hao Liu10be3b22012-12-13 02:40:20 +00001267 // Read 1024 bytes from /proc/cpuinfo, which should contain the Features line
1268 // in all cases.
1269 char buffer[1024];
Rafael Espindola97935a92014-12-17 02:32:44 +00001270 ssize_t CPUInfoSize = readCpuInfo(buffer, sizeof(buffer));
1271 if (CPUInfoSize == -1)
1272 return false;
Hao Liu10be3b22012-12-13 02:40:20 +00001273
1274 StringRef Str(buffer, CPUInfoSize);
1275
1276 SmallVector<StringRef, 32> Lines;
1277 Str.split(Lines, "\n");
1278
Tobias Grosserbd9e5492013-06-11 21:45:01 +00001279 SmallVector<StringRef, 32> CPUFeatures;
1280
1281 // Look for the CPU features.
Hao Liu10be3b22012-12-13 02:40:20 +00001282 for (unsigned I = 0, E = Lines.size(); I != E; ++I)
Tobias Grosserbd9e5492013-06-11 21:45:01 +00001283 if (Lines[I].startswith("Features")) {
Chandler Carruthe4405e92015-09-10 06:12:31 +00001284 Lines[I].split(CPUFeatures, ' ');
Tobias Grosserbd9e5492013-06-11 21:45:01 +00001285 break;
Hao Liu10be3b22012-12-13 02:40:20 +00001286 }
1287
Bradley Smith9288b212014-05-22 11:44:34 +00001288#if defined(__aarch64__)
1289 // Keep track of which crypto features we have seen
Eric Christopher5db9d662016-06-02 21:03:19 +00001290 enum { CAP_AES = 0x1, CAP_PMULL = 0x2, CAP_SHA1 = 0x4, CAP_SHA2 = 0x8 };
Bradley Smith9288b212014-05-22 11:44:34 +00001291 uint32_t crypto = 0;
1292#endif
1293
Tobias Grosserbd9e5492013-06-11 21:45:01 +00001294 for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) {
1295 StringRef LLVMFeatureStr = StringSwitch<StringRef>(CPUFeatures[I])
Bradley Smith9288b212014-05-22 11:44:34 +00001296#if defined(__aarch64__)
Eric Christopher5db9d662016-06-02 21:03:19 +00001297 .Case("asimd", "neon")
1298 .Case("fp", "fp-armv8")
1299 .Case("crc32", "crc")
Bradley Smith9288b212014-05-22 11:44:34 +00001300#else
Eric Christopher5db9d662016-06-02 21:03:19 +00001301 .Case("half", "fp16")
1302 .Case("neon", "neon")
1303 .Case("vfpv3", "vfp3")
1304 .Case("vfpv3d16", "d16")
1305 .Case("vfpv4", "vfp4")
1306 .Case("idiva", "hwdiv-arm")
1307 .Case("idivt", "hwdiv")
Bradley Smith9288b212014-05-22 11:44:34 +00001308#endif
Eric Christopher5db9d662016-06-02 21:03:19 +00001309 .Default("");
Tobias Grosserbd9e5492013-06-11 21:45:01 +00001310
Bradley Smith9288b212014-05-22 11:44:34 +00001311#if defined(__aarch64__)
Alp Tokerda0c7932014-05-31 21:26:28 +00001312 // We need to check crypto separately since we need all of the crypto
Bradley Smith9288b212014-05-22 11:44:34 +00001313 // extensions to enable the subtarget feature
1314 if (CPUFeatures[I] == "aes")
Bradley Smith63c8b1b2014-05-23 10:14:13 +00001315 crypto |= CAP_AES;
Bradley Smith9288b212014-05-22 11:44:34 +00001316 else if (CPUFeatures[I] == "pmull")
Bradley Smith63c8b1b2014-05-23 10:14:13 +00001317 crypto |= CAP_PMULL;
Bradley Smith9288b212014-05-22 11:44:34 +00001318 else if (CPUFeatures[I] == "sha1")
Bradley Smith63c8b1b2014-05-23 10:14:13 +00001319 crypto |= CAP_SHA1;
Bradley Smith9288b212014-05-22 11:44:34 +00001320 else if (CPUFeatures[I] == "sha2")
Bradley Smith63c8b1b2014-05-23 10:14:13 +00001321 crypto |= CAP_SHA2;
Bradley Smith9288b212014-05-22 11:44:34 +00001322#endif
1323
Tobias Grosserbd9e5492013-06-11 21:45:01 +00001324 if (LLVMFeatureStr != "")
David Blaikie5106ce72014-11-19 05:49:42 +00001325 Features[LLVMFeatureStr] = true;
Hao Liu10be3b22012-12-13 02:40:20 +00001326 }
1327
Bradley Smith9288b212014-05-22 11:44:34 +00001328#if defined(__aarch64__)
1329 // If we have all crypto bits we can add the feature
Bradley Smith63c8b1b2014-05-23 10:14:13 +00001330 if (crypto == (CAP_AES | CAP_PMULL | CAP_SHA1 | CAP_SHA2))
David Blaikie5106ce72014-11-19 05:49:42 +00001331 Features["crypto"] = true;
Bradley Smith9288b212014-05-22 11:44:34 +00001332#endif
1333
Tobias Grosserbd9e5492013-06-11 21:45:01 +00001334 return true;
Hao Liu10be3b22012-12-13 02:40:20 +00001335}
1336#else
Eric Christopher5db9d662016-06-02 21:03:19 +00001337bool sys::getHostCPUFeatures(StringMap<bool> &Features) { return false; }
Hao Liu10be3b22012-12-13 02:40:20 +00001338#endif
Peter Collingbournea51c6ed2013-01-16 17:27:22 +00001339
1340std::string sys::getProcessTriple() {
Duncan Sandse2cd1392013-07-17 11:01:05 +00001341 Triple PT(Triple::normalize(LLVM_HOST_TRIPLE));
Peter Collingbournea51c6ed2013-01-16 17:27:22 +00001342
1343 if (sizeof(void *) == 8 && PT.isArch32Bit())
1344 PT = PT.get64BitArchVariant();
1345 if (sizeof(void *) == 4 && PT.isArch64Bit())
1346 PT = PT.get32BitArchVariant();
1347
1348 return PT.str();
1349}