blob: 752edd52b454fb3d9e55a2f1c727b3834d77b6e2 [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"
Chad Rosier3a884f52012-05-14 20:35:04 +000028#include "llvm/Analysis/CaptureTracking.h"
29#include "llvm/Analysis/Dominators.h"
30#include "llvm/Analysis/ValueTracking.h"
Reid Spencer6df60a92006-06-07 20:00:19 +000031#include "llvm/Pass.h"
Chris Lattner53ad0ed2002-08-22 18:25:32 +000032#include "llvm/BasicBlock.h"
Duncan Sandsdff67102007-12-01 07:51:45 +000033#include "llvm/Function.h"
Owen Andersoncd895252009-02-03 06:27:22 +000034#include "llvm/IntrinsicInst.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000035#include "llvm/Instructions.h"
Dan Gohmanb2143b62010-09-14 21:25:10 +000036#include "llvm/LLVMContext.h"
Chris Lattner5b3a4552005-03-17 15:38:16 +000037#include "llvm/Type.h"
Micah Villmow3574eca2012-10-08 16:38:25 +000038#include "llvm/DataLayout.h"
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +000039#include "llvm/Target/TargetLibraryInfo.h"
Chris Lattner992860c2004-03-15 04:07:29 +000040using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000041
Chris Lattner53ad0ed2002-08-22 18:25:32 +000042// Register the AliasAnalysis interface, providing a nice name to refer to.
Owen Anderson081c34b2010-10-19 17:21:58 +000043INITIALIZE_ANALYSIS_GROUP(AliasAnalysis, "Alias Analysis", NoAA)
Devang Patel19974732007-05-03 01:11:54 +000044char AliasAnalysis::ID = 0;
Chris Lattner53ad0ed2002-08-22 18:25:32 +000045
Chris Lattner5a24d702004-05-23 21:15:48 +000046//===----------------------------------------------------------------------===//
47// Default chaining methods
48//===----------------------------------------------------------------------===//
49
50AliasAnalysis::AliasResult
Dan Gohmanb2143b62010-09-14 21:25:10 +000051AliasAnalysis::alias(const Location &LocA, const Location &LocB) {
Chris Lattner5a24d702004-05-23 21:15:48 +000052 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohmanb2143b62010-09-14 21:25:10 +000053 return AA->alias(LocA, LocB);
Chris Lattner5a24d702004-05-23 21:15:48 +000054}
55
Dan Gohmana25e5db2010-11-08 16:45:26 +000056bool AliasAnalysis::pointsToConstantMemory(const Location &Loc,
57 bool OrLocal) {
Chris Lattner5a24d702004-05-23 21:15:48 +000058 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohmana25e5db2010-11-08 16:45:26 +000059 return AA->pointsToConstantMemory(Loc, OrLocal);
Chris Lattner5a24d702004-05-23 21:15:48 +000060}
61
Chris Lattner5a24d702004-05-23 21:15:48 +000062void AliasAnalysis::deleteValue(Value *V) {
63 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
64 AA->deleteValue(V);
65}
66
67void AliasAnalysis::copyValue(Value *From, Value *To) {
68 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
69 AA->copyValue(From, To);
70}
71
Owen Andersonab6acc62011-01-03 21:38:41 +000072void AliasAnalysis::addEscapingUse(Use &U) {
73 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
74 AA->addEscapingUse(U);
75}
76
77
Chris Lattner5a24d702004-05-23 21:15:48 +000078AliasAnalysis::ModRefResult
Dan Gohman6ce9d8b2010-08-06 01:25:49 +000079AliasAnalysis::getModRefInfo(ImmutableCallSite CS,
Dan Gohmanb2143b62010-09-14 21:25:10 +000080 const Location &Loc) {
Dan Gohman852dda42010-10-25 16:28:57 +000081 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman6ce9d8b2010-08-06 01:25:49 +000082
83 ModRefBehavior MRB = getModRefBehavior(CS);
84 if (MRB == DoesNotAccessMemory)
85 return NoModRef;
86
87 ModRefResult Mask = ModRef;
Dan Gohmanc07661c2010-11-09 20:06:55 +000088 if (onlyReadsMemory(MRB))
Dan Gohman6ce9d8b2010-08-06 01:25:49 +000089 Mask = Ref;
Dan Gohmanc07661c2010-11-09 20:06:55 +000090
Dan Gohman68a60562010-11-10 18:17:28 +000091 if (onlyAccessesArgPointees(MRB)) {
Dan Gohman6ce9d8b2010-08-06 01:25:49 +000092 bool doesAlias = false;
Dan Gohmanbddc1ca2011-04-27 18:39:03 +000093 if (doesAccessArgPointees(MRB)) {
94 MDNode *CSTag = CS.getInstruction()->getMetadata(LLVMContext::MD_tbaa);
Dan Gohman68a60562010-11-10 18:17:28 +000095 for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
Dan Gohmanbddc1ca2011-04-27 18:39:03 +000096 AI != AE; ++AI) {
97 const Value *Arg = *AI;
98 if (!Arg->getType()->isPointerTy())
99 continue;
100 Location CSLoc(Arg, UnknownSize, CSTag);
101 if (!isNoAlias(CSLoc, Loc)) {
Dan Gohman68a60562010-11-10 18:17:28 +0000102 doesAlias = true;
103 break;
104 }
Dan Gohmanbddc1ca2011-04-27 18:39:03 +0000105 }
106 }
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000107 if (!doesAlias)
108 return NoModRef;
109 }
110
Dan Gohmanb2143b62010-09-14 21:25:10 +0000111 // If Loc is a constant memory location, the call definitely could not
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000112 // modify the memory location.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000113 if ((Mask & Mod) && pointsToConstantMemory(Loc))
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000114 Mask = ModRefResult(Mask & ~Mod);
115
Dan Gohmane46a3882010-10-25 16:29:52 +0000116 // If this is the end of the chain, don't forward.
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000117 if (!AA) return Mask;
118
119 // Otherwise, fall back to the next AA in the chain. But we can merge
120 // in any mask we've managed to compute.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000121 return ModRefResult(AA->getModRefInfo(CS, Loc) & Mask);
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000122}
123
124AliasAnalysis::ModRefResult
Dan Gohman79fca6f2010-08-03 21:48:53 +0000125AliasAnalysis::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) {
Dan Gohman852dda42010-10-25 16:28:57 +0000126 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000127
128 // If CS1 or CS2 are readnone, they don't interact.
129 ModRefBehavior CS1B = getModRefBehavior(CS1);
130 if (CS1B == DoesNotAccessMemory) return NoModRef;
131
132 ModRefBehavior CS2B = getModRefBehavior(CS2);
133 if (CS2B == DoesNotAccessMemory) return NoModRef;
134
135 // If they both only read from memory, there is no dependence.
Dan Gohmanc07661c2010-11-09 20:06:55 +0000136 if (onlyReadsMemory(CS1B) && onlyReadsMemory(CS2B))
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000137 return NoModRef;
138
139 AliasAnalysis::ModRefResult Mask = ModRef;
140
141 // If CS1 only reads memory, the only dependence on CS2 can be
142 // from CS1 reading memory written by CS2.
Dan Gohmanc07661c2010-11-09 20:06:55 +0000143 if (onlyReadsMemory(CS1B))
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000144 Mask = ModRefResult(Mask & Ref);
145
146 // If CS2 only access memory through arguments, accumulate the mod/ref
147 // information from CS1's references to the memory referenced by
148 // CS2's arguments.
Dan Gohman68a60562010-11-10 18:17:28 +0000149 if (onlyAccessesArgPointees(CS2B)) {
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000150 AliasAnalysis::ModRefResult R = NoModRef;
Dan Gohmanbddc1ca2011-04-27 18:39:03 +0000151 if (doesAccessArgPointees(CS2B)) {
152 MDNode *CS2Tag = CS2.getInstruction()->getMetadata(LLVMContext::MD_tbaa);
Dan Gohman68a60562010-11-10 18:17:28 +0000153 for (ImmutableCallSite::arg_iterator
154 I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) {
Dan Gohmanbddc1ca2011-04-27 18:39:03 +0000155 const Value *Arg = *I;
156 if (!Arg->getType()->isPointerTy())
157 continue;
158 Location CS2Loc(Arg, UnknownSize, CS2Tag);
159 R = ModRefResult((R | getModRefInfo(CS1, CS2Loc)) & Mask);
Dan Gohman68a60562010-11-10 18:17:28 +0000160 if (R == Mask)
161 break;
162 }
Dan Gohmanbddc1ca2011-04-27 18:39:03 +0000163 }
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000164 return R;
165 }
166
167 // If CS1 only accesses memory through arguments, check if CS2 references
168 // any of the memory referenced by CS1's arguments. If not, return NoModRef.
Dan Gohman68a60562010-11-10 18:17:28 +0000169 if (onlyAccessesArgPointees(CS1B)) {
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000170 AliasAnalysis::ModRefResult R = NoModRef;
Dan Gohmanbddc1ca2011-04-27 18:39:03 +0000171 if (doesAccessArgPointees(CS1B)) {
172 MDNode *CS1Tag = CS1.getInstruction()->getMetadata(LLVMContext::MD_tbaa);
Dan Gohman68a60562010-11-10 18:17:28 +0000173 for (ImmutableCallSite::arg_iterator
Dan Gohmanbddc1ca2011-04-27 18:39:03 +0000174 I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I) {
175 const Value *Arg = *I;
176 if (!Arg->getType()->isPointerTy())
177 continue;
178 Location CS1Loc(Arg, UnknownSize, CS1Tag);
179 if (getModRefInfo(CS2, CS1Loc) != NoModRef) {
Dan Gohman68a60562010-11-10 18:17:28 +0000180 R = Mask;
181 break;
182 }
Dan Gohmanbddc1ca2011-04-27 18:39:03 +0000183 }
184 }
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000185 if (R == NoModRef)
186 return R;
187 }
188
Dan Gohmane46a3882010-10-25 16:29:52 +0000189 // If this is the end of the chain, don't forward.
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000190 if (!AA) return Mask;
191
192 // Otherwise, fall back to the next AA in the chain. But we can merge
193 // in any mask we've managed to compute.
194 return ModRefResult(AA->getModRefInfo(CS1, CS2) & Mask);
195}
196
197AliasAnalysis::ModRefBehavior
198AliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
Dan Gohman852dda42010-10-25 16:28:57 +0000199 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000200
201 ModRefBehavior Min = UnknownModRefBehavior;
202
203 // Call back into the alias analysis with the other form of getModRefBehavior
204 // to see if it can give a better response.
205 if (const Function *F = CS.getCalledFunction())
206 Min = getModRefBehavior(F);
207
Dan Gohmane46a3882010-10-25 16:29:52 +0000208 // If this is the end of the chain, don't forward.
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000209 if (!AA) return Min;
210
211 // Otherwise, fall back to the next AA in the chain. But we can merge
212 // in any result we've managed to compute.
Dan Gohman42c31a72010-11-10 01:02:18 +0000213 return ModRefBehavior(AA->getModRefBehavior(CS) & Min);
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000214}
215
216AliasAnalysis::ModRefBehavior
217AliasAnalysis::getModRefBehavior(const Function *F) {
Chris Lattner5a24d702004-05-23 21:15:48 +0000218 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000219 return AA->getModRefBehavior(F);
Chris Lattner5a24d702004-05-23 21:15:48 +0000220}
221
Chris Lattner5a24d702004-05-23 21:15:48 +0000222//===----------------------------------------------------------------------===//
223// AliasAnalysis non-virtual helper method implementation
224//===----------------------------------------------------------------------===//
225
Dan Gohman6d8eb152010-11-11 21:50:19 +0000226AliasAnalysis::Location AliasAnalysis::getLocation(const LoadInst *LI) {
227 return Location(LI->getPointerOperand(),
228 getTypeStoreSize(LI->getType()),
229 LI->getMetadata(LLVMContext::MD_tbaa));
230}
231
232AliasAnalysis::Location AliasAnalysis::getLocation(const StoreInst *SI) {
233 return Location(SI->getPointerOperand(),
234 getTypeStoreSize(SI->getValueOperand()->getType()),
235 SI->getMetadata(LLVMContext::MD_tbaa));
236}
237
238AliasAnalysis::Location AliasAnalysis::getLocation(const VAArgInst *VI) {
239 return Location(VI->getPointerOperand(),
240 UnknownSize,
241 VI->getMetadata(LLVMContext::MD_tbaa));
242}
243
Eli Friedman46cb5af2011-09-26 20:15:28 +0000244AliasAnalysis::Location
245AliasAnalysis::getLocation(const AtomicCmpXchgInst *CXI) {
246 return Location(CXI->getPointerOperand(),
247 getTypeStoreSize(CXI->getCompareOperand()->getType()),
248 CXI->getMetadata(LLVMContext::MD_tbaa));
249}
250
251AliasAnalysis::Location
252AliasAnalysis::getLocation(const AtomicRMWInst *RMWI) {
253 return Location(RMWI->getPointerOperand(),
254 getTypeStoreSize(RMWI->getValOperand()->getType()),
255 RMWI->getMetadata(LLVMContext::MD_tbaa));
256}
Chris Lattnere90c5cb2010-11-21 07:51:27 +0000257
258AliasAnalysis::Location
259AliasAnalysis::getLocationForSource(const MemTransferInst *MTI) {
260 uint64_t Size = UnknownSize;
261 if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
262 Size = C->getValue().getZExtValue();
263
Dan Gohman387f28a2010-12-16 02:51:19 +0000264 // memcpy/memmove can have TBAA tags. For memcpy, they apply
265 // to both the source and the destination.
266 MDNode *TBAATag = MTI->getMetadata(LLVMContext::MD_tbaa);
267
268 return Location(MTI->getRawSource(), Size, TBAATag);
Chris Lattnere90c5cb2010-11-21 07:51:27 +0000269}
270
271AliasAnalysis::Location
Chris Lattner9dc9e812010-11-30 01:48:20 +0000272AliasAnalysis::getLocationForDest(const MemIntrinsic *MTI) {
Chris Lattnere90c5cb2010-11-21 07:51:27 +0000273 uint64_t Size = UnknownSize;
274 if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
275 Size = C->getValue().getZExtValue();
Dan Gohman387f28a2010-12-16 02:51:19 +0000276
277 // memcpy/memmove can have TBAA tags. For memcpy, they apply
278 // to both the source and the destination.
279 MDNode *TBAATag = MTI->getMetadata(LLVMContext::MD_tbaa);
Chris Lattnere90c5cb2010-11-21 07:51:27 +0000280
Dan Gohman387f28a2010-12-16 02:51:19 +0000281 return Location(MTI->getRawDest(), Size, TBAATag);
Chris Lattnere90c5cb2010-11-21 07:51:27 +0000282}
283
284
285
Chris Lattner14ac8772003-02-26 19:26:51 +0000286AliasAnalysis::ModRefResult
Dan Gohmanb2143b62010-09-14 21:25:10 +0000287AliasAnalysis::getModRefInfo(const LoadInst *L, const Location &Loc) {
Eli Friedman667ccf22011-08-15 20:54:19 +0000288 // Be conservative in the face of volatile/atomic.
289 if (!L->isUnordered())
Dan Gohmanb9db52d2010-08-06 18:11:28 +0000290 return ModRef;
291
Dan Gohman14a498a2010-08-03 17:27:43 +0000292 // If the load address doesn't alias the given address, it doesn't read
293 // or write the specified memory.
Dan Gohman6d8eb152010-11-11 21:50:19 +0000294 if (!alias(getLocation(L), Loc))
Dan Gohman14a498a2010-08-03 17:27:43 +0000295 return NoModRef;
296
Dan Gohman14a498a2010-08-03 17:27:43 +0000297 // Otherwise, a load just reads.
298 return Ref;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000299}
300
Chris Lattner14ac8772003-02-26 19:26:51 +0000301AliasAnalysis::ModRefResult
Dan Gohmanb2143b62010-09-14 21:25:10 +0000302AliasAnalysis::getModRefInfo(const StoreInst *S, const Location &Loc) {
Eli Friedman667ccf22011-08-15 20:54:19 +0000303 // Be conservative in the face of volatile/atomic.
304 if (!S->isUnordered())
Dan Gohmanb9db52d2010-08-06 18:11:28 +0000305 return ModRef;
306
Dan Gohman9b8639c2010-08-06 18:10:45 +0000307 // If the store address cannot alias the pointer in question, then the
308 // specified memory cannot be modified by the store.
Dan Gohman6d8eb152010-11-11 21:50:19 +0000309 if (!alias(getLocation(S), Loc))
Chris Lattnerf4d904d2004-01-30 22:16:42 +0000310 return NoModRef;
311
312 // If the pointer is a pointer to constant memory, then it could not have been
313 // modified by this store.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000314 if (pointsToConstantMemory(Loc))
Dan Gohman14a498a2010-08-03 17:27:43 +0000315 return NoModRef;
316
317 // Otherwise, a store just writes.
318 return Mod;
Chris Lattner14ac8772003-02-26 19:26:51 +0000319}
320
Dan Gohmane26a7b52010-08-06 18:24:38 +0000321AliasAnalysis::ModRefResult
Dan Gohmanb2143b62010-09-14 21:25:10 +0000322AliasAnalysis::getModRefInfo(const VAArgInst *V, const Location &Loc) {
Dan Gohmane26a7b52010-08-06 18:24:38 +0000323 // If the va_arg address cannot alias the pointer in question, then the
324 // specified memory cannot be accessed by the va_arg.
Dan Gohman6d8eb152010-11-11 21:50:19 +0000325 if (!alias(getLocation(V), Loc))
Dan Gohmane26a7b52010-08-06 18:24:38 +0000326 return NoModRef;
327
328 // If the pointer is a pointer to constant memory, then it could not have been
329 // modified by this va_arg.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000330 if (pointsToConstantMemory(Loc))
Dan Gohmane26a7b52010-08-06 18:24:38 +0000331 return NoModRef;
332
333 // Otherwise, a va_arg reads and writes.
334 return ModRef;
335}
336
Eli Friedman46cb5af2011-09-26 20:15:28 +0000337AliasAnalysis::ModRefResult
338AliasAnalysis::getModRefInfo(const AtomicCmpXchgInst *CX, const Location &Loc) {
339 // Acquire/Release cmpxchg has properties that matter for arbitrary addresses.
340 if (CX->getOrdering() > Monotonic)
341 return ModRef;
342
343 // If the cmpxchg address does not alias the location, it does not access it.
344 if (!alias(getLocation(CX), Loc))
345 return NoModRef;
346
347 return ModRef;
348}
349
350AliasAnalysis::ModRefResult
351AliasAnalysis::getModRefInfo(const AtomicRMWInst *RMW, const Location &Loc) {
352 // Acquire/Release atomicrmw has properties that matter for arbitrary addresses.
353 if (RMW->getOrdering() > Monotonic)
354 return ModRef;
355
356 // If the atomicrmw address does not alias the location, it does not access it.
357 if (!alias(getLocation(RMW), Loc))
358 return NoModRef;
359
360 return ModRef;
361}
362
Chad Rosier3a884f52012-05-14 20:35:04 +0000363namespace {
364 /// Only find pointer captures which happen before the given instruction. Uses
365 /// the dominator tree to determine whether one instruction is before another.
366 struct CapturesBefore : public CaptureTracker {
367 CapturesBefore(const Instruction *I, DominatorTree *DT)
368 : BeforeHere(I), DT(DT), Captured(false) {}
369
370 void tooManyUses() { Captured = true; }
371
372 bool shouldExplore(Use *U) {
373 Instruction *I = cast<Instruction>(U->getUser());
374 BasicBlock *BB = I->getParent();
375 if (BeforeHere != I &&
376 (!DT->isReachableFromEntry(BB) || DT->dominates(BeforeHere, I)))
377 return false;
378 return true;
379 }
380
381 bool captured(Use *U) {
382 Instruction *I = cast<Instruction>(U->getUser());
383 BasicBlock *BB = I->getParent();
384 if (BeforeHere != I &&
385 (!DT->isReachableFromEntry(BB) || DT->dominates(BeforeHere, I)))
386 return false;
387 Captured = true;
388 return true;
389 }
390
391 const Instruction *BeforeHere;
392 DominatorTree *DT;
393
394 bool Captured;
395 };
396}
397
398// FIXME: this is really just shoring-up a deficiency in alias analysis.
399// BasicAA isn't willing to spend linear time determining whether an alloca
400// was captured before or after this particular call, while we are. However,
401// with a smarter AA in place, this test is just wasting compile time.
402AliasAnalysis::ModRefResult
403AliasAnalysis::callCapturesBefore(const Instruction *I,
404 const AliasAnalysis::Location &MemLoc,
405 DominatorTree *DT) {
406 if (!DT || !TD) return AliasAnalysis::ModRef;
407
408 const Value *Object = GetUnderlyingObject(MemLoc.Ptr, TD);
409 if (!isIdentifiedObject(Object) || isa<GlobalValue>(Object) ||
410 isa<Constant>(Object))
411 return AliasAnalysis::ModRef;
412
413 ImmutableCallSite CS(I);
414 if (!CS.getInstruction() || CS.getInstruction() == Object)
415 return AliasAnalysis::ModRef;
416
417 CapturesBefore CB(I, DT);
418 llvm::PointerMayBeCaptured(Object, &CB);
419 if (CB.Captured)
420 return AliasAnalysis::ModRef;
421
422 unsigned ArgNo = 0;
423 for (ImmutableCallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
424 CI != CE; ++CI, ++ArgNo) {
425 // Only look at the no-capture or byval pointer arguments. If this
426 // pointer were passed to arguments that were neither of these, then it
427 // couldn't be no-capture.
428 if (!(*CI)->getType()->isPointerTy() ||
429 (!CS.doesNotCapture(ArgNo) && !CS.isByValArgument(ArgNo)))
430 continue;
431
432 // If this is a no-capture pointer argument, see if we can tell that it
433 // is impossible to alias the pointer we're checking. If not, we have to
434 // assume that the call could touch the pointer, even though it doesn't
435 // escape.
436 if (!isNoAlias(AliasAnalysis::Location(*CI),
437 AliasAnalysis::Location(Object))) {
438 return AliasAnalysis::ModRef;
439 }
440 }
441 return AliasAnalysis::NoModRef;
442}
Eli Friedman46cb5af2011-09-26 20:15:28 +0000443
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000444// AliasAnalysis destructor: DO NOT move this to the header file for
445// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
446// the AliasAnalysis.o file in the current .a file, causing alias analysis
447// support to not be included in the tool correctly!
448//
449AliasAnalysis::~AliasAnalysis() {}
450
Dan Gohman5a56bf62008-05-30 00:02:02 +0000451/// InitializeAliasAnalysis - Subclasses must call this method to initialize the
Chris Lattner14ac8772003-02-26 19:26:51 +0000452/// AliasAnalysis interface before any other methods are called.
453///
454void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
Micah Villmow3574eca2012-10-08 16:38:25 +0000455 TD = P->getAnalysisIfAvailable<DataLayout>();
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000456 TLI = P->getAnalysisIfAvailable<TargetLibraryInfo>();
Chris Lattner5a24d702004-05-23 21:15:48 +0000457 AA = &P->getAnalysis<AliasAnalysis>();
Chris Lattner14ac8772003-02-26 19:26:51 +0000458}
459
460// getAnalysisUsage - All alias analysis implementations should invoke this
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000461// directly (using AliasAnalysis::getAnalysisUsage(AU)).
Chris Lattner14ac8772003-02-26 19:26:51 +0000462void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5a24d702004-05-23 21:15:48 +0000463 AU.addRequired<AliasAnalysis>(); // All AA's chain
Chris Lattner14ac8772003-02-26 19:26:51 +0000464}
465
Micah Villmow3574eca2012-10-08 16:38:25 +0000466/// getTypeStoreSize - Return the DataLayout store size for the given type,
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000467/// if known, or a conservative value otherwise.
468///
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000469uint64_t AliasAnalysis::getTypeStoreSize(Type *Ty) {
Dan Gohmanf3a925d2010-10-19 17:06:23 +0000470 return TD ? TD->getTypeStoreSize(Ty) : UnknownSize;
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000471}
472
Chris Lattnerf9355f62002-08-22 22:46:39 +0000473/// canBasicBlockModify - Return true if it is possible for execution of the
474/// specified basic block to modify the value pointed to by Ptr.
475///
Chris Lattner14ac8772003-02-26 19:26:51 +0000476bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
Dan Gohmanb2143b62010-09-14 21:25:10 +0000477 const Location &Loc) {
478 return canInstructionRangeModify(BB.front(), BB.back(), Loc);
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000479}
480
Chris Lattnerf9355f62002-08-22 22:46:39 +0000481/// canInstructionRangeModify - Return true if it is possible for the execution
482/// of the specified instructions to modify the value pointed to by Ptr. The
483/// instructions to consider are all of the instructions in the range of [I1,I2]
484/// INCLUSIVE. I1 and I2 must be in the same basic block.
485///
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000486bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
487 const Instruction &I2,
Dan Gohmanb2143b62010-09-14 21:25:10 +0000488 const Location &Loc) {
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000489 assert(I1.getParent() == I2.getParent() &&
490 "Instructions not in same basic block!");
Dan Gohman79fca6f2010-08-03 21:48:53 +0000491 BasicBlock::const_iterator I = &I1;
492 BasicBlock::const_iterator E = &I2;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000493 ++E; // Convert from inclusive to exclusive range.
494
Chris Lattner14ac8772003-02-26 19:26:51 +0000495 for (; I != E; ++I) // Check every instruction in range
Dan Gohmanb2143b62010-09-14 21:25:10 +0000496 if (getModRefInfo(I, Loc) & Mod)
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000497 return true;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000498 return false;
499}
500
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000501/// isNoAliasCall - Return true if this pointer is returned by a noalias
502/// function.
503bool llvm::isNoAliasCall(const Value *V) {
504 if (isa<CallInst>(V) || isa<InvokeInst>(V))
Dan Gohman79fca6f2010-08-03 21:48:53 +0000505 return ImmutableCallSite(cast<Instruction>(V))
Bill Wendling3e2d76c2012-10-09 21:38:14 +0000506 .paramHasAttr(0, Attributes::NoAlias);
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000507 return false;
508}
509
510/// isIdentifiedObject - Return true if this pointer refers to a distinct and
511/// identifiable object. This returns true for:
Dan Gohman5753a4a2009-08-27 17:52:56 +0000512/// Global Variables and Functions (but not Global Aliases)
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000513/// Allocas and Mallocs
Dan Gohman9e86f432010-07-07 14:27:09 +0000514/// ByVal and NoAlias Arguments
515/// NoAlias returns
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000516///
Dan Gohman9e86f432010-07-07 14:27:09 +0000517bool llvm::isIdentifiedObject(const Value *V) {
Dan Gohman6be2bd52010-06-29 00:50:39 +0000518 if (isa<AllocaInst>(V))
Dan Gohman5753a4a2009-08-27 17:52:56 +0000519 return true;
520 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000521 return true;
Dan Gohman9e86f432010-07-07 14:27:09 +0000522 if (isNoAliasCall(V))
523 return true;
524 if (const Argument *A = dyn_cast<Argument>(V))
525 return A->hasNoAliasAttr() || A->hasByValAttr();
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000526 return false;
527}
Nick Lewycky55f4ab82012-02-25 10:56:28 +0000528
529/// isKnownNonNull - Return true if we know that the specified value is never
530/// null.
531bool llvm::isKnownNonNull(const Value *V) {
532 // Alloca never returns null, malloc might.
533 if (isa<AllocaInst>(V)) return true;
534
535 // A byval argument is never null.
536 if (const Argument *A = dyn_cast<Argument>(V))
537 return A->hasByValAttr();
538
539 // Global values are not null unless extern weak.
540 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
541 return !GV->hasExternalWeakLinkage();
542 return false;
543}