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"
57 AliasAnalysis *AA; // Previous Alias Analysis to chain to.
60 /// InitializeAliasAnalysis - Subclasses must call this method to initialize
61 /// the AliasAnalysis interface before any other methods are called. This is
62 /// typically called by the run* methods of these subclasses. This may be
63 /// called multiple times.
65 void InitializeAliasAnalysis(Pass *P);
67 /// getAnalysisUsage - All alias analysis implementations should invoke this
68 /// directly (using AliasAnalysis::getAnalysisUsage(AU)).
69 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
72 static char ID; // Class identification, replacement for typeinfo
73 AliasAnalysis() : TD(0), AA(0) {}
74 virtual ~AliasAnalysis(); // We want to be subclassed
76 /// UnknownSize - This is a special value which can be used with the
77 /// size arguments in alias queries to indicate that the caller does not
78 /// know the sizes of the potential memory references.
79 static uint64_t const UnknownSize = ~UINT64_C(0);
81 /// getTargetData - Return a pointer to the current TargetData object, or
82 /// null if no TargetData object is available.
84 const TargetData *getTargetData() const { return TD; }
86 /// getTypeStoreSize - Return the TargetData store size for the given type,
87 /// if known, or a conservative value otherwise.
89 uint64_t getTypeStoreSize(const Type *Ty);
91 //===--------------------------------------------------------------------===//
95 /// Location - A description of a memory location.
97 /// Ptr - The address of the start of the location.
99 /// Size - The maximum size of the location, in address-units, or
100 /// UnknownSize if the size is not known. Note that an unknown size does
101 /// not mean the pointer aliases the entire virtual address space, because
102 /// there are restrictions on stepping out of one object and into another.
103 /// See http://llvm.org/docs/LangRef.html#pointeraliasing
105 /// TBAATag - The metadata node which describes the TBAA type of
106 /// the location, or null if there is no known unique tag.
107 const MDNode *TBAATag;
109 explicit Location(const Value *P = 0,
110 uint64_t S = UnknownSize,
112 : Ptr(P), Size(S), TBAATag(N) {}
114 Location getWithNewPtr(const Value *NewPtr) const {
115 Location Copy(*this);
120 Location getWithNewSize(uint64_t NewSize) const {
121 Location Copy(*this);
126 Location getWithoutTBAATag() const {
127 Location Copy(*this);
133 /// getLocation - Fill in Loc with information about the memory reference by
134 /// the given instruction.
135 Location getLocation(const LoadInst *LI);
136 Location getLocation(const StoreInst *SI);
137 Location getLocation(const VAArgInst *VI);
139 /// Alias analysis result - Either we know for sure that it does not alias, we
140 /// know for sure it must alias, or we don't know anything: The two pointers
141 /// _might_ alias. This enum is designed so you can do things like:
142 /// if (AA.alias(P1, P2)) { ... }
143 /// to check to see if two pointers might alias.
145 /// See docs/AliasAnalysis.html for more information on the specific meanings
149 NoAlias = 0, ///< No dependencies.
150 MayAlias = 1, ///< Anything goes.
151 MustAlias = 2 ///< Pointers are equal.
154 /// alias - The main low level interface to the alias analysis implementation.
155 /// Returns an AliasResult indicating whether the two pointers are aliased to
156 /// each other. This is the interface that must be implemented by specific
157 /// alias analysis implementations.
158 virtual AliasResult alias(const Location &LocA, const Location &LocB);
160 /// alias - A convenience wrapper.
161 AliasResult alias(const Value *V1, uint64_t V1Size,
162 const Value *V2, uint64_t V2Size) {
163 return alias(Location(V1, V1Size), Location(V2, V2Size));
166 /// alias - A convenience wrapper.
167 AliasResult alias(const Value *V1, const Value *V2) {
168 return alias(V1, UnknownSize, V2, UnknownSize);
171 /// isNoAlias - A trivial helper function to check to see if the specified
172 /// pointers are no-alias.
173 bool isNoAlias(const Location &LocA, const Location &LocB) {
174 return alias(LocA, LocB) == NoAlias;
177 /// isNoAlias - A convenience wrapper.
178 bool isNoAlias(const Value *V1, uint64_t V1Size,
179 const Value *V2, uint64_t V2Size) {
180 return isNoAlias(Location(V1, V1Size), Location(V2, V2Size));
183 /// pointsToConstantMemory - If the specified memory location is
184 /// known to be constant, return true. If OrLocal is true and the
185 /// specified memory location is known to be "local" (derived from
186 /// an alloca), return true. Otherwise return false.
187 virtual bool pointsToConstantMemory(const Location &Loc,
188 bool OrLocal = false);
190 /// pointsToConstantMemory - A convenient wrapper.
191 bool pointsToConstantMemory(const Value *P, bool OrLocal = false) {
192 return pointsToConstantMemory(Location(P), OrLocal);
195 //===--------------------------------------------------------------------===//
196 /// Simple mod/ref information...
199 /// ModRefResult - Represent the result of a mod/ref query. Mod and Ref are
200 /// bits which may be or'd together.
202 enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 };
204 /// These values define additional bits used to define the
205 /// ModRefBehavior values.
206 enum { Nowhere = 0, ArgumentPointees = 4, Anywhere = 8 | ArgumentPointees };
208 /// ModRefBehavior - Summary of how a function affects memory in the program.
209 /// Loads from constant globals are not considered memory accesses for this
210 /// interface. Also, functions may freely modify stack space local to their
211 /// invocation without having to report it through these interfaces.
212 enum ModRefBehavior {
213 /// DoesNotAccessMemory - This function does not perform any non-local loads
214 /// or stores to memory.
216 /// This property corresponds to the GCC 'const' attribute.
217 /// This property corresponds to the LLVM IR 'readnone' attribute.
218 /// This property corresponds to the IntrNoMem LLVM intrinsic flag.
219 DoesNotAccessMemory = Nowhere | NoModRef,
221 /// OnlyReadsArgumentPointees - The only memory references in this function
222 /// (if it has any) are non-volatile loads from objects pointed to by its
223 /// pointer-typed arguments, with arbitrary offsets.
225 /// This property corresponds to the IntrReadArgMem LLVM intrinsic flag.
226 OnlyReadsArgumentPointees = ArgumentPointees | Ref,
228 /// OnlyAccessesArgumentPointees - The only memory references in this
229 /// function (if it has any) are non-volatile loads and stores from objects
230 /// pointed to by its pointer-typed arguments, with arbitrary offsets.
232 /// This property corresponds to the IntrReadWriteArgMem LLVM intrinsic flag.
233 OnlyAccessesArgumentPointees = ArgumentPointees | ModRef,
235 /// OnlyReadsMemory - This function does not perform any non-local stores or
236 /// volatile loads, but may read from any memory location.
238 /// This property corresponds to the GCC 'pure' attribute.
239 /// This property corresponds to the LLVM IR 'readonly' attribute.
240 /// This property corresponds to the IntrReadMem LLVM intrinsic flag.
241 OnlyReadsMemory = Anywhere | Ref,
243 /// UnknownModRefBehavior - This indicates that the function could not be
244 /// classified into one of the behaviors above.
245 UnknownModRefBehavior = Anywhere | ModRef
248 /// getModRefBehavior - Return the behavior when calling the given call site.
249 virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS);
251 /// getModRefBehavior - Return the behavior when calling the given function.
252 /// For use when the call site is not known.
253 virtual ModRefBehavior getModRefBehavior(const Function *F);
255 /// doesNotAccessMemory - If the specified call is known to never read or
256 /// write memory, return true. If the call only reads from known-constant
257 /// memory, it is also legal to return true. Calls that unwind the stack
258 /// are legal for this predicate.
260 /// Many optimizations (such as CSE and LICM) can be performed on such calls
261 /// without worrying about aliasing properties, and many calls have this
262 /// property (e.g. calls to 'sin' and 'cos').
264 /// This property corresponds to the GCC 'const' attribute.
266 bool doesNotAccessMemory(ImmutableCallSite CS) {
267 return getModRefBehavior(CS) == DoesNotAccessMemory;
270 /// doesNotAccessMemory - If the specified function is known to never read or
271 /// write memory, return true. For use when the call site is not known.
273 bool doesNotAccessMemory(const Function *F) {
274 return getModRefBehavior(F) == DoesNotAccessMemory;
277 /// onlyReadsMemory - If the specified call is known to only read from
278 /// non-volatile memory (or not access memory at all), return true. Calls
279 /// that unwind the stack are legal for this predicate.
281 /// This property allows many common optimizations to be performed in the
282 /// absence of interfering store instructions, such as CSE of strlen calls.
284 /// This property corresponds to the GCC 'pure' attribute.
286 bool onlyReadsMemory(ImmutableCallSite CS) {
287 return onlyReadsMemory(getModRefBehavior(CS));
290 /// onlyReadsMemory - If the specified function is known to only read from
291 /// non-volatile memory (or not access memory at all), return true. For use
292 /// when the call site is not known.
294 bool onlyReadsMemory(const Function *F) {
295 return onlyReadsMemory(getModRefBehavior(F));
298 /// onlyReadsMemory - Return true if functions with the specified behavior are
299 /// known to only read from non-volatile memory (or not access memory at all).
301 static bool onlyReadsMemory(ModRefBehavior MRB) {
305 /// onlyAccessesArgPointees - Return true if functions with the specified
306 /// behavior are known to read and write at most from objects pointed to by
307 /// their pointer-typed arguments (with arbitrary offsets).
309 static bool onlyAccessesArgPointees(ModRefBehavior MRB) {
310 return !(MRB & Anywhere & ~ArgumentPointees);
313 /// doesAccessArgPointees - Return true if functions with the specified
314 /// behavior are known to potentially read or write from objects pointed
315 /// to be their pointer-typed arguments (with arbitrary offsets).
317 static bool doesAccessArgPointees(ModRefBehavior MRB) {
318 return (MRB & ModRef) && (MRB & ArgumentPointees);
321 /// getModRefInfo - Return information about whether or not an instruction may
322 /// read or write the specified memory location. An instruction
323 /// that doesn't read or write memory may be trivially LICM'd for example.
324 ModRefResult getModRefInfo(const Instruction *I,
325 const Location &Loc) {
326 switch (I->getOpcode()) {
327 case Instruction::VAArg: return getModRefInfo((const VAArgInst*)I, Loc);
328 case Instruction::Load: return getModRefInfo((const LoadInst*)I, Loc);
329 case Instruction::Store: return getModRefInfo((const StoreInst*)I, Loc);
330 case Instruction::Call: return getModRefInfo((const CallInst*)I, Loc);
331 case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,Loc);
332 default: return NoModRef;
336 /// getModRefInfo - A convenience wrapper.
337 ModRefResult getModRefInfo(const Instruction *I,
338 const Value *P, uint64_t Size) {
339 return getModRefInfo(I, Location(P, Size));
342 /// getModRefInfo (for call sites) - Return whether information about whether
343 /// a particular call site modifies or reads the specified memory location.
344 virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
345 const Location &Loc);
347 /// getModRefInfo (for call sites) - A convenience wrapper.
348 ModRefResult getModRefInfo(ImmutableCallSite CS,
349 const Value *P, uint64_t Size) {
350 return getModRefInfo(CS, Location(P, Size));
353 /// getModRefInfo (for calls) - Return whether information about whether
354 /// a particular call modifies or reads the specified memory location.
355 ModRefResult getModRefInfo(const CallInst *C, const Location &Loc) {
356 return getModRefInfo(ImmutableCallSite(C), Loc);
359 /// getModRefInfo (for calls) - A convenience wrapper.
360 ModRefResult getModRefInfo(const CallInst *C, const Value *P, uint64_t Size) {
361 return getModRefInfo(C, Location(P, Size));
364 /// getModRefInfo (for invokes) - Return whether information about whether
365 /// a particular invoke modifies or reads the specified memory location.
366 ModRefResult getModRefInfo(const InvokeInst *I,
367 const Location &Loc) {
368 return getModRefInfo(ImmutableCallSite(I), Loc);
371 /// getModRefInfo (for invokes) - A convenience wrapper.
372 ModRefResult getModRefInfo(const InvokeInst *I,
373 const Value *P, uint64_t Size) {
374 return getModRefInfo(I, Location(P, Size));
377 /// getModRefInfo (for loads) - Return whether information about whether
378 /// a particular load modifies or reads the specified memory location.
379 ModRefResult getModRefInfo(const LoadInst *L, const Location &Loc);
381 /// getModRefInfo (for loads) - A convenience wrapper.
382 ModRefResult getModRefInfo(const LoadInst *L, const Value *P, uint64_t Size) {
383 return getModRefInfo(L, Location(P, Size));
386 /// getModRefInfo (for stores) - Return whether information about whether
387 /// a particular store modifies or reads the specified memory location.
388 ModRefResult getModRefInfo(const StoreInst *S, const Location &Loc);
390 /// getModRefInfo (for stores) - A convenience wrapper.
391 ModRefResult getModRefInfo(const StoreInst *S, const Value *P, uint64_t Size) {
392 return getModRefInfo(S, Location(P, Size));
395 /// getModRefInfo (for va_args) - Return whether information about whether
396 /// a particular va_arg modifies or reads the specified memory location.
397 ModRefResult getModRefInfo(const VAArgInst* I, const Location &Loc);
399 /// getModRefInfo (for va_args) - A convenience wrapper.
400 ModRefResult getModRefInfo(const VAArgInst* I, const Value* P, uint64_t Size) {
401 return getModRefInfo(I, Location(P, Size));
404 /// getModRefInfo - Return information about whether two call sites may refer
405 /// to the same set of memory locations. See
406 /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo
408 virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
409 ImmutableCallSite CS2);
411 //===--------------------------------------------------------------------===//
412 /// Higher level methods for querying mod/ref information.
415 /// canBasicBlockModify - Return true if it is possible for execution of the
416 /// specified basic block to modify the value pointed to by Ptr.
417 bool canBasicBlockModify(const BasicBlock &BB, const Location &Loc);
419 /// canBasicBlockModify - A convenience wrapper.
420 bool canBasicBlockModify(const BasicBlock &BB, const Value *P, uint64_t Size){
421 return canBasicBlockModify(BB, Location(P, Size));
424 /// canInstructionRangeModify - Return true if it is possible for the
425 /// execution of the specified instructions to modify the value pointed to by
426 /// Ptr. The instructions to consider are all of the instructions in the
427 /// range of [I1,I2] INCLUSIVE. I1 and I2 must be in the same basic block.
428 bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
429 const Location &Loc);
431 /// canInstructionRangeModify - A convenience wrapper.
432 bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
433 const Value *Ptr, uint64_t Size) {
434 return canInstructionRangeModify(I1, I2, Location(Ptr, Size));
437 //===--------------------------------------------------------------------===//
438 /// Methods that clients should call when they transform the program to allow
439 /// alias analyses to update their internal data structures. Note that these
440 /// methods may be called on any instruction, regardless of whether or not
441 /// they have pointer-analysis implications.
444 /// deleteValue - This method should be called whenever an LLVM Value is
445 /// deleted from the program, for example when an instruction is found to be
446 /// redundant and is eliminated.
448 virtual void deleteValue(Value *V);
450 /// copyValue - This method should be used whenever a preexisting value in the
451 /// program is copied or cloned, introducing a new value. Note that analysis
452 /// implementations should tolerate clients that use this method to introduce
453 /// the same value multiple times: if the analysis already knows about a
454 /// value, it should ignore the request.
456 virtual void copyValue(Value *From, Value *To);
458 /// replaceWithNewValue - This method is the obvious combination of the two
459 /// above, and it provided as a helper to simplify client code.
461 void replaceWithNewValue(Value *Old, Value *New) {
467 /// isNoAliasCall - Return true if this pointer is returned by a noalias
469 bool isNoAliasCall(const Value *V);
471 /// isIdentifiedObject - Return true if this pointer refers to a distinct and
472 /// identifiable object. This returns true for:
473 /// Global Variables and Functions (but not Global Aliases)
474 /// Allocas and Mallocs
475 /// ByVal and NoAlias Arguments
478 bool isIdentifiedObject(const Value *V);
480 } // End llvm namespace