Duncan Sands | e0aa0d6 | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 1 | //===--- CaptureTracking.cpp - Determine whether a pointer is captured ----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains routines that help determine which pointers are captured. |
| 11 | // A pointer value is captured if the function makes a copy of any part of the |
| 12 | // pointer that outlives the call. Not being captured means, more or less, that |
| 13 | // the pointer is only dereferenced and not stored in a global. Returning part |
| 14 | // of the pointer as the function return value may or may not count as capturing |
| 15 | // the pointer, depending on the context. |
| 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Jakub Staszak | 173bce3 | 2012-01-17 22:16:31 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallSet.h" |
| 20 | #include "llvm/ADT/SmallVector.h" |
Jakub Staszak | c733bf2 | 2013-03-10 00:34:01 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/AliasAnalysis.h" |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/CFG.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/CaptureTracking.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 24 | #include "llvm/IR/CallSite.h" |
Jakub Staszak | c733bf2 | 2013-03-10 00:34:01 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Constants.h" |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Dominators.h" |
Jakub Staszak | c733bf2 | 2013-03-10 00:34:01 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Instructions.h" |
Jakub Staszak | c733bf2 | 2013-03-10 00:34:01 +0000 | [diff] [blame] | 28 | |
Duncan Sands | e0aa0d6 | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Nick Lewycky | aa2a00d | 2011-11-21 18:32:21 +0000 | [diff] [blame] | 31 | CaptureTracker::~CaptureTracker() {} |
| 32 | |
Chandler Carruth | 64e9aa5 | 2014-03-05 10:21:48 +0000 | [diff] [blame] | 33 | bool CaptureTracker::shouldExplore(const Use *U) { return true; } |
Nick Lewycky | 7c3b5d9 | 2012-10-08 22:12:48 +0000 | [diff] [blame] | 34 | |
Nick Lewycky | 7013a19 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 35 | namespace { |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 36 | struct SimpleCaptureTracker : public CaptureTracker { |
Nick Lewycky | 7013a19 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 37 | explicit SimpleCaptureTracker(bool ReturnCaptures) |
| 38 | : ReturnCaptures(ReturnCaptures), Captured(false) {} |
| 39 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 40 | void tooManyUses() override { Captured = true; } |
Nick Lewycky | 7013a19 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 41 | |
Chandler Carruth | 64e9aa5 | 2014-03-05 10:21:48 +0000 | [diff] [blame] | 42 | bool captured(const Use *U) override { |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 43 | if (isa<ReturnInst>(U->getUser()) && !ReturnCaptures) |
Chad Rosier | 8244b1d | 2012-05-10 23:38:07 +0000 | [diff] [blame] | 44 | return false; |
Nick Lewycky | 7013a19 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 45 | |
| 46 | Captured = true; |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | bool ReturnCaptures; |
| 51 | |
| 52 | bool Captured; |
| 53 | }; |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 54 | |
Bruno Cardoso Lopes | 7900ef8 | 2015-06-24 17:53:17 +0000 | [diff] [blame^] | 55 | struct NumberedInstCache { |
| 56 | SmallDenseMap<const Instruction *, unsigned, 32> NumberedInsts; |
| 57 | BasicBlock::const_iterator LastInstFound; |
| 58 | unsigned LastInstPos; |
| 59 | const BasicBlock *BB; |
| 60 | |
| 61 | NumberedInstCache(const BasicBlock *BasicB) : LastInstPos(0), BB(BasicB) { |
| 62 | LastInstFound = BB->end(); |
| 63 | } |
| 64 | |
| 65 | /// \brief Find the first instruction 'A' or 'B' in 'BB'. Number out |
| 66 | /// instruction while walking 'BB'. |
| 67 | const Instruction *find(const Instruction *A, const Instruction *B) { |
| 68 | const Instruction *Inst = nullptr; |
| 69 | assert(!(LastInstFound == BB->end() && LastInstPos != 0) && |
| 70 | "Instruction supposed to be in NumberedInsts"); |
| 71 | |
| 72 | // Start the search with the instruction found in the last lookup round. |
| 73 | auto II = BB->begin(); |
| 74 | auto IE = BB->end(); |
| 75 | if (LastInstFound != IE) |
| 76 | II = std::next(LastInstFound); |
| 77 | |
| 78 | // Number all instructions up to the point where we find 'A' or 'B'. |
| 79 | for (++LastInstPos; II != IE; ++II, ++LastInstPos) { |
| 80 | Inst = cast<Instruction>(II); |
| 81 | NumberedInsts[Inst] = LastInstPos; |
| 82 | if (Inst == A || Inst == B) |
| 83 | break; |
| 84 | } |
| 85 | |
| 86 | assert(II != IE && "Instruction not found?"); |
| 87 | LastInstFound = II; |
| 88 | return Inst; |
| 89 | } |
| 90 | |
| 91 | /// \brief Find out whether 'A' dominates 'B', meaning whether 'A' |
| 92 | /// comes before 'B' in 'BB'. This is a simplification that considers |
| 93 | /// cached instruction positions and ignores other basic blocks, being |
| 94 | /// only relevant to compare relative instructions positions inside 'BB'. |
| 95 | bool dominates(const Instruction *A, const Instruction *B) { |
| 96 | assert(A->getParent() == B->getParent() && |
| 97 | "Instructions must be in the same basic block!"); |
| 98 | |
| 99 | unsigned NA = NumberedInsts.lookup(A); |
| 100 | unsigned NB = NumberedInsts.lookup(B); |
| 101 | if (NA && NB) |
| 102 | return NA < NB; |
| 103 | if (NA) |
| 104 | return true; |
| 105 | if (NB) |
| 106 | return false; |
| 107 | |
| 108 | return A == find(A, B); |
| 109 | } |
| 110 | }; |
| 111 | |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 112 | /// Only find pointer captures which happen before the given instruction. Uses |
| 113 | /// the dominator tree to determine whether one instruction is before another. |
| 114 | /// Only support the case where the Value is defined in the same basic block |
| 115 | /// as the given instruction and the use. |
| 116 | struct CapturesBefore : public CaptureTracker { |
Bruno Cardoso Lopes | 7900ef8 | 2015-06-24 17:53:17 +0000 | [diff] [blame^] | 117 | |
Hal Finkel | d32803b | 2014-07-21 21:30:22 +0000 | [diff] [blame] | 118 | CapturesBefore(bool ReturnCaptures, const Instruction *I, DominatorTree *DT, |
| 119 | bool IncludeI) |
Bruno Cardoso Lopes | 7900ef8 | 2015-06-24 17:53:17 +0000 | [diff] [blame^] | 120 | : LocalInstCache(I->getParent()), BeforeHere(I), DT(DT), |
| 121 | ReturnCaptures(ReturnCaptures), IncludeI(IncludeI), Captured(false) {} |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 122 | |
| 123 | void tooManyUses() override { Captured = true; } |
| 124 | |
Bruno Cardoso Lopes | 7900ef8 | 2015-06-24 17:53:17 +0000 | [diff] [blame^] | 125 | bool isSafeToPrune(Instruction *I) { |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 126 | BasicBlock *BB = I->getParent(); |
| 127 | // We explore this usage only if the usage can reach "BeforeHere". |
| 128 | // If use is not reachable from entry, there is no need to explore. |
| 129 | if (BeforeHere != I && !DT->isReachableFromEntry(BB)) |
Bruno Cardoso Lopes | 7900ef8 | 2015-06-24 17:53:17 +0000 | [diff] [blame^] | 130 | return true; |
| 131 | |
| 132 | // Compute the case where both instructions are inside the same basic |
| 133 | // block. Since instructions in the same BB as BeforeHere are numbered in |
| 134 | // 'LocalInstCache', avoid using 'dominates' and 'isPotentiallyReachable' |
| 135 | // which are very expensive for large basic blocks. |
| 136 | if (BB == BeforeHere->getParent()) { |
| 137 | // 'I' dominates 'BeforeHere' => not safe to prune. |
| 138 | // |
| 139 | // The value defined by an invoke dominates an instruction only if it |
| 140 | // dominates every instruction in UseBB. A PHI is dominated only if |
| 141 | // the instruction dominates every possible use in the UseBB. Since |
| 142 | // UseBB == BB, avoid pruning. |
| 143 | if (isa<InvokeInst>(BeforeHere) || isa<PHINode>(I) || I == BeforeHere) |
| 144 | return false; |
| 145 | if (!LocalInstCache.dominates(BeforeHere, I)) |
| 146 | return false; |
| 147 | |
| 148 | // 'BeforeHere' comes before 'I', it's safe to prune if we also |
| 149 | // guarantee that 'I' never reaches 'BeforeHere' through a back-edge or |
| 150 | // by its successors, i.e, prune if: |
| 151 | // |
| 152 | // (1) BB is an entry block or have no sucessors. |
| 153 | // (2) There's no path coming back through BB sucessors. |
| 154 | if (BB == &BB->getParent()->getEntryBlock() || |
| 155 | !BB->getTerminator()->getNumSuccessors()) |
| 156 | return true; |
| 157 | |
| 158 | SmallVector<BasicBlock*, 32> Worklist; |
| 159 | Worklist.append(succ_begin(BB), succ_end(BB)); |
| 160 | if (!isPotentiallyReachableFromMany(Worklist, BB, DT)) |
| 161 | return true; |
| 162 | |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 163 | return false; |
Bruno Cardoso Lopes | 7900ef8 | 2015-06-24 17:53:17 +0000 | [diff] [blame^] | 164 | } |
| 165 | |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 166 | // If the value is defined in the same basic block as use and BeforeHere, |
| 167 | // there is no need to explore the use if BeforeHere dominates use. |
| 168 | // Check whether there is a path from I to BeforeHere. |
| 169 | if (BeforeHere != I && DT->dominates(BeforeHere, I) && |
| 170 | !isPotentiallyReachable(I, BeforeHere, DT)) |
Bruno Cardoso Lopes | 7900ef8 | 2015-06-24 17:53:17 +0000 | [diff] [blame^] | 171 | return true; |
| 172 | |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | bool shouldExplore(const Use *U) override { |
| 177 | Instruction *I = cast<Instruction>(U->getUser()); |
| 178 | |
| 179 | if (BeforeHere == I && !IncludeI) |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 180 | return false; |
Bruno Cardoso Lopes | 7900ef8 | 2015-06-24 17:53:17 +0000 | [diff] [blame^] | 181 | |
| 182 | if (isSafeToPrune(I)) |
| 183 | return false; |
| 184 | |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 185 | return true; |
| 186 | } |
| 187 | |
| 188 | bool captured(const Use *U) override { |
| 189 | if (isa<ReturnInst>(U->getUser()) && !ReturnCaptures) |
| 190 | return false; |
| 191 | |
Bruno Cardoso Lopes | 7900ef8 | 2015-06-24 17:53:17 +0000 | [diff] [blame^] | 192 | if (!shouldExplore(U)) |
Hal Finkel | d32803b | 2014-07-21 21:30:22 +0000 | [diff] [blame] | 193 | return false; |
| 194 | |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 195 | Captured = true; |
| 196 | return true; |
| 197 | } |
| 198 | |
Bruno Cardoso Lopes | 7900ef8 | 2015-06-24 17:53:17 +0000 | [diff] [blame^] | 199 | NumberedInstCache LocalInstCache; |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 200 | const Instruction *BeforeHere; |
| 201 | DominatorTree *DT; |
| 202 | |
| 203 | bool ReturnCaptures; |
Hal Finkel | d32803b | 2014-07-21 21:30:22 +0000 | [diff] [blame] | 204 | bool IncludeI; |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 205 | |
| 206 | bool Captured; |
| 207 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 208 | } |
Dan Gohman | 2d27b19 | 2009-12-08 23:59:12 +0000 | [diff] [blame] | 209 | |
Duncan Sands | e0aa0d6 | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 210 | /// PointerMayBeCaptured - Return true if this pointer value may be captured |
| 211 | /// by the enclosing function (which is required to exist). This routine can |
| 212 | /// be expensive, so consider caching the results. The boolean ReturnCaptures |
| 213 | /// specifies whether returning the value (or part of it) from the function |
Dan Gohman | 94e6176 | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 214 | /// counts as capturing it or not. The boolean StoreCaptures specified whether |
| 215 | /// storing the value (or part of it) into memory anywhere automatically |
Duncan Sands | e0aa0d6 | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 216 | /// counts as capturing it or not. |
Dan Gohman | 94e6176 | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 217 | bool llvm::PointerMayBeCaptured(const Value *V, |
| 218 | bool ReturnCaptures, bool StoreCaptures) { |
Nick Lewycky | 063ae58 | 2011-11-21 19:42:56 +0000 | [diff] [blame] | 219 | assert(!isa<GlobalValue>(V) && |
| 220 | "It doesn't make sense to ask whether a global is captured."); |
| 221 | |
Nick Lewycky | 7013a19 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 222 | // TODO: If StoreCaptures is not true, we could do Fancy analysis |
| 223 | // to determine whether this store is not actually an escape point. |
| 224 | // In that case, BasicAliasAnalysis should be updated as well to |
| 225 | // take advantage of this. |
| 226 | (void)StoreCaptures; |
Duncan Sands | e0aa0d6 | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 227 | |
Nick Lewycky | 7013a19 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 228 | SimpleCaptureTracker SCT(ReturnCaptures); |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 229 | PointerMayBeCaptured(V, &SCT); |
Nick Lewycky | 7013a19 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 230 | return SCT.Captured; |
Duncan Sands | e0aa0d6 | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 231 | } |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 232 | |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 233 | /// PointerMayBeCapturedBefore - Return true if this pointer value may be |
| 234 | /// captured by the enclosing function (which is required to exist). If a |
| 235 | /// DominatorTree is provided, only captures which happen before the given |
| 236 | /// instruction are considered. This routine can be expensive, so consider |
| 237 | /// caching the results. The boolean ReturnCaptures specifies whether |
| 238 | /// returning the value (or part of it) from the function counts as capturing |
| 239 | /// it or not. The boolean StoreCaptures specified whether storing the value |
| 240 | /// (or part of it) into memory anywhere automatically counts as capturing it |
| 241 | /// or not. |
| 242 | bool llvm::PointerMayBeCapturedBefore(const Value *V, bool ReturnCaptures, |
| 243 | bool StoreCaptures, const Instruction *I, |
Hal Finkel | d32803b | 2014-07-21 21:30:22 +0000 | [diff] [blame] | 244 | DominatorTree *DT, bool IncludeI) { |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 245 | assert(!isa<GlobalValue>(V) && |
| 246 | "It doesn't make sense to ask whether a global is captured."); |
| 247 | |
| 248 | if (!DT) |
| 249 | return PointerMayBeCaptured(V, ReturnCaptures, StoreCaptures); |
| 250 | |
| 251 | // TODO: See comment in PointerMayBeCaptured regarding what could be done |
| 252 | // with StoreCaptures. |
| 253 | |
Hal Finkel | d32803b | 2014-07-21 21:30:22 +0000 | [diff] [blame] | 254 | CapturesBefore CB(ReturnCaptures, I, DT, IncludeI); |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 255 | PointerMayBeCaptured(V, &CB); |
| 256 | return CB.Captured; |
| 257 | } |
| 258 | |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 259 | /// TODO: Write a new FunctionPass AliasAnalysis so that it can keep |
| 260 | /// a cache. Then we can move the code from BasicAliasAnalysis into |
| 261 | /// that path, and remove this threshold. |
| 262 | static int const Threshold = 20; |
| 263 | |
| 264 | void llvm::PointerMayBeCaptured(const Value *V, CaptureTracker *Tracker) { |
| 265 | assert(V->getType()->isPointerTy() && "Capture is for pointers only!"); |
Chandler Carruth | 64e9aa5 | 2014-03-05 10:21:48 +0000 | [diff] [blame] | 266 | SmallVector<const Use *, Threshold> Worklist; |
| 267 | SmallSet<const Use *, Threshold> Visited; |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 268 | int Count = 0; |
| 269 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 270 | for (const Use &U : V->uses()) { |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 271 | // If there are lots of uses, conservatively say that the value |
| 272 | // is captured to avoid taking too much compile time. |
| 273 | if (Count++ >= Threshold) |
| 274 | return Tracker->tooManyUses(); |
| 275 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 276 | if (!Tracker->shouldExplore(&U)) continue; |
| 277 | Visited.insert(&U); |
| 278 | Worklist.push_back(&U); |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | while (!Worklist.empty()) { |
Chandler Carruth | 64e9aa5 | 2014-03-05 10:21:48 +0000 | [diff] [blame] | 282 | const Use *U = Worklist.pop_back_val(); |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 283 | Instruction *I = cast<Instruction>(U->getUser()); |
| 284 | V = U->get(); |
| 285 | |
| 286 | switch (I->getOpcode()) { |
| 287 | case Instruction::Call: |
| 288 | case Instruction::Invoke: { |
| 289 | CallSite CS(I); |
| 290 | // Not captured if the callee is readonly, doesn't return a copy through |
| 291 | // its return value and doesn't unwind (a readonly function can leak bits |
| 292 | // by throwing an exception or not depending on the input value). |
| 293 | if (CS.onlyReadsMemory() && CS.doesNotThrow() && I->getType()->isVoidTy()) |
| 294 | break; |
| 295 | |
| 296 | // Not captured if only passed via 'nocapture' arguments. Note that |
| 297 | // calling a function pointer does not in itself cause the pointer to |
| 298 | // be captured. This is a subtle point considering that (for example) |
| 299 | // the callee might return its own address. It is analogous to saying |
| 300 | // that loading a value from a pointer does not cause the pointer to be |
| 301 | // captured, even though the loaded value might be the pointer itself |
| 302 | // (think of self-referential objects). |
| 303 | CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end(); |
| 304 | for (CallSite::arg_iterator A = B; A != E; ++A) |
| 305 | if (A->get() == V && !CS.doesNotCapture(A - B)) |
| 306 | // The parameter is not marked 'nocapture' - captured. |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 307 | if (Tracker->captured(U)) |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 308 | return; |
| 309 | break; |
| 310 | } |
| 311 | case Instruction::Load: |
| 312 | // Loading from a pointer does not cause it to be captured. |
| 313 | break; |
| 314 | case Instruction::VAArg: |
| 315 | // "va-arg" from a pointer does not cause it to be captured. |
| 316 | break; |
| 317 | case Instruction::Store: |
| 318 | if (V == I->getOperand(0)) |
| 319 | // Stored the pointer - conservatively assume it may be captured. |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 320 | if (Tracker->captured(U)) |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 321 | return; |
| 322 | // Storing to the pointee does not cause the pointer to be captured. |
| 323 | break; |
| 324 | case Instruction::BitCast: |
| 325 | case Instruction::GetElementPtr: |
| 326 | case Instruction::PHI: |
| 327 | case Instruction::Select: |
Matt Arsenault | e55a2c2 | 2014-01-14 19:11:52 +0000 | [diff] [blame] | 328 | case Instruction::AddrSpaceCast: |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 329 | // The original value is not captured via this if the new value isn't. |
Benjamin Kramer | d2757ba | 2013-10-03 13:24:02 +0000 | [diff] [blame] | 330 | Count = 0; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 331 | for (Use &UU : I->uses()) { |
Benjamin Kramer | d2757ba | 2013-10-03 13:24:02 +0000 | [diff] [blame] | 332 | // If there are lots of uses, conservatively say that the value |
| 333 | // is captured to avoid taking too much compile time. |
| 334 | if (Count++ >= Threshold) |
| 335 | return Tracker->tooManyUses(); |
| 336 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 337 | if (Visited.insert(&UU).second) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 338 | if (Tracker->shouldExplore(&UU)) |
| 339 | Worklist.push_back(&UU); |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 340 | } |
| 341 | break; |
| 342 | case Instruction::ICmp: |
| 343 | // Don't count comparisons of a no-alias return value against null as |
| 344 | // captures. This allows us to ignore comparisons of malloc results |
| 345 | // with null, for example. |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 346 | if (ConstantPointerNull *CPN = |
| 347 | dyn_cast<ConstantPointerNull>(I->getOperand(1))) |
| 348 | if (CPN->getType()->getAddressSpace() == 0) |
| 349 | if (isNoAliasCall(V->stripPointerCasts())) |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 350 | break; |
| 351 | // Otherwise, be conservative. There are crazy ways to capture pointers |
| 352 | // using comparisons. |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 353 | if (Tracker->captured(U)) |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 354 | return; |
| 355 | break; |
| 356 | default: |
| 357 | // Something else - be conservative and say it is captured. |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 358 | if (Tracker->captured(U)) |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 359 | return; |
| 360 | break; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | // All uses examined. |
| 365 | } |