blob: 94b63d77a6158d85427d53d6194f9d7a1121b390 [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) {}
Chris Lattnerbe1094e2003-02-13 21:44:18 +000022 ~Steens() {
23 releaseMyMemory();
24 assert(ResultGraph == 0 && "releaseMemory not called?");
25 }
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000026
27 //------------------------------------------------
28 // Implement the Pass API
29 //
30
31 // run - Build up the result graph, representing the pointer graph for the
32 // program.
33 //
34 bool run(Module &M);
35
Chris Lattnerbe1094e2003-02-13 21:44:18 +000036 virtual void releaseMyMemory() { delete ResultGraph; ResultGraph = 0; }
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000037
38 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
39 AU.setPreservesAll(); // Does not transform code...
40 AU.addRequired<LocalDataStructures>(); // Uses local dsgraph
41 AU.addRequired<AliasAnalysis>(); // Chains to another AA impl...
42 }
43
44 // print - Implement the Pass::print method...
45 void print(std::ostream &O, const Module *M) const {
46 assert(ResultGraph && "Result graph has not yet been computed!");
47 ResultGraph->writeGraphToFile(O, "steensgaards");
48 }
49
50 //------------------------------------------------
51 // Implement the AliasAnalysis API
52 //
53
54 // alias - This is the only method here that does anything interesting...
Chris Lattnerd747e8f2002-11-06 18:08:32 +000055 Result alias(const Value *V1, const Value *V2);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000056
Chris Lattner4f53bef2002-11-01 17:34:23 +000057 /// canCallModify - Not implemented yet: FIXME
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000058 ///
Chris Lattnerd747e8f2002-11-06 18:08:32 +000059 Result canCallModify(const CallInst &CI, const Value *Ptr) {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000060 return MayAlias;
61 }
62
Chris Lattner4f53bef2002-11-01 17:34:23 +000063 /// canInvokeModify - Not implemented yet: FIXME
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000064 ///
Chris Lattnerd747e8f2002-11-06 18:08:32 +000065 Result canInvokeModify(const InvokeInst &I, const Value *Ptr) {
Chris Lattner4f53bef2002-11-01 17:34:23 +000066 return MayAlias;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000067 }
68
69 private:
Vikram S. Adve42fd1692002-10-20 18:07:37 +000070 void ResolveFunctionCall(Function *F, const DSCallSite &Call,
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000071 DSNodeHandle &RetVal);
72 };
73
74 // Register the pass...
75 RegisterOpt<Steens> X("steens-aa",
Chris Lattner6022ef42003-02-08 23:03:17 +000076 "Steensgaard's alias analysis (DSGraph based)");
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000077
78 // Register as an implementation of AliasAnalysis
79 RegisterAnalysisGroup<AliasAnalysis, Steens> Y;
80}
81
82
83/// ResolveFunctionCall - Resolve the actual arguments of a call to function F
84/// with the specified call site descriptor. This function links the arguments
85/// and the return value for the call site context-insensitively.
86///
Chris Lattnere6c0b5d2003-02-04 00:03:37 +000087void Steens::ResolveFunctionCall(Function *F, const DSCallSite &Call,
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000088 DSNodeHandle &RetVal) {
89 assert(ResultGraph != 0 && "Result graph not allocated!");
Chris Lattner41c04f72003-02-01 04:52:08 +000090 hash_map<Value*, DSNodeHandle> &ValMap = ResultGraph->getScalarMap();
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000091
Vikram S. Adve42fd1692002-10-20 18:07:37 +000092 // Handle the return value of the function...
Chris Lattner0969c502002-10-21 02:08:03 +000093 if (Call.getRetVal().getNode() && RetVal.getNode())
94 RetVal.mergeWith(Call.getRetVal());
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000095
96 // Loop over all pointer arguments, resolving them to their provided pointers
Vikram S. Adve26b98262002-10-20 21:41:02 +000097 unsigned PtrArgIdx = 0;
Chris Lattner88c72942003-02-10 18:16:19 +000098 for (Function::aiterator AI = F->abegin(), AE = F->aend();
99 AI != AE && PtrArgIdx < Call.getNumPtrArgs(); ++AI) {
Chris Lattner41c04f72003-02-01 04:52:08 +0000100 hash_map<Value*, DSNodeHandle>::iterator I = ValMap.find(AI);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000101 if (I != ValMap.end()) // If its a pointer argument...
Chris Lattnere6c0b5d2003-02-04 00:03:37 +0000102 I->second.mergeWith(Call.getPtrArg(PtrArgIdx++));
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000103 }
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000104}
105
106
107/// run - Build up the result graph, representing the pointer graph for the
108/// program.
109///
110bool Steens::run(Module &M) {
111 assert(ResultGraph == 0 && "Result graph already allocated!");
112 LocalDataStructures &LDS = getAnalysis<LocalDataStructures>();
113
114 // Create a new, empty, graph...
115 ResultGraph = new DSGraph();
Chris Lattnere6c0b5d2003-02-04 00:03:37 +0000116 GlobalsGraph = new DSGraph();
117 ResultGraph->setGlobalsGraph(GlobalsGraph);
Chris Lattneraf283512003-02-09 19:26:47 +0000118 ResultGraph->setPrintAuxCalls();
119
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000120 // 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 //
Chris Lattner0f777ab2003-02-10 00:14:57 +0000128 unsigned Count = 0;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000129 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
130 if (!I->isExternal()) {
Chris Lattner41c04f72003-02-01 04:52:08 +0000131 hash_map<Value*, DSNodeHandle> ValMap;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000132 { // Scope to free NodeMap memory ASAP
Chris Lattner41c04f72003-02-01 04:52:08 +0000133 hash_map<const DSNode*, DSNodeHandle> NodeMap;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000134 const DSGraph &FDSG = LDS.getDSGraph(*I);
135 DSNodeHandle RetNode = ResultGraph->cloneInto(FDSG, ValMap, NodeMap);
136
137 // Keep track of the return node of the function's graph if it returns a
138 // value...
139 //
140 if (RetNode.getNode())
141 RetValMap[I] = RetNode;
142 }
143
Chris Lattnerc875f022002-11-03 21:27:48 +0000144 // Incorporate the inlined Function's ScalarMap into the global
145 // ScalarMap...
Chris Lattner41c04f72003-02-01 04:52:08 +0000146 hash_map<Value*, DSNodeHandle> &GVM = ResultGraph->getScalarMap();
Chris Lattnere6c0b5d2003-02-04 00:03:37 +0000147 for (hash_map<Value*, DSNodeHandle>::iterator I = ValMap.begin(),
148 E = ValMap.end(); I != E; ++I)
149 GVM[I->first].mergeWith(I->second);
Chris Lattner0f777ab2003-02-10 00:14:57 +0000150
151 if ((++Count & 1) == 0) // Prune nodes out every other time...
152 ResultGraph->removeTriviallyDeadNodes();
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000153 }
154
155 // FIXME: Must recalculate and use the Incomplete markers!!
156
157 // Now that we have all of the graphs inlined, we can go about eliminating
158 // call nodes...
159 //
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000160 std::vector<DSCallSite> &Calls =
Chris Lattner01fb0c72002-11-08 21:27:37 +0000161 ResultGraph->getAuxFunctionCalls();
162 assert(Calls.empty() && "Aux call list is already in use??");
163
164 // Start with a copy of the original call sites...
165 Calls = ResultGraph->getFunctionCalls();
166
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000167 for (unsigned i = 0; i != Calls.size(); ) {
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000168 DSCallSite &CurCall = Calls[i];
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000169
170 // Loop over the called functions, eliminating as many as possible...
Chris Lattner923fc052003-02-05 21:59:58 +0000171 std::vector<GlobalValue*> CallTargets;
172 if (CurCall.isDirectCall())
173 CallTargets.push_back(CurCall.getCalleeFunc());
174 else
175 CallTargets = CurCall.getCalleeNode()->getGlobals();
176
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000177 for (unsigned c = 0; c != CallTargets.size(); ) {
178 // If we can eliminate this function call, do so!
179 bool Eliminated = false;
180 if (Function *F = dyn_cast<Function>(CallTargets[c]))
181 if (!F->isExternal()) {
182 ResolveFunctionCall(F, CurCall, RetValMap[F]);
183 Eliminated = true;
184 }
Chris Lattner0f777ab2003-02-10 00:14:57 +0000185 if (Eliminated) {
186 CallTargets[c] = CallTargets.back();
187 CallTargets.pop_back();
188 } else
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000189 ++c; // Cannot eliminate this call, skip over it...
190 }
191
Chris Lattner0f777ab2003-02-10 00:14:57 +0000192 if (CallTargets.empty()) { // Eliminated all calls?
193 CurCall = Calls.back(); // Remove entry
194 Calls.pop_back();
195 } else
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000196 ++i; // Skip this call site...
197 }
198
Chris Lattner72d29a42003-02-11 23:11:51 +0000199 RetValMap.clear();
200
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000201 // Update the "incomplete" markers on the nodes, ignoring unknownness due to
202 // incoming arguments...
203 ResultGraph->maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000204 ResultGraph->markIncompleteNodes(DSGraph::IgnoreFormalArgs);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000205
206 // Remove any nodes that are dead after all of the merging we have done...
Chris Lattnere6c0b5d2003-02-04 00:03:37 +0000207 // FIXME: We should be able to disable the globals graph for steens!
Chris Lattner394471f2003-01-23 22:05:33 +0000208 ResultGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000209
Chris Lattner47a307a2003-02-09 18:42:16 +0000210 DEBUG(print(std::cerr, &M));
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000211 return false;
212}
213
214// alias - This is the only method here that does anything interesting...
Chris Lattnerd747e8f2002-11-06 18:08:32 +0000215AliasAnalysis::Result Steens::alias(const Value *V1, const Value *V2) {
Misha Brukmanf4445df2002-12-12 05:34:10 +0000216 assert(ResultGraph && "Result graph has not been computed yet!");
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000217
Chris Lattner0a91e632003-02-03 22:51:53 +0000218 hash_map<Value*, DSNodeHandle> &GSM = ResultGraph->getScalarMap();
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000219
Chris Lattner0a91e632003-02-03 22:51:53 +0000220 hash_map<Value*, DSNodeHandle>::iterator I = GSM.find(const_cast<Value*>(V1));
221 if (I != GSM.end() && I->second.getNode()) {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000222 DSNodeHandle &V1H = I->second;
Chris Lattner0a91e632003-02-03 22:51:53 +0000223 hash_map<Value*, DSNodeHandle>::iterator J=GSM.find(const_cast<Value*>(V2));
224 if (J != GSM.end() && J->second.getNode()) {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000225 DSNodeHandle &V2H = J->second;
226 // If the two pointers point to different data structure graph nodes, they
227 // cannot alias!
Chris Lattner6022ef42003-02-08 23:03:17 +0000228 if (V1H.getNode() != V2H.getNode()) // FIXME: Handle incompleteness!
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000229 return NoAlias;
Chris Lattner6022ef42003-02-08 23:03:17 +0000230
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000231 // FIXME: If the two pointers point to the same node, and the offsets are
232 // different, and the LinkIndex vector doesn't alias the section, then the
233 // two pointers do not alias. We need access size information for the two
234 // accesses though!
235 //
236 }
237 }
238
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000239 // If we cannot determine alias properties based on our graph, fall back on
240 // some other AA implementation.
241 //
242 return getAnalysis<AliasAnalysis>().alias(V1, V2);
243}