Skip to content

Commit 9303546

Browse files
committed
[CostModel] Unify getMemoryOpCost
Use getMemoryOpCost from the generic implementation of getUserCost and have getInstructionThroughput return the result of that for loads and stores. This also means that the X86 implementation of getUserCost can be removed with the functionality folded into its getMemoryOpCost. Differential Revision: https://reviews.llvm.org/D80984
1 parent 4ffe6bd commit 9303546

File tree

10 files changed

+58
-32
lines changed

10 files changed

+58
-32
lines changed

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,19 @@ class TargetTransformInfoImplCRTPBase : public TargetTransformInfoImplBase {
851851
case Instruction::ZExt:
852852
case Instruction::AddrSpaceCast:
853853
return TargetTTI->getCastInstrCost(Opcode, Ty, OpTy, CostKind, I);
854+
case Instruction::Store: {
855+
auto *SI = cast<StoreInst>(U);
856+
Type *ValTy = U->getOperand(0)->getType();
857+
return TargetTTI->getMemoryOpCost(Opcode, ValTy, SI->getAlign(),
858+
SI->getPointerAddressSpace(),
859+
CostKind, I);
860+
}
861+
case Instruction::Load: {
862+
auto *LI = cast<LoadInst>(U);
863+
return TargetTTI->getMemoryOpCost(Opcode, U->getType(), LI->getAlign(),
864+
LI->getPointerAddressSpace(),
865+
CostKind, I);
866+
}
854867
}
855868
// By default, just classify everything as 'basic'.
856869
return TTI::TCC_Basic;

llvm/include/llvm/CodeGen/BasicTTIImpl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,8 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
888888

889889
// Assuming that all loads of legal types cost 1.
890890
unsigned Cost = LT.first;
891+
if (CostKind != TTI::TCK_RecipThroughput)
892+
return Cost;
891893

