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/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"
23 class DSAA : public ModulePass, public AliasAnalysis {
29 //------------------------------------------------
30 // Implement the Pass API
33 // run - Build up the result graph, representing the pointer graph for the
36 bool runOnModule(Module &M) {
37 InitializeAliasAnalysis(this);
38 TD = &getAnalysis<TDDataStructures>();
39 BU = &getAnalysis<BUDataStructures>();
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
50 //------------------------------------------------
51 // Implement the AliasAnalysis API
54 AliasResult alias(const Value *V1, unsigned V1Size,
55 const Value *V2, unsigned V2Size);
57 void getMustAliases(Value *P, std::vector<Value*> &RetVals);
59 ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
60 ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
61 return AliasAnalysis::getModRefInfo(CS1,CS2);
64 virtual void deleteValue(Value *V) {
69 virtual void copyValue(Value *From, Value *To) {
70 if (From == To) return;
71 BU->copyValue(From, To);
72 TD->copyValue(From, To);
76 DSGraph *getGraphForValue(const Value *V);
79 // Register the pass...
80 RegisterOpt<DSAA> X("ds-aa", "Data Structure Graph Based Alias Analysis");
82 // Register as an implementation of AliasAnalysis
83 RegisterAnalysisGroup<AliasAnalysis, DSAA> Y;
86 ModulePass *llvm::createDSAAPass() { return new DSAA(); }
88 // getGraphForValue - Return the DSGraph to use for queries about the specified
91 DSGraph *DSAA::getGraphForValue(const Value *V) {
92 if (const Instruction *I = dyn_cast<Instruction>(V))
93 return &TD->getDSGraph(*I->getParent()->getParent());
94 else if (const Argument *A = dyn_cast<Argument>(V))
95 return &TD->getDSGraph(*A->getParent());
96 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
97 return &TD->getDSGraph(*BB->getParent());
101 // isSinglePhysicalObject - For now, the only case that we know that there is
102 // only one memory object in the node is when there is a single global in the
103 // node, and the only composition bit set is Global.
105 static bool isSinglePhysicalObject(DSNode *N) {
106 assert(N->isComplete() && "Can only tell if this is a complete object!");
107 return N->isGlobalNode() && N->getGlobals().size() == 1 &&
108 !N->isHeapNode() && !N->isAllocaNode() && !N->isUnknownNode();
111 // alias - This is the only method here that does anything interesting...
112 AliasAnalysis::AliasResult DSAA::alias(const Value *V1, unsigned V1Size,
113 const Value *V2, unsigned V2Size) {
114 if (V1 == V2) return MustAlias;
116 DSGraph *G1 = getGraphForValue(V1);
117 DSGraph *G2 = getGraphForValue(V2);
118 assert((!G1 || !G2 || G1 == G2) && "Alias query for 2 different functions?");
120 // Get the graph to use...
121 DSGraph &G = *(G1 ? G1 : (G2 ? G2 : &TD->getGlobalsGraph()));
123 const DSGraph::ScalarMapTy &GSM = G.getScalarMap();
124 DSGraph::ScalarMapTy::const_iterator I = GSM.find((Value*)V1);
125 if (I == GSM.end()) return NoAlias;
127 assert(I->second.getNode() && "Scalar map points to null node?");
128 DSGraph::ScalarMapTy::const_iterator J = GSM.find((Value*)V2);
129 if (J == GSM.end()) return NoAlias;
131 assert(J->second.getNode() && "Scalar map points to null node?");
133 DSNode *N1 = I->second.getNode(), *N2 = J->second.getNode();
134 unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset();
136 // We can only make a judgment of one of the nodes is complete...
137 if (N1->isComplete() || N2->isComplete()) {
139 return NoAlias; // Completely different nodes.
141 #if 0 // This does not correctly handle arrays!
142 // Both point to the same node and same offset, and there is only one
143 // physical memory object represented in the node, return must alias.
145 // FIXME: This isn't correct because we do not handle array indexing
148 if (O1 == O2 && isSinglePhysicalObject(N1))
149 return MustAlias; // Exactly the same object & offset
152 // See if they point to different offsets... if so, we may be able to
153 // determine that they do not alias...
155 if (O2 < O1) { // Ensure that O1 <= O2
158 std::swap(V1Size, V2Size);
161 // FIXME: This is not correct because we do not handle array
162 // indexing correctly with this check!
163 //if (O1+V1Size <= O2) return NoAlias;
167 // FIXME: we could improve on this by checking the globals graph for aliased
169 return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
172 /// getModRefInfo - does a callsite modify or reference a value?
174 AliasAnalysis::ModRefResult
175 DSAA::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
176 Function *F = CS.getCalledFunction();
177 if (!F) return pointsToConstantMemory(P) ? Ref : ModRef;
178 if (F->isExternal()) return ModRef;
180 // Clone the function TD graph, clearing off Mod/Ref flags
181 const Function *csParent = CS.getInstruction()->getParent()->getParent();
182 DSGraph TDGraph(TD->getDSGraph(*csParent));
183 TDGraph.maskNodeTypes(0);
185 // Insert the callee's BU graph into the TD graph
186 const DSGraph &BUGraph = BU->getDSGraph(*F);
187 TDGraph.mergeInGraph(TDGraph.getDSCallSiteForCallSite(CS),
190 // Report the flags that have been added
191 const DSNodeHandle &DSH = TDGraph.getNodeForValue(P);
192 if (const DSNode *N = DSH.getNode())
194 return N->isRead() ? ModRef : Mod;
196 return N->isRead() ? Ref : NoModRef;
201 /// getMustAliases - If there are any pointers known that must alias this
202 /// pointer, return them now. This allows alias-set based alias analyses to
203 /// perform a form a value numbering (which is exposed by load-vn). If an alias
204 /// analysis supports this, it should ADD any must aliased pointers to the
205 /// specified vector.
207 void DSAA::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
208 #if 0 // This does not correctly handle arrays!
209 // Currently the only must alias information we can provide is to say that
210 // something is equal to a global value. If we already have a global value,
211 // don't get worked up about it.
212 if (!isa<GlobalValue>(P)) {
213 DSGraph *G = getGraphForValue(P);
214 if (!G) G = &TD->getGlobalsGraph();
216 // The only must alias information we can currently determine occurs when
217 // the node for P is a global node with only one entry.
218 DSGraph::ScalarMapTy::const_iterator I = G->getScalarMap().find(P);
219 if (I != G->getScalarMap().end()) {
220 DSNode *N = I->second.getNode();
221 if (N->isComplete() && isSinglePhysicalObject(N))
222 RetVals.push_back(N->getGlobals()[0]);
226 return AliasAnalysis::getMustAliases(P, RetVals);