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"
27 /// PointerIntPair - This class implements a pair of a pointer and small
28 /// integer. It is designed to represent this in the space required by one
29 /// pointer by bitmangling the integer into the low part of the pointer. This
30 /// can only be done for small integers: typically up to 3 bits, but it depends
31 /// on the number of bits available according to PointerLikeTypeTraits for the
34 /// Note that PointerIntPair always puts the IntVal part in the highest bits
35 /// possible. For example, PointerIntPair<void*, 1, bool> will put the bit for
36 /// the bool into bit #2, not bit #0, which allows the low two bits to be used
37 /// for something else. For example, this allows:
38 /// PointerIntPair<PointerIntPair<void*, 1, bool>, 1, bool>
39 /// ... and the two bools will land in different bits.
41 template <typename PointerTy, unsigned IntBits, typename IntType=unsigned,
42 typename PtrTraits = PointerLikeTypeTraits<PointerTy> >
43 class PointerIntPair {
45 static_assert(PtrTraits::NumLowBitsAvailable <
46 std::numeric_limits<uintptr_t>::digits,
47 "cannot use a pointer type that has all bits free");
48 static_assert(IntBits <= PtrTraits::NumLowBitsAvailable,
49 "PointerIntPair with integer size too large for pointer");
51 /// PointerBitMask - The bits that come from the pointer.
53 ~(uintptr_t)(((intptr_t)1 << PtrTraits::NumLowBitsAvailable)-1),
55 /// IntShift - The number of low bits that we reserve for other uses, and
57 IntShift = (uintptr_t)PtrTraits::NumLowBitsAvailable-IntBits,
59 /// IntMask - This is the unshifted mask for valid bits of the int type.
60 IntMask = (uintptr_t)(((intptr_t)1 << IntBits)-1),
62 // ShiftedIntMask - This is the bits for the integer shifted in place.
63 ShiftedIntMask = (uintptr_t)(IntMask << IntShift)
67 PointerIntPair() : Value(0) {}
68 PointerIntPair(PointerTy PtrVal, IntType IntVal) {
69 setPointerAndInt(PtrVal, IntVal);
71 explicit PointerIntPair(PointerTy PtrVal) {
72 initWithPointer(PtrVal);
75 PointerTy getPointer() const {
76 return PtrTraits::getFromVoidPointer(
77 reinterpret_cast<void*>(Value & PointerBitMask));
80 IntType getInt() const {
81 return (IntType)((Value >> IntShift) & IntMask);
84 void setPointer(PointerTy PtrVal) {
86 = reinterpret_cast<intptr_t>(PtrTraits::getAsVoidPointer(PtrVal));
87 assert((PtrWord & ~PointerBitMask) == 0 &&
88 "Pointer is not sufficiently aligned");
89 // Preserve all low bits, just update the pointer.
90 Value = PtrWord | (Value & ~PointerBitMask);
93 void setInt(IntType IntVal) {
94 intptr_t IntWord = static_cast<intptr_t>(IntVal);
95 assert((IntWord & ~IntMask) == 0 && "Integer too large for field");
97 // Preserve all bits other than the ones we are updating.
98 Value &= ~ShiftedIntMask; // Remove integer field.
99 Value |= IntWord << IntShift; // Set new integer.
102 void initWithPointer(PointerTy PtrVal) {
104 = reinterpret_cast<intptr_t>(PtrTraits::getAsVoidPointer(PtrVal));
105 assert((PtrWord & ~PointerBitMask) == 0 &&
106 "Pointer is not sufficiently aligned");
110 void setPointerAndInt(PointerTy PtrVal, IntType IntVal) {
112 = reinterpret_cast<intptr_t>(PtrTraits::getAsVoidPointer(PtrVal));
113 assert((PtrWord & ~PointerBitMask) == 0 &&
114 "Pointer is not sufficiently aligned");
115 intptr_t IntWord = static_cast<intptr_t>(IntVal);
116 assert((IntWord & ~IntMask) == 0 && "Integer too large for field");
118 Value = PtrWord | (IntWord << IntShift);
121 PointerTy const *getAddrOfPointer() const {
122 return const_cast<PointerIntPair *>(this)->getAddrOfPointer();
125 PointerTy *getAddrOfPointer() {
126 assert(Value == reinterpret_cast<intptr_t>(getPointer()) &&
127 "Can only return the address if IntBits is cleared and "
128 "PtrTraits doesn't change the pointer");
129 return reinterpret_cast<PointerTy *>(&Value);
132 void *getOpaqueValue() const { return reinterpret_cast<void*>(Value); }
133 void setFromOpaqueValue(void *Val) { Value = reinterpret_cast<intptr_t>(Val);}
135 static PointerIntPair getFromOpaqueValue(void *V) {
136 PointerIntPair P; P.setFromOpaqueValue(V); return P;
139 // Allow PointerIntPairs to be created from const void * if and only if the
140 // pointer type could be created from a const void *.
141 static PointerIntPair getFromOpaqueValue(const void *V) {
142 (void)PtrTraits::getFromVoidPointer(V);
143 return getFromOpaqueValue(const_cast<void *>(V));
146 bool operator==(const PointerIntPair &RHS) const {return Value == RHS.Value;}
147 bool operator!=(const PointerIntPair &RHS) const {return Value != RHS.Value;}
148 bool operator<(const PointerIntPair &RHS) const {return Value < RHS.Value;}
149 bool operator>(const PointerIntPair &RHS) const {return Value > RHS.Value;}
150 bool operator<=(const PointerIntPair &RHS) const {return Value <= RHS.Value;}
151 bool operator>=(const PointerIntPair &RHS) const {return Value >= RHS.Value;}
154 template <typename T> struct isPodLike;
155 template<typename PointerTy, unsigned IntBits, typename IntType>
156 struct isPodLike<PointerIntPair<PointerTy, IntBits, IntType> > {
157 static const bool value = true;
160 // Provide specialization of DenseMapInfo for PointerIntPair.
161 template<typename PointerTy, unsigned IntBits, typename IntType>
162 struct DenseMapInfo<PointerIntPair<PointerTy, IntBits, IntType> > {
163 typedef PointerIntPair<PointerTy, IntBits, IntType> Ty;
164 static Ty getEmptyKey() {
165 uintptr_t Val = static_cast<uintptr_t>(-1);
166 Val <<= PointerLikeTypeTraits<Ty>::NumLowBitsAvailable;
167 return Ty::getFromOpaqueValue(reinterpret_cast<void *>(Val));
169 static Ty getTombstoneKey() {
170 uintptr_t Val = static_cast<uintptr_t>(-2);
171 Val <<= PointerLikeTypeTraits<PointerTy>::NumLowBitsAvailable;
172 return Ty::getFromOpaqueValue(reinterpret_cast<void *>(Val));
174 static unsigned getHashValue(Ty V) {
175 uintptr_t IV = reinterpret_cast<uintptr_t>(V.getOpaqueValue());
176 return unsigned(IV) ^ unsigned(IV >> 9);
178 static bool isEqual(const Ty &LHS, const Ty &RHS) { return LHS == RHS; }
181 // Teach SmallPtrSet that PointerIntPair is "basically a pointer".
182 template<typename PointerTy, unsigned IntBits, typename IntType,
184 class PointerLikeTypeTraits<PointerIntPair<PointerTy, IntBits, IntType,
188 getAsVoidPointer(const PointerIntPair<PointerTy, IntBits, IntType> &P) {
189 return P.getOpaqueValue();
191 static inline PointerIntPair<PointerTy, IntBits, IntType>
192 getFromVoidPointer(void *P) {
193 return PointerIntPair<PointerTy, IntBits, IntType>::getFromOpaqueValue(P);
195 static inline PointerIntPair<PointerTy, IntBits, IntType>
196 getFromVoidPointer(const void *P) {
197 return PointerIntPair<PointerTy, IntBits, IntType>::getFromOpaqueValue(P);
200 NumLowBitsAvailable = PtrTraits::NumLowBitsAvailable - IntBits
204 } // end namespace llvm