Skip to content

Commit e0c1e23

Browse files
authored
[TTI] Constify BasicTTIImplBase::thisT() (NFCI) (llvm#136575)
The main change is making `thisT` method `const`, the rest of the changes is fixing compilation errors (*). (*) There are two tricky methods, `getVectorInstrCost()` and `getIntImmCost()`. They have several overloads; some of these overloads are typically pulled in to derived classes using the `using` directive, and then hidden by methods in the derived class. The compiler does not complain if the hiding methods are not marked as `const`, which means that clients will use the methods from the base class. If after this change your target fails cost model tests, this must be the reason. To resolve the issue you need to make all hiding overloads `const`. See the second commit in this PR. Pull Request: llvm#136575
1 parent c873ca2 commit e0c1e23

26 files changed

+421
-385
lines changed

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class TargetTransformInfoImplBase {
4747

4848
const DataLayout &getDataLayout() const { return DL; }
4949

50+
// FIXME: It looks like this implementation is dead. All clients appear to
51+
// use the (non-const) version from `TargetTransformInfoImplCRTPBase`.
5052
InstructionCost getGEPCost(Type *PointeeType, const Value *Ptr,
5153
ArrayRef<const Value *> Operands, Type *AccessType,
5254
TTI::TargetCostKind CostKind) const {

llvm/include/llvm/CodeGen/BasicTTIImpl.h

+50-44
Large diffs are not rendered by default.

llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp

+24-23
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ bool AArch64TTIImpl::shouldMaximizeVectorBandwidth(
371371
/// Calculate the cost of materializing a 64-bit value. This helper
372372
/// method might only calculate a fraction of a larger immediate. Therefore it
373373
/// is valid to return a cost of ZERO.
374-
InstructionCost AArch64TTIImpl::getIntImmCost(int64_t Val) {
374+
InstructionCost AArch64TTIImpl::getIntImmCost(int64_t Val) const {
375375
// Check if the immediate can be encoded within an instruction.
376376
if (Val == 0 || AArch64_AM::isLogicalImmediate(Val, 64))
377377
return 0;
@@ -386,8 +386,9 @@ InstructionCost AArch64TTIImpl::getIntImmCost(int64_t Val) {
386386
}
387387

388388
/// Calculate the cost of materializing the given constant.
389-
InstructionCost AArch64TTIImpl::getIntImmCost(const APInt &Imm, Type *Ty,
390-
TTI::TargetCostKind CostKind) {
389+
InstructionCost
390+
AArch64TTIImpl::getIntImmCost(const APInt &Imm, Type *Ty,
391+
TTI::TargetCostKind CostKind) const {
391392
assert(Ty->isIntegerTy());
392393

393394
unsigned BitSize = Ty->getPrimitiveSizeInBits();
@@ -577,7 +578,7 @@ static InstructionCost getHistogramCost(const IntrinsicCostAttributes &ICA) {
577578

578579
InstructionCost
579580
AArch64TTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
580-
TTI::TargetCostKind CostKind) {
581+
TTI::TargetCostKind CostKind) const {
581582
// The code-generator is currently not able to handle scalable vectors
582583
// of <vscale x 1 x eltty> yet, so return an invalid cost to avoid selecting
583584
// it. This change will be removed when code-generation for these types is
@@ -2806,7 +2807,7 @@ AArch64TTIImpl::getRegisterBitWidth(TargetTransformInfo::RegisterKind K) const {
28062807

28072808
bool AArch64TTIImpl::isWideningInstruction(Type *DstTy, unsigned Opcode,
28082809
ArrayRef<const Value *> Args,
2809-
Type *SrcOverrideTy) {
2810+
Type *SrcOverrideTy) const {
28102811
// A helper that returns a vector type from the given type. The number of
28112812
// elements in type Ty determines the vector width.
28122813
auto toVectorTy = [&](Type *ArgTy) {
@@ -2903,7 +2904,7 @@ bool AArch64TTIImpl::isWideningInstruction(Type *DstTy, unsigned Opcode,
29032904
// trunc i16 (lshr (add %x, %y), 1) -> i8
29042905
//
29052906
bool AArch64TTIImpl::isExtPartOfAvgExpr(const Instruction *ExtUser, Type *Dst,
2906-
Type *Src) {
2907+
Type *Src) const {
29072908
// The source should be a legal vector type.
29082909
if (!Src->isVectorTy() || !TLI->isTypeLegal(TLI->getValueType(DL, Src)) ||
29092910
(Src->isScalableTy() && !ST->hasSVE2()))
@@ -2948,7 +2949,7 @@ InstructionCost AArch64TTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst,
29482949
Type *Src,
29492950
TTI::CastContextHint CCH,
29502951
TTI::TargetCostKind CostKind,
2951-
const Instruction *I) {
2952+
const Instruction *I) const {
29522953
int ISD = TLI->InstructionOpcodeToISD(Opcode);
29532954
assert(ISD && "Invalid opcode");
29542955
// If the cast is observable, and it is used by a widening instruction (e.g.,
@@ -3619,7 +3620,7 @@ InstructionCost AArch64TTIImpl::getExtractWithExtendCost(unsigned Opcode,
36193620

36203621
InstructionCost AArch64TTIImpl::getCFInstrCost(unsigned Opcode,
36213622
TTI::TargetCostKind CostKind,
3622-
const Instruction *I) {
3623+
const Instruction *I) const {
36233624
if (CostKind != TTI::TCK_RecipThroughput)
36243625
return Opcode == Instruction::PHI ? 0 : 1;
36253626
assert(CostKind == TTI::TCK_RecipThroughput && "unexpected CostKind");
@@ -3630,7 +3631,7 @@ InstructionCost AArch64TTIImpl::getCFInstrCost(unsigned Opcode,
36303631
InstructionCost AArch64TTIImpl::getVectorInstrCostHelper(
36313632
unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind, unsigned Index,
36323633
bool HasRealUse, const Instruction *I, Value *Scalar,
3633-
ArrayRef<std::tuple<Value *, User *, int>> ScalarUserAndIdx) {
3634+
ArrayRef<std::tuple<Value *, User *, int>> ScalarUserAndIdx) const {
36343635
assert(Val->isVectorTy() && "This must be a vector type");
36353636

36363637
if (Index != -1U) {
@@ -3802,7 +3803,7 @@ InstructionCost AArch64TTIImpl::getVectorInstrCostHelper(
38023803
InstructionCost AArch64TTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val,
38033804
TTI::TargetCostKind CostKind,
38043805
unsigned Index, Value *Op0,
3805-
Value *Op1) {
3806+
Value *Op1) const {
38063807
bool HasRealUse =
38073808
Opcode == Instruction::InsertElement && Op0 && !isa<UndefValue>(Op0);
38083809
return getVectorInstrCostHelper(Opcode, Val, CostKind, Index, HasRealUse);
@@ -3826,7 +3827,7 @@ InstructionCost AArch64TTIImpl::getVectorInstrCost(const Instruction &I,
38263827

38273828
InstructionCost AArch64TTIImpl::getScalarizationOverhead(
38283829
VectorType *Ty, const APInt &DemandedElts, bool Insert, bool Extract,
3829-
TTI::TargetCostKind CostKind, ArrayRef<Value *> VL) {
3830+
TTI::TargetCostKind CostKind, ArrayRef<Value *> VL) const {
38303831
if (isa<ScalableVectorType>(Ty))
38313832
return InstructionCost::getInvalid();
38323833
if (Ty->getElementType()->isFloatingPointTy())
@@ -3840,8 +3841,7 @@ InstructionCost AArch64TTIImpl::getScalarizationOverhead(
38403841
InstructionCost AArch64TTIImpl::getArithmeticInstrCost(
38413842
unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind,
38423843
TTI::OperandValueInfo Op1Info, TTI::OperandValueInfo Op2Info,
3843-
ArrayRef<const Value *> Args,
3844-
const Instruction *CxtI) {
3844+
ArrayRef<const Value *> Args, const Instruction *CxtI) const {
38453845

38463846
// The code-generator is currently not able to handle scalable vectors
38473847
// of <vscale x 1 x eltty> yet, so return an invalid cost to avoid selecting
@@ -4171,7 +4171,7 @@ InstructionCost AArch64TTIImpl::getAddressComputationCost(Type *Ty,
41714171
InstructionCost AArch64TTIImpl::getCmpSelInstrCost(
41724172
unsigned Opcode, Type *ValTy, Type *CondTy, CmpInst::Predicate VecPred,
41734173
TTI::TargetCostKind CostKind, TTI::OperandValueInfo Op1Info,
4174-
TTI::OperandValueInfo Op2Info, const Instruction *I) {
4174+
TTI::OperandValueInfo Op2Info, const Instruction *I) const {
41754175
// TODO: Handle other cost kinds.
41764176
if (CostKind != TTI::TCK_RecipThroughput)
41774177
return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, VecPred, CostKind,
@@ -4284,7 +4284,7 @@ bool AArch64TTIImpl::prefersVectorizedAddressing() const {
42844284
InstructionCost
42854285
AArch64TTIImpl::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
42864286
Align Alignment, unsigned AddressSpace,
4287-
TTI::TargetCostKind CostKind) {
4287+
TTI::TargetCostKind CostKind) const {
42884288
if (useNeonVector(Src))
42894289
return BaseT::getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace,
42904290
CostKind);
@@ -4331,7 +4331,7 @@ static unsigned getSVEGatherScatterOverhead(unsigned Opcode,
43314331

43324332
InstructionCost AArch64TTIImpl::getGatherScatterOpCost(
43334333
unsigned Opcode, Type *DataTy, const Value *Ptr, bool VariableMask,
4334-
Align Alignment, TTI::TargetCostKind CostKind, const Instruction *I) {
4334+
Align Alignment, TTI::TargetCostKind CostKind, const Instruction *I) const {
43354335
if (useNeonVector(DataTy) || !isLegalMaskedGatherScatter(DataTy))
43364336
return BaseT::getGatherScatterOpCost(Opcode, DataTy, Ptr, VariableMask,
43374337
Alignment, CostKind, I);
@@ -4371,7 +4371,7 @@ InstructionCost AArch64TTIImpl::getMemoryOpCost(unsigned Opcode, Type *Ty,
43714371
unsigned AddressSpace,
43724372
TTI::TargetCostKind CostKind,
43734373
TTI::OperandValueInfo OpInfo,
4374-
const Instruction *I) {
4374+
const Instruction *I) const {
43754375
EVT VT = TLI->getValueType(DL, Ty, true);
43764376
// Type legalization can't handle structs
43774377
if (VT == MVT::Other)
@@ -4980,7 +4980,7 @@ bool AArch64TTIImpl::isLegalToVectorizeReduction(
49804980
InstructionCost
49814981
AArch64TTIImpl::getMinMaxReductionCost(Intrinsic::ID IID, VectorType *Ty,
49824982
FastMathFlags FMF,
4983-
TTI::TargetCostKind CostKind) {
4983+
TTI::TargetCostKind CostKind) const {
49844984
// The code-generator is currently not able to handle scalable vectors
49854985
// of <vscale x 1 x eltty> yet, so return an invalid cost to avoid selecting
49864986
// it. This change will be removed when code-generation for these types is
@@ -5005,7 +5005,7 @@ AArch64TTIImpl::getMinMaxReductionCost(Intrinsic::ID IID, VectorType *Ty,
50055005
}
50065006

50075007
InstructionCost AArch64TTIImpl::getArithmeticReductionCostSVE(
5008-
unsigned Opcode, VectorType *ValTy, TTI::TargetCostKind CostKind) {
5008+
unsigned Opcode, VectorType *ValTy, TTI::TargetCostKind CostKind) const {
50095009
std::pair<InstructionCost, MVT> LT = getTypeLegalizationCost(ValTy);
50105010
InstructionCost LegalizationCost = 0;
50115011
if (LT.first > 1) {
@@ -5032,7 +5032,7 @@ InstructionCost AArch64TTIImpl::getArithmeticReductionCostSVE(
50325032
InstructionCost
50335033
AArch64TTIImpl::getArithmeticReductionCost(unsigned Opcode, VectorType *ValTy,
50345034
std::optional<FastMathFlags> FMF,
5035-
TTI::TargetCostKind CostKind) {
5035+
TTI::TargetCostKind CostKind) const {
50365036
// The code-generator is currently not able to handle scalable vectors
50375037
// of <vscale x 1 x eltty> yet, so return an invalid cost to avoid selecting
50385038
// it. This change will be removed when code-generation for these types is
@@ -5207,8 +5207,9 @@ AArch64TTIImpl::getMulAccReductionCost(bool IsUnsigned, Type *ResTy,
52075207
return BaseT::getMulAccReductionCost(IsUnsigned, ResTy, VecTy, CostKind);
52085208
}
52095209

5210-
InstructionCost AArch64TTIImpl::getSpliceCost(VectorType *Tp, int Index,
5211-
TTI::TargetCostKind CostKind) {
5210+
InstructionCost
5211+
AArch64TTIImpl::getSpliceCost(VectorType *Tp, int Index,
5212+
TTI::TargetCostKind CostKind) const {
52125213
static const CostTblEntry ShuffleTbl[] = {
52135214
{ TTI::SK_Splice, MVT::nxv16i8, 1 },
52145215
{ TTI::SK_Splice, MVT::nxv8i16, 1 },
@@ -5340,7 +5341,7 @@ InstructionCost AArch64TTIImpl::getPartialReductionCost(
53405341
InstructionCost AArch64TTIImpl::getShuffleCost(
53415342
TTI::ShuffleKind Kind, VectorType *Tp, ArrayRef<int> Mask,
53425343
TTI::TargetCostKind CostKind, int Index, VectorType *SubTp,
5343-
ArrayRef<const Value *> Args, const Instruction *CxtI) {
5344+
ArrayRef<const Value *> Args, const Instruction *CxtI) const {
53445345
std::pair<InstructionCost, MVT> LT = getTypeLegalizationCost(Tp);
53455346

53465347
// If we have a Mask, and the LT is being legalized somehow, split the Mask

llvm/lib/Target/AArch64/AArch64TargetTransformInfo.h

+32-28
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class AArch64TTIImpl : public BasicTTIImplBase<AArch64TTIImpl> {
6161

6262
bool isWideningInstruction(Type *DstTy, unsigned Opcode,
6363
ArrayRef<const Value *> Args,
64-
Type *SrcOverrideTy = nullptr);
64+
Type *SrcOverrideTy = nullptr) const;
6565

6666
// A helper function called by 'getVectorInstrCost'.
6767
//
@@ -75,7 +75,7 @@ class AArch64TTIImpl : public BasicTTIImplBase<AArch64TTIImpl> {
7575
InstructionCost getVectorInstrCostHelper(
7676
unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind, unsigned Index,
7777
bool HasRealUse, const Instruction *I = nullptr, Value *Scalar = nullptr,
78-
ArrayRef<std::tuple<Value *, User *, int>> ScalarUserAndIdx = {});
78+
ArrayRef<std::tuple<Value *, User *, int>> ScalarUserAndIdx = {}) const;
7979

8080
public:
8181
explicit AArch64TTIImpl(const AArch64TargetMachine *TM, const Function &F)
@@ -99,9 +99,9 @@ class AArch64TTIImpl : public BasicTTIImplBase<AArch64TTIImpl> {
9999
/// @{
100100

101101
using BaseT::getIntImmCost;
102-
InstructionCost getIntImmCost(int64_t Val);
102+
InstructionCost getIntImmCost(int64_t Val) const;
103103
InstructionCost getIntImmCost(const APInt &Imm, Type *Ty,
104-
TTI::TargetCostKind CostKind);
104+
TTI::TargetCostKind CostKind) const;
105105
InstructionCost getIntImmCostInst(unsigned Opcode, unsigned Idx,
106106
const APInt &Imm, Type *Ty,
107107
TTI::TargetCostKind CostKind,
@@ -131,7 +131,7 @@ class AArch64TTIImpl : public BasicTTIImplBase<AArch64TTIImpl> {
131131
}
132132

133133
InstructionCost getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
134-
TTI::TargetCostKind CostKind);
134+
TTI::TargetCostKind CostKind) const;
135135

136136
std::optional<Instruction *> instCombineIntrinsic(InstCombiner &IC,
137137
IntrinsicInst &II) const;
@@ -173,30 +173,32 @@ class AArch64TTIImpl : public BasicTTIImplBase<AArch64TTIImpl> {
173173

174174
InstructionCost getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
175175
Align Alignment, unsigned AddressSpace,
176-
TTI::TargetCostKind CostKind);
176+
TTI::TargetCostKind CostKind) const;
177177

178178
InstructionCost getGatherScatterOpCost(unsigned Opcode, Type *DataTy,
179179
const Value *Ptr, bool VariableMask,
180180
Align Alignment,
181181
TTI::TargetCostKind CostKind,
182-
const Instruction *I = nullptr);
182+
const Instruction *I = nullptr) const;
183183

184-
bool isExtPartOfAvgExpr(const Instruction *ExtUser, Type *Dst, Type *Src);
184+
bool isExtPartOfAvgExpr(const Instruction *ExtUser, Type *Dst,
185+
Type *Src) const;
185186

186187
InstructionCost getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src,
187188
TTI::CastContextHint CCH,
188189
TTI::TargetCostKind CostKind,
189-
const Instruction *I = nullptr);
190+
const Instruction *I = nullptr) const;
190191

191192
InstructionCost getExtractWithExtendCost(unsigned Opcode, Type *Dst,
192193
VectorType *VecTy, unsigned Index);
193194

194195
InstructionCost getCFInstrCost(unsigned Opcode, TTI::TargetCostKind CostKind,
195-
const Instruction *I = nullptr);
196+
const Instruction *I = nullptr) const;
196197

197198
InstructionCost getVectorInstrCost(unsigned Opcode, Type *Val,
198199
TTI::TargetCostKind CostKind,
199-
unsigned Index, Value *Op0, Value *Op1);
200+
unsigned Index, Value *Op0,
201+
Value *Op1) const;
200202

201203
/// \param ScalarUserAndIdx encodes the information about extracts from a
202204
/// vector with 'Scalar' being the value being extracted,'User' being the user
@@ -213,20 +215,21 @@ class AArch64TTIImpl : public BasicTTIImplBase<AArch64TTIImpl> {
213215

214216
InstructionCost getMinMaxReductionCost(Intrinsic::ID IID, VectorType *Ty,
215217
FastMathFlags FMF,
216-
TTI::TargetCostKind CostKind);
218+
TTI::TargetCostKind CostKind) const;
217219

218-
InstructionCost getArithmeticReductionCostSVE(unsigned Opcode,
219-
VectorType *ValTy,
220-
TTI::TargetCostKind CostKind);
220+
InstructionCost
221+
getArithmeticReductionCostSVE(unsigned Opcode, VectorType *ValTy,
222+
TTI::TargetCostKind CostKind) const;
221223

222224
InstructionCost getSpliceCost(VectorType *Tp, int Index,
223-
TTI::TargetCostKind CostKind);
225+
TTI::TargetCostKind CostKind) const;
224226

225227
InstructionCost getArithmeticInstrCost(
226228
unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind,
227229
TTI::OperandValueInfo Op1Info = {TTI::OK_AnyValue, TTI::OP_None},
228230
TTI::OperandValueInfo Op2Info = {TTI::OK_AnyValue, TTI::OP_None},
229-
ArrayRef<const Value *> Args = {}, const Instruction *CxtI = nullptr);
231+
ArrayRef<const Value *> Args = {},
232+
const Instruction *CxtI = nullptr) const;
230233

231234
InstructionCost getAddressComputationCost(Type *Ty, ScalarEvolution *SE,
232235
const SCEV *Ptr);
@@ -236,17 +239,17 @@ class AArch64TTIImpl : public BasicTTIImplBase<AArch64TTIImpl> {
236239
TTI::TargetCostKind CostKind,
237240
TTI::OperandValueInfo Op1Info = {TTI::OK_AnyValue, TTI::OP_None},
238241
TTI::OperandValueInfo Op2Info = {TTI::OK_AnyValue, TTI::OP_None},
239-
const Instruction *I = nullptr);
242+
const Instruction *I = nullptr) const;
240243

241244
TTI::MemCmpExpansionOptions enableMemCmpExpansion(bool OptSize,
242245
bool IsZeroCmp) const;
243246
bool useNeonVector(const Type *Ty) const;
244247

245-
InstructionCost
246-
getMemoryOpCost(unsigned Opcode, Type *Src, MaybeAlign Alignment,
247-
unsigned AddressSpace, TTI::TargetCostKind CostKind,
248-
TTI::OperandValueInfo OpInfo = {TTI::OK_AnyValue, TTI::OP_None},
249-
const Instruction *I = nullptr);
248+
InstructionCost getMemoryOpCost(
249+
unsigned Opcode, Type *Src, MaybeAlign Alignment, unsigned AddressSpace,
250+
TTI::TargetCostKind CostKind,
251+
TTI::OperandValueInfo OpInfo = {TTI::OK_AnyValue, TTI::OP_None},
252+
const Instruction *I = nullptr) const;
250253

251254
InstructionCost getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys);
252255

@@ -423,9 +426,10 @@ class AArch64TTIImpl : public BasicTTIImplBase<AArch64TTIImpl> {
423426
return ST->hasSVE();
424427
}
425428

426-
InstructionCost getArithmeticReductionCost(unsigned Opcode, VectorType *Ty,
427-
std::optional<FastMathFlags> FMF,
428-
TTI::TargetCostKind CostKind);
429+
InstructionCost
430+
getArithmeticReductionCost(unsigned Opcode, VectorType *Ty,
431+
std::optional<FastMathFlags> FMF,
432+
TTI::TargetCostKind CostKind) const;
429433

430434
InstructionCost getExtendedReductionCost(unsigned Opcode, bool IsUnsigned,
431435
Type *ResTy, VectorType *ValTy,
@@ -441,13 +445,13 @@ class AArch64TTIImpl : public BasicTTIImplBase<AArch64TTIImpl> {
441445
TTI::TargetCostKind CostKind, int Index,
442446
VectorType *SubTp,
443447
ArrayRef<const Value *> Args = {},
444-
const Instruction *CxtI = nullptr);
448+
const Instruction *CxtI = nullptr) const;
445449

446450
InstructionCost getScalarizationOverhead(VectorType *Ty,
447451
const APInt &DemandedElts,
448452
bool Insert, bool Extract,
449453
TTI::TargetCostKind CostKind,
450-
ArrayRef<Value *> VL = {});
454+
ArrayRef<Value *> VL = {}) const;
451455

452456
/// Return the cost of the scaling factor used in the addressing
453457
/// mode represented by AM for this target, for a load/store

0 commit comments

Comments
 (0)