blob: 0400e01d63a7467cac3dc3baa92b241bab6d33b2 [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
59 void getMustAliases(Value *P, std::vector<Value*> &RetVals);
60
Chris Lattner484e3022004-05-23 21:14:27 +000061 ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
Reid Spencer4a7ebfa2004-12-07 08:11:24 +000062 ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
63 return AliasAnalysis::getModRefInfo(CS1,CS2);
64 }
Chris Lattner92e41d52004-01-30 22:20:55 +000065
Chris Lattner851b5342005-01-24 20:00:14 +000066 virtual void deleteValue(Value *V) {
67 BU->deleteValue(V);
68 TD->deleteValue(V);
69 }
70
71 virtual void copyValue(Value *From, Value *To) {
72 if (From == To) return;
73 BU->copyValue(From, To);
74 TD->copyValue(From, To);
75 }
76
Chris Lattner5c70dc02003-06-29 00:54:08 +000077 private:
78 DSGraph *getGraphForValue(const Value *V);
Chris Lattner8105cfb2003-02-03 22:50:46 +000079 };
80
81 // Register the pass...
82 RegisterOpt<DSAA> X("ds-aa", "Data Structure Graph Based Alias Analysis");
83
84 // Register as an implementation of AliasAnalysis
85 RegisterAnalysisGroup<AliasAnalysis, DSAA> Y;
86}
87
Jeff Cohen1d7b5de2005-01-09 20:42:52 +000088ModulePass *llvm::createDSAAPass() { return new DSAA(); }
89
Chris Lattner5c70dc02003-06-29 00:54:08 +000090// getGraphForValue - Return the DSGraph to use for queries about the specified
91// value...
Chris Lattner8105cfb2003-02-03 22:50:46 +000092//
Chris Lattner5c70dc02003-06-29 00:54:08 +000093DSGraph *DSAA::getGraphForValue(const Value *V) {
Chris Lattner8105cfb2003-02-03 22:50:46 +000094 if (const Instruction *I = dyn_cast<Instruction>(V))
Chris Lattner5c70dc02003-06-29 00:54:08 +000095 return &TD->getDSGraph(*I->getParent()->getParent());
Chris Lattner8105cfb2003-02-03 22:50:46 +000096 else if (const Argument *A = dyn_cast<Argument>(V))
Chris Lattner5c70dc02003-06-29 00:54:08 +000097 return &TD->getDSGraph(*A->getParent());
Chris Lattner8105cfb2003-02-03 22:50:46 +000098 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattner5c70dc02003-06-29 00:54:08 +000099 return &TD->getDSGraph(*BB->getParent());
Chris Lattner8105cfb2003-02-03 22:50:46 +0000100 return 0;
101}
102
Chris Lattner1cee7792005-03-20 02:14:15 +0000103#if 0
Chris Lattner5c70dc02003-06-29 00:54:08 +0000104// isSinglePhysicalObject - For now, the only case that we know that there is
105// only one memory object in the node is when there is a single global in the
106// node, and the only composition bit set is Global.
107//
108static bool isSinglePhysicalObject(DSNode *N) {
109 assert(N->isComplete() && "Can only tell if this is a complete object!");
110 return N->isGlobalNode() && N->getGlobals().size() == 1 &&
111 !N->isHeapNode() && !N->isAllocaNode() && !N->isUnknownNode();
112}
Chris Lattner1cee7792005-03-20 02:14:15 +0000113#endif
Chris Lattner5c70dc02003-06-29 00:54:08 +0000114
Chris Lattner8105cfb2003-02-03 22:50:46 +0000115// alias - This is the only method here that does anything interesting...
Chris Lattnera612afc2003-02-26 19:29:36 +0000116AliasAnalysis::AliasResult DSAA::alias(const Value *V1, unsigned V1Size,
117 const Value *V2, unsigned V2Size) {
Chris Lattnerbd92b732003-06-19 21:15:11 +0000118 if (V1 == V2) return MustAlias;
119
Chris Lattner5c70dc02003-06-29 00:54:08 +0000120 DSGraph *G1 = getGraphForValue(V1);
121 DSGraph *G2 = getGraphForValue(V2);
122 assert((!G1 || !G2 || G1 == G2) && "Alias query for 2 different functions?");
Chris Lattner8105cfb2003-02-03 22:50:46 +0000123
Chris Lattner5c70dc02003-06-29 00:54:08 +0000124 // Get the graph to use...
125 DSGraph &G = *(G1 ? G1 : (G2 ? G2 : &TD->getGlobalsGraph()));
Chris Lattnerbd92b732003-06-19 21:15:11 +0000126
Chris Lattner5c70dc02003-06-29 00:54:08 +0000127 const DSGraph::ScalarMapTy &GSM = G.getScalarMap();
128 DSGraph::ScalarMapTy::const_iterator I = GSM.find((Value*)V1);
Chris Lattnerbac7da82004-04-26 14:44:08 +0000129 if (I == GSM.end()) return NoAlias;
Chris Lattner511f60c2005-03-18 00:21:03 +0000130
Chris Lattnerbac7da82004-04-26 14:44:08 +0000131 DSGraph::ScalarMapTy::const_iterator J = GSM.find((Value*)V2);
132 if (J == GSM.end()) return NoAlias;
133
Chris Lattnerbac7da82004-04-26 14:44:08 +0000134 DSNode *N1 = I->second.getNode(), *N2 = J->second.getNode();
135 unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset();
Chris Lattner511f60c2005-03-18 00:21:03 +0000136 if (N1 == 0 || N2 == 0)
137 return MayAlias; // Can't tell whether anything aliases null.
Chris Lattnerbd92b732003-06-19 21:15:11 +0000138
Chris Lattnerbac7da82004-04-26 14:44:08 +0000139 // We can only make a judgment of one of the nodes is complete...
140 if (N1->isComplete() || N2->isComplete()) {
141 if (N1 != N2)
142 return NoAlias; // Completely different nodes.
Chris Lattnerbd92b732003-06-19 21:15:11 +0000143
Chris Lattner10c45d62003-07-02 23:56:51 +0000144#if 0 // This does not correctly handle arrays!
Chris Lattnerbac7da82004-04-26 14:44:08 +0000145 // Both point to the same node and same offset, and there is only one
146 // physical memory object represented in the node, return must alias.
147 //
148 // FIXME: This isn't correct because we do not handle array indexing
149 // correctly.
Chris Lattnerbd92b732003-06-19 21:15:11 +0000150
Chris Lattnerbac7da82004-04-26 14:44:08 +0000151 if (O1 == O2 && isSinglePhysicalObject(N1))
152 return MustAlias; // Exactly the same object & offset
Chris Lattner10c45d62003-07-02 23:56:51 +0000153#endif
Chris Lattnerbd92b732003-06-19 21:15:11 +0000154
Chris Lattnerbac7da82004-04-26 14:44:08 +0000155 // See if they point to different offsets... if so, we may be able to
156 // determine that they do not alias...
157 if (O1 != O2) {
158 if (O2 < O1) { // Ensure that O1 <= O2
159 std::swap(V1, V2);
160 std::swap(O1, O2);
161 std::swap(V1Size, V2Size);
Chris Lattner8105cfb2003-02-03 22:50:46 +0000162 }
Chris Lattnerbac7da82004-04-26 14:44:08 +0000163
164 // FIXME: This is not correct because we do not handle array
165 // indexing correctly with this check!
166 //if (O1+V1Size <= O2) return NoAlias;
Chris Lattner8105cfb2003-02-03 22:50:46 +0000167 }
168 }
169
170 // FIXME: we could improve on this by checking the globals graph for aliased
171 // global queries...
Chris Lattner484e3022004-05-23 21:14:27 +0000172 return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
Chris Lattner8105cfb2003-02-03 22:50:46 +0000173}
Chris Lattner5c70dc02003-06-29 00:54:08 +0000174
Misha Brukmana975a9a2004-03-12 06:14:22 +0000175/// getModRefInfo - does a callsite modify or reference a value?
176///
177AliasAnalysis::ModRefResult
178DSAA::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
Chris Lattner50cb9b42005-03-17 20:16:58 +0000179 AliasAnalysis::ModRefResult Result =AliasAnalysis::getModRefInfo(CS, P, Size);
Misha Brukmana975a9a2004-03-12 06:14:22 +0000180 Function *F = CS.getCalledFunction();
Chris Lattner50cb9b42005-03-17 20:16:58 +0000181
182 if (!F || F->isExternal() || Result == NoModRef)
183 return Result;
Misha Brukmana975a9a2004-03-12 06:14:22 +0000184
Chris Lattner511f60c2005-03-18 00:21:03 +0000185 // Get the graphs for the callee and caller. Note that we want the BU graph
186 // for the callee because we don't want all caller's effects incorporated!
187 const Function *Caller = CS.getInstruction()->getParent()->getParent();
188 DSGraph &CallerTDGraph = TD->getDSGraph(*Caller);
189 DSGraph &CalleeBUGraph = BU->getDSGraph(*F);
Misha Brukmana975a9a2004-03-12 06:14:22 +0000190
Chris Lattner511f60c2005-03-18 00:21:03 +0000191 // Figure out which node in the TD graph this pointer corresponds to.
192 DSScalarMap &CallerSM = CallerTDGraph.getScalarMap();
193 DSScalarMap::iterator NI = CallerSM.find(P);
194 if (NI == CallerSM.end()) {
Chris Lattnerdb7436a2005-03-18 23:18:30 +0000195 if (isa<ConstantPointerNull>(P) || isa<UndefValue>(P))
Chris Lattner511f60c2005-03-18 00:21:03 +0000196 Result = NoModRef; // null is never modified :)
197 else {
Chris Lattner1c8327b2005-03-17 20:33:27 +0000198 assert(isa<GlobalVariable>(P) &&
199 cast<GlobalVariable>(P)->getType()->getElementType()->isFirstClassType() &&
200 "This isn't a global that DSA inconsiderately dropped "
201 "from the graph?");
Chris Lattner511f60c2005-03-18 00:21:03 +0000202 }
203 return Result;
Chris Lattner50cb9b42005-03-17 20:16:58 +0000204 }
Chris Lattner511f60c2005-03-18 00:21:03 +0000205
206 const DSNode *N = NI->second.getNode();
207 assert(N && "Null pointer in scalar map??");
208
209 // Compute the mapping from nodes in the callee graph to the nodes in the
210 // caller graph for this call site.
211 DSGraph::NodeMapTy CalleeCallerMap;
212 DSCallSite DSCS = CallerTDGraph.getDSCallSiteForCallSite(CS);
213 CallerTDGraph.computeCalleeCallerMapping(DSCS, *F, CalleeBUGraph,
214 CalleeCallerMap);
215
216 // Loop over all of the nodes in the callee that correspond to "N", keeping
217 // track of aggregate mod/ref info.
218 bool NeverReads = true, NeverWrites = true;
219 for (DSGraph::NodeMapTy::iterator I = CalleeCallerMap.begin(),
220 E = CalleeCallerMap.end(); I != E; ++I)
221 if (I->second.getNode() == N) {
222 if (I->first->isModified())
223 NeverWrites = false;
224 if (I->first->isRead())
225 NeverReads = false;
226 if (NeverReads == false && NeverWrites == false)
227 return Result;
228 }
229
230 if (NeverWrites) // We proved it was not modified.
231 Result = ModRefResult(Result & ~Mod);
232 if (NeverReads) // We proved it was not read.
233 Result = ModRefResult(Result & ~Ref);
234
Chris Lattner50cb9b42005-03-17 20:16:58 +0000235 return Result;
Misha Brukmana975a9a2004-03-12 06:14:22 +0000236}
237
Chris Lattner5c70dc02003-06-29 00:54:08 +0000238
239/// getMustAliases - If there are any pointers known that must alias this
240/// pointer, return them now. This allows alias-set based alias analyses to
241/// perform a form a value numbering (which is exposed by load-vn). If an alias
242/// analysis supports this, it should ADD any must aliased pointers to the
243/// specified vector.
244///
245void DSAA::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
Chris Lattner10c45d62003-07-02 23:56:51 +0000246#if 0 // This does not correctly handle arrays!
Chris Lattner9cd04842003-07-02 04:39:13 +0000247 // Currently the only must alias information we can provide is to say that
248 // something is equal to a global value. If we already have a global value,
249 // don't get worked up about it.
250 if (!isa<GlobalValue>(P)) {
251 DSGraph *G = getGraphForValue(P);
252 if (!G) G = &TD->getGlobalsGraph();
253
254 // The only must alias information we can currently determine occurs when
255 // the node for P is a global node with only one entry.
Chris Lattner02da0322004-01-27 21:51:19 +0000256 DSGraph::ScalarMapTy::const_iterator I = G->getScalarMap().find(P);
257 if (I != G->getScalarMap().end()) {
Chris Lattner9cd04842003-07-02 04:39:13 +0000258 DSNode *N = I->second.getNode();
259 if (N->isComplete() && isSinglePhysicalObject(N))
260 RetVals.push_back(N->getGlobals()[0]);
261 }
Chris Lattner5c70dc02003-06-29 00:54:08 +0000262 }
Chris Lattner10c45d62003-07-02 23:56:51 +0000263#endif
Chris Lattner484e3022004-05-23 21:14:27 +0000264 return AliasAnalysis::getMustAliases(P, RetVals);
Chris Lattner5c70dc02003-06-29 00:54:08 +0000265}
Brian Gaeked0fde302003-11-11 22:41:34 +0000266