[llvm] 356d7e6 - [IR] Handle nofree noalias in canBeFreed() (#200194)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 1 03:30:38 PDT 2026


Author: Nikita Popov
Date: 2026-06-01T12:30:32+02:00
New Revision: 356d7e6bf429a7ff3ae798bb994631ef33891a44

URL: https://github.com/llvm/llvm-project/commit/356d7e6bf429a7ff3ae798bb994631ef33891a44
DIFF: https://github.com/llvm/llvm-project/commit/356d7e6bf429a7ff3ae798bb994631ef33891a44.diff

LOG: [IR] Handle nofree noalias in canBeFreed() (#200194)

Based on the argument nofree semantics specified in
https://github.com/llvm/llvm-project/pull/195658, we can conclude that
an argument with both nofree and noalias cannot be freed.

This also handles the case of readonly + noalias, to be consistent with
the logic for functions (and because we had a FIXME for it...)

Added: 
    

Modified: 
    llvm/lib/IR/Value.cpp
    llvm/test/Analysis/ValueTracking/memory-dereferenceable.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/Value.cpp b/llvm/lib/IR/Value.cpp
index f5a501897f0bb..d66f5fc337740 100644
--- a/llvm/lib/IR/Value.cpp
+++ b/llvm/lib/IR/Value.cpp
@@ -841,6 +841,12 @@ bool Value::canBeFreed() const {
     const Function *F = A->getParent();
     if (F->doesNotFreeMemory())
       return false;
+
+    // nofree on the argument ensures that it cannot be freed through that
+    // pointer. noalias additionally ensures that it can't be freed through
+    // another pointer to the same allocation. Readonly implies nofree.
+    if ((A->hasNoFreeAttr() || A->onlyReadsMemory()) && A->hasNoAliasAttr())
+      return false;
   }
 
   if (auto *ITP = dyn_cast<IntToPtrInst>(this);

diff  --git a/llvm/test/Analysis/ValueTracking/memory-dereferenceable.ll b/llvm/test/Analysis/ValueTracking/memory-dereferenceable.ll
index 43f2c156b2ddc..a03a76373f803 100644
--- a/llvm/test/Analysis/ValueTracking/memory-dereferenceable.ll
+++ b/llvm/test/Analysis/ValueTracking/memory-dereferenceable.ll
@@ -263,9 +263,7 @@ define void @infer_func_attrs2(ptr dereferenceable(8) %p) readonly {
 }
 
 ; CHECK-LABEL: 'infer_noalias1'
-; GLOBAL: %p
-; POINT-NOT: %p
-; FIXME: Can be inferred from attributes
+; CHECK: %p
 define void @infer_noalias1(ptr dereferenceable(8) noalias nofree %p) {
   call void @mayfree()
   %v = load i32, ptr %p
@@ -273,15 +271,30 @@ define void @infer_noalias1(ptr dereferenceable(8) noalias nofree %p) {
 }
 
 ; CHECK-LABEL: 'infer_noalias2'
+; CHECK: %p
+define void @infer_noalias2(ptr dereferenceable(8) noalias readonly %p) {
+  call void @mayfree()
+  %v = load i32, ptr %p
+  ret void
+}
+
+; CHECK-LABEL: 'infer_missing_noalias1'
 ; GLOBAL: %p
 ; POINT-NOT: %p
-; FIXME: Can be inferred from attributes
-define void @infer_noalias2(ptr dereferenceable(8) noalias readonly %p) nosync {
+define void @infer_missing_noalias1(ptr dereferenceable(8) nofree %p) {
   call void @mayfree()
   %v = load i32, ptr %p
   ret void
 }
 
+; CHECK-LABEL: 'infer_missing_noalias2'
+; GLOBAL: %p
+; POINT-NOT: %p
+define void @infer_missing_noalias2(ptr dereferenceable(8) readonly %p) {
+  call void @mayfree()
+  %v = load i32, ptr %p
+  ret void
+}
 
 ; Just check that we don't crash.
 ; CHECK-LABEL: 'opaque_type_crasher'


        


More information about the llvm-commits mailing list