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