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
53 const Ptr *getPointerAddress() const { return reinterpret_cast<Ptr *>(Data); }
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
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
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
148 return Value == RHS.Value;
149 }
150
152 return Value != RHS.Value;
153 }
154
157
159 return Value <= RHS.Value;
160 }
161
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");
175
176 PointerBitMask = (~(uintptr_t)0) << PtrTraits::NumLowBitsAvailable,
177
178
179
180 IntShift = (uintptr_t)PtrTraits::NumLowBitsAvailable - IntBits,
181
182
183 IntMask = ((uintptr_t)1 << IntBits) - 1,
184
185
187 };
188
190 return PtrTraits::getFromVoidPointer(
192 }
193
197
198 static intptr_t updatePointer(intptr_t OrigValue, PointerT Ptr) {
199 intptr_t PtrWord =
200 reinterpret_cast<intptr_t>(PtrTraits::getAsVoidPointer(Ptr));
202 "Pointer is not sufficiently aligned");
203
205 }
206
213};
214
215
216template <typename PointerTy, unsigned IntBits, typename IntType>
219
221 uintptr_t Val = static_cast<uintptr_t>(-1);
222 Val <<= PointerLikeTypeTraits::NumLowBitsAvailable;
224 }
225
227 uintptr_t Val = static_cast<uintptr_t>(-2);
228 Val <<= PointerLikeTypeTraits::NumLowBitsAvailable;
230 }
231
233 uintptr_t IV = reinterpret_cast<uintptr_t>(V.getOpaqueValue());
235 }
236
238};
239
240
241template <typename PointerTy, unsigned IntBits, typename IntType,
242 typename PtrTraits>
245 static inline void *
247 return P.getOpaqueValue();
248 }
249
254
259
261 PtrTraits::NumLowBitsAvailable - IntBits;
262};
263
264
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)
272 else
273 return Pair.getInt();
274}
275
276}
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}
291
292#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Analysis containing CSE Info
static const uint32_t IV[8]
PointerIntPair - This class implements a pair of a pointer and small integer.
Definition PointerIntPair.h:80
bool operator<(const PointerIntPair &RHS) const
Definition PointerIntPair.h:155
void setPointer(PointerTy PtrVal) &
Definition PointerIntPair.h:98
PointerIntPair(PointerTy PtrVal)
Definition PointerIntPair.h:92
bool operator!=(const PointerIntPair &RHS) const
Definition PointerIntPair.h:151
static PointerIntPair getFromOpaqueValue(const void *V)
Definition PointerIntPair.h:142
bool operator==(const PointerIntPair &RHS) const
Definition PointerIntPair.h:147
PointerIntPair(PointerTy PtrVal, IntType IntVal)
Definition PointerIntPair.h:88
void initWithPointer(PointerTy PtrVal) &
Definition PointerIntPair.h:106
IntType getInt() const
Definition PointerIntPair.h:96
bool operator<=(const PointerIntPair &RHS) const
Definition PointerIntPair.h:158
void setInt(IntType IntVal) &
Definition PointerIntPair.h:102
void setPointerAndInt(PointerTy PtrVal, IntType IntVal) &
Definition PointerIntPair.h:110
void * getOpaqueValue() const
Definition PointerIntPair.h:126
constexpr PointerIntPair()=default
bool operator>=(const PointerIntPair &RHS) const
Definition PointerIntPair.h:162
static PointerIntPair getFromOpaqueValue(void *V)
Definition PointerIntPair.h:134
PointerTy * getAddrOfPointer()
Definition PointerIntPair.h:119
bool operator>(const PointerIntPair &RHS) const
Definition PointerIntPair.h:156
PointerTy const * getAddrOfPointer() const
Definition PointerIntPair.h:115
PointerTy getPointer() const
Definition PointerIntPair.h:94
void setFromOpaqueValue(void *Val) &
Definition PointerIntPair.h:130
LLVM Value Representation.
A self-contained host- and target-independent arbitrary-precision floating-point software implementat...
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
Definition PointerIntPair.h:268
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Implement std::hash so that hash_code can be used in STL containers.
static unsigned getHashValue(Ty V)
Definition PointerIntPair.h:232
static Ty getEmptyKey()
Definition PointerIntPair.h:220
static Ty getTombstoneKey()
Definition PointerIntPair.h:226
PointerIntPair< PointerTy, IntBits, IntType > Ty
Definition PointerIntPair.h:218
static bool isEqual(const Ty &LHS, const Ty &RHS)
Definition PointerIntPair.h:237
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition PointerIntPair.h:168
static PointerT getPointer(intptr_t Value)
Definition PointerIntPair.h:189
static intptr_t updateInt(intptr_t OrigValue, intptr_t Int)
Definition PointerIntPair.h:207
static intptr_t getInt(intptr_t Value)
Definition PointerIntPair.h:194
MaskAndShiftConstants
Definition PointerIntPair.h:174
@ IntShift
IntShift - The number of low bits that we reserve for other uses, and keep zero.
Definition PointerIntPair.h:180
@ PointerBitMask
PointerBitMask - The bits that come from the pointer.
Definition PointerIntPair.h:176
@ ShiftedIntMask
Definition PointerIntPair.h:186
@ IntMask
IntMask - This is the unshifted mask for valid bits of the int type.
Definition PointerIntPair.h:183
static intptr_t updatePointer(intptr_t OrigValue, PointerT Ptr)
Definition PointerIntPair.h:198
static void * getAsVoidPointer(const PointerIntPair< PointerTy, IntBits, IntType > &P)
Definition PointerIntPair.h:246
static PointerIntPair< PointerTy, IntBits, IntType > getFromVoidPointer(void *P)
Definition PointerIntPair.h:251
static PointerIntPair< PointerTy, IntBits, IntType > getFromVoidPointer(const void *P)
Definition PointerIntPair.h:256
static constexpr int NumLowBitsAvailable
Definition PointerIntPair.h:260
A traits type that is used to handle pointer types and things that are just wrappers for pointers as ...
Definition PointerIntPair.h:28
constexpr intptr_t asInt() const
Definition PointerIntPair.h:39
constexpr PunnedPointer(intptr_t i=0)
Definition PointerIntPair.h:37
const Ptr * getPointerAddress() const
Definition PointerIntPair.h:53
Ptr * getPointerAddress()
Definition PointerIntPair.h:52
constexpr PunnedPointer & operator=(intptr_t V)
Definition PointerIntPair.h:47