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