[llvm] [BasicAA] Remove special malloc handling (PR #197180)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 24 01:20:58 PDT 2026


https://github.com/nikic updated https://github.com/llvm/llvm-project/pull/197180

>From 38d08912d79f0cb7593e08640e0b2d66e17f074a Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Tue, 12 May 2026 14:53:56 +0200
Subject: [PATCH] [BasicAA] Remove special malloc handling

---
 llvm/include/llvm/Analysis/MemoryBuiltins.h |  5 -----
 llvm/lib/Analysis/BasicAliasAnalysis.cpp    | 14 --------------
 llvm/lib/Analysis/MemoryBuiltins.cpp        |  8 --------
 llvm/test/Analysis/BasicAA/new.ll           | 14 ++++++++++++++
 llvm/test/Transforms/GVN/nonescaping.ll     |  2 +-
 5 files changed, 15 insertions(+), 28 deletions(-)
 create mode 100644 llvm/test/Analysis/BasicAA/new.ll

diff --git a/llvm/include/llvm/Analysis/MemoryBuiltins.h b/llvm/include/llvm/Analysis/MemoryBuiltins.h
index a42d662146563..ebe644cee53e1 100644
--- a/llvm/include/llvm/Analysis/MemoryBuiltins.h
+++ b/llvm/include/llvm/Analysis/MemoryBuiltins.h
@@ -58,11 +58,6 @@ LLVM_ABI bool
 isAllocationFn(const Value *V,
                function_ref<const TargetLibraryInfo &(Function &)> GetTLI);
 
-/// Tests if a value is a call or invoke to a library function that
-/// allocates memory similar to malloc or calloc.
-LLVM_ABI bool isMallocOrCallocLikeFn(const Value *V,
-                                     const TargetLibraryInfo *TLI);
-
 /// Tests if a value is a call or invoke to a library function that
 /// allocates memory (either malloc, calloc, or strdup like).
 LLVM_ABI bool isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI);
diff --git a/llvm/lib/Analysis/BasicAliasAnalysis.cpp b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
index 480d4046e04ff..e33fde77cf365 100644
--- a/llvm/lib/Analysis/BasicAliasAnalysis.cpp
+++ b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
@@ -1033,20 +1033,6 @@ ModRefInfo BasicAAResult::getModRefInfo(const CallBase *Call,
   if (!isModAndRefSet(Result))
     return Result;
 
-  // If the call is malloc/calloc like, we can assume that it doesn't
-  // modify any IR visible value.  This is only valid because we assume these
-  // routines do not read values visible in the IR.  TODO: Consider special
-  // casing realloc and strdup routines which access only their arguments as
-  // well.  Or alternatively, replace all of this with inaccessiblememonly once
-  // that's implemented fully.
-  if (isMallocOrCallocLikeFn(Call, &TLI)) {
-    // Be conservative if the accessed pointer may alias the allocation -
-    // fallback to the generic handling below.
-    if (AAQI.AAR.alias(MemoryLocation::getBeforeOrAfter(Call), Loc, AAQI) ==
-        AliasResult::NoAlias)
-      return ModRefInfo::NoModRef;
-  }
-
   // Like assumes, invariant.start intrinsics were also marked as arbitrarily
   // writing so that proper control dependencies are maintained but they never
   // mod any particular memory location visible to the IR.
diff --git a/llvm/lib/Analysis/MemoryBuiltins.cpp b/llvm/lib/Analysis/MemoryBuiltins.cpp
index aea1fec55b5bc..b7f2ee74721fe 100644
--- a/llvm/lib/Analysis/MemoryBuiltins.cpp
+++ b/llvm/lib/Analysis/MemoryBuiltins.cpp
@@ -294,14 +294,6 @@ bool llvm::isAllocationFn(
          checkFnAllocKind(V, AllocFnKind::Alloc | AllocFnKind::Realloc);
 }
 
-/// Tests if a value is a call or invoke to a library function that
-/// allocates memory similar to malloc or calloc.
-bool llvm::isMallocOrCallocLikeFn(const Value *V,
-                                  const TargetLibraryInfo *TLI) {
-  // TODO: Function behavior does not match name.
-  return getAllocationData(V, MallocOrOpNewLike, TLI).has_value();
-}
-
 /// Tests if a value is a call or invoke to a library function that
 /// allocates memory (either malloc, calloc, or strdup like).
 bool llvm::isAllocLikeFn(const Value *V, const TargetLibraryInfo *TLI) {
diff --git a/llvm/test/Analysis/BasicAA/new.ll b/llvm/test/Analysis/BasicAA/new.ll
new file mode 100644
index 0000000000000..8e12b08622c7f
--- /dev/null
+++ b/llvm/test/Analysis/BasicAA/new.ll
@@ -0,0 +1,14 @@
+; RUN: opt -passes=aa-eval -print-all-alias-modref-info -disable-output < %s 2>&1 | FileCheck %s
+
+; Don't assume that operator new without attributes does not access unrelated
+; memory.
+
+declare noalias ptr @_Znwm(i64)
+
+; CHECK-LABEL: Function: test:
+; CHECK: Both ModRef:  Ptr: i8* %p	<->  %1 = call ptr @_Znwm(i64 4)
+define void @test(ptr %p) {
+  call ptr @_Znwm(i64 4)
+  load i8, ptr %p
+  ret void
+}
diff --git a/llvm/test/Transforms/GVN/nonescaping.ll b/llvm/test/Transforms/GVN/nonescaping.ll
index 30f2c0eee4593..5db807fb95942 100644
--- a/llvm/test/Transforms/GVN/nonescaping.ll
+++ b/llvm/test/Transforms/GVN/nonescaping.ll
@@ -6,7 +6,7 @@ target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f3
 
 declare noalias ptr @malloc(i64) nounwind allockind("alloc,uninitialized") allocsize(0) inaccessiblememonly
 declare noalias ptr @calloc(i64, i64) allockind("alloc,zeroed") allocsize(0,1) inaccessiblememonly
-declare noalias ptr @_Znwm(i64)
+declare noalias ptr @_Znwm(i64) memory(inaccessiblemem: readwrite)
 declare void @escape(ptr)
 
 define i8 @test_malloc(ptr %p) {



More information about the llvm-commits mailing list