Duncan Sands | e0aa0d6 | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 1 | //===--- CaptureTracking.cpp - Determine whether a pointer is captured ----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains routines that help determine which pointers are captured. |
| 11 | // A pointer value is captured if the function makes a copy of any part of the |
| 12 | // pointer that outlives the call. Not being captured means, more or less, that |
| 13 | // the pointer is only dereferenced and not stored in a global. Returning part |
| 14 | // of the pointer as the function return value may or may not count as capturing |
| 15 | // the pointer, depending on the context. |
| 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Jakub Staszak | 173bce3 | 2012-01-17 22:16:31 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallSet.h" |
| 20 | #include "llvm/ADT/SmallVector.h" |
Jakub Staszak | c733bf2 | 2013-03-10 00:34:01 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/AliasAnalysis.h" |
Duncan Sands | e0aa0d6 | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/CaptureTracking.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 23 | #include "llvm/IR/CallSite.h" |
Jakub Staszak | c733bf2 | 2013-03-10 00:34:01 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Constants.h" |
| 25 | #include "llvm/IR/Instructions.h" |
Jakub Staszak | c733bf2 | 2013-03-10 00:34:01 +0000 | [diff] [blame] | 26 | |
Duncan Sands | e0aa0d6 | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Nick Lewycky | aa2a00d | 2011-11-21 18:32:21 +0000 | [diff] [blame] | 29 | CaptureTracker::~CaptureTracker() {} |
| 30 | |
Chandler Carruth | 64e9aa5 | 2014-03-05 10:21:48 +0000 | [diff] [blame] | 31 | bool CaptureTracker::shouldExplore(const Use *U) { return true; } |
Nick Lewycky | 7c3b5d9 | 2012-10-08 22:12:48 +0000 | [diff] [blame] | 32 | |
Nick Lewycky | 7013a19 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 33 | namespace { |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 34 | struct SimpleCaptureTracker : public CaptureTracker { |
Nick Lewycky | 7013a19 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 35 | explicit SimpleCaptureTracker(bool ReturnCaptures) |
| 36 | : ReturnCaptures(ReturnCaptures), Captured(false) {} |
| 37 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 38 | void tooManyUses() override { Captured = true; } |
Nick Lewycky | 7013a19 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 39 | |
Chandler Carruth | 64e9aa5 | 2014-03-05 10:21:48 +0000 | [diff] [blame] | 40 | bool captured(const Use *U) override { |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 41 | if (isa<ReturnInst>(U->getUser()) && !ReturnCaptures) |
Chad Rosier | 8244b1d | 2012-05-10 23:38:07 +0000 | [diff] [blame] | 42 | return false; |
Nick Lewycky | 7013a19 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 43 | |
| 44 | Captured = true; |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | bool ReturnCaptures; |
| 49 | |
| 50 | bool Captured; |
| 51 | }; |
| 52 | } |
Dan Gohman | 2d27b19 | 2009-12-08 23:59:12 +0000 | [diff] [blame] | 53 | |
Duncan Sands | e0aa0d6 | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 54 | /// PointerMayBeCaptured - Return true if this pointer value may be captured |
| 55 | /// by the enclosing function (which is required to exist). This routine can |
| 56 | /// be expensive, so consider caching the results. The boolean ReturnCaptures |
| 57 | /// specifies whether returning the value (or part of it) from the function |
Dan Gohman | 94e6176 | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 58 | /// counts as capturing it or not. The boolean StoreCaptures specified whether |
| 59 | /// storing the value (or part of it) into memory anywhere automatically |
Duncan Sands | e0aa0d6 | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 60 | /// counts as capturing it or not. |
Dan Gohman | 94e6176 | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 61 | bool llvm::PointerMayBeCaptured(const Value *V, |
| 62 | bool ReturnCaptures, bool StoreCaptures) { |
Nick Lewycky | 063ae58 | 2011-11-21 19:42:56 +0000 | [diff] [blame] | 63 | assert(!isa<GlobalValue>(V) && |
| 64 | "It doesn't make sense to ask whether a global is captured."); |
| 65 | |
Nick Lewycky | 7013a19 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 66 | // TODO: If StoreCaptures is not true, we could do Fancy analysis |
| 67 | // to determine whether this store is not actually an escape point. |
| 68 | // In that case, BasicAliasAnalysis should be updated as well to |
| 69 | // take advantage of this. |
| 70 | (void)StoreCaptures; |
Duncan Sands | e0aa0d6 | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 71 | |
Nick Lewycky | 7013a19 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 72 | SimpleCaptureTracker SCT(ReturnCaptures); |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 73 | PointerMayBeCaptured(V, &SCT); |
Nick Lewycky | 7013a19 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 74 | return SCT.Captured; |
Duncan Sands | e0aa0d6 | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 75 | } |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 76 | |
| 77 | /// TODO: Write a new FunctionPass AliasAnalysis so that it can keep |
| 78 | /// a cache. Then we can move the code from BasicAliasAnalysis into |
| 79 | /// that path, and remove this threshold. |
| 80 | static int const Threshold = 20; |
| 81 | |
| 82 | void llvm::PointerMayBeCaptured(const Value *V, CaptureTracker *Tracker) { |
| 83 | assert(V->getType()->isPointerTy() && "Capture is for pointers only!"); |
Chandler Carruth | 64e9aa5 | 2014-03-05 10:21:48 +0000 | [diff] [blame] | 84 | SmallVector<const Use *, Threshold> Worklist; |
| 85 | SmallSet<const Use *, Threshold> Visited; |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 86 | int Count = 0; |
| 87 | |
| 88 | for (Value::const_use_iterator UI = V->use_begin(), UE = V->use_end(); |
| 89 | UI != UE; ++UI) { |
| 90 | // If there are lots of uses, conservatively say that the value |
| 91 | // is captured to avoid taking too much compile time. |
| 92 | if (Count++ >= Threshold) |
| 93 | return Tracker->tooManyUses(); |
| 94 | |
| 95 | Use *U = &UI.getUse(); |
| 96 | if (!Tracker->shouldExplore(U)) continue; |
| 97 | Visited.insert(U); |
| 98 | Worklist.push_back(U); |
| 99 | } |
| 100 | |
| 101 | while (!Worklist.empty()) { |
Chandler Carruth | 64e9aa5 | 2014-03-05 10:21:48 +0000 | [diff] [blame] | 102 | const Use *U = Worklist.pop_back_val(); |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 103 | Instruction *I = cast<Instruction>(U->getUser()); |
| 104 | V = U->get(); |
| 105 | |
| 106 | switch (I->getOpcode()) { |
| 107 | case Instruction::Call: |
| 108 | case Instruction::Invoke: { |
| 109 | CallSite CS(I); |
| 110 | // Not captured if the callee is readonly, doesn't return a copy through |
| 111 | // its return value and doesn't unwind (a readonly function can leak bits |
| 112 | // by throwing an exception or not depending on the input value). |
| 113 | if (CS.onlyReadsMemory() && CS.doesNotThrow() && I->getType()->isVoidTy()) |
| 114 | break; |
| 115 | |
| 116 | // Not captured if only passed via 'nocapture' arguments. Note that |
| 117 | // calling a function pointer does not in itself cause the pointer to |
| 118 | // be captured. This is a subtle point considering that (for example) |
| 119 | // the callee might return its own address. It is analogous to saying |
| 120 | // that loading a value from a pointer does not cause the pointer to be |
| 121 | // captured, even though the loaded value might be the pointer itself |
| 122 | // (think of self-referential objects). |
| 123 | CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end(); |
| 124 | for (CallSite::arg_iterator A = B; A != E; ++A) |
| 125 | if (A->get() == V && !CS.doesNotCapture(A - B)) |
| 126 | // The parameter is not marked 'nocapture' - captured. |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 127 | if (Tracker->captured(U)) |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 128 | return; |
| 129 | break; |
| 130 | } |
| 131 | case Instruction::Load: |
| 132 | // Loading from a pointer does not cause it to be captured. |
| 133 | break; |
| 134 | case Instruction::VAArg: |
| 135 | // "va-arg" from a pointer does not cause it to be captured. |
| 136 | break; |
| 137 | case Instruction::Store: |
| 138 | if (V == I->getOperand(0)) |
| 139 | // Stored the pointer - conservatively assume it may be captured. |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 140 | if (Tracker->captured(U)) |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 141 | return; |
| 142 | // Storing to the pointee does not cause the pointer to be captured. |
| 143 | break; |
| 144 | case Instruction::BitCast: |
| 145 | case Instruction::GetElementPtr: |
| 146 | case Instruction::PHI: |
| 147 | case Instruction::Select: |
Matt Arsenault | e55a2c2 | 2014-01-14 19:11:52 +0000 | [diff] [blame] | 148 | case Instruction::AddrSpaceCast: |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 149 | // The original value is not captured via this if the new value isn't. |
Benjamin Kramer | d2757ba | 2013-10-03 13:24:02 +0000 | [diff] [blame] | 150 | Count = 0; |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 151 | for (Instruction::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 152 | UI != UE; ++UI) { |
Benjamin Kramer | d2757ba | 2013-10-03 13:24:02 +0000 | [diff] [blame] | 153 | // If there are lots of uses, conservatively say that the value |
| 154 | // is captured to avoid taking too much compile time. |
| 155 | if (Count++ >= Threshold) |
| 156 | return Tracker->tooManyUses(); |
| 157 | |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 158 | Use *U = &UI.getUse(); |
| 159 | if (Visited.insert(U)) |
| 160 | if (Tracker->shouldExplore(U)) |
| 161 | Worklist.push_back(U); |
| 162 | } |
| 163 | break; |
| 164 | case Instruction::ICmp: |
| 165 | // Don't count comparisons of a no-alias return value against null as |
| 166 | // captures. This allows us to ignore comparisons of malloc results |
| 167 | // with null, for example. |
Nick Lewycky | c2ec072 | 2013-07-06 00:29:58 +0000 | [diff] [blame] | 168 | if (ConstantPointerNull *CPN = |
| 169 | dyn_cast<ConstantPointerNull>(I->getOperand(1))) |
| 170 | if (CPN->getType()->getAddressSpace() == 0) |
| 171 | if (isNoAliasCall(V->stripPointerCasts())) |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 172 | break; |
| 173 | // Otherwise, be conservative. There are crazy ways to capture pointers |
| 174 | // using comparisons. |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 175 | if (Tracker->captured(U)) |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 176 | return; |
| 177 | break; |
| 178 | default: |
| 179 | // Something else - be conservative and say it is captured. |
Nick Lewycky | 4c378a4 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 180 | if (Tracker->captured(U)) |
Nick Lewycky | 6ae03c3 | 2011-11-20 19:37:06 +0000 | [diff] [blame] | 181 | return; |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // All uses examined. |
| 187 | } |