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 //===----------------------------------------------------------------------===//
18 #include "llvm/Analysis/CaptureTracking.h"
19 #include "llvm/ADT/SmallSet.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/Analysis/AliasAnalysis.h"
22 #include "llvm/Analysis/CFG.h"
23 #include "llvm/Analysis/OrderedBasicBlock.h"
24 #include "llvm/Analysis/ValueTracking.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/Dominators.h"
27 #include "llvm/IR/Instructions.h"
28 #include "llvm/IR/IntrinsicInst.h"
32 CaptureTracker::~CaptureTracker() {}
34 bool CaptureTracker::shouldExplore(const Use
*U
) { return true; }
37 struct SimpleCaptureTracker
: public CaptureTracker
{
38 explicit SimpleCaptureTracker(bool ReturnCaptures
)
39 : ReturnCaptures(ReturnCaptures
), Captured(false) {}
41 void tooManyUses() override
{ Captured
= true; }
43 bool captured(const Use
*U
) override
{
44 if (isa
<ReturnInst
>(U
->getUser()) && !ReturnCaptures
)
56 /// Only find pointer captures which happen before the given instruction. Uses
57 /// the dominator tree to determine whether one instruction is before another.
58 /// Only support the case where the Value is defined in the same basic block
59 /// as the given instruction and the use.
60 struct CapturesBefore
: public CaptureTracker
{
62 CapturesBefore(bool ReturnCaptures
, const Instruction
*I
, const DominatorTree
*DT
,
63 bool IncludeI
, OrderedBasicBlock
*IC
)
64 : OrderedBB(IC
), BeforeHere(I
), DT(DT
),
65 ReturnCaptures(ReturnCaptures
), IncludeI(IncludeI
), Captured(false) {}
67 void tooManyUses() override
{ Captured
= true; }
69 bool isSafeToPrune(Instruction
*I
) {
70 BasicBlock
*BB
= I
->getParent();
71 // We explore this usage only if the usage can reach "BeforeHere".
72 // If use is not reachable from entry, there is no need to explore.
73 if (BeforeHere
!= I
&& !DT
->isReachableFromEntry(BB
))
76 // Compute the case where both instructions are inside the same basic
77 // block. Since instructions in the same BB as BeforeHere are numbered in
78 // 'OrderedBB', avoid using 'dominates' and 'isPotentiallyReachable'
79 // which are very expensive for large basic blocks.
80 if (BB
== BeforeHere
->getParent()) {
81 // 'I' dominates 'BeforeHere' => not safe to prune.
83 // The value defined by an invoke dominates an instruction only
84 // if it dominates every instruction in UseBB. A PHI is dominated only
85 // if the instruction dominates every possible use in the UseBB. Since
86 // UseBB == BB, avoid pruning.
87 if (isa
<InvokeInst
>(BeforeHere
) || isa
<PHINode
>(I
) || I
== BeforeHere
)
89 if (!OrderedBB
->dominates(BeforeHere
, I
))
92 // 'BeforeHere' comes before 'I', it's safe to prune if we also
93 // guarantee that 'I' never reaches 'BeforeHere' through a back-edge or
94 // by its successors, i.e, prune if:
96 // (1) BB is an entry block or have no successors.
97 // (2) There's no path coming back through BB successors.
98 if (BB
== &BB
->getParent()->getEntryBlock() ||
99 !BB
->getTerminator()->getNumSuccessors())
102 SmallVector
<BasicBlock
*, 32> Worklist
;
103 Worklist
.append(succ_begin(BB
), succ_end(BB
));
104 return !isPotentiallyReachableFromMany(Worklist
, BB
, nullptr, DT
);
107 // If the value is defined in the same basic block as use and BeforeHere,
108 // there is no need to explore the use if BeforeHere dominates use.
109 // Check whether there is a path from I to BeforeHere.
110 if (BeforeHere
!= I
&& DT
->dominates(BeforeHere
, I
) &&
111 !isPotentiallyReachable(I
, BeforeHere
, nullptr, DT
))
117 bool shouldExplore(const Use
*U
) override
{
118 Instruction
*I
= cast
<Instruction
>(U
->getUser());
120 if (BeforeHere
== I
&& !IncludeI
)
123 if (isSafeToPrune(I
))
129 bool captured(const Use
*U
) override
{
130 if (isa
<ReturnInst
>(U
->getUser()) && !ReturnCaptures
)
133 if (!shouldExplore(U
))
140 OrderedBasicBlock
*OrderedBB
;
141 const Instruction
*BeforeHere
;
142 const DominatorTree
*DT
;
151 /// PointerMayBeCaptured - Return true if this pointer value may be captured
152 /// by the enclosing function (which is required to exist). This routine can
153 /// be expensive, so consider caching the results. The boolean ReturnCaptures
154 /// specifies whether returning the value (or part of it) from the function
155 /// counts as capturing it or not. The boolean StoreCaptures specified whether
156 /// storing the value (or part of it) into memory anywhere automatically
157 /// counts as capturing it or not.
158 bool llvm::PointerMayBeCaptured(const Value
*V
,
159 bool ReturnCaptures
, bool StoreCaptures
,
160 unsigned MaxUsesToExplore
) {
161 assert(!isa
<GlobalValue
>(V
) &&
162 "It doesn't make sense to ask whether a global is captured.");
164 // TODO: If StoreCaptures is not true, we could do Fancy analysis
165 // to determine whether this store is not actually an escape point.
166 // In that case, BasicAliasAnalysis should be updated as well to
167 // take advantage of this.
170 SimpleCaptureTracker
SCT(ReturnCaptures
);
171 PointerMayBeCaptured(V
, &SCT
, MaxUsesToExplore
);
175 /// PointerMayBeCapturedBefore - Return true if this pointer value may be
176 /// captured by the enclosing function (which is required to exist). If a
177 /// DominatorTree is provided, only captures which happen before the given
178 /// instruction are considered. This routine can be expensive, so consider
179 /// caching the results. The boolean ReturnCaptures specifies whether
180 /// returning the value (or part of it) from the function counts as capturing
181 /// it or not. The boolean StoreCaptures specified whether storing the value
182 /// (or part of it) into memory anywhere automatically counts as capturing it
183 /// or not. A ordered basic block \p OBB can be used in order to speed up
184 /// queries about relative order among instructions in the same basic block.
185 bool llvm::PointerMayBeCapturedBefore(const Value
*V
, bool ReturnCaptures
,
186 bool StoreCaptures
, const Instruction
*I
,
187 const DominatorTree
*DT
, bool IncludeI
,
188 OrderedBasicBlock
*OBB
,
189 unsigned MaxUsesToExplore
) {
190 assert(!isa
<GlobalValue
>(V
) &&
191 "It doesn't make sense to ask whether a global is captured.");
192 bool UseNewOBB
= OBB
== nullptr;
195 return PointerMayBeCaptured(V
, ReturnCaptures
, StoreCaptures
,
198 OBB
= new OrderedBasicBlock(I
->getParent());
200 // TODO: See comment in PointerMayBeCaptured regarding what could be done
201 // with StoreCaptures.
203 CapturesBefore
CB(ReturnCaptures
, I
, DT
, IncludeI
, OBB
);
204 PointerMayBeCaptured(V
, &CB
, MaxUsesToExplore
);
211 void llvm::PointerMayBeCaptured(const Value
*V
, CaptureTracker
*Tracker
,
212 unsigned MaxUsesToExplore
) {
213 assert(V
->getType()->isPointerTy() && "Capture is for pointers only!");
214 SmallVector
<const Use
*, DefaultMaxUsesToExplore
> Worklist
;
215 SmallSet
<const Use
*, DefaultMaxUsesToExplore
> Visited
;
217 auto AddUses
= [&](const Value
*V
) {
219 for (const Use
&U
: V
->uses()) {
220 // If there are lots of uses, conservatively say that the value
221 // is captured to avoid taking too much compile time.
222 if (Count
++ >= MaxUsesToExplore
)
223 return Tracker
->tooManyUses();
224 if (!Visited
.insert(&U
).second
)
226 if (!Tracker
->shouldExplore(&U
))
228 Worklist
.push_back(&U
);
233 while (!Worklist
.empty()) {
234 const Use
*U
= Worklist
.pop_back_val();
235 Instruction
*I
= cast
<Instruction
>(U
->getUser());
238 switch (I
->getOpcode()) {
239 case Instruction::Call
:
240 case Instruction::Invoke
: {
241 auto *Call
= cast
<CallBase
>(I
);
242 // Not captured if the callee is readonly, doesn't return a copy through
243 // its return value and doesn't unwind (a readonly function can leak bits
244 // by throwing an exception or not depending on the input value).
245 if (Call
->onlyReadsMemory() && Call
->doesNotThrow() &&
246 Call
->getType()->isVoidTy())
249 // The pointer is not captured if returned pointer is not captured.
250 // NOTE: CaptureTracking users should not assume that only functions
251 // marked with nocapture do not capture. This means that places like
252 // GetUnderlyingObject in ValueTracking or DecomposeGEPExpression
253 // in BasicAA also need to know about this property.
254 if (isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(Call
,
260 // Volatile operations effectively capture the memory location that they
261 // load and store to.
262 if (auto *MI
= dyn_cast
<MemIntrinsic
>(Call
))
263 if (MI
->isVolatile())
264 if (Tracker
->captured(U
))
267 // Not captured if only passed via 'nocapture' arguments. Note that
268 // calling a function pointer does not in itself cause the pointer to
269 // be captured. This is a subtle point considering that (for example)
270 // the callee might return its own address. It is analogous to saying
271 // that loading a value from a pointer does not cause the pointer to be
272 // captured, even though the loaded value might be the pointer itself
273 // (think of self-referential objects).
274 for (auto IdxOpPair
: enumerate(Call
->data_ops())) {
275 int Idx
= IdxOpPair
.index();
276 Value
*A
= IdxOpPair
.value();
277 if (A
== V
&& !Call
->doesNotCapture(Idx
))
278 // The parameter is not marked 'nocapture' - captured.
279 if (Tracker
->captured(U
))
284 case Instruction::Load
:
285 // Volatile loads make the address observable.
286 if (cast
<LoadInst
>(I
)->isVolatile())
287 if (Tracker
->captured(U
))
290 case Instruction::VAArg
:
291 // "va-arg" from a pointer does not cause it to be captured.
293 case Instruction::Store
:
294 // Stored the pointer - conservatively assume it may be captured.
295 // Volatile stores make the address observable.
296 if (V
== I
->getOperand(0) || cast
<StoreInst
>(I
)->isVolatile())
297 if (Tracker
->captured(U
))
300 case Instruction::AtomicRMW
: {
301 // atomicrmw conceptually includes both a load and store from
302 // the same location.
303 // As with a store, the location being accessed is not captured,
304 // but the value being stored is.
305 // Volatile stores make the address observable.
306 auto *ARMWI
= cast
<AtomicRMWInst
>(I
);
307 if (ARMWI
->getValOperand() == V
|| ARMWI
->isVolatile())
308 if (Tracker
->captured(U
))
312 case Instruction::AtomicCmpXchg
: {
313 // cmpxchg conceptually includes both a load and store from
314 // the same location.
315 // As with a store, the location being accessed is not captured,
316 // but the value being stored is.
317 // Volatile stores make the address observable.
318 auto *ACXI
= cast
<AtomicCmpXchgInst
>(I
);
319 if (ACXI
->getCompareOperand() == V
|| ACXI
->getNewValOperand() == V
||
321 if (Tracker
->captured(U
))
325 case Instruction::BitCast
:
326 case Instruction::GetElementPtr
:
327 case Instruction::PHI
:
328 case Instruction::Select
:
329 case Instruction::AddrSpaceCast
:
330 // The original value is not captured via this if the new value isn't.
333 case Instruction::ICmp
: {
334 unsigned Idx
= (I
->getOperand(0) == V
) ? 0 : 1;
335 unsigned OtherIdx
= 1 - Idx
;
336 if (auto *CPN
= dyn_cast
<ConstantPointerNull
>(I
->getOperand(OtherIdx
))) {
337 // Don't count comparisons of a no-alias return value against null as
338 // captures. This allows us to ignore comparisons of malloc results
339 // with null, for example.
340 if (CPN
->getType()->getAddressSpace() == 0)
341 if (isNoAliasCall(V
->stripPointerCasts()))
343 if (!I
->getFunction()->nullPointerIsDefined()) {
344 auto *O
= I
->getOperand(Idx
)->stripPointerCastsSameRepresentation();
345 // An inbounds GEP can either be a valid pointer (pointing into
346 // or to the end of an allocation), or be null in the default
347 // address space. So for an inbounds GEPs there is no way to let
348 // the pointer escape using clever GEP hacking because doing so
349 // would make the pointer point outside of the allocated object
350 // and thus make the GEP result a poison value.
351 if (auto *GEP
= dyn_cast
<GetElementPtrInst
>(O
))
352 if (GEP
->isInBounds())
354 // Comparing a dereferenceable_or_null argument against null
355 // cannot lead to pointer escapes, because if it is not null it
356 // must be a valid (in-bounds) pointer.
358 if (O
->getPointerDereferenceableBytes(I
->getModule()->getDataLayout(),
363 // Comparison against value stored in global variable. Given the pointer
364 // does not escape, its value cannot be guessed and stored separately in a
366 auto *LI
= dyn_cast
<LoadInst
>(I
->getOperand(OtherIdx
));
367 if (LI
&& isa
<GlobalVariable
>(LI
->getPointerOperand()))
369 // Otherwise, be conservative. There are crazy ways to capture pointers
370 // using comparisons.
371 if (Tracker
->captured(U
))
376 // Something else - be conservative and say it is captured.
377 if (Tracker
->captured(U
))
383 // All uses examined.