LLVM: include/llvm/ADT/PointerIntPair.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14#ifndef LLVM_ADT_POINTERINTPAIR_H
15#define LLVM_ADT_POINTERINTPAIR_H
16
20#include
21#include
22#include
23#include
24
25namespace llvm {
26
29 static_assert(sizeof(Ptr) == sizeof(intptr_t), "");
30
31
32
33 static_assert(std::is_trivially_destructible::value, "");
34 static_assert(std::is_trivially_copy_constructible::value, "");
35 static_assert(std::is_trivially_move_constructible::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
48 std::memcpy(Data, &V, sizeof(Data));
49 return *this;
50 }
51
54
55private:
56 alignas(Ptr) unsigned char Data[sizeof(Ptr)];
57};
58}
59
60template <typename T, typename Enable> struct DenseMapInfo;
61template <typename PointerT, unsigned IntBits, typename PtrTraits>
62struct PointerIntPairInfo;
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77template <typename PointerTy, unsigned IntBits, typename IntType = unsigned,
78 typename PtrTraits = PointerLikeTypeTraits,
79 typename Info = PointerIntPairInfo<PointerTy, IntBits, PtrTraits>>
81
82 using InfoTy = Info;
84
85public:
87
90 }
91
93
95
96 IntType getInt() const { return (IntType)Info::getInt(Value); }
97
99 Value = Info::updatePointer(Value, PtrVal);
100 }
101
103 Value = Info::updateInt(Value, static_cast<intptr_t>(IntVal));
104 }
105
107 Value = Info::updatePointer(0, PtrVal);
108 }
109
111 Value = Info::updateInt(Info::updatePointer(0, PtrVal),
112 static_cast<intptr_t>(IntVal));
113 }
114
117 }
118
121 "Can only return the address if IntBits is cleared and "
122 "PtrTraits doesn't change the pointer");
123 return Value.getPointerAddress();
124 }
125
127 return reinterpret_cast<void *>(Value.asInt());
128 }
129
131 Value = reinterpret_cast<intptr_t>(Val);
132 }
133
136 P.setFromOpaqueValue(V);
137 return P;
138 }
139
140
141
143 (void)PtrTraits::getFromVoidPointer(V);
145 }
146
149 }
150
153 }
154
157
160 }
161
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");
175
177 ~(uintptr_t)(((intptr_t)1 << PtrTraits::NumLowBitsAvailable) - 1),
178
179
180
181 IntShift = (uintptr_t)PtrTraits::NumLowBitsAvailable - IntBits,
182
183
184 IntMask = (uintptr_t)(((intptr_t)1 << IntBits) - 1),
185
186
189
191 return PtrTraits::getFromVoidPointer(
193 }
194
197 }
198
200 intptr_t PtrWord =
201 reinterpret_cast<intptr_t>(PtrTraits::getAsVoidPointer(Ptr));
203 "Pointer is not sufficiently aligned");
204
205 return PtrWord | (OrigValue & ~PointerBitMask);
206 }
207
208 static intptr_t updateInt(intptr_t OrigValue, intptr_t Int) {
209 intptr_t IntWord = static_cast<intptr_t>(Int);
210 assert((IntWord & ~IntMask) == 0 && "Integer too large for field");
211
212
214 }
215};
216
217
218template <typename PointerTy, unsigned IntBits, typename IntType>
221
223 uintptr_t Val = static_cast<uintptr_t>(-1);
224 Val <<= PointerLikeTypeTraits::NumLowBitsAvailable;
225 return Ty::getFromOpaqueValue(reinterpret_cast<void *>(Val));
226 }
227
229 uintptr_t Val = static_cast<uintptr_t>(-2);
230 Val <<= PointerLikeTypeTraits::NumLowBitsAvailable;
231 return Ty::getFromOpaqueValue(reinterpret_cast<void *>(Val));
232 }
233
235 uintptr_t IV = reinterpret_cast<uintptr_t>(V.getOpaqueValue());
237 }
238
240};
241
242
243template <typename PointerTy, unsigned IntBits, typename IntType,
244 typename PtrTraits>
247 static inline void *
249 return P.getOpaqueValue();
250 }
251
255 }
256
260 }
261
262 static constexpr int NumLowBitsAvailable =
263 PtrTraits::NumLowBitsAvailable - IntBits;
264};
265
266
267template <std::size_t I, typename PointerTy, unsigned IntBits, typename IntType,
268 typename PtrTraits, typename Info>
269decltype(auto)
271 static_assert(I < 2);
272 if constexpr (I == 0)
274 else
275 return Pair.getInt();
276}
277
278}
279
280namespace std {
281template <typename PointerTy, unsigned IntBits, typename IntType,
282 typename PtrTraits, typename Info>
283struct tuple_size<
284 llvm::PointerIntPair<PointerTy, IntBits, IntType, PtrTraits, Info>>
285 : std::integral_constant<std::size_t, 2> {};
286
287template <std::size_t I, typename PointerTy, unsigned IntBits, typename IntType,
288 typename PtrTraits, typename Info>
289struct tuple_element<
290 I, llvm::PointerIntPair<PointerTy, IntBits, IntType, PtrTraits, Info>>
291 : std::conditional<I == 0, PointerTy, IntType> {};
292}
293
294#endif
Analysis containing CSE Info
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const uint32_t IV[8]
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) &
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.
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
Implement std::hash so that hash_code can be used in STL containers.
static unsigned getHashValue(Ty V)
static Ty getTombstoneKey()
static bool isEqual(const Ty &LHS, const Ty &RHS)
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
Ptr * getPointerAddress()
constexpr PunnedPointer & operator=(intptr_t V)