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