[llvm] r374728 - [InstCombine] don't assume 'inbounds' for bitcast deref or null pointer in non-default address space

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 13 10:19:08 PDT 2019


Author: spatel
Date: Sun Oct 13 10:19:08 2019
New Revision: 374728

URL: http://llvm.org/viewvc/llvm-project?rev=374728&view=rev
Log:
[InstCombine] don't assume 'inbounds' for bitcast deref or null pointer in non-default address space

Follow-up to D68244 to account for a corner case discussed in:
https://bugs.llvm.org/show_bug.cgi?id=43501

Add one more restriction: if the pointer is deref-or-null and in a non-default
(non-zero) address space, we can't assume inbounds.

Differential Revision: https://reviews.llvm.org/D68706

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp
    llvm/trunk/test/Transforms/InstCombine/load-bitcast-vec.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp?rev=374728&r1=374727&r2=374728&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp Sun Oct 13 10:19:08 2019
@@ -2344,8 +2344,16 @@ Instruction *InstCombiner::visitBitCast(
       // If the source pointer is dereferenceable, then assume it points to an
       // allocated object and apply "inbounds" to the GEP.
       bool CanBeNull;
-      if (Src->getPointerDereferenceableBytes(DL, CanBeNull))
-        GEP->setIsInBounds();
+      if (Src->getPointerDereferenceableBytes(DL, CanBeNull)) {
+        // In a non-default address space (not 0), a null pointer can not be
+        // assumed inbounds, so ignore that case (dereferenceable_or_null).
+        // The reason is that 'null' is not treated differently in these address
+        // spaces, and we consequently ignore the 'gep inbounds' special case
+        // for 'null' which allows 'inbounds' on 'null' if the indices are
+        // zeros.
+        if (SrcPTy->getAddressSpace() == 0 || !CanBeNull)
+          GEP->setIsInBounds();
+      }
       return GEP;
     }
   }

Modified: llvm/trunk/test/Transforms/InstCombine/load-bitcast-vec.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/load-bitcast-vec.ll?rev=374728&r1=374727&r2=374728&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/load-bitcast-vec.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/load-bitcast-vec.ll Sun Oct 13 10:19:08 2019
@@ -100,11 +100,11 @@ define float @matching_scalar_smallest_d
   ret float %r
 }
 
-; TODO: Is a null pointer inbounds in any address space?
+; A null pointer can't be assumed inbounds in a non-default address space.
 
 define float @matching_scalar_smallest_deref_or_null_addrspace(<4 x float> addrspace(4)* dereferenceable_or_null(1) %p) {
 ; CHECK-LABEL: @matching_scalar_smallest_deref_or_null_addrspace(
-; CHECK-NEXT:    [[BC:%.*]] = getelementptr inbounds <4 x float>, <4 x float> addrspace(4)* [[P:%.*]], i64 0, i64 0
+; CHECK-NEXT:    [[BC:%.*]] = getelementptr <4 x float>, <4 x float> addrspace(4)* [[P:%.*]], i64 0, i64 0
 ; CHECK-NEXT:    [[R:%.*]] = load float, float addrspace(4)* [[BC]], align 16
 ; CHECK-NEXT:    ret float [[R]]
 ;




More information about the llvm-commits mailing list