blob: 536b75986eb39bba76d7757b141b3eacad4709f6 [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 Gohman6ce9d8b2010-08-06 01:25:49 +000068AliasAnalysis::getModRefInfo(ImmutableCallSite CS,
69 const Value *P, unsigned Size) {
70 // Don't assert AA because BasicAA calls us in order to make use of the
71 // logic here.
72
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)
84 if (!isNoAlias(*AI, ~0U, P, Size)) {
85 doesAlias = true;
86 break;
87 }
88
89 if (!doesAlias)
90 return NoModRef;
91 }
92
93 // If P points to a constant memory location, the call definitely could not
94 // modify the memory location.
95 if ((Mask & Mod) && pointsToConstantMemory(P))
96 Mask = ModRefResult(Mask & ~Mod);
97
98 // If this is BasicAA, don't forward.
99 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.
103 return ModRefResult(AA->getModRefInfo(CS, P, Size) & Mask);
104}
105
106AliasAnalysis::ModRefResult
Dan Gohman79fca6f2010-08-03 21:48:53 +0000107AliasAnalysis::getModRefInfo(ImmutableCallSite CS1, ImmutableCallSite CS2) {
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000108 // Don't assert AA because BasicAA calls us in order to make use of the
109 // logic here.
110
111 // If CS1 or CS2 are readnone, they don't interact.
112 ModRefBehavior CS1B = getModRefBehavior(CS1);
113 if (CS1B == DoesNotAccessMemory) return NoModRef;
114
115 ModRefBehavior CS2B = getModRefBehavior(CS2);
116 if (CS2B == DoesNotAccessMemory) return NoModRef;
117
118 // If they both only read from memory, there is no dependence.
119 if (CS1B == OnlyReadsMemory && CS2B == OnlyReadsMemory)
120 return NoModRef;
121
122 AliasAnalysis::ModRefResult Mask = ModRef;
123
124 // If CS1 only reads memory, the only dependence on CS2 can be
125 // from CS1 reading memory written by CS2.
126 if (CS1B == OnlyReadsMemory)
127 Mask = ModRefResult(Mask & Ref);
128
129 // If CS2 only access memory through arguments, accumulate the mod/ref
130 // information from CS1's references to the memory referenced by
131 // CS2's arguments.
132 if (CS2B == AccessesArguments) {
133 AliasAnalysis::ModRefResult R = NoModRef;
134 for (ImmutableCallSite::arg_iterator
135 I = CS2.arg_begin(), E = CS2.arg_end(); I != E; ++I) {
136 R = ModRefResult((R | getModRefInfo(CS1, *I, UnknownSize)) & Mask);
137 if (R == Mask)
138 break;
139 }
140 return R;
141 }
142
143 // If CS1 only accesses memory through arguments, check if CS2 references
144 // any of the memory referenced by CS1's arguments. If not, return NoModRef.
145 if (CS1B == AccessesArguments) {
146 AliasAnalysis::ModRefResult R = NoModRef;
147 for (ImmutableCallSite::arg_iterator
148 I = CS1.arg_begin(), E = CS1.arg_end(); I != E; ++I)
149 if (getModRefInfo(CS2, *I, UnknownSize) != NoModRef) {
150 R = Mask;
151 break;
152 }
153 if (R == NoModRef)
154 return R;
155 }
156
157 // If this is BasicAA, don't forward.
158 if (!AA) return Mask;
159
160 // Otherwise, fall back to the next AA in the chain. But we can merge
161 // in any mask we've managed to compute.
162 return ModRefResult(AA->getModRefInfo(CS1, CS2) & Mask);
163}
164
165AliasAnalysis::ModRefBehavior
166AliasAnalysis::getModRefBehavior(ImmutableCallSite CS) {
167 // Don't assert AA because BasicAA calls us in order to make use of the
168 // logic here.
169
170 ModRefBehavior Min = UnknownModRefBehavior;
171
172 // Call back into the alias analysis with the other form of getModRefBehavior
173 // to see if it can give a better response.
174 if (const Function *F = CS.getCalledFunction())
175 Min = getModRefBehavior(F);
176
177 // If this is BasicAA, don't forward.
178 if (!AA) return Min;
179
180 // Otherwise, fall back to the next AA in the chain. But we can merge
181 // in any result we've managed to compute.
182 return std::min(AA->getModRefBehavior(CS), Min);
183}
184
185AliasAnalysis::ModRefBehavior
186AliasAnalysis::getModRefBehavior(const Function *F) {
Chris Lattner5a24d702004-05-23 21:15:48 +0000187 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!");
Dan Gohman6ce9d8b2010-08-06 01:25:49 +0000188 return AA->getModRefBehavior(F);
Chris Lattner5a24d702004-05-23 21:15:48 +0000189}
190
Dan Gohman65924112010-09-08 01:32:20 +0000191AliasAnalysis::DependenceResult
192AliasAnalysis::getDependence(const Instruction *First,
193 DependenceQueryFlags FirstFlags,
194 const Instruction *Second,
195 DependenceQueryFlags SecondFlags) {
196 assert(AA && "AA didn't call InitializeAliasAnalyais in its run method!");
197 return AA->getDependence(First, FirstFlags, Second, SecondFlags);
198}
Chris Lattner5a24d702004-05-23 21:15:48 +0000199
200//===----------------------------------------------------------------------===//
201// AliasAnalysis non-virtual helper method implementation
202//===----------------------------------------------------------------------===//
203
Chris Lattner14ac8772003-02-26 19:26:51 +0000204AliasAnalysis::ModRefResult
Dan Gohman79fca6f2010-08-03 21:48:53 +0000205AliasAnalysis::getModRefInfo(const LoadInst *L, const Value *P, unsigned Size) {
Dan Gohmanb9db52d2010-08-06 18:11:28 +0000206 // Be conservative in the face of volatile.
207 if (L->isVolatile())
208 return ModRef;
209
Dan Gohman14a498a2010-08-03 17:27:43 +0000210 // If the load address doesn't alias the given address, it doesn't read
211 // or write the specified memory.
212 if (!alias(L->getOperand(0), getTypeStoreSize(L->getType()), P, Size))
213 return NoModRef;
214
Dan Gohman14a498a2010-08-03 17:27:43 +0000215 // Otherwise, a load just reads.
216 return Ref;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000217}
218
Chris Lattner14ac8772003-02-26 19:26:51 +0000219AliasAnalysis::ModRefResult
Dan Gohman79fca6f2010-08-03 21:48:53 +0000220AliasAnalysis::getModRefInfo(const StoreInst *S, const Value *P, unsigned Size) {
Dan Gohmanb9db52d2010-08-06 18:11:28 +0000221 // Be conservative in the face of volatile.
222 if (S->isVolatile())
223 return ModRef;
224
Dan Gohman9b8639c2010-08-06 18:10:45 +0000225 // If the store address cannot alias the pointer in question, then the
226 // specified memory cannot be modified by the store.
Duncan Sands514ab342007-11-01 20:53:16 +0000227 if (!alias(S->getOperand(1),
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000228 getTypeStoreSize(S->getOperand(0)->getType()), P, Size))
Chris Lattnerf4d904d2004-01-30 22:16:42 +0000229 return NoModRef;
230
231 // If the pointer is a pointer to constant memory, then it could not have been
232 // modified by this store.
Dan Gohman14a498a2010-08-03 17:27:43 +0000233 if (pointsToConstantMemory(P))
234 return NoModRef;
235
236 // Otherwise, a store just writes.
237 return Mod;
Chris Lattner14ac8772003-02-26 19:26:51 +0000238}
239
Dan Gohmane26a7b52010-08-06 18:24:38 +0000240AliasAnalysis::ModRefResult
241AliasAnalysis::getModRefInfo(const VAArgInst *V, const Value *P, unsigned Size) {
242 // If the va_arg address cannot alias the pointer in question, then the
243 // specified memory cannot be accessed by the va_arg.
244 if (!alias(V->getOperand(0), UnknownSize, P, Size))
245 return NoModRef;
246
247 // If the pointer is a pointer to constant memory, then it could not have been
248 // modified by this va_arg.
249 if (pointsToConstantMemory(P))
250 return NoModRef;
251
252 // Otherwise, a va_arg reads and writes.
253 return ModRef;
254}
255
Dan Gohman65924112010-09-08 01:32:20 +0000256AliasAnalysis::DependenceResult
257AliasAnalysis::getDependenceViaModRefInfo(const Instruction *First,
258 DependenceQueryFlags FirstFlags,
259 const Instruction *Second,
260 DependenceQueryFlags SecondFlags) {
261 if (const LoadInst *L = dyn_cast<LoadInst>(First)) {
262 // Be over-conservative with volatile for now.
263 if (L->isVolatile())
264 return Unknown;
265
266 // Forward this query to getModRefInfo.
267 switch (getModRefInfo(Second,
268 L->getPointerOperand(),
269 getTypeStoreSize(L->getType()))) {
270 case NoModRef:
271 // Second doesn't reference First's memory, so they're independent.
272 return Independent;
273
274 case Ref:
275 // Second only reads from the memory read from by First. If it
276 // also writes to any other memory, be conservative.
277 if (Second->mayWriteToMemory())
278 return Unknown;
279
280 // If it's loading the same size from the same address, we can
281 // give a more precise result.
282 if (const LoadInst *SecondL = dyn_cast<LoadInst>(Second)) {
283 unsigned LSize = getTypeStoreSize(L->getType());
284 unsigned SecondLSize = getTypeStoreSize(SecondL->getType());
285 if (alias(L->getPointerOperand(), LSize,
286 SecondL->getPointerOperand(), SecondLSize) ==
287 MustAlias) {
288 // If the loads are the same size, it's ReadThenRead.
289 if (LSize == SecondLSize)
290 return ReadThenRead;
291
292 // If the second load is smaller, it's only ReadThenReadSome.
293 if (LSize > SecondLSize)
294 return ReadThenReadSome;
295 }
296 }
297
298 // Otherwise it's just two loads.
299 return Independent;
300
301 case Mod:
302 // Second only writes to the memory read from by First. If it
303 // also reads from any other memory, be conservative.
304 if (Second->mayReadFromMemory())
305 return Unknown;
306
307 // If it's storing the same size to the same address, we can
308 // give a more precise result.
309 if (const StoreInst *SecondS = dyn_cast<StoreInst>(Second)) {
310 unsigned LSize = getTypeStoreSize(L->getType());
311 unsigned SecondSSize = getTypeStoreSize(SecondS->getType());
312 if (alias(L->getPointerOperand(), LSize,
313 SecondS->getPointerOperand(), SecondSSize) ==
314 MustAlias) {
315 // If the load and the store are the same size, it's ReadThenWrite.
316 if (LSize == SecondSSize)
317 return ReadThenWrite;
318 }
319 }
320
321 // Otherwise we don't know if it could be writing to other memory.
322 return Unknown;
323
324 case ModRef:
325 // Second reads and writes to the memory read from by First.
326 // We don't have a way to express that.
327 return Unknown;
328 }
329
330 } else if (const StoreInst *S = dyn_cast<StoreInst>(First)) {
331 // Be over-conservative with volatile for now.
332 if (S->isVolatile())
333 return Unknown;
334
335 // Forward this query to getModRefInfo.
336 switch (getModRefInfo(Second,
337 S->getPointerOperand(),
338 getTypeStoreSize(S->getValueOperand()->getType()))) {
339 case NoModRef:
340 // Second doesn't reference First's memory, so they're independent.
341 return Independent;
342
343 case Ref:
344 // Second only reads from the memory written to by First. If it
345 // also writes to any other memory, be conservative.
346 if (Second->mayWriteToMemory())
347 return Unknown;
348
349 // If it's loading the same size from the same address, we can
350 // give a more precise result.
351 if (const LoadInst *SecondL = dyn_cast<LoadInst>(Second)) {
352 unsigned SSize = getTypeStoreSize(S->getValueOperand()->getType());
353 unsigned SecondLSize = getTypeStoreSize(SecondL->getType());
354 if (alias(S->getPointerOperand(), SSize,
355 SecondL->getPointerOperand(), SecondLSize) ==
356 MustAlias) {
357 // If the store and the load are the same size, it's WriteThenRead.
358 if (SSize == SecondLSize)
359 return WriteThenRead;
360
361 // If the load is smaller, it's only WriteThenReadSome.
362 if (SSize > SecondLSize)
363 return WriteThenReadSome;
364 }
365 }
366
367 // Otherwise we don't know if it could be reading from other memory.
368 return Unknown;
369
370 case Mod:
371 // Second only writes to the memory written to by First. If it
372 // also reads from any other memory, be conservative.
373 if (Second->mayReadFromMemory())
374 return Unknown;
375
376 // If it's storing the same size to the same address, we can
377 // give a more precise result.
378 if (const StoreInst *SecondS = dyn_cast<StoreInst>(Second)) {
379 unsigned SSize = getTypeStoreSize(S->getValueOperand()->getType());
380 unsigned SecondSSize = getTypeStoreSize(SecondS->getType());
381 if (alias(S->getPointerOperand(), SSize,
382 SecondS->getPointerOperand(), SecondSSize) ==
383 MustAlias) {
384 // If the stores are the same size, it's WriteThenWrite.
385 if (SSize == SecondSSize)
386 return WriteThenWrite;
387
388 // If the second store is larger, it's only WriteSomeThenWrite.
389 if (SSize < SecondSSize)
390 return WriteSomeThenWrite;
391 }
392 }
393
394 // Otherwise we don't know if it could be writing to other memory.
395 return Unknown;
396
397 case ModRef:
398 // Second reads and writes to the memory written to by First.
399 // We don't have a way to express that.
400 return Unknown;
401 }
402
403 } else if (const VAArgInst *V = dyn_cast<VAArgInst>(First)) {
404 // Forward this query to getModRefInfo.
405 if (getModRefInfo(Second, V->getOperand(0), UnknownSize) == NoModRef)
406 // Second doesn't reference First's memory, so they're independent.
407 return Independent;
408
409 } else if (ImmutableCallSite FirstCS = cast<Value>(First)) {
410 // If both instructions are calls/invokes we can use the two-callsite
411 // form of getModRefInfo.
412 if (ImmutableCallSite SecondCS = cast<Value>(Second))
413 // getModRefInfo's arguments are backwards from intuition.
414 switch (getModRefInfo(SecondCS, FirstCS)) {
415 case NoModRef:
416 // Second doesn't reference First's memory, so they're independent.
417 return Independent;
418
419 case Ref:
420 // If they're both read-only, there's no dependence.
421 if (FirstCS.onlyReadsMemory() && SecondCS.onlyReadsMemory())
422 return Independent;
423
424 // Otherwise it's not obvious what we can do here.
425 return Unknown;
426
427 case Mod:
428 // It's not obvious what we can do here.
429 return Unknown;
430
431 case ModRef:
432 // I know, right?
433 return Unknown;
434 }
435 }
436
437 // For anything else, be conservative.
438 return Unknown;
439}
Dan Gohmane26a7b52010-08-06 18:24:38 +0000440
Duncan Sandsdff67102007-12-01 07:51:45 +0000441AliasAnalysis::ModRefBehavior
Dan Gohman79fca6f2010-08-03 21:48:53 +0000442AliasAnalysis::getIntrinsicModRefBehavior(unsigned iid) {
Duncan Sandsd869b382009-02-14 10:56:35 +0000443#define GET_INTRINSIC_MODREF_BEHAVIOR
444#include "llvm/Intrinsics.gen"
445#undef GET_INTRINSIC_MODREF_BEHAVIOR
Duncan Sandsdff67102007-12-01 07:51:45 +0000446}
447
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000448// AliasAnalysis destructor: DO NOT move this to the header file for
449// AliasAnalysis or else clients of the AliasAnalysis class may not depend on
450// the AliasAnalysis.o file in the current .a file, causing alias analysis
451// support to not be included in the tool correctly!
452//
453AliasAnalysis::~AliasAnalysis() {}
454
Dan Gohman5a56bf62008-05-30 00:02:02 +0000455/// InitializeAliasAnalysis - Subclasses must call this method to initialize the
Chris Lattner14ac8772003-02-26 19:26:51 +0000456/// AliasAnalysis interface before any other methods are called.
457///
458void AliasAnalysis::InitializeAliasAnalysis(Pass *P) {
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000459 TD = P->getAnalysisIfAvailable<TargetData>();
Chris Lattner5a24d702004-05-23 21:15:48 +0000460 AA = &P->getAnalysis<AliasAnalysis>();
Chris Lattner14ac8772003-02-26 19:26:51 +0000461}
462
463// getAnalysisUsage - All alias analysis implementations should invoke this
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000464// directly (using AliasAnalysis::getAnalysisUsage(AU)).
Chris Lattner14ac8772003-02-26 19:26:51 +0000465void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5a24d702004-05-23 21:15:48 +0000466 AU.addRequired<AliasAnalysis>(); // All AA's chain
Chris Lattner14ac8772003-02-26 19:26:51 +0000467}
468
Dan Gohmanfc2a3ed2009-07-25 00:48:42 +0000469/// getTypeStoreSize - Return the TargetData store size for the given type,
470/// if known, or a conservative value otherwise.
471///
472unsigned AliasAnalysis::getTypeStoreSize(const Type *Ty) {
473 return TD ? TD->getTypeStoreSize(Ty) : ~0u;
474}
475
Chris Lattnerf9355f62002-08-22 22:46:39 +0000476/// canBasicBlockModify - Return true if it is possible for execution of the
477/// specified basic block to modify the value pointed to by Ptr.
478///
Chris Lattner14ac8772003-02-26 19:26:51 +0000479bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB,
480 const Value *Ptr, unsigned Size) {
481 return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size);
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000482}
483
Chris Lattnerf9355f62002-08-22 22:46:39 +0000484/// canInstructionRangeModify - Return true if it is possible for the execution
485/// of the specified instructions to modify the value pointed to by Ptr. The
486/// instructions to consider are all of the instructions in the range of [I1,I2]
487/// INCLUSIVE. I1 and I2 must be in the same basic block.
488///
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000489bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1,
490 const Instruction &I2,
Chris Lattner14ac8772003-02-26 19:26:51 +0000491 const Value *Ptr, unsigned Size) {
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000492 assert(I1.getParent() == I2.getParent() &&
493 "Instructions not in same basic block!");
Dan Gohman79fca6f2010-08-03 21:48:53 +0000494 BasicBlock::const_iterator I = &I1;
495 BasicBlock::const_iterator E = &I2;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000496 ++E; // Convert from inclusive to exclusive range.
497
Chris Lattner14ac8772003-02-26 19:26:51 +0000498 for (; I != E; ++I) // Check every instruction in range
Dan Gohman79fca6f2010-08-03 21:48:53 +0000499 if (getModRefInfo(I, Ptr, Size) & Mod)
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000500 return true;
Chris Lattner53ad0ed2002-08-22 18:25:32 +0000501 return false;
502}
503
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000504/// isNoAliasCall - Return true if this pointer is returned by a noalias
505/// function.
506bool llvm::isNoAliasCall(const Value *V) {
507 if (isa<CallInst>(V) || isa<InvokeInst>(V))
Dan Gohman79fca6f2010-08-03 21:48:53 +0000508 return ImmutableCallSite(cast<Instruction>(V))
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000509 .paramHasAttr(0, Attribute::NoAlias);
510 return false;
511}
512
513/// isIdentifiedObject - Return true if this pointer refers to a distinct and
514/// identifiable object. This returns true for:
Dan Gohman5753a4a2009-08-27 17:52:56 +0000515/// Global Variables and Functions (but not Global Aliases)
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000516/// Allocas and Mallocs
Dan Gohman9e86f432010-07-07 14:27:09 +0000517/// ByVal and NoAlias Arguments
518/// NoAlias returns
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000519///
Dan Gohman9e86f432010-07-07 14:27:09 +0000520bool llvm::isIdentifiedObject(const Value *V) {
Dan Gohman6be2bd52010-06-29 00:50:39 +0000521 if (isa<AllocaInst>(V))
Dan Gohman5753a4a2009-08-27 17:52:56 +0000522 return true;
523 if (isa<GlobalValue>(V) && !isa<GlobalAlias>(V))
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000524 return true;
Dan Gohman9e86f432010-07-07 14:27:09 +0000525 if (isNoAliasCall(V))
526 return true;
527 if (const Argument *A = dyn_cast<Argument>(V))
528 return A->hasNoAliasAttr() || A->hasByValAttr();
Dan Gohmana5f81bb2009-02-03 01:28:32 +0000529 return false;
530}
531
Chris Lattnerd501c132003-02-26 19:41:54 +0000532// Because of the way .a files work, we must force the BasicAA implementation to
533// be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run
534// the risk of AliasAnalysis being used, but the default implementation not
535// being linked into the tool that uses it.
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000536DEFINING_FILE_FOR(AliasAnalysis)