[llvm] [Loads][NFC] Add isDereferenceablePointer overload taking a size (PR #203905)

Nikolas Klauser via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 15 06:39:39 PDT 2026


https://github.com/philnik777 updated https://github.com/llvm/llvm-project/pull/203905

>From 2b9c82662e8e6913d23b92d7552c65fb9e4679ad Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Mon, 15 Jun 2026 15:29:15 +0200
Subject: [PATCH] [Loads][NFC] Add isDereferenceablePointer overload taking a
 size

---
 llvm/include/llvm/Analysis/Loads.h             | 18 ++++++++++--------
 llvm/lib/Analysis/Loads.cpp                    |  5 +++++
 llvm/lib/CodeGen/MachineOperand.cpp            |  4 ++--
 .../InstCombine/InstCombineCalls.cpp           |  6 +++---
 llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp |  4 ++--
 llvm/lib/Transforms/Scalar/MergeICmps.cpp      |  4 ++--
 llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp |  2 +-
 7 files changed, 25 insertions(+), 18 deletions(-)

diff --git a/llvm/include/llvm/Analysis/Loads.h b/llvm/include/llvm/Analysis/Loads.h
index c48f2ee76b743..9190e47707215 100644
--- a/llvm/include/llvm/Analysis/Loads.h
+++ b/llvm/include/llvm/Analysis/Loads.h
@@ -36,14 +36,6 @@ class SCEVPredicate;
 template <typename T> class SmallVectorImpl;
 class TargetLibraryInfo;
 
-/// Return true if this is always a dereferenceable pointer. If the context
-/// instruction is specified perform context-sensitive analysis and return true
-/// if the pointer is dereferenceable at the specified instruction.
-/// If \p IgnoreFree is set, ignore potential frees of the object.
-LLVM_ABI bool isDereferenceablePointer(const Value *V, Type *Ty,
-                                       const SimplifyQuery &Q,
-                                       bool IgnoreFree = false);
-
 /// Returns true if V is always a dereferenceable pointer with alignment
 /// greater or equal than requested. If the context instruction is specified
 /// performs context-sensitive analysis and returns true if the pointer is
@@ -65,6 +57,16 @@ LLVM_ABI bool isDereferenceableAndAlignedPointer(const Value *V,
                                                  const SimplifyQuery &Q,
                                                  bool IgnoreFree = false);
 
+/// Equivalent to isDereferenceableAndAlignedPointer with an alignment of 1.
+LLVM_ABI bool isDereferenceablePointer(const Value *V, Type *Ty,
+                                       const SimplifyQuery &Q,
+                                       bool IgnoreFree = false);
+
+/// Equivalent to isDereferenceableAndAlignedPointer with an alignment of 1.
+LLVM_ABI bool isDereferenceablePointer(const Value *V, const APInt &Size,
+                                       const SimplifyQuery &Q,
+                                       bool IgnoreFree = false);
+
 /// Return true if we know that executing a load from this value cannot trap.
 ///
 /// If ScanFrom is specified this method performs context-sensitive analysis
diff --git a/llvm/lib/Analysis/Loads.cpp b/llvm/lib/Analysis/Loads.cpp
index 28e07eb69cc65..69f886ba32aba 100644
--- a/llvm/lib/Analysis/Loads.cpp
+++ b/llvm/lib/Analysis/Loads.cpp
@@ -267,6 +267,11 @@ bool llvm::isDereferenceablePointer(const Value *V, Type *Ty,
   return isDereferenceableAndAlignedPointer(V, Ty, Align(1), SQ, IgnoreFree);
 }
 
+bool llvm::isDereferenceablePointer(const Value *V, const APInt &Size,
+                                    const SimplifyQuery &Q, bool IgnoreFree) {
+  return isDereferenceableAndAlignedPointer(V, Align(1), Size, Q, IgnoreFree);
+}
+
 /// Test if A and B will obviously have the same value.
 ///
 /// This includes recognizing that %t0 and %t1 will have the same
diff --git a/llvm/lib/CodeGen/MachineOperand.cpp b/llvm/lib/CodeGen/MachineOperand.cpp
index 96aac20610ff7..518759952667e 100644
--- a/llvm/lib/CodeGen/MachineOperand.cpp
+++ b/llvm/lib/CodeGen/MachineOperand.cpp
@@ -1134,8 +1134,8 @@ bool MachinePointerInfo::isDereferenceable(unsigned Size, LLVMContext &C,
   if (BasePtr == nullptr)
     return false;
 
-  return isDereferenceableAndAlignedPointer(
-      BasePtr, Align(1), APInt(DL.getPointerSizeInBits(), Offset + Size),
+  return isDereferenceablePointer(
+      BasePtr, APInt(DL.getPointerSizeInBits(), Offset + Size),
       SimplifyQuery(DL, dyn_cast<Instruction>(BasePtr)));
 }
 
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index a5915cf092c50..b53ec51f65240 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -3699,9 +3699,9 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
         if (!Count)
           break;
 
-        if (*Count == 0 || isDereferenceableAndAlignedPointer(
-                               Ptr, Align(1), APInt(64, *Count),
-                               getSimplifyQuery().getWithInstruction(II)))
+        if (*Count == 0 ||
+            isDereferenceablePointer(Ptr, APInt(64, *Count),
+                                     getSimplifyQuery().getWithInstruction(II)))
           return RemoveBundle();
 
         break;
diff --git a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
index 68671ef4b81aa..3f6bb6f408797 100644
--- a/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
+++ b/llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
@@ -922,8 +922,8 @@ bool MemCpyOptPass::performCallSlotOptzn(Instruction *cpyLoad,
   bool ExplicitlyDereferenceableOnly;
   if (!isWritableObject(getUnderlyingObject(cpyDest),
                         ExplicitlyDereferenceableOnly) ||
-      !isDereferenceableAndAlignedPointer(cpyDest, Align(1), APInt(64, cpySize),
-                                          SimplifyQuery(DL, DT, AC, C))) {
+      !isDereferenceablePointer(cpyDest, APInt(64, cpySize),
+                                SimplifyQuery(DL, DT, AC, C))) {
     LLVM_DEBUG(dbgs() << "Call Slot: Dest pointer not dereferenceable\n");
     return false;
   }
diff --git a/llvm/lib/Transforms/Scalar/MergeICmps.cpp b/llvm/lib/Transforms/Scalar/MergeICmps.cpp
index f8beaab7c6f5c..fbcbf537faf54 100644
--- a/llvm/lib/Transforms/Scalar/MergeICmps.cpp
+++ b/llvm/lib/Transforms/Scalar/MergeICmps.cpp
@@ -761,8 +761,8 @@ bool BCECmpChain::isDereferenceable() {
 
     APInt Size(64, SizeInBits / 8);
     SimplifyQuery SQ(DL, CxtI);
-    if (!isDereferenceableAndAlignedPointer(Lhs, Align(1), Size, SQ) ||
-        !isDereferenceableAndAlignedPointer(Rhs, Align(1), Size, SQ))
+    if (!isDereferenceablePointer(Lhs, Size, SQ) ||
+        !isDereferenceablePointer(Rhs, Size, SQ))
       return false;
   }
   return true;
diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
index 1cbc8d0bef5ae..2c68468c390b3 100644
--- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -268,7 +268,7 @@ static bool canTransformToMemCmp(CallInst *CI, Value *Str, uint64_t Len,
   if (!isOnlyUsedInComparisonWithZero(CI))
     return false;
 
-  if (!isDereferenceableAndAlignedPointer(Str, Align(1), APInt(64, Len), DL))
+  if (!isDereferenceablePointer(Str, APInt(64, Len), DL))
     return false;
 
   if (CI->getFunction()->hasFnAttribute(Attribute::SanitizeMemory))



More information about the llvm-commits mailing list