0727f737441f9c4ada08fdd75823fac450b025c1
[oota-llvm.git] / include / llvm / ADT / PointerIntPair.h
1 //===- llvm/ADT/PointerIntPair.h - Pair for pointer and int -----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the PointerIntPair class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_ADT_POINTERINTPAIR_H
15 #define LLVM_ADT_POINTERINTPAIR_H
16
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/PointerLikeTypeTraits.h"
19 #include <cassert>
20 #include <limits>
21
22 namespace llvm {
23
24 template <typename T> struct DenseMapInfo;
25
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
31 /// type.
32 ///
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.
39 ///
40 template <typename PointerTy, unsigned IntBits, typename IntType = unsigned,
41           typename PtrTraits = PointerLikeTypeTraits<PointerTy>>
42 class PointerIntPair {
43   intptr_t Value;
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");
49   enum : uintptr_t {
50     /// PointerBitMask - The bits that come from the pointer.
51     PointerBitMask =
52         ~(uintptr_t)(((intptr_t)1 << PtrTraits::NumLowBitsAvailable) - 1),
53
54     /// IntShift - The number of low bits that we reserve for other uses, and
55     /// keep zero.
56     IntShift = (uintptr_t)PtrTraits::NumLowBitsAvailable - IntBits,
57
58     /// IntMask - This is the unshifted mask for valid bits of the int type.
59     IntMask = (uintptr_t)(((intptr_t)1 << IntBits) - 1),
60
61     // ShiftedIntMask - This is the bits for the integer shifted in place.
62     ShiftedIntMask = (uintptr_t)(IntMask << IntShift)
63   };
64
65 public:
66   PointerIntPair() : Value(0) {}
67   PointerIntPair(PointerTy PtrVal, IntType IntVal) {
68     setPointerAndInt(PtrVal, IntVal);
69   }
70   explicit PointerIntPair(PointerTy PtrVal) { initWithPointer(PtrVal); }
71
72   PointerTy getPointer() const {
73     return PtrTraits::getFromVoidPointer(
74         reinterpret_cast<void *>(Value & PointerBitMask));
75   }
76
77   IntType getInt() const { return (IntType)((Value >> IntShift) & IntMask); }
78
79   void setPointer(PointerTy PtrVal) {
80     intptr_t PtrWord =
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);
86   }
87
88   void setInt(IntType IntVal) {
89     intptr_t IntWord = static_cast<intptr_t>(IntVal);
90     assert((IntWord & ~IntMask) == 0 && "Integer too large for field");
91
92     // Preserve all bits other than the ones we are updating.
93     Value &= ~ShiftedIntMask;     // Remove integer field.
94     Value |= IntWord << IntShift; // Set new integer.
95   }
96
97   void initWithPointer(PointerTy PtrVal) {
98     intptr_t PtrWord =
99         reinterpret_cast<intptr_t>(PtrTraits::getAsVoidPointer(PtrVal));
100     assert((PtrWord & ~PointerBitMask) == 0 &&
101            "Pointer is not sufficiently aligned");
102     Value = PtrWord;
103   }
104
105   void setPointerAndInt(PointerTy PtrVal, IntType IntVal) {
106     intptr_t PtrWord =
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");
112
113     Value = PtrWord | (IntWord << IntShift);
114   }
115
116   PointerTy const *getAddrOfPointer() const {
117     return const_cast<PointerIntPair *>(this)->getAddrOfPointer();
118   }
119
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);
125   }
126
127   void *getOpaqueValue() const { return reinterpret_cast<void *>(Value); }
128   void setFromOpaqueValue(void *Val) {
129     Value = reinterpret_cast<intptr_t>(Val);
130   }
131
132   static PointerIntPair getFromOpaqueValue(void *V) {
133     PointerIntPair P;
134     P.setFromOpaqueValue(V);
135     return P;
136   }
137
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));
143   }
144
145   bool operator==(const PointerIntPair &RHS) const {
146     return Value == RHS.Value;
147   }
148   bool operator!=(const PointerIntPair &RHS) const {
149     return Value != RHS.Value;
150   }
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;
155   }
156   bool operator>=(const PointerIntPair &RHS) const {
157     return Value >= RHS.Value;
158   }
159 };
160
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;
165 };
166
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));
175   }
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));
180   }
181   static unsigned getHashValue(Ty V) {
182     uintptr_t IV = reinterpret_cast<uintptr_t>(V.getOpaqueValue());
183     return unsigned(IV) ^ unsigned(IV >> 9);
184   }
185   static bool isEqual(const Ty &LHS, const Ty &RHS) { return LHS == RHS; }
186 };
187
188 // Teach SmallPtrSet that PointerIntPair is "basically a pointer".
189 template <typename PointerTy, unsigned IntBits, typename IntType,
190           typename PtrTraits>
191 class PointerLikeTypeTraits<
192     PointerIntPair<PointerTy, IntBits, IntType, PtrTraits>> {
193 public:
194   static inline void *
195   getAsVoidPointer(const PointerIntPair<PointerTy, IntBits, IntType> &P) {
196     return P.getOpaqueValue();
197   }
198   static inline PointerIntPair<PointerTy, IntBits, IntType>
199   getFromVoidPointer(void *P) {
200     return PointerIntPair<PointerTy, IntBits, IntType>::getFromOpaqueValue(P);
201   }
202   static inline PointerIntPair<PointerTy, IntBits, IntType>
203   getFromVoidPointer(const void *P) {
204     return PointerIntPair<PointerTy, IntBits, IntType>::getFromOpaqueValue(P);
205   }
206   enum { NumLowBitsAvailable = PtrTraits::NumLowBitsAvailable - IntBits };
207 };
208
209 } // end namespace llvm
210 #endif