1//===--- X86.cpp - Implement X86 target feature support -------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements X86 TargetInfo objects.
10//
11//===----------------------------------------------------------------------===//
12
13#include "X86.h"
14#include "clang/Basic/Builtins.h"
15#include "clang/Basic/Diagnostic.h"
16#include "clang/Basic/TargetBuiltins.h"
17#include "llvm/ADT/StringRef.h"
18#include "llvm/ADT/StringSwitch.h"
19#include "llvm/TargetParser/X86TargetParser.h"
20#include <optional>
21
22namespace clang {
23namespace targets {
24
25// The x86-32 builtins are a subset and prefix of the x86-64 builtins.
26static constexpr int NumX86Builtins =
27 X86::LastX86CommonBuiltin - Builtin::FirstTSBuiltin + 1;
28static constexpr int NumX86_64Builtins =
29 X86::LastTSBuiltin - X86::FirstX86_64Builtin;
30static constexpr int NumBuiltins = X86::LastTSBuiltin - Builtin::FirstTSBuiltin;
31static_assert(NumBuiltins == (NumX86Builtins + NumX86_64Builtins));
32
33namespace X86 {
34#define GET_BUILTIN_STR_TABLE
35#include "clang/Basic/BuiltinsX86.inc"
36#undef GET_BUILTIN_STR_TABLE
37
38static constexpr Builtin::Info BuiltinInfos[] = {
39#define GET_BUILTIN_INFOS
40#include "clang/Basic/BuiltinsX86.inc"
41#undef GET_BUILTIN_INFOS
42};
43
44static constexpr Builtin::Info PrefixedBuiltinInfos[] = {
45#define GET_BUILTIN_PREFIXED_INFOS
46#include "clang/Basic/BuiltinsX86.inc"
47#undef GET_BUILTIN_PREFIXED_INFOS
48};
49static_assert((std::size(BuiltinInfos) + std::size(PrefixedBuiltinInfos)) ==
50 NumX86Builtins);
51} // namespace X86
52
53namespace X86_64 {
54#define GET_BUILTIN_STR_TABLE
55#include "clang/Basic/BuiltinsX86_64.inc"
56#undef GET_BUILTIN_STR_TABLE
57
58static constexpr Builtin::Info BuiltinInfos[] = {
59#define GET_BUILTIN_INFOS
60#include "clang/Basic/BuiltinsX86_64.inc"
61#undef GET_BUILTIN_INFOS
62};
63
64static constexpr Builtin::Info PrefixedBuiltinInfos[] = {
65#define GET_BUILTIN_PREFIXED_INFOS
66#include "clang/Basic/BuiltinsX86_64.inc"
67#undef GET_BUILTIN_PREFIXED_INFOS
68};
69static_assert((std::size(BuiltinInfos) + std::size(PrefixedBuiltinInfos)) ==
70 NumX86_64Builtins);
71} // namespace X86_64
72
73static const char *const GCCRegNames[] = {
74 "ax", "dx", "cx", "bx", "si", "di", "bp", "sp",
75 "st", "st(1)", "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)",
76 "argp", "flags", "fpcr", "fpsr", "dirflag", "frame", "xmm0", "xmm1",
77 "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7", "mm0", "mm1",
78 "mm2", "mm3", "mm4", "mm5", "mm6", "mm7", "r8", "r9",
79 "r10", "r11", "r12", "r13", "r14", "r15", "xmm8", "xmm9",
80 "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15", "ymm0", "ymm1",
81 "ymm2", "ymm3", "ymm4", "ymm5", "ymm6", "ymm7", "ymm8", "ymm9",
82 "ymm10", "ymm11", "ymm12", "ymm13", "ymm14", "ymm15", "xmm16", "xmm17",
83 "xmm18", "xmm19", "xmm20", "xmm21", "xmm22", "xmm23", "xmm24", "xmm25",
84 "xmm26", "xmm27", "xmm28", "xmm29", "xmm30", "xmm31", "ymm16", "ymm17",
85 "ymm18", "ymm19", "ymm20", "ymm21", "ymm22", "ymm23", "ymm24", "ymm25",
86 "ymm26", "ymm27", "ymm28", "ymm29", "ymm30", "ymm31", "zmm0", "zmm1",
87 "zmm2", "zmm3", "zmm4", "zmm5", "zmm6", "zmm7", "zmm8", "zmm9",
88 "zmm10", "zmm11", "zmm12", "zmm13", "zmm14", "zmm15", "zmm16", "zmm17",
89 "zmm18", "zmm19", "zmm20", "zmm21", "zmm22", "zmm23", "zmm24", "zmm25",
90 "zmm26", "zmm27", "zmm28", "zmm29", "zmm30", "zmm31", "k0", "k1",
91 "k2", "k3", "k4", "k5", "k6", "k7",
92 "cr0", "cr2", "cr3", "cr4", "cr8",
93 "dr0", "dr1", "dr2", "dr3", "dr6", "dr7",
94 "bnd0", "bnd1", "bnd2", "bnd3",
95 "tmm0", "tmm1", "tmm2", "tmm3", "tmm4", "tmm5", "tmm6", "tmm7",
96 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
97 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
98};
99
100const TargetInfo::AddlRegName AddlRegNames[] = {
101 {.Names: {"al", "ah", "eax", "rax"}, .RegNum: 0},
102 {.Names: {"bl", "bh", "ebx", "rbx"}, .RegNum: 3},
103 {.Names: {"cl", "ch", "ecx", "rcx"}, .RegNum: 2},
104 {.Names: {"dl", "dh", "edx", "rdx"}, .RegNum: 1},
105 {.Names: {"esi", "rsi"}, .RegNum: 4},
106 {.Names: {"edi", "rdi"}, .RegNum: 5},
107 {.Names: {"esp", "rsp"}, .RegNum: 7},
108 {.Names: {"ebp", "rbp"}, .RegNum: 6},
109 {.Names: {"r8d", "r8w", "r8b"}, .RegNum: 38},
110 {.Names: {"r9d", "r9w", "r9b"}, .RegNum: 39},
111 {.Names: {"r10d", "r10w", "r10b"}, .RegNum: 40},
112 {.Names: {"r11d", "r11w", "r11b"}, .RegNum: 41},
113 {.Names: {"r12d", "r12w", "r12b"}, .RegNum: 42},
114 {.Names: {"r13d", "r13w", "r13b"}, .RegNum: 43},
115 {.Names: {"r14d", "r14w", "r14b"}, .RegNum: 44},
116 {.Names: {"r15d", "r15w", "r15b"}, .RegNum: 45},
117 {.Names: {"r16d", "r16w", "r16b"}, .RegNum: 165},
118 {.Names: {"r17d", "r17w", "r17b"}, .RegNum: 166},
119 {.Names: {"r18d", "r18w", "r18b"}, .RegNum: 167},
120 {.Names: {"r19d", "r19w", "r19b"}, .RegNum: 168},
121 {.Names: {"r20d", "r20w", "r20b"}, .RegNum: 169},
122 {.Names: {"r21d", "r21w", "r21b"}, .RegNum: 170},
123 {.Names: {"r22d", "r22w", "r22b"}, .RegNum: 171},
124 {.Names: {"r23d", "r23w", "r23b"}, .RegNum: 172},
125 {.Names: {"r24d", "r24w", "r24b"}, .RegNum: 173},
126 {.Names: {"r25d", "r25w", "r25b"}, .RegNum: 174},
127 {.Names: {"r26d", "r26w", "r26b"}, .RegNum: 175},
128 {.Names: {"r27d", "r27w", "r27b"}, .RegNum: 176},
129 {.Names: {"r28d", "r28w", "r28b"}, .RegNum: 177},
130 {.Names: {"r29d", "r29w", "r29b"}, .RegNum: 178},
131 {.Names: {"r30d", "r30w", "r30b"}, .RegNum: 179},
132 {.Names: {"r31d", "r31w", "r31b"}, .RegNum: 180},
133};
134} // namespace targets
135} // namespace clang
136
137using namespace clang;
138using namespace clang::targets;
139
140bool X86TargetInfo::setFPMath(StringRef Name) {
141 if (Name == "387") {
142 FPMath = FP_387;
143 return true;
144 }
145 if (Name == "sse") {
146 FPMath = FP_SSE;
147 return true;
148 }
149 return false;
150}
151
152bool X86TargetInfo::initFeatureMap(
153 llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
154 const std::vector<std::string> &FeaturesVec) const {
155 // FIXME: This *really* should not be here.
156 // X86_64 always has SSE2.
157 if (getTriple().getArch() == llvm::Triple::x86_64)
158 setFeatureEnabled(Features, Name: "sse2", Enabled: true);
159
160 using namespace llvm::X86;
161
162 SmallVector<StringRef, 16> CPUFeatures;
163 getFeaturesForCPU(CPU, Features&: CPUFeatures);
164 for (auto &F : CPUFeatures)
165 setFeatureEnabled(Features, Name: F, Enabled: true);
166
167 std::vector<std::string> UpdatedFeaturesVec;
168 for (const auto &Feature : FeaturesVec) {
169 // Expand general-regs-only to -x86, -mmx and -sse
170 if (Feature == "+general-regs-only") {
171 UpdatedFeaturesVec.push_back(x: "-x87");
172 UpdatedFeaturesVec.push_back(x: "-mmx");
173 UpdatedFeaturesVec.push_back(x: "-sse");
174 continue;
175 }
176
177 if (Feature == "+apxf" || Feature == "-apxf") {
178 char Sign = Feature[0];
179 for (const char *Sub :
180 {"egpr", "push2pop2", "ppx", "ndd", "ccmp", "nf", "zu", "jmpabs"})
181 UpdatedFeaturesVec.push_back(x: Sign + std::string(Sub));
182 continue;
183 }
184
185 UpdatedFeaturesVec.push_back(x: Feature);
186 }
187
188 if (!TargetInfo::initFeatureMap(Features, Diags, CPU, FeatureVec: UpdatedFeaturesVec))
189 return false;
190
191 // Can't do this earlier because we need to be able to explicitly enable
192 // or disable these features and the things that they depend upon.
193
194 // Enable popcnt if sse4.2 is enabled and popcnt is not explicitly disabled.
195 auto I = Features.find(Key: "sse4.2");
196 if (I != Features.end() && I->getValue() &&
197 !llvm::is_contained(Range&: UpdatedFeaturesVec, Element: "-popcnt"))
198 Features["popcnt"] = true;
199
200 // Additionally, if SSE is enabled and mmx is not explicitly disabled,
201 // then enable MMX.
202 I = Features.find(Key: "sse");
203 if (I != Features.end() && I->getValue() &&
204 !llvm::is_contained(Range&: UpdatedFeaturesVec, Element: "-mmx"))
205 Features["mmx"] = true;
206
207 // Enable xsave if avx is enabled and xsave is not explicitly disabled.
208 I = Features.find(Key: "avx");
209 if (I != Features.end() && I->getValue() &&
210 !llvm::is_contained(Range&: UpdatedFeaturesVec, Element: "-xsave"))
211 Features["xsave"] = true;
212
213 // Enable CRC32 if SSE4.2 is enabled and CRC32 is not explicitly disabled.
214 I = Features.find(Key: "sse4.2");
215 if (I != Features.end() && I->getValue() &&
216 !llvm::is_contained(Range&: UpdatedFeaturesVec, Element: "-crc32"))
217 Features["crc32"] = true;
218
219 return true;
220}
221
222void X86TargetInfo::setFeatureEnabled(llvm::StringMap<bool> &Features,
223 StringRef Name, bool Enabled) const {
224 if (Name == "sse4") {
225 // We can get here via the __target__ attribute since that's not controlled
226 // via the -msse4/-mno-sse4 command line alias. Handle this the same way
227 // here - turn on the sse4.2 if enabled, turn off the sse4.1 level if
228 // disabled.
229 if (Enabled)
230 Name = "sse4.2";
231 else
232 Name = "sse4.1";
233 }
234
235 Features[Name] = Enabled;
236 llvm::X86::updateImpliedFeatures(Feature: Name, Enabled, Features);
237}
238
239/// handleTargetFeatures - Perform initialization based on the user
240/// configured set of features.
241bool X86TargetInfo::handleTargetFeatures(std::vector<std::string> &Features,
242 DiagnosticsEngine &Diags) {
243 for (const auto &Feature : Features) {
244 if (Feature[0] != '+')
245 continue;
246
247 if (Feature == "+mmx") {
248 HasMMX = true;
249 } else if (Feature == "+aes") {
250 HasAES = true;
251 } else if (Feature == "+vaes") {
252 HasVAES = true;
253 } else if (Feature == "+pclmul") {
254 HasPCLMUL = true;
255 } else if (Feature == "+vpclmulqdq") {
256 HasVPCLMULQDQ = true;
257 } else if (Feature == "+lzcnt") {
258 HasLZCNT = true;
259 } else if (Feature == "+rdrnd") {
260 HasRDRND = true;
261 } else if (Feature == "+fsgsbase") {
262 HasFSGSBASE = true;
263 } else if (Feature == "+bmi") {
264 HasBMI = true;
265 } else if (Feature == "+bmi2") {
266 HasBMI2 = true;
267 } else if (Feature == "+popcnt") {
268 HasPOPCNT = true;
269 } else if (Feature == "+rtm") {
270 HasRTM = true;
271 } else if (Feature == "+prfchw") {
272 HasPRFCHW = true;
273 } else if (Feature == "+rdseed") {
274 HasRDSEED = true;
275 } else if (Feature == "+adx") {
276 HasADX = true;
277 } else if (Feature == "+tbm") {
278 HasTBM = true;
279 } else if (Feature == "+lwp") {
280 HasLWP = true;
281 } else if (Feature == "+fma") {
282 HasFMA = true;
283 } else if (Feature == "+f16c") {
284 HasF16C = true;
285 } else if (Feature == "+gfni") {
286 HasGFNI = true;
287 } else if (Feature == "+avx10.1") {
288 HasAVX10_1 = true;
289 } else if (Feature == "+avx10.2") {
290 HasAVX10_2 = true;
291 HasFullBFloat16 = true;
292 } else if (Feature == "+avx512cd") {
293 HasAVX512CD = true;
294 } else if (Feature == "+avx512vpopcntdq") {
295 HasAVX512VPOPCNTDQ = true;
296 } else if (Feature == "+avx512vnni") {
297 HasAVX512VNNI = true;
298 } else if (Feature == "+avx512bf16") {
299 HasAVX512BF16 = true;
300 } else if (Feature == "+avx512fp16") {
301 HasAVX512FP16 = true;
302 HasFastHalfType = true;
303 } else if (Feature == "+avx512dq") {
304 HasAVX512DQ = true;
305 } else if (Feature == "+avx512bitalg") {
306 HasAVX512BITALG = true;
307 } else if (Feature == "+avx512bmm") {
308 HasAVX512BMM = true;
309 } else if (Feature == "+avx512bw") {
310 HasAVX512BW = true;
311 } else if (Feature == "+avx512vl") {
312 HasAVX512VL = true;
313 } else if (Feature == "+avx512vbmi") {
314 HasAVX512VBMI = true;
315 } else if (Feature == "+avx512vbmi2") {
316 HasAVX512VBMI2 = true;
317 } else if (Feature == "+avx512ifma") {
318 HasAVX512IFMA = true;
319 } else if (Feature == "+avx512vp2intersect") {
320 HasAVX512VP2INTERSECT = true;
321 } else if (Feature == "+sha") {
322 HasSHA = true;
323 } else if (Feature == "+sha512") {
324 HasSHA512 = true;
325 } else if (Feature == "+shstk") {
326 HasSHSTK = true;
327 } else if (Feature == "+sm3") {
328 HasSM3 = true;
329 } else if (Feature == "+sm4") {
330 HasSM4 = true;
331 } else if (Feature == "+movbe") {
332 HasMOVBE = true;
333 } else if (Feature == "+movrs") {
334 HasMOVRS = true;
335 } else if (Feature == "+sgx") {
336 HasSGX = true;
337 } else if (Feature == "+cx8") {
338 HasCX8 = true;
339 } else if (Feature == "+cx16") {
340 HasCX16 = true;
341 } else if (Feature == "+fxsr") {
342 HasFXSR = true;
343 } else if (Feature == "+xsave") {
344 HasXSAVE = true;
345 } else if (Feature == "+xsaveopt") {
346 HasXSAVEOPT = true;
347 } else if (Feature == "+xsavec") {
348 HasXSAVEC = true;
349 } else if (Feature == "+xsaves") {
350 HasXSAVES = true;
351 } else if (Feature == "+mwaitx") {
352 HasMWAITX = true;
353 } else if (Feature == "+pku") {
354 HasPKU = true;
355 } else if (Feature == "+clflushopt") {
356 HasCLFLUSHOPT = true;
357 } else if (Feature == "+clwb") {
358 HasCLWB = true;
359 } else if (Feature == "+wbnoinvd") {
360 HasWBNOINVD = true;
361 } else if (Feature == "+prefetchi") {
362 HasPREFETCHI = true;
363 } else if (Feature == "+clzero") {
364 HasCLZERO = true;
365 } else if (Feature == "+cldemote") {
366 HasCLDEMOTE = true;
367 } else if (Feature == "+rdpid") {
368 HasRDPID = true;
369 } else if (Feature == "+rdpru") {
370 HasRDPRU = true;
371 } else if (Feature == "+kl") {
372 HasKL = true;
373 } else if (Feature == "+widekl") {
374 HasWIDEKL = true;
375 } else if (Feature == "+retpoline-external-thunk") {
376 HasRetpolineExternalThunk = true;
377 } else if (Feature == "+sahf") {
378 HasLAHFSAHF = true;
379 } else if (Feature == "+waitpkg") {
380 HasWAITPKG = true;
381 } else if (Feature == "+movdiri") {
382 HasMOVDIRI = true;
383 } else if (Feature == "+movdir64b") {
384 HasMOVDIR64B = true;
385 } else if (Feature == "+pconfig") {
386 HasPCONFIG = true;
387 } else if (Feature == "+ptwrite") {
388 HasPTWRITE = true;
389 } else if (Feature == "+invpcid") {
390 HasINVPCID = true;
391 } else if (Feature == "+enqcmd") {
392 HasENQCMD = true;
393 } else if (Feature == "+hreset") {
394 HasHRESET = true;
395 } else if (Feature == "+amx-bf16") {
396 HasAMXBF16 = true;
397 } else if (Feature == "+amx-fp16") {
398 HasAMXFP16 = true;
399 } else if (Feature == "+amx-int8") {
400 HasAMXINT8 = true;
401 } else if (Feature == "+amx-tile") {
402 HasAMXTILE = true;
403 } else if (Feature == "+amx-complex") {
404 HasAMXCOMPLEX = true;
405 } else if (Feature == "+amx-fp8") {
406 HasAMXFP8 = true;
407 } else if (Feature == "+amx-movrs") {
408 HasAMXMOVRS = true;
409 } else if (Feature == "+amx-avx512") {
410 HasAMXAVX512 = true;
411 } else if (Feature == "+cmpccxadd") {
412 HasCMPCCXADD = true;
413 } else if (Feature == "+raoint") {
414 HasRAOINT = true;
415 } else if (Feature == "+avxifma") {
416 HasAVXIFMA = true;
417 } else if (Feature == "+avxneconvert") {
418 HasAVXNECONVERT= true;
419 } else if (Feature == "+avxvnni") {
420 HasAVXVNNI = true;
421 } else if (Feature == "+avxvnniint16") {
422 HasAVXVNNIINT16 = true;
423 } else if (Feature == "+avxvnniint8") {
424 HasAVXVNNIINT8 = true;
425 } else if (Feature == "+serialize") {
426 HasSERIALIZE = true;
427 } else if (Feature == "+tsxldtrk") {
428 HasTSXLDTRK = true;
429 } else if (Feature == "+uintr") {
430 HasUINTR = true;
431 } else if (Feature == "+usermsr") {
432 HasUSERMSR = true;
433 } else if (Feature == "+crc32") {
434 HasCRC32 = true;
435 } else if (Feature == "+x87") {
436 HasX87 = true;
437 } else if (Feature == "+fullbf16") {
438 HasFullBFloat16 = true;
439 } else if (Feature == "+egpr") {
440 HasEGPR = true;
441 } else if (Feature == "+inline-asm-use-gpr32") {
442 HasInlineAsmUseGPR32 = true;
443 } else if (Feature == "+push2pop2") {
444 HasPush2Pop2 = true;
445 } else if (Feature == "+ppx") {
446 HasPPX = true;
447 } else if (Feature == "+ndd") {
448 HasNDD = true;
449 } else if (Feature == "+ccmp") {
450 HasCCMP = true;
451 } else if (Feature == "+nf") {
452 HasNF = true;
453 } else if (Feature == "+cf") {
454 HasCF = true;
455 } else if (Feature == "+zu") {
456 HasZU = true;
457 } else if (Feature == "+jmpabs") {
458 HasJMPABS = true;
459 } else if (Feature == "+branch-hint") {
460 HasBranchHint = true;
461 }
462
463 X86SSEEnum Level = llvm::StringSwitch<X86SSEEnum>(Feature)
464 .Case(S: "+avx512f", Value: AVX512F)
465 .Case(S: "+avx2", Value: AVX2)
466 .Case(S: "+avx", Value: AVX)
467 .Case(S: "+sse4.2", Value: SSE42)
468 .Case(S: "+sse4.1", Value: SSE41)
469 .Case(S: "+ssse3", Value: SSSE3)
470 .Case(S: "+sse3", Value: SSE3)
471 .Case(S: "+sse2", Value: SSE2)
472 .Case(S: "+sse", Value: SSE1)
473 .Default(Value: NoSSE);
474 SSELevel = std::max(a: SSELevel, b: Level);
475
476 HasFloat16 = SSELevel >= SSE2;
477
478 // X86 target has bfloat16 emulation support in the backend, where
479 // bfloat16 is treated as a 32-bit float, arithmetic operations are
480 // performed in 32-bit, and the result is converted back to bfloat16.
481 // Truncation and extension between bfloat16 and 32-bit float are supported
482 // by the compiler-rt library. However, native bfloat16 support is currently
483 // not available in the X86 target. Hence, HasFullBFloat16 will be false
484 // until native bfloat16 support is available. HasFullBFloat16 is used to
485 // determine whether to automatically use excess floating point precision
486 // for bfloat16 arithmetic operations in the front-end.
487 HasBFloat16 = SSELevel >= SSE2;
488
489 XOPEnum XLevel = llvm::StringSwitch<XOPEnum>(Feature)
490 .Case(S: "+xop", Value: XOP)
491 .Case(S: "+fma4", Value: FMA4)
492 .Case(S: "+sse4a", Value: SSE4A)
493 .Default(Value: NoXOP);
494 XOPLevel = std::max(a: XOPLevel, b: XLevel);
495 }
496
497 // LLVM doesn't have a separate switch for fpmath, so only accept it if it
498 // matches the selected sse level.
499 if ((FPMath == FP_SSE && SSELevel < SSE1) ||
500 (FPMath == FP_387 && SSELevel >= SSE1)) {
501 Diags.Report(DiagID: diag::err_target_unsupported_fpmath)
502 << (FPMath == FP_SSE ? "sse" : "387");
503 return false;
504 }
505
506 // FIXME: We should allow long double type on 32-bits to match with GCC.
507 // This requires backend to be able to lower f80 without x87 first.
508 if (!HasX87 && LongDoubleFormat == &llvm::APFloat::x87DoubleExtended())
509 HasLongDouble = false;
510
511 return true;
512}
513
514/// X86TargetInfo::getTargetDefines - Return the set of the X86-specific macro
515/// definitions for this particular subtarget.
516void X86TargetInfo::getTargetDefines(const LangOptions &Opts,
517 MacroBuilder &Builder) const {
518 // Inline assembly supports X86 flag outputs.
519 Builder.defineMacro(Name: "__GCC_ASM_FLAG_OUTPUTS__");
520
521 std::string CodeModel = getTargetOpts().CodeModel;
522 if (CodeModel == "default")
523 CodeModel = "small";
524 Builder.defineMacro(Name: "__code_model_" + CodeModel + "__");
525
526 // Target identification.
527 if (getTriple().getArch() == llvm::Triple::x86_64) {
528 Builder.defineMacro(Name: "__amd64__");
529 Builder.defineMacro(Name: "__amd64");
530 Builder.defineMacro(Name: "__x86_64");
531 Builder.defineMacro(Name: "__x86_64__");
532 if (getTriple().getArchName() == "x86_64h") {
533 Builder.defineMacro(Name: "__x86_64h");
534 Builder.defineMacro(Name: "__x86_64h__");
535 }
536 } else {
537 DefineStd(Builder, MacroName: "i386", Opts);
538 }
539
540 if (getTriple().isLFI()) {
541 Builder.defineMacro(Name: "__LFI__");
542 }
543
544 Builder.defineMacro(Name: "__SEG_GS");
545 Builder.defineMacro(Name: "__SEG_FS");
546 Builder.defineMacro(Name: "__seg_gs", Value: "__attribute__((address_space(256)))");
547 Builder.defineMacro(Name: "__seg_fs", Value: "__attribute__((address_space(257)))");
548
549 // Subtarget options.
550 // FIXME: We are hard-coding the tune parameters based on the CPU, but they
551 // truly should be based on -mtune options.
552 using namespace llvm::X86;
553 switch (CPU) {
554 case CK_None:
555 break;
556 case CK_i386:
557 // The rest are coming from the i386 define above.
558 Builder.defineMacro(Name: "__tune_i386__");
559 break;
560 case CK_i486:
561 case CK_WinChipC6:
562 case CK_WinChip2:
563 case CK_C3:
564 defineCPUMacros(Builder, CPUName: "i486");
565 break;
566 case CK_PentiumMMX:
567 Builder.defineMacro(Name: "__pentium_mmx__");
568 Builder.defineMacro(Name: "__tune_pentium_mmx__");
569 [[fallthrough]];
570 case CK_i586:
571 case CK_Pentium:
572 defineCPUMacros(Builder, CPUName: "i586");
573 defineCPUMacros(Builder, CPUName: "pentium");
574 break;
575 case CK_Pentium3:
576 case CK_PentiumM:
577 Builder.defineMacro(Name: "__tune_pentium3__");
578 [[fallthrough]];
579 case CK_Pentium2:
580 case CK_C3_2:
581 Builder.defineMacro(Name: "__tune_pentium2__");
582 [[fallthrough]];
583 case CK_PentiumPro:
584 case CK_i686:
585 defineCPUMacros(Builder, CPUName: "i686");
586 defineCPUMacros(Builder, CPUName: "pentiumpro");
587 break;
588 case CK_Pentium4:
589 defineCPUMacros(Builder, CPUName: "pentium4");
590 break;
591 case CK_Yonah:
592 case CK_Prescott:
593 case CK_Nocona:
594 defineCPUMacros(Builder, CPUName: "nocona");
595 break;
596 case CK_Core2:
597 case CK_Penryn:
598 defineCPUMacros(Builder, CPUName: "core2");
599 break;
600 case CK_Bonnell:
601 defineCPUMacros(Builder, CPUName: "atom");
602 break;
603 case CK_Silvermont:
604 defineCPUMacros(Builder, CPUName: "slm");
605 break;
606 case CK_Goldmont:
607 defineCPUMacros(Builder, CPUName: "goldmont");
608 break;
609 case CK_GoldmontPlus:
610 defineCPUMacros(Builder, CPUName: "goldmont_plus");
611 break;
612 case CK_Tremont:
613 defineCPUMacros(Builder, CPUName: "tremont");
614 break;
615 // Gracemont and later atom-cores use P-core cpu macros.
616 case CK_Gracemont:
617 case CK_Nehalem:
618 case CK_Westmere:
619 case CK_SandyBridge:
620 case CK_IvyBridge:
621 case CK_Haswell:
622 case CK_Broadwell:
623 case CK_SkylakeClient:
624 case CK_SkylakeServer:
625 case CK_Cascadelake:
626 case CK_Cooperlake:
627 case CK_Cannonlake:
628 case CK_IcelakeClient:
629 case CK_Rocketlake:
630 case CK_IcelakeServer:
631 case CK_Tigerlake:
632 case CK_SapphireRapids:
633 case CK_Alderlake:
634 case CK_Raptorlake:
635 case CK_Meteorlake:
636 case CK_Arrowlake:
637 case CK_ArrowlakeS:
638 case CK_Lunarlake:
639 case CK_Pantherlake:
640 case CK_Wildcatlake:
641 case CK_Novalake:
642 case CK_Sierraforest:
643 case CK_Grandridge:
644 case CK_Graniterapids:
645 case CK_GraniterapidsD:
646 case CK_Emeraldrapids:
647 case CK_Clearwaterforest:
648 case CK_Diamondrapids:
649 // FIXME: Historically, we defined this legacy name, it would be nice to
650 // remove it at some point. We've never exposed fine-grained names for
651 // recent primary x86 CPUs, and we should keep it that way.
652 defineCPUMacros(Builder, CPUName: "corei7");
653 break;
654 case CK_KNL:
655 defineCPUMacros(Builder, CPUName: "knl");
656 break;
657 case CK_KNM:
658 break;
659 case CK_Lakemont:
660 defineCPUMacros(Builder, CPUName: "i586", /*Tuning*/false);
661 defineCPUMacros(Builder, CPUName: "pentium", /*Tuning*/false);
662 Builder.defineMacro(Name: "__tune_lakemont__");
663 break;
664 case CK_K6_2:
665 Builder.defineMacro(Name: "__k6_2__");
666 Builder.defineMacro(Name: "__tune_k6_2__");
667 [[fallthrough]];
668 case CK_K6_3:
669 if (CPU != CK_K6_2) { // In case of fallthrough
670 // FIXME: GCC may be enabling these in cases where some other k6
671 // architecture is specified but -m3dnow is explicitly provided. The
672 // exact semantics need to be determined and emulated here.
673 Builder.defineMacro(Name: "__k6_3__");
674 Builder.defineMacro(Name: "__tune_k6_3__");
675 }
676 [[fallthrough]];
677 case CK_K6:
678 defineCPUMacros(Builder, CPUName: "k6");
679 break;
680 case CK_Athlon:
681 case CK_AthlonXP:
682 defineCPUMacros(Builder, CPUName: "athlon");
683 if (SSELevel != NoSSE) {
684 Builder.defineMacro(Name: "__athlon_sse__");
685 Builder.defineMacro(Name: "__tune_athlon_sse__");
686 }
687 break;
688 case CK_K8:
689 case CK_K8SSE3:
690 case CK_x86_64:
691 defineCPUMacros(Builder, CPUName: "k8");
692 break;
693 case CK_x86_64_v2:
694 case CK_x86_64_v3:
695 case CK_x86_64_v4:
696 break;
697 case CK_AMDFAM10:
698 defineCPUMacros(Builder, CPUName: "amdfam10");
699 break;
700 case CK_BTVER1:
701 defineCPUMacros(Builder, CPUName: "btver1");
702 break;
703 case CK_BTVER2:
704 defineCPUMacros(Builder, CPUName: "btver2");
705 break;
706 case CK_BDVER1:
707 defineCPUMacros(Builder, CPUName: "bdver1");
708 break;
709 case CK_BDVER2:
710 defineCPUMacros(Builder, CPUName: "bdver2");
711 break;
712 case CK_BDVER3:
713 defineCPUMacros(Builder, CPUName: "bdver3");
714 break;
715 case CK_BDVER4:
716 defineCPUMacros(Builder, CPUName: "bdver4");
717 break;
718 case CK_ZNVER1:
719 defineCPUMacros(Builder, CPUName: "znver1");
720 break;
721 case CK_ZNVER2:
722 defineCPUMacros(Builder, CPUName: "znver2");
723 break;
724 case CK_ZNVER3:
725 defineCPUMacros(Builder, CPUName: "znver3");
726 break;
727 case CK_ZNVER4:
728 defineCPUMacros(Builder, CPUName: "znver4");
729 break;
730 case CK_ZNVER5:
731 defineCPUMacros(Builder, CPUName: "znver5");
732 break;
733 case CK_ZNVER6:
734 defineCPUMacros(Builder, CPUName: "znver6");
735 break;
736 case CK_Geode:
737 defineCPUMacros(Builder, CPUName: "geode");
738 break;
739 case CK_C86_4G_M4:
740 defineCPUMacros(Builder, CPUName: "c86_4g_m4");
741 break;
742 case CK_C86_4G_M6:
743 defineCPUMacros(Builder, CPUName: "c86_4g_m6");
744 break;
745 case CK_C86_4G_M7:
746 defineCPUMacros(Builder, CPUName: "c86_4g_m7");
747 break;
748 case CK_C86_4G_M8:
749 defineCPUMacros(Builder, CPUName: "c86_4g_m8");
750 break;
751 }
752
753 // Target properties.
754 Builder.defineMacro(Name: "__REGISTER_PREFIX__", Value: "");
755
756 // Define __NO_MATH_INLINES on linux/x86 so that we don't get inline
757 // functions in glibc header files that use FP Stack inline asm which the
758 // backend can't deal with (PR879).
759 Builder.defineMacro(Name: "__NO_MATH_INLINES");
760
761 if (HasAES)
762 Builder.defineMacro(Name: "__AES__");
763
764 if (HasVAES)
765 Builder.defineMacro(Name: "__VAES__");
766
767 if (HasPCLMUL)
768 Builder.defineMacro(Name: "__PCLMUL__");
769
770 if (HasVPCLMULQDQ)
771 Builder.defineMacro(Name: "__VPCLMULQDQ__");
772
773 // Note, in 32-bit mode, GCC does not define the macro if -mno-sahf. In LLVM,
774 // the feature flag only applies to 64-bit mode.
775 if (HasLAHFSAHF || getTriple().getArch() == llvm::Triple::x86)
776 Builder.defineMacro(Name: "__LAHF_SAHF__");
777
778 if (HasLZCNT)
779 Builder.defineMacro(Name: "__LZCNT__");
780
781 if (HasRDRND)
782 Builder.defineMacro(Name: "__RDRND__");
783
784 if (HasFSGSBASE)
785 Builder.defineMacro(Name: "__FSGSBASE__");
786
787 if (HasBMI)
788 Builder.defineMacro(Name: "__BMI__");
789
790 if (HasBMI2)
791 Builder.defineMacro(Name: "__BMI2__");
792
793 if (HasPOPCNT)
794 Builder.defineMacro(Name: "__POPCNT__");
795
796 if (HasRTM)
797 Builder.defineMacro(Name: "__RTM__");
798
799 if (HasPRFCHW)
800 Builder.defineMacro(Name: "__PRFCHW__");
801
802 if (HasRDSEED)
803 Builder.defineMacro(Name: "__RDSEED__");
804
805 if (HasADX)
806 Builder.defineMacro(Name: "__ADX__");
807
808 if (HasTBM)
809 Builder.defineMacro(Name: "__TBM__");
810
811 if (HasLWP)
812 Builder.defineMacro(Name: "__LWP__");
813
814 if (HasMWAITX)
815 Builder.defineMacro(Name: "__MWAITX__");
816
817 if (HasMOVBE)
818 Builder.defineMacro(Name: "__MOVBE__");
819
820 switch (XOPLevel) {
821 case XOP:
822 Builder.defineMacro(Name: "__XOP__");
823 [[fallthrough]];
824 case FMA4:
825 Builder.defineMacro(Name: "__FMA4__");
826 [[fallthrough]];
827 case SSE4A:
828 Builder.defineMacro(Name: "__SSE4A__");
829 [[fallthrough]];
830 case NoXOP:
831 break;
832 }
833
834 if (HasFMA)
835 Builder.defineMacro(Name: "__FMA__");
836
837 if (HasF16C)
838 Builder.defineMacro(Name: "__F16C__");
839
840 if (HasGFNI)
841 Builder.defineMacro(Name: "__GFNI__");
842
843 if (HasAVX10_1) {
844 Builder.defineMacro(Name: "__AVX10_1__");
845 Builder.defineMacro(Name: "__AVX10_1_512__");
846 }
847 if (HasAVX10_2) {
848 Builder.defineMacro(Name: "__AVX10_2__");
849 Builder.defineMacro(Name: "__AVX10_2_512__");
850 }
851 if (HasAVX512CD)
852 Builder.defineMacro(Name: "__AVX512CD__");
853 if (HasAVX512VPOPCNTDQ)
854 Builder.defineMacro(Name: "__AVX512VPOPCNTDQ__");
855 if (HasAVX512VNNI)
856 Builder.defineMacro(Name: "__AVX512VNNI__");
857 if (HasAVX512BF16)
858 Builder.defineMacro(Name: "__AVX512BF16__");
859 if (HasAVX512FP16)
860 Builder.defineMacro(Name: "__AVX512FP16__");
861 if (HasAVX512DQ)
862 Builder.defineMacro(Name: "__AVX512DQ__");
863 if (HasAVX512BITALG)
864 Builder.defineMacro(Name: "__AVX512BITALG__");
865 if (HasAVX512BMM)
866 Builder.defineMacro(Name: "__AVX512BMM__");
867 if (HasAVX512BW)
868 Builder.defineMacro(Name: "__AVX512BW__");
869 if (HasAVX512VL) {
870 Builder.defineMacro(Name: "__AVX512VL__");
871 }
872 if (HasAVX512VBMI)
873 Builder.defineMacro(Name: "__AVX512VBMI__");
874 if (HasAVX512VBMI2)
875 Builder.defineMacro(Name: "__AVX512VBMI2__");
876 if (HasAVX512IFMA)
877 Builder.defineMacro(Name: "__AVX512IFMA__");
878 if (HasAVX512VP2INTERSECT)
879 Builder.defineMacro(Name: "__AVX512VP2INTERSECT__");
880 if (HasSHA)
881 Builder.defineMacro(Name: "__SHA__");
882 if (HasSHA512)
883 Builder.defineMacro(Name: "__SHA512__");
884
885 if (HasFXSR)
886 Builder.defineMacro(Name: "__FXSR__");
887 if (HasXSAVE)
888 Builder.defineMacro(Name: "__XSAVE__");
889 if (HasXSAVEOPT)
890 Builder.defineMacro(Name: "__XSAVEOPT__");
891 if (HasXSAVEC)
892 Builder.defineMacro(Name: "__XSAVEC__");
893 if (HasXSAVES)
894 Builder.defineMacro(Name: "__XSAVES__");
895 if (HasPKU)
896 Builder.defineMacro(Name: "__PKU__");
897 if (HasCLFLUSHOPT)
898 Builder.defineMacro(Name: "__CLFLUSHOPT__");
899 if (HasCLWB)
900 Builder.defineMacro(Name: "__CLWB__");
901 if (HasWBNOINVD)
902 Builder.defineMacro(Name: "__WBNOINVD__");
903 if (HasSHSTK)
904 Builder.defineMacro(Name: "__SHSTK__");
905 if (HasSGX)
906 Builder.defineMacro(Name: "__SGX__");
907 if (HasSM3)
908 Builder.defineMacro(Name: "__SM3__");
909 if (HasSM4)
910 Builder.defineMacro(Name: "__SM4__");
911 if (HasPREFETCHI)
912 Builder.defineMacro(Name: "__PREFETCHI__");
913 if (HasCLZERO)
914 Builder.defineMacro(Name: "__CLZERO__");
915 if (HasKL)
916 Builder.defineMacro(Name: "__KL__");
917 if (HasWIDEKL)
918 Builder.defineMacro(Name: "__WIDEKL__");
919 if (HasRDPID)
920 Builder.defineMacro(Name: "__RDPID__");
921 if (HasRDPRU)
922 Builder.defineMacro(Name: "__RDPRU__");
923 if (HasCLDEMOTE)
924 Builder.defineMacro(Name: "__CLDEMOTE__");
925 if (HasWAITPKG)
926 Builder.defineMacro(Name: "__WAITPKG__");
927 if (HasMOVDIRI)
928 Builder.defineMacro(Name: "__MOVDIRI__");
929 if (HasMOVDIR64B)
930 Builder.defineMacro(Name: "__MOVDIR64B__");
931 if (HasMOVRS)
932 Builder.defineMacro(Name: "__MOVRS__");
933 if (HasPCONFIG)
934 Builder.defineMacro(Name: "__PCONFIG__");
935 if (HasPTWRITE)
936 Builder.defineMacro(Name: "__PTWRITE__");
937 if (HasINVPCID)
938 Builder.defineMacro(Name: "__INVPCID__");
939 if (HasENQCMD)
940 Builder.defineMacro(Name: "__ENQCMD__");
941 if (HasHRESET)
942 Builder.defineMacro(Name: "__HRESET__");
943 if (HasAMXTILE)
944 Builder.defineMacro(Name: "__AMX_TILE__");
945 if (HasAMXINT8)
946 Builder.defineMacro(Name: "__AMX_INT8__");
947 if (HasAMXBF16)
948 Builder.defineMacro(Name: "__AMX_BF16__");
949 if (HasAMXFP16)
950 Builder.defineMacro(Name: "__AMX_FP16__");
951 if (HasAMXCOMPLEX)
952 Builder.defineMacro(Name: "__AMX_COMPLEX__");
953 if (HasAMXFP8)
954 Builder.defineMacro(Name: "__AMX_FP8__");
955 if (HasAMXMOVRS)
956 Builder.defineMacro(Name: "__AMX_MOVRS__");
957 if (HasAMXAVX512)
958 Builder.defineMacro(Name: "__AMX_AVX512__");
959 if (HasCMPCCXADD)
960 Builder.defineMacro(Name: "__CMPCCXADD__");
961 if (HasRAOINT)
962 Builder.defineMacro(Name: "__RAOINT__");
963 if (HasAVXIFMA)
964 Builder.defineMacro(Name: "__AVXIFMA__");
965 if (HasAVXNECONVERT)
966 Builder.defineMacro(Name: "__AVXNECONVERT__");
967 if (HasAVXVNNI)
968 Builder.defineMacro(Name: "__AVXVNNI__");
969 if (HasAVXVNNIINT16)
970 Builder.defineMacro(Name: "__AVXVNNIINT16__");
971 if (HasAVXVNNIINT8)
972 Builder.defineMacro(Name: "__AVXVNNIINT8__");
973 if (HasSERIALIZE)
974 Builder.defineMacro(Name: "__SERIALIZE__");
975 if (HasTSXLDTRK)
976 Builder.defineMacro(Name: "__TSXLDTRK__");
977 if (HasUINTR)
978 Builder.defineMacro(Name: "__UINTR__");
979 if (HasUSERMSR)
980 Builder.defineMacro(Name: "__USERMSR__");
981 if (HasCRC32)
982 Builder.defineMacro(Name: "__CRC32__");
983 if (HasEGPR)
984 Builder.defineMacro(Name: "__EGPR__");
985 if (HasPush2Pop2)
986 Builder.defineMacro(Name: "__PUSH2POP2__");
987 if (HasPPX)
988 Builder.defineMacro(Name: "__PPX__");
989 if (HasNDD)
990 Builder.defineMacro(Name: "__NDD__");
991 if (HasCCMP)
992 Builder.defineMacro(Name: "__CCMP__");
993 if (HasNF)
994 Builder.defineMacro(Name: "__NF__");
995 if (HasCF)
996 Builder.defineMacro(Name: "__CF__");
997 if (HasZU)
998 Builder.defineMacro(Name: "__ZU__");
999 if (HasJMPABS)
1000 Builder.defineMacro(Name: "__JMPABS__");
1001 if (HasEGPR && HasPush2Pop2 && HasPPX && HasNDD && HasCCMP && HasNF &&
1002 HasZU && HasJMPABS)
1003 Builder.defineMacro(Name: "__APX_F__");
1004 if (HasEGPR && HasInlineAsmUseGPR32)
1005 Builder.defineMacro(Name: "__APX_INLINE_ASM_USE_GPR32__");
1006
1007 // Each case falls through to the previous one here.
1008 switch (SSELevel) {
1009 case AVX512F:
1010 Builder.defineMacro(Name: "__AVX512F__");
1011 [[fallthrough]];
1012 case AVX2:
1013 Builder.defineMacro(Name: "__AVX2__");
1014 [[fallthrough]];
1015 case AVX:
1016 Builder.defineMacro(Name: "__AVX__");
1017 [[fallthrough]];
1018 case SSE42:
1019 Builder.defineMacro(Name: "__SSE4_2__");
1020 [[fallthrough]];
1021 case SSE41:
1022 Builder.defineMacro(Name: "__SSE4_1__");
1023 [[fallthrough]];
1024 case SSSE3:
1025 Builder.defineMacro(Name: "__SSSE3__");
1026 [[fallthrough]];
1027 case SSE3:
1028 Builder.defineMacro(Name: "__SSE3__");
1029 [[fallthrough]];
1030 case SSE2:
1031 Builder.defineMacro(Name: "__SSE2__");
1032 Builder.defineMacro(Name: "__SSE2_MATH__"); // -mfp-math=sse always implied.
1033 [[fallthrough]];
1034 case SSE1:
1035 Builder.defineMacro(Name: "__SSE__");
1036 Builder.defineMacro(Name: "__SSE_MATH__"); // -mfp-math=sse always implied.
1037 [[fallthrough]];
1038 case NoSSE:
1039 break;
1040 }
1041
1042 if (Opts.MicrosoftExt && getTriple().getArch() == llvm::Triple::x86) {
1043 switch (SSELevel) {
1044 case AVX512F:
1045 case AVX2:
1046 case AVX:
1047 case SSE42:
1048 case SSE41:
1049 case SSSE3:
1050 case SSE3:
1051 case SSE2:
1052 Builder.defineMacro(Name: "_M_IX86_FP", Value: Twine(2));
1053 break;
1054 case SSE1:
1055 Builder.defineMacro(Name: "_M_IX86_FP", Value: Twine(1));
1056 break;
1057 default:
1058 Builder.defineMacro(Name: "_M_IX86_FP", Value: Twine(0));
1059 break;
1060 }
1061 }
1062
1063 // Each case falls through to the previous one here.
1064 if (HasMMX) {
1065 Builder.defineMacro(Name: "__MMX__");
1066 }
1067
1068 if (CPU >= CK_i486 || CPU == CK_None) {
1069 Builder.defineMacro(Name: "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
1070 Builder.defineMacro(Name: "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
1071 Builder.defineMacro(Name: "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
1072 }
1073 if (HasCX8)
1074 Builder.defineMacro(Name: "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
1075 if (HasCX16 && getTriple().getArch() == llvm::Triple::x86_64)
1076 Builder.defineMacro(Name: "__GCC_HAVE_SYNC_COMPARE_AND_SWAP_16");
1077
1078 if (HasFloat128)
1079 Builder.defineMacro(Name: "__SIZEOF_FLOAT128__", Value: "16");
1080
1081 if (Opts.CFProtectionReturn || Opts.CFProtectionBranch)
1082 Builder.defineMacro(Name: "__CET__", Value: Twine{(Opts.CFProtectionReturn << 1) |
1083 Opts.CFProtectionBranch});
1084}
1085
1086bool X86TargetInfo::isValidFeatureName(StringRef Name) const {
1087 return llvm::StringSwitch<bool>(Name)
1088 .Case(S: "adx", Value: true)
1089 .Case(S: "aes", Value: true)
1090 .Case(S: "amx-avx512", Value: true)
1091 .Case(S: "amx-bf16", Value: true)
1092 .Case(S: "amx-complex", Value: true)
1093 .Case(S: "amx-fp16", Value: true)
1094 .Case(S: "amx-fp8", Value: true)
1095 .Case(S: "amx-int8", Value: true)
1096 .Case(S: "amx-movrs", Value: true)
1097 .Case(S: "amx-tile", Value: true)
1098 .Case(S: "avx", Value: true)
1099 .Case(S: "avx10.1", Value: true)
1100 .Case(S: "avx10.2", Value: true)
1101 .Case(S: "avx2", Value: true)
1102 .Case(S: "avx512f", Value: true)
1103 .Case(S: "avx512cd", Value: true)
1104 .Case(S: "avx512vpopcntdq", Value: true)
1105 .Case(S: "avx512vnni", Value: true)
1106 .Case(S: "avx512bf16", Value: true)
1107 .Case(S: "avx512fp16", Value: true)
1108 .Case(S: "avx512dq", Value: true)
1109 .Case(S: "avx512bitalg", Value: true)
1110 .Case(S: "avx512bmm", Value: true)
1111 .Case(S: "avx512bw", Value: true)
1112 .Case(S: "avx512vl", Value: true)
1113 .Case(S: "avx512vbmi", Value: true)
1114 .Case(S: "avx512vbmi2", Value: true)
1115 .Case(S: "avx512ifma", Value: true)
1116 .Case(S: "avx512vp2intersect", Value: true)
1117 .Case(S: "avxifma", Value: true)
1118 .Case(S: "avxneconvert", Value: true)
1119 .Case(S: "avxvnni", Value: true)
1120 .Case(S: "avxvnniint16", Value: true)
1121 .Case(S: "avxvnniint8", Value: true)
1122 .Case(S: "bmi", Value: true)
1123 .Case(S: "bmi2", Value: true)
1124 .Case(S: "cldemote", Value: true)
1125 .Case(S: "clflushopt", Value: true)
1126 .Case(S: "clwb", Value: true)
1127 .Case(S: "clzero", Value: true)
1128 .Case(S: "cmpccxadd", Value: true)
1129 .Case(S: "crc32", Value: true)
1130 .Case(S: "cx16", Value: true)
1131 .Case(S: "enqcmd", Value: true)
1132 .Case(S: "f16c", Value: true)
1133 .Case(S: "fma", Value: true)
1134 .Case(S: "fma4", Value: true)
1135 .Case(S: "fsgsbase", Value: true)
1136 .Case(S: "fxsr", Value: true)
1137 .Case(S: "general-regs-only", Value: true)
1138 .Case(S: "gfni", Value: true)
1139 .Case(S: "hreset", Value: true)
1140 .Case(S: "invpcid", Value: true)
1141 .Case(S: "kl", Value: true)
1142 .Case(S: "widekl", Value: true)
1143 .Case(S: "lwp", Value: true)
1144 .Case(S: "lzcnt", Value: true)
1145 .Case(S: "mmx", Value: true)
1146 .Case(S: "movbe", Value: true)
1147 .Case(S: "movrs", Value: true)
1148 .Case(S: "movdiri", Value: true)
1149 .Case(S: "movdir64b", Value: true)
1150 .Case(S: "mwaitx", Value: true)
1151 .Case(S: "pclmul", Value: true)
1152 .Case(S: "pconfig", Value: true)
1153 .Case(S: "pku", Value: true)
1154 .Case(S: "popcnt", Value: true)
1155 .Case(S: "prefer-256-bit", Value: true)
1156 .Case(S: "prefetchi", Value: true)
1157 .Case(S: "prfchw", Value: true)
1158 .Case(S: "ptwrite", Value: true)
1159 .Case(S: "raoint", Value: true)
1160 .Case(S: "rdpid", Value: true)
1161 .Case(S: "rdpru", Value: true)
1162 .Case(S: "rdrnd", Value: true)
1163 .Case(S: "rdseed", Value: true)
1164 .Case(S: "rtm", Value: true)
1165 .Case(S: "sahf", Value: true)
1166 .Case(S: "serialize", Value: true)
1167 .Case(S: "sgx", Value: true)
1168 .Case(S: "sha", Value: true)
1169 .Case(S: "sha512", Value: true)
1170 .Case(S: "shstk", Value: true)
1171 .Case(S: "sm3", Value: true)
1172 .Case(S: "sm4", Value: true)
1173 .Case(S: "sse", Value: true)
1174 .Case(S: "sse2", Value: true)
1175 .Case(S: "sse3", Value: true)
1176 .Case(S: "ssse3", Value: true)
1177 .Case(S: "sse4", Value: true)
1178 .Case(S: "sse4.1", Value: true)
1179 .Case(S: "sse4.2", Value: true)
1180 .Case(S: "sse4a", Value: true)
1181 .Case(S: "tbm", Value: true)
1182 .Case(S: "tsxldtrk", Value: true)
1183 .Case(S: "uintr", Value: true)
1184 .Case(S: "usermsr", Value: true)
1185 .Case(S: "vaes", Value: true)
1186 .Case(S: "vpclmulqdq", Value: true)
1187 .Case(S: "wbnoinvd", Value: true)
1188 .Case(S: "waitpkg", Value: true)
1189 .Case(S: "x87", Value: true)
1190 .Case(S: "xop", Value: true)
1191 .Case(S: "xsave", Value: true)
1192 .Case(S: "xsavec", Value: true)
1193 .Case(S: "xsaves", Value: true)
1194 .Case(S: "xsaveopt", Value: true)
1195 .Case(S: "apxf", Value: true)
1196 .Case(S: "egpr", Value: true)
1197 .Case(S: "push2pop2", Value: true)
1198 .Case(S: "ppx", Value: true)
1199 .Case(S: "ndd", Value: true)
1200 .Case(S: "ccmp", Value: true)
1201 .Case(S: "nf", Value: true)
1202 .Case(S: "cf", Value: true)
1203 .Case(S: "zu", Value: true)
1204 .Case(S: "jmpabs", Value: true)
1205 .Default(Value: false);
1206}
1207
1208bool X86TargetInfo::hasFeature(StringRef Feature) const {
1209 return llvm::StringSwitch<bool>(Feature)
1210 .Case(S: "adx", Value: HasADX)
1211 .Case(S: "aes", Value: HasAES)
1212 .Case(S: "amx-avx512", Value: HasAMXAVX512)
1213 .Case(S: "amx-bf16", Value: HasAMXBF16)
1214 .Case(S: "amx-complex", Value: HasAMXCOMPLEX)
1215 .Case(S: "amx-fp16", Value: HasAMXFP16)
1216 .Case(S: "amx-fp8", Value: HasAMXFP8)
1217 .Case(S: "amx-int8", Value: HasAMXINT8)
1218 .Case(S: "amx-movrs", Value: HasAMXMOVRS)
1219 .Case(S: "amx-tile", Value: HasAMXTILE)
1220 .Case(S: "avx", Value: SSELevel >= AVX)
1221 .Case(S: "avx10.1", Value: HasAVX10_1)
1222 .Case(S: "avx10.2", Value: HasAVX10_2)
1223 .Case(S: "avx2", Value: SSELevel >= AVX2)
1224 .Case(S: "avx512f", Value: SSELevel >= AVX512F)
1225 .Case(S: "avx512cd", Value: HasAVX512CD)
1226 .Case(S: "avx512vpopcntdq", Value: HasAVX512VPOPCNTDQ)
1227 .Case(S: "avx512vnni", Value: HasAVX512VNNI)
1228 .Case(S: "avx512bf16", Value: HasAVX512BF16)
1229 .Case(S: "avx512fp16", Value: HasAVX512FP16)
1230 .Case(S: "avx512dq", Value: HasAVX512DQ)
1231 .Case(S: "avx512bitalg", Value: HasAVX512BITALG)
1232 .Case(S: "avx512bmm", Value: HasAVX512BMM)
1233 .Case(S: "avx512bw", Value: HasAVX512BW)
1234 .Case(S: "avx512vl", Value: HasAVX512VL)
1235 .Case(S: "avx512vbmi", Value: HasAVX512VBMI)
1236 .Case(S: "avx512vbmi2", Value: HasAVX512VBMI2)
1237 .Case(S: "avx512ifma", Value: HasAVX512IFMA)
1238 .Case(S: "avx512vp2intersect", Value: HasAVX512VP2INTERSECT)
1239 .Case(S: "avxifma", Value: HasAVXIFMA)
1240 .Case(S: "avxneconvert", Value: HasAVXNECONVERT)
1241 .Case(S: "avxvnni", Value: HasAVXVNNI)
1242 .Case(S: "avxvnniint16", Value: HasAVXVNNIINT16)
1243 .Case(S: "avxvnniint8", Value: HasAVXVNNIINT8)
1244 .Case(S: "bmi", Value: HasBMI)
1245 .Case(S: "bmi2", Value: HasBMI2)
1246 .Case(S: "cldemote", Value: HasCLDEMOTE)
1247 .Case(S: "clflushopt", Value: HasCLFLUSHOPT)
1248 .Case(S: "clwb", Value: HasCLWB)
1249 .Case(S: "clzero", Value: HasCLZERO)
1250 .Case(S: "cmpccxadd", Value: HasCMPCCXADD)
1251 .Case(S: "crc32", Value: HasCRC32)
1252 .Case(S: "cx8", Value: HasCX8)
1253 .Case(S: "cx16", Value: HasCX16)
1254 .Case(S: "enqcmd", Value: HasENQCMD)
1255 .Case(S: "f16c", Value: HasF16C)
1256 .Case(S: "fma", Value: HasFMA)
1257 .Case(S: "fma4", Value: XOPLevel >= FMA4)
1258 .Case(S: "fsgsbase", Value: HasFSGSBASE)
1259 .Case(S: "fxsr", Value: HasFXSR)
1260 .Case(S: "gfni", Value: HasGFNI)
1261 .Case(S: "hreset", Value: HasHRESET)
1262 .Case(S: "invpcid", Value: HasINVPCID)
1263 .Case(S: "kl", Value: HasKL)
1264 .Case(S: "widekl", Value: HasWIDEKL)
1265 .Case(S: "lwp", Value: HasLWP)
1266 .Case(S: "lzcnt", Value: HasLZCNT)
1267 .Case(S: "mmx", Value: HasMMX)
1268 .Case(S: "movbe", Value: HasMOVBE)
1269 .Case(S: "movrs", Value: HasMOVRS)
1270 .Case(S: "movdiri", Value: HasMOVDIRI)
1271 .Case(S: "movdir64b", Value: HasMOVDIR64B)
1272 .Case(S: "mwaitx", Value: HasMWAITX)
1273 .Case(S: "pclmul", Value: HasPCLMUL)
1274 .Case(S: "pconfig", Value: HasPCONFIG)
1275 .Case(S: "pku", Value: HasPKU)
1276 .Case(S: "popcnt", Value: HasPOPCNT)
1277 .Case(S: "prefetchi", Value: HasPREFETCHI)
1278 .Case(S: "prfchw", Value: HasPRFCHW)
1279 .Case(S: "ptwrite", Value: HasPTWRITE)
1280 .Case(S: "raoint", Value: HasRAOINT)
1281 .Case(S: "rdpid", Value: HasRDPID)
1282 .Case(S: "rdpru", Value: HasRDPRU)
1283 .Case(S: "rdrnd", Value: HasRDRND)
1284 .Case(S: "rdseed", Value: HasRDSEED)
1285 .Case(S: "retpoline-external-thunk", Value: HasRetpolineExternalThunk)
1286 .Case(S: "rtm", Value: HasRTM)
1287 .Case(S: "sahf", Value: HasLAHFSAHF)
1288 .Case(S: "serialize", Value: HasSERIALIZE)
1289 .Case(S: "sgx", Value: HasSGX)
1290 .Case(S: "sha", Value: HasSHA)
1291 .Case(S: "sha512", Value: HasSHA512)
1292 .Case(S: "shstk", Value: HasSHSTK)
1293 .Case(S: "sm3", Value: HasSM3)
1294 .Case(S: "sm4", Value: HasSM4)
1295 .Case(S: "sse", Value: SSELevel >= SSE1)
1296 .Case(S: "sse2", Value: SSELevel >= SSE2)
1297 .Case(S: "sse3", Value: SSELevel >= SSE3)
1298 .Case(S: "ssse3", Value: SSELevel >= SSSE3)
1299 .Case(S: "sse4.1", Value: SSELevel >= SSE41)
1300 .Case(S: "sse4.2", Value: SSELevel >= SSE42)
1301 .Case(S: "sse4a", Value: XOPLevel >= SSE4A)
1302 .Case(S: "tbm", Value: HasTBM)
1303 .Case(S: "tsxldtrk", Value: HasTSXLDTRK)
1304 .Case(S: "uintr", Value: HasUINTR)
1305 .Case(S: "usermsr", Value: HasUSERMSR)
1306 .Case(S: "vaes", Value: HasVAES)
1307 .Case(S: "vpclmulqdq", Value: HasVPCLMULQDQ)
1308 .Case(S: "wbnoinvd", Value: HasWBNOINVD)
1309 .Case(S: "waitpkg", Value: HasWAITPKG)
1310 .Case(S: "x86", Value: true)
1311 .Case(S: "x86_32", Value: getTriple().getArch() == llvm::Triple::x86)
1312 .Case(S: "x86_64", Value: getTriple().getArch() == llvm::Triple::x86_64)
1313 .Case(S: "x87", Value: HasX87)
1314 .Case(S: "xop", Value: XOPLevel >= XOP)
1315 .Case(S: "xsave", Value: HasXSAVE)
1316 .Case(S: "xsavec", Value: HasXSAVEC)
1317 .Case(S: "xsaves", Value: HasXSAVES)
1318 .Case(S: "xsaveopt", Value: HasXSAVEOPT)
1319 .Case(S: "fullbf16", Value: HasFullBFloat16)
1320 .Case(S: "egpr", Value: HasEGPR)
1321 .Case(S: "push2pop2", Value: HasPush2Pop2)
1322 .Case(S: "ppx", Value: HasPPX)
1323 .Case(S: "ndd", Value: HasNDD)
1324 .Case(S: "ccmp", Value: HasCCMP)
1325 .Case(S: "nf", Value: HasNF)
1326 .Case(S: "cf", Value: HasCF)
1327 .Case(S: "zu", Value: HasZU)
1328 .Case(S: "jmpabs", Value: HasJMPABS)
1329 .Case(S: "branch-hint", Value: HasBranchHint)
1330 .Default(Value: false);
1331}
1332
1333// We can't use a generic validation scheme for the features accepted here
1334// versus subtarget features accepted in the target attribute because the
1335// bitfield structure that's initialized in the runtime only supports the
1336// below currently rather than the full range of subtarget features. (See
1337// X86TargetInfo::hasFeature for a somewhat comprehensive list).
1338bool X86TargetInfo::validateCpuSupports(StringRef FeatureStr) const {
1339 return llvm::StringSwitch<bool>(FeatureStr)
1340#define X86_FEATURE_COMPAT(ENUM, STR, PRIORITY, ABI_VALUE) .Case(STR, true)
1341#define X86_MICROARCH_LEVEL(ENUM, STR, PRIORITY, ABI_VALUE) .Case(STR, true)
1342#include "llvm/TargetParser/X86TargetParser.def"
1343 .Default(Value: false);
1344}
1345
1346static llvm::X86::ProcessorFeatures getFeature(StringRef Name) {
1347 return llvm::StringSwitch<llvm::X86::ProcessorFeatures>(Name)
1348#define X86_FEATURE_COMPAT(ENUM, STR, PRIORITY, ABI_VALUE) \
1349 .Case(STR, llvm::X86::FEATURE_##ENUM)
1350
1351#include "llvm/TargetParser/X86TargetParser.def"
1352 ;
1353 // Note, this function should only be used after ensuring the value is
1354 // correct, so it asserts if the value is out of range.
1355}
1356
1357llvm::APInt X86TargetInfo::getFMVPriority(ArrayRef<StringRef> Features) const {
1358 auto getPriority = [](StringRef Feature) -> unsigned {
1359 // Valid CPUs have a 'key feature' that compares just better than its key
1360 // feature.
1361 using namespace llvm::X86;
1362 CPUKind Kind = parseArchX86(CPU: Feature);
1363 if (Kind != CK_None) {
1364 ProcessorFeatures KeyFeature = getKeyFeature(Kind);
1365 return (getFeaturePriority(Feat: KeyFeature) << 1) + 1;
1366 }
1367 // Now we know we have a feature, so get its priority and shift it a few so
1368 // that we have sufficient room for the CPUs (above).
1369 return getFeaturePriority(Feat: getFeature(Name: Feature)) << 1;
1370 };
1371
1372 unsigned Priority = 0;
1373 for (StringRef Feature : Features)
1374 if (!Feature.empty())
1375 Priority = std::max(a: Priority, b: getPriority(Feature));
1376 return llvm::APInt(32, Priority);
1377}
1378
1379bool X86TargetInfo::validateCPUSpecificCPUDispatch(StringRef Name) const {
1380 return llvm::X86::validateCPUSpecificCPUDispatch(Name);
1381}
1382
1383char X86TargetInfo::CPUSpecificManglingCharacter(StringRef Name) const {
1384 return llvm::X86::getCPUDispatchMangling(Name);
1385}
1386
1387void X86TargetInfo::getCPUSpecificCPUDispatchFeatures(
1388 StringRef Name, llvm::SmallVectorImpl<StringRef> &Features) const {
1389 SmallVector<StringRef, 32> TargetCPUFeatures;
1390 llvm::X86::getFeaturesForCPU(CPU: Name, Features&: TargetCPUFeatures, NeedPlus: true);
1391 for (auto &F : TargetCPUFeatures)
1392 Features.push_back(Elt: F);
1393}
1394
1395// We can't use a generic validation scheme for the cpus accepted here
1396// versus subtarget cpus accepted in the target attribute because the
1397// variables intitialized by the runtime only support the below currently
1398// rather than the full range of cpus.
1399bool X86TargetInfo::validateCpuIs(StringRef FeatureStr) const {
1400 return llvm::StringSwitch<bool>(FeatureStr)
1401#define X86_VENDOR(ENUM, STRING, ABI_VALUE) .Case(STRING, true)
1402#define X86_CPU_TYPE(ENUM, STR, ABI_VALUE) .Case(STR, true)
1403#define X86_CPU_SUBTYPE(ENUM, STR, ABI_VALUE) .Case(STR, true)
1404#include "llvm/TargetParser/X86TargetParser.def"
1405 .Default(Value: false);
1406}
1407
1408static unsigned matchAsmCCConstraint(const char *Name) {
1409 auto RV = llvm::StringSwitch<unsigned>(Name)
1410 .Case(S: "@cca", Value: 4)
1411 .Case(S: "@ccae", Value: 5)
1412 .Case(S: "@ccb", Value: 4)
1413 .Case(S: "@ccbe", Value: 5)
1414 .Case(S: "@ccc", Value: 4)
1415 .Case(S: "@cce", Value: 4)
1416 .Case(S: "@ccz", Value: 4)
1417 .Case(S: "@ccg", Value: 4)
1418 .Case(S: "@ccge", Value: 5)
1419 .Case(S: "@ccl", Value: 4)
1420 .Case(S: "@ccle", Value: 5)
1421 .Case(S: "@ccna", Value: 5)
1422 .Case(S: "@ccnae", Value: 6)
1423 .Case(S: "@ccnb", Value: 5)
1424 .Case(S: "@ccnbe", Value: 6)
1425 .Case(S: "@ccnc", Value: 5)
1426 .Case(S: "@ccne", Value: 5)
1427 .Case(S: "@ccnz", Value: 5)
1428 .Case(S: "@ccng", Value: 5)
1429 .Case(S: "@ccnge", Value: 6)
1430 .Case(S: "@ccnl", Value: 5)
1431 .Case(S: "@ccnle", Value: 6)
1432 .Case(S: "@ccno", Value: 5)
1433 .Case(S: "@ccnp", Value: 5)
1434 .Case(S: "@ccns", Value: 5)
1435 .Case(S: "@cco", Value: 4)
1436 .Case(S: "@ccp", Value: 4)
1437 .Case(S: "@ccs", Value: 4)
1438 .Default(Value: 0);
1439 return RV;
1440}
1441
1442bool X86TargetInfo::validateAsmConstraint(
1443 const char *&Name, TargetInfo::ConstraintInfo &Info) const {
1444 switch (*Name) {
1445 default:
1446 return false;
1447 // Constant constraints.
1448 case 'e': // 32-bit signed integer constant for use with sign-extending x86_64
1449 // instructions.
1450 case 'Z': // 32-bit unsigned integer constant for use with zero-extending
1451 // x86_64 instructions.
1452 case 's':
1453 Info.setRequiresImmediate();
1454 return true;
1455 case 'I':
1456 Info.setRequiresImmediate(Min: 0, Max: 31);
1457 return true;
1458 case 'J':
1459 Info.setRequiresImmediate(Min: 0, Max: 63);
1460 return true;
1461 case 'K':
1462 Info.setRequiresImmediate(Min: -128, Max: 127);
1463 return true;
1464 case 'L':
1465 Info.setRequiresImmediate({int(0xff), int(0xffff), int(0xffffffff)});
1466 return true;
1467 case 'M':
1468 Info.setRequiresImmediate(Min: 0, Max: 3);
1469 return true;
1470 case 'N':
1471 Info.setRequiresImmediate(Min: 0, Max: 255);
1472 return true;
1473 case 'O':
1474 Info.setRequiresImmediate(Min: 0, Max: 127);
1475 return true;
1476 case 'W':
1477 switch (*++Name) {
1478 default:
1479 return false;
1480 case 's':
1481 Info.setAllowsRegister();
1482 return true;
1483 }
1484 // Register constraints.
1485 case 'Y': // 'Y' is the first character for several 2-character constraints.
1486 // Shift the pointer to the second character of the constraint.
1487 Name++;
1488 switch (*Name) {
1489 default:
1490 return false;
1491 case 'z': // First SSE register.
1492 case '2':
1493 case 't': // Any SSE register, when SSE2 is enabled.
1494 case 'i': // Any SSE register, when SSE2 and inter-unit moves enabled.
1495 case 'm': // Any MMX register, when inter-unit moves enabled.
1496 case 'k': // AVX512 arch mask registers: k1-k7.
1497 Info.setAllowsRegister();
1498 return true;
1499 }
1500 case 'f': // Any x87 floating point stack register.
1501 // Constraint 'f' cannot be used for output operands.
1502 if (Info.ConstraintStr[0] == '=' || Info.ConstraintStr[0] == '+')
1503 return false;
1504 Info.setAllowsRegister();
1505 return true;
1506 case 'a': // eax.
1507 case 'b': // ebx.
1508 case 'c': // ecx.
1509 case 'd': // edx.
1510 case 'S': // esi.
1511 case 'D': // edi.
1512 case 'A': // edx:eax.
1513 case 't': // Top of floating point stack.
1514 case 'u': // Second from top of floating point stack.
1515 case 'q': // Any register accessible as [r]l: a, b, c, and d.
1516 case 'y': // Any MMX register.
1517 case 'v': // Any {X,Y,Z}MM register (Arch & context dependent)
1518 case 'x': // Any SSE register.
1519 case 'k': // Any AVX512 mask register (same as Yk, additionally allows k0
1520 // for intermideate k reg operations).
1521 case 'Q': // Any register accessible as [r]h: a, b, c, and d.
1522 case 'R': // "Legacy" registers: ax, bx, cx, dx, di, si, sp, bp.
1523 case 'l': // "Index" registers: any general register that can be used as an
1524 // index in a base+index memory access.
1525 Info.setAllowsRegister();
1526 return true;
1527 // Floating point constant constraints.
1528 case 'C': // SSE floating point constant.
1529 case 'G': // x87 floating point constant.
1530 return true;
1531 case 'j':
1532 Name++;
1533 switch (*Name) {
1534 default:
1535 return false;
1536 case 'r':
1537 Info.setAllowsRegister();
1538 return true;
1539 case 'R':
1540 Info.setAllowsRegister();
1541 return true;
1542 }
1543 case '@':
1544 // CC condition changes.
1545 if (auto Len = matchAsmCCConstraint(Name)) {
1546 Name += Len - 1;
1547 Info.setAllowsRegister();
1548 Info.setOutputOperandBounds(Min: 0, Max: 2);
1549 return true;
1550 }
1551 return false;
1552 }
1553}
1554
1555// Below is based on the following information:
1556// +------------------------------------+-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
1557// | Processor Name | Cache Line Size (Bytes) | Source |
1558// +------------------------------------+-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
1559// | i386 | 64 | https://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-optimization-manual.pdf |
1560// | i486 | 16 | "four doublewords" (doubleword = 32 bits, 4 bits * 32 bits = 16 bytes) https://en.wikichip.org/w/images/d/d3/i486_MICROPROCESSOR_HARDWARE_REFERENCE_MANUAL_%281990%29.pdf and http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.126.4216&rep=rep1&type=pdf (page 29) |
1561// | i586/Pentium MMX | 32 | https://www.7-cpu.com/cpu/P-MMX.html |
1562// | i686/Pentium | 32 | https://www.7-cpu.com/cpu/P6.html |
1563// | Netburst/Pentium4 | 64 | https://www.7-cpu.com/cpu/P4-180.html |
1564// | Atom | 64 | https://www.7-cpu.com/cpu/Atom.html |
1565// | Westmere | 64 | https://en.wikichip.org/wiki/intel/microarchitectures/sandy_bridge_(client) "Cache Architecture" |
1566// | Sandy Bridge | 64 | https://en.wikipedia.org/wiki/Sandy_Bridge and https://www.7-cpu.com/cpu/SandyBridge.html |
1567// | Ivy Bridge | 64 | https://blog.stuffedcow.net/2013/01/ivb-cache-replacement/ and https://www.7-cpu.com/cpu/IvyBridge.html |
1568// | Haswell | 64 | https://www.7-cpu.com/cpu/Haswell.html |
1569// | Broadwell | 64 | https://www.7-cpu.com/cpu/Broadwell.html |
1570// | Skylake (including skylake-avx512) | 64 | https://www.nas.nasa.gov/hecc/support/kb/skylake-processors_550.html "Cache Hierarchy" |
1571// | Cascade Lake | 64 | https://www.nas.nasa.gov/hecc/support/kb/cascade-lake-processors_579.html "Cache Hierarchy" |
1572// | Skylake | 64 | https://en.wikichip.org/wiki/intel/microarchitectures/kaby_lake "Memory Hierarchy" |
1573// | Ice Lake | 64 | https://www.7-cpu.com/cpu/Ice_Lake.html |
1574// | Knights Landing | 64 | https://software.intel.com/en-us/articles/intel-xeon-phi-processor-7200-family-memory-management-optimizations "The Intel® Xeon Phi™ Processor Architecture" |
1575// | Knights Mill | 64 | https://software.intel.com/sites/default/files/managed/9e/bc/64-ia-32-architectures-optimization-manual.pdf?countrylabel=Colombia "2.5.5.2 L1 DCache " |
1576// +------------------------------------+-------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
1577std::optional<unsigned> X86TargetInfo::getCPUCacheLineSize() const {
1578 using namespace llvm::X86;
1579 switch (CPU) {
1580 // i386
1581 case CK_i386:
1582 // i486
1583 case CK_i486:
1584 case CK_WinChipC6:
1585 case CK_WinChip2:
1586 case CK_C3:
1587 // Lakemont
1588 case CK_Lakemont:
1589 return 16;
1590
1591 // i586
1592 case CK_i586:
1593 case CK_Pentium:
1594 case CK_PentiumMMX:
1595 // i686
1596 case CK_PentiumPro:
1597 case CK_i686:
1598 case CK_Pentium2:
1599 case CK_Pentium3:
1600 case CK_PentiumM:
1601 case CK_C3_2:
1602 // K6
1603 case CK_K6:
1604 case CK_K6_2:
1605 case CK_K6_3:
1606 // Geode
1607 case CK_Geode:
1608 return 32;
1609
1610 // Netburst
1611 case CK_Pentium4:
1612 case CK_Prescott:
1613 case CK_Nocona:
1614 // Atom
1615 case CK_Bonnell:
1616 case CK_Silvermont:
1617 case CK_Goldmont:
1618 case CK_GoldmontPlus:
1619 case CK_Tremont:
1620 case CK_Gracemont:
1621
1622 case CK_Westmere:
1623 case CK_SandyBridge:
1624 case CK_IvyBridge:
1625 case CK_Haswell:
1626 case CK_Broadwell:
1627 case CK_SkylakeClient:
1628 case CK_SkylakeServer:
1629 case CK_Cascadelake:
1630 case CK_Nehalem:
1631 case CK_Cooperlake:
1632 case CK_Cannonlake:
1633 case CK_Tigerlake:
1634 case CK_SapphireRapids:
1635 case CK_IcelakeClient:
1636 case CK_Rocketlake:
1637 case CK_IcelakeServer:
1638 case CK_Alderlake:
1639 case CK_Raptorlake:
1640 case CK_Meteorlake:
1641 case CK_Arrowlake:
1642 case CK_ArrowlakeS:
1643 case CK_Lunarlake:
1644 case CK_Pantherlake:
1645 case CK_Wildcatlake:
1646 case CK_Novalake:
1647 case CK_Sierraforest:
1648 case CK_Grandridge:
1649 case CK_Graniterapids:
1650 case CK_GraniterapidsD:
1651 case CK_Emeraldrapids:
1652 case CK_Clearwaterforest:
1653 case CK_Diamondrapids:
1654 case CK_KNL:
1655 case CK_KNM:
1656 // K7
1657 case CK_Athlon:
1658 case CK_AthlonXP:
1659 // K8
1660 case CK_K8:
1661 case CK_K8SSE3:
1662 case CK_AMDFAM10:
1663 // Bobcat
1664 case CK_BTVER1:
1665 case CK_BTVER2:
1666 // Bulldozer
1667 case CK_BDVER1:
1668 case CK_BDVER2:
1669 case CK_BDVER3:
1670 case CK_BDVER4:
1671 // Zen
1672 case CK_ZNVER1:
1673 case CK_ZNVER2:
1674 case CK_ZNVER3:
1675 case CK_ZNVER4:
1676 case CK_ZNVER5:
1677 case CK_ZNVER6:
1678 // Hygon
1679 case CK_C86_4G_M4:
1680 case CK_C86_4G_M6:
1681 case CK_C86_4G_M7:
1682 case CK_C86_4G_M8:
1683 // Deprecated
1684 case CK_x86_64:
1685 case CK_x86_64_v2:
1686 case CK_x86_64_v3:
1687 case CK_x86_64_v4:
1688 case CK_Yonah:
1689 case CK_Penryn:
1690 case CK_Core2:
1691 return 64;
1692
1693 // The following currently have unknown cache line sizes (but they are probably all 64):
1694 // Core
1695 case CK_None:
1696 return std::nullopt;
1697 }
1698 llvm_unreachable("Unknown CPU kind");
1699}
1700
1701bool X86TargetInfo::validateOutputSize(const llvm::StringMap<bool> &FeatureMap,
1702 StringRef Constraint,
1703 unsigned Size) const {
1704 // Strip off constraint modifiers.
1705 Constraint = Constraint.ltrim(Chars: "=+&");
1706
1707 return validateOperandSize(FeatureMap, Constraint, Size);
1708}
1709
1710bool X86TargetInfo::validateInputSize(const llvm::StringMap<bool> &FeatureMap,
1711 StringRef Constraint,
1712 unsigned Size) const {
1713 return validateOperandSize(FeatureMap, Constraint, Size);
1714}
1715
1716bool X86TargetInfo::validateOperandSize(const llvm::StringMap<bool> &FeatureMap,
1717 StringRef Constraint,
1718 unsigned Size) const {
1719 switch (Constraint[0]) {
1720 default:
1721 break;
1722 case 'k':
1723 // Registers k0-k7 (AVX512) size limit is 64 bit.
1724 case 'y':
1725 return Size <= 64;
1726 case 'f':
1727 case 't':
1728 case 'u':
1729 return Size <= 128;
1730 case 'Y':
1731 // 'Y' is the first character for several 2-character constraints.
1732 switch (Constraint[1]) {
1733 default:
1734 return false;
1735 case 'm':
1736 // 'Ym' is synonymous with 'y'.
1737 case 'k':
1738 return Size <= 64;
1739 case 'z':
1740 // XMM0/YMM/ZMM0
1741 if (hasFeatureEnabled(Features: FeatureMap, Name: "avx512f"))
1742 // ZMM0 can be used if target supports AVX512F.
1743 return Size <= 512U;
1744 else if (hasFeatureEnabled(Features: FeatureMap, Name: "avx"))
1745 // YMM0 can be used if target supports AVX.
1746 return Size <= 256U;
1747 else if (hasFeatureEnabled(Features: FeatureMap, Name: "sse"))
1748 return Size <= 128U;
1749 return false;
1750 case 'i':
1751 case 't':
1752 case '2':
1753 // 'Yi','Yt','Y2' are synonymous with 'x' when SSE2 is enabled.
1754 if (SSELevel < SSE2)
1755 return false;
1756 break;
1757 }
1758 break;
1759 case 'v':
1760 case 'x':
1761 if (hasFeatureEnabled(Features: FeatureMap, Name: "avx512f"))
1762 // 512-bit zmm registers can be used if target supports AVX512F.
1763 return Size <= 512U;
1764 else if (hasFeatureEnabled(Features: FeatureMap, Name: "avx"))
1765 // 256-bit ymm registers can be used if target supports AVX.
1766 return Size <= 256U;
1767 return Size <= 128U;
1768
1769 }
1770
1771 return true;
1772}
1773
1774std::string X86TargetInfo::convertConstraint(const char *&Constraint) const {
1775 switch (*Constraint) {
1776 case '@':
1777 if (auto Len = matchAsmCCConstraint(Name: Constraint)) {
1778 std::string Converted = "{" + std::string(Constraint, Len) + "}";
1779 Constraint += Len - 1;
1780 return Converted;
1781 }
1782 return std::string(1, *Constraint);
1783 case 'a':
1784 return std::string("{ax}");
1785 case 'b':
1786 return std::string("{bx}");
1787 case 'c':
1788 return std::string("{cx}");
1789 case 'd':
1790 return std::string("{dx}");
1791 case 'S':
1792 return std::string("{si}");
1793 case 'D':
1794 return std::string("{di}");
1795 case 'p': // Keep 'p' constraint (address).
1796 return std::string("p");
1797 case 't': // top of floating point stack.
1798 return std::string("{st}");
1799 case 'u': // second from top of floating point stack.
1800 return std::string("{st(1)}"); // second from top of floating point stack.
1801 case 'W':
1802 assert(Constraint[1] == 's');
1803 return '^' + std::string(Constraint++, 2);
1804 case 'Y':
1805 switch (Constraint[1]) {
1806 default:
1807 // Break from inner switch and fall through (copy single char),
1808 // continue parsing after copying the current constraint into
1809 // the return string.
1810 break;
1811 case 'k':
1812 case 'm':
1813 case 'i':
1814 case 't':
1815 case 'z':
1816 case '2':
1817 // "^" hints llvm that this is a 2 letter constraint.
1818 // "Constraint++" is used to promote the string iterator
1819 // to the next constraint.
1820 return std::string("^") + std::string(Constraint++, 2);
1821 }
1822 [[fallthrough]];
1823 case 'j':
1824 switch (Constraint[1]) {
1825 default:
1826 // Break from inner switch and fall through (copy single char),
1827 // continue parsing after copying the current constraint into
1828 // the return string.
1829 break;
1830 case 'r':
1831 case 'R':
1832 // "^" hints llvm that this is a 2 letter constraint.
1833 // "Constraint++" is used to promote the string iterator
1834 // to the next constraint.
1835 return std::string("^") + std::string(Constraint++, 2);
1836 }
1837 [[fallthrough]];
1838 default:
1839 return std::string(1, *Constraint);
1840 }
1841}
1842
1843void X86TargetInfo::fillValidCPUList(SmallVectorImpl<StringRef> &Values) const {
1844 bool Only64Bit = getTriple().getArch() != llvm::Triple::x86;
1845 llvm::X86::fillValidCPUArchList(Values, Only64Bit);
1846}
1847
1848void X86TargetInfo::fillValidTuneCPUList(SmallVectorImpl<StringRef> &Values) const {
1849 llvm::X86::fillValidTuneCPUList(Values);
1850}
1851
1852ArrayRef<const char *> X86TargetInfo::getGCCRegNames() const {
1853 return llvm::ArrayRef(GCCRegNames);
1854}
1855
1856ArrayRef<TargetInfo::AddlRegName> X86TargetInfo::getGCCAddlRegNames() const {
1857 return llvm::ArrayRef(AddlRegNames);
1858}
1859
1860llvm::SmallVector<Builtin::InfosShard>
1861X86_32TargetInfo::getTargetBuiltins() const {
1862 return {
1863 {.Strings: &X86::BuiltinStrings, .Infos: X86::BuiltinInfos},
1864 {.Strings: &X86::BuiltinStrings, .Infos: X86::PrefixedBuiltinInfos, .NamePrefix: "__builtin_ia32_"},
1865 };
1866}
1867
1868llvm::SmallVector<Builtin::InfosShard>
1869X86_64TargetInfo::getTargetBuiltins() const {
1870 return {
1871 {.Strings: &X86::BuiltinStrings, .Infos: X86::BuiltinInfos},
1872 {.Strings: &X86::BuiltinStrings, .Infos: X86::PrefixedBuiltinInfos, .NamePrefix: "__builtin_ia32_"},
1873 {.Strings: &X86_64::BuiltinStrings, .Infos: X86_64::BuiltinInfos},
1874 {.Strings: &X86_64::BuiltinStrings, .Infos: X86_64::PrefixedBuiltinInfos,
1875 .NamePrefix: "__builtin_ia32_"},
1876 };
1877}
1878
1879unsigned
1880MicrosoftX86_64TargetInfo::getMinGlobalAlign(uint64_t TypeSize,
1881 bool HasNonWeakDef) const {
1882 unsigned Align =
1883 WindowsX86_64TargetInfo::getMinGlobalAlign(Size: TypeSize, HasNonWeakDef);
1884
1885 return std::max(a: Align, b: Microsoft64BitMinGlobalAlign(TypeSize));
1886}
1887