892894
if (Src->isVectorTy() &&
893895
Src->getPrimitiveSizeInBits() < LT.second.getSizeInBits()) {

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,17 +1307,9 @@ int TargetTransformInfo::getInstructionThroughput(const Instruction *I) const {
13071307
return getCmpSelInstrCost(I->getOpcode(), ValTy, I->getType(),
13081308
CostKind, I);
13091309
}
1310-
case Instruction::Store: {
1311-
const StoreInst *SI = cast<StoreInst>(I);
1312-
Type *ValTy = SI->getValueOperand()->getType();
1313-
return getMemoryOpCost(I->getOpcode(), ValTy, SI->getAlign(),
1314-
SI->getPointerAddressSpace(), CostKind, I);
1315-
}
1316-
case Instruction::Load: {
1317-
const LoadInst *LI = cast<LoadInst>(I);
1318-
return getMemoryOpCost(I->getOpcode(), I->getType(), LI->getAlign(),
1319-
LI->getPointerAddressSpace(), CostKind, I);
1320-
}
1310+
case Instruction::Store:
1311+
case Instruction::Load:
1312+
return getUserCost(I, CostKind);
13211313
case Instruction::ZExt:
13221314
case Instruction::SExt:
13231315
case Instruction::FPToUI:

llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,10 @@ int AArch64TTIImpl::getMemoryOpCost(unsigned Opcode, Type *Ty,
671671
MaybeAlign Alignment, unsigned AddressSpace,
672672
TTI::TargetCostKind CostKind,
673673
const Instruction *I) {
674+
// TODO: Handle other cost kinds.
675+
if (CostKind != TTI::TCK_RecipThroughput)
676+
return 1;
677+
674678
auto LT = TLI->getTypeLegalizationCost(DL, Ty);
675679

676680
if (ST->isMisaligned128StoreSlow() && Opcode == Instruction::Store &&

llvm/lib/Target/ARM/ARMTargetTransformInfo.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,10 @@ int ARMTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
878878
MaybeAlign Alignment, unsigned AddressSpace,
879879
TTI::TargetCostKind CostKind,
880880
const Instruction *I) {
881+
// TODO: Handle other cost kinds.
882+
if (CostKind != TTI::TCK_RecipThroughput)
883+
return 1;
884+
881885
std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src);
882886

883887
if (ST->hasNEON() && Src->isVectorTy() &&

llvm/lib/Target/Hexagon/HexagonTargetTransformInfo.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ unsigned HexagonTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
152152
TTI::TargetCostKind CostKind,
153153
const Instruction *I) {
154154
assert(Opcode == Instruction::Load || Opcode == Instruction::Store);
155+
// TODO: Handle other cost kinds.
156+
if (CostKind != TTI::TCK_RecipThroughput)
157+
return 1;
158+
155159
if (Opcode == Instruction::Store)
156160
return BaseT::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace,
157161
CostKind, I);

llvm/lib/Target/PowerPC/PPCTargetTransformInfo.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,12 @@ int PPCTTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx,
212212
unsigned
213213
PPCTTIImpl::getUserCost(const User *U, ArrayRef<const Value *> Operands,
214214
TTI::TargetCostKind CostKind) {
215-
// We already implement getCastInstrCost and perform the vector adjustment there.
216-
if (!isa<CastInst>(U) && U->getType()->isVectorTy()) {
215+
// We already implement getCastInstrCost and getMemoryOpCost where we perform
216+
// the vector adjustment there.
217+
if (isa<CastInst>(U) || isa<LoadInst>(U) || isa<StoreInst>(U))
218+
return BaseT::getUserCost(U, Operands, CostKind);
219+
220+
if (U->getType()->isVectorTy()) {
217221
// Instructions that need to be split should cost more.
218222
std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, U->getType());
219223
return LT.first * BaseT::getUserCost(U, Operands, CostKind);
@@ -862,6 +866,10 @@ int PPCTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
862866

863867
int Cost = BaseT::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace,
864868
CostKind);
869+
// TODO: Handle other cost kinds.
870+
if (CostKind != TTI::TCK_RecipThroughput)
871+
return Cost;
872+
865873
Cost = vectorCostAdjustment(Cost, Opcode, Src, nullptr);
866874

867875
bool IsAltivecType = ST->hasAltivec() &&

llvm/lib/Target/SystemZ/SystemZTargetTransformInfo.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,10 @@ int SystemZTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
10291029
const Instruction *I) {
10301030
assert(!Src->isVoidTy() && "Invalid type");
10311031

1032+
// TODO: Handle other cost kinds.
1033+
if (CostKind != TTI::TCK_RecipThroughput)
1034+
return 1;
1035+
10321036
if (!Src->isVectorTy() && Opcode == Instruction::Load && I != nullptr) {
10331037
// Store the load or its truncated or extended value in FoldedValue.
10341038
const Instruction *FoldedValue = nullptr;

llvm/lib/Target/X86/X86TargetTransformInfo.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2961,6 +2961,20 @@ int X86TTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
29612961
MaybeAlign Alignment, unsigned AddressSpace,
29622962
TTI::TargetCostKind CostKind,
29632963
const Instruction *I) {
2964+
// TODO: Handle other cost kinds.
2965+
if (CostKind != TTI::TCK_RecipThroughput) {
2966+
if (isa_and_nonnull<StoreInst>(I)) {
2967+
Value *Ptr = I->getOperand(1);
2968+
// Store instruction with index and scale costs 2 Uops.
2969+
// Check the preceding GEP to identify non-const indices.
2970+
if (auto *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
2971+
if (!all_of(GEP->indices(), [](Value *V) { return isa<Constant>(V); }))
2972+
return TTI::TCC_Basic * 2;
2973+
}
2974+
}
2975+
return TTI::TCC_Basic;
2976+
}
2977+
29642978
// Handle non-power-of-two vectors such as <3 x float>
29652979
if (VectorType *VTy = dyn_cast<VectorType>(Src)) {
29662980
unsigned NumElem = VTy->getNumElements();
@@ -3807,22 +3821,6 @@ int X86TTIImpl::getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx,
38073821
return X86TTIImpl::getIntImmCost(Imm, Ty, CostKind);
38083822
}
38093823

3810-
unsigned
3811-
X86TTIImpl::getUserCost(const User *U, ArrayRef<const Value *> Operands,
3812-
TTI::TargetCostKind CostKind) {
3813-
if (isa<StoreInst>(U)) {
3814-
Value *Ptr = U->getOperand(1);
3815-
// Store instruction with index and scale costs 2 Uops.
3816-
// Check the preceding GEP to identify non-const indices.
3817-
if (auto GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
3818-
if (!all_of(GEP->indices(), [](Value *V) { return isa<Constant>(V); }))
3819-
return TTI::TCC_Basic * 2;
3820-
}
3821-
return TTI::TCC_Basic;
3822-
}
3823-
return BaseT::getUserCost(U, Operands, CostKind);
3824-
}
3825-
38263824
// Return an average cost of Gather / Scatter instruction, maybe improved later
38273825
int X86TTIImpl::getGSVectorCost(unsigned Opcode, Type *SrcVTy, Value *Ptr,
38283826
unsigned Alignment, unsigned AddressSpace) {

llvm/lib/Target/X86/X86TargetTransformInfo.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,6 @@ class X86TTIImpl : public BasicTTIImplBase<X86TTIImpl> {
191191

192192
int getIntImmCost(const APInt &Imm, Type *Ty, TTI::TargetCostKind CostKind);
193193

194-
unsigned getUserCost(const User *U, ArrayRef<const Value *> Operands,
195-
TTI::TargetCostKind);
196-
197194
int getIntImmCostInst(unsigned Opcode, unsigned Idx, const APInt &Imm, Type *Ty,
198195
TTI::TargetCostKind CostKind);
199196
int getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,

0 commit comments

Comments
 (0)