blob: 892c7dfe61960edae4411a090a350f1fa4e601ed [file] [log] [blame]
Chris Lattner8105cfb2003-02-03 22:50:46 +00001//===- DataStructureAA.cpp - Data Structure Based 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 Lattner8105cfb2003-02-03 22:50:46 +00009//
10// This pass uses the top-down data structure graphs to implement a simple
11// context sensitive alias analysis.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner1c8327b2005-03-17 20:33:27 +000015#include "llvm/Constants.h"
16#include "llvm/DerivedTypes.h"
Misha Brukmana975a9a2004-03-12 06:14:22 +000017#include "llvm/Module.h"
18#include "llvm/Analysis/AliasAnalysis.h"
Jeff Cohen1d7b5de2005-01-09 20:42:52 +000019#include "llvm/Analysis/Passes.h"
Chris Lattner4dabb2c2004-07-07 06:32:21 +000020#include "llvm/Analysis/DataStructure/DataStructure.h"
21#include "llvm/Analysis/DataStructure/DSGraph.h"
Chris Lattner9a927292003-11-12 23:11:14 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Chris Lattner8105cfb2003-02-03 22:50:46 +000024namespace {
Chris Lattnerb12914b2004-09-20 04:48:05 +000025 class DSAA : public ModulePass, public AliasAnalysis {
Chris Lattner8105cfb2003-02-03 22:50:46 +000026 TDDataStructures *TD;
Misha Brukmana975a9a2004-03-12 06:14:22 +000027 BUDataStructures *BU;
Chris Lattner1b9a2aa2005-03-26 23:29:03 +000028
29 // These members are used to cache mod/ref information to make us return
30 // results faster, particularly for aa-eval. On the first request of
31 // mod/ref information for a particular call site, we compute and store the
32 // calculated nodemap for the call site. Any time DSA info is updated we
33 // free this information, and when we move onto a new call site, this
34 // information is also freed.
35 CallSite MapCS;
36 std::multimap<DSNode*, const DSNode*> CallerCalleeMap;
Chris Lattner8105cfb2003-02-03 22:50:46 +000037 public:
38 DSAA() : TD(0) {}
Chris Lattner1b9a2aa2005-03-26 23:29:03 +000039 ~DSAA() {
40 InvalidateCache();
41 }
42
43 void InvalidateCache() {
44 MapCS = CallSite();
45 CallerCalleeMap.clear();
46 }
Chris Lattner8105cfb2003-02-03 22:50:46 +000047
48 //------------------------------------------------
49 // Implement the Pass API
50 //
51
52 // run - Build up the result graph, representing the pointer graph for the
53 // program.
54 //
Chris Lattnerb12914b2004-09-20 04:48:05 +000055 bool runOnModule(Module &M) {
Chris Lattnera612afc2003-02-26 19:29:36 +000056 InitializeAliasAnalysis(this);
Chris Lattner8105cfb2003-02-03 22:50:46 +000057 TD = &getAnalysis<TDDataStructures>();
Misha Brukmana975a9a2004-03-12 06:14:22 +000058 BU = &getAnalysis<BUDataStructures>();
Chris Lattner8105cfb2003-02-03 22:50:46 +000059 return false;
60 }
61
62 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnera612afc2003-02-26 19:29:36 +000063 AliasAnalysis::getAnalysisUsage(AU);
Misha Brukmana975a9a2004-03-12 06:14:22 +000064 AU.setPreservesAll(); // Does not transform code
65 AU.addRequiredTransitive<TDDataStructures>(); // Uses TD Datastructures
66 AU.addRequiredTransitive<BUDataStructures>(); // Uses BU Datastructures
Chris Lattner8105cfb2003-02-03 22:50:46 +000067 }
68
69 //------------------------------------------------
70 // Implement the AliasAnalysis API
71 //
72
Chris Lattnera612afc2003-02-26 19:29:36 +000073 AliasResult alias(const Value *V1, unsigned V1Size,
74 const Value *V2, unsigned V2Size);
Chris Lattner5c70dc02003-06-29 00:54:08 +000075
Chris Lattner484e3022004-05-23 21:14:27 +000076 ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
Reid Spencer4a7ebfa2004-12-07 08:11:24 +000077 ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
78 return AliasAnalysis::getModRefInfo(CS1,CS2);
79 }
Chris Lattner92e41d52004-01-30 22:20:55 +000080
Chris Lattner851b5342005-01-24 20:00:14 +000081 virtual void deleteValue(Value *V) {
Chris Lattner1b9a2aa2005-03-26 23:29:03 +000082 InvalidateCache();
Chris Lattner851b5342005-01-24 20:00:14 +000083 BU->deleteValue(V);
84 TD->deleteValue(V);
85 }
86
87 virtual void copyValue(Value *From, Value *To) {
88 if (From == To) return;
Chris Lattner1b9a2aa2005-03-26 23:29:03 +000089 InvalidateCache();
Chris Lattner851b5342005-01-24 20:00:14 +000090 BU->copyValue(From, To);
91 TD->copyValue(From, To);
92 }
93
Chris Lattner5c70dc02003-06-29 00:54:08 +000094 private:
95 DSGraph *getGraphForValue(const Value *V);
Chris Lattner8105cfb2003-02-03 22:50:46 +000096 };
97
98 // Register the pass...
99 RegisterOpt<DSAA> X("ds-aa", "Data Structure Graph Based Alias Analysis");
100
101 // Register as an implementation of AliasAnalysis
102 RegisterAnalysisGroup<AliasAnalysis, DSAA> Y;
103}
104
Jeff Cohen1d7b5de2005-01-09 20:42:52 +0000105ModulePass *llvm::createDSAAPass() { return new DSAA(); }
106
Chris Lattner5c70dc02003-06-29 00:54:08 +0000107// getGraphForValue - Return the DSGraph to use for queries about the specified
108// value...
Chris Lattner8105cfb2003-02-03 22:50:46 +0000109//
Chris Lattner5c70dc02003-06-29 00:54:08 +0000110DSGraph *DSAA::getGraphForValue(const Value *V) {
Chris Lattner8105cfb2003-02-03 22:50:46 +0000111 if (const Instruction *I = dyn_cast<Instruction>(V))
Chris Lattner5c70dc02003-06-29 00:54:08 +0000112 return &TD->getDSGraph(*I->getParent()->getParent());
Chris Lattner8105cfb2003-02-03 22:50:46 +0000113 else if (const Argument *A = dyn_cast<Argument>(V))
Chris Lattner5c70dc02003-06-29 00:54:08 +0000114 return &TD->getDSGraph(*A->getParent());
Chris Lattner8105cfb2003-02-03 22:50:46 +0000115 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattner5c70dc02003-06-29 00:54:08 +0000116 return &TD->getDSGraph(*BB->getParent());
Chris Lattner8105cfb2003-02-03 22:50:46 +0000117 return 0;
118}
119
Chris Lattnera612afc2003-02-26 19:29:36 +0000120AliasAnalysis::AliasResult DSAA::alias(const Value *V1, unsigned V1Size,
121 const Value *V2, unsigned V2Size) {
Chris Lattnerbd92b732003-06-19 21:15:11 +0000122 if (V1 == V2) return MustAlias;
123
Chris Lattner5c70dc02003-06-29 00:54:08 +0000124 DSGraph *G1 = getGraphForValue(V1);
125 DSGraph *G2 = getGraphForValue(V2);
126 assert((!G1 || !G2 || G1 == G2) && "Alias query for 2 different functions?");
Chris Lattner8105cfb2003-02-03 22:50:46 +0000127
Chris Lattner5c70dc02003-06-29 00:54:08 +0000128 // Get the graph to use...
129 DSGraph &G = *(G1 ? G1 : (G2 ? G2 : &TD->getGlobalsGraph()));
Chris Lattnerbd92b732003-06-19 21:15:11 +0000130
Chris Lattner5c70dc02003-06-29 00:54:08 +0000131 const DSGraph::ScalarMapTy &GSM = G.getScalarMap();
132 DSGraph::ScalarMapTy::const_iterator I = GSM.find((Value*)V1);
Chris Lattnerbac7da82004-04-26 14:44:08 +0000133 if (I == GSM.end()) return NoAlias;
Chris Lattner511f60c2005-03-18 00:21:03 +0000134
Chris Lattnerbac7da82004-04-26 14:44:08 +0000135 DSGraph::ScalarMapTy::const_iterator J = GSM.find((Value*)V2);
136 if (J == GSM.end()) return NoAlias;
137
Chris Lattnerbac7da82004-04-26 14:44:08 +0000138 DSNode *N1 = I->second.getNode(), *N2 = J->second.getNode();
139 unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset();
Chris Lattner511f60c2005-03-18 00:21:03 +0000140 if (N1 == 0 || N2 == 0)
141 return MayAlias; // Can't tell whether anything aliases null.
Chris Lattnerbd92b732003-06-19 21:15:11 +0000142
Chris Lattnerbac7da82004-04-26 14:44:08 +0000143 // We can only make a judgment of one of the nodes is complete...
144 if (N1->isComplete() || N2->isComplete()) {
145 if (N1 != N2)
146 return NoAlias; // Completely different nodes.
Chris Lattnerbd92b732003-06-19 21:15:11 +0000147
Chris Lattnerbac7da82004-04-26 14:44:08 +0000148 // See if they point to different offsets... if so, we may be able to
149 // determine that they do not alias...
150 if (O1 != O2) {
151 if (O2 < O1) { // Ensure that O1 <= O2
152 std::swap(V1, V2);
153 std::swap(O1, O2);
154 std::swap(V1Size, V2Size);
Chris Lattner8105cfb2003-02-03 22:50:46 +0000155 }
Chris Lattnerbac7da82004-04-26 14:44:08 +0000156
Chris Lattner2f72f942005-03-23 01:47:19 +0000157 if (O1+V1Size <= O2)
158 return NoAlias;
Chris Lattner8105cfb2003-02-03 22:50:46 +0000159 }
160 }
161
162 // FIXME: we could improve on this by checking the globals graph for aliased
163 // global queries...
Chris Lattner484e3022004-05-23 21:14:27 +0000164 return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
Chris Lattner8105cfb2003-02-03 22:50:46 +0000165}
Chris Lattner5c70dc02003-06-29 00:54:08 +0000166
Misha Brukmana975a9a2004-03-12 06:14:22 +0000167/// getModRefInfo - does a callsite modify or reference a value?
168///
169AliasAnalysis::ModRefResult
170DSAA::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
Chris Lattner1b9a2aa2005-03-26 23:29:03 +0000171 DSNode *N = 0;
172 // First step, check our cache.
173 if (CS.getInstruction() == MapCS.getInstruction()) {
174 {
175 const Function *Caller = CS.getInstruction()->getParent()->getParent();
176 DSGraph &CallerTDGraph = TD->getDSGraph(*Caller);
177
178 // Figure out which node in the TD graph this pointer corresponds to.
179 DSScalarMap &CallerSM = CallerTDGraph.getScalarMap();
180 DSScalarMap::iterator NI = CallerSM.find(P);
181 if (NI == CallerSM.end()) {
182 InvalidateCache();
183 return DSAA::getModRefInfo(CS, P, Size);
184 }
185 N = NI->second.getNode();
186 }
187
188 HaveMappingInfo:
189 assert(N && "Null pointer in scalar map??");
190
191 typedef std::multimap<DSNode*, const DSNode*>::iterator NodeMapIt;
192 std::pair<NodeMapIt, NodeMapIt> Range = CallerCalleeMap.equal_range(N);
193
194 // Loop over all of the nodes in the callee that correspond to "N", keeping
195 // track of aggregate mod/ref info.
196 bool NeverReads = true, NeverWrites = true;
197 for (; Range.first != Range.second; ++Range.first) {
198 if (Range.first->second->isModified())
199 NeverWrites = false;
200 if (Range.first->second->isRead())
201 NeverReads = false;
202 if (NeverReads == false && NeverWrites == false)
203 return AliasAnalysis::getModRefInfo(CS, P, Size);
204 }
205
206 ModRefResult Result = ModRef;
207 if (NeverWrites) // We proved it was not modified.
208 Result = ModRefResult(Result & ~Mod);
209 if (NeverReads) // We proved it was not read.
210 Result = ModRefResult(Result & ~Ref);
211
212 return ModRefResult(Result & AliasAnalysis::getModRefInfo(CS, P, Size));
213 }
214
215 // Any cached info we have is for the wrong function.
216 InvalidateCache();
217
Misha Brukmana975a9a2004-03-12 06:14:22 +0000218 Function *F = CS.getCalledFunction();
Chris Lattner50cb9b42005-03-17 20:16:58 +0000219
Chris Lattner1b9a2aa2005-03-26 23:29:03 +0000220 if (!F) return AliasAnalysis::getModRefInfo(CS, P, Size);
Misha Brukmana975a9a2004-03-12 06:14:22 +0000221
Chris Lattner7d671b82005-03-24 03:04:50 +0000222 if (F->isExternal()) {
223 // If we are calling an external function, and if this global doesn't escape
224 // the portion of the program we have analyzed, we can draw conclusions
225 // based on whether the global escapes the program.
226 Function *Caller = CS.getInstruction()->getParent()->getParent();
227 DSGraph *G = &TD->getDSGraph(*Caller);
228 DSScalarMap::iterator NI = G->getScalarMap().find(P);
229 if (NI == G->getScalarMap().end()) {
230 // If it wasn't in the local function graph, check the global graph. This
231 // can occur for globals who are locally reference but hoisted out to the
232 // globals graph despite that.
233 G = G->getGlobalsGraph();
234 NI = G->getScalarMap().find(P);
235 if (NI == G->getScalarMap().end())
Chris Lattner1b9a2aa2005-03-26 23:29:03 +0000236 return AliasAnalysis::getModRefInfo(CS, P, Size);
Chris Lattner7d671b82005-03-24 03:04:50 +0000237 }
238
239 // If we found a node and it's complete, it cannot be passed out to the
240 // called function.
241 if (NI->second.getNode()->isComplete())
242 return NoModRef;
Chris Lattner1b9a2aa2005-03-26 23:29:03 +0000243 return AliasAnalysis::getModRefInfo(CS, P, Size);
Chris Lattner7d671b82005-03-24 03:04:50 +0000244 }
245
Chris Lattner511f60c2005-03-18 00:21:03 +0000246 // Get the graphs for the callee and caller. Note that we want the BU graph
247 // for the callee because we don't want all caller's effects incorporated!
248 const Function *Caller = CS.getInstruction()->getParent()->getParent();
249 DSGraph &CallerTDGraph = TD->getDSGraph(*Caller);
250 DSGraph &CalleeBUGraph = BU->getDSGraph(*F);
Misha Brukmana975a9a2004-03-12 06:14:22 +0000251
Chris Lattner511f60c2005-03-18 00:21:03 +0000252 // Figure out which node in the TD graph this pointer corresponds to.
253 DSScalarMap &CallerSM = CallerTDGraph.getScalarMap();
254 DSScalarMap::iterator NI = CallerSM.find(P);
255 if (NI == CallerSM.end()) {
Chris Lattner1b9a2aa2005-03-26 23:29:03 +0000256 ModRefResult Result = ModRef;
Chris Lattnerdb7436a2005-03-18 23:18:30 +0000257 if (isa<ConstantPointerNull>(P) || isa<UndefValue>(P))
Chris Lattner1b9a2aa2005-03-26 23:29:03 +0000258 return NoModRef; // null is never modified :)
Chris Lattner511f60c2005-03-18 00:21:03 +0000259 else {
Chris Lattner1c8327b2005-03-17 20:33:27 +0000260 assert(isa<GlobalVariable>(P) &&
261 cast<GlobalVariable>(P)->getType()->getElementType()->isFirstClassType() &&
262 "This isn't a global that DSA inconsiderately dropped "
263 "from the graph?");
Chris Lattnerbc499de2005-03-26 22:47:03 +0000264
265 DSGraph &GG = *CallerTDGraph.getGlobalsGraph();
266 DSScalarMap::iterator NI = GG.getScalarMap().find(P);
267 if (NI != GG.getScalarMap().end() && !NI->second.isNull()) {
268 // Otherwise, if the node is only M or R, return this. This can be
269 // useful for globals that should be marked const but are not.
270 DSNode *N = NI->second.getNode();
271 if (!N->isModified())
272 Result = (ModRefResult)(Result & ~Mod);
273 if (!N->isRead())
274 Result = (ModRefResult)(Result & ~Ref);
275 }
Chris Lattner511f60c2005-03-18 00:21:03 +0000276 }
Chris Lattner511f60c2005-03-18 00:21:03 +0000277
Chris Lattner1b9a2aa2005-03-26 23:29:03 +0000278 if (Result == NoModRef) return Result;
279 return ModRefResult(Result & AliasAnalysis::getModRefInfo(CS, P, Size));
280 }
Chris Lattner511f60c2005-03-18 00:21:03 +0000281
282 // Compute the mapping from nodes in the callee graph to the nodes in the
283 // caller graph for this call site.
284 DSGraph::NodeMapTy CalleeCallerMap;
285 DSCallSite DSCS = CallerTDGraph.getDSCallSiteForCallSite(CS);
286 CallerTDGraph.computeCalleeCallerMapping(DSCS, *F, CalleeBUGraph,
287 CalleeCallerMap);
288
Chris Lattner1b9a2aa2005-03-26 23:29:03 +0000289 // Remember the mapping and the call site for future queries.
290 MapCS = CS;
291
292 // Invert the mapping into CalleeCallerInvMap.
Chris Lattner511f60c2005-03-18 00:21:03 +0000293 for (DSGraph::NodeMapTy::iterator I = CalleeCallerMap.begin(),
294 E = CalleeCallerMap.end(); I != E; ++I)
Chris Lattner1b9a2aa2005-03-26 23:29:03 +0000295 CallerCalleeMap.insert(std::make_pair(I->second.getNode(), I->first));
Chris Lattner511f60c2005-03-18 00:21:03 +0000296
Chris Lattner1b9a2aa2005-03-26 23:29:03 +0000297 N = NI->second.getNode();
298 goto HaveMappingInfo;
Misha Brukmana975a9a2004-03-12 06:14:22 +0000299}