[InstCombine] Prefer source over result element type (NFC)
For single-index GEPs the source and result element types are the
same, but using the source type is semantically more correct.
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 7d26807..960c5a29 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -485,7 +485,7 @@
}
if (auto *TGEP = dyn_cast<GetElementPtrInst>(TI)) {
auto *FGEP = cast<GetElementPtrInst>(FI);
- Type *ElementType = TGEP->getResultElementType();
+ Type *ElementType = TGEP->getSourceElementType();
return TGEP->isInBounds() && FGEP->isInBounds()
? GetElementPtrInst::CreateInBounds(ElementType, Op0, {Op1})
: GetElementPtrInst::Create(ElementType, Op0, {Op1});
@@ -3706,7 +3706,7 @@
Value *Idx = Gep->getOperand(1);
if (isa<VectorType>(CondVal->getType()) && !isa<VectorType>(Idx->getType()))
return nullptr;
- Type *ElementType = Gep->getResultElementType();
+ Type *ElementType = Gep->getSourceElementType();
Value *NewT = Idx;
Value *NewF = Constant::getNullValue(Idx->getType());
if (Swap)
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 0d96d46..6ec9668 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2973,10 +2973,10 @@
cast<OverflowingBinaryOperator>(GEP.getOperand(1))->hasNoSignedWrap(),
Idx1, Idx2);
auto *NewPtr =
- Builder.CreateGEP(GEP.getResultElementType(), GEP.getPointerOperand(),
+ Builder.CreateGEP(GEP.getSourceElementType(), GEP.getPointerOperand(),
Idx1, "", IsInBounds);
return replaceInstUsesWith(
- GEP, Builder.CreateGEP(GEP.getResultElementType(), NewPtr, Idx2, "",
+ GEP, Builder.CreateGEP(GEP.getSourceElementType(), NewPtr, Idx2, "",
IsInBounds));
}
ConstantInt *C;
@@ -2991,12 +2991,12 @@
bool IsInBounds = CanPreserveInBounds(
/*IsNSW=*/true, Idx1, C);
auto *NewPtr = Builder.CreateGEP(
- GEP.getResultElementType(), GEP.getPointerOperand(),
+ GEP.getSourceElementType(), GEP.getPointerOperand(),
Builder.CreateSExt(Idx1, GEP.getOperand(1)->getType()), "",
IsInBounds);
return replaceInstUsesWith(
GEP,
- Builder.CreateGEP(GEP.getResultElementType(), NewPtr,
+ Builder.CreateGEP(GEP.getSourceElementType(), NewPtr,
Builder.CreateSExt(C, GEP.getOperand(1)->getType()),
"", IsInBounds));
}