1 //===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the generic AliasAnalysis interface which is used as the
11 // common interface used by all clients and implementations of alias analysis.
13 // This file also implements the default version of the AliasAnalysis interface
14 // that is to be used when no other implementation is specified. This does some
15 // simple tests that detect obvious cases: two different global pointers cannot
16 // alias, a global cannot alias a malloc, two different mallocs cannot alias,
19 // This alias analysis implementation really isn't very good for anything, but
20 // it is very fast, and makes a nice clean default implementation. Because it
21 // handles lots of little corner cases, other, more complex, alias analysis
22 // implementations may choose to rely on this pass to resolve these simple and
25 //===----------------------------------------------------------------------===//
27 #include "llvm/Analysis/AliasAnalysis.h"
28 #include "llvm/Pass.h"
29 #include "llvm/BasicBlock.h"
30 #include "llvm/Function.h"
31 #include "llvm/Instructions.h"
32 #include "llvm/Type.h"
33 #include "llvm/Target/TargetData.h"
36 // Register the AliasAnalysis interface, providing a nice name to refer to.
38 RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis");
40 char AliasAnalysis::ID = 0;
42 //===----------------------------------------------------------------------===//
43 // Default chaining methods
44 //===----------------------------------------------------------------------===//
46 AliasAnalysis::AliasResult
47 AliasAnalysis::alias(const Value *V1, unsigned V1Size,
48 const Value *V2, unsigned V2Size) {
49 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
50 return AA->alias(V1, V1Size, V2, V2Size);
53 void AliasAnalysis::getMustAliases(Value *P, std::vector<Value*> &RetVals) {
54 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
55 return AA->getMustAliases(P, RetVals);
58 bool AliasAnalysis::pointsToConstantMemory(const Value *P) {
59 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
60 return AA->pointsToConstantMemory(P);
63 AliasAnalysis::ModRefBehavior
64 AliasAnalysis::getModRefBehavior(Function *F, CallSite CS,
65 std::vector<PointerAccessInfo> *Info) {
66 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
67 return AA->getModRefBehavior(F, CS, Info);
70 bool AliasAnalysis::hasNoModRefInfoForCalls() const {
71 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
72 return AA->hasNoModRefInfoForCalls();
75 void AliasAnalysis::deleteValue(Value *V) {
76 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
80 void AliasAnalysis::copyValue(Value *From, Value *To) {
81 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
82 AA->copyValue(From, To);
85 AliasAnalysis::ModRefResult
86 AliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) {
87 // FIXME: we can do better.
88 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
89 return AA->getModRefInfo(CS1, CS2);
93 //===----------------------------------------------------------------------===//
94 // AliasAnalysis non-virtual helper method implementation
95 //===----------------------------------------------------------------------===//
97 AliasAnalysis::ModRefResult
98 AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) {
99 return alias(L->getOperand(0), TD->getTypeStoreSize(L->getType()),
100 P, Size) ? Ref : NoModRef;
103 AliasAnalysis::ModRefResult
104 AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) {
105 // If the stored address cannot alias the pointer in question, then the
106 // pointer cannot be modified by the store.
107 if (!alias(S->getOperand(1),
108 TD->getTypeStoreSize(S->getOperand(0)->getType()), P, Size))
111 // If the pointer is a pointer to constant memory, then it could not have been
112 // modified by this store.
113 return pointsToConstantMemory(P) ? NoModRef : Mod;
116 AliasAnalysis::ModRefBehavior
117 AliasAnalysis::getModRefBehavior(CallSite CS,
118 std::vector<PointerAccessInfo> *Info) {
119 if (CS.doesNotAccessMemory())
120 // Can't do better than this.
121 return DoesNotAccessMemory;
122 ModRefBehavior MRB = UnknownModRefBehavior;
123 if (Function *F = CS.getCalledFunction())
124 MRB = getModRefBehavior(F, CS, Info);
125 if (MRB != DoesNotAccessMemory && CS.onlyReadsMemory())
126 return OnlyReadsMemory;
130 AliasAnalysis::ModRefBehavior
131 AliasAnalysis::getModRefBehavior(Function *F,
132 std::vector<PointerAccessInfo> *Info) {
133 if (F->doesNotAccessMemory())
134 // Can't do better than this.
135 return DoesNotAccessMemory;
136 ModRefBehavior MRB = getModRefBehavior(F, CallSite(), Info);
137 if (MRB != DoesNotAccessMemory && F->onlyReadsMemory())
138 return OnlyReadsMemory;
142 AliasAnalysis::ModRefResult
143 AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
144 ModRefResult Mask = ModRef;
145 ModRefBehavior MRB = getModRefBehavior(CS);
146 if (MRB == OnlyReadsMemory)
148 else if (MRB == DoesNotAccessMemory)
151 if (!AA) return Mask;
153 // If P points to a constant memory location, the call definitely could not
154 // modify the memory location.
155 if ((Mask & Mod) && AA->pointsToConstantMemory(P))
156 Mask = ModRefResult(Mask & ~Mod);
158 return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size));
161 // AliasAnalysis destructor: DO NOT move this to the header file for
162 // AliasAnalysis or else clients of the AliasAnalysis class may not depend on
163 // the AliasAnalysis.o file in the current .a file, causing alias analysis
164 // support to not be included in the tool correctly!
166 AliasAnalysis::~AliasAnalysis() {}
168 /// setTargetData - Subclasses must call this method to initialize the
169 /// AliasAnalysis interface before any other methods are called.
171 void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
172 TD = &P->getAnalysis<TargetData>();
173 AA = &P->getAnalysis<AliasAnalysis>();
176 // getAnalysisUsage - All alias analysis implementations should invoke this
177 // directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that
178 // TargetData is required by the pass.
179 void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
180 AU.addRequired<TargetData>(); // All AA's need TargetData.
181 AU.addRequired<AliasAnalysis>(); // All AA's chain
184 /// canBasicBlockModify - Return true if it is possible for execution of the
185 /// specified basic block to modify the value pointed to by Ptr.
187 bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
188 const Value *Ptr, unsigned Size) {
189 return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
192 /// canInstructionRangeModify - Return true if it is possible for the execution
193 /// of the specified instructions to modify the value pointed to by Ptr. The
194 /// instructions to consider are all of the instructions in the range of [I1,I2]
195 /// INCLUSIVE. I1 and I2 must be in the same basic block.
197 bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
198 const Instruction &I2,
199 const Value *Ptr, unsigned Size) {
200 assert(I1.getParent() == I2.getParent() &&
201 "Instructions not in same basic block!");
202 BasicBlock::iterator I = const_cast<Instruction*>(&I1);
203 BasicBlock::iterator E = const_cast<Instruction*>(&I2);
204 ++E; // Convert from inclusive to exclusive range.
206 for (; I != E; ++I) // Check every instruction in range
207 if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod)
212 // Because of the way .a files work, we must force the BasicAA implementation to
213 // be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run
214 // the risk of AliasAnalysis being used, but the default implementation not
215 // being linked into the tool that uses it.
216 DEFINING_FILE_FOR(AliasAnalysis)