blob: 24765228001dcc9a1cf67cfd755c4657f2bce6f2 [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"
Chris Lattnerc5f21de2002-10-02 22:14:38 +000011#include "llvm/Analysis/DSGraph.h"
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000012#include "llvm/Analysis/AliasAnalysis.h"
13#include "llvm/Module.h"
14#include "Support/Statistic.h"
15
16namespace {
Misha Brukmanf4445df2002-12-12 05:34:10 +000017 Statistic<> NumNoAlias ("steens", "Number of 'no alias' replies");
18 Statistic<> NumMayAlias ("steens", "Number of 'may alias' replies");
19};
20
21namespace {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000022 class Steens : public Pass, public AliasAnalysis {
23 DSGraph *ResultGraph;
24 public:
25 Steens() : ResultGraph(0) {}
26 ~Steens() { assert(ResultGraph == 0 && "releaseMemory not called?"); }
27
28 //------------------------------------------------
29 // Implement the Pass API
30 //
31
32 // run - Build up the result graph, representing the pointer graph for the
33 // program.
34 //
35 bool run(Module &M);
36
37 virtual void releaseMemory() { delete ResultGraph; ResultGraph = 0; }
38
39 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
40 AU.setPreservesAll(); // Does not transform code...
41 AU.addRequired<LocalDataStructures>(); // Uses local dsgraph
42 AU.addRequired<AliasAnalysis>(); // Chains to another AA impl...
43 }
44
45 // print - Implement the Pass::print method...
46 void print(std::ostream &O, const Module *M) const {
47 assert(ResultGraph && "Result graph has not yet been computed!");
48 ResultGraph->writeGraphToFile(O, "steensgaards");
49 }
50
51 //------------------------------------------------
52 // Implement the AliasAnalysis API
53 //
54
55 // alias - This is the only method here that does anything interesting...
Chris Lattnerd747e8f2002-11-06 18:08:32 +000056 Result alias(const Value *V1, const Value *V2);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000057
Chris Lattner4f53bef2002-11-01 17:34:23 +000058 /// canCallModify - Not implemented yet: FIXME
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000059 ///
Chris Lattnerd747e8f2002-11-06 18:08:32 +000060 Result canCallModify(const CallInst &CI, const Value *Ptr) {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000061 return MayAlias;
62 }
63
Chris Lattner4f53bef2002-11-01 17:34:23 +000064 /// canInvokeModify - Not implemented yet: FIXME
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000065 ///
Chris Lattnerd747e8f2002-11-06 18:08:32 +000066 Result canInvokeModify(const InvokeInst &I, const Value *Ptr) {
Chris Lattner4f53bef2002-11-01 17:34:23 +000067 return MayAlias;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000068 }
69
70 private:
Vikram S. Adve42fd1692002-10-20 18:07:37 +000071 void ResolveFunctionCall(Function *F, const DSCallSite &Call,
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000072 DSNodeHandle &RetVal);
73 };
74
75 // Register the pass...
76 RegisterOpt<Steens> X("steens-aa",
77 "Steensgaard's FlowInsensitive/ConIns alias analysis");
78
79 // Register as an implementation of AliasAnalysis
80 RegisterAnalysisGroup<AliasAnalysis, Steens> Y;
81}
82
83
84/// ResolveFunctionCall - Resolve the actual arguments of a call to function F
85/// with the specified call site descriptor. This function links the arguments
86/// and the return value for the call site context-insensitively.
87///
88void Steens::ResolveFunctionCall(Function *F,
Vikram S. Adve42fd1692002-10-20 18:07:37 +000089 const DSCallSite &Call,
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000090 DSNodeHandle &RetVal) {
91 assert(ResultGraph != 0 && "Result graph not allocated!");
Chris Lattner41c04f72003-02-01 04:52:08 +000092 hash_map<Value*, DSNodeHandle> &ValMap = ResultGraph->getScalarMap();
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000093
Vikram S. Adve42fd1692002-10-20 18:07:37 +000094 // Handle the return value of the function...
Chris Lattner0969c502002-10-21 02:08:03 +000095 if (Call.getRetVal().getNode() && RetVal.getNode())
96 RetVal.mergeWith(Call.getRetVal());
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000097
98 // Loop over all pointer arguments, resolving them to their provided pointers
Vikram S. Adve26b98262002-10-20 21:41:02 +000099 unsigned PtrArgIdx = 0;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000100 for (Function::aiterator AI = F->abegin(), AE = F->aend(); AI != AE; ++AI) {
Chris Lattner41c04f72003-02-01 04:52:08 +0000101 hash_map<Value*, DSNodeHandle>::iterator I = ValMap.find(AI);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000102 if (I != ValMap.end()) // If its a pointer argument...
Chris Lattner0969c502002-10-21 02:08:03 +0000103 I->second.addEdgeTo(Call.getPtrArg(PtrArgIdx++));
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000104 }
105
Vikram S. Adve26b98262002-10-20 21:41:02 +0000106 assert(PtrArgIdx == Call.getNumPtrArgs() && "Argument resolution mismatch!");
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000107}
108
109
110/// run - Build up the result graph, representing the pointer graph for the
111/// program.
112///
113bool Steens::run(Module &M) {
114 assert(ResultGraph == 0 && "Result graph already allocated!");
115 LocalDataStructures &LDS = getAnalysis<LocalDataStructures>();
116
117 // Create a new, empty, graph...
118 ResultGraph = new DSGraph();
119
120 // RetValMap - Keep track of the return values for all functions that return
121 // valid pointers.
122 //
Chris Lattner41c04f72003-02-01 04:52:08 +0000123 hash_map<Function*, DSNodeHandle> RetValMap;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000124
125 // Loop over the rest of the module, merging graphs for non-external functions
126 // into this graph.
127 //
128 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
129 if (!I->isExternal()) {
Chris Lattner41c04f72003-02-01 04:52:08 +0000130 hash_map<Value*, DSNodeHandle> ValMap;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000131 { // Scope to free NodeMap memory ASAP
Chris Lattner41c04f72003-02-01 04:52:08 +0000132 hash_map<const DSNode*, DSNodeHandle> NodeMap;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000133 const DSGraph &FDSG = LDS.getDSGraph(*I);
134 DSNodeHandle RetNode = ResultGraph->cloneInto(FDSG, ValMap, NodeMap);
135
136 // Keep track of the return node of the function's graph if it returns a
137 // value...
138 //
139 if (RetNode.getNode())
140 RetValMap[I] = RetNode;
141 }
142
Chris Lattnerc875f022002-11-03 21:27:48 +0000143 // Incorporate the inlined Function's ScalarMap into the global
144 // ScalarMap...
Chris Lattner41c04f72003-02-01 04:52:08 +0000145 hash_map<Value*, DSNodeHandle> &GVM = ResultGraph->getScalarMap();
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000146
147 while (!ValMap.empty()) { // Loop over value map, moving entries over...
148 const std::pair<Value*, DSNodeHandle> &DSN = *ValMap.begin();
Chris Lattner41c04f72003-02-01 04:52:08 +0000149 hash_map<Value*, DSNodeHandle>::iterator I = GVM.find(DSN.first);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000150 if (I == GVM.end())
151 GVM[DSN.first] = DSN.second;
152 else
153 I->second.mergeWith(DSN.second);
154 ValMap.erase(ValMap.begin());
155 }
156 }
157
158 // FIXME: Must recalculate and use the Incomplete markers!!
159
160 // Now that we have all of the graphs inlined, we can go about eliminating
161 // call nodes...
162 //
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000163 std::vector<DSCallSite> &Calls =
Chris Lattner01fb0c72002-11-08 21:27:37 +0000164 ResultGraph->getAuxFunctionCalls();
165 assert(Calls.empty() && "Aux call list is already in use??");
166
167 // Start with a copy of the original call sites...
168 Calls = ResultGraph->getFunctionCalls();
169
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000170 for (unsigned i = 0; i != Calls.size(); ) {
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000171 DSCallSite &CurCall = Calls[i];
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000172
173 // Loop over the called functions, eliminating as many as possible...
Chris Lattner0969c502002-10-21 02:08:03 +0000174 std::vector<GlobalValue*> CallTargets =
175 CurCall.getCallee().getNode()->getGlobals();
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000176 for (unsigned c = 0; c != CallTargets.size(); ) {
177 // If we can eliminate this function call, do so!
178 bool Eliminated = false;
179 if (Function *F = dyn_cast<Function>(CallTargets[c]))
180 if (!F->isExternal()) {
181 ResolveFunctionCall(F, CurCall, RetValMap[F]);
182 Eliminated = true;
183 }
184 if (Eliminated)
185 CallTargets.erase(CallTargets.begin()+c);
186 else
187 ++c; // Cannot eliminate this call, skip over it...
188 }
189
190 if (CallTargets.empty()) // Eliminated all calls?
191 Calls.erase(Calls.begin()+i); // Remove from call list...
192 else
193 ++i; // Skip this call site...
194 }
195
196 // Update the "incomplete" markers on the nodes, ignoring unknownness due to
197 // incoming arguments...
198 ResultGraph->maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000199 ResultGraph->markIncompleteNodes(DSGraph::IgnoreFormalArgs);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000200
201 // Remove any nodes that are dead after all of the merging we have done...
Chris Lattner394471f2003-01-23 22:05:33 +0000202 ResultGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000203
204 DEBUG(print(std::cerr, &M));
205 return false;
206}
207
208// alias - This is the only method here that does anything interesting...
Chris Lattnerd747e8f2002-11-06 18:08:32 +0000209AliasAnalysis::Result Steens::alias(const Value *V1, const Value *V2) {
Misha Brukmanf4445df2002-12-12 05:34:10 +0000210 assert(ResultGraph && "Result graph has not been computed yet!");
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000211
Chris Lattner0a91e632003-02-03 22:51:53 +0000212 hash_map<Value*, DSNodeHandle> &GSM = ResultGraph->getScalarMap();
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000213
Chris Lattner0a91e632003-02-03 22:51:53 +0000214 hash_map<Value*, DSNodeHandle>::iterator I = GSM.find(const_cast<Value*>(V1));
215 if (I != GSM.end() && I->second.getNode()) {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000216 DSNodeHandle &V1H = I->second;
Chris Lattner0a91e632003-02-03 22:51:53 +0000217 hash_map<Value*, DSNodeHandle>::iterator J=GSM.find(const_cast<Value*>(V2));
218 if (J != GSM.end() && J->second.getNode()) {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000219 DSNodeHandle &V2H = J->second;
220 // If the two pointers point to different data structure graph nodes, they
221 // cannot alias!
Chris Lattner0a91e632003-02-03 22:51:53 +0000222 if (V1H.getNode() != V2H.getNode()) { // FIXME: Handle incompleteness!
Misha Brukmanf4445df2002-12-12 05:34:10 +0000223 ++NumNoAlias;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000224 return NoAlias;
Misha Brukmanf4445df2002-12-12 05:34:10 +0000225 }
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000226 // FIXME: If the two pointers point to the same node, and the offsets are
227 // different, and the LinkIndex vector doesn't alias the section, then the
228 // two pointers do not alias. We need access size information for the two
229 // accesses though!
230 //
231 }
232 }
233
Misha Brukmanf4445df2002-12-12 05:34:10 +0000234 // Since Steensgaard cannot do any better, count it as a 'may alias'
235 ++NumMayAlias;
236
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000237 // If we cannot determine alias properties based on our graph, fall back on
238 // some other AA implementation.
239 //
240 return getAnalysis<AliasAnalysis>().alias(V1, V2);
241}