Index: include/llvm/Analysis/TargetTransformInfo.h =================================================================== --- include/llvm/Analysis/TargetTransformInfo.h +++ include/llvm/Analysis/TargetTransformInfo.h @@ -162,6 +162,13 @@ /// the EXT operation. int getExtCost(const Instruction *I, const Value *Src) const; + /// \brief Estimate the cost of Store operation when lowered + /// + /// The contract for this function is the same as \c getOperationCost. + /// This is the basic query to estimate the cost of Store operation in terms + /// of \c TargetCostConstants. + int getStoreCost(const Instruction *I) const; + /// \brief Estimate the cost of a function call when lowered. /// /// The contract for this is the same as \c getOperationCost except that it @@ -865,6 +872,7 @@ virtual int getGEPCost(Type *PointeeType, const Value *Ptr, ArrayRef Operands) = 0; virtual int getExtCost(const Instruction *I, const Value *Src) = 0; + virtual int getStoreCost(const Instruction *I) = 0; virtual int getCallCost(FunctionType *FTy, int NumArgs) = 0; virtual int getCallCost(const Function *F, int NumArgs) = 0; virtual int getCallCost(const Function *F, @@ -1043,6 +1051,9 @@ int getExtCost(const Instruction *I, const Value *Src) override { return Impl.getExtCost(I, Src); } + int getStoreCost(const Instruction *I) override { + return Impl.getStoreCost(I); + } int getCallCost(FunctionType *FTy, int NumArgs) override { return Impl.getCallCost(FTy, NumArgs); } Index: include/llvm/Analysis/TargetTransformInfoImpl.h =================================================================== --- include/llvm/Analysis/TargetTransformInfoImpl.h +++ include/llvm/Analysis/TargetTransformInfoImpl.h @@ -705,6 +705,12 @@ return static_cast(this)->getIntrinsicCost(IID, RetTy, ParamTys); } + unsigned getStoreCost(const Instruction *I) { + return static_cast(this) + ->getOperationCost(Instruction::Store, I->getType(), + I->getOperand(0)->getType()); + } + unsigned getUserCost(const User *U, ArrayRef Operands) { if (isa(U)) return TTI::TCC_Free; // Model all PHI nodes as free. @@ -738,6 +744,9 @@ return static_cast(this)->getExtCost(CI, Operands.back()); } + if (isa(U)) + return static_cast(this)->getStoreCost(cast(U)); + return static_cast(this)->getOperationCost( Operator::getOpcode(U), U->getType(), U->getNumOperands() == 1 ? U->getOperand(0)->getType() : nullptr); Index: lib/Analysis/TargetTransformInfo.cpp =================================================================== --- lib/Analysis/TargetTransformInfo.cpp +++ lib/Analysis/TargetTransformInfo.cpp @@ -87,6 +87,10 @@ return TTIImpl->getExtCost(I, Src); } +int TargetTransformInfo::getStoreCost(const Instruction *I) const { + return TTIImpl->getStoreCost(I); +} + int TargetTransformInfo::getIntrinsicCost( Intrinsic::ID IID, Type *RetTy, ArrayRef Arguments) const { int Cost = TTIImpl->getIntrinsicCost(IID, RetTy, Arguments); Index: lib/Target/X86/X86TargetTransformInfo.h =================================================================== --- lib/Target/X86/X86TargetTransformInfo.h +++ lib/Target/X86/X86TargetTransformInfo.h @@ -101,6 +101,8 @@ int getIntImmCost(const APInt &Imm, Type *Ty); + int getStoreCost(const Instruction *I); + int getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm, Type *Ty); int getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm, Type *Ty); Index: lib/Target/X86/X86TargetTransformInfo.cpp =================================================================== --- lib/Target/X86/X86TargetTransformInfo.cpp +++ lib/Target/X86/X86TargetTransformInfo.cpp @@ -2046,6 +2046,18 @@ return X86TTIImpl::getIntImmCost(Imm, Ty); } +int X86TTIImpl::getStoreCost(const Instruction *I) { + assert(isa(I) && "Expected Store Instruction in getStoreCost()"); + Value *Ptr = I->getOperand(1); + // Store operation takes 2 UOps. If the address is simple, without scale*index + // arithmetics, address calculation goes to another port. + if (auto GEP = dyn_cast(Ptr)) { + if (!all_of(GEP->indices(), [](Value *V) { return isa(V); })) + return TTI::TCC_Basic * 2; + } + return TTI::TCC_Basic; +} + // Return an average cost of Gather / Scatter instruction, maybe improved later int X86TTIImpl::getGSVectorCost(unsigned Opcode, Type *SrcVTy, Value *Ptr, unsigned Alignment, unsigned AddressSpace) {