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/DataStructure.h"
18 #include "llvm/Analysis/DataStructure/DSGraph.h"
19 #include "llvm/Analysis/AliasAnalysis.h"
20 #include "llvm/Analysis/Passes.h"
21 #include "llvm/Module.h"
22 #include "llvm/Support/Debug.h"
26 class Steens : public ModulePass, public AliasAnalysis {
28 DSGraph *GlobalsGraph; // FIXME: Eliminate globals graph stuff from DNE
30 EquivalenceClasses<GlobalValue*> GlobalECs; // Always empty
32 Steens() : ResultGraph(0), GlobalsGraph(0) {}
35 assert(ResultGraph == 0 && "releaseMemory not called?");
38 //------------------------------------------------
39 // Implement the Pass API
42 // run - Build up the result graph, representing the pointer graph for the
45 bool runOnModule(Module &M);
47 virtual void releaseMyMemory() { delete ResultGraph; ResultGraph = 0; }
49 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
50 AliasAnalysis::getAnalysisUsage(AU);
51 AU.setPreservesAll(); // Does not transform code...
52 AU.addRequired<LocalDataStructures>(); // Uses local dsgraph
55 // print - Implement the Pass::print method...
56 void print(std::ostream &O, const Module *M) const {
57 assert(ResultGraph && "Result graph has not yet been computed!");
58 ResultGraph->writeGraphToFile(O, "steensgaards");
61 //------------------------------------------------
62 // Implement the AliasAnalysis API
65 // alias - This is the only method here that does anything interesting...
66 AliasResult alias(const Value *V1, unsigned V1Size,
67 const Value *V2, unsigned V2Size);
70 void ResolveFunctionCall(Function *F, const DSCallSite &Call,
71 DSNodeHandle &RetVal);
74 // Register the pass...
75 RegisterOpt<Steens> X("steens-aa",
76 "Steensgaard's alias analysis (DSGraph based)");
78 // Register as an implementation of AliasAnalysis
79 RegisterAnalysisGroup<AliasAnalysis, Steens> Y;
82 ModulePass *llvm::createSteensgaardPass() { return new Steens(); }
84 /// ResolveFunctionCall - Resolve the actual arguments of a call to function F
85 /// with the specified call site descriptor. This function links the arguments
86 /// and the return value for the call site context-insensitively.
88 void Steens::ResolveFunctionCall(Function *F, const DSCallSite &Call,
89 DSNodeHandle &RetVal) {
90 assert(ResultGraph != 0 && "Result graph not allocated!");
91 DSGraph::ScalarMapTy &ValMap = ResultGraph->getScalarMap();
93 // Handle the return value of the function...
94 if (Call.getRetVal().getNode() && RetVal.getNode())
95 RetVal.mergeWith(Call.getRetVal());
97 // Loop over all pointer arguments, resolving them to their provided pointers
98 unsigned PtrArgIdx = 0;
99 for (Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
100 AI != AE && PtrArgIdx < Call.getNumPtrArgs(); ++AI) {
101 DSGraph::ScalarMapTy::iterator I = ValMap.find(AI);
102 if (I != ValMap.end()) // If its a pointer argument...
103 I->second.mergeWith(Call.getPtrArg(PtrArgIdx++));
108 /// run - Build up the result graph, representing the pointer graph for the
111 bool Steens::runOnModule(Module &M) {
112 InitializeAliasAnalysis(this);
113 assert(ResultGraph == 0 && "Result graph already allocated!");
114 LocalDataStructures &LDS = getAnalysis<LocalDataStructures>();
116 // Create a new, empty, graph...
117 ResultGraph = new DSGraph(GlobalECs, getTargetData());
118 GlobalsGraph = new DSGraph(GlobalECs, getTargetData());
119 ResultGraph->setGlobalsGraph(GlobalsGraph);
121 // Loop over the rest of the module, merging graphs for non-external functions
124 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
125 if (!I->isExternal())
126 ResultGraph->cloneInto(LDS.getDSGraph(*I));
128 ResultGraph->removeTriviallyDeadNodes();
130 // FIXME: Must recalculate and use the Incomplete markers!!
132 // Now that we have all of the graphs inlined, we can go about eliminating
135 std::list<DSCallSite> &Calls = ResultGraph->getAuxFunctionCalls();
136 assert(Calls.empty() && "Aux call list is already in use??");
138 // Start with a copy of the original call sites.
139 Calls = ResultGraph->getFunctionCalls();
141 for (std::list<DSCallSite>::iterator CI = Calls.begin(), E = Calls.end();
143 DSCallSite &CurCall = *CI++;
145 // Loop over the called functions, eliminating as many as possible...
146 std::vector<Function*> CallTargets;
147 if (CurCall.isDirectCall())
148 CallTargets.push_back(CurCall.getCalleeFunc());
150 CurCall.getCalleeNode()->addFullFunctionList(CallTargets);
152 for (unsigned c = 0; c != CallTargets.size(); ) {
153 // If we can eliminate this function call, do so!
154 Function *F = CallTargets[c];
155 if (!F->isExternal()) {
156 ResolveFunctionCall(F, CurCall, ResultGraph->getReturnNodes()[F]);
157 CallTargets[c] = CallTargets.back();
158 CallTargets.pop_back();
160 ++c; // Cannot eliminate this call, skip over it...
163 if (CallTargets.empty()) { // Eliminated all calls?
164 std::list<DSCallSite>::iterator I = CI;
165 Calls.erase(--I); // Remove entry
169 // Remove our knowledge of what the return values of the functions are.
170 ResultGraph->getReturnNodes().clear();
172 // Update the "incomplete" markers on the nodes, ignoring unknownness due to
173 // incoming arguments...
174 ResultGraph->maskIncompleteMarkers();
175 ResultGraph->markIncompleteNodes(DSGraph::IgnoreFormalArgs |
176 DSGraph::IgnoreGlobals);
178 // Remove any nodes that are dead after all of the merging we have done...
179 // FIXME: We should be able to disable the globals graph for steens!
180 ResultGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
182 DEBUG(print(std::cerr, &M));
186 // alias - This is the only method here that does anything interesting...
187 AliasAnalysis::AliasResult Steens::alias(const Value *V1, unsigned V1Size,
188 const Value *V2, unsigned V2Size) {
189 assert(ResultGraph && "Result graph has not been computed yet!");
191 DSGraph::ScalarMapTy &GSM = ResultGraph->getScalarMap();
193 DSGraph::ScalarMapTy::iterator I = GSM.find(const_cast<Value*>(V1));
194 if (I != GSM.end() && !I->second.isNull() &&
195 I->second.getNode()->isComplete()) {
196 DSNodeHandle &V1H = I->second;
197 DSGraph::ScalarMapTy::iterator J=GSM.find(const_cast<Value*>(V2));
198 if (J != GSM.end() && !J->second.isNull() &&
199 J->second.getNode()->isComplete()) {
200 DSNodeHandle &V2H = J->second;
201 // If the two pointers point to different data structure graph nodes, they
203 if (V1H.getNode() != V2H.getNode())
206 // See if they point to different offsets... if so, we may be able to
207 // determine that they do not alias...
208 unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset();
210 if (O2 < O1) { // Ensure that O1 <= O2
213 std::swap(V1Size, V2Size);
222 // If we cannot determine alias properties based on our graph, fall back on
223 // some other AA implementation.
225 return AliasAnalysis::alias(V1, V1Size, V2, V2Size);