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