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 { return (IntType)Info::getInt(Value); }
60 void setPointer(PointerTy PtrVal) {
61 Value = Info::updatePointer(Value, PtrVal);
64 void setInt(IntType IntVal) { Value = Info::updateInt(Value, IntVal); }
66 void initWithPointer(PointerTy PtrVal) {
67 Value = Info::updatePointer(0, PtrVal);
70 void setPointerAndInt(PointerTy PtrVal, IntType IntVal) {
71 Value = Info::updateInt(Info::updatePointer(0, PtrVal), IntVal);
74 PointerTy const *getAddrOfPointer() const {
75 return const_cast<PointerIntPair *>(this)->getAddrOfPointer();
78 PointerTy *getAddrOfPointer() {
79 assert(Value == reinterpret_cast<intptr_t>(getPointer()) &&
80 "Can only return the address if IntBits is cleared and "
81 "PtrTraits doesn't change the pointer");
82 return reinterpret_cast<PointerTy *>(&Value);
85 void *getOpaqueValue() const { return reinterpret_cast<void *>(Value); }
86 void setFromOpaqueValue(void *Val) {
87 Value = reinterpret_cast<intptr_t>(Val);
90 static PointerIntPair getFromOpaqueValue(void *V) {
92 P.setFromOpaqueValue(V);
96 // Allow PointerIntPairs to be created from const void * if and only if the
97 // pointer type could be created from a const void *.
98 static PointerIntPair getFromOpaqueValue(const void *V) {
99 (void)PtrTraits::getFromVoidPointer(V);
100 return getFromOpaqueValue(const_cast<void *>(V));
103 bool operator==(const PointerIntPair &RHS) const {
104 return Value == RHS.Value;
106 bool operator!=(const PointerIntPair &RHS) const {
107 return Value != RHS.Value;
109 bool operator<(const PointerIntPair &RHS) const { return Value < RHS.Value; }
110 bool operator>(const PointerIntPair &RHS) const { return Value > RHS.Value; }
111 bool operator<=(const PointerIntPair &RHS) const {
112 return Value <= RHS.Value;
114 bool operator>=(const PointerIntPair &RHS) const {
115 return Value >= RHS.Value;
119 template <typename PointerT, unsigned IntBits, typename PtrTraits>
120 struct PointerIntPairInfo {
121 static_assert(PtrTraits::NumLowBitsAvailable <
122 std::numeric_limits<uintptr_t>::digits,
123 "cannot use a pointer type that has all bits free");
124 static_assert(IntBits <= PtrTraits::NumLowBitsAvailable,
125 "PointerIntPair with integer size too large for pointer");
127 /// PointerBitMask - The bits that come from the pointer.
129 ~(uintptr_t)(((intptr_t)1 << PtrTraits::NumLowBitsAvailable) - 1),
131 /// IntShift - The number of low bits that we reserve for other uses, and
133 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),
138 // ShiftedIntMask - This is the bits for the integer shifted in place.
139 ShiftedIntMask = (uintptr_t)(IntMask << IntShift)
142 static PointerT getPointer(intptr_t Value) {
143 return PtrTraits::getFromVoidPointer(
144 reinterpret_cast<void *>(Value & PointerBitMask));
147 static intptr_t getInt(intptr_t Value) {
148 return (Value >> IntShift) & IntMask;
151 static intptr_t updatePointer(intptr_t OrigValue, PointerT Ptr) {
153 reinterpret_cast<intptr_t>(PtrTraits::getAsVoidPointer(Ptr));
154 assert((PtrWord & ~PointerBitMask) == 0 &&
155 "Pointer is not sufficiently aligned");
156 // Preserve all low bits, just update the pointer.
157 return PtrWord | (OrigValue & ~PointerBitMask);
160 static intptr_t updateInt(intptr_t OrigValue, intptr_t Int) {
161 intptr_t IntWord = static_cast<intptr_t>(Int);
162 assert((IntWord & ~IntMask) == 0 && "Integer too large for field");
164 // Preserve all bits other than the ones we are updating.
165 return (OrigValue & ~ShiftedIntMask) | IntWord << IntShift;
169 template <typename T> struct isPodLike;
170 template <typename PointerTy, unsigned IntBits, typename IntType>
171 struct isPodLike<PointerIntPair<PointerTy, IntBits, IntType>> {
172 static const bool value = true;
175 // Provide specialization of DenseMapInfo for PointerIntPair.
176 template <typename PointerTy, unsigned IntBits, typename IntType>
177 struct DenseMapInfo<PointerIntPair<PointerTy, IntBits, IntType>> {
178 typedef PointerIntPair<PointerTy, IntBits, IntType> Ty;
179 static Ty getEmptyKey() {
180 uintptr_t Val = static_cast<uintptr_t>(-1);
181 Val <<= PointerLikeTypeTraits<Ty>::NumLowBitsAvailable;
182 return Ty::getFromOpaqueValue(reinterpret_cast<void *>(Val));
184 static Ty getTombstoneKey() {
185 uintptr_t Val = static_cast<uintptr_t>(-2);
186 Val <<= PointerLikeTypeTraits<PointerTy>::NumLowBitsAvailable;
187 return Ty::getFromOpaqueValue(reinterpret_cast<void *>(Val));
189 static unsigned getHashValue(Ty V) {
190 uintptr_t IV = reinterpret_cast<uintptr_t>(V.getOpaqueValue());
191 return unsigned(IV) ^ unsigned(IV >> 9);
193 static bool isEqual(const Ty &LHS, const Ty &RHS) { return LHS == RHS; }
196 // Teach SmallPtrSet that PointerIntPair is "basically a pointer".
197 template <typename PointerTy, unsigned IntBits, typename IntType,
199 class PointerLikeTypeTraits<
200 PointerIntPair<PointerTy, IntBits, IntType, PtrTraits>> {
203 getAsVoidPointer(const PointerIntPair<PointerTy, IntBits, IntType> &P) {
204 return P.getOpaqueValue();
206 static inline PointerIntPair<PointerTy, IntBits, IntType>
207 getFromVoidPointer(void *P) {
208 return PointerIntPair<PointerTy, IntBits, IntType>::getFromOpaqueValue(P);
210 static inline PointerIntPair<PointerTy, IntBits, IntType>
211 getFromVoidPointer(const void *P) {
212 return PointerIntPair<PointerTy, IntBits, IntType>::getFromOpaqueValue(P);
214 enum { NumLowBitsAvailable = PtrTraits::NumLowBitsAvailable - IntBits };
217 } // end namespace llvm