1 //===- llvm/Analysis/TargetTransformInfo.cpp ------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/Analysis/TargetTransformInfo.h" 10 #include "llvm/Analysis/CFG.h" 11 #include "llvm/Analysis/LoopIterator.h" 12 #include "llvm/Analysis/TargetTransformInfoImpl.h" 13 #include "llvm/IR/CFG.h" 14 #include "llvm/IR/DataLayout.h" 15 #include "llvm/IR/Dominators.h" 16 #include "llvm/IR/Instruction.h" 17 #include "llvm/IR/Instructions.h" 18 #include "llvm/IR/IntrinsicInst.h" 19 #include "llvm/IR/Module.h" 20 #include "llvm/IR/Operator.h" 21 #include "llvm/IR/PatternMatch.h" 22 #include "llvm/InitializePasses.h" 23 #include "llvm/Support/CommandLine.h" 24 #include "llvm/Support/ErrorHandling.h" 25 #include <utility> 26 27 using namespace llvm; 28 using namespace PatternMatch; 29 30 #define DEBUG_TYPE "tti" 31 32 static cl::opt<bool> EnableReduxCost("costmodel-reduxcost", cl::init(false), 33 cl::Hidden, 34 cl::desc("Recognize reduction patterns.")); 35 36 namespace { 37 /// No-op implementation of the TTI interface using the utility base 38 /// classes. 39 /// 40 /// This is used when no target specific information is available. 41 struct NoTTIImpl : TargetTransformInfoImplCRTPBase<NoTTIImpl> { 42 explicit NoTTIImpl(const DataLayout &DL) 43 : TargetTransformInfoImplCRTPBase<NoTTIImpl>(DL) {} 44 }; 45 } // namespace 46 47 bool HardwareLoopInfo::canAnalyze(LoopInfo &LI) { 48 // If the loop has irreducible control flow, it can not be converted to 49 // Hardware loop. 50 LoopBlocksRPO RPOT(L); 51 RPOT.perform(&LI); 52 if (containsIrreducibleCFG<const BasicBlock *>(RPOT, LI)) 53 return false; 54 return true; 55 } 56 57 IntrinsicCostAttributes::IntrinsicCostAttributes( 58 Intrinsic::ID Id, const CallBase &CI, InstructionCost ScalarizationCost) 59 : II(dyn_cast<IntrinsicInst>(&CI)), RetTy(CI.getType()), IID(Id), 60 ScalarizationCost(ScalarizationCost) { 61 62 if (const auto *FPMO = dyn_cast<FPMathOperator>(&CI)) 63 FMF = FPMO->getFastMathFlags(); 64 65 Arguments.insert(Arguments.begin(), CI.arg_begin(), CI.arg_end()); 66 FunctionType *FTy = CI.getCalledFunction()->getFunctionType(); 67 ParamTys.insert(ParamTys.begin(), FTy->param_begin(), FTy->param_end()); 68 } 69 70 IntrinsicCostAttributes::IntrinsicCostAttributes(Intrinsic::ID Id, Type *RTy, 71 ArrayRef<Type *> Tys, 72 FastMathFlags Flags, 73 const IntrinsicInst *I, 74 InstructionCost ScalarCost) 75 : II(I), RetTy(RTy), IID(Id), FMF(Flags), ScalarizationCost(ScalarCost) { 76 ParamTys.insert(ParamTys.begin(), Tys.begin(), Tys.end()); 77 } 78 79 IntrinsicCostAttributes::IntrinsicCostAttributes(Intrinsic::ID Id, Type *Ty, 80 ArrayRef<const Value *> Args) 81 : RetTy(Ty), IID(Id) { 82 83 Arguments.insert(Arguments.begin(), Args.begin(), Args.end()); 84 ParamTys.reserve(Arguments.size()); 85 for (unsigned Idx = 0, Size = Arguments.size(); Idx != Size; ++Idx) 86 ParamTys.push_back(Arguments[Idx]->getType()); 87 } 88 89 IntrinsicCostAttributes::IntrinsicCostAttributes(Intrinsic::ID Id, Type *RTy, 90 ArrayRef<const Value *> Args, 91 ArrayRef<Type *> Tys, 92 FastMathFlags Flags, 93 const IntrinsicInst *I, 94 InstructionCost ScalarCost) 95 : II(I), RetTy(RTy), IID(Id), FMF(Flags), ScalarizationCost(ScalarCost) { 96 ParamTys.insert(ParamTys.begin(), Tys.begin(), Tys.end()); 97 Arguments.insert(Arguments.begin(), Args.begin(), Args.end()); 98 } 99 100 bool HardwareLoopInfo::isHardwareLoopCandidate(ScalarEvolution &SE, 101 LoopInfo &LI, DominatorTree &DT, 102 bool ForceNestedLoop, 103 bool ForceHardwareLoopPHI) { 104 SmallVector<BasicBlock *, 4> ExitingBlocks; 105 L->getExitingBlocks(ExitingBlocks); 106 107 for (BasicBlock *BB : ExitingBlocks) { 108 // If we pass the updated counter back through a phi, we need to know 109 // which latch the updated value will be coming from. 110 if (!L->isLoopLatch(BB)) { 111 if (ForceHardwareLoopPHI || CounterInReg) 112 continue; 113 } 114 115 const SCEV *EC = SE.getExitCount(L, BB); 116 if (isa<SCEVCouldNotCompute>(EC)) 117 continue; 118 if (const SCEVConstant *ConstEC = dyn_cast<SCEVConstant>(EC)) { 119 if (ConstEC->getValue()->isZero()) 120 continue; 121 } else if (!SE.isLoopInvariant(EC, L)) 122 continue; 123 124 if (SE.getTypeSizeInBits(EC->getType()) > CountType->getBitWidth()) 125 continue; 126 127 // If this exiting block is contained in a nested loop, it is not eligible 128 // for insertion of the branch-and-decrement since the inner loop would 129 // end up messing up the value in the CTR. 130 if (!IsNestingLegal && LI.getLoopFor(BB) != L && !ForceNestedLoop) 131 continue; 132 133 // We now have a loop-invariant count of loop iterations (which is not the 134 // constant zero) for which we know that this loop will not exit via this 135 // existing block. 136 137 // We need to make sure that this block will run on every loop iteration. 138 // For this to be true, we must dominate all blocks with backedges. Such 139 // blocks are in-loop predecessors to the header block. 140 bool NotAlways = false; 141 for (BasicBlock *Pred : predecessors(L->getHeader())) { 142 if (!L->contains(Pred)) 143 continue; 144 145 if (!DT.dominates(BB, Pred)) { 146 NotAlways = true; 147 break; 148 } 149 } 150 151 if (NotAlways) 152 continue; 153 154 // Make sure this blocks ends with a conditional branch. 155 Instruction *TI = BB->getTerminator(); 156 if (!TI) 157 continue; 158 159 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { 160 if (!BI->isConditional()) 161 continue; 162 163 ExitBranch = BI; 164 } else 165 continue; 166 167 // Note that this block may not be the loop latch block, even if the loop 168 // has a latch block. 169 ExitBlock = BB; 170 TripCount = SE.getAddExpr(EC, SE.getOne(EC->getType())); 171 172 if (!EC->getType()->isPointerTy() && EC->getType() != CountType) 173 TripCount = SE.getZeroExtendExpr(TripCount, CountType); 174 175 break; 176 } 177 178 if (!ExitBlock) 179 return false; 180 return true; 181 } 182 183 TargetTransformInfo::TargetTransformInfo(const DataLayout &DL) 184 : TTIImpl(new Model<NoTTIImpl>(NoTTIImpl(DL))) {} 185 186 TargetTransformInfo::~TargetTransformInfo() {} 187 188 TargetTransformInfo::TargetTransformInfo(TargetTransformInfo &&Arg) 189 : TTIImpl(std::move(Arg.TTIImpl)) {} 190 191 TargetTransformInfo &TargetTransformInfo::operator=(TargetTransformInfo &&RHS) { 192 TTIImpl = std::move(RHS.TTIImpl); 193 return *this; 194 } 195 196 unsigned TargetTransformInfo::getInliningThresholdMultiplier() const { 197 return TTIImpl->getInliningThresholdMultiplier(); 198 } 199 200 unsigned 201 TargetTransformInfo::adjustInliningThreshold(const CallBase *CB) const { 202 return TTIImpl->adjustInliningThreshold(CB); 203 } 204 205 int TargetTransformInfo::getInlinerVectorBonusPercent() const { 206 return TTIImpl->getInlinerVectorBonusPercent(); 207 } 208 209 InstructionCost 210 TargetTransformInfo::getGEPCost(Type *PointeeType, const Value *Ptr, 211 ArrayRef<const Value *> Operands, 212 TTI::TargetCostKind CostKind) const { 213 return TTIImpl->getGEPCost(PointeeType, Ptr, Operands, CostKind); 214 } 215 216 unsigned TargetTransformInfo::getEstimatedNumberOfCaseClusters( 217 const SwitchInst &SI, unsigned &JTSize, ProfileSummaryInfo *PSI, 218 BlockFrequencyInfo *BFI) const { 219 return TTIImpl->getEstimatedNumberOfCaseClusters(SI, JTSize, PSI, BFI); 220 } 221 222 InstructionCost 223 TargetTransformInfo::getUserCost(const User *U, 224 ArrayRef<const Value *> Operands, 225 enum TargetCostKind CostKind) const { 226 InstructionCost Cost = TTIImpl->getUserCost(U, Operands, CostKind); 227 assert((CostKind == TTI::TCK_RecipThroughput || Cost >= 0) && 228 "TTI should not produce negative costs!"); 229 return Cost; 230 } 231 232 BranchProbability TargetTransformInfo::getPredictableBranchThreshold() const { 233 return TTIImpl->getPredictableBranchThreshold(); 234 } 235 236 bool TargetTransformInfo::hasBranchDivergence() const { 237 return TTIImpl->hasBranchDivergence(); 238 } 239 240 bool TargetTransformInfo::useGPUDivergenceAnalysis() const { 241 return TTIImpl->useGPUDivergenceAnalysis(); 242 } 243 244 bool TargetTransformInfo::isSourceOfDivergence(const Value *V) const { 245 return TTIImpl->isSourceOfDivergence(V); 246 } 247 248 bool llvm::TargetTransformInfo::isAlwaysUniform(const Value *V) const { 249 return TTIImpl->isAlwaysUniform(V); 250 } 251 252 unsigned TargetTransformInfo::getFlatAddressSpace() const { 253 return TTIImpl->getFlatAddressSpace(); 254 } 255 256 bool TargetTransformInfo::collectFlatAddressOperands( 257 SmallVectorImpl<int> &OpIndexes, Intrinsic::ID IID) const { 258 return TTIImpl->collectFlatAddressOperands(OpIndexes, IID); 259 } 260 261 bool TargetTransformInfo::isNoopAddrSpaceCast(unsigned FromAS, 262 unsigned ToAS) const { 263 return TTIImpl->isNoopAddrSpaceCast(FromAS, ToAS); 264 } 265 266 unsigned TargetTransformInfo::getAssumedAddrSpace(const Value *V) const { 267 return TTIImpl->getAssumedAddrSpace(V); 268 } 269 270 Value *TargetTransformInfo::rewriteIntrinsicWithAddressSpace( 271 IntrinsicInst *II, Value *OldV, Value *NewV) const { 272 return TTIImpl->rewriteIntrinsicWithAddressSpace(II, OldV, NewV); 273 } 274 275 bool TargetTransformInfo::isLoweredToCall(const Function *F) const { 276 return TTIImpl->isLoweredToCall(F); 277 } 278 279 bool TargetTransformInfo::isHardwareLoopProfitable( 280 Loop *L, ScalarEvolution &SE, AssumptionCache &AC, 281 TargetLibraryInfo *LibInfo, HardwareLoopInfo &HWLoopInfo) const { 282 return TTIImpl->isHardwareLoopProfitable(L, SE, AC, LibInfo, HWLoopInfo); 283 } 284 285 bool TargetTransformInfo::preferPredicateOverEpilogue( 286 Loop *L, LoopInfo *LI, ScalarEvolution &SE, AssumptionCache &AC, 287 TargetLibraryInfo *TLI, DominatorTree *DT, 288 const LoopAccessInfo *LAI) const { 289 return TTIImpl->preferPredicateOverEpilogue(L, LI, SE, AC, TLI, DT, LAI); 290 } 291 292 bool TargetTransformInfo::emitGetActiveLaneMask() const { 293 return TTIImpl->emitGetActiveLaneMask(); 294 } 295 296 Optional<Instruction *> 297 TargetTransformInfo::instCombineIntrinsic(InstCombiner &IC, 298 IntrinsicInst &II) const { 299 return TTIImpl->instCombineIntrinsic(IC, II); 300 } 301 302 Optional<Value *> TargetTransformInfo::simplifyDemandedUseBitsIntrinsic( 303 InstCombiner &IC, IntrinsicInst &II, APInt DemandedMask, KnownBits &Known, 304 bool &KnownBitsComputed) const { 305 return TTIImpl->simplifyDemandedUseBitsIntrinsic(IC, II, DemandedMask, Known, 306 KnownBitsComputed); 307 } 308 309 Optional<Value *> TargetTransformInfo::simplifyDemandedVectorEltsIntrinsic( 310 InstCombiner &IC, IntrinsicInst &II, APInt DemandedElts, APInt &UndefElts, 311 APInt &UndefElts2, APInt &UndefElts3, 312 std::function<void(Instruction *, unsigned, APInt, APInt &)> 313 SimplifyAndSetOp) const { 314 return TTIImpl->simplifyDemandedVectorEltsIntrinsic( 315 IC, II, DemandedElts, UndefElts, UndefElts2, UndefElts3, 316 SimplifyAndSetOp); 317 } 318 319 void TargetTransformInfo::getUnrollingPreferences( 320 Loop *L, ScalarEvolution &SE, UnrollingPreferences &UP) const { 321 return TTIImpl->getUnrollingPreferences(L, SE, UP); 322 } 323 324 void TargetTransformInfo::getPeelingPreferences(Loop *L, ScalarEvolution &SE, 325 PeelingPreferences &PP) const { 326 return TTIImpl->getPeelingPreferences(L, SE, PP); 327 } 328 329 bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const { 330 return TTIImpl->isLegalAddImmediate(Imm); 331 } 332 333 bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const { 334 return TTIImpl->isLegalICmpImmediate(Imm); 335 } 336 337 bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, 338 int64_t BaseOffset, 339 bool HasBaseReg, int64_t Scale, 340 unsigned AddrSpace, 341 Instruction *I) const { 342 return TTIImpl->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg, 343 Scale, AddrSpace, I); 344 } 345 346 bool TargetTransformInfo::isLSRCostLess(LSRCost &C1, LSRCost &C2) const { 347 return TTIImpl->isLSRCostLess(C1, C2); 348 } 349 350 bool TargetTransformInfo::isNumRegsMajorCostOfLSR() const { 351 return TTIImpl->isNumRegsMajorCostOfLSR(); 352 } 353 354 bool TargetTransformInfo::isProfitableLSRChainElement(Instruction *I) const { 355 return TTIImpl->isProfitableLSRChainElement(I); 356 } 357 358 bool TargetTransformInfo::canMacroFuseCmp() const { 359 return TTIImpl->canMacroFuseCmp(); 360 } 361 362 bool TargetTransformInfo::canSaveCmp(Loop *L, BranchInst **BI, 363 ScalarEvolution *SE, LoopInfo *LI, 364 DominatorTree *DT, AssumptionCache *AC, 365 TargetLibraryInfo *LibInfo) const { 366 return TTIImpl->canSaveCmp(L, BI, SE, LI, DT, AC, LibInfo); 367 } 368 369 TTI::AddressingModeKind 370 TargetTransformInfo::getPreferredAddressingMode(const Loop *L, 371 ScalarEvolution *SE) const { 372 return TTIImpl->getPreferredAddressingMode(L, SE); 373 } 374 375 bool TargetTransformInfo::isLegalMaskedStore(Type *DataType, 376 Align Alignment) const { 377 return TTIImpl->isLegalMaskedStore(DataType, Alignment); 378 } 379 380 bool TargetTransformInfo::isLegalMaskedLoad(Type *DataType, 381 Align Alignment) const { 382 return TTIImpl->isLegalMaskedLoad(DataType, Alignment); 383 } 384 385 bool TargetTransformInfo::isLegalNTStore(Type *DataType, 386 Align Alignment) const { 387 return TTIImpl->isLegalNTStore(DataType, Alignment); 388 } 389 390 bool TargetTransformInfo::isLegalNTLoad(Type *DataType, Align Alignment) const { 391 return TTIImpl->isLegalNTLoad(DataType, Alignment); 392 } 393 394 bool TargetTransformInfo::isLegalMaskedGather(Type *DataType, 395 Align Alignment) const { 396 return TTIImpl->isLegalMaskedGather(DataType, Alignment); 397 } 398 399 bool TargetTransformInfo::isLegalMaskedScatter(Type *DataType, 400 Align Alignment) const { 401 return TTIImpl->isLegalMaskedScatter(DataType, Alignment); 402 } 403 404 bool TargetTransformInfo::isLegalMaskedCompressStore(Type *DataType) const { 405 return TTIImpl->isLegalMaskedCompressStore(DataType); 406 } 407 408 bool TargetTransformInfo::isLegalMaskedExpandLoad(Type *DataType) const { 409 return TTIImpl->isLegalMaskedExpandLoad(DataType); 410 } 411 412 bool TargetTransformInfo::hasDivRemOp(Type *DataType, bool IsSigned) const { 413 return TTIImpl->hasDivRemOp(DataType, IsSigned); 414 } 415 416 bool TargetTransformInfo::hasVolatileVariant(Instruction *I, 417 unsigned AddrSpace) const { 418 return TTIImpl->hasVolatileVariant(I, AddrSpace); 419 } 420 421 bool TargetTransformInfo::prefersVectorizedAddressing() const { 422 return TTIImpl->prefersVectorizedAddressing(); 423 } 424 425 InstructionCost TargetTransformInfo::getScalingFactorCost( 426 Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset, bool HasBaseReg, 427 int64_t Scale, unsigned AddrSpace) const { 428 InstructionCost Cost = TTIImpl->getScalingFactorCost( 429 Ty, BaseGV, BaseOffset, HasBaseReg, Scale, AddrSpace); 430 assert(Cost >= 0 && "TTI should not produce negative costs!"); 431 return Cost; 432 } 433 434 bool TargetTransformInfo::LSRWithInstrQueries() const { 435 return TTIImpl->LSRWithInstrQueries(); 436 } 437 438 bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const { 439 return TTIImpl->isTruncateFree(Ty1, Ty2); 440 } 441 442 bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const { 443 return TTIImpl->isProfitableToHoist(I); 444 } 445 446 bool TargetTransformInfo::useAA() const { return TTIImpl->useAA(); } 447 448 bool TargetTransformInfo::isTypeLegal(Type *Ty) const { 449 return TTIImpl->isTypeLegal(Ty); 450 } 451 452 unsigned TargetTransformInfo::getRegUsageForType(Type *Ty) const { 453 return TTIImpl->getRegUsageForType(Ty); 454 } 455 456 bool TargetTransformInfo::shouldBuildLookupTables() const { 457 return TTIImpl->shouldBuildLookupTables(); 458 } 459 460 bool TargetTransformInfo::shouldBuildLookupTablesForConstant( 461 Constant *C) const { 462 return TTIImpl->shouldBuildLookupTablesForConstant(C); 463 } 464 465 bool TargetTransformInfo::shouldBuildRelLookupTables() const { 466 return TTIImpl->shouldBuildRelLookupTables(); 467 } 468 469 bool TargetTransformInfo::useColdCCForColdCall(Function &F) const { 470 return TTIImpl->useColdCCForColdCall(F); 471 } 472 473 InstructionCost 474 TargetTransformInfo::getScalarizationOverhead(VectorType *Ty, 475 const APInt &DemandedElts, 476 bool Insert, bool Extract) const { 477 return TTIImpl->getScalarizationOverhead(Ty, DemandedElts, Insert, Extract); 478 } 479 480 InstructionCost TargetTransformInfo::getOperandsScalarizationOverhead( 481 ArrayRef<const Value *> Args, ArrayRef<Type *> Tys) const { 482 return TTIImpl->getOperandsScalarizationOverhead(Args, Tys); 483 } 484 485 bool TargetTransformInfo::supportsEfficientVectorElementLoadStore() const { 486 return TTIImpl->supportsEfficientVectorElementLoadStore(); 487 } 488 489 bool TargetTransformInfo::enableAggressiveInterleaving( 490 bool LoopHasReductions) const { 491 return TTIImpl->enableAggressiveInterleaving(LoopHasReductions); 492 } 493 494 TargetTransformInfo::MemCmpExpansionOptions 495 TargetTransformInfo::enableMemCmpExpansion(bool OptSize, bool IsZeroCmp) const { 496 return TTIImpl->enableMemCmpExpansion(OptSize, IsZeroCmp); 497 } 498 499 bool TargetTransformInfo::enableInterleavedAccessVectorization() const { 500 return TTIImpl->enableInterleavedAccessVectorization(); 501 } 502 503 bool TargetTransformInfo::enableMaskedInterleavedAccessVectorization() const { 504 return TTIImpl->enableMaskedInterleavedAccessVectorization(); 505 } 506 507 bool TargetTransformInfo::isFPVectorizationPotentiallyUnsafe() const { 508 return TTIImpl->isFPVectorizationPotentiallyUnsafe(); 509 } 510 511 bool TargetTransformInfo::allowsMisalignedMemoryAccesses(LLVMContext &Context, 512 unsigned BitWidth, 513 unsigned AddressSpace, 514 Align Alignment, 515 bool *Fast) const { 516 return TTIImpl->allowsMisalignedMemoryAccesses(Context, BitWidth, 517 AddressSpace, Alignment, Fast); 518 } 519 520 TargetTransformInfo::PopcntSupportKind 521 TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const { 522 return TTIImpl->getPopcntSupport(IntTyWidthInBit); 523 } 524 525 bool TargetTransformInfo::haveFastSqrt(Type *Ty) const { 526 return TTIImpl->haveFastSqrt(Ty); 527 } 528 529 bool TargetTransformInfo::isFCmpOrdCheaperThanFCmpZero(Type *Ty) const { 530 return TTIImpl->isFCmpOrdCheaperThanFCmpZero(Ty); 531 } 532 533 InstructionCost TargetTransformInfo::getFPOpCost(Type *Ty) const { 534 InstructionCost Cost = TTIImpl->getFPOpCost(Ty); 535 assert(Cost >= 0 && "TTI should not produce negative costs!"); 536 return Cost; 537 } 538 539 int TargetTransformInfo::getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx, 540 const APInt &Imm, 541 Type *Ty) const { 542 int Cost = TTIImpl->getIntImmCodeSizeCost(Opcode, Idx, Imm, Ty); 543 assert(Cost >= 0 && "TTI should not produce negative costs!"); 544 return Cost; 545 } 546 547 InstructionCost 548 TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty, 549 TTI::TargetCostKind CostKind) const { 550 InstructionCost Cost = TTIImpl->getIntImmCost(Imm, Ty, CostKind); 551 assert(Cost >= 0 && "TTI should not produce negative costs!"); 552 return Cost; 553 } 554 555 InstructionCost TargetTransformInfo::getIntImmCostInst( 556 unsigned Opcode, unsigned Idx, const APInt &Imm, Type *Ty, 557 TTI::TargetCostKind CostKind, Instruction *Inst) const { 558 InstructionCost Cost = 559 TTIImpl->getIntImmCostInst(Opcode, Idx, Imm, Ty, CostKind, Inst); 560 assert(Cost >= 0 && "TTI should not produce negative costs!"); 561 return Cost; 562 } 563 564 InstructionCost 565 TargetTransformInfo::getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx, 566 const APInt &Imm, Type *Ty, 567 TTI::TargetCostKind CostKind) const { 568 InstructionCost Cost = 569 TTIImpl->getIntImmCostIntrin(IID, Idx, Imm, Ty, CostKind); 570 assert(Cost >= 0 && "TTI should not produce negative costs!"); 571 return Cost; 572 } 573 574 unsigned TargetTransformInfo::getNumberOfRegisters(unsigned ClassID) const { 575 return TTIImpl->getNumberOfRegisters(ClassID); 576 } 577 578 unsigned TargetTransformInfo::getRegisterClassForType(bool Vector, 579 Type *Ty) const { 580 return TTIImpl->getRegisterClassForType(Vector, Ty); 581 } 582 583 const char *TargetTransformInfo::getRegisterClassName(unsigned ClassID) const { 584 return TTIImpl->getRegisterClassName(ClassID); 585 } 586 587 TypeSize TargetTransformInfo::getRegisterBitWidth( 588 TargetTransformInfo::RegisterKind K) const { 589 return TTIImpl->getRegisterBitWidth(K); 590 } 591 592 unsigned TargetTransformInfo::getMinVectorRegisterBitWidth() const { 593 return TTIImpl->getMinVectorRegisterBitWidth(); 594 } 595 596 Optional<unsigned> TargetTransformInfo::getMaxVScale() const { 597 return TTIImpl->getMaxVScale(); 598 } 599 600 bool TargetTransformInfo::shouldMaximizeVectorBandwidth() const { 601 return TTIImpl->shouldMaximizeVectorBandwidth(); 602 } 603 604 ElementCount TargetTransformInfo::getMinimumVF(unsigned ElemWidth, 605 bool IsScalable) const { 606 return TTIImpl->getMinimumVF(ElemWidth, IsScalable); 607 } 608 609 unsigned TargetTransformInfo::getMaximumVF(unsigned ElemWidth, 610 unsigned Opcode) const { 611 return TTIImpl->getMaximumVF(ElemWidth, Opcode); 612 } 613 614 bool TargetTransformInfo::shouldConsiderAddressTypePromotion( 615 const Instruction &I, bool &AllowPromotionWithoutCommonHeader) const { 616 return TTIImpl->shouldConsiderAddressTypePromotion( 617 I, AllowPromotionWithoutCommonHeader); 618 } 619 620 unsigned TargetTransformInfo::getCacheLineSize() const { 621 return TTIImpl->getCacheLineSize(); 622 } 623 624 llvm::Optional<unsigned> 625 TargetTransformInfo::getCacheSize(CacheLevel Level) const { 626 return TTIImpl->getCacheSize(Level); 627 } 628 629 llvm::Optional<unsigned> 630 TargetTransformInfo::getCacheAssociativity(CacheLevel Level) const { 631 return TTIImpl->getCacheAssociativity(Level); 632 } 633 634 unsigned TargetTransformInfo::getPrefetchDistance() const { 635 return TTIImpl->getPrefetchDistance(); 636 } 637 638 unsigned TargetTransformInfo::getMinPrefetchStride( 639 unsigned NumMemAccesses, unsigned NumStridedMemAccesses, 640 unsigned NumPrefetches, bool HasCall) const { 641 return TTIImpl->getMinPrefetchStride(NumMemAccesses, NumStridedMemAccesses, 642 NumPrefetches, HasCall); 643 } 644 645 unsigned TargetTransformInfo::getMaxPrefetchIterationsAhead() const { 646 return TTIImpl->getMaxPrefetchIterationsAhead(); 647 } 648 649 bool TargetTransformInfo::enableWritePrefetching() const { 650 return TTIImpl->enableWritePrefetching(); 651 } 652 653 unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const { 654 return TTIImpl->getMaxInterleaveFactor(VF); 655 } 656 657 TargetTransformInfo::OperandValueKind 658 TargetTransformInfo::getOperandInfo(const Value *V, 659 OperandValueProperties &OpProps) { 660 OperandValueKind OpInfo = OK_AnyValue; 661 OpProps = OP_None; 662 663 if (const auto *CI = dyn_cast<ConstantInt>(V)) { 664 if (CI->getValue().isPowerOf2()) 665 OpProps = OP_PowerOf2; 666 return OK_UniformConstantValue; 667 } 668 669 // A broadcast shuffle creates a uniform value. 670 // TODO: Add support for non-zero index broadcasts. 671 // TODO: Add support for different source vector width. 672 if (const auto *ShuffleInst = dyn_cast<ShuffleVectorInst>(V)) 673 if (ShuffleInst->isZeroEltSplat()) 674 OpInfo = OK_UniformValue; 675 676 const Value *Splat = getSplatValue(V); 677 678 // Check for a splat of a constant or for a non uniform vector of constants 679 // and check if the constant(s) are all powers of two. 680 if (isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) { 681 OpInfo = OK_NonUniformConstantValue; 682 if (Splat) { 683 OpInfo = OK_UniformConstantValue; 684 if (auto *CI = dyn_cast<ConstantInt>(Splat)) 685 if (CI->getValue().isPowerOf2()) 686 OpProps = OP_PowerOf2; 687 } else if (const auto *CDS = dyn_cast<ConstantDataSequential>(V)) { 688 OpProps = OP_PowerOf2; 689 for (unsigned I = 0, E = CDS->getNumElements(); I != E; ++I) { 690 if (auto *CI = dyn_cast<ConstantInt>(CDS->getElementAsConstant(I))) 691 if (CI->getValue().isPowerOf2()) 692 continue; 693 OpProps = OP_None; 694 break; 695 } 696 } 697 } 698 699 // Check for a splat of a uniform value. This is not loop aware, so return 700 // true only for the obviously uniform cases (argument, globalvalue) 701 if (Splat && (isa<Argument>(Splat) || isa<GlobalValue>(Splat))) 702 OpInfo = OK_UniformValue; 703 704 return OpInfo; 705 } 706 707 InstructionCost TargetTransformInfo::getArithmeticInstrCost( 708 unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind, 709 OperandValueKind Opd1Info, OperandValueKind Opd2Info, 710 OperandValueProperties Opd1PropInfo, OperandValueProperties Opd2PropInfo, 711 ArrayRef<const Value *> Args, const Instruction *CxtI) const { 712 InstructionCost Cost = 713 TTIImpl->getArithmeticInstrCost(Opcode, Ty, CostKind, Opd1Info, Opd2Info, 714 Opd1PropInfo, Opd2PropInfo, Args, CxtI); 715 assert(Cost >= 0 && "TTI should not produce negative costs!"); 716 return Cost; 717 } 718 719 InstructionCost TargetTransformInfo::getShuffleCost(ShuffleKind Kind, 720 VectorType *Ty, 721 ArrayRef<int> Mask, 722 int Index, 723 VectorType *SubTp) const { 724 InstructionCost Cost = TTIImpl->getShuffleCost(Kind, Ty, Mask, Index, SubTp); 725 assert(Cost >= 0 && "TTI should not produce negative costs!"); 726 return Cost; 727 } 728 729 TTI::CastContextHint 730 TargetTransformInfo::getCastContextHint(const Instruction *I) { 731 if (!I) 732 return CastContextHint::None; 733 734 auto getLoadStoreKind = [](const Value *V, unsigned LdStOp, unsigned MaskedOp, 735 unsigned GatScatOp) { 736 const Instruction *I = dyn_cast<Instruction>(V); 737 if (!I) 738 return CastContextHint::None; 739 740 if (I->getOpcode() == LdStOp) 741 return CastContextHint::Normal; 742 743 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { 744 if (II->getIntrinsicID() == MaskedOp) 745 return TTI::CastContextHint::Masked; 746 if (II->getIntrinsicID() == GatScatOp) 747 return TTI::CastContextHint::GatherScatter; 748 } 749 750 return TTI::CastContextHint::None; 751 }; 752 753 switch (I->getOpcode()) { 754 case Instruction::ZExt: 755 case Instruction::SExt: 756 case Instruction::FPExt: 757 return getLoadStoreKind(I->getOperand(0), Instruction::Load, 758 Intrinsic::masked_load, Intrinsic::masked_gather); 759 case Instruction::Trunc: 760 case Instruction::FPTrunc: 761 if (I->hasOneUse()) 762 return getLoadStoreKind(*I->user_begin(), Instruction::Store, 763 Intrinsic::masked_store, 764 Intrinsic::masked_scatter); 765 break; 766 default: 767 return CastContextHint::None; 768 } 769 770 return TTI::CastContextHint::None; 771 } 772 773 InstructionCost TargetTransformInfo::getCastInstrCost( 774 unsigned Opcode, Type *Dst, Type *Src, CastContextHint CCH, 775 TTI::TargetCostKind CostKind, const Instruction *I) const { 776 assert((I == nullptr || I->getOpcode() == Opcode) && 777 "Opcode should reflect passed instruction."); 778 InstructionCost Cost = 779 TTIImpl->getCastInstrCost(Opcode, Dst, Src, CCH, CostKind, I); 780 assert(Cost >= 0 && "TTI should not produce negative costs!"); 781 return Cost; 782 } 783 784 InstructionCost TargetTransformInfo::getExtractWithExtendCost( 785 unsigned Opcode, Type *Dst, VectorType *VecTy, unsigned Index) const { 786 InstructionCost Cost = 787 TTIImpl->getExtractWithExtendCost(Opcode, Dst, VecTy, Index); 788 assert(Cost >= 0 && "TTI should not produce negative costs!"); 789 return Cost; 790 } 791 792 InstructionCost TargetTransformInfo::getCFInstrCost( 793 unsigned Opcode, TTI::TargetCostKind CostKind, const Instruction *I) const { 794 assert((I == nullptr || I->getOpcode() == Opcode) && 795 "Opcode should reflect passed instruction."); 796 InstructionCost Cost = TTIImpl->getCFInstrCost(Opcode, CostKind, I); 797 assert(Cost >= 0 && "TTI should not produce negative costs!"); 798 return Cost; 799 } 800 801 InstructionCost TargetTransformInfo::getCmpSelInstrCost( 802 unsigned Opcode, Type *ValTy, Type *CondTy, CmpInst::Predicate VecPred, 803 TTI::TargetCostKind CostKind, const Instruction *I) const { 804 assert((I == nullptr || I->getOpcode() == Opcode) && 805 "Opcode should reflect passed instruction."); 806 InstructionCost Cost = 807 TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy, VecPred, CostKind, I); 808 assert(Cost >= 0 && "TTI should not produce negative costs!"); 809 return Cost; 810 } 811 812 InstructionCost TargetTransformInfo::getVectorInstrCost(unsigned Opcode, 813 Type *Val, 814 unsigned Index) const { 815 InstructionCost Cost = TTIImpl->getVectorInstrCost(Opcode, Val, Index); 816 assert(Cost >= 0 && "TTI should not produce negative costs!"); 817 return Cost; 818 } 819 820 InstructionCost TargetTransformInfo::getMemoryOpCost( 821 unsigned Opcode, Type *Src, Align Alignment, unsigned AddressSpace, 822 TTI::TargetCostKind CostKind, const Instruction *I) const { 823 assert((I == nullptr || I->getOpcode() == Opcode) && 824 "Opcode should reflect passed instruction."); 825 InstructionCost Cost = TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, 826 AddressSpace, CostKind, I); 827 assert(Cost >= 0 && "TTI should not produce negative costs!"); 828 return Cost; 829 } 830 831 InstructionCost TargetTransformInfo::getMaskedMemoryOpCost( 832 unsigned Opcode, Type *Src, Align Alignment, unsigned AddressSpace, 833 TTI::TargetCostKind CostKind) const { 834 InstructionCost Cost = TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, 835 AddressSpace, CostKind); 836 assert(Cost >= 0 && "TTI should not produce negative costs!"); 837 return Cost; 838 } 839 840 InstructionCost TargetTransformInfo::getGatherScatterOpCost( 841 unsigned Opcode, Type *DataTy, const Value *Ptr, bool VariableMask, 842 Align Alignment, TTI::TargetCostKind CostKind, const Instruction *I) const { 843 InstructionCost Cost = TTIImpl->getGatherScatterOpCost( 844 Opcode, DataTy, Ptr, VariableMask, Alignment, CostKind, I); 845 assert(Cost >= 0 && "TTI should not produce negative costs!"); 846 return Cost; 847 } 848 849 InstructionCost TargetTransformInfo::getInterleavedMemoryOpCost( 850 unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices, 851 Align Alignment, unsigned AddressSpace, TTI::TargetCostKind CostKind, 852 bool UseMaskForCond, bool UseMaskForGaps) const { 853 InstructionCost Cost = TTIImpl->getInterleavedMemoryOpCost( 854 Opcode, VecTy, Factor, Indices, Alignment, AddressSpace, CostKind, 855 UseMaskForCond, UseMaskForGaps); 856 assert(Cost >= 0 && "TTI should not produce negative costs!"); 857 return Cost; 858 } 859 860 InstructionCost 861 TargetTransformInfo::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA, 862 TTI::TargetCostKind CostKind) const { 863 InstructionCost Cost = TTIImpl->getIntrinsicInstrCost(ICA, CostKind); 864 assert(Cost >= 0 && "TTI should not produce negative costs!"); 865 return Cost; 866 } 867 868 InstructionCost 869 TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy, 870 ArrayRef<Type *> Tys, 871 TTI::TargetCostKind CostKind) const { 872 InstructionCost Cost = TTIImpl->getCallInstrCost(F, RetTy, Tys, CostKind); 873 assert(Cost >= 0 && "TTI should not produce negative costs!"); 874 return Cost; 875 } 876 877 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const { 878 return TTIImpl->getNumberOfParts(Tp); 879 } 880 881 InstructionCost 882 TargetTransformInfo::getAddressComputationCost(Type *Tp, ScalarEvolution *SE, 883 const SCEV *Ptr) const { 884 InstructionCost Cost = TTIImpl->getAddressComputationCost(Tp, SE, Ptr); 885 assert(Cost >= 0 && "TTI should not produce negative costs!"); 886 return Cost; 887 } 888 889 InstructionCost TargetTransformInfo::getMemcpyCost(const Instruction *I) const { 890 InstructionCost Cost = TTIImpl->getMemcpyCost(I); 891 assert(Cost >= 0 && "TTI should not produce negative costs!"); 892 return Cost; 893 } 894 895 InstructionCost TargetTransformInfo::getArithmeticReductionCost( 896 unsigned Opcode, VectorType *Ty, bool IsPairwiseForm, 897 TTI::TargetCostKind CostKind) const { 898 InstructionCost Cost = 899 TTIImpl->getArithmeticReductionCost(Opcode, Ty, IsPairwiseForm, CostKind); 900 assert(Cost >= 0 && "TTI should not produce negative costs!"); 901 return Cost; 902 } 903 904 InstructionCost TargetTransformInfo::getMinMaxReductionCost( 905 VectorType *Ty, VectorType *CondTy, bool IsPairwiseForm, bool IsUnsigned, 906 TTI::TargetCostKind CostKind) const { 907 InstructionCost Cost = TTIImpl->getMinMaxReductionCost( 908 Ty, CondTy, IsPairwiseForm, IsUnsigned, CostKind); 909 assert(Cost >= 0 && "TTI should not produce negative costs!"); 910 return Cost; 911 } 912 913 InstructionCost TargetTransformInfo::getExtendedAddReductionCost( 914 bool IsMLA, bool IsUnsigned, Type *ResTy, VectorType *Ty, 915 TTI::TargetCostKind CostKind) const { 916 return TTIImpl->getExtendedAddReductionCost(IsMLA, IsUnsigned, ResTy, Ty, 917 CostKind); 918 } 919 920 unsigned 921 TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const { 922 return TTIImpl->getCostOfKeepingLiveOverCall(Tys); 923 } 924 925 bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst, 926 MemIntrinsicInfo &Info) const { 927 return TTIImpl->getTgtMemIntrinsic(Inst, Info); 928 } 929 930 unsigned TargetTransformInfo::getAtomicMemIntrinsicMaxElementSize() const { 931 return TTIImpl->getAtomicMemIntrinsicMaxElementSize(); 932 } 933 934 Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic( 935 IntrinsicInst *Inst, Type *ExpectedType) const { 936 return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType); 937 } 938 939 Type *TargetTransformInfo::getMemcpyLoopLoweringType( 940 LLVMContext &Context, Value *Length, unsigned SrcAddrSpace, 941 unsigned DestAddrSpace, unsigned SrcAlign, unsigned DestAlign) const { 942 return TTIImpl->getMemcpyLoopLoweringType(Context, Length, SrcAddrSpace, 943 DestAddrSpace, SrcAlign, DestAlign); 944 } 945 946 void TargetTransformInfo::getMemcpyLoopResidualLoweringType( 947 SmallVectorImpl<Type *> &OpsOut, LLVMContext &Context, 948 unsigned RemainingBytes, unsigned SrcAddrSpace, unsigned DestAddrSpace, 949 unsigned SrcAlign, unsigned DestAlign) const { 950 TTIImpl->getMemcpyLoopResidualLoweringType(OpsOut, Context, RemainingBytes, 951 SrcAddrSpace, DestAddrSpace, 952 SrcAlign, DestAlign); 953 } 954 955 bool TargetTransformInfo::areInlineCompatible(const Function *Caller, 956 const Function *Callee) const { 957 return TTIImpl->areInlineCompatible(Caller, Callee); 958 } 959 960 bool TargetTransformInfo::areFunctionArgsABICompatible( 961 const Function *Caller, const Function *Callee, 962 SmallPtrSetImpl<Argument *> &Args) const { 963 return TTIImpl->areFunctionArgsABICompatible(Caller, Callee, Args); 964 } 965 966 bool TargetTransformInfo::isIndexedLoadLegal(MemIndexedMode Mode, 967 Type *Ty) const { 968 return TTIImpl->isIndexedLoadLegal(Mode, Ty); 969 } 970 971 bool TargetTransformInfo::isIndexedStoreLegal(MemIndexedMode Mode, 972 Type *Ty) const { 973 return TTIImpl->isIndexedStoreLegal(Mode, Ty); 974 } 975 976 unsigned TargetTransformInfo::getLoadStoreVecRegBitWidth(unsigned AS) const { 977 return TTIImpl->getLoadStoreVecRegBitWidth(AS); 978 } 979 980 bool TargetTransformInfo::isLegalToVectorizeLoad(LoadInst *LI) const { 981 return TTIImpl->isLegalToVectorizeLoad(LI); 982 } 983 984 bool TargetTransformInfo::isLegalToVectorizeStore(StoreInst *SI) const { 985 return TTIImpl->isLegalToVectorizeStore(SI); 986 } 987 988 bool TargetTransformInfo::isLegalToVectorizeLoadChain( 989 unsigned ChainSizeInBytes, Align Alignment, unsigned AddrSpace) const { 990 return TTIImpl->isLegalToVectorizeLoadChain(ChainSizeInBytes, Alignment, 991 AddrSpace); 992 } 993 994 bool TargetTransformInfo::isLegalToVectorizeStoreChain( 995 unsigned ChainSizeInBytes, Align Alignment, unsigned AddrSpace) const { 996 return TTIImpl->isLegalToVectorizeStoreChain(ChainSizeInBytes, Alignment, 997 AddrSpace); 998 } 999 1000 bool TargetTransformInfo::isLegalToVectorizeReduction( 1001 RecurrenceDescriptor RdxDesc, ElementCount VF) const { 1002 return TTIImpl->isLegalToVectorizeReduction(RdxDesc, VF); 1003 } 1004 1005 unsigned TargetTransformInfo::getLoadVectorFactor(unsigned VF, 1006 unsigned LoadSize, 1007 unsigned ChainSizeInBytes, 1008 VectorType *VecTy) const { 1009 return TTIImpl->getLoadVectorFactor(VF, LoadSize, ChainSizeInBytes, VecTy); 1010 } 1011 1012 unsigned TargetTransformInfo::getStoreVectorFactor(unsigned VF, 1013 unsigned StoreSize, 1014 unsigned ChainSizeInBytes, 1015 VectorType *VecTy) const { 1016 return TTIImpl->getStoreVectorFactor(VF, StoreSize, ChainSizeInBytes, VecTy); 1017 } 1018 1019 bool TargetTransformInfo::preferInLoopReduction(unsigned Opcode, Type *Ty, 1020 ReductionFlags Flags) const { 1021 return TTIImpl->preferInLoopReduction(Opcode, Ty, Flags); 1022 } 1023 1024 bool TargetTransformInfo::preferPredicatedReductionSelect( 1025 unsigned Opcode, Type *Ty, ReductionFlags Flags) const { 1026 return TTIImpl->preferPredicatedReductionSelect(Opcode, Ty, Flags); 1027 } 1028 1029 bool TargetTransformInfo::shouldExpandReduction(const IntrinsicInst *II) const { 1030 return TTIImpl->shouldExpandReduction(II); 1031 } 1032 1033 unsigned TargetTransformInfo::getGISelRematGlobalCost() const { 1034 return TTIImpl->getGISelRematGlobalCost(); 1035 } 1036 1037 bool TargetTransformInfo::supportsScalableVectors() const { 1038 return TTIImpl->supportsScalableVectors(); 1039 } 1040 1041 bool TargetTransformInfo::hasActiveVectorLength() const { 1042 return TTIImpl->hasActiveVectorLength(); 1043 } 1044 1045 InstructionCost 1046 TargetTransformInfo::getInstructionLatency(const Instruction *I) const { 1047 return TTIImpl->getInstructionLatency(I); 1048 } 1049 1050 static bool matchPairwiseShuffleMask(ShuffleVectorInst *SI, bool IsLeft, 1051 unsigned Level) { 1052 // We don't need a shuffle if we just want to have element 0 in position 0 of 1053 // the vector. 1054 if (!SI && Level == 0 && IsLeft) 1055 return true; 1056 else if (!SI) 1057 return false; 1058 1059 SmallVector<int, 32> Mask( 1060 cast<FixedVectorType>(SI->getType())->getNumElements(), -1); 1061 1062 // Build a mask of 0, 2, ... (left) or 1, 3, ... (right) depending on whether 1063 // we look at the left or right side. 1064 for (unsigned i = 0, e = (1 << Level), val = !IsLeft; i != e; ++i, val += 2) 1065 Mask[i] = val; 1066 1067 ArrayRef<int> ActualMask = SI->getShuffleMask(); 1068 return Mask == ActualMask; 1069 } 1070 1071 static Optional<TTI::ReductionData> getReductionData(Instruction *I) { 1072 Value *L, *R; 1073 if (m_BinOp(m_Value(L), m_Value(R)).match(I)) 1074 return TTI::ReductionData(TTI::RK_Arithmetic, I->getOpcode(), L, R); 1075 if (auto *SI = dyn_cast<SelectInst>(I)) { 1076 if (m_SMin(m_Value(L), m_Value(R)).match(SI) || 1077 m_SMax(m_Value(L), m_Value(R)).match(SI) || 1078 m_OrdFMin(m_Value(L), m_Value(R)).match(SI) || 1079 m_OrdFMax(m_Value(L), m_Value(R)).match(SI) || 1080 m_UnordFMin(m_Value(L), m_Value(R)).match(SI) || 1081 m_UnordFMax(m_Value(L), m_Value(R)).match(SI)) { 1082 auto *CI = cast<CmpInst>(SI->getCondition()); 1083 return TTI::ReductionData(TTI::RK_MinMax, CI->getOpcode(), L, R); 1084 } 1085 if (m_UMin(m_Value(L), m_Value(R)).match(SI) || 1086 m_UMax(m_Value(L), m_Value(R)).match(SI)) { 1087 auto *CI = cast<CmpInst>(SI->getCondition()); 1088 return TTI::ReductionData(TTI::RK_UnsignedMinMax, CI->getOpcode(), L, R); 1089 } 1090 } 1091 return llvm::None; 1092 } 1093 1094 static TTI::ReductionKind matchPairwiseReductionAtLevel(Instruction *I, 1095 unsigned Level, 1096 unsigned NumLevels) { 1097 // Match one level of pairwise operations. 1098 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef, 1099 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef> 1100 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef, 1101 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef> 1102 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1 1103 if (!I) 1104 return TTI::RK_None; 1105 1106 assert(I->getType()->isVectorTy() && "Expecting a vector type"); 1107 1108 Optional<TTI::ReductionData> RD = getReductionData(I); 1109 if (!RD) 1110 return TTI::RK_None; 1111 1112 ShuffleVectorInst *LS = dyn_cast<ShuffleVectorInst>(RD->LHS); 1113 if (!LS && Level) 1114 return TTI::RK_None; 1115 ShuffleVectorInst *RS = dyn_cast<ShuffleVectorInst>(RD->RHS); 1116 if (!RS && Level) 1117 return TTI::RK_None; 1118 1119 // On level 0 we can omit one shufflevector instruction. 1120 if (!Level && !RS && !LS) 1121 return TTI::RK_None; 1122 1123 // Shuffle inputs must match. 1124 Value *NextLevelOpL = LS ? LS->getOperand(0) : nullptr; 1125 Value *NextLevelOpR = RS ? RS->getOperand(0) : nullptr; 1126 Value *NextLevelOp = nullptr; 1127 if (NextLevelOpR && NextLevelOpL) { 1128 // If we have two shuffles their operands must match. 1129 if (NextLevelOpL != NextLevelOpR) 1130 return TTI::RK_None; 1131 1132 NextLevelOp = NextLevelOpL; 1133 } else if (Level == 0 && (NextLevelOpR || NextLevelOpL)) { 1134 // On the first level we can omit the shufflevector <0, undef,...>. So the 1135 // input to the other shufflevector <1, undef> must match with one of the 1136 // inputs to the current binary operation. 1137 // Example: 1138 // %NextLevelOpL = shufflevector %R, <1, undef ...> 1139 // %BinOp = fadd %NextLevelOpL, %R 1140 if (NextLevelOpL && NextLevelOpL != RD->RHS) 1141 return TTI::RK_None; 1142 else if (NextLevelOpR && NextLevelOpR != RD->LHS) 1143 return TTI::RK_None; 1144 1145 NextLevelOp = NextLevelOpL ? RD->RHS : RD->LHS; 1146 } else 1147 return TTI::RK_None; 1148 1149 // Check that the next levels binary operation exists and matches with the 1150 // current one. 1151 if (Level + 1 != NumLevels) { 1152 if (!isa<Instruction>(NextLevelOp)) 1153 return TTI::RK_None; 1154 Optional<TTI::ReductionData> NextLevelRD = 1155 getReductionData(cast<Instruction>(NextLevelOp)); 1156 if (!NextLevelRD || !RD->hasSameData(*NextLevelRD)) 1157 return TTI::RK_None; 1158 } 1159 1160 // Shuffle mask for pairwise operation must match. 1161 if (matchPairwiseShuffleMask(LS, /*IsLeft=*/true, Level)) { 1162 if (!matchPairwiseShuffleMask(RS, /*IsLeft=*/false, Level)) 1163 return TTI::RK_None; 1164 } else if (matchPairwiseShuffleMask(RS, /*IsLeft=*/true, Level)) { 1165 if (!matchPairwiseShuffleMask(LS, /*IsLeft=*/false, Level)) 1166 return TTI::RK_None; 1167 } else { 1168 return TTI::RK_None; 1169 } 1170 1171 if (++Level == NumLevels) 1172 return RD->Kind; 1173 1174 // Match next level. 1175 return matchPairwiseReductionAtLevel(dyn_cast<Instruction>(NextLevelOp), Level, 1176 NumLevels); 1177 } 1178 1179 TTI::ReductionKind TTI::matchPairwiseReduction( 1180 const ExtractElementInst *ReduxRoot, unsigned &Opcode, VectorType *&Ty) { 1181 if (!EnableReduxCost) 1182 return TTI::RK_None; 1183 1184 // Need to extract the first element. 1185 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1)); 1186 unsigned Idx = ~0u; 1187 if (CI) 1188 Idx = CI->getZExtValue(); 1189 if (Idx != 0) 1190 return TTI::RK_None; 1191 1192 auto *RdxStart = dyn_cast<Instruction>(ReduxRoot->getOperand(0)); 1193 if (!RdxStart) 1194 return TTI::RK_None; 1195 Optional<TTI::ReductionData> RD = getReductionData(RdxStart); 1196 if (!RD) 1197 return TTI::RK_None; 1198 1199 auto *VecTy = cast<FixedVectorType>(RdxStart->getType()); 1200 unsigned NumVecElems = VecTy->getNumElements(); 1201 if (!isPowerOf2_32(NumVecElems)) 1202 return TTI::RK_None; 1203 1204 // We look for a sequence of shuffle,shuffle,add triples like the following 1205 // that builds a pairwise reduction tree. 1206 // 1207 // (X0, X1, X2, X3) 1208 // (X0 + X1, X2 + X3, undef, undef) 1209 // ((X0 + X1) + (X2 + X3), undef, undef, undef) 1210 // 1211 // %rdx.shuf.0.0 = shufflevector <4 x float> %rdx, <4 x float> undef, 1212 // <4 x i32> <i32 0, i32 2 , i32 undef, i32 undef> 1213 // %rdx.shuf.0.1 = shufflevector <4 x float> %rdx, <4 x float> undef, 1214 // <4 x i32> <i32 1, i32 3, i32 undef, i32 undef> 1215 // %bin.rdx.0 = fadd <4 x float> %rdx.shuf.0.0, %rdx.shuf.0.1 1216 // %rdx.shuf.1.0 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef, 1217 // <4 x i32> <i32 0, i32 undef, i32 undef, i32 undef> 1218 // %rdx.shuf.1.1 = shufflevector <4 x float> %bin.rdx.0, <4 x float> undef, 1219 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef> 1220 // %bin.rdx8 = fadd <4 x float> %rdx.shuf.1.0, %rdx.shuf.1.1 1221 // %r = extractelement <4 x float> %bin.rdx8, i32 0 1222 if (matchPairwiseReductionAtLevel(RdxStart, 0, Log2_32(NumVecElems)) == 1223 TTI::RK_None) 1224 return TTI::RK_None; 1225 1226 Opcode = RD->Opcode; 1227 Ty = VecTy; 1228 1229 return RD->Kind; 1230 } 1231 1232 static std::pair<Value *, ShuffleVectorInst *> 1233 getShuffleAndOtherOprd(Value *L, Value *R) { 1234 ShuffleVectorInst *S = nullptr; 1235 1236 if ((S = dyn_cast<ShuffleVectorInst>(L))) 1237 return std::make_pair(R, S); 1238 1239 S = dyn_cast<ShuffleVectorInst>(R); 1240 return std::make_pair(L, S); 1241 } 1242 1243 TTI::ReductionKind TTI::matchVectorSplittingReduction( 1244 const ExtractElementInst *ReduxRoot, unsigned &Opcode, VectorType *&Ty) { 1245 1246 if (!EnableReduxCost) 1247 return TTI::RK_None; 1248 1249 // Need to extract the first element. 1250 ConstantInt *CI = dyn_cast<ConstantInt>(ReduxRoot->getOperand(1)); 1251 unsigned Idx = ~0u; 1252 if (CI) 1253 Idx = CI->getZExtValue(); 1254 if (Idx != 0) 1255 return TTI::RK_None; 1256 1257 auto *RdxStart = dyn_cast<Instruction>(ReduxRoot->getOperand(0)); 1258 if (!RdxStart) 1259 return TTI::RK_None; 1260 Optional<TTI::ReductionData> RD = getReductionData(RdxStart); 1261 if (!RD) 1262 return TTI::RK_None; 1263 1264 auto *VecTy = cast<FixedVectorType>(ReduxRoot->getOperand(0)->getType()); 1265 unsigned NumVecElems = VecTy->getNumElements(); 1266 if (!isPowerOf2_32(NumVecElems)) 1267 return TTI::RK_None; 1268 1269 // We look for a sequence of shuffles and adds like the following matching one 1270 // fadd, shuffle vector pair at a time. 1271 // 1272 // %rdx.shuf = shufflevector <4 x float> %rdx, <4 x float> undef, 1273 // <4 x i32> <i32 2, i32 3, i32 undef, i32 undef> 1274 // %bin.rdx = fadd <4 x float> %rdx, %rdx.shuf 1275 // %rdx.shuf7 = shufflevector <4 x float> %bin.rdx, <4 x float> undef, 1276 // <4 x i32> <i32 1, i32 undef, i32 undef, i32 undef> 1277 // %bin.rdx8 = fadd <4 x float> %bin.rdx, %rdx.shuf7 1278 // %r = extractelement <4 x float> %bin.rdx8, i32 0 1279 1280 unsigned MaskStart = 1; 1281 Instruction *RdxOp = RdxStart; 1282 SmallVector<int, 32> ShuffleMask(NumVecElems, 0); 1283 unsigned NumVecElemsRemain = NumVecElems; 1284 while (NumVecElemsRemain - 1) { 1285 // Check for the right reduction operation. 1286 if (!RdxOp) 1287 return TTI::RK_None; 1288 Optional<TTI::ReductionData> RDLevel = getReductionData(RdxOp); 1289 if (!RDLevel || !RDLevel->hasSameData(*RD)) 1290 return TTI::RK_None; 1291 1292 Value *NextRdxOp; 1293 ShuffleVectorInst *Shuffle; 1294 std::tie(NextRdxOp, Shuffle) = 1295 getShuffleAndOtherOprd(RDLevel->LHS, RDLevel->RHS); 1296 1297 // Check the current reduction operation and the shuffle use the same value. 1298 if (Shuffle == nullptr) 1299 return TTI::RK_None; 1300 if (Shuffle->getOperand(0) != NextRdxOp) 1301 return TTI::RK_None; 1302 1303 // Check that shuffle masks matches. 1304 for (unsigned j = 0; j != MaskStart; ++j) 1305 ShuffleMask[j] = MaskStart + j; 1306 // Fill the rest of the mask with -1 for undef. 1307 std::fill(&ShuffleMask[MaskStart], ShuffleMask.end(), -1); 1308 1309 ArrayRef<int> Mask = Shuffle->getShuffleMask(); 1310 if (ShuffleMask != Mask) 1311 return TTI::RK_None; 1312 1313 RdxOp = dyn_cast<Instruction>(NextRdxOp); 1314 NumVecElemsRemain /= 2; 1315 MaskStart *= 2; 1316 } 1317 1318 Opcode = RD->Opcode; 1319 Ty = VecTy; 1320 return RD->Kind; 1321 } 1322 1323 TTI::ReductionKind 1324 TTI::matchVectorReduction(const ExtractElementInst *Root, unsigned &Opcode, 1325 VectorType *&Ty, bool &IsPairwise) { 1326 TTI::ReductionKind RdxKind = matchVectorSplittingReduction(Root, Opcode, Ty); 1327 if (RdxKind != TTI::ReductionKind::RK_None) { 1328 IsPairwise = false; 1329 return RdxKind; 1330 } 1331 IsPairwise = true; 1332 return matchPairwiseReduction(Root, Opcode, Ty); 1333 } 1334 1335 InstructionCost 1336 TargetTransformInfo::getInstructionThroughput(const Instruction *I) const { 1337 TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput; 1338 1339 switch (I->getOpcode()) { 1340 case Instruction::GetElementPtr: 1341 case Instruction::Ret: 1342 case Instruction::PHI: 1343 case Instruction::Br: 1344 case Instruction::Add: 1345 case Instruction::FAdd: 1346 case Instruction::Sub: 1347 case Instruction::FSub: 1348 case Instruction::Mul: 1349 case Instruction::FMul: 1350 case Instruction::UDiv: 1351 case Instruction::SDiv: 1352 case Instruction::FDiv: 1353 case Instruction::URem: 1354 case Instruction::SRem: 1355 case Instruction::FRem: 1356 case Instruction::Shl: 1357 case Instruction::LShr: 1358 case Instruction::AShr: 1359 case Instruction::And: 1360 case Instruction::Or: 1361 case Instruction::Xor: 1362 case Instruction::FNeg: 1363 case Instruction::Select: 1364 case Instruction::ICmp: 1365 case Instruction::FCmp: 1366 case Instruction::Store: 1367 case Instruction::Load: 1368 case Instruction::ZExt: 1369 case Instruction::SExt: 1370 case Instruction::FPToUI: 1371 case Instruction::FPToSI: 1372 case Instruction::FPExt: 1373 case Instruction::PtrToInt: 1374 case Instruction::IntToPtr: 1375 case Instruction::SIToFP: 1376 case Instruction::UIToFP: 1377 case Instruction::Trunc: 1378 case Instruction::FPTrunc: 1379 case Instruction::BitCast: 1380 case Instruction::AddrSpaceCast: 1381 case Instruction::ExtractElement: 1382 case Instruction::InsertElement: 1383 case Instruction::ExtractValue: 1384 case Instruction::ShuffleVector: 1385 case Instruction::Call: 1386 case Instruction::Switch: 1387 return getUserCost(I, CostKind); 1388 default: 1389 // We don't have any information on this instruction. 1390 return -1; 1391 } 1392 } 1393 1394 TargetTransformInfo::Concept::~Concept() {} 1395 1396 TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {} 1397 1398 TargetIRAnalysis::TargetIRAnalysis( 1399 std::function<Result(const Function &)> TTICallback) 1400 : TTICallback(std::move(TTICallback)) {} 1401 1402 TargetIRAnalysis::Result TargetIRAnalysis::run(const Function &F, 1403 FunctionAnalysisManager &) { 1404 return TTICallback(F); 1405 } 1406 1407 AnalysisKey TargetIRAnalysis::Key; 1408 1409 TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(const Function &F) { 1410 return Result(F.getParent()->getDataLayout()); 1411 } 1412 1413 // Register the basic pass. 1414 INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti", 1415 "Target Transform Information", false, true) 1416 char TargetTransformInfoWrapperPass::ID = 0; 1417 1418 void TargetTransformInfoWrapperPass::anchor() {} 1419 1420 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass() 1421 : ImmutablePass(ID) { 1422 initializeTargetTransformInfoWrapperPassPass( 1423 *PassRegistry::getPassRegistry()); 1424 } 1425 1426 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass( 1427 TargetIRAnalysis TIRA) 1428 : ImmutablePass(ID), TIRA(std::move(TIRA)) { 1429 initializeTargetTransformInfoWrapperPassPass( 1430 *PassRegistry::getPassRegistry()); 1431 } 1432 1433 TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(const Function &F) { 1434 FunctionAnalysisManager DummyFAM; 1435 TTI = TIRA.run(F, DummyFAM); 1436 return *TTI; 1437 } 1438 1439 ImmutablePass * 1440 llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) { 1441 return new TargetTransformInfoWrapperPass(std::move(TIRA)); 1442 } 1443