blob: f452c9e67661b776588de7cc49facfa910e86472 [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;
Dan Gohmanc07661c2010-11-09 20:06:55 +000078 if (onlyReadsMemory(MRB))
Dan Gohman6ce9d8b2010-08-06 01:25:49 +000079 Mask = Ref;
Dan Gohmanc07661c2010-11-09 20:06:55 +000080
Dan Gohman68a60562010-11-10 18:17:28 +000081 if (onlyAccessesArgPointees(MRB)) {
Dan Gohman6ce9d8b2010-08-06 01:25:49 +000082 bool doesAlias = false;
Dan Gohman68a60562010-11-10 18:17:28 +000083 if (doesAccessArgPointees(MRB))
84 for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
85 AI != AE; ++AI)
86 if (!isNoAlias(Location(*AI), Loc)) {
87 doesAlias = true;
88 break;
89 }
Dan Gohman6ce9d8b2010-08-06 01:25:49 +000090
91 if (!doesAlias)
92 return NoModRef;
93 }
94
Dan Gohmanb2143b62010-09-14 21:25:10 +000095 // If Loc is a constant memory location, the call definitely could not
Dan Gohman6ce9d8b2010-08-06 01:25:49 +000096 // modify the memory location.
Dan Gohmanb2143b62010-09-14 21:25:10 +000097 if ((Mask & Mod) && pointsToConstantMemory(Loc))
Dan Gohman6ce9d8b2010-08-06 01:25:49 +000098 Mask = ModRefResult(Mask & ~Mod);
99
Dan Gohmane46a3882010-10-25 16:29:52 +0000100 // If this is the end of the chain, don't forward.
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000101 if (!AA) return Mask;
102
103 // Otherwise, fall back to the next AA in the chain. But we can merge
104 // in any mask we've managed to compute.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000105 return ModRefResult(AA->getModRefInfo(CS, Loc) & Mask);
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000106}
107
108AliasAnalysis::ModRefResult
Dan Gohman79fca6f2010-08-03 21:48:53 +0000109AliasAnalysis::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) {
Dan Gohman852dda42010-10-25 16:28:57 +0000110 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000111
112 // If CS1 or CS2 are readnone, they don't interact.
113 ModRefBehavior CS1B = getModRefBehavior(CS1);
114 if (CS1B == DoesNotAccessMemory) return NoModRef;
115
116 ModRefBehavior CS2B = getModRefBehavior(CS2);
117 if (CS2B == DoesNotAccessMemory) return NoModRef;
118
119 // If they both only read from memory, there is no dependence.
Dan Gohmanc07661c2010-11-09 20:06:55 +0000120 if (onlyReadsMemory(CS1B) && onlyReadsMemory(CS2B))
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000121 return NoModRef;
122
123 AliasAnalysis::ModRefResult Mask = ModRef;
124
125 // If CS1 only reads memory, the only dependence on CS2 can be
126 // from CS1 reading memory written by CS2.
Dan Gohmanc07661c2010-11-09 20:06:55 +0000127 if (onlyReadsMemory(CS1B))
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000128 Mask = ModRefResult(Mask & Ref);
129
130 // If CS2 only access memory through arguments, accumulate the mod/ref
131 // information from CS1's references to the memory referenced by
132 // CS2's arguments.
Dan Gohman68a60562010-11-10 18:17:28 +0000133 if (onlyAccessesArgPointees(CS2B)) {
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000134 AliasAnalysis::ModRefResult R = NoModRef;
Dan Gohman68a60562010-11-10 18:17:28 +0000135 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);
139 if (R == Mask)
140 break;
141 }
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000142 return R;
143 }
144
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.
Dan Gohman68a60562010-11-10 18:17:28 +0000147 if (onlyAccessesArgPointees(CS1B)) {
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000148 AliasAnalysis::ModRefResult R = NoModRef;
Dan Gohman68a60562010-11-10 18:17:28 +0000149 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) {
153 R = Mask;
154 break;
155 }
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000156 if (R == NoModRef)
157 return R;
158 }
159
Dan Gohmane46a3882010-10-25 16:29:52 +0000160 // If this is the end of the chain, don't forward.
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000161 if (!AA) return Mask;
162
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);
166}
167
168AliasAnalysis::ModRefBehavior
169AliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
Dan Gohman852dda42010-10-25 16:28:57 +0000170 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000171
172 ModRefBehavior Min = UnknownModRefBehavior;
173
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);
178
Dan Gohmane46a3882010-10-25 16:29:52 +0000179 // If this is the end of the chain, don't forward.
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000180 if (!AA) return Min;
181
182 // Otherwise, fall back to the next AA in the chain. But we can merge
183 // in any result we've managed to compute.
Dan Gohman42c31a72010-11-10 01:02:18 +0000184 return ModRefBehavior(AA->getModRefBehavior(CS) & Min);
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000185}
186
187AliasAnalysis::ModRefBehavior
188AliasAnalysis::getModRefBehavior(const Function *F) {
Chris Lattner5a24d702004-05-23 21:15:48 +0000189 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000190 return AA->getModRefBehavior(F);
Chris Lattner5a24d702004-05-23 21:15:48 +0000191}
192
Chris Lattner5a24d702004-05-23 21:15:48 +0000193//===----------------------------------------------------------------------===//
194// AliasAnalysis non-virtual helper method implementation
195//===----------------------------------------------------------------------===//
196
Dan Gohman6d8eb152010-11-11 21:50:19 +0000197AliasAnalysis::Location AliasAnalysis::getLocation(const LoadInst *LI) {
198 return Location(LI->getPointerOperand(),
199 getTypeStoreSize(LI->getType()),
200 LI->getMetadata(LLVMContext::MD_tbaa));
201}
202
203AliasAnalysis::Location AliasAnalysis::getLocation(const StoreInst *SI) {
204 return Location(SI->getPointerOperand(),
205 getTypeStoreSize(SI->getValueOperand()->getType()),
206 SI->getMetadata(LLVMContext::MD_tbaa));
207}
208
209AliasAnalysis::Location AliasAnalysis::getLocation(const VAArgInst *VI) {
210 return Location(VI->getPointerOperand(),
211 UnknownSize,
212 VI->getMetadata(LLVMContext::MD_tbaa));
213}
214
Chris Lattnere90c5cb2010-11-21 07:51:27 +0000215
216AliasAnalysis::Location
217AliasAnalysis::getLocationForSource(const MemTransferInst *MTI) {
218 uint64_t Size = UnknownSize;
219 if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
220 Size = C->getValue().getZExtValue();
221
222 // FIXME: Can memcpy/memmove have TBAA tags?
223 return Location(MTI->getRawSource(), Size, 0);
224}
225
226AliasAnalysis::Location
227AliasAnalysis::getLocationForDest(const MemTransferInst *MTI) {
228 uint64_t Size = UnknownSize;
229 if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
230 Size = C->getValue().getZExtValue();
231
232 // FIXME: Can memcpy/memmove have TBAA tags?
233 return Location(MTI->getRawDest(), Size, 0);
234}
235
236
237
Chris Lattner14ac8772003-02-26 19:26:51 +0000238AliasAnalysis::ModRefResult
Dan Gohmanb2143b62010-09-14 21:25:10 +0000239AliasAnalysis::getModRefInfo(const LoadInst *L, const Location &Loc) {
Dan Gohmanb9db52d2010-08-06 18:11:28 +0000240 // Be conservative in the face of volatile.
241 if (L->isVolatile())
242 return ModRef;
243
Dan Gohman14a498a2010-08-03 17:27:43 +0000244 // If the load address doesn't alias the given address, it doesn't read
245 // or write the specified memory.
Dan Gohman6d8eb152010-11-11 21:50:19 +0000246 if (!alias(getLocation(L), Loc))
Dan Gohman14a498a2010-08-03 17:27:43 +0000247 return NoModRef;
248
Dan Gohman14a498a2010-08-03 17:27:43 +0000249 // Otherwise, a load just reads.
250 return Ref;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000251}
252
Chris Lattner14ac8772003-02-26 19:26:51 +0000253AliasAnalysis::ModRefResult
Dan Gohmanb2143b62010-09-14 21:25:10 +0000254AliasAnalysis::getModRefInfo(const StoreInst *S, const Location &Loc) {
Dan Gohmanb9db52d2010-08-06 18:11:28 +0000255 // Be conservative in the face of volatile.
256 if (S->isVolatile())
257 return ModRef;
258
Dan Gohman9b8639c2010-08-06 18:10:45 +0000259 // If the store address cannot alias the pointer in question, then the
260 // specified memory cannot be modified by the store.
Dan Gohman6d8eb152010-11-11 21:50:19 +0000261 if (!alias(getLocation(S), Loc))
Chris Lattnerf4d904d2004-01-30 22:16:42 +0000262 return NoModRef;
263
264 // If the pointer is a pointer to constant memory, then it could not have been
265 // modified by this store.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000266 if (pointsToConstantMemory(Loc))
Dan Gohman14a498a2010-08-03 17:27:43 +0000267 return NoModRef;
268
269 // Otherwise, a store just writes.
270 return Mod;
Chris Lattner14ac8772003-02-26 19:26:51 +0000271}
272
Dan Gohmane26a7b52010-08-06 18:24:38 +0000273AliasAnalysis::ModRefResult
Dan Gohmanb2143b62010-09-14 21:25:10 +0000274AliasAnalysis::getModRefInfo(const VAArgInst *V, const Location &Loc) {
Dan Gohmane26a7b52010-08-06 18:24:38 +0000275 // If the va_arg address cannot alias the pointer in question, then the
276 // specified memory cannot be accessed by the va_arg.
Dan Gohman6d8eb152010-11-11 21:50:19 +0000277 if (!alias(getLocation(V), Loc))
Dan Gohmane26a7b52010-08-06 18:24:38 +0000278 return NoModRef;
279
280 // If the pointer is a pointer to constant memory, then it could not have been
281 // modified by this va_arg.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000282 if (pointsToConstantMemory(Loc))
Dan Gohmane26a7b52010-08-06 18:24:38 +0000283 return NoModRef;
284
285 // Otherwise, a va_arg reads and writes.
286 return ModRef;
287}
288
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000289// AliasAnalysis destructor: DO NOT move this to the header file for
290// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
291// the AliasAnalysis.o file in the current .a file, causing alias analysis
292// support to not be included in the tool correctly!
293//
294AliasAnalysis::~AliasAnalysis() {}
295
Dan Gohman5a56bf62008-05-30 00:02:02 +0000296/// InitializeAliasAnalysis - Subclasses must call this method to initialize the
Chris Lattner14ac8772003-02-26 19:26:51 +0000297/// AliasAnalysis interface before any other methods are called.
298///
299void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000300 TD = P->getAnalysisIfAvailable<TargetData>();
Chris Lattner5a24d702004-05-23 21:15:48 +0000301 AA = &P->getAnalysis<AliasAnalysis>();
Chris Lattner14ac8772003-02-26 19:26:51 +0000302}
303
304// getAnalysisUsage - All alias analysis implementations should invoke this
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000305// directly (using AliasAnalysis::getAnalysisUsage(AU)).
Chris Lattner14ac8772003-02-26 19:26:51 +0000306void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5a24d702004-05-23 21:15:48 +0000307 AU.addRequired<AliasAnalysis>(); // All AA's chain
Chris Lattner14ac8772003-02-26 19:26:51 +0000308}
309
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000310/// getTypeStoreSize - Return the TargetData store size for the given type,
311/// if known, or a conservative value otherwise.
312///
Dan Gohman3da848b2010-10-19 22:54:46 +0000313uint64_t AliasAnalysis::getTypeStoreSize(const Type *Ty) {
Dan Gohmanf3a925d2010-10-19 17:06:23 +0000314 return TD ? TD->getTypeStoreSize(Ty) : UnknownSize;
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000315}
316
Chris Lattnerf9355f62002-08-22 22:46:39 +0000317/// canBasicBlockModify - Return true if it is possible for execution of the
318/// specified basic block to modify the value pointed to by Ptr.
319///
Chris Lattner14ac8772003-02-26 19:26:51 +0000320bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
Dan Gohmanb2143b62010-09-14 21:25:10 +0000321 const Location &Loc) {
322 return canInstructionRangeModify(BB.front(), BB.back(), Loc);
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000323}
324
Chris Lattnerf9355f62002-08-22 22:46:39 +0000325/// canInstructionRangeModify - Return true if it is possible for the execution
326/// of the specified instructions to modify the value pointed to by Ptr. The
327/// instructions to consider are all of the instructions in the range of [I1,I2]
328/// INCLUSIVE. I1 and I2 must be in the same basic block.
329///
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000330bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
331 const Instruction &I2,
Dan Gohmanb2143b62010-09-14 21:25:10 +0000332 const Location &Loc) {
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000333 assert(I1.getParent() == I2.getParent() &&
334 "Instructions not in same basic block!");
Dan Gohman79fca6f2010-08-03 21:48:53 +0000335 BasicBlock::const_iterator I = &I1;
336 BasicBlock::const_iterator E = &I2;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000337 ++E; // Convert from inclusive to exclusive range.
338
Chris Lattner14ac8772003-02-26 19:26:51 +0000339 for (; I != E; ++I) // Check every instruction in range
Dan Gohmanb2143b62010-09-14 21:25:10 +0000340 if (getModRefInfo(I, Loc) & Mod)
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000341 return true;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000342 return false;
343}
344
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000345/// isNoAliasCall - Return true if this pointer is returned by a noalias
346/// function.
347bool llvm::isNoAliasCall(const Value *V) {
348 if (isa<CallInst>(V) || isa<InvokeInst>(V))
Dan Gohman79fca6f2010-08-03 21:48:53 +0000349 return ImmutableCallSite(cast<Instruction>(V))
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000350 .paramHasAttr(0, Attribute::NoAlias);
351 return false;
352}
353
354/// isIdentifiedObject - Return true if this pointer refers to a distinct and
355/// identifiable object. This returns true for:
Dan Gohman5753a4a2009-08-27 17:52:56 +0000356/// Global Variables and Functions (but not Global Aliases)
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000357/// Allocas and Mallocs
Dan Gohman9e86f432010-07-07 14:27:09 +0000358/// ByVal and NoAlias Arguments
359/// NoAlias returns
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000360///
Dan Gohman9e86f432010-07-07 14:27:09 +0000361bool llvm::isIdentifiedObject(const Value *V) {
Dan Gohman6be2bd52010-06-29 00:50:39 +0000362 if (isa<AllocaInst>(V))
Dan Gohman5753a4a2009-08-27 17:52:56 +0000363 return true;
364 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000365 return true;
Dan Gohman9e86f432010-07-07 14:27:09 +0000366 if (isNoAliasCall(V))
367 return true;
368 if (const Argument *A = dyn_cast<Argument>(V))
369 return A->hasNoAliasAttr() || A->hasByValAttr();
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000370 return false;
371}