blob: 2ed5f37d415271c06c8c89aeebec9e5cafd351ff [file] [log] [blame]
Chris Lattner53ad0ed2002-08-22 18:25:32 +00001//===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner53ad0ed2002-08-22 18:25:32 +00009//
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.
12//
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,
17// etc.
18//
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
23// easy cases.
24//
25//===----------------------------------------------------------------------===//
26
Chris Lattnerd501c132003-02-26 19:41:54 +000027#include "llvm/Analysis/AliasAnalysis.h"
Reid Spencer6df60a92006-06-07 20:00:19 +000028#include "llvm/Pass.h"
Chris Lattner53ad0ed2002-08-22 18:25:32 +000029#include "llvm/BasicBlock.h"
Duncan Sandsdff67102007-12-01 07:51:45 +000030#include "llvm/Function.h"
Owen Andersoncd895252009-02-03 06:27:22 +000031#include "llvm/IntrinsicInst.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000032#include "llvm/Instructions.h"
Chris Lattner5b3a4552005-03-17 15:38:16 +000033#include "llvm/Type.h"
Chris Lattner14ac8772003-02-26 19:26:51 +000034#include "llvm/Target/TargetData.h"
Chris Lattner992860c2004-03-15 04:07:29 +000035using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000036
Chris Lattner53ad0ed2002-08-22 18:25:32 +000037// Register the AliasAnalysis interface, providing a nice name to refer to.
Dan Gohman844731a2008-05-13 00:00:25 +000038static RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis");
Devang Patel19974732007-05-03 01:11:54 +000039char AliasAnalysis::ID = 0;
Chris Lattner53ad0ed2002-08-22 18:25:32 +000040
Chris Lattner5a24d702004-05-23 21:15:48 +000041//===----------------------------------------------------------------------===//
42// Default chaining methods
43//===----------------------------------------------------------------------===//
44
45AliasAnalysis::AliasResult
46AliasAnalysis::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);
50}
51
Chris Lattner5a24d702004-05-23 21:15:48 +000052bool AliasAnalysis::pointsToConstantMemory(const Value *P) {
53 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
54 return AA->pointsToConstantMemory(P);
55}
56
Chris Lattner5a24d702004-05-23 21:15:48 +000057void AliasAnalysis::deleteValue(Value *V) {
58 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
59 AA->deleteValue(V);
60}
61
62void AliasAnalysis::copyValue(Value *From, Value *To) {
63 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
64 AA->copyValue(From, To);
65}
66
67AliasAnalysis::ModRefResult
Dan Gohman79fca6f2010-08-03 21:48:53 +000068AliasAnalysis::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) {
Chris Lattner5a24d702004-05-23 21:15:48 +000069 // FIXME: we can do better.
70 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
71 return AA->getModRefInfo(CS1, CS2);
72}
73
74
75//===----------------------------------------------------------------------===//
76// AliasAnalysis non-virtual helper method implementation
77//===----------------------------------------------------------------------===//
78
Chris Lattner14ac8772003-02-26 19:26:51 +000079AliasAnalysis::ModRefResult
Dan Gohman79fca6f2010-08-03 21:48:53 +000080AliasAnalysis::getModRefInfo(const LoadInst *L, const Value *P, unsigned Size) {
Dan Gohman14a498a2010-08-03 17:27:43 +000081 // 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))
84 return NoModRef;
85
86 // Be conservative in the face of volatile.
87 if (L->isVolatile())
88 return ModRef;
89
90 // Otherwise, a load just reads.
91 return Ref;
Chris Lattner53ad0ed2002-08-22 18:25:32 +000092}
93
Chris Lattner14ac8772003-02-26 19:26:51 +000094AliasAnalysis::ModRefResult
Dan Gohman79fca6f2010-08-03 21:48:53 +000095AliasAnalysis::getModRefInfo(const StoreInst *S, const Value *P, unsigned Size) {
Chris Lattnerf4d904d2004-01-30 22:16:42 +000096 // If the stored address cannot alias the pointer in question, then the
97 // pointer cannot be modified by the store.
Duncan Sands514ab342007-11-01 20:53:16 +000098 if (!alias(S->getOperand(1),
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +000099 getTypeStoreSize(S->getOperand(0)->getType()), P, Size))
Chris Lattnerf4d904d2004-01-30 22:16:42 +0000100 return NoModRef;
101
Dan Gohman14a498a2010-08-03 17:27:43 +0000102 // Be conservative in the face of volatile.
103 if (S->isVolatile())
104 return ModRef;
105
Chris Lattnerf4d904d2004-01-30 22:16:42 +0000106 // If the pointer is a pointer to constant memory, then it could not have been
107 // modified by this store.
Dan Gohman14a498a2010-08-03 17:27:43 +0000108 if (pointsToConstantMemory(P))
109 return NoModRef;
110
111 // Otherwise, a store just writes.
112 return Mod;
Chris Lattner14ac8772003-02-26 19:26:51 +0000113}
114
Duncan Sandsdff67102007-12-01 07:51:45 +0000115AliasAnalysis::ModRefBehavior
Dan Gohman79fca6f2010-08-03 21:48:53 +0000116AliasAnalysis::getModRefBehavior(ImmutableCallSite CS,
Duncan Sandsdff67102007-12-01 07:51:45 +0000117 std::vector<PointerAccessInfo> *Info) {
Duncan Sands7915cbe2007-12-12 16:01:40 +0000118 if (CS.doesNotAccessMemory())
Duncan Sandsdff67102007-12-01 07:51:45 +0000119 // Can't do better than this.
120 return DoesNotAccessMemory;
Owen Andersone7942202009-02-05 23:36:27 +0000121 ModRefBehavior MRB = getModRefBehavior(CS.getCalledFunction(), Info);
Duncan Sands7915cbe2007-12-12 16:01:40 +0000122 if (MRB != DoesNotAccessMemory && CS.onlyReadsMemory())
Duncan Sandsdff67102007-12-01 07:51:45 +0000123 return OnlyReadsMemory;
124 return MRB;
125}
126
127AliasAnalysis::ModRefBehavior
Dan Gohman79fca6f2010-08-03 21:48:53 +0000128AliasAnalysis::getModRefBehavior(const Function *F,
Duncan Sandsdff67102007-12-01 07:51:45 +0000129 std::vector<PointerAccessInfo> *Info) {
Duncan Sandsddd8c452009-02-13 17:32:26 +0000130 if (F) {
131 if (F->doesNotAccessMemory())
132 // Can't do better than this.
133 return DoesNotAccessMemory;
Duncan Sandsd869b382009-02-14 10:56:35 +0000134 if (F->onlyReadsMemory())
Duncan Sandsddd8c452009-02-13 17:32:26 +0000135 return OnlyReadsMemory;
Duncan Sands7c422ac2010-01-06 08:45:52 +0000136 if (unsigned id = F->getIntrinsicID())
Dan Gohman79fca6f2010-08-03 21:48:53 +0000137 return getIntrinsicModRefBehavior(id);
Duncan Sands7c422ac2010-01-06 08:45:52 +0000138 }
139 return UnknownModRefBehavior;
140}
141
Dan Gohman79fca6f2010-08-03 21:48:53 +0000142AliasAnalysis::ModRefBehavior
143AliasAnalysis::getIntrinsicModRefBehavior(unsigned iid) {
Duncan Sandsd869b382009-02-14 10:56:35 +0000144#define GET_INTRINSIC_MODREF_BEHAVIOR
145#include "llvm/Intrinsics.gen"
146#undef GET_INTRINSIC_MODREF_BEHAVIOR
Duncan Sandsdff67102007-12-01 07:51:45 +0000147}
148
Chris Lattner992860c2004-03-15 04:07:29 +0000149AliasAnalysis::ModRefResult
Dan Gohman79fca6f2010-08-03 21:48:53 +0000150AliasAnalysis::getModRefInfo(ImmutableCallSite CS,
151 const Value *P, unsigned Size) {
Duncan Sandsdff67102007-12-01 07:51:45 +0000152 ModRefBehavior MRB = getModRefBehavior(CS);
Duncan Sandsddd8c452009-02-13 17:32:26 +0000153 if (MRB == DoesNotAccessMemory)
Duncan Sandsdff67102007-12-01 07:51:45 +0000154 return NoModRef;
Chris Lattner403ac2e2009-11-23 16:46:41 +0000155
156 ModRefResult Mask = ModRef;
157 if (MRB == OnlyReadsMemory)
Duncan Sandsddd8c452009-02-13 17:32:26 +0000158 Mask = Ref;
Owen Andersone7942202009-02-05 23:36:27 +0000159 else if (MRB == AliasAnalysis::AccessesArguments) {
160 bool doesAlias = false;
Dan Gohman79fca6f2010-08-03 21:48:53 +0000161 for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
Owen Andersone7942202009-02-05 23:36:27 +0000162 AI != AE; ++AI)
Chris Lattner403ac2e2009-11-23 16:46:41 +0000163 if (!isNoAlias(*AI, ~0U, P, Size)) {
Owen Andersone7942202009-02-05 23:36:27 +0000164 doesAlias = true;
165 break;
166 }
Duncan Sandsddd8c452009-02-13 17:32:26 +0000167
Owen Andersone7942202009-02-05 23:36:27 +0000168 if (!doesAlias)
169 return NoModRef;
170 }
Chris Lattner992860c2004-03-15 04:07:29 +0000171
Chris Lattner5a24d702004-05-23 21:15:48 +0000172 if (!AA) return Mask;
173
Chris Lattner992860c2004-03-15 04:07:29 +0000174 // If P points to a constant memory location, the call definitely could not
175 // modify the memory location.
Chris Lattner5a24d702004-05-23 21:15:48 +0000176 if ((Mask & Mod) && AA->pointsToConstantMemory(P))
Chris Lattnerd433bde2005-03-23 22:06:41 +0000177 Mask = ModRefResult(Mask & ~Mod);
Chris Lattner992860c2004-03-15 04:07:29 +0000178
Chris Lattner5a24d702004-05-23 21:15:48 +0000179 return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size));
Chris Lattner992860c2004-03-15 04:07:29 +0000180}
181
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000182// AliasAnalysis destructor: DO NOT move this to the header file for
183// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
184// the AliasAnalysis.o file in the current .a file, causing alias analysis
185// support to not be included in the tool correctly!
186//
187AliasAnalysis::~AliasAnalysis() {}
188
Dan Gohman5a56bf62008-05-30 00:02:02 +0000189/// InitializeAliasAnalysis - Subclasses must call this method to initialize the
Chris Lattner14ac8772003-02-26 19:26:51 +0000190/// AliasAnalysis interface before any other methods are called.
191///
192void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000193 TD = P->getAnalysisIfAvailable<TargetData>();
Chris Lattner5a24d702004-05-23 21:15:48 +0000194 AA = &P->getAnalysis<AliasAnalysis>();
Chris Lattner14ac8772003-02-26 19:26:51 +0000195}
196
197// getAnalysisUsage - All alias analysis implementations should invoke this
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000198// directly (using AliasAnalysis::getAnalysisUsage(AU)).
Chris Lattner14ac8772003-02-26 19:26:51 +0000199void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5a24d702004-05-23 21:15:48 +0000200 AU.addRequired<AliasAnalysis>(); // All AA's chain
Chris Lattner14ac8772003-02-26 19:26:51 +0000201}
202
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000203/// getTypeStoreSize - Return the TargetData store size for the given type,
204/// if known, or a conservative value otherwise.
205///
206unsigned AliasAnalysis::getTypeStoreSize(const Type *Ty) {
207 return TD ? TD->getTypeStoreSize(Ty) : ~0u;
208}
209
Chris Lattnerf9355f62002-08-22 22:46:39 +0000210/// canBasicBlockModify - Return true if it is possible for execution of the
211/// specified basic block to modify the value pointed to by Ptr.
212///
Chris Lattner14ac8772003-02-26 19:26:51 +0000213bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
214 const Value *Ptr, unsigned Size) {
215 return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000216}
217
Chris Lattnerf9355f62002-08-22 22:46:39 +0000218/// canInstructionRangeModify - Return true if it is possible for the execution
219/// of the specified instructions to modify the value pointed to by Ptr. The
220/// instructions to consider are all of the instructions in the range of [I1,I2]
221/// INCLUSIVE. I1 and I2 must be in the same basic block.
222///
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000223bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
224 const Instruction &I2,
Chris Lattner14ac8772003-02-26 19:26:51 +0000225 const Value *Ptr, unsigned Size) {
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000226 assert(I1.getParent() == I2.getParent() &&
227 "Instructions not in same basic block!");
Dan Gohman79fca6f2010-08-03 21:48:53 +0000228 BasicBlock::const_iterator I = &I1;
229 BasicBlock::const_iterator E = &I2;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000230 ++E; // Convert from inclusive to exclusive range.
231
Chris Lattner14ac8772003-02-26 19:26:51 +0000232 for (; I != E; ++I) // Check every instruction in range
Dan Gohman79fca6f2010-08-03 21:48:53 +0000233 if (getModRefInfo(I, Ptr, Size) & Mod)
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000234 return true;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000235 return false;
236}
237
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000238/// isNoAliasCall - Return true if this pointer is returned by a noalias
239/// function.
240bool llvm::isNoAliasCall(const Value *V) {
241 if (isa<CallInst>(V) || isa<InvokeInst>(V))
Dan Gohman79fca6f2010-08-03 21:48:53 +0000242 return ImmutableCallSite(cast<Instruction>(V))
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000243 .paramHasAttr(0, Attribute::NoAlias);
244 return false;
245}
246
247/// isIdentifiedObject - Return true if this pointer refers to a distinct and
248/// identifiable object. This returns true for:
Dan Gohman5753a4a2009-08-27 17:52:56 +0000249/// Global Variables and Functions (but not Global Aliases)
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000250/// Allocas and Mallocs
Dan Gohman9e86f432010-07-07 14:27:09 +0000251/// ByVal and NoAlias Arguments
252/// NoAlias returns
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000253///
Dan Gohman9e86f432010-07-07 14:27:09 +0000254bool llvm::isIdentifiedObject(const Value *V) {
Dan Gohman6be2bd52010-06-29 00:50:39 +0000255 if (isa<AllocaInst>(V))
Dan Gohman5753a4a2009-08-27 17:52:56 +0000256 return true;
257 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000258 return true;
Dan Gohman9e86f432010-07-07 14:27:09 +0000259 if (isNoAliasCall(V))
260 return true;
261 if (const Argument *A = dyn_cast<Argument>(V))
262 return A->hasNoAliasAttr() || A->hasByValAttr();
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000263 return false;
264}
265
Chris Lattnerd501c132003-02-26 19:41:54 +0000266// Because of the way .a files work, we must force the BasicAA implementation to
267// be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run
268// the risk of AliasAnalysis being used, but the default implementation not
269// being linked into the tool that uses it.
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000270DEFINING_FILE_FOR(AliasAnalysis)