Fix a minor bug
[oota-llvm.git] / lib / Analysis / DataStructure / DataStructureAA.cpp
1 //===- DataStructureAA.cpp - Data Structure Based Alias Analysis ----------===//
2 // 
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 //===----------------------------------------------------------------------===//
9 //
10 // This pass uses the top-down data structure graphs to implement a simple
11 // context sensitive alias analysis.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Analysis/DataStructure.h"
16 #include "llvm/Analysis/DSGraph.h"
17 #include "llvm/Analysis/AliasAnalysis.h"
18 #include "llvm/Module.h"
19 using namespace llvm;
20
21 namespace {
22   class DSAA : public Pass, public AliasAnalysis {
23     TDDataStructures *TD;
24   public:
25     DSAA() : TD(0) {}
26
27     //------------------------------------------------
28     // Implement the Pass API
29     //
30
31     // run - Build up the result graph, representing the pointer graph for the
32     // program.
33     //
34     bool run(Module &M) {
35       InitializeAliasAnalysis(this);
36       TD = &getAnalysis<TDDataStructures>();
37       return false;
38     }
39
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...
45     }
46
47     //------------------------------------------------
48     // Implement the AliasAnalysis API
49     //  
50
51     AliasResult alias(const Value *V1, unsigned V1Size,
52                       const Value *V2, unsigned V2Size);
53
54     void getMustAliases(Value *P, std::vector<Value*> &RetVals);
55
56     bool pointsToConstantMemory(const Value *P) {
57       return getAnalysis<AliasAnalysis>().pointsToConstantMemory(P);
58     }
59
60   private:
61     DSGraph *getGraphForValue(const Value *V);
62   };
63
64   // Register the pass...
65   RegisterOpt<DSAA> X("ds-aa", "Data Structure Graph Based Alias Analysis");
66
67   // Register as an implementation of AliasAnalysis
68   RegisterAnalysisGroup<AliasAnalysis, DSAA> Y;
69 }
70
71 // getGraphForValue - Return the DSGraph to use for queries about the specified
72 // value...
73 //
74 DSGraph *DSAA::getGraphForValue(const Value *V) {
75   if (const Instruction *I = dyn_cast<Instruction>(V))
76     return &TD->getDSGraph(*I->getParent()->getParent());
77   else if (const Argument *A = dyn_cast<Argument>(V))
78     return &TD->getDSGraph(*A->getParent());
79   else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
80     return &TD->getDSGraph(*BB->getParent());
81   return 0;
82 }
83
84 // isSinglePhysicalObject - For now, the only case that we know that there is
85 // only one memory object in the node is when there is a single global in the
86 // node, and the only composition bit set is Global.
87 //
88 static bool isSinglePhysicalObject(DSNode *N) {
89   assert(N->isComplete() && "Can only tell if this is a complete object!");
90   return N->isGlobalNode() && N->getGlobals().size() == 1 &&
91          !N->isHeapNode() && !N->isAllocaNode() && !N->isUnknownNode();
92 }
93
94 // alias - This is the only method here that does anything interesting...
95 AliasAnalysis::AliasResult DSAA::alias(const Value *V1, unsigned V1Size,
96                                        const Value *V2, unsigned V2Size) {
97   if (V1 == V2) return MustAlias;
98
99   DSGraph *G1 = getGraphForValue(V1);
100   DSGraph *G2 = getGraphForValue(V2);
101   assert((!G1 || !G2 || G1 == G2) && "Alias query for 2 different functions?");
102   
103   // Get the graph to use...
104   DSGraph &G = *(G1 ? G1 : (G2 ? G2 : &TD->getGlobalsGraph()));
105
106   const DSGraph::ScalarMapTy &GSM = G.getScalarMap();
107   DSGraph::ScalarMapTy::const_iterator I = GSM.find((Value*)V1);
108   if (I != GSM.end()) {
109     assert(I->second.getNode() && "Scalar map points to null node?");
110     DSGraph::ScalarMapTy::const_iterator J = GSM.find((Value*)V2);
111     if (J != GSM.end()) {
112       assert(J->second.getNode() && "Scalar map points to null node?");
113
114       DSNode  *N1 = I->second.getNode(),  *N2 = J->second.getNode();
115       unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset();
116         
117       // We can only make a judgment of one of the nodes is complete...
118       if (N1->isComplete() || N2->isComplete()) {
119         if (N1 != N2)
120           return NoAlias;   // Completely different nodes.
121
122 #if 0  // This does not correctly handle arrays!
123         // Both point to the same node and same offset, and there is only one
124         // physical memory object represented in the node, return must alias.
125         //
126         // FIXME: This isn't correct because we do not handle array indexing
127         // correctly.
128
129         if (O1 == O2 && isSinglePhysicalObject(N1))
130           return MustAlias; // Exactly the same object & offset
131 #endif
132
133         // See if they point to different offsets...  if so, we may be able to
134         // determine that they do not alias...
135         if (O1 != O2) {
136           if (O2 < O1) {    // Ensure that O1 <= O2
137             std::swap(V1, V2);
138             std::swap(O1, O2);
139             std::swap(V1Size, V2Size);
140           }
141
142           // FIXME: This is not correct because we do not handle array
143           // indexing correctly with this check!
144           //if (O1+V1Size <= O2) return NoAlias;
145         }
146       }
147     }
148   }
149
150   // FIXME: we could improve on this by checking the globals graph for aliased
151   // global queries...
152   return getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size);
153 }
154
155
156 /// getMustAliases - If there are any pointers known that must alias this
157 /// pointer, return them now.  This allows alias-set based alias analyses to
158 /// perform a form a value numbering (which is exposed by load-vn).  If an alias
159 /// analysis supports this, it should ADD any must aliased pointers to the
160 /// specified vector.
161 ///
162 void DSAA::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
163 #if 0    // This does not correctly handle arrays!
164   // Currently the only must alias information we can provide is to say that
165   // something is equal to a global value. If we already have a global value,
166   // don't get worked up about it.
167   if (!isa<GlobalValue>(P)) {
168     DSGraph *G = getGraphForValue(P);
169     if (!G) G = &TD->getGlobalsGraph();
170     
171     // The only must alias information we can currently determine occurs when
172     // the node for P is a global node with only one entry.
173     DSGraph::ScalarMapTy::const_iterator I = G->getScalarMap().find(P);
174     if (I != G->getScalarMap().end()) {
175       DSNode *N = I->second.getNode();
176       if (N->isComplete() && isSinglePhysicalObject(N))
177         RetVals.push_back(N->getGlobals()[0]);
178     }
179   }
180 #endif
181   return getAnalysis<AliasAnalysis>().getMustAliases(P, RetVals);
182 }
183