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_ALIAS_ANALYSIS_H
38 #define LLVM_ANALYSIS_ALIAS_ANALYSIS_H
40 #include "llvm/Support/CallSite.h"
51 class MemTransferInst;
59 AliasAnalysis *AA; // Previous Alias Analysis to chain to.
62 /// InitializeAliasAnalysis - Subclasses must call this method to initialize
63 /// the AliasAnalysis interface before any other methods are called. This is
64 /// typically called by the run* methods of these subclasses. This may be
65 /// called multiple times.
67 void InitializeAliasAnalysis(Pass *P);
69 /// getAnalysisUsage - All alias analysis implementations should invoke this
70 /// directly (using AliasAnalysis::getAnalysisUsage(AU)).
71 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
74 static char ID; // Class identification, replacement for typeinfo
75 AliasAnalysis() : TD(0), AA(0) {}
76 virtual ~AliasAnalysis(); // We want to be subclassed
78 /// UnknownSize - This is a special value which can be used with the
79 /// size arguments in alias queries to indicate that the caller does not
80 /// know the sizes of the potential memory references.
81 static uint64_t const UnknownSize = ~UINT64_C(0);
83 /// getTargetData - Return a pointer to the current TargetData object, or
84 /// null if no TargetData object is available.
86 const TargetData *getTargetData() const { return TD; }
88 /// getTypeStoreSize - Return the TargetData store size for the given type,
89 /// if known, or a conservative value otherwise.
91 uint64_t getTypeStoreSize(const Type *Ty);
93 //===--------------------------------------------------------------------===//
97 /// Location - A description of a memory location.
99 /// Ptr - The address of the start of the location.
101 /// Size - The maximum size of the location, in address-units, or
102 /// UnknownSize if the size is not known. Note that an unknown size does
103 /// not mean the pointer aliases the entire virtual address space, because
104 /// there are restrictions on stepping out of one object and into another.
105 /// See http://llvm.org/docs/LangRef.html#pointeraliasing
107 /// TBAATag - The metadata node which describes the TBAA type of
108 /// the location, or null if there is no known unique tag.
109 const MDNode *TBAATag;
111 explicit Location(const Value *P = 0,
112 uint64_t S = UnknownSize,
114 : Ptr(P), Size(S), TBAATag(N) {}
116 Location getWithNewPtr(const Value *NewPtr) const {
117 Location Copy(*this);
122 Location getWithNewSize(uint64_t NewSize) const {
123 Location Copy(*this);
128 Location getWithoutTBAATag() const {
129 Location Copy(*this);
135 /// getLocation - Fill in Loc with information about the memory reference by
136 /// the given instruction.
137 Location getLocation(const LoadInst *LI);
138 Location getLocation(const StoreInst *SI);
139 Location getLocation(const VAArgInst *VI);
140 static Location getLocationForSource(const MemTransferInst *MTI);
141 static Location getLocationForDest(const MemIntrinsic *MI);
143 /// Alias analysis result - Either we know for sure that it does not alias, we
144 /// know for sure it must alias, or we don't know anything: The two pointers
145 /// _might_ alias. This enum is designed so you can do things like:
146 /// if (AA.alias(P1, P2)) { ... }
147 /// to check to see if two pointers might alias.
149 /// See docs/AliasAnalysis.html for more information on the specific meanings
153 NoAlias = 0, ///< No dependencies.
154 MayAlias = 1, ///< Anything goes.
155 MustAlias = 2 ///< Pointers are equal.
158 /// alias - The main low level interface to the alias analysis implementation.
159 /// Returns an AliasResult indicating whether the two pointers are aliased to
160 /// each other. This is the interface that must be implemented by specific
161 /// alias analysis implementations.
162 virtual AliasResult alias(const Location &LocA, const Location &LocB);
164 /// alias - A convenience wrapper.
165 AliasResult alias(const Value *V1, uint64_t V1Size,
166 const Value *V2, uint64_t V2Size) {
167 return alias(Location(V1, V1Size), Location(V2, V2Size));
170 /// alias - A convenience wrapper.
171 AliasResult alias(const Value *V1, const Value *V2) {
172 return alias(V1, UnknownSize, V2, UnknownSize);
175 /// isNoAlias - A trivial helper function to check to see if the specified
176 /// pointers are no-alias.
177 bool isNoAlias(const Location &LocA, const Location &LocB) {
178 return alias(LocA, LocB) == NoAlias;
181 /// isNoAlias - A convenience wrapper.
182 bool isNoAlias(const Value *V1, uint64_t V1Size,
183 const Value *V2, uint64_t V2Size) {
184 return isNoAlias(Location(V1, V1Size), Location(V2, V2Size));
187 /// pointsToConstantMemory - If the specified memory location is
188 /// known to be constant, return true. If OrLocal is true and the
189 /// specified memory location is known to be "local" (derived from
190 /// an alloca), return true. Otherwise return false.
191 virtual bool pointsToConstantMemory(const Location &Loc,
192 bool OrLocal = false);
194 /// pointsToConstantMemory - A convenient wrapper.
195 bool pointsToConstantMemory(const Value *P, bool OrLocal = false) {
196 return pointsToConstantMemory(Location(P), OrLocal);
199 //===--------------------------------------------------------------------===//
200 /// Simple mod/ref information...
203 /// ModRefResult - Represent the result of a mod/ref query. Mod and Ref are
204 /// bits which may be or'd together.
206 enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 };
208 /// These values define additional bits used to define the
209 /// ModRefBehavior values.
210 enum { Nowhere = 0, ArgumentPointees = 4, Anywhere = 8 | ArgumentPointees };
212 /// ModRefBehavior - Summary of how a function affects memory in the program.
213 /// Loads from constant globals are not considered memory accesses for this
214 /// interface. Also, functions may freely modify stack space local to their
215 /// invocation without having to report it through these interfaces.
216 enum ModRefBehavior {
217 /// DoesNotAccessMemory - This function does not perform any non-local loads
218 /// or stores to memory.
220 /// This property corresponds to the GCC 'const' attribute.
221 /// This property corresponds to the LLVM IR 'readnone' attribute.
222 /// This property corresponds to the IntrNoMem LLVM intrinsic flag.
223 DoesNotAccessMemory = Nowhere | NoModRef,
225 /// OnlyReadsArgumentPointees - The only memory references in this function
226 /// (if it has any) are non-volatile loads from objects pointed to by its
227 /// pointer-typed arguments, with arbitrary offsets.
229 /// This property corresponds to the IntrReadArgMem LLVM intrinsic flag.
230 OnlyReadsArgumentPointees = ArgumentPointees | Ref,
232 /// OnlyAccessesArgumentPointees - The only memory references in this
233 /// function (if it has any) are non-volatile loads and stores from objects
234 /// pointed to by its pointer-typed arguments, with arbitrary offsets.
236 /// This property corresponds to the IntrReadWriteArgMem LLVM intrinsic flag.
237 OnlyAccessesArgumentPointees = ArgumentPointees | ModRef,
239 /// OnlyReadsMemory - This function does not perform any non-local stores or
240 /// volatile loads, but may read from any memory location.
242 /// This property corresponds to the GCC 'pure' attribute.
243 /// This property corresponds to the LLVM IR 'readonly' attribute.
244 /// This property corresponds to the IntrReadMem LLVM intrinsic flag.
245 OnlyReadsMemory = Anywhere | Ref,
247 /// UnknownModRefBehavior - This indicates that the function could not be
248 /// classified into one of the behaviors above.
249 UnknownModRefBehavior = Anywhere | ModRef
252 /// getModRefBehavior - Return the behavior when calling the given call site.
253 virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS);
255 /// getModRefBehavior - Return the behavior when calling the given function.
256 /// For use when the call site is not known.
257 virtual ModRefBehavior getModRefBehavior(const Function *F);
259 /// doesNotAccessMemory - If the specified call is known to never read or
260 /// write memory, return true. If the call only reads from known-constant
261 /// memory, it is also legal to return true. Calls that unwind the stack
262 /// are legal for this predicate.
264 /// Many optimizations (such as CSE and LICM) can be performed on such calls
265 /// without worrying about aliasing properties, and many calls have this
266 /// property (e.g. calls to 'sin' and 'cos').
268 /// This property corresponds to the GCC 'const' attribute.
270 bool doesNotAccessMemory(ImmutableCallSite CS) {
271 return getModRefBehavior(CS) == DoesNotAccessMemory;
274 /// doesNotAccessMemory - If the specified function is known to never read or
275 /// write memory, return true. For use when the call site is not known.
277 bool doesNotAccessMemory(const Function *F) {
278 return getModRefBehavior(F) == DoesNotAccessMemory;
281 /// onlyReadsMemory - If the specified call is known to only read from
282 /// non-volatile memory (or not access memory at all), return true. Calls
283 /// that unwind the stack are legal for this predicate.
285 /// This property allows many common optimizations to be performed in the
286 /// absence of interfering store instructions, such as CSE of strlen calls.
288 /// This property corresponds to the GCC 'pure' attribute.
290 bool onlyReadsMemory(ImmutableCallSite CS) {
291 return onlyReadsMemory(getModRefBehavior(CS));
294 /// onlyReadsMemory - If the specified function is known to only read from
295 /// non-volatile memory (or not access memory at all), return true. For use
296 /// when the call site is not known.
298 bool onlyReadsMemory(const Function *F) {
299 return onlyReadsMemory(getModRefBehavior(F));
302 /// onlyReadsMemory - Return true if functions with the specified behavior are
303 /// known to only read from non-volatile memory (or not access memory at all).
305 static bool onlyReadsMemory(ModRefBehavior MRB) {
309 /// onlyAccessesArgPointees - Return true if functions with the specified
310 /// behavior are known to read and write at most from objects pointed to by
311 /// their pointer-typed arguments (with arbitrary offsets).
313 static bool onlyAccessesArgPointees(ModRefBehavior MRB) {
314 return !(MRB & Anywhere & ~ArgumentPointees);
317 /// doesAccessArgPointees - Return true if functions with the specified
318 /// behavior are known to potentially read or write from objects pointed
319 /// to be their pointer-typed arguments (with arbitrary offsets).
321 static bool doesAccessArgPointees(ModRefBehavior MRB) {
322 return (MRB & ModRef) && (MRB & ArgumentPointees);
325 /// getModRefInfo - Return information about whether or not an instruction may
326 /// read or write the specified memory location. An instruction
327 /// that doesn't read or write memory may be trivially LICM'd for example.
328 ModRefResult getModRefInfo(const Instruction *I,
329 const Location &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::Call: return getModRefInfo((const CallInst*)I, Loc);
335 case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,Loc);
336 default: return NoModRef;
340 /// getModRefInfo - A convenience wrapper.
341 ModRefResult getModRefInfo(const Instruction *I,
342 const Value *P, uint64_t Size) {
343 return getModRefInfo(I, Location(P, Size));
346 /// getModRefInfo (for call sites) - Return whether information about whether
347 /// a particular call site modifies or reads the specified memory location.
348 virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
349 const Location &Loc);
351 /// getModRefInfo (for call sites) - A convenience wrapper.
352 ModRefResult getModRefInfo(ImmutableCallSite CS,
353 const Value *P, uint64_t Size) {
354 return getModRefInfo(CS, Location(P, Size));
357 /// getModRefInfo (for calls) - Return whether information about whether
358 /// a particular call modifies or reads the specified memory location.
359 ModRefResult getModRefInfo(const CallInst *C, const Location &Loc) {
360 return getModRefInfo(ImmutableCallSite(C), Loc);
363 /// getModRefInfo (for calls) - A convenience wrapper.
364 ModRefResult getModRefInfo(const CallInst *C, const Value *P, uint64_t Size) {
365 return getModRefInfo(C, Location(P, Size));
368 /// getModRefInfo (for invokes) - Return whether information about whether
369 /// a particular invoke modifies or reads the specified memory location.
370 ModRefResult getModRefInfo(const InvokeInst *I,
371 const Location &Loc) {
372 return getModRefInfo(ImmutableCallSite(I), Loc);
375 /// getModRefInfo (for invokes) - A convenience wrapper.
376 ModRefResult getModRefInfo(const InvokeInst *I,
377 const Value *P, uint64_t Size) {
378 return getModRefInfo(I, Location(P, Size));
381 /// getModRefInfo (for loads) - Return whether information about whether
382 /// a particular load modifies or reads the specified memory location.
383 ModRefResult getModRefInfo(const LoadInst *L, const Location &Loc);
385 /// getModRefInfo (for loads) - A convenience wrapper.
386 ModRefResult getModRefInfo(const LoadInst *L, const Value *P, uint64_t Size) {
387 return getModRefInfo(L, Location(P, Size));
390 /// getModRefInfo (for stores) - Return whether information about whether
391 /// a particular store modifies or reads the specified memory location.
392 ModRefResult getModRefInfo(const StoreInst *S, const Location &Loc);
394 /// getModRefInfo (for stores) - A convenience wrapper.
395 ModRefResult getModRefInfo(const StoreInst *S, const Value *P, uint64_t Size) {
396 return getModRefInfo(S, Location(P, Size));
399 /// getModRefInfo (for va_args) - Return whether information about whether
400 /// a particular va_arg modifies or reads the specified memory location.
401 ModRefResult getModRefInfo(const VAArgInst* I, const Location &Loc);
403 /// getModRefInfo (for va_args) - A convenience wrapper.
404 ModRefResult getModRefInfo(const VAArgInst* I, const Value* P, uint64_t Size) {
405 return getModRefInfo(I, Location(P, Size));
408 /// getModRefInfo - Return information about whether two call sites may refer
409 /// to the same set of memory locations. See
410 /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo
412 virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
413 ImmutableCallSite CS2);
415 //===--------------------------------------------------------------------===//
416 /// Higher level methods for querying mod/ref information.
419 /// canBasicBlockModify - Return true if it is possible for execution of the
420 /// specified basic block to modify the value pointed to by Ptr.
421 bool canBasicBlockModify(const BasicBlock &BB, const Location &Loc);
423 /// canBasicBlockModify - A convenience wrapper.
424 bool canBasicBlockModify(const BasicBlock &BB, const Value *P, uint64_t Size){
425 return canBasicBlockModify(BB, Location(P, Size));
428 /// canInstructionRangeModify - Return true if it is possible for the
429 /// execution of the specified instructions to modify the value pointed to by
430 /// Ptr. The instructions to consider are all of the instructions in the
431 /// range of [I1,I2] INCLUSIVE. I1 and I2 must be in the same basic block.
432 bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
433 const Location &Loc);
435 /// canInstructionRangeModify - A convenience wrapper.
436 bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
437 const Value *Ptr, uint64_t Size) {
438 return canInstructionRangeModify(I1, I2, Location(Ptr, Size));
441 //===--------------------------------------------------------------------===//
442 /// Methods that clients should call when they transform the program to allow
443 /// alias analyses to update their internal data structures. Note that these
444 /// methods may be called on any instruction, regardless of whether or not
445 /// they have pointer-analysis implications.
448 /// deleteValue - This method should be called whenever an LLVM Value is
449 /// deleted from the program, for example when an instruction is found to be
450 /// redundant and is eliminated.
452 virtual void deleteValue(Value *V);
454 /// copyValue - This method should be used whenever a preexisting value in the
455 /// program is copied or cloned, introducing a new value. Note that analysis
456 /// implementations should tolerate clients that use this method to introduce
457 /// the same value multiple times: if the analysis already knows about a
458 /// value, it should ignore the request.
460 virtual void copyValue(Value *From, Value *To);
462 /// replaceWithNewValue - This method is the obvious combination of the two
463 /// above, and it provided as a helper to simplify client code.
465 void replaceWithNewValue(Value *Old, Value *New) {
471 /// isNoAliasCall - Return true if this pointer is returned by a noalias
473 bool isNoAliasCall(const Value *V);
475 /// isIdentifiedObject - Return true if this pointer refers to a distinct and
476 /// identifiable object. This returns true for:
477 /// Global Variables and Functions (but not Global Aliases)
478 /// Allocas and Mallocs
479 /// ByVal and NoAlias Arguments
482 bool isIdentifiedObject(const Value *V);
484 } // End llvm namespace