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