AliasAnalysis.h revision 8e78cc4e130a8773cc8a2be2a94c4a97317ac383
1//===- llvm/Analysis/AliasAnalysis.h - Alias Analysis Interface -*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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.
14//
15// Implementations of this interface must implement the various virtual methods,
16// which automatically provides functionality for the entire suite of client
17// APIs.
18//
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.
24//
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.
34//
35//===----------------------------------------------------------------------===//
36
37#ifndef LLVM_ANALYSIS_ALIAS_ANALYSIS_H
38#define LLVM_ANALYSIS_ALIAS_ANALYSIS_H
39
40#include "llvm/Support/CallSite.h"
41#include <vector>
42
43namespace llvm {
44
45class LoadInst;
46class StoreInst;
47class VAArgInst;
48class TargetData;
49class Pass;
50class AnalysisUsage;
51
52class AliasAnalysis {
53protected:
54  const TargetData *TD;
55
56private:
57  AliasAnalysis *AA;       // Previous Alias Analysis to chain to.
58
59protected:
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.
64  ///
65  void InitializeAliasAnalysis(Pass *P);
66
67  /// getAnalysisUsage - All alias analysis implementations should invoke this
68  /// directly (using AliasAnalysis::getAnalysisUsage(AU)).
69  virtual void getAnalysisUsage(AnalysisUsage &AU) const;
70
71public:
72  static char ID; // Class identification, replacement for typeinfo
73  AliasAnalysis() : TD(0), AA(0) {}
74  virtual ~AliasAnalysis();  // We want to be subclassed
75
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);
80
81  /// getTargetData - Return a pointer to the current TargetData object, or
82  /// null if no TargetData object is available.
83  ///
84  const TargetData *getTargetData() const { return TD; }
85
86  /// getTypeStoreSize - Return the TargetData store size for the given type,
87  /// if known, or a conservative value otherwise.
88  ///
89  uint64_t getTypeStoreSize(const Type *Ty);
90
91  //===--------------------------------------------------------------------===//
92  /// Alias Queries...
93  ///
94
95  /// Location - A description of a memory location.
96  struct Location {
97    /// Ptr - The address of the start of the location.
98    const Value *Ptr;
99    /// Size - The maximum size of the location, or UnknownSize if the size is
100    /// not known.  Note that an unknown size does not mean the pointer aliases
101    /// the entire virtual address space, because there are restrictions on
102    /// stepping out of one object and into another.
103    /// See http://llvm.org/docs/LangRef.html#pointeraliasing
104    uint64_t Size;
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;
108
109    explicit Location(const Value *P = 0,
110                      uint64_t S = UnknownSize,
111                      const MDNode *N = 0)
112      : Ptr(P), Size(S), TBAATag(N) {}
113
114    Location getWithNewPtr(const Value *NewPtr) const {
115      Location Copy(*this);
116      Copy.Ptr = NewPtr;
117      return Copy;
118    }
119
120    Location getWithNewSize(uint64_t NewSize) const {
121      Location Copy(*this);
122      Copy.Size = NewSize;
123      return Copy;
124    }
125
126    Location getWithoutTBAATag() const {
127      Location Copy(*this);
128      Copy.TBAATag = 0;
129      return Copy;
130    }
131  };
132
133  /// Alias analysis result - Either we know for sure that it does not alias, we
134  /// know for sure it must alias, or we don't know anything: The two pointers
135  /// _might_ alias.  This enum is designed so you can do things like:
136  ///     if (AA.alias(P1, P2)) { ... }
137  /// to check to see if two pointers might alias.
138  ///
139  /// See docs/AliasAnalysis.html for more information on the specific meanings
140  /// of these values.
141  ///
142  enum AliasResult {
143    NoAlias = 0,        ///< No dependencies.
144    MayAlias = 1,       ///< Anything goes.
145    MustAlias = 2       ///< Pointers are equal.
146  };
147
148  /// alias - The main low level interface to the alias analysis implementation.
149  /// Returns an AliasResult indicating whether the two pointers are aliased to
150  /// each other.  This is the interface that must be implemented by specific
151  /// alias analysis implementations.
152  virtual AliasResult alias(const Location &LocA, const Location &LocB);
153
154  /// alias - A convenience wrapper.
155  AliasResult alias(const Value *V1, uint64_t V1Size,
156                    const Value *V2, uint64_t V2Size) {
157    return alias(Location(V1, V1Size), Location(V2, V2Size));
158  }
159
160  /// alias - A convenience wrapper.
161  AliasResult alias(const Value *V1, const Value *V2) {
162    return alias(V1, UnknownSize, V2, UnknownSize);
163  }
164
165  /// isNoAlias - A trivial helper function to check to see if the specified
166  /// pointers are no-alias.
167  bool isNoAlias(const Location &LocA, const Location &LocB) {
168    return alias(LocA, LocB) == NoAlias;
169  }
170
171  /// isNoAlias - A convenience wrapper.
172  bool isNoAlias(const Value *V1, uint64_t V1Size,
173                 const Value *V2, uint64_t V2Size) {
174    return isNoAlias(Location(V1, V1Size), Location(V2, V2Size));
175  }
176
177  /// pointsToConstantMemory - If the specified memory location is
178  /// known to be constant, return true. If OrLocal is true and the
179  /// specified memory location is known to be "local" (derived from
180  /// an alloca), return true. Otherwise return false.
181  virtual bool pointsToConstantMemory(const Location &Loc,
182                                      bool OrLocal = false);
183
184  /// pointsToConstantMemory - A convenient wrapper.
185  bool pointsToConstantMemory(const Value *P, bool OrLocal = false) {
186    return pointsToConstantMemory(Location(P), OrLocal);
187  }
188
189  //===--------------------------------------------------------------------===//
190  /// Simple mod/ref information...
191  ///
192
193  /// ModRefResult - Represent the result of a mod/ref query.  Mod and Ref are
194  /// bits which may be or'd together.
195  ///
196  enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 };
197
198  /// These values define additional bits used to define the
199  /// ModRefBehavior values.
200  enum { Nowhere = 0, ArgumentPointees = 4, Anywhere = 8 | ArgumentPointees };
201
202  /// ModRefBehavior - Summary of how a function affects memory in the program.
203  /// Loads from constant globals are not considered memory accesses for this
204  /// interface.  Also, functions may freely modify stack space local to their
205  /// invocation without having to report it through these interfaces.
206  enum ModRefBehavior {
207    /// DoesNotAccessMemory - This function does not perform any non-local loads
208    /// or stores to memory.
209    ///
210    /// This property corresponds to the GCC 'const' attribute.
211    /// This property corresponds to the LLVM IR 'readnone' attribute.
212    /// This property corresponds to the IntrNoMem LLVM intrinsic flag.
213    DoesNotAccessMemory = Nowhere | NoModRef,
214
215    /// OnlyReadsArgumentPointees - The only memory references in this function
216    /// (if it has any) are non-volatile loads from objects pointed to by its
217    /// pointer-typed arguments, with arbitrary offsets.
218    ///
219    /// This property corresponds to the IntrReadArgMem LLVM intrinsic flag.
220    OnlyReadsArgumentPointees = ArgumentPointees | Ref,
221
222    /// OnlyAccessesArgumentPointees - The only memory references in this
223    /// function (if it has any) are non-volatile loads and stores from objects
224    /// pointed to by its pointer-typed arguments, with arbitrary offsets.
225    ///
226    /// This property corresponds to the IntrReadWriteArgMem LLVM intrinsic flag.
227    OnlyAccessesArgumentPointees = ArgumentPointees | ModRef,
228
229    /// OnlyReadsMemory - This function does not perform any non-local stores or
230    /// volatile loads, but may read from any memory location.
231    ///
232    /// This property corresponds to the GCC 'pure' attribute.
233    /// This property corresponds to the LLVM IR 'readonly' attribute.
234    /// This property corresponds to the IntrReadMem LLVM intrinsic flag.
235    OnlyReadsMemory = Anywhere | Ref,
236
237    /// UnknownModRefBehavior - This indicates that the function could not be
238    /// classified into one of the behaviors above.
239    UnknownModRefBehavior = Anywhere | ModRef
240  };
241
242  /// getModRefBehavior - Return the behavior when calling the given call site.
243  virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS);
244
245  /// getModRefBehavior - Return the behavior when calling the given function.
246  /// For use when the call site is not known.
247  virtual ModRefBehavior getModRefBehavior(const Function *F);
248
249  /// doesNotAccessMemory - If the specified call is known to never read or
250  /// write memory, return true.  If the call only reads from known-constant
251  /// memory, it is also legal to return true.  Calls that unwind the stack
252  /// are legal for this predicate.
253  ///
254  /// Many optimizations (such as CSE and LICM) can be performed on such calls
255  /// without worrying about aliasing properties, and many calls have this
256  /// property (e.g. calls to 'sin' and 'cos').
257  ///
258  /// This property corresponds to the GCC 'const' attribute.
259  ///
260  bool doesNotAccessMemory(ImmutableCallSite CS) {
261    return getModRefBehavior(CS) == DoesNotAccessMemory;
262  }
263
264  /// doesNotAccessMemory - If the specified function is known to never read or
265  /// write memory, return true.  For use when the call site is not known.
266  ///
267  bool doesNotAccessMemory(const Function *F) {
268    return getModRefBehavior(F) == DoesNotAccessMemory;
269  }
270
271  /// onlyReadsMemory - If the specified call is known to only read from
272  /// non-volatile memory (or not access memory at all), return true.  Calls
273  /// that unwind the stack are legal for this predicate.
274  ///
275  /// This property allows many common optimizations to be performed in the
276  /// absence of interfering store instructions, such as CSE of strlen calls.
277  ///
278  /// This property corresponds to the GCC 'pure' attribute.
279  ///
280  bool onlyReadsMemory(ImmutableCallSite CS) {
281    return onlyReadsMemory(getModRefBehavior(CS));
282  }
283
284  /// onlyReadsMemory - If the specified function is known to only read from
285  /// non-volatile memory (or not access memory at all), return true.  For use
286  /// when the call site is not known.
287  ///
288  bool onlyReadsMemory(const Function *F) {
289    return onlyReadsMemory(getModRefBehavior(F));
290  }
291
292  /// onlyReadsMemory - Return true if functions with the specified behavior are
293  /// known to only read from non-volatile memory (or not access memory at all).
294  ///
295  static bool onlyReadsMemory(ModRefBehavior MRB) {
296    return !(MRB & Mod);
297  }
298
299  /// onlyAccessesArgPointees - Return true if functions with the specified
300  /// behavior are known to read and write at most from objects pointed to by
301  /// their pointer-typed arguments (with arbitrary offsets).
302  ///
303  static bool onlyAccessesArgPointees(ModRefBehavior MRB) {
304    return !(MRB & Anywhere & ~ArgumentPointees);
305  }
306
307  /// doesAccessArgPointees - Return true if functions with the specified
308  /// behavior are known to potentially read or write  from objects pointed
309  /// to be their pointer-typed arguments (with arbitrary offsets).
310  ///
311  static bool doesAccessArgPointees(ModRefBehavior MRB) {
312    return (MRB & ModRef) && (MRB & ArgumentPointees);
313  }
314
315  /// getModRefInfo - Return information about whether or not an instruction may
316  /// read or write the specified memory location.  An instruction
317  /// that doesn't read or write memory may be trivially LICM'd for example.
318  ModRefResult getModRefInfo(const Instruction *I,
319                             const Location &Loc) {
320    switch (I->getOpcode()) {
321    case Instruction::VAArg:  return getModRefInfo((const VAArgInst*)I, Loc);
322    case Instruction::Load:   return getModRefInfo((const LoadInst*)I,  Loc);
323    case Instruction::Store:  return getModRefInfo((const StoreInst*)I, Loc);
324    case Instruction::Call:   return getModRefInfo((const CallInst*)I,  Loc);
325    case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,Loc);
326    default:                  return NoModRef;
327    }
328  }
329
330  /// getModRefInfo - A convenience wrapper.
331  ModRefResult getModRefInfo(const Instruction *I,
332                             const Value *P, uint64_t Size) {
333    return getModRefInfo(I, Location(P, Size));
334  }
335
336  /// getModRefInfo (for call sites) - Return whether information about whether
337  /// a particular call site modifies or reads the specified memory location.
338  virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
339                                     const Location &Loc);
340
341  /// getModRefInfo (for call sites) - A convenience wrapper.
342  ModRefResult getModRefInfo(ImmutableCallSite CS,
343                             const Value *P, uint64_t Size) {
344    return getModRefInfo(CS, Location(P, Size));
345  }
346
347  /// getModRefInfo (for calls) - Return whether information about whether
348  /// a particular call modifies or reads the specified memory location.
349  ModRefResult getModRefInfo(const CallInst *C, const Location &Loc) {
350    return getModRefInfo(ImmutableCallSite(C), Loc);
351  }
352
353  /// getModRefInfo (for calls) - A convenience wrapper.
354  ModRefResult getModRefInfo(const CallInst *C, const Value *P, uint64_t Size) {
355    return getModRefInfo(C, Location(P, Size));
356  }
357
358  /// getModRefInfo (for invokes) - Return whether information about whether
359  /// a particular invoke modifies or reads the specified memory location.
360  ModRefResult getModRefInfo(const InvokeInst *I,
361                             const Location &Loc) {
362    return getModRefInfo(ImmutableCallSite(I), Loc);
363  }
364
365  /// getModRefInfo (for invokes) - A convenience wrapper.
366  ModRefResult getModRefInfo(const InvokeInst *I,
367                             const Value *P, uint64_t Size) {
368    return getModRefInfo(I, Location(P, Size));
369  }
370
371  /// getModRefInfo (for loads) - Return whether information about whether
372  /// a particular load modifies or reads the specified memory location.
373  ModRefResult getModRefInfo(const LoadInst *L, const Location &Loc);
374
375  /// getModRefInfo (for loads) - A convenience wrapper.
376  ModRefResult getModRefInfo(const LoadInst *L, const Value *P, uint64_t Size) {
377    return getModRefInfo(L, Location(P, Size));
378  }
379
380  /// getModRefInfo (for stores) - Return whether information about whether
381  /// a particular store modifies or reads the specified memory location.
382  ModRefResult getModRefInfo(const StoreInst *S, const Location &Loc);
383
384  /// getModRefInfo (for stores) - A convenience wrapper.
385  ModRefResult getModRefInfo(const StoreInst *S, const Value *P, uint64_t Size) {
386    return getModRefInfo(S, Location(P, Size));
387  }
388
389  /// getModRefInfo (for va_args) - Return whether information about whether
390  /// a particular va_arg modifies or reads the specified memory location.
391  ModRefResult getModRefInfo(const VAArgInst* I, const Location &Loc);
392
393  /// getModRefInfo (for va_args) - A convenience wrapper.
394  ModRefResult getModRefInfo(const VAArgInst* I, const Value* P, uint64_t Size) {
395    return getModRefInfo(I, Location(P, Size));
396  }
397
398  /// getModRefInfo - Return information about whether two call sites may refer
399  /// to the same set of memory locations.  See
400  ///   http://llvm.org/docs/AliasAnalysis.html#ModRefInfo
401  /// for details.
402  virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
403                                     ImmutableCallSite CS2);
404
405  //===--------------------------------------------------------------------===//
406  /// Higher level methods for querying mod/ref information.
407  ///
408
409  /// canBasicBlockModify - Return true if it is possible for execution of the
410  /// specified basic block to modify the value pointed to by Ptr.
411  bool canBasicBlockModify(const BasicBlock &BB, const Location &Loc);
412
413  /// canBasicBlockModify - A convenience wrapper.
414  bool canBasicBlockModify(const BasicBlock &BB, const Value *P, uint64_t Size){
415    return canBasicBlockModify(BB, Location(P, Size));
416  }
417
418  /// canInstructionRangeModify - Return true if it is possible for the
419  /// execution of the specified instructions to modify the value pointed to by
420  /// Ptr.  The instructions to consider are all of the instructions in the
421  /// range of [I1,I2] INCLUSIVE.  I1 and I2 must be in the same basic block.
422  bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
423                                 const Location &Loc);
424
425  /// canInstructionRangeModify - A convenience wrapper.
426  bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
427                                 const Value *Ptr, uint64_t Size) {
428    return canInstructionRangeModify(I1, I2, Location(Ptr, Size));
429  }
430
431  //===--------------------------------------------------------------------===//
432  /// Methods that clients should call when they transform the program to allow
433  /// alias analyses to update their internal data structures.  Note that these
434  /// methods may be called on any instruction, regardless of whether or not
435  /// they have pointer-analysis implications.
436  ///
437
438  /// deleteValue - This method should be called whenever an LLVM Value is
439  /// deleted from the program, for example when an instruction is found to be
440  /// redundant and is eliminated.
441  ///
442  virtual void deleteValue(Value *V);
443
444  /// copyValue - This method should be used whenever a preexisting value in the
445  /// program is copied or cloned, introducing a new value.  Note that analysis
446  /// implementations should tolerate clients that use this method to introduce
447  /// the same value multiple times: if the analysis already knows about a
448  /// value, it should ignore the request.
449  ///
450  virtual void copyValue(Value *From, Value *To);
451
452  /// replaceWithNewValue - This method is the obvious combination of the two
453  /// above, and it provided as a helper to simplify client code.
454  ///
455  void replaceWithNewValue(Value *Old, Value *New) {
456    copyValue(Old, New);
457    deleteValue(Old);
458  }
459};
460
461/// isNoAliasCall - Return true if this pointer is returned by a noalias
462/// function.
463bool isNoAliasCall(const Value *V);
464
465/// isIdentifiedObject - Return true if this pointer refers to a distinct and
466/// identifiable object.  This returns true for:
467///    Global Variables and Functions (but not Global Aliases)
468///    Allocas and Mallocs
469///    ByVal and NoAlias Arguments
470///    NoAlias returns
471///
472bool isIdentifiedObject(const Value *V);
473
474} // End llvm namespace
475
476#endif
477