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 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 /// Alias analysis result - Either we know for sure that it does not alias, we
103 /// know for sure it must alias, or we don't know anything: The two pointers
104 /// _might_ alias. This enum is designed so you can do things like:
105 /// if (AA.alias(P1, P2)) { ... }
106 /// to check to see if two pointers might alias.
108 /// See docs/AliasAnalysis.html for more information on the specific meanings
112 NoAlias = 0, ///< No dependencies.
113 MayAlias, ///< Anything goes.
114 PartialAlias, ///< Pointers differ, but pointees overlap.
115 MustAlias ///< Pointers are equal.
118 /// alias - The main low level interface to the alias analysis implementation.
119 /// Returns an AliasResult indicating whether the two pointers are aliased to
120 /// each other. This is the interface that must be implemented by specific
121 /// alias analysis implementations.
122 virtual AliasResult alias(const MemoryLocation &LocA,
123 const MemoryLocation &LocB);
125 /// alias - A convenience wrapper.
126 AliasResult alias(const Value *V1, uint64_t V1Size,
127 const Value *V2, uint64_t V2Size) {
128 return alias(MemoryLocation(V1, V1Size), MemoryLocation(V2, V2Size));
131 /// alias - A convenience wrapper.
132 AliasResult alias(const Value *V1, const Value *V2) {
133 return alias(V1, UnknownSize, V2, UnknownSize);
136 /// isNoAlias - A trivial helper function to check to see if the specified
137 /// pointers are no-alias.
138 bool isNoAlias(const MemoryLocation &LocA, const MemoryLocation &LocB) {
139 return alias(LocA, LocB) == NoAlias;
142 /// isNoAlias - A convenience wrapper.
143 bool isNoAlias(const Value *V1, uint64_t V1Size,
144 const Value *V2, uint64_t V2Size) {
145 return isNoAlias(MemoryLocation(V1, V1Size), MemoryLocation(V2, V2Size));
148 /// isNoAlias - A convenience wrapper.
149 bool isNoAlias(const Value *V1, const Value *V2) {
150 return isNoAlias(MemoryLocation(V1), MemoryLocation(V2));
153 /// isMustAlias - A convenience wrapper.
154 bool isMustAlias(const MemoryLocation &LocA, const MemoryLocation &LocB) {
155 return alias(LocA, LocB) == MustAlias;
158 /// isMustAlias - A convenience wrapper.
159 bool isMustAlias(const Value *V1, const Value *V2) {
160 return alias(V1, 1, V2, 1) == MustAlias;
163 /// pointsToConstantMemory - If the specified memory location is
164 /// known to be constant, return true. If OrLocal is true and the
165 /// specified memory location is known to be "local" (derived from
166 /// an alloca), return true. Otherwise return false.
167 virtual bool pointsToConstantMemory(const MemoryLocation &Loc,
168 bool OrLocal = false);
170 /// pointsToConstantMemory - A convenient wrapper.
171 bool pointsToConstantMemory(const Value *P, bool OrLocal = false) {
172 return pointsToConstantMemory(MemoryLocation(P), OrLocal);
175 //===--------------------------------------------------------------------===//
176 /// Simple mod/ref information...
179 /// ModRefResult - Represent the result of a mod/ref query. Mod and Ref are
180 /// bits which may be or'd together.
182 enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 };
184 /// These values define additional bits used to define the
185 /// ModRefBehavior values.
186 enum { Nowhere = 0, ArgumentPointees = 4, Anywhere = 8 | ArgumentPointees };
188 /// ModRefBehavior - Summary of how a function affects memory in the program.
189 /// Loads from constant globals are not considered memory accesses for this
190 /// interface. Also, functions may freely modify stack space local to their
191 /// invocation without having to report it through these interfaces.
192 enum ModRefBehavior {
193 /// DoesNotAccessMemory - This function does not perform any non-local loads
194 /// or stores to memory.
196 /// This property corresponds to the GCC 'const' attribute.
197 /// This property corresponds to the LLVM IR 'readnone' attribute.
198 /// This property corresponds to the IntrNoMem LLVM intrinsic flag.
199 DoesNotAccessMemory = Nowhere | NoModRef,
201 /// OnlyReadsArgumentPointees - The only memory references in this function
202 /// (if it has any) are non-volatile loads from objects pointed to by its
203 /// pointer-typed arguments, with arbitrary offsets.
205 /// This property corresponds to the IntrReadArgMem LLVM intrinsic flag.
206 OnlyReadsArgumentPointees = ArgumentPointees | Ref,
208 /// OnlyAccessesArgumentPointees - The only memory references in this
209 /// function (if it has any) are non-volatile loads and stores from objects
210 /// pointed to by its pointer-typed arguments, with arbitrary offsets.
212 /// This property corresponds to the IntrReadWriteArgMem LLVM intrinsic flag.
213 OnlyAccessesArgumentPointees = ArgumentPointees | ModRef,
215 /// OnlyReadsMemory - This function does not perform any non-local stores or
216 /// volatile loads, but may read from any memory location.
218 /// This property corresponds to the GCC 'pure' attribute.
219 /// This property corresponds to the LLVM IR 'readonly' attribute.
220 /// This property corresponds to the IntrReadMem LLVM intrinsic flag.
221 OnlyReadsMemory = Anywhere | Ref,
223 /// UnknownModRefBehavior - This indicates that the function could not be
224 /// classified into one of the behaviors above.
225 UnknownModRefBehavior = Anywhere | ModRef
228 /// Get the ModRef info associated with a pointer argument of a callsite. The
229 /// result's bits are set to indicate the allowed aliasing ModRef kinds. Note
230 /// that these bits do not necessarily account for the overall behavior of
231 /// the function, but rather only provide additional per-argument
233 virtual ModRefResult getArgModRefInfo(ImmutableCallSite CS, unsigned ArgIdx);
235 /// getModRefBehavior - Return the behavior when calling the given call site.
236 virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS);
238 /// getModRefBehavior - Return the behavior when calling the given function.
239 /// For use when the call site is not known.
240 virtual ModRefBehavior getModRefBehavior(const Function *F);
242 /// doesNotAccessMemory - If the specified call is known to never read or
243 /// write memory, return true. If the call only reads from known-constant
244 /// memory, it is also legal to return true. Calls that unwind the stack
245 /// are legal for this predicate.
247 /// Many optimizations (such as CSE and LICM) can be performed on such calls
248 /// without worrying about aliasing properties, and many calls have this
249 /// property (e.g. calls to 'sin' and 'cos').
251 /// This property corresponds to the GCC 'const' attribute.
253 bool doesNotAccessMemory(ImmutableCallSite CS) {
254 return getModRefBehavior(CS) == DoesNotAccessMemory;
257 /// doesNotAccessMemory - If the specified function is known to never read or
258 /// write memory, return true. For use when the call site is not known.
260 bool doesNotAccessMemory(const Function *F) {
261 return getModRefBehavior(F) == DoesNotAccessMemory;
264 /// onlyReadsMemory - If the specified call is known to only read from
265 /// non-volatile memory (or not access memory at all), return true. Calls
266 /// that unwind the stack are legal for this predicate.
268 /// This property allows many common optimizations to be performed in the
269 /// absence of interfering store instructions, such as CSE of strlen calls.
271 /// This property corresponds to the GCC 'pure' attribute.
273 bool onlyReadsMemory(ImmutableCallSite CS) {
274 return onlyReadsMemory(getModRefBehavior(CS));
277 /// onlyReadsMemory - If the specified function is known to only read from
278 /// non-volatile memory (or not access memory at all), return true. For use
279 /// when the call site is not known.
281 bool onlyReadsMemory(const Function *F) {
282 return onlyReadsMemory(getModRefBehavior(F));
285 /// onlyReadsMemory - Return true if functions with the specified behavior are
286 /// known to only read from non-volatile memory (or not access memory at all).
288 static bool onlyReadsMemory(ModRefBehavior MRB) {
292 /// onlyAccessesArgPointees - Return true if functions with the specified
293 /// behavior are known to read and write at most from objects pointed to by
294 /// their pointer-typed arguments (with arbitrary offsets).
296 static bool onlyAccessesArgPointees(ModRefBehavior MRB) {
297 return !(MRB & Anywhere & ~ArgumentPointees);
300 /// doesAccessArgPointees - Return true if functions with the specified
301 /// behavior are known to potentially read or write from objects pointed
302 /// to be their pointer-typed arguments (with arbitrary offsets).
304 static bool doesAccessArgPointees(ModRefBehavior MRB) {
305 return (MRB & ModRef) && (MRB & ArgumentPointees);
308 /// getModRefInfo - Return information about whether or not an
309 /// instruction may read or write memory (without regard to a
310 /// specific location)
311 ModRefResult getModRefInfo(const Instruction *I) {
312 if (auto CS = ImmutableCallSite(I)) {
313 auto MRB = getModRefBehavior(CS);
323 return getModRefInfo(I, MemoryLocation());
326 /// getModRefInfo - Return information about whether or not an instruction may
327 /// read or write the specified memory location. An instruction
328 /// that doesn't read or write memory may be trivially LICM'd for example.
329 ModRefResult getModRefInfo(const Instruction *I, const MemoryLocation &Loc) {
330 switch (I->getOpcode()) {
331 case Instruction::VAArg: return getModRefInfo((const VAArgInst*)I, Loc);
332 case Instruction::Load: return getModRefInfo((const LoadInst*)I, Loc);
333 case Instruction::Store: return getModRefInfo((const StoreInst*)I, Loc);
334 case Instruction::Fence: return getModRefInfo((const FenceInst*)I, Loc);
335 case Instruction::AtomicCmpXchg:
336 return getModRefInfo((const AtomicCmpXchgInst*)I, Loc);
337 case Instruction::AtomicRMW:
338 return getModRefInfo((const AtomicRMWInst*)I, Loc);
339 case Instruction::Call: return getModRefInfo((const CallInst*)I, Loc);
340 case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,Loc);
341 default: return NoModRef;
345 /// getModRefInfo - A convenience wrapper.
346 ModRefResult getModRefInfo(const Instruction *I,
347 const Value *P, uint64_t Size) {
348 return getModRefInfo(I, MemoryLocation(P, Size));
351 /// getModRefInfo (for call sites) - Return information about whether
352 /// a particular call site modifies or reads the specified memory location.
353 virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
354 const MemoryLocation &Loc);
356 /// getModRefInfo (for call sites) - A convenience wrapper.
357 ModRefResult getModRefInfo(ImmutableCallSite CS,
358 const Value *P, uint64_t Size) {
359 return getModRefInfo(CS, MemoryLocation(P, Size));
362 /// getModRefInfo (for calls) - Return information about whether
363 /// a particular call modifies or reads the specified memory location.
364 ModRefResult getModRefInfo(const CallInst *C, const MemoryLocation &Loc) {
365 return getModRefInfo(ImmutableCallSite(C), Loc);
368 /// getModRefInfo (for calls) - A convenience wrapper.
369 ModRefResult getModRefInfo(const CallInst *C, const Value *P, uint64_t Size) {
370 return getModRefInfo(C, MemoryLocation(P, Size));
373 /// getModRefInfo (for invokes) - Return information about whether
374 /// a particular invoke modifies or reads the specified memory location.
375 ModRefResult getModRefInfo(const InvokeInst *I, const MemoryLocation &Loc) {
376 return getModRefInfo(ImmutableCallSite(I), Loc);
379 /// getModRefInfo (for invokes) - A convenience wrapper.
380 ModRefResult getModRefInfo(const InvokeInst *I,
381 const Value *P, uint64_t Size) {
382 return getModRefInfo(I, MemoryLocation(P, Size));
385 /// getModRefInfo (for loads) - Return information about whether
386 /// a particular load modifies or reads the specified memory location.
387 ModRefResult getModRefInfo(const LoadInst *L, const MemoryLocation &Loc);
389 /// getModRefInfo (for loads) - A convenience wrapper.
390 ModRefResult getModRefInfo(const LoadInst *L, const Value *P, uint64_t Size) {
391 return getModRefInfo(L, MemoryLocation(P, Size));
394 /// getModRefInfo (for stores) - Return information about whether
395 /// a particular store modifies or reads the specified memory location.
396 ModRefResult getModRefInfo(const StoreInst *S, const MemoryLocation &Loc);
398 /// getModRefInfo (for stores) - A convenience wrapper.
399 ModRefResult getModRefInfo(const StoreInst *S, const Value *P, uint64_t Size){
400 return getModRefInfo(S, MemoryLocation(P, Size));
403 /// getModRefInfo (for fences) - Return information about whether
404 /// a particular store modifies or reads the specified memory location.
405 ModRefResult getModRefInfo(const FenceInst *S, const MemoryLocation &Loc) {
406 // Conservatively correct. (We could possibly be a bit smarter if
407 // Loc is a alloca that doesn't escape.)
411 /// getModRefInfo (for fences) - A convenience wrapper.
412 ModRefResult getModRefInfo(const FenceInst *S, const Value *P, uint64_t Size){
413 return getModRefInfo(S, MemoryLocation(P, Size));
416 /// getModRefInfo (for cmpxchges) - Return information about whether
417 /// a particular cmpxchg modifies or reads the specified memory location.
418 ModRefResult getModRefInfo(const AtomicCmpXchgInst *CX,
419 const MemoryLocation &Loc);
421 /// getModRefInfo (for cmpxchges) - A convenience wrapper.
422 ModRefResult getModRefInfo(const AtomicCmpXchgInst *CX,
423 const Value *P, unsigned Size) {
424 return getModRefInfo(CX, MemoryLocation(P, Size));
427 /// getModRefInfo (for atomicrmws) - Return information about whether
428 /// a particular atomicrmw modifies or reads the specified memory location.
429 ModRefResult getModRefInfo(const AtomicRMWInst *RMW,
430 const MemoryLocation &Loc);
432 /// getModRefInfo (for atomicrmws) - A convenience wrapper.
433 ModRefResult getModRefInfo(const AtomicRMWInst *RMW,
434 const Value *P, unsigned Size) {
435 return getModRefInfo(RMW, MemoryLocation(P, Size));
438 /// getModRefInfo (for va_args) - Return information about whether
439 /// a particular va_arg modifies or reads the specified memory location.
440 ModRefResult getModRefInfo(const VAArgInst *I, const MemoryLocation &Loc);
442 /// getModRefInfo (for va_args) - A convenience wrapper.
443 ModRefResult getModRefInfo(const VAArgInst* I, const Value* P, uint64_t Size){
444 return getModRefInfo(I, MemoryLocation(P, Size));
446 /// getModRefInfo - Return information about whether a call and an instruction
447 /// may refer to the same memory locations.
448 ModRefResult getModRefInfo(Instruction *I,
449 ImmutableCallSite Call);
451 /// getModRefInfo - Return information about whether two call sites may refer
452 /// to the same set of memory locations. See
453 /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo
455 virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
456 ImmutableCallSite CS2);
458 /// callCapturesBefore - Return information about whether a particular call
459 /// site modifies or reads the specified memory location.
460 ModRefResult callCapturesBefore(const Instruction *I,
461 const MemoryLocation &MemLoc,
464 /// callCapturesBefore - A convenience wrapper.
465 ModRefResult callCapturesBefore(const Instruction *I, const Value *P,
466 uint64_t Size, DominatorTree *DT) {
467 return callCapturesBefore(I, MemoryLocation(P, Size), DT);
470 //===--------------------------------------------------------------------===//
471 /// Higher level methods for querying mod/ref information.
474 /// canBasicBlockModify - Return true if it is possible for execution of the
475 /// specified basic block to modify the location Loc.
476 bool canBasicBlockModify(const BasicBlock &BB, const MemoryLocation &Loc);
478 /// canBasicBlockModify - A convenience wrapper.
479 bool canBasicBlockModify(const BasicBlock &BB, const Value *P, uint64_t Size){
480 return canBasicBlockModify(BB, MemoryLocation(P, Size));
483 /// canInstructionRangeModRef - Return true if it is possible for the
484 /// execution of the specified instructions to mod\ref (according to the
485 /// mode) the location Loc. The instructions to consider are all
486 /// of the instructions in the range of [I1,I2] INCLUSIVE.
487 /// I1 and I2 must be in the same basic block.
488 bool canInstructionRangeModRef(const Instruction &I1, const Instruction &I2,
489 const MemoryLocation &Loc,
490 const ModRefResult Mode);
492 /// canInstructionRangeModRef - A convenience wrapper.
493 bool canInstructionRangeModRef(const Instruction &I1,
494 const Instruction &I2, const Value *Ptr,
495 uint64_t Size, const ModRefResult Mode) {
496 return canInstructionRangeModRef(I1, I2, MemoryLocation(Ptr, Size), Mode);
499 //===--------------------------------------------------------------------===//
500 /// Methods that clients should call when they transform the program to allow
501 /// alias analyses to update their internal data structures. Note that these
502 /// methods may be called on any instruction, regardless of whether or not
503 /// they have pointer-analysis implications.
506 /// deleteValue - This method should be called whenever an LLVM Value is
507 /// deleted from the program, for example when an instruction is found to be
508 /// redundant and is eliminated.
510 virtual void deleteValue(Value *V);
512 /// copyValue - This method should be used whenever a preexisting value in the
513 /// program is copied or cloned, introducing a new value. Note that analysis
514 /// implementations should tolerate clients that use this method to introduce
515 /// the same value multiple times: if the analysis already knows about a
516 /// value, it should ignore the request.
518 virtual void copyValue(Value *From, Value *To);
520 /// addEscapingUse - This method should be used whenever an escaping use is
521 /// added to a pointer value. Analysis implementations may either return
522 /// conservative responses for that value in the future, or may recompute
523 /// some or all internal state to continue providing precise responses.
525 /// Escaping uses are considered by anything _except_ the following:
526 /// - GEPs or bitcasts of the pointer
527 /// - Loads through the pointer
528 /// - Stores through (but not of) the pointer
529 virtual void addEscapingUse(Use &U);
531 /// replaceWithNewValue - This method is the obvious combination of the two
532 /// above, and it provided as a helper to simplify client code.
534 void replaceWithNewValue(Value *Old, Value *New) {
540 /// isNoAliasCall - Return true if this pointer is returned by a noalias
542 bool isNoAliasCall(const Value *V);
544 /// isNoAliasArgument - Return true if this is an argument with the noalias
546 bool isNoAliasArgument(const Value *V);
548 /// isIdentifiedObject - Return true if this pointer refers to a distinct and
549 /// identifiable object. This returns true for:
550 /// Global Variables and Functions (but not Global Aliases)
552 /// ByVal and NoAlias Arguments
553 /// NoAlias returns (e.g. calls to malloc)
555 bool isIdentifiedObject(const Value *V);
557 /// isIdentifiedFunctionLocal - Return true if V is umabigously identified
558 /// at the function-level. Different IdentifiedFunctionLocals can't alias.
559 /// Further, an IdentifiedFunctionLocal can not alias with any function
560 /// arguments other than itself, which is not necessarily true for
561 /// IdentifiedObjects.
562 bool isIdentifiedFunctionLocal(const Value *V);
564 } // End llvm namespace