blob: 10a8b1165df6445c6b80927fee8dfe51051c76ab [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"
20#include "llvm/Instructions.h"
21#include "llvm/Value.h"
Dan Gohman76c638a2009-11-20 00:50:47 +000022#include "llvm/Analysis/AliasAnalysis.h"
Duncan Sands8556d2a2009-01-18 12:19:30 +000023#include "llvm/ADT/SmallSet.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/Support/CallSite.h"
26using namespace llvm;
27
Dan Gohman8456d602009-12-08 23:59:12 +000028/// As its comment mentions, PointerMayBeCaptured can be expensive.
29/// However, it's not easy for BasicAA to cache the result, because
30/// it's an ImmutablePass. To work around this, bound queries at a
31/// fixed number of uses.
32///
33/// TODO: Write a new FunctionPass AliasAnalysis so that it can keep
34/// a cache. Then we can move the code from BasicAliasAnalysis into
35/// that path, and remove this threshold.
36static int const Threshold = 20;
37
Duncan Sands8556d2a2009-01-18 12:19:30 +000038/// PointerMayBeCaptured - Return true if this pointer value may be captured
39/// by the enclosing function (which is required to exist). This routine can
40/// be expensive, so consider caching the results. The boolean ReturnCaptures
41/// specifies whether returning the value (or part of it) from the function
Dan Gohmanf94b5ed2009-11-19 21:57:48 +000042/// counts as capturing it or not. The boolean StoreCaptures specified whether
43/// storing the value (or part of it) into memory anywhere automatically
Duncan Sands8556d2a2009-01-18 12:19:30 +000044/// counts as capturing it or not.
Dan Gohmanf94b5ed2009-11-19 21:57:48 +000045bool llvm::PointerMayBeCaptured(const Value *V,
46 bool ReturnCaptures, bool StoreCaptures) {
Duncan Sands8556d2a2009-01-18 12:19:30 +000047 assert(isa<PointerType>(V->getType()) && "Capture is for pointers only!");
Dan Gohman89452f72009-12-09 18:48:53 +000048 SmallVector<Use*, Threshold> Worklist;
49 SmallSet<Use*, Threshold> Visited;
Dan Gohman8456d602009-12-08 23:59:12 +000050 int Count = 0;
Duncan Sands8556d2a2009-01-18 12:19:30 +000051
52 for (Value::use_const_iterator UI = V->use_begin(), UE = V->use_end();
53 UI != UE; ++UI) {
Dan Gohman686abbb2009-12-09 00:28:42 +000054 // If there are lots of uses, conservatively say that the value
Dan Gohman8456d602009-12-08 23:59:12 +000055 // is captured to avoid taking too much compile time.
56 if (Count++ >= Threshold)
57 return true;
Dan Gohman686abbb2009-12-09 00:28:42 +000058
59 Use *U = &UI.getUse();
60 Visited.insert(U);
61 Worklist.push_back(U);
Duncan Sands8556d2a2009-01-18 12:19:30 +000062 }
63
64 while (!Worklist.empty()) {
65 Use *U = Worklist.pop_back_val();
66 Instruction *I = cast<Instruction>(U->getUser());
67 V = U->get();
68
69 switch (I->getOpcode()) {
70 case Instruction::Call:
71 case Instruction::Invoke: {
Duncan Sands19784262009-05-07 18:08:34 +000072 CallSite CS = CallSite::get(I);
73 // Not captured if the callee is readonly, doesn't return a copy through
74 // its return value and doesn't unwind (a readonly function can leak bits
75 // by throwing an exception or not depending on the input value).
Dan Gohman452ae472009-11-20 00:43:11 +000076 if (CS.onlyReadsMemory() && CS.doesNotThrow() && I->getType()->isVoidTy())
Duncan Sands19784262009-05-07 18:08:34 +000077 break;
Duncan Sands8556d2a2009-01-18 12:19:30 +000078
79 // Not captured if only passed via 'nocapture' arguments. Note that
80 // calling a function pointer does not in itself cause the pointer to
81 // be captured. This is a subtle point considering that (for example)
82 // the callee might return its own address. It is analogous to saying
83 // that loading a value from a pointer does not cause the pointer to be
84 // captured, even though the loaded value might be the pointer itself
85 // (think of self-referential objects).
86 CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
87 for (CallSite::arg_iterator A = B; A != E; ++A)
Duncan Sands19784262009-05-07 18:08:34 +000088 if (A->get() == V && !CS.paramHasAttr(A - B + 1, Attribute::NoCapture))
89 // The parameter is not marked 'nocapture' - captured.
90 return true;
91 // Only passed via 'nocapture' arguments, or is the called function - not
92 // captured.
Duncan Sands8556d2a2009-01-18 12:19:30 +000093 break;
94 }
Duncan Sands8556d2a2009-01-18 12:19:30 +000095 case Instruction::Load:
96 // Loading from a pointer does not cause it to be captured.
Duncan Sands19784262009-05-07 18:08:34 +000097 break;
Duncan Sands8556d2a2009-01-18 12:19:30 +000098 case Instruction::Ret:
99 if (ReturnCaptures)
100 return true;
Duncan Sands19784262009-05-07 18:08:34 +0000101 break;
Duncan Sands8556d2a2009-01-18 12:19:30 +0000102 case Instruction::Store:
103 if (V == I->getOperand(0))
Dan Gohmanf94b5ed2009-11-19 21:57:48 +0000104 // Stored the pointer - conservatively assume it may be captured.
105 // TODO: If StoreCaptures is not true, we could do Fancy analysis
106 // to determine whether this store is not actually an escape point.
107 // In that case, BasicAliasAnalysis should be updated as well to
108 // take advantage of this.
Duncan Sands8556d2a2009-01-18 12:19:30 +0000109 return true;
110 // Storing to the pointee does not cause the pointer to be captured.
Duncan Sands19784262009-05-07 18:08:34 +0000111 break;
112 case Instruction::BitCast:
113 case Instruction::GetElementPtr:
114 case Instruction::PHI:
115 case Instruction::Select:
116 // The original value is not captured via this if the new value isn't.
117 for (Instruction::use_iterator UI = I->use_begin(), UE = I->use_end();
118 UI != UE; ++UI) {
119 Use *U = &UI.getUse();
120 if (Visited.insert(U))
121 Worklist.push_back(U);
122 }
123 break;
Dan Gohmanae079c22009-11-20 01:34:03 +0000124 case Instruction::ICmp:
Dan Gohman837be072009-11-20 17:50:21 +0000125 // Don't count comparisons of a no-alias return value against null as
126 // captures. This allows us to ignore comparisons of malloc results
127 // with null, for example.
Dan Gohman5bd698e2009-11-20 19:33:16 +0000128 if (isNoAliasCall(V->stripPointerCasts()))
Dan Gohman76c638a2009-11-20 00:50:47 +0000129 if (ConstantPointerNull *CPN =
130 dyn_cast<ConstantPointerNull>(I->getOperand(1)))
131 if (CPN->getType()->getAddressSpace() == 0)
132 break;
Dan Gohman76c638a2009-11-20 00:50:47 +0000133 // Otherwise, be conservative. There are crazy ways to capture pointers
134 // using comparisons.
Dan Gohman2a0fab12009-11-19 21:34:07 +0000135 return true;
Duncan Sands19784262009-05-07 18:08:34 +0000136 default:
137 // Something else - be conservative and say it is captured.
Duncan Sands8556d2a2009-01-18 12:19:30 +0000138 return true;
Duncan Sands8556d2a2009-01-18 12:19:30 +0000139 }
140 }
141
142 // All uses examined - not captured.
143 return false;
144}