[llvm-branch-commits] [llvm] 759b8c1 - [InstCombine] Handle different pointer types when folding gep of null

Nikita Popov via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Dec 23 13:03:46 PST 2020


Author: Nikita Popov
Date: 2020-12-23T21:58:26+01:00
New Revision: 759b8c11c398c20f12503356f8aef604e0bf82be

URL: https://github.com/llvm/llvm-project/commit/759b8c11c398c20f12503356f8aef604e0bf82be
DIFF: https://github.com/llvm/llvm-project/commit/759b8c11c398c20f12503356f8aef604e0bf82be.diff

LOG: [InstCombine] Handle different pointer types when folding gep of null

The source pointer type is not necessarily the same as the result
pointer type, so we can't simply return the original null pointer,
it might be a different one.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
    llvm/test/Transforms/InstCombine/getelementptr.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 7bde3845522f..8ace625e1fad 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1859,7 +1859,8 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
   if (isa<ConstantPointerNull>(PtrOp) && GEP.isInBounds() &&
       !NullPointerIsDefined(GEP.getFunction(),
                             PtrOp->getType()->getPointerAddressSpace()))
-    return replaceInstUsesWith(GEP, PtrOp);
+    if (auto *PtrTy = dyn_cast<PointerType>(GEPType))
+      return replaceInstUsesWith(GEP, ConstantPointerNull::get(PtrTy));
 
   // Eliminate unneeded casts for indices, and replace indices which displace
   // by multiples of a zero size type with zero.

diff  --git a/llvm/test/Transforms/InstCombine/getelementptr.ll b/llvm/test/Transforms/InstCombine/getelementptr.ll
index 6524c3c03639..f1af3fda79e7 100644
--- a/llvm/test/Transforms/InstCombine/getelementptr.ll
+++ b/llvm/test/Transforms/InstCombine/getelementptr.ll
@@ -1263,4 +1263,12 @@ define i8* @gep_null_defined(i64 %idx) null_pointer_is_valid {
   ret i8* %gep
 }
 
+define i8* @gep_null_inbounds_
diff erent_type(i64 %idx1, i64 %idx2) {
+; CHECK-LABEL: @gep_null_inbounds_
diff erent_type(
+; CHECK-NEXT:    ret i8* null
+;
+  %gep = getelementptr inbounds [0 x i8], [0 x i8]* null, i64 %idx1, i64 %idx2
+  ret i8* %gep
+}
+
 !0 = !{!"branch_weights", i32 2, i32 10}


        


More information about the llvm-branch-commits mailing list