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;
58 AliasAnalysis *AA; // Previous Alias Analysis to chain to.
61 /// InitializeAliasAnalysis - Subclasses must call this method to initialize
62 /// the AliasAnalysis interface before any other methods are called. This is
63 /// typically called by the run* methods of these subclasses. This may be
64 /// called multiple times.
66 void InitializeAliasAnalysis(Pass *P);
68 /// getAnalysisUsage - All alias analysis implementations should invoke this
69 /// directly (using AliasAnalysis::getAnalysisUsage(AU)).
70 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
73 static char ID; // Class identification, replacement for typeinfo
74 AliasAnalysis() : TD(0), AA(0) {}
75 virtual ~AliasAnalysis(); // We want to be subclassed
77 /// UnknownSize - This is a special value which can be used with the
78 /// size arguments in alias queries to indicate that the caller does not
79 /// know the sizes of the potential memory references.
80 static uint64_t const UnknownSize = ~UINT64_C(0);
82 /// getTargetData - Return a pointer to the current TargetData object, or
83 /// null if no TargetData object is available.
85 const TargetData *getTargetData() const { return TD; }
87 /// getTypeStoreSize - Return the TargetData store size for the given type,
88 /// if known, or a conservative value otherwise.
90 uint64_t getTypeStoreSize(const Type *Ty);
92 //===--------------------------------------------------------------------===//
96 /// Location - A description of a memory location.
98 /// Ptr - The address of the start of the location.
100 /// Size - The maximum size of the location, in address-units, or
101 /// UnknownSize if the size is not known. Note that an unknown size does
102 /// not mean the pointer aliases the entire virtual address space, because
103 /// there are restrictions on stepping out of one object and into another.
104 /// See http://llvm.org/docs/LangRef.html#pointeraliasing
106 /// TBAATag - The metadata node which describes the TBAA type of
107 /// the location, or null if there is no known unique tag.
108 const MDNode *TBAATag;
110 explicit Location(const Value *P = 0,
111 uint64_t S = UnknownSize,
113 : Ptr(P), Size(S), TBAATag(N) {}
115 Location getWithNewPtr(const Value *NewPtr) const {
116 Location Copy(*this);
121 Location getWithNewSize(uint64_t NewSize) const {
122 Location Copy(*this);
127 Location getWithoutTBAATag() const {
128 Location Copy(*this);
134 /// getLocation - Fill in Loc with information about the memory reference by
135 /// the given instruction.
136 Location getLocation(const LoadInst *LI);
137 Location getLocation(const StoreInst *SI);
138 Location getLocation(const VAArgInst *VI);
139 Location getLocationForSource(const MemTransferInst *MTI);
140 Location getLocationForDest(const MemTransferInst *MTI);
142 /// Alias analysis result - Either we know for sure that it does not alias, we
143 /// know for sure it must alias, or we don't know anything: The two pointers
144 /// _might_ alias. This enum is designed so you can do things like:
145 /// if (AA.alias(P1, P2)) { ... }
146 /// to check to see if two pointers might alias.
148 /// See docs/AliasAnalysis.html for more information on the specific meanings
152 NoAlias = 0, ///< No dependencies.
153 MayAlias = 1, ///< Anything goes.
154 MustAlias = 2 ///< Pointers are equal.
157 /// alias - The main low level interface to the alias analysis implementation.
158 /// Returns an AliasResult indicating whether the two pointers are aliased to
159 /// each other. This is the interface that must be implemented by specific
160 /// alias analysis implementations.
161 virtual AliasResult alias(const Location &LocA, const Location &LocB);
163 /// alias - A convenience wrapper.
164 AliasResult alias(const Value *V1, uint64_t V1Size,
165 const Value *V2, uint64_t V2Size) {
166 return alias(Location(V1, V1Size), Location(V2, V2Size));
169 /// alias - A convenience wrapper.
170 AliasResult alias(const Value *V1, const Value *V2) {
171 return alias(V1, UnknownSize, V2, UnknownSize);
174 /// isNoAlias - A trivial helper function to check to see if the specified
175 /// pointers are no-alias.
176 bool isNoAlias(const Location &LocA, const Location &LocB) {
177 return alias(LocA, LocB) == NoAlias;
180 /// isNoAlias - A convenience wrapper.
181 bool isNoAlias(const Value *V1, uint64_t V1Size,
182 const Value *V2, uint64_t V2Size) {
183 return isNoAlias(Location(V1, V1Size), Location(V2, V2Size));
186 /// pointsToConstantMemory - If the specified memory location is
187 /// known to be constant, return true. If OrLocal is true and the
188 /// specified memory location is known to be "local" (derived from
189 /// an alloca), return true. Otherwise return false.
190 virtual bool pointsToConstantMemory(const Location &Loc,
191 bool OrLocal = false);
193 /// pointsToConstantMemory - A convenient wrapper.
194 bool pointsToConstantMemory(const Value *P, bool OrLocal = false) {
195 return pointsToConstantMemory(Location(P), OrLocal);
198 //===--------------------------------------------------------------------===//
199 /// Simple mod/ref information...
202 /// ModRefResult - Represent the result of a mod/ref query. Mod and Ref are
203 /// bits which may be or'd together.
205 enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 };
207 /// These values define additional bits used to define the
208 /// ModRefBehavior values.
209 enum { Nowhere = 0, ArgumentPointees = 4, Anywhere = 8 | ArgumentPointees };
211 /// ModRefBehavior - Summary of how a function affects memory in the program.
212 /// Loads from constant globals are not considered memory accesses for this
213 /// interface. Also, functions may freely modify stack space local to their
214 /// invocation without having to report it through these interfaces.
215 enum ModRefBehavior {
216 /// DoesNotAccessMemory - This function does not perform any non-local loads
217 /// or stores to memory.
219 /// This property corresponds to the GCC 'const' attribute.
220 /// This property corresponds to the LLVM IR 'readnone' attribute.
221 /// This property corresponds to the IntrNoMem LLVM intrinsic flag.
222 DoesNotAccessMemory = Nowhere | NoModRef,
224 /// OnlyReadsArgumentPointees - The only memory references in this function
225 /// (if it has any) are non-volatile loads from objects pointed to by its
226 /// pointer-typed arguments, with arbitrary offsets.
228 /// This property corresponds to the IntrReadArgMem LLVM intrinsic flag.
229 OnlyReadsArgumentPointees = ArgumentPointees | Ref,
231 /// OnlyAccessesArgumentPointees - The only memory references in this
232 /// function (if it has any) are non-volatile loads and stores from objects
233 /// pointed to by its pointer-typed arguments, with arbitrary offsets.
235 /// This property corresponds to the IntrReadWriteArgMem LLVM intrinsic flag.
236 OnlyAccessesArgumentPointees = ArgumentPointees | ModRef,
238 /// OnlyReadsMemory - This function does not perform any non-local stores or
239 /// volatile loads, but may read from any memory location.
241 /// This property corresponds to the GCC 'pure' attribute.
242 /// This property corresponds to the LLVM IR 'readonly' attribute.
243 /// This property corresponds to the IntrReadMem LLVM intrinsic flag.
244 OnlyReadsMemory = Anywhere | Ref,
246 /// UnknownModRefBehavior - This indicates that the function could not be
247 /// classified into one of the behaviors above.
248 UnknownModRefBehavior = Anywhere | ModRef
251 /// getModRefBehavior - Return the behavior when calling the given call site.
252 virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS);
254 /// getModRefBehavior - Return the behavior when calling the given function.
255 /// For use when the call site is not known.
256 virtual ModRefBehavior getModRefBehavior(const Function *F);
258 /// doesNotAccessMemory - If the specified call is known to never read or
259 /// write memory, return true. If the call only reads from known-constant
260 /// memory, it is also legal to return true. Calls that unwind the stack
261 /// are legal for this predicate.
263 /// Many optimizations (such as CSE and LICM) can be performed on such calls
264 /// without worrying about aliasing properties, and many calls have this
265 /// property (e.g. calls to 'sin' and 'cos').
267 /// This property corresponds to the GCC 'const' attribute.
269 bool doesNotAccessMemory(ImmutableCallSite CS) {
270 return getModRefBehavior(CS) == DoesNotAccessMemory;
273 /// doesNotAccessMemory - If the specified function is known to never read or
274 /// write memory, return true. For use when the call site is not known.
276 bool doesNotAccessMemory(const Function *F) {
277 return getModRefBehavior(F) == DoesNotAccessMemory;
280 /// onlyReadsMemory - If the specified call is known to only read from
281 /// non-volatile memory (or not access memory at all), return true. Calls
282 /// that unwind the stack are legal for this predicate.
284 /// This property allows many common optimizations to be performed in the
285 /// absence of interfering store instructions, such as CSE of strlen calls.
287 /// This property corresponds to the GCC 'pure' attribute.
289 bool onlyReadsMemory(ImmutableCallSite CS) {
290 return onlyReadsMemory(getModRefBehavior(CS));
293 /// onlyReadsMemory - If the specified function is known to only read from
294 /// non-volatile memory (or not access memory at all), return true. For use
295 /// when the call site is not known.
297 bool onlyReadsMemory(const Function *F) {
298 return onlyReadsMemory(getModRefBehavior(F));
301 /// onlyReadsMemory - Return true if functions with the specified behavior are
302 /// known to only read from non-volatile memory (or not access memory at all).
304 static bool onlyReadsMemory(ModRefBehavior MRB) {
308 /// onlyAccessesArgPointees - Return true if functions with the specified
309 /// behavior are known to read and write at most from objects pointed to by
310 /// their pointer-typed arguments (with arbitrary offsets).
312 static bool onlyAccessesArgPointees(ModRefBehavior MRB) {
313 return !(MRB & Anywhere & ~ArgumentPointees);
316 /// doesAccessArgPointees - Return true if functions with the specified
317 /// behavior are known to potentially read or write from objects pointed
318 /// to be their pointer-typed arguments (with arbitrary offsets).
320 static bool doesAccessArgPointees(ModRefBehavior MRB) {
321 return (MRB & ModRef) && (MRB & ArgumentPointees);
324 /// getModRefInfo - Return information about whether or not an instruction may
325 /// read or write the specified memory location. An instruction
326 /// that doesn't read or write memory may be trivially LICM'd for example.
327 ModRefResult getModRefInfo(const Instruction *I,
328 const Location &Loc) {
329 switch (I->getOpcode()) {
330 case Instruction::VAArg: return getModRefInfo((const VAArgInst*)I, Loc);
331 case Instruction::Load: return getModRefInfo((const LoadInst*)I, Loc);
332 case Instruction::Store: return getModRefInfo((const StoreInst*)I, Loc);
333 case Instruction::Call: return getModRefInfo((const CallInst*)I, Loc);
334 case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,Loc);
335 default: return NoModRef;
339 /// getModRefInfo - A convenience wrapper.
340 ModRefResult getModRefInfo(const Instruction *I,
341 const Value *P, uint64_t Size) {
342 return getModRefInfo(I, Location(P, Size));
345 /// getModRefInfo (for call sites) - Return whether information about whether
346 /// a particular call site modifies or reads the specified memory location.
347 virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
348 const Location &Loc);
350 /// getModRefInfo (for call sites) - A convenience wrapper.
351 ModRefResult getModRefInfo(ImmutableCallSite CS,
352 const Value *P, uint64_t Size) {
353 return getModRefInfo(CS, Location(P, Size));
356 /// getModRefInfo (for calls) - Return whether information about whether
357 /// a particular call modifies or reads the specified memory location.
358 ModRefResult getModRefInfo(const CallInst *C, const Location &Loc) {
359 return getModRefInfo(ImmutableCallSite(C), Loc);
362 /// getModRefInfo (for calls) - A convenience wrapper.
363 ModRefResult getModRefInfo(const CallInst *C, const Value *P, uint64_t Size) {
364 return getModRefInfo(C, Location(P, Size));
367 /// getModRefInfo (for invokes) - Return whether information about whether
368 /// a particular invoke modifies or reads the specified memory location.
369 ModRefResult getModRefInfo(const InvokeInst *I,
370 const Location &Loc) {
371 return getModRefInfo(ImmutableCallSite(I), Loc);
374 /// getModRefInfo (for invokes) - A convenience wrapper.
375 ModRefResult getModRefInfo(const InvokeInst *I,
376 const Value *P, uint64_t Size) {
377 return getModRefInfo(I, Location(P, Size));
380 /// getModRefInfo (for loads) - Return whether information about whether
381 /// a particular load modifies or reads the specified memory location.
382 ModRefResult getModRefInfo(const LoadInst *L, const Location &Loc);
384 /// getModRefInfo (for loads) - A convenience wrapper.
385 ModRefResult getModRefInfo(const LoadInst *L, const Value *P, uint64_t Size) {
386 return getModRefInfo(L, Location(P, Size));
389 /// getModRefInfo (for stores) - Return whether information about whether
390 /// a particular store modifies or reads the specified memory location.
391 ModRefResult getModRefInfo(const StoreInst *S, const Location &Loc);
393 /// getModRefInfo (for stores) - A convenience wrapper.
394 ModRefResult getModRefInfo(const StoreInst *S, const Value *P, uint64_t Size) {
395 return getModRefInfo(S, Location(P, Size));
398 /// getModRefInfo (for va_args) - Return whether information about whether
399 /// a particular va_arg modifies or reads the specified memory location.
400 ModRefResult getModRefInfo(const VAArgInst* I, const Location &Loc);
402 /// getModRefInfo (for va_args) - A convenience wrapper.
403 ModRefResult getModRefInfo(const VAArgInst* I, const Value* P, uint64_t Size) {
404 return getModRefInfo(I, Location(P, Size));
407 /// getModRefInfo - Return information about whether two call sites may refer
408 /// to the same set of memory locations. See
409 /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo
411 virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
412 ImmutableCallSite CS2);
414 //===--------------------------------------------------------------------===//
415 /// Higher level methods for querying mod/ref information.
418 /// canBasicBlockModify - Return true if it is possible for execution of the
419 /// specified basic block to modify the value pointed to by Ptr.
420 bool canBasicBlockModify(const BasicBlock &BB, const Location &Loc);
422 /// canBasicBlockModify - A convenience wrapper.
423 bool canBasicBlockModify(const BasicBlock &BB, const Value *P, uint64_t Size){
424 return canBasicBlockModify(BB, Location(P, Size));
427 /// canInstructionRangeModify - Return true if it is possible for the
428 /// execution of the specified instructions to modify the value pointed to by
429 /// Ptr. The instructions to consider are all of the instructions in the
430 /// range of [I1,I2] INCLUSIVE. I1 and I2 must be in the same basic block.
431 bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
432 const Location &Loc);
434 /// canInstructionRangeModify - A convenience wrapper.
435 bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
436 const Value *Ptr, uint64_t Size) {
437 return canInstructionRangeModify(I1, I2, Location(Ptr, Size));
440 //===--------------------------------------------------------------------===//
441 /// Methods that clients should call when they transform the program to allow
442 /// alias analyses to update their internal data structures. Note that these
443 /// methods may be called on any instruction, regardless of whether or not
444 /// they have pointer-analysis implications.
447 /// deleteValue - This method should be called whenever an LLVM Value is
448 /// deleted from the program, for example when an instruction is found to be
449 /// redundant and is eliminated.
451 virtual void deleteValue(Value *V);
453 /// copyValue - This method should be used whenever a preexisting value in the
454 /// program is copied or cloned, introducing a new value. Note that analysis
455 /// implementations should tolerate clients that use this method to introduce
456 /// the same value multiple times: if the analysis already knows about a
457 /// value, it should ignore the request.
459 virtual void copyValue(Value *From, Value *To);
461 /// replaceWithNewValue - This method is the obvious combination of the two
462 /// above, and it provided as a helper to simplify client code.
464 void replaceWithNewValue(Value *Old, Value *New) {
470 /// isNoAliasCall - Return true if this pointer is returned by a noalias
472 bool isNoAliasCall(const Value *V);
474 /// isIdentifiedObject - Return true if this pointer refers to a distinct and
475 /// identifiable object. This returns true for:
476 /// Global Variables and Functions (but not Global Aliases)
477 /// Allocas and Mallocs
478 /// ByVal and NoAlias Arguments
481 bool isIdentifiedObject(const Value *V);
483 } // End llvm namespace