1 //===- DataStructureAA.cpp - Data Structure Based Alias Analysis ----------===//
3 // This pass uses the top-down data structure graphs to implement a simple
4 // context sensitive alias analysis.
6 //===----------------------------------------------------------------------===//
8 #include "llvm/Analysis/DataStructure.h"
9 #include "llvm/Analysis/DSGraph.h"
10 #include "llvm/Analysis/AliasAnalysis.h"
11 #include "llvm/Module.h"
14 class DSAA : public Pass, public AliasAnalysis {
19 //------------------------------------------------
20 // Implement the Pass API
23 // run - Build up the result graph, representing the pointer graph for the
27 InitializeAliasAnalysis(this);
28 TD = &getAnalysis<TDDataStructures>();
32 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
33 AliasAnalysis::getAnalysisUsage(AU);
34 AU.setPreservesAll(); // Does not transform code...
35 AU.addRequired<TDDataStructures>(); // Uses TD Datastructures
36 AU.addRequired<AliasAnalysis>(); // Chains to another AA impl...
39 //------------------------------------------------
40 // Implement the AliasAnalysis API
43 AliasResult alias(const Value *V1, unsigned V1Size,
44 const Value *V2, unsigned V2Size);
46 void getMustAliases(Value *P, std::vector<Value*> &RetVals);
49 DSGraph *getGraphForValue(const Value *V);
52 // Register the pass...
53 RegisterOpt<DSAA> X("ds-aa", "Data Structure Graph Based Alias Analysis");
55 // Register as an implementation of AliasAnalysis
56 RegisterAnalysisGroup<AliasAnalysis, DSAA> Y;
59 // getGraphForValue - Return the DSGraph to use for queries about the specified
62 DSGraph *DSAA::getGraphForValue(const Value *V) {
63 if (const Instruction *I = dyn_cast<Instruction>(V))
64 return &TD->getDSGraph(*I->getParent()->getParent());
65 else if (const Argument *A = dyn_cast<Argument>(V))
66 return &TD->getDSGraph(*A->getParent());
67 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
68 return &TD->getDSGraph(*BB->getParent());
72 // 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.
76 static 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();
82 // alias - This is the only method here that does anything interesting...
83 AliasAnalysis::AliasResult DSAA::alias(const Value *V1, unsigned V1Size,
84 const Value *V2, unsigned V2Size) {
85 if (V1 == V2) return MustAlias;
87 DSGraph *G1 = getGraphForValue(V1);
88 DSGraph *G2 = getGraphForValue(V2);
89 assert((!G1 || !G2 || G1 == G2) && "Alias query for 2 different functions?");
91 // Get the graph to use...
92 DSGraph &G = *(G1 ? G1 : (G2 ? G2 : &TD->getGlobalsGraph()));
94 const DSGraph::ScalarMapTy &GSM = G.getScalarMap();
95 DSGraph::ScalarMapTy::const_iterator I = GSM.find((Value*)V1);
97 assert(I->second.getNode() && "Scalar map points to null node?");
98 DSGraph::ScalarMapTy::const_iterator J = GSM.find((Value*)V2);
100 assert(J->second.getNode() && "Scalar map points to null node?");
102 DSNode *N1 = I->second.getNode(), *N2 = J->second.getNode();
103 unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset();
105 // We can only make a judgement of one of the nodes is complete...
106 if (N1->isComplete() || N2->isComplete()) {
108 return NoAlias; // Completely different nodes.
110 #if 0 // This does not correctly handle arrays!
111 // 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.
114 // FIXME: This isn't correct because we do not handle array indexing
117 if (O1 == O2 && isSinglePhysicalObject(N1))
118 return MustAlias; // Exactly the same object & offset
121 // See if they point to different offsets... if so, we may be able to
122 // determine that they do not alias...
124 if (O2 < O1) { // Ensure that O1 <= O2
127 std::swap(V1Size, V2Size);
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;
138 // FIXME: we could improve on this by checking the globals graph for aliased
140 return getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size);
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.
150 void DSAA::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
151 #if 0 // This does not correctly handle arrays!
152 // 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();
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]);
170 return getAnalysis<AliasAnalysis>().getMustAliases(P, RetVals);