blob: d513ebbc2a9d6bef655a18b056dd34ae19ba543f [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"
Dan Gohmanb2143b62010-09-14 21:25:10 +000033#include "llvm/LLVMContext.h"
Chris Lattner5b3a4552005-03-17 15:38:16 +000034#include "llvm/Type.h"
Chris Lattner14ac8772003-02-26 19:26:51 +000035#include "llvm/Target/TargetData.h"
Chris Lattner992860c2004-03-15 04:07:29 +000036using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000037
Chris Lattner53ad0ed2002-08-22 18:25:32 +000038// Register the AliasAnalysis interface, providing a nice name to refer to.
Owen Anderson081c34b2010-10-19 17:21:58 +000039INITIALIZE_ANALYSIS_GROUP(AliasAnalysis, "Alias Analysis", NoAA)
Devang Patel19974732007-05-03 01:11:54 +000040char AliasAnalysis::ID = 0;
Chris Lattner53ad0ed2002-08-22 18:25:32 +000041
Chris Lattner5a24d702004-05-23 21:15:48 +000042//===----------------------------------------------------------------------===//
43// Default chaining methods
44//===----------------------------------------------------------------------===//
45
46AliasAnalysis::AliasResult
Dan Gohmanb2143b62010-09-14 21:25:10 +000047AliasAnalysis::alias(const Location &LocA, const Location &LocB) {
Chris Lattner5a24d702004-05-23 21:15:48 +000048 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohmanb2143b62010-09-14 21:25:10 +000049 return AA->alias(LocA, LocB);
Chris Lattner5a24d702004-05-23 21:15:48 +000050}
51
Dan Gohmana25e5db2010-11-08 16:45:26 +000052bool AliasAnalysis::pointsToConstantMemory(const Location &Loc,
53 bool OrLocal) {
Chris Lattner5a24d702004-05-23 21:15:48 +000054 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohmana25e5db2010-11-08 16:45:26 +000055 return AA->pointsToConstantMemory(Loc, OrLocal);
Chris Lattner5a24d702004-05-23 21:15:48 +000056}
57
Chris Lattner5a24d702004-05-23 21:15:48 +000058void AliasAnalysis::deleteValue(Value *V) {
59 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
60 AA->deleteValue(V);
61}
62
63void AliasAnalysis::copyValue(Value *From, Value *To) {
64 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
65 AA->copyValue(From, To);
66}
67
68AliasAnalysis::ModRefResult
Dan Gohman6ce9d8b2010-08-06 01:25:49 +000069AliasAnalysis::getModRefInfo(ImmutableCallSite CS,
Dan Gohmanb2143b62010-09-14 21:25:10 +000070 const Location &Loc) {
Dan Gohman852dda42010-10-25 16:28:57 +000071 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman6ce9d8b2010-08-06 01:25:49 +000072
73 ModRefBehavior MRB = getModRefBehavior(CS);
74 if (MRB == DoesNotAccessMemory)
75 return NoModRef;
76
77 ModRefResult Mask = ModRef;
78 if (MRB == OnlyReadsMemory)
79 Mask = Ref;
80 else if (MRB == AliasAnalysis::AccessesArguments) {
81 bool doesAlias = false;
82 for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
83 AI != AE; ++AI)
Dan Gohmanb2143b62010-09-14 21:25:10 +000084 if (!isNoAlias(Location(*AI), Loc)) {
Dan Gohman6ce9d8b2010-08-06 01:25:49 +000085 doesAlias = true;
86 break;
87 }
88
89 if (!doesAlias)
90 return NoModRef;
91 }
92
Dan Gohmanb2143b62010-09-14 21:25:10 +000093 // If Loc is a constant memory location, the call definitely could not
Dan Gohman6ce9d8b2010-08-06 01:25:49 +000094 // modify the memory location.
Dan Gohmanb2143b62010-09-14 21:25:10 +000095 if ((Mask & Mod) && pointsToConstantMemory(Loc))
Dan Gohman6ce9d8b2010-08-06 01:25:49 +000096 Mask = ModRefResult(Mask & ~Mod);
97
Dan Gohmane46a3882010-10-25 16:29:52 +000098 // If this is the end of the chain, don't forward.
Dan Gohman6ce9d8b2010-08-06 01:25:49 +000099 if (!AA) return Mask;
100
101 // Otherwise, fall back to the next AA in the chain. But we can merge
102 // in any mask we've managed to compute.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000103 return ModRefResult(AA->getModRefInfo(CS, Loc) & Mask);
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000104}
105
106AliasAnalysis::ModRefResult
Dan Gohman79fca6f2010-08-03 21:48:53 +0000107AliasAnalysis::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) {
Dan Gohman852dda42010-10-25 16:28:57 +0000108 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000109
110 // If CS1 or CS2 are readnone, they don't interact.
111 ModRefBehavior CS1B = getModRefBehavior(CS1);
112 if (CS1B == DoesNotAccessMemory) return NoModRef;
113
114 ModRefBehavior CS2B = getModRefBehavior(CS2);
115 if (CS2B == DoesNotAccessMemory) return NoModRef;
116
117 // If they both only read from memory, there is no dependence.
118 if (CS1B == OnlyReadsMemory && CS2B == OnlyReadsMemory)
119 return NoModRef;
120
121 AliasAnalysis::ModRefResult Mask = ModRef;
122
123 // If CS1 only reads memory, the only dependence on CS2 can be
124 // from CS1 reading memory written by CS2.
125 if (CS1B == OnlyReadsMemory)
126 Mask = ModRefResult(Mask & Ref);
127
128 // If CS2 only access memory through arguments, accumulate the mod/ref
129 // information from CS1's references to the memory referenced by
130 // CS2's arguments.
131 if (CS2B == AccessesArguments) {
132 AliasAnalysis::ModRefResult R = NoModRef;
133 for (ImmutableCallSite::arg_iterator
134 I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) {
135 R = ModRefResult((R | getModRefInfo(CS1, *I, UnknownSize)) & Mask);
136 if (R == Mask)
137 break;
138 }
139 return R;
140 }
141
142 // If CS1 only accesses memory through arguments, check if CS2 references
143 // any of the memory referenced by CS1's arguments. If not, return NoModRef.
144 if (CS1B == AccessesArguments) {
145 AliasAnalysis::ModRefResult R = NoModRef;
146 for (ImmutableCallSite::arg_iterator
147 I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I)
148 if (getModRefInfo(CS2, *I, UnknownSize) != NoModRef) {
149 R = Mask;
150 break;
151 }
152 if (R == NoModRef)
153 return R;
154 }
155
Dan Gohmane46a3882010-10-25 16:29:52 +0000156 // If this is the end of the chain, don't forward.
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000157 if (!AA) return Mask;
158
159 // Otherwise, fall back to the next AA in the chain. But we can merge
160 // in any mask we've managed to compute.
161 return ModRefResult(AA->getModRefInfo(CS1, CS2) & Mask);
162}
163
164AliasAnalysis::ModRefBehavior
165AliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
Dan Gohman852dda42010-10-25 16:28:57 +0000166 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000167
168 ModRefBehavior Min = UnknownModRefBehavior;
169
170 // Call back into the alias analysis with the other form of getModRefBehavior
171 // to see if it can give a better response.
172 if (const Function *F = CS.getCalledFunction())
173 Min = getModRefBehavior(F);
174
Dan Gohmane46a3882010-10-25 16:29:52 +0000175 // If this is the end of the chain, don't forward.
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000176 if (!AA) return Min;
177
178 // Otherwise, fall back to the next AA in the chain. But we can merge
179 // in any result we've managed to compute.
180 return std::min(AA->getModRefBehavior(CS), Min);
181}
182
183AliasAnalysis::ModRefBehavior
184AliasAnalysis::getModRefBehavior(const Function *F) {
Chris Lattner5a24d702004-05-23 21:15:48 +0000185 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000186 return AA->getModRefBehavior(F);
Chris Lattner5a24d702004-05-23 21:15:48 +0000187}
188
Chris Lattner5a24d702004-05-23 21:15:48 +0000189//===----------------------------------------------------------------------===//
190// AliasAnalysis non-virtual helper method implementation
191//===----------------------------------------------------------------------===//
192
Chris Lattner14ac8772003-02-26 19:26:51 +0000193AliasAnalysis::ModRefResult
Dan Gohmanb2143b62010-09-14 21:25:10 +0000194AliasAnalysis::getModRefInfo(const LoadInst *L, const Location &Loc) {
Dan Gohmanb9db52d2010-08-06 18:11:28 +0000195 // Be conservative in the face of volatile.
196 if (L->isVolatile())
197 return ModRef;
198
Dan Gohman14a498a2010-08-03 17:27:43 +0000199 // If the load address doesn't alias the given address, it doesn't read
200 // or write the specified memory.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000201 if (!alias(Location(L->getOperand(0),
202 getTypeStoreSize(L->getType()),
203 L->getMetadata(LLVMContext::MD_tbaa)),
204 Loc))
Dan Gohman14a498a2010-08-03 17:27:43 +0000205 return NoModRef;
206
Dan Gohman14a498a2010-08-03 17:27:43 +0000207 // Otherwise, a load just reads.
208 return Ref;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000209}
210
Chris Lattner14ac8772003-02-26 19:26:51 +0000211AliasAnalysis::ModRefResult
Dan Gohmanb2143b62010-09-14 21:25:10 +0000212AliasAnalysis::getModRefInfo(const StoreInst *S, const Location &Loc) {
Dan Gohmanb9db52d2010-08-06 18:11:28 +0000213 // Be conservative in the face of volatile.
214 if (S->isVolatile())
215 return ModRef;
216
Dan Gohman9b8639c2010-08-06 18:10:45 +0000217 // If the store address cannot alias the pointer in question, then the
218 // specified memory cannot be modified by the store.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000219 if (!alias(Location(S->getOperand(1),
220 getTypeStoreSize(S->getOperand(0)->getType()),
221 S->getMetadata(LLVMContext::MD_tbaa)),
222 Loc))
Chris Lattnerf4d904d2004-01-30 22:16:42 +0000223 return NoModRef;
224
225 // If the pointer is a pointer to constant memory, then it could not have been
226 // modified by this store.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000227 if (pointsToConstantMemory(Loc))
Dan Gohman14a498a2010-08-03 17:27:43 +0000228 return NoModRef;
229
230 // Otherwise, a store just writes.
231 return Mod;
Chris Lattner14ac8772003-02-26 19:26:51 +0000232}
233
Dan Gohmane26a7b52010-08-06 18:24:38 +0000234AliasAnalysis::ModRefResult
Dan Gohmanb2143b62010-09-14 21:25:10 +0000235AliasAnalysis::getModRefInfo(const VAArgInst *V, const Location &Loc) {
Dan Gohmane26a7b52010-08-06 18:24:38 +0000236 // If the va_arg address cannot alias the pointer in question, then the
237 // specified memory cannot be accessed by the va_arg.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000238 if (!alias(Location(V->getOperand(0),
239 UnknownSize,
240 V->getMetadata(LLVMContext::MD_tbaa)),
241 Loc))
Dan Gohmane26a7b52010-08-06 18:24:38 +0000242 return NoModRef;
243
244 // If the pointer is a pointer to constant memory, then it could not have been
245 // modified by this va_arg.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000246 if (pointsToConstantMemory(Loc))
Dan Gohmane26a7b52010-08-06 18:24:38 +0000247 return NoModRef;
248
249 // Otherwise, a va_arg reads and writes.
250 return ModRef;
251}
252
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000253// AliasAnalysis destructor: DO NOT move this to the header file for
254// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
255// the AliasAnalysis.o file in the current .a file, causing alias analysis
256// support to not be included in the tool correctly!
257//
258AliasAnalysis::~AliasAnalysis() {}
259
Dan Gohman5a56bf62008-05-30 00:02:02 +0000260/// InitializeAliasAnalysis - Subclasses must call this method to initialize the
Chris Lattner14ac8772003-02-26 19:26:51 +0000261/// AliasAnalysis interface before any other methods are called.
262///
263void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000264 TD = P->getAnalysisIfAvailable<TargetData>();
Chris Lattner5a24d702004-05-23 21:15:48 +0000265 AA = &P->getAnalysis<AliasAnalysis>();
Chris Lattner14ac8772003-02-26 19:26:51 +0000266}
267
268// getAnalysisUsage - All alias analysis implementations should invoke this
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000269// directly (using AliasAnalysis::getAnalysisUsage(AU)).
Chris Lattner14ac8772003-02-26 19:26:51 +0000270void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5a24d702004-05-23 21:15:48 +0000271 AU.addRequired<AliasAnalysis>(); // All AA's chain
Chris Lattner14ac8772003-02-26 19:26:51 +0000272}
273
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000274/// getTypeStoreSize - Return the TargetData store size for the given type,
275/// if known, or a conservative value otherwise.
276///
Dan Gohman3da848b2010-10-19 22:54:46 +0000277uint64_t AliasAnalysis::getTypeStoreSize(const Type *Ty) {
Dan Gohmanf3a925d2010-10-19 17:06:23 +0000278 return TD ? TD->getTypeStoreSize(Ty) : UnknownSize;
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000279}
280
Chris Lattnerf9355f62002-08-22 22:46:39 +0000281/// canBasicBlockModify - Return true if it is possible for execution of the
282/// specified basic block to modify the value pointed to by Ptr.
283///
Chris Lattner14ac8772003-02-26 19:26:51 +0000284bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
Dan Gohmanb2143b62010-09-14 21:25:10 +0000285 const Location &Loc) {
286 return canInstructionRangeModify(BB.front(), BB.back(), Loc);
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000287}
288
Chris Lattnerf9355f62002-08-22 22:46:39 +0000289/// canInstructionRangeModify - Return true if it is possible for the execution
290/// of the specified instructions to modify the value pointed to by Ptr. The
291/// instructions to consider are all of the instructions in the range of [I1,I2]
292/// INCLUSIVE. I1 and I2 must be in the same basic block.
293///
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000294bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
295 const Instruction &I2,
Dan Gohmanb2143b62010-09-14 21:25:10 +0000296 const Location &Loc) {
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000297 assert(I1.getParent() == I2.getParent() &&
298 "Instructions not in same basic block!");
Dan Gohman79fca6f2010-08-03 21:48:53 +0000299 BasicBlock::const_iterator I = &I1;
300 BasicBlock::const_iterator E = &I2;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000301 ++E; // Convert from inclusive to exclusive range.
302
Chris Lattner14ac8772003-02-26 19:26:51 +0000303 for (; I != E; ++I) // Check every instruction in range
Dan Gohmanb2143b62010-09-14 21:25:10 +0000304 if (getModRefInfo(I, Loc) & Mod)
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000305 return true;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000306 return false;
307}
308
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000309/// isNoAliasCall - Return true if this pointer is returned by a noalias
310/// function.
311bool llvm::isNoAliasCall(const Value *V) {
312 if (isa<CallInst>(V) || isa<InvokeInst>(V))
Dan Gohman79fca6f2010-08-03 21:48:53 +0000313 return ImmutableCallSite(cast<Instruction>(V))
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000314 .paramHasAttr(0, Attribute::NoAlias);
315 return false;
316}
317
318/// isIdentifiedObject - Return true if this pointer refers to a distinct and
319/// identifiable object. This returns true for:
Dan Gohman5753a4a2009-08-27 17:52:56 +0000320/// Global Variables and Functions (but not Global Aliases)
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000321/// Allocas and Mallocs
Dan Gohman9e86f432010-07-07 14:27:09 +0000322/// ByVal and NoAlias Arguments
323/// NoAlias returns
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000324///
Dan Gohman9e86f432010-07-07 14:27:09 +0000325bool llvm::isIdentifiedObject(const Value *V) {
Dan Gohman6be2bd52010-06-29 00:50:39 +0000326 if (isa<AllocaInst>(V))
Dan Gohman5753a4a2009-08-27 17:52:56 +0000327 return true;
328 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000329 return true;
Dan Gohman9e86f432010-07-07 14:27:09 +0000330 if (isNoAliasCall(V))
331 return true;
332 if (const Argument *A = dyn_cast<Argument>(V))
333 return A->hasNoAliasAttr() || A->hasByValAttr();
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000334 return false;
335}