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"
43 #include "llvm/Analysis/MemoryLocation.h"
51 class TargetLibraryInfo;
54 class MemTransferInst;
61 const TargetLibraryInfo *TLI;
64 AliasAnalysis *AA; // Previous Alias Analysis to chain to.
67 /// InitializeAliasAnalysis - Subclasses must call this method to initialize
68 /// the AliasAnalysis interface before any other methods are called. This is
69 /// typically called by the run* methods of these subclasses. This may be
70 /// called multiple times.
72 void InitializeAliasAnalysis(Pass *P, const DataLayout *DL);
74 /// getAnalysisUsage - All alias analysis implementations should invoke this
75 /// directly (using AliasAnalysis::getAnalysisUsage(AU)).
76 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
79 static char ID; // Class identification, replacement for typeinfo
80 AliasAnalysis() : DL(nullptr), TLI(nullptr), AA(nullptr) {}
81 virtual ~AliasAnalysis(); // We want to be subclassed
83 /// UnknownSize - This is a special value which can be used with the
84 /// size arguments in alias queries to indicate that the caller does not
85 /// know the sizes of the potential memory references.
86 static uint64_t const UnknownSize = MemoryLocation::UnknownSize;
88 /// getTargetLibraryInfo - Return a pointer to the current TargetLibraryInfo
89 /// object, or null if no TargetLibraryInfo object is available.
91 const TargetLibraryInfo *getTargetLibraryInfo() const { return TLI; }
93 /// getTypeStoreSize - Return the DataLayout store size for the given type,
94 /// if known, or a conservative value otherwise.
96 uint64_t getTypeStoreSize(Type *Ty);
98 //===--------------------------------------------------------------------===//
102 /// Legacy typedef for the AA location object. New code should use \c
103 /// MemoryLocation directly.
104 typedef MemoryLocation Location;
106 /// Alias analysis result - Either we know for sure that it does not alias, we
107 /// know for sure it must alias, or we don't know anything: The two pointers
108 /// _might_ alias. This enum is designed so you can do things like:
109 /// if (AA.alias(P1, P2)) { ... }
110 /// to check to see if two pointers might alias.
112 /// See docs/AliasAnalysis.html for more information on the specific meanings
116 NoAlias = 0, ///< No dependencies.
117 MayAlias, ///< Anything goes.
118 PartialAlias, ///< Pointers differ, but pointees overlap.
119 MustAlias ///< Pointers are equal.
122 /// alias - The main low level interface to the alias analysis implementation.
123 /// Returns an AliasResult indicating whether the two pointers are aliased to
124 /// each other. This is the interface that must be implemented by specific
125 /// alias analysis implementations.
126 virtual AliasResult alias(const Location &LocA, const Location &LocB);
128 /// alias - A convenience wrapper.
129 AliasResult alias(const Value *V1, uint64_t V1Size,
130 const Value *V2, uint64_t V2Size) {
131 return alias(Location(V1, V1Size), Location(V2, V2Size));
134 /// alias - A convenience wrapper.
135 AliasResult alias(const Value *V1, const Value *V2) {
136 return alias(V1, UnknownSize, V2, UnknownSize);
139 /// isNoAlias - A trivial helper function to check to see if the specified
140 /// pointers are no-alias.
141 bool isNoAlias(const Location &LocA, const Location &LocB) {
142 return alias(LocA, LocB) == NoAlias;
145 /// isNoAlias - A convenience wrapper.
146 bool isNoAlias(const Value *V1, uint64_t V1Size,
147 const Value *V2, uint64_t V2Size) {
148 return isNoAlias(Location(V1, V1Size), Location(V2, V2Size));
151 /// isNoAlias - A convenience wrapper.
152 bool isNoAlias(const Value *V1, const Value *V2) {
153 return isNoAlias(Location(V1), Location(V2));
156 /// isMustAlias - A convenience wrapper.
157 bool isMustAlias(const Location &LocA, const Location &LocB) {
158 return alias(LocA, LocB) == MustAlias;
161 /// isMustAlias - A convenience wrapper.
162 bool isMustAlias(const Value *V1, const Value *V2) {
163 return alias(V1, 1, V2, 1) == MustAlias;
166 /// pointsToConstantMemory - If the specified memory location is
167 /// known to be constant, return true. If OrLocal is true and the
168 /// specified memory location is known to be "local" (derived from
169 /// an alloca), return true. Otherwise return false.
170 virtual bool pointsToConstantMemory(const Location &Loc,
171 bool OrLocal = false);
173 /// pointsToConstantMemory - A convenient wrapper.
174 bool pointsToConstantMemory(const Value *P, bool OrLocal = false) {
175 return pointsToConstantMemory(Location(P), OrLocal);
178 //===--------------------------------------------------------------------===//
179 /// Simple mod/ref information...
182 /// ModRefResult - Represent the result of a mod/ref query. Mod and Ref are
183 /// bits which may be or'd together.
185 enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 };
187 /// These values define additional bits used to define the
188 /// ModRefBehavior values.
189 enum { Nowhere = 0, ArgumentPointees = 4, Anywhere = 8 | ArgumentPointees };
191 /// ModRefBehavior - Summary of how a function affects memory in the program.
192 /// Loads from constant globals are not considered memory accesses for this
193 /// interface. Also, functions may freely modify stack space local to their
194 /// invocation without having to report it through these interfaces.
195 enum ModRefBehavior {
196 /// DoesNotAccessMemory - This function does not perform any non-local loads
197 /// or stores to memory.
199 /// This property corresponds to the GCC 'const' attribute.
200 /// This property corresponds to the LLVM IR 'readnone' attribute.
201 /// This property corresponds to the IntrNoMem LLVM intrinsic flag.
202 DoesNotAccessMemory = Nowhere | NoModRef,
204 /// OnlyReadsArgumentPointees - The only memory references in this function
205 /// (if it has any) are non-volatile loads from objects pointed to by its
206 /// pointer-typed arguments, with arbitrary offsets.
208 /// This property corresponds to the IntrReadArgMem LLVM intrinsic flag.
209 OnlyReadsArgumentPointees = ArgumentPointees | Ref,
211 /// OnlyAccessesArgumentPointees - The only memory references in this
212 /// function (if it has any) are non-volatile loads and stores from objects
213 /// pointed to by its pointer-typed arguments, with arbitrary offsets.
215 /// This property corresponds to the IntrReadWriteArgMem LLVM intrinsic flag.
216 OnlyAccessesArgumentPointees = ArgumentPointees | ModRef,
218 /// OnlyReadsMemory - This function does not perform any non-local stores or
219 /// volatile loads, but may read from any memory location.
221 /// This property corresponds to the GCC 'pure' attribute.
222 /// This property corresponds to the LLVM IR 'readonly' attribute.
223 /// This property corresponds to the IntrReadMem LLVM intrinsic flag.
224 OnlyReadsMemory = Anywhere | Ref,
226 /// UnknownModRefBehavior - This indicates that the function could not be
227 /// classified into one of the behaviors above.
228 UnknownModRefBehavior = Anywhere | ModRef
231 /// Get the ModRef info associated with a pointer argument of a callsite. The
232 /// result's bits are set to indicate the allowed aliasing ModRef kinds. Note
233 /// that these bits do not necessarily account for the overall behavior of
234 /// the function, but rather only provide additional per-argument
236 virtual ModRefResult getArgModRefInfo(ImmutableCallSite CS, unsigned ArgIdx);
238 /// getModRefBehavior - Return the behavior when calling the given call site.
239 virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS);
241 /// getModRefBehavior - Return the behavior when calling the given function.
242 /// For use when the call site is not known.
243 virtual ModRefBehavior getModRefBehavior(const Function *F);
245 /// doesNotAccessMemory - If the specified call is known to never read or
246 /// write memory, return true. If the call only reads from known-constant
247 /// memory, it is also legal to return true. Calls that unwind the stack
248 /// are legal for this predicate.
250 /// Many optimizations (such as CSE and LICM) can be performed on such calls
251 /// without worrying about aliasing properties, and many calls have this
252 /// property (e.g. calls to 'sin' and 'cos').
254 /// This property corresponds to the GCC 'const' attribute.
256 bool doesNotAccessMemory(ImmutableCallSite CS) {
257 return getModRefBehavior(CS) == DoesNotAccessMemory;
260 /// doesNotAccessMemory - If the specified function is known to never read or
261 /// write memory, return true. For use when the call site is not known.
263 bool doesNotAccessMemory(const Function *F) {
264 return getModRefBehavior(F) == DoesNotAccessMemory;
267 /// onlyReadsMemory - If the specified call is known to only read from
268 /// non-volatile memory (or not access memory at all), return true. Calls
269 /// that unwind the stack are legal for this predicate.
271 /// This property allows many common optimizations to be performed in the
272 /// absence of interfering store instructions, such as CSE of strlen calls.
274 /// This property corresponds to the GCC 'pure' attribute.
276 bool onlyReadsMemory(ImmutableCallSite CS) {
277 return onlyReadsMemory(getModRefBehavior(CS));
280 /// onlyReadsMemory - If the specified function is known to only read from
281 /// non-volatile memory (or not access memory at all), return true. For use
282 /// when the call site is not known.
284 bool onlyReadsMemory(const Function *F) {
285 return onlyReadsMemory(getModRefBehavior(F));
288 /// onlyReadsMemory - Return true if functions with the specified behavior are
289 /// known to only read from non-volatile memory (or not access memory at all).
291 static bool onlyReadsMemory(ModRefBehavior MRB) {
295 /// onlyAccessesArgPointees - Return true if functions with the specified
296 /// behavior are known to read and write at most from objects pointed to by
297 /// their pointer-typed arguments (with arbitrary offsets).
299 static bool onlyAccessesArgPointees(ModRefBehavior MRB) {
300 return !(MRB & Anywhere & ~ArgumentPointees);
303 /// doesAccessArgPointees - Return true if functions with the specified
304 /// behavior are known to potentially read or write from objects pointed
305 /// to be their pointer-typed arguments (with arbitrary offsets).
307 static bool doesAccessArgPointees(ModRefBehavior MRB) {
308 return (MRB & ModRef) && (MRB & ArgumentPointees);
311 /// getModRefInfo - Return information about whether or not an
312 /// instruction may read or write memory (without regard to a
313 /// specific location)
314 ModRefResult getModRefInfo(const Instruction *I) {
315 if (auto CS = ImmutableCallSite(I)) {
316 auto MRB = getModRefBehavior(CS);
326 return getModRefInfo(I, Location());
329 /// getModRefInfo - Return information about whether or not an instruction may
330 /// read or write the specified memory location. An instruction
331 /// that doesn't read or write memory may be trivially LICM'd for example.
332 ModRefResult getModRefInfo(const Instruction *I,
333 const Location &Loc) {
334 switch (I->getOpcode()) {
335 case Instruction::VAArg: return getModRefInfo((const VAArgInst*)I, Loc);
336 case Instruction::Load: return getModRefInfo((const LoadInst*)I, Loc);
337 case Instruction::Store: return getModRefInfo((const StoreInst*)I, Loc);
338 case Instruction::Fence: return getModRefInfo((const FenceInst*)I, Loc);
339 case Instruction::AtomicCmpXchg:
340 return getModRefInfo((const AtomicCmpXchgInst*)I, Loc);
341 case Instruction::AtomicRMW:
342 return getModRefInfo((const AtomicRMWInst*)I, Loc);
343 case Instruction::Call: return getModRefInfo((const CallInst*)I, Loc);
344 case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,Loc);
345 default: return NoModRef;
349 /// getModRefInfo - A convenience wrapper.
350 ModRefResult getModRefInfo(const Instruction *I,
351 const Value *P, uint64_t Size) {
352 return getModRefInfo(I, Location(P, Size));
355 /// getModRefInfo (for call sites) - Return information about whether
356 /// a particular call site modifies or reads the specified memory location.
357 virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
358 const Location &Loc);
360 /// getModRefInfo (for call sites) - A convenience wrapper.
361 ModRefResult getModRefInfo(ImmutableCallSite CS,
362 const Value *P, uint64_t Size) {
363 return getModRefInfo(CS, Location(P, Size));
366 /// getModRefInfo (for calls) - Return information about whether
367 /// a particular call modifies or reads the specified memory location.
368 ModRefResult getModRefInfo(const CallInst *C, const Location &Loc) {
369 return getModRefInfo(ImmutableCallSite(C), Loc);
372 /// getModRefInfo (for calls) - A convenience wrapper.
373 ModRefResult getModRefInfo(const CallInst *C, const Value *P, uint64_t Size) {
374 return getModRefInfo(C, Location(P, Size));
377 /// getModRefInfo (for invokes) - Return information about whether
378 /// a particular invoke modifies or reads the specified memory location.
379 ModRefResult getModRefInfo(const InvokeInst *I,
380 const Location &Loc) {
381 return getModRefInfo(ImmutableCallSite(I), Loc);
384 /// getModRefInfo (for invokes) - A convenience wrapper.
385 ModRefResult getModRefInfo(const InvokeInst *I,
386 const Value *P, uint64_t Size) {
387 return getModRefInfo(I, Location(P, Size));
390 /// getModRefInfo (for loads) - Return information about whether
391 /// a particular load modifies or reads the specified memory location.
392 ModRefResult getModRefInfo(const LoadInst *L, const Location &Loc);
394 /// getModRefInfo (for loads) - A convenience wrapper.
395 ModRefResult getModRefInfo(const LoadInst *L, const Value *P, uint64_t Size) {
396 return getModRefInfo(L, Location(P, Size));
399 /// getModRefInfo (for stores) - Return information about whether
400 /// a particular store modifies or reads the specified memory location.
401 ModRefResult getModRefInfo(const StoreInst *S, const Location &Loc);
403 /// getModRefInfo (for stores) - A convenience wrapper.
404 ModRefResult getModRefInfo(const StoreInst *S, const Value *P, uint64_t Size){
405 return getModRefInfo(S, Location(P, Size));
408 /// getModRefInfo (for fences) - Return information about whether
409 /// a particular store modifies or reads the specified memory location.
410 ModRefResult getModRefInfo(const FenceInst *S, const Location &Loc) {
411 // Conservatively correct. (We could possibly be a bit smarter if
412 // Loc is a alloca that doesn't escape.)
416 /// getModRefInfo (for fences) - A convenience wrapper.
417 ModRefResult getModRefInfo(const FenceInst *S, const Value *P, uint64_t Size){
418 return getModRefInfo(S, Location(P, Size));
421 /// getModRefInfo (for cmpxchges) - Return information about whether
422 /// a particular cmpxchg modifies or reads the specified memory location.
423 ModRefResult getModRefInfo(const AtomicCmpXchgInst *CX, const Location &Loc);
425 /// getModRefInfo (for cmpxchges) - A convenience wrapper.
426 ModRefResult getModRefInfo(const AtomicCmpXchgInst *CX,
427 const Value *P, unsigned Size) {
428 return getModRefInfo(CX, Location(P, Size));
431 /// getModRefInfo (for atomicrmws) - Return information about whether
432 /// a particular atomicrmw modifies or reads the specified memory location.
433 ModRefResult getModRefInfo(const AtomicRMWInst *RMW, const Location &Loc);
435 /// getModRefInfo (for atomicrmws) - A convenience wrapper.
436 ModRefResult getModRefInfo(const AtomicRMWInst *RMW,
437 const Value *P, unsigned Size) {
438 return getModRefInfo(RMW, Location(P, Size));
441 /// getModRefInfo (for va_args) - Return information about whether
442 /// a particular va_arg modifies or reads the specified memory location.
443 ModRefResult getModRefInfo(const VAArgInst* I, const Location &Loc);
445 /// getModRefInfo (for va_args) - A convenience wrapper.
446 ModRefResult getModRefInfo(const VAArgInst* I, const Value* P, uint64_t Size){
447 return getModRefInfo(I, Location(P, Size));
449 /// getModRefInfo - Return information about whether a call and an instruction
450 /// may refer to the same memory locations.
451 ModRefResult getModRefInfo(Instruction *I,
452 ImmutableCallSite Call);
454 /// getModRefInfo - Return information about whether two call sites may refer
455 /// to the same set of memory locations. See
456 /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo
458 virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
459 ImmutableCallSite CS2);
461 /// callCapturesBefore - Return information about whether a particular call
462 /// site modifies or reads the specified memory location.
463 ModRefResult callCapturesBefore(const Instruction *I,
464 const AliasAnalysis::Location &MemLoc,
467 /// callCapturesBefore - A convenience wrapper.
468 ModRefResult callCapturesBefore(const Instruction *I, const Value *P,
469 uint64_t Size, DominatorTree *DT) {
470 return callCapturesBefore(I, Location(P, Size), DT);
473 //===--------------------------------------------------------------------===//
474 /// Higher level methods for querying mod/ref information.
477 /// canBasicBlockModify - Return true if it is possible for execution of the
478 /// specified basic block to modify the location Loc.
479 bool canBasicBlockModify(const BasicBlock &BB, const Location &Loc);
481 /// canBasicBlockModify - A convenience wrapper.
482 bool canBasicBlockModify(const BasicBlock &BB, const Value *P, uint64_t Size){
483 return canBasicBlockModify(BB, Location(P, Size));
486 /// canInstructionRangeModRef - Return true if it is possible for the
487 /// execution of the specified instructions to mod\ref (according to the
488 /// mode) the location Loc. The instructions to consider are all
489 /// of the instructions in the range of [I1,I2] INCLUSIVE.
490 /// I1 and I2 must be in the same basic block.
491 bool canInstructionRangeModRef(const Instruction &I1,
492 const Instruction &I2, const Location &Loc,
493 const ModRefResult Mode);
495 /// canInstructionRangeModRef - A convenience wrapper.
496 bool canInstructionRangeModRef(const Instruction &I1,
497 const Instruction &I2, const Value *Ptr,
498 uint64_t Size, const ModRefResult Mode) {
499 return canInstructionRangeModRef(I1, I2, Location(Ptr, Size), Mode);
502 //===--------------------------------------------------------------------===//
503 /// Methods that clients should call when they transform the program to allow
504 /// alias analyses to update their internal data structures. Note that these
505 /// methods may be called on any instruction, regardless of whether or not
506 /// they have pointer-analysis implications.
509 /// deleteValue - This method should be called whenever an LLVM Value is
510 /// deleted from the program, for example when an instruction is found to be
511 /// redundant and is eliminated.
513 virtual void deleteValue(Value *V);
515 /// copyValue - This method should be used whenever a preexisting value in the
516 /// program is copied or cloned, introducing a new value. Note that analysis
517 /// implementations should tolerate clients that use this method to introduce
518 /// the same value multiple times: if the analysis already knows about a
519 /// value, it should ignore the request.
521 virtual void copyValue(Value *From, Value *To);
523 /// addEscapingUse - This method should be used whenever an escaping use is
524 /// added to a pointer value. Analysis implementations may either return
525 /// conservative responses for that value in the future, or may recompute
526 /// some or all internal state to continue providing precise responses.
528 /// Escaping uses are considered by anything _except_ the following:
529 /// - GEPs or bitcasts of the pointer
530 /// - Loads through the pointer
531 /// - Stores through (but not of) the pointer
532 virtual void addEscapingUse(Use &U);
534 /// replaceWithNewValue - This method is the obvious combination of the two
535 /// above, and it provided as a helper to simplify client code.
537 void replaceWithNewValue(Value *Old, Value *New) {
543 /// isNoAliasCall - Return true if this pointer is returned by a noalias
545 bool isNoAliasCall(const Value *V);
547 /// isNoAliasArgument - Return true if this is an argument with the noalias
549 bool isNoAliasArgument(const Value *V);
551 /// isIdentifiedObject - Return true if this pointer refers to a distinct and
552 /// identifiable object. This returns true for:
553 /// Global Variables and Functions (but not Global Aliases)
555 /// ByVal and NoAlias Arguments
556 /// NoAlias returns (e.g. calls to malloc)
558 bool isIdentifiedObject(const Value *V);
560 /// isIdentifiedFunctionLocal - Return true if V is umabigously identified
561 /// at the function-level. Different IdentifiedFunctionLocals can't alias.
562 /// Further, an IdentifiedFunctionLocal can not alias with any function
563 /// arguments other than itself, which is not necessarily true for
564 /// IdentifiedObjects.
565 bool isIdentifiedFunctionLocal(const Value *V);
567 } // End llvm namespace