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"
25 /// PointerIntPair - This class implements a pair of a pointer and small
26 /// integer. It is designed to represent this in the space required by one
27 /// pointer by bitmangling the integer into the low part of the pointer. This
28 /// can only be done for small integers: typically up to 3 bits, but it depends
29 /// on the number of bits available according to PointerLikeTypeTraits for the
32 /// Note that PointerIntPair always puts the Int part in the highest bits
33 /// possible. For example, PointerIntPair<void*, 1, bool> will put the bit for
34 /// the bool into bit #2, not bit #0, which allows the low two bits to be used
35 /// for something else. For example, this allows:
36 /// PointerIntPair<PointerIntPair<void*, 1, bool>, 1, bool>
37 /// ... and the two bools will land in different bits.
39 template <typename PointerTy, unsigned IntBits, typename IntType=unsigned,
40 typename PtrTraits = PointerLikeTypeTraits<PointerTy> >
41 class PointerIntPair {
44 /// PointerBitMask - The bits that come from the pointer.
46 ~(uintptr_t)(((intptr_t)1 << PtrTraits::NumLowBitsAvailable)-1),
48 /// IntShift - The number of low bits that we reserve for other uses, and
50 IntShift = (uintptr_t)PtrTraits::NumLowBitsAvailable-IntBits,
52 /// IntMask - This is the unshifted mask for valid bits of the int type.
53 IntMask = (uintptr_t)(((intptr_t)1 << IntBits)-1),
55 // ShiftedIntMask - This is the bits for the integer shifted in place.
56 ShiftedIntMask = (uintptr_t)(IntMask << IntShift)
59 PointerIntPair() : Value(0) {}
60 PointerIntPair(PointerTy Ptr, IntType Int) : Value(0) {
61 assert(IntBits <= PtrTraits::NumLowBitsAvailable &&
62 "PointerIntPair formed with integer size too large for pointer");
67 PointerTy getPointer() const {
68 return reinterpret_cast<PointerTy>(Value & PointerBitMask);
71 IntType getInt() const {
72 return (IntType)((Value >> IntShift) & IntMask);
75 void setPointer(PointerTy Ptr) {
76 intptr_t PtrVal = reinterpret_cast<intptr_t>(Ptr);
77 assert((PtrVal & ((1 << PtrTraits::NumLowBitsAvailable)-1)) == 0 &&
78 "Pointer is not sufficiently aligned");
79 // Preserve all low bits, just update the pointer.
80 Value = PtrVal | (Value & ~PointerBitMask);
83 void setInt(IntType Int) {
84 intptr_t IntVal = Int;
85 assert(IntVal < (1 << IntBits) && "Integer too large for field");
87 // Preserve all bits other than the ones we are updating.
88 Value &= ~ShiftedIntMask; // Remove integer field.
89 Value |= IntVal << IntShift; // Set new integer.
92 void *getOpaqueValue() const { return reinterpret_cast<void*>(Value); }
93 void setFromOpaqueValue(void *Val) { Value = reinterpret_cast<intptr_t>(Val);}
95 static PointerIntPair getFromOpaqueValue(void *V) {
96 PointerIntPair P; P.setFromOpaqueValue(V); return P;
99 bool operator==(const PointerIntPair &RHS) const {return Value == RHS.Value;}
100 bool operator!=(const PointerIntPair &RHS) const {return Value != RHS.Value;}
101 bool operator<(const PointerIntPair &RHS) const {return Value < RHS.Value;}
102 bool operator>(const PointerIntPair &RHS) const {return Value > RHS.Value;}
103 bool operator<=(const PointerIntPair &RHS) const {return Value <= RHS.Value;}
104 bool operator>=(const PointerIntPair &RHS) const {return Value >= RHS.Value;}
107 // Provide specialization of DenseMapInfo for PointerIntPair.
108 template<typename PointerTy, unsigned IntBits, typename IntType>
109 struct DenseMapInfo<PointerIntPair<PointerTy, IntBits, IntType> > {
110 typedef PointerIntPair<PointerTy, IntBits, IntType> Ty;
111 static Ty getEmptyKey() {
113 Val <<= PointerLikeTypeTraits<PointerTy>::NumLowBitsAvailable;
114 return Ty(reinterpret_cast<PointerTy>(Val), IntType((1 << IntBits)-1));
116 static Ty getTombstoneKey() {
118 Val <<= PointerLikeTypeTraits<PointerTy>::NumLowBitsAvailable;
119 return Ty(reinterpret_cast<PointerTy>(Val), IntType(0));
121 static unsigned getHashValue(Ty V) {
122 uintptr_t IV = reinterpret_cast<uintptr_t>(V.getOpaqueValue());
123 return unsigned(IV) ^ unsigned(IV >> 9);
125 static bool isEqual(const Ty &LHS, const Ty &RHS) { return LHS == RHS; }
126 static bool isPod() { return true; }
129 // Teach SmallPtrSet that PointerIntPair is "basically a pointer".
130 template<typename PointerTy, unsigned IntBits, typename IntType,
132 class PointerLikeTypeTraits<PointerIntPair<PointerTy, IntBits, IntType,
136 getAsVoidPointer(const PointerIntPair<PointerTy, IntBits, IntType> &P) {
137 return P.getOpaqueValue();
139 static inline PointerIntPair<PointerTy, IntBits, IntType>
140 getFromVoidPointer(void *P) {
141 return PointerIntPair<PointerTy, IntBits, IntType>::getFromOpaqueValue(P);
144 NumLowBitsAvailable =
145 PointerLikeTypeTraits<PointerTy>::NumLowBitsAvailable - IntBits
149 } // end namespace llvm