X86.cpp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  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/TargetParser.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. };
  61. const TargetInfo::AddlRegName AddlRegNames[] = {
  62. {{"al", "ah", "eax", "rax"}, 0},
  63. {{"bl", "bh", "ebx", "rbx"}, 3},
  64. {{"cl", "ch", "ecx", "rcx"}, 2},
  65. {{"dl", "dh", "edx", "rdx"}, 1},
  66. {{"esi", "rsi"}, 4},
  67. {{"edi", "rdi"}, 5},
  68. {{"esp", "rsp"}, 7},
  69. {{"ebp", "rbp"}, 6},
  70. {{"r8d", "r8w", "r8b"}, 38},
  71. {{"r9d", "r9w", "r9b"}, 39},
  72. {{"r10d", "r10w", "r10b"}, 40},
  73. {{"r11d", "r11w", "r11b"}, 41},
  74. {{"r12d", "r12w", "r12b"}, 42},
  75. {{"r13d", "r13w", "r13b"}, 43},
  76. {{"r14d", "r14w", "r14b"}, 44},
  77. {{"r15d", "r15w", "r15b"}, 45},
  78. };
  79. } // namespace targets
  80. } // namespace clang
  81. using namespace clang;
  82. using namespace clang::targets;
  83. bool X86TargetInfo::setFPMath(StringRef Name) {
  84. if (Name == "387") {
  85. FPMath = FP_387;
  86. return true;
  87. }
  88. if (Name == "sse") {
  89. FPMath = FP_SSE;
  90. return true;
  91. }
  92. return false;
  93. }
  94. bool X86TargetInfo::initFeatureMap(
  95. llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
  96. const std::vector<std::string> &FeaturesVec) const {
  97. // FIXME: This *really* should not be here.
  98. // X86_64 always has SSE2.
  99. if (getTriple().getArch() == llvm::Triple::x86_64)
  100. setFeatureEnabledImpl(Features, "sse2", true);
  101. const CPUKind Kind = getCPUKind(CPU);
  102. // Enable X87 for all X86 processors but Lakemont.
  103. if (Kind != CK_Lakemont)
  104. setFeatureEnabledImpl(Features, "x87", true);
  105. // Enable cmpxchg8 for i586 and greater CPUs. Include generic for backwards
  106. // compatibility.
  107. if (Kind >= CK_i586 || Kind == CK_Generic)
  108. setFeatureEnabledImpl(Features, "cx8", true);
  109. switch (Kind) {
  110. case CK_Generic:
  111. case CK_i386:
  112. case CK_i486:
  113. case CK_i586:
  114. case CK_Pentium:
  115. case CK_PentiumPro:
  116. case CK_i686:
  117. case CK_Lakemont:
  118. break;
  119. case CK_PentiumMMX:
  120. case CK_Pentium2:
  121. case CK_K6:
  122. case CK_WinChipC6:
  123. setFeatureEnabledImpl(Features, "mmx", true);
  124. break;
  125. case CK_Cooperlake:
  126. // CPX inherits all CLX features plus AVX512BF16
  127. setFeatureEnabledImpl(Features, "avx512bf16", true);
  128. LLVM_FALLTHROUGH;
  129. case CK_Cascadelake:
  130. // CLX inherits all SKX features plus AVX512VNNI
  131. setFeatureEnabledImpl(Features, "avx512vnni", true);
  132. LLVM_FALLTHROUGH;
  133. case CK_SkylakeServer:
  134. setFeatureEnabledImpl(Features, "avx512f", true);
  135. setFeatureEnabledImpl(Features, "avx512cd", true);
  136. setFeatureEnabledImpl(Features, "avx512dq", true);
  137. setFeatureEnabledImpl(Features, "avx512bw", true);
  138. setFeatureEnabledImpl(Features, "avx512vl", true);
  139. setFeatureEnabledImpl(Features, "clwb", true);
  140. setFeatureEnabledImpl(Features, "pku", true);
  141. // SkylakeServer cores inherits all SKL features, except SGX
  142. goto SkylakeCommon;
  143. case CK_Tigerlake:
  144. setFeatureEnabledImpl(Features, "avx512vp2intersect", true);
  145. setFeatureEnabledImpl(Features, "movdiri", true);
  146. setFeatureEnabledImpl(Features, "movdir64b", true);
  147. setFeatureEnabledImpl(Features, "shstk", true);
  148. // Tigerlake cores inherits IcelakeClient, except pconfig and wbnoinvd
  149. goto IcelakeCommon;
  150. case CK_IcelakeServer:
  151. setFeatureEnabledImpl(Features, "pconfig", true);
  152. setFeatureEnabledImpl(Features, "wbnoinvd", true);
  153. LLVM_FALLTHROUGH;
  154. case CK_IcelakeClient:
  155. IcelakeCommon:
  156. setFeatureEnabledImpl(Features, "vaes", true);
  157. setFeatureEnabledImpl(Features, "gfni", true);
  158. setFeatureEnabledImpl(Features, "vpclmulqdq", true);
  159. setFeatureEnabledImpl(Features, "avx512bitalg", true);
  160. setFeatureEnabledImpl(Features, "avx512vbmi2", true);
  161. setFeatureEnabledImpl(Features, "avx512vnni", true);
  162. setFeatureEnabledImpl(Features, "avx512vpopcntdq", true);
  163. setFeatureEnabledImpl(Features, "rdpid", true);
  164. setFeatureEnabledImpl(Features, "clwb", true);
  165. LLVM_FALLTHROUGH;
  166. case CK_Cannonlake:
  167. setFeatureEnabledImpl(Features, "avx512f", true);
  168. setFeatureEnabledImpl(Features, "avx512cd", true);
  169. setFeatureEnabledImpl(Features, "avx512dq", true);
  170. setFeatureEnabledImpl(Features, "avx512bw", true);
  171. setFeatureEnabledImpl(Features, "avx512vl", true);
  172. setFeatureEnabledImpl(Features, "avx512ifma", true);
  173. setFeatureEnabledImpl(Features, "avx512vbmi", true);
  174. setFeatureEnabledImpl(Features, "pku", true);
  175. setFeatureEnabledImpl(Features, "sha", true);
  176. LLVM_FALLTHROUGH;
  177. case CK_SkylakeClient:
  178. setFeatureEnabledImpl(Features, "sgx", true);
  179. // SkylakeServer cores inherits all SKL features, except SGX
  180. SkylakeCommon:
  181. setFeatureEnabledImpl(Features, "xsavec", true);
  182. setFeatureEnabledImpl(Features, "xsaves", true);
  183. setFeatureEnabledImpl(Features, "clflushopt", true);
  184. setFeatureEnabledImpl(Features, "aes", true);
  185. LLVM_FALLTHROUGH;
  186. case CK_Broadwell:
  187. setFeatureEnabledImpl(Features, "rdseed", true);
  188. setFeatureEnabledImpl(Features, "adx", true);
  189. setFeatureEnabledImpl(Features, "prfchw", true);
  190. LLVM_FALLTHROUGH;
  191. case CK_Haswell:
  192. setFeatureEnabledImpl(Features, "avx2", true);
  193. setFeatureEnabledImpl(Features, "lzcnt", true);
  194. setFeatureEnabledImpl(Features, "bmi", true);
  195. setFeatureEnabledImpl(Features, "bmi2", true);
  196. setFeatureEnabledImpl(Features, "fma", true);
  197. setFeatureEnabledImpl(Features, "invpcid", true);
  198. setFeatureEnabledImpl(Features, "movbe", true);
  199. LLVM_FALLTHROUGH;
  200. case CK_IvyBridge:
  201. setFeatureEnabledImpl(Features, "rdrnd", true);
  202. setFeatureEnabledImpl(Features, "f16c", true);
  203. setFeatureEnabledImpl(Features, "fsgsbase", true);
  204. LLVM_FALLTHROUGH;
  205. case CK_SandyBridge:
  206. setFeatureEnabledImpl(Features, "avx", true);
  207. setFeatureEnabledImpl(Features, "xsave", true);
  208. setFeatureEnabledImpl(Features, "xsaveopt", true);
  209. LLVM_FALLTHROUGH;
  210. case CK_Westmere:
  211. setFeatureEnabledImpl(Features, "pclmul", true);
  212. LLVM_FALLTHROUGH;
  213. case CK_Nehalem:
  214. setFeatureEnabledImpl(Features, "sse4.2", true);
  215. LLVM_FALLTHROUGH;
  216. case CK_Penryn:
  217. setFeatureEnabledImpl(Features, "sse4.1", true);
  218. LLVM_FALLTHROUGH;
  219. case CK_Core2:
  220. setFeatureEnabledImpl(Features, "ssse3", true);
  221. setFeatureEnabledImpl(Features, "sahf", true);
  222. LLVM_FALLTHROUGH;
  223. case CK_Nocona:
  224. setFeatureEnabledImpl(Features, "cx16", true);
  225. LLVM_FALLTHROUGH;
  226. case CK_Yonah:
  227. case CK_Prescott:
  228. setFeatureEnabledImpl(Features, "sse3", true);
  229. LLVM_FALLTHROUGH;
  230. case CK_PentiumM:
  231. case CK_Pentium4:
  232. case CK_x86_64:
  233. setFeatureEnabledImpl(Features, "sse2", true);
  234. LLVM_FALLTHROUGH;
  235. case CK_Pentium3:
  236. case CK_C3_2:
  237. setFeatureEnabledImpl(Features, "sse", true);
  238. setFeatureEnabledImpl(Features, "fxsr", true);
  239. break;
  240. case CK_Tremont:
  241. setFeatureEnabledImpl(Features, "cldemote", true);
  242. setFeatureEnabledImpl(Features, "movdiri", true);
  243. setFeatureEnabledImpl(Features, "movdir64b", true);
  244. setFeatureEnabledImpl(Features, "gfni", true);
  245. setFeatureEnabledImpl(Features, "waitpkg", true);
  246. LLVM_FALLTHROUGH;
  247. case CK_GoldmontPlus:
  248. setFeatureEnabledImpl(Features, "ptwrite", true);
  249. setFeatureEnabledImpl(Features, "rdpid", true);
  250. setFeatureEnabledImpl(Features, "sgx", true);
  251. LLVM_FALLTHROUGH;
  252. case CK_Goldmont:
  253. setFeatureEnabledImpl(Features, "sha", true);
  254. setFeatureEnabledImpl(Features, "rdseed", true);
  255. setFeatureEnabledImpl(Features, "xsave", true);
  256. setFeatureEnabledImpl(Features, "xsaveopt", true);
  257. setFeatureEnabledImpl(Features, "xsavec", true);
  258. setFeatureEnabledImpl(Features, "xsaves", true);
  259. setFeatureEnabledImpl(Features, "clflushopt", true);
  260. setFeatureEnabledImpl(Features, "fsgsbase", true);
  261. setFeatureEnabledImpl(Features, "aes", true);
  262. LLVM_FALLTHROUGH;
  263. case CK_Silvermont:
  264. setFeatureEnabledImpl(Features, "rdrnd", true);
  265. setFeatureEnabledImpl(Features, "pclmul", true);
  266. setFeatureEnabledImpl(Features, "sse4.2", true);
  267. setFeatureEnabledImpl(Features, "prfchw", true);
  268. LLVM_FALLTHROUGH;
  269. case CK_Bonnell:
  270. setFeatureEnabledImpl(Features, "movbe", true);
  271. setFeatureEnabledImpl(Features, "ssse3", true);
  272. setFeatureEnabledImpl(Features, "fxsr", true);
  273. setFeatureEnabledImpl(Features, "cx16", true);
  274. setFeatureEnabledImpl(Features, "sahf", true);
  275. break;
  276. case CK_KNM:
  277. // TODO: Add avx5124fmaps/avx5124vnniw.
  278. setFeatureEnabledImpl(Features, "avx512vpopcntdq", true);
  279. LLVM_FALLTHROUGH;
  280. case CK_KNL:
  281. setFeatureEnabledImpl(Features, "avx512f", true);
  282. setFeatureEnabledImpl(Features, "avx512cd", true);
  283. setFeatureEnabledImpl(Features, "avx512er", true);
  284. setFeatureEnabledImpl(Features, "avx512pf", true);
  285. setFeatureEnabledImpl(Features, "prfchw", true);
  286. setFeatureEnabledImpl(Features, "prefetchwt1", true);
  287. setFeatureEnabledImpl(Features, "fxsr", true);
  288. setFeatureEnabledImpl(Features, "rdseed", true);
  289. setFeatureEnabledImpl(Features, "adx", true);
  290. setFeatureEnabledImpl(Features, "lzcnt", true);
  291. setFeatureEnabledImpl(Features, "bmi", true);
  292. setFeatureEnabledImpl(Features, "bmi2", true);
  293. setFeatureEnabledImpl(Features, "fma", true);
  294. setFeatureEnabledImpl(Features, "rdrnd", true);
  295. setFeatureEnabledImpl(Features, "f16c", true);
  296. setFeatureEnabledImpl(Features, "fsgsbase", true);
  297. setFeatureEnabledImpl(Features, "aes", true);
  298. setFeatureEnabledImpl(Features, "pclmul", true);
  299. setFeatureEnabledImpl(Features, "cx16", true);
  300. setFeatureEnabledImpl(Features, "xsaveopt", true);
  301. setFeatureEnabledImpl(Features, "xsave", true);
  302. setFeatureEnabledImpl(Features, "movbe", true);
  303. setFeatureEnabledImpl(Features, "sahf", true);
  304. break;
  305. case CK_K6_2:
  306. case CK_K6_3:
  307. case CK_WinChip2:
  308. case CK_C3:
  309. setFeatureEnabledImpl(Features, "3dnow", true);
  310. break;
  311. case CK_AMDFAM10:
  312. setFeatureEnabledImpl(Features, "sse4a", true);
  313. setFeatureEnabledImpl(Features, "lzcnt", true);
  314. setFeatureEnabledImpl(Features, "popcnt", true);
  315. setFeatureEnabledImpl(Features, "sahf", true);
  316. LLVM_FALLTHROUGH;
  317. case CK_K8SSE3:
  318. setFeatureEnabledImpl(Features, "sse3", true);
  319. LLVM_FALLTHROUGH;
  320. case CK_K8:
  321. setFeatureEnabledImpl(Features, "sse2", true);
  322. LLVM_FALLTHROUGH;
  323. case CK_AthlonXP:
  324. setFeatureEnabledImpl(Features, "sse", true);
  325. setFeatureEnabledImpl(Features, "fxsr", true);
  326. LLVM_FALLTHROUGH;
  327. case CK_Athlon:
  328. case CK_Geode:
  329. setFeatureEnabledImpl(Features, "3dnowa", true);
  330. break;
  331. case CK_BTVER2:
  332. setFeatureEnabledImpl(Features, "avx", true);
  333. setFeatureEnabledImpl(Features, "aes", true);
  334. setFeatureEnabledImpl(Features, "pclmul", true);
  335. setFeatureEnabledImpl(Features, "bmi", true);
  336. setFeatureEnabledImpl(Features, "f16c", true);
  337. setFeatureEnabledImpl(Features, "xsaveopt", true);
  338. setFeatureEnabledImpl(Features, "movbe", true);
  339. LLVM_FALLTHROUGH;
  340. case CK_BTVER1:
  341. setFeatureEnabledImpl(Features, "ssse3", true);
  342. setFeatureEnabledImpl(Features, "sse4a", true);
  343. setFeatureEnabledImpl(Features, "lzcnt", true);
  344. setFeatureEnabledImpl(Features, "popcnt", true);
  345. setFeatureEnabledImpl(Features, "prfchw", true);
  346. setFeatureEnabledImpl(Features, "cx16", true);
  347. setFeatureEnabledImpl(Features, "fxsr", true);
  348. setFeatureEnabledImpl(Features, "sahf", true);
  349. break;
  350. case CK_ZNVER2:
  351. setFeatureEnabledImpl(Features, "clwb", true);
  352. setFeatureEnabledImpl(Features, "rdpid", true);
  353. setFeatureEnabledImpl(Features, "wbnoinvd", true);
  354. LLVM_FALLTHROUGH;
  355. case CK_ZNVER1:
  356. setFeatureEnabledImpl(Features, "adx", true);
  357. setFeatureEnabledImpl(Features, "aes", true);
  358. setFeatureEnabledImpl(Features, "avx2", true);
  359. setFeatureEnabledImpl(Features, "bmi", true);
  360. setFeatureEnabledImpl(Features, "bmi2", true);
  361. setFeatureEnabledImpl(Features, "clflushopt", true);
  362. setFeatureEnabledImpl(Features, "clzero", true);
  363. setFeatureEnabledImpl(Features, "cx16", true);
  364. setFeatureEnabledImpl(Features, "f16c", true);
  365. setFeatureEnabledImpl(Features, "fma", true);
  366. setFeatureEnabledImpl(Features, "fsgsbase", true);
  367. setFeatureEnabledImpl(Features, "fxsr", true);
  368. setFeatureEnabledImpl(Features, "lzcnt", true);
  369. setFeatureEnabledImpl(Features, "mwaitx", true);
  370. setFeatureEnabledImpl(Features, "movbe", true);
  371. setFeatureEnabledImpl(Features, "pclmul", true);
  372. setFeatureEnabledImpl(Features, "popcnt", true);
  373. setFeatureEnabledImpl(Features, "prfchw", true);
  374. setFeatureEnabledImpl(Features, "rdrnd", true);
  375. setFeatureEnabledImpl(Features, "rdseed", true);
  376. setFeatureEnabledImpl(Features, "sahf", true);
  377. setFeatureEnabledImpl(Features, "sha", true);
  378. setFeatureEnabledImpl(Features, "sse4a", true);
  379. setFeatureEnabledImpl(Features, "xsave", true);
  380. setFeatureEnabledImpl(Features, "xsavec", true);
  381. setFeatureEnabledImpl(Features, "xsaveopt", true);
  382. setFeatureEnabledImpl(Features, "xsaves", true);
  383. break;
  384. case CK_BDVER4:
  385. setFeatureEnabledImpl(Features, "avx2", true);
  386. setFeatureEnabledImpl(Features, "bmi2", true);
  387. setFeatureEnabledImpl(Features, "mwaitx", true);
  388. LLVM_FALLTHROUGH;
  389. case CK_BDVER3:
  390. setFeatureEnabledImpl(Features, "fsgsbase", true);
  391. setFeatureEnabledImpl(Features, "xsaveopt", true);
  392. LLVM_FALLTHROUGH;
  393. case CK_BDVER2:
  394. setFeatureEnabledImpl(Features, "bmi", true);
  395. setFeatureEnabledImpl(Features, "fma", true);
  396. setFeatureEnabledImpl(Features, "f16c", true);
  397. setFeatureEnabledImpl(Features, "tbm", true);
  398. LLVM_FALLTHROUGH;
  399. case CK_BDVER1:
  400. // xop implies avx, sse4a and fma4.
  401. setFeatureEnabledImpl(Features, "xop", true);
  402. setFeatureEnabledImpl(Features, "lwp", true);
  403. setFeatureEnabledImpl(Features, "lzcnt", true);
  404. setFeatureEnabledImpl(Features, "aes", true);
  405. setFeatureEnabledImpl(Features, "pclmul", true);
  406. setFeatureEnabledImpl(Features, "prfchw", true);
  407. setFeatureEnabledImpl(Features, "cx16", true);
  408. setFeatureEnabledImpl(Features, "fxsr", true);
  409. setFeatureEnabledImpl(Features, "xsave", true);
  410. setFeatureEnabledImpl(Features, "sahf", true);
  411. break;
  412. }
  413. if (!TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec))
  414. return false;
  415. // Can't do this earlier because we need to be able to explicitly enable
  416. // or disable these features and the things that they depend upon.
  417. // Enable popcnt if sse4.2 is enabled and popcnt is not explicitly disabled.
  418. auto I = Features.find("sse4.2");
  419. if (I != Features.end() && I->getValue() &&
  420. llvm::find(FeaturesVec, "-popcnt") == FeaturesVec.end())
  421. Features["popcnt"] = true;
  422. // Enable prfchw if 3DNow! is enabled and prfchw is not explicitly disabled.
  423. I = Features.find("3dnow");
  424. if (I != Features.end() && I->getValue() &&
  425. llvm::find(FeaturesVec, "-prfchw") == FeaturesVec.end())
  426. Features["prfchw"] = true;
  427. // Additionally, if SSE is enabled and mmx is not explicitly disabled,
  428. // then enable MMX.
  429. I = Features.find("sse");
  430. if (I != Features.end() && I->getValue() &&
  431. llvm::find(FeaturesVec, "-mmx") == FeaturesVec.end())
  432. Features["mmx"] = true;
  433. return true;
  434. }
  435. void X86TargetInfo::setSSELevel(llvm::StringMap<bool> &Features,
  436. X86SSEEnum Level, bool Enabled) {
  437. if (Enabled) {
  438. switch (Level) {
  439. case AVX512F:
  440. Features["avx512f"] = true;
  441. Features["fma"] = true;
  442. Features["f16c"] = true;
  443. LLVM_FALLTHROUGH;
  444. case AVX2:
  445. Features["avx2"] = true;
  446. LLVM_FALLTHROUGH;
  447. case AVX:
  448. Features["avx"] = true;
  449. Features["xsave"] = true;
  450. LLVM_FALLTHROUGH;
  451. case SSE42:
  452. Features["sse4.2"] = true;
  453. LLVM_FALLTHROUGH;
  454. case SSE41:
  455. Features["sse4.1"] = true;
  456. LLVM_FALLTHROUGH;
  457. case SSSE3:
  458. Features["ssse3"] = true;
  459. LLVM_FALLTHROUGH;
  460. case SSE3:
  461. Features["sse3"] = true;
  462. LLVM_FALLTHROUGH;
  463. case SSE2:
  464. Features["sse2"] = true;
  465. LLVM_FALLTHROUGH;
  466. case SSE1:
  467. Features["sse"] = true;
  468. LLVM_FALLTHROUGH;
  469. case NoSSE:
  470. break;
  471. }
  472. return;
  473. }
  474. switch (Level) {
  475. case NoSSE:
  476. case SSE1:
  477. Features["sse"] = false;
  478. LLVM_FALLTHROUGH;
  479. case SSE2:
  480. Features["sse2"] = Features["pclmul"] = Features["aes"] = false;
  481. Features["sha"] = Features["gfni"] = false;
  482. LLVM_FALLTHROUGH;
  483. case SSE3:
  484. Features["sse3"] = false;
  485. setXOPLevel(Features, NoXOP, false);
  486. LLVM_FALLTHROUGH;
  487. case SSSE3:
  488. Features["ssse3"] = false;
  489. LLVM_FALLTHROUGH;
  490. case SSE41:
  491. Features["sse4.1"] = false;
  492. LLVM_FALLTHROUGH;
  493. case SSE42:
  494. Features["sse4.2"] = false;
  495. LLVM_FALLTHROUGH;
  496. case AVX:
  497. Features["fma"] = Features["avx"] = Features["f16c"] = false;
  498. Features["xsave"] = Features["xsaveopt"] = Features["vaes"] = false;
  499. Features["vpclmulqdq"] = false;
  500. setXOPLevel(Features, FMA4, false);
  501. LLVM_FALLTHROUGH;
  502. case AVX2:
  503. Features["avx2"] = false;
  504. LLVM_FALLTHROUGH;
  505. case AVX512F:
  506. Features["avx512f"] = Features["avx512cd"] = Features["avx512er"] = false;
  507. Features["avx512pf"] = Features["avx512dq"] = Features["avx512bw"] = false;
  508. Features["avx512vl"] = Features["avx512vbmi"] = false;
  509. Features["avx512ifma"] = Features["avx512vpopcntdq"] = false;
  510. Features["avx512bitalg"] = Features["avx512vnni"] = false;
  511. Features["avx512vbmi2"] = Features["avx512bf16"] = false;
  512. Features["avx512vp2intersect"] = false;
  513. break;
  514. }
  515. }
  516. void X86TargetInfo::setMMXLevel(llvm::StringMap<bool> &Features,
  517. MMX3DNowEnum Level, bool Enabled) {
  518. if (Enabled) {
  519. switch (Level) {
  520. case AMD3DNowAthlon:
  521. Features["3dnowa"] = true;
  522. LLVM_FALLTHROUGH;
  523. case AMD3DNow:
  524. Features["3dnow"] = true;
  525. LLVM_FALLTHROUGH;
  526. case MMX:
  527. Features["mmx"] = true;
  528. LLVM_FALLTHROUGH;
  529. case NoMMX3DNow:
  530. break;
  531. }
  532. return;
  533. }
  534. switch (Level) {
  535. case NoMMX3DNow:
  536. case MMX:
  537. Features["mmx"] = false;
  538. LLVM_FALLTHROUGH;
  539. case AMD3DNow:
  540. Features["3dnow"] = false;
  541. LLVM_FALLTHROUGH;
  542. case AMD3DNowAthlon:
  543. Features["3dnowa"] = false;
  544. break;
  545. }
  546. }
  547. void X86TargetInfo::setXOPLevel(llvm::StringMap<bool> &Features, XOPEnum Level,
  548. bool Enabled) {
  549. if (Enabled) {
  550. switch (Level) {
  551. case XOP:
  552. Features["xop"] = true;
  553. LLVM_FALLTHROUGH;
  554. case FMA4:
  555. Features["fma4"] = true;
  556. setSSELevel(Features, AVX, true);
  557. LLVM_FALLTHROUGH;
  558. case SSE4A:
  559. Features["sse4a"] = true;
  560. setSSELevel(Features, SSE3, true);
  561. LLVM_FALLTHROUGH;
  562. case NoXOP:
  563. break;
  564. }
  565. return;
  566. }
  567. switch (Level) {
  568. case NoXOP:
  569. case SSE4A:
  570. Features["sse4a"] = false;
  571. LLVM_FALLTHROUGH;
  572. case FMA4:
  573. Features["fma4"] = false;
  574. LLVM_FALLTHROUGH;
  575. case XOP:
  576. Features["xop"] = false;
  577. break;
  578. }
  579. }
  580. void X86TargetInfo::setFeatureEnabledImpl(llvm::StringMap<bool> &Features,
  581. StringRef Name, bool Enabled) {
  582. // This is a bit of a hack to deal with the sse4 target feature when used
  583. // as part of the target attribute. We handle sse4 correctly everywhere
  584. // else. See below for more information on how we handle the sse4 options.
  585. if (Name != "sse4")
  586. Features[Name] = Enabled;
  587. if (Name == "mmx") {
  588. setMMXLevel(Features, MMX, Enabled);
  589. } else if (Name == "sse") {
  590. setSSELevel(Features, SSE1, Enabled);
  591. } else if (Name == "sse2") {
  592. setSSELevel(Features, SSE2, Enabled);
  593. } else if (Name == "sse3") {
  594. setSSELevel(Features, SSE3, Enabled);
  595. } else if (Name == "ssse3") {
  596. setSSELevel(Features, SSSE3, Enabled);
  597. } else if (Name == "sse4.2") {
  598. setSSELevel(Features, SSE42, Enabled);
  599. } else if (Name == "sse4.1") {
  600. setSSELevel(Features, SSE41, Enabled);
  601. } else if (Name == "3dnow") {
  602. setMMXLevel(Features, AMD3DNow, Enabled);
  603. } else if (Name == "3dnowa") {
  604. setMMXLevel(Features, AMD3DNowAthlon, Enabled);
  605. } else if (Name == "aes") {
  606. if (Enabled)
  607. setSSELevel(Features, SSE2, Enabled);
  608. else
  609. Features["vaes"] = false;
  610. } else if (Name == "vaes") {
  611. if (Enabled) {
  612. setSSELevel(Features, AVX, Enabled);
  613. Features["aes"] = true;
  614. }
  615. } else if (Name == "pclmul") {
  616. if (Enabled)
  617. setSSELevel(Features, SSE2, Enabled);
  618. else
  619. Features["vpclmulqdq"] = false;
  620. } else if (Name == "vpclmulqdq") {
  621. if (Enabled) {
  622. setSSELevel(Features, AVX, Enabled);
  623. Features["pclmul"] = true;
  624. }
  625. } else if (Name == "gfni") {
  626. if (Enabled)
  627. setSSELevel(Features, SSE2, Enabled);
  628. } else if (Name == "avx") {
  629. setSSELevel(Features, AVX, Enabled);
  630. } else if (Name == "avx2") {
  631. setSSELevel(Features, AVX2, Enabled);
  632. } else if (Name == "avx512f") {
  633. setSSELevel(Features, AVX512F, Enabled);
  634. } else if (Name.startswith("avx512")) {
  635. if (Enabled)
  636. setSSELevel(Features, AVX512F, Enabled);
  637. // Enable BWI instruction if certain features are being enabled.
  638. if ((Name == "avx512vbmi" || Name == "avx512vbmi2" ||
  639. Name == "avx512bitalg" || Name == "avx512bf16") && Enabled)
  640. Features["avx512bw"] = true;
  641. // Also disable some features if BWI is being disabled.
  642. if (Name == "avx512bw" && !Enabled) {
  643. Features["avx512vbmi"] = false;
  644. Features["avx512vbmi2"] = false;
  645. Features["avx512bitalg"] = false;
  646. Features["avx512bf16"] = false;
  647. }
  648. } else if (Name == "fma") {
  649. if (Enabled)
  650. setSSELevel(Features, AVX, Enabled);
  651. else
  652. setSSELevel(Features, AVX512F, Enabled);
  653. } else if (Name == "fma4") {
  654. setXOPLevel(Features, FMA4, Enabled);
  655. } else if (Name == "xop") {
  656. setXOPLevel(Features, XOP, Enabled);
  657. } else if (Name == "sse4a") {
  658. setXOPLevel(Features, SSE4A, Enabled);
  659. } else if (Name == "f16c") {
  660. if (Enabled)
  661. setSSELevel(Features, AVX, Enabled);
  662. else
  663. setSSELevel(Features, AVX512F, Enabled);
  664. } else if (Name == "sha") {
  665. if (Enabled)
  666. setSSELevel(Features, SSE2, Enabled);
  667. } else if (Name == "sse4") {
  668. // We can get here via the __target__ attribute since that's not controlled
  669. // via the -msse4/-mno-sse4 command line alias. Handle this the same way
  670. // here - turn on the sse4.2 if enabled, turn off the sse4.1 level if
  671. // disabled.
  672. if (Enabled)
  673. setSSELevel(Features, SSE42, Enabled);
  674. else
  675. setSSELevel(Features, SSE41, Enabled);
  676. } else if (Name == "xsave") {
  677. if (!Enabled)
  678. Features["xsaveopt"] = false;
  679. } else if (Name == "xsaveopt" || Name == "xsavec" || Name == "xsaves") {
  680. if (Enabled)
  681. Features["xsave"] = true;
  682. }
  683. }
  684. /// handleTargetFeatures - Perform initialization based on the user
  685. /// configured set of features.
  686. bool X86TargetInfo::handleTargetFeatures(std::vector<std::string> &Features,
  687. DiagnosticsEngine &Diags) {
  688. for (const auto &Feature : Features) {
  689. if (Feature[0] != '+')
  690. continue;
  691. if (Feature == "+aes") {
  692. HasAES = true;
  693. } else if (Feature == "+vaes") {
  694. HasVAES = true;
  695. } else if (Feature == "+pclmul") {
  696. HasPCLMUL = true;
  697. } else if (Feature == "+vpclmulqdq") {
  698. HasVPCLMULQDQ = true;
  699. } else if (Feature == "+lzcnt") {
  700. HasLZCNT = true;
  701. } else if (Feature == "+rdrnd") {
  702. HasRDRND = true;
  703. } else if (Feature == "+fsgsbase") {
  704. HasFSGSBASE = true;
  705. } else if (Feature == "+bmi") {
  706. HasBMI = true;
  707. } else if (Feature == "+bmi2") {
  708. HasBMI2 = true;
  709. } else if (Feature == "+popcnt") {
  710. HasPOPCNT = true;
  711. } else if (Feature == "+rtm") {
  712. HasRTM = true;
  713. } else if (Feature == "+prfchw") {
  714. HasPRFCHW = true;
  715. } else if (Feature == "+rdseed") {
  716. HasRDSEED = true;
  717. } else if (Feature == "+adx") {
  718. HasADX = true;
  719. } else if (Feature == "+tbm") {
  720. HasTBM = true;
  721. } else if (Feature == "+lwp") {
  722. HasLWP = true;
  723. } else if (Feature == "+fma") {
  724. HasFMA = true;
  725. } else if (Feature == "+f16c") {
  726. HasF16C = true;
  727. } else if (Feature == "+gfni") {
  728. HasGFNI = true;
  729. } else if (Feature == "+avx512cd") {
  730. HasAVX512CD = true;
  731. } else if (Feature == "+avx512vpopcntdq") {
  732. HasAVX512VPOPCNTDQ = true;
  733. } else if (Feature == "+avx512vnni") {
  734. HasAVX512VNNI = true;
  735. } else if (Feature == "+avx512bf16") {
  736. HasAVX512BF16 = true;
  737. } else if (Feature == "+avx512er") {
  738. HasAVX512ER = true;
  739. } else if (Feature == "+avx512pf") {
  740. HasAVX512PF = true;
  741. } else if (Feature == "+avx512dq") {
  742. HasAVX512DQ = true;
  743. } else if (Feature == "+avx512bitalg") {
  744. HasAVX512BITALG = true;
  745. } else if (Feature == "+avx512bw") {
  746. HasAVX512BW = true;
  747. } else if (Feature == "+avx512vl") {
  748. HasAVX512VL = true;
  749. } else if (Feature == "+avx512vbmi") {
  750. HasAVX512VBMI = true;
  751. } else if (Feature == "+avx512vbmi2") {
  752. HasAVX512VBMI2 = true;
  753. } else if (Feature == "+avx512ifma") {
  754. HasAVX512IFMA = true;
  755. } else if (Feature == "+avx512vp2intersect") {
  756. HasAVX512VP2INTERSECT = true;
  757. } else if (Feature == "+sha") {
  758. HasSHA = true;
  759. } else if (Feature == "+shstk") {
  760. HasSHSTK = true;
  761. } else if (Feature == "+movbe") {
  762. HasMOVBE = true;
  763. } else if (Feature == "+sgx") {
  764. HasSGX = true;
  765. } else if (Feature == "+cx8") {
  766. HasCX8 = true;
  767. } else if (Feature == "+cx16") {
  768. HasCX16 = true;
  769. } else if (Feature == "+fxsr") {
  770. HasFXSR = true;
  771. } else if (Feature == "+xsave") {
  772. HasXSAVE = true;
  773. } else if (Feature == "+xsaveopt") {
  774. HasXSAVEOPT = true;
  775. } else if (Feature == "+xsavec") {
  776. HasXSAVEC = true;
  777. } else if (Feature == "+xsaves") {
  778. HasXSAVES = true;
  779. } else if (Feature == "+mwaitx") {
  780. HasMWAITX = true;
  781. } else if (Feature == "+pku") {
  782. HasPKU = true;
  783. } else if (Feature == "+clflushopt") {
  784. HasCLFLUSHOPT = true;
  785. } else if (Feature == "+clwb") {
  786. HasCLWB = true;
  787. } else if (Feature == "+wbnoinvd") {
  788. HasWBNOINVD = true;
  789. } else if (Feature == "+prefetchwt1") {
  790. HasPREFETCHWT1 = true;
  791. } else if (Feature == "+clzero") {
  792. HasCLZERO = true;
  793. } else if (Feature == "+cldemote") {
  794. HasCLDEMOTE = true;
  795. } else if (Feature == "+rdpid") {
  796. HasRDPID = true;
  797. } else if (Feature == "+retpoline-external-thunk") {
  798. HasRetpolineExternalThunk = true;
  799. } else if (Feature == "+sahf") {
  800. HasLAHFSAHF = true;
  801. } else if (Feature == "+waitpkg") {
  802. HasWAITPKG = true;
  803. } else if (Feature == "+movdiri") {
  804. HasMOVDIRI = true;
  805. } else if (Feature == "+movdir64b") {
  806. HasMOVDIR64B = true;
  807. } else if (Feature == "+pconfig") {
  808. HasPCONFIG = true;
  809. } else if (Feature == "+ptwrite") {
  810. HasPTWRITE = true;
  811. } else if (Feature == "+invpcid") {
  812. HasINVPCID = true;
  813. } else if (Feature == "+enqcmd") {
  814. HasENQCMD = true;
  815. }
  816. X86SSEEnum Level = llvm::StringSwitch<X86SSEEnum>(Feature)
  817. .Case("+avx512f", AVX512F)
  818. .Case("+avx2", AVX2)
  819. .Case("+avx", AVX)
  820. .Case("+sse4.2", SSE42)
  821. .Case("+sse4.1", SSE41)
  822. .Case("+ssse3", SSSE3)
  823. .Case("+sse3", SSE3)
  824. .Case("+sse2", SSE2)
  825. .Case("+sse", SSE1)
  826. .Default(NoSSE);
  827. SSELevel = std::max(SSELevel, Level);
  828. MMX3DNowEnum ThreeDNowLevel = llvm::StringSwitch<MMX3DNowEnum>(Feature)
  829. .Case("+3dnowa", AMD3DNowAthlon)
  830. .Case("+3dnow", AMD3DNow)
  831. .Case("+mmx", MMX)
  832. .Default(NoMMX3DNow);
  833. MMX3DNowLevel = std::max(MMX3DNowLevel, ThreeDNowLevel);
  834. XOPEnum XLevel = llvm::StringSwitch<XOPEnum>(Feature)
  835. .Case("+xop", XOP)
  836. .Case("+fma4", FMA4)
  837. .Case("+sse4a", SSE4A)
  838. .Default(NoXOP);
  839. XOPLevel = std::max(XOPLevel, XLevel);
  840. }
  841. // LLVM doesn't have a separate switch for fpmath, so only accept it if it
  842. // matches the selected sse level.
  843. if ((FPMath == FP_SSE && SSELevel < SSE1) ||
  844. (FPMath == FP_387 && SSELevel >= SSE1)) {
  845. Diags.Report(diag::err_target_unsupported_fpmath)
  846. << (FPMath == FP_SSE ? "sse" : "387");
  847. return false;
  848. }
  849. SimdDefaultAlign =
  850. hasFeature("avx512f") ? 512 : hasFeature("avx") ? 256 : 128;
  851. return true;
  852. }
  853. /// X86TargetInfo::getTargetDefines - Return the set of the X86-specific macro
  854. /// definitions for this particular subtarget.
  855. void X86TargetInfo::getTargetDefines(const LangOptions &Opts,
  856. MacroBuilder &Builder) const {
  857. // Inline assembly supports X86 flag outputs.
  858. Builder.defineMacro("__GCC_ASM_FLAG_OUTPUTS__");
  859. std::string CodeModel = getTargetOpts().CodeModel;
  860. if (CodeModel == "default")
  861. CodeModel = "small";
  862. Builder.defineMacro("__code_model_" + CodeModel + "_");
  863. // Target identification.
  864. if (getTriple().getArch() == llvm::Triple::x86_64) {
  865. Builder.defineMacro("__amd64__");
  866. Builder.defineMacro("__amd64");
  867. Builder.defineMacro("__x86_64");
  868. Builder.defineMacro("__x86_64__");
  869. if (getTriple().getArchName() == "x86_64h") {
  870. Builder.defineMacro("__x86_64h");
  871. Builder.defineMacro("__x86_64h__");
  872. }
  873. } else {
  874. DefineStd(Builder, "i386", Opts);
  875. }
  876. Builder.defineMacro("__SEG_GS");
  877. Builder.defineMacro("__SEG_FS");
  878. Builder.defineMacro("__seg_gs", "__attribute__((address_space(256)))");
  879. Builder.defineMacro("__seg_fs", "__attribute__((address_space(257)))");
  880. // Subtarget options.
  881. // FIXME: We are hard-coding the tune parameters based on the CPU, but they
  882. // truly should be based on -mtune options.
  883. switch (CPU) {
  884. case CK_Generic:
  885. break;
  886. case CK_i386:
  887. // The rest are coming from the i386 define above.
  888. Builder.defineMacro("__tune_i386__");
  889. break;
  890. case CK_i486:
  891. case CK_WinChipC6:
  892. case CK_WinChip2:
  893. case CK_C3:
  894. defineCPUMacros(Builder, "i486");
  895. break;
  896. case CK_PentiumMMX:
  897. Builder.defineMacro("__pentium_mmx__");
  898. Builder.defineMacro("__tune_pentium_mmx__");
  899. LLVM_FALLTHROUGH;
  900. case CK_i586:
  901. case CK_Pentium:
  902. defineCPUMacros(Builder, "i586");
  903. defineCPUMacros(Builder, "pentium");
  904. break;
  905. case CK_Pentium3:
  906. case CK_PentiumM:
  907. Builder.defineMacro("__tune_pentium3__");
  908. LLVM_FALLTHROUGH;
  909. case CK_Pentium2:
  910. case CK_C3_2:
  911. Builder.defineMacro("__tune_pentium2__");
  912. LLVM_FALLTHROUGH;
  913. case CK_PentiumPro:
  914. case CK_i686:
  915. defineCPUMacros(Builder, "i686");
  916. defineCPUMacros(Builder, "pentiumpro");
  917. break;
  918. case CK_Pentium4:
  919. defineCPUMacros(Builder, "pentium4");
  920. break;
  921. case CK_Yonah:
  922. case CK_Prescott:
  923. case CK_Nocona:
  924. defineCPUMacros(Builder, "nocona");
  925. break;
  926. case CK_Core2:
  927. case CK_Penryn:
  928. defineCPUMacros(Builder, "core2");
  929. break;
  930. case CK_Bonnell:
  931. defineCPUMacros(Builder, "atom");
  932. break;
  933. case CK_Silvermont:
  934. defineCPUMacros(Builder, "slm");
  935. break;
  936. case CK_Goldmont:
  937. defineCPUMacros(Builder, "goldmont");
  938. break;
  939. case CK_GoldmontPlus:
  940. defineCPUMacros(Builder, "goldmont_plus");
  941. break;
  942. case CK_Tremont:
  943. defineCPUMacros(Builder, "tremont");
  944. break;
  945. case CK_Nehalem:
  946. case CK_Westmere:
  947. case CK_SandyBridge:
  948. case CK_IvyBridge:
  949. case CK_Haswell:
  950. case CK_Broadwell:
  951. case CK_SkylakeClient:
  952. case CK_SkylakeServer:
  953. case CK_Cascadelake:
  954. case CK_Cooperlake:
  955. case CK_Cannonlake:
  956. case CK_IcelakeClient:
  957. case CK_IcelakeServer:
  958. case CK_Tigerlake:
  959. // FIXME: Historically, we defined this legacy name, it would be nice to
  960. // remove it at some point. We've never exposed fine-grained names for
  961. // recent primary x86 CPUs, and we should keep it that way.
  962. defineCPUMacros(Builder, "corei7");
  963. break;
  964. case CK_KNL:
  965. defineCPUMacros(Builder, "knl");
  966. break;
  967. case CK_KNM:
  968. break;
  969. case CK_Lakemont:
  970. defineCPUMacros(Builder, "i586", /*Tuning*/false);
  971. defineCPUMacros(Builder, "pentium", /*Tuning*/false);
  972. Builder.defineMacro("__tune_lakemont__");
  973. break;
  974. case CK_K6_2:
  975. Builder.defineMacro("__k6_2__");
  976. Builder.defineMacro("__tune_k6_2__");
  977. LLVM_FALLTHROUGH;
  978. case CK_K6_3:
  979. if (CPU != CK_K6_2) { // In case of fallthrough
  980. // FIXME: GCC may be enabling these in cases where some other k6
  981. // architecture is specified but -m3dnow is explicitly provided. The
  982. // exact semantics need to be determined and emulated here.
  983. Builder.defineMacro("__k6_3__");
  984. Builder.defineMacro("__tune_k6_3__");
  985. }
  986. LLVM_FALLTHROUGH;
  987. case CK_K6:
  988. defineCPUMacros(Builder, "k6");
  989. break;
  990. case CK_Athlon:
  991. case CK_AthlonXP:
  992. defineCPUMacros(Builder, "athlon");
  993. if (SSELevel != NoSSE) {
  994. Builder.defineMacro("__athlon_sse__");
  995. Builder.defineMacro("__tune_athlon_sse__");
  996. }
  997. break;
  998. case CK_K8:
  999. case CK_K8SSE3:
  1000. case CK_x86_64:
  1001. defineCPUMacros(Builder, "k8");
  1002. break;
  1003. case CK_AMDFAM10:
  1004. defineCPUMacros(Builder, "amdfam10");
  1005. break;
  1006. case CK_BTVER1:
  1007. defineCPUMacros(Builder, "btver1");
  1008. break;
  1009. case CK_BTVER2:
  1010. defineCPUMacros(Builder, "btver2");
  1011. break;
  1012. case CK_BDVER1:
  1013. defineCPUMacros(Builder, "bdver1");
  1014. break;
  1015. case CK_BDVER2:
  1016. defineCPUMacros(Builder, "bdver2");
  1017. break;
  1018. case CK_BDVER3:
  1019. defineCPUMacros(Builder, "bdver3");
  1020. break;
  1021. case CK_BDVER4:
  1022. defineCPUMacros(Builder, "bdver4");
  1023. break;
  1024. case CK_ZNVER1:
  1025. defineCPUMacros(Builder, "znver1");
  1026. break;
  1027. case CK_ZNVER2:
  1028. defineCPUMacros(Builder, "znver2");
  1029. break;
  1030. case CK_Geode:
  1031. defineCPUMacros(Builder, "geode");
  1032. break;
  1033. }
  1034. // Target properties.
  1035. Builder.defineMacro("__REGISTER_PREFIX__", "");
  1036. // Define __NO_MATH_INLINES on linux/x86 so that we don't get inline
  1037. // functions in glibc header files that use FP Stack inline asm which the
  1038. // backend can't deal with (PR879).
  1039. Builder.defineMacro("__NO_MATH_INLINES");
  1040. if (HasAES)
  1041. Builder.defineMacro("__AES__");
  1042. if (HasVAES)
  1043. Builder.defineMacro("__VAES__");
  1044. if (HasPCLMUL)
  1045. Builder.defineMacro("__PCLMUL__");
  1046. if (HasVPCLMULQDQ)
  1047. Builder.defineMacro("__VPCLMULQDQ__");
  1048. if (HasLZCNT)
  1049. Builder.defineMacro("__LZCNT__");
  1050. if (HasRDRND)
  1051. Builder.defineMacro("__RDRND__");
  1052. if (HasFSGSBASE)
  1053. Builder.defineMacro("__FSGSBASE__");
  1054. if (HasBMI)
  1055. Builder.defineMacro("__BMI__");
  1056. if (HasBMI2)
  1057. Builder.defineMacro("__BMI2__");
  1058. if (HasPOPCNT)
  1059. Builder.defineMacro("__POPCNT__");
  1060. if (HasRTM)
  1061. Builder.defineMacro("__RTM__");
  1062. if (HasPRFCHW)
  1063. Builder.defineMacro("__PRFCHW__");
  1064. if (HasRDSEED)
  1065. Builder.defineMacro("__RDSEED__");
  1066. if (HasADX)
  1067. Builder.defineMacro("__ADX__");
  1068. if (HasTBM)
  1069. Builder.defineMacro("__TBM__");
  1070. if (HasLWP)
  1071. Builder.defineMacro("__LWP__");
  1072. if (HasMWAITX)
  1073. Builder.defineMacro("__MWAITX__");
  1074. if (HasMOVBE)
  1075. Builder.defineMacro("__MOVBE__");
  1076. switch (XOPLevel) {
  1077. case XOP:
  1078. Builder.defineMacro("__XOP__");
  1079. LLVM_FALLTHROUGH;
  1080. case FMA4:
  1081. Builder.defineMacro("__FMA4__");
  1082. LLVM_FALLTHROUGH;
  1083. case SSE4A:
  1084. Builder.defineMacro("__SSE4A__");
  1085. LLVM_FALLTHROUGH;
  1086. case NoXOP:
  1087. break;
  1088. }
  1089. if (HasFMA)
  1090. Builder.defineMacro("__FMA__");
  1091. if (HasF16C)
  1092. Builder.defineMacro("__F16C__");
  1093. if (HasGFNI)
  1094. Builder.defineMacro("__GFNI__");
  1095. if (HasAVX512CD)
  1096. Builder.defineMacro("__AVX512CD__");
  1097. if (HasAVX512VPOPCNTDQ)
  1098. Builder.defineMacro("__AVX512VPOPCNTDQ__");
  1099. if (HasAVX512VNNI)
  1100. Builder.defineMacro("__AVX512VNNI__");
  1101. if (HasAVX512BF16)
  1102. Builder.defineMacro("__AVX512BF16__");
  1103. if (HasAVX512ER)
  1104. Builder.defineMacro("__AVX512ER__");
  1105. if (HasAVX512PF)
  1106. Builder.defineMacro("__AVX512PF__");
  1107. if (HasAVX512DQ)
  1108. Builder.defineMacro("__AVX512DQ__");
  1109. if (HasAVX512BITALG)
  1110. Builder.defineMacro("__AVX512BITALG__");
  1111. if (HasAVX512BW)
  1112. Builder.defineMacro("__AVX512BW__");
  1113. if (HasAVX512VL)
  1114. Builder.defineMacro("__AVX512VL__");
  1115. if (HasAVX512VBMI)
  1116. Builder.defineMacro("__AVX512VBMI__");
  1117. if (HasAVX512VBMI2)
  1118. Builder.defineMacro("__AVX512VBMI2__");
  1119. if (HasAVX512IFMA)
  1120. Builder.defineMacro("__AVX512IFMA__");
  1121. if (HasAVX512VP2INTERSECT)
  1122. Builder.defineMacro("__AVX512VP2INTERSECT__");
  1123. if (HasSHA)
  1124. Builder.defineMacro("__SHA__");
  1125. if (HasFXSR)
  1126. Builder.defineMacro("__FXSR__");
  1127. if (HasXSAVE)
  1128. Builder.defineMacro("__XSAVE__");
  1129. if (HasXSAVEOPT)
  1130. Builder.defineMacro("__XSAVEOPT__");
  1131. if (HasXSAVEC)
  1132. Builder.defineMacro("__XSAVEC__");
  1133. if (HasXSAVES)
  1134. Builder.defineMacro("__XSAVES__");
  1135. if (HasPKU)
  1136. Builder.defineMacro("__PKU__");
  1137. if (HasCLFLUSHOPT)
  1138. Builder.defineMacro("__CLFLUSHOPT__");
  1139. if (HasCLWB)
  1140. Builder.defineMacro("__CLWB__");
  1141. if (HasWBNOINVD)
  1142. Builder.defineMacro("__WBNOINVD__");
  1143. if (HasSHSTK)
  1144. Builder.defineMacro("__SHSTK__");
  1145. if (HasSGX)
  1146. Builder.defineMacro("__SGX__");
  1147. if (HasPREFETCHWT1)
  1148. Builder.defineMacro("__PREFETCHWT1__");
  1149. if (HasCLZERO)
  1150. Builder.defineMacro("__CLZERO__");
  1151. if (HasRDPID)
  1152. Builder.defineMacro("__RDPID__");
  1153. if (HasCLDEMOTE)
  1154. Builder.defineMacro("__CLDEMOTE__");
  1155. if (HasWAITPKG)
  1156. Builder.defineMacro("__WAITPKG__");
  1157. if (HasMOVDIRI)
  1158. Builder.defineMacro("__MOVDIRI__");
  1159. if (HasMOVDIR64B)
  1160. Builder.defineMacro("__MOVDIR64B__");
  1161. if (HasPCONFIG)
  1162. Builder.defineMacro("__PCONFIG__");
  1163. if (HasPTWRITE)
  1164. Builder.defineMacro("__PTWRITE__");
  1165. if (HasINVPCID)
  1166. Builder.defineMacro("__INVPCID__");
  1167. if (HasENQCMD)
  1168. Builder.defineMacro("__ENQCMD__");
  1169. // Each case falls through to the previous one here.
  1170. switch (SSELevel) {
  1171. case AVX512F:
  1172. Builder.defineMacro("__AVX512F__");
  1173. LLVM_FALLTHROUGH;
  1174. case AVX2:
  1175. Builder.defineMacro("__AVX2__");
  1176. LLVM_FALLTHROUGH;
  1177. case AVX:
  1178. Builder.defineMacro("__AVX__");
  1179. LLVM_FALLTHROUGH;
  1180. case SSE42:
  1181. Builder.defineMacro("__SSE4_2__");
  1182. LLVM_FALLTHROUGH;
  1183. case SSE41:
  1184. Builder.defineMacro("__SSE4_1__");
  1185. LLVM_FALLTHROUGH;
  1186. case SSSE3:
  1187. Builder.defineMacro("__SSSE3__");
  1188. LLVM_FALLTHROUGH;
  1189. case SSE3:
  1190. Builder.defineMacro("__SSE3__");
  1191. LLVM_FALLTHROUGH;
  1192. case SSE2:
  1193. Builder.defineMacro("__SSE2__");
  1194. Builder.defineMacro("__SSE2_MATH__"); // -mfp-math=sse always implied.
  1195. LLVM_FALLTHROUGH;
  1196. case SSE1:
  1197. Builder.defineMacro("__SSE__");
  1198. Builder.defineMacro("__SSE_MATH__"); // -mfp-math=sse always implied.
  1199. LLVM_FALLTHROUGH;
  1200. case NoSSE:
  1201. break;
  1202. }
  1203. if (Opts.MicrosoftExt && getTriple().getArch() == llvm::Triple::x86) {
  1204. switch (SSELevel) {
  1205. case AVX512F:
  1206. case AVX2:
  1207. case AVX:
  1208. case SSE42:
  1209. case SSE41:
  1210. case SSSE3:
  1211. case SSE3:
  1212. case SSE2:
  1213. Builder.defineMacro("_M_IX86_FP", Twine(2));
  1214. break;
  1215. case SSE1:
  1216. Builder.defineMacro("_M_IX86_FP", Twine(1));
  1217. break;
  1218. default:
  1219. Builder.defineMacro("_M_IX86_FP", Twine(0));
  1220. break;
  1221. }
  1222. }
  1223. // Each case falls through to the previous one here.
  1224. switch (MMX3DNowLevel) {
  1225. case AMD3DNowAthlon:
  1226. Builder.defineMacro("__3dNOW_A__");
  1227. LLVM_FALLTHROUGH;
  1228. case AMD3DNow:
  1229. Builder.defineMacro("__3dNOW__");
  1230. LLVM_FALLTHROUGH;
  1231. case MMX:
  1232. Builder.defineMacro("__MMX__");
  1233. LLVM_FALLTHROUGH;
  1234. case NoMMX3DNow:
  1235. break;
  1236. }
  1237. if (CPU >= CK_i486 || CPU == CK_Generic) {
  1238. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
  1239. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
  1240. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
  1241. }
  1242. if (HasCX8)
  1243. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
  1244. if (HasCX16 && getTriple().getArch() == llvm::Triple::x86_64)
  1245. Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_16");
  1246. if (HasFloat128)
  1247. Builder.defineMacro("__SIZEOF_FLOAT128__", "16");
  1248. }
  1249. bool X86TargetInfo::isValidFeatureName(StringRef Name) const {
  1250. return llvm::StringSwitch<bool>(Name)
  1251. .Case("3dnow", true)
  1252. .Case("3dnowa", true)
  1253. .Case("adx", true)
  1254. .Case("aes", true)
  1255. .Case("avx", true)
  1256. .Case("avx2", true)
  1257. .Case("avx512f", true)
  1258. .Case("avx512cd", true)
  1259. .Case("avx512vpopcntdq", true)
  1260. .Case("avx512vnni", true)
  1261. .Case("avx512bf16", true)
  1262. .Case("avx512er", true)
  1263. .Case("avx512pf", true)
  1264. .Case("avx512dq", true)
  1265. .Case("avx512bitalg", true)
  1266. .Case("avx512bw", true)
  1267. .Case("avx512vl", true)
  1268. .Case("avx512vbmi", true)
  1269. .Case("avx512vbmi2", true)
  1270. .Case("avx512ifma", true)
  1271. .Case("avx512vp2intersect", true)
  1272. .Case("bmi", true)
  1273. .Case("bmi2", true)
  1274. .Case("cldemote", true)
  1275. .Case("clflushopt", true)
  1276. .Case("clwb", true)
  1277. .Case("clzero", true)
  1278. .Case("cx16", true)
  1279. .Case("enqcmd", true)
  1280. .Case("f16c", true)
  1281. .Case("fma", true)
  1282. .Case("fma4", true)
  1283. .Case("fsgsbase", true)
  1284. .Case("fxsr", true)
  1285. .Case("gfni", true)
  1286. .Case("invpcid", true)
  1287. .Case("lwp", true)
  1288. .Case("lzcnt", true)
  1289. .Case("mmx", true)
  1290. .Case("movbe", true)
  1291. .Case("movdiri", true)
  1292. .Case("movdir64b", true)
  1293. .Case("mwaitx", true)
  1294. .Case("pclmul", true)
  1295. .Case("pconfig", true)
  1296. .Case("pku", true)
  1297. .Case("popcnt", true)
  1298. .Case("prefetchwt1", true)
  1299. .Case("prfchw", true)
  1300. .Case("ptwrite", true)
  1301. .Case("rdpid", true)
  1302. .Case("rdrnd", true)
  1303. .Case("rdseed", true)
  1304. .Case("rtm", true)
  1305. .Case("sahf", true)
  1306. .Case("sgx", true)
  1307. .Case("sha", true)
  1308. .Case("shstk", true)
  1309. .Case("sse", true)
  1310. .Case("sse2", true)
  1311. .Case("sse3", true)
  1312. .Case("ssse3", true)
  1313. .Case("sse4", true)
  1314. .Case("sse4.1", true)
  1315. .Case("sse4.2", true)
  1316. .Case("sse4a", true)
  1317. .Case("tbm", true)
  1318. .Case("vaes", true)
  1319. .Case("vpclmulqdq", true)
  1320. .Case("wbnoinvd", true)
  1321. .Case("waitpkg", true)
  1322. .Case("x87", true)
  1323. .Case("xop", true)
  1324. .Case("xsave", true)
  1325. .Case("xsavec", true)
  1326. .Case("xsaves", true)
  1327. .Case("xsaveopt", true)
  1328. .Default(false);
  1329. }
  1330. bool X86TargetInfo::hasFeature(StringRef Feature) const {
  1331. return llvm::StringSwitch<bool>(Feature)
  1332. .Case("adx", HasADX)
  1333. .Case("aes", HasAES)
  1334. .Case("avx", SSELevel >= AVX)
  1335. .Case("avx2", SSELevel >= AVX2)
  1336. .Case("avx512f", SSELevel >= AVX512F)
  1337. .Case("avx512cd", HasAVX512CD)
  1338. .Case("avx512vpopcntdq", HasAVX512VPOPCNTDQ)
  1339. .Case("avx512vnni", HasAVX512VNNI)
  1340. .Case("avx512bf16", HasAVX512BF16)
  1341. .Case("avx512er", HasAVX512ER)
  1342. .Case("avx512pf", HasAVX512PF)
  1343. .Case("avx512dq", HasAVX512DQ)
  1344. .Case("avx512bitalg", HasAVX512BITALG)
  1345. .Case("avx512bw", HasAVX512BW)
  1346. .Case("avx512vl", HasAVX512VL)
  1347. .Case("avx512vbmi", HasAVX512VBMI)
  1348. .Case("avx512vbmi2", HasAVX512VBMI2)
  1349. .Case("avx512ifma", HasAVX512IFMA)
  1350. .Case("avx512vp2intersect", HasAVX512VP2INTERSECT)
  1351. .Case("bmi", HasBMI)
  1352. .Case("bmi2", HasBMI2)
  1353. .Case("cldemote", HasCLDEMOTE)
  1354. .Case("clflushopt", HasCLFLUSHOPT)
  1355. .Case("clwb", HasCLWB)
  1356. .Case("clzero", HasCLZERO)
  1357. .Case("cx8", HasCX8)
  1358. .Case("cx16", HasCX16)
  1359. .Case("enqcmd", HasENQCMD)
  1360. .Case("f16c", HasF16C)
  1361. .Case("fma", HasFMA)
  1362. .Case("fma4", XOPLevel >= FMA4)
  1363. .Case("fsgsbase", HasFSGSBASE)
  1364. .Case("fxsr", HasFXSR)
  1365. .Case("gfni", HasGFNI)
  1366. .Case("invpcid", HasINVPCID)
  1367. .Case("lwp", HasLWP)
  1368. .Case("lzcnt", HasLZCNT)
  1369. .Case("mm3dnow", MMX3DNowLevel >= AMD3DNow)
  1370. .Case("mm3dnowa", MMX3DNowLevel >= AMD3DNowAthlon)
  1371. .Case("mmx", MMX3DNowLevel >= MMX)
  1372. .Case("movbe", HasMOVBE)
  1373. .Case("movdiri", HasMOVDIRI)
  1374. .Case("movdir64b", HasMOVDIR64B)
  1375. .Case("mwaitx", HasMWAITX)
  1376. .Case("pclmul", HasPCLMUL)
  1377. .Case("pconfig", HasPCONFIG)
  1378. .Case("pku", HasPKU)
  1379. .Case("popcnt", HasPOPCNT)
  1380. .Case("prefetchwt1", HasPREFETCHWT1)
  1381. .Case("prfchw", HasPRFCHW)
  1382. .Case("ptwrite", HasPTWRITE)
  1383. .Case("rdpid", HasRDPID)
  1384. .Case("rdrnd", HasRDRND)
  1385. .Case("rdseed", HasRDSEED)
  1386. .Case("retpoline-external-thunk", HasRetpolineExternalThunk)
  1387. .Case("rtm", HasRTM)
  1388. .Case("sahf", HasLAHFSAHF)
  1389. .Case("sgx", HasSGX)
  1390. .Case("sha", HasSHA)
  1391. .Case("shstk", HasSHSTK)
  1392. .Case("sse", SSELevel >= SSE1)
  1393. .Case("sse2", SSELevel >= SSE2)
  1394. .Case("sse3", SSELevel >= SSE3)
  1395. .Case("ssse3", SSELevel >= SSSE3)
  1396. .Case("sse4.1", SSELevel >= SSE41)
  1397. .Case("sse4.2", SSELevel >= SSE42)
  1398. .Case("sse4a", XOPLevel >= SSE4A)
  1399. .Case("tbm", HasTBM)
  1400. .Case("vaes", HasVAES)
  1401. .Case("vpclmulqdq", HasVPCLMULQDQ)
  1402. .Case("wbnoinvd", HasWBNOINVD)
  1403. .Case("waitpkg", HasWAITPKG)
  1404. .Case("x86", true)
  1405. .Case("x86_32", getTriple().getArch() == llvm::Triple::x86)
  1406. .Case("x86_64", getTriple().getArch() == llvm::Triple::x86_64)
  1407. .Case("xop", XOPLevel >= XOP)
  1408. .Case("xsave", HasXSAVE)
  1409. .Case("xsavec", HasXSAVEC)
  1410. .Case("xsaves", HasXSAVES)
  1411. .Case("xsaveopt", HasXSAVEOPT)
  1412. .Default(false);
  1413. }
  1414. // We can't use a generic validation scheme for the features accepted here
  1415. // versus subtarget features accepted in the target attribute because the
  1416. // bitfield structure that's initialized in the runtime only supports the
  1417. // below currently rather than the full range of subtarget features. (See
  1418. // X86TargetInfo::hasFeature for a somewhat comprehensive list).
  1419. bool X86TargetInfo::validateCpuSupports(StringRef FeatureStr) const {
  1420. return llvm::StringSwitch<bool>(FeatureStr)
  1421. #define X86_FEATURE_COMPAT(VAL, ENUM, STR) .Case(STR, true)
  1422. #include "llvm/Support/X86TargetParser.def"
  1423. .Default(false);
  1424. }
  1425. static llvm::X86::ProcessorFeatures getFeature(StringRef Name) {
  1426. return llvm::StringSwitch<llvm::X86::ProcessorFeatures>(Name)
  1427. #define X86_FEATURE_COMPAT(VAL, ENUM, STR) .Case(STR, llvm::X86::ENUM)
  1428. #include "llvm/Support/X86TargetParser.def"
  1429. ;
  1430. // Note, this function should only be used after ensuring the value is
  1431. // correct, so it asserts if the value is out of range.
  1432. }
  1433. static unsigned getFeaturePriority(llvm::X86::ProcessorFeatures Feat) {
  1434. enum class FeatPriority {
  1435. #define FEATURE(FEAT) FEAT,
  1436. #include "clang/Basic/X86Target.def"
  1437. };
  1438. switch (Feat) {
  1439. #define FEATURE(FEAT) \
  1440. case llvm::X86::FEAT: \
  1441. return static_cast<unsigned>(FeatPriority::FEAT);
  1442. #include "clang/Basic/X86Target.def"
  1443. default:
  1444. llvm_unreachable("No Feature Priority for non-CPUSupports Features");
  1445. }
  1446. }
  1447. unsigned X86TargetInfo::multiVersionSortPriority(StringRef Name) const {
  1448. // Valid CPUs have a 'key feature' that compares just better than its key
  1449. // feature.
  1450. CPUKind Kind = getCPUKind(Name);
  1451. if (Kind != CK_Generic) {
  1452. switch (Kind) {
  1453. default:
  1454. llvm_unreachable(
  1455. "CPU Type without a key feature used in 'target' attribute");
  1456. #define PROC_WITH_FEAT(ENUM, STR, IS64, KEY_FEAT) \
  1457. case CK_##ENUM: \
  1458. return (getFeaturePriority(llvm::X86::KEY_FEAT) << 1) + 1;
  1459. #include "clang/Basic/X86Target.def"
  1460. }
  1461. }
  1462. // Now we know we have a feature, so get its priority and shift it a few so
  1463. // that we have sufficient room for the CPUs (above).
  1464. return getFeaturePriority(getFeature(Name)) << 1;
  1465. }
  1466. bool X86TargetInfo::validateCPUSpecificCPUDispatch(StringRef Name) const {
  1467. return llvm::StringSwitch<bool>(Name)
  1468. #define CPU_SPECIFIC(NAME, MANGLING, FEATURES) .Case(NAME, true)
  1469. #define CPU_SPECIFIC_ALIAS(NEW_NAME, NAME) .Case(NEW_NAME, true)
  1470. #include "clang/Basic/X86Target.def"
  1471. .Default(false);
  1472. }
  1473. static StringRef CPUSpecificCPUDispatchNameDealias(StringRef Name) {
  1474. return llvm::StringSwitch<StringRef>(Name)
  1475. #define CPU_SPECIFIC_ALIAS(NEW_NAME, NAME) .Case(NEW_NAME, NAME)
  1476. #include "clang/Basic/X86Target.def"
  1477. .Default(Name);
  1478. }
  1479. char X86TargetInfo::CPUSpecificManglingCharacter(StringRef Name) const {
  1480. return llvm::StringSwitch<char>(CPUSpecificCPUDispatchNameDealias(Name))
  1481. #define CPU_SPECIFIC(NAME, MANGLING, FEATURES) .Case(NAME, MANGLING)
  1482. #include "clang/Basic/X86Target.def"
  1483. .Default(0);
  1484. }
  1485. void X86TargetInfo::getCPUSpecificCPUDispatchFeatures(
  1486. StringRef Name, llvm::SmallVectorImpl<StringRef> &Features) const {
  1487. StringRef WholeList =
  1488. llvm::StringSwitch<StringRef>(CPUSpecificCPUDispatchNameDealias(Name))
  1489. #define CPU_SPECIFIC(NAME, MANGLING, FEATURES) .Case(NAME, FEATURES)
  1490. #include "clang/Basic/X86Target.def"
  1491. .Default("");
  1492. WholeList.split(Features, ',', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
  1493. }
  1494. // We can't use a generic validation scheme for the cpus accepted here
  1495. // versus subtarget cpus accepted in the target attribute because the
  1496. // variables intitialized by the runtime only support the below currently
  1497. // rather than the full range of cpus.
  1498. bool X86TargetInfo::validateCpuIs(StringRef FeatureStr) const {
  1499. return llvm::StringSwitch<bool>(FeatureStr)
  1500. #define X86_VENDOR(ENUM, STRING) .Case(STRING, true)
  1501. #define X86_CPU_TYPE_COMPAT_WITH_ALIAS(ARCHNAME, ENUM, STR, ALIAS) \
  1502. .Cases(STR, ALIAS, true)
  1503. #define X86_CPU_TYPE_COMPAT(ARCHNAME, ENUM, STR) .Case(STR, true)
  1504. #define X86_CPU_SUBTYPE_COMPAT(ARCHNAME, ENUM, STR) .Case(STR, true)
  1505. #include "llvm/Support/X86TargetParser.def"
  1506. .Default(false);
  1507. }
  1508. static unsigned matchAsmCCConstraint(const char *&Name) {
  1509. auto RV = llvm::StringSwitch<unsigned>(Name)
  1510. .Case("@cca", 4)
  1511. .Case("@ccae", 5)
  1512. .Case("@ccb", 4)
  1513. .Case("@ccbe", 5)
  1514. .Case("@ccc", 4)
  1515. .Case("@cce", 4)
  1516. .Case("@ccz", 4)
  1517. .Case("@ccg", 4)
  1518. .Case("@ccge", 5)
  1519. .Case("@ccl", 4)
  1520. .Case("@ccle", 5)
  1521. .Case("@ccna", 5)
  1522. .Case("@ccnae", 6)
  1523. .Case("@ccnb", 5)
  1524. .Case("@ccnbe", 6)
  1525. .Case("@ccnc", 5)
  1526. .Case("@ccne", 5)
  1527. .Case("@ccnz", 5)
  1528. .Case("@ccng", 5)
  1529. .Case("@ccnge", 6)
  1530. .Case("@ccnl", 5)
  1531. .Case("@ccnle", 6)
  1532. .Case("@ccno", 5)
  1533. .Case("@ccnp", 5)
  1534. .Case("@ccns", 5)
  1535. .Case("@cco", 4)
  1536. .Case("@ccp", 4)
  1537. .Case("@ccs", 4)
  1538. .Default(0);
  1539. return RV;
  1540. }
  1541. bool X86TargetInfo::validateAsmConstraint(
  1542. const char *&Name, TargetInfo::ConstraintInfo &Info) const {
  1543. switch (*Name) {
  1544. default:
  1545. return false;
  1546. // Constant constraints.
  1547. case 'e': // 32-bit signed integer constant for use with sign-extending x86_64
  1548. // instructions.
  1549. case 'Z': // 32-bit unsigned integer constant for use with zero-extending
  1550. // x86_64 instructions.
  1551. case 's':
  1552. Info.setRequiresImmediate();
  1553. return true;
  1554. case 'I':
  1555. Info.setRequiresImmediate(0, 31);
  1556. return true;
  1557. case 'J':
  1558. Info.setRequiresImmediate(0, 63);
  1559. return true;
  1560. case 'K':
  1561. Info.setRequiresImmediate(-128, 127);
  1562. return true;
  1563. case 'L':
  1564. Info.setRequiresImmediate({int(0xff), int(0xffff), int(0xffffffff)});
  1565. return true;
  1566. case 'M':
  1567. Info.setRequiresImmediate(0, 3);
  1568. return true;
  1569. case 'N':
  1570. Info.setRequiresImmediate(0, 255);
  1571. return true;
  1572. case 'O':
  1573. Info.setRequiresImmediate(0, 127);
  1574. return true;
  1575. // Register constraints.
  1576. case 'Y': // 'Y' is the first character for several 2-character constraints.
  1577. // Shift the pointer to the second character of the constraint.
  1578. Name++;
  1579. switch (*Name) {
  1580. default:
  1581. return false;
  1582. case 'z':
  1583. case '0': // First SSE register.
  1584. case '2':
  1585. case 't': // Any SSE register, when SSE2 is enabled.
  1586. case 'i': // Any SSE register, when SSE2 and inter-unit moves enabled.
  1587. case 'm': // Any MMX register, when inter-unit moves enabled.
  1588. case 'k': // AVX512 arch mask registers: k1-k7.
  1589. Info.setAllowsRegister();
  1590. return true;
  1591. }
  1592. case 'f': // Any x87 floating point stack register.
  1593. // Constraint 'f' cannot be used for output operands.
  1594. if (Info.ConstraintStr[0] == '=')
  1595. return false;
  1596. Info.setAllowsRegister();
  1597. return true;
  1598. case 'a': // eax.
  1599. case 'b': // ebx.
  1600. case 'c': // ecx.
  1601. case 'd': // edx.
  1602. case 'S': // esi.
  1603. case 'D': // edi.
  1604. case 'A': // edx:eax.
  1605. case 't': // Top of floating point stack.
  1606. case 'u': // Second from top of floating point stack.
  1607. case 'q': // Any register accessible as [r]l: a, b, c, and d.
  1608. case 'y': // Any MMX register.
  1609. case 'v': // Any {X,Y,Z}MM register (Arch & context dependent)
  1610. case 'x': // Any SSE register.
  1611. case 'k': // Any AVX512 mask register (same as Yk, additionally allows k0
  1612. // for intermideate k reg operations).
  1613. case 'Q': // Any register accessible as [r]h: a, b, c, and d.
  1614. case 'R': // "Legacy" registers: ax, bx, cx, dx, di, si, sp, bp.
  1615. case 'l': // "Index" registers: any general register that can be used as an
  1616. // index in a base+index memory access.
  1617. Info.setAllowsRegister();
  1618. return true;
  1619. // Floating point constant constraints.
  1620. case 'C': // SSE floating point constant.
  1621. case 'G': // x87 floating point constant.
  1622. return true;
  1623. case '@':
  1624. // CC condition changes.
  1625. if (auto Len = matchAsmCCConstraint(Name)) {
  1626. Name += Len - 1;
  1627. Info.setAllowsRegister();
  1628. return true;
  1629. }
  1630. return false;
  1631. }
  1632. }
  1633. bool X86TargetInfo::validateOutputSize(StringRef Constraint,
  1634. unsigned Size) const {
  1635. // Strip off constraint modifiers.
  1636. while (Constraint[0] == '=' || Constraint[0] == '+' || Constraint[0] == '&')
  1637. Constraint = Constraint.substr(1);
  1638. return validateOperandSize(Constraint, Size);
  1639. }
  1640. bool X86TargetInfo::validateInputSize(StringRef Constraint,
  1641. unsigned Size) const {
  1642. return validateOperandSize(Constraint, Size);
  1643. }
  1644. bool X86TargetInfo::validateOperandSize(StringRef Constraint,
  1645. unsigned Size) const {
  1646. switch (Constraint[0]) {
  1647. default:
  1648. break;
  1649. case 'k':
  1650. // Registers k0-k7 (AVX512) size limit is 64 bit.
  1651. case 'y':
  1652. return Size <= 64;
  1653. case 'f':
  1654. case 't':
  1655. case 'u':
  1656. return Size <= 128;
  1657. case 'Y':
  1658. // 'Y' is the first character for several 2-character constraints.
  1659. switch (Constraint[1]) {
  1660. default:
  1661. return false;
  1662. case 'm':
  1663. // 'Ym' is synonymous with 'y'.
  1664. case 'k':
  1665. return Size <= 64;
  1666. case 'z':
  1667. case '0':
  1668. // XMM0
  1669. if (SSELevel >= SSE1)
  1670. return Size <= 128U;
  1671. return false;
  1672. case 'i':
  1673. case 't':
  1674. case '2':
  1675. // 'Yi','Yt','Y2' are synonymous with 'x' when SSE2 is enabled.
  1676. if (SSELevel < SSE2)
  1677. return false;
  1678. break;
  1679. }
  1680. LLVM_FALLTHROUGH;
  1681. case 'v':
  1682. case 'x':
  1683. if (SSELevel >= AVX512F)
  1684. // 512-bit zmm registers can be used if target supports AVX512F.
  1685. return Size <= 512U;
  1686. else if (SSELevel >= AVX)
  1687. // 256-bit ymm registers can be used if target supports AVX.
  1688. return Size <= 256U;
  1689. return Size <= 128U;
  1690. }
  1691. return true;
  1692. }
  1693. std::string X86TargetInfo::convertConstraint(const char *&Constraint) const {
  1694. switch (*Constraint) {
  1695. case '@':
  1696. if (auto Len = matchAsmCCConstraint(Constraint)) {
  1697. std::string Converted = "{" + std::string(Constraint, Len) + "}";
  1698. Constraint += Len - 1;
  1699. return Converted;
  1700. }
  1701. return std::string(1, *Constraint);
  1702. case 'a':
  1703. return std::string("{ax}");
  1704. case 'b':
  1705. return std::string("{bx}");
  1706. case 'c':
  1707. return std::string("{cx}");
  1708. case 'd':
  1709. return std::string("{dx}");
  1710. case 'S':
  1711. return std::string("{si}");
  1712. case 'D':
  1713. return std::string("{di}");
  1714. case 'p': // address
  1715. return std::string("im");
  1716. case 't': // top of floating point stack.
  1717. return std::string("{st}");
  1718. case 'u': // second from top of floating point stack.
  1719. return std::string("{st(1)}"); // second from top of floating point stack.
  1720. case 'Y':
  1721. switch (Constraint[1]) {
  1722. default:
  1723. // Break from inner switch and fall through (copy single char),
  1724. // continue parsing after copying the current constraint into
  1725. // the return string.
  1726. break;
  1727. case 'k':
  1728. case 'm':
  1729. case 'i':
  1730. case 't':
  1731. case 'z':
  1732. case '0':
  1733. case '2':
  1734. // "^" hints llvm that this is a 2 letter constraint.
  1735. // "Constraint++" is used to promote the string iterator
  1736. // to the next constraint.
  1737. return std::string("^") + std::string(Constraint++, 2);
  1738. }
  1739. LLVM_FALLTHROUGH;
  1740. default:
  1741. return std::string(1, *Constraint);
  1742. }
  1743. }
  1744. bool X86TargetInfo::checkCPUKind(CPUKind Kind) const {
  1745. // Perform any per-CPU checks necessary to determine if this CPU is
  1746. // acceptable.
  1747. switch (Kind) {
  1748. case CK_Generic:
  1749. // No processor selected!
  1750. return false;
  1751. #define PROC(ENUM, STRING, IS64BIT) \
  1752. case CK_##ENUM: \
  1753. return IS64BIT || getTriple().getArch() == llvm::Triple::x86;
  1754. #include "clang/Basic/X86Target.def"
  1755. }
  1756. llvm_unreachable("Unhandled CPU kind");
  1757. }
  1758. void X86TargetInfo::fillValidCPUList(SmallVectorImpl<StringRef> &Values) const {
  1759. #define PROC(ENUM, STRING, IS64BIT) \
  1760. if (IS64BIT || getTriple().getArch() == llvm::Triple::x86) \
  1761. Values.emplace_back(STRING);
  1762. // For aliases we need to lookup the CPUKind to check get the 64-bit ness.
  1763. #define PROC_ALIAS(ENUM, ALIAS) \
  1764. if (checkCPUKind(CK_##ENUM)) \
  1765. Values.emplace_back(ALIAS);
  1766. #include "clang/Basic/X86Target.def"
  1767. }
  1768. X86TargetInfo::CPUKind X86TargetInfo::getCPUKind(StringRef CPU) const {
  1769. return llvm::StringSwitch<CPUKind>(CPU)
  1770. #define PROC(ENUM, STRING, IS64BIT) .Case(STRING, CK_##ENUM)
  1771. #define PROC_ALIAS(ENUM, ALIAS) .Case(ALIAS, CK_##ENUM)
  1772. #include "clang/Basic/X86Target.def"
  1773. .Default(CK_Generic);
  1774. }
  1775. ArrayRef<const char *> X86TargetInfo::getGCCRegNames() const {
  1776. return llvm::makeArrayRef(GCCRegNames);
  1777. }
  1778. ArrayRef<TargetInfo::AddlRegName> X86TargetInfo::getGCCAddlRegNames() const {
  1779. return llvm::makeArrayRef(AddlRegNames);
  1780. }
  1781. ArrayRef<Builtin::Info> X86_32TargetInfo::getTargetBuiltins() const {
  1782. return llvm::makeArrayRef(BuiltinInfoX86, clang::X86::LastX86CommonBuiltin -
  1783. Builtin::FirstTSBuiltin + 1);
  1784. }
  1785. ArrayRef<Builtin::Info> X86_64TargetInfo::getTargetBuiltins() const {
  1786. return llvm::makeArrayRef(BuiltinInfoX86,
  1787. X86::LastTSBuiltin - Builtin::FirstTSBuiltin);
  1788. }