Disable incorrect mustalias code
[oota-llvm.git] / lib / Analysis / DataStructure / DataStructureAA.cpp
1 //===- 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"
12
13 namespace {
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) {
27       InitializeAliasAnalysis(this);
28       TD = &getAnalysis<TDDataStructures>();
29       return false;
30     }
31
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...
37     }
38
39     //------------------------------------------------
40     // Implement the AliasAnalysis API
41     //  
42
43     AliasResult alias(const Value *V1, unsigned V1Size,
44                       const Value *V2, unsigned V2Size);
45
46     void getMustAliases(Value *P, std::vector<Value*> &RetVals);
47
48   private:
49     DSGraph *getGraphForValue(const Value *V);
50   };
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
59 // getGraphForValue - Return the DSGraph to use for queries about the specified
60 // value...
61 //
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());
69   return 0;
70 }
71
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.
75 //
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();
80 }
81
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;
86
87   DSGraph *G1 = getGraphForValue(V1);
88   DSGraph *G2 = getGraphForValue(V2);
89   assert((!G1 || !G2 || G1 == G2) && "Alias query for 2 different functions?");
90   
91   // Get the graph to use...
92   DSGraph &G = *(G1 ? G1 : (G2 ? G2 : &TD->getGlobalsGraph()));
93
94   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();
104         
105       // 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.
109
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.
113         //
114         // FIXME: This isn't correct because we do not handle array indexing
115         // correctly.
116
117         if (O1 == O2 && isSinglePhysicalObject(N1))
118           return MustAlias; // Exactly the same object & offset
119 #endif
120
121         // 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);
128           }
129
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;
133         }
134       }
135     }
136   }
137
138   // FIXME: we could improve on this by checking the globals graph for aliased
139   // global queries...
140   return getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size);
141 }
142
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 ///
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();
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     }
168   }
169 #endif
170   return getAnalysis<AliasAnalysis>().getMustAliases(P, RetVals);
171 }