1 //===- DataStructureAA.cpp - Data Structure Based Alias Analysis ----------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This pass uses the top-down data structure graphs to implement a simple
11 // context sensitive alias analysis.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/DataStructure.h"
16 #include "llvm/Analysis/DSGraph.h"
17 #include "llvm/Analysis/AliasAnalysis.h"
18 #include "llvm/Module.h"
22 class DSAA : public Pass, public AliasAnalysis {
27 //------------------------------------------------
28 // Implement the Pass API
31 // run - Build up the result graph, representing the pointer graph for the
35 InitializeAliasAnalysis(this);
36 TD = &getAnalysis<TDDataStructures>();
40 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
41 AliasAnalysis::getAnalysisUsage(AU);
42 AU.setPreservesAll(); // Does not transform code...
43 AU.addRequired<TDDataStructures>(); // Uses TD Datastructures
44 AU.addRequired<AliasAnalysis>(); // Chains to another AA impl...
47 //------------------------------------------------
48 // Implement the AliasAnalysis API
51 AliasResult alias(const Value *V1, unsigned V1Size,
52 const Value *V2, unsigned V2Size);
54 void getMustAliases(Value *P, std::vector<Value*> &RetVals);
57 DSGraph *getGraphForValue(const Value *V);
60 // Register the pass...
61 RegisterOpt<DSAA> X("ds-aa", "Data Structure Graph Based Alias Analysis");
63 // Register as an implementation of AliasAnalysis
64 RegisterAnalysisGroup<AliasAnalysis, DSAA> Y;
67 // getGraphForValue - Return the DSGraph to use for queries about the specified
70 DSGraph *DSAA::getGraphForValue(const Value *V) {
71 if (const Instruction *I = dyn_cast<Instruction>(V))
72 return &TD->getDSGraph(*I->getParent()->getParent());
73 else if (const Argument *A = dyn_cast<Argument>(V))
74 return &TD->getDSGraph(*A->getParent());
75 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
76 return &TD->getDSGraph(*BB->getParent());
80 // isSinglePhysicalObject - For now, the only case that we know that there is
81 // only one memory object in the node is when there is a single global in the
82 // node, and the only composition bit set is Global.
84 static bool isSinglePhysicalObject(DSNode *N) {
85 assert(N->isComplete() && "Can only tell if this is a complete object!");
86 return N->isGlobalNode() && N->getGlobals().size() == 1 &&
87 !N->isHeapNode() && !N->isAllocaNode() && !N->isUnknownNode();
90 // alias - This is the only method here that does anything interesting...
91 AliasAnalysis::AliasResult DSAA::alias(const Value *V1, unsigned V1Size,
92 const Value *V2, unsigned V2Size) {
93 if (V1 == V2) return MustAlias;
95 DSGraph *G1 = getGraphForValue(V1);
96 DSGraph *G2 = getGraphForValue(V2);
97 assert((!G1 || !G2 || G1 == G2) && "Alias query for 2 different functions?");
99 // Get the graph to use...
100 DSGraph &G = *(G1 ? G1 : (G2 ? G2 : &TD->getGlobalsGraph()));
102 const DSGraph::ScalarMapTy &GSM = G.getScalarMap();
103 DSGraph::ScalarMapTy::const_iterator I = GSM.find((Value*)V1);
104 if (I != GSM.end()) {
105 assert(I->second.getNode() && "Scalar map points to null node?");
106 DSGraph::ScalarMapTy::const_iterator J = GSM.find((Value*)V2);
107 if (J != GSM.end()) {
108 assert(J->second.getNode() && "Scalar map points to null node?");
110 DSNode *N1 = I->second.getNode(), *N2 = J->second.getNode();
111 unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset();
113 // We can only make a judgment of one of the nodes is complete...
114 if (N1->isComplete() || N2->isComplete()) {
116 return NoAlias; // Completely different nodes.
118 #if 0 // This does not correctly handle arrays!
119 // Both point to the same node and same offset, and there is only one
120 // physical memory object represented in the node, return must alias.
122 // FIXME: This isn't correct because we do not handle array indexing
125 if (O1 == O2 && isSinglePhysicalObject(N1))
126 return MustAlias; // Exactly the same object & offset
129 // See if they point to different offsets... if so, we may be able to
130 // determine that they do not alias...
132 if (O2 < O1) { // Ensure that O1 <= O2
135 std::swap(V1Size, V2Size);
138 // FIXME: This is not correct because we do not handle array
139 // indexing correctly with this check!
140 //if (O1+V1Size <= O2) return NoAlias;
146 // FIXME: we could improve on this by checking the globals graph for aliased
148 return getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size);
152 /// getMustAliases - If there are any pointers known that must alias this
153 /// pointer, return them now. This allows alias-set based alias analyses to
154 /// perform a form a value numbering (which is exposed by load-vn). If an alias
155 /// analysis supports this, it should ADD any must aliased pointers to the
156 /// specified vector.
158 void DSAA::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
159 #if 0 // This does not correctly handle arrays!
160 // Currently the only must alias information we can provide is to say that
161 // something is equal to a global value. If we already have a global value,
162 // don't get worked up about it.
163 if (!isa<GlobalValue>(P)) {
164 DSGraph *G = getGraphForValue(P);
165 if (!G) G = &TD->getGlobalsGraph();
167 // The only must alias information we can currently determine occurs when
168 // the node for P is a global node with only one entry.
169 DSGraph::ScalarMapTy::const_iterator I = G->getScalarMap().find(P);
170 if (I != G->getScalarMap().end()) {
171 DSNode *N = I->second.getNode();
172 if (N->isComplete() && isSinglePhysicalObject(N))
173 RetVals.push_back(N->getGlobals()[0]);
177 return getAnalysis<AliasAnalysis>().getMustAliases(P, RetVals);