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_ALIASANALYSIS_H
38 #define LLVM_ANALYSIS_ALIASANALYSIS_H
40 #include "llvm/ADT/DenseMap.h"
41 #include "llvm/IR/CallSite.h"
42 #include "llvm/IR/Metadata.h"
50 class TargetLibraryInfo;
53 class MemTransferInst;
60 const TargetLibraryInfo *TLI;
63 AliasAnalysis *AA; // Previous Alias Analysis to chain to.
66 /// InitializeAliasAnalysis - Subclasses must call this method to initialize
67 /// the AliasAnalysis interface before any other methods are called. This is
68 /// typically called by the run* methods of these subclasses. This may be
69 /// called multiple times.
71 void InitializeAliasAnalysis(Pass *P, const DataLayout *DL);
73 /// getAnalysisUsage - All alias analysis implementations should invoke this
74 /// directly (using AliasAnalysis::getAnalysisUsage(AU)).
75 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
78 static char ID; // Class identification, replacement for typeinfo
79 AliasAnalysis() : DL(nullptr), TLI(nullptr), AA(nullptr) {}
80 virtual ~AliasAnalysis(); // We want to be subclassed
82 /// UnknownSize - This is a special value which can be used with the
83 /// size arguments in alias queries to indicate that the caller does not
84 /// know the sizes of the potential memory references.
85 static uint64_t const UnknownSize = ~UINT64_C(0);
87 /// getTargetLibraryInfo - Return a pointer to the current TargetLibraryInfo
88 /// object, or null if no TargetLibraryInfo object is available.
90 const TargetLibraryInfo *getTargetLibraryInfo() const { return TLI; }
92 /// getTypeStoreSize - Return the DataLayout store size for the given type,
93 /// if known, or a conservative value otherwise.
95 uint64_t getTypeStoreSize(Type *Ty);
97 //===--------------------------------------------------------------------===//
101 /// Location - A description of a memory location.
103 /// Ptr - The address of the start of the location.
105 /// Size - The maximum size of the location, in address-units, or
106 /// UnknownSize if the size is not known. Note that an unknown size does
107 /// not mean the pointer aliases the entire virtual address space, because
108 /// there are restrictions on stepping out of one object and into another.
109 /// See http://llvm.org/docs/LangRef.html#pointeraliasing
111 /// AATags - The metadata nodes which describes the aliasing of the
112 /// location (each member is null if that kind of information is
116 explicit Location(const Value *P = nullptr, uint64_t S = UnknownSize,
117 const AAMDNodes &N = AAMDNodes())
118 : Ptr(P), Size(S), AATags(N) {}
120 Location getWithNewPtr(const Value *NewPtr) const {
121 Location Copy(*this);
126 Location getWithNewSize(uint64_t NewSize) const {
127 Location Copy(*this);
132 Location getWithoutAATags() const {
133 Location Copy(*this);
134 Copy.AATags = AAMDNodes();
139 /// getLocation - Fill in Loc with information about the memory reference by
140 /// the given instruction.
141 Location getLocation(const LoadInst *LI);
142 Location getLocation(const StoreInst *SI);
143 Location getLocation(const VAArgInst *VI);
144 Location getLocation(const AtomicCmpXchgInst *CXI);
145 Location getLocation(const AtomicRMWInst *RMWI);
146 static Location getLocationForSource(const MemTransferInst *MTI);
147 static Location getLocationForDest(const MemIntrinsic *MI);
149 /// Alias analysis result - Either we know for sure that it does not alias, we
150 /// know for sure it must alias, or we don't know anything: The two pointers
151 /// _might_ alias. This enum is designed so you can do things like:
152 /// if (AA.alias(P1, P2)) { ... }
153 /// to check to see if two pointers might alias.
155 /// See docs/AliasAnalysis.html for more information on the specific meanings
159 NoAlias = 0, ///< No dependencies.
160 MayAlias, ///< Anything goes.
161 PartialAlias, ///< Pointers differ, but pointees overlap.
162 MustAlias ///< Pointers are equal.
165 /// alias - The main low level interface to the alias analysis implementation.
166 /// Returns an AliasResult indicating whether the two pointers are aliased to
167 /// each other. This is the interface that must be implemented by specific
168 /// alias analysis implementations.
169 virtual AliasResult alias(const Location &LocA, const Location &LocB);
171 /// alias - A convenience wrapper.
172 AliasResult alias(const Value *V1, uint64_t V1Size,
173 const Value *V2, uint64_t V2Size) {
174 return alias(Location(V1, V1Size), Location(V2, V2Size));
177 /// alias - A convenience wrapper.
178 AliasResult alias(const Value *V1, const Value *V2) {
179 return alias(V1, UnknownSize, V2, UnknownSize);
182 /// isNoAlias - A trivial helper function to check to see if the specified
183 /// pointers are no-alias.
184 bool isNoAlias(const Location &LocA, const Location &LocB) {
185 return alias(LocA, LocB) == NoAlias;
188 /// isNoAlias - A convenience wrapper.
189 bool isNoAlias(const Value *V1, uint64_t V1Size,
190 const Value *V2, uint64_t V2Size) {
191 return isNoAlias(Location(V1, V1Size), Location(V2, V2Size));
194 /// isNoAlias - A convenience wrapper.
195 bool isNoAlias(const Value *V1, const Value *V2) {
196 return isNoAlias(Location(V1), Location(V2));
199 /// isMustAlias - A convenience wrapper.
200 bool isMustAlias(const Location &LocA, const Location &LocB) {
201 return alias(LocA, LocB) == MustAlias;
204 /// isMustAlias - A convenience wrapper.
205 bool isMustAlias(const Value *V1, const Value *V2) {
206 return alias(V1, 1, V2, 1) == MustAlias;
209 /// pointsToConstantMemory - If the specified memory location is
210 /// known to be constant, return true. If OrLocal is true and the
211 /// specified memory location is known to be "local" (derived from
212 /// an alloca), return true. Otherwise return false.
213 virtual bool pointsToConstantMemory(const Location &Loc,
214 bool OrLocal = false);
216 /// pointsToConstantMemory - A convenient wrapper.
217 bool pointsToConstantMemory(const Value *P, bool OrLocal = false) {
218 return pointsToConstantMemory(Location(P), OrLocal);
221 //===--------------------------------------------------------------------===//
222 /// Simple mod/ref information...
225 /// ModRefResult - Represent the result of a mod/ref query. Mod and Ref are
226 /// bits which may be or'd together.
228 enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 };
230 /// These values define additional bits used to define the
231 /// ModRefBehavior values.
232 enum { Nowhere = 0, ArgumentPointees = 4, Anywhere = 8 | ArgumentPointees };
234 /// ModRefBehavior - Summary of how a function affects memory in the program.
235 /// Loads from constant globals are not considered memory accesses for this
236 /// interface. Also, functions may freely modify stack space local to their
237 /// invocation without having to report it through these interfaces.
238 enum ModRefBehavior {
239 /// DoesNotAccessMemory - This function does not perform any non-local loads
240 /// or stores to memory.
242 /// This property corresponds to the GCC 'const' attribute.
243 /// This property corresponds to the LLVM IR 'readnone' attribute.
244 /// This property corresponds to the IntrNoMem LLVM intrinsic flag.
245 DoesNotAccessMemory = Nowhere | NoModRef,
247 /// OnlyReadsArgumentPointees - The only memory references in this function
248 /// (if it has any) are non-volatile loads from objects pointed to by its
249 /// pointer-typed arguments, with arbitrary offsets.
251 /// This property corresponds to the IntrReadArgMem LLVM intrinsic flag.
252 OnlyReadsArgumentPointees = ArgumentPointees | Ref,
254 /// OnlyAccessesArgumentPointees - The only memory references in this
255 /// function (if it has any) are non-volatile loads and stores from objects
256 /// pointed to by its pointer-typed arguments, with arbitrary offsets.
258 /// This property corresponds to the IntrReadWriteArgMem LLVM intrinsic flag.
259 OnlyAccessesArgumentPointees = ArgumentPointees | ModRef,
261 /// OnlyReadsMemory - This function does not perform any non-local stores or
262 /// volatile loads, but may read from any memory location.
264 /// This property corresponds to the GCC 'pure' attribute.
265 /// This property corresponds to the LLVM IR 'readonly' attribute.
266 /// This property corresponds to the IntrReadMem LLVM intrinsic flag.
267 OnlyReadsMemory = Anywhere | Ref,
269 /// UnknownModRefBehavior - This indicates that the function could not be
270 /// classified into one of the behaviors above.
271 UnknownModRefBehavior = Anywhere | ModRef
274 /// Get the location associated with a pointer argument of a callsite.
275 /// The mask bits are set to indicate the allowed aliasing ModRef kinds.
276 /// Note that these mask bits do not necessarily account for the overall
277 /// behavior of the function, but rather only provide additional
278 /// per-argument information.
279 virtual Location getArgLocation(ImmutableCallSite CS, unsigned ArgIdx,
282 /// getModRefBehavior - Return the behavior when calling the given call site.
283 virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS);
285 /// getModRefBehavior - Return the behavior when calling the given function.
286 /// For use when the call site is not known.
287 virtual ModRefBehavior getModRefBehavior(const Function *F);
289 /// doesNotAccessMemory - If the specified call is known to never read or
290 /// write memory, return true. If the call only reads from known-constant
291 /// memory, it is also legal to return true. Calls that unwind the stack
292 /// are legal for this predicate.
294 /// Many optimizations (such as CSE and LICM) can be performed on such calls
295 /// without worrying about aliasing properties, and many calls have this
296 /// property (e.g. calls to 'sin' and 'cos').
298 /// This property corresponds to the GCC 'const' attribute.
300 bool doesNotAccessMemory(ImmutableCallSite CS) {
301 return getModRefBehavior(CS) == DoesNotAccessMemory;
304 /// doesNotAccessMemory - If the specified function is known to never read or
305 /// write memory, return true. For use when the call site is not known.
307 bool doesNotAccessMemory(const Function *F) {
308 return getModRefBehavior(F) == DoesNotAccessMemory;
311 /// onlyReadsMemory - If the specified call is known to only read from
312 /// non-volatile memory (or not access memory at all), return true. Calls
313 /// that unwind the stack are legal for this predicate.
315 /// This property allows many common optimizations to be performed in the
316 /// absence of interfering store instructions, such as CSE of strlen calls.
318 /// This property corresponds to the GCC 'pure' attribute.
320 bool onlyReadsMemory(ImmutableCallSite CS) {
321 return onlyReadsMemory(getModRefBehavior(CS));
324 /// onlyReadsMemory - If the specified function is known to only read from
325 /// non-volatile memory (or not access memory at all), return true. For use
326 /// when the call site is not known.
328 bool onlyReadsMemory(const Function *F) {
329 return onlyReadsMemory(getModRefBehavior(F));
332 /// onlyReadsMemory - Return true if functions with the specified behavior are
333 /// known to only read from non-volatile memory (or not access memory at all).
335 static bool onlyReadsMemory(ModRefBehavior MRB) {
339 /// onlyAccessesArgPointees - Return true if functions with the specified
340 /// behavior are known to read and write at most from objects pointed to by
341 /// their pointer-typed arguments (with arbitrary offsets).
343 static bool onlyAccessesArgPointees(ModRefBehavior MRB) {
344 return !(MRB & Anywhere & ~ArgumentPointees);
347 /// doesAccessArgPointees - Return true if functions with the specified
348 /// behavior are known to potentially read or write from objects pointed
349 /// to be their pointer-typed arguments (with arbitrary offsets).
351 static bool doesAccessArgPointees(ModRefBehavior MRB) {
352 return (MRB & ModRef) && (MRB & ArgumentPointees);
355 /// getModRefInfo - Return information about whether or not an
356 /// instruction may read or write memory (without regard to a
357 /// specific location)
358 ModRefResult getModRefInfo(const Instruction *I) {
359 if (auto CS = ImmutableCallSite(I)) {
360 auto MRB = getModRefBehavior(CS);
370 return getModRefInfo(I, Location());
373 /// getModRefInfo - Return information about whether or not an instruction may
374 /// read or write the specified memory location. An instruction
375 /// that doesn't read or write memory may be trivially LICM'd for example.
376 ModRefResult getModRefInfo(const Instruction *I,
377 const Location &Loc) {
378 switch (I->getOpcode()) {
379 case Instruction::VAArg: return getModRefInfo((const VAArgInst*)I, Loc);
380 case Instruction::Load: return getModRefInfo((const LoadInst*)I, Loc);
381 case Instruction::Store: return getModRefInfo((const StoreInst*)I, Loc);
382 case Instruction::Fence: return getModRefInfo((const FenceInst*)I, Loc);
383 case Instruction::AtomicCmpXchg:
384 return getModRefInfo((const AtomicCmpXchgInst*)I, Loc);
385 case Instruction::AtomicRMW:
386 return getModRefInfo((const AtomicRMWInst*)I, Loc);
387 case Instruction::Call: return getModRefInfo((const CallInst*)I, Loc);
388 case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,Loc);
389 default: return NoModRef;
393 /// getModRefInfo - A convenience wrapper.
394 ModRefResult getModRefInfo(const Instruction *I,
395 const Value *P, uint64_t Size) {
396 return getModRefInfo(I, Location(P, Size));
399 /// getModRefInfo (for call sites) - Return information about whether
400 /// a particular call site modifies or reads the specified memory location.
401 virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
402 const Location &Loc);
404 /// getModRefInfo (for call sites) - A convenience wrapper.
405 ModRefResult getModRefInfo(ImmutableCallSite CS,
406 const Value *P, uint64_t Size) {
407 return getModRefInfo(CS, Location(P, Size));
410 /// getModRefInfo (for calls) - Return information about whether
411 /// a particular call modifies or reads the specified memory location.
412 ModRefResult getModRefInfo(const CallInst *C, const Location &Loc) {
413 return getModRefInfo(ImmutableCallSite(C), Loc);
416 /// getModRefInfo (for calls) - A convenience wrapper.
417 ModRefResult getModRefInfo(const CallInst *C, const Value *P, uint64_t Size) {
418 return getModRefInfo(C, Location(P, Size));
421 /// getModRefInfo (for invokes) - Return information about whether
422 /// a particular invoke modifies or reads the specified memory location.
423 ModRefResult getModRefInfo(const InvokeInst *I,
424 const Location &Loc) {
425 return getModRefInfo(ImmutableCallSite(I), Loc);
428 /// getModRefInfo (for invokes) - A convenience wrapper.
429 ModRefResult getModRefInfo(const InvokeInst *I,
430 const Value *P, uint64_t Size) {
431 return getModRefInfo(I, Location(P, Size));
434 /// getModRefInfo (for loads) - Return information about whether
435 /// a particular load modifies or reads the specified memory location.
436 ModRefResult getModRefInfo(const LoadInst *L, const Location &Loc);
438 /// getModRefInfo (for loads) - A convenience wrapper.
439 ModRefResult getModRefInfo(const LoadInst *L, const Value *P, uint64_t Size) {
440 return getModRefInfo(L, Location(P, Size));
443 /// getModRefInfo (for stores) - Return information about whether
444 /// a particular store modifies or reads the specified memory location.
445 ModRefResult getModRefInfo(const StoreInst *S, const Location &Loc);
447 /// getModRefInfo (for stores) - A convenience wrapper.
448 ModRefResult getModRefInfo(const StoreInst *S, const Value *P, uint64_t Size){
449 return getModRefInfo(S, Location(P, Size));
452 /// getModRefInfo (for fences) - Return information about whether
453 /// a particular store modifies or reads the specified memory location.
454 ModRefResult getModRefInfo(const FenceInst *S, const Location &Loc) {
455 // Conservatively correct. (We could possibly be a bit smarter if
456 // Loc is a alloca that doesn't escape.)
460 /// getModRefInfo (for fences) - A convenience wrapper.
461 ModRefResult getModRefInfo(const FenceInst *S, const Value *P, uint64_t Size){
462 return getModRefInfo(S, Location(P, Size));
465 /// getModRefInfo (for cmpxchges) - Return information about whether
466 /// a particular cmpxchg modifies or reads the specified memory location.
467 ModRefResult getModRefInfo(const AtomicCmpXchgInst *CX, const Location &Loc);
469 /// getModRefInfo (for cmpxchges) - A convenience wrapper.
470 ModRefResult getModRefInfo(const AtomicCmpXchgInst *CX,
471 const Value *P, unsigned Size) {
472 return getModRefInfo(CX, Location(P, Size));
475 /// getModRefInfo (for atomicrmws) - Return information about whether
476 /// a particular atomicrmw modifies or reads the specified memory location.
477 ModRefResult getModRefInfo(const AtomicRMWInst *RMW, const Location &Loc);
479 /// getModRefInfo (for atomicrmws) - A convenience wrapper.
480 ModRefResult getModRefInfo(const AtomicRMWInst *RMW,
481 const Value *P, unsigned Size) {
482 return getModRefInfo(RMW, Location(P, Size));
485 /// getModRefInfo (for va_args) - Return information about whether
486 /// a particular va_arg modifies or reads the specified memory location.
487 ModRefResult getModRefInfo(const VAArgInst* I, const Location &Loc);
489 /// getModRefInfo (for va_args) - A convenience wrapper.
490 ModRefResult getModRefInfo(const VAArgInst* I, const Value* P, uint64_t Size){
491 return getModRefInfo(I, Location(P, Size));
494 /// getModRefInfo - Return information about whether two call sites may refer
495 /// to the same set of memory locations. See
496 /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo
498 virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
499 ImmutableCallSite CS2);
501 /// callCapturesBefore - Return information about whether a particular call
502 /// site modifies or reads the specified memory location.
503 ModRefResult callCapturesBefore(const Instruction *I,
504 const AliasAnalysis::Location &MemLoc,
507 /// callCapturesBefore - A convenience wrapper.
508 ModRefResult callCapturesBefore(const Instruction *I, const Value *P,
509 uint64_t Size, DominatorTree *DT) {
510 return callCapturesBefore(I, Location(P, Size), DT);
513 //===--------------------------------------------------------------------===//
514 /// Higher level methods for querying mod/ref information.
517 /// canBasicBlockModify - Return true if it is possible for execution of the
518 /// specified basic block to modify the location Loc.
519 bool canBasicBlockModify(const BasicBlock &BB, const Location &Loc);
521 /// canBasicBlockModify - A convenience wrapper.
522 bool canBasicBlockModify(const BasicBlock &BB, const Value *P, uint64_t Size){
523 return canBasicBlockModify(BB, Location(P, Size));
526 /// canInstructionRangeModRef - Return true if it is possible for the
527 /// execution of the specified instructions to mod\ref (according to the
528 /// mode) the location Loc. The instructions to consider are all
529 /// of the instructions in the range of [I1,I2] INCLUSIVE.
530 /// I1 and I2 must be in the same basic block.
531 bool canInstructionRangeModRef(const Instruction &I1,
532 const Instruction &I2, const Location &Loc,
533 const ModRefResult Mode);
535 /// canInstructionRangeModRef - A convenience wrapper.
536 bool canInstructionRangeModRef(const Instruction &I1,
537 const Instruction &I2, const Value *Ptr,
538 uint64_t Size, const ModRefResult Mode) {
539 return canInstructionRangeModRef(I1, I2, Location(Ptr, Size), Mode);
542 //===--------------------------------------------------------------------===//
543 /// Methods that clients should call when they transform the program to allow
544 /// alias analyses to update their internal data structures. Note that these
545 /// methods may be called on any instruction, regardless of whether or not
546 /// they have pointer-analysis implications.
549 /// deleteValue - This method should be called whenever an LLVM Value is
550 /// deleted from the program, for example when an instruction is found to be
551 /// redundant and is eliminated.
553 virtual void deleteValue(Value *V);
555 /// copyValue - This method should be used whenever a preexisting value in the
556 /// program is copied or cloned, introducing a new value. Note that analysis
557 /// implementations should tolerate clients that use this method to introduce
558 /// the same value multiple times: if the analysis already knows about a
559 /// value, it should ignore the request.
561 virtual void copyValue(Value *From, Value *To);
563 /// addEscapingUse - This method should be used whenever an escaping use is
564 /// added to a pointer value. Analysis implementations may either return
565 /// conservative responses for that value in the future, or may recompute
566 /// some or all internal state to continue providing precise responses.
568 /// Escaping uses are considered by anything _except_ the following:
569 /// - GEPs or bitcasts of the pointer
570 /// - Loads through the pointer
571 /// - Stores through (but not of) the pointer
572 virtual void addEscapingUse(Use &U);
574 /// replaceWithNewValue - This method is the obvious combination of the two
575 /// above, and it provided as a helper to simplify client code.
577 void replaceWithNewValue(Value *Old, Value *New) {
583 // Specialize DenseMapInfo for Location.
585 struct DenseMapInfo<AliasAnalysis::Location> {
586 static inline AliasAnalysis::Location getEmptyKey() {
587 return AliasAnalysis::Location(DenseMapInfo<const Value *>::getEmptyKey(),
590 static inline AliasAnalysis::Location getTombstoneKey() {
591 return AliasAnalysis::Location(
592 DenseMapInfo<const Value *>::getTombstoneKey(), 0);
594 static unsigned getHashValue(const AliasAnalysis::Location &Val) {
595 return DenseMapInfo<const Value *>::getHashValue(Val.Ptr) ^
596 DenseMapInfo<uint64_t>::getHashValue(Val.Size) ^
597 DenseMapInfo<AAMDNodes>::getHashValue(Val.AATags);
599 static bool isEqual(const AliasAnalysis::Location &LHS,
600 const AliasAnalysis::Location &RHS) {
601 return LHS.Ptr == RHS.Ptr &&
602 LHS.Size == RHS.Size &&
603 LHS.AATags == RHS.AATags;
607 /// isNoAliasCall - Return true if this pointer is returned by a noalias
609 bool isNoAliasCall(const Value *V);
611 /// isNoAliasArgument - Return true if this is an argument with the noalias
613 bool isNoAliasArgument(const Value *V);
615 /// isIdentifiedObject - Return true if this pointer refers to a distinct and
616 /// identifiable object. This returns true for:
617 /// Global Variables and Functions (but not Global Aliases)
619 /// ByVal and NoAlias Arguments
620 /// NoAlias returns (e.g. calls to malloc)
622 bool isIdentifiedObject(const Value *V);
624 /// isIdentifiedFunctionLocal - Return true if V is umabigously identified
625 /// at the function-level. Different IdentifiedFunctionLocals can't alias.
626 /// Further, an IdentifiedFunctionLocal can not alias with any function
627 /// arguments other than itself, which is not necessarily true for
628 /// IdentifiedObjects.
629 bool isIdentifiedFunctionLocal(const Value *V);
631 } // End llvm namespace