Skip to content

Commit 224fd6f

Browse files
committed
[NFC][CaptureTracking] Move static function isNonEscapingLocalObject to llvm namespace
Function isNonEscapingLocalObject is a static one within BasicAliasAnalysis.cpp. It wraps around PointerMayBeCaptured of CaptureTracking, checking whether a pointer is to a function-local object, which never escapes from the function. Although at the moment, isNonEscapingLocalObject is used only by BasicAliasAnalysis, its functionality can be used by other pass(es), one of which I will put up for review very soon. Instead of copying the contents of this static function, I move it to llvm scope, and place it amongst other functions with similar functionality in CaptureTracking. The rationale for the location are: - Pointer escape and pointer being captured are actually two sides of the same coin - isNonEscapingLocalObject is wrapping around another function in CaptureTracking Reviewed By: jdoerfert (Johannes Doerfert) Differential Revision: https://reviews.llvm.org/D89465
1 parent 67f189e commit 224fd6f

File tree

3 files changed

+49
-44
lines changed

3 files changed

+49
-44
lines changed

llvm/include/llvm/Analysis/CaptureTracking.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#ifndef LLVM_ANALYSIS_CAPTURETRACKING_H
1414
#define LLVM_ANALYSIS_CAPTURETRACKING_H
1515

16+
#include "llvm/ADT/DenseMap.h"
17+
1618
namespace llvm {
1719

1820
class Value;
@@ -94,6 +96,12 @@ namespace llvm {
9496
/// is zero, a default value is assumed.
9597
void PointerMayBeCaptured(const Value *V, CaptureTracker *Tracker,
9698
unsigned MaxUsesToExplore = 0);
99+
100+
/// Returns true if the pointer is to a function-local object that never
101+
/// escapes from the function.
102+
bool isNonEscapingLocalObject(
103+
const Value *V,
104+
SmallDenseMap<const Value *, bool, 8> *IsCapturedCache = nullptr);
97105
} // end namespace llvm
98106

99107
#endif

llvm/lib/Analysis/BasicAliasAnalysis.cpp

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -115,50 +115,6 @@ bool BasicAAResult::invalidate(Function &Fn, const PreservedAnalyses &PA,
115115
// Useful predicates
116116
//===----------------------------------------------------------------------===//
117117

118-
/// Returns true if the pointer is to a function-local object that never
119-
/// escapes from the function.
120-
static bool isNonEscapingLocalObject(
121-
const Value *V,
122-
SmallDenseMap<const Value *, bool, 8> *IsCapturedCache = nullptr) {
123-
SmallDenseMap<const Value *, bool, 8>::iterator CacheIt;
124-
if (IsCapturedCache) {
125-
bool Inserted;
126-
std::tie(CacheIt, Inserted) = IsCapturedCache->insert({V, false});
127-
if (!Inserted)
128-
// Found cached result, return it!
129-
return CacheIt->second;
130-
}
131-
132-
// If this is a local allocation, check to see if it escapes.
133-
if (isa<AllocaInst>(V) || isNoAliasCall(V)) {
134-
// Set StoreCaptures to True so that we can assume in our callers that the
135-
// pointer is not the result of a load instruction. Currently
136-
// PointerMayBeCaptured doesn't have any special analysis for the
137-
// StoreCaptures=false case; if it did, our callers could be refined to be
138-
// more precise.
139-
auto Ret = !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true);
140-
if (IsCapturedCache)
141-
CacheIt->second = Ret;
142-
return Ret;
143-
}
144-
145-
// If this is an argument that corresponds to a byval or noalias argument,
146-
// then it has not escaped before entering the function. Check if it escapes
147-
// inside the function.
148-
if (const Argument *A = dyn_cast<Argument>(V))
149-
if (A->hasByValAttr() || A->hasNoAliasAttr()) {
150-
// Note even if the argument is marked nocapture, we still need to check
151-
// for copies made inside the function. The nocapture attribute only
152-
// specifies that there are no copies made that outlive the function.
153-
auto Ret = !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true);
154-
if (IsCapturedCache)
155-
CacheIt->second = Ret;
156-
return Ret;
157-
}
158-
159-
return false;
160-
}
161-
162118
/// Returns true if the pointer is one which would have been considered an
163119
/// escape by isNonEscapingLocalObject.
164120
static bool isEscapeSource(const Value *V) {

llvm/lib/Analysis/CaptureTracking.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,3 +395,44 @@ void llvm::PointerMayBeCaptured(const Value *V, CaptureTracker *Tracker,
395395

396396
// All uses examined.
397397
}
398+
399+
bool llvm::isNonEscapingLocalObject(
400+
const Value *V, SmallDenseMap<const Value *, bool, 8> *IsCapturedCache) {
401+
SmallDenseMap<const Value *, bool, 8>::iterator CacheIt;
402+
if (IsCapturedCache) {
403+
bool Inserted;
404+
std::tie(CacheIt, Inserted) = IsCapturedCache->insert({V, false});
405+
if (!Inserted)
406+
// Found cached result, return it!
407+
return CacheIt->second;
408+
}
409+
410+
// If this is a local allocation, check to see if it escapes.
411+
if (isa<AllocaInst>(V) || isNoAliasCall(V)) {
412+
// Set StoreCaptures to True so that we can assume in our callers that the
413+
// pointer is not the result of a load instruction. Currently
414+
// PointerMayBeCaptured doesn't have any special analysis for the
415+
// StoreCaptures=false case; if it did, our callers could be refined to be
416+
// more precise.
417+
auto Ret = !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true);
418+
if (IsCapturedCache)
419+
CacheIt->second = Ret;
420+
return Ret;
421+
}
422+
423+
// If this is an argument that corresponds to a byval or noalias argument,
424+
// then it has not escaped before entering the function. Check if it escapes
425+
// inside the function.
426+
if (const Argument *A = dyn_cast<Argument>(V))
427+
if (A->hasByValAttr() || A->hasNoAliasAttr()) {
428+
// Note even if the argument is marked nocapture, we still need to check
429+
// for copies made inside the function. The nocapture attribute only
430+
// specifies that there are no copies made that outlive the function.
431+
auto Ret = !PointerMayBeCaptured(V, false, /*StoreCaptures=*/true);
432+
if (IsCapturedCache)
433+
CacheIt->second = Ret;
434+
return Ret;
435+
}
436+
437+
return false;
438+
}

0 commit comments

Comments
 (0)