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" |
| 20 | #include "llvm/Instructions.h" |
| 21 | #include "llvm/Value.h" |
Dan Gohman | 76c638a | 2009-11-20 00:50:47 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/AliasAnalysis.h" |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallSet.h" |
| 24 | #include "llvm/ADT/SmallVector.h" |
| 25 | #include "llvm/Support/CallSite.h" |
| 26 | using namespace llvm; |
| 27 | |
| 28 | /// PointerMayBeCaptured - Return true if this pointer value may be captured |
| 29 | /// by the enclosing function (which is required to exist). This routine can |
| 30 | /// be expensive, so consider caching the results. The boolean ReturnCaptures |
| 31 | /// specifies whether returning the value (or part of it) from the function |
Dan Gohman | f94b5ed | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 32 | /// counts as capturing it or not. The boolean StoreCaptures specified whether |
| 33 | /// storing the value (or part of it) into memory anywhere automatically |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 34 | /// counts as capturing it or not. |
Dan Gohman | f94b5ed | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 35 | bool llvm::PointerMayBeCaptured(const Value *V, |
| 36 | bool ReturnCaptures, bool StoreCaptures) { |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 37 | assert(isa<PointerType>(V->getType()) && "Capture is for pointers only!"); |
| 38 | SmallVector<Use*, 16> Worklist; |
| 39 | SmallSet<Use*, 16> Visited; |
| 40 | |
| 41 | for (Value::use_const_iterator UI = V->use_begin(), UE = V->use_end(); |
| 42 | UI != UE; ++UI) { |
| 43 | Use *U = &UI.getUse(); |
| 44 | Visited.insert(U); |
| 45 | Worklist.push_back(U); |
| 46 | } |
| 47 | |
| 48 | while (!Worklist.empty()) { |
| 49 | Use *U = Worklist.pop_back_val(); |
| 50 | Instruction *I = cast<Instruction>(U->getUser()); |
| 51 | V = U->get(); |
| 52 | |
| 53 | switch (I->getOpcode()) { |
| 54 | case Instruction::Call: |
| 55 | case Instruction::Invoke: { |
Duncan Sands | 1978426 | 2009-05-07 18:08:34 +0000 | [diff] [blame] | 56 | CallSite CS = CallSite::get(I); |
| 57 | // Not captured if the callee is readonly, doesn't return a copy through |
| 58 | // its return value and doesn't unwind (a readonly function can leak bits |
| 59 | // by throwing an exception or not depending on the input value). |
Dan Gohman | 452ae47 | 2009-11-20 00:43:11 +0000 | [diff] [blame] | 60 | if (CS.onlyReadsMemory() && CS.doesNotThrow() && I->getType()->isVoidTy()) |
Duncan Sands | 1978426 | 2009-05-07 18:08:34 +0000 | [diff] [blame] | 61 | break; |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 62 | |
| 63 | // Not captured if only passed via 'nocapture' arguments. Note that |
| 64 | // calling a function pointer does not in itself cause the pointer to |
| 65 | // be captured. This is a subtle point considering that (for example) |
| 66 | // the callee might return its own address. It is analogous to saying |
| 67 | // that loading a value from a pointer does not cause the pointer to be |
| 68 | // captured, even though the loaded value might be the pointer itself |
| 69 | // (think of self-referential objects). |
| 70 | CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end(); |
| 71 | for (CallSite::arg_iterator A = B; A != E; ++A) |
Duncan Sands | 1978426 | 2009-05-07 18:08:34 +0000 | [diff] [blame] | 72 | if (A->get() == V && !CS.paramHasAttr(A - B + 1, Attribute::NoCapture)) |
| 73 | // The parameter is not marked 'nocapture' - captured. |
| 74 | return true; |
| 75 | // Only passed via 'nocapture' arguments, or is the called function - not |
| 76 | // captured. |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 77 | break; |
| 78 | } |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 79 | case Instruction::Load: |
| 80 | // Loading from a pointer does not cause it to be captured. |
Duncan Sands | 1978426 | 2009-05-07 18:08:34 +0000 | [diff] [blame] | 81 | break; |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 82 | case Instruction::Ret: |
| 83 | if (ReturnCaptures) |
| 84 | return true; |
Duncan Sands | 1978426 | 2009-05-07 18:08:34 +0000 | [diff] [blame] | 85 | break; |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 86 | case Instruction::Store: |
| 87 | if (V == I->getOperand(0)) |
Dan Gohman | f94b5ed | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 88 | // Stored the pointer - conservatively assume it may be captured. |
| 89 | // TODO: If StoreCaptures is not true, we could do Fancy analysis |
| 90 | // to determine whether this store is not actually an escape point. |
| 91 | // In that case, BasicAliasAnalysis should be updated as well to |
| 92 | // take advantage of this. |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 93 | return true; |
| 94 | // Storing to the pointee does not cause the pointer to be captured. |
Duncan Sands | 1978426 | 2009-05-07 18:08:34 +0000 | [diff] [blame] | 95 | break; |
| 96 | case Instruction::BitCast: |
| 97 | case Instruction::GetElementPtr: |
| 98 | case Instruction::PHI: |
| 99 | case Instruction::Select: |
| 100 | // The original value is not captured via this if the new value isn't. |
| 101 | for (Instruction::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 102 | UI != UE; ++UI) { |
| 103 | Use *U = &UI.getUse(); |
| 104 | if (Visited.insert(U)) |
| 105 | Worklist.push_back(U); |
| 106 | } |
| 107 | break; |
Dan Gohman | ae079c2 | 2009-11-20 01:34:03 +0000 | [diff] [blame] | 108 | case Instruction::ICmp: |
Dan Gohman | 837be07 | 2009-11-20 17:50:21 +0000 | [diff] [blame] | 109 | // Don't count comparisons of a no-alias return value against null as |
| 110 | // captures. This allows us to ignore comparisons of malloc results |
| 111 | // with null, for example. |
| 112 | if (isNoAliasCall(V)) |
Dan Gohman | 76c638a | 2009-11-20 00:50:47 +0000 | [diff] [blame] | 113 | if (ConstantPointerNull *CPN = |
| 114 | dyn_cast<ConstantPointerNull>(I->getOperand(1))) |
| 115 | if (CPN->getType()->getAddressSpace() == 0) |
| 116 | break; |
Dan Gohman | 76c638a | 2009-11-20 00:50:47 +0000 | [diff] [blame] | 117 | // Otherwise, be conservative. There are crazy ways to capture pointers |
| 118 | // using comparisons. |
Dan Gohman | 2a0fab1 | 2009-11-19 21:34:07 +0000 | [diff] [blame] | 119 | return true; |
Duncan Sands | 1978426 | 2009-05-07 18:08:34 +0000 | [diff] [blame] | 120 | default: |
| 121 | // Something else - be conservative and say it is captured. |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 122 | return true; |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | // All uses examined - not captured. |
| 127 | return false; |
| 128 | } |