1e0aa0d67SDuncan Sands //===--- CaptureTracking.cpp - Determine whether a pointer is captured ----===//
2e0aa0d67SDuncan Sands //
3e0aa0d67SDuncan Sands //                     The LLVM Compiler Infrastructure
4e0aa0d67SDuncan Sands //
5e0aa0d67SDuncan Sands // This file is distributed under the University of Illinois Open Source
6e0aa0d67SDuncan Sands // License. See LICENSE.TXT for details.
7e0aa0d67SDuncan Sands //
8e0aa0d67SDuncan Sands //===----------------------------------------------------------------------===//
9e0aa0d67SDuncan Sands //
10e0aa0d67SDuncan Sands // This file contains routines that help determine which pointers are captured.
11e0aa0d67SDuncan Sands // A pointer value is captured if the function makes a copy of any part of the
12e0aa0d67SDuncan Sands // pointer that outlives the call.  Not being captured means, more or less, that
13e0aa0d67SDuncan Sands // the pointer is only dereferenced and not stored in a global.  Returning part
14e0aa0d67SDuncan Sands // of the pointer as the function return value may or may not count as capturing
15e0aa0d67SDuncan Sands // the pointer, depending on the context.
16e0aa0d67SDuncan Sands //
17e0aa0d67SDuncan Sands //===----------------------------------------------------------------------===//
18e0aa0d67SDuncan Sands 
196bda14b3SChandler Carruth #include "llvm/Analysis/CaptureTracking.h"
20173bce3dSJakub Staszak #include "llvm/ADT/SmallSet.h"
21173bce3dSJakub Staszak #include "llvm/ADT/SmallVector.h"
22c733bf26SJakub Staszak #include "llvm/Analysis/AliasAnalysis.h"
23b0356217SHal Finkel #include "llvm/Analysis/CFG.h"
24dfc1d96eSBruno Cardoso Lopes #include "llvm/Analysis/OrderedBasicBlock.h"
25219b89b9SChandler Carruth #include "llvm/IR/CallSite.h"
26c733bf26SJakub Staszak #include "llvm/IR/Constants.h"
27b0356217SHal Finkel #include "llvm/IR/Dominators.h"
28c733bf26SJakub Staszak #include "llvm/IR/Instructions.h"
297f32420eSDavid Majnemer #include "llvm/IR/IntrinsicInst.h"
30c733bf26SJakub Staszak 
31e0aa0d67SDuncan Sands using namespace llvm;
32e0aa0d67SDuncan Sands 
33aa2a00dbSNick Lewycky CaptureTracker::~CaptureTracker() {}
34aa2a00dbSNick Lewycky 
3564e9aa5cSChandler Carruth bool CaptureTracker::shouldExplore(const Use *U) { return true; }
367c3b5d94SNick Lewycky 
377013a19eSNick Lewycky namespace {
386ae03c33SNick Lewycky   struct SimpleCaptureTracker : public CaptureTracker {
397013a19eSNick Lewycky     explicit SimpleCaptureTracker(bool ReturnCaptures)
407013a19eSNick Lewycky       : ReturnCaptures(ReturnCaptures), Captured(false) {}
417013a19eSNick Lewycky 
42e9ba759cSCraig Topper     void tooManyUses() override { Captured = true; }
437013a19eSNick Lewycky 
4464e9aa5cSChandler Carruth     bool captured(const Use *U) override {
454c378a44SNick Lewycky       if (isa<ReturnInst>(U->getUser()) && !ReturnCaptures)
467013a19eSNick Lewycky         return false;
477013a19eSNick Lewycky 
487013a19eSNick Lewycky       Captured = true;
497013a19eSNick Lewycky       return true;
507013a19eSNick Lewycky     }
517013a19eSNick Lewycky 
527013a19eSNick Lewycky     bool ReturnCaptures;
537013a19eSNick Lewycky 
547013a19eSNick Lewycky     bool Captured;
557013a19eSNick Lewycky   };
56b0356217SHal Finkel 
57b0356217SHal Finkel   /// Only find pointer captures which happen before the given instruction. Uses
58b0356217SHal Finkel   /// the dominator tree to determine whether one instruction is before another.
59b0356217SHal Finkel   /// Only support the case where the Value is defined in the same basic block
60b0356217SHal Finkel   /// as the given instruction and the use.
61b0356217SHal Finkel   struct CapturesBefore : public CaptureTracker {
627900ef89SBruno Cardoso Lopes 
633c148720SDaniel Neilson     CapturesBefore(bool ReturnCaptures, const Instruction *I, const DominatorTree *DT,
64dfc1d96eSBruno Cardoso Lopes                    bool IncludeI, OrderedBasicBlock *IC)
65dfc1d96eSBruno Cardoso Lopes       : OrderedBB(IC), BeforeHere(I), DT(DT),
667900ef89SBruno Cardoso Lopes         ReturnCaptures(ReturnCaptures), IncludeI(IncludeI), Captured(false) {}
67b0356217SHal Finkel 
68b0356217SHal Finkel     void tooManyUses() override { Captured = true; }
69b0356217SHal Finkel 
707900ef89SBruno Cardoso Lopes     bool isSafeToPrune(Instruction *I) {
71b0356217SHal Finkel       BasicBlock *BB = I->getParent();
72b0356217SHal Finkel       // We explore this usage only if the usage can reach "BeforeHere".
73b0356217SHal Finkel       // If use is not reachable from entry, there is no need to explore.
74b0356217SHal Finkel       if (BeforeHere != I && !DT->isReachableFromEntry(BB))
757900ef89SBruno Cardoso Lopes         return true;
767900ef89SBruno Cardoso Lopes 
777900ef89SBruno Cardoso Lopes       // Compute the case where both instructions are inside the same basic
787900ef89SBruno Cardoso Lopes       // block. Since instructions in the same BB as BeforeHere are numbered in
79dfc1d96eSBruno Cardoso Lopes       // 'OrderedBB', avoid using 'dominates' and 'isPotentiallyReachable'
807900ef89SBruno Cardoso Lopes       // which are very expensive for large basic blocks.
817900ef89SBruno Cardoso Lopes       if (BB == BeforeHere->getParent()) {
827900ef89SBruno Cardoso Lopes         // 'I' dominates 'BeforeHere' => not safe to prune.
837900ef89SBruno Cardoso Lopes         //
848a1c45d6SDavid Majnemer         // The value defined by an invoke dominates an instruction only
850bc0eef7SDavid Majnemer         // if it dominates every instruction in UseBB. A PHI is dominated only
860bc0eef7SDavid Majnemer         // if the instruction dominates every possible use in the UseBB. Since
877900ef89SBruno Cardoso Lopes         // UseBB == BB, avoid pruning.
888a1c45d6SDavid Majnemer         if (isa<InvokeInst>(BeforeHere) || isa<PHINode>(I) || I == BeforeHere)
89b0356217SHal Finkel           return false;
90dfc1d96eSBruno Cardoso Lopes         if (!OrderedBB->dominates(BeforeHere, I))
917900ef89SBruno Cardoso Lopes           return false;
927900ef89SBruno Cardoso Lopes 
937900ef89SBruno Cardoso Lopes         // 'BeforeHere' comes before 'I', it's safe to prune if we also
947900ef89SBruno Cardoso Lopes         // guarantee that 'I' never reaches 'BeforeHere' through a back-edge or
957900ef89SBruno Cardoso Lopes         // by its successors, i.e, prune if:
967900ef89SBruno Cardoso Lopes         //
97713b5ba2SHiroshi Inoue         //  (1) BB is an entry block or have no successors.
98713b5ba2SHiroshi Inoue         //  (2) There's no path coming back through BB successors.
997900ef89SBruno Cardoso Lopes         if (BB == &BB->getParent()->getEntryBlock() ||
1007900ef89SBruno Cardoso Lopes             !BB->getTerminator()->getNumSuccessors())
1017900ef89SBruno Cardoso Lopes           return true;
1027900ef89SBruno Cardoso Lopes 
1037900ef89SBruno Cardoso Lopes         SmallVector<BasicBlock*, 32> Worklist;
1047900ef89SBruno Cardoso Lopes         Worklist.append(succ_begin(BB), succ_end(BB));
105484e48e3SAlexander Kornienko         return !isPotentiallyReachableFromMany(Worklist, BB, DT);
1067900ef89SBruno Cardoso Lopes       }
1077900ef89SBruno Cardoso Lopes 
108b0356217SHal Finkel       // If the value is defined in the same basic block as use and BeforeHere,
109b0356217SHal Finkel       // there is no need to explore the use if BeforeHere dominates use.
110b0356217SHal Finkel       // Check whether there is a path from I to BeforeHere.
111b0356217SHal Finkel       if (BeforeHere != I && DT->dominates(BeforeHere, I) &&
112b0356217SHal Finkel           !isPotentiallyReachable(I, BeforeHere, DT))
1137900ef89SBruno Cardoso Lopes         return true;
1147900ef89SBruno Cardoso Lopes 
115b0356217SHal Finkel       return false;
1167900ef89SBruno Cardoso Lopes     }
1177900ef89SBruno Cardoso Lopes 
1187900ef89SBruno Cardoso Lopes     bool shouldExplore(const Use *U) override {
1197900ef89SBruno Cardoso Lopes       Instruction *I = cast<Instruction>(U->getUser());
1207900ef89SBruno Cardoso Lopes 
1217900ef89SBruno Cardoso Lopes       if (BeforeHere == I && !IncludeI)
1227900ef89SBruno Cardoso Lopes         return false;
1237900ef89SBruno Cardoso Lopes 
1247900ef89SBruno Cardoso Lopes       if (isSafeToPrune(I))
1257900ef89SBruno Cardoso Lopes         return false;
1267900ef89SBruno Cardoso Lopes 
127b0356217SHal Finkel       return true;
128b0356217SHal Finkel     }
129b0356217SHal Finkel 
130b0356217SHal Finkel     bool captured(const Use *U) override {
131b0356217SHal Finkel       if (isa<ReturnInst>(U->getUser()) && !ReturnCaptures)
132b0356217SHal Finkel         return false;
133b0356217SHal Finkel 
1347900ef89SBruno Cardoso Lopes       if (!shouldExplore(U))
135d32803b6SHal Finkel         return false;
136d32803b6SHal Finkel 
137b0356217SHal Finkel       Captured = true;
138b0356217SHal Finkel       return true;
139b0356217SHal Finkel     }
140b0356217SHal Finkel 
141dfc1d96eSBruno Cardoso Lopes     OrderedBasicBlock *OrderedBB;
142b0356217SHal Finkel     const Instruction *BeforeHere;
1433c148720SDaniel Neilson     const DominatorTree *DT;
144b0356217SHal Finkel 
145b0356217SHal Finkel     bool ReturnCaptures;
146d32803b6SHal Finkel     bool IncludeI;
147b0356217SHal Finkel 
148b0356217SHal Finkel     bool Captured;
149b0356217SHal Finkel   };
150f00654e3SAlexander Kornienko }
1512d27b191SDan Gohman 
152e0aa0d67SDuncan Sands /// PointerMayBeCaptured - Return true if this pointer value may be captured
153e0aa0d67SDuncan Sands /// by the enclosing function (which is required to exist).  This routine can
154e0aa0d67SDuncan Sands /// be expensive, so consider caching the results.  The boolean ReturnCaptures
155e0aa0d67SDuncan Sands /// specifies whether returning the value (or part of it) from the function
15694e61762SDan Gohman /// counts as capturing it or not.  The boolean StoreCaptures specified whether
15794e61762SDan Gohman /// storing the value (or part of it) into memory anywhere automatically
158e0aa0d67SDuncan Sands /// counts as capturing it or not.
15994e61762SDan Gohman bool llvm::PointerMayBeCaptured(const Value *V,
16094e61762SDan Gohman                                 bool ReturnCaptures, bool StoreCaptures) {
161063ae589SNick Lewycky   assert(!isa<GlobalValue>(V) &&
162063ae589SNick Lewycky          "It doesn't make sense to ask whether a global is captured.");
163063ae589SNick Lewycky 
16494e61762SDan Gohman   // TODO: If StoreCaptures is not true, we could do Fancy analysis
16594e61762SDan Gohman   // to determine whether this store is not actually an escape point.
16694e61762SDan Gohman   // In that case, BasicAliasAnalysis should be updated as well to
16794e61762SDan Gohman   // take advantage of this.
1687013a19eSNick Lewycky   (void)StoreCaptures;
169e0aa0d67SDuncan Sands 
1707013a19eSNick Lewycky   SimpleCaptureTracker SCT(ReturnCaptures);
1716ae03c33SNick Lewycky   PointerMayBeCaptured(V, &SCT);
1727013a19eSNick Lewycky   return SCT.Captured;
173e0aa0d67SDuncan Sands }
1746ae03c33SNick Lewycky 
175b0356217SHal Finkel /// PointerMayBeCapturedBefore - Return true if this pointer value may be
176b0356217SHal Finkel /// captured by the enclosing function (which is required to exist). If a
177b0356217SHal Finkel /// DominatorTree is provided, only captures which happen before the given
178b0356217SHal Finkel /// instruction are considered. This routine can be expensive, so consider
179b0356217SHal Finkel /// caching the results.  The boolean ReturnCaptures specifies whether
180b0356217SHal Finkel /// returning the value (or part of it) from the function counts as capturing
181b0356217SHal Finkel /// it or not.  The boolean StoreCaptures specified whether storing the value
182b0356217SHal Finkel /// (or part of it) into memory anywhere automatically counts as capturing it
183dfc1d96eSBruno Cardoso Lopes /// or not. A ordered basic block \p OBB can be used in order to speed up
184dfc1d96eSBruno Cardoso Lopes /// queries about relative order among instructions in the same basic block.
185b0356217SHal Finkel bool llvm::PointerMayBeCapturedBefore(const Value *V, bool ReturnCaptures,
186b0356217SHal Finkel                                       bool StoreCaptures, const Instruction *I,
1873c148720SDaniel Neilson                                       const DominatorTree *DT, bool IncludeI,
188dfc1d96eSBruno Cardoso Lopes                                       OrderedBasicBlock *OBB) {
189b0356217SHal Finkel   assert(!isa<GlobalValue>(V) &&
190b0356217SHal Finkel          "It doesn't make sense to ask whether a global is captured.");
191dfc1d96eSBruno Cardoso Lopes   bool UseNewOBB = OBB == nullptr;
192b0356217SHal Finkel 
193b0356217SHal Finkel   if (!DT)
194b0356217SHal Finkel     return PointerMayBeCaptured(V, ReturnCaptures, StoreCaptures);
195dfc1d96eSBruno Cardoso Lopes   if (UseNewOBB)
196dfc1d96eSBruno Cardoso Lopes     OBB = new OrderedBasicBlock(I->getParent());
197b0356217SHal Finkel 
198b0356217SHal Finkel   // TODO: See comment in PointerMayBeCaptured regarding what could be done
199b0356217SHal Finkel   // with StoreCaptures.
200b0356217SHal Finkel 
201dfc1d96eSBruno Cardoso Lopes   CapturesBefore CB(ReturnCaptures, I, DT, IncludeI, OBB);
202b0356217SHal Finkel   PointerMayBeCaptured(V, &CB);
203dfc1d96eSBruno Cardoso Lopes 
204dfc1d96eSBruno Cardoso Lopes   if (UseNewOBB)
205dfc1d96eSBruno Cardoso Lopes     delete OBB;
206b0356217SHal Finkel   return CB.Captured;
207b0356217SHal Finkel }
208b0356217SHal Finkel 
2096ae03c33SNick Lewycky /// TODO: Write a new FunctionPass AliasAnalysis so that it can keep
2106ae03c33SNick Lewycky /// a cache. Then we can move the code from BasicAliasAnalysis into
2116ae03c33SNick Lewycky /// that path, and remove this threshold.
2126ae03c33SNick Lewycky static int const Threshold = 20;
2136ae03c33SNick Lewycky 
2146ae03c33SNick Lewycky void llvm::PointerMayBeCaptured(const Value *V, CaptureTracker *Tracker) {
2156ae03c33SNick Lewycky   assert(V->getType()->isPointerTy() && "Capture is for pointers only!");
21664e9aa5cSChandler Carruth   SmallVector<const Use *, Threshold> Worklist;
21764e9aa5cSChandler Carruth   SmallSet<const Use *, Threshold> Visited;
2186ae03c33SNick Lewycky 
219*e9832dfdSPiotr Padlewski   auto AddUses = [&](const Value *V) {
220*e9832dfdSPiotr Padlewski     int Count = 0;
221cdf47884SChandler Carruth     for (const Use &U : V->uses()) {
2226ae03c33SNick Lewycky       // If there are lots of uses, conservatively say that the value
2236ae03c33SNick Lewycky       // is captured to avoid taking too much compile time.
2246ae03c33SNick Lewycky       if (Count++ >= Threshold)
2256ae03c33SNick Lewycky         return Tracker->tooManyUses();
226*e9832dfdSPiotr Padlewski       if (!Visited.insert(&U).second)
227*e9832dfdSPiotr Padlewski         continue;
228*e9832dfdSPiotr Padlewski       if (!Tracker->shouldExplore(&U))
229*e9832dfdSPiotr Padlewski         continue;
230cdf47884SChandler Carruth       Worklist.push_back(&U);
2316ae03c33SNick Lewycky     }
232*e9832dfdSPiotr Padlewski   };
233*e9832dfdSPiotr Padlewski   AddUses(V);
2346ae03c33SNick Lewycky 
2356ae03c33SNick Lewycky   while (!Worklist.empty()) {
23664e9aa5cSChandler Carruth     const Use *U = Worklist.pop_back_val();
2376ae03c33SNick Lewycky     Instruction *I = cast<Instruction>(U->getUser());
2386ae03c33SNick Lewycky     V = U->get();
2396ae03c33SNick Lewycky 
2406ae03c33SNick Lewycky     switch (I->getOpcode()) {
2416ae03c33SNick Lewycky     case Instruction::Call:
2426ae03c33SNick Lewycky     case Instruction::Invoke: {
2436ae03c33SNick Lewycky       CallSite CS(I);
2446ae03c33SNick Lewycky       // Not captured if the callee is readonly, doesn't return a copy through
2456ae03c33SNick Lewycky       // its return value and doesn't unwind (a readonly function can leak bits
2466ae03c33SNick Lewycky       // by throwing an exception or not depending on the input value).
2476ae03c33SNick Lewycky       if (CS.onlyReadsMemory() && CS.doesNotThrow() && I->getType()->isVoidTy())
2486ae03c33SNick Lewycky         break;
2496ae03c33SNick Lewycky 
250*e9832dfdSPiotr Padlewski       // launder.invariant.group only captures pointer by returning it,
251*e9832dfdSPiotr Padlewski       // so the pointer wasn't captured if returned pointer is not captured.
252*e9832dfdSPiotr Padlewski       if (CS.getIntrinsicID() == Intrinsic::launder_invariant_group) {
253*e9832dfdSPiotr Padlewski         AddUses(I);
254*e9832dfdSPiotr Padlewski         break;
255*e9832dfdSPiotr Padlewski       }
256*e9832dfdSPiotr Padlewski 
2577f32420eSDavid Majnemer       // Volatile operations effectively capture the memory location that they
2587f32420eSDavid Majnemer       // load and store to.
2597f32420eSDavid Majnemer       if (auto *MI = dyn_cast<MemIntrinsic>(I))
2607f32420eSDavid Majnemer         if (MI->isVolatile())
2617f32420eSDavid Majnemer           if (Tracker->captured(U))
2627f32420eSDavid Majnemer             return;
2637f32420eSDavid Majnemer 
2646ae03c33SNick Lewycky       // Not captured if only passed via 'nocapture' arguments.  Note that
2656ae03c33SNick Lewycky       // calling a function pointer does not in itself cause the pointer to
2666ae03c33SNick Lewycky       // be captured.  This is a subtle point considering that (for example)
2676ae03c33SNick Lewycky       // the callee might return its own address.  It is analogous to saying
2686ae03c33SNick Lewycky       // that loading a value from a pointer does not cause the pointer to be
2696ae03c33SNick Lewycky       // captured, even though the loaded value might be the pointer itself
2706ae03c33SNick Lewycky       // (think of self-referential objects).
271ea34382dSSanjoy Das       CallSite::data_operand_iterator B =
272ea34382dSSanjoy Das         CS.data_operands_begin(), E = CS.data_operands_end();
273ea34382dSSanjoy Das       for (CallSite::data_operand_iterator A = B; A != E; ++A)
2746ae03c33SNick Lewycky         if (A->get() == V && !CS.doesNotCapture(A - B))
2756ae03c33SNick Lewycky           // The parameter is not marked 'nocapture' - captured.
2764c378a44SNick Lewycky           if (Tracker->captured(U))
2776ae03c33SNick Lewycky             return;
2786ae03c33SNick Lewycky       break;
2796ae03c33SNick Lewycky     }
2806ae03c33SNick Lewycky     case Instruction::Load:
2817f32420eSDavid Majnemer       // Volatile loads make the address observable.
2827f32420eSDavid Majnemer       if (cast<LoadInst>(I)->isVolatile())
2837f32420eSDavid Majnemer         if (Tracker->captured(U))
2847f32420eSDavid Majnemer           return;
2856ae03c33SNick Lewycky       break;
2866ae03c33SNick Lewycky     case Instruction::VAArg:
2876ae03c33SNick Lewycky       // "va-arg" from a pointer does not cause it to be captured.
2886ae03c33SNick Lewycky       break;
2896ae03c33SNick Lewycky     case Instruction::Store:
2906ae03c33SNick Lewycky         // Stored the pointer - conservatively assume it may be captured.
2917f32420eSDavid Majnemer         // Volatile stores make the address observable.
2927f32420eSDavid Majnemer       if (V == I->getOperand(0) || cast<StoreInst>(I)->isVolatile())
293bd09e86fSPhilip Reames         if (Tracker->captured(U))
294bd09e86fSPhilip Reames           return;
295bd09e86fSPhilip Reames       break;
2967f32420eSDavid Majnemer     case Instruction::AtomicRMW: {
2977f32420eSDavid Majnemer       // atomicrmw conceptually includes both a load and store from
2987f32420eSDavid Majnemer       // the same location.
2997f32420eSDavid Majnemer       // As with a store, the location being accessed is not captured,
3007f32420eSDavid Majnemer       // but the value being stored is.
3017f32420eSDavid Majnemer       // Volatile stores make the address observable.
3027f32420eSDavid Majnemer       auto *ARMWI = cast<AtomicRMWInst>(I);
3037f32420eSDavid Majnemer       if (ARMWI->getValOperand() == V || ARMWI->isVolatile())
3047f32420eSDavid Majnemer         if (Tracker->captured(U))
3057f32420eSDavid Majnemer           return;
3067f32420eSDavid Majnemer       break;
3077f32420eSDavid Majnemer     }
3087f32420eSDavid Majnemer     case Instruction::AtomicCmpXchg: {
3097f32420eSDavid Majnemer       // cmpxchg conceptually includes both a load and store from
3107f32420eSDavid Majnemer       // the same location.
3117f32420eSDavid Majnemer       // As with a store, the location being accessed is not captured,
3127f32420eSDavid Majnemer       // but the value being stored is.
3137f32420eSDavid Majnemer       // Volatile stores make the address observable.
3147f32420eSDavid Majnemer       auto *ACXI = cast<AtomicCmpXchgInst>(I);
3157f32420eSDavid Majnemer       if (ACXI->getCompareOperand() == V || ACXI->getNewValOperand() == V ||
3167f32420eSDavid Majnemer           ACXI->isVolatile())
3177f32420eSDavid Majnemer         if (Tracker->captured(U))
3187f32420eSDavid Majnemer           return;
3197f32420eSDavid Majnemer       break;
3207f32420eSDavid Majnemer     }
3216ae03c33SNick Lewycky     case Instruction::BitCast:
3226ae03c33SNick Lewycky     case Instruction::GetElementPtr:
3236ae03c33SNick Lewycky     case Instruction::PHI:
3246ae03c33SNick Lewycky     case Instruction::Select:
325e55a2c2eSMatt Arsenault     case Instruction::AddrSpaceCast:
3266ae03c33SNick Lewycky       // The original value is not captured via this if the new value isn't.
327*e9832dfdSPiotr Padlewski       AddUses(I);
3286ae03c33SNick Lewycky       break;
32943d7e1cbSAnna Thomas     case Instruction::ICmp: {
3306ae03c33SNick Lewycky       // Don't count comparisons of a no-alias return value against null as
3316ae03c33SNick Lewycky       // captures. This allows us to ignore comparisons of malloc results
3326ae03c33SNick Lewycky       // with null, for example.
3336ae03c33SNick Lewycky       if (ConstantPointerNull *CPN =
3346ae03c33SNick Lewycky           dyn_cast<ConstantPointerNull>(I->getOperand(1)))
3356ae03c33SNick Lewycky         if (CPN->getType()->getAddressSpace() == 0)
336c2ec0725SNick Lewycky           if (isNoAliasCall(V->stripPointerCasts()))
3376ae03c33SNick Lewycky             break;
33843d7e1cbSAnna Thomas       // Comparison against value stored in global variable. Given the pointer
33943d7e1cbSAnna Thomas       // does not escape, its value cannot be guessed and stored separately in a
34043d7e1cbSAnna Thomas       // global variable.
34143d7e1cbSAnna Thomas       unsigned OtherIndex = (I->getOperand(0) == V) ? 1 : 0;
34243d7e1cbSAnna Thomas       auto *LI = dyn_cast<LoadInst>(I->getOperand(OtherIndex));
34343d7e1cbSAnna Thomas       if (LI && isa<GlobalVariable>(LI->getPointerOperand()))
34443d7e1cbSAnna Thomas         break;
3456ae03c33SNick Lewycky       // Otherwise, be conservative. There are crazy ways to capture pointers
3466ae03c33SNick Lewycky       // using comparisons.
3474c378a44SNick Lewycky       if (Tracker->captured(U))
3486ae03c33SNick Lewycky         return;
3496ae03c33SNick Lewycky       break;
35043d7e1cbSAnna Thomas     }
3516ae03c33SNick Lewycky     default:
3526ae03c33SNick Lewycky       // Something else - be conservative and say it is captured.
3534c378a44SNick Lewycky       if (Tracker->captured(U))
3546ae03c33SNick Lewycky         return;
3556ae03c33SNick Lewycky       break;
3566ae03c33SNick Lewycky     }
3576ae03c33SNick Lewycky   }
3586ae03c33SNick Lewycky 
3596ae03c33SNick Lewycky   // All uses examined.
3606ae03c33SNick Lewycky }
361