blob: dd07ee6f8249aefc63816d430f8eb99a567c78d0 [file] [log] [blame]
Chris Lattner1c7ce2c2002-10-01 22:34:12 +00001//===- Steensgaard.cpp - Context Insensitive Alias Analysis ---------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner1c7ce2c2002-10-01 22:34:12 +00009//
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.
14//
15//===----------------------------------------------------------------------===//
16
Chris Lattner4dabb2c2004-07-07 06:32:21 +000017#include "llvm/Analysis/DataStructure/DataStructure.h"
18#include "llvm/Analysis/DataStructure/DSGraph.h"
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000019#include "llvm/Analysis/AliasAnalysis.h"
Jeff Cohen1d7b5de2005-01-09 20:42:52 +000020#include "llvm/Analysis/Passes.h"
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000021#include "llvm/Module.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/Support/Debug.h"
Chris Lattner9a927292003-11-12 23:11:14 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Misha Brukmanf4445df2002-12-12 05:34:10 +000025namespace {
Chris Lattnerb12914b2004-09-20 04:48:05 +000026 class Steens : public ModulePass, public AliasAnalysis {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000027 DSGraph *ResultGraph;
Chris Lattnere6c0b5d2003-02-04 00:03:37 +000028 DSGraph *GlobalsGraph; // FIXME: Eliminate globals graph stuff from DNE
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000029 public:
Chris Lattnera612afc2003-02-26 19:29:36 +000030 Steens() : ResultGraph(0), GlobalsGraph(0) {}
Chris Lattnerbe1094e2003-02-13 21:44:18 +000031 ~Steens() {
32 releaseMyMemory();
33 assert(ResultGraph == 0 && "releaseMemory not called?");
34 }
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000035
36 //------------------------------------------------
37 // Implement the Pass API
38 //
39
40 // run - Build up the result graph, representing the pointer graph for the
41 // program.
42 //
Chris Lattnerb12914b2004-09-20 04:48:05 +000043 bool runOnModule(Module &M);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000044
Chris Lattnerbe1094e2003-02-13 21:44:18 +000045 virtual void releaseMyMemory() { delete ResultGraph; ResultGraph = 0; }
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000046
47 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnera612afc2003-02-26 19:29:36 +000048 AliasAnalysis::getAnalysisUsage(AU);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000049 AU.setPreservesAll(); // Does not transform code...
50 AU.addRequired<LocalDataStructures>(); // Uses local dsgraph
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000051 }
52
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");
57 }
58
59 //------------------------------------------------
60 // Implement the AliasAnalysis API
61 //
62
63 // alias - This is the only method here that does anything interesting...
Chris Lattnera612afc2003-02-26 19:29:36 +000064 AliasResult alias(const Value *V1, unsigned V1Size,
65 const Value *V2, unsigned V2Size);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000066
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000067 private:
Vikram S. Adve42fd1692002-10-20 18:07:37 +000068 void ResolveFunctionCall(Function *F, const DSCallSite &Call,
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000069 DSNodeHandle &RetVal);
70 };
71
72 // Register the pass...
73 RegisterOpt<Steens> X("steens-aa",
Chris Lattner6022ef42003-02-08 23:03:17 +000074 "Steensgaard's alias analysis (DSGraph based)");
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000075
76 // Register as an implementation of AliasAnalysis
77 RegisterAnalysisGroup<AliasAnalysis, Steens> Y;
78}
79
Jeff Cohen1d7b5de2005-01-09 20:42:52 +000080ModulePass *llvm::createSteensgaardPass() { return new Steens(); }
81
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000082/// ResolveFunctionCall - Resolve the actual arguments of a call to function F
83/// with the specified call site descriptor. This function links the arguments
84/// and the return value for the call site context-insensitively.
85///
Chris Lattnere6c0b5d2003-02-04 00:03:37 +000086void Steens::ResolveFunctionCall(Function *F, const DSCallSite &Call,
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000087 DSNodeHandle &RetVal) {
88 assert(ResultGraph != 0 && "Result graph not allocated!");
Chris Lattner8d327672003-06-30 03:36:09 +000089 DSGraph::ScalarMapTy &ValMap = ResultGraph->getScalarMap();
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000090
Vikram S. Adve42fd1692002-10-20 18:07:37 +000091 // Handle the return value of the function...
Chris Lattner0969c502002-10-21 02:08:03 +000092 if (Call.getRetVal().getNode() && RetVal.getNode())
93 RetVal.mergeWith(Call.getRetVal());
Chris Lattner1c7ce2c2002-10-01 22:34:12 +000094
95 // Loop over all pointer arguments, resolving them to their provided pointers
Vikram S. Adve26b98262002-10-20 21:41:02 +000096 unsigned PtrArgIdx = 0;
Chris Lattnere4d5c442005-03-15 04:54:21 +000097 for (Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
Chris Lattner88c72942003-02-10 18:16:19 +000098 AI != AE && PtrArgIdx < Call.getNumPtrArgs(); ++AI) {
Chris Lattner8d327672003-06-30 03:36:09 +000099 DSGraph::ScalarMapTy::iterator I = ValMap.find(AI);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000100 if (I != ValMap.end()) // If its a pointer argument...
Chris Lattnere6c0b5d2003-02-04 00:03:37 +0000101 I->second.mergeWith(Call.getPtrArg(PtrArgIdx++));
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000102 }
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000103}
104
105
106/// run - Build up the result graph, representing the pointer graph for the
107/// program.
108///
Chris Lattnerb12914b2004-09-20 04:48:05 +0000109bool Steens::runOnModule(Module &M) {
Chris Lattnera612afc2003-02-26 19:29:36 +0000110 InitializeAliasAnalysis(this);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000111 assert(ResultGraph == 0 && "Result graph already allocated!");
112 LocalDataStructures &LDS = getAnalysis<LocalDataStructures>();
113
114 // Create a new, empty, graph...
Chris Lattner15869aa2003-11-02 22:27:28 +0000115 ResultGraph = new DSGraph(getTargetData());
116 GlobalsGraph = new DSGraph(getTargetData());
Chris Lattnere6c0b5d2003-02-04 00:03:37 +0000117 ResultGraph->setGlobalsGraph(GlobalsGraph);
Chris Lattneraf283512003-02-09 19:26:47 +0000118 ResultGraph->setPrintAuxCalls();
119
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000120 // RetValMap - Keep track of the return values for all functions that return
121 // valid pointers.
122 //
Chris Lattner5a540632003-06-30 03:15:25 +0000123 DSGraph::ReturnNodesTy RetValMap;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000124
125 // Loop over the rest of the module, merging graphs for non-external functions
126 // into this graph.
127 //
Chris Lattner0f777ab2003-02-10 00:14:57 +0000128 unsigned Count = 0;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000129 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
130 if (!I->isExternal()) {
Chris Lattner5a540632003-06-30 03:15:25 +0000131 DSGraph::ScalarMapTy ValMap;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000132 { // Scope to free NodeMap memory ASAP
Chris Lattner5a540632003-06-30 03:15:25 +0000133 DSGraph::NodeMapTy NodeMap;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000134 const DSGraph &FDSG = LDS.getDSGraph(*I);
Chris Lattner091f7762004-01-23 01:44:53 +0000135 ResultGraph->cloneInto(FDSG, ValMap, RetValMap, NodeMap,
136 DSGraph::UpdateInlinedGlobals);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000137 }
138
Chris Lattnerc875f022002-11-03 21:27:48 +0000139 // Incorporate the inlined Function's ScalarMap into the global
140 // ScalarMap...
Chris Lattner5a540632003-06-30 03:15:25 +0000141 DSGraph::ScalarMapTy &GVM = ResultGraph->getScalarMap();
Chris Lattner8d327672003-06-30 03:36:09 +0000142 for (DSGraph::ScalarMapTy::iterator I = ValMap.begin(),
Chris Lattnere6c0b5d2003-02-04 00:03:37 +0000143 E = ValMap.end(); I != E; ++I)
144 GVM[I->first].mergeWith(I->second);
Chris Lattner0f777ab2003-02-10 00:14:57 +0000145
146 if ((++Count & 1) == 0) // Prune nodes out every other time...
147 ResultGraph->removeTriviallyDeadNodes();
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000148 }
149
150 // FIXME: Must recalculate and use the Incomplete markers!!
151
152 // Now that we have all of the graphs inlined, we can go about eliminating
153 // call nodes...
154 //
Chris Lattnera9548d92005-01-30 23:51:02 +0000155 std::list<DSCallSite> &Calls = ResultGraph->getAuxFunctionCalls();
Chris Lattner01fb0c72002-11-08 21:27:37 +0000156 assert(Calls.empty() && "Aux call list is already in use??");
157
Chris Lattnera9548d92005-01-30 23:51:02 +0000158 // Start with a copy of the original call sites.
Chris Lattner01fb0c72002-11-08 21:27:37 +0000159 Calls = ResultGraph->getFunctionCalls();
160
Chris Lattnera9548d92005-01-30 23:51:02 +0000161 for (std::list<DSCallSite>::iterator CI = Calls.begin(), E = Calls.end();
162 CI != E;) {
163 DSCallSite &CurCall = *CI++;
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000164
165 // Loop over the called functions, eliminating as many as possible...
Chris Lattner923fc052003-02-05 21:59:58 +0000166 std::vector<GlobalValue*> CallTargets;
167 if (CurCall.isDirectCall())
168 CallTargets.push_back(CurCall.getCalleeFunc());
169 else
170 CallTargets = CurCall.getCalleeNode()->getGlobals();
171
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000172 for (unsigned c = 0; c != CallTargets.size(); ) {
173 // If we can eliminate this function call, do so!
174 bool Eliminated = false;
175 if (Function *F = dyn_cast<Function>(CallTargets[c]))
176 if (!F->isExternal()) {
177 ResolveFunctionCall(F, CurCall, RetValMap[F]);
178 Eliminated = true;
179 }
Chris Lattner0f777ab2003-02-10 00:14:57 +0000180 if (Eliminated) {
181 CallTargets[c] = CallTargets.back();
182 CallTargets.pop_back();
183 } else
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000184 ++c; // Cannot eliminate this call, skip over it...
185 }
186
Chris Lattner0f777ab2003-02-10 00:14:57 +0000187 if (CallTargets.empty()) { // Eliminated all calls?
Chris Lattnera9548d92005-01-30 23:51:02 +0000188 std::list<DSCallSite>::iterator I = CI;
189 Calls.erase(--I); // Remove entry
190 }
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000191 }
192
Chris Lattner72d29a42003-02-11 23:11:51 +0000193 RetValMap.clear();
194
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000195 // Update the "incomplete" markers on the nodes, ignoring unknownness due to
196 // incoming arguments...
197 ResultGraph->maskIncompleteMarkers();
Chris Lattner394471f2003-01-23 22:05:33 +0000198 ResultGraph->markIncompleteNodes(DSGraph::IgnoreFormalArgs);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000199
200 // Remove any nodes that are dead after all of the merging we have done...
Chris Lattnere6c0b5d2003-02-04 00:03:37 +0000201 // FIXME: We should be able to disable the globals graph for steens!
Chris Lattner394471f2003-01-23 22:05:33 +0000202 ResultGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000203
Chris Lattner47a307a2003-02-09 18:42:16 +0000204 DEBUG(print(std::cerr, &M));
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000205 return false;
206}
207
208// alias - This is the only method here that does anything interesting...
Chris Lattnera612afc2003-02-26 19:29:36 +0000209AliasAnalysis::AliasResult Steens::alias(const Value *V1, unsigned V1Size,
210 const Value *V2, unsigned V2Size) {
211 // FIXME: HANDLE Size argument!
Misha Brukmanf4445df2002-12-12 05:34:10 +0000212 assert(ResultGraph && "Result graph has not been computed yet!");
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000213
Chris Lattner8d327672003-06-30 03:36:09 +0000214 DSGraph::ScalarMapTy &GSM = ResultGraph->getScalarMap();
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000215
Chris Lattner8d327672003-06-30 03:36:09 +0000216 DSGraph::ScalarMapTy::iterator I = GSM.find(const_cast<Value*>(V1));
Chris Lattner0a91e632003-02-03 22:51:53 +0000217 if (I != GSM.end() && I->second.getNode()) {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000218 DSNodeHandle &V1H = I->second;
Chris Lattner8d327672003-06-30 03:36:09 +0000219 DSGraph::ScalarMapTy::iterator J=GSM.find(const_cast<Value*>(V2));
Chris Lattner0a91e632003-02-03 22:51:53 +0000220 if (J != GSM.end() && J->second.getNode()) {
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000221 DSNodeHandle &V2H = J->second;
222 // If the two pointers point to different data structure graph nodes, they
223 // cannot alias!
Chris Lattner6022ef42003-02-08 23:03:17 +0000224 if (V1H.getNode() != V2H.getNode()) // FIXME: Handle incompleteness!
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000225 return NoAlias;
Chris Lattner6022ef42003-02-08 23:03:17 +0000226
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000227 // FIXME: If the two pointers point to the same node, and the offsets are
228 // different, and the LinkIndex vector doesn't alias the section, then the
229 // two pointers do not alias. We need access size information for the two
230 // accesses though!
231 //
232 }
233 }
234
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000235 // If we cannot determine alias properties based on our graph, fall back on
236 // some other AA implementation.
237 //
Chris Lattner1bf34082004-05-23 21:13:51 +0000238 return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
Chris Lattner1c7ce2c2002-10-01 22:34:12 +0000239}