1 //===-- X86TargetTransformInfo.cpp - X86 specific TTI pass ----------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// \file 10 /// This file implements a TargetTransformInfo analysis pass specific to the 11 /// X86 target machine. It uses the target's detailed information to provide 12 /// more precise answers to certain TTI queries, while letting the target 13 /// independent and default TTI implementations handle the rest. 14 /// 15 //===----------------------------------------------------------------------===// 16 17 #include "X86TargetTransformInfo.h" 18 #include "llvm/Analysis/TargetTransformInfo.h" 19 #include "llvm/CodeGen/BasicTTIImpl.h" 20 #include "llvm/IR/IntrinsicInst.h" 21 #include "llvm/Support/Debug.h" 22 #include "llvm/Target/CostTable.h" 23 #include "llvm/Target/TargetLowering.h" 24 25 using namespace llvm; 26 27 #define DEBUG_TYPE "x86tti" 28 29 //===----------------------------------------------------------------------===// 30 // 31 // X86 cost model. 32 // 33 //===----------------------------------------------------------------------===// 34 35 TargetTransformInfo::PopcntSupportKind 36 X86TTIImpl::getPopcntSupport(unsigned TyWidth) { 37 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2"); 38 // TODO: Currently the __builtin_popcount() implementation using SSE3 39 // instructions is inefficient. Once the problem is fixed, we should 40 // call ST->hasSSE3() instead of ST->hasPOPCNT(). 41 return ST->hasPOPCNT() ? TTI::PSK_FastHardware : TTI::PSK_Software; 42 } 43 44 unsigned X86TTIImpl::getNumberOfRegisters(bool Vector) { 45 if (Vector && !ST->hasSSE1()) 46 return 0; 47 48 if (ST->is64Bit()) { 49 if (Vector && ST->hasAVX512()) 50 return 32; 51 return 16; 52 } 53 return 8; 54 } 55 56 unsigned X86TTIImpl::getRegisterBitWidth(bool Vector) { 57 if (Vector) { 58 if (ST->hasAVX512()) return 512; 59 if (ST->hasAVX()) return 256; 60 if (ST->hasSSE1()) return 128; 61 return 0; 62 } 63 64 if (ST->is64Bit()) 65 return 64; 66 67 return 32; 68 } 69 70 unsigned X86TTIImpl::getMaxInterleaveFactor(unsigned VF) { 71 // If the loop will not be vectorized, don't interleave the loop. 72 // Let regular unroll to unroll the loop, which saves the overflow 73 // check and memory check cost. 74 if (VF == 1) 75 return 1; 76 77 if (ST->isAtom()) 78 return 1; 79 80 // Sandybridge and Haswell have multiple execution ports and pipelined 81 // vector units. 82 if (ST->hasAVX()) 83 return 4; 84 85 return 2; 86 } 87 88 int X86TTIImpl::getArithmeticInstrCost( 89 unsigned Opcode, Type *Ty, TTI::OperandValueKind Op1Info, 90 TTI::OperandValueKind Op2Info, TTI::OperandValueProperties Opd1PropInfo, 91 TTI::OperandValueProperties Opd2PropInfo) { 92 // Legalize the type. 93 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty); 94 95 int ISD = TLI->InstructionOpcodeToISD(Opcode); 96 assert(ISD && "Invalid opcode"); 97 98 if (ISD == ISD::SDIV && 99 Op2Info == TargetTransformInfo::OK_UniformConstantValue && 100 Opd2PropInfo == TargetTransformInfo::OP_PowerOf2) { 101 // On X86, vector signed division by constants power-of-two are 102 // normally expanded to the sequence SRA + SRL + ADD + SRA. 103 // The OperandValue properties many not be same as that of previous 104 // operation;conservatively assume OP_None. 105 int Cost = 2 * getArithmeticInstrCost(Instruction::AShr, Ty, Op1Info, 106 Op2Info, TargetTransformInfo::OP_None, 107 TargetTransformInfo::OP_None); 108 Cost += getArithmeticInstrCost(Instruction::LShr, Ty, Op1Info, Op2Info, 109 TargetTransformInfo::OP_None, 110 TargetTransformInfo::OP_None); 111 Cost += getArithmeticInstrCost(Instruction::Add, Ty, Op1Info, Op2Info, 112 TargetTransformInfo::OP_None, 113 TargetTransformInfo::OP_None); 114 115 return Cost; 116 } 117 118 static const CostTblEntry AVX2UniformConstCostTable[] = { 119 { ISD::SRA, MVT::v4i64, 4 }, // 2 x psrad + shuffle. 120 121 { ISD::SDIV, MVT::v16i16, 6 }, // vpmulhw sequence 122 { ISD::UDIV, MVT::v16i16, 6 }, // vpmulhuw sequence 123 { ISD::SDIV, MVT::v8i32, 15 }, // vpmuldq sequence 124 { ISD::UDIV, MVT::v8i32, 15 }, // vpmuludq sequence 125 }; 126 127 if (Op2Info == TargetTransformInfo::OK_UniformConstantValue && 128 ST->hasAVX2()) { 129 if (const auto *Entry = CostTableLookup(AVX2UniformConstCostTable, ISD, 130 LT.second)) 131 return LT.first * Entry->Cost; 132 } 133 134 static const CostTblEntry AVX512CostTable[] = { 135 { ISD::SHL, MVT::v16i32, 1 }, 136 { ISD::SRL, MVT::v16i32, 1 }, 137 { ISD::SRA, MVT::v16i32, 1 }, 138 { ISD::SHL, MVT::v8i64, 1 }, 139 { ISD::SRL, MVT::v8i64, 1 }, 140 { ISD::SRA, MVT::v8i64, 1 }, 141 }; 142 143 if (ST->hasAVX512()) { 144 if (const auto *Entry = CostTableLookup(AVX512CostTable, ISD, LT.second)) 145 return LT.first * Entry->Cost; 146 } 147 148 static const CostTblEntry AVX2CostTable[] = { 149 // Shifts on v4i64/v8i32 on AVX2 is legal even though we declare to 150 // customize them to detect the cases where shift amount is a scalar one. 151 { ISD::SHL, MVT::v4i32, 1 }, 152 { ISD::SRL, MVT::v4i32, 1 }, 153 { ISD::SRA, MVT::v4i32, 1 }, 154 { ISD::SHL, MVT::v8i32, 1 }, 155 { ISD::SRL, MVT::v8i32, 1 }, 156 { ISD::SRA, MVT::v8i32, 1 }, 157 { ISD::SHL, MVT::v2i64, 1 }, 158 { ISD::SRL, MVT::v2i64, 1 }, 159 { ISD::SHL, MVT::v4i64, 1 }, 160 { ISD::SRL, MVT::v4i64, 1 }, 161 }; 162 163 // Look for AVX2 lowering tricks. 164 if (ST->hasAVX2()) { 165 if (ISD == ISD::SHL && LT.second == MVT::v16i16 && 166 (Op2Info == TargetTransformInfo::OK_UniformConstantValue || 167 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue)) 168 // On AVX2, a packed v16i16 shift left by a constant build_vector 169 // is lowered into a vector multiply (vpmullw). 170 return LT.first; 171 172 if (const auto *Entry = CostTableLookup(AVX2CostTable, ISD, LT.second)) 173 return LT.first * Entry->Cost; 174 } 175 176 static const CostTblEntry XOPCostTable[] = { 177 // 128bit shifts take 1cy, but right shifts require negation beforehand. 178 { ISD::SHL, MVT::v16i8, 1 }, 179 { ISD::SRL, MVT::v16i8, 2 }, 180 { ISD::SRA, MVT::v16i8, 2 }, 181 { ISD::SHL, MVT::v8i16, 1 }, 182 { ISD::SRL, MVT::v8i16, 2 }, 183 { ISD::SRA, MVT::v8i16, 2 }, 184 { ISD::SHL, MVT::v4i32, 1 }, 185 { ISD::SRL, MVT::v4i32, 2 }, 186 { ISD::SRA, MVT::v4i32, 2 }, 187 { ISD::SHL, MVT::v2i64, 1 }, 188 { ISD::SRL, MVT::v2i64, 2 }, 189 { ISD::SRA, MVT::v2i64, 2 }, 190 // 256bit shifts require splitting if AVX2 didn't catch them above. 191 { ISD::SHL, MVT::v32i8, 2 }, 192 { ISD::SRL, MVT::v32i8, 4 }, 193 { ISD::SRA, MVT::v32i8, 4 }, 194 { ISD::SHL, MVT::v16i16, 2 }, 195 { ISD::SRL, MVT::v16i16, 4 }, 196 { ISD::SRA, MVT::v16i16, 4 }, 197 { ISD::SHL, MVT::v8i32, 2 }, 198 { ISD::SRL, MVT::v8i32, 4 }, 199 { ISD::SRA, MVT::v8i32, 4 }, 200 { ISD::SHL, MVT::v4i64, 2 }, 201 { ISD::SRL, MVT::v4i64, 4 }, 202 { ISD::SRA, MVT::v4i64, 4 }, 203 }; 204 205 // Look for XOP lowering tricks. 206 if (ST->hasXOP()) { 207 if (const auto *Entry = CostTableLookup(XOPCostTable, ISD, LT.second)) 208 return LT.first * Entry->Cost; 209 } 210 211 static const CostTblEntry AVX2CustomCostTable[] = { 212 { ISD::SHL, MVT::v32i8, 11 }, // vpblendvb sequence. 213 { ISD::SHL, MVT::v16i16, 10 }, // extend/vpsrlvd/pack sequence. 214 215 { ISD::SRL, MVT::v32i8, 11 }, // vpblendvb sequence. 216 { ISD::SRL, MVT::v16i16, 10 }, // extend/vpsrlvd/pack sequence. 217 218 { ISD::SRA, MVT::v32i8, 24 }, // vpblendvb sequence. 219 { ISD::SRA, MVT::v16i16, 10 }, // extend/vpsravd/pack sequence. 220 { ISD::SRA, MVT::v2i64, 4 }, // srl/xor/sub sequence. 221 { ISD::SRA, MVT::v4i64, 4 }, // srl/xor/sub sequence. 222 223 // Vectorizing division is a bad idea. See the SSE2 table for more comments. 224 { ISD::SDIV, MVT::v32i8, 32*20 }, 225 { ISD::SDIV, MVT::v16i16, 16*20 }, 226 { ISD::SDIV, MVT::v8i32, 8*20 }, 227 { ISD::SDIV, MVT::v4i64, 4*20 }, 228 { ISD::UDIV, MVT::v32i8, 32*20 }, 229 { ISD::UDIV, MVT::v16i16, 16*20 }, 230 { ISD::UDIV, MVT::v8i32, 8*20 }, 231 { ISD::UDIV, MVT::v4i64, 4*20 }, 232 }; 233 234 // Look for AVX2 lowering tricks for custom cases. 235 if (ST->hasAVX2()) { 236 if (const auto *Entry = CostTableLookup(AVX2CustomCostTable, ISD, 237 LT.second)) 238 return LT.first * Entry->Cost; 239 } 240 241 static const CostTblEntry 242 SSE2UniformConstCostTable[] = { 243 // We don't correctly identify costs of casts because they are marked as 244 // custom. 245 // Constant splats are cheaper for the following instructions. 246 { ISD::SHL, MVT::v16i8, 1 }, // psllw. 247 { ISD::SHL, MVT::v32i8, 2 }, // psllw. 248 { ISD::SHL, MVT::v8i16, 1 }, // psllw. 249 { ISD::SHL, MVT::v16i16, 2 }, // psllw. 250 { ISD::SHL, MVT::v4i32, 1 }, // pslld 251 { ISD::SHL, MVT::v8i32, 2 }, // pslld 252 { ISD::SHL, MVT::v2i64, 1 }, // psllq. 253 { ISD::SHL, MVT::v4i64, 2 }, // psllq. 254 255 { ISD::SRL, MVT::v16i8, 1 }, // psrlw. 256 { ISD::SRL, MVT::v32i8, 2 }, // psrlw. 257 { ISD::SRL, MVT::v8i16, 1 }, // psrlw. 258 { ISD::SRL, MVT::v16i16, 2 }, // psrlw. 259 { ISD::SRL, MVT::v4i32, 1 }, // psrld. 260 { ISD::SRL, MVT::v8i32, 2 }, // psrld. 261 { ISD::SRL, MVT::v2i64, 1 }, // psrlq. 262 { ISD::SRL, MVT::v4i64, 2 }, // psrlq. 263 264 { ISD::SRA, MVT::v16i8, 4 }, // psrlw, pand, pxor, psubb. 265 { ISD::SRA, MVT::v32i8, 8 }, // psrlw, pand, pxor, psubb. 266 { ISD::SRA, MVT::v8i16, 1 }, // psraw. 267 { ISD::SRA, MVT::v16i16, 2 }, // psraw. 268 { ISD::SRA, MVT::v4i32, 1 }, // psrad. 269 { ISD::SRA, MVT::v8i32, 2 }, // psrad. 270 { ISD::SRA, MVT::v2i64, 4 }, // 2 x psrad + shuffle. 271 { ISD::SRA, MVT::v4i64, 8 }, // 2 x psrad + shuffle. 272 273 { ISD::SDIV, MVT::v8i16, 6 }, // pmulhw sequence 274 { ISD::UDIV, MVT::v8i16, 6 }, // pmulhuw sequence 275 { ISD::SDIV, MVT::v4i32, 19 }, // pmuludq sequence 276 { ISD::UDIV, MVT::v4i32, 15 }, // pmuludq sequence 277 }; 278 279 if (Op2Info == TargetTransformInfo::OK_UniformConstantValue && 280 ST->hasSSE2()) { 281 // pmuldq sequence. 282 if (ISD == ISD::SDIV && LT.second == MVT::v4i32 && ST->hasSSE41()) 283 return LT.first * 15; 284 285 if (const auto *Entry = CostTableLookup(SSE2UniformConstCostTable, ISD, 286 LT.second)) 287 return LT.first * Entry->Cost; 288 } 289 290 if (ISD == ISD::SHL && 291 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) { 292 MVT VT = LT.second; 293 // Vector shift left by non uniform constant can be lowered 294 // into vector multiply (pmullw/pmulld). 295 if ((VT == MVT::v8i16 && ST->hasSSE2()) || 296 (VT == MVT::v4i32 && ST->hasSSE41())) 297 return LT.first; 298 299 // v16i16 and v8i32 shifts by non-uniform constants are lowered into a 300 // sequence of extract + two vector multiply + insert. 301 if ((VT == MVT::v8i32 || VT == MVT::v16i16) && 302 (ST->hasAVX() && !ST->hasAVX2())) 303 ISD = ISD::MUL; 304 305 // A vector shift left by non uniform constant is converted 306 // into a vector multiply; the new multiply is eventually 307 // lowered into a sequence of shuffles and 2 x pmuludq. 308 if (VT == MVT::v4i32 && ST->hasSSE2()) 309 ISD = ISD::MUL; 310 } 311 312 static const CostTblEntry SSE2CostTable[] = { 313 // We don't correctly identify costs of casts because they are marked as 314 // custom. 315 // For some cases, where the shift amount is a scalar we would be able 316 // to generate better code. Unfortunately, when this is the case the value 317 // (the splat) will get hoisted out of the loop, thereby making it invisible 318 // to ISel. The cost model must return worst case assumptions because it is 319 // used for vectorization and we don't want to make vectorized code worse 320 // than scalar code. 321 { ISD::SHL, MVT::v16i8, 26 }, // cmpgtb sequence. 322 { ISD::SHL, MVT::v32i8, 2*26 }, // cmpgtb sequence. 323 { ISD::SHL, MVT::v8i16, 32 }, // cmpgtb sequence. 324 { ISD::SHL, MVT::v16i16, 2*32 }, // cmpgtb sequence. 325 { ISD::SHL, MVT::v4i32, 2*5 }, // We optimized this using mul. 326 { ISD::SHL, MVT::v8i32, 2*2*5 }, // We optimized this using mul. 327 { ISD::SHL, MVT::v2i64, 4 }, // splat+shuffle sequence. 328 { ISD::SHL, MVT::v4i64, 2*4 }, // splat+shuffle sequence. 329 330 { ISD::SRL, MVT::v16i8, 26 }, // cmpgtb sequence. 331 { ISD::SRL, MVT::v32i8, 2*26 }, // cmpgtb sequence. 332 { ISD::SRL, MVT::v8i16, 32 }, // cmpgtb sequence. 333 { ISD::SRL, MVT::v16i16, 2*32 }, // cmpgtb sequence. 334 { ISD::SRL, MVT::v4i32, 16 }, // Shift each lane + blend. 335 { ISD::SRL, MVT::v8i32, 2*16 }, // Shift each lane + blend. 336 { ISD::SRL, MVT::v2i64, 4 }, // splat+shuffle sequence. 337 { ISD::SRL, MVT::v4i64, 2*4 }, // splat+shuffle sequence. 338 339 { ISD::SRA, MVT::v16i8, 54 }, // unpacked cmpgtb sequence. 340 { ISD::SRA, MVT::v32i8, 2*54 }, // unpacked cmpgtb sequence. 341 { ISD::SRA, MVT::v8i16, 32 }, // cmpgtb sequence. 342 { ISD::SRA, MVT::v16i16, 2*32 }, // cmpgtb sequence. 343 { ISD::SRA, MVT::v4i32, 16 }, // Shift each lane + blend. 344 { ISD::SRA, MVT::v8i32, 2*16 }, // Shift each lane + blend. 345 { ISD::SRA, MVT::v2i64, 12 }, // srl/xor/sub sequence. 346 { ISD::SRA, MVT::v4i64, 2*12 }, // srl/xor/sub sequence. 347 348 // It is not a good idea to vectorize division. We have to scalarize it and 349 // in the process we will often end up having to spilling regular 350 // registers. The overhead of division is going to dominate most kernels 351 // anyways so try hard to prevent vectorization of division - it is 352 // generally a bad idea. Assume somewhat arbitrarily that we have to be able 353 // to hide "20 cycles" for each lane. 354 { ISD::SDIV, MVT::v16i8, 16*20 }, 355 { ISD::SDIV, MVT::v8i16, 8*20 }, 356 { ISD::SDIV, MVT::v4i32, 4*20 }, 357 { ISD::SDIV, MVT::v2i64, 2*20 }, 358 { ISD::UDIV, MVT::v16i8, 16*20 }, 359 { ISD::UDIV, MVT::v8i16, 8*20 }, 360 { ISD::UDIV, MVT::v4i32, 4*20 }, 361 { ISD::UDIV, MVT::v2i64, 2*20 }, 362 }; 363 364 if (ST->hasSSE2()) { 365 if (const auto *Entry = CostTableLookup(SSE2CostTable, ISD, LT.second)) 366 return LT.first * Entry->Cost; 367 } 368 369 static const CostTblEntry AVX1CostTable[] = { 370 // We don't have to scalarize unsupported ops. We can issue two half-sized 371 // operations and we only need to extract the upper YMM half. 372 // Two ops + 1 extract + 1 insert = 4. 373 { ISD::MUL, MVT::v16i16, 4 }, 374 { ISD::MUL, MVT::v8i32, 4 }, 375 { ISD::SUB, MVT::v8i32, 4 }, 376 { ISD::ADD, MVT::v8i32, 4 }, 377 { ISD::SUB, MVT::v4i64, 4 }, 378 { ISD::ADD, MVT::v4i64, 4 }, 379 // A v4i64 multiply is custom lowered as two split v2i64 vectors that then 380 // are lowered as a series of long multiplies(3), shifts(4) and adds(2) 381 // Because we believe v4i64 to be a legal type, we must also include the 382 // split factor of two in the cost table. Therefore, the cost here is 18 383 // instead of 9. 384 { ISD::MUL, MVT::v4i64, 18 }, 385 }; 386 387 // Look for AVX1 lowering tricks. 388 if (ST->hasAVX() && !ST->hasAVX2()) { 389 MVT VT = LT.second; 390 391 if (const auto *Entry = CostTableLookup(AVX1CostTable, ISD, VT)) 392 return LT.first * Entry->Cost; 393 } 394 395 // Custom lowering of vectors. 396 static const CostTblEntry CustomLowered[] = { 397 // A v2i64/v4i64 and multiply is custom lowered as a series of long 398 // multiplies(3), shifts(4) and adds(2). 399 { ISD::MUL, MVT::v2i64, 9 }, 400 { ISD::MUL, MVT::v4i64, 9 }, 401 }; 402 if (const auto *Entry = CostTableLookup(CustomLowered, ISD, LT.second)) 403 return LT.first * Entry->Cost; 404 405 // Special lowering of v4i32 mul on sse2, sse3: Lower v4i32 mul as 2x shuffle, 406 // 2x pmuludq, 2x shuffle. 407 if (ISD == ISD::MUL && LT.second == MVT::v4i32 && ST->hasSSE2() && 408 !ST->hasSSE41()) 409 return LT.first * 6; 410 411 // Fallback to the default implementation. 412 return BaseT::getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info); 413 } 414 415 int X86TTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index, 416 Type *SubTp) { 417 // We only estimate the cost of reverse and alternate shuffles. 418 if (Kind != TTI::SK_Reverse && Kind != TTI::SK_Alternate) 419 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp); 420 421 if (Kind == TTI::SK_Reverse) { 422 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp); 423 int Cost = 1; 424 if (LT.second.getSizeInBits() > 128) 425 Cost = 3; // Extract + insert + copy. 426 427 // Multiple by the number of parts. 428 return Cost * LT.first; 429 } 430 431 if (Kind == TTI::SK_Alternate) { 432 // 64-bit packed float vectors (v2f32) are widened to type v4f32. 433 // 64-bit packed integer vectors (v2i32) are promoted to type v2i64. 434 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp); 435 436 // The backend knows how to generate a single VEX.256 version of 437 // instruction VPBLENDW if the target supports AVX2. 438 if (ST->hasAVX2() && LT.second == MVT::v16i16) 439 return LT.first; 440 441 static const CostTblEntry AVXAltShuffleTbl[] = { 442 {ISD::VECTOR_SHUFFLE, MVT::v4i64, 1}, // vblendpd 443 {ISD::VECTOR_SHUFFLE, MVT::v4f64, 1}, // vblendpd 444 445 {ISD::VECTOR_SHUFFLE, MVT::v8i32, 1}, // vblendps 446 {ISD::VECTOR_SHUFFLE, MVT::v8f32, 1}, // vblendps 447 448 // This shuffle is custom lowered into a sequence of: 449 // 2x vextractf128 , 2x vpblendw , 1x vinsertf128 450 {ISD::VECTOR_SHUFFLE, MVT::v16i16, 5}, 451 452 // This shuffle is custom lowered into a long sequence of: 453 // 2x vextractf128 , 4x vpshufb , 2x vpor , 1x vinsertf128 454 {ISD::VECTOR_SHUFFLE, MVT::v32i8, 9} 455 }; 456 457 if (ST->hasAVX()) 458 if (const auto *Entry = CostTableLookup(AVXAltShuffleTbl, 459 ISD::VECTOR_SHUFFLE, LT.second)) 460 return LT.first * Entry->Cost; 461 462 static const CostTblEntry SSE41AltShuffleTbl[] = { 463 // These are lowered into movsd. 464 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1}, 465 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1}, 466 467 // packed float vectors with four elements are lowered into BLENDI dag 468 // nodes. A v4i32/v4f32 BLENDI generates a single 'blendps'/'blendpd'. 469 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 1}, 470 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 1}, 471 472 // This shuffle generates a single pshufw. 473 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 1}, 474 475 // There is no instruction that matches a v16i8 alternate shuffle. 476 // The backend will expand it into the sequence 'pshufb + pshufb + or'. 477 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 3} 478 }; 479 480 if (ST->hasSSE41()) 481 if (const auto *Entry = CostTableLookup(SSE41AltShuffleTbl, ISD::VECTOR_SHUFFLE, 482 LT.second)) 483 return LT.first * Entry->Cost; 484 485 static const CostTblEntry SSSE3AltShuffleTbl[] = { 486 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1}, // movsd 487 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1}, // movsd 488 489 // SSE3 doesn't have 'blendps'. The following shuffles are expanded into 490 // the sequence 'shufps + pshufd' 491 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2}, 492 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2}, 493 494 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 3}, // pshufb + pshufb + or 495 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 3} // pshufb + pshufb + or 496 }; 497 498 if (ST->hasSSSE3()) 499 if (const auto *Entry = CostTableLookup(SSSE3AltShuffleTbl, 500 ISD::VECTOR_SHUFFLE, LT.second)) 501 return LT.first * Entry->Cost; 502 503 static const CostTblEntry SSEAltShuffleTbl[] = { 504 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1}, // movsd 505 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1}, // movsd 506 507 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2}, // shufps + pshufd 508 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2}, // shufps + pshufd 509 510 // This is expanded into a long sequence of four extract + four insert. 511 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 8}, // 4 x pextrw + 4 pinsrw. 512 513 // 8 x (pinsrw + pextrw + and + movb + movzb + or) 514 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 48} 515 }; 516 517 // Fall-back (SSE3 and SSE2). 518 if (const auto *Entry = CostTableLookup(SSEAltShuffleTbl, 519 ISD::VECTOR_SHUFFLE, LT.second)) 520 return LT.first * Entry->Cost; 521 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp); 522 } 523 524 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp); 525 } 526 527 int X86TTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) { 528 int ISD = TLI->InstructionOpcodeToISD(Opcode); 529 assert(ISD && "Invalid opcode"); 530 531 static const TypeConversionCostTblEntry AVX512DQConversionTbl[] = { 532 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 1 }, 533 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i64, 1 }, 534 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i64, 1 }, 535 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i64, 1 }, 536 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i64, 1 }, 537 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i64, 1 }, 538 539 { ISD::FP_TO_UINT, MVT::v2i64, MVT::v2f64, 1 }, 540 { ISD::FP_TO_UINT, MVT::v4i64, MVT::v4f64, 1 }, 541 { ISD::FP_TO_UINT, MVT::v8i64, MVT::v8f64, 1 }, 542 { ISD::FP_TO_UINT, MVT::v2i64, MVT::v2f32, 1 }, 543 { ISD::FP_TO_UINT, MVT::v4i64, MVT::v4f32, 1 }, 544 { ISD::FP_TO_UINT, MVT::v8i64, MVT::v8f32, 1 }, 545 }; 546 547 static const TypeConversionCostTblEntry AVX512FConversionTbl[] = { 548 { ISD::FP_EXTEND, MVT::v8f64, MVT::v8f32, 1 }, 549 { ISD::FP_EXTEND, MVT::v8f64, MVT::v16f32, 3 }, 550 { ISD::FP_ROUND, MVT::v8f32, MVT::v8f64, 1 }, 551 552 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i32, 1 }, 553 { ISD::TRUNCATE, MVT::v16i16, MVT::v16i32, 1 }, 554 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i64, 1 }, 555 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 1 }, 556 557 // v16i1 -> v16i32 - load + broadcast 558 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i1, 2 }, 559 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i1, 2 }, 560 561 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 1 }, 562 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 1 }, 563 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i16, 1 }, 564 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i16, 1 }, 565 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i32, 1 }, 566 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i32, 1 }, 567 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i16, 1 }, 568 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i16, 1 }, 569 570 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i1, 3 }, 571 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i8, 2 }, 572 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i16, 2 }, 573 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i32, 1 }, 574 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i1, 4 }, 575 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i8, 2 }, 576 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i16, 2 }, 577 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i32, 1 }, 578 579 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i1, 3 }, 580 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i8, 2 }, 581 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i16, 2 }, 582 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i32, 1 }, 583 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 1 }, 584 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 }, 585 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i1, 4 }, 586 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i16, 2 }, 587 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i32, 1 }, 588 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i8, 2 }, 589 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 2 }, 590 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 2 }, 591 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i8, 2 }, 592 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i16, 2 }, 593 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i32, 1 }, 594 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i8, 2 }, 595 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i16, 5 }, 596 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i32, 2 }, 597 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 5 }, 598 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i64, 12 }, 599 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i64, 26 }, 600 601 { ISD::FP_TO_UINT, MVT::v2i32, MVT::v2f32, 1 }, 602 { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f32, 1 }, 603 { ISD::FP_TO_UINT, MVT::v8i32, MVT::v8f32, 1 }, 604 { ISD::FP_TO_UINT, MVT::v16i32, MVT::v16f32, 1 }, 605 }; 606 607 static const TypeConversionCostTblEntry AVX2ConversionTbl[] = { 608 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 1 }, 609 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 1 }, 610 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 3 }, 611 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 3 }, 612 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 3 }, 613 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 3 }, 614 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 1 }, 615 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 1 }, 616 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 3 }, 617 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 3 }, 618 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 3 }, 619 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 3 }, 620 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 3 }, 621 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 }, 622 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 1 }, 623 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 1 }, 624 625 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 2 }, 626 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 2 }, 627 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 2 }, 628 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 2 }, 629 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 2 }, 630 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 4 }, 631 632 { ISD::FP_EXTEND, MVT::v8f64, MVT::v8f32, 3 }, 633 { ISD::FP_ROUND, MVT::v8f32, MVT::v8f64, 3 }, 634 635 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 8 }, 636 }; 637 638 static const TypeConversionCostTblEntry AVXConversionTbl[] = { 639 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 4 }, 640 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 4 }, 641 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 7 }, 642 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 4 }, 643 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 7 }, 644 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 4 }, 645 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 4 }, 646 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 4 }, 647 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 6 }, 648 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 4 }, 649 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 6 }, 650 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 4 }, 651 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 6 }, 652 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 }, 653 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 4 }, 654 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 4 }, 655 656 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 4 }, 657 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 4 }, 658 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 4 }, 659 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 4 }, 660 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 5 }, 661 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i16, 4 }, 662 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 9 }, 663 664 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i1, 8 }, 665 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i8, 8 }, 666 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 }, 667 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i32, 1 }, 668 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i1, 3 }, 669 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 }, 670 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i16, 3 }, 671 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 }, 672 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i1, 3 }, 673 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i8, 3 }, 674 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i16, 3 }, 675 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i32, 1 }, 676 677 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i1, 6 }, 678 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 5 }, 679 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 }, 680 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 9 }, 681 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i1, 7 }, 682 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 2 }, 683 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 }, 684 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 6 }, 685 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i1, 7 }, 686 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i8, 2 }, 687 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i16, 2 }, 688 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i32, 6 }, 689 // The generic code to compute the scalar overhead is currently broken. 690 // Workaround this limitation by estimating the scalarization overhead 691 // here. We have roughly 10 instructions per scalar element. 692 // Multiply that by the vector width. 693 // FIXME: remove that when PR19268 is fixed. 694 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 }, 695 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i64, 4*10 }, 696 697 { ISD::FP_TO_SINT, MVT::v8i8, MVT::v8f32, 7 }, 698 { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 1 }, 699 // This node is expanded into scalarized operations but BasicTTI is overly 700 // optimistic estimating its cost. It computes 3 per element (one 701 // vector-extract, one scalar conversion and one vector-insert). The 702 // problem is that the inserts form a read-modify-write chain so latency 703 // should be factored in too. Inflating the cost per element by 1. 704 { ISD::FP_TO_UINT, MVT::v8i32, MVT::v8f32, 8*4 }, 705 { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f64, 4*4 }, 706 }; 707 708 static const TypeConversionCostTblEntry SSE2ConvTbl[] = { 709 // These are somewhat magic numbers justified by looking at the output of 710 // Intel's IACA, running some kernels and making sure when we take 711 // legalization into account the throughput will be overestimated. 712 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 }, 713 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 }, 714 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 }, 715 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 }, 716 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 }, 717 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 }, 718 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 }, 719 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 }, 720 // There are faster sequences for float conversions. 721 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 }, 722 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 8 }, 723 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 }, 724 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 }, 725 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 }, 726 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 15 }, 727 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 }, 728 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 }, 729 }; 730 731 std::pair<int, MVT> LTSrc = TLI->getTypeLegalizationCost(DL, Src); 732 std::pair<int, MVT> LTDest = TLI->getTypeLegalizationCost(DL, Dst); 733 734 if (ST->hasSSE2() && !ST->hasAVX()) { 735 if (const auto *Entry = ConvertCostTableLookup(SSE2ConvTbl, ISD, 736 LTDest.second, LTSrc.second)) 737 return LTSrc.first * Entry->Cost; 738 } 739 740 EVT SrcTy = TLI->getValueType(DL, Src); 741 EVT DstTy = TLI->getValueType(DL, Dst); 742 743 // The function getSimpleVT only handles simple value types. 744 if (!SrcTy.isSimple() || !DstTy.isSimple()) 745 return BaseT::getCastInstrCost(Opcode, Dst, Src); 746 747 if (ST->hasDQI()) 748 if (const auto *Entry = ConvertCostTableLookup(AVX512DQConversionTbl, ISD, 749 DstTy.getSimpleVT(), 750 SrcTy.getSimpleVT())) 751 return Entry->Cost; 752 753 if (ST->hasAVX512()) 754 if (const auto *Entry = ConvertCostTableLookup(AVX512FConversionTbl, ISD, 755 DstTy.getSimpleVT(), 756 SrcTy.getSimpleVT())) 757 return Entry->Cost; 758 759 if (ST->hasAVX2()) { 760 if (const auto *Entry = ConvertCostTableLookup(AVX2ConversionTbl, ISD, 761 DstTy.getSimpleVT(), 762 SrcTy.getSimpleVT())) 763 return Entry->Cost; 764 } 765 766 if (ST->hasAVX()) { 767 if (const auto *Entry = ConvertCostTableLookup(AVXConversionTbl, ISD, 768 DstTy.getSimpleVT(), 769 SrcTy.getSimpleVT())) 770 return Entry->Cost; 771 } 772 773 return BaseT::getCastInstrCost(Opcode, Dst, Src); 774 } 775 776 int X86TTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy) { 777 // Legalize the type. 778 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy); 779 780 MVT MTy = LT.second; 781 782 int ISD = TLI->InstructionOpcodeToISD(Opcode); 783 assert(ISD && "Invalid opcode"); 784 785 static const CostTblEntry SSE42CostTbl[] = { 786 { ISD::SETCC, MVT::v2f64, 1 }, 787 { ISD::SETCC, MVT::v4f32, 1 }, 788 { ISD::SETCC, MVT::v2i64, 1 }, 789 { ISD::SETCC, MVT::v4i32, 1 }, 790 { ISD::SETCC, MVT::v8i16, 1 }, 791 { ISD::SETCC, MVT::v16i8, 1 }, 792 }; 793 794 static const CostTblEntry AVX1CostTbl[] = { 795 { ISD::SETCC, MVT::v4f64, 1 }, 796 { ISD::SETCC, MVT::v8f32, 1 }, 797 // AVX1 does not support 8-wide integer compare. 798 { ISD::SETCC, MVT::v4i64, 4 }, 799 { ISD::SETCC, MVT::v8i32, 4 }, 800 { ISD::SETCC, MVT::v16i16, 4 }, 801 { ISD::SETCC, MVT::v32i8, 4 }, 802 }; 803 804 static const CostTblEntry AVX2CostTbl[] = { 805 { ISD::SETCC, MVT::v4i64, 1 }, 806 { ISD::SETCC, MVT::v8i32, 1 }, 807 { ISD::SETCC, MVT::v16i16, 1 }, 808 { ISD::SETCC, MVT::v32i8, 1 }, 809 }; 810 811 static const CostTblEntry AVX512CostTbl[] = { 812 { ISD::SETCC, MVT::v8i64, 1 }, 813 { ISD::SETCC, MVT::v16i32, 1 }, 814 { ISD::SETCC, MVT::v8f64, 1 }, 815 { ISD::SETCC, MVT::v16f32, 1 }, 816 }; 817 818 if (ST->hasAVX512()) 819 if (const auto *Entry = CostTableLookup(AVX512CostTbl, ISD, MTy)) 820 return LT.first * Entry->Cost; 821 822 if (ST->hasAVX2()) 823 if (const auto *Entry = CostTableLookup(AVX2CostTbl, ISD, MTy)) 824 return LT.first * Entry->Cost; 825 826 if (ST->hasAVX()) 827 if (const auto *Entry = CostTableLookup(AVX1CostTbl, ISD, MTy)) 828 return LT.first * Entry->Cost; 829 830 if (ST->hasSSE42()) 831 if (const auto *Entry = CostTableLookup(SSE42CostTbl, ISD, MTy)) 832 return LT.first * Entry->Cost; 833 834 return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy); 835 } 836 837 int X86TTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) { 838 assert(Val->isVectorTy() && "This must be a vector type"); 839 840 if (Index != -1U) { 841 // Legalize the type. 842 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Val); 843 844 // This type is legalized to a scalar type. 845 if (!LT.second.isVector()) 846 return 0; 847 848 // The type may be split. Normalize the index to the new type. 849 unsigned Width = LT.second.getVectorNumElements(); 850 Index = Index % Width; 851 852 // Floating point scalars are already located in index #0. 853 if (Val->getScalarType()->isFloatingPointTy() && Index == 0) 854 return 0; 855 } 856 857 return BaseT::getVectorInstrCost(Opcode, Val, Index); 858 } 859 860 int X86TTIImpl::getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) { 861 assert (Ty->isVectorTy() && "Can only scalarize vectors"); 862 int Cost = 0; 863 864 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) { 865 if (Insert) 866 Cost += getVectorInstrCost(Instruction::InsertElement, Ty, i); 867 if (Extract) 868 Cost += getVectorInstrCost(Instruction::ExtractElement, Ty, i); 869 } 870 871 return Cost; 872 } 873 874 int X86TTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment, 875 unsigned AddressSpace) { 876 // Handle non-power-of-two vectors such as <3 x float> 877 if (VectorType *VTy = dyn_cast<VectorType>(Src)) { 878 unsigned NumElem = VTy->getVectorNumElements(); 879 880 // Handle a few common cases: 881 // <3 x float> 882 if (NumElem == 3 && VTy->getScalarSizeInBits() == 32) 883 // Cost = 64 bit store + extract + 32 bit store. 884 return 3; 885 886 // <3 x double> 887 if (NumElem == 3 && VTy->getScalarSizeInBits() == 64) 888 // Cost = 128 bit store + unpack + 64 bit store. 889 return 3; 890 891 // Assume that all other non-power-of-two numbers are scalarized. 892 if (!isPowerOf2_32(NumElem)) { 893 int Cost = BaseT::getMemoryOpCost(Opcode, VTy->getScalarType(), Alignment, 894 AddressSpace); 895 int SplitCost = getScalarizationOverhead(Src, Opcode == Instruction::Load, 896 Opcode == Instruction::Store); 897 return NumElem * Cost + SplitCost; 898 } 899 } 900 901 // Legalize the type. 902 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src); 903 assert((Opcode == Instruction::Load || Opcode == Instruction::Store) && 904 "Invalid Opcode"); 905 906 // Each load/store unit costs 1. 907 int Cost = LT.first * 1; 908 909 // On Sandybridge 256bit load/stores are double pumped 910 // (but not on Haswell). 911 if (LT.second.getSizeInBits() > 128 && !ST->hasAVX2()) 912 Cost*=2; 913 914 return Cost; 915 } 916 917 int X86TTIImpl::getMaskedMemoryOpCost(unsigned Opcode, Type *SrcTy, 918 unsigned Alignment, 919 unsigned AddressSpace) { 920 VectorType *SrcVTy = dyn_cast<VectorType>(SrcTy); 921 if (!SrcVTy) 922 // To calculate scalar take the regular cost, without mask 923 return getMemoryOpCost(Opcode, SrcTy, Alignment, AddressSpace); 924 925 unsigned NumElem = SrcVTy->getVectorNumElements(); 926 VectorType *MaskTy = 927 VectorType::get(Type::getInt8Ty(getGlobalContext()), NumElem); 928 if ((Opcode == Instruction::Load && !isLegalMaskedLoad(SrcVTy)) || 929 (Opcode == Instruction::Store && !isLegalMaskedStore(SrcVTy)) || 930 !isPowerOf2_32(NumElem)) { 931 // Scalarization 932 int MaskSplitCost = getScalarizationOverhead(MaskTy, false, true); 933 int ScalarCompareCost = getCmpSelInstrCost( 934 Instruction::ICmp, Type::getInt8Ty(getGlobalContext()), nullptr); 935 int BranchCost = getCFInstrCost(Instruction::Br); 936 int MaskCmpCost = NumElem * (BranchCost + ScalarCompareCost); 937 938 int ValueSplitCost = getScalarizationOverhead( 939 SrcVTy, Opcode == Instruction::Load, Opcode == Instruction::Store); 940 int MemopCost = 941 NumElem * BaseT::getMemoryOpCost(Opcode, SrcVTy->getScalarType(), 942 Alignment, AddressSpace); 943 return MemopCost + ValueSplitCost + MaskSplitCost + MaskCmpCost; 944 } 945 946 // Legalize the type. 947 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, SrcVTy); 948 auto VT = TLI->getValueType(DL, SrcVTy); 949 int Cost = 0; 950 if (VT.isSimple() && LT.second != VT.getSimpleVT() && 951 LT.second.getVectorNumElements() == NumElem) 952 // Promotion requires expand/truncate for data and a shuffle for mask. 953 Cost += getShuffleCost(TTI::SK_Alternate, SrcVTy, 0, nullptr) + 954 getShuffleCost(TTI::SK_Alternate, MaskTy, 0, nullptr); 955 956 else if (LT.second.getVectorNumElements() > NumElem) { 957 VectorType *NewMaskTy = VectorType::get(MaskTy->getVectorElementType(), 958 LT.second.getVectorNumElements()); 959 // Expanding requires fill mask with zeroes 960 Cost += getShuffleCost(TTI::SK_InsertSubvector, NewMaskTy, 0, MaskTy); 961 } 962 if (!ST->hasAVX512()) 963 return Cost + LT.first*4; // Each maskmov costs 4 964 965 // AVX-512 masked load/store is cheapper 966 return Cost+LT.first; 967 } 968 969 int X86TTIImpl::getAddressComputationCost(Type *Ty, bool IsComplex) { 970 // Address computations in vectorized code with non-consecutive addresses will 971 // likely result in more instructions compared to scalar code where the 972 // computation can more often be merged into the index mode. The resulting 973 // extra micro-ops can significantly decrease throughput. 974 unsigned NumVectorInstToHideOverhead = 10; 975 976 if (Ty->isVectorTy() && IsComplex) 977 return NumVectorInstToHideOverhead; 978 979 return BaseT::getAddressComputationCost(Ty, IsComplex); 980 } 981 982 int X86TTIImpl::getReductionCost(unsigned Opcode, Type *ValTy, 983 bool IsPairwise) { 984 985 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy); 986 987 MVT MTy = LT.second; 988 989 int ISD = TLI->InstructionOpcodeToISD(Opcode); 990 assert(ISD && "Invalid opcode"); 991 992 // We use the Intel Architecture Code Analyzer(IACA) to measure the throughput 993 // and make it as the cost. 994 995 static const CostTblEntry SSE42CostTblPairWise[] = { 996 { ISD::FADD, MVT::v2f64, 2 }, 997 { ISD::FADD, MVT::v4f32, 4 }, 998 { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6". 999 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5". 1000 { ISD::ADD, MVT::v8i16, 5 }, 1001 }; 1002 1003 static const CostTblEntry AVX1CostTblPairWise[] = { 1004 { ISD::FADD, MVT::v4f32, 4 }, 1005 { ISD::FADD, MVT::v4f64, 5 }, 1006 { ISD::FADD, MVT::v8f32, 7 }, 1007 { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5". 1008 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5". 1009 { ISD::ADD, MVT::v4i64, 5 }, // The data reported by the IACA tool is "4.8". 1010 { ISD::ADD, MVT::v8i16, 5 }, 1011 { ISD::ADD, MVT::v8i32, 5 }, 1012 }; 1013 1014 static const CostTblEntry SSE42CostTblNoPairWise[] = { 1015 { ISD::FADD, MVT::v2f64, 2 }, 1016 { ISD::FADD, MVT::v4f32, 4 }, 1017 { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6". 1018 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.3". 1019 { ISD::ADD, MVT::v8i16, 4 }, // The data reported by the IACA tool is "4.3". 1020 }; 1021 1022 static const CostTblEntry AVX1CostTblNoPairWise[] = { 1023 { ISD::FADD, MVT::v4f32, 3 }, 1024 { ISD::FADD, MVT::v4f64, 3 }, 1025 { ISD::FADD, MVT::v8f32, 4 }, 1026 { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5". 1027 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "2.8". 1028 { ISD::ADD, MVT::v4i64, 3 }, 1029 { ISD::ADD, MVT::v8i16, 4 }, 1030 { ISD::ADD, MVT::v8i32, 5 }, 1031 }; 1032 1033 if (IsPairwise) { 1034 if (ST->hasAVX()) 1035 if (const auto *Entry = CostTableLookup(AVX1CostTblPairWise, ISD, MTy)) 1036 return LT.first * Entry->Cost; 1037 1038 if (ST->hasSSE42()) 1039 if (const auto *Entry = CostTableLookup(SSE42CostTblPairWise, ISD, MTy)) 1040 return LT.first * Entry->Cost; 1041 } else { 1042 if (ST->hasAVX()) 1043 if (const auto *Entry = CostTableLookup(AVX1CostTblNoPairWise, ISD, MTy)) 1044 return LT.first * Entry->Cost; 1045 1046 if (ST->hasSSE42()) 1047 if (const auto *Entry = CostTableLookup(SSE42CostTblNoPairWise, ISD, MTy)) 1048 return LT.first * Entry->Cost; 1049 } 1050 1051 return BaseT::getReductionCost(Opcode, ValTy, IsPairwise); 1052 } 1053 1054 /// \brief Calculate the cost of materializing a 64-bit value. This helper 1055 /// method might only calculate a fraction of a larger immediate. Therefore it 1056 /// is valid to return a cost of ZERO. 1057 int X86TTIImpl::getIntImmCost(int64_t Val) { 1058 if (Val == 0) 1059 return TTI::TCC_Free; 1060 1061 if (isInt<32>(Val)) 1062 return TTI::TCC_Basic; 1063 1064 return 2 * TTI::TCC_Basic; 1065 } 1066 1067 int X86TTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) { 1068 assert(Ty->isIntegerTy()); 1069 1070 unsigned BitSize = Ty->getPrimitiveSizeInBits(); 1071 if (BitSize == 0) 1072 return ~0U; 1073 1074 // Never hoist constants larger than 128bit, because this might lead to 1075 // incorrect code generation or assertions in codegen. 1076 // Fixme: Create a cost model for types larger than i128 once the codegen 1077 // issues have been fixed. 1078 if (BitSize > 128) 1079 return TTI::TCC_Free; 1080 1081 if (Imm == 0) 1082 return TTI::TCC_Free; 1083 1084 // Sign-extend all constants to a multiple of 64-bit. 1085 APInt ImmVal = Imm; 1086 if (BitSize & 0x3f) 1087 ImmVal = Imm.sext((BitSize + 63) & ~0x3fU); 1088 1089 // Split the constant into 64-bit chunks and calculate the cost for each 1090 // chunk. 1091 int Cost = 0; 1092 for (unsigned ShiftVal = 0; ShiftVal < BitSize; ShiftVal += 64) { 1093 APInt Tmp = ImmVal.ashr(ShiftVal).sextOrTrunc(64); 1094 int64_t Val = Tmp.getSExtValue(); 1095 Cost += getIntImmCost(Val); 1096 } 1097 // We need at least one instruction to materialze the constant. 1098 return std::max(1, Cost); 1099 } 1100 1101 int X86TTIImpl::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm, 1102 Type *Ty) { 1103 assert(Ty->isIntegerTy()); 1104 1105 unsigned BitSize = Ty->getPrimitiveSizeInBits(); 1106 // There is no cost model for constants with a bit size of 0. Return TCC_Free 1107 // here, so that constant hoisting will ignore this constant. 1108 if (BitSize == 0) 1109 return TTI::TCC_Free; 1110 1111 unsigned ImmIdx = ~0U; 1112 switch (Opcode) { 1113 default: 1114 return TTI::TCC_Free; 1115 case Instruction::GetElementPtr: 1116 // Always hoist the base address of a GetElementPtr. This prevents the 1117 // creation of new constants for every base constant that gets constant 1118 // folded with the offset. 1119 if (Idx == 0) 1120 return 2 * TTI::TCC_Basic; 1121 return TTI::TCC_Free; 1122 case Instruction::Store: 1123 ImmIdx = 0; 1124 break; 1125 case Instruction::And: 1126 // We support 64-bit ANDs with immediates with 32-bits of leading zeroes 1127 // by using a 32-bit operation with implicit zero extension. Detect such 1128 // immediates here as the normal path expects bit 31 to be sign extended. 1129 if (Idx == 1 && Imm.getBitWidth() == 64 && isUInt<32>(Imm.getZExtValue())) 1130 return TTI::TCC_Free; 1131 // Fallthrough 1132 case Instruction::Add: 1133 case Instruction::Sub: 1134 case Instruction::Mul: 1135 case Instruction::UDiv: 1136 case Instruction::SDiv: 1137 case Instruction::URem: 1138 case Instruction::SRem: 1139 case Instruction::Or: 1140 case Instruction::Xor: 1141 case Instruction::ICmp: 1142 ImmIdx = 1; 1143 break; 1144 // Always return TCC_Free for the shift value of a shift instruction. 1145 case Instruction::Shl: 1146 case Instruction::LShr: 1147 case Instruction::AShr: 1148 if (Idx == 1) 1149 return TTI::TCC_Free; 1150 break; 1151 case Instruction::Trunc: 1152 case Instruction::ZExt: 1153 case Instruction::SExt: 1154 case Instruction::IntToPtr: 1155 case Instruction::PtrToInt: 1156 case Instruction::BitCast: 1157 case Instruction::PHI: 1158 case Instruction::Call: 1159 case Instruction::Select: 1160 case Instruction::Ret: 1161 case Instruction::Load: 1162 break; 1163 } 1164 1165 if (Idx == ImmIdx) { 1166 int NumConstants = (BitSize + 63) / 64; 1167 int Cost = X86TTIImpl::getIntImmCost(Imm, Ty); 1168 return (Cost <= NumConstants * TTI::TCC_Basic) 1169 ? static_cast<int>(TTI::TCC_Free) 1170 : Cost; 1171 } 1172 1173 return X86TTIImpl::getIntImmCost(Imm, Ty); 1174 } 1175 1176 int X86TTIImpl::getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm, 1177 Type *Ty) { 1178 assert(Ty->isIntegerTy()); 1179 1180 unsigned BitSize = Ty->getPrimitiveSizeInBits(); 1181 // There is no cost model for constants with a bit size of 0. Return TCC_Free 1182 // here, so that constant hoisting will ignore this constant. 1183 if (BitSize == 0) 1184 return TTI::TCC_Free; 1185 1186 switch (IID) { 1187 default: 1188 return TTI::TCC_Free; 1189 case Intrinsic::sadd_with_overflow: 1190 case Intrinsic::uadd_with_overflow: 1191 case Intrinsic::ssub_with_overflow: 1192 case Intrinsic::usub_with_overflow: 1193 case Intrinsic::smul_with_overflow: 1194 case Intrinsic::umul_with_overflow: 1195 if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue())) 1196 return TTI::TCC_Free; 1197 break; 1198 case Intrinsic::experimental_stackmap: 1199 if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue()))) 1200 return TTI::TCC_Free; 1201 break; 1202 case Intrinsic::experimental_patchpoint_void: 1203 case Intrinsic::experimental_patchpoint_i64: 1204 if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue()))) 1205 return TTI::TCC_Free; 1206 break; 1207 } 1208 return X86TTIImpl::getIntImmCost(Imm, Ty); 1209 } 1210 1211 bool X86TTIImpl::isLegalMaskedLoad(Type *DataTy) { 1212 Type *ScalarTy = DataTy->getScalarType(); 1213 int DataWidth = isa<PointerType>(ScalarTy) ? 1214 DL.getPointerSizeInBits() : ScalarTy->getPrimitiveSizeInBits(); 1215 1216 return (DataWidth >= 32 && ST->hasAVX2()); 1217 } 1218 1219 bool X86TTIImpl::isLegalMaskedStore(Type *DataType) { 1220 return isLegalMaskedLoad(DataType); 1221 } 1222 1223 bool X86TTIImpl::isLegalMaskedGather(Type *DataTy) { 1224 // This function is called now in two cases: from the Loop Vectorizer 1225 // and from the Scalarizer. 1226 // When the Loop Vectorizer asks about legality of the feature, 1227 // the vectorization factor is not calculated yet. The Loop Vectorizer 1228 // sends a scalar type and the decision is based on the width of the 1229 // scalar element. 1230 // Later on, the cost model will estimate usage this intrinsic based on 1231 // the vector type. 1232 // The Scalarizer asks again about legality. It sends a vector type. 1233 // In this case we can reject non-power-of-2 vectors. 1234 if (isa<VectorType>(DataTy) && !isPowerOf2_32(DataTy->getVectorNumElements())) 1235 return false; 1236 Type *ScalarTy = DataTy->getScalarType(); 1237 int DataWidth = isa<PointerType>(ScalarTy) ? 1238 DL.getPointerSizeInBits() : ScalarTy->getPrimitiveSizeInBits(); 1239 1240 // AVX-512 allows gather and scatter 1241 return DataWidth >= 32 && ST->hasAVX512(); 1242 } 1243 1244 bool X86TTIImpl::isLegalMaskedScatter(Type *DataType) { 1245 return isLegalMaskedGather(DataType); 1246 } 1247 1248 bool X86TTIImpl::areInlineCompatible(const Function *Caller, 1249 const Function *Callee) const { 1250 const TargetMachine &TM = getTLI()->getTargetMachine(); 1251 1252 // Work this as a subsetting of subtarget features. 1253 const FeatureBitset &CallerBits = 1254 TM.getSubtargetImpl(*Caller)->getFeatureBits(); 1255 const FeatureBitset &CalleeBits = 1256 TM.getSubtargetImpl(*Callee)->getFeatureBits(); 1257 1258 // FIXME: This is likely too limiting as it will include subtarget features 1259 // that we might not care about for inlining, but it is conservatively 1260 // correct. 1261 return (CallerBits & CalleeBits) == CalleeBits; 1262 } 1263