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