PointerIntPair.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //===- llvm/ADT/PointerIntPair.h - Pair for pointer and int -----*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines the PointerIntPair class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_ADT_POINTERINTPAIR_H
  13. #define LLVM_ADT_POINTERINTPAIR_H
  14. #include "llvm/Support/Compiler.h"
  15. #include "llvm/Support/PointerLikeTypeTraits.h"
  16. #include "llvm/Support/type_traits.h"
  17. #include <cassert>
  18. #include <cstdint>
  19. #include <limits>
  20. namespace llvm {
  21. template <typename T> struct DenseMapInfo;
  22. template <typename PointerT, unsigned IntBits, typename PtrTraits>
  23. struct PointerIntPairInfo;
  24. /// PointerIntPair - This class implements a pair of a pointer and small
  25. /// integer. It is designed to represent this in the space required by one
  26. /// pointer by bitmangling the integer into the low part of the pointer. This
  27. /// can only be done for small integers: typically up to 3 bits, but it depends
  28. /// on the number of bits available according to PointerLikeTypeTraits for the
  29. /// type.
  30. ///
  31. /// Note that PointerIntPair always puts the IntVal part in the highest bits
  32. /// possible. For example, PointerIntPair<void*, 1, bool> will put the bit for
  33. /// the bool into bit #2, not bit #0, which allows the low two bits to be used
  34. /// for something else. For example, this allows:
  35. /// PointerIntPair<PointerIntPair<void*, 1, bool>, 1, bool>
  36. /// ... and the two bools will land in different bits.
  37. template <typename PointerTy, unsigned IntBits, typename IntType = unsigned,
  38. typename PtrTraits = PointerLikeTypeTraits<PointerTy>,
  39. typename Info = PointerIntPairInfo<PointerTy, IntBits, PtrTraits>>
  40. class PointerIntPair {
  41. // Used by MSVC visualizer and generally helpful for debugging/visualizing.
  42. using InfoTy = Info;
  43. intptr_t Value = 0;
  44. public:
  45. constexpr PointerIntPair() = default;
  46. PointerIntPair(PointerTy PtrVal, IntType IntVal) {
  47. setPointerAndInt(PtrVal, IntVal);
  48. }
  49. explicit PointerIntPair(PointerTy PtrVal) { initWithPointer(PtrVal); }
  50. PointerTy getPointer() const { return Info::getPointer(Value); }
  51. IntType getInt() const { return (IntType)Info::getInt(Value); }
  52. void setPointer(PointerTy PtrVal) LLVM_LVALUE_FUNCTION {
  53. Value = Info::updatePointer(Value, PtrVal);
  54. }
  55. void setInt(IntType IntVal) LLVM_LVALUE_FUNCTION {
  56. Value = Info::updateInt(Value, static_cast<intptr_t>(IntVal));
  57. }
  58. void initWithPointer(PointerTy PtrVal) LLVM_LVALUE_FUNCTION {
  59. Value = Info::updatePointer(0, PtrVal);
  60. }
  61. void setPointerAndInt(PointerTy PtrVal, IntType IntVal) LLVM_LVALUE_FUNCTION {
  62. Value = Info::updateInt(Info::updatePointer(0, PtrVal),
  63. static_cast<intptr_t>(IntVal));
  64. }
  65. PointerTy const *getAddrOfPointer() const {
  66. return const_cast<PointerIntPair *>(this)->getAddrOfPointer();
  67. }
  68. PointerTy *getAddrOfPointer() {
  69. assert(Value == reinterpret_cast<intptr_t>(getPointer()) &&
  70. "Can only return the address if IntBits is cleared and "
  71. "PtrTraits doesn't change the pointer");
  72. return reinterpret_cast<PointerTy *>(&Value);
  73. }
  74. void *getOpaqueValue() const { return reinterpret_cast<void *>(Value); }
  75. void setFromOpaqueValue(void *Val) LLVM_LVALUE_FUNCTION {
  76. Value = reinterpret_cast<intptr_t>(Val);
  77. }
  78. static PointerIntPair getFromOpaqueValue(void *V) {
  79. PointerIntPair P;
  80. P.setFromOpaqueValue(V);
  81. return P;
  82. }
  83. // Allow PointerIntPairs to be created from const void * if and only if the
  84. // pointer type could be created from a const void *.
  85. static PointerIntPair getFromOpaqueValue(const void *V) {
  86. (void)PtrTraits::getFromVoidPointer(V);
  87. return getFromOpaqueValue(const_cast<void *>(V));
  88. }
  89. bool operator==(const PointerIntPair &RHS) const {
  90. return Value == RHS.Value;
  91. }
  92. bool operator!=(const PointerIntPair &RHS) const {
  93. return Value != RHS.Value;
  94. }
  95. bool operator<(const PointerIntPair &RHS) const { return Value < RHS.Value; }
  96. bool operator>(const PointerIntPair &RHS) const { return Value > RHS.Value; }
  97. bool operator<=(const PointerIntPair &RHS) const {
  98. return Value <= RHS.Value;
  99. }
  100. bool operator>=(const PointerIntPair &RHS) const {
  101. return Value >= RHS.Value;
  102. }
  103. };
  104. // Specialize is_trivially_copyable to avoid limitation of llvm::is_trivially_copyable
  105. // when compiled with gcc 4.9.
  106. template <typename PointerTy, unsigned IntBits, typename IntType,
  107. typename PtrTraits,
  108. typename Info>
  109. struct is_trivially_copyable<PointerIntPair<PointerTy, IntBits, IntType, PtrTraits, Info>> : std::true_type {
  110. #ifdef HAVE_STD_IS_TRIVIALLY_COPYABLE
  111. static_assert(std::is_trivially_copyable<PointerIntPair<PointerTy, IntBits, IntType, PtrTraits, Info>>::value,
  112. "inconsistent behavior between llvm:: and std:: implementation of is_trivially_copyable");
  113. #endif
  114. };
  115. template <typename PointerT, unsigned IntBits, typename PtrTraits>
  116. struct PointerIntPairInfo {
  117. static_assert(PtrTraits::NumLowBitsAvailable <
  118. std::numeric_limits<uintptr_t>::digits,
  119. "cannot use a pointer type that has all bits free");
  120. static_assert(IntBits <= PtrTraits::NumLowBitsAvailable,
  121. "PointerIntPair with integer size too large for pointer");
  122. enum MaskAndShiftConstants : uintptr_t {
  123. /// PointerBitMask - The bits that come from the pointer.
  124. PointerBitMask =
  125. ~(uintptr_t)(((intptr_t)1 << PtrTraits::NumLowBitsAvailable) - 1),
  126. /// IntShift - The number of low bits that we reserve for other uses, and
  127. /// keep zero.
  128. IntShift = (uintptr_t)PtrTraits::NumLowBitsAvailable - IntBits,
  129. /// IntMask - This is the unshifted mask for valid bits of the int type.
  130. IntMask = (uintptr_t)(((intptr_t)1 << IntBits) - 1),
  131. // ShiftedIntMask - This is the bits for the integer shifted in place.
  132. ShiftedIntMask = (uintptr_t)(IntMask << IntShift)
  133. };
  134. static PointerT getPointer(intptr_t Value) {
  135. return PtrTraits::getFromVoidPointer(
  136. reinterpret_cast<void *>(Value & PointerBitMask));
  137. }
  138. static intptr_t getInt(intptr_t Value) {
  139. return (Value >> IntShift) & IntMask;
  140. }
  141. static intptr_t updatePointer(intptr_t OrigValue, PointerT Ptr) {
  142. intptr_t PtrWord =
  143. reinterpret_cast<intptr_t>(PtrTraits::getAsVoidPointer(Ptr));
  144. assert((PtrWord & ~PointerBitMask) == 0 &&
  145. "Pointer is not sufficiently aligned");
  146. // Preserve all low bits, just update the pointer.
  147. return PtrWord | (OrigValue & ~PointerBitMask);
  148. }
  149. static intptr_t updateInt(intptr_t OrigValue, intptr_t Int) {
  150. intptr_t IntWord = static_cast<intptr_t>(Int);
  151. assert((IntWord & ~IntMask) == 0 && "Integer too large for field");
  152. // Preserve all bits other than the ones we are updating.
  153. return (OrigValue & ~ShiftedIntMask) | IntWord << IntShift;
  154. }
  155. };
  156. // Provide specialization of DenseMapInfo for PointerIntPair.
  157. template <typename PointerTy, unsigned IntBits, typename IntType>
  158. struct DenseMapInfo<PointerIntPair<PointerTy, IntBits, IntType>> {
  159. using Ty = PointerIntPair<PointerTy, IntBits, IntType>;
  160. static Ty getEmptyKey() {
  161. uintptr_t Val = static_cast<uintptr_t>(-1);
  162. Val <<= PointerLikeTypeTraits<Ty>::NumLowBitsAvailable;
  163. return Ty::getFromOpaqueValue(reinterpret_cast<void *>(Val));
  164. }
  165. static Ty getTombstoneKey() {
  166. uintptr_t Val = static_cast<uintptr_t>(-2);
  167. Val <<= PointerLikeTypeTraits<PointerTy>::NumLowBitsAvailable;
  168. return Ty::getFromOpaqueValue(reinterpret_cast<void *>(Val));
  169. }
  170. static unsigned getHashValue(Ty V) {
  171. uintptr_t IV = reinterpret_cast<uintptr_t>(V.getOpaqueValue());
  172. return unsigned(IV) ^ unsigned(IV >> 9);
  173. }
  174. static bool isEqual(const Ty &LHS, const Ty &RHS) { return LHS == RHS; }
  175. };
  176. // Teach SmallPtrSet that PointerIntPair is "basically a pointer".
  177. template <typename PointerTy, unsigned IntBits, typename IntType,
  178. typename PtrTraits>
  179. struct PointerLikeTypeTraits<
  180. PointerIntPair<PointerTy, IntBits, IntType, PtrTraits>> {
  181. static inline void *
  182. getAsVoidPointer(const PointerIntPair<PointerTy, IntBits, IntType> &P) {
  183. return P.getOpaqueValue();
  184. }
  185. static inline PointerIntPair<PointerTy, IntBits, IntType>
  186. getFromVoidPointer(void *P) {
  187. return PointerIntPair<PointerTy, IntBits, IntType>::getFromOpaqueValue(P);
  188. }
  189. static inline PointerIntPair<PointerTy, IntBits, IntType>
  190. getFromVoidPointer(const void *P) {
  191. return PointerIntPair<PointerTy, IntBits, IntType>::getFromOpaqueValue(P);
  192. }
  193. static constexpr int NumLowBitsAvailable =
  194. PtrTraits::NumLowBitsAvailable - IntBits;
  195. };
  196. } // end namespace llvm
  197. #endif // LLVM_ADT_POINTERINTPAIR_H