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::UINT_TO_FP, MVT::v2f32, MVT::v2i64, 1 }, 720 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 1 }, 721 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i64, 1 }, 722 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i64, 1 }, 723 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i64, 1 }, 724 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i64, 1 }, 725 726 { ISD::FP_TO_UINT, MVT::v2i64, MVT::v2f32, 1 }, 727 { ISD::FP_TO_UINT, MVT::v4i64, MVT::v4f32, 1 }, 728 { ISD::FP_TO_UINT, MVT::v8i64, MVT::v8f32, 1 }, 729 { ISD::FP_TO_UINT, MVT::v2i64, MVT::v2f64, 1 }, 730 { ISD::FP_TO_UINT, MVT::v4i64, MVT::v4f64, 1 }, 731 { ISD::FP_TO_UINT, MVT::v8i64, MVT::v8f64, 1 }, 732 }; 733 734 // TODO: For AVX512DQ + AVX512VL, we also have cheap casts for 128-bit and 735 // 256-bit wide vectors. 736 737 static const TypeConversionCostTblEntry AVX512FConversionTbl[] = { 738 { ISD::FP_EXTEND, MVT::v8f64, MVT::v8f32, 1 }, 739 { ISD::FP_EXTEND, MVT::v8f64, MVT::v16f32, 3 }, 740 { ISD::FP_ROUND, MVT::v8f32, MVT::v8f64, 1 }, 741 742 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i32, 1 }, 743 { ISD::TRUNCATE, MVT::v16i16, MVT::v16i32, 1 }, 744 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i64, 1 }, 745 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 1 }, 746 747 // v16i1 -> v16i32 - load + broadcast 748 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i1, 2 }, 749 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i1, 2 }, 750 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 1 }, 751 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 1 }, 752 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i16, 1 }, 753 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i16, 1 }, 754 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i16, 1 }, 755 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i16, 1 }, 756 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i32, 1 }, 757 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i32, 1 }, 758 759 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i1, 4 }, 760 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i1, 3 }, 761 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i8, 2 }, 762 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i8, 2 }, 763 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i16, 2 }, 764 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i16, 2 }, 765 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i32, 1 }, 766 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i32, 1 }, 767 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i64, 26 }, 768 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i64, 26 }, 769 770 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i1, 4 }, 771 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i1, 3 }, 772 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i8, 2 }, 773 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i8, 2 }, 774 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 2 }, 775 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i8, 2 }, 776 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i8, 2 }, 777 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i16, 5 }, 778 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i16, 2 }, 779 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 2 }, 780 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i16, 2 }, 781 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i16, 2 }, 782 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i32, 2 }, 783 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i32, 1 }, 784 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 }, 785 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i32, 1 }, 786 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 1 }, 787 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i32, 1 }, 788 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i32, 1 }, 789 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i64, 5 }, 790 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 5 }, 791 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i64, 12 }, 792 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i64, 26 }, 793 794 { ISD::FP_TO_UINT, MVT::v2i32, MVT::v2f32, 1 }, 795 { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f32, 1 }, 796 { ISD::FP_TO_UINT, MVT::v8i32, MVT::v8f32, 1 }, 797 { ISD::FP_TO_UINT, MVT::v16i32, MVT::v16f32, 1 }, 798 }; 799 800 static const TypeConversionCostTblEntry AVX2ConversionTbl[] = { 801 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 3 }, 802 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 3 }, 803 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 3 }, 804 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 3 }, 805 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 3 }, 806 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 3 }, 807 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 3 }, 808 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 3 }, 809 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 1 }, 810 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 1 }, 811 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 3 }, 812 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 }, 813 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 1 }, 814 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 1 }, 815 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 1 }, 816 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 1 }, 817 818 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 2 }, 819 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 2 }, 820 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 2 }, 821 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 2 }, 822 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 2 }, 823 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 4 }, 824 825 { ISD::FP_EXTEND, MVT::v8f64, MVT::v8f32, 3 }, 826 { ISD::FP_ROUND, MVT::v8f32, MVT::v8f64, 3 }, 827 828 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 8 }, 829 }; 830 831 static const TypeConversionCostTblEntry AVXConversionTbl[] = { 832 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 6 }, 833 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 4 }, 834 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 7 }, 835 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 4 }, 836 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 6 }, 837 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 4 }, 838 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 7 }, 839 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 4 }, 840 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 4 }, 841 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 4 }, 842 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 6 }, 843 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 }, 844 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 4 }, 845 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 4 }, 846 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 4 }, 847 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 4 }, 848 849 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i16, 4 }, 850 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 4 }, 851 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 5 }, 852 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 4 }, 853 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 4 }, 854 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 4 }, 855 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 9 }, 856 857 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i1, 3 }, 858 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i1, 3 }, 859 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i1, 8 }, 860 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 }, 861 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i8, 3 }, 862 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i8, 8 }, 863 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i16, 3 }, 864 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i16, 3 }, 865 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 }, 866 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 }, 867 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i32, 1 }, 868 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i32, 1 }, 869 870 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i1, 7 }, 871 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i1, 7 }, 872 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i1, 6 }, 873 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 2 }, 874 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i8, 2 }, 875 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 5 }, 876 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 }, 877 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i16, 2 }, 878 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 }, 879 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i32, 6 }, 880 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 6 }, 881 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i32, 6 }, 882 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 9 }, 883 // The generic code to compute the scalar overhead is currently broken. 884 // Workaround this limitation by estimating the scalarization overhead 885 // here. We have roughly 10 instructions per scalar element. 886 // Multiply that by the vector width. 887 // FIXME: remove that when PR19268 is fixed. 888 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 10 }, 889 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i64, 20 }, 890 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i64, 13 }, 891 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i64, 13 }, 892 893 { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 1 }, 894 { ISD::FP_TO_SINT, MVT::v8i8, MVT::v8f32, 7 }, 895 // This node is expanded into scalarized operations but BasicTTI is overly 896 // optimistic estimating its cost. It computes 3 per element (one 897 // vector-extract, one scalar conversion and one vector-insert). The 898 // problem is that the inserts form a read-modify-write chain so latency 899 // should be factored in too. Inflating the cost per element by 1. 900 { ISD::FP_TO_UINT, MVT::v8i32, MVT::v8f32, 8*4 }, 901 { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f64, 4*4 }, 902 903 { ISD::FP_EXTEND, MVT::v4f64, MVT::v4f32, 1 }, 904 { ISD::FP_ROUND, MVT::v4f32, MVT::v4f64, 1 }, 905 }; 906 907 static const TypeConversionCostTblEntry SSE41ConversionTbl[] = { 908 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 2 }, 909 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 2 }, 910 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 2 }, 911 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 2 }, 912 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 2 }, 913 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 2 }, 914 915 { ISD::ZERO_EXTEND, MVT::v4i16, MVT::v4i8, 1 }, 916 { ISD::SIGN_EXTEND, MVT::v4i16, MVT::v4i8, 2 }, 917 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i8, 1 }, 918 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i8, 1 }, 919 { ISD::ZERO_EXTEND, MVT::v8i16, MVT::v8i8, 1 }, 920 { ISD::SIGN_EXTEND, MVT::v8i16, MVT::v8i8, 1 }, 921 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 2 }, 922 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 2 }, 923 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 2 }, 924 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 2 }, 925 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 4 }, 926 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 4 }, 927 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i16, 1 }, 928 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i16, 1 }, 929 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 2 }, 930 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 2 }, 931 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i16, 4 }, 932 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i16, 4 }, 933 934 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i16, 2 }, 935 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i16, 1 }, 936 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i32, 1 }, 937 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i32, 1 }, 938 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 3 }, 939 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 3 }, 940 { ISD::TRUNCATE, MVT::v16i16, MVT::v16i32, 6 }, 941 942 }; 943 944 static const TypeConversionCostTblEntry SSE2ConversionTbl[] = { 945 // These are somewhat magic numbers justified by looking at the output of 946 // Intel's IACA, running some kernels and making sure when we take 947 // legalization into account the throughput will be overestimated. 948 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 }, 949 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 }, 950 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 }, 951 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 }, 952 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 5 }, 953 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 }, 954 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 }, 955 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 }, 956 957 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 }, 958 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 }, 959 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 }, 960 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 }, 961 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 }, 962 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 8 }, 963 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 }, 964 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 }, 965 966 { ISD::FP_TO_SINT, MVT::v2i32, MVT::v2f64, 3 }, 967 968 { ISD::ZERO_EXTEND, MVT::v4i16, MVT::v4i8, 1 }, 969 { ISD::SIGN_EXTEND, MVT::v4i16, MVT::v4i8, 6 }, 970 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i8, 2 }, 971 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i8, 3 }, 972 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 4 }, 973 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 8 }, 974 { ISD::ZERO_EXTEND, MVT::v8i16, MVT::v8i8, 1 }, 975 { ISD::SIGN_EXTEND, MVT::v8i16, MVT::v8i8, 2 }, 976 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 6 }, 977 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 6 }, 978 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 3 }, 979 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 4 }, 980 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 9 }, 981 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 12 }, 982 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i16, 1 }, 983 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i16, 2 }, 984 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 }, 985 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 10 }, 986 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 3 }, 987 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 4 }, 988 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i16, 6 }, 989 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i16, 8 }, 990 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 3 }, 991 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 5 }, 992 993 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i16, 4 }, 994 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i16, 2 }, 995 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i16, 3 }, 996 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i32, 3 }, 997 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i32, 3 }, 998 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 4 }, 999 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i32, 7 }, 1000 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 5 }, 1001 { ISD::TRUNCATE, MVT::v16i16, MVT::v16i32, 10 }, 1002 }; 1003 1004 std::pair<int, MVT> LTSrc = TLI->getTypeLegalizationCost(DL, Src); 1005 std::pair<int, MVT> LTDest = TLI->getTypeLegalizationCost(DL, Dst); 1006 1007 if (ST->hasSSE2() && !ST->hasAVX()) { 1008 if (const auto *Entry = ConvertCostTableLookup(SSE2ConversionTbl, ISD, 1009 LTDest.second, LTSrc.second)) 1010 return LTSrc.first * Entry->Cost; 1011 } 1012 1013 EVT SrcTy = TLI->getValueType(DL, Src); 1014 EVT DstTy = TLI->getValueType(DL, Dst); 1015 1016 // The function getSimpleVT only handles simple value types. 1017 if (!SrcTy.isSimple() || !DstTy.isSimple()) 1018 return BaseT::getCastInstrCost(Opcode, Dst, Src); 1019 1020 if (ST->hasDQI()) 1021 if (const auto *Entry = ConvertCostTableLookup(AVX512DQConversionTbl, ISD, 1022 DstTy.getSimpleVT(), 1023 SrcTy.getSimpleVT())) 1024 return Entry->Cost; 1025 1026 if (ST->hasAVX512()) 1027 if (const auto *Entry = ConvertCostTableLookup(AVX512FConversionTbl, ISD, 1028 DstTy.getSimpleVT(), 1029 SrcTy.getSimpleVT())) 1030 return Entry->Cost; 1031 1032 if (ST->hasAVX2()) { 1033 if (const auto *Entry = ConvertCostTableLookup(AVX2ConversionTbl, ISD, 1034 DstTy.getSimpleVT(), 1035 SrcTy.getSimpleVT())) 1036 return Entry->Cost; 1037 } 1038 1039 if (ST->hasAVX()) { 1040 if (const auto *Entry = ConvertCostTableLookup(AVXConversionTbl, ISD, 1041 DstTy.getSimpleVT(), 1042 SrcTy.getSimpleVT())) 1043 return Entry->Cost; 1044 } 1045 1046 if (ST->hasSSE41()) { 1047 if (const auto *Entry = ConvertCostTableLookup(SSE41ConversionTbl, ISD, 1048 DstTy.getSimpleVT(), 1049 SrcTy.getSimpleVT())) 1050 return Entry->Cost; 1051 } 1052 1053 if (ST->hasSSE2()) { 1054 if (const auto *Entry = ConvertCostTableLookup(SSE2ConversionTbl, ISD, 1055 DstTy.getSimpleVT(), 1056 SrcTy.getSimpleVT())) 1057 return Entry->Cost; 1058 } 1059 1060 return BaseT::getCastInstrCost(Opcode, Dst, Src); 1061 } 1062 1063 int X86TTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy) { 1064 // Legalize the type. 1065 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy); 1066 1067 MVT MTy = LT.second; 1068 1069 int ISD = TLI->InstructionOpcodeToISD(Opcode); 1070 assert(ISD && "Invalid opcode"); 1071 1072 static const CostTblEntry SSE2CostTbl[] = { 1073 { ISD::SETCC, MVT::v2i64, 8 }, 1074 { ISD::SETCC, MVT::v4i32, 1 }, 1075 { ISD::SETCC, MVT::v8i16, 1 }, 1076 { ISD::SETCC, MVT::v16i8, 1 }, 1077 }; 1078 1079 static const CostTblEntry SSE42CostTbl[] = { 1080 { ISD::SETCC, MVT::v2f64, 1 }, 1081 { ISD::SETCC, MVT::v4f32, 1 }, 1082 { ISD::SETCC, MVT::v2i64, 1 }, 1083 }; 1084 1085 static const CostTblEntry AVX1CostTbl[] = { 1086 { ISD::SETCC, MVT::v4f64, 1 }, 1087 { ISD::SETCC, MVT::v8f32, 1 }, 1088 // AVX1 does not support 8-wide integer compare. 1089 { ISD::SETCC, MVT::v4i64, 4 }, 1090 { ISD::SETCC, MVT::v8i32, 4 }, 1091 { ISD::SETCC, MVT::v16i16, 4 }, 1092 { ISD::SETCC, MVT::v32i8, 4 }, 1093 }; 1094 1095 static const CostTblEntry AVX2CostTbl[] = { 1096 { ISD::SETCC, MVT::v4i64, 1 }, 1097 { ISD::SETCC, MVT::v8i32, 1 }, 1098 { ISD::SETCC, MVT::v16i16, 1 }, 1099 { ISD::SETCC, MVT::v32i8, 1 }, 1100 }; 1101 1102 static const CostTblEntry AVX512CostTbl[] = { 1103 { ISD::SETCC, MVT::v8i64, 1 }, 1104 { ISD::SETCC, MVT::v16i32, 1 }, 1105 { ISD::SETCC, MVT::v8f64, 1 }, 1106 { ISD::SETCC, MVT::v16f32, 1 }, 1107 }; 1108 1109 if (ST->hasAVX512()) 1110 if (const auto *Entry = CostTableLookup(AVX512CostTbl, ISD, MTy)) 1111 return LT.first * Entry->Cost; 1112 1113 if (ST->hasAVX2()) 1114 if (const auto *Entry = CostTableLookup(AVX2CostTbl, ISD, MTy)) 1115 return LT.first * Entry->Cost; 1116 1117 if (ST->hasAVX()) 1118 if (const auto *Entry = CostTableLookup(AVX1CostTbl, ISD, MTy)) 1119 return LT.first * Entry->Cost; 1120 1121 if (ST->hasSSE42()) 1122 if (const auto *Entry = CostTableLookup(SSE42CostTbl, ISD, MTy)) 1123 return LT.first * Entry->Cost; 1124 1125 if (ST->hasSSE2()) 1126 if (const auto *Entry = CostTableLookup(SSE2CostTbl, ISD, MTy)) 1127 return LT.first * Entry->Cost; 1128 1129 return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy); 1130 } 1131 1132 int X86TTIImpl::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy, 1133 ArrayRef<Type *> Tys, FastMathFlags FMF) { 1134 // Costs should match the codegen from: 1135 // BITREVERSE: llvm\test\CodeGen\X86\vector-bitreverse.ll 1136 // BSWAP: llvm\test\CodeGen\X86\bswap-vector.ll 1137 // CTLZ: llvm\test\CodeGen\X86\vector-lzcnt-*.ll 1138 // CTPOP: llvm\test\CodeGen\X86\vector-popcnt-*.ll 1139 // CTTZ: llvm\test\CodeGen\X86\vector-tzcnt-*.ll 1140 static const CostTblEntry XOPCostTbl[] = { 1141 { ISD::BITREVERSE, MVT::v4i64, 4 }, 1142 { ISD::BITREVERSE, MVT::v8i32, 4 }, 1143 { ISD::BITREVERSE, MVT::v16i16, 4 }, 1144 { ISD::BITREVERSE, MVT::v32i8, 4 }, 1145 { ISD::BITREVERSE, MVT::v2i64, 1 }, 1146 { ISD::BITREVERSE, MVT::v4i32, 1 }, 1147 { ISD::BITREVERSE, MVT::v8i16, 1 }, 1148 { ISD::BITREVERSE, MVT::v16i8, 1 }, 1149 { ISD::BITREVERSE, MVT::i64, 3 }, 1150 { ISD::BITREVERSE, MVT::i32, 3 }, 1151 { ISD::BITREVERSE, MVT::i16, 3 }, 1152 { ISD::BITREVERSE, MVT::i8, 3 } 1153 }; 1154 static const CostTblEntry AVX2CostTbl[] = { 1155 { ISD::BITREVERSE, MVT::v4i64, 5 }, 1156 { ISD::BITREVERSE, MVT::v8i32, 5 }, 1157 { ISD::BITREVERSE, MVT::v16i16, 5 }, 1158 { ISD::BITREVERSE, MVT::v32i8, 5 }, 1159 { ISD::BSWAP, MVT::v4i64, 1 }, 1160 { ISD::BSWAP, MVT::v8i32, 1 }, 1161 { ISD::BSWAP, MVT::v16i16, 1 }, 1162 { ISD::CTLZ, MVT::v4i64, 23 }, 1163 { ISD::CTLZ, MVT::v8i32, 18 }, 1164 { ISD::CTLZ, MVT::v16i16, 14 }, 1165 { ISD::CTLZ, MVT::v32i8, 9 }, 1166 { ISD::CTPOP, MVT::v4i64, 7 }, 1167 { ISD::CTPOP, MVT::v8i32, 11 }, 1168 { ISD::CTPOP, MVT::v16i16, 9 }, 1169 { ISD::CTPOP, MVT::v32i8, 6 }, 1170 { ISD::CTTZ, MVT::v4i64, 10 }, 1171 { ISD::CTTZ, MVT::v8i32, 14 }, 1172 { ISD::CTTZ, MVT::v16i16, 12 }, 1173 { ISD::CTTZ, MVT::v32i8, 9 }, 1174 { ISD::FSQRT, MVT::f32, 7 }, // Haswell from http://www.agner.org/ 1175 { ISD::FSQRT, MVT::v4f32, 7 }, // Haswell from http://www.agner.org/ 1176 { ISD::FSQRT, MVT::v8f32, 14 }, // Haswell from http://www.agner.org/ 1177 { ISD::FSQRT, MVT::f64, 14 }, // Haswell from http://www.agner.org/ 1178 { ISD::FSQRT, MVT::v2f64, 14 }, // Haswell from http://www.agner.org/ 1179 { ISD::FSQRT, MVT::v4f64, 28 }, // Haswell from http://www.agner.org/ 1180 }; 1181 static const CostTblEntry AVX1CostTbl[] = { 1182 { ISD::BITREVERSE, MVT::v4i64, 10 }, 1183 { ISD::BITREVERSE, MVT::v8i32, 10 }, 1184 { ISD::BITREVERSE, MVT::v16i16, 10 }, 1185 { ISD::BITREVERSE, MVT::v32i8, 10 }, 1186 { ISD::BSWAP, MVT::v4i64, 4 }, 1187 { ISD::BSWAP, MVT::v8i32, 4 }, 1188 { ISD::BSWAP, MVT::v16i16, 4 }, 1189 { ISD::CTLZ, MVT::v4i64, 46 }, 1190 { ISD::CTLZ, MVT::v8i32, 36 }, 1191 { ISD::CTLZ, MVT::v16i16, 28 }, 1192 { ISD::CTLZ, MVT::v32i8, 18 }, 1193 { ISD::CTPOP, MVT::v4i64, 14 }, 1194 { ISD::CTPOP, MVT::v8i32, 22 }, 1195 { ISD::CTPOP, MVT::v16i16, 18 }, 1196 { ISD::CTPOP, MVT::v32i8, 12 }, 1197 { ISD::CTTZ, MVT::v4i64, 20 }, 1198 { ISD::CTTZ, MVT::v8i32, 28 }, 1199 { ISD::CTTZ, MVT::v16i16, 24 }, 1200 { ISD::CTTZ, MVT::v32i8, 18 }, 1201 { ISD::FSQRT, MVT::f32, 14 }, // SNB from http://www.agner.org/ 1202 { ISD::FSQRT, MVT::v4f32, 14 }, // SNB from http://www.agner.org/ 1203 { ISD::FSQRT, MVT::v8f32, 28 }, // SNB from http://www.agner.org/ 1204 { ISD::FSQRT, MVT::f64, 21 }, // SNB from http://www.agner.org/ 1205 { ISD::FSQRT, MVT::v2f64, 21 }, // SNB from http://www.agner.org/ 1206 { ISD::FSQRT, MVT::v4f64, 43 }, // SNB from http://www.agner.org/ 1207 }; 1208 static const CostTblEntry SSE42CostTbl[] = { 1209 { ISD::FSQRT, MVT::f32, 18 }, // Nehalem from http://www.agner.org/ 1210 { ISD::FSQRT, MVT::v4f32, 18 }, // Nehalem from http://www.agner.org/ 1211 }; 1212 static const CostTblEntry SSSE3CostTbl[] = { 1213 { ISD::BITREVERSE, MVT::v2i64, 5 }, 1214 { ISD::BITREVERSE, MVT::v4i32, 5 }, 1215 { ISD::BITREVERSE, MVT::v8i16, 5 }, 1216 { ISD::BITREVERSE, MVT::v16i8, 5 }, 1217 { ISD::BSWAP, MVT::v2i64, 1 }, 1218 { ISD::BSWAP, MVT::v4i32, 1 }, 1219 { ISD::BSWAP, MVT::v8i16, 1 }, 1220 { ISD::CTLZ, MVT::v2i64, 23 }, 1221 { ISD::CTLZ, MVT::v4i32, 18 }, 1222 { ISD::CTLZ, MVT::v8i16, 14 }, 1223 { ISD::CTLZ, MVT::v16i8, 9 }, 1224 { ISD::CTPOP, MVT::v2i64, 7 }, 1225 { ISD::CTPOP, MVT::v4i32, 11 }, 1226 { ISD::CTPOP, MVT::v8i16, 9 }, 1227 { ISD::CTPOP, MVT::v16i8, 6 }, 1228 { ISD::CTTZ, MVT::v2i64, 10 }, 1229 { ISD::CTTZ, MVT::v4i32, 14 }, 1230 { ISD::CTTZ, MVT::v8i16, 12 }, 1231 { ISD::CTTZ, MVT::v16i8, 9 } 1232 }; 1233 static const CostTblEntry SSE2CostTbl[] = { 1234 { ISD::BSWAP, MVT::v2i64, 7 }, 1235 { ISD::BSWAP, MVT::v4i32, 7 }, 1236 { ISD::BSWAP, MVT::v8i16, 7 }, 1237 { ISD::CTLZ, MVT::v2i64, 25 }, 1238 { ISD::CTLZ, MVT::v4i32, 26 }, 1239 { ISD::CTLZ, MVT::v8i16, 20 }, 1240 { ISD::CTLZ, MVT::v16i8, 17 }, 1241 { ISD::CTPOP, MVT::v2i64, 12 }, 1242 { ISD::CTPOP, MVT::v4i32, 15 }, 1243 { ISD::CTPOP, MVT::v8i16, 13 }, 1244 { ISD::CTPOP, MVT::v16i8, 10 }, 1245 { ISD::CTTZ, MVT::v2i64, 14 }, 1246 { ISD::CTTZ, MVT::v4i32, 18 }, 1247 { ISD::CTTZ, MVT::v8i16, 16 }, 1248 { ISD::CTTZ, MVT::v16i8, 13 }, 1249 { ISD::FSQRT, MVT::f64, 32 }, // Nehalem from http://www.agner.org/ 1250 { ISD::FSQRT, MVT::v2f64, 32 }, // Nehalem from http://www.agner.org/ 1251 }; 1252 static const CostTblEntry SSE1CostTbl[] = { 1253 { ISD::FSQRT, MVT::f32, 28 }, // Pentium III from http://www.agner.org/ 1254 { ISD::FSQRT, MVT::v4f32, 56 }, // Pentium III from http://www.agner.org/ 1255 }; 1256 1257 unsigned ISD = ISD::DELETED_NODE; 1258 switch (IID) { 1259 default: 1260 break; 1261 case Intrinsic::bitreverse: 1262 ISD = ISD::BITREVERSE; 1263 break; 1264 case Intrinsic::bswap: 1265 ISD = ISD::BSWAP; 1266 break; 1267 case Intrinsic::ctlz: 1268 ISD = ISD::CTLZ; 1269 break; 1270 case Intrinsic::ctpop: 1271 ISD = ISD::CTPOP; 1272 break; 1273 case Intrinsic::cttz: 1274 ISD = ISD::CTTZ; 1275 break; 1276 case Intrinsic::sqrt: 1277 ISD = ISD::FSQRT; 1278 break; 1279 } 1280 1281 // Legalize the type. 1282 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, RetTy); 1283 MVT MTy = LT.second; 1284 1285 // Attempt to lookup cost. 1286 if (ST->hasXOP()) 1287 if (const auto *Entry = CostTableLookup(XOPCostTbl, ISD, MTy)) 1288 return LT.first * Entry->Cost; 1289 1290 if (ST->hasAVX2()) 1291 if (const auto *Entry = CostTableLookup(AVX2CostTbl, ISD, MTy)) 1292 return LT.first * Entry->Cost; 1293 1294 if (ST->hasAVX()) 1295 if (const auto *Entry = CostTableLookup(AVX1CostTbl, ISD, MTy)) 1296 return LT.first * Entry->Cost; 1297 1298 if (ST->hasSSE42()) 1299 if (const auto *Entry = CostTableLookup(SSE42CostTbl, ISD, MTy)) 1300 return LT.first * Entry->Cost; 1301 1302 if (ST->hasSSSE3()) 1303 if (const auto *Entry = CostTableLookup(SSSE3CostTbl, ISD, MTy)) 1304 return LT.first * Entry->Cost; 1305 1306 if (ST->hasSSE2()) 1307 if (const auto *Entry = CostTableLookup(SSE2CostTbl, ISD, MTy)) 1308 return LT.first * Entry->Cost; 1309 1310 if (ST->hasSSE1()) 1311 if (const auto *Entry = CostTableLookup(SSE1CostTbl, ISD, MTy)) 1312 return LT.first * Entry->Cost; 1313 1314 return BaseT::getIntrinsicInstrCost(IID, RetTy, Tys, FMF); 1315 } 1316 1317 int X86TTIImpl::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy, 1318 ArrayRef<Value *> Args, FastMathFlags FMF) { 1319 return BaseT::getIntrinsicInstrCost(IID, RetTy, Args, FMF); 1320 } 1321 1322 int X86TTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) { 1323 assert(Val->isVectorTy() && "This must be a vector type"); 1324 1325 Type *ScalarType = Val->getScalarType(); 1326 1327 if (Index != -1U) { 1328 // Legalize the type. 1329 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Val); 1330 1331 // This type is legalized to a scalar type. 1332 if (!LT.second.isVector()) 1333 return 0; 1334 1335 // The type may be split. Normalize the index to the new type. 1336 unsigned Width = LT.second.getVectorNumElements(); 1337 Index = Index % Width; 1338 1339 // Floating point scalars are already located in index #0. 1340 if (ScalarType->isFloatingPointTy() && Index == 0) 1341 return 0; 1342 } 1343 1344 // Add to the base cost if we know that the extracted element of a vector is 1345 // destined to be moved to and used in the integer register file. 1346 int RegisterFileMoveCost = 0; 1347 if (Opcode == Instruction::ExtractElement && ScalarType->isPointerTy()) 1348 RegisterFileMoveCost = 1; 1349 1350 return BaseT::getVectorInstrCost(Opcode, Val, Index) + RegisterFileMoveCost; 1351 } 1352 1353 int X86TTIImpl::getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) { 1354 assert (Ty->isVectorTy() && "Can only scalarize vectors"); 1355 int Cost = 0; 1356 1357 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) { 1358 if (Insert) 1359 Cost += getVectorInstrCost(Instruction::InsertElement, Ty, i); 1360 if (Extract) 1361 Cost += getVectorInstrCost(Instruction::ExtractElement, Ty, i); 1362 } 1363 1364 return Cost; 1365 } 1366 1367 int X86TTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment, 1368 unsigned AddressSpace) { 1369 // Handle non-power-of-two vectors such as <3 x float> 1370 if (VectorType *VTy = dyn_cast<VectorType>(Src)) { 1371 unsigned NumElem = VTy->getVectorNumElements(); 1372 1373 // Handle a few common cases: 1374 // <3 x float> 1375 if (NumElem == 3 && VTy->getScalarSizeInBits() == 32) 1376 // Cost = 64 bit store + extract + 32 bit store. 1377 return 3; 1378 1379 // <3 x double> 1380 if (NumElem == 3 && VTy->getScalarSizeInBits() == 64) 1381 // Cost = 128 bit store + unpack + 64 bit store. 1382 return 3; 1383 1384 // Assume that all other non-power-of-two numbers are scalarized. 1385 if (!isPowerOf2_32(NumElem)) { 1386 int Cost = BaseT::getMemoryOpCost(Opcode, VTy->getScalarType(), Alignment, 1387 AddressSpace); 1388 int SplitCost = getScalarizationOverhead(Src, Opcode == Instruction::Load, 1389 Opcode == Instruction::Store); 1390 return NumElem * Cost + SplitCost; 1391 } 1392 } 1393 1394 // Legalize the type. 1395 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src); 1396 assert((Opcode == Instruction::Load || Opcode == Instruction::Store) && 1397 "Invalid Opcode"); 1398 1399 // Each load/store unit costs 1. 1400 int Cost = LT.first * 1; 1401 1402 // This isn't exactly right. We're using slow unaligned 32-byte accesses as a 1403 // proxy for a double-pumped AVX memory interface such as on Sandybridge. 1404 if (LT.second.getStoreSize() == 32 && ST->isUnalignedMem32Slow()) 1405 Cost *= 2; 1406 1407 return Cost; 1408 } 1409 1410 int X86TTIImpl::getMaskedMemoryOpCost(unsigned Opcode, Type *SrcTy, 1411 unsigned Alignment, 1412 unsigned AddressSpace) { 1413 VectorType *SrcVTy = dyn_cast<VectorType>(SrcTy); 1414 if (!SrcVTy) 1415 // To calculate scalar take the regular cost, without mask 1416 return getMemoryOpCost(Opcode, SrcTy, Alignment, AddressSpace); 1417 1418 unsigned NumElem = SrcVTy->getVectorNumElements(); 1419 VectorType *MaskTy = 1420 VectorType::get(Type::getInt8Ty(SrcVTy->getContext()), NumElem); 1421 if ((Opcode == Instruction::Load && !isLegalMaskedLoad(SrcVTy)) || 1422 (Opcode == Instruction::Store && !isLegalMaskedStore(SrcVTy)) || 1423 !isPowerOf2_32(NumElem)) { 1424 // Scalarization 1425 int MaskSplitCost = getScalarizationOverhead(MaskTy, false, true); 1426 int ScalarCompareCost = getCmpSelInstrCost( 1427 Instruction::ICmp, Type::getInt8Ty(SrcVTy->getContext()), nullptr); 1428 int BranchCost = getCFInstrCost(Instruction::Br); 1429 int MaskCmpCost = NumElem * (BranchCost + ScalarCompareCost); 1430 1431 int ValueSplitCost = getScalarizationOverhead( 1432 SrcVTy, Opcode == Instruction::Load, Opcode == Instruction::Store); 1433 int MemopCost = 1434 NumElem * BaseT::getMemoryOpCost(Opcode, SrcVTy->getScalarType(), 1435 Alignment, AddressSpace); 1436 return MemopCost + ValueSplitCost + MaskSplitCost + MaskCmpCost; 1437 } 1438 1439 // Legalize the type. 1440 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, SrcVTy); 1441 auto VT = TLI->getValueType(DL, SrcVTy); 1442 int Cost = 0; 1443 if (VT.isSimple() && LT.second != VT.getSimpleVT() && 1444 LT.second.getVectorNumElements() == NumElem) 1445 // Promotion requires expand/truncate for data and a shuffle for mask. 1446 Cost += getShuffleCost(TTI::SK_Alternate, SrcVTy, 0, nullptr) + 1447 getShuffleCost(TTI::SK_Alternate, MaskTy, 0, nullptr); 1448 1449 else if (LT.second.getVectorNumElements() > NumElem) { 1450 VectorType *NewMaskTy = VectorType::get(MaskTy->getVectorElementType(), 1451 LT.second.getVectorNumElements()); 1452 // Expanding requires fill mask with zeroes 1453 Cost += getShuffleCost(TTI::SK_InsertSubvector, NewMaskTy, 0, MaskTy); 1454 } 1455 if (!ST->hasAVX512()) 1456 return Cost + LT.first*4; // Each maskmov costs 4 1457 1458 // AVX-512 masked load/store is cheapper 1459 return Cost+LT.first; 1460 } 1461 1462 int X86TTIImpl::getAddressComputationCost(Type *Ty, bool IsComplex) { 1463 // Address computations in vectorized code with non-consecutive addresses will 1464 // likely result in more instructions compared to scalar code where the 1465 // computation can more often be merged into the index mode. The resulting 1466 // extra micro-ops can significantly decrease throughput. 1467 unsigned NumVectorInstToHideOverhead = 10; 1468 1469 if (Ty->isVectorTy() && IsComplex) 1470 return NumVectorInstToHideOverhead; 1471 1472 return BaseT::getAddressComputationCost(Ty, IsComplex); 1473 } 1474 1475 int X86TTIImpl::getReductionCost(unsigned Opcode, Type *ValTy, 1476 bool IsPairwise) { 1477 1478 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy); 1479 1480 MVT MTy = LT.second; 1481 1482 int ISD = TLI->InstructionOpcodeToISD(Opcode); 1483 assert(ISD && "Invalid opcode"); 1484 1485 // We use the Intel Architecture Code Analyzer(IACA) to measure the throughput 1486 // and make it as the cost. 1487 1488 static const CostTblEntry SSE42CostTblPairWise[] = { 1489 { ISD::FADD, MVT::v2f64, 2 }, 1490 { ISD::FADD, MVT::v4f32, 4 }, 1491 { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6". 1492 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5". 1493 { ISD::ADD, MVT::v8i16, 5 }, 1494 }; 1495 1496 static const CostTblEntry AVX1CostTblPairWise[] = { 1497 { ISD::FADD, MVT::v4f32, 4 }, 1498 { ISD::FADD, MVT::v4f64, 5 }, 1499 { ISD::FADD, MVT::v8f32, 7 }, 1500 { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5". 1501 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5". 1502 { ISD::ADD, MVT::v4i64, 5 }, // The data reported by the IACA tool is "4.8". 1503 { ISD::ADD, MVT::v8i16, 5 }, 1504 { ISD::ADD, MVT::v8i32, 5 }, 1505 }; 1506 1507 static const CostTblEntry SSE42CostTblNoPairWise[] = { 1508 { ISD::FADD, MVT::v2f64, 2 }, 1509 { ISD::FADD, MVT::v4f32, 4 }, 1510 { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6". 1511 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.3". 1512 { ISD::ADD, MVT::v8i16, 4 }, // The data reported by the IACA tool is "4.3". 1513 }; 1514 1515 static const CostTblEntry AVX1CostTblNoPairWise[] = { 1516 { ISD::FADD, MVT::v4f32, 3 }, 1517 { ISD::FADD, MVT::v4f64, 3 }, 1518 { ISD::FADD, MVT::v8f32, 4 }, 1519 { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5". 1520 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "2.8". 1521 { ISD::ADD, MVT::v4i64, 3 }, 1522 { ISD::ADD, MVT::v8i16, 4 }, 1523 { ISD::ADD, MVT::v8i32, 5 }, 1524 }; 1525 1526 if (IsPairwise) { 1527 if (ST->hasAVX()) 1528 if (const auto *Entry = CostTableLookup(AVX1CostTblPairWise, ISD, MTy)) 1529 return LT.first * Entry->Cost; 1530 1531 if (ST->hasSSE42()) 1532 if (const auto *Entry = CostTableLookup(SSE42CostTblPairWise, ISD, MTy)) 1533 return LT.first * Entry->Cost; 1534 } else { 1535 if (ST->hasAVX()) 1536 if (const auto *Entry = CostTableLookup(AVX1CostTblNoPairWise, ISD, MTy)) 1537 return LT.first * Entry->Cost; 1538 1539 if (ST->hasSSE42()) 1540 if (const auto *Entry = CostTableLookup(SSE42CostTblNoPairWise, ISD, MTy)) 1541 return LT.first * Entry->Cost; 1542 } 1543 1544 return BaseT::getReductionCost(Opcode, ValTy, IsPairwise); 1545 } 1546 1547 /// \brief Calculate the cost of materializing a 64-bit value. This helper 1548 /// method might only calculate a fraction of a larger immediate. Therefore it 1549 /// is valid to return a cost of ZERO. 1550 int X86TTIImpl::getIntImmCost(int64_t Val) { 1551 if (Val == 0) 1552 return TTI::TCC_Free; 1553 1554 if (isInt<32>(Val)) 1555 return TTI::TCC_Basic; 1556 1557 return 2 * TTI::TCC_Basic; 1558 } 1559 1560 int X86TTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) { 1561 assert(Ty->isIntegerTy()); 1562 1563 unsigned BitSize = Ty->getPrimitiveSizeInBits(); 1564 if (BitSize == 0) 1565 return ~0U; 1566 1567 // Never hoist constants larger than 128bit, because this might lead to 1568 // incorrect code generation or assertions in codegen. 1569 // Fixme: Create a cost model for types larger than i128 once the codegen 1570 // issues have been fixed. 1571 if (BitSize > 128) 1572 return TTI::TCC_Free; 1573 1574 if (Imm == 0) 1575 return TTI::TCC_Free; 1576 1577 // Sign-extend all constants to a multiple of 64-bit. 1578 APInt ImmVal = Imm; 1579 if (BitSize & 0x3f) 1580 ImmVal = Imm.sext((BitSize + 63) & ~0x3fU); 1581 1582 // Split the constant into 64-bit chunks and calculate the cost for each 1583 // chunk. 1584 int Cost = 0; 1585 for (unsigned ShiftVal = 0; ShiftVal < BitSize; ShiftVal += 64) { 1586 APInt Tmp = ImmVal.ashr(ShiftVal).sextOrTrunc(64); 1587 int64_t Val = Tmp.getSExtValue(); 1588 Cost += getIntImmCost(Val); 1589 } 1590 // We need at least one instruction to materialize the constant. 1591 return std::max(1, Cost); 1592 } 1593 1594 int X86TTIImpl::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm, 1595 Type *Ty) { 1596 assert(Ty->isIntegerTy()); 1597 1598 unsigned BitSize = Ty->getPrimitiveSizeInBits(); 1599 // There is no cost model for constants with a bit size of 0. Return TCC_Free 1600 // here, so that constant hoisting will ignore this constant. 1601 if (BitSize == 0) 1602 return TTI::TCC_Free; 1603 1604 unsigned ImmIdx = ~0U; 1605 switch (Opcode) { 1606 default: 1607 return TTI::TCC_Free; 1608 case Instruction::GetElementPtr: 1609 // Always hoist the base address of a GetElementPtr. This prevents the 1610 // creation of new constants for every base constant that gets constant 1611 // folded with the offset. 1612 if (Idx == 0) 1613 return 2 * TTI::TCC_Basic; 1614 return TTI::TCC_Free; 1615 case Instruction::Store: 1616 ImmIdx = 0; 1617 break; 1618 case Instruction::ICmp: 1619 // This is an imperfect hack to prevent constant hoisting of 1620 // compares that might be trying to check if a 64-bit value fits in 1621 // 32-bits. The backend can optimize these cases using a right shift by 32. 1622 // Ideally we would check the compare predicate here. There also other 1623 // similar immediates the backend can use shifts for. 1624 if (Idx == 1 && Imm.getBitWidth() == 64) { 1625 uint64_t ImmVal = Imm.getZExtValue(); 1626 if (ImmVal == 0x100000000ULL || ImmVal == 0xffffffff) 1627 return TTI::TCC_Free; 1628 } 1629 ImmIdx = 1; 1630 break; 1631 case Instruction::And: 1632 // We support 64-bit ANDs with immediates with 32-bits of leading zeroes 1633 // by using a 32-bit operation with implicit zero extension. Detect such 1634 // immediates here as the normal path expects bit 31 to be sign extended. 1635 if (Idx == 1 && Imm.getBitWidth() == 64 && isUInt<32>(Imm.getZExtValue())) 1636 return TTI::TCC_Free; 1637 LLVM_FALLTHROUGH; 1638 case Instruction::Add: 1639 case Instruction::Sub: 1640 case Instruction::Mul: 1641 case Instruction::UDiv: 1642 case Instruction::SDiv: 1643 case Instruction::URem: 1644 case Instruction::SRem: 1645 case Instruction::Or: 1646 case Instruction::Xor: 1647 ImmIdx = 1; 1648 break; 1649 // Always return TCC_Free for the shift value of a shift instruction. 1650 case Instruction::Shl: 1651 case Instruction::LShr: 1652 case Instruction::AShr: 1653 if (Idx == 1) 1654 return TTI::TCC_Free; 1655 break; 1656 case Instruction::Trunc: 1657 case Instruction::ZExt: 1658 case Instruction::SExt: 1659 case Instruction::IntToPtr: 1660 case Instruction::PtrToInt: 1661 case Instruction::BitCast: 1662 case Instruction::PHI: 1663 case Instruction::Call: 1664 case Instruction::Select: 1665 case Instruction::Ret: 1666 case Instruction::Load: 1667 break; 1668 } 1669 1670 if (Idx == ImmIdx) { 1671 int NumConstants = (BitSize + 63) / 64; 1672 int Cost = X86TTIImpl::getIntImmCost(Imm, Ty); 1673 return (Cost <= NumConstants * TTI::TCC_Basic) 1674 ? static_cast<int>(TTI::TCC_Free) 1675 : Cost; 1676 } 1677 1678 return X86TTIImpl::getIntImmCost(Imm, Ty); 1679 } 1680 1681 int X86TTIImpl::getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm, 1682 Type *Ty) { 1683 assert(Ty->isIntegerTy()); 1684 1685 unsigned BitSize = Ty->getPrimitiveSizeInBits(); 1686 // There is no cost model for constants with a bit size of 0. Return TCC_Free 1687 // here, so that constant hoisting will ignore this constant. 1688 if (BitSize == 0) 1689 return TTI::TCC_Free; 1690 1691 switch (IID) { 1692 default: 1693 return TTI::TCC_Free; 1694 case Intrinsic::sadd_with_overflow: 1695 case Intrinsic::uadd_with_overflow: 1696 case Intrinsic::ssub_with_overflow: 1697 case Intrinsic::usub_with_overflow: 1698 case Intrinsic::smul_with_overflow: 1699 case Intrinsic::umul_with_overflow: 1700 if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue())) 1701 return TTI::TCC_Free; 1702 break; 1703 case Intrinsic::experimental_stackmap: 1704 if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue()))) 1705 return TTI::TCC_Free; 1706 break; 1707 case Intrinsic::experimental_patchpoint_void: 1708 case Intrinsic::experimental_patchpoint_i64: 1709 if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue()))) 1710 return TTI::TCC_Free; 1711 break; 1712 } 1713 return X86TTIImpl::getIntImmCost(Imm, Ty); 1714 } 1715 1716 // Return an average cost of Gather / Scatter instruction, maybe improved later 1717 int X86TTIImpl::getGSVectorCost(unsigned Opcode, Type *SrcVTy, Value *Ptr, 1718 unsigned Alignment, unsigned AddressSpace) { 1719 1720 assert(isa<VectorType>(SrcVTy) && "Unexpected type in getGSVectorCost"); 1721 unsigned VF = SrcVTy->getVectorNumElements(); 1722 1723 // Try to reduce index size from 64 bit (default for GEP) 1724 // to 32. It is essential for VF 16. If the index can't be reduced to 32, the 1725 // operation will use 16 x 64 indices which do not fit in a zmm and needs 1726 // to split. Also check that the base pointer is the same for all lanes, 1727 // and that there's at most one variable index. 1728 auto getIndexSizeInBits = [](Value *Ptr, const DataLayout& DL) { 1729 unsigned IndexSize = DL.getPointerSizeInBits(); 1730 GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr); 1731 if (IndexSize < 64 || !GEP) 1732 return IndexSize; 1733 1734 unsigned NumOfVarIndices = 0; 1735 Value *Ptrs = GEP->getPointerOperand(); 1736 if (Ptrs->getType()->isVectorTy() && !getSplatValue(Ptrs)) 1737 return IndexSize; 1738 for (unsigned i = 1; i < GEP->getNumOperands(); ++i) { 1739 if (isa<Constant>(GEP->getOperand(i))) 1740 continue; 1741 Type *IndxTy = GEP->getOperand(i)->getType(); 1742 if (IndxTy->isVectorTy()) 1743 IndxTy = IndxTy->getVectorElementType(); 1744 if ((IndxTy->getPrimitiveSizeInBits() == 64 && 1745 !isa<SExtInst>(GEP->getOperand(i))) || 1746 ++NumOfVarIndices > 1) 1747 return IndexSize; // 64 1748 } 1749 return (unsigned)32; 1750 }; 1751 1752 1753 // Trying to reduce IndexSize to 32 bits for vector 16. 1754 // By default the IndexSize is equal to pointer size. 1755 unsigned IndexSize = (VF >= 16) ? getIndexSizeInBits(Ptr, DL) : 1756 DL.getPointerSizeInBits(); 1757 1758 Type *IndexVTy = VectorType::get(IntegerType::get(SrcVTy->getContext(), 1759 IndexSize), VF); 1760 std::pair<int, MVT> IdxsLT = TLI->getTypeLegalizationCost(DL, IndexVTy); 1761 std::pair<int, MVT> SrcLT = TLI->getTypeLegalizationCost(DL, SrcVTy); 1762 int SplitFactor = std::max(IdxsLT.first, SrcLT.first); 1763 if (SplitFactor > 1) { 1764 // Handle splitting of vector of pointers 1765 Type *SplitSrcTy = VectorType::get(SrcVTy->getScalarType(), VF / SplitFactor); 1766 return SplitFactor * getGSVectorCost(Opcode, SplitSrcTy, Ptr, Alignment, 1767 AddressSpace); 1768 } 1769 1770 // The gather / scatter cost is given by Intel architects. It is a rough 1771 // number since we are looking at one instruction in a time. 1772 const int GSOverhead = 2; 1773 return GSOverhead + VF * getMemoryOpCost(Opcode, SrcVTy->getScalarType(), 1774 Alignment, AddressSpace); 1775 } 1776 1777 /// Return the cost of full scalarization of gather / scatter operation. 1778 /// 1779 /// Opcode - Load or Store instruction. 1780 /// SrcVTy - The type of the data vector that should be gathered or scattered. 1781 /// VariableMask - The mask is non-constant at compile time. 1782 /// Alignment - Alignment for one element. 1783 /// AddressSpace - pointer[s] address space. 1784 /// 1785 int X86TTIImpl::getGSScalarCost(unsigned Opcode, Type *SrcVTy, 1786 bool VariableMask, unsigned Alignment, 1787 unsigned AddressSpace) { 1788 unsigned VF = SrcVTy->getVectorNumElements(); 1789 1790 int MaskUnpackCost = 0; 1791 if (VariableMask) { 1792 VectorType *MaskTy = 1793 VectorType::get(Type::getInt1Ty(SrcVTy->getContext()), VF); 1794 MaskUnpackCost = getScalarizationOverhead(MaskTy, false, true); 1795 int ScalarCompareCost = 1796 getCmpSelInstrCost(Instruction::ICmp, Type::getInt1Ty(SrcVTy->getContext()), 1797 nullptr); 1798 int BranchCost = getCFInstrCost(Instruction::Br); 1799 MaskUnpackCost += VF * (BranchCost + ScalarCompareCost); 1800 } 1801 1802 // The cost of the scalar loads/stores. 1803 int MemoryOpCost = VF * getMemoryOpCost(Opcode, SrcVTy->getScalarType(), 1804 Alignment, AddressSpace); 1805 1806 int InsertExtractCost = 0; 1807 if (Opcode == Instruction::Load) 1808 for (unsigned i = 0; i < VF; ++i) 1809 // Add the cost of inserting each scalar load into the vector 1810 InsertExtractCost += 1811 getVectorInstrCost(Instruction::InsertElement, SrcVTy, i); 1812 else 1813 for (unsigned i = 0; i < VF; ++i) 1814 // Add the cost of extracting each element out of the data vector 1815 InsertExtractCost += 1816 getVectorInstrCost(Instruction::ExtractElement, SrcVTy, i); 1817 1818 return MemoryOpCost + MaskUnpackCost + InsertExtractCost; 1819 } 1820 1821 /// Calculate the cost of Gather / Scatter operation 1822 int X86TTIImpl::getGatherScatterOpCost(unsigned Opcode, Type *SrcVTy, 1823 Value *Ptr, bool VariableMask, 1824 unsigned Alignment) { 1825 assert(SrcVTy->isVectorTy() && "Unexpected data type for Gather/Scatter"); 1826 unsigned VF = SrcVTy->getVectorNumElements(); 1827 PointerType *PtrTy = dyn_cast<PointerType>(Ptr->getType()); 1828 if (!PtrTy && Ptr->getType()->isVectorTy()) 1829 PtrTy = dyn_cast<PointerType>(Ptr->getType()->getVectorElementType()); 1830 assert(PtrTy && "Unexpected type for Ptr argument"); 1831 unsigned AddressSpace = PtrTy->getAddressSpace(); 1832 1833 bool Scalarize = false; 1834 if ((Opcode == Instruction::Load && !isLegalMaskedGather(SrcVTy)) || 1835 (Opcode == Instruction::Store && !isLegalMaskedScatter(SrcVTy))) 1836 Scalarize = true; 1837 // Gather / Scatter for vector 2 is not profitable on KNL / SKX 1838 // Vector-4 of gather/scatter instruction does not exist on KNL. 1839 // We can extend it to 8 elements, but zeroing upper bits of 1840 // the mask vector will add more instructions. Right now we give the scalar 1841 // cost of vector-4 for KNL. TODO: Check, maybe the gather/scatter instruction is 1842 // better in the VariableMask case. 1843 if (VF == 2 || (VF == 4 && !ST->hasVLX())) 1844 Scalarize = true; 1845 1846 if (Scalarize) 1847 return getGSScalarCost(Opcode, SrcVTy, VariableMask, Alignment, AddressSpace); 1848 1849 return getGSVectorCost(Opcode, SrcVTy, Ptr, Alignment, AddressSpace); 1850 } 1851 1852 bool X86TTIImpl::isLegalMaskedLoad(Type *DataTy) { 1853 Type *ScalarTy = DataTy->getScalarType(); 1854 int DataWidth = isa<PointerType>(ScalarTy) ? 1855 DL.getPointerSizeInBits() : ScalarTy->getPrimitiveSizeInBits(); 1856 1857 return ((DataWidth == 32 || DataWidth == 64) && ST->hasAVX()) || 1858 ((DataWidth == 8 || DataWidth == 16) && ST->hasBWI()); 1859 } 1860 1861 bool X86TTIImpl::isLegalMaskedStore(Type *DataType) { 1862 return isLegalMaskedLoad(DataType); 1863 } 1864 1865 bool X86TTIImpl::isLegalMaskedGather(Type *DataTy) { 1866 // This function is called now in two cases: from the Loop Vectorizer 1867 // and from the Scalarizer. 1868 // When the Loop Vectorizer asks about legality of the feature, 1869 // the vectorization factor is not calculated yet. The Loop Vectorizer 1870 // sends a scalar type and the decision is based on the width of the 1871 // scalar element. 1872 // Later on, the cost model will estimate usage this intrinsic based on 1873 // the vector type. 1874 // The Scalarizer asks again about legality. It sends a vector type. 1875 // In this case we can reject non-power-of-2 vectors. 1876 if (isa<VectorType>(DataTy) && !isPowerOf2_32(DataTy->getVectorNumElements())) 1877 return false; 1878 Type *ScalarTy = DataTy->getScalarType(); 1879 int DataWidth = isa<PointerType>(ScalarTy) ? 1880 DL.getPointerSizeInBits() : ScalarTy->getPrimitiveSizeInBits(); 1881 1882 // AVX-512 allows gather and scatter 1883 return (DataWidth == 32 || DataWidth == 64) && ST->hasAVX512(); 1884 } 1885 1886 bool X86TTIImpl::isLegalMaskedScatter(Type *DataType) { 1887 return isLegalMaskedGather(DataType); 1888 } 1889 1890 bool X86TTIImpl::areInlineCompatible(const Function *Caller, 1891 const Function *Callee) const { 1892 const TargetMachine &TM = getTLI()->getTargetMachine(); 1893 1894 // Work this as a subsetting of subtarget features. 1895 const FeatureBitset &CallerBits = 1896 TM.getSubtargetImpl(*Caller)->getFeatureBits(); 1897 const FeatureBitset &CalleeBits = 1898 TM.getSubtargetImpl(*Callee)->getFeatureBits(); 1899 1900 // FIXME: This is likely too limiting as it will include subtarget features 1901 // that we might not care about for inlining, but it is conservatively 1902 // correct. 1903 return (CallerBits & CalleeBits) == CalleeBits; 1904 } 1905 1906 bool X86TTIImpl::enableInterleavedAccessVectorization() { 1907 // TODO: We expect this to be beneficial regardless of arch, 1908 // but there are currently some unexplained performance artifacts on Atom. 1909 // As a temporary solution, disable on Atom. 1910 return !(ST->isAtom() || ST->isSLM()); 1911 } 1912