[CodeGen][GlobalISel] Add a getVectorIdxWidth and getVectorIdxLLT. (#131526)
From #106446, this adds a variant of getVectorIdxTy that returns an LLT.
Many uses only look at the width, so a getVectorIdxWidth was added as
the common base.
diff --git a/llvm/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h b/llvm/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h
index 7b0475a..f9dcbeb 100644
--- a/llvm/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h
+++ b/llvm/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h
@@ -1375,10 +1375,9 @@
MachineInstrBuilder buildExtractVectorElementConstant(const DstOp &Res,
const SrcOp &Val,
const int Idx) {
- auto TLI = getMF().getSubtarget().getTargetLowering();
- unsigned VecIdxWidth = TLI->getVectorIdxTy(getDataLayout()).getSizeInBits();
- return buildExtractVectorElement(
- Res, Val, buildConstant(LLT::scalar(VecIdxWidth), Idx));
+ const TargetLowering *TLI = getMF().getSubtarget().getTargetLowering();
+ LLT IdxTy = TLI->getVectorIdxLLT(getDataLayout());
+ return buildExtractVectorElement(Res, Val, buildConstant(IdxTy, Idx));
}
/// Build and insert \p Res = G_EXTRACT_VECTOR_ELT \p Val, \p Idx
diff --git a/llvm/include/llvm/CodeGen/TargetLowering.h b/llvm/include/llvm/CodeGen/TargetLowering.h
index a3fb4e9..053e9d1 100644
--- a/llvm/include/llvm/CodeGen/TargetLowering.h
+++ b/llvm/include/llvm/CodeGen/TargetLowering.h
@@ -416,11 +416,25 @@
return ShiftValueTy;
}
+ /// Returns the type to be used for the index operand vector operations. By
+ /// default we assume it will have the same size as an address space 0
+ /// pointer.
+ virtual unsigned getVectorIdxWidth(const DataLayout &DL) const {
+ return DL.getPointerSizeInBits(0);
+ }
+
/// Returns the type to be used for the index operand of:
/// ISD::INSERT_VECTOR_ELT, ISD::EXTRACT_VECTOR_ELT,
/// ISD::INSERT_SUBVECTOR, and ISD::EXTRACT_SUBVECTOR
- virtual MVT getVectorIdxTy(const DataLayout &DL) const {
- return getPointerTy(DL);
+ MVT getVectorIdxTy(const DataLayout &DL) const {
+ return MVT::getIntegerVT(getVectorIdxWidth(DL));
+ }
+
+ /// Returns the type to be used for the index operand of:
+ /// G_INSERT_VECTOR_ELT, G_EXTRACT_VECTOR_ELT,
+ /// G_INSERT_SUBVECTOR, and G_EXTRACT_SUBVECTOR
+ LLT getVectorIdxLLT(const DataLayout &DL) const {
+ return LLT::scalar(getVectorIdxWidth(DL));
}
/// Returns the type to be used for the EVL/AVL operand of VP nodes:
diff --git a/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp b/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
index b85239e..6014f57 100644
--- a/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
@@ -3174,7 +3174,7 @@
Register Res = getOrCreateVReg(U);
Register Val = getOrCreateVReg(*U.getOperand(0));
Register Elt = getOrCreateVReg(*U.getOperand(1));
- unsigned PreferredVecIdxWidth = TLI->getVectorIdxTy(*DL).getSizeInBits();
+ unsigned PreferredVecIdxWidth = TLI->getVectorIdxWidth(*DL);
Register Idx;
if (auto *CI = dyn_cast<ConstantInt>(U.getOperand(2))) {
if (CI->getBitWidth() != PreferredVecIdxWidth) {
@@ -3200,7 +3200,7 @@
Register Elt = getOrCreateVReg(*U.getOperand(1));
ConstantInt *CI = cast<ConstantInt>(U.getOperand(2));
- unsigned PreferredVecIdxWidth = TLI->getVectorIdxTy(*DL).getSizeInBits();
+ unsigned PreferredVecIdxWidth = TLI->getVectorIdxWidth(*DL);
// Resize Index to preferred index width.
if (CI->getBitWidth() != PreferredVecIdxWidth) {
@@ -3255,7 +3255,7 @@
Register Res = getOrCreateVReg(U);
Register Val = getOrCreateVReg(*U.getOperand(0));
- unsigned PreferredVecIdxWidth = TLI->getVectorIdxTy(*DL).getSizeInBits();
+ unsigned PreferredVecIdxWidth = TLI->getVectorIdxWidth(*DL);
Register Idx;
if (auto *CI = dyn_cast<ConstantInt>(U.getOperand(1))) {
if (CI->getBitWidth() != PreferredVecIdxWidth) {
@@ -3279,7 +3279,7 @@
Register Res = getOrCreateVReg(U);
Register Vec = getOrCreateVReg(*U.getOperand(0));
ConstantInt *CI = cast<ConstantInt>(U.getOperand(1));
- unsigned PreferredVecIdxWidth = TLI->getVectorIdxTy(*DL).getSizeInBits();
+ unsigned PreferredVecIdxWidth = TLI->getVectorIdxWidth(*DL);
// Resize Index to preferred index width.
if (CI->getBitWidth() != PreferredVecIdxWidth) {
diff --git a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
index ed8bd25..622d6d0 100644
--- a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
@@ -4229,7 +4229,7 @@
unsigned NumBits = MemTy.getSizeInBits();
LLT IntTy = LLT::scalar(NumBits);
auto CurrVal = MIRBuilder.buildConstant(IntTy, 0);
- LLT IdxTy = getLLTForMVT(TLI.getVectorIdxTy(MF.getDataLayout()));
+ LLT IdxTy = TLI.getVectorIdxLLT(MF.getDataLayout());
for (unsigned I = 0, E = MemTy.getNumElements(); I < E; ++I) {
auto Elt = MIRBuilder.buildExtractVectorElement(
@@ -6277,7 +6277,7 @@
auto NeutralElement = getNeutralElementForVecReduce(
MI.getOpcode(), MIRBuilder, MoreTy.getElementType());
- LLT IdxTy(TLI.getVectorIdxTy(MIRBuilder.getDataLayout()));
+ LLT IdxTy(TLI.getVectorIdxLLT(MIRBuilder.getDataLayout()));
for (size_t i = OrigTy.getNumElements(), e = MoreTy.getNumElements();
i != e; i++) {
auto Idx = MIRBuilder.buildConstant(IdxTy, i);
diff --git a/llvm/lib/CodeGen/MachineVerifier.cpp b/llvm/lib/CodeGen/MachineVerifier.cpp
index 87d3033..b806790 100644
--- a/llvm/lib/CodeGen/MachineVerifier.cpp
+++ b/llvm/lib/CodeGen/MachineVerifier.cpp
@@ -1993,8 +1993,7 @@
}
auto TLI = MF->getSubtarget().getTargetLowering();
- if (IdxTy.getSizeInBits() !=
- TLI->getVectorIdxTy(MF->getDataLayout()).getFixedSizeInBits()) {
+ if (IdxTy.getSizeInBits() != TLI->getVectorIdxWidth(MF->getDataLayout())) {
report("Index type must match VectorIdxTy", MI);
break;
}
@@ -2023,8 +2022,7 @@
}
auto TLI = MF->getSubtarget().getTargetLowering();
- if (IdxTy.getSizeInBits() !=
- TLI->getVectorIdxTy(MF->getDataLayout()).getFixedSizeInBits()) {
+ if (IdxTy.getSizeInBits() != TLI->getVectorIdxWidth(MF->getDataLayout())) {
report("Index type must match VectorIdxTy", MI);
break;
}
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 0b2d182..d1f92c9 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -7549,7 +7549,7 @@
N1VT.getVectorMinNumElements()) &&
"Extract subvector overflow!");
assert(N2C->getAPIntValue().getBitWidth() ==
- TLI->getVectorIdxTy(getDataLayout()).getFixedSizeInBits() &&
+ TLI->getVectorIdxWidth(getDataLayout()) &&
"Constant index for EXTRACT_SUBVECTOR has an invalid size");
// Trivial extraction.
@@ -7783,7 +7783,7 @@
VT.getVectorMinNumElements()) &&
"Insert subvector overflow!");
assert(N3->getAsAPIntVal().getBitWidth() ==
- TLI->getVectorIdxTy(getDataLayout()).getFixedSizeInBits() &&
+ TLI->getVectorIdxWidth(getDataLayout()) &&
"Constant index for INSERT_SUBVECTOR has an invalid size");
// Trivial insertion.
diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.h b/llvm/lib/Target/AArch64/AArch64ISelLowering.h
index 1987c89..bc0c3a8 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.h
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.h
@@ -616,6 +616,11 @@
}
}
+ unsigned getVectorIdxWidth(const DataLayout &DL) const override {
+ // The VectorIdx type is i64, with both normal and ilp32.
+ return 64;
+ }
+
bool targetShrinkDemandedConstant(SDValue Op, const APInt &DemandedBits,
const APInt &DemandedElts,
TargetLoweringOpt &TLO) const override;
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUISelLowering.cpp b/llvm/lib/Target/AMDGPU/AMDGPUISelLowering.cpp
index ade81f1..281c06f 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUISelLowering.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUISelLowering.cpp
@@ -796,8 +796,8 @@
return EVT::getIntegerVT(Context, 32 * ((Size + 31) / 32));
}
-MVT AMDGPUTargetLowering::getVectorIdxTy(const DataLayout &) const {
- return MVT::i32;
+unsigned AMDGPUTargetLowering::getVectorIdxWidth(const DataLayout &) const {
+ return 32;
}
bool AMDGPUTargetLowering::isSelectSupported(SelectSupportKind SelType) const {
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUISelLowering.h b/llvm/lib/Target/AMDGPU/AMDGPUISelLowering.h
index c74dc79..bb1dae1 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUISelLowering.h
+++ b/llvm/lib/Target/AMDGPU/AMDGPUISelLowering.h
@@ -209,7 +209,7 @@
EVT getTypeForExtReturn(LLVMContext &Context, EVT VT,
ISD::NodeType ExtendKind) const override;
- MVT getVectorIdxTy(const DataLayout &) const override;
+ unsigned getVectorIdxWidth(const DataLayout &) const override;
bool isSelectSupported(SelectSupportKind) const override;
bool isFPImmLegal(const APFloat &Imm, EVT VT,
diff --git a/llvm/lib/Target/SPIRV/SPIRVISelLowering.h b/llvm/lib/Target/SPIRV/SPIRVISelLowering.h
index 77356b7..eb78299 100644
--- a/llvm/lib/Target/SPIRV/SPIRVISelLowering.h
+++ b/llvm/lib/Target/SPIRV/SPIRVISelLowering.h
@@ -43,9 +43,7 @@
// This is to prevent sexts of non-i64 vector indices which are generated
// within general IRTranslator hence type generation for it is omitted.
- MVT getVectorIdxTy(const DataLayout &DL) const override {
- return MVT::getIntegerVT(32);
- }
+ unsigned getVectorIdxWidth(const DataLayout &DL) const override { return 32; }
unsigned getNumRegistersForCallingConv(LLVMContext &Context,
CallingConv::ID CC,
EVT VT) const override;
diff --git a/llvm/lib/Target/SystemZ/SystemZISelLowering.h b/llvm/lib/Target/SystemZ/SystemZISelLowering.h
index a97962c..86b5f82 100644
--- a/llvm/lib/Target/SystemZ/SystemZISelLowering.h
+++ b/llvm/lib/Target/SystemZ/SystemZISelLowering.h
@@ -430,10 +430,10 @@
MVT getScalarShiftAmountTy(const DataLayout &, EVT) const override {
return MVT::i32;
}
- MVT getVectorIdxTy(const DataLayout &DL) const override {
+ unsigned getVectorIdxWidth(const DataLayout &DL) const override {
// Only the lower 12 bits of an element index are used, so we don't
// want to clobber the upper 32 bits of a GPR unnecessarily.
- return MVT::i32;
+ return 32;
}
TargetLoweringBase::LegalizeTypeAction getPreferredVectorAction(MVT VT)
const override {