xref: /freebsd-src/contrib/llvm-project/llvm/lib/TargetParser/Host.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1bdd1243dSDimitry Andric //===-- Host.cpp - Implement OS Host Detection ------------------*- C++ -*-===//
2bdd1243dSDimitry Andric //
3bdd1243dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4bdd1243dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5bdd1243dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6bdd1243dSDimitry Andric //
7bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
8bdd1243dSDimitry Andric //
9bdd1243dSDimitry Andric //  This file implements the operating system Host detection.
10bdd1243dSDimitry Andric //
11bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
12bdd1243dSDimitry Andric 
13bdd1243dSDimitry Andric #include "llvm/TargetParser/Host.h"
14bdd1243dSDimitry Andric #include "llvm/ADT/SmallVector.h"
15bdd1243dSDimitry Andric #include "llvm/ADT/StringMap.h"
16bdd1243dSDimitry Andric #include "llvm/ADT/StringRef.h"
17bdd1243dSDimitry Andric #include "llvm/ADT/StringSwitch.h"
18bdd1243dSDimitry Andric #include "llvm/Config/llvm-config.h"
19bdd1243dSDimitry Andric #include "llvm/Support/MemoryBuffer.h"
20bdd1243dSDimitry Andric #include "llvm/Support/raw_ostream.h"
21bdd1243dSDimitry Andric #include "llvm/TargetParser/Triple.h"
22bdd1243dSDimitry Andric #include "llvm/TargetParser/X86TargetParser.h"
23bdd1243dSDimitry Andric #include <string.h>
24bdd1243dSDimitry Andric 
25bdd1243dSDimitry Andric // Include the platform-specific parts of this class.
26bdd1243dSDimitry Andric #ifdef LLVM_ON_UNIX
27bdd1243dSDimitry Andric #include "Unix/Host.inc"
28bdd1243dSDimitry Andric #include <sched.h>
29bdd1243dSDimitry Andric #endif
30bdd1243dSDimitry Andric #ifdef _WIN32
31bdd1243dSDimitry Andric #include "Windows/Host.inc"
32bdd1243dSDimitry Andric #endif
33bdd1243dSDimitry Andric #ifdef _MSC_VER
34bdd1243dSDimitry Andric #include <intrin.h>
35bdd1243dSDimitry Andric #endif
36bdd1243dSDimitry Andric #ifdef __MVS__
37bdd1243dSDimitry Andric #include "llvm/Support/BCD.h"
38bdd1243dSDimitry Andric #endif
39bdd1243dSDimitry Andric #if defined(__APPLE__)
40bdd1243dSDimitry Andric #include <mach/host_info.h>
41bdd1243dSDimitry Andric #include <mach/mach.h>
42bdd1243dSDimitry Andric #include <mach/mach_host.h>
43bdd1243dSDimitry Andric #include <mach/machine.h>
44bdd1243dSDimitry Andric #include <sys/param.h>
45bdd1243dSDimitry Andric #include <sys/sysctl.h>
46bdd1243dSDimitry Andric #endif
47bdd1243dSDimitry Andric #ifdef _AIX
48bdd1243dSDimitry Andric #include <sys/systemcfg.h>
49bdd1243dSDimitry Andric #endif
50bdd1243dSDimitry Andric #if defined(__sun__) && defined(__svr4__)
51bdd1243dSDimitry Andric #include <kstat.h>
52bdd1243dSDimitry Andric #endif
53bdd1243dSDimitry Andric 
54bdd1243dSDimitry Andric #define DEBUG_TYPE "host-detection"
55bdd1243dSDimitry Andric 
56bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
57bdd1243dSDimitry Andric //
58bdd1243dSDimitry Andric //  Implementations of the CPU detection routines
59bdd1243dSDimitry Andric //
60bdd1243dSDimitry Andric //===----------------------------------------------------------------------===//
61bdd1243dSDimitry Andric 
62bdd1243dSDimitry Andric using namespace llvm;
63bdd1243dSDimitry Andric 
64bdd1243dSDimitry Andric static std::unique_ptr<llvm::MemoryBuffer>
65bdd1243dSDimitry Andric     LLVM_ATTRIBUTE_UNUSED getProcCpuinfoContent() {
66bdd1243dSDimitry Andric   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =
67bdd1243dSDimitry Andric       llvm::MemoryBuffer::getFileAsStream("/proc/cpuinfo");
68bdd1243dSDimitry Andric   if (std::error_code EC = Text.getError()) {
69bdd1243dSDimitry Andric     llvm::errs() << "Can't read "
70bdd1243dSDimitry Andric                  << "/proc/cpuinfo: " << EC.message() << "\n";
71bdd1243dSDimitry Andric     return nullptr;
72bdd1243dSDimitry Andric   }
73bdd1243dSDimitry Andric   return std::move(*Text);
74bdd1243dSDimitry Andric }
75bdd1243dSDimitry Andric 
76bdd1243dSDimitry Andric StringRef sys::detail::getHostCPUNameForPowerPC(StringRef ProcCpuinfoContent) {
77bdd1243dSDimitry Andric   // Access to the Processor Version Register (PVR) on PowerPC is privileged,
78bdd1243dSDimitry Andric   // and so we must use an operating-system interface to determine the current
79bdd1243dSDimitry Andric   // processor type. On Linux, this is exposed through the /proc/cpuinfo file.
80bdd1243dSDimitry Andric   const char *generic = "generic";
81bdd1243dSDimitry Andric 
82bdd1243dSDimitry Andric   // The cpu line is second (after the 'processor: 0' line), so if this
83bdd1243dSDimitry Andric   // buffer is too small then something has changed (or is wrong).
84bdd1243dSDimitry Andric   StringRef::const_iterator CPUInfoStart = ProcCpuinfoContent.begin();
85bdd1243dSDimitry Andric   StringRef::const_iterator CPUInfoEnd = ProcCpuinfoContent.end();
86bdd1243dSDimitry Andric 
87bdd1243dSDimitry Andric   StringRef::const_iterator CIP = CPUInfoStart;
88bdd1243dSDimitry Andric 
89bdd1243dSDimitry Andric   StringRef::const_iterator CPUStart = nullptr;
90bdd1243dSDimitry Andric   size_t CPULen = 0;
91bdd1243dSDimitry Andric 
92bdd1243dSDimitry Andric   // We need to find the first line which starts with cpu, spaces, and a colon.
93bdd1243dSDimitry Andric   // After the colon, there may be some additional spaces and then the cpu type.
94bdd1243dSDimitry Andric   while (CIP < CPUInfoEnd && CPUStart == nullptr) {
95bdd1243dSDimitry Andric     if (CIP < CPUInfoEnd && *CIP == '\n')
96bdd1243dSDimitry Andric       ++CIP;
97bdd1243dSDimitry Andric 
98bdd1243dSDimitry Andric     if (CIP < CPUInfoEnd && *CIP == 'c') {
99bdd1243dSDimitry Andric       ++CIP;
100bdd1243dSDimitry Andric       if (CIP < CPUInfoEnd && *CIP == 'p') {
101bdd1243dSDimitry Andric         ++CIP;
102bdd1243dSDimitry Andric         if (CIP < CPUInfoEnd && *CIP == 'u') {
103bdd1243dSDimitry Andric           ++CIP;
104bdd1243dSDimitry Andric           while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t'))
105bdd1243dSDimitry Andric             ++CIP;
106bdd1243dSDimitry Andric 
107bdd1243dSDimitry Andric           if (CIP < CPUInfoEnd && *CIP == ':') {
108bdd1243dSDimitry Andric             ++CIP;
109bdd1243dSDimitry Andric             while (CIP < CPUInfoEnd && (*CIP == ' ' || *CIP == '\t'))
110bdd1243dSDimitry Andric               ++CIP;
111bdd1243dSDimitry Andric 
112bdd1243dSDimitry Andric             if (CIP < CPUInfoEnd) {
113bdd1243dSDimitry Andric               CPUStart = CIP;
114bdd1243dSDimitry Andric               while (CIP < CPUInfoEnd && (*CIP != ' ' && *CIP != '\t' &&
115bdd1243dSDimitry Andric                                           *CIP != ',' && *CIP != '\n'))
116bdd1243dSDimitry Andric                 ++CIP;
117bdd1243dSDimitry Andric               CPULen = CIP - CPUStart;
118bdd1243dSDimitry Andric             }
119bdd1243dSDimitry Andric           }
120bdd1243dSDimitry Andric         }
121bdd1243dSDimitry Andric       }
122bdd1243dSDimitry Andric     }
123bdd1243dSDimitry Andric 
124bdd1243dSDimitry Andric     if (CPUStart == nullptr)
125bdd1243dSDimitry Andric       while (CIP < CPUInfoEnd && *CIP != '\n')
126bdd1243dSDimitry Andric         ++CIP;
127bdd1243dSDimitry Andric   }
128bdd1243dSDimitry Andric 
129bdd1243dSDimitry Andric   if (CPUStart == nullptr)
130bdd1243dSDimitry Andric     return generic;
131bdd1243dSDimitry Andric 
132bdd1243dSDimitry Andric   return StringSwitch<const char *>(StringRef(CPUStart, CPULen))
133bdd1243dSDimitry Andric       .Case("604e", "604e")
134bdd1243dSDimitry Andric       .Case("604", "604")
135bdd1243dSDimitry Andric       .Case("7400", "7400")
136bdd1243dSDimitry Andric       .Case("7410", "7400")
137bdd1243dSDimitry Andric       .Case("7447", "7400")
138bdd1243dSDimitry Andric       .Case("7455", "7450")
139bdd1243dSDimitry Andric       .Case("G4", "g4")
140bdd1243dSDimitry Andric       .Case("POWER4", "970")
141bdd1243dSDimitry Andric       .Case("PPC970FX", "970")
142bdd1243dSDimitry Andric       .Case("PPC970MP", "970")
143bdd1243dSDimitry Andric       .Case("G5", "g5")
144bdd1243dSDimitry Andric       .Case("POWER5", "g5")
145bdd1243dSDimitry Andric       .Case("A2", "a2")
146bdd1243dSDimitry Andric       .Case("POWER6", "pwr6")
147bdd1243dSDimitry Andric       .Case("POWER7", "pwr7")
148bdd1243dSDimitry Andric       .Case("POWER8", "pwr8")
149bdd1243dSDimitry Andric       .Case("POWER8E", "pwr8")
150bdd1243dSDimitry Andric       .Case("POWER8NVL", "pwr8")
151bdd1243dSDimitry Andric       .Case("POWER9", "pwr9")
152bdd1243dSDimitry Andric       .Case("POWER10", "pwr10")
153bdd1243dSDimitry Andric       // FIXME: If we get a simulator or machine with the capabilities of
154bdd1243dSDimitry Andric       // mcpu=future, we should revisit this and add the name reported by the
155bdd1243dSDimitry Andric       // simulator/machine.
156bdd1243dSDimitry Andric       .Default(generic);
157bdd1243dSDimitry Andric }
158bdd1243dSDimitry Andric 
159bdd1243dSDimitry Andric StringRef sys::detail::getHostCPUNameForARM(StringRef ProcCpuinfoContent) {
160bdd1243dSDimitry Andric   // The cpuid register on arm is not accessible from user space. On Linux,
161bdd1243dSDimitry Andric   // it is exposed through the /proc/cpuinfo file.
162bdd1243dSDimitry Andric 
163bdd1243dSDimitry Andric   // Read 32 lines from /proc/cpuinfo, which should contain the CPU part line
164bdd1243dSDimitry Andric   // in all cases.
165bdd1243dSDimitry Andric   SmallVector<StringRef, 32> Lines;
166bdd1243dSDimitry Andric   ProcCpuinfoContent.split(Lines, "\n");
167bdd1243dSDimitry Andric 
168bdd1243dSDimitry Andric   // Look for the CPU implementer line.
169bdd1243dSDimitry Andric   StringRef Implementer;
170bdd1243dSDimitry Andric   StringRef Hardware;
171bdd1243dSDimitry Andric   StringRef Part;
172bdd1243dSDimitry Andric   for (unsigned I = 0, E = Lines.size(); I != E; ++I) {
173*5f757f3fSDimitry Andric     if (Lines[I].starts_with("CPU implementer"))
174bdd1243dSDimitry Andric       Implementer = Lines[I].substr(15).ltrim("\t :");
175*5f757f3fSDimitry Andric     if (Lines[I].starts_with("Hardware"))
176bdd1243dSDimitry Andric       Hardware = Lines[I].substr(8).ltrim("\t :");
177*5f757f3fSDimitry Andric     if (Lines[I].starts_with("CPU part"))
178bdd1243dSDimitry Andric       Part = Lines[I].substr(8).ltrim("\t :");
179bdd1243dSDimitry Andric   }
180bdd1243dSDimitry Andric 
181bdd1243dSDimitry Andric   if (Implementer == "0x41") { // ARM Ltd.
182bdd1243dSDimitry Andric     // MSM8992/8994 may give cpu part for the core that the kernel is running on,
183bdd1243dSDimitry Andric     // which is undeterministic and wrong. Always return cortex-a53 for these SoC.
184*5f757f3fSDimitry Andric     if (Hardware.ends_with("MSM8994") || Hardware.ends_with("MSM8996"))
185bdd1243dSDimitry Andric       return "cortex-a53";
186bdd1243dSDimitry Andric 
187bdd1243dSDimitry Andric 
188bdd1243dSDimitry Andric     // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
189bdd1243dSDimitry Andric     // values correspond to the "Part number" in the CP15/c0 register. The
190bdd1243dSDimitry Andric     // contents are specified in the various processor manuals.
191bdd1243dSDimitry Andric     // This corresponds to the Main ID Register in Technical Reference Manuals.
192bdd1243dSDimitry Andric     // and is used in programs like sys-utils
193bdd1243dSDimitry Andric     return StringSwitch<const char *>(Part)
194bdd1243dSDimitry Andric         .Case("0x926", "arm926ej-s")
195bdd1243dSDimitry Andric         .Case("0xb02", "mpcore")
196bdd1243dSDimitry Andric         .Case("0xb36", "arm1136j-s")
197bdd1243dSDimitry Andric         .Case("0xb56", "arm1156t2-s")
198bdd1243dSDimitry Andric         .Case("0xb76", "arm1176jz-s")
199bdd1243dSDimitry Andric         .Case("0xc08", "cortex-a8")
200bdd1243dSDimitry Andric         .Case("0xc09", "cortex-a9")
201bdd1243dSDimitry Andric         .Case("0xc0f", "cortex-a15")
202bdd1243dSDimitry Andric         .Case("0xc20", "cortex-m0")
203bdd1243dSDimitry Andric         .Case("0xc23", "cortex-m3")
204bdd1243dSDimitry Andric         .Case("0xc24", "cortex-m4")
205*5f757f3fSDimitry Andric         .Case("0xd24", "cortex-m52")
206bdd1243dSDimitry Andric         .Case("0xd22", "cortex-m55")
207bdd1243dSDimitry Andric         .Case("0xd02", "cortex-a34")
208bdd1243dSDimitry Andric         .Case("0xd04", "cortex-a35")
209bdd1243dSDimitry Andric         .Case("0xd03", "cortex-a53")
210bdd1243dSDimitry Andric         .Case("0xd05", "cortex-a55")
211bdd1243dSDimitry Andric         .Case("0xd46", "cortex-a510")
212*5f757f3fSDimitry Andric         .Case("0xd80", "cortex-a520")
213bdd1243dSDimitry Andric         .Case("0xd07", "cortex-a57")
214bdd1243dSDimitry Andric         .Case("0xd08", "cortex-a72")
215bdd1243dSDimitry Andric         .Case("0xd09", "cortex-a73")
216bdd1243dSDimitry Andric         .Case("0xd0a", "cortex-a75")
217bdd1243dSDimitry Andric         .Case("0xd0b", "cortex-a76")
218bdd1243dSDimitry Andric         .Case("0xd0d", "cortex-a77")
219bdd1243dSDimitry Andric         .Case("0xd41", "cortex-a78")
220bdd1243dSDimitry Andric         .Case("0xd47", "cortex-a710")
221bdd1243dSDimitry Andric         .Case("0xd4d", "cortex-a715")
222*5f757f3fSDimitry Andric         .Case("0xd81", "cortex-a720")
223bdd1243dSDimitry Andric         .Case("0xd44", "cortex-x1")
224bdd1243dSDimitry Andric         .Case("0xd4c", "cortex-x1c")
225bdd1243dSDimitry Andric         .Case("0xd48", "cortex-x2")
226bdd1243dSDimitry Andric         .Case("0xd4e", "cortex-x3")
227*5f757f3fSDimitry Andric         .Case("0xd82", "cortex-x4")
228bdd1243dSDimitry Andric         .Case("0xd0c", "neoverse-n1")
229bdd1243dSDimitry Andric         .Case("0xd49", "neoverse-n2")
230bdd1243dSDimitry Andric         .Case("0xd40", "neoverse-v1")
231bdd1243dSDimitry Andric         .Case("0xd4f", "neoverse-v2")
232bdd1243dSDimitry Andric         .Default("generic");
233bdd1243dSDimitry Andric   }
234bdd1243dSDimitry Andric 
235bdd1243dSDimitry Andric   if (Implementer == "0x42" || Implementer == "0x43") { // Broadcom | Cavium.
236bdd1243dSDimitry Andric     return StringSwitch<const char *>(Part)
237bdd1243dSDimitry Andric       .Case("0x516", "thunderx2t99")
238bdd1243dSDimitry Andric       .Case("0x0516", "thunderx2t99")
239bdd1243dSDimitry Andric       .Case("0xaf", "thunderx2t99")
240bdd1243dSDimitry Andric       .Case("0x0af", "thunderx2t99")
241bdd1243dSDimitry Andric       .Case("0xa1", "thunderxt88")
242bdd1243dSDimitry Andric       .Case("0x0a1", "thunderxt88")
243bdd1243dSDimitry Andric       .Default("generic");
244bdd1243dSDimitry Andric   }
245bdd1243dSDimitry Andric 
246bdd1243dSDimitry Andric   if (Implementer == "0x46") { // Fujitsu Ltd.
247bdd1243dSDimitry Andric     return StringSwitch<const char *>(Part)
248bdd1243dSDimitry Andric       .Case("0x001", "a64fx")
249bdd1243dSDimitry Andric       .Default("generic");
250bdd1243dSDimitry Andric   }
251bdd1243dSDimitry Andric 
252bdd1243dSDimitry Andric   if (Implementer == "0x4e") { // NVIDIA Corporation
253bdd1243dSDimitry Andric     return StringSwitch<const char *>(Part)
254bdd1243dSDimitry Andric         .Case("0x004", "carmel")
255bdd1243dSDimitry Andric         .Default("generic");
256bdd1243dSDimitry Andric   }
257bdd1243dSDimitry Andric 
258bdd1243dSDimitry Andric   if (Implementer == "0x48") // HiSilicon Technologies, Inc.
259bdd1243dSDimitry Andric     // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
260bdd1243dSDimitry Andric     // values correspond to the "Part number" in the CP15/c0 register. The
261bdd1243dSDimitry Andric     // contents are specified in the various processor manuals.
262bdd1243dSDimitry Andric     return StringSwitch<const char *>(Part)
263bdd1243dSDimitry Andric       .Case("0xd01", "tsv110")
264bdd1243dSDimitry Andric       .Default("generic");
265bdd1243dSDimitry Andric 
266bdd1243dSDimitry Andric   if (Implementer == "0x51") // Qualcomm Technologies, Inc.
267bdd1243dSDimitry Andric     // The CPU part is a 3 digit hexadecimal number with a 0x prefix. The
268bdd1243dSDimitry Andric     // values correspond to the "Part number" in the CP15/c0 register. The
269bdd1243dSDimitry Andric     // contents are specified in the various processor manuals.
270bdd1243dSDimitry Andric     return StringSwitch<const char *>(Part)
271bdd1243dSDimitry Andric         .Case("0x06f", "krait") // APQ8064
272bdd1243dSDimitry Andric         .Case("0x201", "kryo")
273bdd1243dSDimitry Andric         .Case("0x205", "kryo")
274bdd1243dSDimitry Andric         .Case("0x211", "kryo")
275bdd1243dSDimitry Andric         .Case("0x800", "cortex-a73") // Kryo 2xx Gold
276bdd1243dSDimitry Andric         .Case("0x801", "cortex-a73") // Kryo 2xx Silver
277bdd1243dSDimitry Andric         .Case("0x802", "cortex-a75") // Kryo 3xx Gold
278bdd1243dSDimitry Andric         .Case("0x803", "cortex-a75") // Kryo 3xx Silver
279bdd1243dSDimitry Andric         .Case("0x804", "cortex-a76") // Kryo 4xx Gold
280bdd1243dSDimitry Andric         .Case("0x805", "cortex-a76") // Kryo 4xx/5xx Silver
281bdd1243dSDimitry Andric         .Case("0xc00", "falkor")
282bdd1243dSDimitry Andric         .Case("0xc01", "saphira")
283bdd1243dSDimitry Andric         .Default("generic");
284bdd1243dSDimitry Andric   if (Implementer == "0x53") { // Samsung Electronics Co., Ltd.
285bdd1243dSDimitry Andric     // The Exynos chips have a convoluted ID scheme that doesn't seem to follow
286bdd1243dSDimitry Andric     // any predictive pattern across variants and parts.
287bdd1243dSDimitry Andric     unsigned Variant = 0, Part = 0;
288bdd1243dSDimitry Andric 
289bdd1243dSDimitry Andric     // Look for the CPU variant line, whose value is a 1 digit hexadecimal
290bdd1243dSDimitry Andric     // number, corresponding to the Variant bits in the CP15/C0 register.
291bdd1243dSDimitry Andric     for (auto I : Lines)
292bdd1243dSDimitry Andric       if (I.consume_front("CPU variant"))
293bdd1243dSDimitry Andric         I.ltrim("\t :").getAsInteger(0, Variant);
294bdd1243dSDimitry Andric 
295bdd1243dSDimitry Andric     // Look for the CPU part line, whose value is a 3 digit hexadecimal
296bdd1243dSDimitry Andric     // number, corresponding to the PartNum bits in the CP15/C0 register.
297bdd1243dSDimitry Andric     for (auto I : Lines)
298bdd1243dSDimitry Andric       if (I.consume_front("CPU part"))
299bdd1243dSDimitry Andric         I.ltrim("\t :").getAsInteger(0, Part);
300bdd1243dSDimitry Andric 
301bdd1243dSDimitry Andric     unsigned Exynos = (Variant << 12) | Part;
302bdd1243dSDimitry Andric     switch (Exynos) {
303bdd1243dSDimitry Andric     default:
304bdd1243dSDimitry Andric       // Default by falling through to Exynos M3.
305bdd1243dSDimitry Andric       [[fallthrough]];
306bdd1243dSDimitry Andric     case 0x1002:
307bdd1243dSDimitry Andric       return "exynos-m3";
308bdd1243dSDimitry Andric     case 0x1003:
309bdd1243dSDimitry Andric       return "exynos-m4";
310bdd1243dSDimitry Andric     }
311bdd1243dSDimitry Andric   }
312bdd1243dSDimitry Andric 
313bdd1243dSDimitry Andric   if (Implementer == "0xc0") { // Ampere Computing
314bdd1243dSDimitry Andric     return StringSwitch<const char *>(Part)
315bdd1243dSDimitry Andric         .Case("0xac3", "ampere1")
316bdd1243dSDimitry Andric         .Case("0xac4", "ampere1a")
317bdd1243dSDimitry Andric         .Default("generic");
318bdd1243dSDimitry Andric   }
319bdd1243dSDimitry Andric 
320bdd1243dSDimitry Andric   return "generic";
321bdd1243dSDimitry Andric }
322bdd1243dSDimitry Andric 
323bdd1243dSDimitry Andric namespace {
324bdd1243dSDimitry Andric StringRef getCPUNameFromS390Model(unsigned int Id, bool HaveVectorSupport) {
325bdd1243dSDimitry Andric   switch (Id) {
326bdd1243dSDimitry Andric     case 2064:  // z900 not supported by LLVM
327bdd1243dSDimitry Andric     case 2066:
328bdd1243dSDimitry Andric     case 2084:  // z990 not supported by LLVM
329bdd1243dSDimitry Andric     case 2086:
330bdd1243dSDimitry Andric     case 2094:  // z9-109 not supported by LLVM
331bdd1243dSDimitry Andric     case 2096:
332bdd1243dSDimitry Andric       return "generic";
333bdd1243dSDimitry Andric     case 2097:
334bdd1243dSDimitry Andric     case 2098:
335bdd1243dSDimitry Andric       return "z10";
336bdd1243dSDimitry Andric     case 2817:
337bdd1243dSDimitry Andric     case 2818:
338bdd1243dSDimitry Andric       return "z196";
339bdd1243dSDimitry Andric     case 2827:
340bdd1243dSDimitry Andric     case 2828:
341bdd1243dSDimitry Andric       return "zEC12";
342bdd1243dSDimitry Andric     case 2964:
343bdd1243dSDimitry Andric     case 2965:
344bdd1243dSDimitry Andric       return HaveVectorSupport? "z13" : "zEC12";
345bdd1243dSDimitry Andric     case 3906:
346bdd1243dSDimitry Andric     case 3907:
347bdd1243dSDimitry Andric       return HaveVectorSupport? "z14" : "zEC12";
348bdd1243dSDimitry Andric     case 8561:
349bdd1243dSDimitry Andric     case 8562:
350bdd1243dSDimitry Andric       return HaveVectorSupport? "z15" : "zEC12";
351bdd1243dSDimitry Andric     case 3931:
352bdd1243dSDimitry Andric     case 3932:
353bdd1243dSDimitry Andric     default:
354bdd1243dSDimitry Andric       return HaveVectorSupport? "z16" : "zEC12";
355bdd1243dSDimitry Andric   }
356bdd1243dSDimitry Andric }
357bdd1243dSDimitry Andric } // end anonymous namespace
358bdd1243dSDimitry Andric 
359bdd1243dSDimitry Andric StringRef sys::detail::getHostCPUNameForS390x(StringRef ProcCpuinfoContent) {
360bdd1243dSDimitry Andric   // STIDP is a privileged operation, so use /proc/cpuinfo instead.
361bdd1243dSDimitry Andric 
362bdd1243dSDimitry Andric   // The "processor 0:" line comes after a fair amount of other information,
363bdd1243dSDimitry Andric   // including a cache breakdown, but this should be plenty.
364bdd1243dSDimitry Andric   SmallVector<StringRef, 32> Lines;
365bdd1243dSDimitry Andric   ProcCpuinfoContent.split(Lines, "\n");
366bdd1243dSDimitry Andric 
367bdd1243dSDimitry Andric   // Look for the CPU features.
368bdd1243dSDimitry Andric   SmallVector<StringRef, 32> CPUFeatures;
369bdd1243dSDimitry Andric   for (unsigned I = 0, E = Lines.size(); I != E; ++I)
370*5f757f3fSDimitry Andric     if (Lines[I].starts_with("features")) {
371bdd1243dSDimitry Andric       size_t Pos = Lines[I].find(':');
372bdd1243dSDimitry Andric       if (Pos != StringRef::npos) {
373bdd1243dSDimitry Andric         Lines[I].drop_front(Pos + 1).split(CPUFeatures, ' ');
374bdd1243dSDimitry Andric         break;
375bdd1243dSDimitry Andric       }
376bdd1243dSDimitry Andric     }
377bdd1243dSDimitry Andric 
378bdd1243dSDimitry Andric   // We need to check for the presence of vector support independently of
379bdd1243dSDimitry Andric   // the machine type, since we may only use the vector register set when
380bdd1243dSDimitry Andric   // supported by the kernel (and hypervisor).
381bdd1243dSDimitry Andric   bool HaveVectorSupport = false;
382bdd1243dSDimitry Andric   for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) {
383bdd1243dSDimitry Andric     if (CPUFeatures[I] == "vx")
384bdd1243dSDimitry Andric       HaveVectorSupport = true;
385bdd1243dSDimitry Andric   }
386bdd1243dSDimitry Andric 
387bdd1243dSDimitry Andric   // Now check the processor machine type.
388bdd1243dSDimitry Andric   for (unsigned I = 0, E = Lines.size(); I != E; ++I) {
389*5f757f3fSDimitry Andric     if (Lines[I].starts_with("processor ")) {
390bdd1243dSDimitry Andric       size_t Pos = Lines[I].find("machine = ");
391bdd1243dSDimitry Andric       if (Pos != StringRef::npos) {
392bdd1243dSDimitry Andric         Pos += sizeof("machine = ") - 1;
393bdd1243dSDimitry Andric         unsigned int Id;
394bdd1243dSDimitry Andric         if (!Lines[I].drop_front(Pos).getAsInteger(10, Id))
395bdd1243dSDimitry Andric           return getCPUNameFromS390Model(Id, HaveVectorSupport);
396bdd1243dSDimitry Andric       }
397bdd1243dSDimitry Andric       break;
398bdd1243dSDimitry Andric     }
399bdd1243dSDimitry Andric   }
400bdd1243dSDimitry Andric 
401bdd1243dSDimitry Andric   return "generic";
402bdd1243dSDimitry Andric }
403bdd1243dSDimitry Andric 
404bdd1243dSDimitry Andric StringRef sys::detail::getHostCPUNameForRISCV(StringRef ProcCpuinfoContent) {
405bdd1243dSDimitry Andric   // There are 24 lines in /proc/cpuinfo
406bdd1243dSDimitry Andric   SmallVector<StringRef> Lines;
407bdd1243dSDimitry Andric   ProcCpuinfoContent.split(Lines, "\n");
408bdd1243dSDimitry Andric 
409bdd1243dSDimitry Andric   // Look for uarch line to determine cpu name
410bdd1243dSDimitry Andric   StringRef UArch;
411bdd1243dSDimitry Andric   for (unsigned I = 0, E = Lines.size(); I != E; ++I) {
412*5f757f3fSDimitry Andric     if (Lines[I].starts_with("uarch")) {
413bdd1243dSDimitry Andric       UArch = Lines[I].substr(5).ltrim("\t :");
414bdd1243dSDimitry Andric       break;
415bdd1243dSDimitry Andric     }
416bdd1243dSDimitry Andric   }
417bdd1243dSDimitry Andric 
418bdd1243dSDimitry Andric   return StringSwitch<const char *>(UArch)
419bdd1243dSDimitry Andric       .Case("sifive,u74-mc", "sifive-u74")
420bdd1243dSDimitry Andric       .Case("sifive,bullet0", "sifive-u74")
421bdd1243dSDimitry Andric       .Default("generic");
422bdd1243dSDimitry Andric }
423bdd1243dSDimitry Andric 
424bdd1243dSDimitry Andric StringRef sys::detail::getHostCPUNameForBPF() {
425bdd1243dSDimitry Andric #if !defined(__linux__) || !defined(__x86_64__)
426bdd1243dSDimitry Andric   return "generic";
427bdd1243dSDimitry Andric #else
428bdd1243dSDimitry Andric   uint8_t v3_insns[40] __attribute__ ((aligned (8))) =
429bdd1243dSDimitry Andric       /* BPF_MOV64_IMM(BPF_REG_0, 0) */
430bdd1243dSDimitry Andric     { 0xb7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
431bdd1243dSDimitry Andric       /* BPF_MOV64_IMM(BPF_REG_2, 1) */
432bdd1243dSDimitry Andric       0xb7, 0x2, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
433bdd1243dSDimitry Andric       /* BPF_JMP32_REG(BPF_JLT, BPF_REG_0, BPF_REG_2, 1) */
434bdd1243dSDimitry Andric       0xae, 0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0,
435bdd1243dSDimitry Andric       /* BPF_MOV64_IMM(BPF_REG_0, 1) */
436bdd1243dSDimitry Andric       0xb7, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
437bdd1243dSDimitry Andric       /* BPF_EXIT_INSN() */
438bdd1243dSDimitry Andric       0x95, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
439bdd1243dSDimitry Andric 
440bdd1243dSDimitry Andric   uint8_t v2_insns[40] __attribute__ ((aligned (8))) =
441bdd1243dSDimitry Andric       /* BPF_MOV64_IMM(BPF_REG_0, 0) */
442bdd1243dSDimitry Andric     { 0xb7, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
443bdd1243dSDimitry Andric       /* BPF_MOV64_IMM(BPF_REG_2, 1) */
444bdd1243dSDimitry Andric       0xb7, 0x2, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
445bdd1243dSDimitry Andric       /* BPF_JMP_REG(BPF_JLT, BPF_REG_0, BPF_REG_2, 1) */
446bdd1243dSDimitry Andric       0xad, 0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0,
447bdd1243dSDimitry Andric       /* BPF_MOV64_IMM(BPF_REG_0, 1) */
448bdd1243dSDimitry Andric       0xb7, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,
449bdd1243dSDimitry Andric       /* BPF_EXIT_INSN() */
450bdd1243dSDimitry Andric       0x95, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
451bdd1243dSDimitry Andric 
452bdd1243dSDimitry Andric   struct bpf_prog_load_attr {
453bdd1243dSDimitry Andric     uint32_t prog_type;
454bdd1243dSDimitry Andric     uint32_t insn_cnt;
455bdd1243dSDimitry Andric     uint64_t insns;
456bdd1243dSDimitry Andric     uint64_t license;
457bdd1243dSDimitry Andric     uint32_t log_level;
458bdd1243dSDimitry Andric     uint32_t log_size;
459bdd1243dSDimitry Andric     uint64_t log_buf;
460bdd1243dSDimitry Andric     uint32_t kern_version;
461bdd1243dSDimitry Andric     uint32_t prog_flags;
462bdd1243dSDimitry Andric   } attr = {};
463bdd1243dSDimitry Andric   attr.prog_type = 1; /* BPF_PROG_TYPE_SOCKET_FILTER */
464bdd1243dSDimitry Andric   attr.insn_cnt = 5;
465bdd1243dSDimitry Andric   attr.insns = (uint64_t)v3_insns;
466bdd1243dSDimitry Andric   attr.license = (uint64_t)"DUMMY";
467bdd1243dSDimitry Andric 
468bdd1243dSDimitry Andric   int fd = syscall(321 /* __NR_bpf */, 5 /* BPF_PROG_LOAD */, &attr,
469bdd1243dSDimitry Andric                    sizeof(attr));
470bdd1243dSDimitry Andric   if (fd >= 0) {
471bdd1243dSDimitry Andric     close(fd);
472bdd1243dSDimitry Andric     return "v3";
473bdd1243dSDimitry Andric   }
474bdd1243dSDimitry Andric 
475bdd1243dSDimitry Andric   /* Clear the whole attr in case its content changed by syscall. */
476bdd1243dSDimitry Andric   memset(&attr, 0, sizeof(attr));
477bdd1243dSDimitry Andric   attr.prog_type = 1; /* BPF_PROG_TYPE_SOCKET_FILTER */
478bdd1243dSDimitry Andric   attr.insn_cnt = 5;
479bdd1243dSDimitry Andric   attr.insns = (uint64_t)v2_insns;
480bdd1243dSDimitry Andric   attr.license = (uint64_t)"DUMMY";
481bdd1243dSDimitry Andric   fd = syscall(321 /* __NR_bpf */, 5 /* BPF_PROG_LOAD */, &attr, sizeof(attr));
482bdd1243dSDimitry Andric   if (fd >= 0) {
483bdd1243dSDimitry Andric     close(fd);
484bdd1243dSDimitry Andric     return "v2";
485bdd1243dSDimitry Andric   }
486bdd1243dSDimitry Andric   return "v1";
487bdd1243dSDimitry Andric #endif
488bdd1243dSDimitry Andric }
489bdd1243dSDimitry Andric 
490bdd1243dSDimitry Andric #if defined(__i386__) || defined(_M_IX86) || \
491bdd1243dSDimitry Andric     defined(__x86_64__) || defined(_M_X64)
492bdd1243dSDimitry Andric 
493bdd1243dSDimitry Andric // The check below for i386 was copied from clang's cpuid.h (__get_cpuid_max).
494bdd1243dSDimitry Andric // Check motivated by bug reports for OpenSSL crashing on CPUs without CPUID
495bdd1243dSDimitry Andric // support. Consequently, for i386, the presence of CPUID is checked first
496bdd1243dSDimitry Andric // via the corresponding eflags bit.
497bdd1243dSDimitry Andric // Removal of cpuid.h header motivated by PR30384
498bdd1243dSDimitry Andric // Header cpuid.h and method __get_cpuid_max are not used in llvm, clang, openmp
499bdd1243dSDimitry Andric // or test-suite, but are used in external projects e.g. libstdcxx
500bdd1243dSDimitry Andric static bool isCpuIdSupported() {
501bdd1243dSDimitry Andric #if defined(__GNUC__) || defined(__clang__)
502bdd1243dSDimitry Andric #if defined(__i386__)
503bdd1243dSDimitry Andric   int __cpuid_supported;
504bdd1243dSDimitry Andric   __asm__("  pushfl\n"
505bdd1243dSDimitry Andric           "  popl   %%eax\n"
506bdd1243dSDimitry Andric           "  movl   %%eax,%%ecx\n"
507bdd1243dSDimitry Andric           "  xorl   $0x00200000,%%eax\n"
508bdd1243dSDimitry Andric           "  pushl  %%eax\n"
509bdd1243dSDimitry Andric           "  popfl\n"
510bdd1243dSDimitry Andric           "  pushfl\n"
511bdd1243dSDimitry Andric           "  popl   %%eax\n"
512bdd1243dSDimitry Andric           "  movl   $0,%0\n"
513bdd1243dSDimitry Andric           "  cmpl   %%eax,%%ecx\n"
514bdd1243dSDimitry Andric           "  je     1f\n"
515bdd1243dSDimitry Andric           "  movl   $1,%0\n"
516bdd1243dSDimitry Andric           "1:"
517bdd1243dSDimitry Andric           : "=r"(__cpuid_supported)
518bdd1243dSDimitry Andric           :
519bdd1243dSDimitry Andric           : "eax", "ecx");
520bdd1243dSDimitry Andric   if (!__cpuid_supported)
521bdd1243dSDimitry Andric     return false;
522bdd1243dSDimitry Andric #endif
523bdd1243dSDimitry Andric   return true;
524bdd1243dSDimitry Andric #endif
525bdd1243dSDimitry Andric   return true;
526bdd1243dSDimitry Andric }
527bdd1243dSDimitry Andric 
528bdd1243dSDimitry Andric /// getX86CpuIDAndInfo - Execute the specified cpuid and return the 4 values in
529bdd1243dSDimitry Andric /// the specified arguments.  If we can't run cpuid on the host, return true.
530bdd1243dSDimitry Andric static bool getX86CpuIDAndInfo(unsigned value, unsigned *rEAX, unsigned *rEBX,
531bdd1243dSDimitry Andric                                unsigned *rECX, unsigned *rEDX) {
532bdd1243dSDimitry Andric #if defined(__GNUC__) || defined(__clang__)
533bdd1243dSDimitry Andric #if defined(__x86_64__)
534bdd1243dSDimitry Andric   // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
535bdd1243dSDimitry Andric   // FIXME: should we save this for Clang?
536bdd1243dSDimitry Andric   __asm__("movq\t%%rbx, %%rsi\n\t"
537bdd1243dSDimitry Andric           "cpuid\n\t"
538bdd1243dSDimitry Andric           "xchgq\t%%rbx, %%rsi\n\t"
539bdd1243dSDimitry Andric           : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
540bdd1243dSDimitry Andric           : "a"(value));
541bdd1243dSDimitry Andric   return false;
542bdd1243dSDimitry Andric #elif defined(__i386__)
543bdd1243dSDimitry Andric   __asm__("movl\t%%ebx, %%esi\n\t"
544bdd1243dSDimitry Andric           "cpuid\n\t"
545bdd1243dSDimitry Andric           "xchgl\t%%ebx, %%esi\n\t"
546bdd1243dSDimitry Andric           : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
547bdd1243dSDimitry Andric           : "a"(value));
548bdd1243dSDimitry Andric   return false;
549bdd1243dSDimitry Andric #else
550bdd1243dSDimitry Andric   return true;
551bdd1243dSDimitry Andric #endif
552bdd1243dSDimitry Andric #elif defined(_MSC_VER)
553bdd1243dSDimitry Andric   // The MSVC intrinsic is portable across x86 and x64.
554bdd1243dSDimitry Andric   int registers[4];
555bdd1243dSDimitry Andric   __cpuid(registers, value);
556bdd1243dSDimitry Andric   *rEAX = registers[0];
557bdd1243dSDimitry Andric   *rEBX = registers[1];
558bdd1243dSDimitry Andric   *rECX = registers[2];
559bdd1243dSDimitry Andric   *rEDX = registers[3];
560bdd1243dSDimitry Andric   return false;
561bdd1243dSDimitry Andric #else
562bdd1243dSDimitry Andric   return true;
563bdd1243dSDimitry Andric #endif
564bdd1243dSDimitry Andric }
565bdd1243dSDimitry Andric 
566bdd1243dSDimitry Andric namespace llvm {
567bdd1243dSDimitry Andric namespace sys {
568bdd1243dSDimitry Andric namespace detail {
569bdd1243dSDimitry Andric namespace x86 {
570bdd1243dSDimitry Andric 
571bdd1243dSDimitry Andric VendorSignatures getVendorSignature(unsigned *MaxLeaf) {
572bdd1243dSDimitry Andric   unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
573bdd1243dSDimitry Andric   if (MaxLeaf == nullptr)
574bdd1243dSDimitry Andric     MaxLeaf = &EAX;
575bdd1243dSDimitry Andric   else
576bdd1243dSDimitry Andric     *MaxLeaf = 0;
577bdd1243dSDimitry Andric 
578bdd1243dSDimitry Andric   if (!isCpuIdSupported())
579bdd1243dSDimitry Andric     return VendorSignatures::UNKNOWN;
580bdd1243dSDimitry Andric 
581bdd1243dSDimitry Andric   if (getX86CpuIDAndInfo(0, MaxLeaf, &EBX, &ECX, &EDX) || *MaxLeaf < 1)
582bdd1243dSDimitry Andric     return VendorSignatures::UNKNOWN;
583bdd1243dSDimitry Andric 
584bdd1243dSDimitry Andric   // "Genu ineI ntel"
585bdd1243dSDimitry Andric   if (EBX == 0x756e6547 && EDX == 0x49656e69 && ECX == 0x6c65746e)
586bdd1243dSDimitry Andric     return VendorSignatures::GENUINE_INTEL;
587bdd1243dSDimitry Andric 
588bdd1243dSDimitry Andric   // "Auth enti cAMD"
589bdd1243dSDimitry Andric   if (EBX == 0x68747541 && EDX == 0x69746e65 && ECX == 0x444d4163)
590bdd1243dSDimitry Andric     return VendorSignatures::AUTHENTIC_AMD;
591bdd1243dSDimitry Andric 
592bdd1243dSDimitry Andric   return VendorSignatures::UNKNOWN;
593bdd1243dSDimitry Andric }
594bdd1243dSDimitry Andric 
595bdd1243dSDimitry Andric } // namespace x86
596bdd1243dSDimitry Andric } // namespace detail
597bdd1243dSDimitry Andric } // namespace sys
598bdd1243dSDimitry Andric } // namespace llvm
599bdd1243dSDimitry Andric 
600bdd1243dSDimitry Andric using namespace llvm::sys::detail::x86;
601bdd1243dSDimitry Andric 
602bdd1243dSDimitry Andric /// getX86CpuIDAndInfoEx - Execute the specified cpuid with subleaf and return
603bdd1243dSDimitry Andric /// the 4 values in the specified arguments.  If we can't run cpuid on the host,
604bdd1243dSDimitry Andric /// return true.
605bdd1243dSDimitry Andric static bool getX86CpuIDAndInfoEx(unsigned value, unsigned subleaf,
606bdd1243dSDimitry Andric                                  unsigned *rEAX, unsigned *rEBX, unsigned *rECX,
607bdd1243dSDimitry Andric                                  unsigned *rEDX) {
608bdd1243dSDimitry Andric #if defined(__GNUC__) || defined(__clang__)
609bdd1243dSDimitry Andric #if defined(__x86_64__)
610bdd1243dSDimitry Andric   // gcc doesn't know cpuid would clobber ebx/rbx. Preserve it manually.
611bdd1243dSDimitry Andric   // FIXME: should we save this for Clang?
612bdd1243dSDimitry Andric   __asm__("movq\t%%rbx, %%rsi\n\t"
613bdd1243dSDimitry Andric           "cpuid\n\t"
614bdd1243dSDimitry Andric           "xchgq\t%%rbx, %%rsi\n\t"
615bdd1243dSDimitry Andric           : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
616bdd1243dSDimitry Andric           : "a"(value), "c"(subleaf));
617bdd1243dSDimitry Andric   return false;
618bdd1243dSDimitry Andric #elif defined(__i386__)
619bdd1243dSDimitry Andric   __asm__("movl\t%%ebx, %%esi\n\t"
620bdd1243dSDimitry Andric           "cpuid\n\t"
621bdd1243dSDimitry Andric           "xchgl\t%%ebx, %%esi\n\t"
622bdd1243dSDimitry Andric           : "=a"(*rEAX), "=S"(*rEBX), "=c"(*rECX), "=d"(*rEDX)
623bdd1243dSDimitry Andric           : "a"(value), "c"(subleaf));
624bdd1243dSDimitry Andric   return false;
625bdd1243dSDimitry Andric #else
626bdd1243dSDimitry Andric   return true;
627bdd1243dSDimitry Andric #endif
628bdd1243dSDimitry Andric #elif defined(_MSC_VER)
629bdd1243dSDimitry Andric   int registers[4];
630bdd1243dSDimitry Andric   __cpuidex(registers, value, subleaf);
631bdd1243dSDimitry Andric   *rEAX = registers[0];
632bdd1243dSDimitry Andric   *rEBX = registers[1];
633bdd1243dSDimitry Andric   *rECX = registers[2];
634bdd1243dSDimitry Andric   *rEDX = registers[3];
635bdd1243dSDimitry Andric   return false;
636bdd1243dSDimitry Andric #else
637bdd1243dSDimitry Andric   return true;
638bdd1243dSDimitry Andric #endif
639bdd1243dSDimitry Andric }
640bdd1243dSDimitry Andric 
641bdd1243dSDimitry Andric // Read control register 0 (XCR0). Used to detect features such as AVX.
642bdd1243dSDimitry Andric static bool getX86XCR0(unsigned *rEAX, unsigned *rEDX) {
643bdd1243dSDimitry Andric #if defined(__GNUC__) || defined(__clang__)
644bdd1243dSDimitry Andric   // Check xgetbv; this uses a .byte sequence instead of the instruction
645bdd1243dSDimitry Andric   // directly because older assemblers do not include support for xgetbv and
646bdd1243dSDimitry Andric   // there is no easy way to conditionally compile based on the assembler used.
647bdd1243dSDimitry Andric   __asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(*rEAX), "=d"(*rEDX) : "c"(0));
648bdd1243dSDimitry Andric   return false;
649bdd1243dSDimitry Andric #elif defined(_MSC_FULL_VER) && defined(_XCR_XFEATURE_ENABLED_MASK)
650bdd1243dSDimitry Andric   unsigned long long Result = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
651bdd1243dSDimitry Andric   *rEAX = Result;
652bdd1243dSDimitry Andric   *rEDX = Result >> 32;
653bdd1243dSDimitry Andric   return false;
654bdd1243dSDimitry Andric #else
655bdd1243dSDimitry Andric   return true;
656bdd1243dSDimitry Andric #endif
657bdd1243dSDimitry Andric }
658bdd1243dSDimitry Andric 
659bdd1243dSDimitry Andric static void detectX86FamilyModel(unsigned EAX, unsigned *Family,
660bdd1243dSDimitry Andric                                  unsigned *Model) {
661bdd1243dSDimitry Andric   *Family = (EAX >> 8) & 0xf; // Bits 8 - 11
662bdd1243dSDimitry Andric   *Model = (EAX >> 4) & 0xf;  // Bits 4 - 7
663bdd1243dSDimitry Andric   if (*Family == 6 || *Family == 0xf) {
664bdd1243dSDimitry Andric     if (*Family == 0xf)
665bdd1243dSDimitry Andric       // Examine extended family ID if family ID is F.
666bdd1243dSDimitry Andric       *Family += (EAX >> 20) & 0xff; // Bits 20 - 27
667bdd1243dSDimitry Andric     // Examine extended model ID if family ID is 6 or F.
668bdd1243dSDimitry Andric     *Model += ((EAX >> 16) & 0xf) << 4; // Bits 16 - 19
669bdd1243dSDimitry Andric   }
670bdd1243dSDimitry Andric }
671bdd1243dSDimitry Andric 
672bdd1243dSDimitry Andric static StringRef
673bdd1243dSDimitry Andric getIntelProcessorTypeAndSubtype(unsigned Family, unsigned Model,
674bdd1243dSDimitry Andric                                 const unsigned *Features,
675bdd1243dSDimitry Andric                                 unsigned *Type, unsigned *Subtype) {
676bdd1243dSDimitry Andric   auto testFeature = [&](unsigned F) {
677bdd1243dSDimitry Andric     return (Features[F / 32] & (1U << (F % 32))) != 0;
678bdd1243dSDimitry Andric   };
679bdd1243dSDimitry Andric 
680bdd1243dSDimitry Andric   StringRef CPU;
681bdd1243dSDimitry Andric 
682bdd1243dSDimitry Andric   switch (Family) {
683bdd1243dSDimitry Andric   case 3:
684bdd1243dSDimitry Andric     CPU = "i386";
685bdd1243dSDimitry Andric     break;
686bdd1243dSDimitry Andric   case 4:
687bdd1243dSDimitry Andric     CPU = "i486";
688bdd1243dSDimitry Andric     break;
689bdd1243dSDimitry Andric   case 5:
690bdd1243dSDimitry Andric     if (testFeature(X86::FEATURE_MMX)) {
691bdd1243dSDimitry Andric       CPU = "pentium-mmx";
692bdd1243dSDimitry Andric       break;
693bdd1243dSDimitry Andric     }
694bdd1243dSDimitry Andric     CPU = "pentium";
695bdd1243dSDimitry Andric     break;
696bdd1243dSDimitry Andric   case 6:
697bdd1243dSDimitry Andric     switch (Model) {
698bdd1243dSDimitry Andric     case 0x0f: // Intel Core 2 Duo processor, Intel Core 2 Duo mobile
699bdd1243dSDimitry Andric                // processor, Intel Core 2 Quad processor, Intel Core 2 Quad
700bdd1243dSDimitry Andric                // mobile processor, Intel Core 2 Extreme processor, Intel
701bdd1243dSDimitry Andric                // Pentium Dual-Core processor, Intel Xeon processor, model
702bdd1243dSDimitry Andric                // 0Fh. All processors are manufactured using the 65 nm process.
703bdd1243dSDimitry Andric     case 0x16: // Intel Celeron processor model 16h. All processors are
704bdd1243dSDimitry Andric                // manufactured using the 65 nm process
705bdd1243dSDimitry Andric       CPU = "core2";
706bdd1243dSDimitry Andric       *Type = X86::INTEL_CORE2;
707bdd1243dSDimitry Andric       break;
708bdd1243dSDimitry Andric     case 0x17: // Intel Core 2 Extreme processor, Intel Xeon processor, model
709bdd1243dSDimitry Andric                // 17h. All processors are manufactured using the 45 nm process.
710bdd1243dSDimitry Andric                //
711bdd1243dSDimitry Andric                // 45nm: Penryn , Wolfdale, Yorkfield (XE)
712bdd1243dSDimitry Andric     case 0x1d: // Intel Xeon processor MP. All processors are manufactured using
713bdd1243dSDimitry Andric                // the 45 nm process.
714bdd1243dSDimitry Andric       CPU = "penryn";
715bdd1243dSDimitry Andric       *Type = X86::INTEL_CORE2;
716bdd1243dSDimitry Andric       break;
717bdd1243dSDimitry Andric     case 0x1a: // Intel Core i7 processor and Intel Xeon processor. All
718bdd1243dSDimitry Andric                // processors are manufactured using the 45 nm process.
719bdd1243dSDimitry Andric     case 0x1e: // Intel(R) Core(TM) i7 CPU         870  @ 2.93GHz.
720bdd1243dSDimitry Andric                // As found in a Summer 2010 model iMac.
721bdd1243dSDimitry Andric     case 0x1f:
722bdd1243dSDimitry Andric     case 0x2e:              // Nehalem EX
723bdd1243dSDimitry Andric       CPU = "nehalem";
724bdd1243dSDimitry Andric       *Type = X86::INTEL_COREI7;
725bdd1243dSDimitry Andric       *Subtype = X86::INTEL_COREI7_NEHALEM;
726bdd1243dSDimitry Andric       break;
727bdd1243dSDimitry Andric     case 0x25: // Intel Core i7, laptop version.
728bdd1243dSDimitry Andric     case 0x2c: // Intel Core i7 processor and Intel Xeon processor. All
729bdd1243dSDimitry Andric                // processors are manufactured using the 32 nm process.
730bdd1243dSDimitry Andric     case 0x2f: // Westmere EX
731bdd1243dSDimitry Andric       CPU = "westmere";
732bdd1243dSDimitry Andric       *Type = X86::INTEL_COREI7;
733bdd1243dSDimitry Andric       *Subtype = X86::INTEL_COREI7_WESTMERE;
734bdd1243dSDimitry Andric       break;
735bdd1243dSDimitry Andric     case 0x2a: // Intel Core i7 processor. All processors are manufactured
736bdd1243dSDimitry Andric                // using the 32 nm process.
737bdd1243dSDimitry Andric     case 0x2d:
738bdd1243dSDimitry Andric       CPU = "sandybridge";
739bdd1243dSDimitry Andric       *Type = X86::INTEL_COREI7;
740bdd1243dSDimitry Andric       *Subtype = X86::INTEL_COREI7_SANDYBRIDGE;
741bdd1243dSDimitry Andric       break;
742bdd1243dSDimitry Andric     case 0x3a:
743bdd1243dSDimitry Andric     case 0x3e:              // Ivy Bridge EP
744bdd1243dSDimitry Andric       CPU = "ivybridge";
745bdd1243dSDimitry Andric       *Type = X86::INTEL_COREI7;
746bdd1243dSDimitry Andric       *Subtype = X86::INTEL_COREI7_IVYBRIDGE;
747bdd1243dSDimitry Andric       break;
748bdd1243dSDimitry Andric 
749bdd1243dSDimitry Andric     // Haswell:
750bdd1243dSDimitry Andric     case 0x3c:
751bdd1243dSDimitry Andric     case 0x3f:
752bdd1243dSDimitry Andric     case 0x45:
753bdd1243dSDimitry Andric     case 0x46:
754bdd1243dSDimitry Andric       CPU = "haswell";
755bdd1243dSDimitry Andric       *Type = X86::INTEL_COREI7;
756bdd1243dSDimitry Andric       *Subtype = X86::INTEL_COREI7_HASWELL;
757bdd1243dSDimitry Andric       break;
758bdd1243dSDimitry Andric 
759bdd1243dSDimitry Andric     // Broadwell:
760bdd1243dSDimitry Andric     case 0x3d:
761bdd1243dSDimitry Andric     case 0x47:
762bdd1243dSDimitry Andric     case 0x4f:
763bdd1243dSDimitry Andric     case 0x56:
764bdd1243dSDimitry Andric       CPU = "broadwell";
765bdd1243dSDimitry Andric       *Type = X86::INTEL_COREI7;
766bdd1243dSDimitry Andric       *Subtype = X86::INTEL_COREI7_BROADWELL;
767bdd1243dSDimitry Andric       break;
768bdd1243dSDimitry Andric 
769bdd1243dSDimitry Andric     // Skylake:
770bdd1243dSDimitry Andric     case 0x4e:              // Skylake mobile
771bdd1243dSDimitry Andric     case 0x5e:              // Skylake desktop
772bdd1243dSDimitry Andric     case 0x8e:              // Kaby Lake mobile
773bdd1243dSDimitry Andric     case 0x9e:              // Kaby Lake desktop
774bdd1243dSDimitry Andric     case 0xa5:              // Comet Lake-H/S
775bdd1243dSDimitry Andric     case 0xa6:              // Comet Lake-U
776bdd1243dSDimitry Andric       CPU = "skylake";
777bdd1243dSDimitry Andric       *Type = X86::INTEL_COREI7;
778bdd1243dSDimitry Andric       *Subtype = X86::INTEL_COREI7_SKYLAKE;
779bdd1243dSDimitry Andric       break;
780bdd1243dSDimitry Andric 
781bdd1243dSDimitry Andric     // Rocketlake:
782bdd1243dSDimitry Andric     case 0xa7:
783bdd1243dSDimitry Andric       CPU = "rocketlake";
784bdd1243dSDimitry Andric       *Type = X86::INTEL_COREI7;
785bdd1243dSDimitry Andric       *Subtype = X86::INTEL_COREI7_ROCKETLAKE;
786bdd1243dSDimitry Andric       break;
787bdd1243dSDimitry Andric 
788bdd1243dSDimitry Andric     // Skylake Xeon:
789bdd1243dSDimitry Andric     case 0x55:
790bdd1243dSDimitry Andric       *Type = X86::INTEL_COREI7;
791bdd1243dSDimitry Andric       if (testFeature(X86::FEATURE_AVX512BF16)) {
792bdd1243dSDimitry Andric         CPU = "cooperlake";
793bdd1243dSDimitry Andric         *Subtype = X86::INTEL_COREI7_COOPERLAKE;
794bdd1243dSDimitry Andric       } else if (testFeature(X86::FEATURE_AVX512VNNI)) {
795bdd1243dSDimitry Andric         CPU = "cascadelake";
796bdd1243dSDimitry Andric         *Subtype = X86::INTEL_COREI7_CASCADELAKE;
797bdd1243dSDimitry Andric       } else {
798bdd1243dSDimitry Andric         CPU = "skylake-avx512";
799bdd1243dSDimitry Andric         *Subtype = X86::INTEL_COREI7_SKYLAKE_AVX512;
800bdd1243dSDimitry Andric       }
801bdd1243dSDimitry Andric       break;
802bdd1243dSDimitry Andric 
803bdd1243dSDimitry Andric     // Cannonlake:
804bdd1243dSDimitry Andric     case 0x66:
805bdd1243dSDimitry Andric       CPU = "cannonlake";
806bdd1243dSDimitry Andric       *Type = X86::INTEL_COREI7;
807bdd1243dSDimitry Andric       *Subtype = X86::INTEL_COREI7_CANNONLAKE;
808bdd1243dSDimitry Andric       break;
809bdd1243dSDimitry Andric 
810bdd1243dSDimitry Andric     // Icelake:
811bdd1243dSDimitry Andric     case 0x7d:
812bdd1243dSDimitry Andric     case 0x7e:
813bdd1243dSDimitry Andric       CPU = "icelake-client";
814bdd1243dSDimitry Andric       *Type = X86::INTEL_COREI7;
815bdd1243dSDimitry Andric       *Subtype = X86::INTEL_COREI7_ICELAKE_CLIENT;
816bdd1243dSDimitry Andric       break;
817bdd1243dSDimitry Andric 
818bdd1243dSDimitry Andric     // Tigerlake:
819bdd1243dSDimitry Andric     case 0x8c:
820bdd1243dSDimitry Andric     case 0x8d:
821bdd1243dSDimitry Andric       CPU = "tigerlake";
822bdd1243dSDimitry Andric       *Type = X86::INTEL_COREI7;
823bdd1243dSDimitry Andric       *Subtype = X86::INTEL_COREI7_TIGERLAKE;
824bdd1243dSDimitry Andric       break;
825bdd1243dSDimitry Andric 
826bdd1243dSDimitry Andric     // Alderlake:
827bdd1243dSDimitry Andric     case 0x97:
828bdd1243dSDimitry Andric     case 0x9a:
829*5f757f3fSDimitry Andric     // Gracemont
830*5f757f3fSDimitry Andric     case 0xbe:
831bdd1243dSDimitry Andric     // Raptorlake:
832bdd1243dSDimitry Andric     case 0xb7:
833*5f757f3fSDimitry Andric     case 0xba:
834*5f757f3fSDimitry Andric     case 0xbf:
835bdd1243dSDimitry Andric     // Meteorlake:
836bdd1243dSDimitry Andric     case 0xaa:
837bdd1243dSDimitry Andric     case 0xac:
838bdd1243dSDimitry Andric       CPU = "alderlake";
839bdd1243dSDimitry Andric       *Type = X86::INTEL_COREI7;
840bdd1243dSDimitry Andric       *Subtype = X86::INTEL_COREI7_ALDERLAKE;
841bdd1243dSDimitry Andric       break;
842bdd1243dSDimitry Andric 
843*5f757f3fSDimitry Andric     // Arrowlake:
844*5f757f3fSDimitry Andric     case 0xc5:
845*5f757f3fSDimitry Andric       CPU = "arrowlake";
846*5f757f3fSDimitry Andric       *Type = X86::INTEL_COREI7;
847*5f757f3fSDimitry Andric       *Subtype = X86::INTEL_COREI7_ARROWLAKE;
848*5f757f3fSDimitry Andric       break;
849*5f757f3fSDimitry Andric 
850*5f757f3fSDimitry Andric     // Arrowlake S:
851*5f757f3fSDimitry Andric     case 0xc6:
852*5f757f3fSDimitry Andric     // Lunarlake:
853*5f757f3fSDimitry Andric     case 0xbd:
854*5f757f3fSDimitry Andric       CPU = "arrowlake-s";
855*5f757f3fSDimitry Andric       *Type = X86::INTEL_COREI7;
856*5f757f3fSDimitry Andric       *Subtype = X86::INTEL_COREI7_ARROWLAKE_S;
857*5f757f3fSDimitry Andric       break;
858*5f757f3fSDimitry Andric 
859*5f757f3fSDimitry Andric     // Pantherlake:
860*5f757f3fSDimitry Andric     case 0xcc:
861*5f757f3fSDimitry Andric       CPU = "pantherlake";
862*5f757f3fSDimitry Andric       *Type = X86::INTEL_COREI7;
863*5f757f3fSDimitry Andric       *Subtype = X86::INTEL_COREI7_PANTHERLAKE;
864*5f757f3fSDimitry Andric       break;
865*5f757f3fSDimitry Andric 
866bdd1243dSDimitry Andric     // Graniterapids:
867bdd1243dSDimitry Andric     case 0xad:
868bdd1243dSDimitry Andric       CPU = "graniterapids";
869bdd1243dSDimitry Andric       *Type = X86::INTEL_COREI7;
870bdd1243dSDimitry Andric       *Subtype = X86::INTEL_COREI7_GRANITERAPIDS;
871bdd1243dSDimitry Andric       break;
872bdd1243dSDimitry Andric 
87306c3fb27SDimitry Andric     // Granite Rapids D:
87406c3fb27SDimitry Andric     case 0xae:
87506c3fb27SDimitry Andric       CPU = "graniterapids-d";
87606c3fb27SDimitry Andric       *Type = X86::INTEL_COREI7;
87706c3fb27SDimitry Andric       *Subtype = X86::INTEL_COREI7_GRANITERAPIDS_D;
87806c3fb27SDimitry Andric       break;
87906c3fb27SDimitry Andric 
880bdd1243dSDimitry Andric     // Icelake Xeon:
881bdd1243dSDimitry Andric     case 0x6a:
882bdd1243dSDimitry Andric     case 0x6c:
883bdd1243dSDimitry Andric       CPU = "icelake-server";
884bdd1243dSDimitry Andric       *Type = X86::INTEL_COREI7;
885bdd1243dSDimitry Andric       *Subtype = X86::INTEL_COREI7_ICELAKE_SERVER;
886bdd1243dSDimitry Andric       break;
887bdd1243dSDimitry Andric 
888bdd1243dSDimitry Andric     // Emerald Rapids:
889bdd1243dSDimitry Andric     case 0xcf:
890bdd1243dSDimitry Andric     // Sapphire Rapids:
891bdd1243dSDimitry Andric     case 0x8f:
892bdd1243dSDimitry Andric       CPU = "sapphirerapids";
893bdd1243dSDimitry Andric       *Type = X86::INTEL_COREI7;
894bdd1243dSDimitry Andric       *Subtype = X86::INTEL_COREI7_SAPPHIRERAPIDS;
895bdd1243dSDimitry Andric       break;
896bdd1243dSDimitry Andric 
897bdd1243dSDimitry Andric     case 0x1c: // Most 45 nm Intel Atom processors
898bdd1243dSDimitry Andric     case 0x26: // 45 nm Atom Lincroft
899bdd1243dSDimitry Andric     case 0x27: // 32 nm Atom Medfield
900bdd1243dSDimitry Andric     case 0x35: // 32 nm Atom Midview
901bdd1243dSDimitry Andric     case 0x36: // 32 nm Atom Midview
902bdd1243dSDimitry Andric       CPU = "bonnell";
903bdd1243dSDimitry Andric       *Type = X86::INTEL_BONNELL;
904bdd1243dSDimitry Andric       break;
905bdd1243dSDimitry Andric 
906bdd1243dSDimitry Andric     // Atom Silvermont codes from the Intel software optimization guide.
907bdd1243dSDimitry Andric     case 0x37:
908bdd1243dSDimitry Andric     case 0x4a:
909bdd1243dSDimitry Andric     case 0x4d:
910bdd1243dSDimitry Andric     case 0x5a:
911bdd1243dSDimitry Andric     case 0x5d:
912bdd1243dSDimitry Andric     case 0x4c: // really airmont
913bdd1243dSDimitry Andric       CPU = "silvermont";
914bdd1243dSDimitry Andric       *Type = X86::INTEL_SILVERMONT;
915bdd1243dSDimitry Andric       break;
916bdd1243dSDimitry Andric     // Goldmont:
917bdd1243dSDimitry Andric     case 0x5c: // Apollo Lake
918bdd1243dSDimitry Andric     case 0x5f: // Denverton
919bdd1243dSDimitry Andric       CPU = "goldmont";
920bdd1243dSDimitry Andric       *Type = X86::INTEL_GOLDMONT;
921bdd1243dSDimitry Andric       break;
922bdd1243dSDimitry Andric     case 0x7a:
923bdd1243dSDimitry Andric       CPU = "goldmont-plus";
924bdd1243dSDimitry Andric       *Type = X86::INTEL_GOLDMONT_PLUS;
925bdd1243dSDimitry Andric       break;
926bdd1243dSDimitry Andric     case 0x86:
927*5f757f3fSDimitry Andric     case 0x8a: // Lakefield
928*5f757f3fSDimitry Andric     case 0x96: // Elkhart Lake
929*5f757f3fSDimitry Andric     case 0x9c: // Jasper Lake
930bdd1243dSDimitry Andric       CPU = "tremont";
931bdd1243dSDimitry Andric       *Type = X86::INTEL_TREMONT;
932bdd1243dSDimitry Andric       break;
933bdd1243dSDimitry Andric 
934bdd1243dSDimitry Andric     // Sierraforest:
935bdd1243dSDimitry Andric     case 0xaf:
936bdd1243dSDimitry Andric       CPU = "sierraforest";
937bdd1243dSDimitry Andric       *Type = X86::INTEL_SIERRAFOREST;
938bdd1243dSDimitry Andric       break;
939bdd1243dSDimitry Andric 
940bdd1243dSDimitry Andric     // Grandridge:
941bdd1243dSDimitry Andric     case 0xb6:
942bdd1243dSDimitry Andric       CPU = "grandridge";
943bdd1243dSDimitry Andric       *Type = X86::INTEL_GRANDRIDGE;
944bdd1243dSDimitry Andric       break;
945bdd1243dSDimitry Andric 
946*5f757f3fSDimitry Andric     // Clearwaterforest:
947*5f757f3fSDimitry Andric     case 0xdd:
948*5f757f3fSDimitry Andric       CPU = "clearwaterforest";
949*5f757f3fSDimitry Andric       *Type = X86::INTEL_CLEARWATERFOREST;
950*5f757f3fSDimitry Andric       break;
951*5f757f3fSDimitry Andric 
952bdd1243dSDimitry Andric     // Xeon Phi (Knights Landing + Knights Mill):
953bdd1243dSDimitry Andric     case 0x57:
954bdd1243dSDimitry Andric       CPU = "knl";
955bdd1243dSDimitry Andric       *Type = X86::INTEL_KNL;
956bdd1243dSDimitry Andric       break;
957bdd1243dSDimitry Andric     case 0x85:
958bdd1243dSDimitry Andric       CPU = "knm";
959bdd1243dSDimitry Andric       *Type = X86::INTEL_KNM;
960bdd1243dSDimitry Andric       break;
961bdd1243dSDimitry Andric 
962bdd1243dSDimitry Andric     default: // Unknown family 6 CPU, try to guess.
963bdd1243dSDimitry Andric       // Don't both with Type/Subtype here, they aren't used by the caller.
964bdd1243dSDimitry Andric       // They're used above to keep the code in sync with compiler-rt.
965bdd1243dSDimitry Andric       // TODO detect tigerlake host from model
966bdd1243dSDimitry Andric       if (testFeature(X86::FEATURE_AVX512VP2INTERSECT)) {
967bdd1243dSDimitry Andric         CPU = "tigerlake";
968bdd1243dSDimitry Andric       } else if (testFeature(X86::FEATURE_AVX512VBMI2)) {
969bdd1243dSDimitry Andric         CPU = "icelake-client";
970bdd1243dSDimitry Andric       } else if (testFeature(X86::FEATURE_AVX512VBMI)) {
971bdd1243dSDimitry Andric         CPU = "cannonlake";
972bdd1243dSDimitry Andric       } else if (testFeature(X86::FEATURE_AVX512BF16)) {
973bdd1243dSDimitry Andric         CPU = "cooperlake";
974bdd1243dSDimitry Andric       } else if (testFeature(X86::FEATURE_AVX512VNNI)) {
975bdd1243dSDimitry Andric         CPU = "cascadelake";
976bdd1243dSDimitry Andric       } else if (testFeature(X86::FEATURE_AVX512VL)) {
977bdd1243dSDimitry Andric         CPU = "skylake-avx512";
978bdd1243dSDimitry Andric       } else if (testFeature(X86::FEATURE_AVX512ER)) {
979bdd1243dSDimitry Andric         CPU = "knl";
980bdd1243dSDimitry Andric       } else if (testFeature(X86::FEATURE_CLFLUSHOPT)) {
981bdd1243dSDimitry Andric         if (testFeature(X86::FEATURE_SHA))
982bdd1243dSDimitry Andric           CPU = "goldmont";
983bdd1243dSDimitry Andric         else
984bdd1243dSDimitry Andric           CPU = "skylake";
985bdd1243dSDimitry Andric       } else if (testFeature(X86::FEATURE_ADX)) {
986bdd1243dSDimitry Andric         CPU = "broadwell";
987bdd1243dSDimitry Andric       } else if (testFeature(X86::FEATURE_AVX2)) {
988bdd1243dSDimitry Andric         CPU = "haswell";
989bdd1243dSDimitry Andric       } else if (testFeature(X86::FEATURE_AVX)) {
990bdd1243dSDimitry Andric         CPU = "sandybridge";
991bdd1243dSDimitry Andric       } else if (testFeature(X86::FEATURE_SSE4_2)) {
992bdd1243dSDimitry Andric         if (testFeature(X86::FEATURE_MOVBE))
993bdd1243dSDimitry Andric           CPU = "silvermont";
994bdd1243dSDimitry Andric         else
995bdd1243dSDimitry Andric           CPU = "nehalem";
996bdd1243dSDimitry Andric       } else if (testFeature(X86::FEATURE_SSE4_1)) {
997bdd1243dSDimitry Andric         CPU = "penryn";
998bdd1243dSDimitry Andric       } else if (testFeature(X86::FEATURE_SSSE3)) {
999bdd1243dSDimitry Andric         if (testFeature(X86::FEATURE_MOVBE))
1000bdd1243dSDimitry Andric           CPU = "bonnell";
1001bdd1243dSDimitry Andric         else
1002bdd1243dSDimitry Andric           CPU = "core2";
1003bdd1243dSDimitry Andric       } else if (testFeature(X86::FEATURE_64BIT)) {
1004bdd1243dSDimitry Andric         CPU = "core2";
1005bdd1243dSDimitry Andric       } else if (testFeature(X86::FEATURE_SSE3)) {
1006bdd1243dSDimitry Andric         CPU = "yonah";
1007bdd1243dSDimitry Andric       } else if (testFeature(X86::FEATURE_SSE2)) {
1008bdd1243dSDimitry Andric         CPU = "pentium-m";
1009bdd1243dSDimitry Andric       } else if (testFeature(X86::FEATURE_SSE)) {
1010bdd1243dSDimitry Andric         CPU = "pentium3";
1011bdd1243dSDimitry Andric       } else if (testFeature(X86::FEATURE_MMX)) {
1012bdd1243dSDimitry Andric         CPU = "pentium2";
1013bdd1243dSDimitry Andric       } else {
1014bdd1243dSDimitry Andric         CPU = "pentiumpro";
1015bdd1243dSDimitry Andric       }
1016bdd1243dSDimitry Andric       break;
1017bdd1243dSDimitry Andric     }
1018bdd1243dSDimitry Andric     break;
1019bdd1243dSDimitry Andric   case 15: {
1020bdd1243dSDimitry Andric     if (testFeature(X86::FEATURE_64BIT)) {
1021bdd1243dSDimitry Andric       CPU = "nocona";
1022bdd1243dSDimitry Andric       break;
1023bdd1243dSDimitry Andric     }
1024bdd1243dSDimitry Andric     if (testFeature(X86::FEATURE_SSE3)) {
1025bdd1243dSDimitry Andric       CPU = "prescott";
1026bdd1243dSDimitry Andric       break;
1027bdd1243dSDimitry Andric     }
1028bdd1243dSDimitry Andric     CPU = "pentium4";
1029bdd1243dSDimitry Andric     break;
1030bdd1243dSDimitry Andric   }
1031bdd1243dSDimitry Andric   default:
1032bdd1243dSDimitry Andric     break; // Unknown.
1033bdd1243dSDimitry Andric   }
1034bdd1243dSDimitry Andric 
1035bdd1243dSDimitry Andric   return CPU;
1036bdd1243dSDimitry Andric }
1037bdd1243dSDimitry Andric 
1038bdd1243dSDimitry Andric static StringRef
1039bdd1243dSDimitry Andric getAMDProcessorTypeAndSubtype(unsigned Family, unsigned Model,
1040bdd1243dSDimitry Andric                               const unsigned *Features,
1041bdd1243dSDimitry Andric                               unsigned *Type, unsigned *Subtype) {
1042bdd1243dSDimitry Andric   auto testFeature = [&](unsigned F) {
1043bdd1243dSDimitry Andric     return (Features[F / 32] & (1U << (F % 32))) != 0;
1044bdd1243dSDimitry Andric   };
1045bdd1243dSDimitry Andric 
1046bdd1243dSDimitry Andric   StringRef CPU;
1047bdd1243dSDimitry Andric 
1048bdd1243dSDimitry Andric   switch (Family) {
1049bdd1243dSDimitry Andric   case 4:
1050bdd1243dSDimitry Andric     CPU = "i486";
1051bdd1243dSDimitry Andric     break;
1052bdd1243dSDimitry Andric   case 5:
1053bdd1243dSDimitry Andric     CPU = "pentium";
1054bdd1243dSDimitry Andric     switch (Model) {
1055bdd1243dSDimitry Andric     case 6:
1056bdd1243dSDimitry Andric     case 7:
1057bdd1243dSDimitry Andric       CPU = "k6";
1058bdd1243dSDimitry Andric       break;
1059bdd1243dSDimitry Andric     case 8:
1060bdd1243dSDimitry Andric       CPU = "k6-2";
1061bdd1243dSDimitry Andric       break;
1062bdd1243dSDimitry Andric     case 9:
1063bdd1243dSDimitry Andric     case 13:
1064bdd1243dSDimitry Andric       CPU = "k6-3";
1065bdd1243dSDimitry Andric       break;
1066bdd1243dSDimitry Andric     case 10:
1067bdd1243dSDimitry Andric       CPU = "geode";
1068bdd1243dSDimitry Andric       break;
1069bdd1243dSDimitry Andric     }
1070bdd1243dSDimitry Andric     break;
1071bdd1243dSDimitry Andric   case 6:
1072bdd1243dSDimitry Andric     if (testFeature(X86::FEATURE_SSE)) {
1073bdd1243dSDimitry Andric       CPU = "athlon-xp";
1074bdd1243dSDimitry Andric       break;
1075bdd1243dSDimitry Andric     }
1076bdd1243dSDimitry Andric     CPU = "athlon";
1077bdd1243dSDimitry Andric     break;
1078bdd1243dSDimitry Andric   case 15:
1079bdd1243dSDimitry Andric     if (testFeature(X86::FEATURE_SSE3)) {
1080bdd1243dSDimitry Andric       CPU = "k8-sse3";
1081bdd1243dSDimitry Andric       break;
1082bdd1243dSDimitry Andric     }
1083bdd1243dSDimitry Andric     CPU = "k8";
1084bdd1243dSDimitry Andric     break;
1085bdd1243dSDimitry Andric   case 16:
1086bdd1243dSDimitry Andric     CPU = "amdfam10";
1087bdd1243dSDimitry Andric     *Type = X86::AMDFAM10H; // "amdfam10"
1088bdd1243dSDimitry Andric     switch (Model) {
1089bdd1243dSDimitry Andric     case 2:
1090bdd1243dSDimitry Andric       *Subtype = X86::AMDFAM10H_BARCELONA;
1091bdd1243dSDimitry Andric       break;
1092bdd1243dSDimitry Andric     case 4:
1093bdd1243dSDimitry Andric       *Subtype = X86::AMDFAM10H_SHANGHAI;
1094bdd1243dSDimitry Andric       break;
1095bdd1243dSDimitry Andric     case 8:
1096bdd1243dSDimitry Andric       *Subtype = X86::AMDFAM10H_ISTANBUL;
1097bdd1243dSDimitry Andric       break;
1098bdd1243dSDimitry Andric     }
1099bdd1243dSDimitry Andric     break;
1100bdd1243dSDimitry Andric   case 20:
1101bdd1243dSDimitry Andric     CPU = "btver1";
1102bdd1243dSDimitry Andric     *Type = X86::AMD_BTVER1;
1103bdd1243dSDimitry Andric     break;
1104bdd1243dSDimitry Andric   case 21:
1105bdd1243dSDimitry Andric     CPU = "bdver1";
1106bdd1243dSDimitry Andric     *Type = X86::AMDFAM15H;
1107bdd1243dSDimitry Andric     if (Model >= 0x60 && Model <= 0x7f) {
1108bdd1243dSDimitry Andric       CPU = "bdver4";
1109bdd1243dSDimitry Andric       *Subtype = X86::AMDFAM15H_BDVER4;
1110bdd1243dSDimitry Andric       break; // 60h-7Fh: Excavator
1111bdd1243dSDimitry Andric     }
1112bdd1243dSDimitry Andric     if (Model >= 0x30 && Model <= 0x3f) {
1113bdd1243dSDimitry Andric       CPU = "bdver3";
1114bdd1243dSDimitry Andric       *Subtype = X86::AMDFAM15H_BDVER3;
1115bdd1243dSDimitry Andric       break; // 30h-3Fh: Steamroller
1116bdd1243dSDimitry Andric     }
1117bdd1243dSDimitry Andric     if ((Model >= 0x10 && Model <= 0x1f) || Model == 0x02) {
1118bdd1243dSDimitry Andric       CPU = "bdver2";
1119bdd1243dSDimitry Andric       *Subtype = X86::AMDFAM15H_BDVER2;
1120bdd1243dSDimitry Andric       break; // 02h, 10h-1Fh: Piledriver
1121bdd1243dSDimitry Andric     }
1122bdd1243dSDimitry Andric     if (Model <= 0x0f) {
1123bdd1243dSDimitry Andric       *Subtype = X86::AMDFAM15H_BDVER1;
1124bdd1243dSDimitry Andric       break; // 00h-0Fh: Bulldozer
1125bdd1243dSDimitry Andric     }
1126bdd1243dSDimitry Andric     break;
1127bdd1243dSDimitry Andric   case 22:
1128bdd1243dSDimitry Andric     CPU = "btver2";
1129bdd1243dSDimitry Andric     *Type = X86::AMD_BTVER2;
1130bdd1243dSDimitry Andric     break;
1131bdd1243dSDimitry Andric   case 23:
1132bdd1243dSDimitry Andric     CPU = "znver1";
1133bdd1243dSDimitry Andric     *Type = X86::AMDFAM17H;
1134bdd1243dSDimitry Andric     if ((Model >= 0x30 && Model <= 0x3f) || Model == 0x71) {
1135bdd1243dSDimitry Andric       CPU = "znver2";
1136bdd1243dSDimitry Andric       *Subtype = X86::AMDFAM17H_ZNVER2;
1137bdd1243dSDimitry Andric       break; // 30h-3fh, 71h: Zen2
1138bdd1243dSDimitry Andric     }
1139bdd1243dSDimitry Andric     if (Model <= 0x0f) {
1140bdd1243dSDimitry Andric       *Subtype = X86::AMDFAM17H_ZNVER1;
1141bdd1243dSDimitry Andric       break; // 00h-0Fh: Zen1
1142bdd1243dSDimitry Andric     }
1143bdd1243dSDimitry Andric     break;
1144bdd1243dSDimitry Andric   case 25:
1145bdd1243dSDimitry Andric     CPU = "znver3";
1146bdd1243dSDimitry Andric     *Type = X86::AMDFAM19H;
1147bdd1243dSDimitry Andric     if (Model <= 0x0f || (Model >= 0x20 && Model <= 0x5f)) {
1148bdd1243dSDimitry Andric       // Family 19h Models 00h-0Fh - Zen3
1149bdd1243dSDimitry Andric       // Family 19h Models 20h-2Fh - Zen3
1150bdd1243dSDimitry Andric       // Family 19h Models 30h-3Fh - Zen3
1151bdd1243dSDimitry Andric       // Family 19h Models 40h-4Fh - Zen3+
1152bdd1243dSDimitry Andric       // Family 19h Models 50h-5Fh - Zen3+
1153bdd1243dSDimitry Andric       *Subtype = X86::AMDFAM19H_ZNVER3;
1154bdd1243dSDimitry Andric       break;
1155bdd1243dSDimitry Andric     }
1156bdd1243dSDimitry Andric     if ((Model >= 0x10 && Model <= 0x1f) ||
1157bdd1243dSDimitry Andric         (Model >= 0x60 && Model <= 0x74) ||
1158bdd1243dSDimitry Andric         (Model >= 0x78 && Model <= 0x7b) ||
1159bdd1243dSDimitry Andric         (Model >= 0xA0 && Model <= 0xAf)) {
1160bdd1243dSDimitry Andric       CPU = "znver4";
1161bdd1243dSDimitry Andric       *Subtype = X86::AMDFAM19H_ZNVER4;
1162bdd1243dSDimitry Andric       break; //  "znver4"
1163bdd1243dSDimitry Andric     }
1164bdd1243dSDimitry Andric     break; // family 19h
1165bdd1243dSDimitry Andric   default:
1166bdd1243dSDimitry Andric     break; // Unknown AMD CPU.
1167bdd1243dSDimitry Andric   }
1168bdd1243dSDimitry Andric 
1169bdd1243dSDimitry Andric   return CPU;
1170bdd1243dSDimitry Andric }
1171bdd1243dSDimitry Andric 
1172bdd1243dSDimitry Andric static void getAvailableFeatures(unsigned ECX, unsigned EDX, unsigned MaxLeaf,
1173bdd1243dSDimitry Andric                                  unsigned *Features) {
1174bdd1243dSDimitry Andric   unsigned EAX, EBX;
1175bdd1243dSDimitry Andric 
1176bdd1243dSDimitry Andric   auto setFeature = [&](unsigned F) {
1177bdd1243dSDimitry Andric     Features[F / 32] |= 1U << (F % 32);
1178bdd1243dSDimitry Andric   };
1179bdd1243dSDimitry Andric 
1180bdd1243dSDimitry Andric   if ((EDX >> 15) & 1)
1181bdd1243dSDimitry Andric     setFeature(X86::FEATURE_CMOV);
1182bdd1243dSDimitry Andric   if ((EDX >> 23) & 1)
1183bdd1243dSDimitry Andric     setFeature(X86::FEATURE_MMX);
1184bdd1243dSDimitry Andric   if ((EDX >> 25) & 1)
1185bdd1243dSDimitry Andric     setFeature(X86::FEATURE_SSE);
1186bdd1243dSDimitry Andric   if ((EDX >> 26) & 1)
1187bdd1243dSDimitry Andric     setFeature(X86::FEATURE_SSE2);
1188bdd1243dSDimitry Andric 
1189bdd1243dSDimitry Andric   if ((ECX >> 0) & 1)
1190bdd1243dSDimitry Andric     setFeature(X86::FEATURE_SSE3);
1191bdd1243dSDimitry Andric   if ((ECX >> 1) & 1)
1192bdd1243dSDimitry Andric     setFeature(X86::FEATURE_PCLMUL);
1193bdd1243dSDimitry Andric   if ((ECX >> 9) & 1)
1194bdd1243dSDimitry Andric     setFeature(X86::FEATURE_SSSE3);
1195bdd1243dSDimitry Andric   if ((ECX >> 12) & 1)
1196bdd1243dSDimitry Andric     setFeature(X86::FEATURE_FMA);
1197bdd1243dSDimitry Andric   if ((ECX >> 19) & 1)
1198bdd1243dSDimitry Andric     setFeature(X86::FEATURE_SSE4_1);
1199bdd1243dSDimitry Andric   if ((ECX >> 20) & 1) {
1200bdd1243dSDimitry Andric     setFeature(X86::FEATURE_SSE4_2);
1201bdd1243dSDimitry Andric     setFeature(X86::FEATURE_CRC32);
1202bdd1243dSDimitry Andric   }
1203bdd1243dSDimitry Andric   if ((ECX >> 23) & 1)
1204bdd1243dSDimitry Andric     setFeature(X86::FEATURE_POPCNT);
1205bdd1243dSDimitry Andric   if ((ECX >> 25) & 1)
1206bdd1243dSDimitry Andric     setFeature(X86::FEATURE_AES);
1207bdd1243dSDimitry Andric 
1208bdd1243dSDimitry Andric   if ((ECX >> 22) & 1)
1209bdd1243dSDimitry Andric     setFeature(X86::FEATURE_MOVBE);
1210bdd1243dSDimitry Andric 
1211bdd1243dSDimitry Andric   // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
1212bdd1243dSDimitry Andric   // indicates that the AVX registers will be saved and restored on context
1213bdd1243dSDimitry Andric   // switch, then we have full AVX support.
1214bdd1243dSDimitry Andric   const unsigned AVXBits = (1 << 27) | (1 << 28);
1215bdd1243dSDimitry Andric   bool HasAVX = ((ECX & AVXBits) == AVXBits) && !getX86XCR0(&EAX, &EDX) &&
1216bdd1243dSDimitry Andric                 ((EAX & 0x6) == 0x6);
1217bdd1243dSDimitry Andric #if defined(__APPLE__)
1218bdd1243dSDimitry Andric   // Darwin lazily saves the AVX512 context on first use: trust that the OS will
1219bdd1243dSDimitry Andric   // save the AVX512 context if we use AVX512 instructions, even the bit is not
1220bdd1243dSDimitry Andric   // set right now.
1221bdd1243dSDimitry Andric   bool HasAVX512Save = true;
1222bdd1243dSDimitry Andric #else
1223bdd1243dSDimitry Andric   // AVX512 requires additional context to be saved by the OS.
1224bdd1243dSDimitry Andric   bool HasAVX512Save = HasAVX && ((EAX & 0xe0) == 0xe0);
1225bdd1243dSDimitry Andric #endif
1226bdd1243dSDimitry Andric 
1227bdd1243dSDimitry Andric   if (HasAVX)
1228bdd1243dSDimitry Andric     setFeature(X86::FEATURE_AVX);
1229bdd1243dSDimitry Andric 
1230bdd1243dSDimitry Andric   bool HasLeaf7 =
1231bdd1243dSDimitry Andric       MaxLeaf >= 0x7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
1232bdd1243dSDimitry Andric 
1233bdd1243dSDimitry Andric   if (HasLeaf7 && ((EBX >> 3) & 1))
1234bdd1243dSDimitry Andric     setFeature(X86::FEATURE_BMI);
1235bdd1243dSDimitry Andric   if (HasLeaf7 && ((EBX >> 5) & 1) && HasAVX)
1236bdd1243dSDimitry Andric     setFeature(X86::FEATURE_AVX2);
1237bdd1243dSDimitry Andric   if (HasLeaf7 && ((EBX >> 8) & 1))
1238bdd1243dSDimitry Andric     setFeature(X86::FEATURE_BMI2);
1239bdd1243dSDimitry Andric   if (HasLeaf7 && ((EBX >> 16) & 1) && HasAVX512Save)
1240bdd1243dSDimitry Andric     setFeature(X86::FEATURE_AVX512F);
1241bdd1243dSDimitry Andric   if (HasLeaf7 && ((EBX >> 17) & 1) && HasAVX512Save)
1242bdd1243dSDimitry Andric     setFeature(X86::FEATURE_AVX512DQ);
1243bdd1243dSDimitry Andric   if (HasLeaf7 && ((EBX >> 19) & 1))
1244bdd1243dSDimitry Andric     setFeature(X86::FEATURE_ADX);
1245bdd1243dSDimitry Andric   if (HasLeaf7 && ((EBX >> 21) & 1) && HasAVX512Save)
1246bdd1243dSDimitry Andric     setFeature(X86::FEATURE_AVX512IFMA);
1247bdd1243dSDimitry Andric   if (HasLeaf7 && ((EBX >> 23) & 1))
1248bdd1243dSDimitry Andric     setFeature(X86::FEATURE_CLFLUSHOPT);
1249bdd1243dSDimitry Andric   if (HasLeaf7 && ((EBX >> 26) & 1) && HasAVX512Save)
1250bdd1243dSDimitry Andric     setFeature(X86::FEATURE_AVX512PF);
1251bdd1243dSDimitry Andric   if (HasLeaf7 && ((EBX >> 27) & 1) && HasAVX512Save)
1252bdd1243dSDimitry Andric     setFeature(X86::FEATURE_AVX512ER);
1253bdd1243dSDimitry Andric   if (HasLeaf7 && ((EBX >> 28) & 1) && HasAVX512Save)
1254bdd1243dSDimitry Andric     setFeature(X86::FEATURE_AVX512CD);
1255bdd1243dSDimitry Andric   if (HasLeaf7 && ((EBX >> 29) & 1))
1256bdd1243dSDimitry Andric     setFeature(X86::FEATURE_SHA);
1257bdd1243dSDimitry Andric   if (HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save)
1258bdd1243dSDimitry Andric     setFeature(X86::FEATURE_AVX512BW);
1259bdd1243dSDimitry Andric   if (HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save)
1260bdd1243dSDimitry Andric     setFeature(X86::FEATURE_AVX512VL);
1261bdd1243dSDimitry Andric 
1262bdd1243dSDimitry Andric   if (HasLeaf7 && ((ECX >> 1) & 1) && HasAVX512Save)
1263bdd1243dSDimitry Andric     setFeature(X86::FEATURE_AVX512VBMI);
1264bdd1243dSDimitry Andric   if (HasLeaf7 && ((ECX >> 6) & 1) && HasAVX512Save)
1265bdd1243dSDimitry Andric     setFeature(X86::FEATURE_AVX512VBMI2);
1266bdd1243dSDimitry Andric   if (HasLeaf7 && ((ECX >> 8) & 1))
1267bdd1243dSDimitry Andric     setFeature(X86::FEATURE_GFNI);
1268bdd1243dSDimitry Andric   if (HasLeaf7 && ((ECX >> 10) & 1) && HasAVX)
1269bdd1243dSDimitry Andric     setFeature(X86::FEATURE_VPCLMULQDQ);
1270bdd1243dSDimitry Andric   if (HasLeaf7 && ((ECX >> 11) & 1) && HasAVX512Save)
1271bdd1243dSDimitry Andric     setFeature(X86::FEATURE_AVX512VNNI);
1272bdd1243dSDimitry Andric   if (HasLeaf7 && ((ECX >> 12) & 1) && HasAVX512Save)
1273bdd1243dSDimitry Andric     setFeature(X86::FEATURE_AVX512BITALG);
1274bdd1243dSDimitry Andric   if (HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save)
1275bdd1243dSDimitry Andric     setFeature(X86::FEATURE_AVX512VPOPCNTDQ);
1276bdd1243dSDimitry Andric 
1277bdd1243dSDimitry Andric   if (HasLeaf7 && ((EDX >> 2) & 1) && HasAVX512Save)
1278bdd1243dSDimitry Andric     setFeature(X86::FEATURE_AVX5124VNNIW);
1279bdd1243dSDimitry Andric   if (HasLeaf7 && ((EDX >> 3) & 1) && HasAVX512Save)
1280bdd1243dSDimitry Andric     setFeature(X86::FEATURE_AVX5124FMAPS);
1281bdd1243dSDimitry Andric   if (HasLeaf7 && ((EDX >> 8) & 1) && HasAVX512Save)
1282bdd1243dSDimitry Andric     setFeature(X86::FEATURE_AVX512VP2INTERSECT);
1283bdd1243dSDimitry Andric 
12848a4dda33SDimitry Andric   // EAX from subleaf 0 is the maximum subleaf supported. Some CPUs don't
12858a4dda33SDimitry Andric   // return all 0s for invalid subleaves so check the limit.
1286bdd1243dSDimitry Andric   bool HasLeaf7Subleaf1 =
12878a4dda33SDimitry Andric       HasLeaf7 && EAX >= 1 &&
12888a4dda33SDimitry Andric       !getX86CpuIDAndInfoEx(0x7, 0x1, &EAX, &EBX, &ECX, &EDX);
1289bdd1243dSDimitry Andric   if (HasLeaf7Subleaf1 && ((EAX >> 5) & 1) && HasAVX512Save)
1290bdd1243dSDimitry Andric     setFeature(X86::FEATURE_AVX512BF16);
1291bdd1243dSDimitry Andric 
1292bdd1243dSDimitry Andric   unsigned MaxExtLevel;
1293bdd1243dSDimitry Andric   getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
1294bdd1243dSDimitry Andric 
1295bdd1243dSDimitry Andric   bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
1296bdd1243dSDimitry Andric                      !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
1297bdd1243dSDimitry Andric   if (HasExtLeaf1 && ((ECX >> 6) & 1))
1298bdd1243dSDimitry Andric     setFeature(X86::FEATURE_SSE4_A);
1299bdd1243dSDimitry Andric   if (HasExtLeaf1 && ((ECX >> 11) & 1))
1300bdd1243dSDimitry Andric     setFeature(X86::FEATURE_XOP);
1301bdd1243dSDimitry Andric   if (HasExtLeaf1 && ((ECX >> 16) & 1))
1302bdd1243dSDimitry Andric     setFeature(X86::FEATURE_FMA4);
1303bdd1243dSDimitry Andric 
1304bdd1243dSDimitry Andric   if (HasExtLeaf1 && ((EDX >> 29) & 1))
1305bdd1243dSDimitry Andric     setFeature(X86::FEATURE_64BIT);
1306bdd1243dSDimitry Andric }
1307bdd1243dSDimitry Andric 
1308bdd1243dSDimitry Andric StringRef sys::getHostCPUName() {
1309bdd1243dSDimitry Andric   unsigned MaxLeaf = 0;
1310bdd1243dSDimitry Andric   const VendorSignatures Vendor = getVendorSignature(&MaxLeaf);
1311bdd1243dSDimitry Andric   if (Vendor == VendorSignatures::UNKNOWN)
1312bdd1243dSDimitry Andric     return "generic";
1313bdd1243dSDimitry Andric 
1314bdd1243dSDimitry Andric   unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
1315bdd1243dSDimitry Andric   getX86CpuIDAndInfo(0x1, &EAX, &EBX, &ECX, &EDX);
1316bdd1243dSDimitry Andric 
1317bdd1243dSDimitry Andric   unsigned Family = 0, Model = 0;
1318bdd1243dSDimitry Andric   unsigned Features[(X86::CPU_FEATURE_MAX + 31) / 32] = {0};
1319bdd1243dSDimitry Andric   detectX86FamilyModel(EAX, &Family, &Model);
1320bdd1243dSDimitry Andric   getAvailableFeatures(ECX, EDX, MaxLeaf, Features);
1321bdd1243dSDimitry Andric 
1322bdd1243dSDimitry Andric   // These aren't consumed in this file, but we try to keep some source code the
1323bdd1243dSDimitry Andric   // same or similar to compiler-rt.
1324bdd1243dSDimitry Andric   unsigned Type = 0;
1325bdd1243dSDimitry Andric   unsigned Subtype = 0;
1326bdd1243dSDimitry Andric 
1327bdd1243dSDimitry Andric   StringRef CPU;
1328bdd1243dSDimitry Andric 
1329bdd1243dSDimitry Andric   if (Vendor == VendorSignatures::GENUINE_INTEL) {
1330bdd1243dSDimitry Andric     CPU = getIntelProcessorTypeAndSubtype(Family, Model, Features, &Type,
1331bdd1243dSDimitry Andric                                           &Subtype);
1332bdd1243dSDimitry Andric   } else if (Vendor == VendorSignatures::AUTHENTIC_AMD) {
1333bdd1243dSDimitry Andric     CPU = getAMDProcessorTypeAndSubtype(Family, Model, Features, &Type,
1334bdd1243dSDimitry Andric                                         &Subtype);
1335bdd1243dSDimitry Andric   }
1336bdd1243dSDimitry Andric 
1337bdd1243dSDimitry Andric   if (!CPU.empty())
1338bdd1243dSDimitry Andric     return CPU;
1339bdd1243dSDimitry Andric 
1340bdd1243dSDimitry Andric   return "generic";
1341bdd1243dSDimitry Andric }
1342bdd1243dSDimitry Andric 
1343bdd1243dSDimitry Andric #elif defined(__APPLE__) && defined(__powerpc__)
1344bdd1243dSDimitry Andric StringRef sys::getHostCPUName() {
1345bdd1243dSDimitry Andric   host_basic_info_data_t hostInfo;
1346bdd1243dSDimitry Andric   mach_msg_type_number_t infoCount;
1347bdd1243dSDimitry Andric 
1348bdd1243dSDimitry Andric   infoCount = HOST_BASIC_INFO_COUNT;
1349bdd1243dSDimitry Andric   mach_port_t hostPort = mach_host_self();
1350bdd1243dSDimitry Andric   host_info(hostPort, HOST_BASIC_INFO, (host_info_t)&hostInfo,
1351bdd1243dSDimitry Andric             &infoCount);
1352bdd1243dSDimitry Andric   mach_port_deallocate(mach_task_self(), hostPort);
1353bdd1243dSDimitry Andric 
1354bdd1243dSDimitry Andric   if (hostInfo.cpu_type != CPU_TYPE_POWERPC)
1355bdd1243dSDimitry Andric     return "generic";
1356bdd1243dSDimitry Andric 
1357bdd1243dSDimitry Andric   switch (hostInfo.cpu_subtype) {
1358bdd1243dSDimitry Andric   case CPU_SUBTYPE_POWERPC_601:
1359bdd1243dSDimitry Andric     return "601";
1360bdd1243dSDimitry Andric   case CPU_SUBTYPE_POWERPC_602:
1361bdd1243dSDimitry Andric     return "602";
1362bdd1243dSDimitry Andric   case CPU_SUBTYPE_POWERPC_603:
1363bdd1243dSDimitry Andric     return "603";
1364bdd1243dSDimitry Andric   case CPU_SUBTYPE_POWERPC_603e:
1365bdd1243dSDimitry Andric     return "603e";
1366bdd1243dSDimitry Andric   case CPU_SUBTYPE_POWERPC_603ev:
1367bdd1243dSDimitry Andric     return "603ev";
1368bdd1243dSDimitry Andric   case CPU_SUBTYPE_POWERPC_604:
1369bdd1243dSDimitry Andric     return "604";
1370bdd1243dSDimitry Andric   case CPU_SUBTYPE_POWERPC_604e:
1371bdd1243dSDimitry Andric     return "604e";
1372bdd1243dSDimitry Andric   case CPU_SUBTYPE_POWERPC_620:
1373bdd1243dSDimitry Andric     return "620";
1374bdd1243dSDimitry Andric   case CPU_SUBTYPE_POWERPC_750:
1375bdd1243dSDimitry Andric     return "750";
1376bdd1243dSDimitry Andric   case CPU_SUBTYPE_POWERPC_7400:
1377bdd1243dSDimitry Andric     return "7400";
1378bdd1243dSDimitry Andric   case CPU_SUBTYPE_POWERPC_7450:
1379bdd1243dSDimitry Andric     return "7450";
1380bdd1243dSDimitry Andric   case CPU_SUBTYPE_POWERPC_970:
1381bdd1243dSDimitry Andric     return "970";
1382bdd1243dSDimitry Andric   default:;
1383bdd1243dSDimitry Andric   }
1384bdd1243dSDimitry Andric 
1385bdd1243dSDimitry Andric   return "generic";
1386bdd1243dSDimitry Andric }
1387bdd1243dSDimitry Andric #elif defined(__linux__) && defined(__powerpc__)
1388bdd1243dSDimitry Andric StringRef sys::getHostCPUName() {
1389bdd1243dSDimitry Andric   std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1390bdd1243dSDimitry Andric   StringRef Content = P ? P->getBuffer() : "";
1391bdd1243dSDimitry Andric   return detail::getHostCPUNameForPowerPC(Content);
1392bdd1243dSDimitry Andric }
1393bdd1243dSDimitry Andric #elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1394bdd1243dSDimitry Andric StringRef sys::getHostCPUName() {
1395bdd1243dSDimitry Andric   std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1396bdd1243dSDimitry Andric   StringRef Content = P ? P->getBuffer() : "";
1397bdd1243dSDimitry Andric   return detail::getHostCPUNameForARM(Content);
1398bdd1243dSDimitry Andric }
1399bdd1243dSDimitry Andric #elif defined(__linux__) && defined(__s390x__)
1400bdd1243dSDimitry Andric StringRef sys::getHostCPUName() {
1401bdd1243dSDimitry Andric   std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1402bdd1243dSDimitry Andric   StringRef Content = P ? P->getBuffer() : "";
1403bdd1243dSDimitry Andric   return detail::getHostCPUNameForS390x(Content);
1404bdd1243dSDimitry Andric }
1405bdd1243dSDimitry Andric #elif defined(__MVS__)
1406bdd1243dSDimitry Andric StringRef sys::getHostCPUName() {
1407bdd1243dSDimitry Andric   // Get pointer to Communications Vector Table (CVT).
1408bdd1243dSDimitry Andric   // The pointer is located at offset 16 of the Prefixed Save Area (PSA).
1409bdd1243dSDimitry Andric   // It is stored as 31 bit pointer and will be zero-extended to 64 bit.
1410bdd1243dSDimitry Andric   int *StartToCVTOffset = reinterpret_cast<int *>(0x10);
1411bdd1243dSDimitry Andric   // Since its stored as a 31-bit pointer, get the 4 bytes from the start
1412bdd1243dSDimitry Andric   // of address.
1413bdd1243dSDimitry Andric   int ReadValue = *StartToCVTOffset;
1414bdd1243dSDimitry Andric   // Explicitly clear the high order bit.
1415bdd1243dSDimitry Andric   ReadValue = (ReadValue & 0x7FFFFFFF);
1416bdd1243dSDimitry Andric   char *CVT = reinterpret_cast<char *>(ReadValue);
1417bdd1243dSDimitry Andric   // The model number is located in the CVT prefix at offset -6 and stored as
1418bdd1243dSDimitry Andric   // signless packed decimal.
1419bdd1243dSDimitry Andric   uint16_t Id = *(uint16_t *)&CVT[-6];
1420bdd1243dSDimitry Andric   // Convert number to integer.
1421bdd1243dSDimitry Andric   Id = decodePackedBCD<uint16_t>(Id, false);
1422bdd1243dSDimitry Andric   // Check for vector support. It's stored in field CVTFLAG5 (offset 244),
1423bdd1243dSDimitry Andric   // bit CVTVEF (X'80'). The facilities list is part of the PSA but the vector
1424bdd1243dSDimitry Andric   // extension can only be used if bit CVTVEF is on.
1425bdd1243dSDimitry Andric   bool HaveVectorSupport = CVT[244] & 0x80;
1426bdd1243dSDimitry Andric   return getCPUNameFromS390Model(Id, HaveVectorSupport);
1427bdd1243dSDimitry Andric }
1428bdd1243dSDimitry Andric #elif defined(__APPLE__) && (defined(__arm__) || defined(__aarch64__))
1429bdd1243dSDimitry Andric #define CPUFAMILY_ARM_SWIFT 0x1e2d6381
1430bdd1243dSDimitry Andric #define CPUFAMILY_ARM_CYCLONE 0x37a09642
1431bdd1243dSDimitry Andric #define CPUFAMILY_ARM_TYPHOON 0x2c91a47e
1432bdd1243dSDimitry Andric #define CPUFAMILY_ARM_TWISTER 0x92fb37c8
1433bdd1243dSDimitry Andric #define CPUFAMILY_ARM_HURRICANE 0x67ceee93
1434bdd1243dSDimitry Andric #define CPUFAMILY_ARM_MONSOON_MISTRAL 0xe81e7ef6
1435bdd1243dSDimitry Andric #define CPUFAMILY_ARM_VORTEX_TEMPEST 0x07d34b9f
1436bdd1243dSDimitry Andric #define CPUFAMILY_ARM_LIGHTNING_THUNDER 0x462504d2
1437bdd1243dSDimitry Andric #define CPUFAMILY_ARM_FIRESTORM_ICESTORM 0x1b588bb3
1438bdd1243dSDimitry Andric 
1439bdd1243dSDimitry Andric StringRef sys::getHostCPUName() {
1440bdd1243dSDimitry Andric   uint32_t Family;
1441bdd1243dSDimitry Andric   size_t Length = sizeof(Family);
1442bdd1243dSDimitry Andric   sysctlbyname("hw.cpufamily", &Family, &Length, NULL, 0);
1443bdd1243dSDimitry Andric 
1444bdd1243dSDimitry Andric   switch (Family) {
1445bdd1243dSDimitry Andric   case CPUFAMILY_ARM_SWIFT:
1446bdd1243dSDimitry Andric     return "swift";
1447bdd1243dSDimitry Andric   case CPUFAMILY_ARM_CYCLONE:
1448bdd1243dSDimitry Andric     return "apple-a7";
1449bdd1243dSDimitry Andric   case CPUFAMILY_ARM_TYPHOON:
1450bdd1243dSDimitry Andric     return "apple-a8";
1451bdd1243dSDimitry Andric   case CPUFAMILY_ARM_TWISTER:
1452bdd1243dSDimitry Andric     return "apple-a9";
1453bdd1243dSDimitry Andric   case CPUFAMILY_ARM_HURRICANE:
1454bdd1243dSDimitry Andric     return "apple-a10";
1455bdd1243dSDimitry Andric   case CPUFAMILY_ARM_MONSOON_MISTRAL:
1456bdd1243dSDimitry Andric     return "apple-a11";
1457bdd1243dSDimitry Andric   case CPUFAMILY_ARM_VORTEX_TEMPEST:
1458bdd1243dSDimitry Andric     return "apple-a12";
1459bdd1243dSDimitry Andric   case CPUFAMILY_ARM_LIGHTNING_THUNDER:
1460bdd1243dSDimitry Andric     return "apple-a13";
1461bdd1243dSDimitry Andric   case CPUFAMILY_ARM_FIRESTORM_ICESTORM:
1462bdd1243dSDimitry Andric     return "apple-m1";
1463bdd1243dSDimitry Andric   default:
1464bdd1243dSDimitry Andric     // Default to the newest CPU we know about.
1465bdd1243dSDimitry Andric     return "apple-m1";
1466bdd1243dSDimitry Andric   }
1467bdd1243dSDimitry Andric }
1468bdd1243dSDimitry Andric #elif defined(_AIX)
1469bdd1243dSDimitry Andric StringRef sys::getHostCPUName() {
1470bdd1243dSDimitry Andric   switch (_system_configuration.implementation) {
1471bdd1243dSDimitry Andric   case POWER_4:
1472bdd1243dSDimitry Andric     if (_system_configuration.version == PV_4_3)
1473bdd1243dSDimitry Andric       return "970";
1474bdd1243dSDimitry Andric     return "pwr4";
1475bdd1243dSDimitry Andric   case POWER_5:
1476bdd1243dSDimitry Andric     if (_system_configuration.version == PV_5)
1477bdd1243dSDimitry Andric       return "pwr5";
1478bdd1243dSDimitry Andric     return "pwr5x";
1479bdd1243dSDimitry Andric   case POWER_6:
1480bdd1243dSDimitry Andric     if (_system_configuration.version == PV_6_Compat)
1481bdd1243dSDimitry Andric       return "pwr6";
1482bdd1243dSDimitry Andric     return "pwr6x";
1483bdd1243dSDimitry Andric   case POWER_7:
1484bdd1243dSDimitry Andric     return "pwr7";
1485bdd1243dSDimitry Andric   case POWER_8:
1486bdd1243dSDimitry Andric     return "pwr8";
1487bdd1243dSDimitry Andric   case POWER_9:
1488bdd1243dSDimitry Andric     return "pwr9";
1489bdd1243dSDimitry Andric // TODO: simplify this once the macro is available in all OS levels.
1490bdd1243dSDimitry Andric #ifdef POWER_10
1491bdd1243dSDimitry Andric   case POWER_10:
1492bdd1243dSDimitry Andric #else
1493bdd1243dSDimitry Andric   case 0x40000:
1494bdd1243dSDimitry Andric #endif
1495bdd1243dSDimitry Andric     return "pwr10";
1496bdd1243dSDimitry Andric   default:
1497bdd1243dSDimitry Andric     return "generic";
1498bdd1243dSDimitry Andric   }
1499bdd1243dSDimitry Andric }
150006c3fb27SDimitry Andric #elif defined(__loongarch__)
150106c3fb27SDimitry Andric StringRef sys::getHostCPUName() {
150206c3fb27SDimitry Andric   // Use processor id to detect cpu name.
150306c3fb27SDimitry Andric   uint32_t processor_id;
150406c3fb27SDimitry Andric   __asm__("cpucfg %[prid], $zero\n\t" : [prid] "=r"(processor_id));
150506c3fb27SDimitry Andric   switch (processor_id & 0xff00) {
150606c3fb27SDimitry Andric   case 0xc000: // Loongson 64bit, 4-issue
150706c3fb27SDimitry Andric     return "la464";
150806c3fb27SDimitry Andric   // TODO: Others.
150906c3fb27SDimitry Andric   default:
151006c3fb27SDimitry Andric     break;
151106c3fb27SDimitry Andric   }
151206c3fb27SDimitry Andric   return "generic";
151306c3fb27SDimitry Andric }
1514bdd1243dSDimitry Andric #elif defined(__riscv)
1515bdd1243dSDimitry Andric StringRef sys::getHostCPUName() {
1516bdd1243dSDimitry Andric #if defined(__linux__)
1517bdd1243dSDimitry Andric   std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1518bdd1243dSDimitry Andric   StringRef Content = P ? P->getBuffer() : "";
1519bdd1243dSDimitry Andric   return detail::getHostCPUNameForRISCV(Content);
1520bdd1243dSDimitry Andric #else
1521bdd1243dSDimitry Andric #if __riscv_xlen == 64
1522bdd1243dSDimitry Andric   return "generic-rv64";
1523bdd1243dSDimitry Andric #elif __riscv_xlen == 32
1524bdd1243dSDimitry Andric   return "generic-rv32";
1525bdd1243dSDimitry Andric #else
1526bdd1243dSDimitry Andric #error "Unhandled value of __riscv_xlen"
1527bdd1243dSDimitry Andric #endif
1528bdd1243dSDimitry Andric #endif
1529bdd1243dSDimitry Andric }
1530bdd1243dSDimitry Andric #elif defined(__sparc__)
1531bdd1243dSDimitry Andric #if defined(__linux__)
1532bdd1243dSDimitry Andric StringRef sys::detail::getHostCPUNameForSPARC(StringRef ProcCpuinfoContent) {
1533bdd1243dSDimitry Andric   SmallVector<StringRef> Lines;
1534bdd1243dSDimitry Andric   ProcCpuinfoContent.split(Lines, "\n");
1535bdd1243dSDimitry Andric 
1536bdd1243dSDimitry Andric   // Look for cpu line to determine cpu name
1537bdd1243dSDimitry Andric   StringRef Cpu;
1538bdd1243dSDimitry Andric   for (unsigned I = 0, E = Lines.size(); I != E; ++I) {
1539*5f757f3fSDimitry Andric     if (Lines[I].starts_with("cpu")) {
1540bdd1243dSDimitry Andric       Cpu = Lines[I].substr(5).ltrim("\t :");
1541bdd1243dSDimitry Andric       break;
1542bdd1243dSDimitry Andric     }
1543bdd1243dSDimitry Andric   }
1544bdd1243dSDimitry Andric 
1545bdd1243dSDimitry Andric   return StringSwitch<const char *>(Cpu)
1546bdd1243dSDimitry Andric       .StartsWith("SuperSparc", "supersparc")
1547bdd1243dSDimitry Andric       .StartsWith("HyperSparc", "hypersparc")
1548bdd1243dSDimitry Andric       .StartsWith("SpitFire", "ultrasparc")
1549bdd1243dSDimitry Andric       .StartsWith("BlackBird", "ultrasparc")
1550bdd1243dSDimitry Andric       .StartsWith("Sabre", " ultrasparc")
1551bdd1243dSDimitry Andric       .StartsWith("Hummingbird", "ultrasparc")
1552bdd1243dSDimitry Andric       .StartsWith("Cheetah", "ultrasparc3")
1553bdd1243dSDimitry Andric       .StartsWith("Jalapeno", "ultrasparc3")
1554bdd1243dSDimitry Andric       .StartsWith("Jaguar", "ultrasparc3")
1555bdd1243dSDimitry Andric       .StartsWith("Panther", "ultrasparc3")
1556bdd1243dSDimitry Andric       .StartsWith("Serrano", "ultrasparc3")
1557bdd1243dSDimitry Andric       .StartsWith("UltraSparc T1", "niagara")
1558bdd1243dSDimitry Andric       .StartsWith("UltraSparc T2", "niagara2")
1559bdd1243dSDimitry Andric       .StartsWith("UltraSparc T3", "niagara3")
1560bdd1243dSDimitry Andric       .StartsWith("UltraSparc T4", "niagara4")
1561bdd1243dSDimitry Andric       .StartsWith("UltraSparc T5", "niagara4")
1562bdd1243dSDimitry Andric       .StartsWith("LEON", "leon3")
1563bdd1243dSDimitry Andric       // niagara7/m8 not supported by LLVM yet.
1564bdd1243dSDimitry Andric       .StartsWith("SPARC-M7", "niagara4" /* "niagara7" */)
1565bdd1243dSDimitry Andric       .StartsWith("SPARC-S7", "niagara4" /* "niagara7" */)
1566bdd1243dSDimitry Andric       .StartsWith("SPARC-M8", "niagara4" /* "m8" */)
1567bdd1243dSDimitry Andric       .Default("generic");
1568bdd1243dSDimitry Andric }
1569bdd1243dSDimitry Andric #endif
1570bdd1243dSDimitry Andric 
1571bdd1243dSDimitry Andric StringRef sys::getHostCPUName() {
1572bdd1243dSDimitry Andric #if defined(__linux__)
1573bdd1243dSDimitry Andric   std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1574bdd1243dSDimitry Andric   StringRef Content = P ? P->getBuffer() : "";
1575bdd1243dSDimitry Andric   return detail::getHostCPUNameForSPARC(Content);
1576bdd1243dSDimitry Andric #elif defined(__sun__) && defined(__svr4__)
1577bdd1243dSDimitry Andric   char *buf = NULL;
1578bdd1243dSDimitry Andric   kstat_ctl_t *kc;
1579bdd1243dSDimitry Andric   kstat_t *ksp;
1580bdd1243dSDimitry Andric   kstat_named_t *brand = NULL;
1581bdd1243dSDimitry Andric 
1582bdd1243dSDimitry Andric   kc = kstat_open();
1583bdd1243dSDimitry Andric   if (kc != NULL) {
1584bdd1243dSDimitry Andric     ksp = kstat_lookup(kc, const_cast<char *>("cpu_info"), -1, NULL);
1585bdd1243dSDimitry Andric     if (ksp != NULL && kstat_read(kc, ksp, NULL) != -1 &&
1586bdd1243dSDimitry Andric         ksp->ks_type == KSTAT_TYPE_NAMED)
1587bdd1243dSDimitry Andric       brand =
1588bdd1243dSDimitry Andric           (kstat_named_t *)kstat_data_lookup(ksp, const_cast<char *>("brand"));
1589bdd1243dSDimitry Andric     if (brand != NULL && brand->data_type == KSTAT_DATA_STRING)
1590bdd1243dSDimitry Andric       buf = KSTAT_NAMED_STR_PTR(brand);
1591bdd1243dSDimitry Andric   }
1592bdd1243dSDimitry Andric   kstat_close(kc);
1593bdd1243dSDimitry Andric 
1594bdd1243dSDimitry Andric   return StringSwitch<const char *>(buf)
1595bdd1243dSDimitry Andric       .Case("TMS390S10", "supersparc") // Texas Instruments microSPARC I
1596bdd1243dSDimitry Andric       .Case("TMS390Z50", "supersparc") // Texas Instruments SuperSPARC I
1597bdd1243dSDimitry Andric       .Case("TMS390Z55",
1598bdd1243dSDimitry Andric             "supersparc") // Texas Instruments SuperSPARC I with SuperCache
1599bdd1243dSDimitry Andric       .Case("MB86904", "supersparc") // Fujitsu microSPARC II
1600bdd1243dSDimitry Andric       .Case("MB86907", "supersparc") // Fujitsu TurboSPARC
1601bdd1243dSDimitry Andric       .Case("RT623", "hypersparc")   // Ross hyperSPARC
1602bdd1243dSDimitry Andric       .Case("RT625", "hypersparc")
1603bdd1243dSDimitry Andric       .Case("RT626", "hypersparc")
1604bdd1243dSDimitry Andric       .Case("UltraSPARC-I", "ultrasparc")
1605bdd1243dSDimitry Andric       .Case("UltraSPARC-II", "ultrasparc")
1606bdd1243dSDimitry Andric       .Case("UltraSPARC-IIe", "ultrasparc")
1607bdd1243dSDimitry Andric       .Case("UltraSPARC-IIi", "ultrasparc")
1608bdd1243dSDimitry Andric       .Case("SPARC64-III", "ultrasparc")
1609bdd1243dSDimitry Andric       .Case("SPARC64-IV", "ultrasparc")
1610bdd1243dSDimitry Andric       .Case("UltraSPARC-III", "ultrasparc3")
1611bdd1243dSDimitry Andric       .Case("UltraSPARC-III+", "ultrasparc3")
1612bdd1243dSDimitry Andric       .Case("UltraSPARC-IIIi", "ultrasparc3")
1613bdd1243dSDimitry Andric       .Case("UltraSPARC-IIIi+", "ultrasparc3")
1614bdd1243dSDimitry Andric       .Case("UltraSPARC-IV", "ultrasparc3")
1615bdd1243dSDimitry Andric       .Case("UltraSPARC-IV+", "ultrasparc3")
1616bdd1243dSDimitry Andric       .Case("SPARC64-V", "ultrasparc3")
1617bdd1243dSDimitry Andric       .Case("SPARC64-VI", "ultrasparc3")
1618bdd1243dSDimitry Andric       .Case("SPARC64-VII", "ultrasparc3")
1619bdd1243dSDimitry Andric       .Case("UltraSPARC-T1", "niagara")
1620bdd1243dSDimitry Andric       .Case("UltraSPARC-T2", "niagara2")
1621bdd1243dSDimitry Andric       .Case("UltraSPARC-T2", "niagara2")
1622bdd1243dSDimitry Andric       .Case("UltraSPARC-T2+", "niagara2")
1623bdd1243dSDimitry Andric       .Case("SPARC-T3", "niagara3")
1624bdd1243dSDimitry Andric       .Case("SPARC-T4", "niagara4")
1625bdd1243dSDimitry Andric       .Case("SPARC-T5", "niagara4")
1626bdd1243dSDimitry Andric       // niagara7/m8 not supported by LLVM yet.
1627bdd1243dSDimitry Andric       .Case("SPARC-M7", "niagara4" /* "niagara7" */)
1628bdd1243dSDimitry Andric       .Case("SPARC-S7", "niagara4" /* "niagara7" */)
1629bdd1243dSDimitry Andric       .Case("SPARC-M8", "niagara4" /* "m8" */)
1630bdd1243dSDimitry Andric       .Default("generic");
1631bdd1243dSDimitry Andric #else
1632bdd1243dSDimitry Andric   return "generic";
1633bdd1243dSDimitry Andric #endif
1634bdd1243dSDimitry Andric }
1635bdd1243dSDimitry Andric #else
1636bdd1243dSDimitry Andric StringRef sys::getHostCPUName() { return "generic"; }
1637bdd1243dSDimitry Andric namespace llvm {
1638bdd1243dSDimitry Andric namespace sys {
1639bdd1243dSDimitry Andric namespace detail {
1640bdd1243dSDimitry Andric namespace x86 {
1641bdd1243dSDimitry Andric 
1642bdd1243dSDimitry Andric VendorSignatures getVendorSignature(unsigned *MaxLeaf) {
1643bdd1243dSDimitry Andric   return VendorSignatures::UNKNOWN;
1644bdd1243dSDimitry Andric }
1645bdd1243dSDimitry Andric 
1646bdd1243dSDimitry Andric } // namespace x86
1647bdd1243dSDimitry Andric } // namespace detail
1648bdd1243dSDimitry Andric } // namespace sys
1649bdd1243dSDimitry Andric } // namespace llvm
1650bdd1243dSDimitry Andric #endif
1651bdd1243dSDimitry Andric 
1652bdd1243dSDimitry Andric #if defined(__i386__) || defined(_M_IX86) || \
1653bdd1243dSDimitry Andric     defined(__x86_64__) || defined(_M_X64)
1654bdd1243dSDimitry Andric bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
1655bdd1243dSDimitry Andric   unsigned EAX = 0, EBX = 0, ECX = 0, EDX = 0;
1656bdd1243dSDimitry Andric   unsigned MaxLevel;
1657bdd1243dSDimitry Andric 
1658bdd1243dSDimitry Andric   if (getX86CpuIDAndInfo(0, &MaxLevel, &EBX, &ECX, &EDX) || MaxLevel < 1)
1659bdd1243dSDimitry Andric     return false;
1660bdd1243dSDimitry Andric 
1661bdd1243dSDimitry Andric   getX86CpuIDAndInfo(1, &EAX, &EBX, &ECX, &EDX);
1662bdd1243dSDimitry Andric 
1663bdd1243dSDimitry Andric   Features["cx8"]    = (EDX >>  8) & 1;
1664bdd1243dSDimitry Andric   Features["cmov"]   = (EDX >> 15) & 1;
1665bdd1243dSDimitry Andric   Features["mmx"]    = (EDX >> 23) & 1;
1666bdd1243dSDimitry Andric   Features["fxsr"]   = (EDX >> 24) & 1;
1667bdd1243dSDimitry Andric   Features["sse"]    = (EDX >> 25) & 1;
1668bdd1243dSDimitry Andric   Features["sse2"]   = (EDX >> 26) & 1;
1669bdd1243dSDimitry Andric 
1670bdd1243dSDimitry Andric   Features["sse3"]   = (ECX >>  0) & 1;
1671bdd1243dSDimitry Andric   Features["pclmul"] = (ECX >>  1) & 1;
1672bdd1243dSDimitry Andric   Features["ssse3"]  = (ECX >>  9) & 1;
1673bdd1243dSDimitry Andric   Features["cx16"]   = (ECX >> 13) & 1;
1674bdd1243dSDimitry Andric   Features["sse4.1"] = (ECX >> 19) & 1;
1675bdd1243dSDimitry Andric   Features["sse4.2"] = (ECX >> 20) & 1;
1676bdd1243dSDimitry Andric   Features["crc32"]  = Features["sse4.2"];
1677bdd1243dSDimitry Andric   Features["movbe"]  = (ECX >> 22) & 1;
1678bdd1243dSDimitry Andric   Features["popcnt"] = (ECX >> 23) & 1;
1679bdd1243dSDimitry Andric   Features["aes"]    = (ECX >> 25) & 1;
1680bdd1243dSDimitry Andric   Features["rdrnd"]  = (ECX >> 30) & 1;
1681bdd1243dSDimitry Andric 
1682bdd1243dSDimitry Andric   // If CPUID indicates support for XSAVE, XRESTORE and AVX, and XGETBV
1683bdd1243dSDimitry Andric   // indicates that the AVX registers will be saved and restored on context
1684bdd1243dSDimitry Andric   // switch, then we have full AVX support.
1685bdd1243dSDimitry Andric   bool HasXSave = ((ECX >> 27) & 1) && !getX86XCR0(&EAX, &EDX);
1686bdd1243dSDimitry Andric   bool HasAVXSave = HasXSave && ((ECX >> 28) & 1) && ((EAX & 0x6) == 0x6);
1687bdd1243dSDimitry Andric #if defined(__APPLE__)
1688bdd1243dSDimitry Andric   // Darwin lazily saves the AVX512 context on first use: trust that the OS will
1689bdd1243dSDimitry Andric   // save the AVX512 context if we use AVX512 instructions, even the bit is not
1690bdd1243dSDimitry Andric   // set right now.
1691bdd1243dSDimitry Andric   bool HasAVX512Save = true;
1692bdd1243dSDimitry Andric #else
1693bdd1243dSDimitry Andric   // AVX512 requires additional context to be saved by the OS.
1694bdd1243dSDimitry Andric   bool HasAVX512Save = HasAVXSave && ((EAX & 0xe0) == 0xe0);
1695bdd1243dSDimitry Andric #endif
1696bdd1243dSDimitry Andric   // AMX requires additional context to be saved by the OS.
1697bdd1243dSDimitry Andric   const unsigned AMXBits = (1 << 17) | (1 << 18);
1698bdd1243dSDimitry Andric   bool HasAMXSave = HasXSave && ((EAX & AMXBits) == AMXBits);
1699bdd1243dSDimitry Andric 
1700bdd1243dSDimitry Andric   Features["avx"]   = HasAVXSave;
1701bdd1243dSDimitry Andric   Features["fma"]   = ((ECX >> 12) & 1) && HasAVXSave;
1702bdd1243dSDimitry Andric   // Only enable XSAVE if OS has enabled support for saving YMM state.
1703bdd1243dSDimitry Andric   Features["xsave"] = ((ECX >> 26) & 1) && HasAVXSave;
1704bdd1243dSDimitry Andric   Features["f16c"]  = ((ECX >> 29) & 1) && HasAVXSave;
1705bdd1243dSDimitry Andric 
1706bdd1243dSDimitry Andric   unsigned MaxExtLevel;
1707bdd1243dSDimitry Andric   getX86CpuIDAndInfo(0x80000000, &MaxExtLevel, &EBX, &ECX, &EDX);
1708bdd1243dSDimitry Andric 
1709bdd1243dSDimitry Andric   bool HasExtLeaf1 = MaxExtLevel >= 0x80000001 &&
1710bdd1243dSDimitry Andric                      !getX86CpuIDAndInfo(0x80000001, &EAX, &EBX, &ECX, &EDX);
1711bdd1243dSDimitry Andric   Features["sahf"]   = HasExtLeaf1 && ((ECX >>  0) & 1);
1712bdd1243dSDimitry Andric   Features["lzcnt"]  = HasExtLeaf1 && ((ECX >>  5) & 1);
1713bdd1243dSDimitry Andric   Features["sse4a"]  = HasExtLeaf1 && ((ECX >>  6) & 1);
1714bdd1243dSDimitry Andric   Features["prfchw"] = HasExtLeaf1 && ((ECX >>  8) & 1);
1715bdd1243dSDimitry Andric   Features["xop"]    = HasExtLeaf1 && ((ECX >> 11) & 1) && HasAVXSave;
1716bdd1243dSDimitry Andric   Features["lwp"]    = HasExtLeaf1 && ((ECX >> 15) & 1);
1717bdd1243dSDimitry Andric   Features["fma4"]   = HasExtLeaf1 && ((ECX >> 16) & 1) && HasAVXSave;
1718bdd1243dSDimitry Andric   Features["tbm"]    = HasExtLeaf1 && ((ECX >> 21) & 1);
1719bdd1243dSDimitry Andric   Features["mwaitx"] = HasExtLeaf1 && ((ECX >> 29) & 1);
1720bdd1243dSDimitry Andric 
1721bdd1243dSDimitry Andric   Features["64bit"]  = HasExtLeaf1 && ((EDX >> 29) & 1);
1722bdd1243dSDimitry Andric 
1723bdd1243dSDimitry Andric   // Miscellaneous memory related features, detected by
1724bdd1243dSDimitry Andric   // using the 0x80000008 leaf of the CPUID instruction
1725bdd1243dSDimitry Andric   bool HasExtLeaf8 = MaxExtLevel >= 0x80000008 &&
1726bdd1243dSDimitry Andric                      !getX86CpuIDAndInfo(0x80000008, &EAX, &EBX, &ECX, &EDX);
1727bdd1243dSDimitry Andric   Features["clzero"]   = HasExtLeaf8 && ((EBX >> 0) & 1);
1728bdd1243dSDimitry Andric   Features["rdpru"]    = HasExtLeaf8 && ((EBX >> 4) & 1);
1729bdd1243dSDimitry Andric   Features["wbnoinvd"] = HasExtLeaf8 && ((EBX >> 9) & 1);
1730bdd1243dSDimitry Andric 
1731bdd1243dSDimitry Andric   bool HasLeaf7 =
1732bdd1243dSDimitry Andric       MaxLevel >= 7 && !getX86CpuIDAndInfoEx(0x7, 0x0, &EAX, &EBX, &ECX, &EDX);
1733bdd1243dSDimitry Andric 
1734bdd1243dSDimitry Andric   Features["fsgsbase"]   = HasLeaf7 && ((EBX >>  0) & 1);
1735bdd1243dSDimitry Andric   Features["sgx"]        = HasLeaf7 && ((EBX >>  2) & 1);
1736bdd1243dSDimitry Andric   Features["bmi"]        = HasLeaf7 && ((EBX >>  3) & 1);
1737bdd1243dSDimitry Andric   // AVX2 is only supported if we have the OS save support from AVX.
1738bdd1243dSDimitry Andric   Features["avx2"]       = HasLeaf7 && ((EBX >>  5) & 1) && HasAVXSave;
1739bdd1243dSDimitry Andric   Features["bmi2"]       = HasLeaf7 && ((EBX >>  8) & 1);
1740bdd1243dSDimitry Andric   Features["invpcid"]    = HasLeaf7 && ((EBX >> 10) & 1);
1741bdd1243dSDimitry Andric   Features["rtm"]        = HasLeaf7 && ((EBX >> 11) & 1);
1742bdd1243dSDimitry Andric   // AVX512 is only supported if the OS supports the context save for it.
1743bdd1243dSDimitry Andric   Features["avx512f"]    = HasLeaf7 && ((EBX >> 16) & 1) && HasAVX512Save;
1744bdd1243dSDimitry Andric   Features["avx512dq"]   = HasLeaf7 && ((EBX >> 17) & 1) && HasAVX512Save;
1745bdd1243dSDimitry Andric   Features["rdseed"]     = HasLeaf7 && ((EBX >> 18) & 1);
1746bdd1243dSDimitry Andric   Features["adx"]        = HasLeaf7 && ((EBX >> 19) & 1);
1747bdd1243dSDimitry Andric   Features["avx512ifma"] = HasLeaf7 && ((EBX >> 21) & 1) && HasAVX512Save;
1748bdd1243dSDimitry Andric   Features["clflushopt"] = HasLeaf7 && ((EBX >> 23) & 1);
1749bdd1243dSDimitry Andric   Features["clwb"]       = HasLeaf7 && ((EBX >> 24) & 1);
1750bdd1243dSDimitry Andric   Features["avx512pf"]   = HasLeaf7 && ((EBX >> 26) & 1) && HasAVX512Save;
1751bdd1243dSDimitry Andric   Features["avx512er"]   = HasLeaf7 && ((EBX >> 27) & 1) && HasAVX512Save;
1752bdd1243dSDimitry Andric   Features["avx512cd"]   = HasLeaf7 && ((EBX >> 28) & 1) && HasAVX512Save;
1753bdd1243dSDimitry Andric   Features["sha"]        = HasLeaf7 && ((EBX >> 29) & 1);
1754bdd1243dSDimitry Andric   Features["avx512bw"]   = HasLeaf7 && ((EBX >> 30) & 1) && HasAVX512Save;
1755bdd1243dSDimitry Andric   Features["avx512vl"]   = HasLeaf7 && ((EBX >> 31) & 1) && HasAVX512Save;
1756bdd1243dSDimitry Andric 
1757bdd1243dSDimitry Andric   Features["prefetchwt1"]     = HasLeaf7 && ((ECX >>  0) & 1);
1758bdd1243dSDimitry Andric   Features["avx512vbmi"]      = HasLeaf7 && ((ECX >>  1) & 1) && HasAVX512Save;
1759bdd1243dSDimitry Andric   Features["pku"]             = HasLeaf7 && ((ECX >>  4) & 1);
1760bdd1243dSDimitry Andric   Features["waitpkg"]         = HasLeaf7 && ((ECX >>  5) & 1);
1761bdd1243dSDimitry Andric   Features["avx512vbmi2"]     = HasLeaf7 && ((ECX >>  6) & 1) && HasAVX512Save;
1762bdd1243dSDimitry Andric   Features["shstk"]           = HasLeaf7 && ((ECX >>  7) & 1);
1763bdd1243dSDimitry Andric   Features["gfni"]            = HasLeaf7 && ((ECX >>  8) & 1);
1764bdd1243dSDimitry Andric   Features["vaes"]            = HasLeaf7 && ((ECX >>  9) & 1) && HasAVXSave;
1765bdd1243dSDimitry Andric   Features["vpclmulqdq"]      = HasLeaf7 && ((ECX >> 10) & 1) && HasAVXSave;
1766bdd1243dSDimitry Andric   Features["avx512vnni"]      = HasLeaf7 && ((ECX >> 11) & 1) && HasAVX512Save;
1767bdd1243dSDimitry Andric   Features["avx512bitalg"]    = HasLeaf7 && ((ECX >> 12) & 1) && HasAVX512Save;
1768bdd1243dSDimitry Andric   Features["avx512vpopcntdq"] = HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save;
1769bdd1243dSDimitry Andric   Features["rdpid"]           = HasLeaf7 && ((ECX >> 22) & 1);
1770bdd1243dSDimitry Andric   Features["kl"]              = HasLeaf7 && ((ECX >> 23) & 1); // key locker
1771bdd1243dSDimitry Andric   Features["cldemote"]        = HasLeaf7 && ((ECX >> 25) & 1);
1772bdd1243dSDimitry Andric   Features["movdiri"]         = HasLeaf7 && ((ECX >> 27) & 1);
1773bdd1243dSDimitry Andric   Features["movdir64b"]       = HasLeaf7 && ((ECX >> 28) & 1);
1774bdd1243dSDimitry Andric   Features["enqcmd"]          = HasLeaf7 && ((ECX >> 29) & 1);
1775bdd1243dSDimitry Andric 
1776bdd1243dSDimitry Andric   Features["uintr"]           = HasLeaf7 && ((EDX >> 5) & 1);
1777bdd1243dSDimitry Andric   Features["avx512vp2intersect"] =
1778bdd1243dSDimitry Andric       HasLeaf7 && ((EDX >> 8) & 1) && HasAVX512Save;
1779bdd1243dSDimitry Andric   Features["serialize"]       = HasLeaf7 && ((EDX >> 14) & 1);
1780bdd1243dSDimitry Andric   Features["tsxldtrk"]        = HasLeaf7 && ((EDX >> 16) & 1);
1781bdd1243dSDimitry Andric   // There are two CPUID leafs which information associated with the pconfig
1782bdd1243dSDimitry Andric   // instruction:
1783bdd1243dSDimitry Andric   // EAX=0x7, ECX=0x0 indicates the availability of the instruction (via the 18th
1784bdd1243dSDimitry Andric   // bit of EDX), while the EAX=0x1b leaf returns information on the
1785bdd1243dSDimitry Andric   // availability of specific pconfig leafs.
1786bdd1243dSDimitry Andric   // The target feature here only refers to the the first of these two.
1787bdd1243dSDimitry Andric   // Users might need to check for the availability of specific pconfig
1788bdd1243dSDimitry Andric   // leaves using cpuid, since that information is ignored while
1789bdd1243dSDimitry Andric   // detecting features using the "-march=native" flag.
1790bdd1243dSDimitry Andric   // For more info, see X86 ISA docs.
1791bdd1243dSDimitry Andric   Features["pconfig"] = HasLeaf7 && ((EDX >> 18) & 1);
1792bdd1243dSDimitry Andric   Features["amx-bf16"]   = HasLeaf7 && ((EDX >> 22) & 1) && HasAMXSave;
1793bdd1243dSDimitry Andric   Features["avx512fp16"] = HasLeaf7 && ((EDX >> 23) & 1) && HasAVX512Save;
1794bdd1243dSDimitry Andric   Features["amx-tile"]   = HasLeaf7 && ((EDX >> 24) & 1) && HasAMXSave;
1795bdd1243dSDimitry Andric   Features["amx-int8"]   = HasLeaf7 && ((EDX >> 25) & 1) && HasAMXSave;
17968a4dda33SDimitry Andric   // EAX from subleaf 0 is the maximum subleaf supported. Some CPUs don't
17978a4dda33SDimitry Andric   // return all 0s for invalid subleaves so check the limit.
1798bdd1243dSDimitry Andric   bool HasLeaf7Subleaf1 =
17998a4dda33SDimitry Andric       HasLeaf7 && EAX >= 1 &&
18008a4dda33SDimitry Andric       !getX86CpuIDAndInfoEx(0x7, 0x1, &EAX, &EBX, &ECX, &EDX);
180106c3fb27SDimitry Andric   Features["sha512"]     = HasLeaf7Subleaf1 && ((EAX >> 0) & 1);
180206c3fb27SDimitry Andric   Features["sm3"]        = HasLeaf7Subleaf1 && ((EAX >> 1) & 1);
180306c3fb27SDimitry Andric   Features["sm4"]        = HasLeaf7Subleaf1 && ((EAX >> 2) & 1);
1804bdd1243dSDimitry Andric   Features["raoint"]     = HasLeaf7Subleaf1 && ((EAX >> 3) & 1);
1805bdd1243dSDimitry Andric   Features["avxvnni"]    = HasLeaf7Subleaf1 && ((EAX >> 4) & 1) && HasAVXSave;
1806bdd1243dSDimitry Andric   Features["avx512bf16"] = HasLeaf7Subleaf1 && ((EAX >> 5) & 1) && HasAVX512Save;
1807bdd1243dSDimitry Andric   Features["amx-fp16"]   = HasLeaf7Subleaf1 && ((EAX >> 21) & 1) && HasAMXSave;
1808bdd1243dSDimitry Andric   Features["cmpccxadd"]  = HasLeaf7Subleaf1 && ((EAX >> 7) & 1);
1809bdd1243dSDimitry Andric   Features["hreset"]     = HasLeaf7Subleaf1 && ((EAX >> 22) & 1);
1810bdd1243dSDimitry Andric   Features["avxifma"]    = HasLeaf7Subleaf1 && ((EAX >> 23) & 1) && HasAVXSave;
1811bdd1243dSDimitry Andric   Features["avxvnniint8"] = HasLeaf7Subleaf1 && ((EDX >> 4) & 1) && HasAVXSave;
1812bdd1243dSDimitry Andric   Features["avxneconvert"] = HasLeaf7Subleaf1 && ((EDX >> 5) & 1) && HasAVXSave;
181306c3fb27SDimitry Andric   Features["amx-complex"] = HasLeaf7Subleaf1 && ((EDX >> 8) & 1) && HasAMXSave;
181406c3fb27SDimitry Andric   Features["avxvnniint16"] = HasLeaf7Subleaf1 && ((EDX >> 10) & 1) && HasAVXSave;
1815bdd1243dSDimitry Andric   Features["prefetchi"]  = HasLeaf7Subleaf1 && ((EDX >> 14) & 1);
1816*5f757f3fSDimitry Andric   Features["usermsr"]  = HasLeaf7Subleaf1 && ((EDX >> 15) & 1);
1817*5f757f3fSDimitry Andric   Features["avx10.1-256"] = HasLeaf7Subleaf1 && ((EDX >> 19) & 1);
1818bdd1243dSDimitry Andric 
1819bdd1243dSDimitry Andric   bool HasLeafD = MaxLevel >= 0xd &&
1820bdd1243dSDimitry Andric                   !getX86CpuIDAndInfoEx(0xd, 0x1, &EAX, &EBX, &ECX, &EDX);
1821bdd1243dSDimitry Andric 
1822bdd1243dSDimitry Andric   // Only enable XSAVE if OS has enabled support for saving YMM state.
1823bdd1243dSDimitry Andric   Features["xsaveopt"] = HasLeafD && ((EAX >> 0) & 1) && HasAVXSave;
1824bdd1243dSDimitry Andric   Features["xsavec"]   = HasLeafD && ((EAX >> 1) & 1) && HasAVXSave;
1825bdd1243dSDimitry Andric   Features["xsaves"]   = HasLeafD && ((EAX >> 3) & 1) && HasAVXSave;
1826bdd1243dSDimitry Andric 
1827bdd1243dSDimitry Andric   bool HasLeaf14 = MaxLevel >= 0x14 &&
1828bdd1243dSDimitry Andric                   !getX86CpuIDAndInfoEx(0x14, 0x0, &EAX, &EBX, &ECX, &EDX);
1829bdd1243dSDimitry Andric 
1830bdd1243dSDimitry Andric   Features["ptwrite"] = HasLeaf14 && ((EBX >> 4) & 1);
1831bdd1243dSDimitry Andric 
1832bdd1243dSDimitry Andric   bool HasLeaf19 =
1833bdd1243dSDimitry Andric       MaxLevel >= 0x19 && !getX86CpuIDAndInfo(0x19, &EAX, &EBX, &ECX, &EDX);
1834bdd1243dSDimitry Andric   Features["widekl"] = HasLeaf7 && HasLeaf19 && ((EBX >> 2) & 1);
1835bdd1243dSDimitry Andric 
1836*5f757f3fSDimitry Andric   bool HasLeaf24 =
1837*5f757f3fSDimitry Andric       MaxLevel >= 0x24 && !getX86CpuIDAndInfo(0x24, &EAX, &EBX, &ECX, &EDX);
1838*5f757f3fSDimitry Andric   Features["avx10.1-512"] =
1839*5f757f3fSDimitry Andric       Features["avx10.1-256"] && HasLeaf24 && ((EBX >> 18) & 1);
1840*5f757f3fSDimitry Andric 
1841bdd1243dSDimitry Andric   return true;
1842bdd1243dSDimitry Andric }
1843bdd1243dSDimitry Andric #elif defined(__linux__) && (defined(__arm__) || defined(__aarch64__))
1844bdd1243dSDimitry Andric bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
1845bdd1243dSDimitry Andric   std::unique_ptr<llvm::MemoryBuffer> P = getProcCpuinfoContent();
1846bdd1243dSDimitry Andric   if (!P)
1847bdd1243dSDimitry Andric     return false;
1848bdd1243dSDimitry Andric 
1849bdd1243dSDimitry Andric   SmallVector<StringRef, 32> Lines;
1850bdd1243dSDimitry Andric   P->getBuffer().split(Lines, "\n");
1851bdd1243dSDimitry Andric 
1852bdd1243dSDimitry Andric   SmallVector<StringRef, 32> CPUFeatures;
1853bdd1243dSDimitry Andric 
1854bdd1243dSDimitry Andric   // Look for the CPU features.
1855bdd1243dSDimitry Andric   for (unsigned I = 0, E = Lines.size(); I != E; ++I)
1856*5f757f3fSDimitry Andric     if (Lines[I].starts_with("Features")) {
1857bdd1243dSDimitry Andric       Lines[I].split(CPUFeatures, ' ');
1858bdd1243dSDimitry Andric       break;
1859bdd1243dSDimitry Andric     }
1860bdd1243dSDimitry Andric 
1861bdd1243dSDimitry Andric #if defined(__aarch64__)
1862bdd1243dSDimitry Andric   // Keep track of which crypto features we have seen
1863bdd1243dSDimitry Andric   enum { CAP_AES = 0x1, CAP_PMULL = 0x2, CAP_SHA1 = 0x4, CAP_SHA2 = 0x8 };
1864bdd1243dSDimitry Andric   uint32_t crypto = 0;
1865bdd1243dSDimitry Andric #endif
1866bdd1243dSDimitry Andric 
1867bdd1243dSDimitry Andric   for (unsigned I = 0, E = CPUFeatures.size(); I != E; ++I) {
1868bdd1243dSDimitry Andric     StringRef LLVMFeatureStr = StringSwitch<StringRef>(CPUFeatures[I])
1869bdd1243dSDimitry Andric #if defined(__aarch64__)
1870bdd1243dSDimitry Andric                                    .Case("asimd", "neon")
1871bdd1243dSDimitry Andric                                    .Case("fp", "fp-armv8")
1872bdd1243dSDimitry Andric                                    .Case("crc32", "crc")
1873bdd1243dSDimitry Andric                                    .Case("atomics", "lse")
1874bdd1243dSDimitry Andric                                    .Case("sve", "sve")
1875bdd1243dSDimitry Andric                                    .Case("sve2", "sve2")
1876bdd1243dSDimitry Andric #else
1877bdd1243dSDimitry Andric                                    .Case("half", "fp16")
1878bdd1243dSDimitry Andric                                    .Case("neon", "neon")
1879bdd1243dSDimitry Andric                                    .Case("vfpv3", "vfp3")
1880bdd1243dSDimitry Andric                                    .Case("vfpv3d16", "vfp3d16")
1881bdd1243dSDimitry Andric                                    .Case("vfpv4", "vfp4")
1882bdd1243dSDimitry Andric                                    .Case("idiva", "hwdiv-arm")
1883bdd1243dSDimitry Andric                                    .Case("idivt", "hwdiv")
1884bdd1243dSDimitry Andric #endif
1885bdd1243dSDimitry Andric                                    .Default("");
1886bdd1243dSDimitry Andric 
1887bdd1243dSDimitry Andric #if defined(__aarch64__)
1888bdd1243dSDimitry Andric     // We need to check crypto separately since we need all of the crypto
1889bdd1243dSDimitry Andric     // extensions to enable the subtarget feature
1890bdd1243dSDimitry Andric     if (CPUFeatures[I] == "aes")
1891bdd1243dSDimitry Andric       crypto |= CAP_AES;
1892bdd1243dSDimitry Andric     else if (CPUFeatures[I] == "pmull")
1893bdd1243dSDimitry Andric       crypto |= CAP_PMULL;
1894bdd1243dSDimitry Andric     else if (CPUFeatures[I] == "sha1")
1895bdd1243dSDimitry Andric       crypto |= CAP_SHA1;
1896bdd1243dSDimitry Andric     else if (CPUFeatures[I] == "sha2")
1897bdd1243dSDimitry Andric       crypto |= CAP_SHA2;
1898bdd1243dSDimitry Andric #endif
1899bdd1243dSDimitry Andric 
1900bdd1243dSDimitry Andric     if (LLVMFeatureStr != "")
1901bdd1243dSDimitry Andric       Features[LLVMFeatureStr] = true;
1902bdd1243dSDimitry Andric   }
1903bdd1243dSDimitry Andric 
1904bdd1243dSDimitry Andric #if defined(__aarch64__)
1905bdd1243dSDimitry Andric   // If we have all crypto bits we can add the feature
1906bdd1243dSDimitry Andric   if (crypto == (CAP_AES | CAP_PMULL | CAP_SHA1 | CAP_SHA2))
1907bdd1243dSDimitry Andric     Features["crypto"] = true;
1908bdd1243dSDimitry Andric #endif
1909bdd1243dSDimitry Andric 
1910bdd1243dSDimitry Andric   return true;
1911bdd1243dSDimitry Andric }
1912bdd1243dSDimitry Andric #elif defined(_WIN32) && (defined(__aarch64__) || defined(_M_ARM64))
1913bdd1243dSDimitry Andric bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
1914bdd1243dSDimitry Andric   if (IsProcessorFeaturePresent(PF_ARM_NEON_INSTRUCTIONS_AVAILABLE))
1915bdd1243dSDimitry Andric     Features["neon"] = true;
1916bdd1243dSDimitry Andric   if (IsProcessorFeaturePresent(PF_ARM_V8_CRC32_INSTRUCTIONS_AVAILABLE))
1917bdd1243dSDimitry Andric     Features["crc"] = true;
1918bdd1243dSDimitry Andric   if (IsProcessorFeaturePresent(PF_ARM_V8_CRYPTO_INSTRUCTIONS_AVAILABLE))
1919bdd1243dSDimitry Andric     Features["crypto"] = true;
1920bdd1243dSDimitry Andric 
1921bdd1243dSDimitry Andric   return true;
1922bdd1243dSDimitry Andric }
192306c3fb27SDimitry Andric #elif defined(__linux__) && defined(__loongarch__)
192406c3fb27SDimitry Andric #include <sys/auxv.h>
192506c3fb27SDimitry Andric bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
192606c3fb27SDimitry Andric   unsigned long hwcap = getauxval(AT_HWCAP);
192706c3fb27SDimitry Andric   bool HasFPU = hwcap & (1UL << 3); // HWCAP_LOONGARCH_FPU
192806c3fb27SDimitry Andric   uint32_t cpucfg2 = 0x2;
192906c3fb27SDimitry Andric   __asm__("cpucfg %[cpucfg2], %[cpucfg2]\n\t" : [cpucfg2] "+r"(cpucfg2));
193006c3fb27SDimitry Andric 
193106c3fb27SDimitry Andric   Features["f"] = HasFPU && (cpucfg2 & (1U << 1)); // CPUCFG.2.FP_SP
193206c3fb27SDimitry Andric   Features["d"] = HasFPU && (cpucfg2 & (1U << 2)); // CPUCFG.2.FP_DP
193306c3fb27SDimitry Andric 
193406c3fb27SDimitry Andric   Features["lsx"] = hwcap & (1UL << 4);  // HWCAP_LOONGARCH_LSX
193506c3fb27SDimitry Andric   Features["lasx"] = hwcap & (1UL << 5); // HWCAP_LOONGARCH_LASX
193606c3fb27SDimitry Andric   Features["lvz"] = hwcap & (1UL << 9);  // HWCAP_LOONGARCH_LVZ
193706c3fb27SDimitry Andric 
193806c3fb27SDimitry Andric   return true;
193906c3fb27SDimitry Andric }
1940bdd1243dSDimitry Andric #else
1941bdd1243dSDimitry Andric bool sys::getHostCPUFeatures(StringMap<bool> &Features) { return false; }
1942bdd1243dSDimitry Andric #endif
1943bdd1243dSDimitry Andric 
194406c3fb27SDimitry Andric #if __APPLE__
194506c3fb27SDimitry Andric /// \returns the \p triple, but with the Host's arch spliced in.
194606c3fb27SDimitry Andric static Triple withHostArch(Triple T) {
194706c3fb27SDimitry Andric #if defined(__arm__)
194806c3fb27SDimitry Andric   T.setArch(Triple::arm);
194906c3fb27SDimitry Andric   T.setArchName("arm");
195006c3fb27SDimitry Andric #elif defined(__arm64e__)
195106c3fb27SDimitry Andric   T.setArch(Triple::aarch64, Triple::AArch64SubArch_arm64e);
195206c3fb27SDimitry Andric   T.setArchName("arm64e");
195306c3fb27SDimitry Andric #elif defined(__aarch64__)
195406c3fb27SDimitry Andric   T.setArch(Triple::aarch64);
195506c3fb27SDimitry Andric   T.setArchName("arm64");
195606c3fb27SDimitry Andric #elif defined(__x86_64h__)
195706c3fb27SDimitry Andric   T.setArch(Triple::x86_64);
195806c3fb27SDimitry Andric   T.setArchName("x86_64h");
195906c3fb27SDimitry Andric #elif defined(__x86_64__)
196006c3fb27SDimitry Andric   T.setArch(Triple::x86_64);
196106c3fb27SDimitry Andric   T.setArchName("x86_64");
1962*5f757f3fSDimitry Andric #elif defined(__i386__)
1963*5f757f3fSDimitry Andric   T.setArch(Triple::x86);
1964*5f757f3fSDimitry Andric   T.setArchName("i386");
196506c3fb27SDimitry Andric #elif defined(__powerpc__)
196606c3fb27SDimitry Andric   T.setArch(Triple::ppc);
196706c3fb27SDimitry Andric   T.setArchName("powerpc");
196806c3fb27SDimitry Andric #else
196906c3fb27SDimitry Andric #  error "Unimplemented host arch fixup"
197006c3fb27SDimitry Andric #endif
197106c3fb27SDimitry Andric   return T;
197206c3fb27SDimitry Andric }
197306c3fb27SDimitry Andric #endif
197406c3fb27SDimitry Andric 
1975bdd1243dSDimitry Andric std::string sys::getProcessTriple() {
1976bdd1243dSDimitry Andric   std::string TargetTripleString = updateTripleOSVersion(LLVM_HOST_TRIPLE);
1977bdd1243dSDimitry Andric   Triple PT(Triple::normalize(TargetTripleString));
1978bdd1243dSDimitry Andric 
197906c3fb27SDimitry Andric #if __APPLE__
198006c3fb27SDimitry Andric   /// In Universal builds, LLVM_HOST_TRIPLE will have the wrong arch in one of
198106c3fb27SDimitry Andric   /// the slices. This fixes that up.
198206c3fb27SDimitry Andric   PT = withHostArch(PT);
198306c3fb27SDimitry Andric #endif
198406c3fb27SDimitry Andric 
1985bdd1243dSDimitry Andric   if (sizeof(void *) == 8 && PT.isArch32Bit())
1986bdd1243dSDimitry Andric     PT = PT.get64BitArchVariant();
1987bdd1243dSDimitry Andric   if (sizeof(void *) == 4 && PT.isArch64Bit())
1988bdd1243dSDimitry Andric     PT = PT.get32BitArchVariant();
1989bdd1243dSDimitry Andric 
1990bdd1243dSDimitry Andric   return PT.str();
1991bdd1243dSDimitry Andric }
1992bdd1243dSDimitry Andric 
1993bdd1243dSDimitry Andric void sys::printDefaultTargetAndDetectedCPU(raw_ostream &OS) {
1994bdd1243dSDimitry Andric #if LLVM_VERSION_PRINTER_SHOW_HOST_TARGET_INFO
1995bdd1243dSDimitry Andric   std::string CPU = std::string(sys::getHostCPUName());
1996bdd1243dSDimitry Andric   if (CPU == "generic")
1997bdd1243dSDimitry Andric     CPU = "(unknown)";
1998bdd1243dSDimitry Andric   OS << "  Default target: " << sys::getDefaultTargetTriple() << '\n'
1999bdd1243dSDimitry Andric      << "  Host CPU: " << CPU << '\n';
2000bdd1243dSDimitry Andric #endif
2001bdd1243dSDimitry Andric }
2002