blob: 99b57266329ff95b63cdb1205d6df697a0bfbe9c [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"
Chris Lattner8105cfb2003-02-03 22:50:46 +000017#include "llvm/Analysis/DataStructure.h"
18#include "llvm/Analysis/DSGraph.h"
Chris Lattner9a927292003-11-12 23:11:14 +000019using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000020
Chris Lattner8105cfb2003-02-03 22:50:46 +000021namespace {
22 class DSAA : public Pass, public AliasAnalysis {
23 TDDataStructures *TD;
Misha Brukmana975a9a2004-03-12 06:14:22 +000024 BUDataStructures *BU;
Chris Lattner8105cfb2003-02-03 22:50:46 +000025 public:
26 DSAA() : TD(0) {}
27
28 //------------------------------------------------
29 // Implement the Pass API
30 //
31
32 // run - Build up the result graph, representing the pointer graph for the
33 // program.
34 //
35 bool run(Module &M) {
Chris Lattnera612afc2003-02-26 19:29:36 +000036 InitializeAliasAnalysis(this);
Chris Lattner8105cfb2003-02-03 22:50:46 +000037 TD = &getAnalysis<TDDataStructures>();
Misha Brukmana975a9a2004-03-12 06:14:22 +000038 BU = &getAnalysis<BUDataStructures>();
Chris Lattner8105cfb2003-02-03 22:50:46 +000039 return false;
40 }
41
42 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnera612afc2003-02-26 19:29:36 +000043 AliasAnalysis::getAnalysisUsage(AU);
Misha Brukmana975a9a2004-03-12 06:14:22 +000044 AU.setPreservesAll(); // Does not transform code
45 AU.addRequiredTransitive<TDDataStructures>(); // Uses TD Datastructures
46 AU.addRequiredTransitive<BUDataStructures>(); // Uses BU Datastructures
47 AU.addRequired<AliasAnalysis>(); // Chains to another AA impl
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 Lattner92e41d52004-01-30 22:20:55 +000059 bool pointsToConstantMemory(const Value *P) {
60 return getAnalysis<AliasAnalysis>().pointsToConstantMemory(P);
61 }
Misha Brukmana975a9a2004-03-12 06:14:22 +000062
63 AliasAnalysis::ModRefResult
64 getModRefInfo(CallSite CS, Value *P, unsigned Size);
Chris Lattner92e41d52004-01-30 22:20:55 +000065
Chris Lattner5c70dc02003-06-29 00:54:08 +000066 private:
67 DSGraph *getGraphForValue(const Value *V);
Chris Lattner8105cfb2003-02-03 22:50:46 +000068 };
69
70 // Register the pass...
71 RegisterOpt<DSAA> X("ds-aa", "Data Structure Graph Based Alias Analysis");
72
73 // Register as an implementation of AliasAnalysis
74 RegisterAnalysisGroup<AliasAnalysis, DSAA> Y;
75}
76
Chris Lattner5c70dc02003-06-29 00:54:08 +000077// getGraphForValue - Return the DSGraph to use for queries about the specified
78// value...
Chris Lattner8105cfb2003-02-03 22:50:46 +000079//
Chris Lattner5c70dc02003-06-29 00:54:08 +000080DSGraph *DSAA::getGraphForValue(const Value *V) {
Chris Lattner8105cfb2003-02-03 22:50:46 +000081 if (const Instruction *I = dyn_cast<Instruction>(V))
Chris Lattner5c70dc02003-06-29 00:54:08 +000082 return &TD->getDSGraph(*I->getParent()->getParent());
Chris Lattner8105cfb2003-02-03 22:50:46 +000083 else if (const Argument *A = dyn_cast<Argument>(V))
Chris Lattner5c70dc02003-06-29 00:54:08 +000084 return &TD->getDSGraph(*A->getParent());
Chris Lattner8105cfb2003-02-03 22:50:46 +000085 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattner5c70dc02003-06-29 00:54:08 +000086 return &TD->getDSGraph(*BB->getParent());
Chris Lattner8105cfb2003-02-03 22:50:46 +000087 return 0;
88}
89
Chris Lattner5c70dc02003-06-29 00:54:08 +000090// isSinglePhysicalObject - For now, the only case that we know that there is
91// only one memory object in the node is when there is a single global in the
92// node, and the only composition bit set is Global.
93//
94static bool isSinglePhysicalObject(DSNode *N) {
95 assert(N->isComplete() && "Can only tell if this is a complete object!");
96 return N->isGlobalNode() && N->getGlobals().size() == 1 &&
97 !N->isHeapNode() && !N->isAllocaNode() && !N->isUnknownNode();
98}
99
Chris Lattner8105cfb2003-02-03 22:50:46 +0000100// alias - This is the only method here that does anything interesting...
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 Lattner5c70dc02003-06-29 00:54:08 +0000115
Chris Lattnerbac7da82004-04-26 14:44:08 +0000116 assert(I->second.getNode() && "Scalar map points to null node?");
117 DSGraph::ScalarMapTy::const_iterator J = GSM.find((Value*)V2);
118 if (J == GSM.end()) return NoAlias;
119
120 assert(J->second.getNode() && "Scalar map points to null node?");
121
122 DSNode *N1 = I->second.getNode(), *N2 = J->second.getNode();
123 unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset();
Chris Lattnerbd92b732003-06-19 21:15:11 +0000124
Chris Lattnerbac7da82004-04-26 14:44:08 +0000125 // We can only make a judgment of one of the nodes is complete...
126 if (N1->isComplete() || N2->isComplete()) {
127 if (N1 != N2)
128 return NoAlias; // Completely different nodes.
Chris Lattnerbd92b732003-06-19 21:15:11 +0000129
Chris Lattner10c45d62003-07-02 23:56:51 +0000130#if 0 // This does not correctly handle arrays!
Chris Lattnerbac7da82004-04-26 14:44:08 +0000131 // Both point to the same node and same offset, and there is only one
132 // physical memory object represented in the node, return must alias.
133 //
134 // FIXME: This isn't correct because we do not handle array indexing
135 // correctly.
Chris Lattnerbd92b732003-06-19 21:15:11 +0000136
Chris Lattnerbac7da82004-04-26 14:44:08 +0000137 if (O1 == O2 && isSinglePhysicalObject(N1))
138 return MustAlias; // Exactly the same object & offset
Chris Lattner10c45d62003-07-02 23:56:51 +0000139#endif
Chris Lattnerbd92b732003-06-19 21:15:11 +0000140
Chris Lattnerbac7da82004-04-26 14:44:08 +0000141 // See if they point to different offsets... if so, we may be able to
142 // determine that they do not alias...
143 if (O1 != O2) {
144 if (O2 < O1) { // Ensure that O1 <= O2
145 std::swap(V1, V2);
146 std::swap(O1, O2);
147 std::swap(V1Size, V2Size);
Chris Lattner8105cfb2003-02-03 22:50:46 +0000148 }
Chris Lattnerbac7da82004-04-26 14:44:08 +0000149
150 // FIXME: This is not correct because we do not handle array
151 // indexing correctly with this check!
152 //if (O1+V1Size <= O2) return NoAlias;
Chris Lattner8105cfb2003-02-03 22:50:46 +0000153 }
154 }
155
156 // FIXME: we could improve on this by checking the globals graph for aliased
157 // global queries...
Chris Lattnera612afc2003-02-26 19:29:36 +0000158 return getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size);
Chris Lattner8105cfb2003-02-03 22:50:46 +0000159}
Chris Lattner5c70dc02003-06-29 00:54:08 +0000160
Misha Brukmana975a9a2004-03-12 06:14:22 +0000161/// getModRefInfo - does a callsite modify or reference a value?
162///
163AliasAnalysis::ModRefResult
164DSAA::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
165 Function *F = CS.getCalledFunction();
166 if (!F) return pointsToConstantMemory(P) ? Ref : ModRef;
167 if (F->isExternal()) return ModRef;
168
169 // Clone the function TD graph, clearing off Mod/Ref flags
170 const Function *csParent = CS.getInstruction()->getParent()->getParent();
171 DSGraph TDGraph(TD->getDSGraph(*csParent));
172 TDGraph.maskNodeTypes(0);
173
174 // Insert the callee's BU graph into the TD graph
175 const DSGraph &BUGraph = BU->getDSGraph(*F);
176 TDGraph.mergeInGraph(TDGraph.getDSCallSiteForCallSite(CS),
177 *F, BUGraph, 0);
178
179 // Report the flags that have been added
180 const DSNodeHandle &DSH = TDGraph.getNodeForValue(P);
181 if (const DSNode *N = DSH.getNode())
182 if (N->isModified())
183 return N->isRead() ? ModRef : Mod;
184 else
185 return N->isRead() ? Ref : NoModRef;
186 return NoModRef;
187}
188
Chris Lattner5c70dc02003-06-29 00:54:08 +0000189
190/// getMustAliases - If there are any pointers known that must alias this
191/// pointer, return them now. This allows alias-set based alias analyses to
192/// perform a form a value numbering (which is exposed by load-vn). If an alias
193/// analysis supports this, it should ADD any must aliased pointers to the
194/// specified vector.
195///
196void DSAA::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
Chris Lattner10c45d62003-07-02 23:56:51 +0000197#if 0 // This does not correctly handle arrays!
Chris Lattner9cd04842003-07-02 04:39:13 +0000198 // Currently the only must alias information we can provide is to say that
199 // something is equal to a global value. If we already have a global value,
200 // don't get worked up about it.
201 if (!isa<GlobalValue>(P)) {
202 DSGraph *G = getGraphForValue(P);
203 if (!G) G = &TD->getGlobalsGraph();
204
205 // The only must alias information we can currently determine occurs when
206 // the node for P is a global node with only one entry.
Chris Lattner02da0322004-01-27 21:51:19 +0000207 DSGraph::ScalarMapTy::const_iterator I = G->getScalarMap().find(P);
208 if (I != G->getScalarMap().end()) {
Chris Lattner9cd04842003-07-02 04:39:13 +0000209 DSNode *N = I->second.getNode();
210 if (N->isComplete() && isSinglePhysicalObject(N))
211 RetVals.push_back(N->getGlobals()[0]);
212 }
Chris Lattner5c70dc02003-06-29 00:54:08 +0000213 }
Chris Lattner10c45d62003-07-02 23:56:51 +0000214#endif
Chris Lattner5c70dc02003-06-29 00:54:08 +0000215 return getAnalysis<AliasAnalysis>().getMustAliases(P, RetVals);
216}
Brian Gaeked0fde302003-11-11 22:41:34 +0000217