blob: 9f27e3dd1808505106fd31f0cfa3f73bbc482788 [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"
22#include "llvm/ADT/SmallSet.h"
23#include "llvm/ADT/SmallVector.h"
24#include "llvm/Support/CallSite.h"
25using 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
Dan Gohmanf94b5ed2009-11-19 21:57:48 +000031/// counts as capturing it or not. The boolean StoreCaptures specified whether
32/// storing the value (or part of it) into memory anywhere automatically
Duncan Sands8556d2a2009-01-18 12:19:30 +000033/// counts as capturing it or not.
Dan Gohmanf94b5ed2009-11-19 21:57:48 +000034bool llvm::PointerMayBeCaptured(const Value *V,
35 bool ReturnCaptures, bool StoreCaptures) {
Duncan Sands8556d2a2009-01-18 12:19:30 +000036 assert(isa<PointerType>(V->getType()) && "Capture is for pointers only!");
37 SmallVector<Use*, 16> Worklist;
38 SmallSet<Use*, 16> Visited;
39
40 for (Value::use_const_iterator UI = V->use_begin(), UE = V->use_end();
41 UI != UE; ++UI) {
42 Use *U = &UI.getUse();
43 Visited.insert(U);
44 Worklist.push_back(U);
45 }
46
47 while (!Worklist.empty()) {
48 Use *U = Worklist.pop_back_val();
49 Instruction *I = cast<Instruction>(U->getUser());
50 V = U->get();
51
52 switch (I->getOpcode()) {
53 case Instruction::Call:
54 case Instruction::Invoke: {
Duncan Sands19784262009-05-07 18:08:34 +000055 CallSite CS = CallSite::get(I);
56 // Not captured if the callee is readonly, doesn't return a copy through
57 // its return value and doesn't unwind (a readonly function can leak bits
58 // by throwing an exception or not depending on the input value).
Dan Gohman452ae472009-11-20 00:43:11 +000059 if (CS.onlyReadsMemory() && CS.doesNotThrow() && I->getType()->isVoidTy())
Duncan Sands19784262009-05-07 18:08:34 +000060 break;
Duncan Sands8556d2a2009-01-18 12:19:30 +000061
62 // Not captured if only passed via 'nocapture' arguments. Note that
63 // calling a function pointer does not in itself cause the pointer to
64 // be captured. This is a subtle point considering that (for example)
65 // the callee might return its own address. It is analogous to saying
66 // that loading a value from a pointer does not cause the pointer to be
67 // captured, even though the loaded value might be the pointer itself
68 // (think of self-referential objects).
69 CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
70 for (CallSite::arg_iterator A = B; A != E; ++A)
Duncan Sands19784262009-05-07 18:08:34 +000071 if (A->get() == V && !CS.paramHasAttr(A - B + 1, Attribute::NoCapture))
72 // The parameter is not marked 'nocapture' - captured.
73 return true;
74 // Only passed via 'nocapture' arguments, or is the called function - not
75 // captured.
Duncan Sands8556d2a2009-01-18 12:19:30 +000076 break;
77 }
Duncan Sands8556d2a2009-01-18 12:19:30 +000078 case Instruction::Load:
79 // Loading from a pointer does not cause it to be captured.
Duncan Sands19784262009-05-07 18:08:34 +000080 break;
Duncan Sands8556d2a2009-01-18 12:19:30 +000081 case Instruction::Ret:
82 if (ReturnCaptures)
83 return true;
Duncan Sands19784262009-05-07 18:08:34 +000084 break;
Duncan Sands8556d2a2009-01-18 12:19:30 +000085 case Instruction::Store:
86 if (V == I->getOperand(0))
Dan Gohmanf94b5ed2009-11-19 21:57:48 +000087 // Stored the pointer - conservatively assume it may be captured.
88 // TODO: If StoreCaptures is not true, we could do Fancy analysis
89 // to determine whether this store is not actually an escape point.
90 // In that case, BasicAliasAnalysis should be updated as well to
91 // take advantage of this.
Duncan Sands8556d2a2009-01-18 12:19:30 +000092 return true;
93 // Storing to the pointee does not cause the pointer to be captured.
Duncan Sands19784262009-05-07 18:08:34 +000094 break;
95 case Instruction::BitCast:
96 case Instruction::GetElementPtr:
97 case Instruction::PHI:
98 case Instruction::Select:
99 // The original value is not captured via this if the new value isn't.
100 for (Instruction::use_iterator UI = I->use_begin(), UE = I->use_end();
101 UI != UE; ++UI) {
102 Use *U = &UI.getUse();
103 if (Visited.insert(U))
104 Worklist.push_back(U);
105 }
106 break;
Dan Gohman2a0fab12009-11-19 21:34:07 +0000107 case Instruction::ICmp:
108 // Comparing the pointer against null does not count as a capture.
Dan Gohman6d903092009-11-19 23:53:49 +0000109 if (ConstantPointerNull *CPN =
110 dyn_cast<ConstantPointerNull>(I->getOperand(1)))
111 if (CPN->getType()->getAddressSpace() == 0)
112 break;
Dan Gohman2a0fab12009-11-19 21:34:07 +0000113 return true;
Duncan Sands19784262009-05-07 18:08:34 +0000114 default:
115 // Something else - be conservative and say it is captured.
Duncan Sands8556d2a2009-01-18 12:19:30 +0000116 return true;
Duncan Sands8556d2a2009-01-18 12:19:30 +0000117 }
118 }
119
120 // All uses examined - not captured.
121 return false;
122}