Skip to content

Commit 863c81e

Browse files
committed
[TTI][NFC] Change Index argument in getVectorInstrCost to signed
We currently use an unsigned value for the index, but pass in the value of (unsigned)-1 for unknown indices. This patch changes the type to an int so that we no longer need the cast to unsigned. Restricting the range of indices to 0..SINT_MAX shouldn't cause any problems as it's large enough. I've added a new isKnownVectorIndex helper function to TargetTransformInfo as an easy way of asking if the index is known, instead of constantly comparing the index against -1.
1 parent 541e511 commit 863c81e

24 files changed

+113
-96
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,7 @@ class TargetTransformInfo {
14431443
/// Index = -1 to indicate that there is no information about the index value.
14441444
LLVM_ABI InstructionCost
14451445
getExtractWithExtendCost(unsigned Opcode, Type *Dst, VectorType *VecTy,
1446-
unsigned Index, TTI::TargetCostKind CostKind) const;
1446+
int Index, TTI::TargetCostKind CostKind) const;
14471447

14481448
/// \return The expected cost of control-flow related instructions such as
14491449
/// Phi, Ret, Br, Switch.
@@ -1465,14 +1465,21 @@ class TargetTransformInfo {
14651465
OperandValueInfo Op2Info = {OK_AnyValue, OP_None},
14661466
const Instruction *I = nullptr) const;
14671467

1468+
enum : int {
1469+
UnknownIndex = -1,
1470+
// This will be expanded in a future patch.
1471+
};
1472+
1473+
static inline bool isKnownVectorIndex(int Index) { return Index >= 0; }
1474+
14681475
/// \return The expected cost of vector Insert and Extract.
14691476
/// Use -1 to indicate that there is no information on the index value.
14701477
/// This is used when the instruction is not available; a typical use
14711478
/// case is to provision the cost of vectorization/scalarization in
14721479
/// vectorizer passes.
14731480
LLVM_ABI InstructionCost getVectorInstrCost(unsigned Opcode, Type *Val,
14741481
TTI::TargetCostKind CostKind,
1475-
unsigned Index = -1,
1482+
int Index = UnknownIndex,
14761483
const Value *Op0 = nullptr,
14771484
const Value *Op1 = nullptr) const;
14781485

