1 //===- llvm/Analysis/AliasAnalysis.h - Alias Analysis Interface -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the generic AliasAnalysis interface, which is used as the
11 // common interface used by all clients of alias analysis information, and
12 // implemented by all alias analysis implementations. Mod/Ref information is
13 // also captured by this interface.
15 // Implementations of this interface must implement the various virtual methods,
16 // which automatically provides functionality for the entire suite of client
19 // This API identifies memory regions with the Location class. The pointer
20 // component specifies the base memory address of the region. The Size specifies
21 // the maximum size (in address units) of the memory region, or UnknownSize if
22 // the size is not known. The TBAA tag identifies the "type" of the memory
23 // reference; see the TypeBasedAliasAnalysis class for details.
25 // Some non-obvious details include:
26 // - Pointers that point to two completely different objects in memory never
27 // alias, regardless of the value of the Size component.
28 // - NoAlias doesn't imply inequal pointers. The most obvious example of this
29 // is two pointers to constant memory. Even if they are equal, constant
30 // memory is never stored to, so there will never be any dependencies.
31 // In this and other situations, the pointers may be both NoAlias and
32 // MustAlias at the same time. The current API can only return one result,
33 // though this is rarely a problem in practice.
35 //===----------------------------------------------------------------------===//
37 #ifndef LLVM_ANALYSIS_ALIAS_ANALYSIS_H
38 #define LLVM_ANALYSIS_ALIAS_ANALYSIS_H
40 #include "llvm/Support/CallSite.h"
41 #include "llvm/ADT/DenseMap.h"
51 class MemTransferInst;
60 AliasAnalysis *AA; // Previous Alias Analysis to chain to.
63 /// InitializeAliasAnalysis - Subclasses must call this method to initialize
64 /// the AliasAnalysis interface before any other methods are called. This is
65 /// typically called by the run* methods of these subclasses. This may be
66 /// called multiple times.
68 void InitializeAliasAnalysis(Pass *P);
70 /// getAnalysisUsage - All alias analysis implementations should invoke this
71 /// directly (using AliasAnalysis::getAnalysisUsage(AU)).
72 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
75 static char ID; // Class identification, replacement for typeinfo
76 AliasAnalysis() : TD(0), AA(0) {}
77 virtual ~AliasAnalysis(); // We want to be subclassed
79 /// UnknownSize - This is a special value which can be used with the
80 /// size arguments in alias queries to indicate that the caller does not
81 /// know the sizes of the potential memory references.
82 static uint64_t const UnknownSize = ~UINT64_C(0);
84 /// getTargetData - Return a pointer to the current TargetData object, or
85 /// null if no TargetData object is available.
87 const TargetData *getTargetData() const { return TD; }
89 /// getTypeStoreSize - Return the TargetData store size for the given type,
90 /// if known, or a conservative value otherwise.
92 uint64_t getTypeStoreSize(Type *Ty);
94 //===--------------------------------------------------------------------===//
98 /// Location - A description of a memory location.
100 /// Ptr - The address of the start of the location.
102 /// Size - The maximum size of the location, in address-units, or
103 /// UnknownSize if the size is not known. Note that an unknown size does
104 /// not mean the pointer aliases the entire virtual address space, because
105 /// there are restrictions on stepping out of one object and into another.
106 /// See http://llvm.org/docs/LangRef.html#pointeraliasing
108 /// TBAATag - The metadata node which describes the TBAA type of
109 /// the location, or null if there is no known unique tag.
110 const MDNode *TBAATag;
112 explicit Location(const Value *P = 0, uint64_t S = UnknownSize,
114 : Ptr(P), Size(S), TBAATag(N) {}
116 Location getWithNewPtr(const Value *NewPtr) const {
117 Location Copy(*this);
122 Location getWithNewSize(uint64_t NewSize) const {
123 Location Copy(*this);
128 Location getWithoutTBAATag() const {
129 Location Copy(*this);
135 /// getLocation - Fill in Loc with information about the memory reference by
136 /// the given instruction.
137 Location getLocation(const LoadInst *LI);
138 Location getLocation(const StoreInst *SI);
139 Location getLocation(const VAArgInst *VI);
140 Location getLocation(const AtomicCmpXchgInst *CXI);
141 Location getLocation(const AtomicRMWInst *RMWI);
142 static Location getLocationForSource(const MemTransferInst *MTI);
143 static Location getLocationForDest(const MemIntrinsic *MI);
145 /// Alias analysis result - Either we know for sure that it does not alias, we
146 /// know for sure it must alias, or we don't know anything: The two pointers
147 /// _might_ alias. This enum is designed so you can do things like:
148 /// if (AA.alias(P1, P2)) { ... }
149 /// to check to see if two pointers might alias.
151 /// See docs/AliasAnalysis.html for more information on the specific meanings
155 NoAlias = 0, ///< No dependencies.
156 MayAlias, ///< Anything goes.
157 PartialAlias, ///< Pointers differ, but pointees overlap.
158 MustAlias ///< Pointers are equal.
161 /// alias - The main low level interface to the alias analysis implementation.
162 /// Returns an AliasResult indicating whether the two pointers are aliased to
163 /// each other. This is the interface that must be implemented by specific
164 /// alias analysis implementations.
165 virtual AliasResult alias(const Location &LocA, const Location &LocB);
167 /// alias - A convenience wrapper.
168 AliasResult alias(const Value *V1, uint64_t V1Size,
169 const Value *V2, uint64_t V2Size) {
170 return alias(Location(V1, V1Size), Location(V2, V2Size));
173 /// alias - A convenience wrapper.
174 AliasResult alias(const Value *V1, const Value *V2) {
175 return alias(V1, UnknownSize, V2, UnknownSize);
178 /// isNoAlias - A trivial helper function to check to see if the specified
179 /// pointers are no-alias.
180 bool isNoAlias(const Location &LocA, const Location &LocB) {
181 return alias(LocA, LocB) == NoAlias;
184 /// isNoAlias - A convenience wrapper.
185 bool isNoAlias(const Value *V1, uint64_t V1Size,
186 const Value *V2, uint64_t V2Size) {
187 return isNoAlias(Location(V1, V1Size), Location(V2, V2Size));
190 /// isMustAlias - A convenience wrapper.
191 bool isMustAlias(const Location &LocA, const Location &LocB) {
192 return alias(LocA, LocB) == MustAlias;
195 /// isMustAlias - A convenience wrapper.
196 bool isMustAlias(const Value *V1, const Value *V2) {
197 return alias(V1, 1, V2, 1) == MustAlias;
200 /// pointsToConstantMemory - If the specified memory location is
201 /// known to be constant, return true. If OrLocal is true and the
202 /// specified memory location is known to be "local" (derived from
203 /// an alloca), return true. Otherwise return false.
204 virtual bool pointsToConstantMemory(const Location &Loc,
205 bool OrLocal = false);
207 /// pointsToConstantMemory - A convenient wrapper.
208 bool pointsToConstantMemory(const Value *P, bool OrLocal = false) {
209 return pointsToConstantMemory(Location(P), OrLocal);
212 //===--------------------------------------------------------------------===//
213 /// Simple mod/ref information...
216 /// ModRefResult - Represent the result of a mod/ref query. Mod and Ref are
217 /// bits which may be or'd together.
219 enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 };
221 /// These values define additional bits used to define the
222 /// ModRefBehavior values.
223 enum { Nowhere = 0, ArgumentPointees = 4, Anywhere = 8 | ArgumentPointees };
225 /// ModRefBehavior - Summary of how a function affects memory in the program.
226 /// Loads from constant globals are not considered memory accesses for this
227 /// interface. Also, functions may freely modify stack space local to their
228 /// invocation without having to report it through these interfaces.
229 enum ModRefBehavior {
230 /// DoesNotAccessMemory - This function does not perform any non-local loads
231 /// or stores to memory.
233 /// This property corresponds to the GCC 'const' attribute.
234 /// This property corresponds to the LLVM IR 'readnone' attribute.
235 /// This property corresponds to the IntrNoMem LLVM intrinsic flag.
236 DoesNotAccessMemory = Nowhere | NoModRef,
238 /// OnlyReadsArgumentPointees - The only memory references in this function
239 /// (if it has any) are non-volatile loads from objects pointed to by its
240 /// pointer-typed arguments, with arbitrary offsets.
242 /// This property corresponds to the IntrReadArgMem LLVM intrinsic flag.
243 OnlyReadsArgumentPointees = ArgumentPointees | Ref,
245 /// OnlyAccessesArgumentPointees - The only memory references in this
246 /// function (if it has any) are non-volatile loads and stores from objects
247 /// pointed to by its pointer-typed arguments, with arbitrary offsets.
249 /// This property corresponds to the IntrReadWriteArgMem LLVM intrinsic flag.
250 OnlyAccessesArgumentPointees = ArgumentPointees | ModRef,
252 /// OnlyReadsMemory - This function does not perform any non-local stores or
253 /// volatile loads, but may read from any memory location.
255 /// This property corresponds to the GCC 'pure' attribute.
256 /// This property corresponds to the LLVM IR 'readonly' attribute.
257 /// This property corresponds to the IntrReadMem LLVM intrinsic flag.
258 OnlyReadsMemory = Anywhere | Ref,
260 /// UnknownModRefBehavior - This indicates that the function could not be
261 /// classified into one of the behaviors above.
262 UnknownModRefBehavior = Anywhere | ModRef
265 /// getModRefBehavior - Return the behavior when calling the given call site.
266 virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS);
268 /// getModRefBehavior - Return the behavior when calling the given function.
269 /// For use when the call site is not known.
270 virtual ModRefBehavior getModRefBehavior(const Function *F);
272 /// doesNotAccessMemory - If the specified call is known to never read or
273 /// write memory, return true. If the call only reads from known-constant
274 /// memory, it is also legal to return true. Calls that unwind the stack
275 /// are legal for this predicate.
277 /// Many optimizations (such as CSE and LICM) can be performed on such calls
278 /// without worrying about aliasing properties, and many calls have this
279 /// property (e.g. calls to 'sin' and 'cos').
281 /// This property corresponds to the GCC 'const' attribute.
283 bool doesNotAccessMemory(ImmutableCallSite CS) {
284 return getModRefBehavior(CS) == DoesNotAccessMemory;
287 /// doesNotAccessMemory - If the specified function is known to never read or
288 /// write memory, return true. For use when the call site is not known.
290 bool doesNotAccessMemory(const Function *F) {
291 return getModRefBehavior(F) == DoesNotAccessMemory;
294 /// onlyReadsMemory - If the specified call is known to only read from
295 /// non-volatile memory (or not access memory at all), return true. Calls
296 /// that unwind the stack are legal for this predicate.
298 /// This property allows many common optimizations to be performed in the
299 /// absence of interfering store instructions, such as CSE of strlen calls.
301 /// This property corresponds to the GCC 'pure' attribute.
303 bool onlyReadsMemory(ImmutableCallSite CS) {
304 return onlyReadsMemory(getModRefBehavior(CS));
307 /// onlyReadsMemory - If the specified function is known to only read from
308 /// non-volatile memory (or not access memory at all), return true. For use
309 /// when the call site is not known.
311 bool onlyReadsMemory(const Function *F) {
312 return onlyReadsMemory(getModRefBehavior(F));
315 /// onlyReadsMemory - Return true if functions with the specified behavior are
316 /// known to only read from non-volatile memory (or not access memory at all).
318 static bool onlyReadsMemory(ModRefBehavior MRB) {
322 /// onlyAccessesArgPointees - Return true if functions with the specified
323 /// behavior are known to read and write at most from objects pointed to by
324 /// their pointer-typed arguments (with arbitrary offsets).
326 static bool onlyAccessesArgPointees(ModRefBehavior MRB) {
327 return !(MRB & Anywhere & ~ArgumentPointees);
330 /// doesAccessArgPointees - Return true if functions with the specified
331 /// behavior are known to potentially read or write from objects pointed
332 /// to be their pointer-typed arguments (with arbitrary offsets).
334 static bool doesAccessArgPointees(ModRefBehavior MRB) {
335 return (MRB & ModRef) && (MRB & ArgumentPointees);
338 /// getModRefInfo - Return information about whether or not an instruction may
339 /// read or write the specified memory location. An instruction
340 /// that doesn't read or write memory may be trivially LICM'd for example.
341 ModRefResult getModRefInfo(const Instruction *I,
342 const Location &Loc) {
343 switch (I->getOpcode()) {
344 case Instruction::VAArg: return getModRefInfo((const VAArgInst*)I, Loc);
345 case Instruction::Load: return getModRefInfo((const LoadInst*)I, Loc);
346 case Instruction::Store: return getModRefInfo((const StoreInst*)I, Loc);
347 case Instruction::Fence: return getModRefInfo((const FenceInst*)I, Loc);
348 case Instruction::AtomicCmpXchg:
349 return getModRefInfo((const AtomicCmpXchgInst*)I, Loc);
350 case Instruction::AtomicRMW:
351 return getModRefInfo((const AtomicRMWInst*)I, Loc);
352 case Instruction::Call: return getModRefInfo((const CallInst*)I, Loc);
353 case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,Loc);
354 default: return NoModRef;
358 /// getModRefInfo - A convenience wrapper.
359 ModRefResult getModRefInfo(const Instruction *I,
360 const Value *P, uint64_t Size) {
361 return getModRefInfo(I, Location(P, Size));
364 /// getModRefInfo (for call sites) - Return whether information about whether
365 /// a particular call site modifies or reads the specified memory location.
366 virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
367 const Location &Loc);
369 /// getModRefInfo (for call sites) - A convenience wrapper.
370 ModRefResult getModRefInfo(ImmutableCallSite CS,
371 const Value *P, uint64_t Size) {
372 return getModRefInfo(CS, Location(P, Size));
375 /// getModRefInfo (for calls) - Return whether information about whether
376 /// a particular call modifies or reads the specified memory location.
377 ModRefResult getModRefInfo(const CallInst *C, const Location &Loc) {
378 return getModRefInfo(ImmutableCallSite(C), Loc);
381 /// getModRefInfo (for calls) - A convenience wrapper.
382 ModRefResult getModRefInfo(const CallInst *C, const Value *P, uint64_t Size) {
383 return getModRefInfo(C, Location(P, Size));
386 /// getModRefInfo (for invokes) - Return whether information about whether
387 /// a particular invoke modifies or reads the specified memory location.
388 ModRefResult getModRefInfo(const InvokeInst *I,
389 const Location &Loc) {
390 return getModRefInfo(ImmutableCallSite(I), Loc);
393 /// getModRefInfo (for invokes) - A convenience wrapper.
394 ModRefResult getModRefInfo(const InvokeInst *I,
395 const Value *P, uint64_t Size) {
396 return getModRefInfo(I, Location(P, Size));
399 /// getModRefInfo (for loads) - Return whether information about whether
400 /// a particular load modifies or reads the specified memory location.
401 ModRefResult getModRefInfo(const LoadInst *L, const Location &Loc);
403 /// getModRefInfo (for loads) - A convenience wrapper.
404 ModRefResult getModRefInfo(const LoadInst *L, const Value *P, uint64_t Size) {
405 return getModRefInfo(L, Location(P, Size));
408 /// getModRefInfo (for stores) - Return whether information about whether
409 /// a particular store modifies or reads the specified memory location.
410 ModRefResult getModRefInfo(const StoreInst *S, const Location &Loc);
412 /// getModRefInfo (for stores) - A convenience wrapper.
413 ModRefResult getModRefInfo(const StoreInst *S, const Value *P, uint64_t Size){
414 return getModRefInfo(S, Location(P, Size));
417 /// getModRefInfo (for fences) - Return whether information about whether
418 /// a particular store modifies or reads the specified memory location.
419 ModRefResult getModRefInfo(const FenceInst *S, const Location &Loc) {
420 // Conservatively correct. (We could possibly be a bit smarter if
421 // Loc is a alloca that doesn't escape.)
425 /// getModRefInfo (for fences) - A convenience wrapper.
426 ModRefResult getModRefInfo(const FenceInst *S, const Value *P, uint64_t Size){
427 return getModRefInfo(S, Location(P, Size));
430 /// getModRefInfo (for cmpxchges) - Return whether information about whether
431 /// a particular cmpxchg modifies or reads the specified memory location.
432 ModRefResult getModRefInfo(const AtomicCmpXchgInst *CX, const Location &Loc);
434 /// getModRefInfo (for cmpxchges) - A convenience wrapper.
435 ModRefResult getModRefInfo(const AtomicCmpXchgInst *CX,
436 const Value *P, unsigned Size) {
437 return getModRefInfo(CX, Location(P, Size));
440 /// getModRefInfo (for atomicrmws) - Return whether information about whether
441 /// a particular atomicrmw modifies or reads the specified memory location.
442 ModRefResult getModRefInfo(const AtomicRMWInst *RMW, const Location &Loc);
444 /// getModRefInfo (for atomicrmws) - A convenience wrapper.
445 ModRefResult getModRefInfo(const AtomicRMWInst *RMW,
446 const Value *P, unsigned Size) {
447 return getModRefInfo(RMW, Location(P, Size));
450 /// getModRefInfo (for va_args) - Return whether information about whether
451 /// a particular va_arg modifies or reads the specified memory location.
452 ModRefResult getModRefInfo(const VAArgInst* I, const Location &Loc);
454 /// getModRefInfo (for va_args) - A convenience wrapper.
455 ModRefResult getModRefInfo(const VAArgInst* I, const Value* P, uint64_t Size){
456 return getModRefInfo(I, Location(P, Size));
459 /// getModRefInfo - Return information about whether two call sites may refer
460 /// to the same set of memory locations. See
461 /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo
463 virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
464 ImmutableCallSite CS2);
466 /// callCapturesBefore - Return information about whether a particular call
467 /// site modifies or reads the specified memory location.
468 ModRefResult callCapturesBefore(const Instruction *I,
469 const AliasAnalysis::Location &MemLoc,
472 /// callCapturesBefore - A convenience wrapper.
473 ModRefResult callCapturesBefore(const Instruction *I, const Value *P,
474 uint64_t Size, DominatorTree *DT) {
475 return callCapturesBefore(I, Location(P, Size), DT);
478 //===--------------------------------------------------------------------===//
479 /// Higher level methods for querying mod/ref information.
482 /// canBasicBlockModify - Return true if it is possible for execution of the
483 /// specified basic block to modify the value pointed to by Ptr.
484 bool canBasicBlockModify(const BasicBlock &BB, const Location &Loc);
486 /// canBasicBlockModify - A convenience wrapper.
487 bool canBasicBlockModify(const BasicBlock &BB, const Value *P, uint64_t Size){
488 return canBasicBlockModify(BB, Location(P, Size));
491 /// canInstructionRangeModify - Return true if it is possible for the
492 /// execution of the specified instructions to modify the value pointed to by
493 /// Ptr. The instructions to consider are all of the instructions in the
494 /// range of [I1,I2] INCLUSIVE. I1 and I2 must be in the same basic block.
495 bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
496 const Location &Loc);
498 /// canInstructionRangeModify - A convenience wrapper.
499 bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
500 const Value *Ptr, uint64_t Size) {
501 return canInstructionRangeModify(I1, I2, Location(Ptr, Size));
504 //===--------------------------------------------------------------------===//
505 /// Methods that clients should call when they transform the program to allow
506 /// alias analyses to update their internal data structures. Note that these
507 /// methods may be called on any instruction, regardless of whether or not
508 /// they have pointer-analysis implications.
511 /// deleteValue - This method should be called whenever an LLVM Value is
512 /// deleted from the program, for example when an instruction is found to be
513 /// redundant and is eliminated.
515 virtual void deleteValue(Value *V);
517 /// copyValue - This method should be used whenever a preexisting value in the
518 /// program is copied or cloned, introducing a new value. Note that analysis
519 /// implementations should tolerate clients that use this method to introduce
520 /// the same value multiple times: if the analysis already knows about a
521 /// value, it should ignore the request.
523 virtual void copyValue(Value *From, Value *To);
525 /// addEscapingUse - This method should be used whenever an escaping use is
526 /// added to a pointer value. Analysis implementations may either return
527 /// conservative responses for that value in the future, or may recompute
528 /// some or all internal state to continue providing precise responses.
530 /// Escaping uses are considered by anything _except_ the following:
531 /// - GEPs or bitcasts of the pointer
532 /// - Loads through the pointer
533 /// - Stores through (but not of) the pointer
534 virtual void addEscapingUse(Use &U);
536 /// replaceWithNewValue - This method is the obvious combination of the two
537 /// above, and it provided as a helper to simplify client code.
539 void replaceWithNewValue(Value *Old, Value *New) {
545 // Specialize DenseMapInfo for Location.
547 struct DenseMapInfo<AliasAnalysis::Location> {
548 static inline AliasAnalysis::Location getEmptyKey() {
550 AliasAnalysis::Location(DenseMapInfo<const Value *>::getEmptyKey(),
553 static inline AliasAnalysis::Location getTombstoneKey() {
555 AliasAnalysis::Location(DenseMapInfo<const Value *>::getTombstoneKey(),
558 static unsigned getHashValue(const AliasAnalysis::Location &Val) {
559 return DenseMapInfo<const Value *>::getHashValue(Val.Ptr) ^
560 DenseMapInfo<uint64_t>::getHashValue(Val.Size) ^
561 DenseMapInfo<const MDNode *>::getHashValue(Val.TBAATag);
563 static bool isEqual(const AliasAnalysis::Location &LHS,
564 const AliasAnalysis::Location &RHS) {
565 return LHS.Ptr == RHS.Ptr &&
566 LHS.Size == RHS.Size &&
567 LHS.TBAATag == RHS.TBAATag;
571 /// isNoAliasCall - Return true if this pointer is returned by a noalias
573 bool isNoAliasCall(const Value *V);
575 /// isIdentifiedObject - Return true if this pointer refers to a distinct and
576 /// identifiable object. This returns true for:
577 /// Global Variables and Functions (but not Global Aliases)
578 /// Allocas and Mallocs
579 /// ByVal and NoAlias Arguments
582 bool isIdentifiedObject(const Value *V);
584 /// isKnownNonNull - Return true if this pointer couldn't possibly be null by
585 /// its definition. This returns true for allocas, non-extern-weak globals and
587 bool isKnownNonNull(const Value *V);
589 } // End llvm namespace