AliasAnalysis.h revision 6d8eb156e6be727570b300bac7712f745a318c7d
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 identifies memory regions with the Location class. The pointer 20// component specifies the base memory address of the region. The Size specifies 21// the maximum size (in address units) of the memory region, or UnknownSize if 22// the size is not known. The TBAA tag identifies the "type" of the memory 23// reference; see the TypeBasedAliasAnalysis class for details. 24// 25// Some non-obvious details include: 26// - Pointers that point to two completely different objects in memory never 27// alias, regardless of the value of the Size component. 28// - NoAlias doesn't imply inequal pointers. The most obvious example of this 29// is two pointers to constant memory. Even if they are equal, constant 30// memory is never stored to, so there will never be any dependencies. 31// In this and other situations, the pointers may be both NoAlias and 32// MustAlias at the same time. The current API can only return one result, 33// though this is rarely a problem in practice. 34// 35//===----------------------------------------------------------------------===// 36 37#ifndef LLVM_ANALYSIS_ALIAS_ANALYSIS_H 38#define LLVM_ANALYSIS_ALIAS_ANALYSIS_H 39 40#include "llvm/Support/CallSite.h" 41#include <vector> 42 43namespace llvm { 44 45class LoadInst; 46class StoreInst; 47class VAArgInst; 48class TargetData; 49class Pass; 50class AnalysisUsage; 51 52class AliasAnalysis { 53protected: 54 const TargetData *TD; 55 56private: 57 AliasAnalysis *AA; // Previous Alias Analysis to chain to. 58 59protected: 60 /// InitializeAliasAnalysis - Subclasses must call this method to initialize 61 /// the AliasAnalysis interface before any other methods are called. This is 62 /// typically called by the run* methods of these subclasses. This may be 63 /// called multiple times. 64 /// 65 void InitializeAliasAnalysis(Pass *P); 66 67 /// getAnalysisUsage - All alias analysis implementations should invoke this 68 /// directly (using AliasAnalysis::getAnalysisUsage(AU)). 69 virtual void getAnalysisUsage(AnalysisUsage &AU) const; 70 71public: 72 static char ID; // Class identification, replacement for typeinfo 73 AliasAnalysis() : TD(0), AA(0) {} 74 virtual ~AliasAnalysis(); // We want to be subclassed 75 76 /// UnknownSize - This is a special value which can be used with the 77 /// size arguments in alias queries to indicate that the caller does not 78 /// know the sizes of the potential memory references. 79 static uint64_t const UnknownSize = ~UINT64_C(0); 80 81 /// getTargetData - Return a pointer to the current TargetData object, or 82 /// null if no TargetData object is available. 83 /// 84 const TargetData *getTargetData() const { return TD; } 85 86 /// getTypeStoreSize - Return the TargetData store size for the given type, 87 /// if known, or a conservative value otherwise. 88 /// 89 uint64_t getTypeStoreSize(const Type *Ty); 90 91 //===--------------------------------------------------------------------===// 92 /// Alias Queries... 93 /// 94 95 /// Location - A description of a memory location. 96 struct Location { 97 /// Ptr - The address of the start of the location. 98 const Value *Ptr; 99 /// Size - The maximum size of the location, or UnknownSize if the size is 100 /// not known. Note that an unknown size does not mean the pointer aliases 101 /// the entire virtual address space, because there are restrictions on 102 /// stepping out of one object and into another. 103 /// See http://llvm.org/docs/LangRef.html#pointeraliasing 104 uint64_t Size; 105 /// TBAATag - The metadata node which describes the TBAA type of 106 /// the location, or null if there is no known unique tag. 107 const MDNode *TBAATag; 108 109 explicit Location(const Value *P = 0, 110 uint64_t S = UnknownSize, 111 const MDNode *N = 0) 112 : Ptr(P), Size(S), TBAATag(N) {} 113 114 Location getWithNewPtr(const Value *NewPtr) const { 115 Location Copy(*this); 116 Copy.Ptr = NewPtr; 117 return Copy; 118 } 119 120 Location getWithNewSize(uint64_t NewSize) const { 121 Location Copy(*this); 122 Copy.Size = NewSize; 123 return Copy; 124 } 125 126 Location getWithoutTBAATag() const { 127 Location Copy(*this); 128 Copy.TBAATag = 0; 129 return Copy; 130 } 131 }; 132 133 /// getLocation - Fill in Loc with information about the memory reference by 134 /// the given instruction. 135 Location getLocation(const LoadInst *LI); 136 Location getLocation(const StoreInst *SI); 137 Location getLocation(const VAArgInst *VI); 138 139 /// Alias analysis result - Either we know for sure that it does not alias, we 140 /// know for sure it must alias, or we don't know anything: The two pointers 141 /// _might_ alias. This enum is designed so you can do things like: 142 /// if (AA.alias(P1, P2)) { ... } 143 /// to check to see if two pointers might alias. 144 /// 145 /// See docs/AliasAnalysis.html for more information on the specific meanings 146 /// of these values. 147 /// 148 enum AliasResult { 149 NoAlias = 0, ///< No dependencies. 150 MayAlias = 1, ///< Anything goes. 151 MustAlias = 2 ///< Pointers are equal. 152 }; 153 154 /// alias - The main low level interface to the alias analysis implementation. 155 /// Returns an AliasResult indicating whether the two pointers are aliased to 156 /// each other. This is the interface that must be implemented by specific 157 /// alias analysis implementations. 158 virtual AliasResult alias(const Location &LocA, const Location &LocB); 159 160 /// alias - A convenience wrapper. 161 AliasResult alias(const Value *V1, uint64_t V1Size, 162 const Value *V2, uint64_t V2Size) { 163 return alias(Location(V1, V1Size), Location(V2, V2Size)); 164 } 165 166 /// alias - A convenience wrapper. 167 AliasResult alias(const Value *V1, const Value *V2) { 168 return alias(V1, UnknownSize, V2, UnknownSize); 169 } 170 171 /// isNoAlias - A trivial helper function to check to see if the specified 172 /// pointers are no-alias. 173 bool isNoAlias(const Location &LocA, const Location &LocB) { 174 return alias(LocA, LocB) == NoAlias; 175 } 176 177 /// isNoAlias - A convenience wrapper. 178 bool isNoAlias(const Value *V1, uint64_t V1Size, 179 const Value *V2, uint64_t V2Size) { 180 return isNoAlias(Location(V1, V1Size), Location(V2, V2Size)); 181 } 182 183 /// pointsToConstantMemory - If the specified memory location is 184 /// known to be constant, return true. If OrLocal is true and the 185 /// specified memory location is known to be "local" (derived from 186 /// an alloca), return true. Otherwise return false. 187 virtual bool pointsToConstantMemory(const Location &Loc, 188 bool OrLocal = false); 189 190 /// pointsToConstantMemory - A convenient wrapper. 191 bool pointsToConstantMemory(const Value *P, bool OrLocal = false) { 192 return pointsToConstantMemory(Location(P), OrLocal); 193 } 194 195 //===--------------------------------------------------------------------===// 196 /// Simple mod/ref information... 197 /// 198 199 /// ModRefResult - Represent the result of a mod/ref query. Mod and Ref are 200 /// bits which may be or'd together. 201 /// 202 enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 }; 203 204 /// These values define additional bits used to define the 205 /// ModRefBehavior values. 206 enum { Nowhere = 0, ArgumentPointees = 4, Anywhere = 8 | ArgumentPointees }; 207 208 /// ModRefBehavior - Summary of how a function affects memory in the program. 209 /// Loads from constant globals are not considered memory accesses for this 210 /// interface. Also, functions may freely modify stack space local to their 211 /// invocation without having to report it through these interfaces. 212 enum ModRefBehavior { 213 /// DoesNotAccessMemory - This function does not perform any non-local loads 214 /// or stores to memory. 215 /// 216 /// This property corresponds to the GCC 'const' attribute. 217 /// This property corresponds to the LLVM IR 'readnone' attribute. 218 /// This property corresponds to the IntrNoMem LLVM intrinsic flag. 219 DoesNotAccessMemory = Nowhere | NoModRef, 220 221 /// OnlyReadsArgumentPointees - The only memory references in this function 222 /// (if it has any) are non-volatile loads from objects pointed to by its 223 /// pointer-typed arguments, with arbitrary offsets. 224 /// 225 /// This property corresponds to the IntrReadArgMem LLVM intrinsic flag. 226 OnlyReadsArgumentPointees = ArgumentPointees | Ref, 227 228 /// OnlyAccessesArgumentPointees - The only memory references in this 229 /// function (if it has any) are non-volatile loads and stores from objects 230 /// pointed to by its pointer-typed arguments, with arbitrary offsets. 231 /// 232 /// This property corresponds to the IntrReadWriteArgMem LLVM intrinsic flag. 233 OnlyAccessesArgumentPointees = ArgumentPointees | ModRef, 234 235 /// OnlyReadsMemory - This function does not perform any non-local stores or 236 /// volatile loads, but may read from any memory location. 237 /// 238 /// This property corresponds to the GCC 'pure' attribute. 239 /// This property corresponds to the LLVM IR 'readonly' attribute. 240 /// This property corresponds to the IntrReadMem LLVM intrinsic flag. 241 OnlyReadsMemory = Anywhere | Ref, 242 243 /// UnknownModRefBehavior - This indicates that the function could not be 244 /// classified into one of the behaviors above. 245 UnknownModRefBehavior = Anywhere | ModRef 246 }; 247 248 /// getModRefBehavior - Return the behavior when calling the given call site. 249 virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS); 250 251 /// getModRefBehavior - Return the behavior when calling the given function. 252 /// For use when the call site is not known. 253 virtual ModRefBehavior getModRefBehavior(const Function *F); 254 255 /// doesNotAccessMemory - If the specified call is known to never read or 256 /// write memory, return true. If the call only reads from known-constant 257 /// memory, it is also legal to return true. Calls that unwind the stack 258 /// are legal for this predicate. 259 /// 260 /// Many optimizations (such as CSE and LICM) can be performed on such calls 261 /// without worrying about aliasing properties, and many calls have this 262 /// property (e.g. calls to 'sin' and 'cos'). 263 /// 264 /// This property corresponds to the GCC 'const' attribute. 265 /// 266 bool doesNotAccessMemory(ImmutableCallSite CS) { 267 return getModRefBehavior(CS) == DoesNotAccessMemory; 268 } 269 270 /// doesNotAccessMemory - If the specified function is known to never read or 271 /// write memory, return true. For use when the call site is not known. 272 /// 273 bool doesNotAccessMemory(const Function *F) { 274 return getModRefBehavior(F) == DoesNotAccessMemory; 275 } 276 277 /// onlyReadsMemory - If the specified call is known to only read from 278 /// non-volatile memory (or not access memory at all), return true. Calls 279 /// that unwind the stack are legal for this predicate. 280 /// 281 /// This property allows many common optimizations to be performed in the 282 /// absence of interfering store instructions, such as CSE of strlen calls. 283 /// 284 /// This property corresponds to the GCC 'pure' attribute. 285 /// 286 bool onlyReadsMemory(ImmutableCallSite CS) { 287 return onlyReadsMemory(getModRefBehavior(CS)); 288 } 289 290 /// onlyReadsMemory - If the specified function is known to only read from 291 /// non-volatile memory (or not access memory at all), return true. For use 292 /// when the call site is not known. 293 /// 294 bool onlyReadsMemory(const Function *F) { 295 return onlyReadsMemory(getModRefBehavior(F)); 296 } 297 298 /// onlyReadsMemory - Return true if functions with the specified behavior are 299 /// known to only read from non-volatile memory (or not access memory at all). 300 /// 301 static bool onlyReadsMemory(ModRefBehavior MRB) { 302 return !(MRB & Mod); 303 } 304 305 /// onlyAccessesArgPointees - Return true if functions with the specified 306 /// behavior are known to read and write at most from objects pointed to by 307 /// their pointer-typed arguments (with arbitrary offsets). 308 /// 309 static bool onlyAccessesArgPointees(ModRefBehavior MRB) { 310 return !(MRB & Anywhere & ~ArgumentPointees); 311 } 312 313 /// doesAccessArgPointees - Return true if functions with the specified 314 /// behavior are known to potentially read or write from objects pointed 315 /// to be their pointer-typed arguments (with arbitrary offsets). 316 /// 317 static bool doesAccessArgPointees(ModRefBehavior MRB) { 318 return (MRB & ModRef) && (MRB & ArgumentPointees); 319 } 320 321 /// getModRefInfo - Return information about whether or not an instruction may 322 /// read or write the specified memory location. An instruction 323 /// that doesn't read or write memory may be trivially LICM'd for example. 324 ModRefResult getModRefInfo(const Instruction *I, 325 const Location &Loc) { 326 switch (I->getOpcode()) { 327 case Instruction::VAArg: return getModRefInfo((const VAArgInst*)I, Loc); 328 case Instruction::Load: return getModRefInfo((const LoadInst*)I, Loc); 329 case Instruction::Store: return getModRefInfo((const StoreInst*)I, Loc); 330 case Instruction::Call: return getModRefInfo((const CallInst*)I, Loc); 331 case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,Loc); 332 default: return NoModRef; 333 } 334 } 335 336 /// getModRefInfo - A convenience wrapper. 337 ModRefResult getModRefInfo(const Instruction *I, 338 const Value *P, uint64_t Size) { 339 return getModRefInfo(I, Location(P, Size)); 340 } 341 342 /// getModRefInfo (for call sites) - Return whether information about whether 343 /// a particular call site modifies or reads the specified memory location. 344 virtual ModRefResult getModRefInfo(ImmutableCallSite CS, 345 const Location &Loc); 346 347 /// getModRefInfo (for call sites) - A convenience wrapper. 348 ModRefResult getModRefInfo(ImmutableCallSite CS, 349 const Value *P, uint64_t Size) { 350 return getModRefInfo(CS, Location(P, Size)); 351 } 352 353 /// getModRefInfo (for calls) - Return whether information about whether 354 /// a particular call modifies or reads the specified memory location. 355 ModRefResult getModRefInfo(const CallInst *C, const Location &Loc) { 356 return getModRefInfo(ImmutableCallSite(C), Loc); 357 } 358 359 /// getModRefInfo (for calls) - A convenience wrapper. 360 ModRefResult getModRefInfo(const CallInst *C, const Value *P, uint64_t Size) { 361 return getModRefInfo(C, Location(P, Size)); 362 } 363 364 /// getModRefInfo (for invokes) - Return whether information about whether 365 /// a particular invoke modifies or reads the specified memory location. 366 ModRefResult getModRefInfo(const InvokeInst *I, 367 const Location &Loc) { 368 return getModRefInfo(ImmutableCallSite(I), Loc); 369 } 370 371 /// getModRefInfo (for invokes) - A convenience wrapper. 372 ModRefResult getModRefInfo(const InvokeInst *I, 373 const Value *P, uint64_t Size) { 374 return getModRefInfo(I, Location(P, Size)); 375 } 376 377 /// getModRefInfo (for loads) - Return whether information about whether 378 /// a particular load modifies or reads the specified memory location. 379 ModRefResult getModRefInfo(const LoadInst *L, const Location &Loc); 380 381 /// getModRefInfo (for loads) - A convenience wrapper. 382 ModRefResult getModRefInfo(const LoadInst *L, const Value *P, uint64_t Size) { 383 return getModRefInfo(L, Location(P, Size)); 384 } 385 386 /// getModRefInfo (for stores) - Return whether information about whether 387 /// a particular store modifies or reads the specified memory location. 388 ModRefResult getModRefInfo(const StoreInst *S, const Location &Loc); 389 390 /// getModRefInfo (for stores) - A convenience wrapper. 391 ModRefResult getModRefInfo(const StoreInst *S, const Value *P, uint64_t Size) { 392 return getModRefInfo(S, Location(P, Size)); 393 } 394 395 /// getModRefInfo (for va_args) - Return whether information about whether 396 /// a particular va_arg modifies or reads the specified memory location. 397 ModRefResult getModRefInfo(const VAArgInst* I, const Location &Loc); 398 399 /// getModRefInfo (for va_args) - A convenience wrapper. 400 ModRefResult getModRefInfo(const VAArgInst* I, const Value* P, uint64_t Size) { 401 return getModRefInfo(I, Location(P, Size)); 402 } 403 404 /// getModRefInfo - Return information about whether two call sites may refer 405 /// to the same set of memory locations. See 406 /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo 407 /// for details. 408 virtual ModRefResult getModRefInfo(ImmutableCallSite CS1, 409 ImmutableCallSite CS2); 410 411 //===--------------------------------------------------------------------===// 412 /// Higher level methods for querying mod/ref information. 413 /// 414 415 /// canBasicBlockModify - Return true if it is possible for execution of the 416 /// specified basic block to modify the value pointed to by Ptr. 417 bool canBasicBlockModify(const BasicBlock &BB, const Location &Loc); 418 419 /// canBasicBlockModify - A convenience wrapper. 420 bool canBasicBlockModify(const BasicBlock &BB, const Value *P, uint64_t Size){ 421 return canBasicBlockModify(BB, Location(P, Size)); 422 } 423 424 /// canInstructionRangeModify - Return true if it is possible for the 425 /// execution of the specified instructions to modify the value pointed to by 426 /// Ptr. The instructions to consider are all of the instructions in the 427 /// range of [I1,I2] INCLUSIVE. I1 and I2 must be in the same basic block. 428 bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2, 429 const Location &Loc); 430 431 /// canInstructionRangeModify - A convenience wrapper. 432 bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2, 433 const Value *Ptr, uint64_t Size) { 434 return canInstructionRangeModify(I1, I2, Location(Ptr, Size)); 435 } 436 437 //===--------------------------------------------------------------------===// 438 /// Methods that clients should call when they transform the program to allow 439 /// alias analyses to update their internal data structures. Note that these 440 /// methods may be called on any instruction, regardless of whether or not 441 /// they have pointer-analysis implications. 442 /// 443 444 /// deleteValue - This method should be called whenever an LLVM Value is 445 /// deleted from the program, for example when an instruction is found to be 446 /// redundant and is eliminated. 447 /// 448 virtual void deleteValue(Value *V); 449 450 /// copyValue - This method should be used whenever a preexisting value in the 451 /// program is copied or cloned, introducing a new value. Note that analysis 452 /// implementations should tolerate clients that use this method to introduce 453 /// the same value multiple times: if the analysis already knows about a 454 /// value, it should ignore the request. 455 /// 456 virtual void copyValue(Value *From, Value *To); 457 458 /// replaceWithNewValue - This method is the obvious combination of the two 459 /// above, and it provided as a helper to simplify client code. 460 /// 461 void replaceWithNewValue(Value *Old, Value *New) { 462 copyValue(Old, New); 463 deleteValue(Old); 464 } 465}; 466 467/// isNoAliasCall - Return true if this pointer is returned by a noalias 468/// function. 469bool isNoAliasCall(const Value *V); 470 471/// isIdentifiedObject - Return true if this pointer refers to a distinct and 472/// identifiable object. This returns true for: 473/// Global Variables and Functions (but not Global Aliases) 474/// Allocas and Mallocs 475/// ByVal and NoAlias Arguments 476/// NoAlias returns 477/// 478bool isIdentifiedObject(const Value *V); 479 480} // End llvm namespace 481 482#endif 483