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
24 /// PointerIntPair - This class implements a pair of a pointer and small
25 /// integer. It is designed to represent this in the space required by one
26 /// pointer by bitmangling the integer into the low part of the pointer. This
27 /// can only be done for small integers: typically up to 3 bits, but it depends
28 /// on the alignment returned by the allocator in use.
30 template <typename PointerTy, unsigned IntBits, typename IntType=unsigned>
31 class PointerIntPair {
34 PointerIntPair() : Value(0) {}
35 PointerIntPair(PointerTy Ptr, IntType Int) : Value(0) {
40 PointerTy getPointer() const {
41 return reinterpret_cast<PointerTy>(Value & ~((1 << IntBits)-1));
44 IntType getInt() const {
45 return (IntType)(Value & ((1 << IntBits)-1));
48 void setPointer(PointerTy Ptr) {
49 intptr_t PtrVal = reinterpret_cast<intptr_t>(Ptr);
50 assert((PtrVal & ((1 << IntBits)-1)) == 0 &&
51 "Pointer is not sufficiently aligned");
52 Value = PtrVal | (intptr_t)getInt();
55 void setInt(IntType Int) {
56 intptr_t IntVal = Int;
57 assert(IntVal < (1 << IntBits) && "Integer too large for field");
58 Value = reinterpret_cast<intptr_t>(getPointer()) | IntVal;
61 void *getOpaqueValue() const { return reinterpret_cast<void*>(Value); }
62 void setFromOpaqueValue(void *Val) { Value = reinterpret_cast<intptr_t>(Val);}
64 bool operator==(const PointerIntPair &RHS) const {return Value == RHS.Value;}
65 bool operator!=(const PointerIntPair &RHS) const {return Value != RHS.Value;}
66 bool operator<(const PointerIntPair &RHS) const {return Value < RHS.Value;}
67 bool operator>(const PointerIntPair &RHS) const {return Value > RHS.Value;}
68 bool operator<=(const PointerIntPair &RHS) const {return Value <= RHS.Value;}
69 bool operator>=(const PointerIntPair &RHS) const {return Value >= RHS.Value;}
72 // Provide specialization of DenseMapInfo for PointerIntPair.
73 template<typename PointerTy, unsigned IntBits, typename IntType>
74 struct DenseMapInfo<PointerIntPair<PointerTy, IntBits, IntType> > {
75 typedef PointerIntPair<PointerTy, IntBits, IntType> Ty;
76 static Ty getEmptyKey() {
77 return Ty(reinterpret_cast<PointerTy>(-1 << IntBits),
78 IntType((1 << IntBits)-1));
80 static Ty getTombstoneKey() {
81 return Ty(reinterpret_cast<PointerTy>(-2 << IntBits), IntType(0));
83 static unsigned getHashValue(Ty V) {
84 uintptr_t IV = reinterpret_cast<uintptr_t>(V.getOpaqueValue());
85 return unsigned(IV) ^ unsigned(IV >> 9);
87 static bool isEqual(const Ty &LHS, const Ty &RHS) { return LHS == RHS; }
88 static bool isPod() { return true; }
91 } // end namespace llvm