Thanks to visit codestin.com
Credit goes to llvm.org

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