PointerIntPair.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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) LLVM_LVALUE_FUNCTION {
  59. Value = Info::updatePointer(Value, PtrVal);
  60. }
  61. void setInt(IntType IntVal) LLVM_LVALUE_FUNCTION {
  62. Value = Info::updateInt(Value, static_cast<intptr_t>(IntVal));
  63. }
  64. void initWithPointer(PointerTy PtrVal) LLVM_LVALUE_FUNCTION {
  65. Value = Info::updatePointer(0, PtrVal);
  66. }
  67. void setPointerAndInt(PointerTy PtrVal, IntType IntVal) LLVM_LVALUE_FUNCTION {
  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) LLVM_LVALUE_FUNCTION {
  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. // Specialize is_trivially_copyable to avoid limitation of llvm::is_trivially_copyable
  111. // when compiled with gcc 4.9.
  112. template <typename PointerTy, unsigned IntBits, typename IntType,
  113. typename PtrTraits,
  114. typename Info>
  115. struct is_trivially_copyable<PointerIntPair<PointerTy, IntBits, IntType, PtrTraits, Info>> : std::true_type {
  116. #ifdef HAVE_STD_IS_TRIVIALLY_COPYABLE
  117. static_assert(std::is_trivially_copyable<PointerIntPair<PointerTy, IntBits, IntType, PtrTraits, Info>>::value,
  118. "inconsistent behavior between llvm:: and std:: implementation of is_trivially_copyable");
  119. #endif
  120. };
  121. template <typename PointerT, unsigned IntBits, typename PtrTraits>
  122. struct PointerIntPairInfo {
  123. static_assert(PtrTraits::NumLowBitsAvailable <
  124. std::numeric_limits<uintptr_t>::digits,
  125. "cannot use a pointer type that has all bits free");
  126. static_assert(IntBits <= PtrTraits::NumLowBitsAvailable,
  127. "PointerIntPair with integer size too large for pointer");
  128. enum MaskAndShiftConstants : uintptr_t {
  129. /// PointerBitMask - The bits that come from the pointer.
  130. PointerBitMask =
  131. ~(uintptr_t)(((intptr_t)1 << PtrTraits::NumLowBitsAvailable) - 1),
  132. /// IntShift - The number of low bits that we reserve for other uses, and
  133. /// keep zero.
  134. IntShift = (uintptr_t)PtrTraits::NumLowBitsAvailable - IntBits,
  135. /// IntMask - This is the unshifted mask for valid bits of the int type.
  136. IntMask = (uintptr_t)(((intptr_t)1 << IntBits) - 1),
  137. // ShiftedIntMask - This is the bits for the integer shifted in place.
  138. ShiftedIntMask = (uintptr_t)(IntMask << IntShift)
  139. };
  140. static PointerT getPointer(intptr_t Value) {
  141. return PtrTraits::getFromVoidPointer(
  142. reinterpret_cast<void *>(Value & PointerBitMask));
  143. }
  144. static intptr_t getInt(intptr_t Value) {
  145. return (Value >> IntShift) & IntMask;
  146. }
  147. static intptr_t updatePointer(intptr_t OrigValue, PointerT Ptr) {
  148. intptr_t PtrWord =
  149. reinterpret_cast<intptr_t>(PtrTraits::getAsVoidPointer(Ptr));
  150. assert((PtrWord & ~PointerBitMask) == 0 &&
  151. "Pointer is not sufficiently aligned");
  152. // Preserve all low bits, just update the pointer.
  153. return PtrWord | (OrigValue & ~PointerBitMask);
  154. }
  155. static intptr_t updateInt(intptr_t OrigValue, intptr_t Int) {
  156. intptr_t IntWord = static_cast<intptr_t>(Int);
  157. assert((IntWord & ~IntMask) == 0 && "Integer too large for field");
  158. // Preserve all bits other than the ones we are updating.
  159. return (OrigValue & ~ShiftedIntMask) | IntWord << IntShift;
  160. }
  161. };
  162. // Provide specialization of DenseMapInfo for PointerIntPair.
  163. template <typename PointerTy, unsigned IntBits, typename IntType>
  164. struct DenseMapInfo<PointerIntPair<PointerTy, IntBits, IntType>, void> {
  165. using Ty = PointerIntPair<PointerTy, IntBits, IntType>;
  166. static Ty getEmptyKey() {
  167. uintptr_t Val = static_cast<uintptr_t>(-1);
  168. Val <<= PointerLikeTypeTraits<Ty>::NumLowBitsAvailable;
  169. return Ty::getFromOpaqueValue(reinterpret_cast<void *>(Val));
  170. }
  171. static Ty getTombstoneKey() {
  172. uintptr_t Val = static_cast<uintptr_t>(-2);
  173. Val <<= PointerLikeTypeTraits<PointerTy>::NumLowBitsAvailable;
  174. return Ty::getFromOpaqueValue(reinterpret_cast<void *>(Val));
  175. }
  176. static unsigned getHashValue(Ty V) {
  177. uintptr_t IV = reinterpret_cast<uintptr_t>(V.getOpaqueValue());
  178. return unsigned(IV) ^ unsigned(IV >> 9);
  179. }
  180. static bool isEqual(const Ty &LHS, const Ty &RHS) { return LHS == RHS; }
  181. };
  182. // Teach SmallPtrSet that PointerIntPair is "basically a pointer".
  183. template <typename PointerTy, unsigned IntBits, typename IntType,
  184. typename PtrTraits>
  185. struct PointerLikeTypeTraits<
  186. PointerIntPair<PointerTy, IntBits, IntType, PtrTraits>> {
  187. static inline void *
  188. getAsVoidPointer(const PointerIntPair<PointerTy, IntBits, IntType> &P) {
  189. return P.getOpaqueValue();
  190. }
  191. static inline PointerIntPair<PointerTy, IntBits, IntType>
  192. getFromVoidPointer(void *P) {
  193. return PointerIntPair<PointerTy, IntBits, IntType>::getFromOpaqueValue(P);
  194. }
  195. static inline PointerIntPair<PointerTy, IntBits, IntType>
  196. getFromVoidPointer(const void *P) {
  197. return PointerIntPair<PointerTy, IntBits, IntType>::getFromOpaqueValue(P);
  198. }
  199. static constexpr int NumLowBitsAvailable =
  200. PtrTraits::NumLowBitsAvailable - IntBits;
  201. };
  202. } // end namespace llvm
  203. #endif // LLVM_ADT_POINTERINTPAIR_H
  204. #ifdef __GNUC__
  205. #pragma GCC diagnostic pop
  206. #endif