1 //===-- llvm/Support/Alignment.h - Useful alignment functions ---*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains types to represent alignments.
10 // They are instrumented to guarantee some invariants are preserved and prevent
11 // invalid manipulations.
13 // - Align represents an alignment in bytes, it is always set and always a valid
14 // power of two, its minimum value is 1 which means no alignment requirements.
16 // - MaybeAlign is an optional type, it may be undefined or set. When it's set
17 // you can get the underlying Align type by using the getValue() method.
19 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_SUPPORT_ALIGNMENT_H_
22 #define LLVM_SUPPORT_ALIGNMENT_H_
24 #include "llvm/ADT/Optional.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Support/MathExtras.h"
32 #define ALIGN_CHECK_ISPOSITIVE(decl) \
33 assert(decl > 0 && (#decl " should be defined"))
34 #define ALIGN_CHECK_ISSET(decl) \
35 assert(decl.hasValue() && (#decl " should be defined"))
37 /// This struct is a compact representation of a valid (non-zero power of two)
39 /// It is suitable for use as static global constants.
42 uint8_t ShiftValue
= 0; /// The log2 of the required alignment.
43 /// ShiftValue is less than 64 by construction.
45 friend struct MaybeAlign
;
46 friend unsigned Log2(Align
);
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
);
52 friend bool operator>(Align Lhs
, Align Rhs
);
53 friend unsigned encode(struct MaybeAlign A
);
54 friend struct MaybeAlign
decodeMaybeAlign(unsigned Value
);
56 /// A trivial type to allow construction of constexpr Align.
57 /// This is currently needed to workaround a bug in GCC 5.3 which prevents
58 /// definition of constexpr assign operators.
59 /// https://stackoverflow.com/questions/46756288/explicitly-defaulted-function-cannot-be-declared-as-constexpr-because-the-implic
60 /// FIXME: Remove this, make all assign operators constexpr and introduce user
61 /// defined literals when we don't have to support GCC 5.3 anymore.
62 /// https://llvm.org/docs/GettingStarted.html#getting-a-modern-host-c-toolchain
68 /// Default is byte-aligned.
69 constexpr Align() = default;
70 /// Do not perform checks in case of copy/move construct/assign, because the
71 /// checks have been performed when building `Other`.
72 constexpr Align(const Align
&Other
) = default;
73 constexpr Align(Align
&&Other
) = default;
74 Align
&operator=(const Align
&Other
) = default;
75 Align
&operator=(Align
&&Other
) = default;
77 explicit Align(uint64_t Value
) {
78 assert(Value
> 0 && "Value must not be 0");
79 assert(llvm::isPowerOf2_64(Value
) && "Alignment is not a power of 2");
80 ShiftValue
= Log2_64(Value
);
81 assert(ShiftValue
< 64 && "Broken invariant");
84 /// This is a hole in the type system and should not be abused.
85 /// Needed to interact with C for instance.
86 uint64_t value() const { return uint64_t(1) << ShiftValue
; }
88 /// Returns a default constructed Align which corresponds to no alignment.
89 /// This is useful to test for unalignment as it conveys clear semantic.
90 /// `if (A != Align::None())`
91 /// would be better than
92 /// `if (A > Align(1))`
93 constexpr static const Align
None() { return Align(); }
95 /// Allow constructions of constexpr Align.
96 template <size_t kValue
> constexpr static LogValue
Constant() {
97 return LogValue
{static_cast<uint8_t>(CTLog2
<kValue
>())};
100 /// Allow constructions of constexpr Align from types.
101 /// Compile time equivalent to Align(alignof(T)).
102 template <typename T
> constexpr static LogValue
Of() {
103 return Constant
<std::alignment_of
<T
>::value
>();
106 /// Constexpr constructor from LogValue type.
107 constexpr Align(LogValue CA
) : ShiftValue(CA
.Log
) {}
110 /// Treats the value 0 as a 1, so Align is always at least 1.
111 inline Align
assumeAligned(uint64_t Value
) {
112 return Value
? Align(Value
) : Align();
115 /// This struct is a compact representation of a valid (power of two) or
116 /// undefined (0) alignment.
117 struct MaybeAlign
: public llvm::Optional
<Align
> {
119 using UP
= llvm::Optional
<Align
>;
122 /// Default is undefined.
123 MaybeAlign() = default;
124 /// Do not perform checks in case of copy/move construct/assign, because the
125 /// checks have been performed when building `Other`.
126 MaybeAlign(const MaybeAlign
&Other
) = default;
127 MaybeAlign
&operator=(const MaybeAlign
&Other
) = default;
128 MaybeAlign(MaybeAlign
&&Other
) = default;
129 MaybeAlign
&operator=(MaybeAlign
&&Other
) = default;
131 /// Use llvm::Optional<Align> constructor.
134 explicit MaybeAlign(uint64_t Value
) {
135 assert((Value
== 0 || llvm::isPowerOf2_64(Value
)) &&
136 "Alignment is neither 0 nor a power of 2");
141 /// For convenience, returns a valid alignment or 1 if undefined.
142 Align
valueOrOne() const { return hasValue() ? getValue() : Align(); }
145 /// Checks that SizeInBytes is a multiple of the alignment.
146 inline bool isAligned(Align Lhs
, uint64_t SizeInBytes
) {
147 return SizeInBytes
% Lhs
.value() == 0;
150 /// Checks that SizeInBytes is a multiple of the alignment.
151 /// Returns false if the alignment is undefined.
152 inline bool isAligned(MaybeAlign Lhs
, uint64_t SizeInBytes
) {
153 ALIGN_CHECK_ISSET(Lhs
);
154 return SizeInBytes
% (*Lhs
).value() == 0;
157 /// Checks that Addr is a multiple of the alignment.
158 inline bool isAddrAligned(Align Lhs
, const void *Addr
) {
159 return isAligned(Lhs
, reinterpret_cast<uintptr_t>(Addr
));
162 /// Returns a multiple of A needed to store `Size` bytes.
163 inline uint64_t alignTo(uint64_t Size
, Align A
) {
164 const uint64_t value
= A
.value();
165 // The following line is equivalent to `(Size + value - 1) / value * value`.
167 // The division followed by a multiplication can be thought of as a right
168 // shift followed by a left shift which zeros out the extra bits produced in
169 // the bump; `~(value - 1)` is a mask where all those bits being zeroed out
172 // Most compilers can generate this code but the pattern may be missed when
173 // multiple functions gets inlined.
174 return (Size
+ value
- 1) & ~(value
- 1);
177 /// Returns a multiple of A needed to store `Size` bytes.
178 /// Returns `Size` if current alignment is undefined.
179 inline uint64_t alignTo(uint64_t Size
, MaybeAlign A
) {
180 return A
? alignTo(Size
, A
.getValue()) : Size
;
183 /// Aligns `Addr` to `Alignment` bytes, rounding up.
184 inline uintptr_t alignAddr(const void *Addr
, Align Alignment
) {
185 uintptr_t ArithAddr
= reinterpret_cast<uintptr_t>(Addr
);
186 assert(ArithAddr
+ Alignment
.value() - 1 >= ArithAddr
&& "Overflow");
187 return alignTo(ArithAddr
, Alignment
);
190 /// Returns the offset to the next integer (mod 2**64) that is greater than
191 /// or equal to \p Value and is a multiple of \p Align.
192 inline uint64_t offsetToAlignment(uint64_t Value
, Align Alignment
) {
193 return alignTo(Value
, Alignment
) - Value
;
196 /// Returns the necessary adjustment for aligning `Addr` to `Alignment`
197 /// bytes, rounding up.
198 inline uint64_t offsetToAlignedAddr(const void *Addr
, Align Alignment
) {
199 return offsetToAlignment(reinterpret_cast<uintptr_t>(Addr
), Alignment
);
202 /// Returns the log2 of the alignment.
203 inline unsigned Log2(Align A
) { return A
.ShiftValue
; }
205 /// Returns the log2 of the alignment.
206 /// \pre A must be defined.
207 inline unsigned Log2(MaybeAlign A
) {
208 ALIGN_CHECK_ISSET(A
);
209 return Log2(A
.getValue());
212 /// Returns the alignment that satisfies both alignments.
213 /// Same semantic as MinAlign.
214 inline Align
commonAlignment(Align A
, Align B
) { return std::min(A
, B
); }
216 /// Returns the alignment that satisfies both alignments.
217 /// Same semantic as MinAlign.
218 inline Align
commonAlignment(Align A
, uint64_t Offset
) {
219 return Align(MinAlign(A
.value(), Offset
));
222 /// Returns the alignment that satisfies both alignments.
223 /// Same semantic as MinAlign.
224 inline MaybeAlign
commonAlignment(MaybeAlign A
, MaybeAlign B
) {
225 return A
&& B
? commonAlignment(*A
, *B
) : A
? A
: B
;
228 /// Returns the alignment that satisfies both alignments.
229 /// Same semantic as MinAlign.
230 inline MaybeAlign
commonAlignment(MaybeAlign A
, uint64_t Offset
) {
231 return MaybeAlign(MinAlign((*A
).value(), Offset
));
234 /// Returns a representation of the alignment that encodes undefined as 0.
235 inline unsigned encode(MaybeAlign A
) { return A
? A
->ShiftValue
+ 1 : 0; }
237 /// Dual operation of the encode function above.
238 inline MaybeAlign
decodeMaybeAlign(unsigned Value
) {
242 Out
.ShiftValue
= Value
- 1;
246 /// Returns a representation of the alignment, the encoded value is positive by
248 inline unsigned encode(Align A
) { return encode(MaybeAlign(A
)); }
250 /// Comparisons between Align and scalars. Rhs must be positive.
251 inline bool operator==(Align Lhs
, uint64_t Rhs
) {
252 ALIGN_CHECK_ISPOSITIVE(Rhs
);
253 return Lhs
.value() == Rhs
;
255 inline bool operator!=(Align Lhs
, uint64_t Rhs
) {
256 ALIGN_CHECK_ISPOSITIVE(Rhs
);
257 return Lhs
.value() != Rhs
;
259 inline bool operator<=(Align Lhs
, uint64_t Rhs
) {
260 ALIGN_CHECK_ISPOSITIVE(Rhs
);
261 return Lhs
.value() <= Rhs
;
263 inline bool operator>=(Align Lhs
, uint64_t Rhs
) {
264 ALIGN_CHECK_ISPOSITIVE(Rhs
);
265 return Lhs
.value() >= Rhs
;
267 inline bool operator<(Align Lhs
, uint64_t Rhs
) {
268 ALIGN_CHECK_ISPOSITIVE(Rhs
);
269 return Lhs
.value() < Rhs
;
271 inline bool operator>(Align Lhs
, uint64_t Rhs
) {
272 ALIGN_CHECK_ISPOSITIVE(Rhs
);
273 return Lhs
.value() > Rhs
;
276 /// Comparisons between MaybeAlign and scalars.
277 inline bool operator==(MaybeAlign Lhs
, uint64_t Rhs
) {
278 return Lhs
? (*Lhs
).value() == Rhs
: Rhs
== 0;
280 inline bool operator!=(MaybeAlign Lhs
, uint64_t Rhs
) {
281 return Lhs
? (*Lhs
).value() != Rhs
: Rhs
!= 0;
283 inline bool operator<=(MaybeAlign Lhs
, uint64_t Rhs
) {
284 ALIGN_CHECK_ISSET(Lhs
);
285 ALIGN_CHECK_ISPOSITIVE(Rhs
);
286 return (*Lhs
).value() <= Rhs
;
288 inline bool operator>=(MaybeAlign Lhs
, uint64_t Rhs
) {
289 ALIGN_CHECK_ISSET(Lhs
);
290 ALIGN_CHECK_ISPOSITIVE(Rhs
);
291 return (*Lhs
).value() >= Rhs
;
293 inline bool operator<(MaybeAlign Lhs
, uint64_t Rhs
) {
294 ALIGN_CHECK_ISSET(Lhs
);
295 ALIGN_CHECK_ISPOSITIVE(Rhs
);
296 return (*Lhs
).value() < Rhs
;
298 inline bool operator>(MaybeAlign Lhs
, uint64_t Rhs
) {
299 ALIGN_CHECK_ISSET(Lhs
);
300 ALIGN_CHECK_ISPOSITIVE(Rhs
);
301 return (*Lhs
).value() > Rhs
;
304 /// Comparisons operators between Align.
305 inline bool operator==(Align Lhs
, Align Rhs
) {
306 return Lhs
.ShiftValue
== Rhs
.ShiftValue
;
308 inline bool operator!=(Align Lhs
, Align Rhs
) {
309 return Lhs
.ShiftValue
!= Rhs
.ShiftValue
;
311 inline bool operator<=(Align Lhs
, Align Rhs
) {
312 return Lhs
.ShiftValue
<= Rhs
.ShiftValue
;
314 inline bool operator>=(Align Lhs
, Align Rhs
) {
315 return Lhs
.ShiftValue
>= Rhs
.ShiftValue
;
317 inline bool operator<(Align Lhs
, Align Rhs
) {
318 return Lhs
.ShiftValue
< Rhs
.ShiftValue
;
320 inline bool operator>(Align Lhs
, Align Rhs
) {
321 return Lhs
.ShiftValue
> Rhs
.ShiftValue
;
324 /// Comparisons operators between Align and MaybeAlign.
325 inline bool operator==(Align Lhs
, MaybeAlign Rhs
) {
326 ALIGN_CHECK_ISSET(Rhs
);
327 return Lhs
.value() == (*Rhs
).value();
329 inline bool operator!=(Align Lhs
, MaybeAlign Rhs
) {
330 ALIGN_CHECK_ISSET(Rhs
);
331 return Lhs
.value() != (*Rhs
).value();
333 inline bool operator<=(Align Lhs
, MaybeAlign Rhs
) {
334 ALIGN_CHECK_ISSET(Rhs
);
335 return Lhs
.value() <= (*Rhs
).value();
337 inline bool operator>=(Align Lhs
, MaybeAlign Rhs
) {
338 ALIGN_CHECK_ISSET(Rhs
);
339 return Lhs
.value() >= (*Rhs
).value();
341 inline bool operator<(Align Lhs
, MaybeAlign Rhs
) {
342 ALIGN_CHECK_ISSET(Rhs
);
343 return Lhs
.value() < (*Rhs
).value();
345 inline bool operator>(Align Lhs
, MaybeAlign Rhs
) {
346 ALIGN_CHECK_ISSET(Rhs
);
347 return Lhs
.value() > (*Rhs
).value();
350 /// Comparisons operators between MaybeAlign and Align.
351 inline bool operator==(MaybeAlign Lhs
, Align Rhs
) {
352 ALIGN_CHECK_ISSET(Lhs
);
353 return Lhs
&& (*Lhs
).value() == Rhs
.value();
355 inline bool operator!=(MaybeAlign Lhs
, Align Rhs
) {
356 ALIGN_CHECK_ISSET(Lhs
);
357 return Lhs
&& (*Lhs
).value() != Rhs
.value();
359 inline bool operator<=(MaybeAlign Lhs
, Align Rhs
) {
360 ALIGN_CHECK_ISSET(Lhs
);
361 return Lhs
&& (*Lhs
).value() <= Rhs
.value();
363 inline bool operator>=(MaybeAlign Lhs
, Align Rhs
) {
364 ALIGN_CHECK_ISSET(Lhs
);
365 return Lhs
&& (*Lhs
).value() >= Rhs
.value();
367 inline bool operator<(MaybeAlign Lhs
, Align Rhs
) {
368 ALIGN_CHECK_ISSET(Lhs
);
369 return Lhs
&& (*Lhs
).value() < Rhs
.value();
371 inline bool operator>(MaybeAlign Lhs
, Align Rhs
) {
372 ALIGN_CHECK_ISSET(Lhs
);
373 return Lhs
&& (*Lhs
).value() > Rhs
.value();
376 inline Align
operator/(Align Lhs
, uint64_t Divisor
) {
377 assert(llvm::isPowerOf2_64(Divisor
) &&
378 "Divisor must be positive and a power of 2");
379 assert(Lhs
!= 1 && "Can't halve byte alignment");
380 return Align(Lhs
.value() / Divisor
);
383 inline MaybeAlign
operator/(MaybeAlign Lhs
, uint64_t Divisor
) {
384 assert(llvm::isPowerOf2_64(Divisor
) &&
385 "Divisor must be positive and a power of 2");
386 return Lhs
? Lhs
.getValue() / Divisor
: MaybeAlign();
389 inline Align
max(MaybeAlign Lhs
, Align Rhs
) {
390 return Lhs
&& *Lhs
> Rhs
? *Lhs
: Rhs
;
393 inline Align
max(Align Lhs
, MaybeAlign Rhs
) {
394 return Rhs
&& *Rhs
> Lhs
? *Rhs
: Lhs
;
397 #undef ALIGN_CHECK_ISPOSITIVE
398 #undef ALIGN_CHECK_ISSET
402 #endif // LLVM_SUPPORT_ALIGNMENT_H_