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/DataTypes.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 alignment returned by the allocator in use.
31 template <typename PointerTy, unsigned IntBits, typename IntType=unsigned>
32 class PointerIntPair {
35 PointerIntPair() : Value(0) {}
36 PointerIntPair(PointerTy Ptr, IntType Int) : Value(0) {
41 PointerTy getPointer() const {
42 return reinterpret_cast<PointerTy>(Value & ~((1 << IntBits)-1));
45 IntType getInt() const {
46 return (IntType)(Value & ((1 << IntBits)-1));
49 void setPointer(PointerTy Ptr) {
50 intptr_t PtrVal = reinterpret_cast<intptr_t>(Ptr);
51 assert((PtrVal & ((1 << IntBits)-1)) == 0 &&
52 "Pointer is not sufficiently aligned");
53 Value = PtrVal | (intptr_t)getInt();
56 void setInt(IntType Int) {
57 intptr_t IntVal = Int;
58 assert(IntVal < (1 << IntBits) && "Integer too large for field");
59 Value = reinterpret_cast<intptr_t>(getPointer()) | IntVal;
62 void *getOpaqueValue() const { return reinterpret_cast<void*>(Value); }
63 void setFromOpaqueValue(void *Val) { Value = reinterpret_cast<intptr_t>(Val);}
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;}
70 bool operator>=(const PointerIntPair &RHS) const {return Value >= RHS.Value;}
73 // Provide specialization of DenseMapInfo for PointerIntPair.
74 template<typename PointerTy, unsigned IntBits, typename IntType>
75 struct DenseMapInfo<PointerIntPair<PointerTy, IntBits, IntType> > {
76 typedef PointerIntPair<PointerTy, IntBits, IntType> Ty;
77 static Ty getEmptyKey() {
78 return Ty(reinterpret_cast<PointerTy>(-1 << IntBits),
79 IntType((1 << IntBits)-1));
81 static Ty getTombstoneKey() {
82 return Ty(reinterpret_cast<PointerTy>(-2 << IntBits), IntType(0));
84 static unsigned getHashValue(Ty V) {
85 uintptr_t IV = reinterpret_cast<uintptr_t>(V.getOpaqueValue());
86 return unsigned(IV) ^ unsigned(IV >> 9);
88 static bool isEqual(const Ty &LHS, const Ty &RHS) { return LHS == RHS; }
89 static bool isPod() { return true; }
92 } // end namespace llvm