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

luxufan via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 7 00:15:04 PDT 2023


StephenFan created this revision.
StephenFan added a reviewer: nikic.
Herald added a subscriber: hiraditya.
Herald added a project: All.
StephenFan requested review of this revision.
Herald added subscribers: llvm-commits, wangpc.
Herald added a project: LLVM.

Repository:
  rG LLVM Github Monorepo

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
@@ -2602,7 +2602,9 @@
 
     // A Load tagged with nonnull metadata is never null.
     if (const LoadInst *LI = dyn_cast<LoadInst>(V))
-      if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull))
+      if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
+          (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
+           !NullPointerIsDefined(LI->getFunction(), PtrTy->getAddressSpace())))
         return true;
 
     if (const auto *Call = dyn_cast<CallBase>(V)) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157253.547657.patch
Type: text/x-patch
Size: 1546 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230807/02f016dd/attachment.bin>


More information about the llvm-commits mailing list