1 //===- llvm/Analysis/AliasAnalysis.h - Alias Analysis Interface -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the generic AliasAnalysis interface, which is used as the
11 // common interface used by all clients of alias analysis information, and
12 // implemented by all alias analysis implementations. Mod/Ref information is
13 // also captured by this interface.
15 // Implementations of this interface must implement the various virtual methods,
16 // which automatically provides functionality for the entire suite of client
19 // This API identifies memory regions with the MemoryLocation class. The pointer
20 // component specifies the base memory address of the region. The Size specifies
21 // the maximum size (in address units) of the memory region, or
22 // MemoryLocation::UnknownSize if the size is not known. The TBAA tag
23 // identifies the "type" of the memory reference; see the
24 // TypeBasedAliasAnalysis class for details.
26 // Some non-obvious details include:
27 // - Pointers that point to two completely different objects in memory never
28 // alias, regardless of the value of the Size component.
29 // - NoAlias doesn't imply inequal pointers. The most obvious example of this
30 // is two pointers to constant memory. Even if they are equal, constant
31 // memory is never stored to, so there will never be any dependencies.
32 // In this and other situations, the pointers may be both NoAlias and
33 // MustAlias at the same time. The current API can only return one result,
34 // though this is rarely a problem in practice.
36 //===----------------------------------------------------------------------===//
38 #ifndef LLVM_ANALYSIS_ALIASANALYSIS_H
39 #define LLVM_ANALYSIS_ALIASANALYSIS_H
41 #include "llvm/ADT/DenseMap.h"
42 #include "llvm/IR/CallSite.h"
43 #include "llvm/IR/Metadata.h"
44 #include "llvm/Analysis/MemoryLocation.h"
52 class TargetLibraryInfo;
55 class MemTransferInst;
62 const TargetLibraryInfo *TLI;
65 AliasAnalysis *AA; // Previous Alias Analysis to chain to.
68 /// InitializeAliasAnalysis - Subclasses must call this method to initialize
69 /// the AliasAnalysis interface before any other methods are called. This is
70 /// typically called by the run* methods of these subclasses. This may be
71 /// called multiple times.
73 void InitializeAliasAnalysis(Pass *P, const DataLayout *DL);
75 /// getAnalysisUsage - All alias analysis implementations should invoke this
76 /// directly (using AliasAnalysis::getAnalysisUsage(AU)).
77 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
80 static char ID; // Class identification, replacement for typeinfo
81 AliasAnalysis() : DL(nullptr), TLI(nullptr), AA(nullptr) {}
82 virtual ~AliasAnalysis(); // We want to be subclassed
84 /// getTargetLibraryInfo - Return a pointer to the current TargetLibraryInfo
85 /// object, or null if no TargetLibraryInfo object is available.
87 const TargetLibraryInfo *getTargetLibraryInfo() const { return TLI; }
89 /// getTypeStoreSize - Return the DataLayout store size for the given type,
90 /// if known, or a conservative value otherwise.
92 uint64_t getTypeStoreSize(Type *Ty);
94 //===--------------------------------------------------------------------===//
98 /// Alias analysis result - Either we know for sure that it does not alias, we
99 /// know for sure it must alias, or we don't know anything: The two pointers
100 /// _might_ alias. This enum is designed so you can do things like:
101 /// if (AA.alias(P1, P2)) { ... }
102 /// to check to see if two pointers might alias.
104 /// See docs/AliasAnalysis.html for more information on the specific meanings
108 NoAlias = 0, ///< No dependencies.
109 MayAlias, ///< Anything goes.
110 PartialAlias, ///< Pointers differ, but pointees overlap.
111 MustAlias ///< Pointers are equal.
114 /// alias - The main low level interface to the alias analysis implementation.
115 /// Returns an AliasResult indicating whether the two pointers are aliased to
116 /// each other. This is the interface that must be implemented by specific
117 /// alias analysis implementations.
118 virtual AliasResult alias(const MemoryLocation &LocA,
119 const MemoryLocation &LocB);
121 /// alias - A convenience wrapper.
122 AliasResult alias(const Value *V1, uint64_t V1Size,
123 const Value *V2, uint64_t V2Size) {
124 return alias(MemoryLocation(V1, V1Size), MemoryLocation(V2, V2Size));
127 /// alias - A convenience wrapper.
128 AliasResult alias(const Value *V1, const Value *V2) {
129 return alias(V1, MemoryLocation::UnknownSize, V2,
130 MemoryLocation::UnknownSize);
133 /// isNoAlias - A trivial helper function to check to see if the specified
134 /// pointers are no-alias.
135 bool isNoAlias(const MemoryLocation &LocA, const MemoryLocation &LocB) {
136 return alias(LocA, LocB) == NoAlias;
139 /// isNoAlias - A convenience wrapper.
140 bool isNoAlias(const Value *V1, uint64_t V1Size,
141 const Value *V2, uint64_t V2Size) {
142 return isNoAlias(MemoryLocation(V1, V1Size), MemoryLocation(V2, V2Size));
145 /// isNoAlias - A convenience wrapper.
146 bool isNoAlias(const Value *V1, const Value *V2) {
147 return isNoAlias(MemoryLocation(V1), MemoryLocation(V2));
150 /// isMustAlias - A convenience wrapper.
151 bool isMustAlias(const MemoryLocation &LocA, const MemoryLocation &LocB) {
152 return alias(LocA, LocB) == MustAlias;
155 /// isMustAlias - A convenience wrapper.
156 bool isMustAlias(const Value *V1, const Value *V2) {
157 return alias(V1, 1, V2, 1) == MustAlias;
160 /// pointsToConstantMemory - If the specified memory location is
161 /// known to be constant, return true. If OrLocal is true and the
162 /// specified memory location is known to be "local" (derived from
163 /// an alloca), return true. Otherwise return false.
164 virtual bool pointsToConstantMemory(const MemoryLocation &Loc,
165 bool OrLocal = false);
167 /// pointsToConstantMemory - A convenient wrapper.
168 bool pointsToConstantMemory(const Value *P, bool OrLocal = false) {
169 return pointsToConstantMemory(MemoryLocation(P), OrLocal);
172 //===--------------------------------------------------------------------===//
173 /// Simple mod/ref information...
176 /// ModRefResult - Represent the result of a mod/ref query. Mod and Ref are
177 /// bits which may be or'd together.
179 enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 };
181 /// These values define additional bits used to define the
182 /// ModRefBehavior values.
183 enum { Nowhere = 0, ArgumentPointees = 4, Anywhere = 8 | ArgumentPointees };
185 /// ModRefBehavior - Summary of how a function affects memory in the program.
186 /// Loads from constant globals are not considered memory accesses for this
187 /// interface. Also, functions may freely modify stack space local to their
188 /// invocation without having to report it through these interfaces.
189 enum ModRefBehavior {
190 /// DoesNotAccessMemory - This function does not perform any non-local loads
191 /// or stores to memory.
193 /// This property corresponds to the GCC 'const' attribute.
194 /// This property corresponds to the LLVM IR 'readnone' attribute.
195 /// This property corresponds to the IntrNoMem LLVM intrinsic flag.
196 DoesNotAccessMemory = Nowhere | NoModRef,
198 /// OnlyReadsArgumentPointees - The only memory references in this function
199 /// (if it has any) are non-volatile loads from objects pointed to by its
200 /// pointer-typed arguments, with arbitrary offsets.
202 /// This property corresponds to the IntrReadArgMem LLVM intrinsic flag.
203 OnlyReadsArgumentPointees = ArgumentPointees | Ref,
205 /// OnlyAccessesArgumentPointees - The only memory references in this
206 /// function (if it has any) are non-volatile loads and stores from objects
207 /// pointed to by its pointer-typed arguments, with arbitrary offsets.
209 /// This property corresponds to the IntrReadWriteArgMem LLVM intrinsic flag.
210 OnlyAccessesArgumentPointees = ArgumentPointees | ModRef,
212 /// OnlyReadsMemory - This function does not perform any non-local stores or
213 /// volatile loads, but may read from any memory location.
215 /// This property corresponds to the GCC 'pure' attribute.
216 /// This property corresponds to the LLVM IR 'readonly' attribute.
217 /// This property corresponds to the IntrReadMem LLVM intrinsic flag.
218 OnlyReadsMemory = Anywhere | Ref,
220 /// UnknownModRefBehavior - This indicates that the function could not be
221 /// classified into one of the behaviors above.
222 UnknownModRefBehavior = Anywhere | ModRef
225 /// Get the ModRef info associated with a pointer argument of a callsite. The
226 /// result's bits are set to indicate the allowed aliasing ModRef kinds. Note
227 /// that these bits do not necessarily account for the overall behavior of
228 /// the function, but rather only provide additional per-argument
230 virtual ModRefResult getArgModRefInfo(ImmutableCallSite CS, unsigned ArgIdx);
232 /// getModRefBehavior - Return the behavior when calling the given call site.
233 virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS);
235 /// getModRefBehavior - Return the behavior when calling the given function.
236 /// For use when the call site is not known.
237 virtual ModRefBehavior getModRefBehavior(const Function *F);
239 /// doesNotAccessMemory - If the specified call is known to never read or
240 /// write memory, return true. If the call only reads from known-constant
241 /// memory, it is also legal to return true. Calls that unwind the stack
242 /// are legal for this predicate.
244 /// Many optimizations (such as CSE and LICM) can be performed on such calls
245 /// without worrying about aliasing properties, and many calls have this
246 /// property (e.g. calls to 'sin' and 'cos').
248 /// This property corresponds to the GCC 'const' attribute.
250 bool doesNotAccessMemory(ImmutableCallSite CS) {
251 return getModRefBehavior(CS) == DoesNotAccessMemory;
254 /// doesNotAccessMemory - If the specified function is known to never read or
255 /// write memory, return true. For use when the call site is not known.
257 bool doesNotAccessMemory(const Function *F) {
258 return getModRefBehavior(F) == DoesNotAccessMemory;
261 /// onlyReadsMemory - If the specified call is known to only read from
262 /// non-volatile memory (or not access memory at all), return true. Calls
263 /// that unwind the stack are legal for this predicate.
265 /// This property allows many common optimizations to be performed in the
266 /// absence of interfering store instructions, such as CSE of strlen calls.
268 /// This property corresponds to the GCC 'pure' attribute.
270 bool onlyReadsMemory(ImmutableCallSite CS) {
271 return onlyReadsMemory(getModRefBehavior(CS));
274 /// onlyReadsMemory - If the specified function is known to only read from
275 /// non-volatile memory (or not access memory at all), return true. For use
276 /// when the call site is not known.
278 bool onlyReadsMemory(const Function *F) {
279 return onlyReadsMemory(getModRefBehavior(F));
282 /// onlyReadsMemory - Return true if functions with the specified behavior are
283 /// known to only read from non-volatile memory (or not access memory at all).
285 static bool onlyReadsMemory(ModRefBehavior MRB) {
289 /// onlyAccessesArgPointees - Return true if functions with the specified
290 /// behavior are known to read and write at most from objects pointed to by
291 /// their pointer-typed arguments (with arbitrary offsets).
293 static bool onlyAccessesArgPointees(ModRefBehavior MRB) {
294 return !(MRB & Anywhere & ~ArgumentPointees);
297 /// doesAccessArgPointees - Return true if functions with the specified
298 /// behavior are known to potentially read or write from objects pointed
299 /// to be their pointer-typed arguments (with arbitrary offsets).
301 static bool doesAccessArgPointees(ModRefBehavior MRB) {
302 return (MRB & ModRef) && (MRB & ArgumentPointees);
305 /// getModRefInfo - Return information about whether or not an
306 /// instruction may read or write memory (without regard to a
307 /// specific location)
308 ModRefResult getModRefInfo(const Instruction *I) {
309 if (auto CS = ImmutableCallSite(I)) {
310 auto MRB = getModRefBehavior(CS);
320 return getModRefInfo(I, MemoryLocation());
323 /// getModRefInfo - Return information about whether or not an instruction may
324 /// read or write the specified memory location. An instruction
325 /// that doesn't read or write memory may be trivially LICM'd for example.
326 ModRefResult getModRefInfo(const Instruction *I, const MemoryLocation &Loc) {
327 switch (I->getOpcode()) {
328 case Instruction::VAArg: return getModRefInfo((const VAArgInst*)I, Loc);
329 case Instruction::Load: return getModRefInfo((const LoadInst*)I, Loc);
330 case Instruction::Store: return getModRefInfo((const StoreInst*)I, Loc);
331 case Instruction::Fence: return getModRefInfo((const FenceInst*)I, Loc);
332 case Instruction::AtomicCmpXchg:
333 return getModRefInfo((const AtomicCmpXchgInst*)I, Loc);
334 case Instruction::AtomicRMW:
335 return getModRefInfo((const AtomicRMWInst*)I, Loc);
336 case Instruction::Call: return getModRefInfo((const CallInst*)I, Loc);
337 case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,Loc);
338 default: return NoModRef;
342 /// getModRefInfo - A convenience wrapper.
343 ModRefResult getModRefInfo(const Instruction *I,
344 const Value *P, uint64_t Size) {
345 return getModRefInfo(I, MemoryLocation(P, Size));
348 /// getModRefInfo (for call sites) - Return information about whether
349 /// a particular call site modifies or reads the specified memory location.
350 virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
351 const MemoryLocation &Loc);
353 /// getModRefInfo (for call sites) - A convenience wrapper.
354 ModRefResult getModRefInfo(ImmutableCallSite CS,
355 const Value *P, uint64_t Size) {
356 return getModRefInfo(CS, MemoryLocation(P, Size));
359 /// getModRefInfo (for calls) - Return information about whether
360 /// a particular call modifies or reads the specified memory location.
361 ModRefResult getModRefInfo(const CallInst *C, const MemoryLocation &Loc) {
362 return getModRefInfo(ImmutableCallSite(C), Loc);
365 /// getModRefInfo (for calls) - A convenience wrapper.
366 ModRefResult getModRefInfo(const CallInst *C, const Value *P, uint64_t Size) {
367 return getModRefInfo(C, MemoryLocation(P, Size));
370 /// getModRefInfo (for invokes) - Return information about whether
371 /// a particular invoke modifies or reads the specified memory location.
372 ModRefResult getModRefInfo(const InvokeInst *I, const MemoryLocation &Loc) {
373 return getModRefInfo(ImmutableCallSite(I), Loc);
376 /// getModRefInfo (for invokes) - A convenience wrapper.
377 ModRefResult getModRefInfo(const InvokeInst *I,
378 const Value *P, uint64_t Size) {
379 return getModRefInfo(I, MemoryLocation(P, Size));
382 /// getModRefInfo (for loads) - Return information about whether
383 /// a particular load modifies or reads the specified memory location.
384 ModRefResult getModRefInfo(const LoadInst *L, const MemoryLocation &Loc);
386 /// getModRefInfo (for loads) - A convenience wrapper.
387 ModRefResult getModRefInfo(const LoadInst *L, const Value *P, uint64_t Size) {
388 return getModRefInfo(L, MemoryLocation(P, Size));
391 /// getModRefInfo (for stores) - Return information about whether
392 /// a particular store modifies or reads the specified memory location.
393 ModRefResult getModRefInfo(const StoreInst *S, const MemoryLocation &Loc);
395 /// getModRefInfo (for stores) - A convenience wrapper.
396 ModRefResult getModRefInfo(const StoreInst *S, const Value *P, uint64_t Size){
397 return getModRefInfo(S, MemoryLocation(P, Size));
400 /// getModRefInfo (for fences) - Return information about whether
401 /// a particular store modifies or reads the specified memory location.
402 ModRefResult getModRefInfo(const FenceInst *S, const MemoryLocation &Loc) {
403 // Conservatively correct. (We could possibly be a bit smarter if
404 // Loc is a alloca that doesn't escape.)
408 /// getModRefInfo (for fences) - A convenience wrapper.
409 ModRefResult getModRefInfo(const FenceInst *S, const Value *P, uint64_t Size){
410 return getModRefInfo(S, MemoryLocation(P, Size));
413 /// getModRefInfo (for cmpxchges) - Return information about whether
414 /// a particular cmpxchg modifies or reads the specified memory location.
415 ModRefResult getModRefInfo(const AtomicCmpXchgInst *CX,
416 const MemoryLocation &Loc);
418 /// getModRefInfo (for cmpxchges) - A convenience wrapper.
419 ModRefResult getModRefInfo(const AtomicCmpXchgInst *CX,
420 const Value *P, unsigned Size) {
421 return getModRefInfo(CX, MemoryLocation(P, Size));
424 /// getModRefInfo (for atomicrmws) - Return information about whether
425 /// a particular atomicrmw modifies or reads the specified memory location.
426 ModRefResult getModRefInfo(const AtomicRMWInst *RMW,
427 const MemoryLocation &Loc);
429 /// getModRefInfo (for atomicrmws) - A convenience wrapper.
430 ModRefResult getModRefInfo(const AtomicRMWInst *RMW,
431 const Value *P, unsigned Size) {
432 return getModRefInfo(RMW, MemoryLocation(P, Size));
435 /// getModRefInfo (for va_args) - Return information about whether
436 /// a particular va_arg modifies or reads the specified memory location.
437 ModRefResult getModRefInfo(const VAArgInst *I, const MemoryLocation &Loc);
439 /// getModRefInfo (for va_args) - A convenience wrapper.
440 ModRefResult getModRefInfo(const VAArgInst* I, const Value* P, uint64_t Size){
441 return getModRefInfo(I, MemoryLocation(P, Size));
443 /// getModRefInfo - Return information about whether a call and an instruction
444 /// may refer to the same memory locations.
445 ModRefResult getModRefInfo(Instruction *I,
446 ImmutableCallSite Call);
448 /// getModRefInfo - Return information about whether two call sites may refer
449 /// to the same set of memory locations. See
450 /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo
452 virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
453 ImmutableCallSite CS2);
455 /// callCapturesBefore - Return information about whether a particular call
456 /// site modifies or reads the specified memory location.
457 ModRefResult callCapturesBefore(const Instruction *I,
458 const MemoryLocation &MemLoc,
461 /// callCapturesBefore - A convenience wrapper.
462 ModRefResult callCapturesBefore(const Instruction *I, const Value *P,
463 uint64_t Size, DominatorTree *DT) {
464 return callCapturesBefore(I, MemoryLocation(P, Size), DT);
467 //===--------------------------------------------------------------------===//
468 /// Higher level methods for querying mod/ref information.
471 /// canBasicBlockModify - Return true if it is possible for execution of the
472 /// specified basic block to modify the location Loc.
473 bool canBasicBlockModify(const BasicBlock &BB, const MemoryLocation &Loc);
475 /// canBasicBlockModify - A convenience wrapper.
476 bool canBasicBlockModify(const BasicBlock &BB, const Value *P, uint64_t Size){
477 return canBasicBlockModify(BB, MemoryLocation(P, Size));
480 /// canInstructionRangeModRef - Return true if it is possible for the
481 /// execution of the specified instructions to mod\ref (according to the
482 /// mode) the location Loc. The instructions to consider are all
483 /// of the instructions in the range of [I1,I2] INCLUSIVE.
484 /// I1 and I2 must be in the same basic block.
485 bool canInstructionRangeModRef(const Instruction &I1, const Instruction &I2,
486 const MemoryLocation &Loc,
487 const ModRefResult Mode);
489 /// canInstructionRangeModRef - A convenience wrapper.
490 bool canInstructionRangeModRef(const Instruction &I1,
491 const Instruction &I2, const Value *Ptr,
492 uint64_t Size, const ModRefResult Mode) {
493 return canInstructionRangeModRef(I1, I2, MemoryLocation(Ptr, Size), Mode);
496 //===--------------------------------------------------------------------===//
497 /// Methods that clients should call when they transform the program to allow
498 /// alias analyses to update their internal data structures. Note that these
499 /// methods may be called on any instruction, regardless of whether or not
500 /// they have pointer-analysis implications.
503 /// deleteValue - This method should be called whenever an LLVM Value is
504 /// deleted from the program, for example when an instruction is found to be
505 /// redundant and is eliminated.
507 virtual void deleteValue(Value *V);
509 /// copyValue - This method should be used whenever a preexisting value in the
510 /// program is copied or cloned, introducing a new value. Note that analysis
511 /// implementations should tolerate clients that use this method to introduce
512 /// the same value multiple times: if the analysis already knows about a
513 /// value, it should ignore the request.
515 virtual void copyValue(Value *From, Value *To);
517 /// addEscapingUse - This method should be used whenever an escaping use is
518 /// added to a pointer value. Analysis implementations may either return
519 /// conservative responses for that value in the future, or may recompute
520 /// some or all internal state to continue providing precise responses.
522 /// Escaping uses are considered by anything _except_ the following:
523 /// - GEPs or bitcasts of the pointer
524 /// - Loads through the pointer
525 /// - Stores through (but not of) the pointer
526 virtual void addEscapingUse(Use &U);
528 /// replaceWithNewValue - This method is the obvious combination of the two
529 /// above, and it provided as a helper to simplify client code.
531 void replaceWithNewValue(Value *Old, Value *New) {
537 /// isNoAliasCall - Return true if this pointer is returned by a noalias
539 bool isNoAliasCall(const Value *V);
541 /// isNoAliasArgument - Return true if this is an argument with the noalias
543 bool isNoAliasArgument(const Value *V);
545 /// isIdentifiedObject - Return true if this pointer refers to a distinct and
546 /// identifiable object. This returns true for:
547 /// Global Variables and Functions (but not Global Aliases)
549 /// ByVal and NoAlias Arguments
550 /// NoAlias returns (e.g. calls to malloc)
552 bool isIdentifiedObject(const Value *V);
554 /// isIdentifiedFunctionLocal - Return true if V is umabigously identified
555 /// at the function-level. Different IdentifiedFunctionLocals can't alias.
556 /// Further, an IdentifiedFunctionLocal can not alias with any function
557 /// arguments other than itself, which is not necessarily true for
558 /// IdentifiedObjects.
559 bool isIdentifiedFunctionLocal(const Value *V);
561 } // End llvm namespace