blob: 4940e9d0fc591bf12d301411ea698d430ed67375 [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
Chris Lattner5c70dc02003-06-29 00:54:08 +0000105 // We can only make a judgement of one of the nodes is complete...
106 if (N1->isComplete() || N2->isComplete()) {
107 if (N1 != N2)
108 return NoAlias; // Completely different nodes.
Chris Lattnerbd92b732003-06-19 21:15:11 +0000109
Chris Lattner5c70dc02003-06-29 00:54:08 +0000110 // Both point to the same node and same offset, and there is only one
111 // physical memory object represented in the node, return must alias.
112 //
113 // FIXME: This isn't correct because we do not handle array indexing
114 // correctly.
Chris Lattnerbd92b732003-06-19 21:15:11 +0000115
Chris Lattner5c70dc02003-06-29 00:54:08 +0000116 if (O1 == O2 && isSinglePhysicalObject(N1))
117 return MustAlias; // Exactly the same object & offset
Chris Lattnerbd92b732003-06-19 21:15:11 +0000118
Chris Lattner5c70dc02003-06-29 00:54:08 +0000119 // See if they point to different offsets... if so, we may be able to
120 // determine that they do not alias...
121 if (O1 != O2) {
122 if (O2 < O1) { // Ensure that O1 <= O2
123 std::swap(V1, V2);
124 std::swap(O1, O2);
125 std::swap(V1Size, V2Size);
Chris Lattnerbd92b732003-06-19 21:15:11 +0000126 }
Chris Lattner5c70dc02003-06-29 00:54:08 +0000127
128 // FIXME: This is not correct because we do not handle array
129 // indexing correctly with this check!
130 //if (O1+V1Size <= O2) return NoAlias;
Chris Lattner8105cfb2003-02-03 22:50:46 +0000131 }
132 }
133 }
134 }
135
136 // FIXME: we could improve on this by checking the globals graph for aliased
137 // global queries...
Chris Lattnera612afc2003-02-26 19:29:36 +0000138 return getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size);
Chris Lattner8105cfb2003-02-03 22:50:46 +0000139}
Chris Lattner5c70dc02003-06-29 00:54:08 +0000140
141
142/// getMustAliases - If there are any pointers known that must alias this
143/// pointer, return them now. This allows alias-set based alias analyses to
144/// perform a form a value numbering (which is exposed by load-vn). If an alias
145/// analysis supports this, it should ADD any must aliased pointers to the
146/// specified vector.
147///
148void DSAA::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
Chris Lattner9cd04842003-07-02 04:39:13 +0000149 // Currently the only must alias information we can provide is to say that
150 // something is equal to a global value. If we already have a global value,
151 // don't get worked up about it.
152 if (!isa<GlobalValue>(P)) {
153 DSGraph *G = getGraphForValue(P);
154 if (!G) G = &TD->getGlobalsGraph();
155
156 // The only must alias information we can currently determine occurs when
157 // the node for P is a global node with only one entry.
158 const DSGraph::ScalarMapTy &GSM = G->getScalarMap();
159 DSGraph::ScalarMapTy::const_iterator I = GSM.find(P);
160 if (I != GSM.end()) {
161 DSNode *N = I->second.getNode();
162 if (N->isComplete() && isSinglePhysicalObject(N))
163 RetVals.push_back(N->getGlobals()[0]);
164 }
Chris Lattner5c70dc02003-06-29 00:54:08 +0000165 }
166
167 return getAnalysis<AliasAnalysis>().getMustAliases(P, RetVals);
168}