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,
54 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
55 return AA->pointsToConstantMemory(Loc, OrLocal);
58 void AliasAnalysis::deleteValue(Value *V) {
59 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
63 void AliasAnalysis::copyValue(Value *From, Value *To) {
64 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
65 AA->copyValue(From, To);
68 AliasAnalysis::ModRefResult
69 AliasAnalysis::getModRefInfo(ImmutableCallSite CS,
70 const Location &Loc) {
71 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
73 ModRefBehavior MRB = getModRefBehavior(CS);
74 if (MRB == DoesNotAccessMemory)
77 ModRefResult Mask = ModRef;
78 if (onlyReadsMemory(MRB))
81 if (onlyAccessesArgPointees(MRB)) {
82 bool doesAlias = false;
83 if (doesAccessArgPointees(MRB))
84 for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
86 if (!isNoAlias(Location(*AI), Loc)) {
95 // If Loc is a constant memory location, the call definitely could not
96 // modify the memory location.
97 if ((Mask & Mod) && pointsToConstantMemory(Loc))
98 Mask = ModRefResult(Mask & ~Mod);
100 // If this is the end of the chain, don't forward.
101 if (!AA) return Mask;
103 // Otherwise, fall back to the next AA in the chain. But we can merge
104 // in any mask we've managed to compute.
105 return ModRefResult(AA->getModRefInfo(CS, Loc) & Mask);
108 AliasAnalysis::ModRefResult
109 AliasAnalysis::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) {
110 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
112 // If CS1 or CS2 are readnone, they don't interact.
113 ModRefBehavior CS1B = getModRefBehavior(CS1);
114 if (CS1B == DoesNotAccessMemory) return NoModRef;
116 ModRefBehavior CS2B = getModRefBehavior(CS2);
117 if (CS2B == DoesNotAccessMemory) return NoModRef;
119 // If they both only read from memory, there is no dependence.
120 if (onlyReadsMemory(CS1B) && onlyReadsMemory(CS2B))
123 AliasAnalysis::ModRefResult Mask = ModRef;
125 // If CS1 only reads memory, the only dependence on CS2 can be
126 // from CS1 reading memory written by CS2.
127 if (onlyReadsMemory(CS1B))
128 Mask = ModRefResult(Mask & Ref);
130 // If CS2 only access memory through arguments, accumulate the mod/ref
131 // information from CS1's references to the memory referenced by
133 if (onlyAccessesArgPointees(CS2B)) {
134 AliasAnalysis::ModRefResult R = NoModRef;
135 if (doesAccessArgPointees(CS2B))
136 for (ImmutableCallSite::arg_iterator
137 I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) {
138 R = ModRefResult((R | getModRefInfo(CS1, *I, UnknownSize)) & Mask);
145 // If CS1 only accesses memory through arguments, check if CS2 references
146 // any of the memory referenced by CS1's arguments. If not, return NoModRef.
147 if (onlyAccessesArgPointees(CS1B)) {
148 AliasAnalysis::ModRefResult R = NoModRef;
149 if (doesAccessArgPointees(CS1B))
150 for (ImmutableCallSite::arg_iterator
151 I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I)
152 if (getModRefInfo(CS2, *I, UnknownSize) != NoModRef) {
160 // If this is the end of the chain, don't forward.
161 if (!AA) return Mask;
163 // Otherwise, fall back to the next AA in the chain. But we can merge
164 // in any mask we've managed to compute.
165 return ModRefResult(AA->getModRefInfo(CS1, CS2) & Mask);
168 AliasAnalysis::ModRefBehavior
169 AliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
170 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
172 ModRefBehavior Min = UnknownModRefBehavior;
174 // Call back into the alias analysis with the other form of getModRefBehavior
175 // to see if it can give a better response.
176 if (const Function *F = CS.getCalledFunction())
177 Min = getModRefBehavior(F);
179 // If this is the end of the chain, don't forward.
182 // Otherwise, fall back to the next AA in the chain. But we can merge
183 // in any result we've managed to compute.
184 return ModRefBehavior(AA->getModRefBehavior(CS) & Min);
187 AliasAnalysis::ModRefBehavior
188 AliasAnalysis::getModRefBehavior(const Function *F) {
189 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
190 return AA->getModRefBehavior(F);
193 //===----------------------------------------------------------------------===//
194 // AliasAnalysis non-virtual helper method implementation
195 //===----------------------------------------------------------------------===//
197 AliasAnalysis::ModRefResult
198 AliasAnalysis::getModRefInfo(const LoadInst *L, const Location &Loc) {
199 // Be conservative in the face of volatile.
203 // If the load address doesn't alias the given address, it doesn't read
204 // or write the specified memory.
205 if (!alias(Location(L->getOperand(0),
206 getTypeStoreSize(L->getType()),
207 L->getMetadata(LLVMContext::MD_tbaa)),
211 // Otherwise, a load just reads.
215 AliasAnalysis::ModRefResult
216 AliasAnalysis::getModRefInfo(const StoreInst *S, const Location &Loc) {
217 // Be conservative in the face of volatile.
221 // If the store address cannot alias the pointer in question, then the
222 // specified memory cannot be modified by the store.
223 if (!alias(Location(S->getOperand(1),
224 getTypeStoreSize(S->getOperand(0)->getType()),
225 S->getMetadata(LLVMContext::MD_tbaa)),
229 // If the pointer is a pointer to constant memory, then it could not have been
230 // modified by this store.
231 if (pointsToConstantMemory(Loc))
234 // Otherwise, a store just writes.
238 AliasAnalysis::ModRefResult
239 AliasAnalysis::getModRefInfo(const VAArgInst *V, const Location &Loc) {
240 // If the va_arg address cannot alias the pointer in question, then the
241 // specified memory cannot be accessed by the va_arg.
242 if (!alias(Location(V->getOperand(0),
244 V->getMetadata(LLVMContext::MD_tbaa)),
248 // If the pointer is a pointer to constant memory, then it could not have been
249 // modified by this va_arg.
250 if (pointsToConstantMemory(Loc))
253 // Otherwise, a va_arg reads and writes.
257 // AliasAnalysis destructor: DO NOT move this to the header file for
258 // AliasAnalysis or else clients of the AliasAnalysis class may not depend on
259 // the AliasAnalysis.o file in the current .a file, causing alias analysis
260 // support to not be included in the tool correctly!
262 AliasAnalysis::~AliasAnalysis() {}
264 /// InitializeAliasAnalysis - Subclasses must call this method to initialize the
265 /// AliasAnalysis interface before any other methods are called.
267 void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
268 TD = P->getAnalysisIfAvailable<TargetData>();
269 AA = &P->getAnalysis<AliasAnalysis>();
272 // getAnalysisUsage - All alias analysis implementations should invoke this
273 // directly (using AliasAnalysis::getAnalysisUsage(AU)).
274 void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
275 AU.addRequired<AliasAnalysis>(); // All AA's chain
278 /// getTypeStoreSize - Return the TargetData store size for the given type,
279 /// if known, or a conservative value otherwise.
281 uint64_t AliasAnalysis::getTypeStoreSize(const Type *Ty) {
282 return TD ? TD->getTypeStoreSize(Ty) : UnknownSize;
285 /// canBasicBlockModify - Return true if it is possible for execution of the
286 /// specified basic block to modify the value pointed to by Ptr.
288 bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
289 const Location &Loc) {
290 return canInstructionRangeModify(BB.front(), BB.back(), Loc);
293 /// canInstructionRangeModify - Return true if it is possible for the execution
294 /// of the specified instructions to modify the value pointed to by Ptr. The
295 /// instructions to consider are all of the instructions in the range of [I1,I2]
296 /// INCLUSIVE. I1 and I2 must be in the same basic block.
298 bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
299 const Instruction &I2,
300 const Location &Loc) {
301 assert(I1.getParent() == I2.getParent() &&
302 "Instructions not in same basic block!");
303 BasicBlock::const_iterator I = &I1;
304 BasicBlock::const_iterator E = &I2;
305 ++E; // Convert from inclusive to exclusive range.
307 for (; I != E; ++I) // Check every instruction in range
308 if (getModRefInfo(I, Loc) & Mod)
313 /// isNoAliasCall - Return true if this pointer is returned by a noalias
315 bool llvm::isNoAliasCall(const Value *V) {
316 if (isa<CallInst>(V) || isa<InvokeInst>(V))
317 return ImmutableCallSite(cast<Instruction>(V))
318 .paramHasAttr(0, Attribute::NoAlias);
322 /// isIdentifiedObject - Return true if this pointer refers to a distinct and
323 /// identifiable object. This returns true for:
324 /// Global Variables and Functions (but not Global Aliases)
325 /// Allocas and Mallocs
326 /// ByVal and NoAlias Arguments
329 bool llvm::isIdentifiedObject(const Value *V) {
330 if (isa<AllocaInst>(V))
332 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
334 if (isNoAliasCall(V))
336 if (const Argument *A = dyn_cast<Argument>(V))
337 return A->hasNoAliasAttr() || A->hasByValAttr();