blob: 51df03306f775ad99428c91be904106a06eaec4c [file] [log] [blame]
Chris Lattner8105cfb2003-02-03 22:50:46 +00001//===- DataStructureAA.cpp - Data Structure Based Alias Analysis ----------===//
2//
3// This pass uses the top-down data structure graphs to implement a simple
4// context sensitive alias analysis.
5//
6//===----------------------------------------------------------------------===//
7
8#include "llvm/Analysis/DataStructure.h"
9#include "llvm/Analysis/DSGraph.h"
10#include "llvm/Analysis/AliasAnalysis.h"
11#include "llvm/Module.h"
Chris Lattner8105cfb2003-02-03 22:50:46 +000012
13namespace {
14 class DSAA : public Pass, public AliasAnalysis {
15 TDDataStructures *TD;
16 public:
17 DSAA() : TD(0) {}
18
19 //------------------------------------------------
20 // Implement the Pass API
21 //
22
23 // run - Build up the result graph, representing the pointer graph for the
24 // program.
25 //
26 bool run(Module &M) {
Chris Lattnera612afc2003-02-26 19:29:36 +000027 InitializeAliasAnalysis(this);
Chris Lattner8105cfb2003-02-03 22:50:46 +000028 TD = &getAnalysis<TDDataStructures>();
29 return false;
30 }
31
32 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnera612afc2003-02-26 19:29:36 +000033 AliasAnalysis::getAnalysisUsage(AU);
Chris Lattner8105cfb2003-02-03 22:50:46 +000034 AU.setPreservesAll(); // Does not transform code...
35 AU.addRequired<TDDataStructures>(); // Uses TD Datastructures
36 AU.addRequired<AliasAnalysis>(); // Chains to another AA impl...
37 }
38
39 //------------------------------------------------
40 // Implement the AliasAnalysis API
41 //
42
Chris Lattnera612afc2003-02-26 19:29:36 +000043 AliasResult alias(const Value *V1, unsigned V1Size,
44 const Value *V2, unsigned V2Size);
Chris Lattner5c70dc02003-06-29 00:54:08 +000045
46 void getMustAliases(Value *P, std::vector<Value*> &RetVals);
47
48 private:
49 DSGraph *getGraphForValue(const Value *V);
Chris Lattner8105cfb2003-02-03 22:50:46 +000050 };
51
52 // Register the pass...
53 RegisterOpt<DSAA> X("ds-aa", "Data Structure Graph Based Alias Analysis");
54
55 // Register as an implementation of AliasAnalysis
56 RegisterAnalysisGroup<AliasAnalysis, DSAA> Y;
57}
58
Chris Lattner5c70dc02003-06-29 00:54:08 +000059// getGraphForValue - Return the DSGraph to use for queries about the specified
60// value...
Chris Lattner8105cfb2003-02-03 22:50:46 +000061//
Chris Lattner5c70dc02003-06-29 00:54:08 +000062DSGraph *DSAA::getGraphForValue(const Value *V) {
Chris Lattner8105cfb2003-02-03 22:50:46 +000063 if (const Instruction *I = dyn_cast<Instruction>(V))
Chris Lattner5c70dc02003-06-29 00:54:08 +000064 return &TD->getDSGraph(*I->getParent()->getParent());
Chris Lattner8105cfb2003-02-03 22:50:46 +000065 else if (const Argument *A = dyn_cast<Argument>(V))
Chris Lattner5c70dc02003-06-29 00:54:08 +000066 return &TD->getDSGraph(*A->getParent());
Chris Lattner8105cfb2003-02-03 22:50:46 +000067 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
Chris Lattner5c70dc02003-06-29 00:54:08 +000068 return &TD->getDSGraph(*BB->getParent());
Chris Lattner8105cfb2003-02-03 22:50:46 +000069 return 0;
70}
71
Chris Lattner5c70dc02003-06-29 00:54:08 +000072// isSinglePhysicalObject - For now, the only case that we know that there is
73// only one memory object in the node is when there is a single global in the
74// node, and the only composition bit set is Global.
75//
76static bool isSinglePhysicalObject(DSNode *N) {
77 assert(N->isComplete() && "Can only tell if this is a complete object!");
78 return N->isGlobalNode() && N->getGlobals().size() == 1 &&
79 !N->isHeapNode() && !N->isAllocaNode() && !N->isUnknownNode();
80}
81
Chris Lattner8105cfb2003-02-03 22:50:46 +000082// alias - This is the only method here that does anything interesting...
Chris Lattnera612afc2003-02-26 19:29:36 +000083AliasAnalysis::AliasResult DSAA::alias(const Value *V1, unsigned V1Size,
84 const Value *V2, unsigned V2Size) {
Chris Lattnerbd92b732003-06-19 21:15:11 +000085 if (V1 == V2) return MustAlias;
86
Chris Lattner5c70dc02003-06-29 00:54:08 +000087 DSGraph *G1 = getGraphForValue(V1);
88 DSGraph *G2 = getGraphForValue(V2);
89 assert((!G1 || !G2 || G1 == G2) && "Alias query for 2 different functions?");
Chris Lattner8105cfb2003-02-03 22:50:46 +000090
Chris Lattner5c70dc02003-06-29 00:54:08 +000091 // Get the graph to use...
92 DSGraph &G = *(G1 ? G1 : (G2 ? G2 : &TD->getGlobalsGraph()));
Chris Lattnerbd92b732003-06-19 21:15:11 +000093
Chris Lattner5c70dc02003-06-29 00:54:08 +000094 const DSGraph::ScalarMapTy &GSM = G.getScalarMap();
95 DSGraph::ScalarMapTy::const_iterator I = GSM.find((Value*)V1);
96 if (I != GSM.end()) {
97 assert(I->second.getNode() && "Scalar map points to null node?");
98 DSGraph::ScalarMapTy::const_iterator J = GSM.find((Value*)V2);
99 if (J != GSM.end()) {
100 assert(J->second.getNode() && "Scalar map points to null node?");
101
102 DSNode *N1 = I->second.getNode(), *N2 = J->second.getNode();
103 unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset();
Chris Lattnerbd92b732003-06-19 21:15:11 +0000104
Misha Brukman2f2d0652003-09-11 18:14:24 +0000105 // We can only make a judgment of one of the nodes is complete...
Chris Lattner5c70dc02003-06-29 00:54:08 +0000106 if (N1->isComplete() || N2->isComplete()) {
107 if (N1 != N2)
108 return NoAlias; // Completely different nodes.
Chris Lattnerbd92b732003-06-19 21:15:11 +0000109
Chris Lattner10c45d62003-07-02 23:56:51 +0000110#if 0 // This does not correctly handle arrays!
Chris Lattner5c70dc02003-06-29 00:54:08 +0000111 // Both point to the same node and same offset, and there is only one
112 // physical memory object represented in the node, return must alias.
113 //
114 // FIXME: This isn't correct because we do not handle array indexing
115 // correctly.
Chris Lattnerbd92b732003-06-19 21:15:11 +0000116
Chris Lattner5c70dc02003-06-29 00:54:08 +0000117 if (O1 == O2 && isSinglePhysicalObject(N1))
118 return MustAlias; // Exactly the same object & offset
Chris Lattner10c45d62003-07-02 23:56:51 +0000119#endif
Chris Lattnerbd92b732003-06-19 21:15:11 +0000120
Chris Lattner5c70dc02003-06-29 00:54:08 +0000121 // See if they point to different offsets... if so, we may be able to
122 // determine that they do not alias...
123 if (O1 != O2) {
124 if (O2 < O1) { // Ensure that O1 <= O2
125 std::swap(V1, V2);
126 std::swap(O1, O2);
127 std::swap(V1Size, V2Size);
Chris Lattnerbd92b732003-06-19 21:15:11 +0000128 }
Chris Lattner5c70dc02003-06-29 00:54:08 +0000129
130 // FIXME: This is not correct because we do not handle array
131 // indexing correctly with this check!
132 //if (O1+V1Size <= O2) return NoAlias;
Chris Lattner8105cfb2003-02-03 22:50:46 +0000133 }
134 }
135 }
136 }
137
138 // FIXME: we could improve on this by checking the globals graph for aliased
139 // global queries...
Chris Lattnera612afc2003-02-26 19:29:36 +0000140 return getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size);
Chris Lattner8105cfb2003-02-03 22:50:46 +0000141}
Chris Lattner5c70dc02003-06-29 00:54:08 +0000142
143
144/// getMustAliases - If there are any pointers known that must alias this
145/// pointer, return them now. This allows alias-set based alias analyses to
146/// perform a form a value numbering (which is exposed by load-vn). If an alias
147/// analysis supports this, it should ADD any must aliased pointers to the
148/// specified vector.
149///
150void DSAA::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
Chris Lattner10c45d62003-07-02 23:56:51 +0000151#if 0 // This does not correctly handle arrays!
Chris Lattner9cd04842003-07-02 04:39:13 +0000152 // Currently the only must alias information we can provide is to say that
153 // something is equal to a global value. If we already have a global value,
154 // don't get worked up about it.
155 if (!isa<GlobalValue>(P)) {
156 DSGraph *G = getGraphForValue(P);
157 if (!G) G = &TD->getGlobalsGraph();
158
159 // The only must alias information we can currently determine occurs when
160 // the node for P is a global node with only one entry.
161 const DSGraph::ScalarMapTy &GSM = G->getScalarMap();
162 DSGraph::ScalarMapTy::const_iterator I = GSM.find(P);
163 if (I != GSM.end()) {
164 DSNode *N = I->second.getNode();
165 if (N->isComplete() && isSinglePhysicalObject(N))
166 RetVals.push_back(N->getGlobals()[0]);
167 }
Chris Lattner5c70dc02003-06-29 00:54:08 +0000168 }
Chris Lattner10c45d62003-07-02 23:56:51 +0000169#endif
Chris Lattner5c70dc02003-06-29 00:54:08 +0000170 return getAnalysis<AliasAnalysis>().getMustAliases(P, RetVals);
171}