X86.cpp 51 KB

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