LLVM 24.0.0git
Alignment.h
Go to the documentation of this file.
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.
12//
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.
15//
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 value() method.
18//
19//===----------------------------------------------------------------------===//
20
21#ifndef LLVM_SUPPORT_ALIGNMENT_H_
22#define LLVM_SUPPORT_ALIGNMENT_H_
23
25#include <cassert>
26#include <optional>
27#ifndef NDEBUG
28#include <string>
29#endif // NDEBUG
30
31namespace llvm {
32
33#define ALIGN_CHECK_ISPOSITIVE(decl) \
34 assert(decl > 0 && (#decl " should be defined"))
35
36/// This struct is a compact representation of a valid (non-zero power of two)
37/// alignment.
38/// It is suitable for use as static global constants.
39struct Align {
40private:
41 uint8_t ShiftValue = 0; /// The log2 of the required alignment.
42 /// ShiftValue is less than 64 by construction.
43
44 friend struct MaybeAlign;
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);
52 friend unsigned encode(struct MaybeAlign A);
54
55public:
56 /// Default is byte-aligned.
57 constexpr Align() = default;
58 /// Do not perform checks in case of copy/move construct/assign, because the
59 /// checks have been performed when building `Other`.
60 constexpr Align(const Align &Other) = default;
61 constexpr Align(Align &&Other) = default;
62 constexpr Align &operator=(const Align &Other) = default;
63 constexpr Align &operator=(Align &&Other) = default;
64
65 explicit Align(uint64_t Value) {
66 assert(Value > 0 && "Value must not be 0");
67 assert(llvm::isPowerOf2_64(Value) && "Alignment is not a power of 2");
68 ShiftValue = Log2_64(Value);
69 assert(ShiftValue < 64 && "Broken invariant");
70 }
71
72 /// Returns an alignment of `1 << Log2Value` bytes.
73 constexpr static Align fromLog2(unsigned Log2Value) {
74 assert(Log2Value < 64 && "Alignment exponent is too large");
75 Align Out;
76 Out.ShiftValue = Log2Value;
77 return Out;
78 }
79
80 /// This is a hole in the type system and should not be abused.
81 /// Needed to interact with C for instance.
82 constexpr uint64_t value() const { return uint64_t(1) << ShiftValue; }
83
84 // Returns the previous alignment.
85 Align previous() const {
86 assert(ShiftValue != 0 && "Undefined operation");
87 Align Out;
88 Out.ShiftValue = ShiftValue - 1;
89 return Out;
90 }
91
92 /// Allow constructions of constexpr Align.
93 template <size_t kValue> constexpr static Align Constant() {
95 }
96
97 /// Allow constructions of constexpr Align from types.
98 /// Compile time equivalent to Align(alignof(T)).
99 template <typename T> constexpr static Align Of() {
101 }
102};
103
104/// Treats the value 0 as a 1, so Align is always at least 1.
106 return Value ? Align(Value) : Align();
107}
108
109/// This struct is a compact representation of a valid (power of two) or
110/// undefined (0) alignment.
111struct MaybeAlign : std::optional<Align> {
112private:
113 using UP = std::optional<Align>;
114
115public:
116 /// Default is undefined.
117 MaybeAlign() = default;
118 /// Do not perform checks in case of copy/move construct/assign, because the
119 /// checks have been performed when building `Other`.
120 MaybeAlign(const MaybeAlign &Other) = default;
124
125 constexpr MaybeAlign(std::nullopt_t None) : UP(None) {}
126 constexpr MaybeAlign(Align Value) : UP(Value) {}
129 "Alignment is neither 0 nor a power of 2");
130 if (Value)
131 emplace(Value);
132 }
133
134 /// For convenience, returns a valid alignment or 1 if undefined.
135 Align valueOrOne() const { return value_or(Align()); }
136};
137
138/// Checks that SizeInBytes is a multiple of the alignment.
139inline bool isAligned(Align Lhs, uint64_t SizeInBytes) {
140 return SizeInBytes % Lhs.value() == 0;
141}
142
143/// Checks that Addr is a multiple of the alignment.
144inline bool isAddrAligned(Align Lhs, const void *Addr) {
145 return isAligned(Lhs, reinterpret_cast<uintptr_t>(Addr));
146}
147
148/// Returns a multiple of A needed to store `Size` bytes.
149constexpr inline uint64_t alignTo(uint64_t Size, Align A) {
150 const uint64_t Value = A.value();
151 // The following line is equivalent to `(Size + Value - 1) / Value * Value`.
152
153 // The division followed by a multiplication can be thought of as a right
154 // shift followed by a left shift which zeros out the extra bits produced in
155 // the bump; `~(Value - 1)` is a mask where all those bits being zeroed out
156 // are just zero.
157
158 // Most compilers can generate this code but the pattern may be missed when
159 // multiple functions gets inlined.
160 return (Size + Value - 1) & ~(Value - 1U);
161}
162
163/// If non-zero \p Skew is specified, the return value will be a minimal integer
164/// that is greater than or equal to \p Size and equal to \p A * N + \p Skew for
165/// some integer N. If \p Skew is larger than \p A, its value is adjusted to '\p
166/// Skew mod \p A'.
167///
168/// Examples:
169/// \code
170/// alignTo(5, Align(8), 7) = 7
171/// alignTo(17, Align(8), 1) = 17
172/// alignTo(~0LL, Align(8), 3) = 3
173/// \endcode
175 const uint64_t Value = A.value();
176 Skew %= Value;
177 return alignTo(Size - Skew, A) + Skew;
178}
179
180/// Aligns `Addr` to `Alignment` bytes, rounding up.
181inline uintptr_t alignAddr(const void *Addr, Align Alignment) {
182 uintptr_t ArithAddr = reinterpret_cast<uintptr_t>(Addr);
183 assert(static_cast<uintptr_t>(ArithAddr + Alignment.value() - 1) >=
184 ArithAddr &&
185 "Overflow");
186 return alignTo(ArithAddr, Alignment);
187}
188
189/// Returns the offset to the next integer (mod 2**64) that is greater than
190/// or equal to \p Value and is a multiple of \p Align.
192 return alignTo(Value, Alignment) - Value;
193}
194
195/// Returns the necessary adjustment for aligning `Addr` to `Alignment`
196/// bytes, rounding up.
197inline uint64_t offsetToAlignedAddr(const void *Addr, Align Alignment) {
198 return offsetToAlignment(reinterpret_cast<uintptr_t>(Addr), Alignment);
199}
200
201/// Returns the log2 of the alignment.
202inline unsigned Log2(Align A) { return A.ShiftValue; }
203
204/// Returns the alignment that satisfies both alignments.
205/// Same semantic as MinAlign.
207 return Align(MinAlign(A.value(), Offset));
208}
209
210/// Returns a representation of the alignment that encodes undefined as 0.
211inline unsigned encode(MaybeAlign A) { return A ? A->ShiftValue + 1 : 0; }
212
213/// Dual operation of the encode function above.
215 if (Value == 0)
216 return MaybeAlign();
217 Align Out;
218 Out.ShiftValue = Value - 1;
219 return Out;
220}
221
222/// Returns a representation of the alignment, the encoded value is positive by
223/// definition.
224inline unsigned encode(Align A) { return encode(MaybeAlign(A)); }
225
226/// Comparisons between Align and scalars. Rhs must be positive.
227inline bool operator==(Align Lhs, uint64_t Rhs) {
229 return Lhs.value() == Rhs;
230}
231inline bool operator!=(Align Lhs, uint64_t Rhs) {
233 return Lhs.value() != Rhs;
234}
235inline bool operator<=(Align Lhs, uint64_t Rhs) {
237 return Lhs.value() <= Rhs;
238}
239inline bool operator>=(Align Lhs, uint64_t Rhs) {
241 return Lhs.value() >= Rhs;
242}
243inline bool operator<(Align Lhs, uint64_t Rhs) {
245 return Lhs.value() < Rhs;
246}
247inline bool operator>(Align Lhs, uint64_t Rhs) {
249 return Lhs.value() > Rhs;
250}
251
252/// Comparisons operators between Align.
253inline bool operator==(Align Lhs, Align Rhs) {
254 return Lhs.ShiftValue == Rhs.ShiftValue;
255}
256inline bool operator!=(Align Lhs, Align Rhs) {
257 return Lhs.ShiftValue != Rhs.ShiftValue;
258}
259inline bool operator<=(Align Lhs, Align Rhs) {
260 return Lhs.ShiftValue <= Rhs.ShiftValue;
261}
262inline bool operator>=(Align Lhs, Align Rhs) {
263 return Lhs.ShiftValue >= Rhs.ShiftValue;
264}
265inline bool operator<(Align Lhs, Align Rhs) {
266 return Lhs.ShiftValue < Rhs.ShiftValue;
267}
268inline bool operator>(Align Lhs, Align Rhs) {
269 return Lhs.ShiftValue > Rhs.ShiftValue;
270}
271
272// Don't allow relational comparisons with MaybeAlign.
273bool operator<=(Align Lhs, MaybeAlign Rhs) = delete;
274bool operator>=(Align Lhs, MaybeAlign Rhs) = delete;
275bool operator<(Align Lhs, MaybeAlign Rhs) = delete;
276bool operator>(Align Lhs, MaybeAlign Rhs) = delete;
277
278bool operator<=(MaybeAlign Lhs, Align Rhs) = delete;
279bool operator>=(MaybeAlign Lhs, Align Rhs) = delete;
280bool operator<(MaybeAlign Lhs, Align Rhs) = delete;
281bool operator>(MaybeAlign Lhs, Align Rhs) = delete;
282
283bool operator<=(MaybeAlign Lhs, MaybeAlign Rhs) = delete;
284bool operator>=(MaybeAlign Lhs, MaybeAlign Rhs) = delete;
285bool operator<(MaybeAlign Lhs, MaybeAlign Rhs) = delete;
286bool operator>(MaybeAlign Lhs, MaybeAlign Rhs) = delete;
287
288// Allow equality comparisons between Align and MaybeAlign.
289inline bool operator==(MaybeAlign Lhs, Align Rhs) { return Lhs && *Lhs == Rhs; }
290inline bool operator!=(MaybeAlign Lhs, Align Rhs) { return !(Lhs == Rhs); }
291inline bool operator==(Align Lhs, MaybeAlign Rhs) { return Rhs == Lhs; }
292inline bool operator!=(Align Lhs, MaybeAlign Rhs) { return !(Rhs == Lhs); }
293// Allow equality comparisons with MaybeAlign.
294inline bool operator==(MaybeAlign Lhs, MaybeAlign Rhs) {
295 return (Lhs && Rhs && (*Lhs == *Rhs)) || (!Lhs && !Rhs);
296}
297inline bool operator!=(MaybeAlign Lhs, MaybeAlign Rhs) { return !(Lhs == Rhs); }
298// Allow equality comparisons with std::nullopt.
299inline bool operator==(MaybeAlign Lhs, std::nullopt_t) { return !bool(Lhs); }
300inline bool operator!=(MaybeAlign Lhs, std::nullopt_t) { return bool(Lhs); }
301inline bool operator==(std::nullopt_t, MaybeAlign Rhs) { return !bool(Rhs); }
302inline bool operator!=(std::nullopt_t, MaybeAlign Rhs) { return bool(Rhs); }
303
304#ifndef NDEBUG
305// For usage in LLVM_DEBUG macros.
306inline std::string DebugStr(const Align &A) {
307 return std::to_string(A.value());
308}
309// For usage in LLVM_DEBUG macros.
310inline std::string DebugStr(const MaybeAlign &MA) {
311 if (MA)
312 return std::to_string(MA->value());
313 return "None";
314}
315#endif // NDEBUG
316
317#undef ALIGN_CHECK_ISPOSITIVE
318
319} // namespace llvm
320
321#endif // LLVM_SUPPORT_ALIGNMENT_H_
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
#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.
Definition Value.h:75
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
bool operator<(int64_t V1, const APSInt &V2)
Definition APSInt.h:360
unsigned encode(MaybeAlign A)
Returns a representation of the alignment that encodes undefined as 0.
Definition Alignment.h:211
bool isAligned(Align Lhs, uint64_t SizeInBytes)
Checks that SizeInBytes is a multiple of the alignment.
Definition Alignment.h:139
bool operator!=(uint64_t V1, const APInt &V2)
Definition APInt.h:2139
bool operator>=(int64_t V1, const APSInt &V2)
Definition APSInt.h:359
constexpr size_t ConstantLog2()
Compile time Log2.
Definition MathExtras.h:318
uint64_t offsetToAlignedAddr(const void *Addr, Align Alignment)
Returns the necessary adjustment for aligning Addr to Alignment bytes, rounding up.
Definition Alignment.h:197
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:285
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.
Definition MathExtras.h:332
constexpr T MinAlign(U A, V B)
A and B are either alignments or offsets.
Definition MathExtras.h:352
bool operator>(int64_t V1, const APSInt &V2)
Definition APSInt.h:361
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:149
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:191
@ Other
Any other memory.
Definition ModRef.h:68
MaybeAlign decodeMaybeAlign(unsigned Value)
Dual operation of the encode function above.
Definition Alignment.h:214
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:206
Align assumeAligned(uint64_t Value)
Treats the value 0 as a 1, so Align is always at least 1.
Definition Alignment.h:105
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:202
bool operator<=(int64_t V1, const APSInt &V2)
Definition APSInt.h:358
bool isAddrAligned(Align Lhs, const void *Addr)
Checks that Addr is a multiple of the alignment.
Definition Alignment.h:144
uintptr_t alignAddr(const void *Addr, Align Alignment)
Aligns Addr to Alignment bytes, rounding up.
Definition Alignment.h:181
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:211
Align previous() const
Definition Alignment.h:85
constexpr Align & operator=(const Align &Other)=default
friend bool operator>=(Align Lhs, Align Rhs)
Definition Alignment.h:262
static constexpr Align Of()
Allow constructions of constexpr Align from types.
Definition Alignment.h:99
friend struct MaybeAlign decodeMaybeAlign(unsigned Value)
Dual operation of the encode function above.
Definition Alignment.h:214
Align(uint64_t Value)
Definition Alignment.h:65
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:82
friend bool operator<(Align Lhs, Align Rhs)
Definition Alignment.h:265
static constexpr Align Constant()
Allow constructions of constexpr Align.
Definition Alignment.h:93
static constexpr Align fromLog2(unsigned Log2Value)
Returns an alignment of 1 << Log2Value bytes.
Definition Alignment.h:73
friend bool operator==(Align Lhs, Align Rhs)
Comparisons operators between Align.
Definition Alignment.h:253
friend bool operator<=(Align Lhs, Align Rhs)
Definition Alignment.h:259
friend unsigned Log2(Align)
Returns the log2 of the alignment.
Definition Alignment.h:202
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:256
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:268
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:111
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Definition Alignment.h:135
MaybeAlign(MaybeAlign &&Other)=default
constexpr MaybeAlign(Align Value)
Definition Alignment.h:126
constexpr MaybeAlign(std::nullopt_t None)
Definition Alignment.h:125
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:127
MaybeAlign()=default
Default is undefined.
MaybeAlign & operator=(MaybeAlign &&Other)=default
MaybeAlign & operator=(const MaybeAlign &Other)=default