1 //===- Steensgaard.cpp - Context Insensitive Alias Analysis ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass uses the data structure graphs to implement a simple context
11 // insensitive alias analysis. It does this by computing the local analysis
12 // graphs for all of the functions, then merging them together into a single big
13 // graph without cloning.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Analysis/DataStructure.h"
18 #include "llvm/Analysis/DSGraph.h"
19 #include "llvm/Analysis/AliasAnalysis.h"
20 #include "llvm/Module.h"
21 #include "Support/Debug.h"
25 class Steens : public Pass, public AliasAnalysis {
27 DSGraph *GlobalsGraph; // FIXME: Eliminate globals graph stuff from DNE
29 Steens() : ResultGraph(0), GlobalsGraph(0) {}
32 assert(ResultGraph == 0 && "releaseMemory not called?");
35 //------------------------------------------------
36 // Implement the Pass API
39 // run - Build up the result graph, representing the pointer graph for the
44 virtual void releaseMyMemory() { delete ResultGraph; ResultGraph = 0; }
46 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
47 AliasAnalysis::getAnalysisUsage(AU);
48 AU.setPreservesAll(); // Does not transform code...
49 AU.addRequired<LocalDataStructures>(); // Uses local dsgraph
50 AU.addRequired<AliasAnalysis>(); // Chains to another AA impl...
53 // print - Implement the Pass::print method...
54 void print(std::ostream &O, const Module *M) const {
55 assert(ResultGraph && "Result graph has not yet been computed!");
56 ResultGraph->writeGraphToFile(O, "steensgaards");
59 //------------------------------------------------
60 // Implement the AliasAnalysis API
63 // alias - This is the only method here that does anything interesting...
64 AliasResult alias(const Value *V1, unsigned V1Size,
65 const Value *V2, unsigned V2Size);
68 void ResolveFunctionCall(Function *F, const DSCallSite &Call,
69 DSNodeHandle &RetVal);
72 // Register the pass...
73 RegisterOpt<Steens> X("steens-aa",
74 "Steensgaard's alias analysis (DSGraph based)");
76 // Register as an implementation of AliasAnalysis
77 RegisterAnalysisGroup<AliasAnalysis, Steens> Y;
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.
84 void Steens::ResolveFunctionCall(Function *F, const DSCallSite &Call,
85 DSNodeHandle &RetVal) {
86 assert(ResultGraph != 0 && "Result graph not allocated!");
87 DSGraph::ScalarMapTy &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();
96 AI != AE && PtrArgIdx < Call.getNumPtrArgs(); ++AI) {
97 DSGraph::ScalarMapTy::iterator I = ValMap.find(AI);
98 if (I != ValMap.end()) // If its a pointer argument...
99 I->second.mergeWith(Call.getPtrArg(PtrArgIdx++));
104 /// run - Build up the result graph, representing the pointer graph for the
107 bool Steens::run(Module &M) {
108 InitializeAliasAnalysis(this);
109 assert(ResultGraph == 0 && "Result graph already allocated!");
110 LocalDataStructures &LDS = getAnalysis<LocalDataStructures>();
112 // Create a new, empty, graph...
113 ResultGraph = new DSGraph(getTargetData());
114 GlobalsGraph = new DSGraph(getTargetData());
115 ResultGraph->setGlobalsGraph(GlobalsGraph);
116 ResultGraph->setPrintAuxCalls();
118 // RetValMap - Keep track of the return values for all functions that return
121 DSGraph::ReturnNodesTy RetValMap;
123 // Loop over the rest of the module, merging graphs for non-external functions
127 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
128 if (!I->isExternal()) {
129 DSGraph::ScalarMapTy ValMap;
130 { // Scope to free NodeMap memory ASAP
131 DSGraph::NodeMapTy NodeMap;
132 const DSGraph &FDSG = LDS.getDSGraph(*I);
133 ResultGraph->cloneInto(FDSG, ValMap, RetValMap, NodeMap,
134 DSGraph::UpdateInlinedGlobals);
137 // Incorporate the inlined Function's ScalarMap into the global
139 DSGraph::ScalarMapTy &GVM = ResultGraph->getScalarMap();
140 for (DSGraph::ScalarMapTy::iterator I = ValMap.begin(),
141 E = ValMap.end(); I != E; ++I)
142 GVM[I->first].mergeWith(I->second);
144 if ((++Count & 1) == 0) // Prune nodes out every other time...
145 ResultGraph->removeTriviallyDeadNodes();
148 // FIXME: Must recalculate and use the Incomplete markers!!
150 // Now that we have all of the graphs inlined, we can go about eliminating
153 std::vector<DSCallSite> &Calls =
154 ResultGraph->getAuxFunctionCalls();
155 assert(Calls.empty() && "Aux call list is already in use??");
157 // Start with a copy of the original call sites...
158 Calls = ResultGraph->getFunctionCalls();
160 for (unsigned i = 0; i != Calls.size(); ) {
161 DSCallSite &CurCall = Calls[i];
163 // Loop over the called functions, eliminating as many as possible...
164 std::vector<GlobalValue*> CallTargets;
165 if (CurCall.isDirectCall())
166 CallTargets.push_back(CurCall.getCalleeFunc());
168 CallTargets = CurCall.getCalleeNode()->getGlobals();
170 for (unsigned c = 0; c != CallTargets.size(); ) {
171 // If we can eliminate this function call, do so!
172 bool Eliminated = false;
173 if (Function *F = dyn_cast<Function>(CallTargets[c]))
174 if (!F->isExternal()) {
175 ResolveFunctionCall(F, CurCall, RetValMap[F]);
179 CallTargets[c] = CallTargets.back();
180 CallTargets.pop_back();
182 ++c; // Cannot eliminate this call, skip over it...
185 if (CallTargets.empty()) { // Eliminated all calls?
186 CurCall = Calls.back(); // Remove entry
189 ++i; // Skip this call site...
194 // Update the "incomplete" markers on the nodes, ignoring unknownness due to
195 // incoming arguments...
196 ResultGraph->maskIncompleteMarkers();
197 ResultGraph->markIncompleteNodes(DSGraph::IgnoreFormalArgs);
199 // Remove any nodes that are dead after all of the merging we have done...
200 // FIXME: We should be able to disable the globals graph for steens!
201 ResultGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
203 DEBUG(print(std::cerr, &M));
207 // alias - This is the only method here that does anything interesting...
208 AliasAnalysis::AliasResult Steens::alias(const Value *V1, unsigned V1Size,
209 const Value *V2, unsigned V2Size) {
210 // FIXME: HANDLE Size argument!
211 assert(ResultGraph && "Result graph has not been computed yet!");
213 DSGraph::ScalarMapTy &GSM = ResultGraph->getScalarMap();
215 DSGraph::ScalarMapTy::iterator I = GSM.find(const_cast<Value*>(V1));
216 if (I != GSM.end() && I->second.getNode()) {
217 DSNodeHandle &V1H = I->second;
218 DSGraph::ScalarMapTy::iterator J=GSM.find(const_cast<Value*>(V2));
219 if (J != GSM.end() && J->second.getNode()) {
220 DSNodeHandle &V2H = J->second;
221 // If the two pointers point to different data structure graph nodes, they
223 if (V1H.getNode() != V2H.getNode()) // FIXME: Handle incompleteness!
226 // 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
234 // If we cannot determine alias properties based on our graph, fall back on
235 // some other AA implementation.
237 return getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size);