[Alignment][NFC] Move and type functions from MathExtras to Alignment
[llvm-core.git] / include / llvm / Support / Alignment.h
blobd86f4cce42904ee8059c60ec8b69f4840ee6eab4
1 //===-- llvm/Support/Alignment.h - Useful alignment functions ---*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
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"
27 #include <cassert>
28 #include <limits>
30 namespace llvm {
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)
38 /// alignment.
39 /// It is suitable for use as static global constants.
40 struct Align {
41 private:
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
63 struct LogValue {
64 uint8_t Log;
67 public:
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> {
118 private:
119 using UP = llvm::Optional<Align>;
121 public:
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.
132 using UP::UP;
134 explicit MaybeAlign(uint64_t Value) {
135 assert((Value == 0 || llvm::isPowerOf2_64(Value)) &&
136 "Alignment is neither 0 nor a power of 2");
137 if (Value)
138 emplace(Value);
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 return (Size + A.value() - 1) / A.value() * A.value();
167 /// Returns a multiple of A needed to store `Size` bytes.
168 /// Returns `Size` if current alignment is undefined.
169 inline uint64_t alignTo(uint64_t Size, MaybeAlign A) {
170 return A ? alignTo(Size, A.getValue()) : Size;
173 /// Aligns `Addr` to `Alignment` bytes, rounding up.
174 inline uintptr_t alignAddr(const void *Addr, Align Alignment) {
175 uintptr_t ArithAddr = reinterpret_cast<uintptr_t>(Addr);
176 assert(ArithAddr + Alignment.value() - 1 >= ArithAddr && "Overflow");
177 return alignTo(ArithAddr, Alignment);
180 /// Returns the offset to the next integer (mod 2**64) that is greater than
181 /// or equal to \p Value and is a multiple of \p Align.
182 inline uint64_t offsetToAlignment(uint64_t Value, Align Alignment) {
183 return alignTo(Value, Alignment) - Value;
186 /// Returns the necessary adjustment for aligning `Addr` to `Alignment`
187 /// bytes, rounding up.
188 inline uint64_t offsetToAlignedAddr(const void *Addr, Align Alignment) {
189 return offsetToAlignment(reinterpret_cast<uintptr_t>(Addr), Alignment);
192 /// Returns the log2 of the alignment.
193 inline unsigned Log2(Align A) { return A.ShiftValue; }
195 /// Returns the log2 of the alignment.
196 /// \pre A must be defined.
197 inline unsigned Log2(MaybeAlign A) {
198 ALIGN_CHECK_ISSET(A);
199 return Log2(A.getValue());
202 /// Returns the alignment that satisfies both alignments.
203 /// Same semantic as MinAlign.
204 inline Align commonAlignment(Align A, Align B) { return std::min(A, B); }
206 /// Returns the alignment that satisfies both alignments.
207 /// Same semantic as MinAlign.
208 inline Align commonAlignment(Align A, uint64_t Offset) {
209 return Align(MinAlign(A.value(), Offset));
212 /// Returns the alignment that satisfies both alignments.
213 /// Same semantic as MinAlign.
214 inline MaybeAlign commonAlignment(MaybeAlign A, MaybeAlign B) {
215 return A && B ? commonAlignment(*A, *B) : A ? A : B;
218 /// Returns the alignment that satisfies both alignments.
219 /// Same semantic as MinAlign.
220 inline MaybeAlign commonAlignment(MaybeAlign A, uint64_t Offset) {
221 return MaybeAlign(MinAlign((*A).value(), Offset));
224 /// Returns a representation of the alignment that encodes undefined as 0.
225 inline unsigned encode(MaybeAlign A) { return A ? A->ShiftValue + 1 : 0; }
227 /// Dual operation of the encode function above.
228 inline MaybeAlign decodeMaybeAlign(unsigned Value) {
229 if (Value == 0)
230 return MaybeAlign();
231 Align Out;
232 Out.ShiftValue = Value - 1;
233 return Out;
236 /// Returns a representation of the alignment, the encoded value is positive by
237 /// definition.
238 inline unsigned encode(Align A) { return encode(MaybeAlign(A)); }
240 /// Comparisons between Align and scalars. Rhs must be positive.
241 inline bool operator==(Align Lhs, uint64_t Rhs) {
242 ALIGN_CHECK_ISPOSITIVE(Rhs);
243 return Lhs.value() == Rhs;
245 inline bool operator!=(Align Lhs, uint64_t Rhs) {
246 ALIGN_CHECK_ISPOSITIVE(Rhs);
247 return Lhs.value() != Rhs;
249 inline bool operator<=(Align Lhs, uint64_t Rhs) {
250 ALIGN_CHECK_ISPOSITIVE(Rhs);
251 return Lhs.value() <= Rhs;
253 inline bool operator>=(Align Lhs, uint64_t Rhs) {
254 ALIGN_CHECK_ISPOSITIVE(Rhs);
255 return Lhs.value() >= Rhs;
257 inline bool operator<(Align Lhs, uint64_t Rhs) {
258 ALIGN_CHECK_ISPOSITIVE(Rhs);
259 return Lhs.value() < Rhs;
261 inline bool operator>(Align Lhs, uint64_t Rhs) {
262 ALIGN_CHECK_ISPOSITIVE(Rhs);
263 return Lhs.value() > Rhs;
266 /// Comparisons between MaybeAlign and scalars.
267 inline bool operator==(MaybeAlign Lhs, uint64_t Rhs) {
268 return Lhs ? (*Lhs).value() == Rhs : Rhs == 0;
270 inline bool operator!=(MaybeAlign Lhs, uint64_t Rhs) {
271 return Lhs ? (*Lhs).value() != Rhs : Rhs != 0;
273 inline bool operator<=(MaybeAlign Lhs, uint64_t Rhs) {
274 ALIGN_CHECK_ISSET(Lhs);
275 ALIGN_CHECK_ISPOSITIVE(Rhs);
276 return (*Lhs).value() <= Rhs;
278 inline bool operator>=(MaybeAlign Lhs, uint64_t Rhs) {
279 ALIGN_CHECK_ISSET(Lhs);
280 ALIGN_CHECK_ISPOSITIVE(Rhs);
281 return (*Lhs).value() >= Rhs;
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;
294 /// Comparisons operators between Align.
295 inline bool operator==(Align Lhs, Align Rhs) {
296 return Lhs.ShiftValue == Rhs.ShiftValue;
298 inline bool operator!=(Align Lhs, Align Rhs) {
299 return Lhs.ShiftValue != Rhs.ShiftValue;
301 inline bool operator<=(Align Lhs, Align Rhs) {
302 return Lhs.ShiftValue <= Rhs.ShiftValue;
304 inline bool operator>=(Align Lhs, Align Rhs) {
305 return Lhs.ShiftValue >= Rhs.ShiftValue;
307 inline bool operator<(Align Lhs, Align Rhs) {
308 return Lhs.ShiftValue < Rhs.ShiftValue;
310 inline bool operator>(Align Lhs, Align Rhs) {
311 return Lhs.ShiftValue > Rhs.ShiftValue;
314 /// Comparisons operators between Align and MaybeAlign.
315 inline bool operator==(Align Lhs, MaybeAlign Rhs) {
316 ALIGN_CHECK_ISSET(Rhs);
317 return Lhs.value() == (*Rhs).value();
319 inline bool operator!=(Align Lhs, MaybeAlign Rhs) {
320 ALIGN_CHECK_ISSET(Rhs);
321 return Lhs.value() != (*Rhs).value();
323 inline bool operator<=(Align Lhs, MaybeAlign Rhs) {
324 ALIGN_CHECK_ISSET(Rhs);
325 return Lhs.value() <= (*Rhs).value();
327 inline bool operator>=(Align Lhs, MaybeAlign Rhs) {
328 ALIGN_CHECK_ISSET(Rhs);
329 return Lhs.value() >= (*Rhs).value();
331 inline bool operator<(Align Lhs, MaybeAlign Rhs) {
332 ALIGN_CHECK_ISSET(Rhs);
333 return Lhs.value() < (*Rhs).value();
335 inline bool operator>(Align Lhs, MaybeAlign Rhs) {
336 ALIGN_CHECK_ISSET(Rhs);
337 return Lhs.value() > (*Rhs).value();
340 /// Comparisons operators between MaybeAlign and Align.
341 inline bool operator==(MaybeAlign Lhs, Align Rhs) {
342 ALIGN_CHECK_ISSET(Lhs);
343 return Lhs && (*Lhs).value() == Rhs.value();
345 inline bool operator!=(MaybeAlign Lhs, Align Rhs) {
346 ALIGN_CHECK_ISSET(Lhs);
347 return Lhs && (*Lhs).value() != Rhs.value();
349 inline bool operator<=(MaybeAlign Lhs, Align Rhs) {
350 ALIGN_CHECK_ISSET(Lhs);
351 return Lhs && (*Lhs).value() <= Rhs.value();
353 inline bool operator>=(MaybeAlign Lhs, Align Rhs) {
354 ALIGN_CHECK_ISSET(Lhs);
355 return Lhs && (*Lhs).value() >= Rhs.value();
357 inline bool operator<(MaybeAlign Lhs, Align Rhs) {
358 ALIGN_CHECK_ISSET(Lhs);
359 return Lhs && (*Lhs).value() < Rhs.value();
361 inline bool operator>(MaybeAlign Lhs, Align Rhs) {
362 ALIGN_CHECK_ISSET(Lhs);
363 return Lhs && (*Lhs).value() > Rhs.value();
366 inline Align operator/(Align Lhs, uint64_t Divisor) {
367 assert(llvm::isPowerOf2_64(Divisor) &&
368 "Divisor must be positive and a power of 2");
369 assert(Lhs != 1 && "Can't halve byte alignment");
370 return Align(Lhs.value() / Divisor);
373 inline MaybeAlign operator/(MaybeAlign Lhs, uint64_t Divisor) {
374 assert(llvm::isPowerOf2_64(Divisor) &&
375 "Divisor must be positive and a power of 2");
376 return Lhs ? Lhs.getValue() / Divisor : MaybeAlign();
379 inline Align max(MaybeAlign Lhs, Align Rhs) {
380 return Lhs && *Lhs > Rhs ? *Lhs : Rhs;
383 inline Align max(Align Lhs, MaybeAlign Rhs) {
384 return Rhs && *Rhs > Lhs ? *Rhs : Lhs;
387 #undef ALIGN_CHECK_ISPOSITIVE
388 #undef ALIGN_CHECK_ISSET
390 } // namespace llvm
392 #endif // LLVM_SUPPORT_ALIGNMENT_H_