1 //===--- CaptureTracking.cpp - Determine whether a pointer is captured ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
17 //===----------------------------------------------------------------------===//
19 #include "llvm/Analysis/CaptureTracking.h"
22 CaptureTracker::~CaptureTracker() {}
25 struct SimpleCaptureTracker : public CaptureTracker {
26 explicit SimpleCaptureTracker(bool ReturnCaptures)
27 : ReturnCaptures(ReturnCaptures), Captured(false) {}
29 void tooManyUses() { Captured = true; }
31 bool shouldExplore(Use *U) { return true; }
33 bool captured(Instruction *I) {
34 if (isa<ReturnInst>(I) && !ReturnCaptures)
47 /// PointerMayBeCaptured - Return true if this pointer value may be captured
48 /// by the enclosing function (which is required to exist). This routine can
49 /// be expensive, so consider caching the results. The boolean ReturnCaptures
50 /// specifies whether returning the value (or part of it) from the function
51 /// counts as capturing it or not. The boolean StoreCaptures specified whether
52 /// storing the value (or part of it) into memory anywhere automatically
53 /// counts as capturing it or not.
54 bool llvm::PointerMayBeCaptured(const Value *V,
55 bool ReturnCaptures, bool StoreCaptures) {
56 // TODO: If StoreCaptures is not true, we could do Fancy analysis
57 // to determine whether this store is not actually an escape point.
58 // In that case, BasicAliasAnalysis should be updated as well to
59 // take advantage of this.
62 SimpleCaptureTracker SCT(ReturnCaptures);
63 PointerMayBeCaptured(V, &SCT);
67 /// TODO: Write a new FunctionPass AliasAnalysis so that it can keep
68 /// a cache. Then we can move the code from BasicAliasAnalysis into
69 /// that path, and remove this threshold.
70 static int const Threshold = 20;
72 void llvm::PointerMayBeCaptured(const Value *V, CaptureTracker *Tracker) {
73 assert(V->getType()->isPointerTy() && "Capture is for pointers only!");
74 SmallVector<Use*, Threshold> Worklist;
75 SmallSet<Use*, Threshold> Visited;
78 for (Value::const_use_iterator UI = V->use_begin(), UE = V->use_end();
80 // If there are lots of uses, conservatively say that the value
81 // is captured to avoid taking too much compile time.
82 if (Count++ >= Threshold)
83 return Tracker->tooManyUses();
85 Use *U = &UI.getUse();
86 if (!Tracker->shouldExplore(U)) continue;
88 Worklist.push_back(U);
91 while (!Worklist.empty()) {
92 Use *U = Worklist.pop_back_val();
93 Instruction *I = cast<Instruction>(U->getUser());
96 switch (I->getOpcode()) {
97 case Instruction::Call:
98 case Instruction::Invoke: {
100 // Not captured if the callee is readonly, doesn't return a copy through
101 // its return value and doesn't unwind (a readonly function can leak bits
102 // by throwing an exception or not depending on the input value).
103 if (CS.onlyReadsMemory() && CS.doesNotThrow() && I->getType()->isVoidTy())
106 // Not captured if only passed via 'nocapture' arguments. Note that
107 // calling a function pointer does not in itself cause the pointer to
108 // be captured. This is a subtle point considering that (for example)
109 // the callee might return its own address. It is analogous to saying
110 // that loading a value from a pointer does not cause the pointer to be
111 // captured, even though the loaded value might be the pointer itself
112 // (think of self-referential objects).
113 CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
114 for (CallSite::arg_iterator A = B; A != E; ++A)
115 if (A->get() == V && !CS.doesNotCapture(A - B))
116 // The parameter is not marked 'nocapture' - captured.
117 if (Tracker->captured(I))
121 case Instruction::Load:
122 // Loading from a pointer does not cause it to be captured.
124 case Instruction::VAArg:
125 // "va-arg" from a pointer does not cause it to be captured.
127 case Instruction::Store:
128 if (V == I->getOperand(0))
129 // Stored the pointer - conservatively assume it may be captured.
130 if (Tracker->captured(I))
132 // Storing to the pointee does not cause the pointer to be captured.
134 case Instruction::BitCast:
135 case Instruction::GetElementPtr:
136 case Instruction::PHI:
137 case Instruction::Select:
138 // The original value is not captured via this if the new value isn't.
139 for (Instruction::use_iterator UI = I->use_begin(), UE = I->use_end();
141 Use *U = &UI.getUse();
142 if (Visited.insert(U))
143 if (Tracker->shouldExplore(U))
144 Worklist.push_back(U);
147 case Instruction::ICmp:
148 // Don't count comparisons of a no-alias return value against null as
149 // captures. This allows us to ignore comparisons of malloc results
150 // with null, for example.
151 if (isNoAliasCall(V->stripPointerCasts()))
152 if (ConstantPointerNull *CPN =
153 dyn_cast<ConstantPointerNull>(I->getOperand(1)))
154 if (CPN->getType()->getAddressSpace() == 0)
156 // Otherwise, be conservative. There are crazy ways to capture pointers
157 // using comparisons.
158 if (Tracker->captured(I))
162 // Something else - be conservative and say it is captured.
163 if (Tracker->captured(I))
169 // All uses examined.