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