blob: 649b760b706912704962821acbb64caebb579c71 [file] [log] [blame]
Duncan Sandsb233fb52009-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"
Victor Hernandezf9a7a332009-10-26 23:43:48 +000020#include "llvm/Analysis/MallocHelper.h"
Duncan Sandsb233fb52009-01-18 12:19:30 +000021#include "llvm/Instructions.h"
22#include "llvm/Value.h"
23#include "llvm/ADT/SmallSet.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/Support/CallSite.h"
26using namespace llvm;
27
28/// PointerMayBeCaptured - Return true if this pointer value may be captured
29/// by the enclosing function (which is required to exist). This routine can
30/// be expensive, so consider caching the results. The boolean ReturnCaptures
31/// specifies whether returning the value (or part of it) from the function
32/// counts as capturing it or not.
33bool llvm::PointerMayBeCaptured(const Value *V, bool ReturnCaptures) {
34 assert(isa<PointerType>(V->getType()) && "Capture is for pointers only!");
35 SmallVector<Use*, 16> Worklist;
36 SmallSet<Use*, 16> Visited;
37
38 for (Value::use_const_iterator UI = V->use_begin(), UE = V->use_end();
39 UI != UE; ++UI) {
40 Use *U = &UI.getUse();
41 Visited.insert(U);
42 Worklist.push_back(U);
43 }
44
45 while (!Worklist.empty()) {
46 Use *U = Worklist.pop_back_val();
47 Instruction *I = cast<Instruction>(U->getUser());
48 V = U->get();
49
50 switch (I->getOpcode()) {
51 case Instruction::Call:
Victor Hernandezf9a7a332009-10-26 23:43:48 +000052 if (isFreeCall(I))
53 // Freeing a pointer does not cause it to be captured.
54 break;
Duncan Sandsb233fb52009-01-18 12:19:30 +000055 case Instruction::Invoke: {
Duncan Sandsa4edebc2009-05-07 18:08:34 +000056 CallSite CS = CallSite::get(I);
57 // Not captured if the callee is readonly, doesn't return a copy through
58 // its return value and doesn't unwind (a readonly function can leak bits
59 // by throwing an exception or not depending on the input value).
60 if (CS.onlyReadsMemory() && CS.doesNotThrow() &&
Owen Anderson35b47072009-08-13 21:58:54 +000061 I->getType() == Type::getVoidTy(V->getContext()))
Duncan Sandsa4edebc2009-05-07 18:08:34 +000062 break;
Duncan Sandsb233fb52009-01-18 12:19:30 +000063
64 // Not captured if only passed via 'nocapture' arguments. Note that
65 // calling a function pointer does not in itself cause the pointer to
66 // be captured. This is a subtle point considering that (for example)
67 // the callee might return its own address. It is analogous to saying
68 // that loading a value from a pointer does not cause the pointer to be
69 // captured, even though the loaded value might be the pointer itself
70 // (think of self-referential objects).
71 CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
72 for (CallSite::arg_iterator A = B; A != E; ++A)
Duncan Sandsa4edebc2009-05-07 18:08:34 +000073 if (A->get() == V && !CS.paramHasAttr(A - B + 1, Attribute::NoCapture))
74 // The parameter is not marked 'nocapture' - captured.
75 return true;
76 // Only passed via 'nocapture' arguments, or is the called function - not
77 // captured.
Duncan Sandsb233fb52009-01-18 12:19:30 +000078 break;
79 }
Duncan Sandsb233fb52009-01-18 12:19:30 +000080 case Instruction::Load:
81 // Loading from a pointer does not cause it to be captured.
Duncan Sandsa4edebc2009-05-07 18:08:34 +000082 break;
Duncan Sandsb233fb52009-01-18 12:19:30 +000083 case Instruction::Ret:
84 if (ReturnCaptures)
85 return true;
Duncan Sandsa4edebc2009-05-07 18:08:34 +000086 break;
Duncan Sandsb233fb52009-01-18 12:19:30 +000087 case Instruction::Store:
88 if (V == I->getOperand(0))
89 // Stored the pointer - it may be captured.
90 return true;
91 // Storing to the pointee does not cause the pointer to be captured.
Duncan Sandsa4edebc2009-05-07 18:08:34 +000092 break;
93 case Instruction::BitCast:
94 case Instruction::GetElementPtr:
95 case Instruction::PHI:
96 case Instruction::Select:
97 // The original value is not captured via this if the new value isn't.
98 for (Instruction::use_iterator UI = I->use_begin(), UE = I->use_end();
99 UI != UE; ++UI) {
100 Use *U = &UI.getUse();
101 if (Visited.insert(U))
102 Worklist.push_back(U);
103 }
104 break;
105 default:
106 // Something else - be conservative and say it is captured.
Duncan Sandsb233fb52009-01-18 12:19:30 +0000107 return true;
Duncan Sandsb233fb52009-01-18 12:19:30 +0000108 }
109 }
110
111 // All uses examined - not captured.
112 return false;
113}