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 location associated with a pointer argument of a callsite.
232 /// The mask bits are set to indicate the allowed aliasing ModRef kinds.
233 /// Note that these mask bits do not necessarily account for the overall
234 /// behavior of the function, but rather only provide additional
235 /// per-argument information.
236 virtual Location getArgLocation(ImmutableCallSite CS, unsigned ArgIdx,
239 /// getModRefBehavior - Return the behavior when calling the given call site.
240 virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS);
242 /// getModRefBehavior - Return the behavior when calling the given function.
243 /// For use when the call site is not known.
244 virtual ModRefBehavior getModRefBehavior(const Function *F);
246 /// doesNotAccessMemory - If the specified call is known to never read or
247 /// write memory, return true. If the call only reads from known-constant
248 /// memory, it is also legal to return true. Calls that unwind the stack
249 /// are legal for this predicate.
251 /// Many optimizations (such as CSE and LICM) can be performed on such calls
252 /// without worrying about aliasing properties, and many calls have this
253 /// property (e.g. calls to 'sin' and 'cos').
255 /// This property corresponds to the GCC 'const' attribute.
257 bool doesNotAccessMemory(ImmutableCallSite CS) {
258 return getModRefBehavior(CS) == DoesNotAccessMemory;
261 /// doesNotAccessMemory - If the specified function is known to never read or
262 /// write memory, return true. For use when the call site is not known.
264 bool doesNotAccessMemory(const Function *F) {
265 return getModRefBehavior(F) == DoesNotAccessMemory;
268 /// onlyReadsMemory - If the specified call is known to only read from
269 /// non-volatile memory (or not access memory at all), return true. Calls
270 /// that unwind the stack are legal for this predicate.
272 /// This property allows many common optimizations to be performed in the
273 /// absence of interfering store instructions, such as CSE of strlen calls.
275 /// This property corresponds to the GCC 'pure' attribute.
277 bool onlyReadsMemory(ImmutableCallSite CS) {
278 return onlyReadsMemory(getModRefBehavior(CS));
281 /// onlyReadsMemory - If the specified function is known to only read from
282 /// non-volatile memory (or not access memory at all), return true. For use
283 /// when the call site is not known.
285 bool onlyReadsMemory(const Function *F) {
286 return onlyReadsMemory(getModRefBehavior(F));
289 /// onlyReadsMemory - Return true if functions with the specified behavior are
290 /// known to only read from non-volatile memory (or not access memory at all).
292 static bool onlyReadsMemory(ModRefBehavior MRB) {
296 /// onlyAccessesArgPointees - Return true if functions with the specified
297 /// behavior are known to read and write at most from objects pointed to by
298 /// their pointer-typed arguments (with arbitrary offsets).
300 static bool onlyAccessesArgPointees(ModRefBehavior MRB) {
301 return !(MRB & Anywhere & ~ArgumentPointees);
304 /// doesAccessArgPointees - Return true if functions with the specified
305 /// behavior are known to potentially read or write from objects pointed
306 /// to be their pointer-typed arguments (with arbitrary offsets).
308 static bool doesAccessArgPointees(ModRefBehavior MRB) {
309 return (MRB & ModRef) && (MRB & ArgumentPointees);
312 /// getModRefInfo - Return information about whether or not an
313 /// instruction may read or write memory (without regard to a
314 /// specific location)
315 ModRefResult getModRefInfo(const Instruction *I) {
316 if (auto CS = ImmutableCallSite(I)) {
317 auto MRB = getModRefBehavior(CS);
327 return getModRefInfo(I, Location());
330 /// getModRefInfo - Return information about whether or not an instruction may
331 /// read or write the specified memory location. An instruction
332 /// that doesn't read or write memory may be trivially LICM'd for example.
333 ModRefResult getModRefInfo(const Instruction *I,
334 const Location &Loc) {
335 switch (I->getOpcode()) {
336 case Instruction::VAArg: return getModRefInfo((const VAArgInst*)I, Loc);
337 case Instruction::Load: return getModRefInfo((const LoadInst*)I, Loc);
338 case Instruction::Store: return getModRefInfo((const StoreInst*)I, Loc);
339 case Instruction::Fence: return getModRefInfo((const FenceInst*)I, Loc);
340 case Instruction::AtomicCmpXchg:
341 return getModRefInfo((const AtomicCmpXchgInst*)I, Loc);
342 case Instruction::AtomicRMW:
343 return getModRefInfo((const AtomicRMWInst*)I, Loc);
344 case Instruction::Call: return getModRefInfo((const CallInst*)I, Loc);
345 case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,Loc);
346 default: return NoModRef;
350 /// getModRefInfo - A convenience wrapper.
351 ModRefResult getModRefInfo(const Instruction *I,
352 const Value *P, uint64_t Size) {
353 return getModRefInfo(I, Location(P, Size));
356 /// getModRefInfo (for call sites) - Return information about whether
357 /// a particular call site modifies or reads the specified memory location.
358 virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
359 const Location &Loc);
361 /// getModRefInfo (for call sites) - A convenience wrapper.
362 ModRefResult getModRefInfo(ImmutableCallSite CS,
363 const Value *P, uint64_t Size) {
364 return getModRefInfo(CS, Location(P, Size));
367 /// getModRefInfo (for calls) - Return information about whether
368 /// a particular call modifies or reads the specified memory location.
369 ModRefResult getModRefInfo(const CallInst *C, const Location &Loc) {
370 return getModRefInfo(ImmutableCallSite(C), Loc);
373 /// getModRefInfo (for calls) - A convenience wrapper.
374 ModRefResult getModRefInfo(const CallInst *C, const Value *P, uint64_t Size) {
375 return getModRefInfo(C, Location(P, Size));
378 /// getModRefInfo (for invokes) - Return information about whether
379 /// a particular invoke modifies or reads the specified memory location.
380 ModRefResult getModRefInfo(const InvokeInst *I,
381 const Location &Loc) {
382 return getModRefInfo(ImmutableCallSite(I), Loc);
385 /// getModRefInfo (for invokes) - A convenience wrapper.
386 ModRefResult getModRefInfo(const InvokeInst *I,
387 const Value *P, uint64_t Size) {
388 return getModRefInfo(I, Location(P, Size));
391 /// getModRefInfo (for loads) - Return information about whether
392 /// a particular load modifies or reads the specified memory location.
393 ModRefResult getModRefInfo(const LoadInst *L, const Location &Loc);
395 /// getModRefInfo (for loads) - A convenience wrapper.
396 ModRefResult getModRefInfo(const LoadInst *L, const Value *P, uint64_t Size) {
397 return getModRefInfo(L, Location(P, Size));
400 /// getModRefInfo (for stores) - Return information about whether
401 /// a particular store modifies or reads the specified memory location.
402 ModRefResult getModRefInfo(const StoreInst *S, const Location &Loc);
404 /// getModRefInfo (for stores) - A convenience wrapper.
405 ModRefResult getModRefInfo(const StoreInst *S, const Value *P, uint64_t Size){
406 return getModRefInfo(S, Location(P, Size));
409 /// getModRefInfo (for fences) - Return information about whether
410 /// a particular store modifies or reads the specified memory location.
411 ModRefResult getModRefInfo(const FenceInst *S, const Location &Loc) {
412 // Conservatively correct. (We could possibly be a bit smarter if
413 // Loc is a alloca that doesn't escape.)
417 /// getModRefInfo (for fences) - A convenience wrapper.
418 ModRefResult getModRefInfo(const FenceInst *S, const Value *P, uint64_t Size){
419 return getModRefInfo(S, Location(P, Size));
422 /// getModRefInfo (for cmpxchges) - Return information about whether
423 /// a particular cmpxchg modifies or reads the specified memory location.
424 ModRefResult getModRefInfo(const AtomicCmpXchgInst *CX, const Location &Loc);
426 /// getModRefInfo (for cmpxchges) - A convenience wrapper.
427 ModRefResult getModRefInfo(const AtomicCmpXchgInst *CX,
428 const Value *P, unsigned Size) {
429 return getModRefInfo(CX, Location(P, Size));
432 /// getModRefInfo (for atomicrmws) - Return information about whether
433 /// a particular atomicrmw modifies or reads the specified memory location.
434 ModRefResult getModRefInfo(const AtomicRMWInst *RMW, const Location &Loc);
436 /// getModRefInfo (for atomicrmws) - A convenience wrapper.
437 ModRefResult getModRefInfo(const AtomicRMWInst *RMW,
438 const Value *P, unsigned Size) {
439 return getModRefInfo(RMW, Location(P, Size));
442 /// getModRefInfo (for va_args) - Return information about whether
443 /// a particular va_arg modifies or reads the specified memory location.
444 ModRefResult getModRefInfo(const VAArgInst* I, const Location &Loc);
446 /// getModRefInfo (for va_args) - A convenience wrapper.
447 ModRefResult getModRefInfo(const VAArgInst* I, const Value* P, uint64_t Size){
448 return getModRefInfo(I, Location(P, Size));
450 /// getModRefInfo - Return information about whether a call and an instruction
451 /// may refer to the same memory locations.
452 ModRefResult getModRefInfo(Instruction *I,
453 ImmutableCallSite Call);
455 /// getModRefInfo - Return information about whether two call sites may refer
456 /// to the same set of memory locations. See
457 /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo
459 virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
460 ImmutableCallSite CS2);
462 /// callCapturesBefore - Return information about whether a particular call
463 /// site modifies or reads the specified memory location.
464 ModRefResult callCapturesBefore(const Instruction *I,
465 const AliasAnalysis::Location &MemLoc,
468 /// callCapturesBefore - A convenience wrapper.
469 ModRefResult callCapturesBefore(const Instruction *I, const Value *P,
470 uint64_t Size, DominatorTree *DT) {
471 return callCapturesBefore(I, Location(P, Size), DT);
474 //===--------------------------------------------------------------------===//
475 /// Higher level methods for querying mod/ref information.
478 /// canBasicBlockModify - Return true if it is possible for execution of the
479 /// specified basic block to modify the location Loc.
480 bool canBasicBlockModify(const BasicBlock &BB, const Location &Loc);
482 /// canBasicBlockModify - A convenience wrapper.
483 bool canBasicBlockModify(const BasicBlock &BB, const Value *P, uint64_t Size){
484 return canBasicBlockModify(BB, Location(P, Size));
487 /// canInstructionRangeModRef - Return true if it is possible for the
488 /// execution of the specified instructions to mod\ref (according to the
489 /// mode) the location Loc. The instructions to consider are all
490 /// of the instructions in the range of [I1,I2] INCLUSIVE.
491 /// I1 and I2 must be in the same basic block.
492 bool canInstructionRangeModRef(const Instruction &I1,
493 const Instruction &I2, const Location &Loc,
494 const ModRefResult Mode);
496 /// canInstructionRangeModRef - A convenience wrapper.
497 bool canInstructionRangeModRef(const Instruction &I1,
498 const Instruction &I2, const Value *Ptr,
499 uint64_t Size, const ModRefResult Mode) {
500 return canInstructionRangeModRef(I1, I2, Location(Ptr, Size), Mode);
503 //===--------------------------------------------------------------------===//
504 /// Methods that clients should call when they transform the program to allow
505 /// alias analyses to update their internal data structures. Note that these
506 /// methods may be called on any instruction, regardless of whether or not
507 /// they have pointer-analysis implications.
510 /// deleteValue - This method should be called whenever an LLVM Value is
511 /// deleted from the program, for example when an instruction is found to be
512 /// redundant and is eliminated.
514 virtual void deleteValue(Value *V);
516 /// copyValue - This method should be used whenever a preexisting value in the
517 /// program is copied or cloned, introducing a new value. Note that analysis
518 /// implementations should tolerate clients that use this method to introduce
519 /// the same value multiple times: if the analysis already knows about a
520 /// value, it should ignore the request.
522 virtual void copyValue(Value *From, Value *To);
524 /// addEscapingUse - This method should be used whenever an escaping use is
525 /// added to a pointer value. Analysis implementations may either return
526 /// conservative responses for that value in the future, or may recompute
527 /// some or all internal state to continue providing precise responses.
529 /// Escaping uses are considered by anything _except_ the following:
530 /// - GEPs or bitcasts of the pointer
531 /// - Loads through the pointer
532 /// - Stores through (but not of) the pointer
533 virtual void addEscapingUse(Use &U);
535 /// replaceWithNewValue - This method is the obvious combination of the two
536 /// above, and it provided as a helper to simplify client code.
538 void replaceWithNewValue(Value *Old, Value *New) {
544 /// isNoAliasCall - Return true if this pointer is returned by a noalias
546 bool isNoAliasCall(const Value *V);
548 /// isNoAliasArgument - Return true if this is an argument with the noalias
550 bool isNoAliasArgument(const Value *V);
552 /// isIdentifiedObject - Return true if this pointer refers to a distinct and
553 /// identifiable object. This returns true for:
554 /// Global Variables and Functions (but not Global Aliases)
556 /// ByVal and NoAlias Arguments
557 /// NoAlias returns (e.g. calls to malloc)
559 bool isIdentifiedObject(const Value *V);
561 /// isIdentifiedFunctionLocal - Return true if V is umabigously identified
562 /// at the function-level. Different IdentifiedFunctionLocals can't alias.
563 /// Further, an IdentifiedFunctionLocal can not alias with any function
564 /// arguments other than itself, which is not necessarily true for
565 /// IdentifiedObjects.
566 bool isIdentifiedFunctionLocal(const Value *V);
568 } // End llvm namespace