1 //===- Steensgaard.cpp - Context Insensitive Alias Analysis ---------------===//
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.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Analysis/DataStructure.h"
11 #include "llvm/Analysis/DSGraph.h"
12 #include "llvm/Analysis/AliasAnalysis.h"
13 #include "llvm/Module.h"
14 #include "Support/Statistic.h"
17 class Steens : public Pass, public AliasAnalysis {
20 Steens() : ResultGraph(0) {}
21 ~Steens() { assert(ResultGraph == 0 && "releaseMemory not called?"); }
23 //------------------------------------------------
24 // Implement the Pass API
27 // run - Build up the result graph, representing the pointer graph for the
32 virtual void releaseMemory() { delete ResultGraph; ResultGraph = 0; }
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...
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");
46 //------------------------------------------------
47 // Implement the AliasAnalysis API
50 // alias - This is the only method here that does anything interesting...
51 Result alias(const Value *V1, const Value *V2);
53 /// canCallModify - Not implemented yet: FIXME
55 Result canCallModify(const CallInst &CI, const Value *Ptr) {
59 /// canInvokeModify - Not implemented yet: FIXME
61 Result canInvokeModify(const InvokeInst &I, const Value *Ptr) {
66 void ResolveFunctionCall(Function *F, const DSCallSite &Call,
67 DSNodeHandle &RetVal);
70 // Register the pass...
71 RegisterOpt<Steens> X("steens-aa",
72 "Steensgaard's FlowInsensitive/ConIns alias analysis");
74 // Register as an implementation of AliasAnalysis
75 RegisterAnalysisGroup<AliasAnalysis, Steens> Y;
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.
83 void Steens::ResolveFunctionCall(Function *F,
84 const DSCallSite &Call,
85 DSNodeHandle &RetVal) {
86 assert(ResultGraph != 0 && "Result graph not allocated!");
87 std::map<Value*, DSNodeHandle> &ValMap = ResultGraph->getScalarMap();
89 // Handle the return value of the function...
90 if (Call.getRetVal().getNode() && RetVal.getNode())
91 RetVal.mergeWith(Call.getRetVal());
93 // Loop over all pointer arguments, resolving them to their provided pointers
94 unsigned PtrArgIdx = 0;
95 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...
98 I->second.addEdgeTo(Call.getPtrArg(PtrArgIdx++));
101 assert(PtrArgIdx == Call.getNumPtrArgs() && "Argument resolution mismatch!");
105 /// run - Build up the result graph, representing the pointer graph for the
108 bool Steens::run(Module &M) {
109 assert(ResultGraph == 0 && "Result graph already allocated!");
110 LocalDataStructures &LDS = getAnalysis<LocalDataStructures>();
112 // Create a new, empty, graph...
113 ResultGraph = new DSGraph();
115 // RetValMap - Keep track of the return values for all functions that return
118 std::map<Function*, DSNodeHandle> RetValMap;
120 // Loop over the rest of the module, merging graphs for non-external functions
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*, DSNodeHandle> NodeMap;
128 const DSGraph &FDSG = LDS.getDSGraph(*I);
129 DSNodeHandle RetNode = ResultGraph->cloneInto(FDSG, ValMap, NodeMap);
131 // Keep track of the return node of the function's graph if it returns a
134 if (RetNode.getNode())
135 RetValMap[I] = RetNode;
138 // Incorporate the inlined Function's ScalarMap into the global
140 std::map<Value*, DSNodeHandle> &GVM = ResultGraph->getScalarMap();
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);
146 GVM[DSN.first] = DSN.second;
148 I->second.mergeWith(DSN.second);
149 ValMap.erase(ValMap.begin());
153 // FIXME: Must recalculate and use the Incomplete markers!!
155 // Now that we have all of the graphs inlined, we can go about eliminating
158 std::vector<DSCallSite> &Calls =
159 ResultGraph->getAuxFunctionCalls();
160 assert(Calls.empty() && "Aux call list is already in use??");
162 // Start with a copy of the original call sites...
163 Calls = ResultGraph->getFunctionCalls();
165 for (unsigned i = 0; i != Calls.size(); ) {
166 DSCallSite &CurCall = Calls[i];
168 // Loop over the called functions, eliminating as many as possible...
169 std::vector<GlobalValue*> CallTargets =
170 CurCall.getCallee().getNode()->getGlobals();
171 for (unsigned c = 0; c != CallTargets.size(); ) {
172 // If we can eliminate this function call, do so!
173 bool Eliminated = false;
174 if (Function *F = dyn_cast<Function>(CallTargets[c]))
175 if (!F->isExternal()) {
176 ResolveFunctionCall(F, CurCall, RetValMap[F]);
180 CallTargets.erase(CallTargets.begin()+c);
182 ++c; // Cannot eliminate this call, skip over it...
185 if (CallTargets.empty()) // Eliminated all calls?
186 Calls.erase(Calls.begin()+i); // Remove from call list...
188 ++i; // Skip this call site...
191 // Update the "incomplete" markers on the nodes, ignoring unknownness due to
192 // incoming arguments...
193 ResultGraph->maskIncompleteMarkers();
194 ResultGraph->markIncompleteNodes(false);
196 // Remove any nodes that are dead after all of the merging we have done...
197 ResultGraph->removeTriviallyDeadNodes();
199 DEBUG(print(std::cerr, &M));
203 // alias - This is the only method here that does anything interesting...
204 AliasAnalysis::Result Steens::alias(const Value *V1, const Value *V2) {
205 assert(ResultGraph && "Result grcaph has not yet been computed!");
207 std::map<Value*, DSNodeHandle> &GVM = ResultGraph->getScalarMap();
209 std::map<Value*, DSNodeHandle>::iterator I = GVM.find(const_cast<Value*>(V1));
210 if (I != GVM.end() && I->second.getNode()) {
211 DSNodeHandle &V1H = I->second;
212 std::map<Value*, DSNodeHandle>::iterator J=GVM.find(const_cast<Value*>(V2));
213 if (J != GVM.end() && J->second.getNode()) {
214 DSNodeHandle &V2H = J->second;
215 // If the two pointers point to different data structure graph nodes, they
217 if (V1H.getNode() != V2H.getNode())
220 // FIXME: If the two pointers point to the same node, and the offsets are
221 // different, and the LinkIndex vector doesn't alias the section, then the
222 // two pointers do not alias. We need access size information for the two
228 // If we cannot determine alias properties based on our graph, fall back on
229 // some other AA implementation.
231 return getAnalysis<AliasAnalysis>().alias(V1, V2);