PointerIntPair.h 9.1 KB

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