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 | |
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 | |
Daniel Neilson | 3c14872 | 2018-04-24 21:12:45 +0000 | [diff] [blame] | 62 | CapturesBefore(bool ReturnCaptures, const Instruction *I, const 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 | // |
Hiroshi Inoue | 713b5ba | 2017-07-09 05:54:44 +0000 | [diff] [blame] | 96 | // (1) BB is an entry block or have no successors. |
| 97 | // (2) There's no path coming back through BB successors. |
Bruno Cardoso Lopes | 7900ef8 | 2015-06-24 17:53:17 +0000 | [diff] [blame] | 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)); |
Nick Lewycky | c0ebfbe3 | 2019-04-02 01:05:48 +0000 | [diff] [blame^] | 104 | return !isPotentiallyReachableFromMany(Worklist, BB, nullptr, 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) && |
Nick Lewycky | c0ebfbe3 | 2019-04-02 01:05:48 +0000 | [diff] [blame^] | 111 | !isPotentiallyReachable(I, BeforeHere, nullptr, 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; |
Daniel Neilson | 3c14872 | 2018-04-24 21:12:45 +0000 | [diff] [blame] | 142 | const DominatorTree *DT; |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 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, |
Artur Pilipenko | eba2365 | 2018-11-29 20:08:12 +0000 | [diff] [blame] | 159 | bool ReturnCaptures, bool StoreCaptures, |
| 160 | unsigned MaxUsesToExplore) { |
Nick Lewycky | 063ae58 | 2011-11-21 19:42:56 +0000 | [diff] [blame] | 161 | assert(!isa<GlobalValue>(V) && |
| 162 | "It doesn't make sense to ask whether a global is captured."); |
| 163 | |
Nick Lewycky | 7013a19 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 164 | // TODO: If StoreCaptures is not true, we could do Fancy analysis |
| 165 | // to determine whether this store is not actually an escape point. |
| 166 | // In that case, BasicAliasAnalysis should be updated as well to |
| 167 | // take advantage of this. |
| 168 | (void)StoreCaptures; |
Duncan Sands | e0aa0d6 | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 169 | |
Nick Lewycky | 7013a19 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 170 | SimpleCaptureTracker SCT(ReturnCaptures); |
Artur Pilipenko | 2a0146e | 2018-12-18 03:32:33 +0000 | [diff] [blame] | 171 | PointerMayBeCaptured(V, &SCT, MaxUsesToExplore); |
Nick Lewycky | 7013a19 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 172 | return SCT.Captured; |
Duncan Sands | e0aa0d6 | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 173 | } |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 174 | |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 175 | /// PointerMayBeCapturedBefore - Return true if this pointer value may be |
| 176 | /// captured by the enclosing function (which is required to exist). If a |
| 177 | /// DominatorTree is provided, only captures which happen before the given |
| 178 | /// instruction are considered. This routine can be expensive, so consider |
| 179 | /// caching the results. The boolean ReturnCaptures specifies whether |
| 180 | /// returning the value (or part of it) from the function counts as capturing |
| 181 | /// it or not. The boolean StoreCaptures specified whether storing the value |
| 182 | /// (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] | 183 | /// or not. A ordered basic block \p OBB can be used in order to speed up |
| 184 | /// queries about relative order among instructions in the same basic block. |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 185 | bool llvm::PointerMayBeCapturedBefore(const Value *V, bool ReturnCaptures, |
| 186 | bool StoreCaptures, const Instruction *I, |
Daniel Neilson | 3c14872 | 2018-04-24 21:12:45 +0000 | [diff] [blame] | 187 | const DominatorTree *DT, bool IncludeI, |
Artur Pilipenko | eba2365 | 2018-11-29 20:08:12 +0000 | [diff] [blame] | 188 | OrderedBasicBlock *OBB, |
| 189 | unsigned MaxUsesToExplore) { |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 190 | assert(!isa<GlobalValue>(V) && |
| 191 | "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] | 192 | bool UseNewOBB = OBB == nullptr; |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 193 | |
| 194 | if (!DT) |
Artur Pilipenko | 2a0146e | 2018-12-18 03:32:33 +0000 | [diff] [blame] | 195 | return PointerMayBeCaptured(V, ReturnCaptures, StoreCaptures, |
| 196 | MaxUsesToExplore); |
Bruno Cardoso Lopes | dfc1d96 | 2015-07-31 14:31:35 +0000 | [diff] [blame] | 197 | if (UseNewOBB) |
| 198 | OBB = new OrderedBasicBlock(I->getParent()); |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 199 | |
| 200 | // TODO: See comment in PointerMayBeCaptured regarding what could be done |
| 201 | // with StoreCaptures. |
| 202 | |
Bruno Cardoso Lopes | dfc1d96 | 2015-07-31 14:31:35 +0000 | [diff] [blame] | 203 | CapturesBefore CB(ReturnCaptures, I, DT, IncludeI, OBB); |
Artur Pilipenko | 2a0146e | 2018-12-18 03:32:33 +0000 | [diff] [blame] | 204 | PointerMayBeCaptured(V, &CB, MaxUsesToExplore); |
Bruno Cardoso Lopes | dfc1d96 | 2015-07-31 14:31:35 +0000 | [diff] [blame] | 205 | |
| 206 | if (UseNewOBB) |
| 207 | delete OBB; |
Hal Finkel | b035621 | 2014-07-21 13:15:48 +0000 | [diff] [blame] | 208 | return CB.Captured; |
| 209 | } |
| 210 | |
Artur Pilipenko | eba2365 | 2018-11-29 20:08:12 +0000 | [diff] [blame] | 211 | void llvm::PointerMayBeCaptured(const Value *V, CaptureTracker *Tracker, |
| 212 | unsigned MaxUsesToExplore) { |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 213 | assert(V->getType()->isPointerTy() && "Capture is for pointers only!"); |
Artur Pilipenko | eba2365 | 2018-11-29 20:08:12 +0000 | [diff] [blame] | 214 | SmallVector<const Use *, DefaultMaxUsesToExplore> Worklist; |
| 215 | SmallSet<const Use *, DefaultMaxUsesToExplore> Visited; |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 216 | |
Piotr Padlewski | e9832df | 2018-05-05 10:23:27 +0000 | [diff] [blame] | 217 | auto AddUses = [&](const Value *V) { |
Artur Pilipenko | 8b92c1d | 2018-11-29 02:15:35 +0000 | [diff] [blame] | 218 | unsigned Count = 0; |
Piotr Padlewski | e9832df | 2018-05-05 10:23:27 +0000 | [diff] [blame] | 219 | for (const Use &U : V->uses()) { |
| 220 | // If there are lots of uses, conservatively say that the value |
| 221 | // is captured to avoid taking too much compile time. |
Artur Pilipenko | eba2365 | 2018-11-29 20:08:12 +0000 | [diff] [blame] | 222 | if (Count++ >= MaxUsesToExplore) |
Piotr Padlewski | e9832df | 2018-05-05 10:23:27 +0000 | [diff] [blame] | 223 | return Tracker->tooManyUses(); |
| 224 | if (!Visited.insert(&U).second) |
| 225 | continue; |
| 226 | if (!Tracker->shouldExplore(&U)) |
| 227 | continue; |
| 228 | Worklist.push_back(&U); |
| 229 | } |
| 230 | }; |
| 231 | AddUses(V); |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 232 | |
| 233 | while (!Worklist.empty()) { |
Chandler Carruth | 64e9aa5 | 2014-03-05 10:21:48 +0000 | [diff] [blame] | 234 | const Use *U = Worklist.pop_back_val(); |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 235 | Instruction *I = cast<Instruction>(U->getUser()); |
| 236 | V = U->get(); |
| 237 | |
| 238 | switch (I->getOpcode()) { |
| 239 | case Instruction::Call: |
| 240 | case Instruction::Invoke: { |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 241 | auto *Call = cast<CallBase>(I); |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 242 | // Not captured if the callee is readonly, doesn't return a copy through |
| 243 | // its return value and doesn't unwind (a readonly function can leak bits |
| 244 | // by throwing an exception or not depending on the input value). |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 245 | if (Call->onlyReadsMemory() && Call->doesNotThrow() && |
| 246 | Call->getType()->isVoidTy()) |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 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. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 254 | if (isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(Call)) { |
| 255 | AddUses(Call); |
Piotr Padlewski | e9832df | 2018-05-05 10:23:27 +0000 | [diff] [blame] | 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. |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 261 | if (auto *MI = dyn_cast<MemIntrinsic>(Call)) |
David Majnemer | 7f32420 | 2016-05-26 17:36:22 +0000 | [diff] [blame] | 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). |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 273 | for (auto IdxOpPair : enumerate(Call->data_ops())) { |
| 274 | int Idx = IdxOpPair.index(); |
| 275 | Value *A = IdxOpPair.value(); |
| 276 | if (A == V && !Call->doesNotCapture(Idx)) |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 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; |
Chandler Carruth | 363ac68 | 2019-01-07 05:42:51 +0000 | [diff] [blame] | 280 | } |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 281 | break; |
| 282 | } |
| 283 | case Instruction::Load: |
David Majnemer | 7f32420 | 2016-05-26 17:36:22 +0000 | [diff] [blame] | 284 | // Volatile loads make the address observable. |
| 285 | if (cast<LoadInst>(I)->isVolatile()) |
| 286 | if (Tracker->captured(U)) |
| 287 | return; |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 288 | break; |
| 289 | case Instruction::VAArg: |
| 290 | // "va-arg" from a pointer does not cause it to be captured. |
| 291 | break; |
| 292 | case Instruction::Store: |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 293 | // Stored the pointer - conservatively assume it may be captured. |
David Majnemer | 7f32420 | 2016-05-26 17:36:22 +0000 | [diff] [blame] | 294 | // Volatile stores make the address observable. |
| 295 | if (V == I->getOperand(0) || cast<StoreInst>(I)->isVolatile()) |
Philip Reames | bd09e86 | 2016-02-18 19:23:27 +0000 | [diff] [blame] | 296 | if (Tracker->captured(U)) |
| 297 | return; |
| 298 | break; |
David Majnemer | 7f32420 | 2016-05-26 17:36:22 +0000 | [diff] [blame] | 299 | case Instruction::AtomicRMW: { |
| 300 | // atomicrmw conceptually includes both a load and store from |
| 301 | // the same location. |
| 302 | // As with a store, the location being accessed is not captured, |
| 303 | // but the value being stored is. |
| 304 | // Volatile stores make the address observable. |
| 305 | auto *ARMWI = cast<AtomicRMWInst>(I); |
| 306 | if (ARMWI->getValOperand() == V || ARMWI->isVolatile()) |
| 307 | if (Tracker->captured(U)) |
| 308 | return; |
| 309 | break; |
| 310 | } |
| 311 | case Instruction::AtomicCmpXchg: { |
| 312 | // cmpxchg conceptually includes both a load and store from |
| 313 | // the same location. |
| 314 | // As with a store, the location being accessed is not captured, |
| 315 | // but the value being stored is. |
| 316 | // Volatile stores make the address observable. |
| 317 | auto *ACXI = cast<AtomicCmpXchgInst>(I); |
| 318 | if (ACXI->getCompareOperand() == V || ACXI->getNewValOperand() == V || |
| 319 | ACXI->isVolatile()) |
| 320 | if (Tracker->captured(U)) |
| 321 | return; |
| 322 | break; |
| 323 | } |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 324 | case Instruction::BitCast: |
| 325 | case Instruction::GetElementPtr: |
| 326 | case Instruction::PHI: |
| 327 | case Instruction::Select: |
Matt Arsenault | e55a2c2 | 2014-01-14 19:11:52 +0000 | [diff] [blame] | 328 | case Instruction::AddrSpaceCast: |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 329 | // The original value is not captured via this if the new value isn't. |
Piotr Padlewski | e9832df | 2018-05-05 10:23:27 +0000 | [diff] [blame] | 330 | AddUses(I); |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 331 | break; |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 332 | case Instruction::ICmp: { |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 333 | // Don't count comparisons of a no-alias return value against null as |
| 334 | // captures. This allows us to ignore comparisons of malloc results |
| 335 | // with null, for example. |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 336 | if (ConstantPointerNull *CPN = |
| 337 | dyn_cast<ConstantPointerNull>(I->getOperand(1))) |
| 338 | if (CPN->getType()->getAddressSpace() == 0) |
| 339 | if (isNoAliasCall(V->stripPointerCasts())) |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 340 | break; |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 341 | // Comparison against value stored in global variable. Given the pointer |
| 342 | // does not escape, its value cannot be guessed and stored separately in a |
| 343 | // global variable. |
| 344 | unsigned OtherIndex = (I->getOperand(0) == V) ? 1 : 0; |
| 345 | auto *LI = dyn_cast<LoadInst>(I->getOperand(OtherIndex)); |
| 346 | if (LI && isa<GlobalVariable>(LI->getPointerOperand())) |
| 347 | break; |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 348 | // Otherwise, be conservative. There are crazy ways to capture pointers |
| 349 | // using comparisons. |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 350 | if (Tracker->captured(U)) |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 351 | return; |
| 352 | break; |
Anna Thomas | 43d7e1c | 2016-05-03 14:58:21 +0000 | [diff] [blame] | 353 | } |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 354 | default: |
| 355 | // Something else - be conservative and say it is captured. |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 356 | if (Tracker->captured(U)) |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 357 | return; |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | // All uses examined. |
| 363 | } |