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 /// PointerIntPair - This class implements a pair of a pointer and small
27 /// integer. It is designed to represent this in the space required by one
28 /// pointer by bitmangling the integer into the low part of the pointer. This
29 /// can only be done for small integers: typically up to 3 bits, but it depends
30 /// on the number of bits available according to PointerLikeTypeTraits for the
33 /// Note that PointerIntPair always puts the IntVal part in the highest bits
34 /// possible. For example, PointerIntPair<void*, 1, bool> will put the bit for
35 /// the bool into bit #2, not bit #0, which allows the low two bits to be used
36 /// for something else. For example, this allows:
37 /// PointerIntPair<PointerIntPair<void*, 1, bool>, 1, bool>
38 /// ... and the two bools will land in different bits.
40 template <typename PointerTy, unsigned IntBits, typename IntType = unsigned,
41 typename PtrTraits = PointerLikeTypeTraits<PointerTy>>
42 class PointerIntPair {
44 static_assert(PtrTraits::NumLowBitsAvailable <
45 std::numeric_limits<uintptr_t>::digits,
46 "cannot use a pointer type that has all bits free");
47 static_assert(IntBits <= PtrTraits::NumLowBitsAvailable,
48 "PointerIntPair with integer size too large for pointer");
50 /// PointerBitMask - The bits that come from the pointer.
52 ~(uintptr_t)(((intptr_t)1 << PtrTraits::NumLowBitsAvailable) - 1),
54 /// IntShift - The number of low bits that we reserve for other uses, and
56 IntShift = (uintptr_t)PtrTraits::NumLowBitsAvailable - IntBits,
58 /// IntMask - This is the unshifted mask for valid bits of the int type.
59 IntMask = (uintptr_t)(((intptr_t)1 << IntBits) - 1),
61 // ShiftedIntMask - This is the bits for the integer shifted in place.
62 ShiftedIntMask = (uintptr_t)(IntMask << IntShift)
66 PointerIntPair() : Value(0) {}
67 PointerIntPair(PointerTy PtrVal, IntType IntVal) {
68 setPointerAndInt(PtrVal, IntVal);
70 explicit PointerIntPair(PointerTy PtrVal) { initWithPointer(PtrVal); }
72 PointerTy getPointer() const {
73 return PtrTraits::getFromVoidPointer(
74 reinterpret_cast<void *>(Value & PointerBitMask));
77 IntType getInt() const { return (IntType)((Value >> IntShift) & IntMask); }
79 void setPointer(PointerTy PtrVal) {
81 reinterpret_cast<intptr_t>(PtrTraits::getAsVoidPointer(PtrVal));
82 assert((PtrWord & ~PointerBitMask) == 0 &&
83 "Pointer is not sufficiently aligned");
84 // Preserve all low bits, just update the pointer.
85 Value = PtrWord | (Value & ~PointerBitMask);
88 void setInt(IntType IntVal) {
89 intptr_t IntWord = static_cast<intptr_t>(IntVal);
90 assert((IntWord & ~IntMask) == 0 && "Integer too large for field");
92 // Preserve all bits other than the ones we are updating.
93 Value &= ~ShiftedIntMask; // Remove integer field.
94 Value |= IntWord << IntShift; // Set new integer.
97 void initWithPointer(PointerTy PtrVal) {
99 reinterpret_cast<intptr_t>(PtrTraits::getAsVoidPointer(PtrVal));
100 assert((PtrWord & ~PointerBitMask) == 0 &&
101 "Pointer is not sufficiently aligned");
105 void setPointerAndInt(PointerTy PtrVal, IntType IntVal) {
107 reinterpret_cast<intptr_t>(PtrTraits::getAsVoidPointer(PtrVal));
108 assert((PtrWord & ~PointerBitMask) == 0 &&
109 "Pointer is not sufficiently aligned");
110 intptr_t IntWord = static_cast<intptr_t>(IntVal);
111 assert((IntWord & ~IntMask) == 0 && "Integer too large for field");
113 Value = PtrWord | (IntWord << IntShift);
116 PointerTy const *getAddrOfPointer() const {
117 return const_cast<PointerIntPair *>(this)->getAddrOfPointer();
120 PointerTy *getAddrOfPointer() {
121 assert(Value == reinterpret_cast<intptr_t>(getPointer()) &&
122 "Can only return the address if IntBits is cleared and "
123 "PtrTraits doesn't change the pointer");
124 return reinterpret_cast<PointerTy *>(&Value);
127 void *getOpaqueValue() const { return reinterpret_cast<void *>(Value); }
128 void setFromOpaqueValue(void *Val) {
129 Value = reinterpret_cast<intptr_t>(Val);
132 static PointerIntPair getFromOpaqueValue(void *V) {
134 P.setFromOpaqueValue(V);
138 // Allow PointerIntPairs to be created from const void * if and only if the
139 // pointer type could be created from a const void *.
140 static PointerIntPair getFromOpaqueValue(const void *V) {
141 (void)PtrTraits::getFromVoidPointer(V);
142 return getFromOpaqueValue(const_cast<void *>(V));
145 bool operator==(const PointerIntPair &RHS) const {
146 return Value == RHS.Value;
148 bool operator!=(const PointerIntPair &RHS) const {
149 return Value != RHS.Value;
151 bool operator<(const PointerIntPair &RHS) const { return Value < RHS.Value; }
152 bool operator>(const PointerIntPair &RHS) const { return Value > RHS.Value; }
153 bool operator<=(const PointerIntPair &RHS) const {
154 return Value <= RHS.Value;
156 bool operator>=(const PointerIntPair &RHS) const {
157 return Value >= RHS.Value;
161 template <typename T> struct isPodLike;
162 template <typename PointerTy, unsigned IntBits, typename IntType>
163 struct isPodLike<PointerIntPair<PointerTy, IntBits, IntType>> {
164 static const bool value = true;
167 // Provide specialization of DenseMapInfo for PointerIntPair.
168 template <typename PointerTy, unsigned IntBits, typename IntType>
169 struct DenseMapInfo<PointerIntPair<PointerTy, IntBits, IntType>> {
170 typedef PointerIntPair<PointerTy, IntBits, IntType> Ty;
171 static Ty getEmptyKey() {
172 uintptr_t Val = static_cast<uintptr_t>(-1);
173 Val <<= PointerLikeTypeTraits<Ty>::NumLowBitsAvailable;
174 return Ty::getFromOpaqueValue(reinterpret_cast<void *>(Val));
176 static Ty getTombstoneKey() {
177 uintptr_t Val = static_cast<uintptr_t>(-2);
178 Val <<= PointerLikeTypeTraits<PointerTy>::NumLowBitsAvailable;
179 return Ty::getFromOpaqueValue(reinterpret_cast<void *>(Val));
181 static unsigned getHashValue(Ty V) {
182 uintptr_t IV = reinterpret_cast<uintptr_t>(V.getOpaqueValue());
183 return unsigned(IV) ^ unsigned(IV >> 9);
185 static bool isEqual(const Ty &LHS, const Ty &RHS) { return LHS == RHS; }
188 // Teach SmallPtrSet that PointerIntPair is "basically a pointer".
189 template <typename PointerTy, unsigned IntBits, typename IntType,
191 class PointerLikeTypeTraits<
192 PointerIntPair<PointerTy, IntBits, IntType, PtrTraits>> {
195 getAsVoidPointer(const PointerIntPair<PointerTy, IntBits, IntType> &P) {
196 return P.getOpaqueValue();
198 static inline PointerIntPair<PointerTy, IntBits, IntType>
199 getFromVoidPointer(void *P) {
200 return PointerIntPair<PointerTy, IntBits, IntType>::getFromOpaqueValue(P);
202 static inline PointerIntPair<PointerTy, IntBits, IntType>
203 getFromVoidPointer(const void *P) {
204 return PointerIntPair<PointerTy, IntBits, IntType>::getFromOpaqueValue(P);
206 enum { NumLowBitsAvailable = PtrTraits::NumLowBitsAvailable - IntBits };
209 } // end namespace llvm