PointerIntPair.h 7.8 KB

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