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" |
| 22 | #include "llvm/ADT/SmallSet.h" |
| 23 | #include "llvm/ADT/SmallVector.h" |
| 24 | #include "llvm/Support/CallSite.h" |
| 25 | using namespace llvm; |
| 26 | |
| 27 | /// PointerMayBeCaptured - Return true if this pointer value may be captured |
| 28 | /// by the enclosing function (which is required to exist). This routine can |
| 29 | /// be expensive, so consider caching the results. The boolean ReturnCaptures |
| 30 | /// specifies whether returning the value (or part of it) from the function |
| 31 | /// counts as capturing it or not. |
| 32 | bool llvm::PointerMayBeCaptured(const Value *V, bool ReturnCaptures) { |
| 33 | assert(isa<PointerType>(V->getType()) && "Capture is for pointers only!"); |
| 34 | SmallVector<Use*, 16> Worklist; |
| 35 | SmallSet<Use*, 16> Visited; |
| 36 | |
| 37 | for (Value::use_const_iterator UI = V->use_begin(), UE = V->use_end(); |
| 38 | UI != UE; ++UI) { |
| 39 | Use *U = &UI.getUse(); |
| 40 | Visited.insert(U); |
| 41 | Worklist.push_back(U); |
| 42 | } |
| 43 | |
| 44 | while (!Worklist.empty()) { |
| 45 | Use *U = Worklist.pop_back_val(); |
| 46 | Instruction *I = cast<Instruction>(U->getUser()); |
| 47 | V = U->get(); |
| 48 | |
| 49 | switch (I->getOpcode()) { |
| 50 | case Instruction::Call: |
| 51 | case Instruction::Invoke: { |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame^] | 52 | CallSite CS(I); |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 53 | |
| 54 | // Not captured if only passed via 'nocapture' arguments. Note that |
| 55 | // calling a function pointer does not in itself cause the pointer to |
| 56 | // be captured. This is a subtle point considering that (for example) |
| 57 | // the callee might return its own address. It is analogous to saying |
| 58 | // that loading a value from a pointer does not cause the pointer to be |
| 59 | // captured, even though the loaded value might be the pointer itself |
| 60 | // (think of self-referential objects). |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame^] | 61 | bool MayBeCaptured = false; |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 62 | CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end(); |
| 63 | for (CallSite::arg_iterator A = B; A != E; ++A) |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame^] | 64 | if (A->get() == V && !CS.paramHasAttr(A-B+1, Attribute::NoCapture)) { |
| 65 | // The parameter is not marked 'nocapture' - handled by generic code |
| 66 | // below. |
| 67 | MayBeCaptured = true; |
| 68 | break; |
| 69 | } |
| 70 | if (!MayBeCaptured) |
| 71 | // Only passed via 'nocapture' arguments, or is the called function - |
| 72 | // not captured. |
| 73 | continue; |
| 74 | if (!CS.doesNotThrow()) |
| 75 | // Even a readonly function can leak bits by throwing an exception or |
| 76 | // not depending on the input value. |
| 77 | return true; |
| 78 | // Fall through to the generic code. |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 79 | break; |
| 80 | } |
| 81 | case Instruction::Free: |
| 82 | // Freeing a pointer does not cause it to be captured. |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame^] | 83 | continue; |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 84 | case Instruction::Load: |
| 85 | // Loading from a pointer does not cause it to be captured. |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame^] | 86 | continue; |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 87 | case Instruction::Ret: |
| 88 | if (ReturnCaptures) |
| 89 | return true; |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame^] | 90 | continue; |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 91 | case Instruction::Store: |
| 92 | if (V == I->getOperand(0)) |
| 93 | // Stored the pointer - it may be captured. |
| 94 | return true; |
| 95 | // Storing to the pointee does not cause the pointer to be captured. |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame^] | 96 | continue; |
| 97 | } |
| 98 | |
| 99 | // If it may write to memory and isn't one of the special cases above, |
| 100 | // be conservative and assume the pointer is captured. |
| 101 | if (I->mayWriteToMemory()) |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 102 | return true; |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame^] | 103 | |
| 104 | // If the instruction doesn't write memory, it can only capture by |
| 105 | // having its own value depend on the input value. |
| 106 | const Type* Ty = I->getType(); |
| 107 | if (Ty == Type::VoidTy) |
| 108 | // The value of an instruction can't be a copy if it can't contain any |
| 109 | // information. |
| 110 | continue; |
| 111 | if (!isa<PointerType>(Ty)) |
| 112 | // At the moment, we don't track non-pointer values, so be conservative |
| 113 | // and assume the pointer is captured. |
| 114 | // FIXME: Track these too. This would need to be done very carefully as |
| 115 | // it is easy to leak bits via control flow if integer values are allowed. |
| 116 | return true; |
| 117 | |
| 118 | // The original value is not captured via this if the new value isn't. |
| 119 | for (Instruction::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 120 | UI != UE; ++UI) { |
| 121 | Use *U = &UI.getUse(); |
| 122 | if (Visited.insert(U)) |
| 123 | Worklist.push_back(U); |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | // All uses examined - not captured. |
| 128 | return false; |
| 129 | } |