blob: 74fdb1f590f5987b96919167cc3eac9d03dcc8b1 [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
Misha Brukmana975a9a2004-03-12 06:14:22 +000015#include "llvm/Module.h"
16#include "llvm/Analysis/AliasAnalysis.h"
Jeff Cohen1d7b5de2005-01-09 20:42:52 +000017#include "llvm/Analysis/Passes.h"
Chris Lattner4dabb2c2004-07-07 06:32:21 +000018#include "llvm/Analysis/DataStructure/DataStructure.h"
19#include "llvm/Analysis/DataStructure/DSGraph.h"
Chris Lattner9a927292003-11-12 23:11:14 +000020using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000021
Chris Lattner8105cfb2003-02-03 22:50:46 +000022namespace {
Chris Lattnerb12914b2004-09-20 04:48:05 +000023 class DSAA : public ModulePass, public AliasAnalysis {
Chris Lattner8105cfb2003-02-03 22:50:46 +000024 TDDataStructures *TD;
Misha Brukmana975a9a2004-03-12 06:14:22 +000025 BUDataStructures *BU;
Chris Lattner8105cfb2003-02-03 22:50:46 +000026 public:
27 DSAA() : TD(0) {}
28
29 //------------------------------------------------
30 // Implement the Pass API
31 //
32
33 // run - Build up the result graph, representing the pointer graph for the
34 // program.
35 //
Chris Lattnerb12914b2004-09-20 04:48:05 +000036 bool runOnModule(Module &M) {
Chris Lattnera612afc2003-02-26 19:29:36 +000037 InitializeAliasAnalysis(this);
Chris Lattner8105cfb2003-02-03 22:50:46 +000038 TD = &getAnalysis<TDDataStructures>();
Misha Brukmana975a9a2004-03-12 06:14:22 +000039 BU = &getAnalysis<BUDataStructures>();
Chris Lattner8105cfb2003-02-03 22:50:46 +000040 return false;
41 }
42
43 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnera612afc2003-02-26 19:29:36 +000044 AliasAnalysis::getAnalysisUsage(AU);
Misha Brukmana975a9a2004-03-12 06:14:22 +000045 AU.setPreservesAll(); // Does not transform code
46 AU.addRequiredTransitive<TDDataStructures>(); // Uses TD Datastructures
47 AU.addRequiredTransitive<BUDataStructures>(); // Uses BU Datastructures
Chris Lattner8105cfb2003-02-03 22:50:46 +000048 }
49
50 //------------------------------------------------
51 // Implement the AliasAnalysis API
52 //
53
Chris Lattnera612afc2003-02-26 19:29:36 +000054 AliasResult alias(const Value *V1, unsigned V1Size,
55 const Value *V2, unsigned V2Size);
Chris Lattner5c70dc02003-06-29 00:54:08 +000056
57 void getMustAliases(Value *P, std::vector<Value*> &RetVals);
58
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 Lattner5c70dc02003-06-29 00:54:08 +0000101// isSinglePhysicalObject - For now, the only case that we know that there is
102// only one memory object in the node is when there is a single global in the
103// node, and the only composition bit set is Global.
104//
105static bool isSinglePhysicalObject(DSNode *N) {
106 assert(N->isComplete() && "Can only tell if this is a complete object!");
107 return N->isGlobalNode() && N->getGlobals().size() == 1 &&
108 !N->isHeapNode() && !N->isAllocaNode() && !N->isUnknownNode();
109}
110
Chris Lattner8105cfb2003-02-03 22:50:46 +0000111// alias - This is the only method here that does anything interesting...
Chris Lattnera612afc2003-02-26 19:29:36 +0000112AliasAnalysis::AliasResult DSAA::alias(const Value *V1, unsigned V1Size,
113 const Value *V2, unsigned V2Size) {
Chris Lattnerbd92b732003-06-19 21:15:11 +0000114 if (V1 == V2) return MustAlias;
115
Chris Lattner5c70dc02003-06-29 00:54:08 +0000116 DSGraph *G1 = getGraphForValue(V1);
117 DSGraph *G2 = getGraphForValue(V2);
118 assert((!G1 || !G2 || G1 == G2) && "Alias query for 2 different functions?");
Chris Lattner8105cfb2003-02-03 22:50:46 +0000119
Chris Lattner5c70dc02003-06-29 00:54:08 +0000120 // Get the graph to use...
121 DSGraph &G = *(G1 ? G1 : (G2 ? G2 : &TD->getGlobalsGraph()));
Chris Lattnerbd92b732003-06-19 21:15:11 +0000122
Chris Lattner5c70dc02003-06-29 00:54:08 +0000123 const DSGraph::ScalarMapTy &GSM = G.getScalarMap();
124 DSGraph::ScalarMapTy::const_iterator I = GSM.find((Value*)V1);
Chris Lattnerbac7da82004-04-26 14:44:08 +0000125 if (I == GSM.end()) return NoAlias;
Chris Lattner5c70dc02003-06-29 00:54:08 +0000126
Chris Lattnerbac7da82004-04-26 14:44:08 +0000127 assert(I->second.getNode() && "Scalar map points to null node?");
128 DSGraph::ScalarMapTy::const_iterator J = GSM.find((Value*)V2);
129 if (J == GSM.end()) return NoAlias;
130
131 assert(J->second.getNode() && "Scalar map points to null node?");
132
133 DSNode *N1 = I->second.getNode(), *N2 = J->second.getNode();
134 unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset();
Chris Lattnerbd92b732003-06-19 21:15:11 +0000135
Chris Lattnerbac7da82004-04-26 14:44:08 +0000136 // We can only make a judgment of one of the nodes is complete...
137 if (N1->isComplete() || N2->isComplete()) {
138 if (N1 != N2)
139 return NoAlias; // Completely different nodes.
Chris Lattnerbd92b732003-06-19 21:15:11 +0000140
Chris Lattner10c45d62003-07-02 23:56:51 +0000141#if 0 // This does not correctly handle arrays!
Chris Lattnerbac7da82004-04-26 14:44:08 +0000142 // Both point to the same node and same offset, and there is only one
143 // physical memory object represented in the node, return must alias.
144 //
145 // FIXME: This isn't correct because we do not handle array indexing
146 // correctly.
Chris Lattnerbd92b732003-06-19 21:15:11 +0000147
Chris Lattnerbac7da82004-04-26 14:44:08 +0000148 if (O1 == O2 && isSinglePhysicalObject(N1))
149 return MustAlias; // Exactly the same object & offset
Chris Lattner10c45d62003-07-02 23:56:51 +0000150#endif
Chris Lattnerbd92b732003-06-19 21:15:11 +0000151
Chris Lattnerbac7da82004-04-26 14:44:08 +0000152 // See if they point to different offsets... if so, we may be able to
153 // determine that they do not alias...
154 if (O1 != O2) {
155 if (O2 < O1) { // Ensure that O1 <= O2
156 std::swap(V1, V2);
157 std::swap(O1, O2);
158 std::swap(V1Size, V2Size);
Chris Lattner8105cfb2003-02-03 22:50:46 +0000159 }
Chris Lattnerbac7da82004-04-26 14:44:08 +0000160
161 // FIXME: This is not correct because we do not handle array
162 // indexing correctly with this check!
163 //if (O1+V1Size <= O2) return NoAlias;
Chris Lattner8105cfb2003-02-03 22:50:46 +0000164 }
165 }
166
167 // FIXME: we could improve on this by checking the globals graph for aliased
168 // global queries...
Chris Lattner484e3022004-05-23 21:14:27 +0000169 return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
Chris Lattner8105cfb2003-02-03 22:50:46 +0000170}
Chris Lattner5c70dc02003-06-29 00:54:08 +0000171
Misha Brukmana975a9a2004-03-12 06:14:22 +0000172/// getModRefInfo - does a callsite modify or reference a value?
173///
174AliasAnalysis::ModRefResult
175DSAA::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
176 Function *F = CS.getCalledFunction();
Chris Lattner2e2cce62005-03-17 19:56:18 +0000177 if (!F || F->isExternal())
178 return AliasAnalysis::getModRefInfo(CS, P, Size);
Misha Brukmana975a9a2004-03-12 06:14:22 +0000179
180 // Clone the function TD graph, clearing off Mod/Ref flags
181 const Function *csParent = CS.getInstruction()->getParent()->getParent();
182 DSGraph TDGraph(TD->getDSGraph(*csParent));
Chris Lattner2e2cce62005-03-17 19:56:18 +0000183 TDGraph.maskNodeTypes(~(DSNode::Modified|DSNode::Read));
Misha Brukmana975a9a2004-03-12 06:14:22 +0000184
185 // Insert the callee's BU graph into the TD graph
186 const DSGraph &BUGraph = BU->getDSGraph(*F);
187 TDGraph.mergeInGraph(TDGraph.getDSCallSiteForCallSite(CS),
188 *F, BUGraph, 0);
189
190 // Report the flags that have been added
191 const DSNodeHandle &DSH = TDGraph.getNodeForValue(P);
192 if (const DSNode *N = DSH.getNode())
193 if (N->isModified())
194 return N->isRead() ? ModRef : Mod;
195 else
196 return N->isRead() ? Ref : NoModRef;
197 return NoModRef;
198}
199
Chris Lattner5c70dc02003-06-29 00:54:08 +0000200
201/// getMustAliases - If there are any pointers known that must alias this
202/// pointer, return them now. This allows alias-set based alias analyses to
203/// perform a form a value numbering (which is exposed by load-vn). If an alias
204/// analysis supports this, it should ADD any must aliased pointers to the
205/// specified vector.
206///
207void DSAA::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
Chris Lattner10c45d62003-07-02 23:56:51 +0000208#if 0 // This does not correctly handle arrays!
Chris Lattner9cd04842003-07-02 04:39:13 +0000209 // Currently the only must alias information we can provide is to say that
210 // something is equal to a global value. If we already have a global value,
211 // don't get worked up about it.
212 if (!isa<GlobalValue>(P)) {
213 DSGraph *G = getGraphForValue(P);
214 if (!G) G = &TD->getGlobalsGraph();
215
216 // The only must alias information we can currently determine occurs when
217 // the node for P is a global node with only one entry.
Chris Lattner02da0322004-01-27 21:51:19 +0000218 DSGraph::ScalarMapTy::const_iterator I = G->getScalarMap().find(P);
219 if (I != G->getScalarMap().end()) {
Chris Lattner9cd04842003-07-02 04:39:13 +0000220 DSNode *N = I->second.getNode();
221 if (N->isComplete() && isSinglePhysicalObject(N))
222 RetVals.push_back(N->getGlobals()[0]);
223 }
Chris Lattner5c70dc02003-06-29 00:54:08 +0000224 }
Chris Lattner10c45d62003-07-02 23:56:51 +0000225#endif
Chris Lattner484e3022004-05-23 21:14:27 +0000226 return AliasAnalysis::getMustAliases(P, RetVals);
Chris Lattner5c70dc02003-06-29 00:54:08 +0000227}
Brian Gaeked0fde302003-11-11 22:41:34 +0000228