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

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


This revision was automatically updated to reflect the committed changes.
Closed by commit rG43114cb696c0: [ValueTracking] Dereferenceable and !NullPointerIsDefined imply non-zero (authored by StephenFan).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157253/new/

https://reviews.llvm.org/D157253

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


Index: llvm/test/Transforms/InstSimplify/icmp.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/icmp.ll
+++ llvm/test/Transforms/InstSimplify/icmp.ll
@@ -258,3 +258,23 @@
   %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
+}
Index: llvm/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/lib/Analysis/ValueTracking.cpp
+++ llvm/lib/Analysis/ValueTracking.cpp
@@ -2707,14 +2707,20 @@
     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()) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157253.549784.patch
Type: text/x-patch
Size: 2143 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230814/7ea90ffa/attachment.bin>


More information about the llvm-commits mailing list