[LLVM][NFC] Removing unused functions
[llvm-complete.git] / include / llvm / Support / Alignment.h
blob23dec64f80e3f789414c0e336c2d88ebda2d549c
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 public:
57 /// Default is byte-aligned.
58 Align() = default;
59 /// Do not perform checks in case of copy/move construct/assign, because the
60 /// checks have been performed when building `Other`.
61 Align(const Align &Other) = default;
62 Align &operator=(const Align &Other) = default;
63 Align(Align &&Other) = default;
64 Align &operator=(Align &&Other) = default;
66 explicit Align(uint64_t Value) {
67 assert(Value > 0 && "Value must not be 0");
68 assert(llvm::isPowerOf2_64(Value) && "Alignment is not a power of 2");
69 ShiftValue = Log2_64(Value);
70 assert(ShiftValue < 64 && "Broken invariant");
73 /// This is a hole in the type system and should not be abused.
74 /// Needed to interact with C for instance.
75 uint64_t value() const { return uint64_t(1) << ShiftValue; }
78 /// Treats the value 0 as a 1, so Align is always at least 1.
79 inline Align assumeAligned(uint64_t Value) {
80 return Value ? Align(Value) : Align();
83 /// This struct is a compact representation of a valid (power of two) or
84 /// undefined (0) alignment.
85 struct MaybeAlign : public llvm::Optional<Align> {
86 private:
87 using UP = llvm::Optional<Align>;
89 public:
90 /// Default is undefined.
91 MaybeAlign() = default;
92 /// Do not perform checks in case of copy/move construct/assign, because the
93 /// checks have been performed when building `Other`.
94 MaybeAlign(const MaybeAlign &Other) = default;
95 MaybeAlign &operator=(const MaybeAlign &Other) = default;
96 MaybeAlign(MaybeAlign &&Other) = default;
97 MaybeAlign &operator=(MaybeAlign &&Other) = default;
99 /// Use llvm::Optional<Align> constructor.
100 using UP::UP;
102 explicit MaybeAlign(uint64_t Value) {
103 assert((Value == 0 || llvm::isPowerOf2_64(Value)) &&
104 "Alignment is neither 0 nor a power of 2");
105 if (Value)
106 emplace(Value);
109 /// For convenience, returns a valid alignment or 1 if undefined.
110 Align valueOrOne() const { return hasValue() ? getValue() : Align(); }
113 /// Checks that SizeInBytes is a multiple of the alignment.
114 inline bool isAligned(Align Lhs, uint64_t SizeInBytes) {
115 return SizeInBytes % Lhs.value() == 0;
118 /// Checks that SizeInBytes is a multiple of the alignment.
119 /// Returns false if the alignment is undefined.
120 inline bool isAligned(MaybeAlign Lhs, uint64_t SizeInBytes) {
121 ALIGN_CHECK_ISSET(Lhs);
122 return SizeInBytes % (*Lhs).value() == 0;
125 /// Returns a multiple of A needed to store `Size` bytes.
126 inline uint64_t alignTo(uint64_t Size, Align A) {
127 return (Size + A.value() - 1) / A.value() * A.value();
130 /// Returns a multiple of A needed to store `Size` bytes.
131 /// Returns `Size` if current alignment is undefined.
132 inline uint64_t alignTo(uint64_t Size, MaybeAlign A) {
133 return A ? alignTo(Size, A.getValue()) : Size;
136 /// Returns the log2 of the alignment.
137 inline unsigned Log2(Align A) { return A.ShiftValue; }
139 /// Returns the log2 of the alignment.
140 /// \pre A must be defined.
141 inline unsigned Log2(MaybeAlign A) {
142 ALIGN_CHECK_ISSET(A);
143 return Log2(A.getValue());
146 /// Returns the alignment that satisfies both alignments.
147 /// Same semantic as MinAlign.
148 inline Align commonAlignment(Align A, Align B) { return std::min(A, B); }
150 /// Returns the alignment that satisfies both alignments.
151 /// Same semantic as MinAlign.
152 inline Align commonAlignment(Align A, uint64_t Offset) {
153 return Align(MinAlign(A.value(), Offset));
156 /// Returns the alignment that satisfies both alignments.
157 /// Same semantic as MinAlign.
158 inline MaybeAlign commonAlignment(MaybeAlign A, MaybeAlign B) {
159 return A && B ? commonAlignment(*A, *B) : A ? A : B;
162 /// Returns the alignment that satisfies both alignments.
163 /// Same semantic as MinAlign.
164 inline MaybeAlign commonAlignment(MaybeAlign A, uint64_t Offset) {
165 return MaybeAlign(MinAlign((*A).value(), Offset));
168 /// Returns a representation of the alignment that encodes undefined as 0.
169 inline unsigned encode(MaybeAlign A) { return A ? A->ShiftValue + 1 : 0; }
171 /// Dual operation of the encode function above.
172 inline MaybeAlign decodeMaybeAlign(unsigned Value) {
173 if (Value == 0)
174 return MaybeAlign();
175 Align Out;
176 Out.ShiftValue = Value - 1;
177 return Out;
180 /// Returns a representation of the alignment, the encoded value is positive by
181 /// definition.
182 inline unsigned encode(Align A) { return encode(MaybeAlign(A)); }
184 /// Comparisons between Align and scalars. Rhs must be positive.
185 inline bool operator==(Align Lhs, uint64_t Rhs) {
186 ALIGN_CHECK_ISPOSITIVE(Rhs);
187 return Lhs.value() == Rhs;
189 inline bool operator!=(Align Lhs, uint64_t Rhs) {
190 ALIGN_CHECK_ISPOSITIVE(Rhs);
191 return Lhs.value() != Rhs;
193 inline bool operator<=(Align Lhs, uint64_t Rhs) {
194 ALIGN_CHECK_ISPOSITIVE(Rhs);
195 return Lhs.value() <= Rhs;
197 inline bool operator>=(Align Lhs, uint64_t Rhs) {
198 ALIGN_CHECK_ISPOSITIVE(Rhs);
199 return Lhs.value() >= Rhs;
201 inline bool operator<(Align Lhs, uint64_t Rhs) {
202 ALIGN_CHECK_ISPOSITIVE(Rhs);
203 return Lhs.value() < Rhs;
205 inline bool operator>(Align Lhs, uint64_t Rhs) {
206 ALIGN_CHECK_ISPOSITIVE(Rhs);
207 return Lhs.value() > Rhs;
210 /// Comparisons between MaybeAlign and scalars.
211 inline bool operator==(MaybeAlign Lhs, uint64_t Rhs) {
212 return Lhs ? (*Lhs).value() == Rhs : Rhs == 0;
214 inline bool operator!=(MaybeAlign Lhs, uint64_t Rhs) {
215 return Lhs ? (*Lhs).value() != Rhs : Rhs != 0;
217 inline bool operator<=(MaybeAlign Lhs, uint64_t Rhs) {
218 ALIGN_CHECK_ISSET(Lhs);
219 ALIGN_CHECK_ISPOSITIVE(Rhs);
220 return (*Lhs).value() <= Rhs;
222 inline bool operator>=(MaybeAlign Lhs, uint64_t Rhs) {
223 ALIGN_CHECK_ISSET(Lhs);
224 ALIGN_CHECK_ISPOSITIVE(Rhs);
225 return (*Lhs).value() >= Rhs;
227 inline bool operator<(MaybeAlign Lhs, uint64_t Rhs) {
228 ALIGN_CHECK_ISSET(Lhs);
229 ALIGN_CHECK_ISPOSITIVE(Rhs);
230 return (*Lhs).value() < Rhs;
232 inline bool operator>(MaybeAlign Lhs, uint64_t Rhs) {
233 ALIGN_CHECK_ISSET(Lhs);
234 ALIGN_CHECK_ISPOSITIVE(Rhs);
235 return (*Lhs).value() > Rhs;
238 /// Comparisons operators between Align.
239 inline bool operator==(Align Lhs, Align Rhs) {
240 return Lhs.ShiftValue == Rhs.ShiftValue;
242 inline bool operator!=(Align Lhs, Align Rhs) {
243 return Lhs.ShiftValue != Rhs.ShiftValue;
245 inline bool operator<=(Align Lhs, Align Rhs) {
246 return Lhs.ShiftValue <= Rhs.ShiftValue;
248 inline bool operator>=(Align Lhs, Align Rhs) {
249 return Lhs.ShiftValue >= Rhs.ShiftValue;
251 inline bool operator<(Align Lhs, Align Rhs) {
252 return Lhs.ShiftValue < Rhs.ShiftValue;
254 inline bool operator>(Align Lhs, Align Rhs) {
255 return Lhs.ShiftValue > Rhs.ShiftValue;
258 /// Comparisons operators between Align and MaybeAlign.
259 inline bool operator==(Align Lhs, MaybeAlign Rhs) {
260 ALIGN_CHECK_ISSET(Rhs);
261 return Lhs.value() == (*Rhs).value();
263 inline bool operator!=(Align Lhs, MaybeAlign Rhs) {
264 ALIGN_CHECK_ISSET(Rhs);
265 return Lhs.value() != (*Rhs).value();
267 inline bool operator<=(Align Lhs, MaybeAlign Rhs) {
268 ALIGN_CHECK_ISSET(Rhs);
269 return Lhs.value() <= (*Rhs).value();
271 inline bool operator>=(Align Lhs, MaybeAlign Rhs) {
272 ALIGN_CHECK_ISSET(Rhs);
273 return Lhs.value() >= (*Rhs).value();
275 inline bool operator<(Align Lhs, MaybeAlign Rhs) {
276 ALIGN_CHECK_ISSET(Rhs);
277 return Lhs.value() < (*Rhs).value();
279 inline bool operator>(Align Lhs, MaybeAlign Rhs) {
280 ALIGN_CHECK_ISSET(Rhs);
281 return Lhs.value() > (*Rhs).value();
284 /// Comparisons operators between MaybeAlign and Align.
285 inline bool operator==(MaybeAlign Lhs, Align Rhs) {
286 ALIGN_CHECK_ISSET(Lhs);
287 return Lhs && (*Lhs).value() == Rhs.value();
289 inline bool operator!=(MaybeAlign Lhs, Align Rhs) {
290 ALIGN_CHECK_ISSET(Lhs);
291 return Lhs && (*Lhs).value() != Rhs.value();
293 inline bool operator<=(MaybeAlign Lhs, Align Rhs) {
294 ALIGN_CHECK_ISSET(Lhs);
295 return Lhs && (*Lhs).value() <= Rhs.value();
297 inline bool operator>=(MaybeAlign Lhs, Align Rhs) {
298 ALIGN_CHECK_ISSET(Lhs);
299 return Lhs && (*Lhs).value() >= Rhs.value();
301 inline bool operator<(MaybeAlign Lhs, Align Rhs) {
302 ALIGN_CHECK_ISSET(Lhs);
303 return Lhs && (*Lhs).value() < Rhs.value();
305 inline bool operator>(MaybeAlign Lhs, Align Rhs) {
306 ALIGN_CHECK_ISSET(Lhs);
307 return Lhs && (*Lhs).value() > Rhs.value();
310 inline Align operator/(Align Lhs, uint64_t Divisor) {
311 assert(llvm::isPowerOf2_64(Divisor) &&
312 "Divisor must be positive and a power of 2");
313 assert(Lhs != 1 && "Can't halve byte alignment");
314 return Align(Lhs.value() / Divisor);
317 inline MaybeAlign operator/(MaybeAlign Lhs, uint64_t Divisor) {
318 assert(llvm::isPowerOf2_64(Divisor) &&
319 "Divisor must be positive and a power of 2");
320 return Lhs ? Lhs.getValue() / Divisor : MaybeAlign();
323 #undef ALIGN_CHECK_ISPOSITIVE
324 #undef ALIGN_CHECK_ISSET
326 } // namespace llvm
328 #endif // LLVM_SUPPORT_ALIGNMENT_H_