Add last four createXxxPass functions
[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/Module.h"
16 #include "llvm/Analysis/AliasAnalysis.h"
17 #include "llvm/Analysis/Passes.h"
18 #include "llvm/Analysis/DataStructure/DataStructure.h"
19 #include "llvm/Analysis/DataStructure/DSGraph.h"
20 using namespace llvm;
21
22 namespace {
23   class DSAA : public ModulePass, public AliasAnalysis {
24     TDDataStructures *TD;
25     BUDataStructures *BU;
26   public:
27     DSAA() : TD(0) {}
28
29     //------------------------------------------------
30     // Implement the Pass API
31     //
32
33     // run - Build up the result graph, representing the pointer graph for the
34     // program.
35     //
36     bool runOnModule(Module &M) {
37       InitializeAliasAnalysis(this);
38       TD = &getAnalysis<TDDataStructures>();
39       BU = &getAnalysis<BUDataStructures>();
40       return false;
41     }
42
43     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
44       AliasAnalysis::getAnalysisUsage(AU);
45       AU.setPreservesAll();                         // Does not transform code
46       AU.addRequiredTransitive<TDDataStructures>(); // Uses TD Datastructures
47       AU.addRequiredTransitive<BUDataStructures>(); // Uses BU Datastructures
48     }
49
50     //------------------------------------------------
51     // Implement the AliasAnalysis API
52     //  
53
54     AliasResult alias(const Value *V1, unsigned V1Size,
55                       const Value *V2, unsigned V2Size);
56
57     void getMustAliases(Value *P, std::vector<Value*> &RetVals);
58
59     ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
60     ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
61       return AliasAnalysis::getModRefInfo(CS1,CS2);
62     }
63
64   private:
65     DSGraph *getGraphForValue(const Value *V);
66   };
67
68   // Register the pass...
69   RegisterOpt<DSAA> X("ds-aa", "Data Structure Graph Based Alias Analysis");
70
71   // Register as an implementation of AliasAnalysis
72   RegisterAnalysisGroup<AliasAnalysis, DSAA> Y;
73 }
74
75 ModulePass *llvm::createDSAAPass() { return new DSAA(); }
76
77 // getGraphForValue - Return the DSGraph to use for queries about the specified
78 // value...
79 //
80 DSGraph *DSAA::getGraphForValue(const Value *V) {
81   if (const Instruction *I = dyn_cast<Instruction>(V))
82     return &TD->getDSGraph(*I->getParent()->getParent());
83   else if (const Argument *A = dyn_cast<Argument>(V))
84     return &TD->getDSGraph(*A->getParent());
85   else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
86     return &TD->getDSGraph(*BB->getParent());
87   return 0;
88 }
89
90 // isSinglePhysicalObject - For now, the only case that we know that there is
91 // only one memory object in the node is when there is a single global in the
92 // node, and the only composition bit set is Global.
93 //
94 static bool isSinglePhysicalObject(DSNode *N) {
95   assert(N->isComplete() && "Can only tell if this is a complete object!");
96   return N->isGlobalNode() && N->getGlobals().size() == 1 &&
97          !N->isHeapNode() && !N->isAllocaNode() && !N->isUnknownNode();
98 }
99
100 // alias - This is the only method here that does anything interesting...
101 AliasAnalysis::AliasResult DSAA::alias(const Value *V1, unsigned V1Size,
102                                        const Value *V2, unsigned V2Size) {
103   if (V1 == V2) return MustAlias;
104
105   DSGraph *G1 = getGraphForValue(V1);
106   DSGraph *G2 = getGraphForValue(V2);
107   assert((!G1 || !G2 || G1 == G2) && "Alias query for 2 different functions?");
108   
109   // Get the graph to use...
110   DSGraph &G = *(G1 ? G1 : (G2 ? G2 : &TD->getGlobalsGraph()));
111
112   const DSGraph::ScalarMapTy &GSM = G.getScalarMap();
113   DSGraph::ScalarMapTy::const_iterator I = GSM.find((Value*)V1);
114   if (I == GSM.end()) return NoAlias;
115
116   assert(I->second.getNode() && "Scalar map points to null node?");
117   DSGraph::ScalarMapTy::const_iterator J = GSM.find((Value*)V2);
118   if (J == GSM.end()) return NoAlias;
119
120   assert(J->second.getNode() && "Scalar map points to null node?");
121
122   DSNode  *N1 = I->second.getNode(),  *N2 = J->second.getNode();
123   unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset();
124         
125   // We can only make a judgment of one of the nodes is complete...
126   if (N1->isComplete() || N2->isComplete()) {
127     if (N1 != N2)
128       return NoAlias;   // Completely different nodes.
129
130 #if 0  // This does not correctly handle arrays!
131     // Both point to the same node and same offset, and there is only one
132     // physical memory object represented in the node, return must alias.
133     //
134     // FIXME: This isn't correct because we do not handle array indexing
135     // correctly.
136
137     if (O1 == O2 && isSinglePhysicalObject(N1))
138       return MustAlias; // Exactly the same object & offset
139 #endif
140
141     // See if they point to different offsets...  if so, we may be able to
142     // determine that they do not alias...
143     if (O1 != O2) {
144       if (O2 < O1) {    // Ensure that O1 <= O2
145         std::swap(V1, V2);
146         std::swap(O1, O2);
147         std::swap(V1Size, V2Size);
148       }
149
150       // FIXME: This is not correct because we do not handle array
151       // indexing correctly with this check!
152       //if (O1+V1Size <= O2) return NoAlias;
153     }
154   }
155
156   // FIXME: we could improve on this by checking the globals graph for aliased
157   // global queries...
158   return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
159 }
160
161 /// getModRefInfo - does a callsite modify or reference a value?
162 ///
163 AliasAnalysis::ModRefResult
164 DSAA::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
165   Function *F = CS.getCalledFunction();
166   if (!F) return pointsToConstantMemory(P) ? Ref : ModRef;
167   if (F->isExternal()) return ModRef;
168
169   // Clone the function TD graph, clearing off Mod/Ref flags
170   const Function *csParent = CS.getInstruction()->getParent()->getParent();
171   DSGraph TDGraph(TD->getDSGraph(*csParent));
172   TDGraph.maskNodeTypes(0);
173   
174   // Insert the callee's BU graph into the TD graph
175   const DSGraph &BUGraph = BU->getDSGraph(*F);
176   TDGraph.mergeInGraph(TDGraph.getDSCallSiteForCallSite(CS),
177                        *F, BUGraph, 0);
178
179   // Report the flags that have been added
180   const DSNodeHandle &DSH = TDGraph.getNodeForValue(P);
181   if (const DSNode *N = DSH.getNode())
182     if (N->isModified())
183       return N->isRead() ? ModRef : Mod;
184     else
185       return N->isRead() ? Ref : NoModRef;
186   return NoModRef;
187 }
188
189
190 /// getMustAliases - If there are any pointers known that must alias this
191 /// pointer, return them now.  This allows alias-set based alias analyses to
192 /// perform a form a value numbering (which is exposed by load-vn).  If an alias
193 /// analysis supports this, it should ADD any must aliased pointers to the
194 /// specified vector.
195 ///
196 void DSAA::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
197 #if 0    // This does not correctly handle arrays!
198   // Currently the only must alias information we can provide is to say that
199   // something is equal to a global value. If we already have a global value,
200   // don't get worked up about it.
201   if (!isa<GlobalValue>(P)) {
202     DSGraph *G = getGraphForValue(P);
203     if (!G) G = &TD->getGlobalsGraph();
204     
205     // The only must alias information we can currently determine occurs when
206     // the node for P is a global node with only one entry.
207     DSGraph::ScalarMapTy::const_iterator I = G->getScalarMap().find(P);
208     if (I != G->getScalarMap().end()) {
209       DSNode *N = I->second.getNode();
210       if (N->isComplete() && isSinglePhysicalObject(N))
211         RetVals.push_back(N->getGlobals()[0]);
212     }
213   }
214 #endif
215   return AliasAnalysis::getMustAliases(P, RetVals);
216 }
217