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/AliasAnalysis.h"
12 #include "llvm/Module.h"
13 #include "Support/Statistic.h"
16 class Steens : public Pass, public AliasAnalysis {
19 Steens() : ResultGraph(0) {}
20 ~Steens() { assert(ResultGraph == 0 && "releaseMemory not called?"); }
22 //------------------------------------------------
23 // Implement the Pass API
26 // run - Build up the result graph, representing the pointer graph for the
31 virtual void releaseMemory() { delete ResultGraph; ResultGraph = 0; }
33 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
34 AU.setPreservesAll(); // Does not transform code...
35 AU.addRequired<LocalDataStructures>(); // Uses local dsgraph
36 AU.addRequired<AliasAnalysis>(); // Chains to another AA impl...
39 // print - Implement the Pass::print method...
40 void print(std::ostream &O, const Module *M) const {
41 assert(ResultGraph && "Result graph has not yet been computed!");
42 ResultGraph->writeGraphToFile(O, "steensgaards");
45 //------------------------------------------------
46 // Implement the AliasAnalysis API
49 // alias - This is the only method here that does anything interesting...
50 Result alias(const Value *V1, const Value *V2) const;
52 /// canCallModify - We are not interprocedural, so we do nothing exciting.
54 Result canCallModify(const CallInst &CI, const Value *Ptr) const {
58 /// canInvokeModify - We are not interprocedural, so we do nothing exciting.
60 Result canInvokeModify(const InvokeInst &I, const Value *Ptr) const {
61 return MayAlias; // We are not interprocedural
65 void ResolveFunctionCall(Function *F, const std::vector<DSNodeHandle> &Call,
66 DSNodeHandle &RetVal);
69 // Register the pass...
70 RegisterOpt<Steens> X("steens-aa",
71 "Steensgaard's FlowInsensitive/ConIns alias analysis");
73 // Register as an implementation of AliasAnalysis
74 RegisterAnalysisGroup<AliasAnalysis, Steens> Y;
78 /// ResolveFunctionCall - Resolve the actual arguments of a call to function F
79 /// with the specified call site descriptor. This function links the arguments
80 /// and the return value for the call site context-insensitively.
82 void Steens::ResolveFunctionCall(Function *F,
83 const std::vector<DSNodeHandle> &Call,
84 DSNodeHandle &RetVal) {
85 assert(ResultGraph != 0 && "Result graph not allocated!");
86 std::map<Value*, DSNodeHandle> &ValMap = ResultGraph->getValueMap();
88 // Handle the return value of the function... which is Call[0]
89 if (Call[0].getNode() && RetVal.getNode())
90 RetVal.mergeWith(Call[0]);
92 // Loop over all pointer arguments, resolving them to their provided pointers
93 unsigned ArgIdx = 2; // Skip retval and function to call...
94 for (Function::aiterator AI = F->abegin(), AE = F->aend(); AI != AE; ++AI) {
95 std::map<Value*, DSNodeHandle>::iterator I = ValMap.find(AI);
96 if (I != ValMap.end()) // If its a pointer argument...
97 I->second.addEdgeTo(Call[ArgIdx++]);
100 assert(ArgIdx == Call.size() && "Argument resolution mismatch!");
104 /// run - Build up the result graph, representing the pointer graph for the
107 bool Steens::run(Module &M) {
108 assert(ResultGraph == 0 && "Result graph already allocated!");
109 LocalDataStructures &LDS = getAnalysis<LocalDataStructures>();
111 // Create a new, empty, graph...
112 ResultGraph = new DSGraph();
114 // RetValMap - Keep track of the return values for all functions that return
117 std::map<Function*, DSNodeHandle> RetValMap;
119 // Loop over the rest of the module, merging graphs for non-external functions
122 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
123 if (!I->isExternal()) {
124 std::map<Value*, DSNodeHandle> ValMap;
125 { // Scope to free NodeMap memory ASAP
126 std::map<const DSNode*, DSNode*> NodeMap;
127 const DSGraph &FDSG = LDS.getDSGraph(*I);
128 DSNodeHandle RetNode = ResultGraph->cloneInto(FDSG, ValMap, NodeMap);
130 // Keep track of the return node of the function's graph if it returns a
133 if (RetNode.getNode())
134 RetValMap[I] = RetNode;
137 // Incorporate the inlined Function's ValueMap into the global ValueMap...
138 std::map<Value*, DSNodeHandle> &GVM = ResultGraph->getValueMap();
140 while (!ValMap.empty()) { // Loop over value map, moving entries over...
141 const std::pair<Value*, DSNodeHandle> &DSN = *ValMap.begin();
142 std::map<Value*, DSNodeHandle>::iterator I = GVM.find(DSN.first);
144 GVM[DSN.first] = DSN.second;
146 I->second.mergeWith(DSN.second);
147 ValMap.erase(ValMap.begin());
151 // FIXME: Must recalculate and use the Incomplete markers!!
153 // Now that we have all of the graphs inlined, we can go about eliminating
156 std::vector<std::vector<DSNodeHandle> > &Calls =
157 ResultGraph->getFunctionCalls();
158 for (unsigned i = 0; i != Calls.size(); ) {
159 std::vector<DSNodeHandle> &CurCall = Calls[i];
161 // Loop over the called functions, eliminating as many as possible...
162 std::vector<GlobalValue*> CallTargets = CurCall[1].getNode()->getGlobals();
163 for (unsigned c = 0; c != CallTargets.size(); ) {
164 // If we can eliminate this function call, do so!
165 bool Eliminated = false;
166 if (Function *F = dyn_cast<Function>(CallTargets[c]))
167 if (!F->isExternal()) {
168 ResolveFunctionCall(F, CurCall, RetValMap[F]);
172 CallTargets.erase(CallTargets.begin()+c);
174 ++c; // Cannot eliminate this call, skip over it...
177 if (CallTargets.empty()) // Eliminated all calls?
178 Calls.erase(Calls.begin()+i); // Remove from call list...
180 ++i; // Skip this call site...
183 // Update the "incomplete" markers on the nodes, ignoring unknownness due to
184 // incoming arguments...
185 ResultGraph->maskIncompleteMarkers();
186 ResultGraph->markIncompleteNodes(false);
188 // Remove any nodes that are dead after all of the merging we have done...
189 ResultGraph->removeTriviallyDeadNodes();
191 DEBUG(print(std::cerr, &M));
195 // alias - This is the only method here that does anything interesting...
196 AliasAnalysis::Result Steens::alias(const Value *V1, const Value *V2) const {
197 assert(ResultGraph && "Result grcaph has not yet been computed!");
199 std::map<Value*, DSNodeHandle> &GVM = ResultGraph->getValueMap();
201 std::map<Value*, DSNodeHandle>::iterator I = GVM.find(const_cast<Value*>(V1));
202 if (I != GVM.end() && I->second.getNode()) {
203 DSNodeHandle &V1H = I->second;
204 std::map<Value*, DSNodeHandle>::iterator J=GVM.find(const_cast<Value*>(V2));
205 if (J != GVM.end() && J->second.getNode()) {
206 DSNodeHandle &V2H = J->second;
207 // If the two pointers point to different data structure graph nodes, they
209 if (V1H.getNode() != V2H.getNode())
212 // FIXME: If the two pointers point to the same node, and the offsets are
213 // different, and the LinkIndex vector doesn't alias the section, then the
214 // two pointers do not alias. We need access size information for the two
220 // If we cannot determine alias properties based on our graph, fall back on
221 // some other AA implementation.
223 return getAnalysis<AliasAnalysis>().alias(V1, V2);