1//===--- CaptureTracking.cpp - Determine whether a pointer is captured ----===//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//===----------------------------------------------------------------------===//
9// This file contains routines that help determine which pointers are captured.
10// A pointer value is captured if the function makes a copy of any part of the
11// pointer that outlives the call. Not being captured means, more or less, that
12// the pointer is only dereferenced and not stored in a global. Returning part
13// of the pointer as the function return value may or may not count as capturing
14// the pointer, depending on the context.
16//===----------------------------------------------------------------------===//
32 #define DEBUG_TYPE "capture-tracking"
34 STATISTIC(NumCaptured,
"Number of pointers maybe captured");
35 STATISTIC(NumNotCaptured,
"Number of pointers not captured");
36 STATISTIC(NumCapturedBefore,
"Number of pointers maybe captured before");
37 STATISTIC(NumNotCapturedBefore,
"Number of pointers not captured before");
39/// The default value for MaxUsesToExplore argument. It's relatively small to
40/// keep the cost of analysis reasonable for clients like BasicAliasAnalysis,
41/// where the results can't be cached.
42/// TODO: we should probably introduce a caching CaptureTracking analysis and
43/// use it where possible. The caching version can use much higher limit or
44/// don't have this cap at all.
47 cl::desc(
"Maximal number of uses to explore."),
62 : ReturnCaptures(ReturnCaptures), Mask(Mask), StopFn(StopFn) {}
64 void tooManyUses()
override {
69 Action captured(
const Use *U, UseCaptureInfo CI)
override {
71 return ContinueIgnoringReturn;
88/// Only find pointer captures which happen before the given instruction. Uses
89/// the dominator tree to determine whether one instruction is before another.
90/// Only support the case where the Value is defined in the same basic block
91/// as the given instruction and the use.
94 CapturesBefore(
bool ReturnCaptures,
const Instruction *
I,
95 const DominatorTree *DT,
bool IncludeI,
const LoopInfo *LI,
98 : BeforeHere(
I), DT(DT), ReturnCaptures(ReturnCaptures),
99 IncludeI(IncludeI), LI(LI),
Mask(
Mask), StopFn(StopFn) {}
101 void tooManyUses()
override { CC =
Mask; }
103 bool isSafeToPrune(Instruction *
I) {
107 // We explore this usage only if the usage can reach "BeforeHere".
108 // If use is not reachable from entry, there is no need to explore.
112 // Check whether there is a path from I to BeforeHere.
116 Action captured(
const Use *U, UseCaptureInfo CI)
override {
119 return ContinueIgnoringReturn;
121 // Check isSafeToPrune() here rather than in shouldExplore() to avoid
122 // an expensive reachability query for every instruction we look at.
123 // Instead we only do one for actual capturing candidates.
124 if (isSafeToPrune(
I))
125 // If the use is not reachable, the instruction result isn't either.
126 return ContinueIgnoringReturn;
132 return StopFn(CC) ? Stop :
Continue;
136 const DominatorTree *DT;
148/// Find the 'earliest' instruction before which the pointer is known not to
149/// be captured. Here an instruction A is considered earlier than instruction
150/// B, if A dominates B. If 2 escapes do not dominate each other, the
151/// terminator of the common dominator is chosen. If not all uses cannot be
152/// analyzed, the earliest escape is set to the first instruction in the
153/// function entry block.
154// NOTE: Users have to make sure instructions compared against the earliest
155// escape are not in a cycle.
158 EarliestCaptures(
bool ReturnCaptures, Function &
F,
const DominatorTree &DT,
160 : DT(DT), ReturnCaptures(ReturnCaptures),
F(
F),
Mask(
Mask) {}
162 void tooManyUses()
override {
164 EarliestCapture = &*
F.getEntryBlock().begin();
167 Action captured(
const Use *U, UseCaptureInfo CI)
override {
170 return ContinueIgnoringReturn;
173 if (!EarliestCapture)
180 // Continue analysis, as we need to see all potential captures.
184 const DominatorTree &DT;
198 "It doesn't make sense to ask whether a global is captured.");
202 SimpleCaptureTracker SCT(ReturnCaptures, Mask, StopFn);
214 unsigned MaxUsesToExplore) {
224 unsigned MaxUsesToExplore) {
226 "It doesn't make sense to ask whether a global is captured.");
232 CapturesBefore CB(ReturnCaptures,
I, DT, IncludeI, LI, Mask, StopFn);
237 ++NumNotCapturedBefore;
244 unsigned MaxUsesToExplore,
251std::pair<Instruction *, CaptureComponents>
254 unsigned MaxUsesToExplore) {
256 "It doesn't make sense to ask whether a global is captured.");
258 EarliestCaptures CB(ReturnCaptures,
F, DT, Mask);
263 ++NumNotCapturedBefore;
264 return {CB.EarliestCapture, CB.CC};
270 // TODO: Investigate non-instruction uses.
274 switch (
I->getOpcode()) {
275 case Instruction::Call:
276 case Instruction::Invoke: {
278 // The pointer is not captured if returned pointer is not captured.
279 // NOTE: CaptureTracking users should not assume that only functions
280 // marked with nocapture do not capture. This means that places like
281 // getUnderlyingObject in ValueTracking or DecomposeGEPExpression
282 // in BasicAA also need to know about this property.
286 // Volatile operations effectively capture the memory location that they
287 // load and store to.
289 if (
MI->isVolatile())
292 // Calling a function pointer does not in itself cause the pointer to
293 // be captured. This is a subtle point considering that (for example)
294 // the callee might return its own address. It is analogous to saying
295 // that loading a value from a pointer does not cause the pointer to be
296 // captured, even though the loaded value might be the pointer itself
297 // (think of self-referential objects).
298 if (
Call->isCallee(&U))
301 assert(
Call->isDataOperand(&U) &&
"Non-callee must be data operand");
304 // If the call is readonly and doesn't return a value, only the address
307 if (
Call->onlyReadsMemory() &&
Call->getType()->isVoidTy())
313 case Instruction::Load:
314 // Volatile loads make the address observable.
318 case Instruction::VAArg:
319 // "va-arg" from a pointer does not cause it to be captured.
321 case Instruction::Store:
322 // Stored the pointer - conservatively assume it may be captured.
323 if (U.getOperandNo() == 0)
325 I->getMetadata(LLVMContext::MD_captures));
327 // Volatile stores make the address observable.
331 case Instruction::AtomicRMW: {
332 // atomicrmw conceptually includes both a load and store from
333 // the same location.
334 // As with a store, the location being accessed is not captured,
335 // but the value being stored is.
336 // Volatile stores make the address observable.
338 if (U.getOperandNo() == 1 || ARMWI->isVolatile())
342 case Instruction::AtomicCmpXchg: {
343 // cmpxchg conceptually includes both a load and store from
344 // the same location.
345 // As with a store, the location being accessed is not captured,
346 // but the value being stored is.
347 // Volatile stores make the address observable.
349 if (U.getOperandNo() == 1 || U.getOperandNo() == 2 || ACXI->isVolatile())
353 case Instruction::GetElementPtr:
354 // AA does not support pointers of vectors, so GEP vector splats need to
355 // be considered as captures.
356 if (
I->getType()->isVectorTy())
359 case Instruction::BitCast:
360 case Instruction::PHI:
361 case Instruction::Select:
362 case Instruction::AddrSpaceCast:
363 // The original value is not captured via this if the new value isn't.
365 case Instruction::PtrToAddr:
366 // We treat ptrtoaddr as a location-independent capture of the address even
367 // if it is ultimately not used. Continuing recursive analysis after
368 // ptrtoaddr would be possible, but we'd need logic to do that correctly,
369 // which is not the same as the current pointer following logic.
371 case Instruction::ICmp: {
372 unsigned Idx = U.getOperandNo();
373 unsigned OtherIdx = 1 - Idx;
376 // TODO(captures): Remove these special cases once we make use of
377 // captures(address_is_null).
379 // Don't count comparisons of a no-alias return value against null as
380 // captures. This allows us to ignore comparisons of malloc results
381 // with null, for example.
382 if (U->getType()->getPointerAddressSpace() == 0)
386 // Check whether this is a comparison of the base pointer against
392 // Otherwise, be conservative. There are crazy ways to capture pointers
393 // using comparisons. However, only the address is captured, not the
398 // Something else - be conservative and say it is captured.
404 unsigned MaxUsesToExplore) {
405 assert(V->getType()->isPointerTy() &&
"Capture is for pointers only!");
406 if (MaxUsesToExplore == 0)
413 auto AddUses = [&](
const Value *V) {
414 for (
const Use &U : V->
uses()) {
415 // If there are lots of uses, conservatively say that the value
416 // is captured to avoid taking too much compile time.
417 if (Visited.
size() >= MaxUsesToExplore) {
421 if (!Visited.
insert(&U).second)
432 while (!Worklist.
empty()) {
442 // Fall through to passthrough handling, but only if ResultCC contains
443 // additional components that UseCC does not. We assume that a
444 // capture at this point will be strictly more constraining than a
445 // later capture from following the return value.
451 // TODO(captures): We could keep track of ResultCC for the users.
456 // All uses examined.
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static cl::opt< unsigned > DefaultMaxUsesToExplore("capture-tracking-max-uses-to-explore", cl::Hidden, cl::desc("Maximal number of uses to explore."), cl::init(100))
The default value for MaxUsesToExplore argument.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Represents which components of the pointer may be captured in which location.
CaptureComponents getOtherComponents() const
Get components potentially captured through locations other than the return value.
CaptureComponents getRetComponents() const
Get components potentially captured by the return value.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
LLVM_ABI bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
LLVM_ABI Instruction * findNearestCommonDominator(Instruction *I1, Instruction *I2) const
Find the nearest instruction I that dominates both I1 and I2, in the sense that a result produced bef...
static LLVM_ABI CaptureComponents toCaptureComponents(const MDNode *MD)
Convert !captures metadata to CaptureComponents. MD may be nullptr.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
A Use represents the edge between a Value definition and its users.
LLVM Value Representation.
iterator_range< use_iterator > uses()
An efficient, type-erasing, non-owning reference to a callable.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
initializer< Ty > init(const Ty &Val)
friend class Instruction
Iterator for Instructions in a `BasicBlock.
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI std::pair< Instruction *, CaptureComponents > FindEarliestCapture(const Value *V, Function &F, bool ReturnCaptures, const DominatorTree &DT, CaptureComponents Mask, unsigned MaxUsesToExplore=0)
LLVM_ABI bool isNoAliasCall(const Value *V)
Return true if this pointer is returned by a noalias function.
LLVM_ABI unsigned getDefaultMaxUsesToExploreForCaptureTracking()
getDefaultMaxUsesToExploreForCaptureTracking - Return default value of the maximal number of uses to ...
LLVM_ABI bool PointerMayBeCapturedBefore(const Value *V, bool ReturnCaptures, const Instruction *I, const DominatorTree *DT, bool IncludeI=false, unsigned MaxUsesToExplore=0, const LoopInfo *LI=nullptr)
PointerMayBeCapturedBefore - Return true if this pointer value may be captured by the enclosing funct...
LLVM_ABI bool isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(const CallBase *Call, bool MustPreserveNullness)
{launder,strip}.invariant.group returns pointer that aliases its argument, and it only captures point...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
CaptureComponents
Components of the pointer that may be captured.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
LLVM_ABI bool PointerMayBeCaptured(const Value *V, bool ReturnCaptures, unsigned MaxUsesToExplore=0)
PointerMayBeCaptured - Return true if this pointer value may be captured by the enclosing function (w...
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
bool capturesAnything(CaptureComponents CC)
LLVM_ABI UseCaptureInfo DetermineUseCaptureKind(const Use &U, const Value *Base)
Determine what kind of capture behaviour U may exhibit.
bool capturesNothing(CaptureComponents CC)
LLVM_ABI bool isPotentiallyReachable(const Instruction *From, const Instruction *To, const SmallPtrSetImpl< BasicBlock * > *ExclusionSet=nullptr, const DominatorTree *DT=nullptr, const LoopInfo *LI=nullptr)
Determine whether instruction 'To' is reachable from 'From', without passing through any blocks in Ex...
This callback is used in conjunction with PointerMayBeCaptured.
virtual bool shouldExplore(const Use *U)
shouldExplore - This is the use of a value derived from the pointer.
@ ContinueIgnoringReturn
Continue traversal, but do not follow the return value of the user, even if it has additional capture...
@ Continue
Continue traversal, and also follow the return value of the user if it has additional capture compone...
@ Stop
Stop the traversal.
virtual Action captured(const Use *U, UseCaptureInfo CI)=0
Use U directly captures CI.UseCC and additionally CI.ResultCC through the return value of the user of...
virtual void tooManyUses()=0
tooManyUses - The depth of traversal has breached a limit.
virtual ~CaptureTracker()
Capture information for a specific Use.
CaptureComponents UseCC
Components captured by this use.
CaptureComponents ResultCC
Components captured by the return value of the user of this Use.
static UseCaptureInfo passthrough()