diff --git a/llvm/include/llvm/Analysis/TargetTransformInfo.h b/llvm/include/llvm/Analysis/TargetTransformInfo.h --- a/llvm/include/llvm/Analysis/TargetTransformInfo.h +++ b/llvm/include/llvm/Analysis/TargetTransformInfo.h @@ -236,8 +236,6 @@ Cost = getUserCost(I, kind); break; } - if (Cost == -1) - Cost.setInvalid(); return Cost; } @@ -318,12 +316,12 @@ /// /// The returned cost is defined in terms of \c TargetCostConstants, see its /// comments for a detailed explanation of the cost values. - int getUserCost(const User *U, ArrayRef Operands, - TargetCostKind CostKind) const; + InstructionCost getUserCost(const User *U, ArrayRef Operands, + TargetCostKind CostKind) const; /// This is a helper function which calls the two-argument getUserCost /// with \p Operands which are the current operands U has. - int getUserCost(const User *U, TargetCostKind CostKind) const { + InstructionCost getUserCost(const User *U, TargetCostKind CostKind) const { SmallVector Operands(U->operand_values()); return getUserCost(U, Operands, CostKind); } @@ -1364,11 +1362,11 @@ private: /// Estimate the latency of specified instruction. /// Returns 1 as the default value. - int getInstructionLatency(const Instruction *I) const; + InstructionCost getInstructionLatency(const Instruction *I) const; /// Returns the expected throughput cost of the instruction. /// Returns -1 if the cost is unknown. - int getInstructionThroughput(const Instruction *I) const; + InstructionCost getInstructionThroughput(const Instruction *I) const; /// The abstract base class used to type erase specific TTI /// implementations. @@ -1396,8 +1394,9 @@ getEstimatedNumberOfCaseClusters(const SwitchInst &SI, unsigned &JTSize, ProfileSummaryInfo *PSI, BlockFrequencyInfo *BFI) = 0; - virtual int getUserCost(const User *U, ArrayRef Operands, - TargetCostKind CostKind) = 0; + virtual InstructionCost getUserCost(const User *U, + ArrayRef Operands, + TargetCostKind CostKind) = 0; virtual bool hasBranchDivergence() = 0; virtual bool useGPUDivergenceAnalysis() = 0; virtual bool isSourceOfDivergence(const Value *V) = 0; @@ -1652,7 +1651,7 @@ virtual unsigned getGISelRematGlobalCost() const = 0; virtual bool supportsScalableVectors() const = 0; virtual bool hasActiveVectorLength() const = 0; - virtual int getInstructionLatency(const Instruction *I) = 0; + virtual InstructionCost getInstructionLatency(const Instruction *I) = 0; }; template @@ -1684,8 +1683,8 @@ int getMemcpyCost(const Instruction *I) override { return Impl.getMemcpyCost(I); } - int getUserCost(const User *U, ArrayRef Operands, - TargetCostKind CostKind) override { + InstructionCost getUserCost(const User *U, ArrayRef Operands, + TargetCostKind CostKind) override { return Impl.getUserCost(U, Operands, CostKind); } bool hasBranchDivergence() override { return Impl.hasBranchDivergence(); } @@ -2202,7 +2201,7 @@ return Impl.hasActiveVectorLength(); } - int getInstructionLatency(const Instruction *I) override { + InstructionCost getInstructionLatency(const Instruction *I) override { return Impl.getInstructionLatency(I); } }; diff --git a/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h b/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h --- a/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h +++ b/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h @@ -883,8 +883,8 @@ return TTI::TCC_Basic; } - int getUserCost(const User *U, ArrayRef Operands, - TTI::TargetCostKind CostKind) { + InstructionCost getUserCost(const User *U, ArrayRef Operands, + TTI::TargetCostKind CostKind) { auto *TargetTTI = static_cast(this); // Handle non-intrinsic calls, invokes, and callbr. // FIXME: Unlikely to be true for anything but CodeSize. @@ -1025,7 +1025,9 @@ return TargetTTI->getShuffleCost(TTI::SK_ExtractSubvector, VecSrcTy, SubIndex, VecTy); else if (Shuffle->changesLength()) - return CostKind == TTI::TCK_RecipThroughput ? -1 : 1; + return CostKind == TTI::TCK_RecipThroughput + ? InstructionCost::getInvalid() + : 1; else if (Shuffle->isIdentity()) return 0; else if (Shuffle->isReverse()) @@ -1081,7 +1083,7 @@ return TTI::TCC_Basic; } - int getInstructionLatency(const Instruction *I) { + InstructionCost getInstructionLatency(const Instruction *I) { SmallVector Operands(I->operand_values()); if (getUserCost(I, Operands, TTI::TCK_Latency) == TTI::TCC_Free) return 0; diff --git a/llvm/include/llvm/CodeGen/BasicTTIImpl.h b/llvm/include/llvm/CodeGen/BasicTTIImpl.h --- a/llvm/include/llvm/CodeGen/BasicTTIImpl.h +++ b/llvm/include/llvm/CodeGen/BasicTTIImpl.h @@ -517,7 +517,7 @@ SimplifyAndSetOp); } - int getInstructionLatency(const Instruction *I) { + InstructionCost getInstructionLatency(const Instruction *I) { if (isa(I)) return getST()->getSchedModel().DefaultLoadLatency; diff --git a/llvm/include/llvm/Support/InstructionCost.h b/llvm/include/llvm/Support/InstructionCost.h --- a/llvm/include/llvm/Support/InstructionCost.h +++ b/llvm/include/llvm/Support/InstructionCost.h @@ -210,6 +210,16 @@ void print(raw_ostream &OS) const; }; +inline bool operator==(const InstructionCost::CostType LHS, + const InstructionCost &RHS) { + return RHS == LHS; +} + +inline bool operator!=(const InstructionCost::CostType LHS, + const InstructionCost &RHS) { + return RHS != LHS; +} + inline InstructionCost operator+(const InstructionCost &LHS, const InstructionCost &RHS) { InstructionCost LHS2(LHS); diff --git a/llvm/lib/Analysis/TargetTransformInfo.cpp b/llvm/lib/Analysis/TargetTransformInfo.cpp --- a/llvm/lib/Analysis/TargetTransformInfo.cpp +++ b/llvm/lib/Analysis/TargetTransformInfo.cpp @@ -219,10 +219,11 @@ return TTIImpl->getEstimatedNumberOfCaseClusters(SI, JTSize, PSI, BFI); } -int TargetTransformInfo::getUserCost(const User *U, - ArrayRef Operands, - enum TargetCostKind CostKind) const { - int Cost = TTIImpl->getUserCost(U, Operands, CostKind); +InstructionCost +TargetTransformInfo::getUserCost(const User *U, + ArrayRef Operands, + enum TargetCostKind CostKind) const { + InstructionCost Cost = TTIImpl->getUserCost(U, Operands, CostKind); assert((CostKind == TTI::TCK_RecipThroughput || Cost >= 0) && "TTI should not produce negative costs!"); return Cost; @@ -1026,7 +1027,8 @@ return TTIImpl->supportsScalableVectors(); } -int TargetTransformInfo::getInstructionLatency(const Instruction *I) const { +InstructionCost +TargetTransformInfo::getInstructionLatency(const Instruction *I) const { return TTIImpl->getInstructionLatency(I); } @@ -1315,7 +1317,8 @@ return matchPairwiseReduction(Root, Opcode, Ty); } -int TargetTransformInfo::getInstructionThroughput(const Instruction *I) const { +InstructionCost +TargetTransformInfo::getInstructionThroughput(const Instruction *I) const { TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput; switch (I->getOpcode()) { @@ -1368,7 +1371,7 @@ return getUserCost(I, CostKind); default: // We don't have any information on this instruction. - return -1; + return InstructionCost::getInvalid(); } } diff --git a/llvm/lib/Target/ARM/ARMTargetTransformInfo.cpp b/llvm/lib/Target/ARM/ARMTargetTransformInfo.cpp --- a/llvm/lib/Target/ARM/ARMTargetTransformInfo.cpp +++ b/llvm/lib/Target/ARM/ARMTargetTransformInfo.cpp @@ -2152,7 +2152,7 @@ // Scan the loop: don't unroll loops with calls as this could prevent // inlining. - unsigned Cost = 0; + InstructionCost Cost = 0; for (auto *BB : L->getBlocks()) { for (auto &I : *BB) { // Don't unroll vectorised loop. MVE does not benefit from it as much as diff --git a/llvm/lib/Target/Hexagon/HexagonTargetTransformInfo.h b/llvm/lib/Target/Hexagon/HexagonTargetTransformInfo.h --- a/llvm/lib/Target/Hexagon/HexagonTargetTransformInfo.h +++ b/llvm/lib/Target/Hexagon/HexagonTargetTransformInfo.h @@ -162,8 +162,8 @@ /// @} - int getUserCost(const User *U, ArrayRef Operands, - TTI::TargetCostKind CostKind); + InstructionCost getUserCost(const User *U, ArrayRef Operands, + TTI::TargetCostKind CostKind); // Hexagon specific decision to generate a lookup table. bool shouldBuildLookupTables() const; diff --git a/llvm/lib/Target/Hexagon/HexagonTargetTransformInfo.cpp b/llvm/lib/Target/Hexagon/HexagonTargetTransformInfo.cpp --- a/llvm/lib/Target/Hexagon/HexagonTargetTransformInfo.cpp +++ b/llvm/lib/Target/Hexagon/HexagonTargetTransformInfo.cpp @@ -321,10 +321,9 @@ return ST.getL1CacheLineSize(); } -int -HexagonTTIImpl::getUserCost(const User *U, - ArrayRef Operands, - TTI::TargetCostKind CostKind) { +InstructionCost HexagonTTIImpl::getUserCost(const User *U, + ArrayRef Operands, + TTI::TargetCostKind CostKind) { auto isCastFoldedIntoLoad = [this](const CastInst *CI) -> bool { if (!CI->isIntegerCast()) return false; diff --git a/llvm/lib/Target/PowerPC/PPCTargetTransformInfo.h b/llvm/lib/Target/PowerPC/PPCTargetTransformInfo.h --- a/llvm/lib/Target/PowerPC/PPCTargetTransformInfo.h +++ b/llvm/lib/Target/PowerPC/PPCTargetTransformInfo.h @@ -57,8 +57,8 @@ int getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx, const APInt &Imm, Type *Ty, TTI::TargetCostKind CostKind); - unsigned getUserCost(const User *U, ArrayRef Operands, - TTI::TargetCostKind CostKind); + InstructionCost getUserCost(const User *U, ArrayRef Operands, + TTI::TargetCostKind CostKind); TTI::PopcntSupportKind getPopcntSupport(unsigned TyWidth); bool isHardwareLoopProfitable(Loop *L, ScalarEvolution &SE, diff --git a/llvm/lib/Target/PowerPC/PPCTargetTransformInfo.cpp b/llvm/lib/Target/PowerPC/PPCTargetTransformInfo.cpp --- a/llvm/lib/Target/PowerPC/PPCTargetTransformInfo.cpp +++ b/llvm/lib/Target/PowerPC/PPCTargetTransformInfo.cpp @@ -318,9 +318,9 @@ return PPCTTIImpl::getIntImmCost(Imm, Ty, CostKind); } -unsigned -PPCTTIImpl::getUserCost(const User *U, ArrayRef Operands, - TTI::TargetCostKind CostKind) { +InstructionCost PPCTTIImpl::getUserCost(const User *U, + ArrayRef Operands, + TTI::TargetCostKind CostKind) { // We already implement getCastInstrCost and getMemoryOpCost where we perform // the vector adjustment there. if (isa(U) || isa(U) || isa(U))