LLVM 22.0.0git
PointerIntPair.h
Go to the documentation of this file.
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
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(&R, Data, 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(Data, &V, 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>>
81 // Used by MSVC visualizer and generally helpful for debugging/visualizing.
82 using InfoTy = Info;
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
116 return const_cast<PointerIntPair *>(this)->getAddrOfPointer();
117 }
118
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
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 (void)PtrTraits::getFromVoidPointer(V);
144 return getFromOpaqueValue(const_cast<void *>(V));
145 }
146
147 bool operator==(const PointerIntPair &RHS) const {
148 return Value == RHS.Value;
149 }
150
151 bool operator!=(const PointerIntPair &RHS) const {
152 return Value != RHS.Value;
153 }
154
155 bool operator<(const PointerIntPair &RHS) const { return Value < RHS.Value; }
156 bool operator>(const PointerIntPair &RHS) const { return Value > RHS.Value; }
157
158 bool operator<=(const PointerIntPair &RHS) const {
159 return Value <= RHS.Value;
160 }
161
162 bool operator>=(const PointerIntPair &RHS) const {
163 return Value >= RHS.Value;
164 }
165};
166
167template <typename PointerT, unsigned IntBits, typename PtrTraits>
169 static_assert(PtrTraits::NumLowBitsAvailable <
170 std::numeric_limits<uintptr_t>::digits,
171 "cannot use a pointer type that has all bits free");
172 static_assert(IntBits <= PtrTraits::NumLowBitsAvailable,
173 "PointerIntPair with integer size too large for pointer");
174 enum MaskAndShiftConstants : uintptr_t {
175 /// PointerBitMask - The bits that come from the pointer.
176 PointerBitMask = (~(uintptr_t)0) << PtrTraits::NumLowBitsAvailable,
177
178 /// IntShift - The number of low bits that we reserve for other uses, and
179 /// keep zero.
180 IntShift = (uintptr_t)PtrTraits::NumLowBitsAvailable - IntBits,
181
182 /// IntMask - This is the unshifted mask for valid bits of the int type.
183 IntMask = ((uintptr_t)1 << IntBits) - 1,
184
185 // ShiftedIntMask - This is the bits for the integer shifted in place.
187 };
188
189 static PointerT getPointer(intptr_t Value) {
190 return PtrTraits::getFromVoidPointer(
191 reinterpret_cast<void *>(Value & PointerBitMask));
192 }
193
194 static intptr_t getInt(intptr_t Value) {
195 return (Value >> IntShift) & IntMask;
196 }
197
198 static intptr_t updatePointer(intptr_t OrigValue, PointerT Ptr) {
199 intptr_t PtrWord =
200 reinterpret_cast<intptr_t>(PtrTraits::getAsVoidPointer(Ptr));
201 assert((PtrWord & ~PointerBitMask) == 0 &&
202 "Pointer is not sufficiently aligned");
203 // Preserve all low bits, just update the pointer.
204 return PtrWord | (OrigValue & ~PointerBitMask);
205 }
206
207 static intptr_t updateInt(intptr_t OrigValue, intptr_t Int) {
208 assert((Int & ~IntMask) == 0 && "Integer too large for field");
209
210 // Preserve all bits other than the ones we are updating.
211 return (OrigValue & ~ShiftedIntMask) | Int << IntShift;
212 }
213};
214
215// Provide specialization of DenseMapInfo for PointerIntPair.
216template <typename PointerTy, unsigned IntBits, typename IntType>
217struct DenseMapInfo<PointerIntPair<PointerTy, IntBits, IntType>, void> {
219
220 static Ty getEmptyKey() {
221 uintptr_t Val = static_cast<uintptr_t>(-1);
222 Val <<= PointerLikeTypeTraits<Ty>::NumLowBitsAvailable;
223 return Ty::getFromOpaqueValue(reinterpret_cast<void *>(Val));
224 }
225
227 uintptr_t Val = static_cast<uintptr_t>(-2);
228 Val <<= PointerLikeTypeTraits<PointerTy>::NumLowBitsAvailable;
229 return Ty::getFromOpaqueValue(reinterpret_cast<void *>(Val));
230 }
231
232 static unsigned getHashValue(Ty V) {
233 uintptr_t IV = reinterpret_cast<uintptr_t>(V.getOpaqueValue());
234 return unsigned(IV) ^ unsigned(IV >> 9);
235 }
236
237 static bool isEqual(const Ty &LHS, const Ty &RHS) { return LHS == RHS; }
238};
239
240// Teach SmallPtrSet that PointerIntPair is "basically a pointer".
241template <typename PointerTy, unsigned IntBits, typename IntType,
242 typename PtrTraits>
244 PointerIntPair<PointerTy, IntBits, IntType, PtrTraits>> {
245 static inline void *
247 return P.getOpaqueValue();
248 }
249
254
259
260 static constexpr int NumLowBitsAvailable =
261 PtrTraits::NumLowBitsAvailable - IntBits;
262};
263
264// Allow structured bindings on PointerIntPair.
265template <std::size_t I, typename PointerTy, unsigned IntBits, typename IntType,
266 typename PtrTraits, typename Info>
267decltype(auto)
269 static_assert(I < 2);
270 if constexpr (I == 0)
271 return Pair.getPointer();
272 else
273 return Pair.getInt();
274}
275
276} // end namespace llvm
277
278namespace std {
279template <typename PointerTy, unsigned IntBits, typename IntType,
280 typename PtrTraits, typename Info>
281struct tuple_size<
282 llvm::PointerIntPair<PointerTy, IntBits, IntType, PtrTraits, Info>>
283 : std::integral_constant<std::size_t, 2> {};
284
285template <std::size_t I, typename PointerTy, unsigned IntBits, typename IntType,
286 typename PtrTraits, typename Info>
287struct tuple_element<
288 I, llvm::PointerIntPair<PointerTy, IntBits, IntType, PtrTraits, Info>>
289 : std::conditional<I == 0, PointerTy, IntType> {};
290} // namespace std
291
292#endif // LLVM_ADT_POINTERINTPAIR_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Analysis containing CSE Info
Definition CSEInfo.cpp:27
#define I(x, y, z)
Definition MD5.cpp:58
#define P(N)
Value * RHS
Value * LHS
static const uint32_t IV[8]
Definition blake3_impl.h:83
PointerIntPair - This class implements a pair of a pointer and small integer.
bool operator<(const PointerIntPair &RHS) const
void setPointer(PointerTy PtrVal) &
PointerIntPair(PointerTy PtrVal)
bool operator!=(const PointerIntPair &RHS) const
static PointerIntPair getFromOpaqueValue(const void *V)
bool operator==(const PointerIntPair &RHS) const
PointerIntPair(PointerTy PtrVal, IntType IntVal)
void initWithPointer(PointerTy PtrVal) &
IntType getInt() const
bool operator<=(const PointerIntPair &RHS) const
void setInt(IntType IntVal) &
void setPointerAndInt(PointerTy PtrVal, IntType IntVal) &
void * getOpaqueValue() const
constexpr PointerIntPair()=default
bool operator>=(const PointerIntPair &RHS) const
static PointerIntPair getFromOpaqueValue(void *V)
PointerTy * getAddrOfPointer()
bool operator>(const PointerIntPair &RHS) const
PointerTy const * getAddrOfPointer() const
PointerTy getPointer() const
void setFromOpaqueValue(void *Val) &
LLVM Value Representation.
Definition Value.h:75
These are wrappers over isa* function that allow them to be used in generic algorithms such as llvm:a...
Definition ADL.h:123
This is an optimization pass for GlobalISel generic memory operations.
void * PointerTy
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:851
An information struct used to provide DenseMap with the various necessary components for a given valu...
static PointerT getPointer(intptr_t Value)
static intptr_t updateInt(intptr_t OrigValue, intptr_t Int)
static intptr_t getInt(intptr_t Value)
@ IntShift
IntShift - The number of low bits that we reserve for other uses, and keep zero.
@ PointerBitMask
PointerBitMask - The bits that come from the pointer.
@ IntMask
IntMask - This is the unshifted mask for valid bits of the int type.
static intptr_t updatePointer(intptr_t OrigValue, PointerT Ptr)
static void * getAsVoidPointer(const PointerIntPair< PointerTy, IntBits, IntType > &P)
static PointerIntPair< PointerTy, IntBits, IntType > getFromVoidPointer(void *P)
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 ...
constexpr intptr_t asInt() const
constexpr PunnedPointer(intptr_t i=0)
const Ptr * getPointerAddress() const
constexpr PunnedPointer & operator=(intptr_t V)