LLVM API Documentation

AliasAnalysis.h

Go to the documentation of this file.
00001 //===- llvm/Analysis/AliasAnalysis.h - Alias Analysis Interface -*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the generic AliasAnalysis interface, which is used as the
00011 // common interface used by all clients of alias analysis information, and
00012 // implemented by all alias analysis implementations.  Mod/Ref information is
00013 // also captured by this interface.
00014 //
00015 // Implementations of this interface must implement the various virtual methods,
00016 // which automatically provides functionality for the entire suite of client
00017 // APIs.
00018 //
00019 // This API represents memory as a (Pointer, Size) pair.  The Pointer component
00020 // specifies the base memory address of the region, the Size specifies how large
00021 // of an area is being queried.  If Size is 0, two pointers only alias if they
00022 // are exactly equal.  If size is greater than zero, but small, the two pointers
00023 // alias if the areas pointed to overlap.  If the size is very large (ie, ~0U),
00024 // then the two pointers alias if they may be pointing to components of the same
00025 // memory object.  Pointers that point to two completely different objects in
00026 // memory never alias, regardless of the value of the Size component.
00027 //
00028 //===----------------------------------------------------------------------===//
00029 
00030 #ifndef LLVM_ANALYSIS_ALIAS_ANALYSIS_H
00031 #define LLVM_ANALYSIS_ALIAS_ANALYSIS_H
00032 
00033 #include "llvm/Support/CallSite.h"
00034 #include "llvm/System/IncludeFile.h"
00035 #include <vector>
00036 
00037 namespace llvm {
00038 
00039 class LoadInst;
00040 class StoreInst;
00041 class VAArgInst;
00042 class TargetData;
00043 class Pass;
00044 class AnalysisUsage;
00045 
00046 class AliasAnalysis {
00047 protected:
00048   const TargetData *TD;
00049   AliasAnalysis *AA;       // Previous Alias Analysis to chain to.
00050 
00051   /// InitializeAliasAnalysis - Subclasses must call this method to initialize
00052   /// the AliasAnalysis interface before any other methods are called.  This is
00053   /// typically called by the run* methods of these subclasses.  This may be
00054   /// called multiple times.
00055   ///
00056   void InitializeAliasAnalysis(Pass *P);
00057 
00058   /// getAnalysisUsage - All alias analysis implementations should invoke this
00059   /// directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that
00060   /// TargetData is required by the pass.
00061   virtual void getAnalysisUsage(AnalysisUsage &AU) const;
00062 
00063 public:
00064   static char ID; // Class identification, replacement for typeinfo
00065   AliasAnalysis() : TD(0), AA(0) {}
00066   virtual ~AliasAnalysis();  // We want to be subclassed
00067 
00068   /// getTargetData - Every alias analysis implementation depends on the size of
00069   /// data items in the current Target.  This provides a uniform way to handle
00070   /// it.
00071   ///
00072   const TargetData &getTargetData() const { return *TD; }
00073 
00074   //===--------------------------------------------------------------------===//
00075   /// Alias Queries...
00076   ///
00077 
00078   /// Alias analysis result - Either we know for sure that it does not alias, we
00079   /// know for sure it must alias, or we don't know anything: The two pointers
00080   /// _might_ alias.  This enum is designed so you can do things like:
00081   ///     if (AA.alias(P1, P2)) { ... }
00082   /// to check to see if two pointers might alias.
00083   ///
00084   enum AliasResult { NoAlias = 0, MayAlias = 1, MustAlias = 2 };
00085 
00086   /// alias - The main low level interface to the alias analysis implementation.
00087   /// Returns a Result indicating whether the two pointers are aliased to each
00088   /// other.  This is the interface that must be implemented by specific alias
00089   /// analysis implementations.
00090   ///
00091   virtual AliasResult alias(const Value *V1, unsigned V1Size,
00092                             const Value *V2, unsigned V2Size);
00093 
00094   /// getMustAliases - If there are any pointers known that must alias this
00095   /// pointer, return them now.  This allows alias-set based alias analyses to
00096   /// perform a form a value numbering (which is exposed by load-vn).  If an
00097   /// alias analysis supports this, it should ADD any must aliased pointers to
00098   /// the specified vector.
00099   ///
00100   virtual void getMustAliases(Value *P, std::vector<Value*> &RetVals);
00101 
00102   /// pointsToConstantMemory - If the specified pointer is known to point into
00103   /// constant global memory, return true.  This allows disambiguation of store
00104   /// instructions from constant pointers.
00105   ///
00106   virtual bool pointsToConstantMemory(const Value *P);
00107 
00108   //===--------------------------------------------------------------------===//
00109   /// Simple mod/ref information...
00110   ///
00111 
00112   /// ModRefResult - Represent the result of a mod/ref query.  Mod and Ref are
00113   /// bits which may be or'd together.
00114   ///
00115   enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 };
00116 
00117 
00118   /// ModRefBehavior - Summary of how a function affects memory in the program.
00119   /// Loads from constant globals are not considered memory accesses for this
00120   /// interface.  Also, functions may freely modify stack space local to their
00121   /// invocation without having to report it through these interfaces.
00122   enum ModRefBehavior {
00123     // DoesNotAccessMemory - This function does not perform any non-local loads
00124     // or stores to memory.
00125     //
00126     // This property corresponds to the GCC 'const' attribute.
00127     DoesNotAccessMemory,
00128 
00129     // AccessesArguments - This function accesses function arguments in
00130     // non-volatile and well known ways, but does not access any other memory.
00131     //
00132     // Clients may call getArgumentAccesses to get specific information about
00133     // how pointer arguments are used.
00134     AccessesArguments,
00135 
00136     // AccessesArgumentsAndGlobals - This function has accesses function
00137     // arguments and global variables in non-volatile and well-known ways, but
00138     // does not access any other memory.
00139     //
00140     // Clients may call getArgumentAccesses to get specific information about
00141     // how pointer arguments and globals are used.
00142     AccessesArgumentsAndGlobals,
00143 
00144     // OnlyReadsMemory - This function does not perform any non-local stores or
00145     // volatile loads, but may read from any memory location.
00146     //
00147     // This property corresponds to the GCC 'pure' attribute.
00148     OnlyReadsMemory,
00149 
00150     // UnknownModRefBehavior - This indicates that the function could not be
00151     // classified into one of the behaviors above.
00152     UnknownModRefBehavior
00153   };
00154 
00155   /// PointerAccessInfo - This struct is used to return results for pointers,
00156   /// globals, and the return value of a function.
00157   struct PointerAccessInfo {
00158     /// V - The value this record corresponds to.  This may be an Argument for
00159     /// the function, a GlobalVariable, or null, corresponding to the return
00160     /// value for the function.
00161     Value *V;
00162 
00163     /// ModRefInfo - Whether the pointer is loaded or stored to/from.
00164     ///
00165     ModRefResult ModRefInfo;
00166 
00167     /// AccessType - Specific fine-grained access information for the argument.
00168     /// If none of these classifications is general enough, the
00169     /// getModRefBehavior method should not return AccessesArguments*.  If a
00170     /// record is not returned for a particular argument, the argument is never
00171     /// dead and never dereferenced.
00172     enum AccessType {
00173       /// ScalarAccess - The pointer is dereferenced.
00174       ///
00175       ScalarAccess,
00176 
00177       /// ArrayAccess - The pointer is indexed through as an array of elements.
00178       ///
00179       ArrayAccess,
00180 
00181       /// ElementAccess ?? P->F only?
00182 
00183       /// CallsThrough - Indirect calls are made through the specified function
00184       /// pointer.
00185       CallsThrough
00186     };
00187   };
00188 
00189   /// getModRefBehavior - Return the behavior when calling the given call site.
00190   ModRefBehavior getModRefBehavior(CallSite CS,
00191                                    std::vector<PointerAccessInfo> *Info = 0);
00192 
00193   /// getModRefBehavior - Return the behavior when calling the given function.
00194   /// For use when the call site is not known.
00195   ModRefBehavior getModRefBehavior(Function *F,
00196                                    std::vector<PointerAccessInfo> *Info = 0);
00197 
00198   /// doesNotAccessMemory - If the specified call is known to never read or
00199   /// write memory, return true.  If the call only reads from known-constant
00200   /// memory, it is also legal to return true.  Calls that unwind the stack
00201   /// are legal for this predicate.
00202   ///
00203   /// Many optimizations (such as CSE and LICM) can be performed on such calls
00204   /// without worrying about aliasing properties, and many calls have this
00205   /// property (e.g. calls to 'sin' and 'cos').
00206   ///
00207   /// This property corresponds to the GCC 'const' attribute.
00208   ///
00209   bool doesNotAccessMemory(CallSite CS) {
00210     return getModRefBehavior(CS) == DoesNotAccessMemory;
00211   }
00212 
00213   /// doesNotAccessMemory - If the specified function is known to never read or
00214   /// write memory, return true.  For use when the call site is not known.
00215   ///
00216   bool doesNotAccessMemory(Function *F) {
00217     return getModRefBehavior(F) == DoesNotAccessMemory;
00218   }
00219 
00220   /// onlyReadsMemory - If the specified call is known to only read from
00221   /// non-volatile memory (or not access memory at all), return true.  Calls
00222   /// that unwind the stack are legal for this predicate.
00223   ///
00224   /// This property allows many common optimizations to be performed in the
00225   /// absence of interfering store instructions, such as CSE of strlen calls.
00226   ///
00227   /// This property corresponds to the GCC 'pure' attribute.
00228   ///
00229   bool onlyReadsMemory(CallSite CS) {
00230     ModRefBehavior MRB = getModRefBehavior(CS);
00231     return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory;
00232   }
00233 
00234   /// onlyReadsMemory - If the specified function is known to only read from
00235   /// non-volatile memory (or not access memory at all), return true.  For use
00236   /// when the call site is not known.
00237   ///
00238   bool onlyReadsMemory(Function *F) {
00239     ModRefBehavior MRB = getModRefBehavior(F);
00240     return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory;
00241   }
00242 
00243 
00244   /// getModRefInfo - Return information about whether or not an instruction may
00245   /// read or write memory specified by the pointer operand.  An instruction
00246   /// that doesn't read or write memory may be trivially LICM'd for example.
00247 
00248   /// getModRefInfo (for call sites) - Return whether information about whether
00249   /// a particular call site modifies or reads the memory specified by the
00250   /// pointer.
00251   ///
00252   virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size);
00253 
00254   /// getModRefInfo - Return information about whether two call sites may refer
00255   /// to the same set of memory locations.  This function returns NoModRef if
00256   /// the two calls refer to disjoint memory locations, Ref if CS1 reads memory
00257   /// written by CS2, Mod if CS1 writes to memory read or written by CS2, or
00258   /// ModRef if CS1 might read or write memory accessed by CS2.
00259   ///
00260   virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2);
00261 
00262   /// hasNoModRefInfoForCalls - Return true if the analysis has no mod/ref
00263   /// information for pairs of function calls (other than "pure" and "const"
00264   /// functions).  This can be used by clients to avoid many pointless queries.
00265   /// Remember that if you override this and chain to another analysis, you must
00266   /// make sure that it doesn't have mod/ref info either.
00267   ///
00268   virtual bool hasNoModRefInfoForCalls() const;
00269 
00270 protected:
00271   /// getModRefBehavior - Return the behavior of the specified function if
00272   /// called from the specified call site.  The call site may be null in which
00273   /// case the most generic behavior of this function should be returned.
00274   virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS,
00275                                      std::vector<PointerAccessInfo> *Info = 0);
00276 
00277 public:
00278   /// Convenience functions...
00279   ModRefResult getModRefInfo(LoadInst *L, Value *P, unsigned Size);
00280   ModRefResult getModRefInfo(StoreInst *S, Value *P, unsigned Size);
00281   ModRefResult getModRefInfo(CallInst *C, Value *P, unsigned Size) {
00282     return getModRefInfo(CallSite(C), P, Size);
00283   }
00284   ModRefResult getModRefInfo(InvokeInst *I, Value *P, unsigned Size) {
00285     return getModRefInfo(CallSite(I), P, Size);
00286   }
00287   ModRefResult getModRefInfo(VAArgInst* I, Value* P, unsigned Size) {
00288     return AliasAnalysis::ModRef;
00289   }
00290   ModRefResult getModRefInfo(Instruction *I, Value *P, unsigned Size) {
00291     switch (I->getOpcode()) {
00292     case Instruction::VAArg:  return getModRefInfo((VAArgInst*)I, P, Size);
00293     case Instruction::Load:   return getModRefInfo((LoadInst*)I, P, Size);
00294     case Instruction::Store:  return getModRefInfo((StoreInst*)I, P, Size);
00295     case Instruction::Call:   return getModRefInfo((CallInst*)I, P, Size);
00296     case Instruction::Invoke: return getModRefInfo((InvokeInst*)I, P, Size);
00297     default:                  return NoModRef;
00298     }
00299   }
00300 
00301   //===--------------------------------------------------------------------===//
00302   /// Higher level methods for querying mod/ref information.
00303   ///
00304 
00305   /// canBasicBlockModify - Return true if it is possible for execution of the
00306   /// specified basic block to modify the value pointed to by Ptr.
00307   ///
00308   bool canBasicBlockModify(const BasicBlock &BB, const Value *P, unsigned Size);
00309 
00310   /// canInstructionRangeModify - Return true if it is possible for the
00311   /// execution of the specified instructions to modify the value pointed to by
00312   /// Ptr.  The instructions to consider are all of the instructions in the
00313   /// range of [I1,I2] INCLUSIVE.  I1 and I2 must be in the same basic block.
00314   ///
00315   bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2,
00316                                  const Value *Ptr, unsigned Size);
00317 
00318   //===--------------------------------------------------------------------===//
00319   /// Methods that clients should call when they transform the program to allow
00320   /// alias analyses to update their internal data structures.  Note that these
00321   /// methods may be called on any instruction, regardless of whether or not
00322   /// they have pointer-analysis implications.
00323   ///
00324 
00325   /// deleteValue - This method should be called whenever an LLVM Value is
00326   /// deleted from the program, for example when an instruction is found to be
00327   /// redundant and is eliminated.
00328   ///
00329   virtual void deleteValue(Value *V);
00330 
00331   /// copyValue - This method should be used whenever a preexisting value in the
00332   /// program is copied or cloned, introducing a new value.  Note that analysis
00333   /// implementations should tolerate clients that use this method to introduce
00334   /// the same value multiple times: if the analysis already knows about a
00335   /// value, it should ignore the request.
00336   ///
00337   virtual void copyValue(Value *From, Value *To);
00338 
00339   /// replaceWithNewValue - This method is the obvious combination of the two
00340   /// above, and it provided as a helper to simplify client code.
00341   ///
00342   void replaceWithNewValue(Value *Old, Value *New) {
00343     copyValue(Old, New);
00344     deleteValue(Old);
00345   }
00346 };
00347 
00348 } // End llvm namespace
00349 
00350 // Because of the way .a files work, we must force the BasicAA implementation to
00351 // be pulled in if the AliasAnalysis header is included.  Otherwise we run
00352 // the risk of AliasAnalysis being used, but the default implementation not
00353 // being linked into the tool that uses it.
00354 FORCE_DEFINING_FILE_TO_BE_LINKED(AliasAnalysis)
00355 FORCE_DEFINING_FILE_TO_BE_LINKED(BasicAliasAnalysis)
00356 
00357 #endif



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.