blob: b2c27d1dfc4bf9dd5df16ebd1251730e15362e83 [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"
Jay Foad562b84b2011-04-11 09:35:34 +000020#include "llvm/Constants.h"
Duncan Sands8556d2a2009-01-18 12:19:30 +000021#include "llvm/Instructions.h"
22#include "llvm/Value.h"
Dan Gohman76c638a2009-11-20 00:50:47 +000023#include "llvm/Analysis/AliasAnalysis.h"
Duncan Sands8556d2a2009-01-18 12:19:30 +000024#include "llvm/ADT/SmallSet.h"
25#include "llvm/ADT/SmallVector.h"
26#include "llvm/Support/CallSite.h"
27using namespace llvm;
28
Dan Gohman8456d602009-12-08 23:59:12 +000029/// As its comment mentions, PointerMayBeCaptured can be expensive.
30/// However, it's not easy for BasicAA to cache the result, because
31/// it's an ImmutablePass. To work around this, bound queries at a
32/// fixed number of uses.
33///
34/// TODO: Write a new FunctionPass AliasAnalysis so that it can keep
35/// a cache. Then we can move the code from BasicAliasAnalysis into
36/// that path, and remove this threshold.
37static int const Threshold = 20;
38
Duncan Sands8556d2a2009-01-18 12:19:30 +000039/// PointerMayBeCaptured - Return true if this pointer value may be captured
40/// by the enclosing function (which is required to exist). This routine can
41/// be expensive, so consider caching the results. The boolean ReturnCaptures
42/// specifies whether returning the value (or part of it) from the function
Dan Gohmanf94b5ed2009-11-19 21:57:48 +000043/// counts as capturing it or not. The boolean StoreCaptures specified whether
44/// storing the value (or part of it) into memory anywhere automatically
Duncan Sands8556d2a2009-01-18 12:19:30 +000045/// counts as capturing it or not.
Dan Gohmanf94b5ed2009-11-19 21:57:48 +000046bool llvm::PointerMayBeCaptured(const Value *V,
47 bool ReturnCaptures, bool StoreCaptures) {
Duncan Sands1df98592010-02-16 11:11:14 +000048 assert(V->getType()->isPointerTy() && "Capture is for pointers only!");
Dan Gohman89452f72009-12-09 18:48:53 +000049 SmallVector<Use*, Threshold> Worklist;
50 SmallSet<Use*, Threshold> Visited;
Dan Gohman8456d602009-12-08 23:59:12 +000051 int Count = 0;
Duncan Sands8556d2a2009-01-18 12:19:30 +000052
Gabor Greif60ad7812010-03-25 23:06:16 +000053 for (Value::const_use_iterator UI = V->use_begin(), UE = V->use_end();
Duncan Sands8556d2a2009-01-18 12:19:30 +000054 UI != UE; ++UI) {
Dan Gohman686abbb2009-12-09 00:28:42 +000055 // If there are lots of uses, conservatively say that the value
Dan Gohman8456d602009-12-08 23:59:12 +000056 // is captured to avoid taking too much compile time.
57 if (Count++ >= Threshold)
58 return true;
Dan Gohman686abbb2009-12-09 00:28:42 +000059
60 Use *U = &UI.getUse();
61 Visited.insert(U);
62 Worklist.push_back(U);
Duncan Sands8556d2a2009-01-18 12:19:30 +000063 }
64
65 while (!Worklist.empty()) {
66 Use *U = Worklist.pop_back_val();
67 Instruction *I = cast<Instruction>(U->getUser());
68 V = U->get();
69
70 switch (I->getOpcode()) {
71 case Instruction::Call:
72 case Instruction::Invoke: {
Gabor Greif30ae7922010-07-28 10:57:28 +000073 CallSite CS(I);
Duncan Sands19784262009-05-07 18:08:34 +000074 // Not captured if the callee is readonly, doesn't return a copy through
75 // its return value and doesn't unwind (a readonly function can leak bits
76 // by throwing an exception or not depending on the input value).
Dan Gohman452ae472009-11-20 00:43:11 +000077 if (CS.onlyReadsMemory() && CS.doesNotThrow() && I->getType()->isVoidTy())
Duncan Sands19784262009-05-07 18:08:34 +000078 break;
Duncan Sands8556d2a2009-01-18 12:19:30 +000079
80 // Not captured if only passed via 'nocapture' arguments. Note that
81 // calling a function pointer does not in itself cause the pointer to
82 // be captured. This is a subtle point considering that (for example)
83 // the callee might return its own address. It is analogous to saying
84 // that loading a value from a pointer does not cause the pointer to be
85 // captured, even though the loaded value might be the pointer itself
86 // (think of self-referential objects).
87 CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
88 for (CallSite::arg_iterator A = B; A != E; ++A)
Duncan Sands19784262009-05-07 18:08:34 +000089 if (A->get() == V && !CS.paramHasAttr(A - B + 1, Attribute::NoCapture))
90 // The parameter is not marked 'nocapture' - captured.
91 return true;
92 // Only passed via 'nocapture' arguments, or is the called function - not
93 // captured.
Duncan Sands8556d2a2009-01-18 12:19:30 +000094 break;
95 }
Duncan Sands8556d2a2009-01-18 12:19:30 +000096 case Instruction::Load:
97 // Loading from a pointer does not cause it to be captured.
Duncan Sands19784262009-05-07 18:08:34 +000098 break;
Dan Gohman1cdaa3e2010-11-09 20:09:35 +000099 case Instruction::VAArg:
100 // "va-arg" from a pointer does not cause it to be captured.
101 break;
Duncan Sands8556d2a2009-01-18 12:19:30 +0000102 case Instruction::Ret:
103 if (ReturnCaptures)
104 return true;
Duncan Sands19784262009-05-07 18:08:34 +0000105 break;
Duncan Sands8556d2a2009-01-18 12:19:30 +0000106 case Instruction::Store:
107 if (V == I->getOperand(0))
Dan Gohmanf94b5ed2009-11-19 21:57:48 +0000108 // Stored the pointer - conservatively assume it may be captured.
109 // TODO: If StoreCaptures is not true, we could do Fancy analysis
110 // to determine whether this store is not actually an escape point.
111 // In that case, BasicAliasAnalysis should be updated as well to
112 // take advantage of this.
Duncan Sands8556d2a2009-01-18 12:19:30 +0000113 return true;
114 // Storing to the pointee does not cause the pointer to be captured.
Duncan Sands19784262009-05-07 18:08:34 +0000115 break;
116 case Instruction::BitCast:
117 case Instruction::GetElementPtr:
118 case Instruction::PHI:
119 case Instruction::Select:
120 // The original value is not captured via this if the new value isn't.
121 for (Instruction::use_iterator UI = I->use_begin(), UE = I->use_end();
122 UI != UE; ++UI) {
123 Use *U = &UI.getUse();
124 if (Visited.insert(U))
125 Worklist.push_back(U);
126 }
127 break;
Dan Gohmanae079c22009-11-20 01:34:03 +0000128 case Instruction::ICmp:
Dan Gohman837be072009-11-20 17:50:21 +0000129 // Don't count comparisons of a no-alias return value against null as
130 // captures. This allows us to ignore comparisons of malloc results
131 // with null, for example.
Dan Gohman5bd698e2009-11-20 19:33:16 +0000132 if (isNoAliasCall(V->stripPointerCasts()))
Dan Gohman76c638a2009-11-20 00:50:47 +0000133 if (ConstantPointerNull *CPN =
134 dyn_cast<ConstantPointerNull>(I->getOperand(1)))
135 if (CPN->getType()->getAddressSpace() == 0)
136 break;
Dan Gohman76c638a2009-11-20 00:50:47 +0000137 // Otherwise, be conservative. There are crazy ways to capture pointers
138 // using comparisons.
Dan Gohman2a0fab12009-11-19 21:34:07 +0000139 return true;
Duncan Sands19784262009-05-07 18:08:34 +0000140 default:
141 // Something else - be conservative and say it is captured.
Duncan Sands8556d2a2009-01-18 12:19:30 +0000142 return true;
Duncan Sands8556d2a2009-01-18 12:19:30 +0000143 }
144 }
145
146 // All uses examined - not captured.
147 return false;
148}