blob: 1ea1d889477e2ffdddb2c93be3d9453ea75bcf64 [file] [log] [blame]
Chris Lattner8105cfb2003-02-03 22:50:46 +00001//===- DataStructureAA.cpp - Data Structure Based Alias Analysis ----------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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
Misha Brukman2b37d7c2005-04-21 21:13:18 +000071 //
Chris Lattner8105cfb2003-02-03 22:50:46 +000072
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?");
Misha Brukman2b37d7c2005-04-21 21:13:18 +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;
Misha Brukman2b37d7c2005-04-21 21:13:18 +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)
Chris Lattner5f4c0a82005-03-27 00:02:33 +0000141 // Can't tell whether anything aliases null.
142 return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
Chris Lattner93ab2192005-03-27 21:57:09 +0000143
144 // We can only make a judgment if one of the nodes is complete.
Chris Lattnerbac7da82004-04-26 14:44:08 +0000145 if (N1->isComplete() || N2->isComplete()) {
146 if (N1 != N2)
147 return NoAlias; // Completely different nodes.
Chris Lattnerbd92b732003-06-19 21:15:11 +0000148
Chris Lattnerbac7da82004-04-26 14:44:08 +0000149 // See if they point to different offsets... if so, we may be able to
150 // determine that they do not alias...
151 if (O1 != O2) {
152 if (O2 < O1) { // Ensure that O1 <= O2
153 std::swap(V1, V2);
154 std::swap(O1, O2);
155 std::swap(V1Size, V2Size);
Chris Lattner8105cfb2003-02-03 22:50:46 +0000156 }
Chris Lattnerbac7da82004-04-26 14:44:08 +0000157
Chris Lattner2f72f942005-03-23 01:47:19 +0000158 if (O1+V1Size <= O2)
159 return NoAlias;
Chris Lattner8105cfb2003-02-03 22:50:46 +0000160 }
161 }
162
163 // FIXME: we could improve on this by checking the globals graph for aliased
164 // global queries...
Chris Lattner484e3022004-05-23 21:14:27 +0000165 return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
Chris Lattner8105cfb2003-02-03 22:50:46 +0000166}
Chris Lattner5c70dc02003-06-29 00:54:08 +0000167
Misha Brukmana975a9a2004-03-12 06:14:22 +0000168/// getModRefInfo - does a callsite modify or reference a value?
169///
170AliasAnalysis::ModRefResult
171DSAA::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
Chris Lattner1b9a2aa2005-03-26 23:29:03 +0000172 DSNode *N = 0;
173 // First step, check our cache.
174 if (CS.getInstruction() == MapCS.getInstruction()) {
175 {
176 const Function *Caller = CS.getInstruction()->getParent()->getParent();
177 DSGraph &CallerTDGraph = TD->getDSGraph(*Caller);
178
179 // Figure out which node in the TD graph this pointer corresponds to.
180 DSScalarMap &CallerSM = CallerTDGraph.getScalarMap();
181 DSScalarMap::iterator NI = CallerSM.find(P);
182 if (NI == CallerSM.end()) {
183 InvalidateCache();
184 return DSAA::getModRefInfo(CS, P, Size);
185 }
186 N = NI->second.getNode();
187 }
188
189 HaveMappingInfo:
190 assert(N && "Null pointer in scalar map??");
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000191
Chris Lattner1b9a2aa2005-03-26 23:29:03 +0000192 typedef std::multimap<DSNode*, const DSNode*>::iterator NodeMapIt;
193 std::pair<NodeMapIt, NodeMapIt> Range = CallerCalleeMap.equal_range(N);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000194
Chris Lattner1b9a2aa2005-03-26 23:29:03 +0000195 // Loop over all of the nodes in the callee that correspond to "N", keeping
196 // track of aggregate mod/ref info.
197 bool NeverReads = true, NeverWrites = true;
198 for (; Range.first != Range.second; ++Range.first) {
199 if (Range.first->second->isModified())
200 NeverWrites = false;
201 if (Range.first->second->isRead())
202 NeverReads = false;
203 if (NeverReads == false && NeverWrites == false)
204 return AliasAnalysis::getModRefInfo(CS, P, Size);
205 }
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000206
Chris Lattner1b9a2aa2005-03-26 23:29:03 +0000207 ModRefResult Result = ModRef;
208 if (NeverWrites) // We proved it was not modified.
209 Result = ModRefResult(Result & ~Mod);
210 if (NeverReads) // We proved it was not read.
211 Result = ModRefResult(Result & ~Ref);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000212
Chris Lattner1b9a2aa2005-03-26 23:29:03 +0000213 return ModRefResult(Result & AliasAnalysis::getModRefInfo(CS, P, Size));
214 }
215
216 // Any cached info we have is for the wrong function.
217 InvalidateCache();
218
Misha Brukmana975a9a2004-03-12 06:14:22 +0000219 Function *F = CS.getCalledFunction();
Chris Lattner50cb9b42005-03-17 20:16:58 +0000220
Chris Lattner1b9a2aa2005-03-26 23:29:03 +0000221 if (!F) return AliasAnalysis::getModRefInfo(CS, P, Size);
Misha Brukmana975a9a2004-03-12 06:14:22 +0000222
Chris Lattner7d671b82005-03-24 03:04:50 +0000223 if (F->isExternal()) {
224 // If we are calling an external function, and if this global doesn't escape
225 // the portion of the program we have analyzed, we can draw conclusions
226 // based on whether the global escapes the program.
227 Function *Caller = CS.getInstruction()->getParent()->getParent();
228 DSGraph *G = &TD->getDSGraph(*Caller);
229 DSScalarMap::iterator NI = G->getScalarMap().find(P);
230 if (NI == G->getScalarMap().end()) {
231 // If it wasn't in the local function graph, check the global graph. This
232 // can occur for globals who are locally reference but hoisted out to the
233 // globals graph despite that.
234 G = G->getGlobalsGraph();
235 NI = G->getScalarMap().find(P);
236 if (NI == G->getScalarMap().end())
Chris Lattner1b9a2aa2005-03-26 23:29:03 +0000237 return AliasAnalysis::getModRefInfo(CS, P, Size);
Chris Lattner7d671b82005-03-24 03:04:50 +0000238 }
239
240 // If we found a node and it's complete, it cannot be passed out to the
241 // called function.
242 if (NI->second.getNode()->isComplete())
243 return NoModRef;
Chris Lattner1b9a2aa2005-03-26 23:29:03 +0000244 return AliasAnalysis::getModRefInfo(CS, P, Size);
Chris Lattner7d671b82005-03-24 03:04:50 +0000245 }
246
Chris Lattner511f60c2005-03-18 00:21:03 +0000247 // Get the graphs for the callee and caller. Note that we want the BU graph
248 // for the callee because we don't want all caller's effects incorporated!
249 const Function *Caller = CS.getInstruction()->getParent()->getParent();
250 DSGraph &CallerTDGraph = TD->getDSGraph(*Caller);
251 DSGraph &CalleeBUGraph = BU->getDSGraph(*F);
Misha Brukmana975a9a2004-03-12 06:14:22 +0000252
Chris Lattner511f60c2005-03-18 00:21:03 +0000253 // Figure out which node in the TD graph this pointer corresponds to.
254 DSScalarMap &CallerSM = CallerTDGraph.getScalarMap();
255 DSScalarMap::iterator NI = CallerSM.find(P);
256 if (NI == CallerSM.end()) {
Chris Lattner1b9a2aa2005-03-26 23:29:03 +0000257 ModRefResult Result = ModRef;
Chris Lattnerdb7436a2005-03-18 23:18:30 +0000258 if (isa<ConstantPointerNull>(P) || isa<UndefValue>(P))
Chris Lattner1b9a2aa2005-03-26 23:29:03 +0000259 return NoModRef; // null is never modified :)
Chris Lattner511f60c2005-03-18 00:21:03 +0000260 else {
Chris Lattner1c8327b2005-03-17 20:33:27 +0000261 assert(isa<GlobalVariable>(P) &&
262 cast<GlobalVariable>(P)->getType()->getElementType()->isFirstClassType() &&
263 "This isn't a global that DSA inconsiderately dropped "
264 "from the graph?");
Chris Lattnerbc499de2005-03-26 22:47:03 +0000265
266 DSGraph &GG = *CallerTDGraph.getGlobalsGraph();
267 DSScalarMap::iterator NI = GG.getScalarMap().find(P);
268 if (NI != GG.getScalarMap().end() && !NI->second.isNull()) {
269 // Otherwise, if the node is only M or R, return this. This can be
270 // useful for globals that should be marked const but are not.
271 DSNode *N = NI->second.getNode();
272 if (!N->isModified())
273 Result = (ModRefResult)(Result & ~Mod);
274 if (!N->isRead())
275 Result = (ModRefResult)(Result & ~Ref);
276 }
Chris Lattner511f60c2005-03-18 00:21:03 +0000277 }
Chris Lattner511f60c2005-03-18 00:21:03 +0000278
Chris Lattner1b9a2aa2005-03-26 23:29:03 +0000279 if (Result == NoModRef) return Result;
280 return ModRefResult(Result & AliasAnalysis::getModRefInfo(CS, P, Size));
281 }
Chris Lattner511f60c2005-03-18 00:21:03 +0000282
283 // Compute the mapping from nodes in the callee graph to the nodes in the
284 // caller graph for this call site.
285 DSGraph::NodeMapTy CalleeCallerMap;
286 DSCallSite DSCS = CallerTDGraph.getDSCallSiteForCallSite(CS);
287 CallerTDGraph.computeCalleeCallerMapping(DSCS, *F, CalleeBUGraph,
288 CalleeCallerMap);
289
Chris Lattner1b9a2aa2005-03-26 23:29:03 +0000290 // Remember the mapping and the call site for future queries.
291 MapCS = CS;
292
293 // Invert the mapping into CalleeCallerInvMap.
Chris Lattner511f60c2005-03-18 00:21:03 +0000294 for (DSGraph::NodeMapTy::iterator I = CalleeCallerMap.begin(),
295 E = CalleeCallerMap.end(); I != E; ++I)
Chris Lattner1b9a2aa2005-03-26 23:29:03 +0000296 CallerCalleeMap.insert(std::make_pair(I->second.getNode(), I->first));
Chris Lattner511f60c2005-03-18 00:21:03 +0000297
Chris Lattner1b9a2aa2005-03-26 23:29:03 +0000298 N = NI->second.getNode();
299 goto HaveMappingInfo;
Misha Brukmana975a9a2004-03-12 06:14:22 +0000300}