@@ -1486,7 +1493,7 @@ class TargetTransformInfo {
14861493
/// of the extract(nullptr if user is not known before vectorization) and
14871494
/// 'Idx' being the extract lane.
14881495
LLVM_ABI InstructionCost getVectorInstrCost(
1489-
unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind, unsigned Index,
1496+
unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind, int Index,
14901497
Value *Scalar,
14911498
ArrayRef<std::tuple<Value *, User *, int>> ScalarUserAndIdx) const;
14921499

@@ -1498,7 +1505,7 @@ class TargetTransformInfo {
14981505
/// exists (e.g., from basic blocks during transformation).
14991506
LLVM_ABI InstructionCost getVectorInstrCost(const Instruction &I, Type *Val,
15001507
TTI::TargetCostKind CostKind,
1501-
unsigned Index = -1) const;
1508+
int Index = UnknownIndex) const;
15021509

15031510
/// \return The expected cost of aggregate inserts and extracts. This is
15041511
/// used when the instruction is not available; a typical use case is to

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ class TargetTransformInfoImplBase {
758758

759759
virtual InstructionCost
760760
getExtractWithExtendCost(unsigned Opcode, Type *Dst, VectorType *VecTy,
761-
unsigned Index, TTI::TargetCostKind CostKind) const {
761+
int Index, TTI::TargetCostKind CostKind) const {
762762
return 1;
763763
}
764764

@@ -781,7 +781,7 @@ class TargetTransformInfoImplBase {
781781

782782
virtual InstructionCost getVectorInstrCost(unsigned Opcode, Type *Val,
783783
TTI::TargetCostKind CostKind,
784-
unsigned Index, const Value *Op0,
784+
int Index, const Value *Op0,
785785
const Value *Op1) const {
786786
return 1;
787787
}
@@ -791,15 +791,15 @@ class TargetTransformInfoImplBase {
791791
/// of the extract(nullptr if user is not known before vectorization) and
792792
/// 'Idx' being the extract lane.
793793
virtual InstructionCost getVectorInstrCost(
794-
unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind, unsigned Index,
794+
unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind, int Index,
795795
Value *Scalar,
796796
ArrayRef<std::tuple<Value *, User *, int>> ScalarUserAndIdx) const {
797797
return 1;
798798
}
799799

800800
virtual InstructionCost getVectorInstrCost(const Instruction &I, Type *Val,
801801
TTI::TargetCostKind CostKind,
802-
unsigned Index) const {
802+
int Index) const {
803803
return 1;
804804
}
805805

@@ -1522,7 +1522,7 @@ class TargetTransformInfoImplCRTPBase : public TargetTransformInfoImplBase {
15221522
auto *IE = dyn_cast<InsertElementInst>(U);
15231523
if (!IE)
15241524
return TTI::TCC_Basic; // FIXME
1525-
unsigned Idx = -1;
1525+
int Idx = TargetTransformInfo::UnknownIndex;
15261526
if (auto *CI = dyn_cast<ConstantInt>(Operands[2]))
15271527
if (CI->getValue().getActiveBits() <= 32)
15281528
Idx = CI->getZExtValue();
@@ -1641,7 +1641,7 @@ class TargetTransformInfoImplCRTPBase : public TargetTransformInfoImplBase {
16411641
auto *EEI = dyn_cast<ExtractElementInst>(U);
16421642
if (!EEI)
16431643
return TTI::TCC_Basic; // FIXME
1644-
unsigned Idx = -1;
1644+
int Idx = TargetTransformInfo::UnknownIndex;
16451645
if (auto *CI = dyn_cast<ConstantInt>(Operands[1]))
16461646
if (CI->getValue().getActiveBits() <= 32)
16471647
Idx = CI->getZExtValue();

llvm/include/llvm/CodeGen/BasicTTIImpl.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,7 +1341,7 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
13411341

13421342
InstructionCost
13431343
getExtractWithExtendCost(unsigned Opcode, Type *Dst, VectorType *VecTy,
1344-
unsigned Index,
1344+
int Index,
13451345
TTI::TargetCostKind CostKind) const override {
13461346
return thisT()->getVectorInstrCost(Instruction::ExtractElement, VecTy,
13471347
CostKind, Index, nullptr, nullptr) +
@@ -1409,8 +1409,8 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
14091409
}
14101410

14111411
InstructionCost getVectorInstrCost(unsigned Opcode, Type *Val,
1412-
TTI::TargetCostKind CostKind,
1413-
unsigned Index, const Value *Op0,
1412+
TTI::TargetCostKind CostKind, int Index,
1413+
const Value *Op0,
14141414
const Value *Op1) const override {
14151415
return getRegUsageForType(Val->getScalarType());
14161416
}
@@ -1420,8 +1420,8 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
14201420
/// of the extract(nullptr if user is not known before vectorization) and
14211421
/// 'Idx' being the extract lane.
14221422
InstructionCost getVectorInstrCost(unsigned Opcode, Type *Val,
1423-
TTI::TargetCostKind CostKind,
1424-
unsigned Index, Value *Scalar,
1423+
TTI::TargetCostKind CostKind, int Index,
1424+
Value *Scalar,
14251425
ArrayRef<std::tuple<Value *, User *, int>>
14261426
ScalarUserAndIdx) const override {
14271427
return thisT()->getVectorInstrCost(Opcode, Val, CostKind, Index, nullptr,
@@ -1430,7 +1430,7 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
14301430

14311431
InstructionCost getVectorInstrCost(const Instruction &I, Type *Val,
14321432
TTI::TargetCostKind CostKind,
1433-
unsigned Index) const override {
1433+
int Index) const override {
14341434
Value *Op0 = nullptr;
14351435
Value *Op1 = nullptr;
14361436
if (auto *IE = dyn_cast<InsertElementInst>(&I)) {

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ InstructionCost TargetTransformInfo::getCastInstrCost(
10581058
}
10591059

10601060
InstructionCost TargetTransformInfo::getExtractWithExtendCost(
1061-
unsigned Opcode, Type *Dst, VectorType *VecTy, unsigned Index,
1061+
unsigned Opcode, Type *Dst, VectorType *VecTy, int Index,
10621062
TTI::TargetCostKind CostKind) const {
10631063
InstructionCost Cost =
10641064
TTIImpl->getExtractWithExtendCost(Opcode, Dst, VecTy, Index, CostKind);
@@ -1088,7 +1088,7 @@ InstructionCost TargetTransformInfo::getCmpSelInstrCost(
10881088
}
10891089

10901090
InstructionCost TargetTransformInfo::getVectorInstrCost(
1091-
unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind, unsigned Index,
1091+
unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind, int Index,
10921092
const Value *Op0, const Value *Op1) const {
10931093
assert((Opcode == Instruction::InsertElement ||
10941094
Opcode == Instruction::ExtractElement) &&
@@ -1100,7 +1100,7 @@ InstructionCost TargetTransformInfo::getVectorInstrCost(
11001100
}
11011101

11021102
InstructionCost TargetTransformInfo::getVectorInstrCost(
1103-
unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind, unsigned Index,
1103+
unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind, int Index,
11041104
Value *Scalar,
11051105
ArrayRef<std::tuple<Value *, User *, int>> ScalarUserAndIdx) const {
11061106
assert((Opcode == Instruction::InsertElement ||
@@ -1115,7 +1115,7 @@ InstructionCost TargetTransformInfo::getVectorInstrCost(
11151115
InstructionCost
11161116
TargetTransformInfo::getVectorInstrCost(const Instruction &I, Type *Val,
11171117
TTI::TargetCostKind CostKind,
1118-
unsigned Index) const {
1118+
int Index) const {
11191119
// FIXME: Assert that Opcode is either InsertElement or ExtractElement.
11201120
// This is mentioned in the interface description and respected by all
11211121
// callers, but never asserted upon.

llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3642,7 +3642,7 @@ InstructionCost AArch64TTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst,
36423642

36433643
InstructionCost
36443644
AArch64TTIImpl::getExtractWithExtendCost(unsigned Opcode, Type *Dst,
3645-
VectorType *VecTy, unsigned Index,
3645+
VectorType *VecTy, int Index,
36463646
TTI::TargetCostKind CostKind) const {
36473647

36483648
// Make sure we were given a valid extend opcode.
@@ -3711,12 +3711,12 @@ InstructionCost AArch64TTIImpl::getCFInstrCost(unsigned Opcode,
37113711
}
37123712

37133713
InstructionCost AArch64TTIImpl::getVectorInstrCostHelper(
3714-
unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind, unsigned Index,
3714+
unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind, int Index,
37153715
bool HasRealUse, const Instruction *I, Value *Scalar,
37163716
ArrayRef<std::tuple<Value *, User *, int>> ScalarUserAndIdx) const {
37173717
assert(Val->isVectorTy() && "This must be a vector type");
37183718

3719-
if (Index != -1U) {
3719+
if (TargetTransformInfo::isKnownVectorIndex(Index)) {
37203720
// Legalize the type.
37213721
std::pair<InstructionCost, MVT> LT = getTypeLegalizationCost(Val);
37223722

@@ -3884,16 +3884,15 @@ InstructionCost AArch64TTIImpl::getVectorInstrCostHelper(
38843884

38853885
InstructionCost AArch64TTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val,
38863886
TTI::TargetCostKind CostKind,
3887-
unsigned Index,
3888-
const Value *Op0,
3887+
int Index, const Value *Op0,
38893888
const Value *Op1) const {
38903889
bool HasRealUse =
38913890
Opcode == Instruction::InsertElement && Op0 && !isa<UndefValue>(Op0);
38923891
return getVectorInstrCostHelper(Opcode, Val, CostKind, Index, HasRealUse);
38933892
}
38943893

38953894
InstructionCost AArch64TTIImpl::getVectorInstrCost(
3896-
unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind, unsigned Index,
3895+
unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind, int Index,
38973896
Value *Scalar,
38983897
ArrayRef<std::tuple<Value *, User *, int>> ScalarUserAndIdx) const {
38993898
return getVectorInstrCostHelper(Opcode, Val, CostKind, Index, false, nullptr,
@@ -3903,7 +3902,7 @@ InstructionCost AArch64TTIImpl::getVectorInstrCost(
39033902
InstructionCost AArch64TTIImpl::getVectorInstrCost(const Instruction &I,
39043903
Type *Val,
39053904
TTI::TargetCostKind CostKind,
3906-
unsigned Index) const {
3905+
int Index) const {
39073906
return getVectorInstrCostHelper(I.getOpcode(), Val, CostKind, Index,
39083907
true /* HasRealUse */, &I);
39093908
}
@@ -4052,10 +4051,13 @@ InstructionCost AArch64TTIImpl::getArithmeticInstrCost(
40524051
// loading the vector from constant pool or in some cases, may also result
40534052
// in scalarization. For now, we are approximating this with the
40544053
// scalarization cost.
4055-
auto ExtractCost = 2 * getVectorInstrCost(Instruction::ExtractElement, Ty,
4056-
CostKind, -1, nullptr, nullptr);
4057-
auto InsertCost = getVectorInstrCost(Instruction::InsertElement, Ty,
4058-
CostKind, -1, nullptr, nullptr);
4054+
auto ExtractCost =
4055+
2 * getVectorInstrCost(Instruction::ExtractElement, Ty, CostKind,
4056+
TargetTransformInfo::UnknownIndex, nullptr,
4057+
nullptr);
4058+
auto InsertCost = getVectorInstrCost(
4059+
Instruction::InsertElement, Ty, CostKind,
4060+
TargetTransformInfo::UnknownIndex, nullptr, nullptr);
40594061
unsigned NElts = cast<FixedVectorType>(Ty)->getNumElements();
40604062
return ExtractCost + InsertCost +
40614063
NElts * getArithmeticInstrCost(Opcode, Ty->getScalarType(),
@@ -4153,9 +4155,11 @@ InstructionCost AArch64TTIImpl::getArithmeticInstrCost(
41534155
// On AArch64, without SVE, vector divisions are expanded
41544156
// into scalar divisions of each pair of elements.
41554157
Cost += getVectorInstrCost(Instruction::ExtractElement, Ty, CostKind,
4156-
-1, nullptr, nullptr);
4157-
Cost += getVectorInstrCost(Instruction::InsertElement, Ty, CostKind, -1,
4158-
nullptr, nullptr);
4158+
TargetTransformInfo::UnknownIndex, nullptr,
4159+
nullptr);
4160+
Cost += getVectorInstrCost(Instruction::InsertElement, Ty, CostKind,
4161+
TargetTransformInfo::UnknownIndex, nullptr,
4162+
nullptr);
41594163
}
41604164

41614165
// TODO: if one of the arguments is scalar, then it's not necessary to
@@ -4186,11 +4190,13 @@ InstructionCost AArch64TTIImpl::getArithmeticInstrCost(
41864190
return LT.first;
41874191
return cast<VectorType>(Ty)->getElementCount().getKnownMinValue() *
41884192
(getArithmeticInstrCost(Opcode, Ty->getScalarType(), CostKind) +
4189-
getVectorInstrCost(Instruction::ExtractElement, Ty, CostKind, -1,
4190-
nullptr, nullptr) *
4193+
getVectorInstrCost(Instruction::ExtractElement, Ty, CostKind,
4194+
TargetTransformInfo::UnknownIndex, nullptr,
4195+
nullptr) *
41914196
2 +
4192-
getVectorInstrCost(Instruction::InsertElement, Ty, CostKind, -1,
4193-
nullptr, nullptr));
4197+
getVectorInstrCost(Instruction::InsertElement, Ty, CostKind,
4198+
TargetTransformInfo::UnknownIndex, nullptr,
4199+
nullptr));
41944200
case ISD::ADD:
41954201
case ISD::XOR:
41964202
case ISD::OR:

llvm/lib/Target/AArch64/AArch64TargetTransformInfo.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class AArch64TTIImpl : public BasicTTIImplBase<AArch64TTIImpl> {
7373
/// of the extract(nullptr if user is not known before vectorization) and
7474
/// 'Idx' being the extract lane.
7575
InstructionCost getVectorInstrCostHelper(
76-
unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind, unsigned Index,
76+
unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind, int Index,
7777
bool HasRealUse, const Instruction *I = nullptr, Value *Scalar = nullptr,
7878
ArrayRef<std::tuple<Value *, User *, int>> ScalarUserAndIdx = {}) const;
7979

@@ -197,30 +197,30 @@ class AArch64TTIImpl : public BasicTTIImplBase<AArch64TTIImpl> {
197197

198198
InstructionCost
199199
getExtractWithExtendCost(unsigned Opcode, Type *Dst, VectorType *VecTy,
200-
unsigned Index,
200+
int Index,
201201
TTI::TargetCostKind CostKind) const override;
202202

203203
InstructionCost getCFInstrCost(unsigned Opcode, TTI::TargetCostKind CostKind,
204204
const Instruction *I = nullptr) const override;
205205

206206
InstructionCost getVectorInstrCost(unsigned Opcode, Type *Val,
207-
TTI::TargetCostKind CostKind,
208-
unsigned Index, const Value *Op0,
207+
TTI::TargetCostKind CostKind, int Index,
208+
const Value *Op0,
209209
const Value *Op1) const override;
210210

211211
/// \param ScalarUserAndIdx encodes the information about extracts from a
212212
/// vector with 'Scalar' being the value being extracted,'User' being the user
213213
/// of the extract(nullptr if user is not known before vectorization) and
214214
/// 'Idx' being the extract lane.
215215
InstructionCost getVectorInstrCost(unsigned Opcode, Type *Val,
216-
TTI::TargetCostKind CostKind,
217-
unsigned Index, Value *Scalar,
216+
TTI::TargetCostKind CostKind, int Index,
217+
Value *Scalar,
218218
ArrayRef<std::tuple<Value *, User *, int>>
219219
ScalarUserAndIdx) const override;
220220

221221
InstructionCost getVectorInstrCost(const Instruction &I, Type *Val,
222222
TTI::TargetCostKind CostKind,
223-
unsigned Index) const override;
223+
int Index) const override;
224224

225225
InstructionCost
226226
getMinMaxReductionCost(Intrinsic::ID IID, VectorType *Ty, FastMathFlags FMF,

llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ GCNTTIImpl::getMinMaxReductionCost(Intrinsic::ID IID, VectorType *Ty,
834834

835835
InstructionCost GCNTTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy,
836836
TTI::TargetCostKind CostKind,
837-
unsigned Index, const Value *Op0,
837+
int Index, const Value *Op0,
838838
const Value *Op1) const {
839839
switch (Opcode) {
840840
case Instruction::ExtractElement:
@@ -853,7 +853,7 @@ InstructionCost GCNTTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy,
853853
// operations, and we don't have to copy into a different register class.
854854

855855
// Dynamic indexing isn't free and is best avoided.
856-
return Index == ~0u ? 2 : 0;
856+
return TargetTransformInfo::isKnownVectorIndex(Index) ? 0 : 2;
857857
}
858858
default:
859859
return BaseT::getVectorInstrCost(Opcode, ValTy, CostKind, Index, Op0, Op1);

llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ class GCNTTIImpl final : public BasicTTIImplBase<GCNTTIImpl> {
169169

170170
using BaseT::getVectorInstrCost;
171171
InstructionCost getVectorInstrCost(unsigned Opcode, Type *ValTy,
172-
TTI::TargetCostKind CostKind,
173-
unsigned Index, const Value *Op0,
172+
TTI::TargetCostKind CostKind, int Index,
173+
const Value *Op0,
174174
const Value *Op1) const override;
175175

176176
bool isReadRegisterSourceOfDivergence(const IntrinsicInst *ReadReg) const;

llvm/lib/Target/AMDGPU/R600TargetTransformInfo.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ InstructionCost R600TTIImpl::getCFInstrCost(unsigned Opcode,
110110

111111
InstructionCost R600TTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy,
112112
TTI::TargetCostKind CostKind,
113-
unsigned Index,
114-
const Value *Op0,
113+
int Index, const Value *Op0,
115114
const Value *Op1) const {
116115
switch (Opcode) {
117116
case Instruction::ExtractElement:
@@ -128,7 +127,7 @@ InstructionCost R600TTIImpl::getVectorInstrCost(unsigned Opcode, Type *ValTy,
128127
// operations, and we don't have to copy into a different register class.
129128

130129
// Dynamic indexing isn't free and is best avoided.
131-
return Index == ~0u ? 2 : 0;
130+
return TargetTransformInfo::isKnownVectorIndex(Index) ? 0 : 2;
132131
}
133132
default:
134133
return BaseT::getVectorInstrCost(Opcode, ValTy, CostKind, Index, Op0, Op1);

0 commit comments

Comments
 (0)