Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.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
24#include "llvm/Support/MathExtras.h"
25#include <cassert>
26#include <optional>
27#ifndef NDEBUG
28#include <string>
29#endif// NDEBUG
30
31namespacellvm {
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.
39structAlign {
40private:
41uint8_t ShiftValue = 0;/// The log2 of the required alignment.
42 /// ShiftValue is less than 64 by construction.
43
44friendstructMaybeAlign;
45friendunsignedLog2(Align);
46friendbooloperator==(Align Lhs,Align Rhs);
47friendbooloperator!=(Align Lhs,Align Rhs);
48friendbooloperator<=(Align Lhs,Align Rhs);
49friendbooloperator>=(Align Lhs,Align Rhs);
50friendbooloperator<(Align Lhs,Align Rhs);
51friendbooloperator>(Align Lhs,Align Rhs);
52friendunsignedencode(structMaybeAlignA);
53friendstructMaybeAligndecodeMaybeAlign(unsignedValue);
54
55 /// A trivial type to allow construction of constexpr Align.
56 /// This is currently needed to workaround a bug in GCC 5.3 which prevents
57 /// definition of constexpr assign operators.
58 /// https://stackoverflow.com/questions/46756288/explicitly-defaulted-function-cannot-be-declared-as-constexpr-because-the-implic
59 /// FIXME: Remove this, make all assign operators constexpr and introduce user
60 /// defined literals when we don't have to support GCC 5.3 anymore.
61 /// https://llvm.org/docs/GettingStarted.html#getting-a-modern-host-c-toolchain
62structLogValue {
63uint8_t Log;
64 };
65
66public:
67 /// Default is byte-aligned.
68constexprAlign() =default;
69 /// Do not perform checks in case of copy/move construct/assign, because the
70 /// checks have been performed when building `Other`.
71constexprAlign(constAlign &Other) =default;
72constexprAlign(Align &&Other) =default;
73Align &operator=(constAlign &Other) =default;
74Align &operator=(Align &&Other) =default;
75
76explicitAlign(uint64_tValue) {
77assert(Value > 0 &&"Value must not be 0");
78assert(llvm::isPowerOf2_64(Value) &&"Alignment is not a power of 2");
79 ShiftValue =Log2_64(Value);
80assert(ShiftValue < 64 &&"Broken invariant");
81 }
82
83 /// This is a hole in the type system and should not be abused.
84 /// Needed to interact with C for instance.
85uint64_tvalue() const{returnuint64_t(1) << ShiftValue; }
86
87// Returns the previous alignment.
88Alignprevious() const{
89assert(ShiftValue != 0 &&"Undefined operation");
90Align Out;
91 Out.ShiftValue = ShiftValue - 1;
92return Out;
93 }
94
95 /// Allow constructions of constexpr Align.
96template <size_t kValue>constexprstaticAlignConstant() {
97return LogValue{static_cast<uint8_t>(CTLog2<kValue>())};
98 }
99
100 /// Allow constructions of constexpr Align from types.
101 /// Compile time equivalent to Align(alignof(T)).
102template <typename T>constexprstaticAlignOf() {
103returnConstant<std::alignment_of_v<T>>();
104 }
105
106 /// Constexpr constructor from LogValue type.
107constexprAlign(LogValue CA) : ShiftValue(CA.Log) {}
108};
109
110/// Treats the value 0 as a 1, so Align is always at least 1.
111inlineAlignassumeAligned(uint64_tValue) {
112returnValue ?Align(Value) :Align();
113}
114
115/// This struct is a compact representation of a valid (power of two) or
116/// undefined (0) alignment.
117structMaybeAlign :public std::optional<Align> {
118private:
119usingUP = std::optional<Align>;
120
121public:
122 /// Default is undefined.
123MaybeAlign() =default;
124 /// Do not perform checks in case of copy/move construct/assign, because the
125 /// checks have been performed when building `Other`.
126MaybeAlign(constMaybeAlign &Other) =default;
127MaybeAlign &operator=(constMaybeAlign &Other) =default;
128MaybeAlign(MaybeAlign &&Other) =default;
129MaybeAlign &operator=(MaybeAlign &&Other) =default;
130
131constexprMaybeAlign(std::nullopt_tNone) : UP(None) {}
132constexprMaybeAlign(AlignValue) : UP(Value) {}
133explicitMaybeAlign(uint64_tValue) {
134assert((Value == 0 ||llvm::isPowerOf2_64(Value)) &&
135"Alignment is neither 0 nor a power of 2");
136if (Value)
137emplace(Value);
138 }
139
140 /// For convenience, returns a valid alignment or 1 if undefined.
141AlignvalueOrOne() const{return value_or(Align()); }
142};
143
144/// Checks that SizeInBytes is a multiple of the alignment.
145inlineboolisAligned(Align Lhs,uint64_t SizeInBytes) {
146return SizeInBytes % Lhs.value() == 0;
147}
148
149/// Checks that Addr is a multiple of the alignment.
150inlineboolisAddrAligned(Align Lhs,constvoid *Addr) {
151returnisAligned(Lhs,reinterpret_cast<uintptr_t>(Addr));
152}
153
154/// Returns a multiple of A needed to store `Size` bytes.
155inlineuint64_talignTo(uint64_tSize,AlignA) {
156constuint64_tValue =A.value();
157// The following line is equivalent to `(Size + Value - 1) / Value * Value`.
158
159// The division followed by a multiplication can be thought of as a right
160// shift followed by a left shift which zeros out the extra bits produced in
161// the bump; `~(Value - 1)` is a mask where all those bits being zeroed out
162// are just zero.
163
164// Most compilers can generate this code but the pattern may be missed when
165// multiple functions gets inlined.
166return (Size +Value - 1) & ~(Value - 1U);
167}
168
169/// If non-zero \p Skew is specified, the return value will be a minimal integer
170/// that is greater than or equal to \p Size and equal to \p A * N + \p Skew for
171/// some integer N. If \p Skew is larger than \p A, its value is adjusted to '\p
172/// Skew mod \p A'.
173///
174/// Examples:
175/// \code
176/// alignTo(5, Align(8), 7) = 7
177/// alignTo(17, Align(8), 1) = 17
178/// alignTo(~0LL, Align(8), 3) = 3
179/// \endcode
180inlineuint64_talignTo(uint64_tSize,AlignA,uint64_t Skew) {
181constuint64_tValue =A.value();
182 Skew %=Value;
183returnalignTo(Size - Skew,A) + Skew;
184}
185
186/// Aligns `Addr` to `Alignment` bytes, rounding up.
187inline uintptr_talignAddr(constvoid *Addr,Align Alignment) {
188 uintptr_t ArithAddr =reinterpret_cast<uintptr_t>(Addr);
189assert(static_cast<uintptr_t>(ArithAddr + Alignment.value() - 1) >=
190 ArithAddr &&
191"Overflow");
192returnalignTo(ArithAddr, Alignment);
193}
194
195/// Returns the offset to the next integer (mod 2**64) that is greater than
196/// or equal to \p Value and is a multiple of \p Align.
197inlineuint64_toffsetToAlignment(uint64_tValue,Align Alignment) {
198returnalignTo(Value, Alignment) -Value;
199}
200
201/// Returns the necessary adjustment for aligning `Addr` to `Alignment`
202/// bytes, rounding up.
203inlineuint64_toffsetToAlignedAddr(constvoid *Addr,Align Alignment) {
204returnoffsetToAlignment(reinterpret_cast<uintptr_t>(Addr), Alignment);
205}
206
207/// Returns the log2 of the alignment.
208inlineunsignedLog2(AlignA) {returnA.ShiftValue; }
209
210/// Returns the alignment that satisfies both alignments.
211/// Same semantic as MinAlign.
212inlineAligncommonAlignment(AlignA,uint64_tOffset) {
213returnAlign(MinAlign(A.value(),Offset));
214}
215
216/// Returns a representation of the alignment that encodes undefined as 0.
217inlineunsignedencode(MaybeAlignA) {returnA ?A->ShiftValue + 1 : 0; }
218
219/// Dual operation of the encode function above.
220inlineMaybeAligndecodeMaybeAlign(unsignedValue) {
221if (Value == 0)
222returnMaybeAlign();
223Align Out;
224 Out.ShiftValue =Value - 1;
225return Out;
226}
227
228/// Returns a representation of the alignment, the encoded value is positive by
229/// definition.
230inlineunsignedencode(AlignA) {returnencode(MaybeAlign(A)); }
231
232/// Comparisons between Align and scalars. Rhs must be positive.
233inlinebooloperator==(Align Lhs,uint64_t Rhs) {
234ALIGN_CHECK_ISPOSITIVE(Rhs);
235return Lhs.value() == Rhs;
236}
237inlinebooloperator!=(Align Lhs,uint64_t Rhs) {
238ALIGN_CHECK_ISPOSITIVE(Rhs);
239return Lhs.value() != Rhs;
240}
241inlinebooloperator<=(Align Lhs,uint64_t Rhs) {
242ALIGN_CHECK_ISPOSITIVE(Rhs);
243return Lhs.value() <= Rhs;
244}
245inlinebooloperator>=(Align Lhs,uint64_t Rhs) {
246ALIGN_CHECK_ISPOSITIVE(Rhs);
247return Lhs.value() >= Rhs;
248}
249inlinebooloperator<(Align Lhs,uint64_t Rhs) {
250ALIGN_CHECK_ISPOSITIVE(Rhs);
251return Lhs.value() < Rhs;
252}
253inlinebooloperator>(Align Lhs,uint64_t Rhs) {
254ALIGN_CHECK_ISPOSITIVE(Rhs);
255return Lhs.value() > Rhs;
256}
257
258/// Comparisons operators between Align.
259inlinebooloperator==(Align Lhs,Align Rhs) {
260return Lhs.ShiftValue == Rhs.ShiftValue;
261}
262inlinebooloperator!=(Align Lhs,Align Rhs) {
263return Lhs.ShiftValue != Rhs.ShiftValue;
264}
265inlinebooloperator<=(Align Lhs,Align Rhs) {
266return Lhs.ShiftValue <= Rhs.ShiftValue;
267}
268inlinebooloperator>=(Align Lhs,Align Rhs) {
269return Lhs.ShiftValue >= Rhs.ShiftValue;
270}
271inlinebooloperator<(Align Lhs,Align Rhs) {
272return Lhs.ShiftValue < Rhs.ShiftValue;
273}
274inlinebooloperator>(Align Lhs,Align Rhs) {
275return Lhs.ShiftValue > Rhs.ShiftValue;
276}
277
278// Don't allow relational comparisons with MaybeAlign.
279booloperator<=(Align Lhs,MaybeAlign Rhs) =delete;
280booloperator>=(Align Lhs,MaybeAlign Rhs) =delete;
281booloperator<(Align Lhs,MaybeAlign Rhs) =delete;
282booloperator>(Align Lhs,MaybeAlign Rhs) =delete;
283
284booloperator<=(MaybeAlign Lhs,Align Rhs) =delete;
285booloperator>=(MaybeAlign Lhs,Align Rhs) =delete;
286booloperator<(MaybeAlign Lhs,Align Rhs) =delete;
287booloperator>(MaybeAlign Lhs,Align Rhs) =delete;
288
289booloperator<=(MaybeAlign Lhs,MaybeAlign Rhs) =delete;
290booloperator>=(MaybeAlign Lhs,MaybeAlign Rhs) =delete;
291booloperator<(MaybeAlign Lhs,MaybeAlign Rhs) =delete;
292booloperator>(MaybeAlign Lhs,MaybeAlign Rhs) =delete;
293
294// Allow equality comparisons between Align and MaybeAlign.
295inlinebooloperator==(MaybeAlign Lhs,Align Rhs) {return Lhs && *Lhs == Rhs; }
296inlinebooloperator!=(MaybeAlign Lhs,Align Rhs) {return !(Lhs == Rhs); }
297inlinebooloperator==(Align Lhs,MaybeAlign Rhs) {return Rhs == Lhs; }
298inlinebooloperator!=(Align Lhs,MaybeAlign Rhs) {return !(Rhs == Lhs); }
299// Allow equality comparisons with MaybeAlign.
300inlinebooloperator==(MaybeAlign Lhs,MaybeAlign Rhs) {
301return (Lhs && Rhs && (*Lhs == *Rhs)) || (!Lhs && !Rhs);
302}
303inlinebooloperator!=(MaybeAlign Lhs,MaybeAlign Rhs) {return !(Lhs == Rhs); }
304// Allow equality comparisons with std::nullopt.
305inlinebooloperator==(MaybeAlign Lhs, std::nullopt_t) {return !bool(Lhs); }
306inlinebooloperator!=(MaybeAlign Lhs, std::nullopt_t) {returnbool(Lhs); }
307inlinebooloperator==(std::nullopt_t,MaybeAlign Rhs) {return !bool(Rhs); }
308inlinebooloperator!=(std::nullopt_t,MaybeAlign Rhs) {returnbool(Rhs); }
309
310#ifndef NDEBUG
311// For usage in LLVM_DEBUG macros.
312inline std::stringDebugStr(constAlign &A) {
313return std::to_string(A.value());
314}
315// For usage in LLVM_DEBUG macros.
316inline std::stringDebugStr(constMaybeAlign &MA) {
317if (MA)
318return std::to_string(MA->value());
319return"None";
320}
321#endif// NDEBUG
322
323#undef ALIGN_CHECK_ISPOSITIVE
324
325}// namespace llvm
326
327#endif// LLVM_SUPPORT_ALIGNMENT_H_
ALIGN_CHECK_ISPOSITIVE
#define ALIGN_CHECK_ISPOSITIVE(decl)
Definition:Alignment.h:33
A
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
emplace
static TripleVec::iterator emplace(TripleVec &Container, Triple &&T)
Definition:DylibReader.cpp:36
Addr
uint64_t Addr
Definition:ELFObjHandler.cpp:79
Size
uint64_t Size
Definition:ELFObjHandler.cpp:81
MathExtras.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool
llvm::Constant
This is an important base class in LLVM.
Definition:Constant.h:42
llvm::Value
LLVM Value Representation.
Definition:Value.h:74
uint64_t
uint8_t
unsigned
llvm::dwarf_linker::DebugSectionKind::DebugStr
@ DebugStr
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::Offset
@ Offset
Definition:DWP.cpp:480
llvm::operator<
bool operator<(int64_t V1, const APSInt &V2)
Definition:APSInt.h:361
llvm::encode
unsigned encode(MaybeAlign A)
Returns a representation of the alignment that encodes undefined as 0.
Definition:Alignment.h:217
llvm::isAligned
bool isAligned(Align Lhs, uint64_t SizeInBytes)
Checks that SizeInBytes is a multiple of the alignment.
Definition:Alignment.h:145
llvm::operator!=
bool operator!=(uint64_t V1, const APInt &V2)
Definition:APInt.h:2082
llvm::operator>=
bool operator>=(int64_t V1, const APSInt &V2)
Definition:APSInt.h:360
llvm::offsetToAlignedAddr
uint64_t offsetToAlignedAddr(const void *Addr, Align Alignment)
Returns the necessary adjustment for aligning Addr to Alignment bytes, rounding up.
Definition:Alignment.h:203
llvm::isPowerOf2_64
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition:MathExtras.h:297
llvm::operator==
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
Definition:AddressRanges.h:153
llvm::Log2_64
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:347
llvm::MinAlign
constexpr T MinAlign(U A, V B)
A and B are either alignments or offsets.
Definition:MathExtras.h:367
llvm::operator>
bool operator>(int64_t V1, const APSInt &V2)
Definition:APSInt.h:362
llvm::None
@ None
Definition:CodeGenData.h:106
llvm::offsetToAlignment
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:197
llvm::IRMemLocation::Other
@ Other
Any other memory.
llvm::decodeMaybeAlign
MaybeAlign decodeMaybeAlign(unsigned Value)
Dual operation of the encode function above.
Definition:Alignment.h:220
llvm::alignTo
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition:Alignment.h:155
llvm::commonAlignment
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition:Alignment.h:212
llvm::assumeAligned
Align assumeAligned(uint64_t Value)
Treats the value 0 as a 1, so Align is always at least 1.
Definition:Alignment.h:111
llvm::Log2
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition:Alignment.h:208
llvm::operator<=
bool operator<=(int64_t V1, const APSInt &V2)
Definition:APSInt.h:359
llvm::isAddrAligned
bool isAddrAligned(Align Lhs, const void *Addr)
Checks that Addr is a multiple of the alignment.
Definition:Alignment.h:150
llvm::alignAddr
uintptr_t alignAddr(const void *Addr, Align Alignment)
Aligns Addr to Alignment bytes, rounding up.
Definition:Alignment.h:187
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition:Alignment.h:39
llvm::Align::encode
friend unsigned encode(struct MaybeAlign A)
Returns a representation of the alignment that encodes undefined as 0.
Definition:Alignment.h:217
llvm::Align::previous
Align previous() const
Definition:Alignment.h:88
llvm::Align::operator>=
friend bool operator>=(Align Lhs, Align Rhs)
Definition:Alignment.h:268
llvm::Align::Align
constexpr Align(LogValue CA)
Constexpr constructor from LogValue type.
Definition:Alignment.h:107
llvm::Align::Of
static constexpr Align Of()
Allow constructions of constexpr Align from types.
Definition:Alignment.h:102
llvm::Align::decodeMaybeAlign
friend struct MaybeAlign decodeMaybeAlign(unsigned Value)
Dual operation of the encode function above.
Definition:Alignment.h:220
llvm::Align::Align
Align(uint64_t Value)
Definition:Alignment.h:76
llvm::Align::operator=
Align & operator=(Align &&Other)=default
llvm::Align::value
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition:Alignment.h:85
llvm::Align::Align
constexpr Align(Align &&Other)=default
llvm::Align::operator<
friend bool operator<(Align Lhs, Align Rhs)
Definition:Alignment.h:271
llvm::Align::Constant
static constexpr Align Constant()
Allow constructions of constexpr Align.
Definition:Alignment.h:96
llvm::Align::operator=
Align & operator=(const Align &Other)=default
llvm::Align::operator==
friend bool operator==(Align Lhs, Align Rhs)
Comparisons operators between Align.
Definition:Alignment.h:259
llvm::Align::operator<=
friend bool operator<=(Align Lhs, Align Rhs)
Definition:Alignment.h:265
llvm::Align::Align
constexpr Align()=default
Default is byte-aligned.
llvm::Align::operator!=
friend bool operator!=(Align Lhs, Align Rhs)
Definition:Alignment.h:262
llvm::Align::Log2
friend unsigned Log2(Align)
Returns the log2 of the alignment.
Definition:Alignment.h:208
llvm::Align::Align
constexpr Align(const Align &Other)=default
Do not perform checks in case of copy/move construct/assign, because the checks have been performed w...
llvm::Align::operator>
friend bool operator>(Align Lhs, Align Rhs)
Definition:Alignment.h:274
llvm::MaybeAlign
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition:Alignment.h:117
llvm::MaybeAlign::valueOrOne
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Definition:Alignment.h:141
llvm::MaybeAlign::MaybeAlign
MaybeAlign(MaybeAlign &&Other)=default
llvm::MaybeAlign::MaybeAlign
constexpr MaybeAlign(Align Value)
Definition:Alignment.h:132
llvm::MaybeAlign::MaybeAlign
constexpr MaybeAlign(std::nullopt_t None)
Definition:Alignment.h:131
llvm::MaybeAlign::MaybeAlign
MaybeAlign(const MaybeAlign &Other)=default
Do not perform checks in case of copy/move construct/assign, because the checks have been performed w...
llvm::MaybeAlign::MaybeAlign
MaybeAlign(uint64_t Value)
Definition:Alignment.h:133
llvm::MaybeAlign::MaybeAlign
MaybeAlign()=default
Default is undefined.
llvm::MaybeAlign::operator=
MaybeAlign & operator=(MaybeAlign &&Other)=default
llvm::MaybeAlign::operator=
MaybeAlign & operator=(const MaybeAlign &Other)=default

Generated on Thu Jul 17 2025 10:13:11 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp