[llvm] 9a86d0a - [InstCombine] Prefer source over result element type (NFC)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 17 02:03:08 PDT 2024


Author: Nikita Popov
Date: 2024-06-17T11:03:00+02:00
New Revision: 9a86d0a6b52da4cbeb541b4631baef4ba952dfb8

URL: https://github.com/llvm/llvm-project/commit/9a86d0a6b52da4cbeb541b4631baef4ba952dfb8
DIFF: https://github.com/llvm/llvm-project/commit/9a86d0a6b52da4cbeb541b4631baef4ba952dfb8.diff

LOG: [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.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
    llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 7d26807544d7e..960c5a29569e3 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -485,7 +485,7 @@ Instruction *InstCombinerImpl::foldSelectOpOp(SelectInst &SI, Instruction *TI,
   }
   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 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
     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 0d96d461941c6..6ec9668b2bb3a 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2973,10 +2973,10 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
           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 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
       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));
     }


        


More information about the llvm-commits mailing list