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