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