Move NoAA out of BasicAliasAnalysis.cpp into its own file, now that
[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 <vector>
32
33 namespace llvm {
34
35 class LoadInst;
36 class StoreInst;
37 class VAArgInst;
38 class TargetData;
39 class Pass;
40 class AnalysisUsage;
41
42 class AliasAnalysis {
43 protected:
44   const TargetData *TD;
45
46 private:
47   AliasAnalysis *AA;       // Previous Alias Analysis to chain to.
48
49 protected:
50   /// InitializeAliasAnalysis - Subclasses must call this method to initialize
51   /// the AliasAnalysis interface before any other methods are called.  This is
52   /// typically called by the run* methods of these subclasses.  This may be
53   /// called multiple times.
54   ///
55   void InitializeAliasAnalysis(Pass *P);
56
57   /// getAnalysisUsage - All alias analysis implementations should invoke this
58   /// directly (using AliasAnalysis::getAnalysisUsage(AU)).
59   virtual void getAnalysisUsage(AnalysisUsage &AU) const;
60
61 public:
62   static char ID; // Class identification, replacement for typeinfo
63   AliasAnalysis() : TD(0), AA(0) {}
64   virtual ~AliasAnalysis();  // We want to be subclassed
65
66   /// UnknownSize - This is a special value which can be used with the
67   /// size arguments in alias queries to indicate that the caller does not
68   /// know the sizes of the potential memory references.
69   static uint64_t const UnknownSize = ~UINT64_C(0);
70
71   /// getTargetData - Return a pointer to the current TargetData object, or
72   /// null if no TargetData object is available.
73   ///
74   const TargetData *getTargetData() const { return TD; }
75
76   /// getTypeStoreSize - Return the TargetData store size for the given type,
77   /// if known, or a conservative value otherwise.
78   ///
79   uint64_t getTypeStoreSize(const Type *Ty);
80
81   //===--------------------------------------------------------------------===//
82   /// Alias Queries...
83   ///
84
85   /// Location - A description of a memory location.
86   struct Location {
87     /// Ptr - The address of the start of the location.
88     const Value *Ptr;
89     /// Size - The size of the location.
90     uint64_t Size;
91     /// TBAATag - The metadata node which describes the TBAA type of
92     /// the location, or null if there is no (unique) tag.
93     const MDNode *TBAATag;
94
95     explicit Location(const Value *P = 0,
96                       uint64_t S = UnknownSize,
97                       const MDNode *N = 0)
98       : Ptr(P), Size(S), TBAATag(N) {}
99
100     Location getWithNewPtr(const Value *NewPtr) const {
101       Location Copy(*this);
102       Copy.Ptr = NewPtr;
103       return Copy;
104     }
105
106     Location getWithoutTBAATag() const {
107       Location Copy(*this);
108       Copy.TBAATag = 0;
109       return Copy;
110     }
111   };
112
113   /// Alias analysis result - Either we know for sure that it does not alias, we
114   /// know for sure it must alias, or we don't know anything: The two pointers
115   /// _might_ alias.  This enum is designed so you can do things like:
116   ///     if (AA.alias(P1, P2)) { ... }
117   /// to check to see if two pointers might alias.
118   ///
119   /// See docs/AliasAnalysis.html for more information on the specific meanings
120   /// of these values.
121   ///
122   enum AliasResult { NoAlias = 0, MayAlias = 1, MustAlias = 2 };
123
124   /// alias - The main low level interface to the alias analysis implementation.
125   /// Returns a Result indicating whether the two pointers are aliased to each
126   /// other.  This is the interface that must be implemented by specific alias
127   /// analysis implementations.
128   virtual AliasResult alias(const Location &LocA, const Location &LocB);
129
130   /// alias - A convenience wrapper.
131   AliasResult alias(const Value *V1, uint64_t V1Size,
132                     const Value *V2, uint64_t V2Size) {
133     return alias(Location(V1, V1Size), Location(V2, V2Size));
134   }
135
136   /// alias - A convenience wrapper.
137   AliasResult alias(const Value *V1, const Value *V2) {
138     return alias(V1, UnknownSize, V2, UnknownSize);
139   }
140
141   /// isNoAlias - A trivial helper function to check to see if the specified
142   /// pointers are no-alias.
143   bool isNoAlias(const Location &LocA, const Location &LocB) {
144     return alias(LocA, LocB) == NoAlias;
145   }
146
147   /// isNoAlias - A convenience wrapper.
148   bool isNoAlias(const Value *V1, uint64_t V1Size,
149                  const Value *V2, uint64_t V2Size) {
150     return isNoAlias(Location(V1, V1Size), Location(V2, V2Size));
151   }
152
153   /// pointsToConstantMemory - If the specified memory location is known to be
154   /// constant, return true.  This allows disambiguation of store
155   /// instructions from constant pointers.
156   ///
157   virtual bool pointsToConstantMemory(const Location &Loc);
158
159   /// pointsToConstantMemory - A convenient wrapper.
160   bool pointsToConstantMemory(const Value *P) {
161     return pointsToConstantMemory(Location(P));
162   }
163
164   //===--------------------------------------------------------------------===//
165   /// Simple mod/ref information...
166   ///
167
168   /// ModRefResult - Represent the result of a mod/ref query.  Mod and Ref are
169   /// bits which may be or'd together.
170   ///
171   enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 };
172
173
174   /// ModRefBehavior - Summary of how a function affects memory in the program.
175   /// Loads from constant globals are not considered memory accesses for this
176   /// interface.  Also, functions may freely modify stack space local to their
177   /// invocation without having to report it through these interfaces.
178   enum ModRefBehavior {
179     // DoesNotAccessMemory - This function does not perform any non-local loads
180     // or stores to memory.
181     //
182     // This property corresponds to the GCC 'const' attribute.
183     DoesNotAccessMemory,
184
185     // AccessesArguments - This function accesses function arguments in well
186     // known (possibly volatile) ways, but does not access any other memory.
187     AccessesArguments,
188
189     // AccessesArgumentsAndGlobals - This function has accesses function
190     // arguments and global variables well known (possibly volatile) ways, but
191     // does not access any other memory.
192     AccessesArgumentsAndGlobals,
193
194     // OnlyReadsMemory - This function does not perform any non-local stores or
195     // volatile loads, but may read from any memory location.
196     //
197     // This property corresponds to the GCC 'pure' attribute.
198     OnlyReadsMemory,
199
200     // UnknownModRefBehavior - This indicates that the function could not be
201     // classified into one of the behaviors above.
202     UnknownModRefBehavior
203   };
204
205   /// getModRefBehavior - Return the behavior when calling the given call site.
206   virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS);
207
208   /// getModRefBehavior - Return the behavior when calling the given function.
209   /// For use when the call site is not known.
210   virtual ModRefBehavior getModRefBehavior(const Function *F);
211
212   /// getIntrinsicModRefBehavior - Return the modref behavior of the intrinsic
213   /// with the given id.  Most clients won't need this, because the regular
214   /// getModRefBehavior incorporates this information.
215   static ModRefBehavior getIntrinsicModRefBehavior(unsigned iid);
216
217   /// doesNotAccessMemory - If the specified call is known to never read or
218   /// write memory, return true.  If the call only reads from known-constant
219   /// memory, it is also legal to return true.  Calls that unwind the stack
220   /// are legal for this predicate.
221   ///
222   /// Many optimizations (such as CSE and LICM) can be performed on such calls
223   /// without worrying about aliasing properties, and many calls have this
224   /// property (e.g. calls to 'sin' and 'cos').
225   ///
226   /// This property corresponds to the GCC 'const' attribute.
227   ///
228   bool doesNotAccessMemory(ImmutableCallSite CS) {
229     return getModRefBehavior(CS) == DoesNotAccessMemory;
230   }
231
232   /// doesNotAccessMemory - If the specified function is known to never read or
233   /// write memory, return true.  For use when the call site is not known.
234   ///
235   bool doesNotAccessMemory(const Function *F) {
236     return getModRefBehavior(F) == DoesNotAccessMemory;
237   }
238
239   /// onlyReadsMemory - If the specified call is known to only read from
240   /// non-volatile memory (or not access memory at all), return true.  Calls
241   /// that unwind the stack are legal for this predicate.
242   ///
243   /// This property allows many common optimizations to be performed in the
244   /// absence of interfering store instructions, such as CSE of strlen calls.
245   ///
246   /// This property corresponds to the GCC 'pure' attribute.
247   ///
248   bool onlyReadsMemory(ImmutableCallSite CS) {
249     ModRefBehavior MRB = getModRefBehavior(CS);
250     return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory;
251   }
252
253   /// onlyReadsMemory - If the specified function is known to only read from
254   /// non-volatile memory (or not access memory at all), return true.  For use
255   /// when the call site is not known.
256   ///
257   bool onlyReadsMemory(const Function *F) {
258     ModRefBehavior MRB = getModRefBehavior(F);
259     return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory;
260   }
261
262
263   /// getModRefInfo - Return information about whether or not an instruction may
264   /// read or write the specified memory location.  An instruction
265   /// that doesn't read or write memory may be trivially LICM'd for example.
266   ModRefResult getModRefInfo(const Instruction *I,
267                              const Location &Loc) {
268     switch (I->getOpcode()) {
269     case Instruction::VAArg:  return getModRefInfo((const VAArgInst*)I, Loc);
270     case Instruction::Load:   return getModRefInfo((const LoadInst*)I,  Loc);
271     case Instruction::Store:  return getModRefInfo((const StoreInst*)I, Loc);
272     case Instruction::Call:   return getModRefInfo((const CallInst*)I,  Loc);
273     case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,Loc);
274     default:                  return NoModRef;
275     }
276   }
277
278   /// getModRefInfo - A convenience wrapper.
279   ModRefResult getModRefInfo(const Instruction *I,
280                              const Value *P, uint64_t Size) {
281     return getModRefInfo(I, Location(P, Size));
282   }
283
284   /// getModRefInfo (for call sites) - Return whether information about whether
285   /// a particular call site modifies or reads the specified memory location.
286   virtual ModRefResult getModRefInfo(ImmutableCallSite CS,
287                                      const Location &Loc);
288
289   /// getModRefInfo (for call sites) - A convenience wrapper.
290   ModRefResult getModRefInfo(ImmutableCallSite CS,
291                              const Value *P, uint64_t Size) {
292     return getModRefInfo(CS, Location(P, Size));
293   }
294
295   /// getModRefInfo (for calls) - Return whether information about whether
296   /// a particular call modifies or reads the specified memory location.
297   ModRefResult getModRefInfo(const CallInst *C, const Location &Loc) {
298     return getModRefInfo(ImmutableCallSite(C), Loc);
299   }
300
301   /// getModRefInfo (for calls) - A convenience wrapper.
302   ModRefResult getModRefInfo(const CallInst *C, const Value *P, uint64_t Size) {
303     return getModRefInfo(C, Location(P, Size));
304   }
305
306   /// getModRefInfo (for invokes) - Return whether information about whether
307   /// a particular invoke modifies or reads the specified memory location.
308   ModRefResult getModRefInfo(const InvokeInst *I,
309                              const Location &Loc) {
310     return getModRefInfo(ImmutableCallSite(I), Loc);
311   }
312
313   /// getModRefInfo (for invokes) - A convenience wrapper.
314   ModRefResult getModRefInfo(const InvokeInst *I,
315                              const Value *P, uint64_t Size) {
316     return getModRefInfo(I, Location(P, Size));
317   }
318
319   /// getModRefInfo (for loads) - Return whether information about whether
320   /// a particular load modifies or reads the specified memory location.
321   ModRefResult getModRefInfo(const LoadInst *L, const Location &Loc);
322
323   /// getModRefInfo (for loads) - A convenience wrapper.
324   ModRefResult getModRefInfo(const LoadInst *L, const Value *P, uint64_t Size) {
325     return getModRefInfo(L, Location(P, Size));
326   }
327
328   /// getModRefInfo (for stores) - Return whether information about whether
329   /// a particular store modifies or reads the specified memory location.
330   ModRefResult getModRefInfo(const StoreInst *S, const Location &Loc);
331
332   /// getModRefInfo (for stores) - A convenience wrapper.
333   ModRefResult getModRefInfo(const StoreInst *S, const Value *P, uint64_t Size) {
334     return getModRefInfo(S, Location(P, Size));
335   }
336
337   /// getModRefInfo (for va_args) - Return whether information about whether
338   /// a particular va_arg modifies or reads the specified memory location.
339   ModRefResult getModRefInfo(const VAArgInst* I, const Location &Loc);
340
341   /// getModRefInfo (for va_args) - A convenience wrapper.
342   ModRefResult getModRefInfo(const VAArgInst* I, const Value* P, uint64_t Size) {
343     return getModRefInfo(I, Location(P, Size));
344   }
345
346   /// getModRefInfo - Return information about whether two call sites may refer
347   /// to the same set of memory locations.  See 
348   ///   http://llvm.org/docs/AliasAnalysis.html#ModRefInfo
349   /// for details.
350   virtual ModRefResult getModRefInfo(ImmutableCallSite CS1,
351                                      ImmutableCallSite CS2);
352
353   //===--------------------------------------------------------------------===//
354   /// Higher level methods for querying mod/ref information.
355   ///
356
357   /// canBasicBlockModify - Return true if it is possible for execution of the
358   /// specified basic block to modify the value pointed to by Ptr.
359   bool canBasicBlockModify(const BasicBlock &BB, const Location &Loc);
360
361   /// canBasicBlockModify - A convenience wrapper.
362   bool canBasicBlockModify(const BasicBlock &BB, const Value *P, uint64_t Size){
363     return canBasicBlockModify(BB, Location(P, Size));
364   }
365
366   /// canInstructionRangeModify - Return true if it is possible for the
367   /// execution of the specified instructions to modify the value pointed to by
368   /// Ptr.  The instructions to consider are all of the instructions in the
369   /// range of [I1,I2] INCLUSIVE.  I1 and I2 must be in the same basic block.
370   bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
371                                  const Location &Loc);
372
373   /// canInstructionRangeModify - A convenience wrapper.
374   bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
375                                  const Value *Ptr, uint64_t Size) {
376     return canInstructionRangeModify(I1, I2, Location(Ptr, Size));
377   }
378
379   //===--------------------------------------------------------------------===//
380   /// Methods that clients should call when they transform the program to allow
381   /// alias analyses to update their internal data structures.  Note that these
382   /// methods may be called on any instruction, regardless of whether or not
383   /// they have pointer-analysis implications.
384   ///
385
386   /// deleteValue - This method should be called whenever an LLVM Value is
387   /// deleted from the program, for example when an instruction is found to be
388   /// redundant and is eliminated.
389   ///
390   virtual void deleteValue(Value *V);
391
392   /// copyValue - This method should be used whenever a preexisting value in the
393   /// program is copied or cloned, introducing a new value.  Note that analysis
394   /// implementations should tolerate clients that use this method to introduce
395   /// the same value multiple times: if the analysis already knows about a
396   /// value, it should ignore the request.
397   ///
398   virtual void copyValue(Value *From, Value *To);
399
400   /// replaceWithNewValue - This method is the obvious combination of the two
401   /// above, and it provided as a helper to simplify client code.
402   ///
403   void replaceWithNewValue(Value *Old, Value *New) {
404     copyValue(Old, New);
405     deleteValue(Old);
406   }
407 };
408
409 /// isNoAliasCall - Return true if this pointer is returned by a noalias
410 /// function.
411 bool isNoAliasCall(const Value *V);
412
413 /// isIdentifiedObject - Return true if this pointer refers to a distinct and
414 /// identifiable object.  This returns true for:
415 ///    Global Variables and Functions (but not Global Aliases)
416 ///    Allocas and Mallocs
417 ///    ByVal and NoAlias Arguments
418 ///    NoAlias returns
419 ///
420 bool isIdentifiedObject(const Value *V);
421
422 } // End llvm namespace
423
424 #endif