blob: 69dde4d7fdfcf125666c027cebe5a74599a80d99 [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"
16#include "llvm/Module.h"
17#include "llvm/Support/InstIterator.h"
18#include "llvm/ADT/SmallPtrSet.h"
19using namespace llvm;
20
21char EscapeAnalysis::ID = 0;
22static RegisterPass<EscapeAnalysis> X("escape-analysis",
23 "Pointer Escape Analysis", true, true);
24
25
26/// runOnFunction - Precomputation for escape analysis. This collects all know
27/// "escape points" in the def-use graph of the function. These are
28/// instructions which allow their inputs to escape from the current function.
29bool EscapeAnalysis::runOnFunction(Function& F) {
30 EscapePoints.clear();
31
32 TargetData& TD = getAnalysis<TargetData>();
33 AliasAnalysis& AA = getAnalysis<AliasAnalysis>();
34 Module* M = F.getParent();
35
36 // Walk through all instructions in the function, identifying those that
37 // may allow their inputs to escape.
38 for(inst_iterator II = inst_begin(F), IE = inst_end(F); II != IE; ++II) {
39 Instruction* I = &*II;
40
41 // The most obvious case is stores. Any store that may write to global
42 // memory or to a function argument potentially allows its input to escape.
43 if (StoreInst* S = dyn_cast<StoreInst>(I)) {
44 const Type* StoreType = S->getOperand(0)->getType();
45 unsigned StoreSize = TD.getTypeStoreSize(StoreType);
46 Value* Pointer = S->getPointerOperand();
47
48 bool inserted = false;
49 for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end();
50 AI != AE; ++AI) {
Owen Anderson5efff772008-10-12 06:03:38 +000051 if (!isa<PointerType>(AI->getType())) continue;
Owen Anderson8f28c782008-10-10 08:36:25 +000052 AliasAnalysis::AliasResult R = AA.alias(Pointer, StoreSize, AI, ~0UL);
53 if (R != AliasAnalysis::NoAlias) {
54 EscapePoints.insert(S);
55 inserted = true;
56 break;
57 }
58 }
59
60 if (inserted)
61 continue;
62
63 for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
64 GI != GE; ++GI) {
65 AliasAnalysis::AliasResult R = AA.alias(Pointer, StoreSize, GI, ~0UL);
66 if (R != AliasAnalysis::NoAlias) {
67 EscapePoints.insert(S);
68 break;
69 }
70 }
71
72 // Calls and invokes potentially allow their parameters to escape.
73 // FIXME: This can and should be refined. Intrinsics have known escape
74 // behavior, and alias analysis may be able to tell us more about callees.
75 } else if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
76 EscapePoints.insert(I);
77
78 // Returns allow the return value to escape. This is mostly important
79 // for malloc to alloca promotion.
80 } else if (isa<ReturnInst>(I)) {
81 EscapePoints.insert(I);
Owen Anderson4382f622008-10-12 03:59:45 +000082
83 // Branching on the value of a pointer may allow the value to escape through
84 // methods not discoverable via def-use chaining.
85 } else if(isa<BranchInst>(I) || isa<SwitchInst>(I)) {
86 EscapePoints.insert(I);
Owen Anderson8f28c782008-10-10 08:36:25 +000087 }
88
89 // FIXME: Are there any other possible escape points?
90 }
91
92 return false;
93}
94
95/// escapes - Determines whether the passed allocation can escape from the
96/// current function. It does this by using a simple worklist algorithm to
97/// search for a path in the def-use graph from the allocation to an
98/// escape point.
99/// FIXME: Once we've discovered a path, it would be a good idea to memoize it,
100/// and all of its subpaths, to amortize the cost of future queries.
101bool EscapeAnalysis::escapes(AllocationInst* A) {
Owen Anderson4382f622008-10-12 03:59:45 +0000102 std::vector<Instruction*> worklist;
Owen Anderson8f28c782008-10-10 08:36:25 +0000103 worklist.push_back(A);
104
Owen Anderson4382f622008-10-12 03:59:45 +0000105 SmallPtrSet<Instruction*, 8> visited;
Owen Anderson5efff772008-10-12 06:03:38 +0000106 visited.insert(A);
Owen Anderson8f28c782008-10-10 08:36:25 +0000107 while (!worklist.empty()) {
Owen Anderson4382f622008-10-12 03:59:45 +0000108 Instruction* curr = worklist.back();
Owen Anderson8f28c782008-10-10 08:36:25 +0000109 worklist.pop_back();
110
Owen Anderson4382f622008-10-12 03:59:45 +0000111 if (EscapePoints.count(curr))
112 return true;
Owen Anderson8f28c782008-10-10 08:36:25 +0000113
Owen Anderson5efff772008-10-12 06:03:38 +0000114 if (StoreInst* S = dyn_cast<StoreInst>(curr)) {
115 // We know this must be an instruction, because constant gep's would
116 // have been found to alias a global, so stores to them would have
117 // been in EscapePoints.
118 if (visited.insert(cast<Instruction>(S->getPointerOperand())))
119 worklist.push_back(cast<Instruction>(S->getPointerOperand()));
120 } else {
121 for (Instruction::use_iterator UI = curr->use_begin(),
122 UE = curr->use_end(); UI != UE; ++UI)
123 if (Instruction* U = dyn_cast<Instruction>(UI))
124 if (visited.insert(U))
Owen Anderson8f28c782008-10-10 08:36:25 +0000125 worklist.push_back(U);
Owen Anderson5efff772008-10-12 06:03:38 +0000126 }
Owen Anderson8f28c782008-10-10 08:36:25 +0000127 }
128
129 return false;
130}