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 /// About Cost Model numbers used below it's necessary to say the following: 17 /// the numbers correspond to some "generic" X86 CPU instead of usage of 18 /// concrete CPU model. Usually the numbers correspond to CPU where the feature 19 /// apeared at the first time. For example, if we do Subtarget.hasSSE42() in 20 /// the lookups below the cost is based on Nehalem as that was the first CPU 21 /// to support that feature level and thus has most likely the worst case cost. 22 /// Some examples of other technologies/CPUs: 23 /// SSE 3 - Pentium4 / Athlon64 24 /// SSE 4.1 - Penryn 25 /// SSE 4.2 - Nehalem 26 /// AVX - Sandy Bridge 27 /// AVX2 - Haswell 28 /// AVX-512 - Xeon Phi / Skylake 29 /// And some examples of instruction target dependent costs (latency) 30 /// divss sqrtss rsqrtss 31 /// AMD K7 11-16 19 3 32 /// Piledriver 9-24 13-15 5 33 /// Jaguar 14 16 2 34 /// Pentium II,III 18 30 2 35 /// Nehalem 7-14 7-18 3 36 /// Haswell 10-13 11 5 37 /// TODO: Develop and implement the target dependent cost model and 38 /// specialize cost numbers for different Cost Model Targets such as throughput, 39 /// code size, latency and uop count. 40 //===----------------------------------------------------------------------===// 41 42 #include "X86TargetTransformInfo.h" 43 #include "llvm/Analysis/TargetTransformInfo.h" 44 #include "llvm/CodeGen/BasicTTIImpl.h" 45 #include "llvm/IR/IntrinsicInst.h" 46 #include "llvm/Support/Debug.h" 47 #include "llvm/Target/CostTable.h" 48 #include "llvm/Target/TargetLowering.h" 49 50 using namespace llvm; 51 52 #define DEBUG_TYPE "x86tti" 53 54 //===----------------------------------------------------------------------===// 55 // 56 // X86 cost model. 57 // 58 //===----------------------------------------------------------------------===// 59 60 TargetTransformInfo::PopcntSupportKind 61 X86TTIImpl::getPopcntSupport(unsigned TyWidth) { 62 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2"); 63 // TODO: Currently the __builtin_popcount() implementation using SSE3 64 // instructions is inefficient. Once the problem is fixed, we should 65 // call ST->hasSSE3() instead of ST->hasPOPCNT(). 66 return ST->hasPOPCNT() ? TTI::PSK_FastHardware : TTI::PSK_Software; 67 } 68 69 unsigned X86TTIImpl::getNumberOfRegisters(bool Vector) { 70 if (Vector && !ST->hasSSE1()) 71 return 0; 72 73 if (ST->is64Bit()) { 74 if (Vector && ST->hasAVX512()) 75 return 32; 76 return 16; 77 } 78 return 8; 79 } 80 81 unsigned X86TTIImpl::getRegisterBitWidth(bool Vector) { 82 if (Vector) { 83 if (ST->hasAVX512()) return 512; 84 if (ST->hasAVX()) return 256; 85 if (ST->hasSSE1()) return 128; 86 return 0; 87 } 88 89 if (ST->is64Bit()) 90 return 64; 91 92 return 32; 93 } 94 95 unsigned X86TTIImpl::getMaxInterleaveFactor(unsigned VF) { 96 // If the loop will not be vectorized, don't interleave the loop. 97 // Let regular unroll to unroll the loop, which saves the overflow 98 // check and memory check cost. 99 if (VF == 1) 100 return 1; 101 102 if (ST->isAtom()) 103 return 1; 104 105 // Sandybridge and Haswell have multiple execution ports and pipelined 106 // vector units. 107 if (ST->hasAVX()) 108 return 4; 109 110 return 2; 111 } 112 113 int X86TTIImpl::getArithmeticInstrCost( 114 unsigned Opcode, Type *Ty, TTI::OperandValueKind Op1Info, 115 TTI::OperandValueKind Op2Info, TTI::OperandValueProperties Opd1PropInfo, 116 TTI::OperandValueProperties Opd2PropInfo) { 117 // Legalize the type. 118 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty); 119 120 int ISD = TLI->InstructionOpcodeToISD(Opcode); 121 assert(ISD && "Invalid opcode"); 122 123 if (ISD == ISD::SDIV && 124 Op2Info == TargetTransformInfo::OK_UniformConstantValue && 125 Opd2PropInfo == TargetTransformInfo::OP_PowerOf2) { 126 // On X86, vector signed division by constants power-of-two are 127 // normally expanded to the sequence SRA + SRL + ADD + SRA. 128 // The OperandValue properties many not be same as that of previous 129 // operation;conservatively assume OP_None. 130 int Cost = 2 * getArithmeticInstrCost(Instruction::AShr, Ty, Op1Info, 131 Op2Info, TargetTransformInfo::OP_None, 132 TargetTransformInfo::OP_None); 133 Cost += getArithmeticInstrCost(Instruction::LShr, Ty, Op1Info, Op2Info, 134 TargetTransformInfo::OP_None, 135 TargetTransformInfo::OP_None); 136 Cost += getArithmeticInstrCost(Instruction::Add, Ty, Op1Info, Op2Info, 137 TargetTransformInfo::OP_None, 138 TargetTransformInfo::OP_None); 139 140 return Cost; 141 } 142 143 static const CostTblEntry AVX512BWUniformConstCostTable[] = { 144 { ISD::SDIV, MVT::v32i16, 6 }, // vpmulhw sequence 145 { ISD::UDIV, MVT::v32i16, 6 }, // vpmulhuw sequence 146 }; 147 148 if (Op2Info == TargetTransformInfo::OK_UniformConstantValue && 149 ST->hasBWI()) { 150 if (const auto *Entry = CostTableLookup(AVX512BWUniformConstCostTable, ISD, 151 LT.second)) 152 return LT.first * Entry->Cost; 153 } 154 155 static const CostTblEntry AVX512UniformConstCostTable[] = { 156 { ISD::SDIV, MVT::v16i32, 15 }, // vpmuldq sequence 157 { ISD::UDIV, MVT::v16i32, 15 }, // vpmuludq sequence 158 }; 159 160 if (Op2Info == TargetTransformInfo::OK_UniformConstantValue && 161 ST->hasAVX512()) { 162 if (const auto *Entry = CostTableLookup(AVX512UniformConstCostTable, ISD, 163 LT.second)) 164 return LT.first * Entry->Cost; 165 } 166 167 static const CostTblEntry AVX2UniformConstCostTable[] = { 168 { ISD::SRA, MVT::v4i64, 4 }, // 2 x psrad + shuffle. 169 170 { ISD::SDIV, MVT::v16i16, 6 }, // vpmulhw sequence 171 { ISD::UDIV, MVT::v16i16, 6 }, // vpmulhuw sequence 172 { ISD::SDIV, MVT::v8i32, 15 }, // vpmuldq sequence 173 { ISD::UDIV, MVT::v8i32, 15 }, // vpmuludq sequence 174 }; 175 176 if (Op2Info == TargetTransformInfo::OK_UniformConstantValue && 177 ST->hasAVX2()) { 178 if (const auto *Entry = CostTableLookup(AVX2UniformConstCostTable, ISD, 179 LT.second)) 180 return LT.first * Entry->Cost; 181 } 182 183 static const CostTblEntry SSE2UniformConstCostTable[] = { 184 { ISD::SDIV, MVT::v16i16, 12 }, // pmulhw sequence 185 { ISD::SDIV, MVT::v8i16, 6 }, // pmulhw sequence 186 { ISD::UDIV, MVT::v16i16, 12 }, // pmulhuw sequence 187 { ISD::UDIV, MVT::v8i16, 6 }, // pmulhuw sequence 188 { ISD::SDIV, MVT::v8i32, 38 }, // pmuludq sequence 189 { ISD::SDIV, MVT::v4i32, 19 }, // pmuludq sequence 190 { ISD::UDIV, MVT::v8i32, 30 }, // pmuludq sequence 191 { ISD::UDIV, MVT::v4i32, 15 }, // pmuludq sequence 192 }; 193 194 if (Op2Info == TargetTransformInfo::OK_UniformConstantValue && 195 ST->hasSSE2()) { 196 // pmuldq sequence. 197 if (ISD == ISD::SDIV && LT.second == MVT::v8i32 && ST->hasAVX()) 198 return LT.first * 30; 199 if (ISD == ISD::SDIV && LT.second == MVT::v4i32 && ST->hasSSE41()) 200 return LT.first * 15; 201 202 if (const auto *Entry = CostTableLookup(SSE2UniformConstCostTable, ISD, 203 LT.second)) 204 return LT.first * Entry->Cost; 205 } 206 207 static const CostTblEntry AVX512DQCostTable[] = { 208 { ISD::MUL, MVT::v2i64, 1 }, 209 { ISD::MUL, MVT::v4i64, 1 }, 210 { ISD::MUL, MVT::v8i64, 1 } 211 }; 212 213 // Look for AVX512DQ lowering tricks for custom cases. 214 if (ST->hasDQI()) { 215 if (const auto *Entry = CostTableLookup(AVX512DQCostTable, ISD, 216 LT.second)) 217 return LT.first * Entry->Cost; 218 } 219 220 static const CostTblEntry AVX512BWCostTable[] = { 221 { ISD::MUL, MVT::v64i8, 11 }, // extend/pmullw/trunc sequence. 222 { ISD::MUL, MVT::v32i8, 4 }, // extend/pmullw/trunc sequence. 223 { ISD::MUL, MVT::v16i8, 4 }, // extend/pmullw/trunc sequence. 224 225 // Vectorizing division is a bad idea. See the SSE2 table for more comments. 226 { ISD::SDIV, MVT::v64i8, 64*20 }, 227 { ISD::SDIV, MVT::v32i16, 32*20 }, 228 { ISD::SDIV, MVT::v16i32, 16*20 }, 229 { ISD::SDIV, MVT::v8i64, 8*20 }, 230 { ISD::UDIV, MVT::v64i8, 64*20 }, 231 { ISD::UDIV, MVT::v32i16, 32*20 }, 232 { ISD::UDIV, MVT::v16i32, 16*20 }, 233 { ISD::UDIV, MVT::v8i64, 8*20 }, 234 }; 235 236 // Look for AVX512BW lowering tricks for custom cases. 237 if (ST->hasBWI()) { 238 if (const auto *Entry = CostTableLookup(AVX512BWCostTable, ISD, 239 LT.second)) 240 return LT.first * Entry->Cost; 241 } 242 243 static const CostTblEntry AVX512CostTable[] = { 244 { ISD::SHL, MVT::v16i32, 1 }, 245 { ISD::SRL, MVT::v16i32, 1 }, 246 { ISD::SRA, MVT::v16i32, 1 }, 247 { ISD::SHL, MVT::v8i64, 1 }, 248 { ISD::SRL, MVT::v8i64, 1 }, 249 { ISD::SRA, MVT::v8i64, 1 }, 250 251 { ISD::MUL, MVT::v32i8, 13 }, // extend/pmullw/trunc sequence. 252 { ISD::MUL, MVT::v16i8, 5 }, // extend/pmullw/trunc sequence. 253 }; 254 255 if (ST->hasAVX512()) { 256 if (const auto *Entry = CostTableLookup(AVX512CostTable, ISD, LT.second)) 257 return LT.first * Entry->Cost; 258 } 259 260 static const CostTblEntry AVX2CostTable[] = { 261 // Shifts on v4i64/v8i32 on AVX2 is legal even though we declare to 262 // customize them to detect the cases where shift amount is a scalar one. 263 { ISD::SHL, MVT::v4i32, 1 }, 264 { ISD::SRL, MVT::v4i32, 1 }, 265 { ISD::SRA, MVT::v4i32, 1 }, 266 { ISD::SHL, MVT::v8i32, 1 }, 267 { ISD::SRL, MVT::v8i32, 1 }, 268 { ISD::SRA, MVT::v8i32, 1 }, 269 { ISD::SHL, MVT::v2i64, 1 }, 270 { ISD::SRL, MVT::v2i64, 1 }, 271 { ISD::SHL, MVT::v4i64, 1 }, 272 { ISD::SRL, MVT::v4i64, 1 }, 273 }; 274 275 // Look for AVX2 lowering tricks. 276 if (ST->hasAVX2()) { 277 if (ISD == ISD::SHL && LT.second == MVT::v16i16 && 278 (Op2Info == TargetTransformInfo::OK_UniformConstantValue || 279 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue)) 280 // On AVX2, a packed v16i16 shift left by a constant build_vector 281 // is lowered into a vector multiply (vpmullw). 282 return LT.first; 283 284 if (const auto *Entry = CostTableLookup(AVX2CostTable, ISD, LT.second)) 285 return LT.first * Entry->Cost; 286 } 287 288 static const CostTblEntry XOPCostTable[] = { 289 // 128bit shifts take 1cy, but right shifts require negation beforehand. 290 { ISD::SHL, MVT::v16i8, 1 }, 291 { ISD::SRL, MVT::v16i8, 2 }, 292 { ISD::SRA, MVT::v16i8, 2 }, 293 { ISD::SHL, MVT::v8i16, 1 }, 294 { ISD::SRL, MVT::v8i16, 2 }, 295 { ISD::SRA, MVT::v8i16, 2 }, 296 { ISD::SHL, MVT::v4i32, 1 }, 297 { ISD::SRL, MVT::v4i32, 2 }, 298 { ISD::SRA, MVT::v4i32, 2 }, 299 { ISD::SHL, MVT::v2i64, 1 }, 300 { ISD::SRL, MVT::v2i64, 2 }, 301 { ISD::SRA, MVT::v2i64, 2 }, 302 // 256bit shifts require splitting if AVX2 didn't catch them above. 303 { ISD::SHL, MVT::v32i8, 2 }, 304 { ISD::SRL, MVT::v32i8, 4 }, 305 { ISD::SRA, MVT::v32i8, 4 }, 306 { ISD::SHL, MVT::v16i16, 2 }, 307 { ISD::SRL, MVT::v16i16, 4 }, 308 { ISD::SRA, MVT::v16i16, 4 }, 309 { ISD::SHL, MVT::v8i32, 2 }, 310 { ISD::SRL, MVT::v8i32, 4 }, 311 { ISD::SRA, MVT::v8i32, 4 }, 312 { ISD::SHL, MVT::v4i64, 2 }, 313 { ISD::SRL, MVT::v4i64, 4 }, 314 { ISD::SRA, MVT::v4i64, 4 }, 315 }; 316 317 // Look for XOP lowering tricks. 318 if (ST->hasXOP()) { 319 if (const auto *Entry = CostTableLookup(XOPCostTable, ISD, LT.second)) 320 return LT.first * Entry->Cost; 321 } 322 323 static const CostTblEntry AVX2CustomCostTable[] = { 324 { ISD::SHL, MVT::v32i8, 11 }, // vpblendvb sequence. 325 { ISD::SHL, MVT::v16i16, 10 }, // extend/vpsrlvd/pack sequence. 326 327 { ISD::SRL, MVT::v32i8, 11 }, // vpblendvb sequence. 328 { ISD::SRL, MVT::v16i16, 10 }, // extend/vpsrlvd/pack sequence. 329 330 { ISD::SRA, MVT::v32i8, 24 }, // vpblendvb sequence. 331 { ISD::SRA, MVT::v16i16, 10 }, // extend/vpsravd/pack sequence. 332 { ISD::SRA, MVT::v2i64, 4 }, // srl/xor/sub sequence. 333 { ISD::SRA, MVT::v4i64, 4 }, // srl/xor/sub sequence. 334 335 { ISD::MUL, MVT::v32i8, 17 }, // extend/pmullw/trunc sequence. 336 { ISD::MUL, MVT::v16i8, 7 }, // extend/pmullw/trunc sequence. 337 338 { ISD::FDIV, MVT::f32, 7 }, // Haswell from http://www.agner.org/ 339 { ISD::FDIV, MVT::v4f32, 7 }, // Haswell from http://www.agner.org/ 340 { ISD::FDIV, MVT::v8f32, 14 }, // Haswell from http://www.agner.org/ 341 { ISD::FDIV, MVT::f64, 14 }, // Haswell from http://www.agner.org/ 342 { ISD::FDIV, MVT::v2f64, 14 }, // Haswell from http://www.agner.org/ 343 { ISD::FDIV, MVT::v4f64, 28 }, // Haswell from http://www.agner.org/ 344 }; 345 346 // Look for AVX2 lowering tricks for custom cases. 347 if (ST->hasAVX2()) { 348 if (const auto *Entry = CostTableLookup(AVX2CustomCostTable, ISD, 349 LT.second)) 350 return LT.first * Entry->Cost; 351 } 352 353 static const CostTblEntry AVXCustomCostTable[] = { 354 { ISD::MUL, MVT::v32i8, 26 }, // extend/pmullw/trunc sequence. 355 356 { ISD::FDIV, MVT::f32, 14 }, // SNB from http://www.agner.org/ 357 { ISD::FDIV, MVT::v4f32, 14 }, // SNB from http://www.agner.org/ 358 { ISD::FDIV, MVT::v8f32, 28 }, // SNB from http://www.agner.org/ 359 { ISD::FDIV, MVT::f64, 22 }, // SNB from http://www.agner.org/ 360 { ISD::FDIV, MVT::v2f64, 22 }, // SNB from http://www.agner.org/ 361 { ISD::FDIV, MVT::v4f64, 44 }, // SNB from http://www.agner.org/ 362 363 // Vectorizing division is a bad idea. See the SSE2 table for more comments. 364 { ISD::SDIV, MVT::v32i8, 32*20 }, 365 { ISD::SDIV, MVT::v16i16, 16*20 }, 366 { ISD::SDIV, MVT::v8i32, 8*20 }, 367 { ISD::SDIV, MVT::v4i64, 4*20 }, 368 { ISD::UDIV, MVT::v32i8, 32*20 }, 369 { ISD::UDIV, MVT::v16i16, 16*20 }, 370 { ISD::UDIV, MVT::v8i32, 8*20 }, 371 { ISD::UDIV, MVT::v4i64, 4*20 }, 372 }; 373 374 // Look for AVX2 lowering tricks for custom cases. 375 if (ST->hasAVX()) { 376 if (const auto *Entry = CostTableLookup(AVXCustomCostTable, ISD, 377 LT.second)) 378 return LT.first * Entry->Cost; 379 } 380 381 static const CostTblEntry SSE42FloatCostTable[] = { 382 { ISD::FDIV, MVT::f32, 14 }, // Nehalem from http://www.agner.org/ 383 { ISD::FDIV, MVT::v4f32, 14 }, // Nehalem from http://www.agner.org/ 384 { ISD::FDIV, MVT::f64, 22 }, // Nehalem from http://www.agner.org/ 385 { ISD::FDIV, MVT::v2f64, 22 }, // Nehalem from http://www.agner.org/ 386 }; 387 388 if (ST->hasSSE42()) { 389 if (const auto *Entry = CostTableLookup(SSE42FloatCostTable, ISD, 390 LT.second)) 391 return LT.first * Entry->Cost; 392 } 393 394 static const CostTblEntry 395 SSE2UniformCostTable[] = { 396 // Uniform splats are cheaper for the following instructions. 397 { ISD::SHL, MVT::v16i8, 1 }, // psllw. 398 { ISD::SHL, MVT::v32i8, 2 }, // psllw. 399 { ISD::SHL, MVT::v8i16, 1 }, // psllw. 400 { ISD::SHL, MVT::v16i16, 2 }, // psllw. 401 { ISD::SHL, MVT::v4i32, 1 }, // pslld 402 { ISD::SHL, MVT::v8i32, 2 }, // pslld 403 { ISD::SHL, MVT::v2i64, 1 }, // psllq. 404 { ISD::SHL, MVT::v4i64, 2 }, // psllq. 405 406 { ISD::SRL, MVT::v16i8, 1 }, // psrlw. 407 { ISD::SRL, MVT::v32i8, 2 }, // psrlw. 408 { ISD::SRL, MVT::v8i16, 1 }, // psrlw. 409 { ISD::SRL, MVT::v16i16, 2 }, // psrlw. 410 { ISD::SRL, MVT::v4i32, 1 }, // psrld. 411 { ISD::SRL, MVT::v8i32, 2 }, // psrld. 412 { ISD::SRL, MVT::v2i64, 1 }, // psrlq. 413 { ISD::SRL, MVT::v4i64, 2 }, // psrlq. 414 415 { ISD::SRA, MVT::v16i8, 4 }, // psrlw, pand, pxor, psubb. 416 { ISD::SRA, MVT::v32i8, 8 }, // psrlw, pand, pxor, psubb. 417 { ISD::SRA, MVT::v8i16, 1 }, // psraw. 418 { ISD::SRA, MVT::v16i16, 2 }, // psraw. 419 { ISD::SRA, MVT::v4i32, 1 }, // psrad. 420 { ISD::SRA, MVT::v8i32, 2 }, // psrad. 421 { ISD::SRA, MVT::v2i64, 4 }, // 2 x psrad + shuffle. 422 { ISD::SRA, MVT::v4i64, 8 }, // 2 x psrad + shuffle. 423 }; 424 425 if (ST->hasSSE2() && 426 ((Op2Info == TargetTransformInfo::OK_UniformConstantValue) || 427 (Op2Info == TargetTransformInfo::OK_UniformValue))) { 428 if (const auto *Entry = 429 CostTableLookup(SSE2UniformCostTable, ISD, LT.second)) 430 return LT.first * Entry->Cost; 431 } 432 433 if (ISD == ISD::SHL && 434 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) { 435 MVT VT = LT.second; 436 // Vector shift left by non uniform constant can be lowered 437 // into vector multiply (pmullw/pmulld). 438 if ((VT == MVT::v8i16 && ST->hasSSE2()) || 439 (VT == MVT::v4i32 && ST->hasSSE41())) 440 return LT.first; 441 442 // v16i16 and v8i32 shifts by non-uniform constants are lowered into a 443 // sequence of extract + two vector multiply + insert. 444 if ((VT == MVT::v8i32 || VT == MVT::v16i16) && 445 (ST->hasAVX() && !ST->hasAVX2())) 446 ISD = ISD::MUL; 447 448 // A vector shift left by non uniform constant is converted 449 // into a vector multiply; the new multiply is eventually 450 // lowered into a sequence of shuffles and 2 x pmuludq. 451 if (VT == MVT::v4i32 && ST->hasSSE2()) 452 ISD = ISD::MUL; 453 } 454 455 static const CostTblEntry SSE41CostTable[] = { 456 { ISD::SHL, MVT::v16i8, 11 }, // pblendvb sequence. 457 { ISD::SHL, MVT::v32i8, 2*11 }, // pblendvb sequence. 458 { ISD::SHL, MVT::v8i16, 14 }, // pblendvb sequence. 459 { ISD::SHL, MVT::v16i16, 2*14 }, // pblendvb sequence. 460 461 { ISD::SRL, MVT::v16i8, 12 }, // pblendvb sequence. 462 { ISD::SRL, MVT::v32i8, 2*12 }, // pblendvb sequence. 463 { ISD::SRL, MVT::v8i16, 14 }, // pblendvb sequence. 464 { ISD::SRL, MVT::v16i16, 2*14 }, // pblendvb sequence. 465 { ISD::SRL, MVT::v4i32, 11 }, // Shift each lane + blend. 466 { ISD::SRL, MVT::v8i32, 2*11 }, // Shift each lane + blend. 467 468 { ISD::SRA, MVT::v16i8, 24 }, // pblendvb sequence. 469 { ISD::SRA, MVT::v32i8, 2*24 }, // pblendvb sequence. 470 { ISD::SRA, MVT::v8i16, 14 }, // pblendvb sequence. 471 { ISD::SRA, MVT::v16i16, 2*14 }, // pblendvb sequence. 472 { ISD::SRA, MVT::v4i32, 12 }, // Shift each lane + blend. 473 { ISD::SRA, MVT::v8i32, 2*12 }, // Shift each lane + blend. 474 }; 475 476 if (ST->hasSSE41()) { 477 if (const auto *Entry = CostTableLookup(SSE41CostTable, ISD, LT.second)) 478 return LT.first * Entry->Cost; 479 } 480 481 static const CostTblEntry SSE2CostTable[] = { 482 // We don't correctly identify costs of casts because they are marked as 483 // custom. 484 { ISD::SHL, MVT::v16i8, 26 }, // cmpgtb sequence. 485 { ISD::SHL, MVT::v32i8, 2*26 }, // cmpgtb sequence. 486 { ISD::SHL, MVT::v8i16, 32 }, // cmpgtb sequence. 487 { ISD::SHL, MVT::v16i16, 2*32 }, // cmpgtb sequence. 488 { ISD::SHL, MVT::v4i32, 2*5 }, // We optimized this using mul. 489 { ISD::SHL, MVT::v8i32, 2*2*5 }, // We optimized this using mul. 490 { ISD::SHL, MVT::v2i64, 4 }, // splat+shuffle sequence. 491 { ISD::SHL, MVT::v4i64, 2*4 }, // splat+shuffle sequence. 492 493 { ISD::SRL, MVT::v16i8, 26 }, // cmpgtb sequence. 494 { ISD::SRL, MVT::v32i8, 2*26 }, // cmpgtb sequence. 495 { ISD::SRL, MVT::v8i16, 32 }, // cmpgtb sequence. 496 { ISD::SRL, MVT::v16i16, 2*32 }, // cmpgtb sequence. 497 { ISD::SRL, MVT::v4i32, 16 }, // Shift each lane + blend. 498 { ISD::SRL, MVT::v8i32, 2*16 }, // Shift each lane + blend. 499 { ISD::SRL, MVT::v2i64, 4 }, // splat+shuffle sequence. 500 { ISD::SRL, MVT::v4i64, 2*4 }, // splat+shuffle sequence. 501 502 { ISD::SRA, MVT::v16i8, 54 }, // unpacked cmpgtb sequence. 503 { ISD::SRA, MVT::v32i8, 2*54 }, // unpacked cmpgtb sequence. 504 { ISD::SRA, MVT::v8i16, 32 }, // cmpgtb sequence. 505 { ISD::SRA, MVT::v16i16, 2*32 }, // cmpgtb sequence. 506 { ISD::SRA, MVT::v4i32, 16 }, // Shift each lane + blend. 507 { ISD::SRA, MVT::v8i32, 2*16 }, // Shift each lane + blend. 508 { ISD::SRA, MVT::v2i64, 12 }, // srl/xor/sub sequence. 509 { ISD::SRA, MVT::v4i64, 2*12 }, // srl/xor/sub sequence. 510 511 { ISD::MUL, MVT::v16i8, 12 }, // extend/pmullw/trunc sequence. 512 513 { ISD::FDIV, MVT::f32, 23 }, // Pentium IV from http://www.agner.org/ 514 { ISD::FDIV, MVT::v4f32, 39 }, // Pentium IV from http://www.agner.org/ 515 { ISD::FDIV, MVT::f64, 38 }, // Pentium IV from http://www.agner.org/ 516 { ISD::FDIV, MVT::v2f64, 69 }, // Pentium IV from http://www.agner.org/ 517 518 // It is not a good idea to vectorize division. We have to scalarize it and 519 // in the process we will often end up having to spilling regular 520 // registers. The overhead of division is going to dominate most kernels 521 // anyways so try hard to prevent vectorization of division - it is 522 // generally a bad idea. Assume somewhat arbitrarily that we have to be able 523 // to hide "20 cycles" for each lane. 524 { ISD::SDIV, MVT::v16i8, 16*20 }, 525 { ISD::SDIV, MVT::v8i16, 8*20 }, 526 { ISD::SDIV, MVT::v4i32, 4*20 }, 527 { ISD::SDIV, MVT::v2i64, 2*20 }, 528 { ISD::UDIV, MVT::v16i8, 16*20 }, 529 { ISD::UDIV, MVT::v8i16, 8*20 }, 530 { ISD::UDIV, MVT::v4i32, 4*20 }, 531 { ISD::UDIV, MVT::v2i64, 2*20 }, 532 }; 533 534 if (ST->hasSSE2()) { 535 if (const auto *Entry = CostTableLookup(SSE2CostTable, ISD, LT.second)) 536 return LT.first * Entry->Cost; 537 } 538 539 static const CostTblEntry AVX1CostTable[] = { 540 // We don't have to scalarize unsupported ops. We can issue two half-sized 541 // operations and we only need to extract the upper YMM half. 542 // Two ops + 1 extract + 1 insert = 4. 543 { ISD::MUL, MVT::v16i16, 4 }, 544 { ISD::MUL, MVT::v8i32, 4 }, 545 { ISD::SUB, MVT::v32i8, 4 }, 546 { ISD::ADD, MVT::v32i8, 4 }, 547 { ISD::SUB, MVT::v16i16, 4 }, 548 { ISD::ADD, MVT::v16i16, 4 }, 549 { ISD::SUB, MVT::v8i32, 4 }, 550 { ISD::ADD, MVT::v8i32, 4 }, 551 { ISD::SUB, MVT::v4i64, 4 }, 552 { ISD::ADD, MVT::v4i64, 4 }, 553 // A v4i64 multiply is custom lowered as two split v2i64 vectors that then 554 // are lowered as a series of long multiplies(3), shifts(4) and adds(2) 555 // Because we believe v4i64 to be a legal type, we must also include the 556 // split factor of two in the cost table. Therefore, the cost here is 18 557 // instead of 9. 558 { ISD::MUL, MVT::v4i64, 18 }, 559 }; 560 561 // Look for AVX1 lowering tricks. 562 if (ST->hasAVX() && !ST->hasAVX2()) { 563 MVT VT = LT.second; 564 565 if (const auto *Entry = CostTableLookup(AVX1CostTable, ISD, VT)) 566 return LT.first * Entry->Cost; 567 } 568 569 // Custom lowering of vectors. 570 static const CostTblEntry CustomLowered[] = { 571 // A v2i64/v4i64 and multiply is custom lowered as a series of long 572 // multiplies(3), shifts(4) and adds(2). 573 { ISD::MUL, MVT::v2i64, 9 }, 574 { ISD::MUL, MVT::v4i64, 9 }, 575 { ISD::MUL, MVT::v8i64, 9 } 576 }; 577 if (const auto *Entry = CostTableLookup(CustomLowered, ISD, LT.second)) 578 return LT.first * Entry->Cost; 579 580 // Special lowering of v4i32 mul on sse2, sse3: Lower v4i32 mul as 2x shuffle, 581 // 2x pmuludq, 2x shuffle. 582 if (ISD == ISD::MUL && LT.second == MVT::v4i32 && ST->hasSSE2() && 583 !ST->hasSSE41()) 584 return LT.first * 6; 585 586 static const CostTblEntry SSE1FloatCostTable[] = { 587 { ISD::FDIV, MVT::f32, 17 }, // Pentium III from http://www.agner.org/ 588 { ISD::FDIV, MVT::v4f32, 34 }, // Pentium III from http://www.agner.org/ 589 }; 590 591 if (ST->hasSSE1()) 592 if (const auto *Entry = CostTableLookup(SSE1FloatCostTable, ISD, 593 LT.second)) 594 return LT.first * Entry->Cost; 595 // Fallback to the default implementation. 596 return BaseT::getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info); 597 } 598 599 int X86TTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index, 600 Type *SubTp) { 601 // We only estimate the cost of reverse and alternate shuffles. 602 if (Kind != TTI::SK_Reverse && Kind != TTI::SK_Alternate) 603 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp); 604 605 if (Kind == TTI::SK_Reverse) { 606 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp); 607 int Cost = 1; 608 if (LT.second.getSizeInBits() > 128) 609 Cost = 3; // Extract + insert + copy. 610 611 // Multiple by the number of parts. 612 return Cost * LT.first; 613 } 614 615 if (Kind == TTI::SK_Alternate) { 616 // 64-bit packed float vectors (v2f32) are widened to type v4f32. 617 // 64-bit packed integer vectors (v2i32) are promoted to type v2i64. 618 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp); 619 620 // The backend knows how to generate a single VEX.256 version of 621 // instruction VPBLENDW if the target supports AVX2. 622 if (ST->hasAVX2() && LT.second == MVT::v16i16) 623 return LT.first; 624 625 static const CostTblEntry AVXAltShuffleTbl[] = { 626 {ISD::VECTOR_SHUFFLE, MVT::v4i64, 1}, // vblendpd 627 {ISD::VECTOR_SHUFFLE, MVT::v4f64, 1}, // vblendpd 628 629 {ISD::VECTOR_SHUFFLE, MVT::v8i32, 1}, // vblendps 630 {ISD::VECTOR_SHUFFLE, MVT::v8f32, 1}, // vblendps 631 632 // This shuffle is custom lowered into a sequence of: 633 // 2x vextractf128 , 2x vpblendw , 1x vinsertf128 634 {ISD::VECTOR_SHUFFLE, MVT::v16i16, 5}, 635 636 // This shuffle is custom lowered into a long sequence of: 637 // 2x vextractf128 , 4x vpshufb , 2x vpor , 1x vinsertf128 638 {ISD::VECTOR_SHUFFLE, MVT::v32i8, 9} 639 }; 640 641 if (ST->hasAVX()) 642 if (const auto *Entry = CostTableLookup(AVXAltShuffleTbl, 643 ISD::VECTOR_SHUFFLE, LT.second)) 644 return LT.first * Entry->Cost; 645 646 static const CostTblEntry SSE41AltShuffleTbl[] = { 647 // These are lowered into movsd. 648 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1}, 649 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1}, 650 651 // packed float vectors with four elements are lowered into BLENDI dag 652 // nodes. A v4i32/v4f32 BLENDI generates a single 'blendps'/'blendpd'. 653 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 1}, 654 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 1}, 655 656 // This shuffle generates a single pshufw. 657 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 1}, 658 659 // There is no instruction that matches a v16i8 alternate shuffle. 660 // The backend will expand it into the sequence 'pshufb + pshufb + or'. 661 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 3} 662 }; 663 664 if (ST->hasSSE41()) 665 if (const auto *Entry = CostTableLookup(SSE41AltShuffleTbl, ISD::VECTOR_SHUFFLE, 666 LT.second)) 667 return LT.first * Entry->Cost; 668 669 static const CostTblEntry SSSE3AltShuffleTbl[] = { 670 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1}, // movsd 671 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1}, // movsd 672 673 // SSE3 doesn't have 'blendps'. The following shuffles are expanded into 674 // the sequence 'shufps + pshufd' 675 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2}, 676 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2}, 677 678 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 3}, // pshufb + pshufb + or 679 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 3} // pshufb + pshufb + or 680 }; 681 682 if (ST->hasSSSE3()) 683 if (const auto *Entry = CostTableLookup(SSSE3AltShuffleTbl, 684 ISD::VECTOR_SHUFFLE, LT.second)) 685 return LT.first * Entry->Cost; 686 687 static const CostTblEntry SSEAltShuffleTbl[] = { 688 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1}, // movsd 689 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1}, // movsd 690 691 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2}, // shufps + pshufd 692 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2}, // shufps + pshufd 693 694 // This is expanded into a long sequence of four extract + four insert. 695 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 8}, // 4 x pextrw + 4 pinsrw. 696 697 // 8 x (pinsrw + pextrw + and + movb + movzb + or) 698 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 48} 699 }; 700 701 // Fall-back (SSE3 and SSE2). 702 if (const auto *Entry = CostTableLookup(SSEAltShuffleTbl, 703 ISD::VECTOR_SHUFFLE, LT.second)) 704 return LT.first * Entry->Cost; 705 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp); 706 } 707 708 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp); 709 } 710 711 int X86TTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) { 712 int ISD = TLI->InstructionOpcodeToISD(Opcode); 713 assert(ISD && "Invalid opcode"); 714 715 // FIXME: Need a better design of the cost table to handle non-simple types of 716 // potential massive combinations (elem_num x src_type x dst_type). 717 718 static const TypeConversionCostTblEntry AVX512DQConversionTbl[] = { 719 { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i64, 1 }, 720 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i64, 1 }, 721 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i64, 1 }, 722 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i64, 1 }, 723 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i64, 1 }, 724 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i64, 1 }, 725 726 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i64, 1 }, 727 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 1 }, 728 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i64, 1 }, 729 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i64, 1 }, 730 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i64, 1 }, 731 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i64, 1 }, 732 733 { ISD::FP_TO_SINT, MVT::v2i64, MVT::v2f32, 1 }, 734 { ISD::FP_TO_SINT, MVT::v4i64, MVT::v4f32, 1 }, 735 { ISD::FP_TO_SINT, MVT::v8i64, MVT::v8f32, 1 }, 736 { ISD::FP_TO_SINT, MVT::v2i64, MVT::v2f64, 1 }, 737 { ISD::FP_TO_SINT, MVT::v4i64, MVT::v4f64, 1 }, 738 { ISD::FP_TO_SINT, MVT::v8i64, MVT::v8f64, 1 }, 739 740 { ISD::FP_TO_UINT, MVT::v2i64, MVT::v2f32, 1 }, 741 { ISD::FP_TO_UINT, MVT::v4i64, MVT::v4f32, 1 }, 742 { ISD::FP_TO_UINT, MVT::v8i64, MVT::v8f32, 1 }, 743 { ISD::FP_TO_UINT, MVT::v2i64, MVT::v2f64, 1 }, 744 { ISD::FP_TO_UINT, MVT::v4i64, MVT::v4f64, 1 }, 745 { ISD::FP_TO_UINT, MVT::v8i64, MVT::v8f64, 1 }, 746 }; 747 748 // TODO: For AVX512DQ + AVX512VL, we also have cheap casts for 128-bit and 749 // 256-bit wide vectors. 750 751 static const TypeConversionCostTblEntry AVX512FConversionTbl[] = { 752 { ISD::FP_EXTEND, MVT::v8f64, MVT::v8f32, 1 }, 753 { ISD::FP_EXTEND, MVT::v8f64, MVT::v16f32, 3 }, 754 { ISD::FP_ROUND, MVT::v8f32, MVT::v8f64, 1 }, 755 756 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i32, 1 }, 757 { ISD::TRUNCATE, MVT::v16i16, MVT::v16i32, 1 }, 758 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i64, 1 }, 759 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 1 }, 760 761 // v16i1 -> v16i32 - load + broadcast 762 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i1, 2 }, 763 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i1, 2 }, 764 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 1 }, 765 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 1 }, 766 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i16, 1 }, 767 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i16, 1 }, 768 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i16, 1 }, 769 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i16, 1 }, 770 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i32, 1 }, 771 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i32, 1 }, 772 773 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i1, 4 }, 774 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i1, 3 }, 775 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i8, 2 }, 776 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i8, 2 }, 777 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i16, 2 }, 778 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i16, 2 }, 779 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i32, 1 }, 780 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i32, 1 }, 781 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i64, 26 }, 782 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i64, 26 }, 783 784 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i1, 4 }, 785 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i1, 3 }, 786 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i8, 2 }, 787 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i8, 2 }, 788 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 2 }, 789 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i8, 2 }, 790 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i8, 2 }, 791 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i16, 5 }, 792 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i16, 2 }, 793 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 2 }, 794 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i16, 2 }, 795 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i16, 2 }, 796 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i32, 2 }, 797 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i32, 1 }, 798 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 }, 799 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i32, 1 }, 800 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 1 }, 801 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i32, 1 }, 802 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i32, 1 }, 803 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i64, 5 }, 804 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 5 }, 805 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i64, 12 }, 806 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i64, 26 }, 807 808 { ISD::FP_TO_UINT, MVT::v2i32, MVT::v2f32, 1 }, 809 { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f32, 1 }, 810 { ISD::FP_TO_UINT, MVT::v8i32, MVT::v8f32, 1 }, 811 { ISD::FP_TO_UINT, MVT::v16i32, MVT::v16f32, 1 }, 812 }; 813 814 static const TypeConversionCostTblEntry AVX2ConversionTbl[] = { 815 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 3 }, 816 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 3 }, 817 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 3 }, 818 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 3 }, 819 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 3 }, 820 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 3 }, 821 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 3 }, 822 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 3 }, 823 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 1 }, 824 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 1 }, 825 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 3 }, 826 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 }, 827 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 1 }, 828 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 1 }, 829 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 1 }, 830 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 1 }, 831 832 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 2 }, 833 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 2 }, 834 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 2 }, 835 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 2 }, 836 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 2 }, 837 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 4 }, 838 839 { ISD::FP_EXTEND, MVT::v8f64, MVT::v8f32, 3 }, 840 { ISD::FP_ROUND, MVT::v8f32, MVT::v8f64, 3 }, 841 842 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 8 }, 843 }; 844 845 static const TypeConversionCostTblEntry AVXConversionTbl[] = { 846 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 6 }, 847 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 4 }, 848 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 7 }, 849 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 4 }, 850 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 6 }, 851 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 4 }, 852 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 7 }, 853 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 4 }, 854 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 4 }, 855 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 4 }, 856 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 6 }, 857 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 }, 858 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 4 }, 859 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 4 }, 860 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 4 }, 861 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 4 }, 862 863 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i16, 4 }, 864 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 4 }, 865 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 5 }, 866 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 4 }, 867 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 4 }, 868 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 4 }, 869 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 9 }, 870 871 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i1, 3 }, 872 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i1, 3 }, 873 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i1, 8 }, 874 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 }, 875 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i8, 3 }, 876 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i8, 8 }, 877 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i16, 3 }, 878 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i16, 3 }, 879 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 }, 880 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 }, 881 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i32, 1 }, 882 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i32, 1 }, 883 884 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i1, 7 }, 885 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i1, 7 }, 886 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i1, 6 }, 887 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 2 }, 888 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i8, 2 }, 889 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 5 }, 890 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 }, 891 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i16, 2 }, 892 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 }, 893 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i32, 6 }, 894 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 6 }, 895 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i32, 6 }, 896 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 9 }, 897 // The generic code to compute the scalar overhead is currently broken. 898 // Workaround this limitation by estimating the scalarization overhead 899 // here. We have roughly 10 instructions per scalar element. 900 // Multiply that by the vector width. 901 // FIXME: remove that when PR19268 is fixed. 902 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 10 }, 903 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i64, 20 }, 904 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i64, 13 }, 905 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i64, 13 }, 906 907 { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 1 }, 908 { ISD::FP_TO_SINT, MVT::v8i8, MVT::v8f32, 7 }, 909 // This node is expanded into scalarized operations but BasicTTI is overly 910 // optimistic estimating its cost. It computes 3 per element (one 911 // vector-extract, one scalar conversion and one vector-insert). The 912 // problem is that the inserts form a read-modify-write chain so latency 913 // should be factored in too. Inflating the cost per element by 1. 914 { ISD::FP_TO_UINT, MVT::v8i32, MVT::v8f32, 8*4 }, 915 { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f64, 4*4 }, 916 917 { ISD::FP_EXTEND, MVT::v4f64, MVT::v4f32, 1 }, 918 { ISD::FP_ROUND, MVT::v4f32, MVT::v4f64, 1 }, 919 }; 920 921 static const TypeConversionCostTblEntry SSE41ConversionTbl[] = { 922 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 2 }, 923 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 2 }, 924 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 2 }, 925 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 2 }, 926 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 2 }, 927 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 2 }, 928 929 { ISD::ZERO_EXTEND, MVT::v4i16, MVT::v4i8, 1 }, 930 { ISD::SIGN_EXTEND, MVT::v4i16, MVT::v4i8, 2 }, 931 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i8, 1 }, 932 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i8, 1 }, 933 { ISD::ZERO_EXTEND, MVT::v8i16, MVT::v8i8, 1 }, 934 { ISD::SIGN_EXTEND, MVT::v8i16, MVT::v8i8, 1 }, 935 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 2 }, 936 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 2 }, 937 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 2 }, 938 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 2 }, 939 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 4 }, 940 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 4 }, 941 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i16, 1 }, 942 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i16, 1 }, 943 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 2 }, 944 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 2 }, 945 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i16, 4 }, 946 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i16, 4 }, 947 948 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i16, 2 }, 949 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i16, 1 }, 950 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i32, 1 }, 951 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i32, 1 }, 952 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 3 }, 953 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 3 }, 954 { ISD::TRUNCATE, MVT::v16i16, MVT::v16i32, 6 }, 955 956 }; 957 958 static const TypeConversionCostTblEntry SSE2ConversionTbl[] = { 959 // These are somewhat magic numbers justified by looking at the output of 960 // Intel's IACA, running some kernels and making sure when we take 961 // legalization into account the throughput will be overestimated. 962 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 }, 963 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 }, 964 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 }, 965 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 }, 966 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 5 }, 967 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 }, 968 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 }, 969 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 }, 970 971 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 }, 972 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 }, 973 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 }, 974 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 }, 975 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 }, 976 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 8 }, 977 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 }, 978 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 }, 979 980 { ISD::FP_TO_SINT, MVT::v2i32, MVT::v2f64, 3 }, 981 982 { ISD::ZERO_EXTEND, MVT::v4i16, MVT::v4i8, 1 }, 983 { ISD::SIGN_EXTEND, MVT::v4i16, MVT::v4i8, 6 }, 984 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i8, 2 }, 985 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i8, 3 }, 986 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 4 }, 987 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 8 }, 988 { ISD::ZERO_EXTEND, MVT::v8i16, MVT::v8i8, 1 }, 989 { ISD::SIGN_EXTEND, MVT::v8i16, MVT::v8i8, 2 }, 990 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 6 }, 991 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 6 }, 992 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 3 }, 993 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 4 }, 994 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 9 }, 995 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 12 }, 996 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i16, 1 }, 997 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i16, 2 }, 998 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 }, 999 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 10 }, 1000 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 3 }, 1001 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 4 }, 1002 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i16, 6 }, 1003 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i16, 8 }, 1004 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 3 }, 1005 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 5 }, 1006 1007 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i16, 4 }, 1008 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i16, 2 }, 1009 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i16, 3 }, 1010 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i32, 3 }, 1011 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i32, 3 }, 1012 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 4 }, 1013 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i32, 7 }, 1014 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 5 }, 1015 { ISD::TRUNCATE, MVT::v16i16, MVT::v16i32, 10 }, 1016 }; 1017 1018 std::pair<int, MVT> LTSrc = TLI->getTypeLegalizationCost(DL, Src); 1019 std::pair<int, MVT> LTDest = TLI->getTypeLegalizationCost(DL, Dst); 1020 1021 if (ST->hasSSE2() && !ST->hasAVX()) { 1022 if (const auto *Entry = ConvertCostTableLookup(SSE2ConversionTbl, ISD, 1023 LTDest.second, LTSrc.second)) 1024 return LTSrc.first * Entry->Cost; 1025 } 1026 1027 EVT SrcTy = TLI->getValueType(DL, Src); 1028 EVT DstTy = TLI->getValueType(DL, Dst); 1029 1030 // The function getSimpleVT only handles simple value types. 1031 if (!SrcTy.isSimple() || !DstTy.isSimple()) 1032 return BaseT::getCastInstrCost(Opcode, Dst, Src); 1033 1034 if (ST->hasDQI()) 1035 if (const auto *Entry = ConvertCostTableLookup(AVX512DQConversionTbl, ISD, 1036 DstTy.getSimpleVT(), 1037 SrcTy.getSimpleVT())) 1038 return Entry->Cost; 1039 1040 if (ST->hasAVX512()) 1041 if (const auto *Entry = ConvertCostTableLookup(AVX512FConversionTbl, ISD, 1042 DstTy.getSimpleVT(), 1043 SrcTy.getSimpleVT())) 1044 return Entry->Cost; 1045 1046 if (ST->hasAVX2()) { 1047 if (const auto *Entry = ConvertCostTableLookup(AVX2ConversionTbl, ISD, 1048 DstTy.getSimpleVT(), 1049 SrcTy.getSimpleVT())) 1050 return Entry->Cost; 1051 } 1052 1053 if (ST->hasAVX()) { 1054 if (const auto *Entry = ConvertCostTableLookup(AVXConversionTbl, ISD, 1055 DstTy.getSimpleVT(), 1056 SrcTy.getSimpleVT())) 1057 return Entry->Cost; 1058 } 1059 1060 if (ST->hasSSE41()) { 1061 if (const auto *Entry = ConvertCostTableLookup(SSE41ConversionTbl, ISD, 1062 DstTy.getSimpleVT(), 1063 SrcTy.getSimpleVT())) 1064 return Entry->Cost; 1065 } 1066 1067 if (ST->hasSSE2()) { 1068 if (const auto *Entry = ConvertCostTableLookup(SSE2ConversionTbl, ISD, 1069 DstTy.getSimpleVT(), 1070 SrcTy.getSimpleVT())) 1071 return Entry->Cost; 1072 } 1073 1074 return BaseT::getCastInstrCost(Opcode, Dst, Src); 1075 } 1076 1077 int X86TTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy) { 1078 // Legalize the type. 1079 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy); 1080 1081 MVT MTy = LT.second; 1082 1083 int ISD = TLI->InstructionOpcodeToISD(Opcode); 1084 assert(ISD && "Invalid opcode"); 1085 1086 static const CostTblEntry SSE2CostTbl[] = { 1087 { ISD::SETCC, MVT::v2i64, 8 }, 1088 { ISD::SETCC, MVT::v4i32, 1 }, 1089 { ISD::SETCC, MVT::v8i16, 1 }, 1090 { ISD::SETCC, MVT::v16i8, 1 }, 1091 }; 1092 1093 static const CostTblEntry SSE42CostTbl[] = { 1094 { ISD::SETCC, MVT::v2f64, 1 }, 1095 { ISD::SETCC, MVT::v4f32, 1 }, 1096 { ISD::SETCC, MVT::v2i64, 1 }, 1097 }; 1098 1099 static const CostTblEntry AVX1CostTbl[] = { 1100 { ISD::SETCC, MVT::v4f64, 1 }, 1101 { ISD::SETCC, MVT::v8f32, 1 }, 1102 // AVX1 does not support 8-wide integer compare. 1103 { ISD::SETCC, MVT::v4i64, 4 }, 1104 { ISD::SETCC, MVT::v8i32, 4 }, 1105 { ISD::SETCC, MVT::v16i16, 4 }, 1106 { ISD::SETCC, MVT::v32i8, 4 }, 1107 }; 1108 1109 static const CostTblEntry AVX2CostTbl[] = { 1110 { ISD::SETCC, MVT::v4i64, 1 }, 1111 { ISD::SETCC, MVT::v8i32, 1 }, 1112 { ISD::SETCC, MVT::v16i16, 1 }, 1113 { ISD::SETCC, MVT::v32i8, 1 }, 1114 }; 1115 1116 static const CostTblEntry AVX512CostTbl[] = { 1117 { ISD::SETCC, MVT::v8i64, 1 }, 1118 { ISD::SETCC, MVT::v16i32, 1 }, 1119 { ISD::SETCC, MVT::v8f64, 1 }, 1120 { ISD::SETCC, MVT::v16f32, 1 }, 1121 }; 1122 1123 if (ST->hasAVX512()) 1124 if (const auto *Entry = CostTableLookup(AVX512CostTbl, ISD, MTy)) 1125 return LT.first * Entry->Cost; 1126 1127 if (ST->hasAVX2()) 1128 if (const auto *Entry = CostTableLookup(AVX2CostTbl, ISD, MTy)) 1129 return LT.first * Entry->Cost; 1130 1131 if (ST->hasAVX()) 1132 if (const auto *Entry = CostTableLookup(AVX1CostTbl, ISD, MTy)) 1133 return LT.first * Entry->Cost; 1134 1135 if (ST->hasSSE42()) 1136 if (const auto *Entry = CostTableLookup(SSE42CostTbl, ISD, MTy)) 1137 return LT.first * Entry->Cost; 1138 1139 if (ST->hasSSE2()) 1140 if (const auto *Entry = CostTableLookup(SSE2CostTbl, ISD, MTy)) 1141 return LT.first * Entry->Cost; 1142 1143 return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy); 1144 } 1145 1146 int X86TTIImpl::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy, 1147 ArrayRef<Type *> Tys, FastMathFlags FMF) { 1148 // Costs should match the codegen from: 1149 // BITREVERSE: llvm\test\CodeGen\X86\vector-bitreverse.ll 1150 // BSWAP: llvm\test\CodeGen\X86\bswap-vector.ll 1151 // CTLZ: llvm\test\CodeGen\X86\vector-lzcnt-*.ll 1152 // CTPOP: llvm\test\CodeGen\X86\vector-popcnt-*.ll 1153 // CTTZ: llvm\test\CodeGen\X86\vector-tzcnt-*.ll 1154 static const CostTblEntry XOPCostTbl[] = { 1155 { ISD::BITREVERSE, MVT::v4i64, 4 }, 1156 { ISD::BITREVERSE, MVT::v8i32, 4 }, 1157 { ISD::BITREVERSE, MVT::v16i16, 4 }, 1158 { ISD::BITREVERSE, MVT::v32i8, 4 }, 1159 { ISD::BITREVERSE, MVT::v2i64, 1 }, 1160 { ISD::BITREVERSE, MVT::v4i32, 1 }, 1161 { ISD::BITREVERSE, MVT::v8i16, 1 }, 1162 { ISD::BITREVERSE, MVT::v16i8, 1 }, 1163 { ISD::BITREVERSE, MVT::i64, 3 }, 1164 { ISD::BITREVERSE, MVT::i32, 3 }, 1165 { ISD::BITREVERSE, MVT::i16, 3 }, 1166 { ISD::BITREVERSE, MVT::i8, 3 } 1167 }; 1168 static const CostTblEntry AVX2CostTbl[] = { 1169 { ISD::BITREVERSE, MVT::v4i64, 5 }, 1170 { ISD::BITREVERSE, MVT::v8i32, 5 }, 1171 { ISD::BITREVERSE, MVT::v16i16, 5 }, 1172 { ISD::BITREVERSE, MVT::v32i8, 5 }, 1173 { ISD::BSWAP, MVT::v4i64, 1 }, 1174 { ISD::BSWAP, MVT::v8i32, 1 }, 1175 { ISD::BSWAP, MVT::v16i16, 1 }, 1176 { ISD::CTLZ, MVT::v4i64, 23 }, 1177 { ISD::CTLZ, MVT::v8i32, 18 }, 1178 { ISD::CTLZ, MVT::v16i16, 14 }, 1179 { ISD::CTLZ, MVT::v32i8, 9 }, 1180 { ISD::CTPOP, MVT::v4i64, 7 }, 1181 { ISD::CTPOP, MVT::v8i32, 11 }, 1182 { ISD::CTPOP, MVT::v16i16, 9 }, 1183 { ISD::CTPOP, MVT::v32i8, 6 }, 1184 { ISD::CTTZ, MVT::v4i64, 10 }, 1185 { ISD::CTTZ, MVT::v8i32, 14 }, 1186 { ISD::CTTZ, MVT::v16i16, 12 }, 1187 { ISD::CTTZ, MVT::v32i8, 9 }, 1188 { ISD::FSQRT, MVT::f32, 7 }, // Haswell from http://www.agner.org/ 1189 { ISD::FSQRT, MVT::v4f32, 7 }, // Haswell from http://www.agner.org/ 1190 { ISD::FSQRT, MVT::v8f32, 14 }, // Haswell from http://www.agner.org/ 1191 { ISD::FSQRT, MVT::f64, 14 }, // Haswell from http://www.agner.org/ 1192 { ISD::FSQRT, MVT::v2f64, 14 }, // Haswell from http://www.agner.org/ 1193 { ISD::FSQRT, MVT::v4f64, 28 }, // Haswell from http://www.agner.org/ 1194 }; 1195 static const CostTblEntry AVX1CostTbl[] = { 1196 { ISD::BITREVERSE, MVT::v4i64, 10 }, 1197 { ISD::BITREVERSE, MVT::v8i32, 10 }, 1198 { ISD::BITREVERSE, MVT::v16i16, 10 }, 1199 { ISD::BITREVERSE, MVT::v32i8, 10 }, 1200 { ISD::BSWAP, MVT::v4i64, 4 }, 1201 { ISD::BSWAP, MVT::v8i32, 4 }, 1202 { ISD::BSWAP, MVT::v16i16, 4 }, 1203 { ISD::CTLZ, MVT::v4i64, 46 }, 1204 { ISD::CTLZ, MVT::v8i32, 36 }, 1205 { ISD::CTLZ, MVT::v16i16, 28 }, 1206 { ISD::CTLZ, MVT::v32i8, 18 }, 1207 { ISD::CTPOP, MVT::v4i64, 14 }, 1208 { ISD::CTPOP, MVT::v8i32, 22 }, 1209 { ISD::CTPOP, MVT::v16i16, 18 }, 1210 { ISD::CTPOP, MVT::v32i8, 12 }, 1211 { ISD::CTTZ, MVT::v4i64, 20 }, 1212 { ISD::CTTZ, MVT::v8i32, 28 }, 1213 { ISD::CTTZ, MVT::v16i16, 24 }, 1214 { ISD::CTTZ, MVT::v32i8, 18 }, 1215 { ISD::FSQRT, MVT::f32, 14 }, // SNB from http://www.agner.org/ 1216 { ISD::FSQRT, MVT::v4f32, 14 }, // SNB from http://www.agner.org/ 1217 { ISD::FSQRT, MVT::v8f32, 28 }, // SNB from http://www.agner.org/ 1218 { ISD::FSQRT, MVT::f64, 21 }, // SNB from http://www.agner.org/ 1219 { ISD::FSQRT, MVT::v2f64, 21 }, // SNB from http://www.agner.org/ 1220 { ISD::FSQRT, MVT::v4f64, 43 }, // SNB from http://www.agner.org/ 1221 }; 1222 static const CostTblEntry SSE42CostTbl[] = { 1223 { ISD::FSQRT, MVT::f32, 18 }, // Nehalem from http://www.agner.org/ 1224 { ISD::FSQRT, MVT::v4f32, 18 }, // Nehalem from http://www.agner.org/ 1225 }; 1226 static const CostTblEntry SSSE3CostTbl[] = { 1227 { ISD::BITREVERSE, MVT::v2i64, 5 }, 1228 { ISD::BITREVERSE, MVT::v4i32, 5 }, 1229 { ISD::BITREVERSE, MVT::v8i16, 5 }, 1230 { ISD::BITREVERSE, MVT::v16i8, 5 }, 1231 { ISD::BSWAP, MVT::v2i64, 1 }, 1232 { ISD::BSWAP, MVT::v4i32, 1 }, 1233 { ISD::BSWAP, MVT::v8i16, 1 }, 1234 { ISD::CTLZ, MVT::v2i64, 23 }, 1235 { ISD::CTLZ, MVT::v4i32, 18 }, 1236 { ISD::CTLZ, MVT::v8i16, 14 }, 1237 { ISD::CTLZ, MVT::v16i8, 9 }, 1238 { ISD::CTPOP, MVT::v2i64, 7 }, 1239 { ISD::CTPOP, MVT::v4i32, 11 }, 1240 { ISD::CTPOP, MVT::v8i16, 9 }, 1241 { ISD::CTPOP, MVT::v16i8, 6 }, 1242 { ISD::CTTZ, MVT::v2i64, 10 }, 1243 { ISD::CTTZ, MVT::v4i32, 14 }, 1244 { ISD::CTTZ, MVT::v8i16, 12 }, 1245 { ISD::CTTZ, MVT::v16i8, 9 } 1246 }; 1247 static const CostTblEntry SSE2CostTbl[] = { 1248 { ISD::BSWAP, MVT::v2i64, 7 }, 1249 { ISD::BSWAP, MVT::v4i32, 7 }, 1250 { ISD::BSWAP, MVT::v8i16, 7 }, 1251 { ISD::CTLZ, MVT::v2i64, 25 }, 1252 { ISD::CTLZ, MVT::v4i32, 26 }, 1253 { ISD::CTLZ, MVT::v8i16, 20 }, 1254 { ISD::CTLZ, MVT::v16i8, 17 }, 1255 { ISD::CTPOP, MVT::v2i64, 12 }, 1256 { ISD::CTPOP, MVT::v4i32, 15 }, 1257 { ISD::CTPOP, MVT::v8i16, 13 }, 1258 { ISD::CTPOP, MVT::v16i8, 10 }, 1259 { ISD::CTTZ, MVT::v2i64, 14 }, 1260 { ISD::CTTZ, MVT::v4i32, 18 }, 1261 { ISD::CTTZ, MVT::v8i16, 16 }, 1262 { ISD::CTTZ, MVT::v16i8, 13 }, 1263 { ISD::FSQRT, MVT::f64, 32 }, // Nehalem from http://www.agner.org/ 1264 { ISD::FSQRT, MVT::v2f64, 32 }, // Nehalem from http://www.agner.org/ 1265 }; 1266 static const CostTblEntry SSE1CostTbl[] = { 1267 { ISD::FSQRT, MVT::f32, 28 }, // Pentium III from http://www.agner.org/ 1268 { ISD::FSQRT, MVT::v4f32, 56 }, // Pentium III from http://www.agner.org/ 1269 }; 1270 1271 unsigned ISD = ISD::DELETED_NODE; 1272 switch (IID) { 1273 default: 1274 break; 1275 case Intrinsic::bitreverse: 1276 ISD = ISD::BITREVERSE; 1277 break; 1278 case Intrinsic::bswap: 1279 ISD = ISD::BSWAP; 1280 break; 1281 case Intrinsic::ctlz: 1282 ISD = ISD::CTLZ; 1283 break; 1284 case Intrinsic::ctpop: 1285 ISD = ISD::CTPOP; 1286 break; 1287 case Intrinsic::cttz: 1288 ISD = ISD::CTTZ; 1289 break; 1290 case Intrinsic::sqrt: 1291 ISD = ISD::FSQRT; 1292 break; 1293 } 1294 1295 // Legalize the type. 1296 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, RetTy); 1297 MVT MTy = LT.second; 1298 1299 // Attempt to lookup cost. 1300 if (ST->hasXOP()) 1301 if (const auto *Entry = CostTableLookup(XOPCostTbl, ISD, MTy)) 1302 return LT.first * Entry->Cost; 1303 1304 if (ST->hasAVX2()) 1305 if (const auto *Entry = CostTableLookup(AVX2CostTbl, ISD, MTy)) 1306 return LT.first * Entry->Cost; 1307 1308 if (ST->hasAVX()) 1309 if (const auto *Entry = CostTableLookup(AVX1CostTbl, ISD, MTy)) 1310 return LT.first * Entry->Cost; 1311 1312 if (ST->hasSSE42()) 1313 if (const auto *Entry = CostTableLookup(SSE42CostTbl, ISD, MTy)) 1314 return LT.first * Entry->Cost; 1315 1316 if (ST->hasSSSE3()) 1317 if (const auto *Entry = CostTableLookup(SSSE3CostTbl, ISD, MTy)) 1318 return LT.first * Entry->Cost; 1319 1320 if (ST->hasSSE2()) 1321 if (const auto *Entry = CostTableLookup(SSE2CostTbl, ISD, MTy)) 1322 return LT.first * Entry->Cost; 1323 1324 if (ST->hasSSE1()) 1325 if (const auto *Entry = CostTableLookup(SSE1CostTbl, ISD, MTy)) 1326 return LT.first * Entry->Cost; 1327 1328 return BaseT::getIntrinsicInstrCost(IID, RetTy, Tys, FMF); 1329 } 1330 1331 int X86TTIImpl::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy, 1332 ArrayRef<Value *> Args, FastMathFlags FMF) { 1333 return BaseT::getIntrinsicInstrCost(IID, RetTy, Args, FMF); 1334 } 1335 1336 int X86TTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) { 1337 assert(Val->isVectorTy() && "This must be a vector type"); 1338 1339 Type *ScalarType = Val->getScalarType(); 1340 1341 if (Index != -1U) { 1342 // Legalize the type. 1343 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Val); 1344 1345 // This type is legalized to a scalar type. 1346 if (!LT.second.isVector()) 1347 return 0; 1348 1349 // The type may be split. Normalize the index to the new type. 1350 unsigned Width = LT.second.getVectorNumElements(); 1351 Index = Index % Width; 1352 1353 // Floating point scalars are already located in index #0. 1354 if (ScalarType->isFloatingPointTy() && Index == 0) 1355 return 0; 1356 } 1357 1358 // Add to the base cost if we know that the extracted element of a vector is 1359 // destined to be moved to and used in the integer register file. 1360 int RegisterFileMoveCost = 0; 1361 if (Opcode == Instruction::ExtractElement && ScalarType->isPointerTy()) 1362 RegisterFileMoveCost = 1; 1363 1364 return BaseT::getVectorInstrCost(Opcode, Val, Index) + RegisterFileMoveCost; 1365 } 1366 1367 int X86TTIImpl::getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) { 1368 assert (Ty->isVectorTy() && "Can only scalarize vectors"); 1369 int Cost = 0; 1370 1371 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) { 1372 if (Insert) 1373 Cost += getVectorInstrCost(Instruction::InsertElement, Ty, i); 1374 if (Extract) 1375 Cost += getVectorInstrCost(Instruction::ExtractElement, Ty, i); 1376 } 1377 1378 return Cost; 1379 } 1380 1381 int X86TTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment, 1382 unsigned AddressSpace) { 1383 // Handle non-power-of-two vectors such as <3 x float> 1384 if (VectorType *VTy = dyn_cast<VectorType>(Src)) { 1385 unsigned NumElem = VTy->getVectorNumElements(); 1386 1387 // Handle a few common cases: 1388 // <3 x float> 1389 if (NumElem == 3 && VTy->getScalarSizeInBits() == 32) 1390 // Cost = 64 bit store + extract + 32 bit store. 1391 return 3; 1392 1393 // <3 x double> 1394 if (NumElem == 3 && VTy->getScalarSizeInBits() == 64) 1395 // Cost = 128 bit store + unpack + 64 bit store. 1396 return 3; 1397 1398 // Assume that all other non-power-of-two numbers are scalarized. 1399 if (!isPowerOf2_32(NumElem)) { 1400 int Cost = BaseT::getMemoryOpCost(Opcode, VTy->getScalarType(), Alignment, 1401 AddressSpace); 1402 int SplitCost = getScalarizationOverhead(Src, Opcode == Instruction::Load, 1403 Opcode == Instruction::Store); 1404 return NumElem * Cost + SplitCost; 1405 } 1406 } 1407 1408 // Legalize the type. 1409 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src); 1410 assert((Opcode == Instruction::Load || Opcode == Instruction::Store) && 1411 "Invalid Opcode"); 1412 1413 // Each load/store unit costs 1. 1414 int Cost = LT.first * 1; 1415 1416 // This isn't exactly right. We're using slow unaligned 32-byte accesses as a 1417 // proxy for a double-pumped AVX memory interface such as on Sandybridge. 1418 if (LT.second.getStoreSize() == 32 && ST->isUnalignedMem32Slow()) 1419 Cost *= 2; 1420 1421 return Cost; 1422 } 1423 1424 int X86TTIImpl::getMaskedMemoryOpCost(unsigned Opcode, Type *SrcTy, 1425 unsigned Alignment, 1426 unsigned AddressSpace) { 1427 VectorType *SrcVTy = dyn_cast<VectorType>(SrcTy); 1428 if (!SrcVTy) 1429 // To calculate scalar take the regular cost, without mask 1430 return getMemoryOpCost(Opcode, SrcTy, Alignment, AddressSpace); 1431 1432 unsigned NumElem = SrcVTy->getVectorNumElements(); 1433 VectorType *MaskTy = 1434 VectorType::get(Type::getInt8Ty(SrcVTy->getContext()), NumElem); 1435 if ((Opcode == Instruction::Load && !isLegalMaskedLoad(SrcVTy)) || 1436 (Opcode == Instruction::Store && !isLegalMaskedStore(SrcVTy)) || 1437 !isPowerOf2_32(NumElem)) { 1438 // Scalarization 1439 int MaskSplitCost = getScalarizationOverhead(MaskTy, false, true); 1440 int ScalarCompareCost = getCmpSelInstrCost( 1441 Instruction::ICmp, Type::getInt8Ty(SrcVTy->getContext()), nullptr); 1442 int BranchCost = getCFInstrCost(Instruction::Br); 1443 int MaskCmpCost = NumElem * (BranchCost + ScalarCompareCost); 1444 1445 int ValueSplitCost = getScalarizationOverhead( 1446 SrcVTy, Opcode == Instruction::Load, Opcode == Instruction::Store); 1447 int MemopCost = 1448 NumElem * BaseT::getMemoryOpCost(Opcode, SrcVTy->getScalarType(), 1449 Alignment, AddressSpace); 1450 return MemopCost + ValueSplitCost + MaskSplitCost + MaskCmpCost; 1451 } 1452 1453 // Legalize the type. 1454 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, SrcVTy); 1455 auto VT = TLI->getValueType(DL, SrcVTy); 1456 int Cost = 0; 1457 if (VT.isSimple() && LT.second != VT.getSimpleVT() && 1458 LT.second.getVectorNumElements() == NumElem) 1459 // Promotion requires expand/truncate for data and a shuffle for mask. 1460 Cost += getShuffleCost(TTI::SK_Alternate, SrcVTy, 0, nullptr) + 1461 getShuffleCost(TTI::SK_Alternate, MaskTy, 0, nullptr); 1462 1463 else if (LT.second.getVectorNumElements() > NumElem) { 1464 VectorType *NewMaskTy = VectorType::get(MaskTy->getVectorElementType(), 1465 LT.second.getVectorNumElements()); 1466 // Expanding requires fill mask with zeroes 1467 Cost += getShuffleCost(TTI::SK_InsertSubvector, NewMaskTy, 0, MaskTy); 1468 } 1469 if (!ST->hasAVX512()) 1470 return Cost + LT.first*4; // Each maskmov costs 4 1471 1472 // AVX-512 masked load/store is cheapper 1473 return Cost+LT.first; 1474 } 1475 1476 int X86TTIImpl::getAddressComputationCost(Type *Ty, bool IsComplex) { 1477 // Address computations in vectorized code with non-consecutive addresses will 1478 // likely result in more instructions compared to scalar code where the 1479 // computation can more often be merged into the index mode. The resulting 1480 // extra micro-ops can significantly decrease throughput. 1481 unsigned NumVectorInstToHideOverhead = 10; 1482 1483 if (Ty->isVectorTy() && IsComplex) 1484 return NumVectorInstToHideOverhead; 1485 1486 return BaseT::getAddressComputationCost(Ty, IsComplex); 1487 } 1488 1489 int X86TTIImpl::getReductionCost(unsigned Opcode, Type *ValTy, 1490 bool IsPairwise) { 1491 1492 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy); 1493 1494 MVT MTy = LT.second; 1495 1496 int ISD = TLI->InstructionOpcodeToISD(Opcode); 1497 assert(ISD && "Invalid opcode"); 1498 1499 // We use the Intel Architecture Code Analyzer(IACA) to measure the throughput 1500 // and make it as the cost. 1501 1502 static const CostTblEntry SSE42CostTblPairWise[] = { 1503 { ISD::FADD, MVT::v2f64, 2 }, 1504 { ISD::FADD, MVT::v4f32, 4 }, 1505 { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6". 1506 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5". 1507 { ISD::ADD, MVT::v8i16, 5 }, 1508 }; 1509 1510 static const CostTblEntry AVX1CostTblPairWise[] = { 1511 { ISD::FADD, MVT::v4f32, 4 }, 1512 { ISD::FADD, MVT::v4f64, 5 }, 1513 { ISD::FADD, MVT::v8f32, 7 }, 1514 { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5". 1515 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5". 1516 { ISD::ADD, MVT::v4i64, 5 }, // The data reported by the IACA tool is "4.8". 1517 { ISD::ADD, MVT::v8i16, 5 }, 1518 { ISD::ADD, MVT::v8i32, 5 }, 1519 }; 1520 1521 static const CostTblEntry SSE42CostTblNoPairWise[] = { 1522 { ISD::FADD, MVT::v2f64, 2 }, 1523 { ISD::FADD, MVT::v4f32, 4 }, 1524 { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6". 1525 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.3". 1526 { ISD::ADD, MVT::v8i16, 4 }, // The data reported by the IACA tool is "4.3". 1527 }; 1528 1529 static const CostTblEntry AVX1CostTblNoPairWise[] = { 1530 { ISD::FADD, MVT::v4f32, 3 }, 1531 { ISD::FADD, MVT::v4f64, 3 }, 1532 { ISD::FADD, MVT::v8f32, 4 }, 1533 { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5". 1534 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "2.8". 1535 { ISD::ADD, MVT::v4i64, 3 }, 1536 { ISD::ADD, MVT::v8i16, 4 }, 1537 { ISD::ADD, MVT::v8i32, 5 }, 1538 }; 1539 1540 if (IsPairwise) { 1541 if (ST->hasAVX()) 1542 if (const auto *Entry = CostTableLookup(AVX1CostTblPairWise, ISD, MTy)) 1543 return LT.first * Entry->Cost; 1544 1545 if (ST->hasSSE42()) 1546 if (const auto *Entry = CostTableLookup(SSE42CostTblPairWise, ISD, MTy)) 1547 return LT.first * Entry->Cost; 1548 } else { 1549 if (ST->hasAVX()) 1550 if (const auto *Entry = CostTableLookup(AVX1CostTblNoPairWise, ISD, MTy)) 1551 return LT.first * Entry->Cost; 1552 1553 if (ST->hasSSE42()) 1554 if (const auto *Entry = CostTableLookup(SSE42CostTblNoPairWise, ISD, MTy)) 1555 return LT.first * Entry->Cost; 1556 } 1557 1558 return BaseT::getReductionCost(Opcode, ValTy, IsPairwise); 1559 } 1560 1561 /// \brief Calculate the cost of materializing a 64-bit value. This helper 1562 /// method might only calculate a fraction of a larger immediate. Therefore it 1563 /// is valid to return a cost of ZERO. 1564 int X86TTIImpl::getIntImmCost(int64_t Val) { 1565 if (Val == 0) 1566 return TTI::TCC_Free; 1567 1568 if (isInt<32>(Val)) 1569 return TTI::TCC_Basic; 1570 1571 return 2 * TTI::TCC_Basic; 1572 } 1573 1574 int X86TTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) { 1575 assert(Ty->isIntegerTy()); 1576 1577 unsigned BitSize = Ty->getPrimitiveSizeInBits(); 1578 if (BitSize == 0) 1579 return ~0U; 1580 1581 // Never hoist constants larger than 128bit, because this might lead to 1582 // incorrect code generation or assertions in codegen. 1583 // Fixme: Create a cost model for types larger than i128 once the codegen 1584 // issues have been fixed. 1585 if (BitSize > 128) 1586 return TTI::TCC_Free; 1587 1588 if (Imm == 0) 1589 return TTI::TCC_Free; 1590 1591 // Sign-extend all constants to a multiple of 64-bit. 1592 APInt ImmVal = Imm; 1593 if (BitSize & 0x3f) 1594 ImmVal = Imm.sext((BitSize + 63) & ~0x3fU); 1595 1596 // Split the constant into 64-bit chunks and calculate the cost for each 1597 // chunk. 1598 int Cost = 0; 1599 for (unsigned ShiftVal = 0; ShiftVal < BitSize; ShiftVal += 64) { 1600 APInt Tmp = ImmVal.ashr(ShiftVal).sextOrTrunc(64); 1601 int64_t Val = Tmp.getSExtValue(); 1602 Cost += getIntImmCost(Val); 1603 } 1604 // We need at least one instruction to materialize the constant. 1605 return std::max(1, Cost); 1606 } 1607 1608 int X86TTIImpl::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm, 1609 Type *Ty) { 1610 assert(Ty->isIntegerTy()); 1611 1612 unsigned BitSize = Ty->getPrimitiveSizeInBits(); 1613 // There is no cost model for constants with a bit size of 0. Return TCC_Free 1614 // here, so that constant hoisting will ignore this constant. 1615 if (BitSize == 0) 1616 return TTI::TCC_Free; 1617 1618 unsigned ImmIdx = ~0U; 1619 switch (Opcode) { 1620 default: 1621 return TTI::TCC_Free; 1622 case Instruction::GetElementPtr: 1623 // Always hoist the base address of a GetElementPtr. This prevents the 1624 // creation of new constants for every base constant that gets constant 1625 // folded with the offset. 1626 if (Idx == 0) 1627 return 2 * TTI::TCC_Basic; 1628 return TTI::TCC_Free; 1629 case Instruction::Store: 1630 ImmIdx = 0; 1631 break; 1632 case Instruction::ICmp: 1633 // This is an imperfect hack to prevent constant hoisting of 1634 // compares that might be trying to check if a 64-bit value fits in 1635 // 32-bits. The backend can optimize these cases using a right shift by 32. 1636 // Ideally we would check the compare predicate here. There also other 1637 // similar immediates the backend can use shifts for. 1638 if (Idx == 1 && Imm.getBitWidth() == 64) { 1639 uint64_t ImmVal = Imm.getZExtValue(); 1640 if (ImmVal == 0x100000000ULL || ImmVal == 0xffffffff) 1641 return TTI::TCC_Free; 1642 } 1643 ImmIdx = 1; 1644 break; 1645 case Instruction::And: 1646 // We support 64-bit ANDs with immediates with 32-bits of leading zeroes 1647 // by using a 32-bit operation with implicit zero extension. Detect such 1648 // immediates here as the normal path expects bit 31 to be sign extended. 1649 if (Idx == 1 && Imm.getBitWidth() == 64 && isUInt<32>(Imm.getZExtValue())) 1650 return TTI::TCC_Free; 1651 LLVM_FALLTHROUGH; 1652 case Instruction::Add: 1653 case Instruction::Sub: 1654 case Instruction::Mul: 1655 case Instruction::UDiv: 1656 case Instruction::SDiv: 1657 case Instruction::URem: 1658 case Instruction::SRem: 1659 case Instruction::Or: 1660 case Instruction::Xor: 1661 ImmIdx = 1; 1662 break; 1663 // Always return TCC_Free for the shift value of a shift instruction. 1664 case Instruction::Shl: 1665 case Instruction::LShr: 1666 case Instruction::AShr: 1667 if (Idx == 1) 1668 return TTI::TCC_Free; 1669 break; 1670 case Instruction::Trunc: 1671 case Instruction::ZExt: 1672 case Instruction::SExt: 1673 case Instruction::IntToPtr: 1674 case Instruction::PtrToInt: 1675 case Instruction::BitCast: 1676 case Instruction::PHI: 1677 case Instruction::Call: 1678 case Instruction::Select: 1679 case Instruction::Ret: 1680 case Instruction::Load: 1681 break; 1682 } 1683 1684 if (Idx == ImmIdx) { 1685 int NumConstants = (BitSize + 63) / 64; 1686 int Cost = X86TTIImpl::getIntImmCost(Imm, Ty); 1687 return (Cost <= NumConstants * TTI::TCC_Basic) 1688 ? static_cast<int>(TTI::TCC_Free) 1689 : Cost; 1690 } 1691 1692 return X86TTIImpl::getIntImmCost(Imm, Ty); 1693 } 1694 1695 int X86TTIImpl::getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm, 1696 Type *Ty) { 1697 assert(Ty->isIntegerTy()); 1698 1699 unsigned BitSize = Ty->getPrimitiveSizeInBits(); 1700 // There is no cost model for constants with a bit size of 0. Return TCC_Free 1701 // here, so that constant hoisting will ignore this constant. 1702 if (BitSize == 0) 1703 return TTI::TCC_Free; 1704 1705 switch (IID) { 1706 default: 1707 return TTI::TCC_Free; 1708 case Intrinsic::sadd_with_overflow: 1709 case Intrinsic::uadd_with_overflow: 1710 case Intrinsic::ssub_with_overflow: 1711 case Intrinsic::usub_with_overflow: 1712 case Intrinsic::smul_with_overflow: 1713 case Intrinsic::umul_with_overflow: 1714 if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue())) 1715 return TTI::TCC_Free; 1716 break; 1717 case Intrinsic::experimental_stackmap: 1718 if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue()))) 1719 return TTI::TCC_Free; 1720 break; 1721 case Intrinsic::experimental_patchpoint_void: 1722 case Intrinsic::experimental_patchpoint_i64: 1723 if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue()))) 1724 return TTI::TCC_Free; 1725 break; 1726 } 1727 return X86TTIImpl::getIntImmCost(Imm, Ty); 1728 } 1729 1730 // Return an average cost of Gather / Scatter instruction, maybe improved later 1731 int X86TTIImpl::getGSVectorCost(unsigned Opcode, Type *SrcVTy, Value *Ptr, 1732 unsigned Alignment, unsigned AddressSpace) { 1733 1734 assert(isa<VectorType>(SrcVTy) && "Unexpected type in getGSVectorCost"); 1735 unsigned VF = SrcVTy->getVectorNumElements(); 1736 1737 // Try to reduce index size from 64 bit (default for GEP) 1738 // to 32. It is essential for VF 16. If the index can't be reduced to 32, the 1739 // operation will use 16 x 64 indices which do not fit in a zmm and needs 1740 // to split. Also check that the base pointer is the same for all lanes, 1741 // and that there's at most one variable index. 1742 auto getIndexSizeInBits = [](Value *Ptr, const DataLayout& DL) { 1743 unsigned IndexSize = DL.getPointerSizeInBits(); 1744 GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr); 1745 if (IndexSize < 64 || !GEP) 1746 return IndexSize; 1747 1748 unsigned NumOfVarIndices = 0; 1749 Value *Ptrs = GEP->getPointerOperand(); 1750 if (Ptrs->getType()->isVectorTy() && !getSplatValue(Ptrs)) 1751 return IndexSize; 1752 for (unsigned i = 1; i < GEP->getNumOperands(); ++i) { 1753 if (isa<Constant>(GEP->getOperand(i))) 1754 continue; 1755 Type *IndxTy = GEP->getOperand(i)->getType(); 1756 if (IndxTy->isVectorTy()) 1757 IndxTy = IndxTy->getVectorElementType(); 1758 if ((IndxTy->getPrimitiveSizeInBits() == 64 && 1759 !isa<SExtInst>(GEP->getOperand(i))) || 1760 ++NumOfVarIndices > 1) 1761 return IndexSize; // 64 1762 } 1763 return (unsigned)32; 1764 }; 1765 1766 1767 // Trying to reduce IndexSize to 32 bits for vector 16. 1768 // By default the IndexSize is equal to pointer size. 1769 unsigned IndexSize = (VF >= 16) ? getIndexSizeInBits(Ptr, DL) : 1770 DL.getPointerSizeInBits(); 1771 1772 Type *IndexVTy = VectorType::get(IntegerType::get(SrcVTy->getContext(), 1773 IndexSize), VF); 1774 std::pair<int, MVT> IdxsLT = TLI->getTypeLegalizationCost(DL, IndexVTy); 1775 std::pair<int, MVT> SrcLT = TLI->getTypeLegalizationCost(DL, SrcVTy); 1776 int SplitFactor = std::max(IdxsLT.first, SrcLT.first); 1777 if (SplitFactor > 1) { 1778 // Handle splitting of vector of pointers 1779 Type *SplitSrcTy = VectorType::get(SrcVTy->getScalarType(), VF / SplitFactor); 1780 return SplitFactor * getGSVectorCost(Opcode, SplitSrcTy, Ptr, Alignment, 1781 AddressSpace); 1782 } 1783 1784 // The gather / scatter cost is given by Intel architects. It is a rough 1785 // number since we are looking at one instruction in a time. 1786 const int GSOverhead = 2; 1787 return GSOverhead + VF * getMemoryOpCost(Opcode, SrcVTy->getScalarType(), 1788 Alignment, AddressSpace); 1789 } 1790 1791 /// Return the cost of full scalarization of gather / scatter operation. 1792 /// 1793 /// Opcode - Load or Store instruction. 1794 /// SrcVTy - The type of the data vector that should be gathered or scattered. 1795 /// VariableMask - The mask is non-constant at compile time. 1796 /// Alignment - Alignment for one element. 1797 /// AddressSpace - pointer[s] address space. 1798 /// 1799 int X86TTIImpl::getGSScalarCost(unsigned Opcode, Type *SrcVTy, 1800 bool VariableMask, unsigned Alignment, 1801 unsigned AddressSpace) { 1802 unsigned VF = SrcVTy->getVectorNumElements(); 1803 1804 int MaskUnpackCost = 0; 1805 if (VariableMask) { 1806 VectorType *MaskTy = 1807 VectorType::get(Type::getInt1Ty(SrcVTy->getContext()), VF); 1808 MaskUnpackCost = getScalarizationOverhead(MaskTy, false, true); 1809 int ScalarCompareCost = 1810 getCmpSelInstrCost(Instruction::ICmp, Type::getInt1Ty(SrcVTy->getContext()), 1811 nullptr); 1812 int BranchCost = getCFInstrCost(Instruction::Br); 1813 MaskUnpackCost += VF * (BranchCost + ScalarCompareCost); 1814 } 1815 1816 // The cost of the scalar loads/stores. 1817 int MemoryOpCost = VF * getMemoryOpCost(Opcode, SrcVTy->getScalarType(), 1818 Alignment, AddressSpace); 1819 1820 int InsertExtractCost = 0; 1821 if (Opcode == Instruction::Load) 1822 for (unsigned i = 0; i < VF; ++i) 1823 // Add the cost of inserting each scalar load into the vector 1824 InsertExtractCost += 1825 getVectorInstrCost(Instruction::InsertElement, SrcVTy, i); 1826 else 1827 for (unsigned i = 0; i < VF; ++i) 1828 // Add the cost of extracting each element out of the data vector 1829 InsertExtractCost += 1830 getVectorInstrCost(Instruction::ExtractElement, SrcVTy, i); 1831 1832 return MemoryOpCost + MaskUnpackCost + InsertExtractCost; 1833 } 1834 1835 /// Calculate the cost of Gather / Scatter operation 1836 int X86TTIImpl::getGatherScatterOpCost(unsigned Opcode, Type *SrcVTy, 1837 Value *Ptr, bool VariableMask, 1838 unsigned Alignment) { 1839 assert(SrcVTy->isVectorTy() && "Unexpected data type for Gather/Scatter"); 1840 unsigned VF = SrcVTy->getVectorNumElements(); 1841 PointerType *PtrTy = dyn_cast<PointerType>(Ptr->getType()); 1842 if (!PtrTy && Ptr->getType()->isVectorTy()) 1843 PtrTy = dyn_cast<PointerType>(Ptr->getType()->getVectorElementType()); 1844 assert(PtrTy && "Unexpected type for Ptr argument"); 1845 unsigned AddressSpace = PtrTy->getAddressSpace(); 1846 1847 bool Scalarize = false; 1848 if ((Opcode == Instruction::Load && !isLegalMaskedGather(SrcVTy)) || 1849 (Opcode == Instruction::Store && !isLegalMaskedScatter(SrcVTy))) 1850 Scalarize = true; 1851 // Gather / Scatter for vector 2 is not profitable on KNL / SKX 1852 // Vector-4 of gather/scatter instruction does not exist on KNL. 1853 // We can extend it to 8 elements, but zeroing upper bits of 1854 // the mask vector will add more instructions. Right now we give the scalar 1855 // cost of vector-4 for KNL. TODO: Check, maybe the gather/scatter instruction is 1856 // better in the VariableMask case. 1857 if (VF == 2 || (VF == 4 && !ST->hasVLX())) 1858 Scalarize = true; 1859 1860 if (Scalarize) 1861 return getGSScalarCost(Opcode, SrcVTy, VariableMask, Alignment, AddressSpace); 1862 1863 return getGSVectorCost(Opcode, SrcVTy, Ptr, Alignment, AddressSpace); 1864 } 1865 1866 bool X86TTIImpl::isLegalMaskedLoad(Type *DataTy) { 1867 Type *ScalarTy = DataTy->getScalarType(); 1868 int DataWidth = isa<PointerType>(ScalarTy) ? 1869 DL.getPointerSizeInBits() : ScalarTy->getPrimitiveSizeInBits(); 1870 1871 return ((DataWidth == 32 || DataWidth == 64) && ST->hasAVX()) || 1872 ((DataWidth == 8 || DataWidth == 16) && ST->hasBWI()); 1873 } 1874 1875 bool X86TTIImpl::isLegalMaskedStore(Type *DataType) { 1876 return isLegalMaskedLoad(DataType); 1877 } 1878 1879 bool X86TTIImpl::isLegalMaskedGather(Type *DataTy) { 1880 // This function is called now in two cases: from the Loop Vectorizer 1881 // and from the Scalarizer. 1882 // When the Loop Vectorizer asks about legality of the feature, 1883 // the vectorization factor is not calculated yet. The Loop Vectorizer 1884 // sends a scalar type and the decision is based on the width of the 1885 // scalar element. 1886 // Later on, the cost model will estimate usage this intrinsic based on 1887 // the vector type. 1888 // The Scalarizer asks again about legality. It sends a vector type. 1889 // In this case we can reject non-power-of-2 vectors. 1890 if (isa<VectorType>(DataTy) && !isPowerOf2_32(DataTy->getVectorNumElements())) 1891 return false; 1892 Type *ScalarTy = DataTy->getScalarType(); 1893 int DataWidth = isa<PointerType>(ScalarTy) ? 1894 DL.getPointerSizeInBits() : ScalarTy->getPrimitiveSizeInBits(); 1895 1896 // AVX-512 allows gather and scatter 1897 return (DataWidth == 32 || DataWidth == 64) && ST->hasAVX512(); 1898 } 1899 1900 bool X86TTIImpl::isLegalMaskedScatter(Type *DataType) { 1901 return isLegalMaskedGather(DataType); 1902 } 1903 1904 bool X86TTIImpl::areInlineCompatible(const Function *Caller, 1905 const Function *Callee) const { 1906 const TargetMachine &TM = getTLI()->getTargetMachine(); 1907 1908 // Work this as a subsetting of subtarget features. 1909 const FeatureBitset &CallerBits = 1910 TM.getSubtargetImpl(*Caller)->getFeatureBits(); 1911 const FeatureBitset &CalleeBits = 1912 TM.getSubtargetImpl(*Callee)->getFeatureBits(); 1913 1914 // FIXME: This is likely too limiting as it will include subtarget features 1915 // that we might not care about for inlining, but it is conservatively 1916 // correct. 1917 return (CallerBits & CalleeBits) == CalleeBits; 1918 } 1919 1920 bool X86TTIImpl::enableInterleavedAccessVectorization() { 1921 // TODO: We expect this to be beneficial regardless of arch, 1922 // but there are currently some unexplained performance artifacts on Atom. 1923 // As a temporary solution, disable on Atom. 1924 return !(ST->isAtom() || ST->isSLM()); 1925 } 1926