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/LLVMContext.h"
34 #include "llvm/Type.h"
35 #include "llvm/Target/TargetData.h"
38 // Register the AliasAnalysis interface, providing a nice name to refer to.
39 INITIALIZE_ANALYSIS_GROUP(AliasAnalysis, "Alias Analysis", NoAA)
40 char AliasAnalysis::ID = 0;
42 //===----------------------------------------------------------------------===//
43 // Default chaining methods
44 //===----------------------------------------------------------------------===//
46 AliasAnalysis::AliasResult
47 AliasAnalysis::alias(const Location &LocA, const Location &LocB) {
48 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
49 return AA->alias(LocA, LocB);
52 bool AliasAnalysis::pointsToConstantMemory(const Location &Loc) {
53 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
54 return AA->pointsToConstantMemory(Loc);
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 CS,
69 const Location &Loc) {
70 // Don't assert AA because BasicAA calls us in order to make use of the
73 ModRefBehavior MRB = getModRefBehavior(CS);
74 if (MRB == DoesNotAccessMemory)
77 ModRefResult Mask = ModRef;
78 if (MRB == OnlyReadsMemory)
80 else if (MRB == AliasAnalysis::AccessesArguments) {
81 bool doesAlias = false;
82 for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
84 if (!isNoAlias(Location(*AI), Loc)) {
93 // If Loc is a constant memory location, the call definitely could not
94 // modify the memory location.
95 if ((Mask & Mod) && pointsToConstantMemory(Loc))
96 Mask = ModRefResult(Mask & ~Mod);
98 // If this is BasicAA, don't forward.
101 // Otherwise, fall back to the next AA in the chain. But we can merge
102 // in any mask we've managed to compute.
103 return ModRefResult(AA->getModRefInfo(CS, Loc) & Mask);
106 AliasAnalysis::ModRefResult
107 AliasAnalysis::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) {
108 // Don't assert AA because BasicAA calls us in order to make use of the
111 // If CS1 or CS2 are readnone, they don't interact.
112 ModRefBehavior CS1B = getModRefBehavior(CS1);
113 if (CS1B == DoesNotAccessMemory) return NoModRef;
115 ModRefBehavior CS2B = getModRefBehavior(CS2);
116 if (CS2B == DoesNotAccessMemory) return NoModRef;
118 // If they both only read from memory, there is no dependence.
119 if (CS1B == OnlyReadsMemory && CS2B == OnlyReadsMemory)
122 AliasAnalysis::ModRefResult Mask = ModRef;
124 // If CS1 only reads memory, the only dependence on CS2 can be
125 // from CS1 reading memory written by CS2.
126 if (CS1B == OnlyReadsMemory)
127 Mask = ModRefResult(Mask & Ref);
129 // If CS2 only access memory through arguments, accumulate the mod/ref
130 // information from CS1's references to the memory referenced by
132 if (CS2B == AccessesArguments) {
133 AliasAnalysis::ModRefResult R = NoModRef;
134 for (ImmutableCallSite::arg_iterator
135 I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) {
136 R = ModRefResult((R | getModRefInfo(CS1, *I, UnknownSize)) & Mask);
143 // If CS1 only accesses memory through arguments, check if CS2 references
144 // any of the memory referenced by CS1's arguments. If not, return NoModRef.
145 if (CS1B == AccessesArguments) {
146 AliasAnalysis::ModRefResult R = NoModRef;
147 for (ImmutableCallSite::arg_iterator
148 I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I)
149 if (getModRefInfo(CS2, *I, UnknownSize) != NoModRef) {
157 // If this is BasicAA, don't forward.
158 if (!AA) return Mask;
160 // Otherwise, fall back to the next AA in the chain. But we can merge
161 // in any mask we've managed to compute.
162 return ModRefResult(AA->getModRefInfo(CS1, CS2) & Mask);
165 AliasAnalysis::ModRefBehavior
166 AliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
167 // Don't assert AA because BasicAA calls us in order to make use of the
170 ModRefBehavior Min = UnknownModRefBehavior;
172 // Call back into the alias analysis with the other form of getModRefBehavior
173 // to see if it can give a better response.
174 if (const Function *F = CS.getCalledFunction())
175 Min = getModRefBehavior(F);
177 // If this is BasicAA, don't forward.
180 // Otherwise, fall back to the next AA in the chain. But we can merge
181 // in any result we've managed to compute.
182 return std::min(AA->getModRefBehavior(CS), Min);
185 AliasAnalysis::ModRefBehavior
186 AliasAnalysis::getModRefBehavior(const Function *F) {
187 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
188 return AA->getModRefBehavior(F);
191 //===----------------------------------------------------------------------===//
192 // AliasAnalysis non-virtual helper method implementation
193 //===----------------------------------------------------------------------===//
195 AliasAnalysis::ModRefResult
196 AliasAnalysis::getModRefInfo(const LoadInst *L, const Location &Loc) {
197 // Be conservative in the face of volatile.
201 // If the load address doesn't alias the given address, it doesn't read
202 // or write the specified memory.
203 if (!alias(Location(L->getOperand(0),
204 getTypeStoreSize(L->getType()),
205 L->getMetadata(LLVMContext::MD_tbaa)),
209 // Otherwise, a load just reads.
213 AliasAnalysis::ModRefResult
214 AliasAnalysis::getModRefInfo(const StoreInst *S, const Location &Loc) {
215 // Be conservative in the face of volatile.
219 // If the store address cannot alias the pointer in question, then the
220 // specified memory cannot be modified by the store.
221 if (!alias(Location(S->getOperand(1),
222 getTypeStoreSize(S->getOperand(0)->getType()),
223 S->getMetadata(LLVMContext::MD_tbaa)),
227 // If the pointer is a pointer to constant memory, then it could not have been
228 // modified by this store.
229 if (pointsToConstantMemory(Loc))
232 // Otherwise, a store just writes.
236 AliasAnalysis::ModRefResult
237 AliasAnalysis::getModRefInfo(const VAArgInst *V, const Location &Loc) {
238 // If the va_arg address cannot alias the pointer in question, then the
239 // specified memory cannot be accessed by the va_arg.
240 if (!alias(Location(V->getOperand(0),
242 V->getMetadata(LLVMContext::MD_tbaa)),
246 // If the pointer is a pointer to constant memory, then it could not have been
247 // modified by this va_arg.
248 if (pointsToConstantMemory(Loc))
251 // Otherwise, a va_arg reads and writes.
255 AliasAnalysis::ModRefBehavior
256 AliasAnalysis::getIntrinsicModRefBehavior(unsigned iid) {
257 #define GET_INTRINSIC_MODREF_BEHAVIOR
258 #include "llvm/Intrinsics.gen"
259 #undef GET_INTRINSIC_MODREF_BEHAVIOR
262 // AliasAnalysis destructor: DO NOT move this to the header file for
263 // AliasAnalysis or else clients of the AliasAnalysis class may not depend on
264 // the AliasAnalysis.o file in the current .a file, causing alias analysis
265 // support to not be included in the tool correctly!
267 AliasAnalysis::~AliasAnalysis() {}
269 /// InitializeAliasAnalysis - Subclasses must call this method to initialize the
270 /// AliasAnalysis interface before any other methods are called.
272 void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
273 TD = P->getAnalysisIfAvailable<TargetData>();
274 AA = &P->getAnalysis<AliasAnalysis>();
277 // getAnalysisUsage - All alias analysis implementations should invoke this
278 // directly (using AliasAnalysis::getAnalysisUsage(AU)).
279 void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
280 AU.addRequired<AliasAnalysis>(); // All AA's chain
283 /// getTypeStoreSize - Return the TargetData store size for the given type,
284 /// if known, or a conservative value otherwise.
286 unsigned AliasAnalysis::getTypeStoreSize(const Type *Ty) {
287 return TD ? TD->getTypeStoreSize(Ty) : UnknownSize;
290 /// canBasicBlockModify - Return true if it is possible for execution of the
291 /// specified basic block to modify the value pointed to by Ptr.
293 bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
294 const Location &Loc) {
295 return canInstructionRangeModify(BB.front(), BB.back(), Loc);
298 /// canInstructionRangeModify - Return true if it is possible for the execution
299 /// of the specified instructions to modify the value pointed to by Ptr. The
300 /// instructions to consider are all of the instructions in the range of [I1,I2]
301 /// INCLUSIVE. I1 and I2 must be in the same basic block.
303 bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
304 const Instruction &I2,
305 const Location &Loc) {
306 assert(I1.getParent() == I2.getParent() &&
307 "Instructions not in same basic block!");
308 BasicBlock::const_iterator I = &I1;
309 BasicBlock::const_iterator E = &I2;
310 ++E; // Convert from inclusive to exclusive range.
312 for (; I != E; ++I) // Check every instruction in range
313 if (getModRefInfo(I, Loc) & Mod)
318 /// isNoAliasCall - Return true if this pointer is returned by a noalias
320 bool llvm::isNoAliasCall(const Value *V) {
321 if (isa<CallInst>(V) || isa<InvokeInst>(V))
322 return ImmutableCallSite(cast<Instruction>(V))
323 .paramHasAttr(0, Attribute::NoAlias);
327 /// isIdentifiedObject - Return true if this pointer refers to a distinct and
328 /// identifiable object. This returns true for:
329 /// Global Variables and Functions (but not Global Aliases)
330 /// Allocas and Mallocs
331 /// ByVal and NoAlias Arguments
334 bool llvm::isIdentifiedObject(const Value *V) {
335 if (isa<AllocaInst>(V))
337 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
339 if (isNoAliasCall(V))
341 if (const Argument *A = dyn_cast<Argument>(V))
342 return A->hasNoAliasAttr() || A->hasByValAttr();
346 // Because of the way .a files work, we must force the BasicAA implementation to
347 // be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run
348 // the risk of AliasAnalysis being used, but the default implementation not
349 // being linked into the tool that uses it.
350 DEFINING_FILE_FOR(AliasAnalysis)