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