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