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