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);
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 /// getDataLayout - Return a pointer to the current DataLayout object, or
88 /// null if no DataLayout object is available.
90 const DataLayout *getDataLayout() const { return DL; }
92 /// getTargetLibraryInfo - Return a pointer to the current TargetLibraryInfo
93 /// object, or null if no TargetLibraryInfo object is available.
95 const TargetLibraryInfo *getTargetLibraryInfo() const { return TLI; }
97 /// getTypeStoreSize - Return the DataLayout store size for the given type,
98 /// if known, or a conservative value otherwise.
100 uint64_t getTypeStoreSize(Type *Ty);
102 //===--------------------------------------------------------------------===//
106 /// Location - A description of a memory location.
108 /// Ptr - The address of the start of the location.
110 /// Size - The maximum size of the location, in address-units, or
111 /// UnknownSize if the size is not known. Note that an unknown size does
112 /// not mean the pointer aliases the entire virtual address space, because
113 /// there are restrictions on stepping out of one object and into another.
114 /// See http://llvm.org/docs/LangRef.html#pointeraliasing
116 /// AATags - The metadata nodes which describes the aliasing of the
117 /// location (each member is null if that kind of information is
121 explicit Location(const Value *P = nullptr, uint64_t S = UnknownSize,
122 const AAMDNodes &N = AAMDNodes())
123 : Ptr(P), Size(S), AATags(N) {}
125 Location getWithNewPtr(const Value *NewPtr) const {
126 Location Copy(*this);
131 Location getWithNewSize(uint64_t NewSize) const {
132 Location Copy(*this);
137 Location getWithoutAATags() const {
138 Location Copy(*this);
139 Copy.AATags = AAMDNodes();
144 /// getLocation - Fill in Loc with information about the memory reference by
145 /// the given instruction.
146 Location getLocation(const LoadInst *LI);
147 Location getLocation(const StoreInst *SI);
148 Location getLocation(const VAArgInst *VI);
149 Location getLocation(const AtomicCmpXchgInst *CXI);
150 Location getLocation(const AtomicRMWInst *RMWI);
151 static Location getLocationForSource(const MemTransferInst *MTI);
152 static Location getLocationForDest(const MemIntrinsic *MI);
154 /// Alias analysis result - Either we know for sure that it does not alias, we
155 /// know for sure it must alias, or we don't know anything: The two pointers
156 /// _might_ alias. This enum is designed so you can do things like:
157 /// if (AA.alias(P1, P2)) { ... }
158 /// to check to see if two pointers might alias.
160 /// See docs/AliasAnalysis.html for more information on the specific meanings
164 NoAlias = 0, ///< No dependencies.
165 MayAlias, ///< Anything goes.
166 PartialAlias, ///< Pointers differ, but pointees overlap.
167 MustAlias ///< Pointers are equal.
170 /// alias - The main low level interface to the alias analysis implementation.
171 /// Returns an AliasResult indicating whether the two pointers are aliased to
172 /// each other. This is the interface that must be implemented by specific
173 /// alias analysis implementations.
174 virtual AliasResult alias(const Location &LocA, const Location &LocB);
176 /// alias - A convenience wrapper.
177 AliasResult alias(const Value *V1, uint64_t V1Size,
178 const Value *V2, uint64_t V2Size) {
179 return alias(Location(V1, V1Size), Location(V2, V2Size));
182 /// alias - A convenience wrapper.
183 AliasResult alias(const Value *V1, const Value *V2) {
184 return alias(V1, UnknownSize, V2, UnknownSize);
187 /// isNoAlias - A trivial helper function to check to see if the specified
188 /// pointers are no-alias.
189 bool isNoAlias(const Location &LocA, const Location &LocB) {
190 return alias(LocA, LocB) == NoAlias;
193 /// isNoAlias - A convenience wrapper.
194 bool isNoAlias(const Value *V1, uint64_t V1Size,
195 const Value *V2, uint64_t V2Size) {
196 return isNoAlias(Location(V1, V1Size), Location(V2, V2Size));
199 /// isNoAlias - A convenience wrapper.
200 bool isNoAlias(const Value *V1, const Value *V2) {
201 return isNoAlias(Location(V1), Location(V2));
204 /// isMustAlias - A convenience wrapper.
205 bool isMustAlias(const Location &LocA, const Location &LocB) {
206 return alias(LocA, LocB) == MustAlias;
209 /// isMustAlias - A convenience wrapper.
210 bool isMustAlias(const Value *V1, const Value *V2) {
211 return alias(V1, 1, V2, 1) == MustAlias;
214 /// pointsToConstantMemory - If the specified memory location is
215 /// known to be constant, return true. If OrLocal is true and the
216 /// specified memory location is known to be "local" (derived from
217 /// an alloca), return true. Otherwise return false.
218 virtual bool pointsToConstantMemory(const Location &Loc,
219 bool OrLocal = false);
221 /// pointsToConstantMemory - A convenient wrapper.
222 bool pointsToConstantMemory(const Value *P, bool OrLocal = false) {
223 return pointsToConstantMemory(Location(P), OrLocal);
226 //===--------------------------------------------------------------------===//
227 /// Simple mod/ref information...
230 /// ModRefResult - Represent the result of a mod/ref query. Mod and Ref are
231 /// bits which may be or'd together.
233 enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 };
235 /// These values define additional bits used to define the
236 /// ModRefBehavior values.
237 enum { Nowhere = 0, ArgumentPointees = 4, Anywhere = 8 | ArgumentPointees };
239 /// ModRefBehavior - Summary of how a function affects memory in the program.
240 /// Loads from constant globals are not considered memory accesses for this
241 /// interface. Also, functions may freely modify stack space local to their
242 /// invocation without having to report it through these interfaces.
243 enum ModRefBehavior {
244 /// DoesNotAccessMemory - This function does not perform any non-local loads
245 /// or stores to memory.
247 /// This property corresponds to the GCC 'const' attribute.
248 /// This property corresponds to the LLVM IR 'readnone' attribute.
249 /// This property corresponds to the IntrNoMem LLVM intrinsic flag.
250 DoesNotAccessMemory = Nowhere | NoModRef,
252 /// OnlyReadsArgumentPointees - The only memory references in this function
253 /// (if it has any) are non-volatile loads from objects pointed to by its
254 /// pointer-typed arguments, with arbitrary offsets.
256 /// This property corresponds to the IntrReadArgMem LLVM intrinsic flag.
257 OnlyReadsArgumentPointees = ArgumentPointees | Ref,
259 /// OnlyAccessesArgumentPointees - The only memory references in this
260 /// function (if it has any) are non-volatile loads and stores from objects
261 /// pointed to by its pointer-typed arguments, with arbitrary offsets.
263 /// This property corresponds to the IntrReadWriteArgMem LLVM intrinsic flag.
264 OnlyAccessesArgumentPointees = ArgumentPointees | ModRef,
266 /// OnlyReadsMemory - This function does not perform any non-local stores or
267 /// volatile loads, but may read from any memory location.
269 /// This property corresponds to the GCC 'pure' attribute.
270 /// This property corresponds to the LLVM IR 'readonly' attribute.
271 /// This property corresponds to the IntrReadMem LLVM intrinsic flag.
272 OnlyReadsMemory = Anywhere | Ref,
274 /// UnknownModRefBehavior - This indicates that the function could not be
275 /// classified into one of the behaviors above.
276 UnknownModRefBehavior = Anywhere | ModRef
279 /// Get the location associated with a pointer argument of a callsite.
280 /// The mask bits are set to indicate the allowed aliasing ModRef kinds.
281 /// Note that these mask bits do not necessarily account for the overall
282 /// behavior of the function, but rather only provide additional
283 /// per-argument information.
284 virtual Location getArgLocation(ImmutableCallSite CS, unsigned ArgIdx,
287 /// getModRefBehavior - Return the behavior when calling the given call site.
288 virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS);
290 /// getModRefBehavior - Return the behavior when calling the given function.
291 /// For use when the call site is not known.
292 virtual ModRefBehavior getModRefBehavior(const Function *F);
294 /// doesNotAccessMemory - If the specified call is known to never read or
295 /// write memory, return true. If the call only reads from known-constant
296 /// memory, it is also legal to return true. Calls that unwind the stack
297 /// are legal for this predicate.
299 /// Many optimizations (such as CSE and LICM) can be performed on such calls
300 /// without worrying about aliasing properties, and many calls have this
301 /// property (e.g. calls to 'sin' and 'cos').
303 /// This property corresponds to the GCC 'const' attribute.
305 bool doesNotAccessMemory(ImmutableCallSite CS) {
306 return getModRefBehavior(CS) == DoesNotAccessMemory;
309 /// doesNotAccessMemory - If the specified function is known to never read or
310 /// write memory, return true. For use when the call site is not known.
312 bool doesNotAccessMemory(const Function *F) {
313 return getModRefBehavior(F) == DoesNotAccessMemory;
316 /// onlyReadsMemory - If the specified call is known to only read from
317 /// non-volatile memory (or not access memory at all), return true. Calls
318 /// that unwind the stack are legal for this predicate.
320 /// This property allows many common optimizations to be performed in the
321 /// absence of interfering store instructions, such as CSE of strlen calls.
323 /// This property corresponds to the GCC 'pure' attribute.
325 bool onlyReadsMemory(ImmutableCallSite CS) {
326 return onlyReadsMemory(getModRefBehavior(CS));
329 /// onlyReadsMemory - If the specified function is known to only read from
330 /// non-volatile memory (or not access memory at all), return true. For use
331 /// when the call site is not known.
333 bool onlyReadsMemory(const Function *F) {
334 return onlyReadsMemory(getModRefBehavior(F));
337 /// onlyReadsMemory - Return true if functions with the specified behavior are
338 /// known to only read from non-volatile memory (or not access memory at all).
340 static bool onlyReadsMemory(ModRefBehavior MRB) {
344 /// onlyAccessesArgPointees - Return true if functions with the specified
345 /// behavior are known to read and write at most from objects pointed to by
346 /// their pointer-typed arguments (with arbitrary offsets).
348 static bool onlyAccessesArgPointees(ModRefBehavior MRB) {
349 return !(MRB & Anywhere & ~ArgumentPointees);
352 /// doesAccessArgPointees - Return true if functions with the specified
353 /// behavior are known to potentially read or write from objects pointed
354 /// to be their pointer-typed arguments (with arbitrary offsets).
356 static bool doesAccessArgPointees(ModRefBehavior MRB) {
357 return (MRB & ModRef) && (MRB & ArgumentPointees);
360 /// getModRefInfo - Return information about whether or not an instruction may
361 /// read or write the specified memory location. An instruction
362 /// that doesn't read or write memory may be trivially LICM'd for example.
363 ModRefResult getModRefInfo(const Instruction *I,
364 const Location &Loc) {
365 switch (I->getOpcode()) {
366 case Instruction::VAArg: return getModRefInfo((const VAArgInst*)I, Loc);
367 case Instruction::Load: return getModRefInfo((const LoadInst*)I, Loc);
368 case Instruction::Store: return getModRefInfo((const StoreInst*)I, Loc);
369 case Instruction::Fence: return getModRefInfo((const FenceInst*)I, Loc);
370 case Instruction::AtomicCmpXchg:
371 return getModRefInfo((const AtomicCmpXchgInst*)I, Loc);
372 case Instruction::AtomicRMW:
373 return getModRefInfo((const AtomicRMWInst*)I, Loc);
374 case Instruction::Call: return getModRefInfo((const CallInst*)I, Loc);
375 case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,Loc);
376 default: return NoModRef;
380 /// getModRefInfo - A convenience wrapper.
381 ModRefResult getModRefInfo(const Instruction *I,
382 const Value *P, uint64_t Size) {
383 return getModRefInfo(I, Location(P, Size));
386 /// getModRefInfo (for call sites) - Return information about whether
387 /// a particular call site modifies or reads the specified memory location.
388 virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
389 const Location &Loc);
391 /// getModRefInfo (for call sites) - A convenience wrapper.
392 ModRefResult getModRefInfo(ImmutableCallSite CS,
393 const Value *P, uint64_t Size) {
394 return getModRefInfo(CS, Location(P, Size));
397 /// getModRefInfo (for calls) - Return information about whether
398 /// a particular call modifies or reads the specified memory location.
399 ModRefResult getModRefInfo(const CallInst *C, const Location &Loc) {
400 return getModRefInfo(ImmutableCallSite(C), Loc);
403 /// getModRefInfo (for calls) - A convenience wrapper.
404 ModRefResult getModRefInfo(const CallInst *C, const Value *P, uint64_t Size) {
405 return getModRefInfo(C, Location(P, Size));
408 /// getModRefInfo (for invokes) - Return information about whether
409 /// a particular invoke modifies or reads the specified memory location.
410 ModRefResult getModRefInfo(const InvokeInst *I,
411 const Location &Loc) {
412 return getModRefInfo(ImmutableCallSite(I), Loc);
415 /// getModRefInfo (for invokes) - A convenience wrapper.
416 ModRefResult getModRefInfo(const InvokeInst *I,
417 const Value *P, uint64_t Size) {
418 return getModRefInfo(I, Location(P, Size));
421 /// getModRefInfo (for loads) - Return information about whether
422 /// a particular load modifies or reads the specified memory location.
423 ModRefResult getModRefInfo(const LoadInst *L, const Location &Loc);
425 /// getModRefInfo (for loads) - A convenience wrapper.
426 ModRefResult getModRefInfo(const LoadInst *L, const Value *P, uint64_t Size) {
427 return getModRefInfo(L, Location(P, Size));
430 /// getModRefInfo (for stores) - Return information about whether
431 /// a particular store modifies or reads the specified memory location.
432 ModRefResult getModRefInfo(const StoreInst *S, const Location &Loc);
434 /// getModRefInfo (for stores) - A convenience wrapper.
435 ModRefResult getModRefInfo(const StoreInst *S, const Value *P, uint64_t Size){
436 return getModRefInfo(S, Location(P, Size));
439 /// getModRefInfo (for fences) - Return information about whether
440 /// a particular store modifies or reads the specified memory location.
441 ModRefResult getModRefInfo(const FenceInst *S, const Location &Loc) {
442 // Conservatively correct. (We could possibly be a bit smarter if
443 // Loc is a alloca that doesn't escape.)
447 /// getModRefInfo (for fences) - A convenience wrapper.
448 ModRefResult getModRefInfo(const FenceInst *S, const Value *P, uint64_t Size){
449 return getModRefInfo(S, Location(P, Size));
452 /// getModRefInfo (for cmpxchges) - Return information about whether
453 /// a particular cmpxchg modifies or reads the specified memory location.
454 ModRefResult getModRefInfo(const AtomicCmpXchgInst *CX, const Location &Loc);
456 /// getModRefInfo (for cmpxchges) - A convenience wrapper.
457 ModRefResult getModRefInfo(const AtomicCmpXchgInst *CX,
458 const Value *P, unsigned Size) {
459 return getModRefInfo(CX, Location(P, Size));
462 /// getModRefInfo (for atomicrmws) - Return information about whether
463 /// a particular atomicrmw modifies or reads the specified memory location.
464 ModRefResult getModRefInfo(const AtomicRMWInst *RMW, const Location &Loc);
466 /// getModRefInfo (for atomicrmws) - A convenience wrapper.
467 ModRefResult getModRefInfo(const AtomicRMWInst *RMW,
468 const Value *P, unsigned Size) {
469 return getModRefInfo(RMW, Location(P, Size));
472 /// getModRefInfo (for va_args) - Return information about whether
473 /// a particular va_arg modifies or reads the specified memory location.
474 ModRefResult getModRefInfo(const VAArgInst* I, const Location &Loc);
476 /// getModRefInfo (for va_args) - A convenience wrapper.
477 ModRefResult getModRefInfo(const VAArgInst* I, const Value* P, uint64_t Size){
478 return getModRefInfo(I, Location(P, Size));
481 /// getModRefInfo - Return information about whether two call sites may refer
482 /// to the same set of memory locations. See
483 /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo
485 virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
486 ImmutableCallSite CS2);
488 /// callCapturesBefore - Return information about whether a particular call
489 /// site modifies or reads the specified memory location.
490 ModRefResult callCapturesBefore(const Instruction *I,
491 const AliasAnalysis::Location &MemLoc,
494 /// callCapturesBefore - A convenience wrapper.
495 ModRefResult callCapturesBefore(const Instruction *I, const Value *P,
496 uint64_t Size, DominatorTree *DT) {
497 return callCapturesBefore(I, Location(P, Size), DT);
500 //===--------------------------------------------------------------------===//
501 /// Higher level methods for querying mod/ref information.
504 /// canBasicBlockModify - Return true if it is possible for execution of the
505 /// specified basic block to modify the value pointed to by Ptr.
506 bool canBasicBlockModify(const BasicBlock &BB, const Location &Loc);
508 /// canBasicBlockModify - A convenience wrapper.
509 bool canBasicBlockModify(const BasicBlock &BB, const Value *P, uint64_t Size){
510 return canBasicBlockModify(BB, Location(P, Size));
513 /// canInstructionRangeModify - Return true if it is possible for the
514 /// execution of the specified instructions to modify the value pointed to by
515 /// Ptr. The instructions to consider are all of the instructions in the
516 /// range of [I1,I2] INCLUSIVE. I1 and I2 must be in the same basic block.
517 bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
518 const Location &Loc);
520 /// canInstructionRangeModify - A convenience wrapper.
521 bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
522 const Value *Ptr, uint64_t Size) {
523 return canInstructionRangeModify(I1, I2, Location(Ptr, Size));
526 //===--------------------------------------------------------------------===//
527 /// Methods that clients should call when they transform the program to allow
528 /// alias analyses to update their internal data structures. Note that these
529 /// methods may be called on any instruction, regardless of whether or not
530 /// they have pointer-analysis implications.
533 /// deleteValue - This method should be called whenever an LLVM Value is
534 /// deleted from the program, for example when an instruction is found to be
535 /// redundant and is eliminated.
537 virtual void deleteValue(Value *V);
539 /// copyValue - This method should be used whenever a preexisting value in the
540 /// program is copied or cloned, introducing a new value. Note that analysis
541 /// implementations should tolerate clients that use this method to introduce
542 /// the same value multiple times: if the analysis already knows about a
543 /// value, it should ignore the request.
545 virtual void copyValue(Value *From, Value *To);
547 /// addEscapingUse - This method should be used whenever an escaping use is
548 /// added to a pointer value. Analysis implementations may either return
549 /// conservative responses for that value in the future, or may recompute
550 /// some or all internal state to continue providing precise responses.
552 /// Escaping uses are considered by anything _except_ the following:
553 /// - GEPs or bitcasts of the pointer
554 /// - Loads through the pointer
555 /// - Stores through (but not of) the pointer
556 virtual void addEscapingUse(Use &U);
558 /// replaceWithNewValue - This method is the obvious combination of the two
559 /// above, and it provided as a helper to simplify client code.
561 void replaceWithNewValue(Value *Old, Value *New) {
567 // Specialize DenseMapInfo for Location.
569 struct DenseMapInfo<AliasAnalysis::Location> {
570 static inline AliasAnalysis::Location getEmptyKey() {
572 AliasAnalysis::Location(DenseMapInfo<const Value *>::getEmptyKey(),
575 static inline AliasAnalysis::Location getTombstoneKey() {
577 AliasAnalysis::Location(DenseMapInfo<const Value *>::getTombstoneKey(),
580 static unsigned getHashValue(const AliasAnalysis::Location &Val) {
581 return DenseMapInfo<const Value *>::getHashValue(Val.Ptr) ^
582 DenseMapInfo<uint64_t>::getHashValue(Val.Size) ^
583 DenseMapInfo<AAMDNodes>::getHashValue(Val.AATags);
585 static bool isEqual(const AliasAnalysis::Location &LHS,
586 const AliasAnalysis::Location &RHS) {
587 return LHS.Ptr == RHS.Ptr &&
588 LHS.Size == RHS.Size &&
589 LHS.AATags == RHS.AATags;
593 /// isNoAliasCall - Return true if this pointer is returned by a noalias
595 bool isNoAliasCall(const Value *V);
597 /// isNoAliasArgument - Return true if this is an argument with the noalias
599 bool isNoAliasArgument(const Value *V);
601 /// isIdentifiedObject - Return true if this pointer refers to a distinct and
602 /// identifiable object. This returns true for:
603 /// Global Variables and Functions (but not Global Aliases)
605 /// ByVal and NoAlias Arguments
606 /// NoAlias returns (e.g. calls to malloc)
608 bool isIdentifiedObject(const Value *V);
610 /// isIdentifiedFunctionLocal - Return true if V is umabigously identified
611 /// at the function-level. Different IdentifiedFunctionLocals can't alias.
612 /// Further, an IdentifiedFunctionLocal can not alias with any function
613 /// arguments other than itself, which is not necessarily true for
614 /// IdentifiedObjects.
615 bool isIdentifiedFunctionLocal(const Value *V);
617 } // End llvm namespace