blob: a5a46e187e3588401337131e78e2db319b704b80 [file] [log] [blame]
Owen Anderson8f28c782008-10-10 08:36:25 +00001//===------------- EscapeAnalysis.h - Pointer escape analysis -------------===//
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 provides the implementation of the pointer escape analysis.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "escape-analysis"
15#include "llvm/Analysis/EscapeAnalysis.h"
Owen Andersonb5cf0482008-10-12 08:10:46 +000016#include "llvm/Constants.h"
Owen Anderson8f28c782008-10-10 08:36:25 +000017#include "llvm/Module.h"
18#include "llvm/Support/InstIterator.h"
19#include "llvm/ADT/SmallPtrSet.h"
20using namespace llvm;
21
22char EscapeAnalysis::ID = 0;
23static RegisterPass<EscapeAnalysis> X("escape-analysis",
24 "Pointer Escape Analysis", true, true);
25
26
27/// runOnFunction - Precomputation for escape analysis. This collects all know
28/// "escape points" in the def-use graph of the function. These are
29/// instructions which allow their inputs to escape from the current function.
30bool EscapeAnalysis::runOnFunction(Function& F) {
31 EscapePoints.clear();
32
33 TargetData& TD = getAnalysis<TargetData>();
34 AliasAnalysis& AA = getAnalysis<AliasAnalysis>();
35 Module* M = F.getParent();
36
37 // Walk through all instructions in the function, identifying those that
38 // may allow their inputs to escape.
39 for(inst_iterator II = inst_begin(F), IE = inst_end(F); II != IE; ++II) {
40 Instruction* I = &*II;
41
42 // The most obvious case is stores. Any store that may write to global
43 // memory or to a function argument potentially allows its input to escape.
44 if (StoreInst* S = dyn_cast<StoreInst>(I)) {
45 const Type* StoreType = S->getOperand(0)->getType();
46 unsigned StoreSize = TD.getTypeStoreSize(StoreType);
47 Value* Pointer = S->getPointerOperand();
48
49 bool inserted = false;
50 for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end();
51 AI != AE; ++AI) {
Owen Anderson5efff772008-10-12 06:03:38 +000052 if (!isa<PointerType>(AI->getType())) continue;
Duncan Sandsddbe5cb2008-10-16 09:14:58 +000053 AliasAnalysis::AliasResult R = AA.alias(Pointer, StoreSize, AI, ~0U);
Owen Anderson8f28c782008-10-10 08:36:25 +000054 if (R != AliasAnalysis::NoAlias) {
55 EscapePoints.insert(S);
56 inserted = true;
57 break;
58 }
59 }
60
61 if (inserted)
62 continue;
63
64 for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
65 GI != GE; ++GI) {
Duncan Sandsddbe5cb2008-10-16 09:14:58 +000066 AliasAnalysis::AliasResult R = AA.alias(Pointer, StoreSize, GI, ~0U);
Owen Anderson8f28c782008-10-10 08:36:25 +000067 if (R != AliasAnalysis::NoAlias) {
68 EscapePoints.insert(S);
69 break;
70 }
71 }
72
73 // Calls and invokes potentially allow their parameters to escape.
74 // FIXME: This can and should be refined. Intrinsics have known escape
75 // behavior, and alias analysis may be able to tell us more about callees.
76 } else if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
77 EscapePoints.insert(I);
78
79 // Returns allow the return value to escape. This is mostly important
80 // for malloc to alloca promotion.
81 } else if (isa<ReturnInst>(I)) {
82 EscapePoints.insert(I);
Owen Anderson4382f622008-10-12 03:59:45 +000083
84 // Branching on the value of a pointer may allow the value to escape through
85 // methods not discoverable via def-use chaining.
86 } else if(isa<BranchInst>(I) || isa<SwitchInst>(I)) {
87 EscapePoints.insert(I);
Owen Anderson8f28c782008-10-10 08:36:25 +000088 }
89
90 // FIXME: Are there any other possible escape points?
91 }
92
93 return false;
94}
95
96/// escapes - Determines whether the passed allocation can escape from the
97/// current function. It does this by using a simple worklist algorithm to
98/// search for a path in the def-use graph from the allocation to an
99/// escape point.
100/// FIXME: Once we've discovered a path, it would be a good idea to memoize it,
101/// and all of its subpaths, to amortize the cost of future queries.
Owen Anderson4b089922008-10-12 07:33:29 +0000102bool EscapeAnalysis::escapes(Value* A) {
103 assert(isa<PointerType>(A->getType()) &&
104 "Can't do escape analysis on non-pointer types!");
105
106 std::vector<Value*> worklist;
Owen Anderson8f28c782008-10-10 08:36:25 +0000107 worklist.push_back(A);
108
Owen Anderson4b089922008-10-12 07:33:29 +0000109 SmallPtrSet<Value*, 8> visited;
Owen Anderson5efff772008-10-12 06:03:38 +0000110 visited.insert(A);
Owen Anderson8f28c782008-10-10 08:36:25 +0000111 while (!worklist.empty()) {
Owen Anderson4b089922008-10-12 07:33:29 +0000112 Value* curr = worklist.back();
Owen Anderson8f28c782008-10-10 08:36:25 +0000113 worklist.pop_back();
114
Owen Anderson4b089922008-10-12 07:33:29 +0000115 if (Instruction* I = dyn_cast<Instruction>(curr))
Owen Andersonb5cf0482008-10-12 08:10:46 +0000116 if (EscapePoints.count(I)) {
117 BranchInst* B = dyn_cast<BranchInst>(I);
118 if (!B) return true;
119 Value* condition = B->getCondition();
120 ICmpInst* C = dyn_cast<ICmpInst>(condition);
121 if (!C) return true;
122 Value* O1 = C->getOperand(0);
123 Value* O2 = C->getOperand(1);
124 if (isa<MallocInst>(O1->stripPointerCasts())) {
125 if (!isa<ConstantPointerNull>(O2)) return true;
126 } else if(isa<MallocInst>(O2->stripPointerCasts())) {
127 if (!isa<ConstantPointerNull>(O1)) return true;
128 } else
129 return true;
130 }
Owen Anderson8f28c782008-10-10 08:36:25 +0000131
Owen Anderson5efff772008-10-12 06:03:38 +0000132 if (StoreInst* S = dyn_cast<StoreInst>(curr)) {
133 // We know this must be an instruction, because constant gep's would
134 // have been found to alias a global, so stores to them would have
135 // been in EscapePoints.
136 if (visited.insert(cast<Instruction>(S->getPointerOperand())))
137 worklist.push_back(cast<Instruction>(S->getPointerOperand()));
138 } else {
139 for (Instruction::use_iterator UI = curr->use_begin(),
140 UE = curr->use_end(); UI != UE; ++UI)
141 if (Instruction* U = dyn_cast<Instruction>(UI))
142 if (visited.insert(U))
Owen Anderson8f28c782008-10-10 08:36:25 +0000143 worklist.push_back(U);
Owen Anderson5efff772008-10-12 06:03:38 +0000144 }
Owen Anderson8f28c782008-10-10 08:36:25 +0000145 }
146
147 return false;
Duncan Sandsddbe5cb2008-10-16 09:14:58 +0000148}