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