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