MemoryLocation.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- MemoryLocation.h - Memory location descriptions ----------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. /// \file
  14. /// This file provides utility analysis objects describing memory locations.
  15. /// These are used both by the Alias Analysis infrastructure and more
  16. /// specialized memory analysis layers.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_ANALYSIS_MEMORYLOCATION_H
  20. #define LLVM_ANALYSIS_MEMORYLOCATION_H
  21. #include "llvm/ADT/DenseMapInfo.h"
  22. #include "llvm/IR/Metadata.h"
  23. #include "llvm/Support/TypeSize.h"
  24. #include <optional>
  25. namespace llvm {
  26. class CallBase;
  27. class Instruction;
  28. class LoadInst;
  29. class StoreInst;
  30. class MemTransferInst;
  31. class MemIntrinsic;
  32. class AtomicCmpXchgInst;
  33. class AtomicMemTransferInst;
  34. class AtomicMemIntrinsic;
  35. class AtomicRMWInst;
  36. class AnyMemTransferInst;
  37. class AnyMemIntrinsic;
  38. class TargetLibraryInfo;
  39. class VAArgInst;
  40. class Value;
  41. // Represents the size of a MemoryLocation. Logically, it's an
  42. // std::optional<uint63_t> that also carries a bit to represent whether the
  43. // integer it contains, N, is 'precise'. Precise, in this context, means that we
  44. // know that the area of storage referenced by the given MemoryLocation must be
  45. // precisely N bytes. An imprecise value is formed as the union of two or more
  46. // precise values, and can conservatively represent all of the values unioned
  47. // into it. Importantly, imprecise values are an *upper-bound* on the size of a
  48. // MemoryLocation.
  49. //
  50. // Concretely, a precise MemoryLocation is (%p, 4) in
  51. // store i32 0, i32* %p
  52. //
  53. // Since we know that %p must be at least 4 bytes large at this point.
  54. // Otherwise, we have UB. An example of an imprecise MemoryLocation is (%p, 4)
  55. // at the memcpy in
  56. //
  57. // %n = select i1 %foo, i64 1, i64 4
  58. // call void @llvm.memcpy.p0i8.p0i8.i64(i8* %p, i8* %baz, i64 %n, i32 1,
  59. // i1 false)
  60. //
  61. // ...Since we'll copy *up to* 4 bytes into %p, but we can't guarantee that
  62. // we'll ever actually do so.
  63. //
  64. // If asked to represent a pathologically large value, this will degrade to
  65. // std::nullopt.
  66. class LocationSize {
  67. enum : uint64_t {
  68. BeforeOrAfterPointer = ~uint64_t(0),
  69. AfterPointer = BeforeOrAfterPointer - 1,
  70. MapEmpty = BeforeOrAfterPointer - 2,
  71. MapTombstone = BeforeOrAfterPointer - 3,
  72. ImpreciseBit = uint64_t(1) << 63,
  73. // The maximum value we can represent without falling back to 'unknown'.
  74. MaxValue = (MapTombstone - 1) & ~ImpreciseBit,
  75. };
  76. uint64_t Value;
  77. // Hack to support implicit construction. This should disappear when the
  78. // public LocationSize ctor goes away.
  79. enum DirectConstruction { Direct };
  80. constexpr LocationSize(uint64_t Raw, DirectConstruction): Value(Raw) {}
  81. static_assert(AfterPointer & ImpreciseBit,
  82. "AfterPointer is imprecise by definition.");
  83. static_assert(BeforeOrAfterPointer & ImpreciseBit,
  84. "BeforeOrAfterPointer is imprecise by definition.");
  85. public:
  86. // FIXME: Migrate all users to construct via either `precise` or `upperBound`,
  87. // to make it more obvious at the callsite the kind of size that they're
  88. // providing.
  89. //
  90. // Since the overwhelming majority of users of this provide precise values,
  91. // this assumes the provided value is precise.
  92. constexpr LocationSize(uint64_t Raw)
  93. : Value(Raw > MaxValue ? AfterPointer : Raw) {}
  94. static LocationSize precise(uint64_t Value) { return LocationSize(Value); }
  95. static LocationSize precise(TypeSize Value) {
  96. if (Value.isScalable())
  97. return afterPointer();
  98. return precise(Value.getFixedValue());
  99. }
  100. static LocationSize upperBound(uint64_t Value) {
  101. // You can't go lower than 0, so give a precise result.
  102. if (LLVM_UNLIKELY(Value == 0))
  103. return precise(0);
  104. if (LLVM_UNLIKELY(Value > MaxValue))
  105. return afterPointer();
  106. return LocationSize(Value | ImpreciseBit, Direct);
  107. }
  108. static LocationSize upperBound(TypeSize Value) {
  109. if (Value.isScalable())
  110. return afterPointer();
  111. return upperBound(Value.getFixedValue());
  112. }
  113. /// Any location after the base pointer (but still within the underlying
  114. /// object).
  115. constexpr static LocationSize afterPointer() {
  116. return LocationSize(AfterPointer, Direct);
  117. }
  118. /// Any location before or after the base pointer (but still within the
  119. /// underlying object).
  120. constexpr static LocationSize beforeOrAfterPointer() {
  121. return LocationSize(BeforeOrAfterPointer, Direct);
  122. }
  123. // Sentinel values, generally used for maps.
  124. constexpr static LocationSize mapTombstone() {
  125. return LocationSize(MapTombstone, Direct);
  126. }
  127. constexpr static LocationSize mapEmpty() {
  128. return LocationSize(MapEmpty, Direct);
  129. }
  130. // Returns a LocationSize that can correctly represent either `*this` or
  131. // `Other`.
  132. LocationSize unionWith(LocationSize Other) const {
  133. if (Other == *this)
  134. return *this;
  135. if (Value == BeforeOrAfterPointer || Other.Value == BeforeOrAfterPointer)
  136. return beforeOrAfterPointer();
  137. if (Value == AfterPointer || Other.Value == AfterPointer)
  138. return afterPointer();
  139. return upperBound(std::max(getValue(), Other.getValue()));
  140. }
  141. bool hasValue() const {
  142. return Value != AfterPointer && Value != BeforeOrAfterPointer;
  143. }
  144. uint64_t getValue() const {
  145. assert(hasValue() && "Getting value from an unknown LocationSize!");
  146. return Value & ~ImpreciseBit;
  147. }
  148. // Returns whether or not this value is precise. Note that if a value is
  149. // precise, it's guaranteed to not be unknown.
  150. bool isPrecise() const {
  151. return (Value & ImpreciseBit) == 0;
  152. }
  153. // Convenience method to check if this LocationSize's value is 0.
  154. bool isZero() const { return hasValue() && getValue() == 0; }
  155. /// Whether accesses before the base pointer are possible.
  156. bool mayBeBeforePointer() const { return Value == BeforeOrAfterPointer; }
  157. bool operator==(const LocationSize &Other) const {
  158. return Value == Other.Value;
  159. }
  160. bool operator!=(const LocationSize &Other) const {
  161. return !(*this == Other);
  162. }
  163. // Ordering operators are not provided, since it's unclear if there's only one
  164. // reasonable way to compare:
  165. // - values that don't exist against values that do, and
  166. // - precise values to imprecise values
  167. void print(raw_ostream &OS) const;
  168. // Returns an opaque value that represents this LocationSize. Cannot be
  169. // reliably converted back into a LocationSize.
  170. uint64_t toRaw() const { return Value; }
  171. };
  172. inline raw_ostream &operator<<(raw_ostream &OS, LocationSize Size) {
  173. Size.print(OS);
  174. return OS;
  175. }
  176. /// Representation for a specific memory location.
  177. ///
  178. /// This abstraction can be used to represent a specific location in memory.
  179. /// The goal of the location is to represent enough information to describe
  180. /// abstract aliasing, modification, and reference behaviors of whatever
  181. /// value(s) are stored in memory at the particular location.
  182. ///
  183. /// The primary user of this interface is LLVM's Alias Analysis, but other
  184. /// memory analyses such as MemoryDependence can use it as well.
  185. class MemoryLocation {
  186. public:
  187. /// UnknownSize - This is a special value which can be used with the
  188. /// size arguments in alias queries to indicate that the caller does not
  189. /// know the sizes of the potential memory references.
  190. enum : uint64_t { UnknownSize = ~UINT64_C(0) };
  191. /// The address of the start of the location.
  192. const Value *Ptr;
  193. /// The maximum size of the location, in address-units, or
  194. /// UnknownSize if the size is not known.
  195. ///
  196. /// Note that an unknown size does not mean the pointer aliases the entire
  197. /// virtual address space, because there are restrictions on stepping out of
  198. /// one object and into another. See
  199. /// http://llvm.org/docs/LangRef.html#pointeraliasing
  200. LocationSize Size;
  201. /// The metadata nodes which describes the aliasing of the location (each
  202. /// member is null if that kind of information is unavailable).
  203. AAMDNodes AATags;
  204. void print(raw_ostream &OS) const { OS << *Ptr << " " << Size << "\n"; }
  205. /// Return a location with information about the memory reference by the given
  206. /// instruction.
  207. static MemoryLocation get(const LoadInst *LI);
  208. static MemoryLocation get(const StoreInst *SI);
  209. static MemoryLocation get(const VAArgInst *VI);
  210. static MemoryLocation get(const AtomicCmpXchgInst *CXI);
  211. static MemoryLocation get(const AtomicRMWInst *RMWI);
  212. static MemoryLocation get(const Instruction *Inst) {
  213. return *MemoryLocation::getOrNone(Inst);
  214. }
  215. static std::optional<MemoryLocation> getOrNone(const Instruction *Inst);
  216. /// Return a location representing the source of a memory transfer.
  217. static MemoryLocation getForSource(const MemTransferInst *MTI);
  218. static MemoryLocation getForSource(const AtomicMemTransferInst *MTI);
  219. static MemoryLocation getForSource(const AnyMemTransferInst *MTI);
  220. /// Return a location representing the destination of a memory set or
  221. /// transfer.
  222. static MemoryLocation getForDest(const MemIntrinsic *MI);
  223. static MemoryLocation getForDest(const AtomicMemIntrinsic *MI);
  224. static MemoryLocation getForDest(const AnyMemIntrinsic *MI);
  225. static std::optional<MemoryLocation> getForDest(const CallBase *CI,
  226. const TargetLibraryInfo &TLI);
  227. /// Return a location representing a particular argument of a call.
  228. static MemoryLocation getForArgument(const CallBase *Call, unsigned ArgIdx,
  229. const TargetLibraryInfo *TLI);
  230. static MemoryLocation getForArgument(const CallBase *Call, unsigned ArgIdx,
  231. const TargetLibraryInfo &TLI) {
  232. return getForArgument(Call, ArgIdx, &TLI);
  233. }
  234. /// Return a location that may access any location after Ptr, while remaining
  235. /// within the underlying object.
  236. static MemoryLocation getAfter(const Value *Ptr,
  237. const AAMDNodes &AATags = AAMDNodes()) {
  238. return MemoryLocation(Ptr, LocationSize::afterPointer(), AATags);
  239. }
  240. /// Return a location that may access any location before or after Ptr, while
  241. /// remaining within the underlying object.
  242. static MemoryLocation
  243. getBeforeOrAfter(const Value *Ptr, const AAMDNodes &AATags = AAMDNodes()) {
  244. return MemoryLocation(Ptr, LocationSize::beforeOrAfterPointer(), AATags);
  245. }
  246. // Return the exact size if the exact size is known at compiletime,
  247. // otherwise return MemoryLocation::UnknownSize.
  248. static uint64_t getSizeOrUnknown(const TypeSize &T) {
  249. return T.isScalable() ? UnknownSize : T.getFixedValue();
  250. }
  251. MemoryLocation() : Ptr(nullptr), Size(LocationSize::beforeOrAfterPointer()) {}
  252. explicit MemoryLocation(const Value *Ptr, LocationSize Size,
  253. const AAMDNodes &AATags = AAMDNodes())
  254. : Ptr(Ptr), Size(Size), AATags(AATags) {}
  255. MemoryLocation getWithNewPtr(const Value *NewPtr) const {
  256. MemoryLocation Copy(*this);
  257. Copy.Ptr = NewPtr;
  258. return Copy;
  259. }
  260. MemoryLocation getWithNewSize(LocationSize NewSize) const {
  261. MemoryLocation Copy(*this);
  262. Copy.Size = NewSize;
  263. return Copy;
  264. }
  265. MemoryLocation getWithoutAATags() const {
  266. MemoryLocation Copy(*this);
  267. Copy.AATags = AAMDNodes();
  268. return Copy;
  269. }
  270. bool operator==(const MemoryLocation &Other) const {
  271. return Ptr == Other.Ptr && Size == Other.Size && AATags == Other.AATags;
  272. }
  273. };
  274. // Specialize DenseMapInfo.
  275. template <> struct DenseMapInfo<LocationSize> {
  276. static inline LocationSize getEmptyKey() {
  277. return LocationSize::mapEmpty();
  278. }
  279. static inline LocationSize getTombstoneKey() {
  280. return LocationSize::mapTombstone();
  281. }
  282. static unsigned getHashValue(const LocationSize &Val) {
  283. return DenseMapInfo<uint64_t>::getHashValue(Val.toRaw());
  284. }
  285. static bool isEqual(const LocationSize &LHS, const LocationSize &RHS) {
  286. return LHS == RHS;
  287. }
  288. };
  289. template <> struct DenseMapInfo<MemoryLocation> {
  290. static inline MemoryLocation getEmptyKey() {
  291. return MemoryLocation(DenseMapInfo<const Value *>::getEmptyKey(),
  292. DenseMapInfo<LocationSize>::getEmptyKey());
  293. }
  294. static inline MemoryLocation getTombstoneKey() {
  295. return MemoryLocation(DenseMapInfo<const Value *>::getTombstoneKey(),
  296. DenseMapInfo<LocationSize>::getTombstoneKey());
  297. }
  298. static unsigned getHashValue(const MemoryLocation &Val) {
  299. return DenseMapInfo<const Value *>::getHashValue(Val.Ptr) ^
  300. DenseMapInfo<LocationSize>::getHashValue(Val.Size) ^
  301. DenseMapInfo<AAMDNodes>::getHashValue(Val.AATags);
  302. }
  303. static bool isEqual(const MemoryLocation &LHS, const MemoryLocation &RHS) {
  304. return LHS == RHS;
  305. }
  306. };
  307. }
  308. #endif
  309. #ifdef __GNUC__
  310. #pragma GCC diagnostic pop
  311. #endif