From 15f3e82d4230afb81d908d1fa4b5cc73c5cc238f Mon Sep 17 00:00:00 2001 From: Jingyue Wu Date: Fri, 8 Jul 2016 21:48:05 +0000 Subject: [PATCH] [TTI] Expose TTI::getGEPCost and use it in SLSR and NaryReassociate. NFC. llvm-svn: 274940 --- .../llvm/Analysis/TargetTransformInfoImpl.h | 8 ++-- llvm/include/llvm/CodeGen/BasicTTIImpl.h | 5 +++ llvm/lib/Analysis/TargetTransformInfo.cpp | 5 +++ llvm/lib/Transforms/Scalar/NaryReassociate.cpp | 45 ++++------------------ .../Scalar/StraightLineStrengthReduce.cpp | 45 ++++------------------ 5 files changed, 28 insertions(+), 80 deletions(-) diff --git a/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h b/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h index 2ec48b3..b9baee1 100644 --- a/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h +++ b/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h @@ -102,8 +102,8 @@ public: } } - unsigned getGEPCost(Type *PointeeType, const Value *Ptr, - ArrayRef Operands) { + int getGEPCost(Type *PointeeType, const Value *Ptr, + ArrayRef Operands) { // In the basic model, we just assume that all-constant GEPs will be folded // into their uses via addressing modes. for (unsigned Idx = 0, Size = Operands.size(); Idx != Size; ++Idx) @@ -423,8 +423,8 @@ public: using BaseT::getGEPCost; - unsigned getGEPCost(Type *PointeeType, const Value *Ptr, - ArrayRef Operands) { + int getGEPCost(Type *PointeeType, const Value *Ptr, + ArrayRef Operands) { const GlobalValue *BaseGV = nullptr; if (Ptr != nullptr) { // TODO: will remove this when pointers have an opaque type. diff --git a/llvm/include/llvm/CodeGen/BasicTTIImpl.h b/llvm/include/llvm/CodeGen/BasicTTIImpl.h index 1aa13fb..e4b0aa8 100644 --- a/llvm/include/llvm/CodeGen/BasicTTIImpl.h +++ b/llvm/include/llvm/CodeGen/BasicTTIImpl.h @@ -152,6 +152,11 @@ public: return getTLI()->isTypeLegal(VT); } + int getGEPCost(Type *PointeeType, const Value *Ptr, + ArrayRef Operands) { + return BaseT::getGEPCost(PointeeType, Ptr, Operands); + } + unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy, ArrayRef Arguments) { return BaseT::getIntrinsicCost(IID, RetTy, Arguments); diff --git a/llvm/lib/Analysis/TargetTransformInfo.cpp b/llvm/lib/Analysis/TargetTransformInfo.cpp index 3d118d3..51440f5 100644 --- a/llvm/lib/Analysis/TargetTransformInfo.cpp +++ b/llvm/lib/Analysis/TargetTransformInfo.cpp @@ -71,6 +71,11 @@ unsigned TargetTransformInfo::getInliningThresholdMultiplier() const { return TTIImpl->getInliningThresholdMultiplier(); } +int TargetTransformInfo::getGEPCost(Type *PointeeType, const Value *Ptr, + ArrayRef Operands) const { + return TTIImpl->getGEPCost(PointeeType, Ptr, Operands); +} + int TargetTransformInfo::getIntrinsicCost( Intrinsic::ID IID, Type *RetTy, ArrayRef Arguments) const { int Cost = TTIImpl->getIntrinsicCost(IID, RetTy, Arguments); diff --git a/llvm/lib/Transforms/Scalar/NaryReassociate.cpp b/llvm/lib/Transforms/Scalar/NaryReassociate.cpp index f69c5df..ed754fa 100644 --- a/llvm/lib/Transforms/Scalar/NaryReassociate.cpp +++ b/llvm/lib/Transforms/Scalar/NaryReassociate.cpp @@ -299,49 +299,18 @@ Instruction *NaryReassociate::tryReassociate(Instruction *I) { } } -// FIXME: extract this method into TTI->getGEPCost. static bool isGEPFoldable(GetElementPtrInst *GEP, - const TargetTransformInfo *TTI, - const DataLayout *DL) { - GlobalVariable *BaseGV = nullptr; - int64_t BaseOffset = 0; - bool HasBaseReg = false; - int64_t Scale = 0; - - if (GlobalVariable *GV = dyn_cast(GEP->getPointerOperand())) - BaseGV = GV; - else - HasBaseReg = true; - - gep_type_iterator GTI = gep_type_begin(GEP); - for (auto I = GEP->idx_begin(); I != GEP->idx_end(); ++I, ++GTI) { - if (isa(*GTI)) { - int64_t ElementSize = DL->getTypeAllocSize(GTI.getIndexedType()); - if (ConstantInt *ConstIdx = dyn_cast(*I)) { - BaseOffset += ConstIdx->getSExtValue() * ElementSize; - } else { - // Needs scale register. - if (Scale != 0) { - // No addressing mode takes two scale registers. - return false; - } - Scale = ElementSize; - } - } else { - StructType *STy = cast(*GTI); - uint64_t Field = cast(*I)->getZExtValue(); - BaseOffset += DL->getStructLayout(STy)->getElementOffset(Field); - } - } - - unsigned AddrSpace = GEP->getPointerAddressSpace(); - return TTI->isLegalAddressingMode(GEP->getResultElementType(), BaseGV, - BaseOffset, HasBaseReg, Scale, AddrSpace); + const TargetTransformInfo *TTI) { + SmallVector Indices; + for (auto I = GEP->idx_begin(); I != GEP->idx_end(); ++I) + Indices.push_back(*I); + return TTI->getGEPCost(GEP->getSourceElementType(), GEP->getPointerOperand(), + Indices) == TargetTransformInfo::TCC_Free; } Instruction *NaryReassociate::tryReassociateGEP(GetElementPtrInst *GEP) { // Not worth reassociating GEP if it is foldable. - if (isGEPFoldable(GEP, TTI, DL)) + if (isGEPFoldable(GEP, TTI)) return nullptr; gep_type_iterator GTI = gep_type_begin(*GEP); diff --git a/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp index c9761b7..411033b 100644 --- a/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp +++ b/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp @@ -234,44 +234,13 @@ bool StraightLineStrengthReduce::isBasisFor(const Candidate &Basis, Basis.CandidateKind == C.CandidateKind); } -// TODO: use TTI->getGEPCost. static bool isGEPFoldable(GetElementPtrInst *GEP, - const TargetTransformInfo *TTI, - const DataLayout *DL) { - GlobalVariable *BaseGV = nullptr; - int64_t BaseOffset = 0; - bool HasBaseReg = false; - int64_t Scale = 0; - - if (GlobalVariable *GV = dyn_cast(GEP->getPointerOperand())) - BaseGV = GV; - else - HasBaseReg = true; - - gep_type_iterator GTI = gep_type_begin(GEP); - for (auto I = GEP->idx_begin(); I != GEP->idx_end(); ++I, ++GTI) { - if (isa(*GTI)) { - int64_t ElementSize = DL->getTypeAllocSize(GTI.getIndexedType()); - if (ConstantInt *ConstIdx = dyn_cast(*I)) { - BaseOffset += ConstIdx->getSExtValue() * ElementSize; - } else { - // Needs scale register. - if (Scale != 0) { - // No addressing mode takes two scale registers. - return false; - } - Scale = ElementSize; - } - } else { - StructType *STy = cast(*GTI); - uint64_t Field = cast(*I)->getZExtValue(); - BaseOffset += DL->getStructLayout(STy)->getElementOffset(Field); - } - } - - unsigned AddrSpace = GEP->getPointerAddressSpace(); - return TTI->isLegalAddressingMode(GEP->getResultElementType(), BaseGV, - BaseOffset, HasBaseReg, Scale, AddrSpace); + const TargetTransformInfo *TTI) { + SmallVector Indices; + for (auto I = GEP->idx_begin(); I != GEP->idx_end(); ++I) + Indices.push_back(*I); + return TTI->getGEPCost(GEP->getSourceElementType(), GEP->getPointerOperand(), + Indices) == TargetTransformInfo::TCC_Free; } // Returns whether (Base + Index * Stride) can be folded to an addressing mode. @@ -287,7 +256,7 @@ bool StraightLineStrengthReduce::isFoldable(const Candidate &C, if (C.CandidateKind == Candidate::Add) return isAddFoldable(C.Base, C.Index, C.Stride, TTI); if (C.CandidateKind == Candidate::GEP) - return isGEPFoldable(cast(C.Ins), TTI, DL); + return isGEPFoldable(cast(C.Ins), TTI); return false; } -- 2.7.4