Skip to content

Commit 356d7e6

Browse files
authored
[IR] Handle nofree noalias in canBeFreed() (llvm#200194)
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...)
1 parent bd1b3d4 commit 356d7e6

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

llvm/lib/IR/Value.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,12 @@ bool Value::canBeFreed() const {
841841
const Function *F = A->getParent();
842842
if (F->doesNotFreeMemory())
843843
return false;
844+
845+
// nofree on the argument ensures that it cannot be freed through that
846+
// pointer. noalias additionally ensures that it can't be freed through
847+
// another pointer to the same allocation. Readonly implies nofree.
848+
if ((A->hasNoFreeAttr() || A->onlyReadsMemory()) && A->hasNoAliasAttr())
849+
return false;
844850
}
845851

846852
if (auto *ITP = dyn_cast<IntToPtrInst>(this);

llvm/test/Analysis/ValueTracking/memory-dereferenceable.ll

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,25 +263,38 @@ define void @infer_func_attrs2(ptr dereferenceable(8) %p) readonly {
263263
}
264264

265265
; CHECK-LABEL: 'infer_noalias1'
266-
; GLOBAL: %p
267-
; POINT-NOT: %p
268-
; FIXME: Can be inferred from attributes
266+
; CHECK: %p
269267
define void @infer_noalias1(ptr dereferenceable(8) noalias nofree %p) {
270268
call void @mayfree()
271269
%v = load i32, ptr %p
272270
ret void
273271
}
274272

275273
; CHECK-LABEL: 'infer_noalias2'
274+
; CHECK: %p
275+
define void @infer_noalias2(ptr dereferenceable(8) noalias readonly %p) {
276+
call void @mayfree()
277+
%v = load i32, ptr %p
278+
ret void
279+
}
280+
281+
; CHECK-LABEL: 'infer_missing_noalias1'
276282
; GLOBAL: %p
277283
; POINT-NOT: %p
278-
; FIXME: Can be inferred from attributes
279-
define void @infer_noalias2(ptr dereferenceable(8) noalias readonly %p) nosync {
284+
define void @infer_missing_noalias1(ptr dereferenceable(8) nofree %p) {
280285
call void @mayfree()
281286
%v = load i32, ptr %p
282287
ret void
283288
}
284289

290+
; CHECK-LABEL: 'infer_missing_noalias2'
291+
; GLOBAL: %p
292+
; POINT-NOT: %p
293+
define void @infer_missing_noalias2(ptr dereferenceable(8) readonly %p) {
294+
call void @mayfree()
295+
%v = load i32, ptr %p
296+
ret void
297+
}
285298

286299
; Just check that we don't crash.
287300
; CHECK-LABEL: 'opaque_type_crasher'

0 commit comments

Comments
 (0)