blob: c6728b58ddbfc6a01582e7bdd7b5d5d365473bb6 [file] [log] [blame]
Chris Lattner1c7ce2c2002-10-01 22:34:12 +00001//===- Steensgaard.cpp - Context Insensitive Alias Analysis ---------------===//
2//
3// This pass uses the data structure graphs to implement a simple context
4// insensitive alias analysis. It does this by computing the local analysis
5// graphs for all of the functions, then merging them together into a single big
6// graph without cloning.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/Analysis/DataStructure.h"
11#include "llvm/Analysis/AliasAnalysis.h"
12#include "llvm/Module.h"
13#include "Support/Statistic.h"
14
15namespace {
16 class Steens : public Pass, public AliasAnalysis {
17 DSGraph *ResultGraph;
18 public:
19 Steens() : ResultGraph(0) {}
20 ~Steens() { assert(ResultGraph == 0 && "releaseMemory not called?"); }
21
22 //------------------------------------------------
23 // Implement the Pass API
24 //
25
26 // run - Build up the result graph, representing the pointer graph for the
27 // program.
28 //
29 bool run(Module &M);
30
31 virtual void releaseMemory() { delete ResultGraph; ResultGraph = 0; }
32
33 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
34 AU.setPreservesAll(); // Does not transform code...
35 AU.addRequired<LocalDataStructures>(); // Uses local dsgraph
36 AU.addRequired<AliasAnalysis>(); // Chains to another AA impl...
37 }
38
39 // print - Implement the Pass::print method...
40 void print(std::ostream &O, const Module *M) const {
41 assert(ResultGraph && "Result graph has not yet been computed!");
42 ResultGraph->writeGraphToFile(O, "steensgaards");
43 }
44
45 //------------------------------------------------
46 // Implement the AliasAnalysis API
47 //
48
49 // alias - This is the only method here that does anything interesting...
50 Result alias(const Value *V1, const Value *V2) const;
51
52 /// canCallModify - We are not interprocedural, so we do nothing exciting.
53 ///
54 Result canCallModify(const CallInst &CI, const Value *Ptr) const {
55 return MayAlias;
56 }
57
58 /// canInvokeModify - We are not interprocedural, so we do nothing exciting.
59 ///
60 Result canInvokeModify(const InvokeInst &I, const Value *Ptr) const {
61 return MayAlias; // We are not interprocedural
62 }
63
64 private:
65 void ResolveFunctionCall(Function *F, const std::vector<DSNodeHandle> &Call,
66 DSNodeHandle &RetVal);
67 };
68
69 // Register the pass...
70 RegisterOpt<Steens> X("steens-aa",
71 "Steensgaard's FlowInsensitive/ConIns alias analysis");
72
73 // Register as an implementation of AliasAnalysis
74 RegisterAnalysisGroup<AliasAnalysis, Steens> Y;
75}
76
77
78/// ResolveFunctionCall - Resolve the actual arguments of a call to function F
79/// with the specified call site descriptor. This function links the arguments
80/// and the return value for the call site context-insensitively.
81///
82void Steens::ResolveFunctionCall(Function *F,
83 const std::vector<DSNodeHandle> &Call,
84 DSNodeHandle &RetVal) {
85 assert(ResultGraph != 0 && "Result graph not allocated!");
86 std::map<Value*, DSNodeHandle> &ValMap = ResultGraph->getValueMap();
87
88 // Handle the return value of the function... which is Call[0]
89 if (Call[0].getNode() && RetVal.getNode())
90 RetVal.mergeWith(Call[0]);
91
92 // Loop over all pointer arguments, resolving them to their provided pointers
93 unsigned ArgIdx = 2; // Skip retval and function to call...
94 for (Function::aiterator AI = F->abegin(), AE = F->aend(); AI != AE; ++AI) {
95 std::map<Value*, DSNodeHandle>::iterator I = ValMap.find(AI);
96 if (I != ValMap.end()) // If its a pointer argument...
97 I->second.addEdgeTo(Call[ArgIdx++]);
98 }
99
100 assert(ArgIdx == Call.size() && "Argument resolution mismatch!");
101}
102
103
104/// run - Build up the result graph, representing the pointer graph for the
105/// program.
106///
107bool Steens::run(Module &M) {
108 assert(ResultGraph == 0 && "Result graph already allocated!");
109 LocalDataStructures &LDS = getAnalysis<LocalDataStructures>();
110
111 // Create a new, empty, graph...
112 ResultGraph = new DSGraph();
113
114 // RetValMap - Keep track of the return values for all functions that return
115 // valid pointers.
116 //
117 std::map<Function*, DSNodeHandle> RetValMap;
118
119 // Loop over the rest of the module, merging graphs for non-external functions
120 // into this graph.
121 //
122 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
123 if (!I->isExternal()) {
124 std::map<Value*, DSNodeHandle> ValMap;
125 { // Scope to free NodeMap memory ASAP
126 std::map<const DSNode*, DSNode*> NodeMap;
127 const DSGraph &FDSG = LDS.getDSGraph(*I);
128 DSNodeHandle RetNode = ResultGraph->cloneInto(FDSG, ValMap, NodeMap);
129
130 // Keep track of the return node of the function's graph if it returns a
131 // value...
132 //
133 if (RetNode.getNode())
134 RetValMap[I] = RetNode;
135 }
136
137 // Incorporate the inlined Function's ValueMap into the global ValueMap...
138 std::map<Value*, DSNodeHandle> &GVM = ResultGraph->getValueMap();
139
140 while (!ValMap.empty()) { // Loop over value map, moving entries over...
141 const std::pair<Value*, DSNodeHandle> &DSN = *ValMap.begin();
142 std::map<Value*, DSNodeHandle>::iterator I = GVM.find(DSN.first);
143 if (I == GVM.end())
144 GVM[DSN.first] = DSN.second;
145 else
146 I->second.mergeWith(DSN.second);
147 ValMap.erase(ValMap.begin());
148 }
149 }
150
151 // FIXME: Must recalculate and use the Incomplete markers!!
152
153 // Now that we have all of the graphs inlined, we can go about eliminating
154 // call nodes...
155 //
156 std::vector<std::vector<DSNodeHandle> > &Calls =
157 ResultGraph->getFunctionCalls();
158 for (unsigned i = 0; i != Calls.size(); ) {
159 std::vector<DSNodeHandle> &CurCall = Calls[i];
160
161 // Loop over the called functions, eliminating as many as possible...
162 std::vector<GlobalValue*> CallTargets = CurCall[1].getNode()->getGlobals();
163 for (unsigned c = 0; c != CallTargets.size(); ) {
164 // If we can eliminate this function call, do so!
165 bool Eliminated = false;
166 if (Function *F = dyn_cast<Function>(CallTargets[c]))
167 if (!F->isExternal()) {
168 ResolveFunctionCall(F, CurCall, RetValMap[F]);
169 Eliminated = true;
170 }
171 if (Eliminated)
172 CallTargets.erase(CallTargets.begin()+c);
173 else
174 ++c; // Cannot eliminate this call, skip over it...
175 }
176
177 if (CallTargets.empty()) // Eliminated all calls?
178 Calls.erase(Calls.begin()+i); // Remove from call list...
179 else
180 ++i; // Skip this call site...
181 }
182
183 // Update the "incomplete" markers on the nodes, ignoring unknownness due to
184 // incoming arguments...
185 ResultGraph->maskIncompleteMarkers();
186 ResultGraph->markIncompleteNodes(false);
187
188 // Remove any nodes that are dead after all of the merging we have done...
189 ResultGraph->removeTriviallyDeadNodes();
190
191 DEBUG(print(std::cerr, &M));
192 return false;
193}
194
195// alias - This is the only method here that does anything interesting...
196AliasAnalysis::Result Steens::alias(const Value *V1, const Value *V2) const {
197 assert(ResultGraph && "Result grcaph has not yet been computed!");
198
199 std::map<Value*, DSNodeHandle> &GVM = ResultGraph->getValueMap();
200
201 std::map<Value*, DSNodeHandle>::iterator I = GVM.find(const_cast<Value*>(V1));
202 if (I != GVM.end() && I->second.getNode()) {
203 DSNodeHandle &V1H = I->second;
204 std::map<Value*, DSNodeHandle>::iterator J=GVM.find(const_cast<Value*>(V2));
205 if (J != GVM.end() && J->second.getNode()) {
206 DSNodeHandle &V2H = J->second;
207 // If the two pointers point to different data structure graph nodes, they
208 // cannot alias!
209 if (V1H.getNode() != V2H.getNode())
210 return NoAlias;
211
212 // FIXME: If the two pointers point to the same node, and the offsets are
213 // different, and the LinkIndex vector doesn't alias the section, then the
214 // two pointers do not alias. We need access size information for the two
215 // accesses though!
216 //
217 }
218 }
219
220 // If we cannot determine alias properties based on our graph, fall back on
221 // some other AA implementation.
222 //
223 return getAnalysis<AliasAnalysis>().alias(V1, V2);
224}