[Alignment][NFC] Support compile time constants
[llvm-core.git] / include / llvm / Support / Alignment.h
blob5e8e686150d26b402df5419149022cad9145cf76
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{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 /// Returns a multiple of A needed to store `Size` bytes.
158 inline uint64_t alignTo(uint64_t Size, Align A) {
159 return (Size + A.value() - 1) / A.value() * A.value();
162 /// Returns a multiple of A needed to store `Size` bytes.
163 /// Returns `Size` if current alignment is undefined.
164 inline uint64_t alignTo(uint64_t Size, MaybeAlign A) {
165 return A ? alignTo(Size, A.getValue()) : Size;
168 /// Returns the offset to the next integer (mod 2**64) that is greater than
169 /// or equal to \p Value and is a multiple of \p Align.
170 inline uint64_t offsetToAlignment(uint64_t Value, Align Alignment) {
171 return alignTo(Value, Alignment) - Value;
174 /// Returns the log2 of the alignment.
175 inline unsigned Log2(Align A) { return A.ShiftValue; }
177 /// Returns the log2 of the alignment.
178 /// \pre A must be defined.
179 inline unsigned Log2(MaybeAlign A) {
180 ALIGN_CHECK_ISSET(A);
181 return Log2(A.getValue());
184 /// Returns the alignment that satisfies both alignments.
185 /// Same semantic as MinAlign.
186 inline Align commonAlignment(Align A, Align B) { return std::min(A, B); }
188 /// Returns the alignment that satisfies both alignments.
189 /// Same semantic as MinAlign.
190 inline Align commonAlignment(Align A, uint64_t Offset) {
191 return Align(MinAlign(A.value(), Offset));
194 /// Returns the alignment that satisfies both alignments.
195 /// Same semantic as MinAlign.
196 inline MaybeAlign commonAlignment(MaybeAlign A, MaybeAlign B) {
197 return A && B ? commonAlignment(*A, *B) : A ? A : B;
200 /// Returns the alignment that satisfies both alignments.
201 /// Same semantic as MinAlign.
202 inline MaybeAlign commonAlignment(MaybeAlign A, uint64_t Offset) {
203 return MaybeAlign(MinAlign((*A).value(), Offset));
206 /// Returns a representation of the alignment that encodes undefined as 0.
207 inline unsigned encode(MaybeAlign A) { return A ? A->ShiftValue + 1 : 0; }
209 /// Dual operation of the encode function above.
210 inline MaybeAlign decodeMaybeAlign(unsigned Value) {
211 if (Value == 0)
212 return MaybeAlign();
213 Align Out;
214 Out.ShiftValue = Value - 1;
215 return Out;
218 /// Returns a representation of the alignment, the encoded value is positive by
219 /// definition.
220 inline unsigned encode(Align A) { return encode(MaybeAlign(A)); }
222 /// Comparisons between Align and scalars. Rhs must be positive.
223 inline bool operator==(Align Lhs, uint64_t Rhs) {
224 ALIGN_CHECK_ISPOSITIVE(Rhs);
225 return Lhs.value() == Rhs;
227 inline bool operator!=(Align Lhs, uint64_t Rhs) {
228 ALIGN_CHECK_ISPOSITIVE(Rhs);
229 return Lhs.value() != Rhs;
231 inline bool operator<=(Align Lhs, uint64_t Rhs) {
232 ALIGN_CHECK_ISPOSITIVE(Rhs);
233 return Lhs.value() <= Rhs;
235 inline bool operator>=(Align Lhs, uint64_t Rhs) {
236 ALIGN_CHECK_ISPOSITIVE(Rhs);
237 return Lhs.value() >= Rhs;
239 inline bool operator<(Align Lhs, uint64_t Rhs) {
240 ALIGN_CHECK_ISPOSITIVE(Rhs);
241 return Lhs.value() < Rhs;
243 inline bool operator>(Align Lhs, uint64_t Rhs) {
244 ALIGN_CHECK_ISPOSITIVE(Rhs);
245 return Lhs.value() > Rhs;
248 /// Comparisons between MaybeAlign and scalars.
249 inline bool operator==(MaybeAlign Lhs, uint64_t Rhs) {
250 return Lhs ? (*Lhs).value() == Rhs : Rhs == 0;
252 inline bool operator!=(MaybeAlign Lhs, uint64_t Rhs) {
253 return Lhs ? (*Lhs).value() != Rhs : Rhs != 0;
255 inline bool operator<=(MaybeAlign Lhs, uint64_t Rhs) {
256 ALIGN_CHECK_ISSET(Lhs);
257 ALIGN_CHECK_ISPOSITIVE(Rhs);
258 return (*Lhs).value() <= Rhs;
260 inline bool operator>=(MaybeAlign Lhs, uint64_t Rhs) {
261 ALIGN_CHECK_ISSET(Lhs);
262 ALIGN_CHECK_ISPOSITIVE(Rhs);
263 return (*Lhs).value() >= Rhs;
265 inline bool operator<(MaybeAlign Lhs, uint64_t Rhs) {
266 ALIGN_CHECK_ISSET(Lhs);
267 ALIGN_CHECK_ISPOSITIVE(Rhs);
268 return (*Lhs).value() < Rhs;
270 inline bool operator>(MaybeAlign Lhs, uint64_t Rhs) {
271 ALIGN_CHECK_ISSET(Lhs);
272 ALIGN_CHECK_ISPOSITIVE(Rhs);
273 return (*Lhs).value() > Rhs;
276 /// Comparisons operators between Align.
277 inline bool operator==(Align Lhs, Align Rhs) {
278 return Lhs.ShiftValue == Rhs.ShiftValue;
280 inline bool operator!=(Align Lhs, Align Rhs) {
281 return Lhs.ShiftValue != Rhs.ShiftValue;
283 inline bool operator<=(Align Lhs, Align Rhs) {
284 return Lhs.ShiftValue <= Rhs.ShiftValue;
286 inline bool operator>=(Align Lhs, Align Rhs) {
287 return Lhs.ShiftValue >= Rhs.ShiftValue;
289 inline bool operator<(Align Lhs, Align Rhs) {
290 return Lhs.ShiftValue < Rhs.ShiftValue;
292 inline bool operator>(Align Lhs, Align Rhs) {
293 return Lhs.ShiftValue > Rhs.ShiftValue;
296 /// Comparisons operators between Align and MaybeAlign.
297 inline bool operator==(Align Lhs, MaybeAlign Rhs) {
298 ALIGN_CHECK_ISSET(Rhs);
299 return Lhs.value() == (*Rhs).value();
301 inline bool operator!=(Align Lhs, MaybeAlign Rhs) {
302 ALIGN_CHECK_ISSET(Rhs);
303 return Lhs.value() != (*Rhs).value();
305 inline bool operator<=(Align Lhs, MaybeAlign Rhs) {
306 ALIGN_CHECK_ISSET(Rhs);
307 return Lhs.value() <= (*Rhs).value();
309 inline bool operator>=(Align Lhs, MaybeAlign Rhs) {
310 ALIGN_CHECK_ISSET(Rhs);
311 return Lhs.value() >= (*Rhs).value();
313 inline bool operator<(Align Lhs, MaybeAlign Rhs) {
314 ALIGN_CHECK_ISSET(Rhs);
315 return Lhs.value() < (*Rhs).value();
317 inline bool operator>(Align Lhs, MaybeAlign Rhs) {
318 ALIGN_CHECK_ISSET(Rhs);
319 return Lhs.value() > (*Rhs).value();
322 /// Comparisons operators between MaybeAlign and Align.
323 inline bool operator==(MaybeAlign Lhs, Align Rhs) {
324 ALIGN_CHECK_ISSET(Lhs);
325 return Lhs && (*Lhs).value() == Rhs.value();
327 inline bool operator!=(MaybeAlign Lhs, Align Rhs) {
328 ALIGN_CHECK_ISSET(Lhs);
329 return Lhs && (*Lhs).value() != Rhs.value();
331 inline bool operator<=(MaybeAlign Lhs, Align Rhs) {
332 ALIGN_CHECK_ISSET(Lhs);
333 return Lhs && (*Lhs).value() <= Rhs.value();
335 inline bool operator>=(MaybeAlign Lhs, Align Rhs) {
336 ALIGN_CHECK_ISSET(Lhs);
337 return Lhs && (*Lhs).value() >= Rhs.value();
339 inline bool operator<(MaybeAlign Lhs, Align Rhs) {
340 ALIGN_CHECK_ISSET(Lhs);
341 return Lhs && (*Lhs).value() < Rhs.value();
343 inline bool operator>(MaybeAlign Lhs, Align Rhs) {
344 ALIGN_CHECK_ISSET(Lhs);
345 return Lhs && (*Lhs).value() > Rhs.value();
348 inline Align operator/(Align Lhs, uint64_t Divisor) {
349 assert(llvm::isPowerOf2_64(Divisor) &&
350 "Divisor must be positive and a power of 2");
351 assert(Lhs != 1 && "Can't halve byte alignment");
352 return Align(Lhs.value() / Divisor);
355 inline MaybeAlign operator/(MaybeAlign Lhs, uint64_t Divisor) {
356 assert(llvm::isPowerOf2_64(Divisor) &&
357 "Divisor must be positive and a power of 2");
358 return Lhs ? Lhs.getValue() / Divisor : MaybeAlign();
361 inline Align max(MaybeAlign Lhs, Align Rhs) {
362 return Lhs && *Lhs > Rhs ? *Lhs : Rhs;
365 inline Align max(Align Lhs, MaybeAlign Rhs) {
366 return Rhs && *Rhs > Lhs ? *Rhs : Lhs;
369 #undef ALIGN_CHECK_ISPOSITIVE
370 #undef ALIGN_CHECK_ISSET
372 } // namespace llvm
374 #endif // LLVM_SUPPORT_ALIGNMENT_H_