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/Constants.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Module.h"
18 #include "llvm/Analysis/AliasAnalysis.h"
19 #include "llvm/Analysis/Passes.h"
20 #include "llvm/Analysis/DataStructure/DataStructure.h"
21 #include "llvm/Analysis/DataStructure/DSGraph.h"
25 class DSAA : public ModulePass, public AliasAnalysis {
31 //------------------------------------------------
32 // Implement the Pass API
35 // run - Build up the result graph, representing the pointer graph for the
38 bool runOnModule(Module &M) {
39 InitializeAliasAnalysis(this);
40 TD = &getAnalysis<TDDataStructures>();
41 BU = &getAnalysis<BUDataStructures>();
45 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
46 AliasAnalysis::getAnalysisUsage(AU);
47 AU.setPreservesAll(); // Does not transform code
48 AU.addRequiredTransitive<TDDataStructures>(); // Uses TD Datastructures
49 AU.addRequiredTransitive<BUDataStructures>(); // Uses BU Datastructures
52 //------------------------------------------------
53 // Implement the AliasAnalysis API
56 AliasResult alias(const Value *V1, unsigned V1Size,
57 const Value *V2, unsigned V2Size);
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 AliasAnalysis::AliasResult DSAA::alias(const Value *V1, unsigned V1Size,
102 const Value *V2, unsigned V2Size) {
103 if (V1 == V2) return MustAlias;
105 DSGraph *G1 = getGraphForValue(V1);
106 DSGraph *G2 = getGraphForValue(V2);
107 assert((!G1 || !G2 || G1 == G2) && "Alias query for 2 different functions?");
109 // Get the graph to use...
110 DSGraph &G = *(G1 ? G1 : (G2 ? G2 : &TD->getGlobalsGraph()));
112 const DSGraph::ScalarMapTy &GSM = G.getScalarMap();
113 DSGraph::ScalarMapTy::const_iterator I = GSM.find((Value*)V1);
114 if (I == GSM.end()) return NoAlias;
116 DSGraph::ScalarMapTy::const_iterator J = GSM.find((Value*)V2);
117 if (J == GSM.end()) return NoAlias;
119 DSNode *N1 = I->second.getNode(), *N2 = J->second.getNode();
120 unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset();
121 if (N1 == 0 || N2 == 0)
122 return MayAlias; // Can't tell whether anything aliases null.
124 // We can only make a judgment of one of the nodes is complete...
125 if (N1->isComplete() || N2->isComplete()) {
127 return NoAlias; // Completely different nodes.
129 // See if they point to different offsets... if so, we may be able to
130 // determine that they do not alias...
132 if (O2 < O1) { // Ensure that O1 <= O2
135 std::swap(V1Size, V2Size);
143 // FIXME: we could improve on this by checking the globals graph for aliased
145 return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
148 /// getModRefInfo - does a callsite modify or reference a value?
150 AliasAnalysis::ModRefResult
151 DSAA::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
152 AliasAnalysis::ModRefResult Result =AliasAnalysis::getModRefInfo(CS, P, Size);
153 Function *F = CS.getCalledFunction();
155 if (!F || Result == NoModRef)
158 if (F->isExternal()) {
159 // If we are calling an external function, and if this global doesn't escape
160 // the portion of the program we have analyzed, we can draw conclusions
161 // based on whether the global escapes the program.
162 Function *Caller = CS.getInstruction()->getParent()->getParent();
163 DSGraph *G = &TD->getDSGraph(*Caller);
164 DSScalarMap::iterator NI = G->getScalarMap().find(P);
165 if (NI == G->getScalarMap().end()) {
166 // If it wasn't in the local function graph, check the global graph. This
167 // can occur for globals who are locally reference but hoisted out to the
168 // globals graph despite that.
169 G = G->getGlobalsGraph();
170 NI = G->getScalarMap().find(P);
171 if (NI == G->getScalarMap().end())
175 // If we found a node and it's complete, it cannot be passed out to the
177 if (NI->second.getNode()->isComplete())
182 // Get the graphs for the callee and caller. Note that we want the BU graph
183 // for the callee because we don't want all caller's effects incorporated!
184 const Function *Caller = CS.getInstruction()->getParent()->getParent();
185 DSGraph &CallerTDGraph = TD->getDSGraph(*Caller);
186 DSGraph &CalleeBUGraph = BU->getDSGraph(*F);
188 // Figure out which node in the TD graph this pointer corresponds to.
189 DSScalarMap &CallerSM = CallerTDGraph.getScalarMap();
190 DSScalarMap::iterator NI = CallerSM.find(P);
191 if (NI == CallerSM.end()) {
192 if (isa<ConstantPointerNull>(P) || isa<UndefValue>(P))
193 Result = NoModRef; // null is never modified :)
195 assert(isa<GlobalVariable>(P) &&
196 cast<GlobalVariable>(P)->getType()->getElementType()->isFirstClassType() &&
197 "This isn't a global that DSA inconsiderately dropped "
200 DSGraph &GG = *CallerTDGraph.getGlobalsGraph();
201 DSScalarMap::iterator NI = GG.getScalarMap().find(P);
202 if (NI != GG.getScalarMap().end() && !NI->second.isNull()) {
203 // Otherwise, if the node is only M or R, return this. This can be
204 // useful for globals that should be marked const but are not.
205 DSNode *N = NI->second.getNode();
206 if (!N->isModified())
207 Result = (ModRefResult)(Result & ~Mod);
209 Result = (ModRefResult)(Result & ~Ref);
215 const DSNode *N = NI->second.getNode();
216 assert(N && "Null pointer in scalar map??");
218 // Compute the mapping from nodes in the callee graph to the nodes in the
219 // caller graph for this call site.
220 DSGraph::NodeMapTy CalleeCallerMap;
221 DSCallSite DSCS = CallerTDGraph.getDSCallSiteForCallSite(CS);
222 CallerTDGraph.computeCalleeCallerMapping(DSCS, *F, CalleeBUGraph,
225 // Loop over all of the nodes in the callee that correspond to "N", keeping
226 // track of aggregate mod/ref info.
227 bool NeverReads = true, NeverWrites = true;
228 for (DSGraph::NodeMapTy::iterator I = CalleeCallerMap.begin(),
229 E = CalleeCallerMap.end(); I != E; ++I)
230 if (I->second.getNode() == N) {
231 if (I->first->isModified())
233 if (I->first->isRead())
235 if (NeverReads == false && NeverWrites == false)
239 if (NeverWrites) // We proved it was not modified.
240 Result = ModRefResult(Result & ~Mod);
241 if (NeverReads) // We proved it was not read.
242 Result = ModRefResult(Result & ~Ref);