Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 6, 2025

Conversation

nikic
Copy link
Contributor

@nikic nikic commented Jun 5, 2025

isNonEscapingLocalObject() is only used by SimpleCaptureAnalysis and tightly integrated with its implementation (in particular its cache), so inline and simplify the implementation.

isNonEscapingLocalObject() is only used by SimpleCaptureAnalysis
and tightly integrated with its implementation (in particular its
cache), so inline and simplify the implementation.
@llvmbot llvmbot added the llvm:analysis Includes value tracking, cost tables and constant folding label Jun 5, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 5, 2025

@llvm/pr-subscribers-llvm-analysis

Author: Nikita Popov (nikic)

Changes

isNonEscapingLocalObject() is only used by SimpleCaptureAnalysis and tightly integrated with its implementation (in particular its cache), so inline and simplify the implementation.


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

3 Files Affected:

  • (modified) llvm/include/llvm/Analysis/CaptureTracking.h (-6)
  • (modified) llvm/lib/Analysis/BasicAliasAnalysis.cpp (+11-1)
  • (modified) llvm/lib/Analysis/CaptureTracking.cpp (-24)
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;
-}

Copy link
Contributor

@antoniofrighetto antoniofrighetto left a comment

Choose a reason for hiding this comment

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

Looks reasonable to me, thanks. Maybe s/isNonEscapingLocalObject/isNotCapturedBefore/ in AA doc-comments?

@nikic nikic merged commit 10dd83d into llvm:main Jun 6, 2025
9 of 11 checks passed
@nikic nikic deleted the move-non-escaping-local-object branch June 6, 2025 07:40
rorth pushed a commit to rorth/llvm-project that referenced this pull request Jun 11, 2025
llvm#142971)

isNonEscapingLocalObject() is only used by SimpleCaptureAnalysis and
tightly integrated with its implementation (in particular its cache), so
inline and simplify the implementation.
DhruvSrivastavaX pushed a commit to DhruvSrivastavaX/lldb-for-aix that referenced this pull request Jun 12, 2025
llvm#142971)

isNonEscapingLocalObject() is only used by SimpleCaptureAnalysis and
tightly integrated with its implementation (in particular its cache), so
inline and simplify the implementation.
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants