Skip to content

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

Merged
nikic merged 2 commits into
llvm:mainfrom
nikic:noalias-nofree
Jun 1, 2026
Merged

[IR] Handle nofree noalias in canBeFreed()#200194
nikic merged 2 commits into
llvm:mainfrom
nikic:noalias-nofree

Conversation

@nikic

@nikic nikic commented May 28, 2026

Copy link
Copy Markdown
Contributor

Based on the argument nofree semantics specified in #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...)

Based on the argument nofree semantics specified in
llvm#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...)
@llvmorg-github-actions llvmorg-github-actions Bot added llvm:ir llvm:analysis Includes value tracking, cost tables and constant folding labels May 28, 2026
@llvmorg-github-actions

llvmorg-github-actions Bot commented May 28, 2026

Copy link
Copy Markdown

@llvm/pr-subscribers-llvm-analysis

@llvm/pr-subscribers-llvm-ir

Author: Nikita Popov (nikic)

Changes

Based on the argument nofree semantics specified in #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...)


Full diff: https://github.com/llvm/llvm-project/pull/200194.diff

2 Files Affected:

  • (modified) llvm/lib/IR/Value.cpp (+6)
  • (modified) llvm/test/Analysis/ValueTracking/memory-dereferenceable.ll (+18-5)
diff --git a/llvm/lib/IR/Value.cpp b/llvm/lib/IR/Value.cpp
index 7246adf7ec651..439c3572bdccd 100644
--- a/llvm/lib/IR/Value.cpp
+++ b/llvm/lib/IR/Value.cpp
@@ -842,6 +842,12 @@ bool Value::canBeFreed() const {
     const Function *F = A->getParent();
     if (F->doesNotFreeMemory() && F->hasNoSync())
       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 fc40c68624f2d..0edd95dbbb2f4 100644
--- a/llvm/test/Analysis/ValueTracking/memory-dereferenceable.ll
+++ b/llvm/test/Analysis/ValueTracking/memory-dereferenceable.ll
@@ -265,9 +265,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
@@ -275,15 +273,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) nosync {
+  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'


; CHECK-LABEL: 'infer_noalias2'
; CHECK: %p
define void @infer_noalias2(ptr dereferenceable(8) noalias readonly %p) nosync {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe nit: the other PR may also drop nosync from this, as otherwise now possibly misleading?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, dropped.

@antoniofrighetto antoniofrighetto left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nikic
nikic merged commit 356d7e6 into llvm:main Jun 1, 2026
10 checks passed
@nikic
nikic deleted the noalias-nofree branch June 1, 2026 10:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:analysis Includes value tracking, cost tables and constant folding llvm:ir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants