blob: 69bb5a698c9794ec7a7574d6e67e1db5a46142c4 [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 Lattner8105cfb2003-02-03 22:50:46 +000028 public:
29 DSAA() : TD(0) {}
30
31 //------------------------------------------------
32 // Implement the Pass API
33 //
34
35 // run - Build up the result graph, representing the pointer graph for the
36 // program.
37 //
Chris Lattnerb12914b2004-09-20 04:48:05 +000038 bool runOnModule(Module &M) {
Chris Lattnera612afc2003-02-26 19:29:36 +000039 InitializeAliasAnalysis(this);
Chris Lattner8105cfb2003-02-03 22:50:46 +000040 TD = &getAnalysis<TDDataStructures>();
Misha Brukmana975a9a2004-03-12 06:14:22 +000041 BU = &getAnalysis<BUDataStructures>();
Chris Lattner8105cfb2003-02-03 22:50:46 +000042 return false;
43 }
44
45 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnera612afc2003-02-26 19:29:36 +000046 AliasAnalysis::getAnalysisUsage(AU);
Misha Brukmana975a9a2004-03-12 06:14:22 +000047 AU.setPreservesAll(); // Does not transform code
48 AU.addRequiredTransitive<TDDataStructures>(); // Uses TD Datastructures
49 AU.addRequiredTransitive<BUDataStructures>(); // Uses BU Datastructures
Chris Lattner8105cfb2003-02-03 22:50:46 +000050 }
51
52 //------------------------------------------------
53 // Implement the AliasAnalysis API
54 //
55
Chris Lattnera612afc2003-02-26 19:29:36 +000056 AliasResult alias(const Value *V1, unsigned V1Size,
57 const Value *V2, unsigned V2Size);
Chris Lattner5c70dc02003-06-29 00:54:08 +000058
Chris Lattner484e3022004-05-23 21:14:27 +000059 ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
Reid Spencer4a7ebfa2004-12-07 08:11:24 +000060 ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
61 return AliasAnalysis::getModRefInfo(CS1,CS2);
62 }
Chris Lattner92e41d52004-01-30 22:20:55 +000063
Chris Lattner851b5342005-01-24 20:00:14 +000064 virtual void deleteValue(Value *V) {
65 BU->deleteValue(V);
66 TD->deleteValue(V);
67 }
68
69 virtual void copyValue(Value *From, Value *To) {
70 if (From == To) return;
71 BU->copyValue(From, To);
72 TD->copyValue(From, To);
73 }
74
Chris Lattner5c70dc02003-06-29 00:54:08 +000075 private:
76 DSGraph *getGraphForValue(const Value *V);
Chris Lattner8105cfb2003-02-03 22:50:46 +000077 };
78
79 // Register the pass...
80 RegisterOpt<DSAA> X("ds-aa", "Data Structure Graph Based Alias Analysis");
81
82 // Register as an implementation of AliasAnalysis
83 RegisterAnalysisGroup<AliasAnalysis, DSAA> Y;
84}
85
Jeff Cohen1d7b5de2005-01-09 20:42:52 +000086ModulePass *llvm::createDSAAPass() { return new DSAA(); }
87
Chris Lattner5c70dc02003-06-29 00:54:08 +000088// getGraphForValue - Return the DSGraph to use for queries about the specified
89// value...
Chris Lattner8105cfb2003-02-03 22:50:46 +000090//
Chris Lattner5c70dc02003-06-29 00:54:08 +000091DSGraph *DSAA::getGraphForValue(const Value *V) {
Chris Lattner8105cfb2003-02-03 22:50:46 +000092 if (const Instruction *I = dyn_cast<Instruction>(V))
Chris Lattner5c70dc02003-06-29 00:54:08 +000093 return &TD->getDSGraph(*I->getParent()->getParent());
Chris Lattner8105cfb2003-02-03 22:50:46 +000094 else if (const Argument *A = dyn_cast<Argument>(V))
Chris Lattner5c70dc02003-06-29 00:54:08 +000095 return &TD->getDSGraph(*A->getParent());
Chris Lattner8105cfb2003-02-03 22:50:46 +000096 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattner5c70dc02003-06-29 00:54:08 +000097 return &TD->getDSGraph(*BB->getParent());
Chris Lattner8105cfb2003-02-03 22:50:46 +000098 return 0;
99}
100
Chris Lattnera612afc2003-02-26 19:29:36 +0000101AliasAnalysis::AliasResult DSAA::alias(const Value *V1, unsigned V1Size,
102 const Value *V2, unsigned V2Size) {
Chris Lattnerbd92b732003-06-19 21:15:11 +0000103 if (V1 == V2) return MustAlias;
104
Chris Lattner5c70dc02003-06-29 00:54:08 +0000105 DSGraph *G1 = getGraphForValue(V1);
106 DSGraph *G2 = getGraphForValue(V2);
107 assert((!G1 || !G2 || G1 == G2) && "Alias query for 2 different functions?");
Chris Lattner8105cfb2003-02-03 22:50:46 +0000108
Chris Lattner5c70dc02003-06-29 00:54:08 +0000109 // Get the graph to use...
110 DSGraph &G = *(G1 ? G1 : (G2 ? G2 : &TD->getGlobalsGraph()));
Chris Lattnerbd92b732003-06-19 21:15:11 +0000111
Chris Lattner5c70dc02003-06-29 00:54:08 +0000112 const DSGraph::ScalarMapTy &GSM = G.getScalarMap();
113 DSGraph::ScalarMapTy::const_iterator I = GSM.find((Value*)V1);
Chris Lattnerbac7da82004-04-26 14:44:08 +0000114 if (I == GSM.end()) return NoAlias;
Chris Lattner511f60c2005-03-18 00:21:03 +0000115
Chris Lattnerbac7da82004-04-26 14:44:08 +0000116 DSGraph::ScalarMapTy::const_iterator J = GSM.find((Value*)V2);
117 if (J == GSM.end()) return NoAlias;
118
Chris Lattnerbac7da82004-04-26 14:44:08 +0000119 DSNode *N1 = I->second.getNode(), *N2 = J->second.getNode();
120 unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset();
Chris Lattner511f60c2005-03-18 00:21:03 +0000121 if (N1 == 0 || N2 == 0)
122 return MayAlias; // Can't tell whether anything aliases null.
Chris Lattnerbd92b732003-06-19 21:15:11 +0000123
Chris Lattnerbac7da82004-04-26 14:44:08 +0000124 // We can only make a judgment of one of the nodes is complete...
125 if (N1->isComplete() || N2->isComplete()) {
126 if (N1 != N2)
127 return NoAlias; // Completely different nodes.
Chris Lattnerbd92b732003-06-19 21:15:11 +0000128
Chris Lattnerbac7da82004-04-26 14:44:08 +0000129 // See if they point to different offsets... if so, we may be able to
130 // determine that they do not alias...
131 if (O1 != O2) {
132 if (O2 < O1) { // Ensure that O1 <= O2
133 std::swap(V1, V2);
134 std::swap(O1, O2);
135 std::swap(V1Size, V2Size);
Chris Lattner8105cfb2003-02-03 22:50:46 +0000136 }
Chris Lattnerbac7da82004-04-26 14:44:08 +0000137
Chris Lattner2f72f942005-03-23 01:47:19 +0000138 if (O1+V1Size <= O2)
139 return NoAlias;
Chris Lattner8105cfb2003-02-03 22:50:46 +0000140 }
141 }
142
143 // FIXME: we could improve on this by checking the globals graph for aliased
144 // global queries...
Chris Lattner484e3022004-05-23 21:14:27 +0000145 return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
Chris Lattner8105cfb2003-02-03 22:50:46 +0000146}
Chris Lattner5c70dc02003-06-29 00:54:08 +0000147
Misha Brukmana975a9a2004-03-12 06:14:22 +0000148/// getModRefInfo - does a callsite modify or reference a value?
149///
150AliasAnalysis::ModRefResult
151DSAA::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
Chris Lattner50cb9b42005-03-17 20:16:58 +0000152 AliasAnalysis::ModRefResult Result =AliasAnalysis::getModRefInfo(CS, P, Size);
Misha Brukmana975a9a2004-03-12 06:14:22 +0000153 Function *F = CS.getCalledFunction();
Chris Lattner50cb9b42005-03-17 20:16:58 +0000154
Chris Lattner7d671b82005-03-24 03:04:50 +0000155 if (!F || Result == NoModRef)
Chris Lattner50cb9b42005-03-17 20:16:58 +0000156 return Result;
Misha Brukmana975a9a2004-03-12 06:14:22 +0000157
Chris Lattner7d671b82005-03-24 03:04:50 +0000158 if (F->isExternal()) {
159 // If we are calling an external function, and if this global doesn't escape
160 // the portion of the program we have analyzed, we can draw conclusions
161 // based on whether the global escapes the program.
162 Function *Caller = CS.getInstruction()->getParent()->getParent();
163 DSGraph *G = &TD->getDSGraph(*Caller);
164 DSScalarMap::iterator NI = G->getScalarMap().find(P);
165 if (NI == G->getScalarMap().end()) {
166 // If it wasn't in the local function graph, check the global graph. This
167 // can occur for globals who are locally reference but hoisted out to the
168 // globals graph despite that.
169 G = G->getGlobalsGraph();
170 NI = G->getScalarMap().find(P);
171 if (NI == G->getScalarMap().end())
172 return Result;
173 }
174
175 // If we found a node and it's complete, it cannot be passed out to the
176 // called function.
177 if (NI->second.getNode()->isComplete())
178 return NoModRef;
179 return Result;
180 }
181
Chris Lattner511f60c2005-03-18 00:21:03 +0000182 // Get the graphs for the callee and caller. Note that we want the BU graph
183 // for the callee because we don't want all caller's effects incorporated!
184 const Function *Caller = CS.getInstruction()->getParent()->getParent();
185 DSGraph &CallerTDGraph = TD->getDSGraph(*Caller);
186 DSGraph &CalleeBUGraph = BU->getDSGraph(*F);
Misha Brukmana975a9a2004-03-12 06:14:22 +0000187
Chris Lattner511f60c2005-03-18 00:21:03 +0000188 // Figure out which node in the TD graph this pointer corresponds to.
189 DSScalarMap &CallerSM = CallerTDGraph.getScalarMap();
190 DSScalarMap::iterator NI = CallerSM.find(P);
191 if (NI == CallerSM.end()) {
Chris Lattnerdb7436a2005-03-18 23:18:30 +0000192 if (isa<ConstantPointerNull>(P) || isa<UndefValue>(P))
Chris Lattner511f60c2005-03-18 00:21:03 +0000193 Result = NoModRef; // null is never modified :)
194 else {
Chris Lattner1c8327b2005-03-17 20:33:27 +0000195 assert(isa<GlobalVariable>(P) &&
196 cast<GlobalVariable>(P)->getType()->getElementType()->isFirstClassType() &&
197 "This isn't a global that DSA inconsiderately dropped "
198 "from the graph?");
Chris Lattnerbc499de2005-03-26 22:47:03 +0000199
200 DSGraph &GG = *CallerTDGraph.getGlobalsGraph();
201 DSScalarMap::iterator NI = GG.getScalarMap().find(P);
202 if (NI != GG.getScalarMap().end() && !NI->second.isNull()) {
203 // Otherwise, if the node is only M or R, return this. This can be
204 // useful for globals that should be marked const but are not.
205 DSNode *N = NI->second.getNode();
206 if (!N->isModified())
207 Result = (ModRefResult)(Result & ~Mod);
208 if (!N->isRead())
209 Result = (ModRefResult)(Result & ~Ref);
210 }
Chris Lattner511f60c2005-03-18 00:21:03 +0000211 }
212 return Result;
Chris Lattner50cb9b42005-03-17 20:16:58 +0000213 }
Chris Lattner511f60c2005-03-18 00:21:03 +0000214
215 const DSNode *N = NI->second.getNode();
216 assert(N && "Null pointer in scalar map??");
217
218 // Compute the mapping from nodes in the callee graph to the nodes in the
219 // caller graph for this call site.
220 DSGraph::NodeMapTy CalleeCallerMap;
221 DSCallSite DSCS = CallerTDGraph.getDSCallSiteForCallSite(CS);
222 CallerTDGraph.computeCalleeCallerMapping(DSCS, *F, CalleeBUGraph,
223 CalleeCallerMap);
224
225 // Loop over all of the nodes in the callee that correspond to "N", keeping
226 // track of aggregate mod/ref info.
227 bool NeverReads = true, NeverWrites = true;
228 for (DSGraph::NodeMapTy::iterator I = CalleeCallerMap.begin(),
229 E = CalleeCallerMap.end(); I != E; ++I)
230 if (I->second.getNode() == N) {
231 if (I->first->isModified())
232 NeverWrites = false;
233 if (I->first->isRead())
234 NeverReads = false;
235 if (NeverReads == false && NeverWrites == false)
236 return Result;
237 }
238
239 if (NeverWrites) // We proved it was not modified.
240 Result = ModRefResult(Result & ~Mod);
241 if (NeverReads) // We proved it was not read.
242 Result = ModRefResult(Result & ~Ref);
243
Chris Lattner50cb9b42005-03-17 20:16:58 +0000244 return Result;
Misha Brukmana975a9a2004-03-12 06:14:22 +0000245}