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/Compiler.h"
18 #include "llvm/Support/PointerLikeTypeTraits.h"
24 template <typename T> struct DenseMapInfo;
26 template <typename PointerT, unsigned IntBits, typename PtrTraits>
27 struct PointerIntPairInfo;
29 /// PointerIntPair - This class implements a pair of a pointer and small
30 /// integer. It is designed to represent this in the space required by one
31 /// pointer by bitmangling the integer into the low part of the pointer. This
32 /// can only be done for small integers: typically up to 3 bits, but it depends
33 /// on the number of bits available according to PointerLikeTypeTraits for the
36 /// Note that PointerIntPair always puts the IntVal part in the highest bits
37 /// possible. For example, PointerIntPair<void*, 1, bool> will put the bit for
38 /// the bool into bit #2, not bit #0, which allows the low two bits to be used
39 /// for something else. For example, this allows:
40 /// PointerIntPair<PointerIntPair<void*, 1, bool>, 1, bool>
41 /// ... 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 {
50 PointerIntPair() : Value(0) {}
51 PointerIntPair(PointerTy PtrVal, IntType IntVal) {
52 setPointerAndInt(PtrVal, IntVal);
54 explicit PointerIntPair(PointerTy PtrVal) { initWithPointer(PtrVal); }
56 PointerTy getPointer() const { return Info::getPointer(Value); }
58 IntType getInt() const {
59 return (IntType)Info::getInt(Value);
62 void setPointer(PointerTy PtrVal) {
63 Value = Info::updatePointer(Value, PtrVal);
66 void setInt(IntType IntVal) {
67 Value = Info::updateInt(Value, static_cast<intptr_t>(IntVal));
70 void initWithPointer(PointerTy PtrVal) {
71 Value = Info::updatePointer(0, PtrVal);
74 void setPointerAndInt(PointerTy PtrVal, IntType IntVal) {
75 Value = Info::updateInt(Info::updatePointer(0, PtrVal),
76 static_cast<intptr_t>(IntVal));
79 PointerTy const *getAddrOfPointer() const {
80 return const_cast<PointerIntPair *>(this)->getAddrOfPointer();
83 PointerTy *getAddrOfPointer() {
84 assert(Value == reinterpret_cast<intptr_t>(getPointer()) &&
85 "Can only return the address if IntBits is cleared and "
86 "PtrTraits doesn't change the pointer");
87 return reinterpret_cast<PointerTy *>(&Value);
90 void *getOpaqueValue() const { return reinterpret_cast<void *>(Value); }
91 void setFromOpaqueValue(void *Val) {
92 Value = reinterpret_cast<intptr_t>(Val);
95 static PointerIntPair getFromOpaqueValue(void *V) {
97 P.setFromOpaqueValue(V);
101 // Allow PointerIntPairs to be created from const void * if and only if the
102 // pointer type could be created from a const void *.
103 static PointerIntPair getFromOpaqueValue(const void *V) {
104 (void)PtrTraits::getFromVoidPointer(V);
105 return getFromOpaqueValue(const_cast<void *>(V));
108 bool operator==(const PointerIntPair &RHS) const {
109 return Value == RHS.Value;
111 bool operator!=(const PointerIntPair &RHS) const {
112 return Value != RHS.Value;
114 bool operator<(const PointerIntPair &RHS) const { return Value < RHS.Value; }
115 bool operator>(const PointerIntPair &RHS) const { return Value > RHS.Value; }
116 bool operator<=(const PointerIntPair &RHS) const {
117 return Value <= RHS.Value;
119 bool operator>=(const PointerIntPair &RHS) const {
120 return Value >= RHS.Value;
124 template <typename PointerT, unsigned IntBits, typename PtrTraits>
125 struct PointerIntPairInfo {
126 static_assert(PtrTraits::NumLowBitsAvailable <
127 std::numeric_limits<uintptr_t>::digits,
128 "cannot use a pointer type that has all bits free");
129 static_assert(IntBits <= PtrTraits::NumLowBitsAvailable,
130 "PointerIntPair with integer size too large for pointer");
132 /// PointerBitMask - The bits that come from the pointer.
134 ~(uintptr_t)(((intptr_t)1 << PtrTraits::NumLowBitsAvailable) - 1),
136 /// IntShift - The number of low bits that we reserve for other uses, and
138 IntShift = (uintptr_t)PtrTraits::NumLowBitsAvailable - IntBits,
140 /// IntMask - This is the unshifted mask for valid bits of the int type.
141 IntMask = (uintptr_t)(((intptr_t)1 << IntBits) - 1),
143 // ShiftedIntMask - This is the bits for the integer shifted in place.
144 ShiftedIntMask = (uintptr_t)(IntMask << IntShift)
147 static PointerT getPointer(intptr_t Value) {
148 return PtrTraits::getFromVoidPointer(
149 reinterpret_cast<void *>(Value & PointerBitMask));
152 static intptr_t getInt(intptr_t Value) {
153 return (Value >> IntShift) & IntMask;
156 static intptr_t updatePointer(intptr_t OrigValue, PointerT Ptr) {
158 reinterpret_cast<intptr_t>(PtrTraits::getAsVoidPointer(Ptr));
159 assert((PtrWord & ~PointerBitMask) == 0 &&
160 "Pointer is not sufficiently aligned");
161 // Preserve all low bits, just update the pointer.
162 return PtrWord | (OrigValue & ~PointerBitMask);
165 static intptr_t updateInt(intptr_t OrigValue, intptr_t Int) {
166 intptr_t IntWord = static_cast<intptr_t>(Int);
167 assert((IntWord & ~IntMask) == 0 && "Integer too large for field");
169 // Preserve all bits other than the ones we are updating.
170 return (OrigValue & ~ShiftedIntMask) | IntWord << IntShift;
174 template <typename T> struct isPodLike;
175 template <typename PointerTy, unsigned IntBits, typename IntType>
176 struct isPodLike<PointerIntPair<PointerTy, IntBits, IntType>> {
177 static const bool value = true;
180 // Provide specialization of DenseMapInfo for PointerIntPair.
181 template <typename PointerTy, unsigned IntBits, typename IntType>
182 struct DenseMapInfo<PointerIntPair<PointerTy, IntBits, IntType>> {
183 typedef PointerIntPair<PointerTy, IntBits, IntType> Ty;
184 static Ty getEmptyKey() {
185 uintptr_t Val = static_cast<uintptr_t>(-1);
186 Val <<= PointerLikeTypeTraits<Ty>::NumLowBitsAvailable;
187 return Ty::getFromOpaqueValue(reinterpret_cast<void *>(Val));
189 static Ty getTombstoneKey() {
190 uintptr_t Val = static_cast<uintptr_t>(-2);
191 Val <<= PointerLikeTypeTraits<PointerTy>::NumLowBitsAvailable;
192 return Ty::getFromOpaqueValue(reinterpret_cast<void *>(Val));
194 static unsigned getHashValue(Ty V) {
195 uintptr_t IV = reinterpret_cast<uintptr_t>(V.getOpaqueValue());
196 return unsigned(IV) ^ unsigned(IV >> 9);
198 static bool isEqual(const Ty &LHS, const Ty &RHS) { return LHS == RHS; }
201 // Teach SmallPtrSet that PointerIntPair is "basically a pointer".
202 template <typename PointerTy, unsigned IntBits, typename IntType,
204 class PointerLikeTypeTraits<
205 PointerIntPair<PointerTy, IntBits, IntType, PtrTraits>> {
208 getAsVoidPointer(const PointerIntPair<PointerTy, IntBits, IntType> &P) {
209 return P.getOpaqueValue();
211 static inline PointerIntPair<PointerTy, IntBits, IntType>
212 getFromVoidPointer(void *P) {
213 return PointerIntPair<PointerTy, IntBits, IntType>::getFromOpaqueValue(P);
215 static inline PointerIntPair<PointerTy, IntBits, IntType>
216 getFromVoidPointer(const void *P) {
217 return PointerIntPair<PointerTy, IntBits, IntType>::getFromOpaqueValue(P);
219 enum { NumLowBitsAvailable = PtrTraits::NumLowBitsAvailable - IntBits };
222 } // end namespace llvm