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