MemoryLocation.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //===- MemoryLocation.h - Memory location descriptions ----------*- 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. /// \file
  10. /// This file provides utility analysis objects describing memory locations.
  11. /// These are used both by the Alias Analysis infrastructure and more
  12. /// specialized memory analysis layers.
  13. ///
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_ANALYSIS_MEMORYLOCATION_H
  16. #define LLVM_ANALYSIS_MEMORYLOCATION_H
  17. #include "llvm/ADT/DenseMap.h"
  18. #include "llvm/IR/CallSite.h"
  19. #include "llvm/IR/Metadata.h"
  20. namespace llvm {
  21. class LoadInst;
  22. class StoreInst;
  23. class MemTransferInst;
  24. class MemIntrinsic;
  25. class TargetLibraryInfo;
  26. /// Representation for a specific memory location.
  27. ///
  28. /// This abstraction can be used to represent a specific location in memory.
  29. /// The goal of the location is to represent enough information to describe
  30. /// abstract aliasing, modification, and reference behaviors of whatever
  31. /// value(s) are stored in memory at the particular location.
  32. ///
  33. /// The primary user of this interface is LLVM's Alias Analysis, but other
  34. /// memory analyses such as MemoryDependence can use it as well.
  35. class MemoryLocation {
  36. public:
  37. /// UnknownSize - This is a special value which can be used with the
  38. /// size arguments in alias queries to indicate that the caller does not
  39. /// know the sizes of the potential memory references.
  40. enum : uint64_t { UnknownSize = ~UINT64_C(0) };
  41. /// The address of the start of the location.
  42. const Value *Ptr;
  43. /// The maximum size of the location, in address-units, or
  44. /// UnknownSize if the size is not known.
  45. ///
  46. /// Note that an unknown size does not mean the pointer aliases the entire
  47. /// virtual address space, because there are restrictions on stepping out of
  48. /// one object and into another. See
  49. /// http://llvm.org/docs/LangRef.html#pointeraliasing
  50. uint64_t Size;
  51. /// The metadata nodes which describes the aliasing of the location (each
  52. /// member is null if that kind of information is unavailable).
  53. AAMDNodes AATags;
  54. /// Return a location with information about the memory reference by the given
  55. /// instruction.
  56. static MemoryLocation get(const LoadInst *LI);
  57. static MemoryLocation get(const StoreInst *SI);
  58. static MemoryLocation get(const VAArgInst *VI);
  59. static MemoryLocation get(const AtomicCmpXchgInst *CXI);
  60. static MemoryLocation get(const AtomicRMWInst *RMWI);
  61. static MemoryLocation get(const Instruction *Inst) {
  62. if (auto *I = dyn_cast<LoadInst>(Inst))
  63. return get(I);
  64. else if (auto *I = dyn_cast<StoreInst>(Inst))
  65. return get(I);
  66. else if (auto *I = dyn_cast<VAArgInst>(Inst))
  67. return get(I);
  68. else if (auto *I = dyn_cast<AtomicCmpXchgInst>(Inst))
  69. return get(I);
  70. else if (auto *I = dyn_cast<AtomicRMWInst>(Inst))
  71. return get(I);
  72. llvm_unreachable("unsupported memory instruction");
  73. }
  74. /// Return a location representing the source of a memory transfer.
  75. static MemoryLocation getForSource(const MemTransferInst *MTI);
  76. /// Return a location representing the destination of a memory set or
  77. /// transfer.
  78. static MemoryLocation getForDest(const MemIntrinsic *MI);
  79. /// Return a location representing a particular argument of a call.
  80. static MemoryLocation getForArgument(ImmutableCallSite CS, unsigned ArgIdx,
  81. const TargetLibraryInfo &TLI);
  82. explicit MemoryLocation(const Value *Ptr = nullptr,
  83. uint64_t Size = UnknownSize,
  84. const AAMDNodes &AATags = AAMDNodes())
  85. : Ptr(Ptr), Size(Size), AATags(AATags) {}
  86. MemoryLocation getWithNewPtr(const Value *NewPtr) const {
  87. MemoryLocation Copy(*this);
  88. Copy.Ptr = NewPtr;
  89. return Copy;
  90. }
  91. MemoryLocation getWithNewSize(uint64_t NewSize) const {
  92. MemoryLocation Copy(*this);
  93. Copy.Size = NewSize;
  94. return Copy;
  95. }
  96. MemoryLocation getWithoutAATags() const {
  97. MemoryLocation Copy(*this);
  98. Copy.AATags = AAMDNodes();
  99. return Copy;
  100. }
  101. bool operator==(const MemoryLocation &Other) const {
  102. return Ptr == Other.Ptr && Size == Other.Size && AATags == Other.AATags;
  103. }
  104. };
  105. // Specialize DenseMapInfo for MemoryLocation.
  106. template <> struct DenseMapInfo<MemoryLocation> {
  107. static inline MemoryLocation getEmptyKey() {
  108. return MemoryLocation(DenseMapInfo<const Value *>::getEmptyKey(), 0);
  109. }
  110. static inline MemoryLocation getTombstoneKey() {
  111. return MemoryLocation(DenseMapInfo<const Value *>::getTombstoneKey(), 0);
  112. }
  113. static unsigned getHashValue(const MemoryLocation &Val) {
  114. return DenseMapInfo<const Value *>::getHashValue(Val.Ptr) ^
  115. DenseMapInfo<uint64_t>::getHashValue(Val.Size) ^
  116. DenseMapInfo<AAMDNodes>::getHashValue(Val.AATags);
  117. }
  118. static bool isEqual(const MemoryLocation &LHS, const MemoryLocation &RHS) {
  119. return LHS == RHS;
  120. }
  121. };
  122. }
  123. #endif