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