blob: c621c9f9abc2cfdccaebec3e0dab5e7619740b90 [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"
Dan Gohmand68a0762009-01-05 17:59:02 +000017#include "llvm/Instructions.h"
Owen Anderson8f28c782008-10-10 08:36:25 +000018#include "llvm/Module.h"
Dan Gohmand68a0762009-01-05 17:59:02 +000019#include "llvm/Analysis/AliasAnalysis.h"
Owen Anderson8f28c782008-10-10 08:36:25 +000020#include "llvm/Support/InstIterator.h"
Dan Gohmand68a0762009-01-05 17:59:02 +000021#include "llvm/Target/TargetData.h"
Owen Anderson8f28c782008-10-10 08:36:25 +000022#include "llvm/ADT/SmallPtrSet.h"
Dan Gohmanf5220682008-10-16 20:18:31 +000023#include <vector>
Owen Anderson8f28c782008-10-10 08:36:25 +000024using namespace llvm;
25
26char EscapeAnalysis::ID = 0;
27static RegisterPass<EscapeAnalysis> X("escape-analysis",
28 "Pointer Escape Analysis", true, true);
29
30
Dan Gohmand68a0762009-01-05 17:59:02 +000031void EscapeAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
32 AU.addRequiredTransitive<TargetData>();
33 AU.addRequiredTransitive<AliasAnalysis>();
34 AU.setPreservesAll();
35}
36
Owen Anderson8f28c782008-10-10 08:36:25 +000037/// runOnFunction - Precomputation for escape analysis. This collects all know
38/// "escape points" in the def-use graph of the function. These are
39/// instructions which allow their inputs to escape from the current function.
40bool EscapeAnalysis::runOnFunction(Function& F) {
41 EscapePoints.clear();
42
43 TargetData& TD = getAnalysis<TargetData>();
44 AliasAnalysis& AA = getAnalysis<AliasAnalysis>();
45 Module* M = F.getParent();
46
47 // Walk through all instructions in the function, identifying those that
48 // may allow their inputs to escape.
49 for(inst_iterator II = inst_begin(F), IE = inst_end(F); II != IE; ++II) {
50 Instruction* I = &*II;
51
52 // The most obvious case is stores. Any store that may write to global
53 // memory or to a function argument potentially allows its input to escape.
54 if (StoreInst* S = dyn_cast<StoreInst>(I)) {
55 const Type* StoreType = S->getOperand(0)->getType();
56 unsigned StoreSize = TD.getTypeStoreSize(StoreType);
57 Value* Pointer = S->getPointerOperand();
58
59 bool inserted = false;
60 for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end();
61 AI != AE; ++AI) {
Owen Anderson5efff772008-10-12 06:03:38 +000062 if (!isa<PointerType>(AI->getType())) continue;
Duncan Sandsddbe5cb2008-10-16 09:14:58 +000063 AliasAnalysis::AliasResult R = AA.alias(Pointer, StoreSize, AI, ~0U);
Owen Anderson8f28c782008-10-10 08:36:25 +000064 if (R != AliasAnalysis::NoAlias) {
65 EscapePoints.insert(S);
66 inserted = true;
67 break;
68 }
69 }
70
71 if (inserted)
72 continue;
73
74 for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
75 GI != GE; ++GI) {
Duncan Sandsddbe5cb2008-10-16 09:14:58 +000076 AliasAnalysis::AliasResult R = AA.alias(Pointer, StoreSize, GI, ~0U);
Owen Anderson8f28c782008-10-10 08:36:25 +000077 if (R != AliasAnalysis::NoAlias) {
78 EscapePoints.insert(S);
79 break;
80 }
81 }
82
83 // Calls and invokes potentially allow their parameters to escape.
84 // FIXME: This can and should be refined. Intrinsics have known escape
85 // behavior, and alias analysis may be able to tell us more about callees.
86 } else if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
87 EscapePoints.insert(I);
88
89 // Returns allow the return value to escape. This is mostly important
90 // for malloc to alloca promotion.
91 } else if (isa<ReturnInst>(I)) {
92 EscapePoints.insert(I);
Owen Anderson4382f622008-10-12 03:59:45 +000093
94 // Branching on the value of a pointer may allow the value to escape through
95 // methods not discoverable via def-use chaining.
96 } else if(isa<BranchInst>(I) || isa<SwitchInst>(I)) {
97 EscapePoints.insert(I);
Owen Anderson8f28c782008-10-10 08:36:25 +000098 }
99
100 // FIXME: Are there any other possible escape points?
101 }
102
103 return false;
104}
105
106/// escapes - Determines whether the passed allocation can escape from the
107/// current function. It does this by using a simple worklist algorithm to
108/// search for a path in the def-use graph from the allocation to an
109/// escape point.
110/// FIXME: Once we've discovered a path, it would be a good idea to memoize it,
111/// and all of its subpaths, to amortize the cost of future queries.
Owen Anderson4b089922008-10-12 07:33:29 +0000112bool EscapeAnalysis::escapes(Value* A) {
113 assert(isa<PointerType>(A->getType()) &&
114 "Can't do escape analysis on non-pointer types!");
115
116 std::vector<Value*> worklist;
Owen Anderson8f28c782008-10-10 08:36:25 +0000117 worklist.push_back(A);
118
Owen Anderson4b089922008-10-12 07:33:29 +0000119 SmallPtrSet<Value*, 8> visited;
Owen Anderson5efff772008-10-12 06:03:38 +0000120 visited.insert(A);
Owen Anderson8f28c782008-10-10 08:36:25 +0000121 while (!worklist.empty()) {
Owen Anderson4b089922008-10-12 07:33:29 +0000122 Value* curr = worklist.back();
Owen Anderson8f28c782008-10-10 08:36:25 +0000123 worklist.pop_back();
124
Owen Anderson4b089922008-10-12 07:33:29 +0000125 if (Instruction* I = dyn_cast<Instruction>(curr))
Owen Andersonb5cf0482008-10-12 08:10:46 +0000126 if (EscapePoints.count(I)) {
127 BranchInst* B = dyn_cast<BranchInst>(I);
128 if (!B) return true;
129 Value* condition = B->getCondition();
130 ICmpInst* C = dyn_cast<ICmpInst>(condition);
131 if (!C) return true;
132 Value* O1 = C->getOperand(0);
133 Value* O2 = C->getOperand(1);
134 if (isa<MallocInst>(O1->stripPointerCasts())) {
135 if (!isa<ConstantPointerNull>(O2)) return true;
136 } else if(isa<MallocInst>(O2->stripPointerCasts())) {
137 if (!isa<ConstantPointerNull>(O1)) return true;
138 } else
139 return true;
140 }
Owen Anderson8f28c782008-10-10 08:36:25 +0000141
Owen Anderson5efff772008-10-12 06:03:38 +0000142 if (StoreInst* S = dyn_cast<StoreInst>(curr)) {
143 // We know this must be an instruction, because constant gep's would
144 // have been found to alias a global, so stores to them would have
145 // been in EscapePoints.
146 if (visited.insert(cast<Instruction>(S->getPointerOperand())))
147 worklist.push_back(cast<Instruction>(S->getPointerOperand()));
148 } else {
149 for (Instruction::use_iterator UI = curr->use_begin(),
150 UE = curr->use_end(); UI != UE; ++UI)
151 if (Instruction* U = dyn_cast<Instruction>(UI))
152 if (visited.insert(U))
Owen Anderson8f28c782008-10-10 08:36:25 +0000153 worklist.push_back(U);
Owen Anderson5efff772008-10-12 06:03:38 +0000154 }
Owen Anderson8f28c782008-10-10 08:36:25 +0000155 }
156
157 return false;
Duncan Sandsddbe5cb2008-10-16 09:14:58 +0000158}