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/IntrinsicInst.h"
32 #include "llvm/Instructions.h"
33 #include "llvm/Type.h"
34 #include "llvm/Target/TargetData.h"
37 // Register the AliasAnalysis interface, providing a nice name to refer to.
38 static RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis");
39 char AliasAnalysis::ID = 0;
41 //===----------------------------------------------------------------------===//
42 // Default chaining methods
43 //===----------------------------------------------------------------------===//
45 AliasAnalysis::AliasResult
46 AliasAnalysis::alias(const Value *V1, unsigned V1Size,
47 const Value *V2, unsigned V2Size) {
48 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
49 return AA->alias(V1, V1Size, V2, V2Size);
52 bool AliasAnalysis::pointsToConstantMemory(const Value *P) {
53 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
54 return AA->pointsToConstantMemory(P);
57 void AliasAnalysis::deleteValue(Value *V) {
58 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
62 void AliasAnalysis::copyValue(Value *From, Value *To) {
63 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
64 AA->copyValue(From, To);
67 AliasAnalysis::ModRefResult
68 AliasAnalysis::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) {
69 // FIXME: we can do better.
70 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
71 return AA->getModRefInfo(CS1, CS2);
75 //===----------------------------------------------------------------------===//
76 // AliasAnalysis non-virtual helper method implementation
77 //===----------------------------------------------------------------------===//
79 AliasAnalysis::ModRefResult
80 AliasAnalysis::getModRefInfo(const LoadInst *L, const Value *P, unsigned Size) {
81 // If the load address doesn't alias the given address, it doesn't read
82 // or write the specified memory.
83 if (!alias(L->getOperand(0), getTypeStoreSize(L->getType()), P, Size))
86 // Be conservative in the face of volatile.
90 // Otherwise, a load just reads.
94 AliasAnalysis::ModRefResult
95 AliasAnalysis::getModRefInfo(const StoreInst *S, const Value *P, unsigned Size) {
96 // If the stored address cannot alias the pointer in question, then the
97 // pointer cannot be modified by the store.
98 if (!alias(S->getOperand(1),
99 getTypeStoreSize(S->getOperand(0)->getType()), P, Size))
102 // Be conservative in the face of volatile.
106 // If the pointer is a pointer to constant memory, then it could not have been
107 // modified by this store.
108 if (pointsToConstantMemory(P))
111 // Otherwise, a store just writes.
115 AliasAnalysis::ModRefBehavior
116 AliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
117 if (CS.doesNotAccessMemory())
118 // Can't do better than this.
119 return DoesNotAccessMemory;
120 ModRefBehavior MRB = getModRefBehavior(CS.getCalledFunction());
121 if (MRB != DoesNotAccessMemory && CS.onlyReadsMemory())
122 return OnlyReadsMemory;
126 AliasAnalysis::ModRefBehavior
127 AliasAnalysis::getModRefBehavior(const Function *F) {
129 if (F->doesNotAccessMemory())
130 // Can't do better than this.
131 return DoesNotAccessMemory;
132 if (F->onlyReadsMemory())
133 return OnlyReadsMemory;
134 if (unsigned id = F->getIntrinsicID())
135 return getIntrinsicModRefBehavior(id);
137 return UnknownModRefBehavior;
140 AliasAnalysis::ModRefBehavior
141 AliasAnalysis::getIntrinsicModRefBehavior(unsigned iid) {
142 #define GET_INTRINSIC_MODREF_BEHAVIOR
143 #include "llvm/Intrinsics.gen"
144 #undef GET_INTRINSIC_MODREF_BEHAVIOR
147 AliasAnalysis::ModRefResult
148 AliasAnalysis::getModRefInfo(ImmutableCallSite CS,
149 const Value *P, unsigned Size) {
150 ModRefBehavior MRB = getModRefBehavior(CS);
151 if (MRB == DoesNotAccessMemory)
154 ModRefResult Mask = ModRef;
155 if (MRB == OnlyReadsMemory)
157 else if (MRB == AliasAnalysis::AccessesArguments) {
158 bool doesAlias = false;
159 for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
161 if (!isNoAlias(*AI, ~0U, P, Size)) {
170 if (!AA) return Mask;
172 // If P points to a constant memory location, the call definitely could not
173 // modify the memory location.
174 if ((Mask & Mod) && AA->pointsToConstantMemory(P))
175 Mask = ModRefResult(Mask & ~Mod);
177 return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size));
180 // AliasAnalysis destructor: DO NOT move this to the header file for
181 // AliasAnalysis or else clients of the AliasAnalysis class may not depend on
182 // the AliasAnalysis.o file in the current .a file, causing alias analysis
183 // support to not be included in the tool correctly!
185 AliasAnalysis::~AliasAnalysis() {}
187 /// InitializeAliasAnalysis - Subclasses must call this method to initialize the
188 /// AliasAnalysis interface before any other methods are called.
190 void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
191 TD = P->getAnalysisIfAvailable<TargetData>();
192 AA = &P->getAnalysis<AliasAnalysis>();
195 // getAnalysisUsage - All alias analysis implementations should invoke this
196 // directly (using AliasAnalysis::getAnalysisUsage(AU)).
197 void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
198 AU.addRequired<AliasAnalysis>(); // All AA's chain
201 /// getTypeStoreSize - Return the TargetData store size for the given type,
202 /// if known, or a conservative value otherwise.
204 unsigned AliasAnalysis::getTypeStoreSize(const Type *Ty) {
205 return TD ? TD->getTypeStoreSize(Ty) : ~0u;
208 /// canBasicBlockModify - Return true if it is possible for execution of the
209 /// specified basic block to modify the value pointed to by Ptr.
211 bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
212 const Value *Ptr, unsigned Size) {
213 return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
216 /// canInstructionRangeModify - Return true if it is possible for the execution
217 /// of the specified instructions to modify the value pointed to by Ptr. The
218 /// instructions to consider are all of the instructions in the range of [I1,I2]
219 /// INCLUSIVE. I1 and I2 must be in the same basic block.
221 bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
222 const Instruction &I2,
223 const Value *Ptr, unsigned Size) {
224 assert(I1.getParent() == I2.getParent() &&
225 "Instructions not in same basic block!");
226 BasicBlock::const_iterator I = &I1;
227 BasicBlock::const_iterator E = &I2;
228 ++E; // Convert from inclusive to exclusive range.
230 for (; I != E; ++I) // Check every instruction in range
231 if (getModRefInfo(I, Ptr, Size) & Mod)
236 /// isNoAliasCall - Return true if this pointer is returned by a noalias
238 bool llvm::isNoAliasCall(const Value *V) {
239 if (isa<CallInst>(V) || isa<InvokeInst>(V))
240 return ImmutableCallSite(cast<Instruction>(V))
241 .paramHasAttr(0, Attribute::NoAlias);
245 /// isIdentifiedObject - Return true if this pointer refers to a distinct and
246 /// identifiable object. This returns true for:
247 /// Global Variables and Functions (but not Global Aliases)
248 /// Allocas and Mallocs
249 /// ByVal and NoAlias Arguments
252 bool llvm::isIdentifiedObject(const Value *V) {
253 if (isa<AllocaInst>(V))
255 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
257 if (isNoAliasCall(V))
259 if (const Argument *A = dyn_cast<Argument>(V))
260 return A->hasNoAliasAttr() || A->hasByValAttr();
264 // Because of the way .a files work, we must force the BasicAA implementation to
265 // be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run
266 // the risk of AliasAnalysis being used, but the default implementation not
267 // being linked into the tool that uses it.
268 DEFINING_FILE_FOR(AliasAnalysis)