blob: 6ea2449483780a6a2be0cc100eddab4795f3d232 [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"
Misha Brukmanf4445df2002-12-12 05:34:10 +000014
15namespace {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000016 class Steens : public Pass, public AliasAnalysis {
17 DSGraph *ResultGraph;
Chris Lattnere6c0b5d2003-02-04 00:03:37 +000018 DSGraph *GlobalsGraph; // FIXME: Eliminate globals graph stuff from DNE
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000019 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",
Chris Lattner6022ef42003-02-08 23:03:17 +000072 "Steensgaard's alias analysis (DSGraph based)");
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000073
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///
Chris Lattnere6c0b5d2003-02-04 00:03:37 +000083void Steens::ResolveFunctionCall(Function *F, const DSCallSite &Call,
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000084 DSNodeHandle &RetVal) {
85 assert(ResultGraph != 0 && "Result graph not allocated!");
Chris Lattner41c04f72003-02-01 04:52:08 +000086 hash_map<Value*, DSNodeHandle> &ValMap = ResultGraph->getScalarMap();
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000087
Vikram S. Adve42fd1692002-10-20 18:07:37 +000088 // Handle the return value of the function...
Chris Lattner0969c502002-10-21 02:08:03 +000089 if (Call.getRetVal().getNode() && RetVal.getNode())
90 RetVal.mergeWith(Call.getRetVal());
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000091
92 // Loop over all pointer arguments, resolving them to their provided pointers
Vikram S. Adve26b98262002-10-20 21:41:02 +000093 unsigned PtrArgIdx = 0;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000094 for (Function::aiterator AI = F->abegin(), AE = F->aend(); AI != AE; ++AI) {
Chris Lattner41c04f72003-02-01 04:52:08 +000095 hash_map<Value*, DSNodeHandle>::iterator I = ValMap.find(AI);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000096 if (I != ValMap.end()) // If its a pointer argument...
Chris Lattnere6c0b5d2003-02-04 00:03:37 +000097 I->second.mergeWith(Call.getPtrArg(PtrArgIdx++));
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000098 }
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000099}
100
101
102/// run - Build up the result graph, representing the pointer graph for the
103/// program.
104///
105bool Steens::run(Module &M) {
106 assert(ResultGraph == 0 && "Result graph already allocated!");
107 LocalDataStructures &LDS = getAnalysis<LocalDataStructures>();
108
109 // Create a new, empty, graph...
110 ResultGraph = new DSGraph();
Chris Lattnere6c0b5d2003-02-04 00:03:37 +0000111 GlobalsGraph = new DSGraph();
112 ResultGraph->setGlobalsGraph(GlobalsGraph);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000113 // RetValMap - Keep track of the return values for all functions that return
114 // valid pointers.
115 //
Chris Lattner41c04f72003-02-01 04:52:08 +0000116 hash_map<Function*, DSNodeHandle> RetValMap;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000117
118 // Loop over the rest of the module, merging graphs for non-external functions
119 // into this graph.
120 //
121 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
122 if (!I->isExternal()) {
Chris Lattner41c04f72003-02-01 04:52:08 +0000123 hash_map<Value*, DSNodeHandle> ValMap;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000124 { // Scope to free NodeMap memory ASAP
Chris Lattner41c04f72003-02-01 04:52:08 +0000125 hash_map<const DSNode*, DSNodeHandle> NodeMap;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000126 const DSGraph &FDSG = LDS.getDSGraph(*I);
127 DSNodeHandle RetNode = ResultGraph->cloneInto(FDSG, ValMap, NodeMap);
128
129 // Keep track of the return node of the function's graph if it returns a
130 // value...
131 //
132 if (RetNode.getNode())
133 RetValMap[I] = RetNode;
134 }
135
Chris Lattnerc875f022002-11-03 21:27:48 +0000136 // Incorporate the inlined Function's ScalarMap into the global
137 // ScalarMap...
Chris Lattner41c04f72003-02-01 04:52:08 +0000138 hash_map<Value*, DSNodeHandle> &GVM = ResultGraph->getScalarMap();
Chris Lattnere6c0b5d2003-02-04 00:03:37 +0000139 for (hash_map<Value*, DSNodeHandle>::iterator I = ValMap.begin(),
140 E = ValMap.end(); I != E; ++I)
141 GVM[I->first].mergeWith(I->second);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000142 }
143
144 // FIXME: Must recalculate and use the Incomplete markers!!
145
146 // Now that we have all of the graphs inlined, we can go about eliminating
147 // call nodes...
148 //
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000149 std::vector<DSCallSite> &Calls =
Chris Lattner01fb0c72002-11-08 21:27:37 +0000150 ResultGraph->getAuxFunctionCalls();
151 assert(Calls.empty() && "Aux call list is already in use??");
152
153 // Start with a copy of the original call sites...
154 Calls = ResultGraph->getFunctionCalls();
155
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000156 for (unsigned i = 0; i != Calls.size(); ) {
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000157 DSCallSite &CurCall = Calls[i];
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000158
159 // Loop over the called functions, eliminating as many as possible...
Chris Lattner923fc052003-02-05 21:59:58 +0000160 std::vector<GlobalValue*> CallTargets;
161 if (CurCall.isDirectCall())
162 CallTargets.push_back(CurCall.getCalleeFunc());
163 else
164 CallTargets = CurCall.getCalleeNode()->getGlobals();
165
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();
Chris Lattner394471f2003-01-23 22:05:33 +0000189 ResultGraph->markIncompleteNodes(DSGraph::IgnoreFormalArgs);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000190
191 // Remove any nodes that are dead after all of the merging we have done...
Chris Lattnere6c0b5d2003-02-04 00:03:37 +0000192 // FIXME: We should be able to disable the globals graph for steens!
Chris Lattner394471f2003-01-23 22:05:33 +0000193 ResultGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000194
Chris Lattner6022ef42003-02-08 23:03:17 +0000195 //print(std::cerr, &M);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000196 return false;
197}
198
199// alias - This is the only method here that does anything interesting...
Chris Lattnerd747e8f2002-11-06 18:08:32 +0000200AliasAnalysis::Result Steens::alias(const Value *V1, const Value *V2) {
Misha Brukmanf4445df2002-12-12 05:34:10 +0000201 assert(ResultGraph && "Result graph has not been computed yet!");
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000202
Chris Lattner0a91e632003-02-03 22:51:53 +0000203 hash_map<Value*, DSNodeHandle> &GSM = ResultGraph->getScalarMap();
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000204
Chris Lattner0a91e632003-02-03 22:51:53 +0000205 hash_map<Value*, DSNodeHandle>::iterator I = GSM.find(const_cast<Value*>(V1));
206 if (I != GSM.end() && I->second.getNode()) {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000207 DSNodeHandle &V1H = I->second;
Chris Lattner0a91e632003-02-03 22:51:53 +0000208 hash_map<Value*, DSNodeHandle>::iterator J=GSM.find(const_cast<Value*>(V2));
209 if (J != GSM.end() && J->second.getNode()) {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000210 DSNodeHandle &V2H = J->second;
211 // If the two pointers point to different data structure graph nodes, they
212 // cannot alias!
Chris Lattner6022ef42003-02-08 23:03:17 +0000213 if (V1H.getNode() != V2H.getNode()) // FIXME: Handle incompleteness!
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000214 return NoAlias;
Chris Lattner6022ef42003-02-08 23:03:17 +0000215
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000216 // FIXME: If the two pointers point to the same node, and the offsets are
217 // different, and the LinkIndex vector doesn't alias the section, then the
218 // two pointers do not alias. We need access size information for the two
219 // accesses though!
220 //
221 }
222 }
223
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000224 // If we cannot determine alias properties based on our graph, fall back on
225 // some other AA implementation.
226 //
227 return getAnalysis<AliasAnalysis>().alias(V1, V2);
228}