[llvm] [AA] Merge isNonEscapingLocalObject() into SimpleCaptureAnalysis (NFC) (PR #142971)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 5 08:15:49 PDT 2025


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

>From c59a9048aaf049d5f66c8d3d25c26504957a4ca5 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Thu, 5 Jun 2025 16:00:24 +0200
Subject: [PATCH 1/2] [AA] Merge isNonEscapingLocalObject() into
 SimpleCaptureAnalysis (NFC)

isNonEscapingLocalObject() is only used by SimpleCaptureAnalysis
and tightly integrated with its implementation (in particular its
cache), so inline and simplify the implementation.
---
 llvm/include/llvm/Analysis/CaptureTracking.h |  6 -----
 llvm/lib/Analysis/BasicAliasAnalysis.cpp     | 12 +++++++++-
 llvm/lib/Analysis/CaptureTracking.cpp        | 24 --------------------
 3 files changed, 11 insertions(+), 31 deletions(-)

diff --git a/llvm/include/llvm/Analysis/CaptureTracking.h b/llvm/include/llvm/Analysis/CaptureTracking.h
index ed160ca3596e8..dd6a7f9b14dc6 100644
--- a/llvm/include/llvm/Analysis/CaptureTracking.h
+++ b/llvm/include/llvm/Analysis/CaptureTracking.h
@@ -195,12 +195,6 @@ namespace llvm {
   /// implicit captures such as for external globals.
   LLVM_ABI void PointerMayBeCaptured(const Value *V, CaptureTracker *Tracker,
                                      unsigned MaxUsesToExplore = 0);
-
-  /// Returns true if the pointer is to a function-local object that never
-  /// escapes from the function.
-  LLVM_ABI bool isNonEscapingLocalObject(
-      const Value *V,
-      SmallDenseMap<const Value *, bool, 8> *IsCapturedCache = nullptr);
 } // end namespace llvm
 
 #endif
diff --git a/llvm/lib/Analysis/BasicAliasAnalysis.cpp b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
index b110c2017b9eb..e6675256fd5a0 100644
--- a/llvm/lib/Analysis/BasicAliasAnalysis.cpp
+++ b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
@@ -199,7 +199,17 @@ CaptureAnalysis::~CaptureAnalysis() = default;
 bool SimpleCaptureAnalysis::isNotCapturedBefore(const Value *Object,
                                                 const Instruction *I,
                                                 bool OrAt) {
-  return isNonEscapingLocalObject(Object, &IsCapturedCache);
+  if (!isIdentifiedFunctionLocal(Object))
+    return false;
+
+  auto [CacheIt, Inserted] = IsCapturedCache.insert({Object, false});
+  if (!Inserted)
+    return CacheIt->second;
+
+  bool Ret = !capturesAnything(PointerMayBeCaptured(
+      Object, /*ReturnCaptures=*/false, CaptureComponents::Provenance));
+  CacheIt->second = Ret;
+  return Ret;
 }
 
 static bool isNotInCycle(const Instruction *I, const DominatorTree *DT,
diff --git a/llvm/lib/Analysis/CaptureTracking.cpp b/llvm/lib/Analysis/CaptureTracking.cpp
index 2550b7a4732c1..d08ed17a655e4 100644
--- a/llvm/lib/Analysis/CaptureTracking.cpp
+++ b/llvm/lib/Analysis/CaptureTracking.cpp
@@ -447,27 +447,3 @@ void llvm::PointerMayBeCaptured(const Value *V, CaptureTracker *Tracker,
 
   // All uses examined.
 }
-
-bool llvm::isNonEscapingLocalObject(
-    const Value *V, SmallDenseMap<const Value *, bool, 8> *IsCapturedCache) {
-  SmallDenseMap<const Value *, bool, 8>::iterator CacheIt;
-  if (IsCapturedCache) {
-    bool Inserted;
-    std::tie(CacheIt, Inserted) = IsCapturedCache->insert({V, false});
-    if (!Inserted)
-      // Found cached result, return it!
-      return CacheIt->second;
-  }
-
-  // If this is an identified function-local object, check to see if it escapes.
-  // We only care about provenance here, not address capture.
-  if (isIdentifiedFunctionLocal(V)) {
-    bool Ret = !capturesAnything(PointerMayBeCaptured(
-        V, /*ReturnCaptures=*/false, CaptureComponents::Provenance));
-    if (IsCapturedCache)
-      CacheIt->second = Ret;
-    return Ret;
-  }
-
-  return false;
-}

>From 2f0282acf2c1cbbb67087e8d4adf08ff9de51ebe Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Thu, 5 Jun 2025 17:15:32 +0200
Subject: [PATCH 2/2] Update comments

---
 llvm/include/llvm/Analysis/AliasAnalysis.h | 2 +-
 llvm/lib/Analysis/AliasAnalysis.cpp        | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/Analysis/AliasAnalysis.h b/llvm/include/llvm/Analysis/AliasAnalysis.h
index d8d88639b85a1..0e736b92e550e 100644
--- a/llvm/include/llvm/Analysis/AliasAnalysis.h
+++ b/llvm/include/llvm/Analysis/AliasAnalysis.h
@@ -912,7 +912,7 @@ LLVM_ABI bool isIdentifiedFunctionLocal(const Value *V);
 LLVM_ABI bool isBaseOfObject(const Value *V);
 
 /// Returns true if the pointer is one which would have been considered an
-/// escape by isNonEscapingLocalObject.
+/// escape by isNotCapturedBefore.
 LLVM_ABI bool isEscapeSource(const Value *V);
 
 /// Return true if Object memory is not visible after an unwind, in the sense
diff --git a/llvm/lib/Analysis/AliasAnalysis.cpp b/llvm/lib/Analysis/AliasAnalysis.cpp
index 2afabb75c7cc5..3ec009ca4adde 100644
--- a/llvm/lib/Analysis/AliasAnalysis.cpp
+++ b/llvm/lib/Analysis/AliasAnalysis.cpp
@@ -857,13 +857,13 @@ bool llvm::isEscapeSource(const Value *V) {
     return !CB->hasArgumentWithAdditionalReturnCaptureComponents();
   }
 
-  // The load case works because isNonEscapingLocalObject considers all
+  // The load case works because isNotCapturedBefore considers all
   // stores to be escapes (it passes true for the StoreCaptures argument
   // to PointerMayBeCaptured).
   if (isa<LoadInst>(V))
     return true;
 
-  // The inttoptr case works because isNonEscapingLocalObject considers all
+  // The inttoptr case works because isNotCapturedBefore considers all
   // means of converting or equating a pointer to an int (ptrtoint, ptr store
   // which could be followed by an integer load, ptr<->int compare) as
   // escaping, and objects located at well-known addresses via platform-specific



More information about the llvm-commits mailing list