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