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 "llvm/ADT/DenseMap.h" 42 43 namespace llvm { 44 45 class LoadInst; 46 class StoreInst; 47 class VAArgInst; 48 class TargetData; 49 class Pass; 50 class AnalysisUsage; 51 class MemTransferInst; 52 class MemIntrinsic; 53 54 class AliasAnalysis { 55 protected: 56 const TargetData *TD; 57 58 private: 59 AliasAnalysis *AA; // Previous Alias Analysis to chain to. 60 61 protected: 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 73 public: 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(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 Location getLocation(const AtomicCmpXchgInst *CXI); 140 Location getLocation(const AtomicRMWInst *RMWI); 141 static Location getLocationForSource(const MemTransferInst *MTI); 142 static Location getLocationForDest(const MemIntrinsic *MI); 143 144 /// Alias analysis result - Either we know for sure that it does not alias, we 145 /// know for sure it must alias, or we don't know anything: The two pointers 146 /// _might_ alias. This enum is designed so you can do things like: 147 /// if (AA.alias(P1, P2)) { ... } 148 /// to check to see if two pointers might alias. 149 /// 150 /// See docs/AliasAnalysis.html for more information on the specific meanings 151 /// of these values. 152 /// 153 enum AliasResult { 154 NoAlias = 0, ///< No dependencies. 155 MayAlias, ///< Anything goes. 156 PartialAlias, ///< Pointers differ, but pointees overlap. 157 MustAlias ///< Pointers are equal. 158 }; 159 160 /// alias - The main low level interface to the alias analysis implementation. 161 /// Returns an AliasResult indicating whether the two pointers are aliased to 162 /// each other. This is the interface that must be implemented by specific 163 /// alias analysis implementations. 164 virtual AliasResult alias(const Location &LocA, const Location &LocB); 165 166 /// alias - A convenience wrapper. 167 AliasResult alias(const Value *V1, uint64_t V1Size, 168 const Value *V2, uint64_t V2Size) { 169 return alias(Location(V1, V1Size), Location(V2, V2Size)); 170 } 171 172 /// alias - A convenience wrapper. 173 AliasResult alias(const Value *V1, const Value *V2) { 174 return alias(V1, UnknownSize, V2, UnknownSize); 175 } 176 177 /// isNoAlias - A trivial helper function to check to see if the specified 178 /// pointers are no-alias. 179 bool isNoAlias(const Location &LocA, const Location &LocB) { 180 return alias(LocA, LocB) == NoAlias; 181 } 182 183 /// isNoAlias - A convenience wrapper. 184 bool isNoAlias(const Value *V1, uint64_t V1Size, 185 const Value *V2, uint64_t V2Size) { 186 return isNoAlias(Location(V1, V1Size), Location(V2, V2Size)); 187 } 188 189 /// isMustAlias - A convenience wrapper. 190 bool isMustAlias(const Location &LocA, const Location &LocB) { 191 return alias(LocA, LocB) == MustAlias; 192 } 193 194 /// isMustAlias - A convenience wrapper. 195 bool isMustAlias(const Value *V1, const Value *V2) { 196 return alias(V1, 1, V2, 1) == MustAlias; 197 } 198 199 /// pointsToConstantMemory - If the specified memory location is 200 /// known to be constant, return true. If OrLocal is true and the 201 /// specified memory location is known to be "local" (derived from 202 /// an alloca), return true. Otherwise return false. 203 virtual bool pointsToConstantMemory(const Location &Loc, 204 bool OrLocal = false); 205 206 /// pointsToConstantMemory - A convenient wrapper. 207 bool pointsToConstantMemory(const Value *P, bool OrLocal = false) { 208 return pointsToConstantMemory(Location(P), OrLocal); 209 } 210 211 //===--------------------------------------------------------------------===// 212 /// Simple mod/ref information... 213 /// 214 215 /// ModRefResult - Represent the result of a mod/ref query. Mod and Ref are 216 /// bits which may be or'd together. 217 /// 218 enum ModRefResult { NoModRef = 0, Ref = 1, Mod = 2, ModRef = 3 }; 219 220 /// These values define additional bits used to define the 221 /// ModRefBehavior values. 222 enum { Nowhere = 0, ArgumentPointees = 4, Anywhere = 8 | ArgumentPointees }; 223 224 /// ModRefBehavior - Summary of how a function affects memory in the program. 225 /// Loads from constant globals are not considered memory accesses for this 226 /// interface. Also, functions may freely modify stack space local to their 227 /// invocation without having to report it through these interfaces. 228 enum ModRefBehavior { 229 /// DoesNotAccessMemory - This function does not perform any non-local loads 230 /// or stores to memory. 231 /// 232 /// This property corresponds to the GCC 'const' attribute. 233 /// This property corresponds to the LLVM IR 'readnone' attribute. 234 /// This property corresponds to the IntrNoMem LLVM intrinsic flag. 235 DoesNotAccessMemory = Nowhere | NoModRef, 236 237 /// OnlyReadsArgumentPointees - The only memory references in this function 238 /// (if it has any) are non-volatile loads from objects pointed to by its 239 /// pointer-typed arguments, with arbitrary offsets. 240 /// 241 /// This property corresponds to the IntrReadArgMem LLVM intrinsic flag. 242 OnlyReadsArgumentPointees = ArgumentPointees | Ref, 243 244 /// OnlyAccessesArgumentPointees - The only memory references in this 245 /// function (if it has any) are non-volatile loads and stores from objects 246 /// pointed to by its pointer-typed arguments, with arbitrary offsets. 247 /// 248 /// This property corresponds to the IntrReadWriteArgMem LLVM intrinsic flag. 249 OnlyAccessesArgumentPointees = ArgumentPointees | ModRef, 250 251 /// OnlyReadsMemory - This function does not perform any non-local stores or 252 /// volatile loads, but may read from any memory location. 253 /// 254 /// This property corresponds to the GCC 'pure' attribute. 255 /// This property corresponds to the LLVM IR 'readonly' attribute. 256 /// This property corresponds to the IntrReadMem LLVM intrinsic flag. 257 OnlyReadsMemory = Anywhere | Ref, 258 259 /// UnknownModRefBehavior - This indicates that the function could not be 260 /// classified into one of the behaviors above. 261 UnknownModRefBehavior = Anywhere | ModRef 262 }; 263 264 /// getModRefBehavior - Return the behavior when calling the given call site. 265 virtual ModRefBehavior getModRefBehavior(ImmutableCallSite CS); 266 267 /// getModRefBehavior - Return the behavior when calling the given function. 268 /// For use when the call site is not known. 269 virtual ModRefBehavior getModRefBehavior(const Function *F); 270 271 /// doesNotAccessMemory - If the specified call is known to never read or 272 /// write memory, return true. If the call only reads from known-constant 273 /// memory, it is also legal to return true. Calls that unwind the stack 274 /// are legal for this predicate. 275 /// 276 /// Many optimizations (such as CSE and LICM) can be performed on such calls 277 /// without worrying about aliasing properties, and many calls have this 278 /// property (e.g. calls to 'sin' and 'cos'). 279 /// 280 /// This property corresponds to the GCC 'const' attribute. 281 /// 282 bool doesNotAccessMemory(ImmutableCallSite CS) { 283 return getModRefBehavior(CS) == DoesNotAccessMemory; 284 } 285 286 /// doesNotAccessMemory - If the specified function is known to never read or 287 /// write memory, return true. For use when the call site is not known. 288 /// 289 bool doesNotAccessMemory(const Function *F) { 290 return getModRefBehavior(F) == DoesNotAccessMemory; 291 } 292 293 /// onlyReadsMemory - If the specified call is known to only read from 294 /// non-volatile memory (or not access memory at all), return true. Calls 295 /// that unwind the stack are legal for this predicate. 296 /// 297 /// This property allows many common optimizations to be performed in the 298 /// absence of interfering store instructions, such as CSE of strlen calls. 299 /// 300 /// This property corresponds to the GCC 'pure' attribute. 301 /// 302 bool onlyReadsMemory(ImmutableCallSite CS) { 303 return onlyReadsMemory(getModRefBehavior(CS)); 304 } 305 306 /// onlyReadsMemory - If the specified function is known to only read from 307 /// non-volatile memory (or not access memory at all), return true. For use 308 /// when the call site is not known. 309 /// 310 bool onlyReadsMemory(const Function *F) { 311 return onlyReadsMemory(getModRefBehavior(F)); 312 } 313 314 /// onlyReadsMemory - Return true if functions with the specified behavior are 315 /// known to only read from non-volatile memory (or not access memory at all). 316 /// 317 static bool onlyReadsMemory(ModRefBehavior MRB) { 318 return !(MRB & Mod); 319 } 320 321 /// onlyAccessesArgPointees - Return true if functions with the specified 322 /// behavior are known to read and write at most from objects pointed to by 323 /// their pointer-typed arguments (with arbitrary offsets). 324 /// 325 static bool onlyAccessesArgPointees(ModRefBehavior MRB) { 326 return !(MRB & Anywhere & ~ArgumentPointees); 327 } 328 329 /// doesAccessArgPointees - Return true if functions with the specified 330 /// behavior are known to potentially read or write from objects pointed 331 /// to be their pointer-typed arguments (with arbitrary offsets). 332 /// 333 static bool doesAccessArgPointees(ModRefBehavior MRB) { 334 return (MRB & ModRef) && (MRB & ArgumentPointees); 335 } 336 337 /// getModRefInfo - Return information about whether or not an instruction may 338 /// read or write the specified memory location. An instruction 339 /// that doesn't read or write memory may be trivially LICM'd for example. 340 ModRefResult getModRefInfo(const Instruction *I, 341 const Location &Loc) { 342 switch (I->getOpcode()) { 343 case Instruction::VAArg: return getModRefInfo((const VAArgInst*)I, Loc); 344 case Instruction::Load: return getModRefInfo((const LoadInst*)I, Loc); 345 case Instruction::Store: return getModRefInfo((const StoreInst*)I, Loc); 346 case Instruction::Fence: return getModRefInfo((const FenceInst*)I, Loc); 347 case Instruction::AtomicCmpXchg: 348 return getModRefInfo((const AtomicCmpXchgInst*)I, Loc); 349 case Instruction::AtomicRMW: 350 return getModRefInfo((const AtomicRMWInst*)I, Loc); 351 case Instruction::Call: return getModRefInfo((const CallInst*)I, Loc); 352 case Instruction::Invoke: return getModRefInfo((const InvokeInst*)I,Loc); 353 default: return NoModRef; 354 } 355 } 356 357 /// getModRefInfo - A convenience wrapper. 358 ModRefResult getModRefInfo(const Instruction *I, 359 const Value *P, uint64_t Size) { 360 return getModRefInfo(I, Location(P, Size)); 361 } 362 363 /// getModRefInfo (for call sites) - Return whether information about whether 364 /// a particular call site modifies or reads the specified memory location. 365 virtual ModRefResult getModRefInfo(ImmutableCallSite CS, 366 const Location &Loc); 367 368 /// getModRefInfo (for call sites) - A convenience wrapper. 369 ModRefResult getModRefInfo(ImmutableCallSite CS, 370 const Value *P, uint64_t Size) { 371 return getModRefInfo(CS, Location(P, Size)); 372 } 373 374 /// getModRefInfo (for calls) - Return whether information about whether 375 /// a particular call modifies or reads the specified memory location. 376 ModRefResult getModRefInfo(const CallInst *C, const Location &Loc) { 377 return getModRefInfo(ImmutableCallSite(C), Loc); 378 } 379 380 /// getModRefInfo (for calls) - A convenience wrapper. 381 ModRefResult getModRefInfo(const CallInst *C, const Value *P, uint64_t Size) { 382 return getModRefInfo(C, Location(P, Size)); 383 } 384 385 /// getModRefInfo (for invokes) - Return whether information about whether 386 /// a particular invoke modifies or reads the specified memory location. 387 ModRefResult getModRefInfo(const InvokeInst *I, 388 const Location &Loc) { 389 return getModRefInfo(ImmutableCallSite(I), Loc); 390 } 391 392 /// getModRefInfo (for invokes) - A convenience wrapper. 393 ModRefResult getModRefInfo(const InvokeInst *I, 394 const Value *P, uint64_t Size) { 395 return getModRefInfo(I, Location(P, Size)); 396 } 397 398 /// getModRefInfo (for loads) - Return whether information about whether 399 /// a particular load modifies or reads the specified memory location. 400 ModRefResult getModRefInfo(const LoadInst *L, const Location &Loc); 401 402 /// getModRefInfo (for loads) - A convenience wrapper. 403 ModRefResult getModRefInfo(const LoadInst *L, const Value *P, uint64_t Size) { 404 return getModRefInfo(L, Location(P, Size)); 405 } 406 407 /// getModRefInfo (for stores) - Return whether information about whether 408 /// a particular store modifies or reads the specified memory location. 409 ModRefResult getModRefInfo(const StoreInst *S, const Location &Loc); 410 411 /// getModRefInfo (for stores) - A convenience wrapper. 412 ModRefResult getModRefInfo(const StoreInst *S, const Value *P, uint64_t Size){ 413 return getModRefInfo(S, Location(P, Size)); 414 } 415 416 /// getModRefInfo (for fences) - Return whether information about whether 417 /// a particular store modifies or reads the specified memory location. 418 ModRefResult getModRefInfo(const FenceInst *S, const Location &Loc) { 419 // Conservatively correct. (We could possibly be a bit smarter if 420 // Loc is a alloca that doesn't escape.) 421 return ModRef; 422 } 423 424 /// getModRefInfo (for fences) - A convenience wrapper. 425 ModRefResult getModRefInfo(const FenceInst *S, const Value *P, uint64_t Size){ 426 return getModRefInfo(S, Location(P, Size)); 427 } 428 429 /// getModRefInfo (for cmpxchges) - Return whether information about whether 430 /// a particular cmpxchg modifies or reads the specified memory location. 431 ModRefResult getModRefInfo(const AtomicCmpXchgInst *CX, const Location &Loc); 432 433 /// getModRefInfo (for cmpxchges) - A convenience wrapper. 434 ModRefResult getModRefInfo(const AtomicCmpXchgInst *CX, 435 const Value *P, unsigned Size) { 436 return getModRefInfo(CX, Location(P, Size)); 437 } 438 439 /// getModRefInfo (for atomicrmws) - Return whether information about whether 440 /// a particular atomicrmw modifies or reads the specified memory location. 441 ModRefResult getModRefInfo(const AtomicRMWInst *RMW, const Location &Loc); 442 443 /// getModRefInfo (for atomicrmws) - A convenience wrapper. 444 ModRefResult getModRefInfo(const AtomicRMWInst *RMW, 445 const Value *P, unsigned Size) { 446 return getModRefInfo(RMW, Location(P, Size)); 447 } 448 449 /// getModRefInfo (for va_args) - Return whether information about whether 450 /// a particular va_arg modifies or reads the specified memory location. 451 ModRefResult getModRefInfo(const VAArgInst* I, const Location &Loc); 452 453 /// getModRefInfo (for va_args) - A convenience wrapper. 454 ModRefResult getModRefInfo(const VAArgInst* I, const Value* P, uint64_t Size){ 455 return getModRefInfo(I, Location(P, Size)); 456 } 457 458 /// getModRefInfo - Return information about whether two call sites may refer 459 /// to the same set of memory locations. See 460 /// http://llvm.org/docs/AliasAnalysis.html#ModRefInfo 461 /// for details. 462 virtual ModRefResult getModRefInfo(ImmutableCallSite CS1, 463 ImmutableCallSite CS2); 464 465 //===--------------------------------------------------------------------===// 466 /// Higher level methods for querying mod/ref information. 467 /// 468 469 /// canBasicBlockModify - Return true if it is possible for execution of the 470 /// specified basic block to modify the value pointed to by Ptr. 471 bool canBasicBlockModify(const BasicBlock &BB, const Location &Loc); 472 473 /// canBasicBlockModify - A convenience wrapper. 474 bool canBasicBlockModify(const BasicBlock &BB, const Value *P, uint64_t Size){ 475 return canBasicBlockModify(BB, Location(P, Size)); 476 } 477 478 /// canInstructionRangeModify - Return true if it is possible for the 479 /// execution of the specified instructions to modify the value pointed to by 480 /// Ptr. The instructions to consider are all of the instructions in the 481 /// range of [I1,I2] INCLUSIVE. I1 and I2 must be in the same basic block. 482 bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2, 483 const Location &Loc); 484 485 /// canInstructionRangeModify - A convenience wrapper. 486 bool canInstructionRangeModify(const Instruction &I1, const Instruction &I2, 487 const Value *Ptr, uint64_t Size) { 488 return canInstructionRangeModify(I1, I2, Location(Ptr, Size)); 489 } 490 491 //===--------------------------------------------------------------------===// 492 /// Methods that clients should call when they transform the program to allow 493 /// alias analyses to update their internal data structures. Note that these 494 /// methods may be called on any instruction, regardless of whether or not 495 /// they have pointer-analysis implications. 496 /// 497 498 /// deleteValue - This method should be called whenever an LLVM Value is 499 /// deleted from the program, for example when an instruction is found to be 500 /// redundant and is eliminated. 501 /// 502 virtual void deleteValue(Value *V); 503 504 /// copyValue - This method should be used whenever a preexisting value in the 505 /// program is copied or cloned, introducing a new value. Note that analysis 506 /// implementations should tolerate clients that use this method to introduce 507 /// the same value multiple times: if the analysis already knows about a 508 /// value, it should ignore the request. 509 /// 510 virtual void copyValue(Value *From, Value *To); 511 512 /// addEscapingUse - This method should be used whenever an escaping use is 513 /// added to a pointer value. Analysis implementations may either return 514 /// conservative responses for that value in the future, or may recompute 515 /// some or all internal state to continue providing precise responses. 516 /// 517 /// Escaping uses are considered by anything _except_ the following: 518 /// - GEPs or bitcasts of the pointer 519 /// - Loads through the pointer 520 /// - Stores through (but not of) the pointer 521 virtual void addEscapingUse(Use &U); 522 523 /// replaceWithNewValue - This method is the obvious combination of the two 524 /// above, and it provided as a helper to simplify client code. 525 /// 526 void replaceWithNewValue(Value *Old, Value *New) { 527 copyValue(Old, New); 528 deleteValue(Old); 529 } 530 }; 531 532 // Specialize DenseMapInfo for Location. 533 template<> 534 struct DenseMapInfo<AliasAnalysis::Location> { 535 static inline AliasAnalysis::Location getEmptyKey() { 536 return 537 AliasAnalysis::Location(DenseMapInfo<const Value *>::getEmptyKey(), 538 0, 0); 539 } 540 static inline AliasAnalysis::Location getTombstoneKey() { 541 return 542 AliasAnalysis::Location(DenseMapInfo<const Value *>::getTombstoneKey(), 543 0, 0); 544 } 545 static unsigned getHashValue(const AliasAnalysis::Location &Val) { 546 return DenseMapInfo<const Value *>::getHashValue(Val.Ptr) ^ 547 DenseMapInfo<uint64_t>::getHashValue(Val.Size) ^ 548 DenseMapInfo<const MDNode *>::getHashValue(Val.TBAATag); 549 } 550 static bool isEqual(const AliasAnalysis::Location &LHS, 551 const AliasAnalysis::Location &RHS) { 552 return LHS.Ptr == RHS.Ptr && 553 LHS.Size == RHS.Size && 554 LHS.TBAATag == RHS.TBAATag; 555 } 556 }; 557 558 /// isNoAliasCall - Return true if this pointer is returned by a noalias 559 /// function. 560 bool isNoAliasCall(const Value *V); 561 562 /// isIdentifiedObject - Return true if this pointer refers to a distinct and 563 /// identifiable object. This returns true for: 564 /// Global Variables and Functions (but not Global Aliases) 565 /// Allocas and Mallocs 566 /// ByVal and NoAlias Arguments 567 /// NoAlias returns 568 /// 569 bool isIdentifiedObject(const Value *V); 570 571 } // End llvm namespace 572 573 #endif 574