X86TargetParser.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. //===-- X86TargetParser - Parser for X86 features ---------------*- C++ -*-===//
  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 a target parser to recognise X86 hardware features.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Support/X86TargetParser.h"
  13. #include "llvm/ADT/StringSwitch.h"
  14. #include <numeric>
  15. using namespace llvm;
  16. using namespace llvm::X86;
  17. namespace {
  18. /// Container class for CPU features.
  19. /// This is a constexpr reimplementation of a subset of std::bitset. It would be
  20. /// nice to use std::bitset directly, but it doesn't support constant
  21. /// initialization.
  22. class FeatureBitset {
  23. static constexpr unsigned NUM_FEATURE_WORDS =
  24. (X86::CPU_FEATURE_MAX + 31) / 32;
  25. // This cannot be a std::array, operator[] is not constexpr until C++17.
  26. uint32_t Bits[NUM_FEATURE_WORDS] = {};
  27. public:
  28. constexpr FeatureBitset() = default;
  29. constexpr FeatureBitset(std::initializer_list<unsigned> Init) {
  30. for (auto I : Init)
  31. set(I);
  32. }
  33. bool any() const {
  34. return llvm::any_of(Bits, [](uint64_t V) { return V != 0; });
  35. }
  36. constexpr FeatureBitset &set(unsigned I) {
  37. // GCC <6.2 crashes if this is written in a single statement.
  38. uint32_t NewBits = Bits[I / 32] | (uint32_t(1) << (I % 32));
  39. Bits[I / 32] = NewBits;
  40. return *this;
  41. }
  42. constexpr bool operator[](unsigned I) const {
  43. uint32_t Mask = uint32_t(1) << (I % 32);
  44. return (Bits[I / 32] & Mask) != 0;
  45. }
  46. constexpr FeatureBitset &operator&=(const FeatureBitset &RHS) {
  47. for (unsigned I = 0, E = array_lengthof(Bits); I != E; ++I) {
  48. // GCC <6.2 crashes if this is written in a single statement.
  49. uint32_t NewBits = Bits[I] & RHS.Bits[I];
  50. Bits[I] = NewBits;
  51. }
  52. return *this;
  53. }
  54. constexpr FeatureBitset &operator|=(const FeatureBitset &RHS) {
  55. for (unsigned I = 0, E = array_lengthof(Bits); I != E; ++I) {
  56. // GCC <6.2 crashes if this is written in a single statement.
  57. uint32_t NewBits = Bits[I] | RHS.Bits[I];
  58. Bits[I] = NewBits;
  59. }
  60. return *this;
  61. }
  62. // gcc 5.3 miscompiles this if we try to write this using operator&=.
  63. constexpr FeatureBitset operator&(const FeatureBitset &RHS) const {
  64. FeatureBitset Result;
  65. for (unsigned I = 0, E = array_lengthof(Bits); I != E; ++I)
  66. Result.Bits[I] = Bits[I] & RHS.Bits[I];
  67. return Result;
  68. }
  69. // gcc 5.3 miscompiles this if we try to write this using operator&=.
  70. constexpr FeatureBitset operator|(const FeatureBitset &RHS) const {
  71. FeatureBitset Result;
  72. for (unsigned I = 0, E = array_lengthof(Bits); I != E; ++I)
  73. Result.Bits[I] = Bits[I] | RHS.Bits[I];
  74. return Result;
  75. }
  76. constexpr FeatureBitset operator~() const {
  77. FeatureBitset Result;
  78. for (unsigned I = 0, E = array_lengthof(Bits); I != E; ++I)
  79. Result.Bits[I] = ~Bits[I];
  80. return Result;
  81. }
  82. constexpr bool operator!=(const FeatureBitset &RHS) const {
  83. for (unsigned I = 0, E = array_lengthof(Bits); I != E; ++I)
  84. if (Bits[I] != RHS.Bits[I])
  85. return true;
  86. return false;
  87. }
  88. };
  89. struct ProcInfo {
  90. StringLiteral Name;
  91. X86::CPUKind Kind;
  92. unsigned KeyFeature;
  93. FeatureBitset Features;
  94. };
  95. struct FeatureInfo {
  96. StringLiteral Name;
  97. FeatureBitset ImpliedFeatures;
  98. };
  99. } // end anonymous namespace
  100. #define X86_FEATURE(ENUM, STRING) \
  101. constexpr FeatureBitset Feature##ENUM = {X86::FEATURE_##ENUM};
  102. #include "llvm/Support/X86TargetParser.def"
  103. // Pentium with MMX.
  104. constexpr FeatureBitset FeaturesPentiumMMX =
  105. FeatureX87 | FeatureCMPXCHG8B | FeatureMMX;
  106. // Pentium 2 and 3.
  107. constexpr FeatureBitset FeaturesPentium2 =
  108. FeatureX87 | FeatureCMPXCHG8B | FeatureMMX | FeatureFXSR;
  109. constexpr FeatureBitset FeaturesPentium3 = FeaturesPentium2 | FeatureSSE;
  110. // Pentium 4 CPUs
  111. constexpr FeatureBitset FeaturesPentium4 = FeaturesPentium3 | FeatureSSE2;
  112. constexpr FeatureBitset FeaturesPrescott = FeaturesPentium4 | FeatureSSE3;
  113. constexpr FeatureBitset FeaturesNocona =
  114. FeaturesPrescott | Feature64BIT | FeatureCMPXCHG16B;
  115. // Basic 64-bit capable CPU.
  116. constexpr FeatureBitset FeaturesX86_64 = FeaturesPentium4 | Feature64BIT;
  117. constexpr FeatureBitset FeaturesX86_64_V2 = FeaturesX86_64 | FeatureSAHF |
  118. FeaturePOPCNT | FeatureCRC32 |
  119. FeatureSSE4_2 | FeatureCMPXCHG16B;
  120. constexpr FeatureBitset FeaturesX86_64_V3 =
  121. FeaturesX86_64_V2 | FeatureAVX2 | FeatureBMI | FeatureBMI2 | FeatureF16C |
  122. FeatureFMA | FeatureLZCNT | FeatureMOVBE | FeatureXSAVE;
  123. constexpr FeatureBitset FeaturesX86_64_V4 = FeaturesX86_64_V3 |
  124. FeatureAVX512BW | FeatureAVX512CD |
  125. FeatureAVX512DQ | FeatureAVX512VL;
  126. // Intel Core CPUs
  127. constexpr FeatureBitset FeaturesCore2 =
  128. FeaturesNocona | FeatureSAHF | FeatureSSSE3;
  129. constexpr FeatureBitset FeaturesPenryn = FeaturesCore2 | FeatureSSE4_1;
  130. constexpr FeatureBitset FeaturesNehalem =
  131. FeaturesPenryn | FeaturePOPCNT | FeatureCRC32 | FeatureSSE4_2;
  132. constexpr FeatureBitset FeaturesWestmere = FeaturesNehalem | FeaturePCLMUL;
  133. constexpr FeatureBitset FeaturesSandyBridge =
  134. FeaturesWestmere | FeatureAVX | FeatureXSAVE | FeatureXSAVEOPT;
  135. constexpr FeatureBitset FeaturesIvyBridge =
  136. FeaturesSandyBridge | FeatureF16C | FeatureFSGSBASE | FeatureRDRND;
  137. constexpr FeatureBitset FeaturesHaswell =
  138. FeaturesIvyBridge | FeatureAVX2 | FeatureBMI | FeatureBMI2 | FeatureFMA |
  139. FeatureINVPCID | FeatureLZCNT | FeatureMOVBE;
  140. constexpr FeatureBitset FeaturesBroadwell =
  141. FeaturesHaswell | FeatureADX | FeaturePRFCHW | FeatureRDSEED;
  142. // Intel Knights Landing and Knights Mill
  143. // Knights Landing has feature parity with Broadwell.
  144. constexpr FeatureBitset FeaturesKNL =
  145. FeaturesBroadwell | FeatureAES | FeatureAVX512F | FeatureAVX512CD |
  146. FeatureAVX512ER | FeatureAVX512PF | FeaturePREFETCHWT1;
  147. constexpr FeatureBitset FeaturesKNM = FeaturesKNL | FeatureAVX512VPOPCNTDQ;
  148. // Intel Skylake processors.
  149. constexpr FeatureBitset FeaturesSkylakeClient =
  150. FeaturesBroadwell | FeatureAES | FeatureCLFLUSHOPT | FeatureXSAVEC |
  151. FeatureXSAVES | FeatureSGX;
  152. // SkylakeServer inherits all SkylakeClient features except SGX.
  153. // FIXME: That doesn't match gcc.
  154. constexpr FeatureBitset FeaturesSkylakeServer =
  155. (FeaturesSkylakeClient & ~FeatureSGX) | FeatureAVX512F | FeatureAVX512CD |
  156. FeatureAVX512DQ | FeatureAVX512BW | FeatureAVX512VL | FeatureCLWB |
  157. FeaturePKU;
  158. constexpr FeatureBitset FeaturesCascadeLake =
  159. FeaturesSkylakeServer | FeatureAVX512VNNI;
  160. constexpr FeatureBitset FeaturesCooperLake =
  161. FeaturesCascadeLake | FeatureAVX512BF16;
  162. // Intel 10nm processors.
  163. constexpr FeatureBitset FeaturesCannonlake =
  164. FeaturesSkylakeClient | FeatureAVX512F | FeatureAVX512CD | FeatureAVX512DQ |
  165. FeatureAVX512BW | FeatureAVX512VL | FeatureAVX512IFMA | FeatureAVX512VBMI |
  166. FeaturePKU | FeatureSHA;
  167. constexpr FeatureBitset FeaturesICLClient =
  168. FeaturesCannonlake | FeatureAVX512BITALG | FeatureAVX512VBMI2 |
  169. FeatureAVX512VNNI | FeatureAVX512VPOPCNTDQ | FeatureGFNI | FeatureRDPID |
  170. FeatureVAES | FeatureVPCLMULQDQ;
  171. constexpr FeatureBitset FeaturesRocketlake = FeaturesICLClient & ~FeatureSGX;
  172. constexpr FeatureBitset FeaturesICLServer =
  173. FeaturesICLClient | FeatureCLWB | FeaturePCONFIG | FeatureWBNOINVD;
  174. constexpr FeatureBitset FeaturesTigerlake =
  175. FeaturesICLClient | FeatureAVX512VP2INTERSECT | FeatureMOVDIR64B |
  176. FeatureCLWB | FeatureMOVDIRI | FeatureSHSTK | FeatureKL | FeatureWIDEKL;
  177. constexpr FeatureBitset FeaturesSapphireRapids =
  178. FeaturesICLServer | FeatureAMX_BF16 | FeatureAMX_INT8 | FeatureAMX_TILE |
  179. FeatureAVX512BF16 | FeatureAVX512FP16 | FeatureAVX512VP2INTERSECT |
  180. FeatureAVXVNNI | FeatureCLDEMOTE | FeatureENQCMD | FeatureMOVDIR64B |
  181. FeatureMOVDIRI | FeaturePTWRITE | FeatureSERIALIZE | FeatureSHSTK |
  182. FeatureTSXLDTRK | FeatureUINTR | FeatureWAITPKG;
  183. // Intel Atom processors.
  184. // Bonnell has feature parity with Core2 and adds MOVBE.
  185. constexpr FeatureBitset FeaturesBonnell = FeaturesCore2 | FeatureMOVBE;
  186. // Silvermont has parity with Westmere and Bonnell plus PRFCHW and RDRND.
  187. constexpr FeatureBitset FeaturesSilvermont =
  188. FeaturesBonnell | FeaturesWestmere | FeaturePRFCHW | FeatureRDRND;
  189. constexpr FeatureBitset FeaturesGoldmont =
  190. FeaturesSilvermont | FeatureAES | FeatureCLFLUSHOPT | FeatureFSGSBASE |
  191. FeatureRDSEED | FeatureSHA | FeatureXSAVE | FeatureXSAVEC |
  192. FeatureXSAVEOPT | FeatureXSAVES;
  193. constexpr FeatureBitset FeaturesGoldmontPlus =
  194. FeaturesGoldmont | FeaturePTWRITE | FeatureRDPID | FeatureSGX;
  195. constexpr FeatureBitset FeaturesTremont =
  196. FeaturesGoldmontPlus | FeatureCLWB | FeatureGFNI;
  197. constexpr FeatureBitset FeaturesAlderlake =
  198. FeaturesTremont | FeatureADX | FeatureBMI | FeatureBMI2 | FeatureF16C |
  199. FeatureFMA | FeatureINVPCID | FeatureLZCNT | FeaturePCONFIG | FeaturePKU |
  200. FeatureSERIALIZE | FeatureSHSTK | FeatureVAES | FeatureVPCLMULQDQ |
  201. FeatureCLDEMOTE | FeatureMOVDIR64B | FeatureMOVDIRI | FeatureWAITPKG |
  202. FeatureAVXVNNI | FeatureHRESET | FeatureWIDEKL;
  203. // Geode Processor.
  204. constexpr FeatureBitset FeaturesGeode =
  205. FeatureX87 | FeatureCMPXCHG8B | FeatureMMX | Feature3DNOW | Feature3DNOWA;
  206. // K6 processor.
  207. constexpr FeatureBitset FeaturesK6 = FeatureX87 | FeatureCMPXCHG8B | FeatureMMX;
  208. // K7 and K8 architecture processors.
  209. constexpr FeatureBitset FeaturesAthlon =
  210. FeatureX87 | FeatureCMPXCHG8B | FeatureMMX | Feature3DNOW | Feature3DNOWA;
  211. constexpr FeatureBitset FeaturesAthlonXP =
  212. FeaturesAthlon | FeatureFXSR | FeatureSSE;
  213. constexpr FeatureBitset FeaturesK8 =
  214. FeaturesAthlonXP | FeatureSSE2 | Feature64BIT;
  215. constexpr FeatureBitset FeaturesK8SSE3 = FeaturesK8 | FeatureSSE3;
  216. constexpr FeatureBitset FeaturesAMDFAM10 =
  217. FeaturesK8SSE3 | FeatureCMPXCHG16B | FeatureLZCNT | FeaturePOPCNT |
  218. FeaturePRFCHW | FeatureSAHF | FeatureSSE4_A;
  219. // Bobcat architecture processors.
  220. constexpr FeatureBitset FeaturesBTVER1 =
  221. FeatureX87 | FeatureCMPXCHG8B | FeatureCMPXCHG16B | Feature64BIT |
  222. FeatureFXSR | FeatureLZCNT | FeatureMMX | FeaturePOPCNT | FeaturePRFCHW |
  223. FeatureSSE | FeatureSSE2 | FeatureSSE3 | FeatureSSSE3 | FeatureSSE4_A |
  224. FeatureSAHF;
  225. constexpr FeatureBitset FeaturesBTVER2 =
  226. FeaturesBTVER1 | FeatureAES | FeatureAVX | FeatureBMI | FeatureCRC32 |
  227. FeatureF16C | FeatureMOVBE | FeaturePCLMUL | FeatureXSAVE | FeatureXSAVEOPT;
  228. // AMD Bulldozer architecture processors.
  229. constexpr FeatureBitset FeaturesBDVER1 =
  230. FeatureX87 | FeatureAES | FeatureAVX | FeatureCMPXCHG8B |
  231. FeatureCMPXCHG16B | FeatureCRC32 | Feature64BIT | FeatureFMA4 |
  232. FeatureFXSR | FeatureLWP | FeatureLZCNT | FeatureMMX | FeaturePCLMUL |
  233. FeaturePOPCNT | FeaturePRFCHW | FeatureSAHF | FeatureSSE | FeatureSSE2 |
  234. FeatureSSE3 | FeatureSSSE3 | FeatureSSE4_1 | FeatureSSE4_2 | FeatureSSE4_A |
  235. FeatureXOP | FeatureXSAVE;
  236. constexpr FeatureBitset FeaturesBDVER2 =
  237. FeaturesBDVER1 | FeatureBMI | FeatureFMA | FeatureF16C | FeatureTBM;
  238. constexpr FeatureBitset FeaturesBDVER3 =
  239. FeaturesBDVER2 | FeatureFSGSBASE | FeatureXSAVEOPT;
  240. constexpr FeatureBitset FeaturesBDVER4 = FeaturesBDVER3 | FeatureAVX2 |
  241. FeatureBMI2 | FeatureMOVBE |
  242. FeatureMWAITX | FeatureRDRND;
  243. // AMD Zen architecture processors.
  244. constexpr FeatureBitset FeaturesZNVER1 =
  245. FeatureX87 | FeatureADX | FeatureAES | FeatureAVX | FeatureAVX2 |
  246. FeatureBMI | FeatureBMI2 | FeatureCLFLUSHOPT | FeatureCLZERO |
  247. FeatureCMPXCHG8B | FeatureCMPXCHG16B | FeatureCRC32 | Feature64BIT |
  248. FeatureF16C | FeatureFMA | FeatureFSGSBASE | FeatureFXSR | FeatureLZCNT |
  249. FeatureMMX | FeatureMOVBE | FeatureMWAITX | FeaturePCLMUL | FeaturePOPCNT |
  250. FeaturePRFCHW | FeatureRDRND | FeatureRDSEED | FeatureSAHF | FeatureSHA |
  251. FeatureSSE | FeatureSSE2 | FeatureSSE3 | FeatureSSSE3 | FeatureSSE4_1 |
  252. FeatureSSE4_2 | FeatureSSE4_A | FeatureXSAVE | FeatureXSAVEC |
  253. FeatureXSAVEOPT | FeatureXSAVES;
  254. constexpr FeatureBitset FeaturesZNVER2 =
  255. FeaturesZNVER1 | FeatureCLWB | FeatureRDPID | FeatureWBNOINVD;
  256. static constexpr FeatureBitset FeaturesZNVER3 = FeaturesZNVER2 |
  257. FeatureINVPCID | FeaturePKU |
  258. FeatureVAES | FeatureVPCLMULQDQ;
  259. constexpr ProcInfo Processors[] = {
  260. // Empty processor. Include X87 and CMPXCHG8 for backwards compatibility.
  261. { {""}, CK_None, ~0U, FeatureX87 | FeatureCMPXCHG8B },
  262. // i386-generation processors.
  263. { {"i386"}, CK_i386, ~0U, FeatureX87 },
  264. // i486-generation processors.
  265. { {"i486"}, CK_i486, ~0U, FeatureX87 },
  266. { {"winchip-c6"}, CK_WinChipC6, ~0U, FeaturesPentiumMMX },
  267. { {"winchip2"}, CK_WinChip2, ~0U, FeaturesPentiumMMX | Feature3DNOW },
  268. { {"c3"}, CK_C3, ~0U, FeaturesPentiumMMX | Feature3DNOW },
  269. // i586-generation processors, P5 microarchitecture based.
  270. { {"i586"}, CK_i586, ~0U, FeatureX87 | FeatureCMPXCHG8B },
  271. { {"pentium"}, CK_Pentium, ~0U, FeatureX87 | FeatureCMPXCHG8B },
  272. { {"pentium-mmx"}, CK_PentiumMMX, ~0U, FeaturesPentiumMMX },
  273. // i686-generation processors, P6 / Pentium M microarchitecture based.
  274. { {"pentiumpro"}, CK_PentiumPro, ~0U, FeatureX87 | FeatureCMPXCHG8B },
  275. { {"i686"}, CK_i686, ~0U, FeatureX87 | FeatureCMPXCHG8B },
  276. { {"pentium2"}, CK_Pentium2, ~0U, FeaturesPentium2 },
  277. { {"pentium3"}, CK_Pentium3, ~0U, FeaturesPentium3 },
  278. { {"pentium3m"}, CK_Pentium3, ~0U, FeaturesPentium3 },
  279. { {"pentium-m"}, CK_PentiumM, ~0U, FeaturesPentium4 },
  280. { {"c3-2"}, CK_C3_2, ~0U, FeaturesPentium3 },
  281. { {"yonah"}, CK_Yonah, ~0U, FeaturesPrescott },
  282. // Netburst microarchitecture based processors.
  283. { {"pentium4"}, CK_Pentium4, ~0U, FeaturesPentium4 },
  284. { {"pentium4m"}, CK_Pentium4, ~0U, FeaturesPentium4 },
  285. { {"prescott"}, CK_Prescott, ~0U, FeaturesPrescott },
  286. { {"nocona"}, CK_Nocona, ~0U, FeaturesNocona },
  287. // Core microarchitecture based processors.
  288. { {"core2"}, CK_Core2, ~0U, FeaturesCore2 },
  289. { {"penryn"}, CK_Penryn, ~0U, FeaturesPenryn },
  290. // Atom processors
  291. { {"bonnell"}, CK_Bonnell, FEATURE_SSSE3, FeaturesBonnell },
  292. { {"atom"}, CK_Bonnell, FEATURE_SSSE3, FeaturesBonnell },
  293. { {"silvermont"}, CK_Silvermont, FEATURE_SSE4_2, FeaturesSilvermont },
  294. { {"slm"}, CK_Silvermont, FEATURE_SSE4_2, FeaturesSilvermont },
  295. { {"goldmont"}, CK_Goldmont, FEATURE_SSE4_2, FeaturesGoldmont },
  296. { {"goldmont-plus"}, CK_GoldmontPlus, FEATURE_SSE4_2, FeaturesGoldmontPlus },
  297. { {"tremont"}, CK_Tremont, FEATURE_SSE4_2, FeaturesTremont },
  298. // Nehalem microarchitecture based processors.
  299. { {"nehalem"}, CK_Nehalem, FEATURE_SSE4_2, FeaturesNehalem },
  300. { {"corei7"}, CK_Nehalem, FEATURE_SSE4_2, FeaturesNehalem },
  301. // Westmere microarchitecture based processors.
  302. { {"westmere"}, CK_Westmere, FEATURE_PCLMUL, FeaturesWestmere },
  303. // Sandy Bridge microarchitecture based processors.
  304. { {"sandybridge"}, CK_SandyBridge, FEATURE_AVX, FeaturesSandyBridge },
  305. { {"corei7-avx"}, CK_SandyBridge, FEATURE_AVX, FeaturesSandyBridge },
  306. // Ivy Bridge microarchitecture based processors.
  307. { {"ivybridge"}, CK_IvyBridge, FEATURE_AVX, FeaturesIvyBridge },
  308. { {"core-avx-i"}, CK_IvyBridge, FEATURE_AVX, FeaturesIvyBridge },
  309. // Haswell microarchitecture based processors.
  310. { {"haswell"}, CK_Haswell, FEATURE_AVX2, FeaturesHaswell },
  311. { {"core-avx2"}, CK_Haswell, FEATURE_AVX2, FeaturesHaswell },
  312. // Broadwell microarchitecture based processors.
  313. { {"broadwell"}, CK_Broadwell, FEATURE_AVX2, FeaturesBroadwell },
  314. // Skylake client microarchitecture based processors.
  315. { {"skylake"}, CK_SkylakeClient, FEATURE_AVX2, FeaturesSkylakeClient },
  316. // Skylake server microarchitecture based processors.
  317. { {"skylake-avx512"}, CK_SkylakeServer, FEATURE_AVX512F, FeaturesSkylakeServer },
  318. { {"skx"}, CK_SkylakeServer, FEATURE_AVX512F, FeaturesSkylakeServer },
  319. // Cascadelake Server microarchitecture based processors.
  320. { {"cascadelake"}, CK_Cascadelake, FEATURE_AVX512VNNI, FeaturesCascadeLake },
  321. // Cooperlake Server microarchitecture based processors.
  322. { {"cooperlake"}, CK_Cooperlake, FEATURE_AVX512BF16, FeaturesCooperLake },
  323. // Cannonlake client microarchitecture based processors.
  324. { {"cannonlake"}, CK_Cannonlake, FEATURE_AVX512VBMI, FeaturesCannonlake },
  325. // Icelake client microarchitecture based processors.
  326. { {"icelake-client"}, CK_IcelakeClient, FEATURE_AVX512VBMI2, FeaturesICLClient },
  327. // Rocketlake microarchitecture based processors.
  328. { {"rocketlake"}, CK_Rocketlake, FEATURE_AVX512VBMI2, FeaturesRocketlake },
  329. // Icelake server microarchitecture based processors.
  330. { {"icelake-server"}, CK_IcelakeServer, FEATURE_AVX512VBMI2, FeaturesICLServer },
  331. // Tigerlake microarchitecture based processors.
  332. { {"tigerlake"}, CK_Tigerlake, FEATURE_AVX512VP2INTERSECT, FeaturesTigerlake },
  333. // Sapphire Rapids microarchitecture based processors.
  334. { {"sapphirerapids"}, CK_SapphireRapids, FEATURE_AVX512VP2INTERSECT, FeaturesSapphireRapids },
  335. // Alderlake microarchitecture based processors.
  336. { {"alderlake"}, CK_Alderlake, FEATURE_AVX2, FeaturesAlderlake },
  337. // Knights Landing processor.
  338. { {"knl"}, CK_KNL, FEATURE_AVX512F, FeaturesKNL },
  339. // Knights Mill processor.
  340. { {"knm"}, CK_KNM, FEATURE_AVX5124FMAPS, FeaturesKNM },
  341. // Lakemont microarchitecture based processors.
  342. { {"lakemont"}, CK_Lakemont, ~0U, FeatureCMPXCHG8B },
  343. // K6 architecture processors.
  344. { {"k6"}, CK_K6, ~0U, FeaturesK6 },
  345. { {"k6-2"}, CK_K6_2, ~0U, FeaturesK6 | Feature3DNOW },
  346. { {"k6-3"}, CK_K6_3, ~0U, FeaturesK6 | Feature3DNOW },
  347. // K7 architecture processors.
  348. { {"athlon"}, CK_Athlon, ~0U, FeaturesAthlon },
  349. { {"athlon-tbird"}, CK_Athlon, ~0U, FeaturesAthlon },
  350. { {"athlon-xp"}, CK_AthlonXP, ~0U, FeaturesAthlonXP },
  351. { {"athlon-mp"}, CK_AthlonXP, ~0U, FeaturesAthlonXP },
  352. { {"athlon-4"}, CK_AthlonXP, ~0U, FeaturesAthlonXP },
  353. // K8 architecture processors.
  354. { {"k8"}, CK_K8, ~0U, FeaturesK8 },
  355. { {"athlon64"}, CK_K8, ~0U, FeaturesK8 },
  356. { {"athlon-fx"}, CK_K8, ~0U, FeaturesK8 },
  357. { {"opteron"}, CK_K8, ~0U, FeaturesK8 },
  358. { {"k8-sse3"}, CK_K8SSE3, ~0U, FeaturesK8SSE3 },
  359. { {"athlon64-sse3"}, CK_K8SSE3, ~0U, FeaturesK8SSE3 },
  360. { {"opteron-sse3"}, CK_K8SSE3, ~0U, FeaturesK8SSE3 },
  361. { {"amdfam10"}, CK_AMDFAM10, FEATURE_SSE4_A, FeaturesAMDFAM10 },
  362. { {"barcelona"}, CK_AMDFAM10, FEATURE_SSE4_A, FeaturesAMDFAM10 },
  363. // Bobcat architecture processors.
  364. { {"btver1"}, CK_BTVER1, FEATURE_SSE4_A, FeaturesBTVER1 },
  365. { {"btver2"}, CK_BTVER2, FEATURE_BMI, FeaturesBTVER2 },
  366. // Bulldozer architecture processors.
  367. { {"bdver1"}, CK_BDVER1, FEATURE_XOP, FeaturesBDVER1 },
  368. { {"bdver2"}, CK_BDVER2, FEATURE_FMA, FeaturesBDVER2 },
  369. { {"bdver3"}, CK_BDVER3, FEATURE_FMA, FeaturesBDVER3 },
  370. { {"bdver4"}, CK_BDVER4, FEATURE_AVX2, FeaturesBDVER4 },
  371. // Zen architecture processors.
  372. { {"znver1"}, CK_ZNVER1, FEATURE_AVX2, FeaturesZNVER1 },
  373. { {"znver2"}, CK_ZNVER2, FEATURE_AVX2, FeaturesZNVER2 },
  374. { {"znver3"}, CK_ZNVER3, FEATURE_AVX2, FeaturesZNVER3 },
  375. // Generic 64-bit processor.
  376. { {"x86-64"}, CK_x86_64, ~0U, FeaturesX86_64 },
  377. { {"x86-64-v2"}, CK_x86_64_v2, ~0U, FeaturesX86_64_V2 },
  378. { {"x86-64-v3"}, CK_x86_64_v3, ~0U, FeaturesX86_64_V3 },
  379. { {"x86-64-v4"}, CK_x86_64_v4, ~0U, FeaturesX86_64_V4 },
  380. // Geode processors.
  381. { {"geode"}, CK_Geode, ~0U, FeaturesGeode },
  382. };
  383. constexpr const char *NoTuneList[] = {"x86-64-v2", "x86-64-v3", "x86-64-v4"};
  384. X86::CPUKind llvm::X86::parseArchX86(StringRef CPU, bool Only64Bit) {
  385. for (const auto &P : Processors)
  386. if (P.Name == CPU && (P.Features[FEATURE_64BIT] || !Only64Bit))
  387. return P.Kind;
  388. return CK_None;
  389. }
  390. X86::CPUKind llvm::X86::parseTuneCPU(StringRef CPU, bool Only64Bit) {
  391. if (llvm::is_contained(NoTuneList, CPU))
  392. return CK_None;
  393. return parseArchX86(CPU, Only64Bit);
  394. }
  395. void llvm::X86::fillValidCPUArchList(SmallVectorImpl<StringRef> &Values,
  396. bool Only64Bit) {
  397. for (const auto &P : Processors)
  398. if (!P.Name.empty() && (P.Features[FEATURE_64BIT] || !Only64Bit))
  399. Values.emplace_back(P.Name);
  400. }
  401. void llvm::X86::fillValidTuneCPUList(SmallVectorImpl<StringRef> &Values,
  402. bool Only64Bit) {
  403. for (const ProcInfo &P : Processors)
  404. if (!P.Name.empty() && (P.Features[FEATURE_64BIT] || !Only64Bit) &&
  405. !llvm::is_contained(NoTuneList, P.Name))
  406. Values.emplace_back(P.Name);
  407. }
  408. ProcessorFeatures llvm::X86::getKeyFeature(X86::CPUKind Kind) {
  409. // FIXME: Can we avoid a linear search here? The table might be sorted by
  410. // CPUKind so we could binary search?
  411. for (const auto &P : Processors) {
  412. if (P.Kind == Kind) {
  413. assert(P.KeyFeature != ~0U && "Processor does not have a key feature.");
  414. return static_cast<ProcessorFeatures>(P.KeyFeature);
  415. }
  416. }
  417. llvm_unreachable("Unable to find CPU kind!");
  418. }
  419. // Features with no dependencies.
  420. constexpr FeatureBitset ImpliedFeatures64BIT = {};
  421. constexpr FeatureBitset ImpliedFeaturesADX = {};
  422. constexpr FeatureBitset ImpliedFeaturesBMI = {};
  423. constexpr FeatureBitset ImpliedFeaturesBMI2 = {};
  424. constexpr FeatureBitset ImpliedFeaturesCLDEMOTE = {};
  425. constexpr FeatureBitset ImpliedFeaturesCLFLUSHOPT = {};
  426. constexpr FeatureBitset ImpliedFeaturesCLWB = {};
  427. constexpr FeatureBitset ImpliedFeaturesCLZERO = {};
  428. constexpr FeatureBitset ImpliedFeaturesCMOV = {};
  429. constexpr FeatureBitset ImpliedFeaturesCMPXCHG16B = {};
  430. constexpr FeatureBitset ImpliedFeaturesCMPXCHG8B = {};
  431. constexpr FeatureBitset ImpliedFeaturesCRC32 = {};
  432. constexpr FeatureBitset ImpliedFeaturesENQCMD = {};
  433. constexpr FeatureBitset ImpliedFeaturesFSGSBASE = {};
  434. constexpr FeatureBitset ImpliedFeaturesFXSR = {};
  435. constexpr FeatureBitset ImpliedFeaturesINVPCID = {};
  436. constexpr FeatureBitset ImpliedFeaturesLWP = {};
  437. constexpr FeatureBitset ImpliedFeaturesLZCNT = {};
  438. constexpr FeatureBitset ImpliedFeaturesMWAITX = {};
  439. constexpr FeatureBitset ImpliedFeaturesMOVBE = {};
  440. constexpr FeatureBitset ImpliedFeaturesMOVDIR64B = {};
  441. constexpr FeatureBitset ImpliedFeaturesMOVDIRI = {};
  442. constexpr FeatureBitset ImpliedFeaturesPCONFIG = {};
  443. constexpr FeatureBitset ImpliedFeaturesPOPCNT = {};
  444. constexpr FeatureBitset ImpliedFeaturesPKU = {};
  445. constexpr FeatureBitset ImpliedFeaturesPREFETCHWT1 = {};
  446. constexpr FeatureBitset ImpliedFeaturesPRFCHW = {};
  447. constexpr FeatureBitset ImpliedFeaturesPTWRITE = {};
  448. constexpr FeatureBitset ImpliedFeaturesRDPID = {};
  449. constexpr FeatureBitset ImpliedFeaturesRDRND = {};
  450. constexpr FeatureBitset ImpliedFeaturesRDSEED = {};
  451. constexpr FeatureBitset ImpliedFeaturesRTM = {};
  452. constexpr FeatureBitset ImpliedFeaturesSAHF = {};
  453. constexpr FeatureBitset ImpliedFeaturesSERIALIZE = {};
  454. constexpr FeatureBitset ImpliedFeaturesSGX = {};
  455. constexpr FeatureBitset ImpliedFeaturesSHSTK = {};
  456. constexpr FeatureBitset ImpliedFeaturesTBM = {};
  457. constexpr FeatureBitset ImpliedFeaturesTSXLDTRK = {};
  458. constexpr FeatureBitset ImpliedFeaturesUINTR = {};
  459. constexpr FeatureBitset ImpliedFeaturesWAITPKG = {};
  460. constexpr FeatureBitset ImpliedFeaturesWBNOINVD = {};
  461. constexpr FeatureBitset ImpliedFeaturesVZEROUPPER = {};
  462. constexpr FeatureBitset ImpliedFeaturesX87 = {};
  463. constexpr FeatureBitset ImpliedFeaturesXSAVE = {};
  464. // Not really CPU features, but need to be in the table because clang uses
  465. // target features to communicate them to the backend.
  466. constexpr FeatureBitset ImpliedFeaturesRETPOLINE_EXTERNAL_THUNK = {};
  467. constexpr FeatureBitset ImpliedFeaturesRETPOLINE_INDIRECT_BRANCHES = {};
  468. constexpr FeatureBitset ImpliedFeaturesRETPOLINE_INDIRECT_CALLS = {};
  469. constexpr FeatureBitset ImpliedFeaturesLVI_CFI = {};
  470. constexpr FeatureBitset ImpliedFeaturesLVI_LOAD_HARDENING = {};
  471. // XSAVE features are dependent on basic XSAVE.
  472. constexpr FeatureBitset ImpliedFeaturesXSAVEC = FeatureXSAVE;
  473. constexpr FeatureBitset ImpliedFeaturesXSAVEOPT = FeatureXSAVE;
  474. constexpr FeatureBitset ImpliedFeaturesXSAVES = FeatureXSAVE;
  475. // MMX->3DNOW->3DNOWA chain.
  476. constexpr FeatureBitset ImpliedFeaturesMMX = {};
  477. constexpr FeatureBitset ImpliedFeatures3DNOW = FeatureMMX;
  478. constexpr FeatureBitset ImpliedFeatures3DNOWA = Feature3DNOW;
  479. // SSE/AVX/AVX512F chain.
  480. constexpr FeatureBitset ImpliedFeaturesSSE = {};
  481. constexpr FeatureBitset ImpliedFeaturesSSE2 = FeatureSSE;
  482. constexpr FeatureBitset ImpliedFeaturesSSE3 = FeatureSSE2;
  483. constexpr FeatureBitset ImpliedFeaturesSSSE3 = FeatureSSE3;
  484. constexpr FeatureBitset ImpliedFeaturesSSE4_1 = FeatureSSSE3;
  485. constexpr FeatureBitset ImpliedFeaturesSSE4_2 = FeatureSSE4_1;
  486. constexpr FeatureBitset ImpliedFeaturesAVX = FeatureSSE4_2;
  487. constexpr FeatureBitset ImpliedFeaturesAVX2 = FeatureAVX;
  488. constexpr FeatureBitset ImpliedFeaturesAVX512F =
  489. FeatureAVX2 | FeatureF16C | FeatureFMA;
  490. // Vector extensions that build on SSE or AVX.
  491. constexpr FeatureBitset ImpliedFeaturesAES = FeatureSSE2;
  492. constexpr FeatureBitset ImpliedFeaturesF16C = FeatureAVX;
  493. constexpr FeatureBitset ImpliedFeaturesFMA = FeatureAVX;
  494. constexpr FeatureBitset ImpliedFeaturesGFNI = FeatureSSE2;
  495. constexpr FeatureBitset ImpliedFeaturesPCLMUL = FeatureSSE2;
  496. constexpr FeatureBitset ImpliedFeaturesSHA = FeatureSSE2;
  497. constexpr FeatureBitset ImpliedFeaturesVAES = FeatureAES | FeatureAVX;
  498. constexpr FeatureBitset ImpliedFeaturesVPCLMULQDQ = FeatureAVX | FeaturePCLMUL;
  499. // AVX512 features.
  500. constexpr FeatureBitset ImpliedFeaturesAVX512CD = FeatureAVX512F;
  501. constexpr FeatureBitset ImpliedFeaturesAVX512BW = FeatureAVX512F;
  502. constexpr FeatureBitset ImpliedFeaturesAVX512DQ = FeatureAVX512F;
  503. constexpr FeatureBitset ImpliedFeaturesAVX512ER = FeatureAVX512F;
  504. constexpr FeatureBitset ImpliedFeaturesAVX512PF = FeatureAVX512F;
  505. constexpr FeatureBitset ImpliedFeaturesAVX512VL = FeatureAVX512F;
  506. constexpr FeatureBitset ImpliedFeaturesAVX512BF16 = FeatureAVX512BW;
  507. constexpr FeatureBitset ImpliedFeaturesAVX512BITALG = FeatureAVX512BW;
  508. constexpr FeatureBitset ImpliedFeaturesAVX512IFMA = FeatureAVX512F;
  509. constexpr FeatureBitset ImpliedFeaturesAVX512VNNI = FeatureAVX512F;
  510. constexpr FeatureBitset ImpliedFeaturesAVX512VPOPCNTDQ = FeatureAVX512F;
  511. constexpr FeatureBitset ImpliedFeaturesAVX512VBMI = FeatureAVX512BW;
  512. constexpr FeatureBitset ImpliedFeaturesAVX512VBMI2 = FeatureAVX512BW;
  513. constexpr FeatureBitset ImpliedFeaturesAVX512VP2INTERSECT = FeatureAVX512F;
  514. // FIXME: These two aren't really implemented and just exist in the feature
  515. // list for __builtin_cpu_supports. So omit their dependencies.
  516. constexpr FeatureBitset ImpliedFeaturesAVX5124FMAPS = {};
  517. constexpr FeatureBitset ImpliedFeaturesAVX5124VNNIW = {};
  518. // SSE4_A->FMA4->XOP chain.
  519. constexpr FeatureBitset ImpliedFeaturesSSE4_A = FeatureSSE3;
  520. constexpr FeatureBitset ImpliedFeaturesFMA4 = FeatureAVX | FeatureSSE4_A;
  521. constexpr FeatureBitset ImpliedFeaturesXOP = FeatureFMA4;
  522. // AMX Features
  523. constexpr FeatureBitset ImpliedFeaturesAMX_TILE = {};
  524. constexpr FeatureBitset ImpliedFeaturesAMX_BF16 = FeatureAMX_TILE;
  525. constexpr FeatureBitset ImpliedFeaturesAMX_INT8 = FeatureAMX_TILE;
  526. constexpr FeatureBitset ImpliedFeaturesHRESET = {};
  527. static constexpr FeatureBitset ImpliedFeaturesAVX512FP16 =
  528. FeatureAVX512BW | FeatureAVX512DQ | FeatureAVX512VL;
  529. // Key Locker Features
  530. constexpr FeatureBitset ImpliedFeaturesKL = FeatureSSE2;
  531. constexpr FeatureBitset ImpliedFeaturesWIDEKL = FeatureKL;
  532. // AVXVNNI Features
  533. constexpr FeatureBitset ImpliedFeaturesAVXVNNI = FeatureAVX2;
  534. constexpr FeatureInfo FeatureInfos[X86::CPU_FEATURE_MAX] = {
  535. #define X86_FEATURE(ENUM, STR) {{STR}, ImpliedFeatures##ENUM},
  536. #include "llvm/Support/X86TargetParser.def"
  537. };
  538. void llvm::X86::getFeaturesForCPU(StringRef CPU,
  539. SmallVectorImpl<StringRef> &EnabledFeatures) {
  540. auto I = llvm::find_if(Processors,
  541. [&](const ProcInfo &P) { return P.Name == CPU; });
  542. assert(I != std::end(Processors) && "Processor not found!");
  543. FeatureBitset Bits = I->Features;
  544. // Remove the 64-bit feature which we only use to validate if a CPU can
  545. // be used with 64-bit mode.
  546. Bits &= ~Feature64BIT;
  547. // Add the string version of all set bits.
  548. for (unsigned i = 0; i != CPU_FEATURE_MAX; ++i)
  549. if (Bits[i] && !FeatureInfos[i].Name.empty())
  550. EnabledFeatures.push_back(FeatureInfos[i].Name);
  551. }
  552. // For each feature that is (transitively) implied by this feature, set it.
  553. static void getImpliedEnabledFeatures(FeatureBitset &Bits,
  554. const FeatureBitset &Implies) {
  555. // Fast path: Implies is often empty.
  556. if (!Implies.any())
  557. return;
  558. FeatureBitset Prev;
  559. Bits |= Implies;
  560. do {
  561. Prev = Bits;
  562. for (unsigned i = CPU_FEATURE_MAX; i;)
  563. if (Bits[--i])
  564. Bits |= FeatureInfos[i].ImpliedFeatures;
  565. } while (Prev != Bits);
  566. }
  567. /// Create bit vector of features that are implied disabled if the feature
  568. /// passed in Value is disabled.
  569. static void getImpliedDisabledFeatures(FeatureBitset &Bits, unsigned Value) {
  570. // Check all features looking for any dependent on this feature. If we find
  571. // one, mark it and recursively find any feature that depend on it.
  572. FeatureBitset Prev;
  573. Bits.set(Value);
  574. do {
  575. Prev = Bits;
  576. for (unsigned i = 0; i != CPU_FEATURE_MAX; ++i)
  577. if ((FeatureInfos[i].ImpliedFeatures & Bits).any())
  578. Bits.set(i);
  579. } while (Prev != Bits);
  580. }
  581. void llvm::X86::updateImpliedFeatures(
  582. StringRef Feature, bool Enabled,
  583. StringMap<bool> &Features) {
  584. auto I = llvm::find_if(
  585. FeatureInfos, [&](const FeatureInfo &FI) { return FI.Name == Feature; });
  586. if (I == std::end(FeatureInfos)) {
  587. // FIXME: This shouldn't happen, but may not have all features in the table
  588. // yet.
  589. return;
  590. }
  591. FeatureBitset ImpliedBits;
  592. if (Enabled)
  593. getImpliedEnabledFeatures(ImpliedBits, I->ImpliedFeatures);
  594. else
  595. getImpliedDisabledFeatures(ImpliedBits,
  596. std::distance(std::begin(FeatureInfos), I));
  597. // Update the map entry for all implied features.
  598. for (unsigned i = 0; i != CPU_FEATURE_MAX; ++i)
  599. if (ImpliedBits[i] && !FeatureInfos[i].Name.empty())
  600. Features[FeatureInfos[i].Name] = Enabled;
  601. }
  602. uint64_t llvm::X86::getCpuSupportsMask(ArrayRef<StringRef> FeatureStrs) {
  603. // Processor features and mapping to processor feature value.
  604. uint64_t FeaturesMask = 0;
  605. for (const StringRef &FeatureStr : FeatureStrs) {
  606. unsigned Feature = StringSwitch<unsigned>(FeatureStr)
  607. #define X86_FEATURE_COMPAT(ENUM, STR, PRIORITY) \
  608. .Case(STR, llvm::X86::FEATURE_##ENUM)
  609. #include "llvm/Support/X86TargetParser.def"
  610. ;
  611. FeaturesMask |= (1ULL << Feature);
  612. }
  613. return FeaturesMask;
  614. }
  615. unsigned llvm::X86::getFeaturePriority(ProcessorFeatures Feat) {
  616. #ifndef NDEBUG
  617. // Check that priorities are set properly in the .def file. We expect that
  618. // "compat" features are assigned non-duplicate consecutive priorities
  619. // starting from zero (0, 1, ..., num_features - 1).
  620. #define X86_FEATURE_COMPAT(ENUM, STR, PRIORITY) PRIORITY,
  621. unsigned Priorities[] = {
  622. #include "llvm/Support/X86TargetParser.def"
  623. std::numeric_limits<unsigned>::max() // Need to consume last comma.
  624. };
  625. std::array<unsigned, array_lengthof(Priorities) - 1> HelperList;
  626. std::iota(HelperList.begin(), HelperList.end(), 0);
  627. assert(std::is_permutation(HelperList.begin(), HelperList.end(),
  628. std::begin(Priorities),
  629. std::prev(std::end(Priorities))) &&
  630. "Priorities don't form consecutive range!");
  631. #endif
  632. switch (Feat) {
  633. #define X86_FEATURE_COMPAT(ENUM, STR, PRIORITY) \
  634. case X86::FEATURE_##ENUM: \
  635. return PRIORITY;
  636. #include "llvm/Support/X86TargetParser.def"
  637. default:
  638. llvm_unreachable("No Feature Priority for non-CPUSupports Features");
  639. }
  640. }