blob: 3b6aab13a56835553326436620137a352706ea6c [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"
Chris Lattner14ac8772003-02-26 19:26:51 +000038#include "llvm/Target/TargetData.h"
Chris Lattner992860c2004-03-15 04:07:29 +000039using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000040
Chris Lattner53ad0ed2002-08-22 18:25:32 +000041// Register the AliasAnalysis interface, providing a nice name to refer to.
Owen Anderson081c34b2010-10-19 17:21:58 +000042INITIALIZE_ANALYSIS_GROUP(AliasAnalysis, "Alias Analysis", NoAA)
Devang Patel19974732007-05-03 01:11:54 +000043char AliasAnalysis::ID = 0;
Chris Lattner53ad0ed2002-08-22 18:25:32 +000044
Chris Lattner5a24d702004-05-23 21:15:48 +000045//===----------------------------------------------------------------------===//
46// Default chaining methods
47//===----------------------------------------------------------------------===//
48
49AliasAnalysis::AliasResult
Dan Gohmanb2143b62010-09-14 21:25:10 +000050AliasAnalysis::alias(const Location &LocA, const Location &LocB) {
Chris Lattner5a24d702004-05-23 21:15:48 +000051 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohmanb2143b62010-09-14 21:25:10 +000052 return AA->alias(LocA, LocB);
Chris Lattner5a24d702004-05-23 21:15:48 +000053}
54
Dan Gohmana25e5db2010-11-08 16:45:26 +000055bool AliasAnalysis::pointsToConstantMemory(const Location &Loc,
56 bool OrLocal) {
Chris Lattner5a24d702004-05-23 21:15:48 +000057 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohmana25e5db2010-11-08 16:45:26 +000058 return AA->pointsToConstantMemory(Loc, OrLocal);
Chris Lattner5a24d702004-05-23 21:15:48 +000059}
60
Chris Lattner5a24d702004-05-23 21:15:48 +000061void AliasAnalysis::deleteValue(Value *V) {
62 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
63 AA->deleteValue(V);
64}
65
66void AliasAnalysis::copyValue(Value *From, Value *To) {
67 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
68 AA->copyValue(From, To);
69}
70
Owen Andersonab6acc62011-01-03 21:38:41 +000071void AliasAnalysis::addEscapingUse(Use &U) {
72 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
73 AA->addEscapingUse(U);
74}
75
76
Chris Lattner5a24d702004-05-23 21:15:48 +000077AliasAnalysis::ModRefResult
Dan Gohman6ce9d8b2010-08-06 01:25:49 +000078AliasAnalysis::getModRefInfo(ImmutableCallSite CS,
Dan Gohmanb2143b62010-09-14 21:25:10 +000079 const Location &Loc) {
Dan Gohman852dda42010-10-25 16:28:57 +000080 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman6ce9d8b2010-08-06 01:25:49 +000081
82 ModRefBehavior MRB = getModRefBehavior(CS);
83 if (MRB == DoesNotAccessMemory)
84 return NoModRef;
85
86 ModRefResult Mask = ModRef;
Dan Gohmanc07661c2010-11-09 20:06:55 +000087 if (onlyReadsMemory(MRB))
Dan Gohman6ce9d8b2010-08-06 01:25:49 +000088 Mask = Ref;
Dan Gohmanc07661c2010-11-09 20:06:55 +000089
Dan Gohman68a60562010-11-10 18:17:28 +000090 if (onlyAccessesArgPointees(MRB)) {
Dan Gohman6ce9d8b2010-08-06 01:25:49 +000091 bool doesAlias = false;
Dan Gohmanbddc1ca2011-04-27 18:39:03 +000092 if (doesAccessArgPointees(MRB)) {
93 MDNode *CSTag = CS.getInstruction()->getMetadata(LLVMContext::MD_tbaa);
Dan Gohman68a60562010-11-10 18:17:28 +000094 for (ImmutableCallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
Dan Gohmanbddc1ca2011-04-27 18:39:03 +000095 AI != AE; ++AI) {
96 const Value *Arg = *AI;
97 if (!Arg->getType()->isPointerTy())
98 continue;
99 Location CSLoc(Arg, UnknownSize, CSTag);
100 if (!isNoAlias(CSLoc, Loc)) {
Dan Gohman68a60562010-11-10 18:17:28 +0000101 doesAlias = true;
102 break;
103 }
Dan Gohmanbddc1ca2011-04-27 18:39:03 +0000104 }
105 }
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000106 if (!doesAlias)
107 return NoModRef;
108 }
109
Dan Gohmanb2143b62010-09-14 21:25:10 +0000110 // If Loc is a constant memory location, the call definitely could not
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000111 // modify the memory location.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000112 if ((Mask & Mod) && pointsToConstantMemory(Loc))
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000113 Mask = ModRefResult(Mask & ~Mod);
114
Dan Gohmane46a3882010-10-25 16:29:52 +0000115 // If this is the end of the chain, don't forward.
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000116 if (!AA) return Mask;
117
118 // Otherwise, fall back to the next AA in the chain. But we can merge
119 // in any mask we've managed to compute.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000120 return ModRefResult(AA->getModRefInfo(CS, Loc) & Mask);
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000121}
122
123AliasAnalysis::ModRefResult
Dan Gohman79fca6f2010-08-03 21:48:53 +0000124AliasAnalysis::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) {
Dan Gohman852dda42010-10-25 16:28:57 +0000125 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000126
127 // If CS1 or CS2 are readnone, they don't interact.
128 ModRefBehavior CS1B = getModRefBehavior(CS1);
129 if (CS1B == DoesNotAccessMemory) return NoModRef;
130
131 ModRefBehavior CS2B = getModRefBehavior(CS2);
132 if (CS2B == DoesNotAccessMemory) return NoModRef;
133
134 // If they both only read from memory, there is no dependence.
Dan Gohmanc07661c2010-11-09 20:06:55 +0000135 if (onlyReadsMemory(CS1B) && onlyReadsMemory(CS2B))
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000136 return NoModRef;
137
138 AliasAnalysis::ModRefResult Mask = ModRef;
139
140 // If CS1 only reads memory, the only dependence on CS2 can be
141 // from CS1 reading memory written by CS2.
Dan Gohmanc07661c2010-11-09 20:06:55 +0000142 if (onlyReadsMemory(CS1B))
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000143 Mask = ModRefResult(Mask & Ref);
144
145 // If CS2 only access memory through arguments, accumulate the mod/ref
146 // information from CS1's references to the memory referenced by
147 // CS2's arguments.
Dan Gohman68a60562010-11-10 18:17:28 +0000148 if (onlyAccessesArgPointees(CS2B)) {
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000149 AliasAnalysis::ModRefResult R = NoModRef;
Dan Gohmanbddc1ca2011-04-27 18:39:03 +0000150 if (doesAccessArgPointees(CS2B)) {
151 MDNode *CS2Tag = CS2.getInstruction()->getMetadata(LLVMContext::MD_tbaa);
Dan Gohman68a60562010-11-10 18:17:28 +0000152 for (ImmutableCallSite::arg_iterator
153 I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) {
Dan Gohmanbddc1ca2011-04-27 18:39:03 +0000154 const Value *Arg = *I;
155 if (!Arg->getType()->isPointerTy())
156 continue;
157 Location CS2Loc(Arg, UnknownSize, CS2Tag);
158 R = ModRefResult((R | getModRefInfo(CS1, CS2Loc)) & Mask);
Dan Gohman68a60562010-11-10 18:17:28 +0000159 if (R == Mask)
160 break;
161 }
Dan Gohmanbddc1ca2011-04-27 18:39:03 +0000162 }
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000163 return R;
164 }
165
166 // If CS1 only accesses memory through arguments, check if CS2 references
167 // any of the memory referenced by CS1's arguments. If not, return NoModRef.
Dan Gohman68a60562010-11-10 18:17:28 +0000168 if (onlyAccessesArgPointees(CS1B)) {
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000169 AliasAnalysis::ModRefResult R = NoModRef;
Dan Gohmanbddc1ca2011-04-27 18:39:03 +0000170 if (doesAccessArgPointees(CS1B)) {
171 MDNode *CS1Tag = CS1.getInstruction()->getMetadata(LLVMContext::MD_tbaa);
Dan Gohman68a60562010-11-10 18:17:28 +0000172 for (ImmutableCallSite::arg_iterator
Dan Gohmanbddc1ca2011-04-27 18:39:03 +0000173 I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I) {
174 const Value *Arg = *I;
175 if (!Arg->getType()->isPointerTy())
176 continue;
177 Location CS1Loc(Arg, UnknownSize, CS1Tag);
178 if (getModRefInfo(CS2, CS1Loc) != NoModRef) {
Dan Gohman68a60562010-11-10 18:17:28 +0000179 R = Mask;
180 break;
181 }
Dan Gohmanbddc1ca2011-04-27 18:39:03 +0000182 }
183 }
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000184 if (R == NoModRef)
185 return R;
186 }
187
Dan Gohmane46a3882010-10-25 16:29:52 +0000188 // If this is the end of the chain, don't forward.
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000189 if (!AA) return Mask;
190
191 // Otherwise, fall back to the next AA in the chain. But we can merge
192 // in any mask we've managed to compute.
193 return ModRefResult(AA->getModRefInfo(CS1, CS2) & Mask);
194}
195
196AliasAnalysis::ModRefBehavior
197AliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
Dan Gohman852dda42010-10-25 16:28:57 +0000198 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000199
200 ModRefBehavior Min = UnknownModRefBehavior;
201
202 // Call back into the alias analysis with the other form of getModRefBehavior
203 // to see if it can give a better response.
204 if (const Function *F = CS.getCalledFunction())
205 Min = getModRefBehavior(F);
206
Dan Gohmane46a3882010-10-25 16:29:52 +0000207 // If this is the end of the chain, don't forward.
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000208 if (!AA) return Min;
209
210 // Otherwise, fall back to the next AA in the chain. But we can merge
211 // in any result we've managed to compute.
Dan Gohman42c31a72010-11-10 01:02:18 +0000212 return ModRefBehavior(AA->getModRefBehavior(CS) & Min);
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000213}
214
215AliasAnalysis::ModRefBehavior
216AliasAnalysis::getModRefBehavior(const Function *F) {
Chris Lattner5a24d702004-05-23 21:15:48 +0000217 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000218 return AA->getModRefBehavior(F);
Chris Lattner5a24d702004-05-23 21:15:48 +0000219}
220
Chris Lattner5a24d702004-05-23 21:15:48 +0000221//===----------------------------------------------------------------------===//
222// AliasAnalysis non-virtual helper method implementation
223//===----------------------------------------------------------------------===//
224
Dan Gohman6d8eb152010-11-11 21:50:19 +0000225AliasAnalysis::Location AliasAnalysis::getLocation(const LoadInst *LI) {
226 return Location(LI->getPointerOperand(),
227 getTypeStoreSize(LI->getType()),
228 LI->getMetadata(LLVMContext::MD_tbaa));
229}
230
231AliasAnalysis::Location AliasAnalysis::getLocation(const StoreInst *SI) {
232 return Location(SI->getPointerOperand(),
233 getTypeStoreSize(SI->getValueOperand()->getType()),
234 SI->getMetadata(LLVMContext::MD_tbaa));
235}
236
237AliasAnalysis::Location AliasAnalysis::getLocation(const VAArgInst *VI) {
238 return Location(VI->getPointerOperand(),
239 UnknownSize,
240 VI->getMetadata(LLVMContext::MD_tbaa));
241}
242
Eli Friedman46cb5af2011-09-26 20:15:28 +0000243AliasAnalysis::Location
244AliasAnalysis::getLocation(const AtomicCmpXchgInst *CXI) {
245 return Location(CXI->getPointerOperand(),
246 getTypeStoreSize(CXI->getCompareOperand()->getType()),
247 CXI->getMetadata(LLVMContext::MD_tbaa));
248}
249
250AliasAnalysis::Location
251AliasAnalysis::getLocation(const AtomicRMWInst *RMWI) {
252 return Location(RMWI->getPointerOperand(),
253 getTypeStoreSize(RMWI->getValOperand()->getType()),
254 RMWI->getMetadata(LLVMContext::MD_tbaa));
255}
Chris Lattnere90c5cb2010-11-21 07:51:27 +0000256
257AliasAnalysis::Location
258AliasAnalysis::getLocationForSource(const MemTransferInst *MTI) {
259 uint64_t Size = UnknownSize;
260 if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
261 Size = C->getValue().getZExtValue();
262
Dan Gohman387f28a2010-12-16 02:51:19 +0000263 // memcpy/memmove can have TBAA tags. For memcpy, they apply
264 // to both the source and the destination.
265 MDNode *TBAATag = MTI->getMetadata(LLVMContext::MD_tbaa);
266
267 return Location(MTI->getRawSource(), Size, TBAATag);
Chris Lattnere90c5cb2010-11-21 07:51:27 +0000268}
269
270AliasAnalysis::Location
Chris Lattner9dc9e812010-11-30 01:48:20 +0000271AliasAnalysis::getLocationForDest(const MemIntrinsic *MTI) {
Chris Lattnere90c5cb2010-11-21 07:51:27 +0000272 uint64_t Size = UnknownSize;
273 if (ConstantInt *C = dyn_cast<ConstantInt>(MTI->getLength()))
274 Size = C->getValue().getZExtValue();
Dan Gohman387f28a2010-12-16 02:51:19 +0000275
276 // memcpy/memmove can have TBAA tags. For memcpy, they apply
277 // to both the source and the destination.
278 MDNode *TBAATag = MTI->getMetadata(LLVMContext::MD_tbaa);
Chris Lattnere90c5cb2010-11-21 07:51:27 +0000279
Dan Gohman387f28a2010-12-16 02:51:19 +0000280 return Location(MTI->getRawDest(), Size, TBAATag);
Chris Lattnere90c5cb2010-11-21 07:51:27 +0000281}
282
283
284
Chris Lattner14ac8772003-02-26 19:26:51 +0000285AliasAnalysis::ModRefResult
Dan Gohmanb2143b62010-09-14 21:25:10 +0000286AliasAnalysis::getModRefInfo(const LoadInst *L, const Location &Loc) {
Eli Friedman667ccf22011-08-15 20:54:19 +0000287 // Be conservative in the face of volatile/atomic.
288 if (!L->isUnordered())
Dan Gohmanb9db52d2010-08-06 18:11:28 +0000289 return ModRef;
290
Dan Gohman14a498a2010-08-03 17:27:43 +0000291 // If the load address doesn't alias the given address, it doesn't read
292 // or write the specified memory.
Dan Gohman6d8eb152010-11-11 21:50:19 +0000293 if (!alias(getLocation(L), Loc))
Dan Gohman14a498a2010-08-03 17:27:43 +0000294 return NoModRef;
295
Dan Gohman14a498a2010-08-03 17:27:43 +0000296 // Otherwise, a load just reads.
297 return Ref;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000298}
299
Chris Lattner14ac8772003-02-26 19:26:51 +0000300AliasAnalysis::ModRefResult
Dan Gohmanb2143b62010-09-14 21:25:10 +0000301AliasAnalysis::getModRefInfo(const StoreInst *S, const Location &Loc) {
Eli Friedman667ccf22011-08-15 20:54:19 +0000302 // Be conservative in the face of volatile/atomic.
303 if (!S->isUnordered())
Dan Gohmanb9db52d2010-08-06 18:11:28 +0000304 return ModRef;
305
Dan Gohman9b8639c2010-08-06 18:10:45 +0000306 // If the store address cannot alias the pointer in question, then the
307 // specified memory cannot be modified by the store.
Dan Gohman6d8eb152010-11-11 21:50:19 +0000308 if (!alias(getLocation(S), Loc))
Chris Lattnerf4d904d2004-01-30 22:16:42 +0000309 return NoModRef;
310
311 // If the pointer is a pointer to constant memory, then it could not have been
312 // modified by this store.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000313 if (pointsToConstantMemory(Loc))
Dan Gohman14a498a2010-08-03 17:27:43 +0000314 return NoModRef;
315
316 // Otherwise, a store just writes.
317 return Mod;
Chris Lattner14ac8772003-02-26 19:26:51 +0000318}
319
Dan Gohmane26a7b52010-08-06 18:24:38 +0000320AliasAnalysis::ModRefResult
Dan Gohmanb2143b62010-09-14 21:25:10 +0000321AliasAnalysis::getModRefInfo(const VAArgInst *V, const Location &Loc) {
Dan Gohmane26a7b52010-08-06 18:24:38 +0000322 // If the va_arg address cannot alias the pointer in question, then the
323 // specified memory cannot be accessed by the va_arg.
Dan Gohman6d8eb152010-11-11 21:50:19 +0000324 if (!alias(getLocation(V), Loc))
Dan Gohmane26a7b52010-08-06 18:24:38 +0000325 return NoModRef;
326
327 // If the pointer is a pointer to constant memory, then it could not have been
328 // modified by this va_arg.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000329 if (pointsToConstantMemory(Loc))
Dan Gohmane26a7b52010-08-06 18:24:38 +0000330 return NoModRef;
331
332 // Otherwise, a va_arg reads and writes.
333 return ModRef;
334}
335
Eli Friedman46cb5af2011-09-26 20:15:28 +0000336AliasAnalysis::ModRefResult
337AliasAnalysis::getModRefInfo(const AtomicCmpXchgInst *CX, const Location &Loc) {
338 // Acquire/Release cmpxchg has properties that matter for arbitrary addresses.
339 if (CX->getOrdering() > Monotonic)
340 return ModRef;
341
342 // If the cmpxchg address does not alias the location, it does not access it.
343 if (!alias(getLocation(CX), Loc))
344 return NoModRef;
345
346 return ModRef;
347}
348
349AliasAnalysis::ModRefResult
350AliasAnalysis::getModRefInfo(const AtomicRMWInst *RMW, const Location &Loc) {
351 // Acquire/Release atomicrmw has properties that matter for arbitrary addresses.
352 if (RMW->getOrdering() > Monotonic)
353 return ModRef;
354
355 // If the atomicrmw address does not alias the location, it does not access it.
356 if (!alias(getLocation(RMW), Loc))
357 return NoModRef;
358
359 return ModRef;
360}
361
Chad Rosier3a884f52012-05-14 20:35:04 +0000362namespace {
363 /// Only find pointer captures which happen before the given instruction. Uses
364 /// the dominator tree to determine whether one instruction is before another.
365 struct CapturesBefore : public CaptureTracker {
366 CapturesBefore(const Instruction *I, DominatorTree *DT)
367 : BeforeHere(I), DT(DT), Captured(false) {}
368
369 void tooManyUses() { Captured = true; }
370
371 bool shouldExplore(Use *U) {
372 Instruction *I = cast<Instruction>(U->getUser());
373 BasicBlock *BB = I->getParent();
374 if (BeforeHere != I &&
375 (!DT->isReachableFromEntry(BB) || DT->dominates(BeforeHere, I)))
376 return false;
377 return true;
378 }
379
380 bool captured(Use *U) {
381 Instruction *I = cast<Instruction>(U->getUser());
382 BasicBlock *BB = I->getParent();
383 if (BeforeHere != I &&
384 (!DT->isReachableFromEntry(BB) || DT->dominates(BeforeHere, I)))
385 return false;
386 Captured = true;
387 return true;
388 }
389
390 const Instruction *BeforeHere;
391 DominatorTree *DT;
392
393 bool Captured;
394 };
395}
396
397// FIXME: this is really just shoring-up a deficiency in alias analysis.
398// BasicAA isn't willing to spend linear time determining whether an alloca
399// was captured before or after this particular call, while we are. However,
400// with a smarter AA in place, this test is just wasting compile time.
401AliasAnalysis::ModRefResult
402AliasAnalysis::callCapturesBefore(const Instruction *I,
403 const AliasAnalysis::Location &MemLoc,
404 DominatorTree *DT) {
405 if (!DT || !TD) return AliasAnalysis::ModRef;
406
407 const Value *Object = GetUnderlyingObject(MemLoc.Ptr, TD);
408 if (!isIdentifiedObject(Object) || isa<GlobalValue>(Object) ||
409 isa<Constant>(Object))
410 return AliasAnalysis::ModRef;
411
412 ImmutableCallSite CS(I);
413 if (!CS.getInstruction() || CS.getInstruction() == Object)
414 return AliasAnalysis::ModRef;
415
416 CapturesBefore CB(I, DT);
417 llvm::PointerMayBeCaptured(Object, &CB);
418 if (CB.Captured)
419 return AliasAnalysis::ModRef;
420
421 unsigned ArgNo = 0;
422 for (ImmutableCallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
423 CI != CE; ++CI, ++ArgNo) {
424 // Only look at the no-capture or byval pointer arguments. If this
425 // pointer were passed to arguments that were neither of these, then it
426 // couldn't be no-capture.
427 if (!(*CI)->getType()->isPointerTy() ||
428 (!CS.doesNotCapture(ArgNo) && !CS.isByValArgument(ArgNo)))
429 continue;
430
431 // If this is a no-capture pointer argument, see if we can tell that it
432 // is impossible to alias the pointer we're checking. If not, we have to
433 // assume that the call could touch the pointer, even though it doesn't
434 // escape.
435 if (!isNoAlias(AliasAnalysis::Location(*CI),
436 AliasAnalysis::Location(Object))) {
437 return AliasAnalysis::ModRef;
438 }
439 }
440 return AliasAnalysis::NoModRef;
441}
Eli Friedman46cb5af2011-09-26 20:15:28 +0000442
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000443// AliasAnalysis destructor: DO NOT move this to the header file for
444// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
445// the AliasAnalysis.o file in the current .a file, causing alias analysis
446// support to not be included in the tool correctly!
447//
448AliasAnalysis::~AliasAnalysis() {}
449
Dan Gohman5a56bf62008-05-30 00:02:02 +0000450/// InitializeAliasAnalysis - Subclasses must call this method to initialize the
Chris Lattner14ac8772003-02-26 19:26:51 +0000451/// AliasAnalysis interface before any other methods are called.
452///
453void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000454 TD = P->getAnalysisIfAvailable<TargetData>();
Chris Lattner5a24d702004-05-23 21:15:48 +0000455 AA = &P->getAnalysis<AliasAnalysis>();
Chris Lattner14ac8772003-02-26 19:26:51 +0000456}
457
458// getAnalysisUsage - All alias analysis implementations should invoke this
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000459// directly (using AliasAnalysis::getAnalysisUsage(AU)).
Chris Lattner14ac8772003-02-26 19:26:51 +0000460void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5a24d702004-05-23 21:15:48 +0000461 AU.addRequired<AliasAnalysis>(); // All AA's chain
Chris Lattner14ac8772003-02-26 19:26:51 +0000462}
463
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000464/// getTypeStoreSize - Return the TargetData store size for the given type,
465/// if known, or a conservative value otherwise.
466///
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000467uint64_t AliasAnalysis::getTypeStoreSize(Type *Ty) {
Dan Gohmanf3a925d2010-10-19 17:06:23 +0000468 return TD ? TD->getTypeStoreSize(Ty) : UnknownSize;
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000469}
470
Chris Lattnerf9355f62002-08-22 22:46:39 +0000471/// canBasicBlockModify - Return true if it is possible for execution of the
472/// specified basic block to modify the value pointed to by Ptr.
473///
Chris Lattner14ac8772003-02-26 19:26:51 +0000474bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
Dan Gohmanb2143b62010-09-14 21:25:10 +0000475 const Location &Loc) {
476 return canInstructionRangeModify(BB.front(), BB.back(), Loc);
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000477}
478
Chris Lattnerf9355f62002-08-22 22:46:39 +0000479/// canInstructionRangeModify - Return true if it is possible for the execution
480/// of the specified instructions to modify the value pointed to by Ptr. The
481/// instructions to consider are all of the instructions in the range of [I1,I2]
482/// INCLUSIVE. I1 and I2 must be in the same basic block.
483///
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000484bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
485 const Instruction &I2,
Dan Gohmanb2143b62010-09-14 21:25:10 +0000486 const Location &Loc) {
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000487 assert(I1.getParent() == I2.getParent() &&
488 "Instructions not in same basic block!");
Dan Gohman79fca6f2010-08-03 21:48:53 +0000489 BasicBlock::const_iterator I = &I1;
490 BasicBlock::const_iterator E = &I2;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000491 ++E; // Convert from inclusive to exclusive range.
492
Chris Lattner14ac8772003-02-26 19:26:51 +0000493 for (; I != E; ++I) // Check every instruction in range
Dan Gohmanb2143b62010-09-14 21:25:10 +0000494 if (getModRefInfo(I, Loc) & Mod)
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000495 return true;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000496 return false;
497}
498
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000499/// isNoAliasCall - Return true if this pointer is returned by a noalias
500/// function.
501bool llvm::isNoAliasCall(const Value *V) {
502 if (isa<CallInst>(V) || isa<InvokeInst>(V))
Dan Gohman79fca6f2010-08-03 21:48:53 +0000503 return ImmutableCallSite(cast<Instruction>(V))
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000504 .paramHasAttr(0, Attribute::NoAlias);
505 return false;
506}
507
508/// isIdentifiedObject - Return true if this pointer refers to a distinct and
509/// identifiable object. This returns true for:
Dan Gohman5753a4a2009-08-27 17:52:56 +0000510/// Global Variables and Functions (but not Global Aliases)
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000511/// Allocas and Mallocs
Dan Gohman9e86f432010-07-07 14:27:09 +0000512/// ByVal and NoAlias Arguments
513/// NoAlias returns
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000514///
Dan Gohman9e86f432010-07-07 14:27:09 +0000515bool llvm::isIdentifiedObject(const Value *V) {
Dan Gohman6be2bd52010-06-29 00:50:39 +0000516 if (isa<AllocaInst>(V))
Dan Gohman5753a4a2009-08-27 17:52:56 +0000517 return true;
518 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000519 return true;
Dan Gohman9e86f432010-07-07 14:27:09 +0000520 if (isNoAliasCall(V))
521 return true;
522 if (const Argument *A = dyn_cast<Argument>(V))
523 return A->hasNoAliasAttr() || A->hasByValAttr();
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000524 return false;
525}
Nick Lewycky55f4ab82012-02-25 10:56:28 +0000526
527/// isKnownNonNull - Return true if we know that the specified value is never
528/// null.
529bool llvm::isKnownNonNull(const Value *V) {
530 // Alloca never returns null, malloc might.
531 if (isa<AllocaInst>(V)) return true;
532
533 // A byval argument is never null.
534 if (const Argument *A = dyn_cast<Argument>(V))
535 return A->hasByValAttr();
536
537 // Global values are not null unless extern weak.
538 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
539 return !GV->hasExternalWeakLinkage();
540 return false;
541}