AliasAnalysis.h revision 9769ab22265b313171d201b5928688524a01bd87
1baa3858d3f5d128a5c8466b700098109edcad5f2repo sync//===- llvm/Analysis/AliasAnalysis.h - Alias Analysis Interface -*- C++ -*-===// 2baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// 3baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// The LLVM Compiler Infrastructure 4baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// 5baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// This file was developed by the LLVM research group and is distributed under 6baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// the University of Illinois Open Source License. See LICENSE.TXT for details. 7baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// 8baa3858d3f5d128a5c8466b700098109edcad5f2repo sync//===----------------------------------------------------------------------===// 9baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// 10baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// This file defines the generic AliasAnalysis interface, which is used as the 11baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// common interface used by all clients of alias analysis information, and 12baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// implemented by all alias analysis implementations. Mod/Ref information is 13baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// also captured by this interface. 14baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// 15baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// Implementations of this interface must implement the various virtual methods, 16baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// which automatically provides functionality for the entire suite of client 17baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// APIs. 18baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// 19baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// This API represents memory as a (Pointer, Size) pair. The Pointer component 20baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// specifies the base memory address of the region, the Size specifies how large 21baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// of an area is being queried. If Size is 0, two pointers only alias if they 22baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// are exactly equal. If size is greater than zero, but small, the two pointers 23baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// alias if the areas pointed to overlap. If the size is very large (ie, ~0U), 24baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// then the two pointers alias if they may be pointing to components of the same 25baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// memory object. Pointers that point to two completely different objects in 26baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// memory never alias, regardless of the value of the Size component. 27baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// 28baa3858d3f5d128a5c8466b700098109edcad5f2repo sync//===----------------------------------------------------------------------===// 29baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 30baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#ifndef LLVM_ANALYSIS_ALIAS_ANALYSIS_H 31baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#define LLVM_ANALYSIS_ALIAS_ANALYSIS_H 32baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 33baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "llvm/Support/CallSite.h" 34baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "llvm/Pass.h" // Need this for IncludeFile 35baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 36baa3858d3f5d128a5c8466b700098109edcad5f2repo syncnamespace llvm { 37baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 38baa3858d3f5d128a5c8466b700098109edcad5f2repo syncclass LoadInst; 39baa3858d3f5d128a5c8466b700098109edcad5f2repo syncclass StoreInst; 40baa3858d3f5d128a5c8466b700098109edcad5f2repo syncclass TargetData; 41baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 42baa3858d3f5d128a5c8466b700098109edcad5f2repo syncclass AliasAnalysis { 43baa3858d3f5d128a5c8466b700098109edcad5f2repo syncprotected: 44baa3858d3f5d128a5c8466b700098109edcad5f2repo sync const TargetData *TD; 45baa3858d3f5d128a5c8466b700098109edcad5f2repo sync AliasAnalysis *AA; // Previous Alias Analysis to chain to. 46baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 47baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// InitializeAliasAnalysis - Subclasses must call this method to initialize 48baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// the AliasAnalysis interface before any other methods are called. This is 49baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// typically called by the run* methods of these subclasses. This may be 50baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// called multiple times. 51baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// 52baa3858d3f5d128a5c8466b700098109edcad5f2repo sync void InitializeAliasAnalysis(Pass *P); 53baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 54baa3858d3f5d128a5c8466b700098109edcad5f2repo sync // getAnalysisUsage - All alias analysis implementations should invoke this 55baa3858d3f5d128a5c8466b700098109edcad5f2repo sync // directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that 56baa3858d3f5d128a5c8466b700098109edcad5f2repo sync // TargetData is required by the pass. 57baa3858d3f5d128a5c8466b700098109edcad5f2repo sync virtual void getAnalysisUsage(AnalysisUsage &AU) const; 58baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 59baa3858d3f5d128a5c8466b700098109edcad5f2repo syncpublic: 60baa3858d3f5d128a5c8466b700098109edcad5f2repo sync AliasAnalysis() : TD(0), AA(0) {} 61baa3858d3f5d128a5c8466b700098109edcad5f2repo sync virtual ~AliasAnalysis(); // We want to be subclassed 62baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 63baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// getTargetData - Every alias analysis implementation depends on the size of 64baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// data items in the current Target. This provides a uniform way to handle 65baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// it. 66baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// 67baa3858d3f5d128a5c8466b700098109edcad5f2repo sync const TargetData &getTargetData() const { return *TD; } 68baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 69baa3858d3f5d128a5c8466b700098109edcad5f2repo sync //===--------------------------------------------------------------------===// 70baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// Alias Queries... 71baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// 72baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 73baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// Alias analysis result - Either we know for sure that it does not alias, we 74baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// know for sure it must alias, or we don't know anything: The two pointers 75baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// _might_ alias. This enum is designed so you can do things like: 76baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// if (AA.alias(P1, P2)) { ... } 77baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// to check to see if two pointers might alias. 78baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// 79baa3858d3f5d128a5c8466b700098109edcad5f2repo sync enum AliasResult { NoAlias = 0, MayAlias = 1, MustAlias = 2 }; 80baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 81baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// alias - The main low level interface to the alias analysis implementation. 82baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// Returns a Result indicating whether the two pointers are aliased to each 83baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// other. This is the interface that must be implemented by specific alias 84baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// analysis implementations. 85baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// 86baa3858d3f5d128a5c8466b700098109edcad5f2repo sync virtual AliasResult alias(const Value *V1, unsigned V1Size, 87baa3858d3f5d128a5c8466b700098109edcad5f2repo sync const Value *V2, unsigned V2Size); 88baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 89baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// getMustAliases - If there are any pointers known that must alias this 90baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// pointer, return them now. This allows alias-set based alias analyses to 91baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// perform a form a value numbering (which is exposed by load-vn). If an 92baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// alias analysis supports this, it should ADD any must aliased pointers to 93baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// the specified vector. 94baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// 95baa3858d3f5d128a5c8466b700098109edcad5f2repo sync virtual void getMustAliases(Value *P, std::vector<Value*> &RetVals); 96baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 97baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// pointsToConstantMemory - If the specified pointer is known to point into 98baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// constant global memory, return true. This allows disambiguation of store 99baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// instructions from constant pointers. 100baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// 101baa3858d3f5d128a5c8466b700098109edcad5f2repo sync virtual bool pointsToConstantMemory(const Value *P); 102baa3858d3f5d128a5c8466b700098109edcad5f2repo sync 103baa3858d3f5d128a5c8466b700098109edcad5f2repo sync //===--------------------------------------------------------------------===// 104baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// Simple mod/ref information... 105baa3858d3f5d128a5c8466b700098109edcad5f2repo sync /// 106 107 /// ModRefResult - Represent the result of a mod/ref query. Mod and Ref are 108 /// bits which may be or'd together. 109 /// 110 enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 }; 111 112 113 /// ModRefBehavior - Summary of how a function affects memory in the program. 114 /// Loads from constant globals are not considered memory accesses for this 115 /// interface. Also, functions may freely modify stack space local to their 116 /// invocation without having to report it through these interfaces. 117 enum ModRefBehavior { 118 // DoesNotAccessMemory - This function does not perform any non-local loads 119 // or stores to memory. 120 // 121 // This property corresponds to the GCC 'const' attribute. 122 DoesNotAccessMemory, 123 124 // AccessesArguments - This function accesses function arguments in 125 // non-volatile and well known ways, but does not access any other memory. 126 // 127 // Clients may call getArgumentAccesses to get specific information about 128 // how pointer arguments are used. 129 AccessesArguments, 130 131 // AccessesArgumentsAndGlobals - This function has accesses function 132 // arguments and global variables in non-volatile and well-known ways, but 133 // does not access any other memory. 134 // 135 // Clients may call getArgumentAccesses to get specific information about 136 // how pointer arguments and globals are used. 137 AccessesArgumentsAndGlobals, 138 139 // OnlyReadsMemory - This function does not perform any non-local stores or 140 // volatile loads, but may read from any memory location. 141 // 142 // This property corresponds to the GCC 'pure' attribute. 143 OnlyReadsMemory, 144 145 // UnknownModRefBehavior - This indicates that the function could not be 146 // classified into one of the behaviors above. 147 UnknownModRefBehavior 148 }; 149 150 /// PointerAccessInfo - This struct is used to return results for pointers, 151 /// globals, and the return value of a function. 152 struct PointerAccessInfo { 153 /// V - The value this record corresponds to. This may be an Argument for 154 /// the function, a GlobalVariable, or null, corresponding to the return 155 /// value for the function. 156 Value *V; 157 158 /// ModRefInfo - Whether the pointer is loaded or stored to/from. 159 /// 160 ModRefResult ModRefInfo; 161 162 /// AccessType - Specific fine-grained access information for the argument. 163 /// If none of these classifications is general enough, the 164 /// getModRefBehavior method should not return AccessesArguments*. If a 165 /// record is not returned for a particular argument, the argument is never 166 /// dead and never dereferenced. 167 enum AccessType { 168 /// ScalarAccess - The pointer is dereferenced. 169 /// 170 ScalarAccess, 171 172 /// ArrayAccess - The pointer is indexed through as an array of elements. 173 /// 174 ArrayAccess, 175 176 /// ElementAccess ?? P->F only? 177 178 /// CallsThrough - Indirect calls are made through the specified function 179 /// pointer. 180 CallsThrough, 181 }; 182 }; 183 184 /// getModRefBehavior - Return the behavior of the specified function if 185 /// called from the specified call site. The call site may be null in which 186 /// case the most generic behavior of this function should be returned. 187 virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS, 188 std::vector<PointerAccessInfo> *Info = 0); 189 190 /// doesNotAccessMemory - If the specified function is known to never read or 191 /// write memory, return true. If the function only reads from known-constant 192 /// memory, it is also legal to return true. Functions that unwind the stack 193 /// are not legal for this predicate. 194 /// 195 /// Many optimizations (such as CSE and LICM) can be performed on calls to it, 196 /// without worrying about aliasing properties, and many functions have this 197 /// property (e.g. 'sin' and 'cos'). 198 /// 199 /// This property corresponds to the GCC 'const' attribute. 200 /// 201 bool doesNotAccessMemory(Function *F) { 202 return getModRefBehavior(F, CallSite()) == DoesNotAccessMemory; 203 } 204 205 /// onlyReadsMemory - If the specified function is known to only read from 206 /// non-volatile memory (or not access memory at all), return true. Functions 207 /// that unwind the stack are not legal for this predicate. 208 /// 209 /// This property allows many common optimizations to be performed in the 210 /// absence of interfering store instructions, such as CSE of strlen calls. 211 /// 212 /// This property corresponds to the GCC 'pure' attribute. 213 /// 214 bool onlyReadsMemory(Function *F) { 215 /// FIXME: If the analysis returns more precise info, we can reduce it to 216 /// this. 217 ModRefBehavior MRB = getModRefBehavior(F, CallSite()); 218 return MRB == DoesNotAccessMemory || MRB == OnlyReadsMemory; 219 } 220 221 222 /// getModRefInfo - Return information about whether or not an instruction may 223 /// read or write memory specified by the pointer operand. An instruction 224 /// that doesn't read or write memory may be trivially LICM'd for example. 225 226 /// getModRefInfo (for call sites) - Return whether information about whether 227 /// a particular call site modifies or reads the memory specified by the 228 /// pointer. 229 /// 230 virtual ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size); 231 232 /// getModRefInfo - Return information about whether two call sites may refer 233 /// to the same set of memory locations. This function returns NoModRef if 234 /// the two calls refer to disjoint memory locations, Ref if CS1 reads memory 235 /// written by CS2, Mod if CS1 writes to memory read or written by CS2, or 236 /// ModRef if CS1 might read or write memory accessed by CS2. 237 /// 238 virtual ModRefResult getModRefInfo(CallSite CS1, CallSite CS2); 239 240 /// hasNoModRefInfoForCalls - Return true if the analysis has no mod/ref 241 /// information for pairs of function calls (other than "pure" and "const" 242 /// functions). This can be used by clients to avoid many pointless queries. 243 /// Remember that if you override this and chain to another analysis, you must 244 /// make sure that it doesn't have mod/ref info either. 245 /// 246 virtual bool hasNoModRefInfoForCalls() const; 247 248 /// Convenience functions... 249 ModRefResult getModRefInfo(LoadInst *L, Value *P, unsigned Size); 250 ModRefResult getModRefInfo(StoreInst *S, Value *P, unsigned Size); 251 ModRefResult getModRefInfo(CallInst *C, Value *P, unsigned Size) { 252 return getModRefInfo(CallSite(C), P, Size); 253 } 254 ModRefResult getModRefInfo(InvokeInst *I, Value *P, unsigned Size) { 255 return getModRefInfo(CallSite(I), P, Size); 256 } 257 ModRefResult getModRefInfo(Instruction *I, Value *P, unsigned Size) { 258 switch (I->getOpcode()) { 259 case Instruction::Load: return getModRefInfo((LoadInst*)I, P, Size); 260 case Instruction::Store: return getModRefInfo((StoreInst*)I, P, Size); 261 case Instruction::Call: return getModRefInfo((CallInst*)I, P, Size); 262 case Instruction::Invoke: return getModRefInfo((InvokeInst*)I, P, Size); 263 default: return NoModRef; 264 } 265 } 266 267 //===--------------------------------------------------------------------===// 268 /// Higher level methods for querying mod/ref information. 269 /// 270 271 /// canBasicBlockModify - Return true if it is possible for execution of the 272 /// specified basic block to modify the value pointed to by Ptr. 273 /// 274 bool canBasicBlockModify(const BasicBlock &BB, const Value *P, unsigned Size); 275 276 /// canInstructionRangeModify - Return true if it is possible for the 277 /// execution of the specified instructions to modify the value pointed to by 278 /// Ptr. The instructions to consider are all of the instructions in the 279 /// range of [I1,I2] INCLUSIVE. I1 and I2 must be in the same basic block. 280 /// 281 bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2, 282 const Value *Ptr, unsigned Size); 283 284 //===--------------------------------------------------------------------===// 285 /// Methods that clients should call when they transform the program to allow 286 /// alias analyses to update their internal data structures. Note that these 287 /// methods may be called on any instruction, regardless of whether or not 288 /// they have pointer-analysis implications. 289 /// 290 291 /// deleteValue - This method should be called whenever an LLVM Value is 292 /// deleted from the program, for example when an instruction is found to be 293 /// redundant and is eliminated. 294 /// 295 virtual void deleteValue(Value *V); 296 297 /// copyValue - This method should be used whenever a preexisting value in the 298 /// program is copied or cloned, introducing a new value. Note that analysis 299 /// implementations should tolerate clients that use this method to introduce 300 /// the same value multiple times: if the analysis already knows about a 301 /// value, it should ignore the request. 302 /// 303 virtual void copyValue(Value *From, Value *To); 304 305 /// replaceWithNewValue - This method is the obvious combination of the two 306 /// above, and it provided as a helper to simplify client code. 307 /// 308 void replaceWithNewValue(Value *Old, Value *New) { 309 copyValue(Old, New); 310 deleteValue(Old); 311 } 312}; 313 314// Because of the way .a files work, we must force the BasicAA implementation to 315// be pulled in if the AliasAnalysis header is included. Otherwise we run 316// the risk of AliasAnalysis being used, but the default implementation not 317// being linked into the tool that uses it. 318// 319extern void BasicAAStub(); 320static IncludeFile HDR_INCLUDE_BASICAA_CPP((void*)&BasicAAStub); 321 322} // End llvm namespace 323 324#endif 325