AliasAnalysis.h revision 66e08cf79a052dd75bf6fa2f94abd4c0a18cb019
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 represents memory as a (Pointer, Size) pair.  The Pointer component
20// specifies the base memory address of the region, the Size specifies how large
21// of an area is being queried.  If Size is 0, two pointers only alias if they
22// are exactly equal.  If size is greater than zero, but small, the two pointers
23// alias if the areas pointed to overlap.  If the size is very large (ie, ~0U),
24// then the two pointers alias if they may be pointing to components of the same
25// memory object.  Pointers that point to two completely different objects in
26// memory never alias, regardless of the value of the Size component.
27//
28//===----------------------------------------------------------------------===//
29
30#ifndef LLVM_ANALYSIS_ALIAS_ANALYSIS_H
31#define LLVM_ANALYSIS_ALIAS_ANALYSIS_H
32
33#include "llvm/Support/CallSite.h"
34#include "llvm/System/IncludeFile.h"
35#include <vector>
36
37namespace llvm {
38
39class LoadInst;
40class StoreInst;
41class VAArgInst;
42class TargetData;
43class Pass;
44class AnalysisUsage;
45
46class AliasAnalysis {
47protected:
48  const TargetData *TD;
49  AliasAnalysis *AA;       // Previous Alias Analysis to chain to.
50
51  /// InitializeAliasAnalysis - Subclasses must call this method to initialize
52  /// the AliasAnalysis interface before any other methods are called.  This is
53  /// typically called by the run* methods of these subclasses.  This may be
54  /// called multiple times.
55  ///
56  void InitializeAliasAnalysis(Pass *P);
57
58  /// getAnalysisUsage - All alias analysis implementations should invoke this
59  /// directly (using AliasAnalysis::getAnalysisUsage(AU)).
60  virtual void getAnalysisUsage(AnalysisUsage &AU) const;
61
62public:
63  static char ID; // Class identification, replacement for typeinfo
64  AliasAnalysis() : TD(0), AA(0) {}
65  virtual ~AliasAnalysis();  // We want to be subclassed
66
67  /// getTargetData - Return a pointer to the current TargetData object, or
68  /// null if no TargetData object is available.
69  ///
70  const TargetData *getTargetData() const { return TD; }
71
72  /// getTypeStoreSize - Return the TargetData store size for the given type,
73  /// if known, or a conservative value otherwise.
74  ///
75  unsigned getTypeStoreSize(const Type *Ty);
76
77  //===--------------------------------------------------------------------===//
78  /// Alias Queries...
79  ///
80
81  /// Alias analysis result - Either we know for sure that it does not alias, we
82  /// know for sure it must alias, or we don't know anything: The two pointers
83  /// _might_ alias.  This enum is designed so you can do things like:
84  ///     if (AA.alias(P1, P2)) { ... }
85  /// to check to see if two pointers might alias.
86  ///
87  enum AliasResult { NoAlias = 0, MayAlias = 1, MustAlias = 2 };
88
89  /// alias - The main low level interface to the alias analysis implementation.
90  /// Returns a Result indicating whether the two pointers are aliased to each
91  /// other.  This is the interface that must be implemented by specific alias
92  /// analysis implementations.
93  ///
94  virtual AliasResult alias(const Value *V1, unsigned V1Size,
95                            const Value *V2, unsigned V2Size);
96
97  /// pointsToConstantMemory - If the specified pointer is known to point into
98  /// constant global memory, return true.  This allows disambiguation of store
99  /// instructions from constant pointers.
100  ///
101  virtual bool pointsToConstantMemory(const Value *P);
102
103  //===--------------------------------------------------------------------===//
104  /// Simple mod/ref information...
105  ///
106
107  /// ModRefResult - Represent the result of a mod/ref query.  Mod and Ref are
108  /// bits which may be or'd together.
109  ///
110  enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 };
111
112
113  /// ModRefBehavior - Summary of how a function affects memory in the program.
114  /// Loads from constant globals are not considered memory accesses for this
115  /// interface.  Also, functions may freely modify stack space local to their
116  /// invocation without having to report it through these interfaces.
117  enum ModRefBehavior {
118    // DoesNotAccessMemory - This function does not perform any non-local loads
119    // or stores to memory.
120    //
121    // This property corresponds to the GCC 'const' attribute.
122    DoesNotAccessMemory,
123
124    // AccessesArguments - This function accesses function arguments in well
125    // known (possibly volatile) ways, but does not access any other memory.
126    //
127    // Clients may use the Info parameter of getModRefBehavior to get specific
128    // information about how pointer arguments are used.
129    AccessesArguments,
130
131    // AccessesArgumentsAndGlobals - This function has accesses function
132    // arguments and global variables well known (possibly volatile) ways, but
133    // does not access any other memory.
134    //
135    // Clients may use the Info parameter of getModRefBehavior to get specific
136    // information about how pointer arguments are used.
137    AccessesArgumentsAndGlobals,
138
139    // OnlyReadsMemory - This function does not perform any non-local stores or
140    // volatile loads, but may read from any memory location.
141    //
142    // This property corresponds to the GCC 'pure' attribute.
143    OnlyReadsMemory,
144
145    // UnknownModRefBehavior - This indicates that the function could not be
146    // classified into one of the behaviors above.
147    UnknownModRefBehavior
148  };
149
150  /// PointerAccessInfo - This struct is used to return results for pointers,
151  /// globals, and the return value of a function.
152  struct PointerAccessInfo {
153    /// V - The value this record corresponds to.  This may be an Argument for
154    /// the function, a GlobalVariable, or null, corresponding to the return
155    /// value for the function.
156    Value *V;
157
158    /// ModRefInfo - Whether the pointer is loaded or stored to/from.
159    ///
160    ModRefResult ModRefInfo;
161
162    /// AccessType - Specific fine-grained access information for the argument.
163    /// If none of these classifications is general enough, the
164    /// getModRefBehavior method should not return AccessesArguments*.  If a
165    /// record is not returned for a particular argument, the argument is never
166    /// dead and never dereferenced.
167    enum AccessType {
168      /// ScalarAccess - The pointer is dereferenced.
169      ///
170      ScalarAccess,
171
172      /// ArrayAccess - The pointer is indexed through as an array of elements.
173      ///
174      ArrayAccess,
175
176      /// ElementAccess ?? P->F only?
177
178      /// CallsThrough - Indirect calls are made through the specified function
179      /// pointer.
180      CallsThrough
181    };
182  };
183
184  /// getModRefBehavior - Return the behavior when calling the given call site.
185  virtual ModRefBehavior getModRefBehavior(CallSite CS,
186                                   std::vector<PointerAccessInfo> *Info = 0);
187
188  /// getModRefBehavior - Return the behavior when calling the given function.
189  /// For use when the call site is not known.
190  virtual ModRefBehavior getModRefBehavior(Function *F,
191                                   std::vector<PointerAccessInfo> *Info = 0);
192
193  /// doesNotAccessMemory - If the specified call is known to never read or
194  /// write memory, return true.  If the call only reads from known-constant
195  /// memory, it is also legal to return true.  Calls that unwind the stack
196  /// are legal for this predicate.
197  ///
198  /// Many optimizations (such as CSE and LICM) can be performed on such calls
199  /// without worrying about aliasing properties, and many calls have this
200  /// property (e.g. calls to 'sin' and 'cos').
201  ///
202  /// This property corresponds to the GCC 'const' attribute.
203  ///
204  bool doesNotAccessMemory(CallSite CS) {
205    return getModRefBehavior(CS) == DoesNotAccessMemory;
206  }
207
208  /// doesNotAccessMemory - If the specified function is known to never read or
209  /// write memory, return true.  For use when the call site is not known.
210  ///
211  bool doesNotAccessMemory(Function *F) {
212    return getModRefBehavior(F) == DoesNotAccessMemory;
213  }
214
215  /// onlyReadsMemory - If the specified call is known to only read from
216  /// non-volatile memory (or not access memory at all), return true.  Calls
217  /// that unwind the stack are legal for this predicate.
218  ///
219  /// This property allows many common optimizations to be performed in the
220  /// absence of interfering store instructions, such as CSE of strlen calls.
221  ///
222  /// This property corresponds to the GCC 'pure' attribute.
223  ///
224  bool onlyReadsMemory(CallSite CS) {
225    ModRefBehavior MRB = getModRefBehavior(CS);
226    return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory;
227  }
228
229  /// onlyReadsMemory - If the specified function is known to only read from
230  /// non-volatile memory (or not access memory at all), return true.  For use
231  /// when the call site is not known.
232  ///
233  bool onlyReadsMemory(Function *F) {
234    ModRefBehavior MRB = getModRefBehavior(F);
235    return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory;
236  }
237
238
239  /// getModRefInfo - Return information about whether or not an instruction may
240  /// read or write memory specified by the pointer operand.  An instruction
241  /// that doesn't read or write memory may be trivially LICM'd for example.
242
243  /// getModRefInfo (for call sites) - Return whether information about whether
244  /// a particular call site modifies or reads the memory specified by the
245  /// pointer.
246  ///
247  virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
248
249  /// getModRefInfo - Return information about whether two call sites may refer
250  /// to the same set of memory locations.  This function returns NoModRef if
251  /// the two calls refer to disjoint memory locations, Ref if CS1 reads memory
252  /// written by CS2, Mod if CS1 writes to memory read or written by CS2, or
253  /// ModRef if CS1 might read or write memory accessed by CS2.
254  ///
255  virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2);
256
257public:
258  /// Convenience functions...
259  ModRefResult getModRefInfo(LoadInst *L, Value *P, unsigned Size);
260  ModRefResult getModRefInfo(StoreInst *S, Value *P, unsigned Size);
261  ModRefResult getModRefInfo(CallInst *C, Value *P, unsigned Size) {
262    return getModRefInfo(CallSite(C), P, Size);
263  }
264  ModRefResult getModRefInfo(InvokeInst *I, Value *P, unsigned Size) {
265    return getModRefInfo(CallSite(I), P, Size);
266  }
267  ModRefResult getModRefInfo(VAArgInst* I, Value* P, unsigned Size) {
268    return AliasAnalysis::ModRef;
269  }
270  ModRefResult getModRefInfo(Instruction *I, Value *P, unsigned Size) {
271    switch (I->getOpcode()) {
272    case Instruction::VAArg:  return getModRefInfo((VAArgInst*)I, P, Size);
273    case Instruction::Load:   return getModRefInfo((LoadInst*)I, P, Size);
274    case Instruction::Store:  return getModRefInfo((StoreInst*)I, P, Size);
275    case Instruction::Call:   return getModRefInfo((CallInst*)I, P, Size);
276    case Instruction::Invoke: return getModRefInfo((InvokeInst*)I, P, Size);
277    default:                  return NoModRef;
278    }
279  }
280
281  //===--------------------------------------------------------------------===//
282  /// Higher level methods for querying mod/ref information.
283  ///
284
285  /// canBasicBlockModify - Return true if it is possible for execution of the
286  /// specified basic block to modify the value pointed to by Ptr.
287  ///
288  bool canBasicBlockModify(const BasicBlock &BB, const Value *P, unsigned Size);
289
290  /// canInstructionRangeModify - Return true if it is possible for the
291  /// execution of the specified instructions to modify the value pointed to by
292  /// Ptr.  The instructions to consider are all of the instructions in the
293  /// range of [I1,I2] INCLUSIVE.  I1 and I2 must be in the same basic block.
294  ///
295  bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
296                                 const Value *Ptr, unsigned Size);
297
298  //===--------------------------------------------------------------------===//
299  /// Methods that clients should call when they transform the program to allow
300  /// alias analyses to update their internal data structures.  Note that these
301  /// methods may be called on any instruction, regardless of whether or not
302  /// they have pointer-analysis implications.
303  ///
304
305  /// deleteValue - This method should be called whenever an LLVM Value is
306  /// deleted from the program, for example when an instruction is found to be
307  /// redundant and is eliminated.
308  ///
309  virtual void deleteValue(Value *V);
310
311  /// copyValue - This method should be used whenever a preexisting value in the
312  /// program is copied or cloned, introducing a new value.  Note that analysis
313  /// implementations should tolerate clients that use this method to introduce
314  /// the same value multiple times: if the analysis already knows about a
315  /// value, it should ignore the request.
316  ///
317  virtual void copyValue(Value *From, Value *To);
318
319  /// replaceWithNewValue - This method is the obvious combination of the two
320  /// above, and it provided as a helper to simplify client code.
321  ///
322  void replaceWithNewValue(Value *Old, Value *New) {
323    copyValue(Old, New);
324    deleteValue(Old);
325  }
326};
327
328/// isNoAliasCall - Return true if this pointer is returned by a noalias
329/// function.
330bool isNoAliasCall(const Value *V);
331
332/// isIdentifiedObject - Return true if this pointer refers to a distinct and
333/// identifiable object.  This returns true for:
334///    Global Variables and Functions (but not Global Aliases)
335///    Allocas and Mallocs
336///    ByVal and NoAlias Arguments
337///    NoAlias returns
338///
339bool isIdentifiedObject(const Value *V);
340
341} // End llvm namespace
342
343// Because of the way .a files work, we must force the BasicAA implementation to
344// be pulled in if the AliasAnalysis header is included.  Otherwise we run
345// the risk of AliasAnalysis being used, but the default implementation not
346// being linked into the tool that uses it.
347FORCE_DEFINING_FILE_TO_BE_LINKED(AliasAnalysis)
348FORCE_DEFINING_FILE_TO_BE_LINKED(BasicAliasAnalysis)
349
350#endif
351