blob: 0458670e6fbb70856f04efbfd98e2d93b3cfd257 [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"
Chris Lattner47a307a2003-02-09 18:42:16 +000014#include "Support/Statistic.h"
Misha Brukmanf4445df2002-12-12 05:34:10 +000015
16namespace {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000017 class Steens : public Pass, public AliasAnalysis {
18 DSGraph *ResultGraph;
Chris Lattnere6c0b5d2003-02-04 00:03:37 +000019 DSGraph *GlobalsGraph; // FIXME: Eliminate globals graph stuff from DNE
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000020 public:
21 Steens() : ResultGraph(0) {}
22 ~Steens() { assert(ResultGraph == 0 && "releaseMemory not called?"); }
23
24 //------------------------------------------------
25 // Implement the Pass API
26 //
27
28 // run - Build up the result graph, representing the pointer graph for the
29 // program.
30 //
31 bool run(Module &M);
32
33 virtual void releaseMemory() { delete ResultGraph; ResultGraph = 0; }
34
35 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
36 AU.setPreservesAll(); // Does not transform code...
37 AU.addRequired<LocalDataStructures>(); // Uses local dsgraph
38 AU.addRequired<AliasAnalysis>(); // Chains to another AA impl...
39 }
40
41 // print - Implement the Pass::print method...
42 void print(std::ostream &O, const Module *M) const {
43 assert(ResultGraph && "Result graph has not yet been computed!");
44 ResultGraph->writeGraphToFile(O, "steensgaards");
45 }
46
47 //------------------------------------------------
48 // Implement the AliasAnalysis API
49 //
50
51 // alias - This is the only method here that does anything interesting...
Chris Lattnerd747e8f2002-11-06 18:08:32 +000052 Result alias(const Value *V1, const Value *V2);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000053
Chris Lattner4f53bef2002-11-01 17:34:23 +000054 /// canCallModify - Not implemented yet: FIXME
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000055 ///
Chris Lattnerd747e8f2002-11-06 18:08:32 +000056 Result canCallModify(const CallInst &CI, const Value *Ptr) {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000057 return MayAlias;
58 }
59
Chris Lattner4f53bef2002-11-01 17:34:23 +000060 /// canInvokeModify - Not implemented yet: FIXME
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000061 ///
Chris Lattnerd747e8f2002-11-06 18:08:32 +000062 Result canInvokeModify(const InvokeInst &I, const Value *Ptr) {
Chris Lattner4f53bef2002-11-01 17:34:23 +000063 return MayAlias;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000064 }
65
66 private:
Vikram S. Adve42fd1692002-10-20 18:07:37 +000067 void ResolveFunctionCall(Function *F, const DSCallSite &Call,
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000068 DSNodeHandle &RetVal);
69 };
70
71 // Register the pass...
72 RegisterOpt<Steens> X("steens-aa",
Chris Lattner6022ef42003-02-08 23:03:17 +000073 "Steensgaard's alias analysis (DSGraph based)");
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000074
75 // Register as an implementation of AliasAnalysis
76 RegisterAnalysisGroup<AliasAnalysis, Steens> Y;
77}
78
79
80/// ResolveFunctionCall - Resolve the actual arguments of a call to function F
81/// with the specified call site descriptor. This function links the arguments
82/// and the return value for the call site context-insensitively.
83///
Chris Lattnere6c0b5d2003-02-04 00:03:37 +000084void Steens::ResolveFunctionCall(Function *F, const DSCallSite &Call,
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000085 DSNodeHandle &RetVal) {
86 assert(ResultGraph != 0 && "Result graph not allocated!");
Chris Lattner41c04f72003-02-01 04:52:08 +000087 hash_map<Value*, DSNodeHandle> &ValMap = ResultGraph->getScalarMap();
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000088
Vikram S. Adve42fd1692002-10-20 18:07:37 +000089 // Handle the return value of the function...
Chris Lattner0969c502002-10-21 02:08:03 +000090 if (Call.getRetVal().getNode() && RetVal.getNode())
91 RetVal.mergeWith(Call.getRetVal());
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000092
93 // Loop over all pointer arguments, resolving them to their provided pointers
Vikram S. Adve26b98262002-10-20 21:41:02 +000094 unsigned PtrArgIdx = 0;
Chris Lattner88c72942003-02-10 18:16:19 +000095 for (Function::aiterator AI = F->abegin(), AE = F->aend();
96 AI != AE && PtrArgIdx < Call.getNumPtrArgs(); ++AI) {
Chris Lattner41c04f72003-02-01 04:52:08 +000097 hash_map<Value*, DSNodeHandle>::iterator I = ValMap.find(AI);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000098 if (I != ValMap.end()) // If its a pointer argument...
Chris Lattnere6c0b5d2003-02-04 00:03:37 +000099 I->second.mergeWith(Call.getPtrArg(PtrArgIdx++));
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000100 }
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000101}
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();
Chris Lattnere6c0b5d2003-02-04 00:03:37 +0000113 GlobalsGraph = new DSGraph();
114 ResultGraph->setGlobalsGraph(GlobalsGraph);
Chris Lattneraf283512003-02-09 19:26:47 +0000115 ResultGraph->setPrintAuxCalls();
116
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000117 // RetValMap - Keep track of the return values for all functions that return
118 // valid pointers.
119 //
Chris Lattner41c04f72003-02-01 04:52:08 +0000120 hash_map<Function*, DSNodeHandle> RetValMap;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000121
122 // Loop over the rest of the module, merging graphs for non-external functions
123 // into this graph.
124 //
Chris Lattner0f777ab2003-02-10 00:14:57 +0000125 unsigned Count = 0;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000126 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
127 if (!I->isExternal()) {
Chris Lattner41c04f72003-02-01 04:52:08 +0000128 hash_map<Value*, DSNodeHandle> ValMap;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000129 { // Scope to free NodeMap memory ASAP
Chris Lattner41c04f72003-02-01 04:52:08 +0000130 hash_map<const DSNode*, DSNodeHandle> NodeMap;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000131 const DSGraph &FDSG = LDS.getDSGraph(*I);
132 DSNodeHandle RetNode = ResultGraph->cloneInto(FDSG, ValMap, NodeMap);
133
134 // Keep track of the return node of the function's graph if it returns a
135 // value...
136 //
137 if (RetNode.getNode())
138 RetValMap[I] = RetNode;
139 }
140
Chris Lattnerc875f022002-11-03 21:27:48 +0000141 // Incorporate the inlined Function's ScalarMap into the global
142 // ScalarMap...
Chris Lattner41c04f72003-02-01 04:52:08 +0000143 hash_map<Value*, DSNodeHandle> &GVM = ResultGraph->getScalarMap();
Chris Lattnere6c0b5d2003-02-04 00:03:37 +0000144 for (hash_map<Value*, DSNodeHandle>::iterator I = ValMap.begin(),
145 E = ValMap.end(); I != E; ++I)
146 GVM[I->first].mergeWith(I->second);
Chris Lattner0f777ab2003-02-10 00:14:57 +0000147
148 if ((++Count & 1) == 0) // Prune nodes out every other time...
149 ResultGraph->removeTriviallyDeadNodes();
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000150 }
151
152 // FIXME: Must recalculate and use the Incomplete markers!!
153
154 // Now that we have all of the graphs inlined, we can go about eliminating
155 // call nodes...
156 //
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000157 std::vector<DSCallSite> &Calls =
Chris Lattner01fb0c72002-11-08 21:27:37 +0000158 ResultGraph->getAuxFunctionCalls();
159 assert(Calls.empty() && "Aux call list is already in use??");
160
161 // Start with a copy of the original call sites...
162 Calls = ResultGraph->getFunctionCalls();
163
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000164 for (unsigned i = 0; i != Calls.size(); ) {
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000165 DSCallSite &CurCall = Calls[i];
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000166
167 // Loop over the called functions, eliminating as many as possible...
Chris Lattner923fc052003-02-05 21:59:58 +0000168 std::vector<GlobalValue*> CallTargets;
169 if (CurCall.isDirectCall())
170 CallTargets.push_back(CurCall.getCalleeFunc());
171 else
172 CallTargets = CurCall.getCalleeNode()->getGlobals();
173
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000174 for (unsigned c = 0; c != CallTargets.size(); ) {
175 // If we can eliminate this function call, do so!
176 bool Eliminated = false;
177 if (Function *F = dyn_cast<Function>(CallTargets[c]))
178 if (!F->isExternal()) {
179 ResolveFunctionCall(F, CurCall, RetValMap[F]);
180 Eliminated = true;
181 }
Chris Lattner0f777ab2003-02-10 00:14:57 +0000182 if (Eliminated) {
183 CallTargets[c] = CallTargets.back();
184 CallTargets.pop_back();
185 } else
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000186 ++c; // Cannot eliminate this call, skip over it...
187 }
188
Chris Lattner0f777ab2003-02-10 00:14:57 +0000189 if (CallTargets.empty()) { // Eliminated all calls?
190 CurCall = Calls.back(); // Remove entry
191 Calls.pop_back();
192 } else
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000193 ++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 Lattnere6c0b5d2003-02-04 00:03:37 +0000202 // FIXME: We should be able to disable the globals graph for steens!
Chris Lattner394471f2003-01-23 22:05:33 +0000203 ResultGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000204
Chris Lattner47a307a2003-02-09 18:42:16 +0000205 DEBUG(print(std::cerr, &M));
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000206 return false;
207}
208
209// alias - This is the only method here that does anything interesting...
Chris Lattnerd747e8f2002-11-06 18:08:32 +0000210AliasAnalysis::Result Steens::alias(const Value *V1, const Value *V2) {
Misha Brukmanf4445df2002-12-12 05:34:10 +0000211 assert(ResultGraph && "Result graph has not been computed yet!");
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000212
Chris Lattner0a91e632003-02-03 22:51:53 +0000213 hash_map<Value*, DSNodeHandle> &GSM = ResultGraph->getScalarMap();
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000214
Chris Lattner0a91e632003-02-03 22:51:53 +0000215 hash_map<Value*, DSNodeHandle>::iterator I = GSM.find(const_cast<Value*>(V1));
216 if (I != GSM.end() && I->second.getNode()) {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000217 DSNodeHandle &V1H = I->second;
Chris Lattner0a91e632003-02-03 22:51:53 +0000218 hash_map<Value*, DSNodeHandle>::iterator J=GSM.find(const_cast<Value*>(V2));
219 if (J != GSM.end() && J->second.getNode()) {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000220 DSNodeHandle &V2H = J->second;
221 // If the two pointers point to different data structure graph nodes, they
222 // cannot alias!
Chris Lattner6022ef42003-02-08 23:03:17 +0000223 if (V1H.getNode() != V2H.getNode()) // FIXME: Handle incompleteness!
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000224 return NoAlias;
Chris Lattner6022ef42003-02-08 23:03:17 +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
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000234 // If we cannot determine alias properties based on our graph, fall back on
235 // some other AA implementation.
236 //
237 return getAnalysis<AliasAnalysis>().alias(V1, V2);
238}