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 | |
| 19 | #include "llvm/Analysis/CaptureTracking.h" |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
Nick Lewycky | 8899024 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 22 | namespace { |
Nick Lewycky | 7912ef9 | 2011-11-20 19:37:06 +0000 | [diff] [blame^] | 23 | struct SimpleCaptureTracker : public CaptureTracker { |
Nick Lewycky | 8899024 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 24 | explicit SimpleCaptureTracker(bool ReturnCaptures) |
| 25 | : ReturnCaptures(ReturnCaptures), Captured(false) {} |
| 26 | |
| 27 | void tooManyUses() { Captured = true; } |
| 28 | |
| 29 | bool shouldExplore(Use *U) { return true; } |
| 30 | |
| 31 | bool captured(Instruction *I) { |
| 32 | if (isa<ReturnInst>(I) && !ReturnCaptures) |
| 33 | return false; |
| 34 | |
| 35 | Captured = true; |
| 36 | return true; |
| 37 | } |
| 38 | |
| 39 | bool ReturnCaptures; |
| 40 | |
| 41 | bool Captured; |
| 42 | }; |
| 43 | } |
Dan Gohman | 8456d60 | 2009-12-08 23:59:12 +0000 | [diff] [blame] | 44 | |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 45 | /// PointerMayBeCaptured - Return true if this pointer value may be captured |
| 46 | /// by the enclosing function (which is required to exist). This routine can |
| 47 | /// be expensive, so consider caching the results. The boolean ReturnCaptures |
| 48 | /// specifies whether returning the value (or part of it) from the function |
Dan Gohman | f94b5ed | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 49 | /// counts as capturing it or not. The boolean StoreCaptures specified whether |
| 50 | /// storing the value (or part of it) into memory anywhere automatically |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 51 | /// counts as capturing it or not. |
Dan Gohman | f94b5ed | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 52 | bool llvm::PointerMayBeCaptured(const Value *V, |
| 53 | bool ReturnCaptures, bool StoreCaptures) { |
Nick Lewycky | 8899024 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 54 | // TODO: If StoreCaptures is not true, we could do Fancy analysis |
| 55 | // to determine whether this store is not actually an escape point. |
| 56 | // In that case, BasicAliasAnalysis should be updated as well to |
| 57 | // take advantage of this. |
| 58 | (void)StoreCaptures; |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 59 | |
Nick Lewycky | 8899024 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 60 | SimpleCaptureTracker SCT(ReturnCaptures); |
Nick Lewycky | 7912ef9 | 2011-11-20 19:37:06 +0000 | [diff] [blame^] | 61 | PointerMayBeCaptured(V, &SCT); |
Nick Lewycky | 8899024 | 2011-11-14 22:49:42 +0000 | [diff] [blame] | 62 | return SCT.Captured; |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 63 | } |
Nick Lewycky | 7912ef9 | 2011-11-20 19:37:06 +0000 | [diff] [blame^] | 64 | |
| 65 | /// TODO: Write a new FunctionPass AliasAnalysis so that it can keep |
| 66 | /// a cache. Then we can move the code from BasicAliasAnalysis into |
| 67 | /// that path, and remove this threshold. |
| 68 | static int const Threshold = 20; |
| 69 | |
| 70 | void llvm::PointerMayBeCaptured(const Value *V, CaptureTracker *Tracker) { |
| 71 | assert(V->getType()->isPointerTy() && "Capture is for pointers only!"); |
| 72 | SmallVector<Use*, Threshold> Worklist; |
| 73 | SmallSet<Use*, Threshold> Visited; |
| 74 | int Count = 0; |
| 75 | |
| 76 | for (Value::const_use_iterator UI = V->use_begin(), UE = V->use_end(); |
| 77 | UI != UE; ++UI) { |
| 78 | // If there are lots of uses, conservatively say that the value |
| 79 | // is captured to avoid taking too much compile time. |
| 80 | if (Count++ >= Threshold) |
| 81 | return Tracker->tooManyUses(); |
| 82 | |
| 83 | Use *U = &UI.getUse(); |
| 84 | if (!Tracker->shouldExplore(U)) continue; |
| 85 | Visited.insert(U); |
| 86 | Worklist.push_back(U); |
| 87 | } |
| 88 | |
| 89 | while (!Worklist.empty()) { |
| 90 | Use *U = Worklist.pop_back_val(); |
| 91 | Instruction *I = cast<Instruction>(U->getUser()); |
| 92 | V = U->get(); |
| 93 | |
| 94 | switch (I->getOpcode()) { |
| 95 | case Instruction::Call: |
| 96 | case Instruction::Invoke: { |
| 97 | CallSite CS(I); |
| 98 | // Not captured if the callee is readonly, doesn't return a copy through |
| 99 | // its return value and doesn't unwind (a readonly function can leak bits |
| 100 | // by throwing an exception or not depending on the input value). |
| 101 | if (CS.onlyReadsMemory() && CS.doesNotThrow() && I->getType()->isVoidTy()) |
| 102 | break; |
| 103 | |
| 104 | // Not captured if only passed via 'nocapture' arguments. Note that |
| 105 | // calling a function pointer does not in itself cause the pointer to |
| 106 | // be captured. This is a subtle point considering that (for example) |
| 107 | // the callee might return its own address. It is analogous to saying |
| 108 | // that loading a value from a pointer does not cause the pointer to be |
| 109 | // captured, even though the loaded value might be the pointer itself |
| 110 | // (think of self-referential objects). |
| 111 | CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end(); |
| 112 | for (CallSite::arg_iterator A = B; A != E; ++A) |
| 113 | if (A->get() == V && !CS.doesNotCapture(A - B)) |
| 114 | // The parameter is not marked 'nocapture' - captured. |
| 115 | if (Tracker->captured(I)) |
| 116 | return; |
| 117 | break; |
| 118 | } |
| 119 | case Instruction::Load: |
| 120 | // Loading from a pointer does not cause it to be captured. |
| 121 | break; |
| 122 | case Instruction::VAArg: |
| 123 | // "va-arg" from a pointer does not cause it to be captured. |
| 124 | break; |
| 125 | case Instruction::Store: |
| 126 | if (V == I->getOperand(0)) |
| 127 | // Stored the pointer - conservatively assume it may be captured. |
| 128 | if (Tracker->captured(I)) |
| 129 | return; |
| 130 | // Storing to the pointee does not cause the pointer to be captured. |
| 131 | break; |
| 132 | case Instruction::BitCast: |
| 133 | case Instruction::GetElementPtr: |
| 134 | case Instruction::PHI: |
| 135 | case Instruction::Select: |
| 136 | // The original value is not captured via this if the new value isn't. |
| 137 | for (Instruction::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 138 | UI != UE; ++UI) { |
| 139 | Use *U = &UI.getUse(); |
| 140 | if (Visited.insert(U)) |
| 141 | if (Tracker->shouldExplore(U)) |
| 142 | Worklist.push_back(U); |
| 143 | } |
| 144 | break; |
| 145 | case Instruction::ICmp: |
| 146 | // Don't count comparisons of a no-alias return value against null as |
| 147 | // captures. This allows us to ignore comparisons of malloc results |
| 148 | // with null, for example. |
| 149 | if (isNoAliasCall(V->stripPointerCasts())) |
| 150 | if (ConstantPointerNull *CPN = |
| 151 | dyn_cast<ConstantPointerNull>(I->getOperand(1))) |
| 152 | if (CPN->getType()->getAddressSpace() == 0) |
| 153 | break; |
| 154 | // Otherwise, be conservative. There are crazy ways to capture pointers |
| 155 | // using comparisons. |
| 156 | if (Tracker->captured(I)) |
| 157 | return; |
| 158 | break; |
| 159 | default: |
| 160 | // Something else - be conservative and say it is captured. |
| 161 | if (Tracker->captured(I)) |
| 162 | return; |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // All uses examined. |
| 168 | } |