Make AA private, since subclasses shouldn't (aren't don't) access it directly.
[oota-llvm.git] / include / llvm / Analysis / AliasAnalysis.h
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
34 namespace llvm {
35
36 class LoadInst;
37 class StoreInst;
38 class VAArgInst;
39 class TargetData;
40 class Pass;
41 class AnalysisUsage;
42
43 class AliasAnalysis {
44 protected:
45   const TargetData *TD;
46
47 private:
48   AliasAnalysis *AA;       // Previous Alias Analysis to chain to.
49
50 protected:
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
62 public:
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   /// UnknownSize - This is a special value which can be used with the
68   /// size arguments in alias queries to indicate that the caller does not
69   /// know the sizes of the potential memory references.
70   static unsigned const UnknownSize = ~0u;
71
72   /// getTargetData - Return a pointer to the current TargetData object, or
73   /// null if no TargetData object is available.
74   ///
75   const TargetData *getTargetData() const { return TD; }
76
77   /// getTypeStoreSize - Return the TargetData store size for the given type,
78   /// if known, or a conservative value otherwise.
79   ///
80   unsigned getTypeStoreSize(const Type *Ty);
81
82   //===--------------------------------------------------------------------===//
83   /// Alias Queries...
84   ///
85
86   /// Alias analysis result - Either we know for sure that it does not alias, we
87   /// know for sure it must alias, or we don't know anything: The two pointers
88   /// _might_ alias.  This enum is designed so you can do things like:
89   ///     if (AA.alias(P1, P2)) { ... }
90   /// to check to see if two pointers might alias.
91   ///
92   /// See docs/AliasAnalysis.html for more information on the specific meanings
93   /// of these values.
94   ///
95   enum AliasResult { NoAlias = 0, MayAlias = 1, MustAlias = 2 };
96
97   /// alias - The main low level interface to the alias analysis implementation.
98   /// Returns a Result indicating whether the two pointers are aliased to each
99   /// other.  This is the interface that must be implemented by specific alias
100   /// analysis implementations.
101   ///
102   virtual AliasResult alias(const Value *V1, unsigned V1Size,
103                             const Value *V2, unsigned V2Size);
104
105   /// alias - A convenience wrapper for the case where the sizes are unknown.
106   AliasResult alias(const Value *V1, const Value *V2) {
107     return alias(V1, UnknownSize, V2, UnknownSize);
108   }
109
110   /// isNoAlias - A trivial helper function to check to see if the specified
111   /// pointers are no-alias.
112   bool isNoAlias(const Value *V1, unsigned V1Size,
113                  const Value *V2, unsigned V2Size) {
114     return alias(V1, V1Size, V2, V2Size) == NoAlias;
115   }
116
117   /// pointsToConstantMemory - If the specified pointer is known to point into
118   /// constant global memory, return true.  This allows disambiguation of store
119   /// instructions from constant pointers.
120   ///
121   virtual bool pointsToConstantMemory(const Value *P);
122
123   //===--------------------------------------------------------------------===//
124   /// Simple mod/ref information...
125   ///
126
127   /// ModRefResult - Represent the result of a mod/ref query.  Mod and Ref are
128   /// bits which may be or'd together.
129   ///
130   enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 };
131
132
133   /// ModRefBehavior - Summary of how a function affects memory in the program.
134   /// Loads from constant globals are not considered memory accesses for this
135   /// interface.  Also, functions may freely modify stack space local to their
136   /// invocation without having to report it through these interfaces.
137   enum ModRefBehavior {
138     // DoesNotAccessMemory - This function does not perform any non-local loads
139     // or stores to memory.
140     //
141     // This property corresponds to the GCC 'const' attribute.
142     DoesNotAccessMemory,
143
144     // AccessesArguments - This function accesses function arguments in well
145     // known (possibly volatile) ways, but does not access any other memory.
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     AccessesArgumentsAndGlobals,
152
153     // OnlyReadsMemory - This function does not perform any non-local stores or
154     // volatile loads, but may read from any memory location.
155     //
156     // This property corresponds to the GCC 'pure' attribute.
157     OnlyReadsMemory,
158
159     // UnknownModRefBehavior - This indicates that the function could not be
160     // classified into one of the behaviors above.
161     UnknownModRefBehavior
162   };
163
164   /// getModRefBehavior - Return the behavior when calling the given call site.
165   virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS);
166
167   /// getModRefBehavior - Return the behavior when calling the given function.
168   /// For use when the call site is not known.
169   virtual ModRefBehavior getModRefBehavior(const Function *F);
170
171   /// getIntrinsicModRefBehavior - Return the modref behavior of the intrinsic
172   /// with the given id.
173   static ModRefBehavior getIntrinsicModRefBehavior(unsigned iid);
174
175   /// doesNotAccessMemory - If the specified call is known to never read or
176   /// write memory, return true.  If the call only reads from known-constant
177   /// memory, it is also legal to return true.  Calls that unwind the stack
178   /// are legal for this predicate.
179   ///
180   /// Many optimizations (such as CSE and LICM) can be performed on such calls
181   /// without worrying about aliasing properties, and many calls have this
182   /// property (e.g. calls to 'sin' and 'cos').
183   ///
184   /// This property corresponds to the GCC 'const' attribute.
185   ///
186   bool doesNotAccessMemory(ImmutableCallSite CS) {
187     return getModRefBehavior(CS) == DoesNotAccessMemory;
188   }
189
190   /// doesNotAccessMemory - If the specified function is known to never read or
191   /// write memory, return true.  For use when the call site is not known.
192   ///
193   bool doesNotAccessMemory(const Function *F) {
194     return getModRefBehavior(F) == DoesNotAccessMemory;
195   }
196
197   /// onlyReadsMemory - If the specified call is known to only read from
198   /// non-volatile memory (or not access memory at all), return true.  Calls
199   /// that unwind the stack are legal for this predicate.
200   ///
201   /// This property allows many common optimizations to be performed in the
202   /// absence of interfering store instructions, such as CSE of strlen calls.
203   ///
204   /// This property corresponds to the GCC 'pure' attribute.
205   ///
206   bool onlyReadsMemory(ImmutableCallSite CS) {
207     ModRefBehavior MRB = getModRefBehavior(CS);
208     return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory;
209   }
210
211   /// onlyReadsMemory - If the specified function is known to only read from
212   /// non-volatile memory (or not access memory at all), return true.  For use
213   /// when the call site is not known.
214   ///
215   bool onlyReadsMemory(const Function *F) {
216     ModRefBehavior MRB = getModRefBehavior(F);
217     return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory;
218   }
219
220
221   /// getModRefInfo - Return information about whether or not an instruction may
222   /// read or write memory specified by the pointer operand.  An instruction
223   /// that doesn't read or write memory may be trivially LICM'd for example.
224
225   /// getModRefInfo (for call sites) - Return whether information about whether
226   /// a particular call site modifies or reads the memory specified by the
227   /// pointer.
228   ///
229   virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
230                                      const Value *P, unsigned Size);
231
232   /// getModRefInfo - Return information about whether two call sites may refer
233   /// to the same set of memory locations.  This function returns NoModRef if
234   /// the two calls refer to disjoint memory locations, Ref if CS1 reads memory
235   /// written by CS2, Mod if CS1 writes to memory read or written by CS2, or
236   /// ModRef if CS1 might read or write memory accessed by CS2.
237   ///
238   virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
239                                      ImmutableCallSite CS2);
240
241 public:
242   /// Convenience functions...
243   ModRefResult getModRefInfo(const LoadInst *L, const Value *P, unsigned Size);
244   ModRefResult getModRefInfo(const StoreInst *S, const Value *P, unsigned Size);
245   ModRefResult getModRefInfo(const CallInst *C, const Value *P, unsigned Size) {
246     return getModRefInfo(ImmutableCallSite(C), P, Size);
247   }
248   ModRefResult getModRefInfo(const InvokeInst *I,
249                              const Value *P, unsigned Size) {
250     return getModRefInfo(ImmutableCallSite(I), P, Size);
251   }
252   ModRefResult getModRefInfo(const VAArgInst* I,
253                              const Value* P, unsigned Size) {
254     return AliasAnalysis::ModRef;
255   }
256   ModRefResult getModRefInfo(const Instruction *I,
257                              const Value *P, unsigned Size) {
258     switch (I->getOpcode()) {
259     case Instruction::VAArg:  return getModRefInfo((const VAArgInst*)I, P,Size);
260     case Instruction::Load:   return getModRefInfo((const LoadInst*)I, P, Size);
261     case Instruction::Store:  return getModRefInfo((const StoreInst*)I, P,Size);
262     case Instruction::Call:   return getModRefInfo((const CallInst*)I, P, Size);
263     case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,P,Size);
264     default:                  return NoModRef;
265     }
266   }
267
268   //===--------------------------------------------------------------------===//
269   /// Higher level methods for querying mod/ref information.
270   ///
271
272   /// canBasicBlockModify - Return true if it is possible for execution of the
273   /// specified basic block to modify the value pointed to by Ptr.
274   ///
275   bool canBasicBlockModify(const BasicBlock &BB, const Value *P, unsigned Size);
276
277   /// canInstructionRangeModify - Return true if it is possible for the
278   /// execution of the specified instructions to modify the value pointed to by
279   /// Ptr.  The instructions to consider are all of the instructions in the
280   /// range of [I1,I2] INCLUSIVE.  I1 and I2 must be in the same basic block.
281   ///
282   bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
283                                  const Value *Ptr, unsigned Size);
284
285   //===--------------------------------------------------------------------===//
286   /// Methods that clients should call when they transform the program to allow
287   /// alias analyses to update their internal data structures.  Note that these
288   /// methods may be called on any instruction, regardless of whether or not
289   /// they have pointer-analysis implications.
290   ///
291
292   /// deleteValue - This method should be called whenever an LLVM Value is
293   /// deleted from the program, for example when an instruction is found to be
294   /// redundant and is eliminated.
295   ///
296   virtual void deleteValue(Value *V);
297
298   /// copyValue - This method should be used whenever a preexisting value in the
299   /// program is copied or cloned, introducing a new value.  Note that analysis
300   /// implementations should tolerate clients that use this method to introduce
301   /// the same value multiple times: if the analysis already knows about a
302   /// value, it should ignore the request.
303   ///
304   virtual void copyValue(Value *From, Value *To);
305
306   /// replaceWithNewValue - This method is the obvious combination of the two
307   /// above, and it provided as a helper to simplify client code.
308   ///
309   void replaceWithNewValue(Value *Old, Value *New) {
310     copyValue(Old, New);
311     deleteValue(Old);
312   }
313 };
314
315 /// isNoAliasCall - Return true if this pointer is returned by a noalias
316 /// function.
317 bool isNoAliasCall(const Value *V);
318
319 /// isIdentifiedObject - Return true if this pointer refers to a distinct and
320 /// identifiable object.  This returns true for:
321 ///    Global Variables and Functions (but not Global Aliases)
322 ///    Allocas and Mallocs
323 ///    ByVal and NoAlias Arguments
324 ///    NoAlias returns
325 ///
326 bool isIdentifiedObject(const Value *V);
327
328 } // End llvm namespace
329
330 // Because of the way .a files work, we must force the BasicAA implementation to
331 // be pulled in if the AliasAnalysis header is included.  Otherwise we run
332 // the risk of AliasAnalysis being used, but the default implementation not
333 // being linked into the tool that uses it.
334 FORCE_DEFINING_FILE_TO_BE_LINKED(AliasAnalysis)
335 FORCE_DEFINING_FILE_TO_BE_LINKED(BasicAliasAnalysis)
336
337 #endif