1 //===- llvm/ADT/PointerIntPair.h - Pair for pointer and int -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the PointerIntPair class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_POINTERINTPAIR_H
15 #define LLVM_ADT_POINTERINTPAIR_H
17 #include "llvm/Support/PointerLikeTypeTraits.h"
24 template <typename T> struct DenseMapInfo;
25 template <typename PointerT, unsigned IntBits, typename PtrTraits>
26 struct PointerIntPairInfo;
28 /// PointerIntPair - This class implements a pair of a pointer and small
29 /// integer. It is designed to represent this in the space required by one
30 /// pointer by bitmangling the integer into the low part of the pointer. This
31 /// can only be done for small integers: typically up to 3 bits, but it depends
32 /// on the number of bits available according to PointerLikeTypeTraits for the
35 /// Note that PointerIntPair always puts the IntVal part in the highest bits
36 /// possible. For example, PointerIntPair<void*, 1, bool> will put the bit for
37 /// the bool into bit #2, not bit #0, which allows the low two bits to be used
38 /// for something else. For example, this allows:
39 /// PointerIntPair<PointerIntPair<void*, 1, bool>, 1, bool>
40 /// ... and the two bools will land in different bits.
41 template <typename PointerTy, unsigned IntBits, typename IntType = unsigned,
42 typename PtrTraits = PointerLikeTypeTraits<PointerTy>,
43 typename Info = PointerIntPairInfo<PointerTy, IntBits, PtrTraits>>
44 class PointerIntPair {
48 constexpr PointerIntPair() = default;
50 PointerIntPair(PointerTy PtrVal, IntType IntVal) {
51 setPointerAndInt(PtrVal, IntVal);
54 explicit PointerIntPair(PointerTy PtrVal) { initWithPointer(PtrVal); }
56 PointerTy getPointer() const { return Info::getPointer(Value); }
58 IntType getInt() const { return (IntType)Info::getInt(Value); }
60 void setPointer(PointerTy PtrVal) {
61 Value = Info::updatePointer(Value, PtrVal);
64 void setInt(IntType IntVal) {
65 Value = Info::updateInt(Value, static_cast<intptr_t>(IntVal));
68 void initWithPointer(PointerTy PtrVal) {
69 Value = Info::updatePointer(0, PtrVal);
72 void setPointerAndInt(PointerTy PtrVal, IntType IntVal) {
73 Value = Info::updateInt(Info::updatePointer(0, PtrVal),
74 static_cast<intptr_t>(IntVal));
77 PointerTy const *getAddrOfPointer() const {
78 return const_cast<PointerIntPair *>(this)->getAddrOfPointer();
81 PointerTy *getAddrOfPointer() {
82 assert(Value == reinterpret_cast<intptr_t>(getPointer()) &&
83 "Can only return the address if IntBits is cleared and "
84 "PtrTraits doesn't change the pointer");
85 return reinterpret_cast<PointerTy *>(&Value);
88 void *getOpaqueValue() const { return reinterpret_cast<void *>(Value); }
90 void setFromOpaqueValue(void *Val) {
91 Value = reinterpret_cast<intptr_t>(Val);
94 static PointerIntPair getFromOpaqueValue(void *V) {
96 P.setFromOpaqueValue(V);
100 // Allow PointerIntPairs to be created from const void * if and only if the
101 // pointer type could be created from a const void *.
102 static PointerIntPair getFromOpaqueValue(const void *V) {
103 (void)PtrTraits::getFromVoidPointer(V);
104 return getFromOpaqueValue(const_cast<void *>(V));
107 bool operator==(const PointerIntPair &RHS) const {
108 return Value == RHS.Value;
111 bool operator!=(const PointerIntPair &RHS) const {
112 return Value != RHS.Value;
115 bool operator<(const PointerIntPair &RHS) const { return Value < RHS.Value; }
116 bool operator>(const PointerIntPair &RHS) const { return Value > RHS.Value; }
118 bool operator<=(const PointerIntPair &RHS) const {
119 return Value <= RHS.Value;
122 bool operator>=(const PointerIntPair &RHS) const {
123 return Value >= RHS.Value;
127 template <typename PointerT, unsigned IntBits, typename PtrTraits>
128 struct PointerIntPairInfo {
129 static_assert(PtrTraits::NumLowBitsAvailable <
130 std::numeric_limits<uintptr_t>::digits,
131 "cannot use a pointer type that has all bits free");
132 static_assert(IntBits <= PtrTraits::NumLowBitsAvailable,
133 "PointerIntPair with integer size too large for pointer");
135 /// PointerBitMask - The bits that come from the pointer.
137 ~(uintptr_t)(((intptr_t)1 << PtrTraits::NumLowBitsAvailable) - 1),
139 /// IntShift - The number of low bits that we reserve for other uses, and
141 IntShift = (uintptr_t)PtrTraits::NumLowBitsAvailable - IntBits,
143 /// IntMask - This is the unshifted mask for valid bits of the int type.
144 IntMask = (uintptr_t)(((intptr_t)1 << IntBits) - 1),
146 // ShiftedIntMask - This is the bits for the integer shifted in place.
147 ShiftedIntMask = (uintptr_t)(IntMask << IntShift)
150 static PointerT getPointer(intptr_t Value) {
151 return PtrTraits::getFromVoidPointer(
152 reinterpret_cast<void *>(Value & PointerBitMask));
155 static intptr_t getInt(intptr_t Value) {
156 return (Value >> IntShift) & IntMask;
159 static intptr_t updatePointer(intptr_t OrigValue, PointerT Ptr) {
161 reinterpret_cast<intptr_t>(PtrTraits::getAsVoidPointer(Ptr));
162 assert((PtrWord & ~PointerBitMask) == 0 &&
163 "Pointer is not sufficiently aligned");
164 // Preserve all low bits, just update the pointer.
165 return PtrWord | (OrigValue & ~PointerBitMask);
168 static intptr_t updateInt(intptr_t OrigValue, intptr_t Int) {
169 intptr_t IntWord = static_cast<intptr_t>(Int);
170 assert((IntWord & ~IntMask) == 0 && "Integer too large for field");
172 // Preserve all bits other than the ones we are updating.
173 return (OrigValue & ~ShiftedIntMask) | IntWord << IntShift;
177 template <typename T> struct isPodLike;
178 template <typename PointerTy, unsigned IntBits, typename IntType>
179 struct isPodLike<PointerIntPair<PointerTy, IntBits, IntType>> {
180 static const bool value = true;
183 // Provide specialization of DenseMapInfo for PointerIntPair.
184 template <typename PointerTy, unsigned IntBits, typename IntType>
185 struct DenseMapInfo<PointerIntPair<PointerTy, IntBits, IntType>> {
186 using Ty = PointerIntPair<PointerTy, IntBits, IntType>;
188 static Ty getEmptyKey() {
189 uintptr_t Val = static_cast<uintptr_t>(-1);
190 Val <<= PointerLikeTypeTraits<Ty>::NumLowBitsAvailable;
191 return Ty::getFromOpaqueValue(reinterpret_cast<void *>(Val));
194 static Ty getTombstoneKey() {
195 uintptr_t Val = static_cast<uintptr_t>(-2);
196 Val <<= PointerLikeTypeTraits<PointerTy>::NumLowBitsAvailable;
197 return Ty::getFromOpaqueValue(reinterpret_cast<void *>(Val));
200 static unsigned getHashValue(Ty V) {
201 uintptr_t IV = reinterpret_cast<uintptr_t>(V.getOpaqueValue());
202 return unsigned(IV) ^ unsigned(IV >> 9);
205 static bool isEqual(const Ty &LHS, const Ty &RHS) { return LHS == RHS; }
208 // Teach SmallPtrSet that PointerIntPair is "basically a pointer".
209 template <typename PointerTy, unsigned IntBits, typename IntType,
211 struct PointerLikeTypeTraits<
212 PointerIntPair<PointerTy, IntBits, IntType, PtrTraits>> {
214 getAsVoidPointer(const PointerIntPair<PointerTy, IntBits, IntType> &P) {
215 return P.getOpaqueValue();
218 static inline PointerIntPair<PointerTy, IntBits, IntType>
219 getFromVoidPointer(void *P) {
220 return PointerIntPair<PointerTy, IntBits, IntType>::getFromOpaqueValue(P);
223 static inline PointerIntPair<PointerTy, IntBits, IntType>
224 getFromVoidPointer(const void *P) {
225 return PointerIntPair<PointerTy, IntBits, IntType>::getFromOpaqueValue(P);
228 enum { NumLowBitsAvailable = PtrTraits::NumLowBitsAvailable - IntBits };
231 } // end namespace llvm
233 #endif // LLVM_ADT_POINTERINTPAIR_H