LLVM: include/llvm/ADT/Bitfields.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79#ifndef LLVM_ADT_BITFIELDS_H
80#define LLVM_ADT_BITFIELDS_H
81
82#include
83#include
84#include
85#include
86#include
87#include <type_traits>
88
90
91namespace llvm {
92
94
95
96
97template <typename Bitfield, typename StorageType> struct Impl {
98 static_assert(std::is_unsigned::value,
99 "Storage must be unsigned");
101
102 static constexpr size_t StorageBits = sizeof(StorageType) * CHAR_BIT;
103 static_assert(Bitfield::FirstBit < StorageBits, "Data must fit in mask");
104 static_assert(Bitfield::LastBit < StorageBits, "Data must fit in mask");
105 static constexpr StorageType LowMask =
107 static constexpr StorageType Mask = LowMask << Bitfield::Shift;
108
109
111 assert(UserValue <= UserMaxValue && "value is too big");
112 if constexpr (std::is_unsigned_v) {
114 } else {
115 static_assert(std::is_signed_v,
116 "IntegerType must be signed");
118 }
119 }
120
121
122
124 checkValue(UserValue, Bitfield::UserMaxValue);
125 const StorageType StorageValue = UserValue & LowMask;
127 Packed |= StorageValue << Bitfield::Shift;
128 }
129
130
131
133 const StorageType StorageValue = (Packed & Mask) >> Bitfield::Shift;
134 if constexpr (std::is_signed_v)
136 return StorageValue;
137 }
138
139
140
141 static StorageType test(StorageType Packed) { return Packed & Mask; }
142};
143
144
145
146
147
148
149
150
151
152template <typename T, bool = std::is_enum::value>
154 using type = std::underlying_type_t;
155};
157 static_assert(!std::is_same_v<T, bool> || sizeof(bool) == 1,
158 "T being bool requires sizeof(bool) == 1.");
159 using type = std::conditional_t<std::is_same_v<T, bool>, uint8_t, T>;
160};
161
162}
163
164
166
167
168
169
170
171
172 template <typename T, unsigned Offset, unsigned Size,
173 T MaxValue = std::is_enum::value
174 ? T(0)
175 : std::numeric_limits::max()>
185
186 private:
188
189 static_assert(Bits > 0, "Bits must be non zero");
190 static constexpr size_t TypeBits = sizeof(IntegerType) * CHAR_BIT;
191 static_assert(Bits <= TypeBits, "Bits may not be greater than T size");
192 static_assert(!std::is_enum::value || MaxValue != T(0),
193 "Enum Bitfields must provide a MaxValue");
194 static_assert(!std::is_enum::value ||
195 std::is_unsigned::value,
196 "Enum must be unsigned");
197 static_assert(std::is_integral::value &&
198 std::numeric_limits::is_integer,
199 "IntegerType must be an integer type");
200
201 static constexpr IntegerType UserMaxValue =
203 };
204
205
206 template <typename Bitfield, typename StorageType>
207 static typename Bitfield::Type get(StorageType Packed) {
209 return static_cast<typename Bitfield::Type>(I::extract(Packed));
210 }
211
212
213
214 template <typename Bitfield, typename StorageType>
215 static StorageType test(StorageType Packed) {
217 return I::test(Packed);
218 }
219
220
221
222 template <typename Bitfield, typename StorageType>
223 static void set(StorageType &Packed, typename Bitfield::Type Value) {
225 I::update(Packed, static_cast<typename Bitfield::IntegerType>(Value));
226 }
227
228
229 template <typename A, typename B> static constexpr bool isOverlapping() {
230 return A::LastBit >= B::FirstBit && B::LastBit >= A::FirstBit;
231 }
232
233 template static constexpr bool areContiguous() { return true; }
234 template <typename A, typename B, typename... Others>
236 return A::NextBit == B::FirstBit && areContiguous<B, Others...>();
237 }
238};
239
240}
241
242#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
LLVM Value Representation.
Definition Bitfields.h:93
This is an optimization pass for GlobalISel generic memory operations.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
constexpr T maskTrailingOnes(unsigned N)
Create a bitmask with the N right-most bits set to 1, and all other bits set to 0.
Describes an element of a Bitfield.
Definition Bitfields.h:176
static constexpr unsigned Shift
Definition Bitfields.h:180
static constexpr unsigned Bits
Definition Bitfields.h:181
typename bitfields_details::ResolveUnderlyingType< T >::type IntegerType
Definition Bitfields.h:178
static constexpr unsigned FirstBit
Definition Bitfields.h:182
static constexpr unsigned NextBit
Definition Bitfields.h:184
T Type
Definition Bitfields.h:177
static constexpr unsigned LastBit
Definition Bitfields.h:183
Holds functions to get, set or test bitfields.
Definition Bitfields.h:165
static constexpr bool areContiguous()
Definition Bitfields.h:233
static Bitfield::Type get(StorageType Packed)
Unpacks the field from the Packed value.
Definition Bitfields.h:207
static constexpr bool areContiguous()
Definition Bitfields.h:235
static constexpr bool isOverlapping()
Returns whether the two bitfields share common bits.
Definition Bitfields.h:229
static void set(StorageType &Packed, typename Bitfield::Type Value)
Sets the typed value in the provided Packed value.
Definition Bitfields.h:223
static StorageType test(StorageType Packed)
Return a non-zero value if the field is non-zero.
Definition Bitfields.h:215
Impl is where Bifield description and Storage are put together to interact with values.
Definition Bitfields.h:97
static constexpr size_t StorageBits
Definition Bitfields.h:102
static IntegerType extract(StorageType Packed)
Interprets bits between FirstBit and LastBit of Packed as anIntegerType.
Definition Bitfields.h:132
static void checkValue(IntegerType UserValue, IntegerType UserMaxValue)
Validates that UserValue fits within the bitfield's range.
Definition Bitfields.h:110
static void update(StorageType &Packed, IntegerType UserValue)
Checks UserValue is within bounds and packs it between FirstBit and LastBit of Packed leaving the res...
Definition Bitfields.h:123
static constexpr StorageType LowMask
Definition Bitfields.h:105
static StorageType test(StorageType Packed)
Interprets bits between FirstBit and LastBit of Packed as anIntegerType.
Definition Bitfields.h:141
static constexpr StorageType Mask
Definition Bitfields.h:107
typename Bitfield::IntegerType IntegerType
Definition Bitfields.h:100
std::conditional_t< std::is_same_v< T, bool >, uint8_t, T > type
Definition Bitfields.h:159
Bitfield deals with the following type:
Definition Bitfields.h:153
std::underlying_type_t< T > type
Definition Bitfields.h:154