LLVM  10.0.0svn
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 getValue() method.
18 //
19 //===----------------------------------------------------------------------===//
20 
21 #ifndef LLVM_SUPPORT_ALIGNMENT_H_
22 #define LLVM_SUPPORT_ALIGNMENT_H_
23 
24 #include "llvm/ADT/Optional.h"
25 #include "llvm/Support/Compiler.h"
27 #include <cassert>
28 #include <limits>
29 
30 namespace llvm {
31 
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"))
36 
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.
44 
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);
55 
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;
65 
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");
71  }
72 
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; }
76 };
77 
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();
81 }
82 
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>;
88 
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;
98 
99  /// Use llvm::Optional<Align> constructor.
100  using UP::UP;
101 
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);
107  }
108 
109  /// For convenience, returns a valid alignment or 1 if undefined.
110  Align valueOrOne() const { return hasValue() ? getValue() : Align(); }
111 };
112 
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;
116 }
117 
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;
123 }
124 
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();
128 }
129 
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;
134 }
135 
136 /// Returns the offset to the next integer (mod 2**64) that is greater than
137 /// or equal to \p Value and is a multiple of \p Align.
138 inline uint64_t offsetToAlignment(uint64_t Value, llvm::Align Align) {
139  return alignTo(Value, Align) - Value;
140 }
141 
142 /// Returns the log2 of the alignment.
143 inline unsigned Log2(Align A) { return A.ShiftValue; }
144 
145 /// Returns the log2 of the alignment.
146 /// \pre A must be defined.
147 inline unsigned Log2(MaybeAlign A) {
149  return Log2(A.getValue());
150 }
151 
152 /// Returns the alignment that satisfies both alignments.
153 /// Same semantic as MinAlign.
154 inline Align commonAlignment(Align A, Align B) { return std::min(A, B); }
155 
156 /// Returns the alignment that satisfies both alignments.
157 /// Same semantic as MinAlign.
158 inline Align commonAlignment(Align A, uint64_t Offset) {
159  return Align(MinAlign(A.value(), Offset));
160 }
161 
162 /// Returns the alignment that satisfies both alignments.
163 /// Same semantic as MinAlign.
165  return A && B ? commonAlignment(*A, *B) : A ? A : B;
166 }
167 
168 /// Returns the alignment that satisfies both alignments.
169 /// Same semantic as MinAlign.
171  return MaybeAlign(MinAlign((*A).value(), Offset));
172 }
173 
174 /// Returns a representation of the alignment that encodes undefined as 0.
175 inline unsigned encode(MaybeAlign A) { return A ? A->ShiftValue + 1 : 0; }
176 
177 /// Dual operation of the encode function above.
179  if (Value == 0)
180  return MaybeAlign();
181  Align Out;
182  Out.ShiftValue = Value - 1;
183  return Out;
184 }
185 
186 /// Returns a representation of the alignment, the encoded value is positive by
187 /// definition.
188 inline unsigned encode(Align A) { return encode(MaybeAlign(A)); }
189 
190 /// Comparisons between Align and scalars. Rhs must be positive.
191 inline bool operator==(Align Lhs, uint64_t Rhs) {
193  return Lhs.value() == Rhs;
194 }
195 inline bool operator!=(Align Lhs, uint64_t Rhs) {
197  return Lhs.value() != Rhs;
198 }
199 inline bool operator<=(Align Lhs, uint64_t Rhs) {
201  return Lhs.value() <= Rhs;
202 }
203 inline bool operator>=(Align Lhs, uint64_t Rhs) {
205  return Lhs.value() >= Rhs;
206 }
207 inline bool operator<(Align Lhs, uint64_t Rhs) {
209  return Lhs.value() < Rhs;
210 }
211 inline bool operator>(Align Lhs, uint64_t Rhs) {
213  return Lhs.value() > Rhs;
214 }
215 
216 /// Comparisons between MaybeAlign and scalars.
217 inline bool operator==(MaybeAlign Lhs, uint64_t Rhs) {
218  return Lhs ? (*Lhs).value() == Rhs : Rhs == 0;
219 }
220 inline bool operator!=(MaybeAlign Lhs, uint64_t Rhs) {
221  return Lhs ? (*Lhs).value() != Rhs : Rhs != 0;
222 }
223 inline bool operator<=(MaybeAlign Lhs, uint64_t Rhs) {
224  ALIGN_CHECK_ISSET(Lhs);
226  return (*Lhs).value() <= Rhs;
227 }
228 inline bool operator>=(MaybeAlign Lhs, uint64_t Rhs) {
229  ALIGN_CHECK_ISSET(Lhs);
231  return (*Lhs).value() >= Rhs;
232 }
233 inline bool operator<(MaybeAlign Lhs, uint64_t Rhs) {
234  ALIGN_CHECK_ISSET(Lhs);
236  return (*Lhs).value() < Rhs;
237 }
238 inline bool operator>(MaybeAlign Lhs, uint64_t Rhs) {
239  ALIGN_CHECK_ISSET(Lhs);
241  return (*Lhs).value() > Rhs;
242 }
243 
244 /// Comparisons operators between Align.
245 inline bool operator==(Align Lhs, Align Rhs) {
246  return Lhs.ShiftValue == Rhs.ShiftValue;
247 }
248 inline bool operator!=(Align Lhs, Align Rhs) {
249  return Lhs.ShiftValue != Rhs.ShiftValue;
250 }
251 inline bool operator<=(Align Lhs, Align Rhs) {
252  return Lhs.ShiftValue <= Rhs.ShiftValue;
253 }
254 inline bool operator>=(Align Lhs, Align Rhs) {
255  return Lhs.ShiftValue >= Rhs.ShiftValue;
256 }
257 inline bool operator<(Align Lhs, Align Rhs) {
258  return Lhs.ShiftValue < Rhs.ShiftValue;
259 }
260 inline bool operator>(Align Lhs, Align Rhs) {
261  return Lhs.ShiftValue > Rhs.ShiftValue;
262 }
263 
264 /// Comparisons operators between Align and MaybeAlign.
265 inline bool operator==(Align Lhs, MaybeAlign Rhs) {
266  ALIGN_CHECK_ISSET(Rhs);
267  return Lhs.value() == (*Rhs).value();
268 }
269 inline bool operator!=(Align Lhs, MaybeAlign Rhs) {
270  ALIGN_CHECK_ISSET(Rhs);
271  return Lhs.value() != (*Rhs).value();
272 }
273 inline bool operator<=(Align Lhs, MaybeAlign Rhs) {
274  ALIGN_CHECK_ISSET(Rhs);
275  return Lhs.value() <= (*Rhs).value();
276 }
277 inline bool operator>=(Align Lhs, MaybeAlign Rhs) {
278  ALIGN_CHECK_ISSET(Rhs);
279  return Lhs.value() >= (*Rhs).value();
280 }
281 inline bool operator<(Align Lhs, MaybeAlign Rhs) {
282  ALIGN_CHECK_ISSET(Rhs);
283  return Lhs.value() < (*Rhs).value();
284 }
285 inline bool operator>(Align Lhs, MaybeAlign Rhs) {
286  ALIGN_CHECK_ISSET(Rhs);
287  return Lhs.value() > (*Rhs).value();
288 }
289 
290 /// Comparisons operators between MaybeAlign and Align.
291 inline bool operator==(MaybeAlign Lhs, Align Rhs) {
292  ALIGN_CHECK_ISSET(Lhs);
293  return Lhs && (*Lhs).value() == Rhs.value();
294 }
295 inline bool operator!=(MaybeAlign Lhs, Align Rhs) {
296  ALIGN_CHECK_ISSET(Lhs);
297  return Lhs && (*Lhs).value() != Rhs.value();
298 }
299 inline bool operator<=(MaybeAlign Lhs, Align Rhs) {
300  ALIGN_CHECK_ISSET(Lhs);
301  return Lhs && (*Lhs).value() <= Rhs.value();
302 }
303 inline bool operator>=(MaybeAlign Lhs, Align Rhs) {
304  ALIGN_CHECK_ISSET(Lhs);
305  return Lhs && (*Lhs).value() >= Rhs.value();
306 }
307 inline bool operator<(MaybeAlign Lhs, Align Rhs) {
308  ALIGN_CHECK_ISSET(Lhs);
309  return Lhs && (*Lhs).value() < Rhs.value();
310 }
311 inline bool operator>(MaybeAlign Lhs, Align Rhs) {
312  ALIGN_CHECK_ISSET(Lhs);
313  return Lhs && (*Lhs).value() > Rhs.value();
314 }
315 
316 inline Align operator/(Align Lhs, uint64_t Divisor) {
317  assert(llvm::isPowerOf2_64(Divisor) &&
318  "Divisor must be positive and a power of 2");
319  assert(Lhs != 1 && "Can't halve byte alignment");
320  return Align(Lhs.value() / Divisor);
321 }
322 
323 inline MaybeAlign operator/(MaybeAlign Lhs, uint64_t Divisor) {
324  assert(llvm::isPowerOf2_64(Divisor) &&
325  "Divisor must be positive and a power of 2");
326  return Lhs ? Lhs.getValue() / Divisor : MaybeAlign();
327 }
328 
329 #undef ALIGN_CHECK_ISPOSITIVE
330 #undef ALIGN_CHECK_ISSET
331 
332 } // namespace llvm
333 
334 #endif // LLVM_SUPPORT_ALIGNMENT_H_
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.
Definition: Alignment.h:110
This class represents lattice values for constants.
Definition: AllocatorList.h:23
friend bool operator==(Align Lhs, Align Rhs)
Comparisons operators between Align.
Definition: Alignment.h:245
Align operator/(Align Lhs, uint64_t Divisor)
Definition: Alignment.h:316
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:948
friend bool operator<=(Align Lhs, Align Rhs)
Definition: Alignment.h:251
friend struct MaybeAlign decodeMaybeAlign(unsigned Value)
Dual operation of the encode function above.
Definition: Alignment.h:178
Align()=default
Default is byte-aligned.
Align commonAlignment(Align A, Align B)
Returns the alignment that satisfies both alignments.
Definition: Alignment.h:154
const T & getValue() const LLVM_LVALUE_FUNCTION
Definition: Optional.h:255
friend bool operator>(Align Lhs, Align Rhs)
Definition: Alignment.h:260
friend struct MaybeAlign
The log2 of the required alignment.
Definition: Alignment.h:45
uint64_t value() const
This is a hole in the type system and should not be abused.
Definition: Alignment.h:75
#define ALIGN_CHECK_ISSET(decl)
Definition: Alignment.h:34
uint64_t offsetToAlignment(uint64_t Value, llvm::Align Align)
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:138
Align(uint64_t Value)
Definition: Alignment.h:66
constexpr uint64_t MinAlign(uint64_t A, uint64_t B)
A and B are either alignments or offsets.
Definition: MathExtras.h:614
friend unsigned encode(struct MaybeAlign A)
Returns a representation of the alignment that encodes undefined as 0.
Definition: Alignment.h:175
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition: MathExtras.h:433
friend bool operator>=(Align Lhs, Align Rhs)
Definition: Alignment.h:254
friend bool operator<(Align Lhs, Align Rhs)
Definition: Alignment.h:257
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:40
This struct is a compact representation of a valid (power of two) or undefined (0) alignment...
Definition: Alignment.h:85
Align & operator=(const Align &Other)=default
bool isAligned(Align Lhs, uint64_t SizeInBytes)
Checks that SizeInBytes is a multiple of the alignment.
Definition: Alignment.h:114
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:126
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
uint32_t Size
Definition: Profile.cpp:46
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
friend unsigned Log2(Align)
Returns the log2 of the alignment.
Definition: Alignment.h:143
friend bool operator!=(Align Lhs, Align Rhs)
Definition: Alignment.h:248
#define ALIGN_CHECK_ISPOSITIVE(decl)
Definition: Alignment.h:32
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:544
MaybeAlign(uint64_t Value)
Definition: Alignment.h:102
Align assumeAligned(uint64_t Value)
Treats the value 0 as a 1, so Align is always at least 1.
Definition: Alignment.h:79