LLVM: include/llvm/Support/Alignment.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#ifndef LLVM_SUPPORT_ALIGNMENT_H_
22#define LLVM_SUPPORT_ALIGNMENT_H_
23
25#include
26#include
27#ifndef NDEBUG
28#include
29#endif
30
31namespace llvm {
32
33#define ALIGN_CHECK_ISPOSITIVE(decl) \
34 assert(decl > 0 && (#decl " should be defined"))
35
36
37
38
39struct Align {
40private:
41 uint8_t ShiftValue = 0;
42
43
45 friend unsigned Log2(Align);
46 friend bool operator==(Align Lhs, Align Rhs);
47 friend bool operator!=(Align Lhs, Align Rhs);
48 friend bool operator<=(Align Lhs, Align Rhs);
49 friend bool operator>=(Align Lhs, Align Rhs);
50 friend bool operator<(Align Lhs, Align Rhs);
51 friend bool operator>(Align Lhs, Align Rhs);
54
55 struct FromShiftValue {};
56 constexpr Align(FromShiftValue, uint8_t Shift) : ShiftValue(Shift) {}
57
58public:
59
60 constexpr Align() = default;
61
62
63 constexpr Align(const Align &Other) = default;
67
69 assert(Value > 0 && "Value must not be 0");
72 assert(ShiftValue < 64 && "Broken invariant");
73 }
74
75
76
78
79
81 assert(ShiftValue != 0 && "Undefined operation");
82 Align Out;
83 Out.ShiftValue = ShiftValue - 1;
84 return Out;
85 }
86
87
88 template <size_t kValue> constexpr static Align Constant() {
90 }
91
92
93
94 template constexpr static Align Of() {
96 }
97};
98
99
103
104
105
107private:
108 using UP = std::optional;
109
110public:
111
113
114
119
124 "Alignment is neither 0 nor a power of 2");
127 }
128
129
131};
132
133
135 return SizeInBytes % Lhs.value() == 0;
136}
137
138
140 return isAligned(Lhs, reinterpret_cast<uintptr_t>(Addr));
141}
142
143
146
147
148
149
150
151
152
153
154
156}
157
158
159
160
161
162
163
164
165
166
167
168
174
175
177 uintptr_t ArithAddr = reinterpret_cast<uintptr_t>(Addr);
178 assert(static_cast<uintptr_t>(ArithAddr + Alignment.value() - 1) >=
179 ArithAddr &&
180 "Overflow");
181 return alignTo(ArithAddr, Alignment);
182}
183
184
185
189
190
191
195
196
197inline unsigned Log2(Align A) { return A.ShiftValue; }
198
199
200
204
205
207
208
212 Align Out;
213 Out.ShiftValue = Value - 1;
214 return Out;
215}
216
217
218
220
221
224 return Lhs.value() == Rhs;
225}
228 return Lhs.value() != Rhs;
229}
232 return Lhs.value() <= Rhs;
233}
236 return Lhs.value() >= Rhs;
237}
240 return Lhs.value() < Rhs;
241}
244 return Lhs.value() > Rhs;
245}
246
247
249 return Lhs.ShiftValue == Rhs.ShiftValue;
250}
252 return Lhs.ShiftValue != Rhs.ShiftValue;
253}
255 return Lhs.ShiftValue <= Rhs.ShiftValue;
256}
258 return Lhs.ShiftValue >= Rhs.ShiftValue;
259}
261 return Lhs.ShiftValue < Rhs.ShiftValue;
262}
264 return Lhs.ShiftValue > Rhs.ShiftValue;
265}
266
267
272
277
282
283
288
290 return (Lhs && Rhs && (*Lhs == *Rhs)) || (!Lhs && !Rhs);
291}
293
298
299#ifndef NDEBUG
300
302 return std::to_string(A.value());
303}
304
306 if (MA)
307 return std::to_string(MA->value());
308 return "None";
309}
310#endif
311
312#undef ALIGN_CHECK_ISPOSITIVE
313
314}
315
316#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define ALIGN_CHECK_ISPOSITIVE(decl)
Definition Alignment.h:33
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static TripleVec::iterator emplace(TripleVec &Container, Triple &&T)
LLVM Value Representation.
This is an optimization pass for GlobalISel generic memory operations.
bool operator<(int64_t V1, const APSInt &V2)
unsigned encode(MaybeAlign A)
Returns a representation of the alignment that encodes undefined as 0.
Definition Alignment.h:206
bool isAligned(Align Lhs, uint64_t SizeInBytes)
Checks that SizeInBytes is a multiple of the alignment.
Definition Alignment.h:134
bool operator!=(uint64_t V1, const APInt &V2)
bool operator>=(int64_t V1, const APSInt &V2)
constexpr size_t ConstantLog2()
Compile time Log2.
uint64_t offsetToAlignedAddr(const void *Addr, Align Alignment)
Returns the necessary adjustment for aligning Addr to Alignment bytes, rounding up.
Definition Alignment.h:192
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
constexpr T MinAlign(U A, V B)
A and B are either alignments or offsets.
bool operator>(int64_t V1, const APSInt &V2)
uint64_t offsetToAlignment(uint64_t Value, Align Alignment)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
Definition Alignment.h:186
MaybeAlign decodeMaybeAlign(unsigned Value)
Dual operation of the encode function above.
Definition Alignment.h:209
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:201
Align assumeAligned(uint64_t Value)
Treats the value 0 as a 1, so Align is always at least 1.
Definition Alignment.h:100
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
bool operator<=(int64_t V1, const APSInt &V2)
bool isAddrAligned(Align Lhs, const void *Addr)
Checks that Addr is a multiple of the alignment.
Definition Alignment.h:139
uintptr_t alignAddr(const void *Addr, Align Alignment)
Aligns Addr to Alignment bytes, rounding up.
Definition Alignment.h:176
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
friend unsigned encode(struct MaybeAlign A)
Returns a representation of the alignment that encodes undefined as 0.
Definition Alignment.h:206
Align previous() const
Definition Alignment.h:80
constexpr Align & operator=(const Align &Other)=default
friend bool operator>=(Align Lhs, Align Rhs)
Definition Alignment.h:257
static constexpr Align Of()
Allow constructions of constexpr Align from types.
Definition Alignment.h:94
friend struct MaybeAlign decodeMaybeAlign(unsigned Value)
Dual operation of the encode function above.
Definition Alignment.h:209
Align(uint64_t Value)
Definition Alignment.h:68
constexpr Align(Align &&Other)=default
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
friend bool operator<(Align Lhs, Align Rhs)
Definition Alignment.h:260
static constexpr Align Constant()
Allow constructions of constexpr Align.
Definition Alignment.h:88
friend bool operator==(Align Lhs, Align Rhs)
Comparisons operators between Align.
Definition Alignment.h:248
friend bool operator<=(Align Lhs, Align Rhs)
Definition Alignment.h:254
friend unsigned Log2(Align)
Returns the log2 of the alignment.
Definition Alignment.h:197
constexpr Align()=default
Default is byte-aligned.
constexpr Align & operator=(Align &&Other)=default
friend struct MaybeAlign
The log2 of the required alignment.
Definition Alignment.h:44
friend bool operator!=(Align Lhs, Align Rhs)
Definition Alignment.h:251
constexpr Align(const Align &Other)=default
Do not perform checks in case of copy/move construct/assign, because the checks have been performed w...
friend bool operator>(Align Lhs, Align Rhs)
Definition Alignment.h:263
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:106
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Definition Alignment.h:130
MaybeAlign(MaybeAlign &&Other)=default
constexpr MaybeAlign(Align Value)
Definition Alignment.h:121
constexpr MaybeAlign(std::nullopt_t None)
Definition Alignment.h:120
MaybeAlign(const MaybeAlign &Other)=default
Do not perform checks in case of copy/move construct/assign, because the checks have been performed w...
MaybeAlign(uint64_t Value)
Definition Alignment.h:122
MaybeAlign()=default
Default is undefined.
MaybeAlign & operator=(MaybeAlign &&Other)=default
MaybeAlign & operator=(const MaybeAlign &Other)=default