blob: 03bd70dfa34a64e7ef81959b0d259e2f0c462545 [file] [log] [blame]
Duncan Sands8556d2a2009-01-18 12:19:30 +00001//===--- 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 Sands8556d2a2009-01-18 12:19:30 +000020using namespace llvm;
21
Nick Lewycky6935b782011-11-21 18:32:21 +000022CaptureTracker::~CaptureTracker() {}
23
Nick Lewycky88990242011-11-14 22:49:42 +000024namespace {
Nick Lewycky7912ef92011-11-20 19:37:06 +000025 struct SimpleCaptureTracker : public CaptureTracker {
Nick Lewycky88990242011-11-14 22:49:42 +000026 explicit SimpleCaptureTracker(bool ReturnCaptures)
27 : ReturnCaptures(ReturnCaptures), Captured(false) {}
28
29 void tooManyUses() { Captured = true; }
30
31 bool shouldExplore(Use *U) { return true; }
32
33 bool captured(Instruction *I) {
34 if (isa<ReturnInst>(I) && !ReturnCaptures)
35 return false;
36
37 Captured = true;
38 return true;
39 }
40
41 bool ReturnCaptures;
42
43 bool Captured;
44 };
45}
Dan Gohman8456d602009-12-08 23:59:12 +000046
Duncan Sands8556d2a2009-01-18 12:19:30 +000047/// 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
Dan Gohmanf94b5ed2009-11-19 21:57:48 +000051/// counts as capturing it or not. The boolean StoreCaptures specified whether
52/// storing the value (or part of it) into memory anywhere automatically
Duncan Sands8556d2a2009-01-18 12:19:30 +000053/// counts as capturing it or not.
Dan Gohmanf94b5ed2009-11-19 21:57:48 +000054bool llvm::PointerMayBeCaptured(const Value *V,
55 bool ReturnCaptures, bool StoreCaptures) {
Nick Lewycky88990242011-11-14 22:49:42 +000056 // 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.
60 (void)StoreCaptures;
Duncan Sands8556d2a2009-01-18 12:19:30 +000061
Nick Lewycky88990242011-11-14 22:49:42 +000062 SimpleCaptureTracker SCT(ReturnCaptures);
Nick Lewycky7912ef92011-11-20 19:37:06 +000063 PointerMayBeCaptured(V, &SCT);
Nick Lewycky88990242011-11-14 22:49:42 +000064 return SCT.Captured;
Duncan Sands8556d2a2009-01-18 12:19:30 +000065}
Nick Lewycky7912ef92011-11-20 19:37:06 +000066
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.
70static int const Threshold = 20;
71
72void 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;
76 int Count = 0;
77
78 for (Value::const_use_iterator UI = V->use_begin(), UE = V->use_end();
79 UI != UE; ++UI) {
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();
84
85 Use *U = &UI.getUse();
86 if (!Tracker->shouldExplore(U)) continue;
87 Visited.insert(U);
88 Worklist.push_back(U);
89 }
90
91 while (!Worklist.empty()) {
92 Use *U = Worklist.pop_back_val();
93 Instruction *I = cast<Instruction>(U->getUser());
94 V = U->get();
95
96 switch (I->getOpcode()) {
97 case Instruction::Call:
98 case Instruction::Invoke: {
99 CallSite CS(I);
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())
104 break;
105
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))
118 return;
119 break;
120 }
121 case Instruction::Load:
122 // Loading from a pointer does not cause it to be captured.
123 break;
124 case Instruction::VAArg:
125 // "va-arg" from a pointer does not cause it to be captured.
126 break;
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))
131 return;
132 // Storing to the pointee does not cause the pointer to be captured.
133 break;
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();
140 UI != UE; ++UI) {
141 Use *U = &UI.getUse();
142 if (Visited.insert(U))
143 if (Tracker->shouldExplore(U))
144 Worklist.push_back(U);
145 }
146 break;
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)
155 break;
156 // Otherwise, be conservative. There are crazy ways to capture pointers
157 // using comparisons.
158 if (Tracker->captured(I))
159 return;
160 break;
161 default:
162 // Something else - be conservative and say it is captured.
163 if (Tracker->captured(I))
164 return;
165 break;
166 }
167 }
168
169 // All uses examined.
170}