[llvm] 43114cb - [ValueTracking] Dereferenceable and !NullPointerIsDefined imply non-zero

via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 13 20:58:38 PDT 2023


Author: luxufan
Date: 2023-08-14T11:42:21+08:00
New Revision: 43114cb696c035901b7d31a76bc0ce87d60afc76

URL: https://github.com/llvm/llvm-project/commit/43114cb696c035901b7d31a76bc0ce87d60afc76
DIFF: https://github.com/llvm/llvm-project/commit/43114cb696c035901b7d31a76bc0ce87d60afc76.diff

LOG: [ValueTracking] Dereferenceable and !NullPointerIsDefined imply non-zero

Reviewed By: nikic

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

Added: 
    

Modified: 
    llvm/lib/Analysis/ValueTracking.cpp
    llvm/test/Transforms/InstSimplify/icmp.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 87014a4a6b15e8..5711de18723311 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -2707,14 +2707,20 @@ static bool isKnownNonZeroFromOperator(const Operator *I,
     return isKnownNonZero(I->getOperand(0), Depth, Q) &&
            isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
                                      Depth);
-  case Instruction::Load:
-    // A Load tagged with nonnull metadata is never null.
-    if (Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_nonnull))
-      return true;
+  case Instruction::Load: {
+    auto *LI = cast<LoadInst>(I);
+    // A Load tagged with nonnull or dereferenceable with null pointer undefined
+    // is never null.
+    if (auto *PtrT = dyn_cast<PointerType>(I->getType()))
+      if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
+          (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
+           !NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))
+        return true;
 
     // No need to fall through to computeKnownBits as range metadata is already
     // handled in isKnownNonZero.
     return false;
+  }
   case Instruction::Call:
   case Instruction::Invoke:
     if (I->getType()->isPointerTy()) {

diff  --git a/llvm/test/Transforms/InstSimplify/icmp.ll b/llvm/test/Transforms/InstSimplify/icmp.ll
index 6f0a3fa52c8610..3109768bdfe005 100644
--- a/llvm/test/Transforms/InstSimplify/icmp.ll
+++ b/llvm/test/Transforms/InstSimplify/icmp.ll
@@ -258,3 +258,23 @@ define i1 @sub_even(i8 %x) {
   %cmp = icmp ne i8 %sub, %x
   ret i1 %cmp
 }
+
+define i1 @load_ptr(ptr %p) {
+; CHECK-LABEL: @load_ptr(
+; CHECK-NEXT:    ret i1 true
+;
+  %load_p = load ptr, ptr %p, !dereferenceable !{i64 8}
+  %r = icmp ne ptr %load_p, null
+  ret i1 %r
+}
+
+define i1 @load_ptr_null_valid(ptr %p) null_pointer_is_valid {
+; CHECK-LABEL: @load_ptr_null_valid(
+; CHECK-NEXT:    [[LOAD_P:%.*]] = load ptr, ptr [[P:%.*]], align 8, !dereferenceable !0
+; CHECK-NEXT:    [[R:%.*]] = icmp ne ptr [[LOAD_P]], null
+; CHECK-NEXT:    ret i1 [[R]]
+;
+  %load_p = load ptr, ptr %p, !dereferenceable !{i64 8}
+  %r = icmp ne ptr %load_p, null
+  ret i1 %r
+}


        


More information about the llvm-commits mailing list