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"
49 class TargetLibraryInfo;
52 class MemTransferInst;
59 const TargetLibraryInfo *TLI;
62 AliasAnalysis *AA; // Previous Alias Analysis to chain to.
65 /// InitializeAliasAnalysis - Subclasses must call this method to initialize
66 /// the AliasAnalysis interface before any other methods are called. This is
67 /// typically called by the run* methods of these subclasses. This may be
68 /// called multiple times.
70 void InitializeAliasAnalysis(Pass *P);
72 /// getAnalysisUsage - All alias analysis implementations should invoke this
73 /// directly (using AliasAnalysis::getAnalysisUsage(AU)).
74 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
77 static char ID; // Class identification, replacement for typeinfo
78 AliasAnalysis() : TD(0), TLI(0), AA(0) {}
79 virtual ~AliasAnalysis(); // We want to be subclassed
81 /// UnknownSize - This is a special value which can be used with the
82 /// size arguments in alias queries to indicate that the caller does not
83 /// know the sizes of the potential memory references.
84 static uint64_t const UnknownSize = ~UINT64_C(0);
86 /// getTargetData - Return a pointer to the current TargetData object, or
87 /// null if no TargetData object is available.
89 const TargetData *getTargetData() const { return TD; }
91 /// getTargetLibraryInfo - Return a pointer to the current TargetLibraryInfo
92 /// object, or null if no TargetLibraryInfo object is available.
94 const TargetLibraryInfo *getTargetLibraryInfo() const { return TLI; }
96 /// getTypeStoreSize - Return the TargetData store size for the given type,
97 /// if known, or a conservative value otherwise.
99 uint64_t getTypeStoreSize(Type *Ty);
101 //===--------------------------------------------------------------------===//
105 /// Location - A description of a memory location.
107 /// Ptr - The address of the start of the location.
109 /// Size - The maximum size of the location, in address-units, or
110 /// UnknownSize if the size is not known. Note that an unknown size does
111 /// not mean the pointer aliases the entire virtual address space, because
112 /// there are restrictions on stepping out of one object and into another.
113 /// See http://llvm.org/docs/LangRef.html#pointeraliasing
115 /// TBAATag - The metadata node which describes the TBAA type of
116 /// the location, or null if there is no known unique tag.
117 const MDNode *TBAATag;
119 explicit Location(const Value *P = 0, uint64_t S = UnknownSize,
121 : Ptr(P), Size(S), TBAATag(N) {}
123 Location getWithNewPtr(const Value *NewPtr) const {
124 Location Copy(*this);
129 Location getWithNewSize(uint64_t NewSize) const {
130 Location Copy(*this);
135 Location getWithoutTBAATag() const {
136 Location Copy(*this);
142 /// getLocation - Fill in Loc with information about the memory reference by
143 /// the given instruction.
144 Location getLocation(const LoadInst *LI);
145 Location getLocation(const StoreInst *SI);
146 Location getLocation(const VAArgInst *VI);
147 Location getLocation(const AtomicCmpXchgInst *CXI);
148 Location getLocation(const AtomicRMWInst *RMWI);
149 static Location getLocationForSource(const MemTransferInst *MTI);
150 static Location getLocationForDest(const MemIntrinsic *MI);
152 /// Alias analysis result - Either we know for sure that it does not alias, we
153 /// know for sure it must alias, or we don't know anything: The two pointers
154 /// _might_ alias. This enum is designed so you can do things like:
155 /// if (AA.alias(P1, P2)) { ... }
156 /// to check to see if two pointers might alias.
158 /// See docs/AliasAnalysis.html for more information on the specific meanings
162 NoAlias = 0, ///< No dependencies.
163 MayAlias, ///< Anything goes.
164 PartialAlias, ///< Pointers differ, but pointees overlap.
165 MustAlias ///< Pointers are equal.
168 /// alias - The main low level interface to the alias analysis implementation.
169 /// Returns an AliasResult indicating whether the two pointers are aliased to
170 /// each other. This is the interface that must be implemented by specific
171 /// alias analysis implementations.
172 virtual AliasResult alias(const Location &LocA, const Location &LocB);
174 /// alias - A convenience wrapper.
175 AliasResult alias(const Value *V1, uint64_t V1Size,
176 const Value *V2, uint64_t V2Size) {
177 return alias(Location(V1, V1Size), Location(V2, V2Size));
180 /// alias - A convenience wrapper.
181 AliasResult alias(const Value *V1, const Value *V2) {
182 return alias(V1, UnknownSize, V2, UnknownSize);
185 /// isNoAlias - A trivial helper function to check to see if the specified
186 /// pointers are no-alias.
187 bool isNoAlias(const Location &LocA, const Location &LocB) {
188 return alias(LocA, LocB) == NoAlias;
191 /// isNoAlias - A convenience wrapper.
192 bool isNoAlias(const Value *V1, uint64_t V1Size,
193 const Value *V2, uint64_t V2Size) {
194 return isNoAlias(Location(V1, V1Size), Location(V2, V2Size));
197 /// isMustAlias - A convenience wrapper.
198 bool isMustAlias(const Location &LocA, const Location &LocB) {
199 return alias(LocA, LocB) == MustAlias;
202 /// isMustAlias - A convenience wrapper.
203 bool isMustAlias(const Value *V1, const Value *V2) {
204 return alias(V1, 1, V2, 1) == MustAlias;
207 /// pointsToConstantMemory - If the specified memory location is
208 /// known to be constant, return true. If OrLocal is true and the
209 /// specified memory location is known to be "local" (derived from
210 /// an alloca), return true. Otherwise return false.
211 virtual bool pointsToConstantMemory(const Location &Loc,
212 bool OrLocal = false);
214 /// pointsToConstantMemory - A convenient wrapper.
215 bool pointsToConstantMemory(const Value *P, bool OrLocal = false) {
216 return pointsToConstantMemory(Location(P), OrLocal);
219 //===--------------------------------------------------------------------===//
220 /// Simple mod/ref information...
223 /// ModRefResult - Represent the result of a mod/ref query. Mod and Ref are
224 /// bits which may be or'd together.
226 enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 };
228 /// These values define additional bits used to define the
229 /// ModRefBehavior values.
230 enum { Nowhere = 0, ArgumentPointees = 4, Anywhere = 8 | ArgumentPointees };
232 /// ModRefBehavior - Summary of how a function affects memory in the program.
233 /// Loads from constant globals are not considered memory accesses for this
234 /// interface. Also, functions may freely modify stack space local to their
235 /// invocation without having to report it through these interfaces.
236 enum ModRefBehavior {
237 /// DoesNotAccessMemory - This function does not perform any non-local loads
238 /// or stores to memory.
240 /// This property corresponds to the GCC 'const' attribute.
241 /// This property corresponds to the LLVM IR 'readnone' attribute.
242 /// This property corresponds to the IntrNoMem LLVM intrinsic flag.
243 DoesNotAccessMemory = Nowhere | NoModRef,
245 /// OnlyReadsArgumentPointees - The only memory references in this function
246 /// (if it has any) are non-volatile loads from objects pointed to by its
247 /// pointer-typed arguments, with arbitrary offsets.
249 /// This property corresponds to the IntrReadArgMem LLVM intrinsic flag.
250 OnlyReadsArgumentPointees = ArgumentPointees | Ref,
252 /// OnlyAccessesArgumentPointees - The only memory references in this
253 /// function (if it has any) are non-volatile loads and stores from objects
254 /// pointed to by its pointer-typed arguments, with arbitrary offsets.
256 /// This property corresponds to the IntrReadWriteArgMem LLVM intrinsic flag.
257 OnlyAccessesArgumentPointees = ArgumentPointees | ModRef,
259 /// OnlyReadsMemory - This function does not perform any non-local stores or
260 /// volatile loads, but may read from any memory location.
262 /// This property corresponds to the GCC 'pure' attribute.
263 /// This property corresponds to the LLVM IR 'readonly' attribute.
264 /// This property corresponds to the IntrReadMem LLVM intrinsic flag.
265 OnlyReadsMemory = Anywhere | Ref,
267 /// UnknownModRefBehavior - This indicates that the function could not be
268 /// classified into one of the behaviors above.
269 UnknownModRefBehavior = Anywhere | ModRef
272 /// getModRefBehavior - Return the behavior when calling the given call site.
273 virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS);
275 /// getModRefBehavior - Return the behavior when calling the given function.
276 /// For use when the call site is not known.
277 virtual ModRefBehavior getModRefBehavior(const Function *F);
279 /// doesNotAccessMemory - If the specified call is known to never read or
280 /// write memory, return true. If the call only reads from known-constant
281 /// memory, it is also legal to return true. Calls that unwind the stack
282 /// are legal for this predicate.
284 /// Many optimizations (such as CSE and LICM) can be performed on such calls
285 /// without worrying about aliasing properties, and many calls have this
286 /// property (e.g. calls to 'sin' and 'cos').
288 /// This property corresponds to the GCC 'const' attribute.
290 bool doesNotAccessMemory(ImmutableCallSite CS) {
291 return getModRefBehavior(CS) == DoesNotAccessMemory;
294 /// doesNotAccessMemory - If the specified function is known to never read or
295 /// write memory, return true. For use when the call site is not known.
297 bool doesNotAccessMemory(const Function *F) {
298 return getModRefBehavior(F) == DoesNotAccessMemory;
301 /// onlyReadsMemory - If the specified call is known to only read from
302 /// non-volatile memory (or not access memory at all), return true. Calls
303 /// that unwind the stack are legal for this predicate.
305 /// This property allows many common optimizations to be performed in the
306 /// absence of interfering store instructions, such as CSE of strlen calls.
308 /// This property corresponds to the GCC 'pure' attribute.
310 bool onlyReadsMemory(ImmutableCallSite CS) {
311 return onlyReadsMemory(getModRefBehavior(CS));
314 /// onlyReadsMemory - If the specified function is known to only read from
315 /// non-volatile memory (or not access memory at all), return true. For use
316 /// when the call site is not known.
318 bool onlyReadsMemory(const Function *F) {
319 return onlyReadsMemory(getModRefBehavior(F));
322 /// onlyReadsMemory - Return true if functions with the specified behavior are
323 /// known to only read from non-volatile memory (or not access memory at all).
325 static bool onlyReadsMemory(ModRefBehavior MRB) {
329 /// onlyAccessesArgPointees - Return true if functions with the specified
330 /// behavior are known to read and write at most from objects pointed to by
331 /// their pointer-typed arguments (with arbitrary offsets).
333 static bool onlyAccessesArgPointees(ModRefBehavior MRB) {
334 return !(MRB & Anywhere & ~ArgumentPointees);
337 /// doesAccessArgPointees - Return true if functions with the specified
338 /// behavior are known to potentially read or write from objects pointed
339 /// to be their pointer-typed arguments (with arbitrary offsets).
341 static bool doesAccessArgPointees(ModRefBehavior MRB) {
342 return (MRB & ModRef) && (MRB & ArgumentPointees);
345 /// getModRefInfo - Return information about whether or not an instruction may
346 /// read or write the specified memory location. An instruction
347 /// that doesn't read or write memory may be trivially LICM'd for example.
348 ModRefResult getModRefInfo(const Instruction *I,
349 const Location &Loc) {
350 switch (I->getOpcode()) {
351 case Instruction::VAArg: return getModRefInfo((const VAArgInst*)I, Loc);
352 case Instruction::Load: return getModRefInfo((const LoadInst*)I, Loc);
353 case Instruction::Store: return getModRefInfo((const StoreInst*)I, Loc);
354 case Instruction::Fence: return getModRefInfo((const FenceInst*)I, Loc);
355 case Instruction::AtomicCmpXchg:
356 return getModRefInfo((const AtomicCmpXchgInst*)I, Loc);
357 case Instruction::AtomicRMW:
358 return getModRefInfo((const AtomicRMWInst*)I, Loc);
359 case Instruction::Call: return getModRefInfo((const CallInst*)I, Loc);
360 case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,Loc);
361 default: return NoModRef;
365 /// getModRefInfo - A convenience wrapper.
366 ModRefResult getModRefInfo(const Instruction *I,
367 const Value *P, uint64_t Size) {
368 return getModRefInfo(I, Location(P, Size));
371 /// getModRefInfo (for call sites) - Return whether information about whether
372 /// a particular call site modifies or reads the specified memory location.
373 virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
374 const Location &Loc);
376 /// getModRefInfo (for call sites) - A convenience wrapper.
377 ModRefResult getModRefInfo(ImmutableCallSite CS,
378 const Value *P, uint64_t Size) {
379 return getModRefInfo(CS, Location(P, Size));
382 /// getModRefInfo (for calls) - Return whether information about whether
383 /// a particular call modifies or reads the specified memory location.
384 ModRefResult getModRefInfo(const CallInst *C, const Location &Loc) {
385 return getModRefInfo(ImmutableCallSite(C), Loc);
388 /// getModRefInfo (for calls) - A convenience wrapper.
389 ModRefResult getModRefInfo(const CallInst *C, const Value *P, uint64_t Size) {
390 return getModRefInfo(C, Location(P, Size));
393 /// getModRefInfo (for invokes) - Return whether information about whether
394 /// a particular invoke modifies or reads the specified memory location.
395 ModRefResult getModRefInfo(const InvokeInst *I,
396 const Location &Loc) {
397 return getModRefInfo(ImmutableCallSite(I), Loc);
400 /// getModRefInfo (for invokes) - A convenience wrapper.
401 ModRefResult getModRefInfo(const InvokeInst *I,
402 const Value *P, uint64_t Size) {
403 return getModRefInfo(I, Location(P, Size));
406 /// getModRefInfo (for loads) - Return whether information about whether
407 /// a particular load modifies or reads the specified memory location.
408 ModRefResult getModRefInfo(const LoadInst *L, const Location &Loc);
410 /// getModRefInfo (for loads) - A convenience wrapper.
411 ModRefResult getModRefInfo(const LoadInst *L, const Value *P, uint64_t Size) {
412 return getModRefInfo(L, Location(P, Size));
415 /// getModRefInfo (for stores) - Return whether information about whether
416 /// a particular store modifies or reads the specified memory location.
417 ModRefResult getModRefInfo(const StoreInst *S, const Location &Loc);
419 /// getModRefInfo (for stores) - A convenience wrapper.
420 ModRefResult getModRefInfo(const StoreInst *S, const Value *P, uint64_t Size){
421 return getModRefInfo(S, Location(P, Size));
424 /// getModRefInfo (for fences) - Return whether information about whether
425 /// a particular store modifies or reads the specified memory location.
426 ModRefResult getModRefInfo(const FenceInst *S, const Location &Loc) {
427 // Conservatively correct. (We could possibly be a bit smarter if
428 // Loc is a alloca that doesn't escape.)
432 /// getModRefInfo (for fences) - A convenience wrapper.
433 ModRefResult getModRefInfo(const FenceInst *S, const Value *P, uint64_t Size){
434 return getModRefInfo(S, Location(P, Size));
437 /// getModRefInfo (for cmpxchges) - Return whether information about whether
438 /// a particular cmpxchg modifies or reads the specified memory location.
439 ModRefResult getModRefInfo(const AtomicCmpXchgInst *CX, const Location &Loc);
441 /// getModRefInfo (for cmpxchges) - A convenience wrapper.
442 ModRefResult getModRefInfo(const AtomicCmpXchgInst *CX,
443 const Value *P, unsigned Size) {
444 return getModRefInfo(CX, Location(P, Size));
447 /// getModRefInfo (for atomicrmws) - Return whether information about whether
448 /// a particular atomicrmw modifies or reads the specified memory location.
449 ModRefResult getModRefInfo(const AtomicRMWInst *RMW, const Location &Loc);
451 /// getModRefInfo (for atomicrmws) - A convenience wrapper.
452 ModRefResult getModRefInfo(const AtomicRMWInst *RMW,
453 const Value *P, unsigned Size) {
454 return getModRefInfo(RMW, Location(P, Size));
457 /// getModRefInfo (for va_args) - Return whether information about whether
458 /// a particular va_arg modifies or reads the specified memory location.
459 ModRefResult getModRefInfo(const VAArgInst* I, const Location &Loc);
461 /// getModRefInfo (for va_args) - A convenience wrapper.
462 ModRefResult getModRefInfo(const VAArgInst* I, const Value* P, uint64_t Size){
463 return getModRefInfo(I, Location(P, Size));
466 /// getModRefInfo - Return information about whether two call sites may refer
467 /// to the same set of memory locations. See
468 /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo
470 virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
471 ImmutableCallSite CS2);
473 /// callCapturesBefore - Return information about whether a particular call
474 /// site modifies or reads the specified memory location.
475 ModRefResult callCapturesBefore(const Instruction *I,
476 const AliasAnalysis::Location &MemLoc,
479 /// callCapturesBefore - A convenience wrapper.
480 ModRefResult callCapturesBefore(const Instruction *I, const Value *P,
481 uint64_t Size, DominatorTree *DT) {
482 return callCapturesBefore(I, Location(P, Size), DT);
485 //===--------------------------------------------------------------------===//
486 /// Higher level methods for querying mod/ref information.
489 /// canBasicBlockModify - Return true if it is possible for execution of the
490 /// specified basic block to modify the value pointed to by Ptr.
491 bool canBasicBlockModify(const BasicBlock &BB, const Location &Loc);
493 /// canBasicBlockModify - A convenience wrapper.
494 bool canBasicBlockModify(const BasicBlock &BB, const Value *P, uint64_t Size){
495 return canBasicBlockModify(BB, Location(P, Size));
498 /// canInstructionRangeModify - Return true if it is possible for the
499 /// execution of the specified instructions to modify the value pointed to by
500 /// Ptr. The instructions to consider are all of the instructions in the
501 /// range of [I1,I2] INCLUSIVE. I1 and I2 must be in the same basic block.
502 bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
503 const Location &Loc);
505 /// canInstructionRangeModify - A convenience wrapper.
506 bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
507 const Value *Ptr, uint64_t Size) {
508 return canInstructionRangeModify(I1, I2, Location(Ptr, Size));
511 //===--------------------------------------------------------------------===//
512 /// Methods that clients should call when they transform the program to allow
513 /// alias analyses to update their internal data structures. Note that these
514 /// methods may be called on any instruction, regardless of whether or not
515 /// they have pointer-analysis implications.
518 /// deleteValue - This method should be called whenever an LLVM Value is
519 /// deleted from the program, for example when an instruction is found to be
520 /// redundant and is eliminated.
522 virtual void deleteValue(Value *V);
524 /// copyValue - This method should be used whenever a preexisting value in the
525 /// program is copied or cloned, introducing a new value. Note that analysis
526 /// implementations should tolerate clients that use this method to introduce
527 /// the same value multiple times: if the analysis already knows about a
528 /// value, it should ignore the request.
530 virtual void copyValue(Value *From, Value *To);
532 /// addEscapingUse - This method should be used whenever an escaping use is
533 /// added to a pointer value. Analysis implementations may either return
534 /// conservative responses for that value in the future, or may recompute
535 /// some or all internal state to continue providing precise responses.
537 /// Escaping uses are considered by anything _except_ the following:
538 /// - GEPs or bitcasts of the pointer
539 /// - Loads through the pointer
540 /// - Stores through (but not of) the pointer
541 virtual void addEscapingUse(Use &U);
543 /// replaceWithNewValue - This method is the obvious combination of the two
544 /// above, and it provided as a helper to simplify client code.
546 void replaceWithNewValue(Value *Old, Value *New) {
552 // Specialize DenseMapInfo for Location.
554 struct DenseMapInfo<AliasAnalysis::Location> {
555 static inline AliasAnalysis::Location getEmptyKey() {
557 AliasAnalysis::Location(DenseMapInfo<const Value *>::getEmptyKey(),
560 static inline AliasAnalysis::Location getTombstoneKey() {
562 AliasAnalysis::Location(DenseMapInfo<const Value *>::getTombstoneKey(),
565 static unsigned getHashValue(const AliasAnalysis::Location &Val) {
566 return DenseMapInfo<const Value *>::getHashValue(Val.Ptr) ^
567 DenseMapInfo<uint64_t>::getHashValue(Val.Size) ^
568 DenseMapInfo<const MDNode *>::getHashValue(Val.TBAATag);
570 static bool isEqual(const AliasAnalysis::Location &LHS,
571 const AliasAnalysis::Location &RHS) {
572 return LHS.Ptr == RHS.Ptr &&
573 LHS.Size == RHS.Size &&
574 LHS.TBAATag == RHS.TBAATag;
578 /// isNoAliasCall - Return true if this pointer is returned by a noalias
580 bool isNoAliasCall(const Value *V);
582 /// isIdentifiedObject - Return true if this pointer refers to a distinct and
583 /// identifiable object. This returns true for:
584 /// Global Variables and Functions (but not Global Aliases)
585 /// Allocas and Mallocs
586 /// ByVal and NoAlias Arguments
589 bool isIdentifiedObject(const Value *V);
591 /// isKnownNonNull - Return true if this pointer couldn't possibly be null by
592 /// its definition. This returns true for allocas, non-extern-weak globals and
594 bool isKnownNonNull(const Value *V);
596 } // End llvm namespace