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" |
Bruno Cardoso Lopes | dfc1d96 | 2015-07-31 14:31:35 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/OrderedBasicBlock.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 25 | #include "llvm/IR/CallSite.h" |
Jakub Staszak | c733bf2 | 2013-03-10 00:34:01 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Constants.h" |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Dominators.h" |
Jakub Staszak | c733bf2 | 2013-03-10 00:34:01 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Instructions.h" |
Jakub Staszak | c733bf2 | 2013-03-10 00:34:01 +0000 | [diff] [blame] | 29 | |
Duncan Sands | e0aa0d6 | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
Nick Lewycky | aa2a00d | 2011-11-21 18:32:21 +0000 | [diff] [blame] | 32 | CaptureTracker::~CaptureTracker() {} |
| 33 | |
Chandler Carruth | 64e9aa5 | 2014-03-05 10:21:48 +0000 | [diff] [blame] | 34 | bool CaptureTracker::shouldExplore(const Use *U) { return true; } |
Nick Lewycky | 7c3b5d9 | 2012-10-08 22:12:48 +0000 | [diff] [blame] | 35 | |
Nick Lewycky | 7013a19 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 36 | namespace { |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 37 | struct SimpleCaptureTracker : public CaptureTracker { |
Nick Lewycky | 7013a19 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 38 | explicit SimpleCaptureTracker(bool ReturnCaptures) |
| 39 | : ReturnCaptures(ReturnCaptures), Captured(false) {} |
| 40 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 41 | void tooManyUses() override { Captured = true; } |
Nick Lewycky | 7013a19 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 42 | |
Chandler Carruth | 64e9aa5 | 2014-03-05 10:21:48 +0000 | [diff] [blame] | 43 | bool captured(const Use *U) override { |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 44 | if (isa<ReturnInst>(U->getUser()) && !ReturnCaptures) |
Chad Rosier | 8244b1d | 2012-05-10 23:38:07 +0000 | [diff] [blame] | 45 | return false; |
Nick Lewycky | 7013a19 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 46 | |
| 47 | Captured = true; |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | bool ReturnCaptures; |
| 52 | |
| 53 | bool Captured; |
| 54 | }; |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 55 | |
| 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 { |
Bruno Cardoso Lopes | 7900ef8 | 2015-06-24 17:53:17 +0000 | [diff] [blame] | 61 | |
Hal Finkel | d32803b | 2014-07-21 21:30:22 +0000 | [diff] [blame] | 62 | CapturesBefore(bool ReturnCaptures, const Instruction *I, DominatorTree *DT, |
Bruno Cardoso Lopes | dfc1d96 | 2015-07-31 14:31:35 +0000 | [diff] [blame] | 63 | bool IncludeI, OrderedBasicBlock *IC) |
| 64 | : OrderedBB(IC), BeforeHere(I), DT(DT), |
Bruno Cardoso Lopes | 7900ef8 | 2015-06-24 17:53:17 +0000 | [diff] [blame] | 65 | ReturnCaptures(ReturnCaptures), IncludeI(IncludeI), Captured(false) {} |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 66 | |
| 67 | void tooManyUses() override { Captured = true; } |
| 68 | |
Bruno Cardoso Lopes | 7900ef8 | 2015-06-24 17:53:17 +0000 | [diff] [blame] | 69 | bool isSafeToPrune(Instruction *I) { |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 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)) |
Bruno Cardoso Lopes | 7900ef8 | 2015-06-24 17:53:17 +0000 | [diff] [blame] | 74 | return true; |
| 75 | |
| 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 |
Bruno Cardoso Lopes | dfc1d96 | 2015-07-31 14:31:35 +0000 | [diff] [blame] | 78 | // 'OrderedBB', avoid using 'dominates' and 'isPotentiallyReachable' |
Bruno Cardoso Lopes | 7900ef8 | 2015-06-24 17:53:17 +0000 | [diff] [blame] | 79 | // which are very expensive for large basic blocks. |
| 80 | if (BB == BeforeHere->getParent()) { |
| 81 | // 'I' dominates 'BeforeHere' => not safe to prune. |
| 82 | // |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 83 | // The value defined by an invoke dominates an instruction only |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 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 |
Bruno Cardoso Lopes | 7900ef8 | 2015-06-24 17:53:17 +0000 | [diff] [blame] | 86 | // UseBB == BB, avoid pruning. |
David Majnemer | 8a1c45d | 2015-12-12 05:38:55 +0000 | [diff] [blame] | 87 | if (isa<InvokeInst>(BeforeHere) || isa<PHINode>(I) || I == BeforeHere) |
Bruno Cardoso Lopes | 7900ef8 | 2015-06-24 17:53:17 +0000 | [diff] [blame] | 88 | return false; |
Bruno Cardoso Lopes | dfc1d96 | 2015-07-31 14:31:35 +0000 | [diff] [blame] | 89 | if (!OrderedBB->dominates(BeforeHere, I)) |
Bruno Cardoso Lopes | 7900ef8 | 2015-06-24 17:53:17 +0000 | [diff] [blame] | 90 | return false; |
| 91 | |
| 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: |
| 95 | // |
| 96 | // (1) BB is an entry block or have no sucessors. |
| 97 | // (2) There's no path coming back through BB sucessors. |
| 98 | if (BB == &BB->getParent()->getEntryBlock() || |
| 99 | !BB->getTerminator()->getNumSuccessors()) |
| 100 | return true; |
| 101 | |
| 102 | SmallVector<BasicBlock*, 32> Worklist; |
| 103 | Worklist.append(succ_begin(BB), succ_end(BB)); |
Alexander Kornienko | 484e48e3 | 2015-11-05 21:07:12 +0000 | [diff] [blame] | 104 | return !isPotentiallyReachableFromMany(Worklist, BB, DT); |
Bruno Cardoso Lopes | 7900ef8 | 2015-06-24 17:53:17 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 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, DT)) |
Bruno Cardoso Lopes | 7900ef8 | 2015-06-24 17:53:17 +0000 | [diff] [blame] | 112 | return true; |
| 113 | |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | bool shouldExplore(const Use *U) override { |
| 118 | Instruction *I = cast<Instruction>(U->getUser()); |
| 119 | |
| 120 | if (BeforeHere == I && !IncludeI) |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 121 | return false; |
Bruno Cardoso Lopes | 7900ef8 | 2015-06-24 17:53:17 +0000 | [diff] [blame] | 122 | |
| 123 | if (isSafeToPrune(I)) |
| 124 | return false; |
| 125 | |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 126 | return true; |
| 127 | } |
| 128 | |
| 129 | bool captured(const Use *U) override { |
| 130 | if (isa<ReturnInst>(U->getUser()) && !ReturnCaptures) |
| 131 | return false; |
| 132 | |
Bruno Cardoso Lopes | 7900ef8 | 2015-06-24 17:53:17 +0000 | [diff] [blame] | 133 | if (!shouldExplore(U)) |
Hal Finkel | d32803b | 2014-07-21 21:30:22 +0000 | [diff] [blame] | 134 | return false; |
| 135 | |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 136 | Captured = true; |
| 137 | return true; |
| 138 | } |
| 139 | |
Bruno Cardoso Lopes | dfc1d96 | 2015-07-31 14:31:35 +0000 | [diff] [blame] | 140 | OrderedBasicBlock *OrderedBB; |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 141 | const Instruction *BeforeHere; |
| 142 | DominatorTree *DT; |
| 143 | |
| 144 | bool ReturnCaptures; |
Hal Finkel | d32803b | 2014-07-21 21:30:22 +0000 | [diff] [blame] | 145 | bool IncludeI; |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 146 | |
| 147 | bool Captured; |
| 148 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 149 | } |
Dan Gohman | 2d27b19 | 2009-12-08 23:59:12 +0000 | [diff] [blame] | 150 | |
Duncan Sands | e0aa0d6 | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 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 |
Dan Gohman | 94e6176 | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 155 | /// counts as capturing it or not. The boolean StoreCaptures specified whether |
| 156 | /// storing the value (or part of it) into memory anywhere automatically |
Duncan Sands | e0aa0d6 | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 157 | /// counts as capturing it or not. |
Dan Gohman | 94e6176 | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 158 | bool llvm::PointerMayBeCaptured(const Value *V, |
| 159 | bool ReturnCaptures, bool StoreCaptures) { |
Nick Lewycky | 063ae58 | 2011-11-21 19:42:56 +0000 | [diff] [blame] | 160 | assert(!isa<GlobalValue>(V) && |
| 161 | "It doesn't make sense to ask whether a global is captured."); |
| 162 | |
Nick Lewycky | 7013a19 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 163 | // TODO: If StoreCaptures is not true, we could do Fancy analysis |
| 164 | // to determine whether this store is not actually an escape point. |
| 165 | // In that case, BasicAliasAnalysis should be updated as well to |
| 166 | // take advantage of this. |
| 167 | (void)StoreCaptures; |
Duncan Sands | e0aa0d6 | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 168 | |
Nick Lewycky | 7013a19 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 169 | SimpleCaptureTracker SCT(ReturnCaptures); |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 170 | PointerMayBeCaptured(V, &SCT); |
Nick Lewycky | 7013a19 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 171 | return SCT.Captured; |
Duncan Sands | e0aa0d6 | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 172 | } |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 173 | |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 174 | /// PointerMayBeCapturedBefore - Return true if this pointer value may be |
| 175 | /// captured by the enclosing function (which is required to exist). If a |
| 176 | /// DominatorTree is provided, only captures which happen before the given |
| 177 | /// instruction are considered. This routine can be expensive, so consider |
| 178 | /// caching the results. The boolean ReturnCaptures specifies whether |
| 179 | /// returning the value (or part of it) from the function counts as capturing |
| 180 | /// it or not. The boolean StoreCaptures specified whether storing the value |
| 181 | /// (or part of it) into memory anywhere automatically counts as capturing it |
Bruno Cardoso Lopes | dfc1d96 | 2015-07-31 14:31:35 +0000 | [diff] [blame] | 182 | /// or not. A ordered basic block \p OBB can be used in order to speed up |
| 183 | /// queries about relative order among instructions in the same basic block. |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 184 | bool llvm::PointerMayBeCapturedBefore(const Value *V, bool ReturnCaptures, |
| 185 | bool StoreCaptures, const Instruction *I, |
Bruno Cardoso Lopes | dfc1d96 | 2015-07-31 14:31:35 +0000 | [diff] [blame] | 186 | DominatorTree *DT, bool IncludeI, |
| 187 | OrderedBasicBlock *OBB) { |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 188 | assert(!isa<GlobalValue>(V) && |
| 189 | "It doesn't make sense to ask whether a global is captured."); |
Bruno Cardoso Lopes | dfc1d96 | 2015-07-31 14:31:35 +0000 | [diff] [blame] | 190 | bool UseNewOBB = OBB == nullptr; |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 191 | |
| 192 | if (!DT) |
| 193 | return PointerMayBeCaptured(V, ReturnCaptures, StoreCaptures); |
Bruno Cardoso Lopes | dfc1d96 | 2015-07-31 14:31:35 +0000 | [diff] [blame] | 194 | if (UseNewOBB) |
| 195 | OBB = new OrderedBasicBlock(I->getParent()); |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 196 | |
| 197 | // TODO: See comment in PointerMayBeCaptured regarding what could be done |
| 198 | // with StoreCaptures. |
| 199 | |
Bruno Cardoso Lopes | dfc1d96 | 2015-07-31 14:31:35 +0000 | [diff] [blame] | 200 | CapturesBefore CB(ReturnCaptures, I, DT, IncludeI, OBB); |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 201 | PointerMayBeCaptured(V, &CB); |
Bruno Cardoso Lopes | dfc1d96 | 2015-07-31 14:31:35 +0000 | [diff] [blame] | 202 | |
| 203 | if (UseNewOBB) |
| 204 | delete OBB; |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 205 | return CB.Captured; |
| 206 | } |
| 207 | |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 208 | /// TODO: Write a new FunctionPass AliasAnalysis so that it can keep |
| 209 | /// a cache. Then we can move the code from BasicAliasAnalysis into |
| 210 | /// that path, and remove this threshold. |
| 211 | static int const Threshold = 20; |
| 212 | |
| 213 | void llvm::PointerMayBeCaptured(const Value *V, CaptureTracker *Tracker) { |
| 214 | assert(V->getType()->isPointerTy() && "Capture is for pointers only!"); |
Chandler Carruth | 64e9aa5 | 2014-03-05 10:21:48 +0000 | [diff] [blame] | 215 | SmallVector<const Use *, Threshold> Worklist; |
| 216 | SmallSet<const Use *, Threshold> Visited; |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 217 | int Count = 0; |
| 218 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 219 | for (const Use &U : V->uses()) { |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 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++ >= Threshold) |
| 223 | return Tracker->tooManyUses(); |
| 224 | |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 225 | if (!Tracker->shouldExplore(&U)) continue; |
| 226 | Visited.insert(&U); |
| 227 | Worklist.push_back(&U); |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | while (!Worklist.empty()) { |
Chandler Carruth | 64e9aa5 | 2014-03-05 10:21:48 +0000 | [diff] [blame] | 231 | const Use *U = Worklist.pop_back_val(); |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 232 | Instruction *I = cast<Instruction>(U->getUser()); |
| 233 | V = U->get(); |
| 234 | |
| 235 | switch (I->getOpcode()) { |
| 236 | case Instruction::Call: |
| 237 | case Instruction::Invoke: { |
| 238 | CallSite CS(I); |
| 239 | // Not captured if the callee is readonly, doesn't return a copy through |
| 240 | // its return value and doesn't unwind (a readonly function can leak bits |
| 241 | // by throwing an exception or not depending on the input value). |
| 242 | if (CS.onlyReadsMemory() && CS.doesNotThrow() && I->getType()->isVoidTy()) |
| 243 | break; |
| 244 | |
| 245 | // Not captured if only passed via 'nocapture' arguments. Note that |
| 246 | // calling a function pointer does not in itself cause the pointer to |
| 247 | // be captured. This is a subtle point considering that (for example) |
| 248 | // the callee might return its own address. It is analogous to saying |
| 249 | // that loading a value from a pointer does not cause the pointer to be |
| 250 | // captured, even though the loaded value might be the pointer itself |
| 251 | // (think of self-referential objects). |
Sanjoy Das | ea34382 | 2015-11-04 23:21:06 +0000 | [diff] [blame] | 252 | CallSite::data_operand_iterator B = |
| 253 | CS.data_operands_begin(), E = CS.data_operands_end(); |
| 254 | for (CallSite::data_operand_iterator A = B; A != E; ++A) |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 255 | if (A->get() == V && !CS.doesNotCapture(A - B)) |
| 256 | // The parameter is not marked 'nocapture' - captured. |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 257 | if (Tracker->captured(U)) |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 258 | return; |
| 259 | break; |
| 260 | } |
| 261 | case Instruction::Load: |
| 262 | // Loading from a pointer does not cause it to be captured. |
| 263 | break; |
| 264 | case Instruction::VAArg: |
| 265 | // "va-arg" from a pointer does not cause it to be captured. |
| 266 | break; |
| 267 | case Instruction::Store: |
| 268 | if (V == I->getOperand(0)) |
| 269 | // Stored the pointer - conservatively assume it may be captured. |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 270 | if (Tracker->captured(U)) |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 271 | return; |
| 272 | // Storing to the pointee does not cause the pointer to be captured. |
| 273 | break; |
Philip Reames | bd09e86 | 2016-02-18 19:23:27 +0000 | [diff] [blame] | 274 | case Instruction::AtomicRMW: |
| 275 | case Instruction::AtomicCmpXchg: |
| 276 | // atomicrmw and cmpxchg conceptually include both a load and store from |
| 277 | // the same location. As with a store, the location being accessed is |
| 278 | // not captured, but the value being stored is. (For cmpxchg, we |
| 279 | // probably don't need to capture the original comparison value, but for |
| 280 | // the moment, let's be conservative.) |
| 281 | if (V != I->getOperand(0)) |
| 282 | if (Tracker->captured(U)) |
| 283 | return; |
| 284 | break; |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 285 | case Instruction::BitCast: |
| 286 | case Instruction::GetElementPtr: |
| 287 | case Instruction::PHI: |
| 288 | case Instruction::Select: |
Matt Arsenault | e55a2c2 | 2014-01-14 19:11:52 +0000 | [diff] [blame] | 289 | case Instruction::AddrSpaceCast: |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 290 | // 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] | 291 | Count = 0; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 292 | for (Use &UU : I->uses()) { |
Benjamin Kramer | d2757ba | 2013-10-03 13:24:02 +0000 | [diff] [blame] | 293 | // If there are lots of uses, conservatively say that the value |
| 294 | // is captured to avoid taking too much compile time. |
| 295 | if (Count++ >= Threshold) |
| 296 | return Tracker->tooManyUses(); |
| 297 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 298 | if (Visited.insert(&UU).second) |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 299 | if (Tracker->shouldExplore(&UU)) |
| 300 | Worklist.push_back(&UU); |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 301 | } |
| 302 | break; |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame^] | 303 | case Instruction::ICmp: { |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 304 | // Don't count comparisons of a no-alias return value against null as |
| 305 | // captures. This allows us to ignore comparisons of malloc results |
| 306 | // with null, for example. |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 307 | if (ConstantPointerNull *CPN = |
| 308 | dyn_cast<ConstantPointerNull>(I->getOperand(1))) |
| 309 | if (CPN->getType()->getAddressSpace() == 0) |
| 310 | if (isNoAliasCall(V->stripPointerCasts())) |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 311 | break; |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame^] | 312 | // Comparison against value stored in global variable. Given the pointer |
| 313 | // does not escape, its value cannot be guessed and stored separately in a |
| 314 | // global variable. |
| 315 | unsigned OtherIndex = (I->getOperand(0) == V) ? 1 : 0; |
| 316 | auto *LI = dyn_cast<LoadInst>(I->getOperand(OtherIndex)); |
| 317 | if (LI && isa<GlobalVariable>(LI->getPointerOperand())) |
| 318 | break; |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 319 | // Otherwise, be conservative. There are crazy ways to capture pointers |
| 320 | // using comparisons. |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 321 | if (Tracker->captured(U)) |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 322 | return; |
| 323 | break; |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame^] | 324 | } |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 325 | default: |
| 326 | // Something else - be conservative and say it is captured. |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 327 | if (Tracker->captured(U)) |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 328 | return; |
| 329 | break; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | // All uses examined. |
| 334 | } |