[llvm-branch-commits] [llvm] eb79fd3 - [InstCombine] Fold gep inbounds of null to null
Nikita Popov via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Dec 23 12:46:29 PST 2020
Author: Nikita Popov
Date: 2020-12-23T21:41:53+01:00
New Revision: eb79fd3c928dbbb97f7937963361c1dad2bf8222
URL: https://github.com/llvm/llvm-project/commit/eb79fd3c928dbbb97f7937963361c1dad2bf8222
DIFF: https://github.com/llvm/llvm-project/commit/eb79fd3c928dbbb97f7937963361c1dad2bf8222.diff
LOG: [InstCombine] Fold gep inbounds of null to null
Effectively, this is what we were previously already doing when
the GEP was used in conjunction with a load or store, but this
fold can also be applied more generally:
> The only in bounds address for a null pointer in the default
> address-space is the null pointer itself.
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
llvm/test/Transforms/InstCombine/getelementptr.ll
llvm/test/Transforms/InstCombine/store.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
index 71f165abe52e..153947802f80 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
@@ -903,29 +903,14 @@ static Instruction *replaceGEPIdxWithZero(InstCombinerImpl &IC, Value *Ptr,
}
static bool canSimplifyNullStoreOrGEP(StoreInst &SI) {
- if (NullPointerIsDefined(SI.getFunction(), SI.getPointerAddressSpace()))
- return false;
-
- auto *Ptr = SI.getPointerOperand();
- if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Ptr))
- if (GEPI->isInBounds())
- Ptr = GEPI->getOperand(0);
- return (isa<ConstantPointerNull>(Ptr) &&
- !NullPointerIsDefined(SI.getFunction(), SI.getPointerAddressSpace()));
+ return isa<ConstantPointerNull>(SI.getPointerOperand()) &&
+ !NullPointerIsDefined(SI.getFunction(), SI.getPointerAddressSpace());
}
static bool canSimplifyNullLoadOrGEP(LoadInst &LI, Value *Op) {
- if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) {
- const Value *GEPI0 = GEPI->getOperand(0);
- if (isa<ConstantPointerNull>(GEPI0) && GEPI->isInBounds() &&
- !NullPointerIsDefined(LI.getFunction(), GEPI->getPointerAddressSpace()))
- return true;
- }
- if (isa<UndefValue>(Op) ||
- (isa<ConstantPointerNull>(Op) &&
- !NullPointerIsDefined(LI.getFunction(), LI.getPointerAddressSpace())))
- return true;
- return false;
+ return isa<UndefValue>(Op) ||
+ (isa<ConstantPointerNull>(Op) &&
+ !NullPointerIsDefined(LI.getFunction(), LI.getPointerAddressSpace()));
}
Instruction *InstCombinerImpl::visitLoadInst(LoadInst &LI) {
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index ec932aaf0b9e..7bde3845522f 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1855,6 +1855,12 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Value *PtrOp = GEP.getOperand(0);
+ // The only pointer that is inbounds of null is null.
+ if (isa<ConstantPointerNull>(PtrOp) && GEP.isInBounds() &&
+ !NullPointerIsDefined(GEP.getFunction(),
+ PtrOp->getType()->getPointerAddressSpace()))
+ return replaceInstUsesWith(GEP, PtrOp);
+
// Eliminate unneeded casts for indices, and replace indices which displace
// by multiples of a zero size type with zero.
bool MadeChange = false;
diff --git a/llvm/test/Transforms/InstCombine/getelementptr.ll b/llvm/test/Transforms/InstCombine/getelementptr.ll
index b4124c5edf5e..6524c3c03639 100644
--- a/llvm/test/Transforms/InstCombine/getelementptr.ll
+++ b/llvm/test/Transforms/InstCombine/getelementptr.ll
@@ -1239,8 +1239,7 @@ define i32* @PR45084_extra_use(i1 %cond, %struct.f** %p) {
define i8* @gep_null_inbounds(i64 %idx) {
; CHECK-LABEL: @gep_null_inbounds(
-; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, i8* null, i64 [[IDX:%.*]]
-; CHECK-NEXT: ret i8* [[GEP]]
+; CHECK-NEXT: ret i8* null
;
%gep = getelementptr inbounds i8, i8* null, i64 %idx
ret i8* %gep
diff --git a/llvm/test/Transforms/InstCombine/store.ll b/llvm/test/Transforms/InstCombine/store.ll
index d3842f4bb469..a94ce92d214e 100644
--- a/llvm/test/Transforms/InstCombine/store.ll
+++ b/llvm/test/Transforms/InstCombine/store.ll
@@ -25,8 +25,7 @@ define void @test2(i32* %P) {
define void @store_at_gep_off_null_inbounds(i64 %offset) {
; CHECK-LABEL: @store_at_gep_off_null_inbounds(
-; CHECK-NEXT: [[PTR:%.*]] = getelementptr inbounds i32, i32* null, i64 [[OFFSET:%.*]]
-; CHECK-NEXT: store i32 undef, i32* [[PTR]], align 4
+; CHECK-NEXT: store i32 undef, i32* null, align 536870912
; CHECK-NEXT: ret void
;
%ptr = getelementptr inbounds i32, i32 *null, i64 %offset
More information about the llvm-branch-commits
mailing list