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