[llvm] Preserve inbound of transformed GEPs (PR #86885)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 27 15:54:39 PDT 2024
https://github.com/AtariDreams updated https://github.com/llvm/llvm-project/pull/86885
>From 4b5006c268b167eff29b0df5587e4f99a97c7972 Mon Sep 17 00:00:00 2001
From: Rose <gfunni234 at gmail.com>
Date: Wed, 27 Mar 2024 17:08:16 -0400
Subject: [PATCH] Preserve inbound of transformed GEPs
---
.../InstCombine/InstructionCombining.cpp | 16 ++++++++++++++++
llvm/lib/Transforms/Scalar/LoopFlatten.cpp | 5 +++--
llvm/lib/Transforms/Scalar/NaryReassociate.cpp | 5 ++---
llvm/lib/Transforms/Scalar/Scalarizer.cpp | 10 ++++------
.../Scalar/SeparateConstOffsetFromGEP.cpp | 8 ++++----
.../Transforms/LoopFlatten/loop-flatten-gep.ll | 6 +++---
6 files changed, 32 insertions(+), 18 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 7c40fb4fc86082..0f1c9aea32e6d5 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2895,6 +2895,12 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
// as:
// %newptr = getelementptr i32, ptr %ptr, i64 %idx1
// %newgep = getelementptr i32, ptr %newptr, i64 %idx2
+ if (GEP.isInBounds()) {
+ auto *NewPtr = Builder.CreateInBoundsGEP(GEP.getResultElementType(),
+ GEP.getPointerOperand(), Idx1);
+ return GetElementPtrInst::CreateInBounds(GEP.getResultElementType(),
+ NewPtr, Idx2);
+ }
auto *NewPtr = Builder.CreateGEP(GEP.getResultElementType(),
GEP.getPointerOperand(), Idx1);
return GetElementPtrInst::Create(GEP.getResultElementType(), NewPtr,
@@ -2909,6 +2915,16 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
// as:
// %newptr = getelementptr i32, ptr %ptr, i32 %idx1
// %newgep = getelementptr i32, ptr %newptr, i32 idx2
+
+ if (GEP.isInBounds()) {
+ auto *NewPtr = Builder.CreateInBoundsGEP(
+ GEP.getResultElementType(), GEP.getPointerOperand(),
+ Builder.CreateSExt(Idx1, GEP.getOperand(1)->getType()));
+ return GetElementPtrInst::CreateInBounds(
+ GEP.getResultElementType(), NewPtr,
+ Builder.CreateSExt(C, GEP.getOperand(1)->getType()));
+ }
+
auto *NewPtr = Builder.CreateGEP(
GEP.getResultElementType(), GEP.getPointerOperand(),
Builder.CreateSExt(Idx1, GEP.getOperand(1)->getType()));
diff --git a/llvm/lib/Transforms/Scalar/LoopFlatten.cpp b/llvm/lib/Transforms/Scalar/LoopFlatten.cpp
index 0e9cf328f149be..9316333aa2dd6d 100644
--- a/llvm/lib/Transforms/Scalar/LoopFlatten.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopFlatten.cpp
@@ -808,8 +808,9 @@ static bool DoFlattenLoopPair(FlattenInfo &FI, DominatorTree *DT, LoopInfo *LI,
// we need to insert the new GEP where the old GEP was.
if (!DT->dominates(Base, &*Builder.GetInsertPoint()))
Builder.SetInsertPoint(cast<Instruction>(V));
- OuterValue = Builder.CreateGEP(GEP->getSourceElementType(), Base,
- OuterValue, "flatten." + V->getName());
+ OuterValue =
+ Builder.CreateGEP(GEP->getSourceElementType(), Base, OuterValue,
+ "flatten." + V->getName(), GEP->isInBounds());
}
LLVM_DEBUG(dbgs() << "Replacing: "; V->dump(); dbgs() << "with: ";
diff --git a/llvm/lib/Transforms/Scalar/NaryReassociate.cpp b/llvm/lib/Transforms/Scalar/NaryReassociate.cpp
index 308622615332f8..f6d3978326235a 100644
--- a/llvm/lib/Transforms/Scalar/NaryReassociate.cpp
+++ b/llvm/lib/Transforms/Scalar/NaryReassociate.cpp
@@ -456,9 +456,8 @@ NaryReassociatePass::tryReassociateGEPAtIndex(GetElementPtrInst *GEP,
RHS = Builder.CreateMul(
RHS, ConstantInt::get(PtrIdxTy, IndexedSize / ElementSize));
}
- GetElementPtrInst *NewGEP = cast<GetElementPtrInst>(
- Builder.CreateGEP(GEP->getResultElementType(), Candidate, RHS));
- NewGEP->setIsInBounds(GEP->isInBounds());
+ GetElementPtrInst *NewGEP = cast<GetElementPtrInst>(Builder.CreateGEP(
+ GEP->getResultElementType(), Candidate, RHS, "", GEP->isInBounds()));
NewGEP->takeName(GEP);
return NewGEP;
}
diff --git a/llvm/lib/Transforms/Scalar/Scalarizer.cpp b/llvm/lib/Transforms/Scalar/Scalarizer.cpp
index 3eca9ac7c2673b..c92ea83a49e8f6 100644
--- a/llvm/lib/Transforms/Scalar/Scalarizer.cpp
+++ b/llvm/lib/Transforms/Scalar/Scalarizer.cpp
@@ -845,12 +845,10 @@ bool ScalarizerVisitor::visitGetElementPtrInst(GetElementPtrInst &GEPI) {
else
SplitOps[J] = ScatterOps[J][I];
}
- Res[I] = Builder.CreateGEP(GEPI.getSourceElementType(), SplitOps[0],
- ArrayRef(SplitOps).drop_front(),
- GEPI.getName() + ".i" + Twine(I));
- if (GEPI.isInBounds())
- if (GetElementPtrInst *NewGEPI = dyn_cast<GetElementPtrInst>(Res[I]))
- NewGEPI->setIsInBounds();
+ Res[I] =
+ Builder.CreateGEP(GEPI.getSourceElementType(), SplitOps[0],
+ ArrayRef(SplitOps).drop_front(),
+ GEPI.getName() + ".i" + Twine(I), GEPI.isInBounds());
}
gather(&GEPI, Res, *VS);
return true;
diff --git a/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp b/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
index c54a956fc7e243..789cf580e3ced4 100644
--- a/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
+++ b/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
@@ -1020,11 +1020,11 @@ bool SeparateConstOffsetFromGEP::reorderGEP(GetElementPtrInst *GEP,
// For trivial GEP chains, we can swap the indicies.
auto NewSrc = Builder.CreateGEP(PtrGEPType, PtrGEP->getPointerOperand(),
- SmallVector<Value *, 4>(GEP->indices()));
- cast<GetElementPtrInst>(NewSrc)->setIsInBounds(IsChainInBounds);
+ SmallVector<Value *, 4>(GEP->indices()), "",
+ IsChainInBounds);
auto NewGEP = Builder.CreateGEP(GEPType, NewSrc,
- SmallVector<Value *, 4>(PtrGEP->indices()));
- cast<GetElementPtrInst>(NewGEP)->setIsInBounds(IsChainInBounds);
+ SmallVector<Value *, 4>(PtrGEP->indices()),
+ "", IsChainInBounds);
GEP->replaceAllUsesWith(NewGEP);
RecursivelyDeleteTriviallyDeadInstructions(GEP);
return true;
diff --git a/llvm/test/Transforms/LoopFlatten/loop-flatten-gep.ll b/llvm/test/Transforms/LoopFlatten/loop-flatten-gep.ll
index f4b8ea97237fe6..e30001670b1e95 100644
--- a/llvm/test/Transforms/LoopFlatten/loop-flatten-gep.ll
+++ b/llvm/test/Transforms/LoopFlatten/loop-flatten-gep.ll
@@ -15,7 +15,7 @@ for.outer.preheader:
br label %for.inner.preheader
; CHECK-LABEL: for.inner.preheader:
-; CHECK: %flatten.arrayidx = getelementptr i32, ptr %A, i32 %i
+; CHECK: %flatten.arrayidx = getelementptr inbounds i32, ptr %A, i32 %i
for.inner.preheader:
%i = phi i32 [ 0, %for.outer.preheader ], [ %inc2, %for.outer ]
br label %for.inner
@@ -61,13 +61,13 @@ for.outer.preheader:
br label %for.inner.preheader
; CHECK-LABEL: for.inner.preheader:
-; CHECK-NOT: getelementptr i32, ptr %ptr, i32 %i
+; CHECK-NOT: getelementptr inbounds i32, ptr %ptr, i32 %i
for.inner.preheader:
%i = phi i32 [ 0, %for.outer.preheader ], [ %inc2, %for.outer ]
br label %for.inner
; CHECK-LABEL: for.inner:
-; CHECK: %flatten.arrayidx = getelementptr i32, ptr %ptr, i32 %i
+; CHECK: %flatten.arrayidx = getelementptr inbounds i32, ptr %ptr, i32 %i
; CHECK: store i32 0, ptr %flatten.arrayidx, align 4
; CHECK: br label %for.outer
for.inner:
More information about the llvm-commits
mailing list