LLVM  8.0.1
PointerIntPair.h
Go to the documentation of this file.
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 
18 #include <cassert>
19 #include <cstdint>
20 #include <limits>
21 
22 namespace llvm {
23 
24 template <typename T> struct DenseMapInfo;
25 template <typename PointerT, unsigned IntBits, typename PtrTraits>
27 
28 /// PointerIntPair - This class implements a pair of a pointer and small
29 /// integer. It is designed to represent this in the space required by one
30 /// pointer by bitmangling the integer into the low part of the pointer. This
31 /// can only be done for small integers: typically up to 3 bits, but it depends
32 /// on the number of bits available according to PointerLikeTypeTraits for the
33 /// type.
34 ///
35 /// Note that PointerIntPair always puts the IntVal part in the highest bits
36 /// possible. For example, PointerIntPair<void*, 1, bool> will put the bit for
37 /// the bool into bit #2, not bit #0, which allows the low two bits to be used
38 /// for something else. For example, this allows:
39 /// PointerIntPair<PointerIntPair<void*, 1, bool>, 1, bool>
40 /// ... and the two bools will land in different bits.
41 template <typename PointerTy, unsigned IntBits, typename IntType = unsigned,
42  typename PtrTraits = PointerLikeTypeTraits<PointerTy>,
45  // Used by MSVC visualizer and generally helpful for debugging/visualizing.
46  using InfoTy = Info;
47  intptr_t Value = 0;
48 
49 public:
50  constexpr PointerIntPair() = default;
51 
52  PointerIntPair(PointerTy PtrVal, IntType IntVal) {
53  setPointerAndInt(PtrVal, IntVal);
54  }
55 
56  explicit PointerIntPair(PointerTy PtrVal) { initWithPointer(PtrVal); }
57 
58  PointerTy getPointer() const { return Info::getPointer(Value); }
59 
60  IntType getInt() const { return (IntType)Info::getInt(Value); }
61 
62  void setPointer(PointerTy PtrVal) {
63  Value = Info::updatePointer(Value, PtrVal);
64  }
65 
66  void setInt(IntType IntVal) {
67  Value = Info::updateInt(Value, static_cast<intptr_t>(IntVal));
68  }
69 
70  void initWithPointer(PointerTy PtrVal) {
71  Value = Info::updatePointer(0, PtrVal);
72  }
73 
74  void setPointerAndInt(PointerTy PtrVal, IntType IntVal) {
75  Value = Info::updateInt(Info::updatePointer(0, PtrVal),
76  static_cast<intptr_t>(IntVal));
77  }
78 
79  PointerTy const *getAddrOfPointer() const {
80  return const_cast<PointerIntPair *>(this)->getAddrOfPointer();
81  }
82 
84  assert(Value == reinterpret_cast<intptr_t>(getPointer()) &&
85  "Can only return the address if IntBits is cleared and "
86  "PtrTraits doesn't change the pointer");
87  return reinterpret_cast<PointerTy *>(&Value);
88  }
89 
90  void *getOpaqueValue() const { return reinterpret_cast<void *>(Value); }
91 
92  void setFromOpaqueValue(void *Val) {
93  Value = reinterpret_cast<intptr_t>(Val);
94  }
95 
98  P.setFromOpaqueValue(V);
99  return P;
100  }
101 
102  // Allow PointerIntPairs to be created from const void * if and only if the
103  // pointer type could be created from a const void *.
104  static PointerIntPair getFromOpaqueValue(const void *V) {
105  (void)PtrTraits::getFromVoidPointer(V);
106  return getFromOpaqueValue(const_cast<void *>(V));
107  }
108 
109  bool operator==(const PointerIntPair &RHS) const {
110  return Value == RHS.Value;
111  }
112 
113  bool operator!=(const PointerIntPair &RHS) const {
114  return Value != RHS.Value;
115  }
116 
117  bool operator<(const PointerIntPair &RHS) const { return Value < RHS.Value; }
118  bool operator>(const PointerIntPair &RHS) const { return Value > RHS.Value; }
119 
120  bool operator<=(const PointerIntPair &RHS) const {
121  return Value <= RHS.Value;
122  }
123 
124  bool operator>=(const PointerIntPair &RHS) const {
125  return Value >= RHS.Value;
126  }
127 };
128 
129 template <typename PointerT, unsigned IntBits, typename PtrTraits>
130 struct PointerIntPairInfo {
131  static_assert(PtrTraits::NumLowBitsAvailable <
132  std::numeric_limits<uintptr_t>::digits,
133  "cannot use a pointer type that has all bits free");
134  static_assert(IntBits <= PtrTraits::NumLowBitsAvailable,
135  "PointerIntPair with integer size too large for pointer");
136  enum : uintptr_t {
137  /// PointerBitMask - The bits that come from the pointer.
138  PointerBitMask =
139  ~(uintptr_t)(((intptr_t)1 << PtrTraits::NumLowBitsAvailable) - 1),
140 
141  /// IntShift - The number of low bits that we reserve for other uses, and
142  /// keep zero.
143  IntShift = (uintptr_t)PtrTraits::NumLowBitsAvailable - IntBits,
144 
145  /// IntMask - This is the unshifted mask for valid bits of the int type.
146  IntMask = (uintptr_t)(((intptr_t)1 << IntBits) - 1),
147 
148  // ShiftedIntMask - This is the bits for the integer shifted in place.
149  ShiftedIntMask = (uintptr_t)(IntMask << IntShift)
150  };
151 
152  static PointerT getPointer(intptr_t Value) {
153  return PtrTraits::getFromVoidPointer(
154  reinterpret_cast<void *>(Value & PointerBitMask));
155  }
156 
158  return (Value >> IntShift) & IntMask;
159  }
160 
161  static intptr_t updatePointer(intptr_t OrigValue, PointerT Ptr) {
162  intptr_t PtrWord =
163  reinterpret_cast<intptr_t>(PtrTraits::getAsVoidPointer(Ptr));
164  assert((PtrWord & ~PointerBitMask) == 0 &&
165  "Pointer is not sufficiently aligned");
166  // Preserve all low bits, just update the pointer.
167  return PtrWord | (OrigValue & ~PointerBitMask);
168  }
169 
170  static intptr_t updateInt(intptr_t OrigValue, intptr_t Int) {
171  intptr_t IntWord = static_cast<intptr_t>(Int);
172  assert((IntWord & ~IntMask) == 0 && "Integer too large for field");
173 
174  // Preserve all bits other than the ones we are updating.
175  return (OrigValue & ~ShiftedIntMask) | IntWord << IntShift;
176  }
177 };
178 
179 template <typename T> struct isPodLike;
180 template <typename PointerTy, unsigned IntBits, typename IntType>
181 struct isPodLike<PointerIntPair<PointerTy, IntBits, IntType>> {
182  static const bool value = true;
183 };
184 
185 // Provide specialization of DenseMapInfo for PointerIntPair.
186 template <typename PointerTy, unsigned IntBits, typename IntType>
187 struct DenseMapInfo<PointerIntPair<PointerTy, IntBits, IntType>> {
189 
190  static Ty getEmptyKey() {
191  uintptr_t Val = static_cast<uintptr_t>(-1);
192  Val <<= PointerLikeTypeTraits<Ty>::NumLowBitsAvailable;
193  return Ty::getFromOpaqueValue(reinterpret_cast<void *>(Val));
194  }
195 
196  static Ty getTombstoneKey() {
197  uintptr_t Val = static_cast<uintptr_t>(-2);
198  Val <<= PointerLikeTypeTraits<PointerTy>::NumLowBitsAvailable;
199  return Ty::getFromOpaqueValue(reinterpret_cast<void *>(Val));
200  }
201 
202  static unsigned getHashValue(Ty V) {
203  uintptr_t IV = reinterpret_cast<uintptr_t>(V.getOpaqueValue());
204  return unsigned(IV) ^ unsigned(IV >> 9);
205  }
206 
207  static bool isEqual(const Ty &LHS, const Ty &RHS) { return LHS == RHS; }
208 };
209 
210 // Teach SmallPtrSet that PointerIntPair is "basically a pointer".
211 template <typename PointerTy, unsigned IntBits, typename IntType,
212  typename PtrTraits>
214  PointerIntPair<PointerTy, IntBits, IntType, PtrTraits>> {
215  static inline void *
217  return P.getOpaqueValue();
218  }
219 
223  }
224 
226  getFromVoidPointer(const void *P) {
228  }
229 
230  enum { NumLowBitsAvailable = PtrTraits::NumLowBitsAvailable - IntBits };
231 };
232 
233 } // end namespace llvm
234 
235 #endif // LLVM_ADT_POINTERINTPAIR_H
bool operator!=(const PointerIntPair &RHS) const
void setInt(IntType IntVal)
bool operator>(const PointerIntPair &RHS) const
This class represents lattice values for constants.
Definition: AllocatorList.h:24
PointerTy getPointer() const
bool operator<=(const PointerIntPair &RHS) const
static PointerIntPair getFromOpaqueValue(const void *V)
static PointerIntPair getFromOpaqueValue(void *V)
void * PointerTy
Definition: GenericValue.h:22
void setPointer(PointerTy PtrVal)
PointerTy * getAddrOfPointer()
static unsigned getInt(StringRef R)
Get an unsigned integer, including error checks.
Definition: DataLayout.cpp:213
PointerTy const * getAddrOfPointer() const
void * getOpaqueValue() const
static intptr_t updateInt(intptr_t OrigValue, intptr_t Int)
static PointerIntPair< PointerTy, IntBits, IntType > getFromVoidPointer(const void *P)
A traits type that is used to handle pointer types and things that are just wrappers for pointers as ...
void setFromOpaqueValue(void *Val)
bool operator<(const PointerIntPair &RHS) const
IntType getInt() const
Analysis containing CSE Info
Definition: CSEInfo.cpp:21
#define P(N)
static void * getAsVoidPointer(const PointerIntPair< PointerTy, IntBits, IntType > &P)
PointerIntPair - This class implements a pair of a pointer and small integer.
void initWithPointer(PointerTy PtrVal)
static PointerIntPair< PointerTy, IntBits, IntType > getFromVoidPointer(void *P)
PointerIntPair(PointerTy PtrVal, IntType IntVal)
static PointerT getPointer(intptr_t Value)
static intptr_t getInt(intptr_t Value)
isPodLike - This is a type trait that is used to determine whether a given type can be copied around ...
Definition: ArrayRef.h:530
static intptr_t updatePointer(intptr_t OrigValue, PointerT Ptr)
void setPointerAndInt(PointerTy PtrVal, IntType IntVal)
constexpr PointerIntPair()=default
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool operator>=(const PointerIntPair &RHS) const
LLVM Value Representation.
Definition: Value.h:73
PointerIntPair(PointerTy PtrVal)
bool operator==(const PointerIntPair &RHS) const