1//===- llvm/ADT/PointerIntPair.h - Pair for pointer and int -----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
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 "llvm/Support/type_traits.h"
20#include <cassert>
21#include <cstdint>
22#include <cstring>
23#include <limits>
24
25namespace llvm {
26
27namespace detail {
28template <typename Ptr> struct PunnedPointer {
29 static_assert(sizeof(Ptr) == sizeof(intptr_t), "");
30
31 // Asserts that allow us to let the compiler implement the destructor and
32 // copy/move constructors
33 static_assert(std::is_trivially_destructible<Ptr>::value, "");
34 static_assert(std::is_trivially_copy_constructible<Ptr>::value, "");
35 static_assert(std::is_trivially_move_constructible<Ptr>::value, "");
36
37 explicit constexpr PunnedPointer(intptr_t i = 0) { *this = i; }
38
39 constexpr intptr_t asInt() const {
40 intptr_t R = 0;
41 std::memcpy(dest: &R, src: Data, n: sizeof(R));
42 return R;
43 }
44
45 constexpr operator intptr_t() const { return asInt(); }
46
47 constexpr PunnedPointer &operator=(intptr_t V) {
48 std::memcpy(dest: Data, src: &V, n: sizeof(Data));
49 return *this;
50 }
51
52 Ptr *getPointerAddress() { return reinterpret_cast<Ptr *>(Data); }
53 const Ptr *getPointerAddress() const { return reinterpret_cast<Ptr *>(Data); }
54
55private:
56 alignas(Ptr) unsigned char Data[sizeof(Ptr)];
57};
58} // namespace detail
59
60template <typename T, typename Enable> struct DenseMapInfo;
61template <typename PointerT, unsigned IntBits, typename PtrTraits>
62struct PointerIntPairInfo;
63
64/// PointerIntPair - This class implements a pair of a pointer and small
65/// integer. It is designed to represent this in the space required by one
66/// pointer by bitmangling the integer into the low part of the pointer. This
67/// can only be done for small integers: typically up to 3 bits, but it depends
68/// on the number of bits available according to PointerLikeTypeTraits for the
69/// type.
70///
71/// Note that PointerIntPair always puts the IntVal part in the highest bits
72/// possible. For example, PointerIntPair<void*, 1, bool> will put the bit for
73/// the bool into bit #2, not bit #0, which allows the low two bits to be used
74/// for something else. For example, this allows:
75/// PointerIntPair<PointerIntPair<void*, 1, bool>, 1, bool>
76/// ... and the two bools will land in different bits.
77template <typename PointerTy, unsigned IntBits, typename IntType = unsigned,
78 typename PtrTraits = PointerLikeTypeTraits<PointerTy>,
79 typename Info = PointerIntPairInfo<PointerTy, IntBits, PtrTraits>>
80class PointerIntPair {
81 // Used by MSVC visualizer and generally helpful for debugging/visualizing.
82 using InfoTy = Info;
83 detail::PunnedPointer<PointerTy> Value;
84
85public:
86 constexpr PointerIntPair() = default;
87
88 PointerIntPair(PointerTy PtrVal, IntType IntVal) {
89 setPointerAndInt(PtrVal, IntVal);
90 }
91
92 explicit PointerIntPair(PointerTy PtrVal) { initWithPointer(PtrVal); }
93
94 PointerTy getPointer() const { return Info::getPointer(Value); }
95
96 IntType getInt() const { return (IntType)Info::getInt(Value); }
97
98 void setPointer(PointerTy PtrVal) & {
99 Value = Info::updatePointer(Value, PtrVal);
100 }
101
102 void setInt(IntType IntVal) & {
103 Value = Info::updateInt(Value, static_cast<intptr_t>(IntVal));
104 }
105
106 void initWithPointer(PointerTy PtrVal) & {
107 Value = Info::updatePointer(0, PtrVal);
108 }
109
110 void setPointerAndInt(PointerTy PtrVal, IntType IntVal) & {
111 Value = Info::updateInt(Info::updatePointer(0, PtrVal),
112 static_cast<intptr_t>(IntVal));
113 }
114
115 PointerTy const *getAddrOfPointer() const {
116 return const_cast<PointerIntPair *>(this)->getAddrOfPointer();
117 }
118
119 PointerTy *getAddrOfPointer() {
120 assert(Value == reinterpret_cast<intptr_t>(getPointer()) &&
121 "Can only return the address if IntBits is cleared and "
122 "PtrTraits doesn't change the pointer");
123 return Value.getPointerAddress();
124 }
125
126 void *getOpaqueValue() const {
127 return reinterpret_cast<void *>(Value.asInt());
128 }
129
130 void setFromOpaqueValue(void *Val) & {
131 Value = reinterpret_cast<intptr_t>(Val);
132 }
133
134 static PointerIntPair getFromOpaqueValue(void *V) {
135 PointerIntPair P;
136 P.setFromOpaqueValue(V);
137 return P;
138 }
139
140 // Allow PointerIntPairs to be created from const void * if and only if the
141 // pointer type could be created from a const void *.
142 static PointerIntPair getFromOpaqueValue(const void *V) {
143 static_assert(
144 std::is_convertible_v<decltype(PtrTraits::getFromVoidPointer(V)),
145 PointerTy>);
146 return getFromOpaqueValue(const_cast<void *>(V));
147 }
148
149 bool operator==(const PointerIntPair &RHS) const {
150 return Value == RHS.Value;
151 }
152
153 bool operator!=(const PointerIntPair &RHS) const {
154 return Value != RHS.Value;
155 }
156
157 bool operator<(const PointerIntPair &RHS) const { return Value < RHS.Value; }
158 bool operator>(const PointerIntPair &RHS) const { return Value > RHS.Value; }
159
160 bool operator<=(const PointerIntPair &RHS) const {
161 return Value <= RHS.Value;
162 }
163
164 bool operator>=(const PointerIntPair &RHS) const {
165 return Value >= RHS.Value;
166 }
167};
168
169template <typename PointerT, unsigned IntBits, typename PtrTraits>
170struct PointerIntPairInfo {
171 static_assert(PtrTraits::NumLowBitsAvailable <
172 std::numeric_limits<uintptr_t>::digits,
173 "cannot use a pointer type that has all bits free");
174 static_assert(IntBits <= PtrTraits::NumLowBitsAvailable,
175 "PointerIntPair with integer size too large for pointer");
176 enum MaskAndShiftConstants : uintptr_t {
177 /// PointerBitMask - The bits that come from the pointer.
178 PointerBitMask = (~(uintptr_t)0) << PtrTraits::NumLowBitsAvailable,
179
180 /// IntShift - The number of low bits that we reserve for other uses, and
181 /// keep zero.
182 IntShift = (uintptr_t)PtrTraits::NumLowBitsAvailable - IntBits,
183
184 /// IntMask - This is the unshifted mask for valid bits of the int type.
185 IntMask = ((uintptr_t)1 << IntBits) - 1,
186
187 // ShiftedIntMask - This is the bits for the integer shifted in place.
188 ShiftedIntMask = (uintptr_t)(IntMask << IntShift)
189 };
190
191 static PointerT getPointer(intptr_t Value) {
192 return PtrTraits::getFromVoidPointer(
193 reinterpret_cast<void *>(Value & PointerBitMask));
194 }
195
196 static intptr_t getInt(intptr_t Value) {
197 return (Value >> IntShift) & IntMask;
198 }
199
200 static intptr_t updatePointer(intptr_t OrigValue, PointerT Ptr) {
201 intptr_t PtrWord =
202 reinterpret_cast<intptr_t>(PtrTraits::getAsVoidPointer(Ptr));
203 assert((PtrWord & ~PointerBitMask) == 0 &&
204 "Pointer is not sufficiently aligned");
205 // Preserve all low bits, just update the pointer.
206 return PtrWord | (OrigValue & ~PointerBitMask);
207 }
208
209 static intptr_t updateInt(intptr_t OrigValue, intptr_t Int) {
210 assert((Int & ~IntMask) == 0 && "Integer too large for field");
211
212 // Preserve all bits other than the ones we are updating.
213 return (OrigValue & ~ShiftedIntMask) | Int << IntShift;
214 }
215};
216
217// Provide specialization of DenseMapInfo for PointerIntPair.
218template <typename PointerTy, unsigned IntBits, typename IntType>
219struct DenseMapInfo<PointerIntPair<PointerTy, IntBits, IntType>, void> {
220 using Ty = PointerIntPair<PointerTy, IntBits, IntType>;
221
222 static unsigned getHashValue(Ty V) {
223 uintptr_t IV = reinterpret_cast<uintptr_t>(V.getOpaqueValue());
224 return unsigned(IV) ^ unsigned(IV >> 9);
225 }
226
227 static bool isEqual(const Ty &LHS, const Ty &RHS) { return LHS == RHS; }
228};
229
230// Teach SmallPtrSet that PointerIntPair is "basically a pointer".
231template <typename PointerTy, unsigned IntBits, typename IntType,
232 typename PtrTraits>
233struct PointerLikeTypeTraits<
234 PointerIntPair<PointerTy, IntBits, IntType, PtrTraits>> {
235 static inline void *
236 getAsVoidPointer(const PointerIntPair<PointerTy, IntBits, IntType> &P) {
237 return P.getOpaqueValue();
238 }
239
240 static inline PointerIntPair<PointerTy, IntBits, IntType>
241 getFromVoidPointer(void *P) {
242 return PointerIntPair<PointerTy, IntBits, IntType>::getFromOpaqueValue(P);
243 }
244
245 static inline PointerIntPair<PointerTy, IntBits, IntType>
246 getFromVoidPointer(const void *P) {
247 return PointerIntPair<PointerTy, IntBits, IntType>::getFromOpaqueValue(P);
248 }
249
250 static constexpr int NumLowBitsAvailable =
251 PtrTraits::NumLowBitsAvailable - IntBits;
252};
253
254// Allow structured bindings on PointerIntPair.
255template <std::size_t I, typename PointerTy, unsigned IntBits, typename IntType,
256 typename PtrTraits, typename Info>
257decltype(auto)
258get(const PointerIntPair<PointerTy, IntBits, IntType, PtrTraits, Info> &Pair) {
259 static_assert(I < 2);
260 if constexpr (I == 0)
261 return Pair.getPointer();
262 else
263 return Pair.getInt();
264}
265
266} // end namespace llvm
267
268namespace std {
269template <typename PointerTy, unsigned IntBits, typename IntType,
270 typename PtrTraits, typename Info>
271struct tuple_size<
272 llvm::PointerIntPair<PointerTy, IntBits, IntType, PtrTraits, Info>>
273 : std::integral_constant<std::size_t, 2> {};
274
275template <std::size_t I, typename PointerTy, unsigned IntBits, typename IntType,
276 typename PtrTraits, typename Info>
277struct tuple_element<
278 I, llvm::PointerIntPair<PointerTy, IntBits, IntType, PtrTraits, Info>>
279 : std::conditional<I == 0, PointerTy, IntType> {};
280} // namespace std
281
282#endif // LLVM_ADT_POINTERINTPAIR_H
283