blob: 88d845559f925373420c1b0618b2a028a5aa78df [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 {
17 class Steens : public Pass, public AliasAnalysis {
18 DSGraph *ResultGraph;
19 public:
20 Steens() : ResultGraph(0) {}
21 ~Steens() { assert(ResultGraph == 0 && "releaseMemory not called?"); }
22
23 //------------------------------------------------
24 // Implement the Pass API
25 //
26
27 // run - Build up the result graph, representing the pointer graph for the
28 // program.
29 //
30 bool run(Module &M);
31
32 virtual void releaseMemory() { delete ResultGraph; ResultGraph = 0; }
33
34 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
35 AU.setPreservesAll(); // Does not transform code...
36 AU.addRequired<LocalDataStructures>(); // Uses local dsgraph
37 AU.addRequired<AliasAnalysis>(); // Chains to another AA impl...
38 }
39
40 // print - Implement the Pass::print method...
41 void print(std::ostream &O, const Module *M) const {
42 assert(ResultGraph && "Result graph has not yet been computed!");
43 ResultGraph->writeGraphToFile(O, "steensgaards");
44 }
45
46 //------------------------------------------------
47 // Implement the AliasAnalysis API
48 //
49
50 // alias - This is the only method here that does anything interesting...
Chris Lattnerd747e8f2002-11-06 18:08:32 +000051 Result alias(const Value *V1, const Value *V2);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000052
Chris Lattner4f53bef2002-11-01 17:34:23 +000053 /// canCallModify - Not implemented yet: FIXME
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000054 ///
Chris Lattnerd747e8f2002-11-06 18:08:32 +000055 Result canCallModify(const CallInst &CI, const Value *Ptr) {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000056 return MayAlias;
57 }
58
Chris Lattner4f53bef2002-11-01 17:34:23 +000059 /// canInvokeModify - Not implemented yet: FIXME
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000060 ///
Chris Lattnerd747e8f2002-11-06 18:08:32 +000061 Result canInvokeModify(const InvokeInst &I, const Value *Ptr) {
Chris Lattner4f53bef2002-11-01 17:34:23 +000062 return MayAlias;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000063 }
64
65 private:
Vikram S. Adve42fd1692002-10-20 18:07:37 +000066 void ResolveFunctionCall(Function *F, const DSCallSite &Call,
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000067 DSNodeHandle &RetVal);
68 };
69
70 // Register the pass...
71 RegisterOpt<Steens> X("steens-aa",
72 "Steensgaard's FlowInsensitive/ConIns alias analysis");
73
74 // Register as an implementation of AliasAnalysis
75 RegisterAnalysisGroup<AliasAnalysis, Steens> Y;
76}
77
78
79/// ResolveFunctionCall - Resolve the actual arguments of a call to function F
80/// with the specified call site descriptor. This function links the arguments
81/// and the return value for the call site context-insensitively.
82///
83void Steens::ResolveFunctionCall(Function *F,
Vikram S. Adve42fd1692002-10-20 18:07:37 +000084 const DSCallSite &Call,
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000085 DSNodeHandle &RetVal) {
86 assert(ResultGraph != 0 && "Result graph not allocated!");
Chris Lattnerc875f022002-11-03 21:27:48 +000087 std::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 Lattner1c7ce2c2002-10-01 22:34:12 +000095 for (Function::aiterator AI = F->abegin(), AE = F->aend(); AI != AE; ++AI) {
96 std::map<Value*, DSNodeHandle>::iterator I = ValMap.find(AI);
97 if (I != ValMap.end()) // If its a pointer argument...
Chris Lattner0969c502002-10-21 02:08:03 +000098 I->second.addEdgeTo(Call.getPtrArg(PtrArgIdx++));
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000099 }
100
Vikram S. Adve26b98262002-10-20 21:41:02 +0000101 assert(PtrArgIdx == Call.getNumPtrArgs() && "Argument resolution mismatch!");
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000102}
103
104
105/// run - Build up the result graph, representing the pointer graph for the
106/// program.
107///
108bool Steens::run(Module &M) {
109 assert(ResultGraph == 0 && "Result graph already allocated!");
110 LocalDataStructures &LDS = getAnalysis<LocalDataStructures>();
111
112 // Create a new, empty, graph...
113 ResultGraph = new DSGraph();
114
115 // RetValMap - Keep track of the return values for all functions that return
116 // valid pointers.
117 //
118 std::map<Function*, DSNodeHandle> RetValMap;
119
120 // Loop over the rest of the module, merging graphs for non-external functions
121 // into this graph.
122 //
123 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
124 if (!I->isExternal()) {
125 std::map<Value*, DSNodeHandle> ValMap;
126 { // Scope to free NodeMap memory ASAP
127 std::map<const DSNode*, DSNode*> NodeMap;
128 const DSGraph &FDSG = LDS.getDSGraph(*I);
129 DSNodeHandle RetNode = ResultGraph->cloneInto(FDSG, ValMap, NodeMap);
130
131 // Keep track of the return node of the function's graph if it returns a
132 // value...
133 //
134 if (RetNode.getNode())
135 RetValMap[I] = RetNode;
136 }
137
Chris Lattnerc875f022002-11-03 21:27:48 +0000138 // Incorporate the inlined Function's ScalarMap into the global
139 // ScalarMap...
140 std::map<Value*, DSNodeHandle> &GVM = ResultGraph->getScalarMap();
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000141
142 while (!ValMap.empty()) { // Loop over value map, moving entries over...
143 const std::pair<Value*, DSNodeHandle> &DSN = *ValMap.begin();
144 std::map<Value*, DSNodeHandle>::iterator I = GVM.find(DSN.first);
145 if (I == GVM.end())
146 GVM[DSN.first] = DSN.second;
147 else
148 I->second.mergeWith(DSN.second);
149 ValMap.erase(ValMap.begin());
150 }
151 }
152
153 // FIXME: Must recalculate and use the Incomplete markers!!
154
155 // Now that we have all of the graphs inlined, we can go about eliminating
156 // call nodes...
157 //
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000158 std::vector<DSCallSite> &Calls =
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000159 ResultGraph->getFunctionCalls();
160 for (unsigned i = 0; i != Calls.size(); ) {
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000161 DSCallSite &CurCall = Calls[i];
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000162
163 // Loop over the called functions, eliminating as many as possible...
Chris Lattner0969c502002-10-21 02:08:03 +0000164 std::vector<GlobalValue*> CallTargets =
165 CurCall.getCallee().getNode()->getGlobals();
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000166 for (unsigned c = 0; c != CallTargets.size(); ) {
167 // If we can eliminate this function call, do so!
168 bool Eliminated = false;
169 if (Function *F = dyn_cast<Function>(CallTargets[c]))
170 if (!F->isExternal()) {
171 ResolveFunctionCall(F, CurCall, RetValMap[F]);
172 Eliminated = true;
173 }
174 if (Eliminated)
175 CallTargets.erase(CallTargets.begin()+c);
176 else
177 ++c; // Cannot eliminate this call, skip over it...
178 }
179
180 if (CallTargets.empty()) // Eliminated all calls?
181 Calls.erase(Calls.begin()+i); // Remove from call list...
182 else
183 ++i; // Skip this call site...
184 }
185
186 // Update the "incomplete" markers on the nodes, ignoring unknownness due to
187 // incoming arguments...
188 ResultGraph->maskIncompleteMarkers();
189 ResultGraph->markIncompleteNodes(false);
190
191 // Remove any nodes that are dead after all of the merging we have done...
192 ResultGraph->removeTriviallyDeadNodes();
193
194 DEBUG(print(std::cerr, &M));
195 return false;
196}
197
198// alias - This is the only method here that does anything interesting...
Chris Lattnerd747e8f2002-11-06 18:08:32 +0000199AliasAnalysis::Result Steens::alias(const Value *V1, const Value *V2) {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000200 assert(ResultGraph && "Result grcaph has not yet been computed!");
201
Chris Lattnerc875f022002-11-03 21:27:48 +0000202 std::map<Value*, DSNodeHandle> &GVM = ResultGraph->getScalarMap();
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000203
204 std::map<Value*, DSNodeHandle>::iterator I = GVM.find(const_cast<Value*>(V1));
205 if (I != GVM.end() && I->second.getNode()) {
206 DSNodeHandle &V1H = I->second;
207 std::map<Value*, DSNodeHandle>::iterator J=GVM.find(const_cast<Value*>(V2));
208 if (J != GVM.end() && J->second.getNode()) {
209 DSNodeHandle &V2H = J->second;
210 // If the two pointers point to different data structure graph nodes, they
211 // cannot alias!
212 if (V1H.getNode() != V2H.getNode())
213 return NoAlias;
214
215 // FIXME: If the two pointers point to the same node, and the offsets are
216 // different, and the LinkIndex vector doesn't alias the section, then the
217 // two pointers do not alias. We need access size information for the two
218 // accesses though!
219 //
220 }
221 }
222
223 // If we cannot determine alias properties based on our graph, fall back on
224 // some other AA implementation.
225 //
226 return getAnalysis<AliasAnalysis>().alias(V1, V2);
227}