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