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