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 MemoryLocation 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
22 // MemoryLocation::UnknownSize if the size is not known. The TBAA tag
23 // identifies the "type" of the memory reference; see the
24 // TypeBasedAliasAnalysis class for details.
26 // Some non-obvious details include:
27 // - Pointers that point to two completely different objects in memory never
28 // alias, regardless of the value of the Size component.
29 // - NoAlias doesn't imply inequal pointers. The most obvious example of this
30 // is two pointers to constant memory. Even if they are equal, constant
31 // memory is never stored to, so there will never be any dependencies.
32 // In this and other situations, the pointers may be both NoAlias and
33 // MustAlias at the same time. The current API can only return one result,
34 // though this is rarely a problem in practice.
36 //===----------------------------------------------------------------------===//
38 #ifndef LLVM_ANALYSIS_ALIASANALYSIS_H
39 #define LLVM_ANALYSIS_ALIASANALYSIS_H
41 #include "llvm/ADT/DenseMap.h"
42 #include "llvm/IR/CallSite.h"
43 #include "llvm/IR/Metadata.h"
44 #include "llvm/Analysis/MemoryLocation.h"
52 class TargetLibraryInfo;
55 class MemTransferInst;
58 class OrderedBasicBlock;
60 /// The possible results of an alias query.
62 /// These results are always computed between two MemoryLocation objects as
63 /// a query to some alias analysis.
65 /// Note that these are unscoped enumerations because we would like to support
66 /// implicitly testing a result for the existence of any possible aliasing with
67 /// a conversion to bool, but an "enum class" doesn't support this. The
68 /// canonical names from the literature are suffixed and unique anyways, and so
69 /// they serve as global constants in LLVM for these results.
71 /// See docs/AliasAnalysis.html for more information on the specific meanings
74 /// The two locations do not alias at all.
76 /// This value is arranged to convert to false, while all other values
77 /// convert to true. This allows a boolean context to convert the result to
78 /// a binary flag indicating whether there is the possibility of aliasing.
80 /// The two locations may or may not alias. This is the least precise result.
82 /// The two locations alias, but only due to a partial overlap.
84 /// The two locations precisely alias each other.
88 /// Flags indicating whether a memory access modifies or references memory.
90 /// This is no access at all, a modification, a reference, or both
91 /// a modification and a reference. These are specifically structured such that
92 /// they form a two bit matrix and bit-tests for 'mod' or 'ref' work with any
93 /// of the possible values.
95 /// The access neither references nor modifies the value stored in memory.
97 /// The access references the value stored in memory.
99 /// The access modifies the value stored in memory.
101 /// The access both references and modifies the value stored in memory.
102 MRI_ModRef = MRI_Ref | MRI_Mod
105 /// The locations at which a function might access memory.
107 /// These are primarily used in conjunction with the \c AccessKind bits to
108 /// describe both the nature of access and the locations of access for a
110 enum FunctionModRefLocation {
111 /// Base case is no access to memory.
113 /// Access to memory via argument pointers.
114 FMRL_ArgumentPointees = 4,
115 /// Access to any memory.
116 FMRL_Anywhere = 8 | FMRL_ArgumentPointees
119 /// Summary of how a function affects memory in the program.
121 /// Loads from constant globals are not considered memory accesses for this
122 /// interface. Also, functions may freely modify stack space local to their
123 /// invocation without having to report it through these interfaces.
124 enum FunctionModRefBehavior {
125 /// This function does not perform any non-local loads or stores to memory.
127 /// This property corresponds to the GCC 'const' attribute.
128 /// This property corresponds to the LLVM IR 'readnone' attribute.
129 /// This property corresponds to the IntrNoMem LLVM intrinsic flag.
130 FMRB_DoesNotAccessMemory = FMRL_Nowhere | MRI_NoModRef,
132 /// The only memory references in this function (if it has any) are
133 /// non-volatile loads from objects pointed to by its pointer-typed
134 /// arguments, with arbitrary offsets.
136 /// This property corresponds to the IntrReadArgMem LLVM intrinsic flag.
137 FMRB_OnlyReadsArgumentPointees = FMRL_ArgumentPointees | MRI_Ref,
139 /// The only memory references in this function (if it has any) are
140 /// non-volatile loads and stores from objects pointed to by its
141 /// pointer-typed arguments, with arbitrary offsets.
143 /// This property corresponds to the IntrReadWriteArgMem LLVM intrinsic flag.
144 FMRB_OnlyAccessesArgumentPointees = FMRL_ArgumentPointees | MRI_ModRef,
146 /// This function does not perform any non-local stores or volatile loads,
147 /// but may read from any memory location.
149 /// This property corresponds to the GCC 'pure' attribute.
150 /// This property corresponds to the LLVM IR 'readonly' attribute.
151 /// This property corresponds to the IntrReadMem LLVM intrinsic flag.
152 FMRB_OnlyReadsMemory = FMRL_Anywhere | MRI_Ref,
154 /// This indicates that the function could not be classified into one of the
156 FMRB_UnknownModRefBehavior = FMRL_Anywhere | MRI_ModRef
159 class AliasAnalysis {
161 const DataLayout *DL;
162 const TargetLibraryInfo *TLI;
165 AliasAnalysis *AA; // Previous Alias Analysis to chain to.
168 /// InitializeAliasAnalysis - Subclasses must call this method to initialize
169 /// the AliasAnalysis interface before any other methods are called. This is
170 /// typically called by the run* methods of these subclasses. This may be
171 /// called multiple times.
173 void InitializeAliasAnalysis(Pass *P, const DataLayout *DL);
175 /// getAnalysisUsage - All alias analysis implementations should invoke this
176 /// directly (using AliasAnalysis::getAnalysisUsage(AU)).
177 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
180 static char ID; // Class identification, replacement for typeinfo
181 AliasAnalysis() : DL(nullptr), TLI(nullptr), AA(nullptr) {}
182 virtual ~AliasAnalysis(); // We want to be subclassed
184 /// getTargetLibraryInfo - Return a pointer to the current TargetLibraryInfo
185 /// object, or null if no TargetLibraryInfo object is available.
187 const TargetLibraryInfo *getTargetLibraryInfo() const { return TLI; }
189 /// getTypeStoreSize - Return the DataLayout store size for the given type,
190 /// if known, or a conservative value otherwise.
192 uint64_t getTypeStoreSize(Type *Ty);
194 //===--------------------------------------------------------------------===//
195 /// \name Alias Queries
198 /// The main low level interface to the alias analysis implementation.
199 /// Returns an AliasResult indicating whether the two pointers are aliased to
200 /// each other. This is the interface that must be implemented by specific
201 /// alias analysis implementations.
202 virtual AliasResult alias(const MemoryLocation &LocA,
203 const MemoryLocation &LocB);
205 /// A convenience wrapper around the primary \c alias interface.
206 AliasResult alias(const Value *V1, uint64_t V1Size, const Value *V2,
208 return alias(MemoryLocation(V1, V1Size), MemoryLocation(V2, V2Size));
211 /// A convenience wrapper around the primary \c alias interface.
212 AliasResult alias(const Value *V1, const Value *V2) {
213 return alias(V1, MemoryLocation::UnknownSize, V2,
214 MemoryLocation::UnknownSize);
217 /// A trivial helper function to check to see if the specified pointers are
219 bool isNoAlias(const MemoryLocation &LocA, const MemoryLocation &LocB) {
220 return alias(LocA, LocB) == NoAlias;
223 /// A convenience wrapper around the \c isNoAlias helper interface.
224 bool isNoAlias(const Value *V1, uint64_t V1Size, const Value *V2,
226 return isNoAlias(MemoryLocation(V1, V1Size), MemoryLocation(V2, V2Size));
229 /// A convenience wrapper around the \c isNoAlias helper interface.
230 bool isNoAlias(const Value *V1, const Value *V2) {
231 return isNoAlias(MemoryLocation(V1), MemoryLocation(V2));
234 /// A trivial helper function to check to see if the specified pointers are
236 bool isMustAlias(const MemoryLocation &LocA, const MemoryLocation &LocB) {
237 return alias(LocA, LocB) == MustAlias;
240 /// A convenience wrapper around the \c isMustAlias helper interface.
241 bool isMustAlias(const Value *V1, const Value *V2) {
242 return alias(V1, 1, V2, 1) == MustAlias;
245 /// Checks whether the given location points to constant memory, or if
246 /// \p OrLocal is true whether it points to a local alloca.
247 virtual bool pointsToConstantMemory(const MemoryLocation &Loc,
248 bool OrLocal = false);
250 /// A convenience wrapper around the primary \c pointsToConstantMemory
252 bool pointsToConstantMemory(const Value *P, bool OrLocal = false) {
253 return pointsToConstantMemory(MemoryLocation(P), OrLocal);
257 //===--------------------------------------------------------------------===//
258 /// \name Simple mod/ref information
261 /// Get the ModRef info associated with a pointer argument of a callsite. The
262 /// result's bits are set to indicate the allowed aliasing ModRef kinds. Note
263 /// that these bits do not necessarily account for the overall behavior of
264 /// the function, but rather only provide additional per-argument
266 virtual ModRefInfo getArgModRefInfo(ImmutableCallSite CS, unsigned ArgIdx);
268 /// Return the behavior of the given call site.
269 virtual FunctionModRefBehavior getModRefBehavior(ImmutableCallSite CS);
271 /// Return the behavior when calling the given function.
272 virtual FunctionModRefBehavior getModRefBehavior(const Function *F);
274 /// Checks if the specified call is known to never read or write memory.
276 /// Note that if the call only reads from known-constant memory, it is also
277 /// legal to return true. Also, calls that unwind the stack are legal for
280 /// Many optimizations (such as CSE and LICM) can be performed on such calls
281 /// without worrying about aliasing properties, and many calls have this
282 /// property (e.g. calls to 'sin' and 'cos').
284 /// This property corresponds to the GCC 'const' attribute.
285 bool doesNotAccessMemory(ImmutableCallSite CS) {
286 return getModRefBehavior(CS) == FMRB_DoesNotAccessMemory;
289 /// Checks if the specified function is known to never read or write memory.
291 /// Note that if the function only reads from known-constant memory, it is
292 /// also legal to return true. Also, function that unwind the stack are legal
293 /// for this predicate.
295 /// Many optimizations (such as CSE and LICM) can be performed on such calls
296 /// to such functions without worrying about aliasing properties, and many
297 /// functions have this property (e.g. 'sin' and 'cos').
299 /// This property corresponds to the GCC 'const' attribute.
300 bool doesNotAccessMemory(const Function *F) {
301 return getModRefBehavior(F) == FMRB_DoesNotAccessMemory;
304 /// Checks if the specified call is known to only read from non-volatile
305 /// memory (or not access memory at all).
307 /// Calls that unwind the stack are legal for this predicate.
309 /// This property allows many common optimizations to be performed in the
310 /// absence of interfering store instructions, such as CSE of strlen calls.
312 /// This property corresponds to the GCC 'pure' attribute.
313 bool onlyReadsMemory(ImmutableCallSite CS) {
314 return onlyReadsMemory(getModRefBehavior(CS));
317 /// Checks if the specified function is known to only read from non-volatile
318 /// memory (or not access memory at all).
320 /// Functions that unwind the stack are legal for this predicate.
322 /// This property allows many common optimizations to be performed in the
323 /// absence of interfering store instructions, such as CSE of strlen calls.
325 /// This property corresponds to the GCC 'pure' attribute.
326 bool onlyReadsMemory(const Function *F) {
327 return onlyReadsMemory(getModRefBehavior(F));
330 /// Checks if functions with the specified behavior are known to only read
331 /// from non-volatile memory (or not access memory at all).
332 static bool onlyReadsMemory(FunctionModRefBehavior MRB) {
333 return !(MRB & MRI_Mod);
336 /// Checks if functions with the specified behavior are known to read and
337 /// write at most from objects pointed to by their pointer-typed arguments
338 /// (with arbitrary offsets).
339 static bool onlyAccessesArgPointees(FunctionModRefBehavior MRB) {
340 return !(MRB & FMRL_Anywhere & ~FMRL_ArgumentPointees);
343 /// Checks if functions with the specified behavior are known to potentially
344 /// read or write from objects pointed to be their pointer-typed arguments
345 /// (with arbitrary offsets).
346 static bool doesAccessArgPointees(FunctionModRefBehavior MRB) {
347 return (MRB & MRI_ModRef) && (MRB & FMRL_ArgumentPointees);
350 /// getModRefInfo (for call sites) - Return information about whether
351 /// a particular call site modifies or reads the specified memory location.
352 virtual ModRefInfo getModRefInfo(ImmutableCallSite CS,
353 const MemoryLocation &Loc);
355 /// getModRefInfo (for call sites) - A convenience wrapper.
356 ModRefInfo getModRefInfo(ImmutableCallSite CS, const Value *P,
358 return getModRefInfo(CS, MemoryLocation(P, Size));
361 /// getModRefInfo (for calls) - Return information about whether
362 /// a particular call modifies or reads the specified memory location.
363 ModRefInfo getModRefInfo(const CallInst *C, const MemoryLocation &Loc) {
364 return getModRefInfo(ImmutableCallSite(C), Loc);
367 /// getModRefInfo (for calls) - A convenience wrapper.
368 ModRefInfo getModRefInfo(const CallInst *C, const Value *P, uint64_t Size) {
369 return getModRefInfo(C, MemoryLocation(P, Size));
372 /// getModRefInfo (for invokes) - Return information about whether
373 /// a particular invoke modifies or reads the specified memory location.
374 ModRefInfo getModRefInfo(const InvokeInst *I, const MemoryLocation &Loc) {
375 return getModRefInfo(ImmutableCallSite(I), Loc);
378 /// getModRefInfo (for invokes) - A convenience wrapper.
379 ModRefInfo getModRefInfo(const InvokeInst *I, const Value *P, uint64_t Size) {
380 return getModRefInfo(I, MemoryLocation(P, Size));
383 /// getModRefInfo (for loads) - Return information about whether
384 /// a particular load modifies or reads the specified memory location.
385 ModRefInfo getModRefInfo(const LoadInst *L, const MemoryLocation &Loc);
387 /// getModRefInfo (for loads) - A convenience wrapper.
388 ModRefInfo getModRefInfo(const LoadInst *L, const Value *P, uint64_t Size) {
389 return getModRefInfo(L, MemoryLocation(P, Size));
392 /// getModRefInfo (for stores) - Return information about whether
393 /// a particular store modifies or reads the specified memory location.
394 ModRefInfo getModRefInfo(const StoreInst *S, const MemoryLocation &Loc);
396 /// getModRefInfo (for stores) - A convenience wrapper.
397 ModRefInfo getModRefInfo(const StoreInst *S, const Value *P, uint64_t Size) {
398 return getModRefInfo(S, MemoryLocation(P, Size));
401 /// getModRefInfo (for fences) - Return information about whether
402 /// a particular store modifies or reads the specified memory location.
403 ModRefInfo getModRefInfo(const FenceInst *S, const MemoryLocation &Loc) {
404 // Conservatively correct. (We could possibly be a bit smarter if
405 // Loc is a alloca that doesn't escape.)
409 /// getModRefInfo (for fences) - A convenience wrapper.
410 ModRefInfo getModRefInfo(const FenceInst *S, const Value *P, uint64_t Size) {
411 return getModRefInfo(S, MemoryLocation(P, Size));
414 /// getModRefInfo (for cmpxchges) - Return information about whether
415 /// a particular cmpxchg modifies or reads the specified memory location.
416 ModRefInfo getModRefInfo(const AtomicCmpXchgInst *CX,
417 const MemoryLocation &Loc);
419 /// getModRefInfo (for cmpxchges) - A convenience wrapper.
420 ModRefInfo getModRefInfo(const AtomicCmpXchgInst *CX, const Value *P,
422 return getModRefInfo(CX, MemoryLocation(P, Size));
425 /// getModRefInfo (for atomicrmws) - Return information about whether
426 /// a particular atomicrmw modifies or reads the specified memory location.
427 ModRefInfo getModRefInfo(const AtomicRMWInst *RMW, const MemoryLocation &Loc);
429 /// getModRefInfo (for atomicrmws) - A convenience wrapper.
430 ModRefInfo getModRefInfo(const AtomicRMWInst *RMW, const Value *P,
432 return getModRefInfo(RMW, MemoryLocation(P, Size));
435 /// getModRefInfo (for va_args) - Return information about whether
436 /// a particular va_arg modifies or reads the specified memory location.
437 ModRefInfo getModRefInfo(const VAArgInst *I, const MemoryLocation &Loc);
439 /// getModRefInfo (for va_args) - A convenience wrapper.
440 ModRefInfo getModRefInfo(const VAArgInst *I, const Value *P, uint64_t Size) {
441 return getModRefInfo(I, MemoryLocation(P, Size));
444 /// Check whether or not an instruction may read or write memory (without
445 /// regard to a specific location).
447 /// For function calls, this delegates to the alias-analysis specific
448 /// call-site mod-ref behavior queries. Otherwise it delegates to the generic
449 /// mod ref information query without a location.
450 ModRefInfo getModRefInfo(const Instruction *I) {
451 if (auto CS = ImmutableCallSite(I)) {
452 auto MRB = getModRefBehavior(CS);
453 if (MRB & MRI_ModRef)
455 else if (MRB & MRI_Ref)
457 else if (MRB & MRI_Mod)
462 return getModRefInfo(I, MemoryLocation());
465 /// Check whether or not an instruction may read or write the specified
468 /// An instruction that doesn't read or write memory may be trivially LICM'd
471 /// This primarily delegates to specific helpers above.
472 ModRefInfo getModRefInfo(const Instruction *I, const MemoryLocation &Loc) {
473 switch (I->getOpcode()) {
474 case Instruction::VAArg: return getModRefInfo((const VAArgInst*)I, Loc);
475 case Instruction::Load: return getModRefInfo((const LoadInst*)I, Loc);
476 case Instruction::Store: return getModRefInfo((const StoreInst*)I, Loc);
477 case Instruction::Fence: return getModRefInfo((const FenceInst*)I, Loc);
478 case Instruction::AtomicCmpXchg:
479 return getModRefInfo((const AtomicCmpXchgInst*)I, Loc);
480 case Instruction::AtomicRMW:
481 return getModRefInfo((const AtomicRMWInst*)I, Loc);
482 case Instruction::Call: return getModRefInfo((const CallInst*)I, Loc);
483 case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,Loc);
489 /// A convenience wrapper for constructing the memory location.
490 ModRefInfo getModRefInfo(const Instruction *I, const Value *P,
492 return getModRefInfo(I, MemoryLocation(P, Size));
495 /// Return information about whether a call and an instruction may refer to
496 /// the same memory locations.
497 ModRefInfo getModRefInfo(Instruction *I, ImmutableCallSite Call);
499 /// Return information about whether two call sites may refer to the same set
500 /// of memory locations. See the AA documentation for details:
501 /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo
502 virtual ModRefInfo getModRefInfo(ImmutableCallSite CS1,
503 ImmutableCallSite CS2);
505 /// \brief Return information about whether a particular call site modifies
506 /// or reads the specified memory location \p MemLoc before instruction \p I
507 /// in a BasicBlock. A ordered basic block \p OBB can be used to speed up
508 /// instruction ordering queries inside the BasicBlock containing \p I.
509 ModRefInfo callCapturesBefore(const Instruction *I,
510 const MemoryLocation &MemLoc, DominatorTree *DT,
511 OrderedBasicBlock *OBB = nullptr);
513 /// \brief A convenience wrapper to synthesize a memory location.
514 ModRefInfo callCapturesBefore(const Instruction *I, const Value *P,
515 uint64_t Size, DominatorTree *DT,
516 OrderedBasicBlock *OBB = nullptr) {
517 return callCapturesBefore(I, MemoryLocation(P, Size), DT, OBB);
521 //===--------------------------------------------------------------------===//
522 /// \name Higher level methods for querying mod/ref information.
525 /// Check if it is possible for execution of the specified basic block to
526 /// modify the location Loc.
527 bool canBasicBlockModify(const BasicBlock &BB, const MemoryLocation &Loc);
529 /// A convenience wrapper synthesizing a memory location.
530 bool canBasicBlockModify(const BasicBlock &BB, const Value *P,
532 return canBasicBlockModify(BB, MemoryLocation(P, Size));
535 /// Check if it is possible for the execution of the specified instructions
536 /// to mod\ref (according to the mode) the location Loc.
538 /// The instructions to consider are all of the instructions in the range of
539 /// [I1,I2] INCLUSIVE. I1 and I2 must be in the same basic block.
540 bool canInstructionRangeModRef(const Instruction &I1, const Instruction &I2,
541 const MemoryLocation &Loc,
542 const ModRefInfo Mode);
544 /// A convenience wrapper synthesizing a memory location.
545 bool canInstructionRangeModRef(const Instruction &I1, const Instruction &I2,
546 const Value *Ptr, uint64_t Size,
547 const ModRefInfo Mode) {
548 return canInstructionRangeModRef(I1, I2, MemoryLocation(Ptr, Size), Mode);
552 /// isNoAliasCall - Return true if this pointer is returned by a noalias
554 bool isNoAliasCall(const Value *V);
556 /// isNoAliasArgument - Return true if this is an argument with the noalias
558 bool isNoAliasArgument(const Value *V);
560 /// isIdentifiedObject - Return true if this pointer refers to a distinct and
561 /// identifiable object. This returns true for:
562 /// Global Variables and Functions (but not Global Aliases)
564 /// ByVal and NoAlias Arguments
565 /// NoAlias returns (e.g. calls to malloc)
567 bool isIdentifiedObject(const Value *V);
569 /// isIdentifiedFunctionLocal - Return true if V is umabigously identified
570 /// at the function-level. Different IdentifiedFunctionLocals can't alias.
571 /// Further, an IdentifiedFunctionLocal can not alias with any function
572 /// arguments other than itself, which is not necessarily true for
573 /// IdentifiedObjects.
574 bool isIdentifiedFunctionLocal(const Value *V);
576 } // End llvm namespace