xref: /aosp_15_r20/external/llvm/lib/Analysis/CaptureTracking.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===--- CaptureTracking.cpp - Determine whether a pointer is captured ----===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file contains routines that help determine which pointers are captured.
11*9880d681SAndroid Build Coastguard Worker // A pointer value is captured if the function makes a copy of any part of the
12*9880d681SAndroid Build Coastguard Worker // pointer that outlives the call.  Not being captured means, more or less, that
13*9880d681SAndroid Build Coastguard Worker // the pointer is only dereferenced and not stored in a global.  Returning part
14*9880d681SAndroid Build Coastguard Worker // of the pointer as the function return value may or may not count as capturing
15*9880d681SAndroid Build Coastguard Worker // the pointer, depending on the context.
16*9880d681SAndroid Build Coastguard Worker //
17*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
18*9880d681SAndroid Build Coastguard Worker 
19*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallSet.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallVector.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AliasAnalysis.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/CFG.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/CaptureTracking.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/OrderedBasicBlock.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/CallSite.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constants.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Dominators.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IntrinsicInst.h"
30*9880d681SAndroid Build Coastguard Worker 
31*9880d681SAndroid Build Coastguard Worker using namespace llvm;
32*9880d681SAndroid Build Coastguard Worker 
~CaptureTracker()33*9880d681SAndroid Build Coastguard Worker CaptureTracker::~CaptureTracker() {}
34*9880d681SAndroid Build Coastguard Worker 
shouldExplore(const Use * U)35*9880d681SAndroid Build Coastguard Worker bool CaptureTracker::shouldExplore(const Use *U) { return true; }
36*9880d681SAndroid Build Coastguard Worker 
37*9880d681SAndroid Build Coastguard Worker namespace {
38*9880d681SAndroid Build Coastguard Worker   struct SimpleCaptureTracker : public CaptureTracker {
SimpleCaptureTracker__anonbc9886820111::SimpleCaptureTracker39*9880d681SAndroid Build Coastguard Worker     explicit SimpleCaptureTracker(bool ReturnCaptures)
40*9880d681SAndroid Build Coastguard Worker       : ReturnCaptures(ReturnCaptures), Captured(false) {}
41*9880d681SAndroid Build Coastguard Worker 
tooManyUses__anonbc9886820111::SimpleCaptureTracker42*9880d681SAndroid Build Coastguard Worker     void tooManyUses() override { Captured = true; }
43*9880d681SAndroid Build Coastguard Worker 
captured__anonbc9886820111::SimpleCaptureTracker44*9880d681SAndroid Build Coastguard Worker     bool captured(const Use *U) override {
45*9880d681SAndroid Build Coastguard Worker       if (isa<ReturnInst>(U->getUser()) && !ReturnCaptures)
46*9880d681SAndroid Build Coastguard Worker         return false;
47*9880d681SAndroid Build Coastguard Worker 
48*9880d681SAndroid Build Coastguard Worker       Captured = true;
49*9880d681SAndroid Build Coastguard Worker       return true;
50*9880d681SAndroid Build Coastguard Worker     }
51*9880d681SAndroid Build Coastguard Worker 
52*9880d681SAndroid Build Coastguard Worker     bool ReturnCaptures;
53*9880d681SAndroid Build Coastguard Worker 
54*9880d681SAndroid Build Coastguard Worker     bool Captured;
55*9880d681SAndroid Build Coastguard Worker   };
56*9880d681SAndroid Build Coastguard Worker 
57*9880d681SAndroid Build Coastguard Worker   /// Only find pointer captures which happen before the given instruction. Uses
58*9880d681SAndroid Build Coastguard Worker   /// the dominator tree to determine whether one instruction is before another.
59*9880d681SAndroid Build Coastguard Worker   /// Only support the case where the Value is defined in the same basic block
60*9880d681SAndroid Build Coastguard Worker   /// as the given instruction and the use.
61*9880d681SAndroid Build Coastguard Worker   struct CapturesBefore : public CaptureTracker {
62*9880d681SAndroid Build Coastguard Worker 
CapturesBefore__anonbc9886820111::CapturesBefore63*9880d681SAndroid Build Coastguard Worker     CapturesBefore(bool ReturnCaptures, const Instruction *I, DominatorTree *DT,
64*9880d681SAndroid Build Coastguard Worker                    bool IncludeI, OrderedBasicBlock *IC)
65*9880d681SAndroid Build Coastguard Worker       : OrderedBB(IC), BeforeHere(I), DT(DT),
66*9880d681SAndroid Build Coastguard Worker         ReturnCaptures(ReturnCaptures), IncludeI(IncludeI), Captured(false) {}
67*9880d681SAndroid Build Coastguard Worker 
tooManyUses__anonbc9886820111::CapturesBefore68*9880d681SAndroid Build Coastguard Worker     void tooManyUses() override { Captured = true; }
69*9880d681SAndroid Build Coastguard Worker 
isSafeToPrune__anonbc9886820111::CapturesBefore70*9880d681SAndroid Build Coastguard Worker     bool isSafeToPrune(Instruction *I) {
71*9880d681SAndroid Build Coastguard Worker       BasicBlock *BB = I->getParent();
72*9880d681SAndroid Build Coastguard Worker       // We explore this usage only if the usage can reach "BeforeHere".
73*9880d681SAndroid Build Coastguard Worker       // If use is not reachable from entry, there is no need to explore.
74*9880d681SAndroid Build Coastguard Worker       if (BeforeHere != I && !DT->isReachableFromEntry(BB))
75*9880d681SAndroid Build Coastguard Worker         return true;
76*9880d681SAndroid Build Coastguard Worker 
77*9880d681SAndroid Build Coastguard Worker       // Compute the case where both instructions are inside the same basic
78*9880d681SAndroid Build Coastguard Worker       // block. Since instructions in the same BB as BeforeHere are numbered in
79*9880d681SAndroid Build Coastguard Worker       // 'OrderedBB', avoid using 'dominates' and 'isPotentiallyReachable'
80*9880d681SAndroid Build Coastguard Worker       // which are very expensive for large basic blocks.
81*9880d681SAndroid Build Coastguard Worker       if (BB == BeforeHere->getParent()) {
82*9880d681SAndroid Build Coastguard Worker         // 'I' dominates 'BeforeHere' => not safe to prune.
83*9880d681SAndroid Build Coastguard Worker         //
84*9880d681SAndroid Build Coastguard Worker         // The value defined by an invoke dominates an instruction only
85*9880d681SAndroid Build Coastguard Worker         // if it dominates every instruction in UseBB. A PHI is dominated only
86*9880d681SAndroid Build Coastguard Worker         // if the instruction dominates every possible use in the UseBB. Since
87*9880d681SAndroid Build Coastguard Worker         // UseBB == BB, avoid pruning.
88*9880d681SAndroid Build Coastguard Worker         if (isa<InvokeInst>(BeforeHere) || isa<PHINode>(I) || I == BeforeHere)
89*9880d681SAndroid Build Coastguard Worker           return false;
90*9880d681SAndroid Build Coastguard Worker         if (!OrderedBB->dominates(BeforeHere, I))
91*9880d681SAndroid Build Coastguard Worker           return false;
92*9880d681SAndroid Build Coastguard Worker 
93*9880d681SAndroid Build Coastguard Worker         // 'BeforeHere' comes before 'I', it's safe to prune if we also
94*9880d681SAndroid Build Coastguard Worker         // guarantee that 'I' never reaches 'BeforeHere' through a back-edge or
95*9880d681SAndroid Build Coastguard Worker         // by its successors, i.e, prune if:
96*9880d681SAndroid Build Coastguard Worker         //
97*9880d681SAndroid Build Coastguard Worker         //  (1) BB is an entry block or have no sucessors.
98*9880d681SAndroid Build Coastguard Worker         //  (2) There's no path coming back through BB sucessors.
99*9880d681SAndroid Build Coastguard Worker         if (BB == &BB->getParent()->getEntryBlock() ||
100*9880d681SAndroid Build Coastguard Worker             !BB->getTerminator()->getNumSuccessors())
101*9880d681SAndroid Build Coastguard Worker           return true;
102*9880d681SAndroid Build Coastguard Worker 
103*9880d681SAndroid Build Coastguard Worker         SmallVector<BasicBlock*, 32> Worklist;
104*9880d681SAndroid Build Coastguard Worker         Worklist.append(succ_begin(BB), succ_end(BB));
105*9880d681SAndroid Build Coastguard Worker         return !isPotentiallyReachableFromMany(Worklist, BB, DT);
106*9880d681SAndroid Build Coastguard Worker       }
107*9880d681SAndroid Build Coastguard Worker 
108*9880d681SAndroid Build Coastguard Worker       // If the value is defined in the same basic block as use and BeforeHere,
109*9880d681SAndroid Build Coastguard Worker       // there is no need to explore the use if BeforeHere dominates use.
110*9880d681SAndroid Build Coastguard Worker       // Check whether there is a path from I to BeforeHere.
111*9880d681SAndroid Build Coastguard Worker       if (BeforeHere != I && DT->dominates(BeforeHere, I) &&
112*9880d681SAndroid Build Coastguard Worker           !isPotentiallyReachable(I, BeforeHere, DT))
113*9880d681SAndroid Build Coastguard Worker         return true;
114*9880d681SAndroid Build Coastguard Worker 
115*9880d681SAndroid Build Coastguard Worker       return false;
116*9880d681SAndroid Build Coastguard Worker     }
117*9880d681SAndroid Build Coastguard Worker 
shouldExplore__anonbc9886820111::CapturesBefore118*9880d681SAndroid Build Coastguard Worker     bool shouldExplore(const Use *U) override {
119*9880d681SAndroid Build Coastguard Worker       Instruction *I = cast<Instruction>(U->getUser());
120*9880d681SAndroid Build Coastguard Worker 
121*9880d681SAndroid Build Coastguard Worker       if (BeforeHere == I && !IncludeI)
122*9880d681SAndroid Build Coastguard Worker         return false;
123*9880d681SAndroid Build Coastguard Worker 
124*9880d681SAndroid Build Coastguard Worker       if (isSafeToPrune(I))
125*9880d681SAndroid Build Coastguard Worker         return false;
126*9880d681SAndroid Build Coastguard Worker 
127*9880d681SAndroid Build Coastguard Worker       return true;
128*9880d681SAndroid Build Coastguard Worker     }
129*9880d681SAndroid Build Coastguard Worker 
captured__anonbc9886820111::CapturesBefore130*9880d681SAndroid Build Coastguard Worker     bool captured(const Use *U) override {
131*9880d681SAndroid Build Coastguard Worker       if (isa<ReturnInst>(U->getUser()) && !ReturnCaptures)
132*9880d681SAndroid Build Coastguard Worker         return false;
133*9880d681SAndroid Build Coastguard Worker 
134*9880d681SAndroid Build Coastguard Worker       if (!shouldExplore(U))
135*9880d681SAndroid Build Coastguard Worker         return false;
136*9880d681SAndroid Build Coastguard Worker 
137*9880d681SAndroid Build Coastguard Worker       Captured = true;
138*9880d681SAndroid Build Coastguard Worker       return true;
139*9880d681SAndroid Build Coastguard Worker     }
140*9880d681SAndroid Build Coastguard Worker 
141*9880d681SAndroid Build Coastguard Worker     OrderedBasicBlock *OrderedBB;
142*9880d681SAndroid Build Coastguard Worker     const Instruction *BeforeHere;
143*9880d681SAndroid Build Coastguard Worker     DominatorTree *DT;
144*9880d681SAndroid Build Coastguard Worker 
145*9880d681SAndroid Build Coastguard Worker     bool ReturnCaptures;
146*9880d681SAndroid Build Coastguard Worker     bool IncludeI;
147*9880d681SAndroid Build Coastguard Worker 
148*9880d681SAndroid Build Coastguard Worker     bool Captured;
149*9880d681SAndroid Build Coastguard Worker   };
150*9880d681SAndroid Build Coastguard Worker }
151*9880d681SAndroid Build Coastguard Worker 
152*9880d681SAndroid Build Coastguard Worker /// PointerMayBeCaptured - Return true if this pointer value may be captured
153*9880d681SAndroid Build Coastguard Worker /// by the enclosing function (which is required to exist).  This routine can
154*9880d681SAndroid Build Coastguard Worker /// be expensive, so consider caching the results.  The boolean ReturnCaptures
155*9880d681SAndroid Build Coastguard Worker /// specifies whether returning the value (or part of it) from the function
156*9880d681SAndroid Build Coastguard Worker /// counts as capturing it or not.  The boolean StoreCaptures specified whether
157*9880d681SAndroid Build Coastguard Worker /// storing the value (or part of it) into memory anywhere automatically
158*9880d681SAndroid Build Coastguard Worker /// counts as capturing it or not.
PointerMayBeCaptured(const Value * V,bool ReturnCaptures,bool StoreCaptures)159*9880d681SAndroid Build Coastguard Worker bool llvm::PointerMayBeCaptured(const Value *V,
160*9880d681SAndroid Build Coastguard Worker                                 bool ReturnCaptures, bool StoreCaptures) {
161*9880d681SAndroid Build Coastguard Worker   assert(!isa<GlobalValue>(V) &&
162*9880d681SAndroid Build Coastguard Worker          "It doesn't make sense to ask whether a global is captured.");
163*9880d681SAndroid Build Coastguard Worker 
164*9880d681SAndroid Build Coastguard Worker   // TODO: If StoreCaptures is not true, we could do Fancy analysis
165*9880d681SAndroid Build Coastguard Worker   // to determine whether this store is not actually an escape point.
166*9880d681SAndroid Build Coastguard Worker   // In that case, BasicAliasAnalysis should be updated as well to
167*9880d681SAndroid Build Coastguard Worker   // take advantage of this.
168*9880d681SAndroid Build Coastguard Worker   (void)StoreCaptures;
169*9880d681SAndroid Build Coastguard Worker 
170*9880d681SAndroid Build Coastguard Worker   SimpleCaptureTracker SCT(ReturnCaptures);
171*9880d681SAndroid Build Coastguard Worker   PointerMayBeCaptured(V, &SCT);
172*9880d681SAndroid Build Coastguard Worker   return SCT.Captured;
173*9880d681SAndroid Build Coastguard Worker }
174*9880d681SAndroid Build Coastguard Worker 
175*9880d681SAndroid Build Coastguard Worker /// PointerMayBeCapturedBefore - Return true if this pointer value may be
176*9880d681SAndroid Build Coastguard Worker /// captured by the enclosing function (which is required to exist). If a
177*9880d681SAndroid Build Coastguard Worker /// DominatorTree is provided, only captures which happen before the given
178*9880d681SAndroid Build Coastguard Worker /// instruction are considered. This routine can be expensive, so consider
179*9880d681SAndroid Build Coastguard Worker /// caching the results.  The boolean ReturnCaptures specifies whether
180*9880d681SAndroid Build Coastguard Worker /// returning the value (or part of it) from the function counts as capturing
181*9880d681SAndroid Build Coastguard Worker /// it or not.  The boolean StoreCaptures specified whether storing the value
182*9880d681SAndroid Build Coastguard Worker /// (or part of it) into memory anywhere automatically counts as capturing it
183*9880d681SAndroid Build Coastguard Worker /// or not. A ordered basic block \p OBB can be used in order to speed up
184*9880d681SAndroid Build Coastguard Worker /// queries about relative order among instructions in the same basic block.
PointerMayBeCapturedBefore(const Value * V,bool ReturnCaptures,bool StoreCaptures,const Instruction * I,DominatorTree * DT,bool IncludeI,OrderedBasicBlock * OBB)185*9880d681SAndroid Build Coastguard Worker bool llvm::PointerMayBeCapturedBefore(const Value *V, bool ReturnCaptures,
186*9880d681SAndroid Build Coastguard Worker                                       bool StoreCaptures, const Instruction *I,
187*9880d681SAndroid Build Coastguard Worker                                       DominatorTree *DT, bool IncludeI,
188*9880d681SAndroid Build Coastguard Worker                                       OrderedBasicBlock *OBB) {
189*9880d681SAndroid Build Coastguard Worker   assert(!isa<GlobalValue>(V) &&
190*9880d681SAndroid Build Coastguard Worker          "It doesn't make sense to ask whether a global is captured.");
191*9880d681SAndroid Build Coastguard Worker   bool UseNewOBB = OBB == nullptr;
192*9880d681SAndroid Build Coastguard Worker 
193*9880d681SAndroid Build Coastguard Worker   if (!DT)
194*9880d681SAndroid Build Coastguard Worker     return PointerMayBeCaptured(V, ReturnCaptures, StoreCaptures);
195*9880d681SAndroid Build Coastguard Worker   if (UseNewOBB)
196*9880d681SAndroid Build Coastguard Worker     OBB = new OrderedBasicBlock(I->getParent());
197*9880d681SAndroid Build Coastguard Worker 
198*9880d681SAndroid Build Coastguard Worker   // TODO: See comment in PointerMayBeCaptured regarding what could be done
199*9880d681SAndroid Build Coastguard Worker   // with StoreCaptures.
200*9880d681SAndroid Build Coastguard Worker 
201*9880d681SAndroid Build Coastguard Worker   CapturesBefore CB(ReturnCaptures, I, DT, IncludeI, OBB);
202*9880d681SAndroid Build Coastguard Worker   PointerMayBeCaptured(V, &CB);
203*9880d681SAndroid Build Coastguard Worker 
204*9880d681SAndroid Build Coastguard Worker   if (UseNewOBB)
205*9880d681SAndroid Build Coastguard Worker     delete OBB;
206*9880d681SAndroid Build Coastguard Worker   return CB.Captured;
207*9880d681SAndroid Build Coastguard Worker }
208*9880d681SAndroid Build Coastguard Worker 
209*9880d681SAndroid Build Coastguard Worker /// TODO: Write a new FunctionPass AliasAnalysis so that it can keep
210*9880d681SAndroid Build Coastguard Worker /// a cache. Then we can move the code from BasicAliasAnalysis into
211*9880d681SAndroid Build Coastguard Worker /// that path, and remove this threshold.
212*9880d681SAndroid Build Coastguard Worker static int const Threshold = 20;
213*9880d681SAndroid Build Coastguard Worker 
PointerMayBeCaptured(const Value * V,CaptureTracker * Tracker)214*9880d681SAndroid Build Coastguard Worker void llvm::PointerMayBeCaptured(const Value *V, CaptureTracker *Tracker) {
215*9880d681SAndroid Build Coastguard Worker   assert(V->getType()->isPointerTy() && "Capture is for pointers only!");
216*9880d681SAndroid Build Coastguard Worker   SmallVector<const Use *, Threshold> Worklist;
217*9880d681SAndroid Build Coastguard Worker   SmallSet<const Use *, Threshold> Visited;
218*9880d681SAndroid Build Coastguard Worker   int Count = 0;
219*9880d681SAndroid Build Coastguard Worker 
220*9880d681SAndroid Build Coastguard Worker   for (const Use &U : V->uses()) {
221*9880d681SAndroid Build Coastguard Worker     // If there are lots of uses, conservatively say that the value
222*9880d681SAndroid Build Coastguard Worker     // is captured to avoid taking too much compile time.
223*9880d681SAndroid Build Coastguard Worker     if (Count++ >= Threshold)
224*9880d681SAndroid Build Coastguard Worker       return Tracker->tooManyUses();
225*9880d681SAndroid Build Coastguard Worker 
226*9880d681SAndroid Build Coastguard Worker     if (!Tracker->shouldExplore(&U)) continue;
227*9880d681SAndroid Build Coastguard Worker     Visited.insert(&U);
228*9880d681SAndroid Build Coastguard Worker     Worklist.push_back(&U);
229*9880d681SAndroid Build Coastguard Worker   }
230*9880d681SAndroid Build Coastguard Worker 
231*9880d681SAndroid Build Coastguard Worker   while (!Worklist.empty()) {
232*9880d681SAndroid Build Coastguard Worker     const Use *U = Worklist.pop_back_val();
233*9880d681SAndroid Build Coastguard Worker     Instruction *I = cast<Instruction>(U->getUser());
234*9880d681SAndroid Build Coastguard Worker     V = U->get();
235*9880d681SAndroid Build Coastguard Worker 
236*9880d681SAndroid Build Coastguard Worker     switch (I->getOpcode()) {
237*9880d681SAndroid Build Coastguard Worker     case Instruction::Call:
238*9880d681SAndroid Build Coastguard Worker     case Instruction::Invoke: {
239*9880d681SAndroid Build Coastguard Worker       CallSite CS(I);
240*9880d681SAndroid Build Coastguard Worker       // Not captured if the callee is readonly, doesn't return a copy through
241*9880d681SAndroid Build Coastguard Worker       // its return value and doesn't unwind (a readonly function can leak bits
242*9880d681SAndroid Build Coastguard Worker       // by throwing an exception or not depending on the input value).
243*9880d681SAndroid Build Coastguard Worker       if (CS.onlyReadsMemory() && CS.doesNotThrow() && I->getType()->isVoidTy())
244*9880d681SAndroid Build Coastguard Worker         break;
245*9880d681SAndroid Build Coastguard Worker 
246*9880d681SAndroid Build Coastguard Worker       // Volatile operations effectively capture the memory location that they
247*9880d681SAndroid Build Coastguard Worker       // load and store to.
248*9880d681SAndroid Build Coastguard Worker       if (auto *MI = dyn_cast<MemIntrinsic>(I))
249*9880d681SAndroid Build Coastguard Worker         if (MI->isVolatile())
250*9880d681SAndroid Build Coastguard Worker           if (Tracker->captured(U))
251*9880d681SAndroid Build Coastguard Worker             return;
252*9880d681SAndroid Build Coastguard Worker 
253*9880d681SAndroid Build Coastguard Worker       // Not captured if only passed via 'nocapture' arguments.  Note that
254*9880d681SAndroid Build Coastguard Worker       // calling a function pointer does not in itself cause the pointer to
255*9880d681SAndroid Build Coastguard Worker       // be captured.  This is a subtle point considering that (for example)
256*9880d681SAndroid Build Coastguard Worker       // the callee might return its own address.  It is analogous to saying
257*9880d681SAndroid Build Coastguard Worker       // that loading a value from a pointer does not cause the pointer to be
258*9880d681SAndroid Build Coastguard Worker       // captured, even though the loaded value might be the pointer itself
259*9880d681SAndroid Build Coastguard Worker       // (think of self-referential objects).
260*9880d681SAndroid Build Coastguard Worker       CallSite::data_operand_iterator B =
261*9880d681SAndroid Build Coastguard Worker         CS.data_operands_begin(), E = CS.data_operands_end();
262*9880d681SAndroid Build Coastguard Worker       for (CallSite::data_operand_iterator A = B; A != E; ++A)
263*9880d681SAndroid Build Coastguard Worker         if (A->get() == V && !CS.doesNotCapture(A - B))
264*9880d681SAndroid Build Coastguard Worker           // The parameter is not marked 'nocapture' - captured.
265*9880d681SAndroid Build Coastguard Worker           if (Tracker->captured(U))
266*9880d681SAndroid Build Coastguard Worker             return;
267*9880d681SAndroid Build Coastguard Worker       break;
268*9880d681SAndroid Build Coastguard Worker     }
269*9880d681SAndroid Build Coastguard Worker     case Instruction::Load:
270*9880d681SAndroid Build Coastguard Worker       // Volatile loads make the address observable.
271*9880d681SAndroid Build Coastguard Worker       if (cast<LoadInst>(I)->isVolatile())
272*9880d681SAndroid Build Coastguard Worker         if (Tracker->captured(U))
273*9880d681SAndroid Build Coastguard Worker           return;
274*9880d681SAndroid Build Coastguard Worker       break;
275*9880d681SAndroid Build Coastguard Worker     case Instruction::VAArg:
276*9880d681SAndroid Build Coastguard Worker       // "va-arg" from a pointer does not cause it to be captured.
277*9880d681SAndroid Build Coastguard Worker       break;
278*9880d681SAndroid Build Coastguard Worker     case Instruction::Store:
279*9880d681SAndroid Build Coastguard Worker         // Stored the pointer - conservatively assume it may be captured.
280*9880d681SAndroid Build Coastguard Worker         // Volatile stores make the address observable.
281*9880d681SAndroid Build Coastguard Worker       if (V == I->getOperand(0) || cast<StoreInst>(I)->isVolatile())
282*9880d681SAndroid Build Coastguard Worker         if (Tracker->captured(U))
283*9880d681SAndroid Build Coastguard Worker           return;
284*9880d681SAndroid Build Coastguard Worker       break;
285*9880d681SAndroid Build Coastguard Worker     case Instruction::AtomicRMW: {
286*9880d681SAndroid Build Coastguard Worker       // atomicrmw conceptually includes both a load and store from
287*9880d681SAndroid Build Coastguard Worker       // the same location.
288*9880d681SAndroid Build Coastguard Worker       // As with a store, the location being accessed is not captured,
289*9880d681SAndroid Build Coastguard Worker       // but the value being stored is.
290*9880d681SAndroid Build Coastguard Worker       // Volatile stores make the address observable.
291*9880d681SAndroid Build Coastguard Worker       auto *ARMWI = cast<AtomicRMWInst>(I);
292*9880d681SAndroid Build Coastguard Worker       if (ARMWI->getValOperand() == V || ARMWI->isVolatile())
293*9880d681SAndroid Build Coastguard Worker         if (Tracker->captured(U))
294*9880d681SAndroid Build Coastguard Worker           return;
295*9880d681SAndroid Build Coastguard Worker       break;
296*9880d681SAndroid Build Coastguard Worker     }
297*9880d681SAndroid Build Coastguard Worker     case Instruction::AtomicCmpXchg: {
298*9880d681SAndroid Build Coastguard Worker       // cmpxchg conceptually includes both a load and store from
299*9880d681SAndroid Build Coastguard Worker       // the same location.
300*9880d681SAndroid Build Coastguard Worker       // As with a store, the location being accessed is not captured,
301*9880d681SAndroid Build Coastguard Worker       // but the value being stored is.
302*9880d681SAndroid Build Coastguard Worker       // Volatile stores make the address observable.
303*9880d681SAndroid Build Coastguard Worker       auto *ACXI = cast<AtomicCmpXchgInst>(I);
304*9880d681SAndroid Build Coastguard Worker       if (ACXI->getCompareOperand() == V || ACXI->getNewValOperand() == V ||
305*9880d681SAndroid Build Coastguard Worker           ACXI->isVolatile())
306*9880d681SAndroid Build Coastguard Worker         if (Tracker->captured(U))
307*9880d681SAndroid Build Coastguard Worker           return;
308*9880d681SAndroid Build Coastguard Worker       break;
309*9880d681SAndroid Build Coastguard Worker     }
310*9880d681SAndroid Build Coastguard Worker     case Instruction::BitCast:
311*9880d681SAndroid Build Coastguard Worker     case Instruction::GetElementPtr:
312*9880d681SAndroid Build Coastguard Worker     case Instruction::PHI:
313*9880d681SAndroid Build Coastguard Worker     case Instruction::Select:
314*9880d681SAndroid Build Coastguard Worker     case Instruction::AddrSpaceCast:
315*9880d681SAndroid Build Coastguard Worker       // The original value is not captured via this if the new value isn't.
316*9880d681SAndroid Build Coastguard Worker       Count = 0;
317*9880d681SAndroid Build Coastguard Worker       for (Use &UU : I->uses()) {
318*9880d681SAndroid Build Coastguard Worker         // If there are lots of uses, conservatively say that the value
319*9880d681SAndroid Build Coastguard Worker         // is captured to avoid taking too much compile time.
320*9880d681SAndroid Build Coastguard Worker         if (Count++ >= Threshold)
321*9880d681SAndroid Build Coastguard Worker           return Tracker->tooManyUses();
322*9880d681SAndroid Build Coastguard Worker 
323*9880d681SAndroid Build Coastguard Worker         if (Visited.insert(&UU).second)
324*9880d681SAndroid Build Coastguard Worker           if (Tracker->shouldExplore(&UU))
325*9880d681SAndroid Build Coastguard Worker             Worklist.push_back(&UU);
326*9880d681SAndroid Build Coastguard Worker       }
327*9880d681SAndroid Build Coastguard Worker       break;
328*9880d681SAndroid Build Coastguard Worker     case Instruction::ICmp: {
329*9880d681SAndroid Build Coastguard Worker       // Don't count comparisons of a no-alias return value against null as
330*9880d681SAndroid Build Coastguard Worker       // captures. This allows us to ignore comparisons of malloc results
331*9880d681SAndroid Build Coastguard Worker       // with null, for example.
332*9880d681SAndroid Build Coastguard Worker       if (ConstantPointerNull *CPN =
333*9880d681SAndroid Build Coastguard Worker           dyn_cast<ConstantPointerNull>(I->getOperand(1)))
334*9880d681SAndroid Build Coastguard Worker         if (CPN->getType()->getAddressSpace() == 0)
335*9880d681SAndroid Build Coastguard Worker           if (isNoAliasCall(V->stripPointerCasts()))
336*9880d681SAndroid Build Coastguard Worker             break;
337*9880d681SAndroid Build Coastguard Worker       // Comparison against value stored in global variable. Given the pointer
338*9880d681SAndroid Build Coastguard Worker       // does not escape, its value cannot be guessed and stored separately in a
339*9880d681SAndroid Build Coastguard Worker       // global variable.
340*9880d681SAndroid Build Coastguard Worker       unsigned OtherIndex = (I->getOperand(0) == V) ? 1 : 0;
341*9880d681SAndroid Build Coastguard Worker       auto *LI = dyn_cast<LoadInst>(I->getOperand(OtherIndex));
342*9880d681SAndroid Build Coastguard Worker       if (LI && isa<GlobalVariable>(LI->getPointerOperand()))
343*9880d681SAndroid Build Coastguard Worker         break;
344*9880d681SAndroid Build Coastguard Worker       // Otherwise, be conservative. There are crazy ways to capture pointers
345*9880d681SAndroid Build Coastguard Worker       // using comparisons.
346*9880d681SAndroid Build Coastguard Worker       if (Tracker->captured(U))
347*9880d681SAndroid Build Coastguard Worker         return;
348*9880d681SAndroid Build Coastguard Worker       break;
349*9880d681SAndroid Build Coastguard Worker     }
350*9880d681SAndroid Build Coastguard Worker     default:
351*9880d681SAndroid Build Coastguard Worker       // Something else - be conservative and say it is captured.
352*9880d681SAndroid Build Coastguard Worker       if (Tracker->captured(U))
353*9880d681SAndroid Build Coastguard Worker         return;
354*9880d681SAndroid Build Coastguard Worker       break;
355*9880d681SAndroid Build Coastguard Worker     }
356*9880d681SAndroid Build Coastguard Worker   }
357*9880d681SAndroid Build Coastguard Worker 
358*9880d681SAndroid Build Coastguard Worker   // All uses examined.
359*9880d681SAndroid Build Coastguard Worker }
360