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