[llvm-branch-commits] [llvm] 899faa5 - [InstCombine] Check inbounds in load/store of gep null transform (PR48577)

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


Author: Nikita Popov
Date: 2020-12-23T21:03:22+01:00
New Revision: 899faa50f206073cdd8eeaaa130ffa15f850e656

URL: https://github.com/llvm/llvm-project/commit/899faa50f206073cdd8eeaaa130ffa15f850e656
DIFF: https://github.com/llvm/llvm-project/commit/899faa50f206073cdd8eeaaa130ffa15f850e656.diff

LOG: [InstCombine] Check inbounds in load/store of gep null transform (PR48577)

If the GEP isn't inbounds, then accessing a GEP of null location
is generally not UB.

While this is a minimal fix, the GEP of null handling should
probably be its own fold.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
    llvm/test/Transforms/InstCombine/load.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 ac617ecd4fd1..71f165abe52e 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
@@ -908,7 +908,8 @@ static bool canSimplifyNullStoreOrGEP(StoreInst &SI) {
 
   auto *Ptr = SI.getPointerOperand();
   if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Ptr))
-    Ptr = GEPI->getOperand(0);
+    if (GEPI->isInBounds())
+      Ptr = GEPI->getOperand(0);
   return (isa<ConstantPointerNull>(Ptr) &&
           !NullPointerIsDefined(SI.getFunction(), SI.getPointerAddressSpace()));
 }
@@ -916,7 +917,7 @@ static bool canSimplifyNullStoreOrGEP(StoreInst &SI) {
 static bool canSimplifyNullLoadOrGEP(LoadInst &LI, Value *Op) {
   if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Op)) {
     const Value *GEPI0 = GEPI->getOperand(0);
-    if (isa<ConstantPointerNull>(GEPI0) &&
+    if (isa<ConstantPointerNull>(GEPI0) && GEPI->isInBounds() &&
         !NullPointerIsDefined(LI.getFunction(), GEPI->getPointerAddressSpace()))
       return true;
   }

diff  --git a/llvm/test/Transforms/InstCombine/load.ll b/llvm/test/Transforms/InstCombine/load.ll
index a6a2155be0b5..e4ba908599c9 100644
--- a/llvm/test/Transforms/InstCombine/load.ll
+++ b/llvm/test/Transforms/InstCombine/load.ll
@@ -69,8 +69,9 @@ define i32 @load_gep_null_inbounds(i64 %X) {
 
 define i32 @load_gep_null_not_inbounds(i64 %X) {
 ; CHECK-LABEL: @load_gep_null_not_inbounds(
-; CHECK-NEXT:    store i32 undef, i32* null, align 536870912
-; CHECK-NEXT:    ret i32 undef
+; CHECK-NEXT:    [[V:%.*]] = getelementptr i32, i32* null, i64 [[X:%.*]]
+; CHECK-NEXT:    [[R:%.*]] = load i32, i32* [[V]], align 4
+; CHECK-NEXT:    ret i32 [[R]]
 ;
   %V = getelementptr i32, i32* null, i64 %X
   %R = load i32, i32* %V

diff  --git a/llvm/test/Transforms/InstCombine/store.ll b/llvm/test/Transforms/InstCombine/store.ll
index cda08f89501a..d3842f4bb469 100644
--- a/llvm/test/Transforms/InstCombine/store.ll
+++ b/llvm/test/Transforms/InstCombine/store.ll
@@ -37,7 +37,7 @@ define void @store_at_gep_off_null_inbounds(i64 %offset) {
 define void @store_at_gep_off_null_not_inbounds(i64 %offset) {
 ; CHECK-LABEL: @store_at_gep_off_null_not_inbounds(
 ; CHECK-NEXT:    [[PTR:%.*]] = getelementptr i32, i32* null, i64 [[OFFSET:%.*]]
-; CHECK-NEXT:    store i32 undef, i32* [[PTR]], align 4
+; CHECK-NEXT:    store i32 24, i32* [[PTR]], align 4
 ; CHECK-NEXT:    ret void
 ;
   %ptr = getelementptr i32, i32 *null, i64 %offset


        


More information about the llvm-branch-commits mailing list