1
- // Copied from https://github.com/llvm/llvm-project/blob/8ef3e895ad8ab1724e2b87cabad1dacdc7a397a3 /llvm/include/llvm/Support/Alignment.h
1
+ // Copied from https://github.com/llvm/llvm-project/blob/3d3ef9d073e1e27ea57480b371b7f5a9f5642ed2 /llvm/include/llvm/Support/Alignment.h
2
2
3
3
// ===-- llvm/Support/Alignment.h - Useful alignment functions ---*- C++ -*-===//
4
4
//
@@ -86,6 +86,14 @@ struct Align {
86
86
// / Needed to interact with C for instance.
87
87
uint64_t value () const { return uint64_t (1 ) << ShiftValue; }
88
88
89
+ // Returns the previous alignment.
90
+ Align previous () const {
91
+ assert (ShiftValue != 0 && " Undefined operation" );
92
+ Align Out;
93
+ Out.ShiftValue = ShiftValue - 1 ;
94
+ return Out;
95
+ }
96
+
89
97
// / Allow constructions of constexpr Align.
90
98
template <size_t kValue > constexpr static LogValue Constant () {
91
99
return LogValue{static_cast <uint8_t >(CTLog2<kValue >())};
@@ -133,7 +141,7 @@ struct MaybeAlign : public llvm::Optional<Align> {
133
141
}
134
142
135
143
// / For convenience, returns a valid alignment or 1 if undefined.
136
- Align valueOrOne () const { return hasValue () ? getValue () : Align (); }
144
+ Align valueOrOne () const { return value_or ( Align () ); }
137
145
};
138
146
139
147
// / Checks that SizeInBytes is a multiple of the alignment.
@@ -175,13 +183,7 @@ inline uint64_t alignTo(uint64_t Size, Align A) {
175
183
inline uint64_t alignTo (uint64_t Size, Align A, uint64_t Skew) {
176
184
const uint64_t Value = A.value ();
177
185
Skew %= Value;
178
- return ((Size + Value - 1 - Skew) & ~(Value - 1U )) + Skew;
179
- }
180
-
181
- // / Returns a multiple of A needed to store `Size` bytes.
182
- // / Returns `Size` if current alignment is undefined.
183
- inline uint64_t alignTo (uint64_t Size, MaybeAlign A) {
184
- return A ? alignTo (Size, A.getValue ()) : Size;
186
+ return alignTo (Size - Skew, A) + Skew;
185
187
}
186
188
187
189
// / Aligns `Addr` to `Alignment` bytes, rounding up.
@@ -208,28 +210,12 @@ inline uint64_t offsetToAlignedAddr(const void *Addr, Align Alignment) {
208
210
// / Returns the log2 of the alignment.
209
211
inline unsigned Log2 (Align A) { return A.ShiftValue ; }
210
212
211
- // / Returns the alignment that satisfies both alignments.
212
- // / Same semantic as MinAlign.
213
- inline Align commonAlignment (Align A, Align B) { return std::min (A, B); }
214
-
215
213
// / Returns the alignment that satisfies both alignments.
216
214
// / Same semantic as MinAlign.
217
215
inline Align commonAlignment (Align A, uint64_t Offset) {
218
216
return Align (MinAlign (A.value (), Offset));
219
217
}
220
218
221
- // / Returns the alignment that satisfies both alignments.
222
- // / Same semantic as MinAlign.
223
- inline MaybeAlign commonAlignment (MaybeAlign A, MaybeAlign B) {
224
- return A && B ? commonAlignment (*A, *B) : A ? A : B;
225
- }
226
-
227
- // / Returns the alignment that satisfies both alignments.
228
- // / Same semantic as MinAlign.
229
- inline MaybeAlign commonAlignment (MaybeAlign A, uint64_t Offset) {
230
- return MaybeAlign (MinAlign ((*A).value (), Offset));
231
- }
232
-
233
219
// / Returns a representation of the alignment that encodes undefined as 0.
234
220
inline unsigned encode (MaybeAlign A) { return A ? A->ShiftValue + 1 : 0 ; }
235
221
@@ -272,14 +258,6 @@ inline bool operator>(Align Lhs, uint64_t Rhs) {
272
258
return Lhs.value () > Rhs;
273
259
}
274
260
275
- // / Comparisons between MaybeAlign and scalars.
276
- inline bool operator ==(MaybeAlign Lhs, uint64_t Rhs) {
277
- return Lhs ? (*Lhs).value () == Rhs : Rhs == 0 ;
278
- }
279
- inline bool operator !=(MaybeAlign Lhs, uint64_t Rhs) {
280
- return Lhs ? (*Lhs).value () != Rhs : Rhs != 0 ;
281
- }
282
-
283
261
// / Comparisons operators between Align.
284
262
inline bool operator ==(Align Lhs, Align Rhs) {
285
263
return Lhs.ShiftValue == Rhs.ShiftValue ;
@@ -316,37 +294,6 @@ bool operator>=(MaybeAlign Lhs, MaybeAlign Rhs) = delete;
316
294
bool operator <(MaybeAlign Lhs, MaybeAlign Rhs) = delete ;
317
295
bool operator >(MaybeAlign Lhs, MaybeAlign Rhs) = delete ;
318
296
319
- inline Align operator *(Align Lhs, uint64_t Rhs) {
320
- assert (Rhs > 0 && " Rhs must be positive" );
321
- return Align (Lhs.value () * Rhs);
322
- }
323
-
324
- inline MaybeAlign operator *(MaybeAlign Lhs, uint64_t Rhs) {
325
- assert (Rhs > 0 && " Rhs must be positive" );
326
- return Lhs ? Lhs.getValue () * Rhs : MaybeAlign ();
327
- }
328
-
329
- inline Align operator /(Align Lhs, uint64_t Divisor) {
330
- assert (llvm::isPowerOf2_64 (Divisor) &&
331
- " Divisor must be positive and a power of 2" );
332
- assert (Lhs != 1 && " Can't halve byte alignment" );
333
- return Align (Lhs.value () / Divisor);
334
- }
335
-
336
- inline MaybeAlign operator /(MaybeAlign Lhs, uint64_t Divisor) {
337
- assert (llvm::isPowerOf2_64 (Divisor) &&
338
- " Divisor must be positive and a power of 2" );
339
- return Lhs ? Lhs.getValue () / Divisor : MaybeAlign ();
340
- }
341
-
342
- inline Align max (MaybeAlign Lhs, Align Rhs) {
343
- return Lhs && *Lhs > Rhs ? *Lhs : Rhs;
344
- }
345
-
346
- inline Align max (Align Lhs, MaybeAlign Rhs) {
347
- return Rhs && *Rhs > Lhs ? *Rhs : Lhs;
348
- }
349
-
350
297
#ifndef NDEBUG
351
298
// For usage in LLVM_DEBUG macros.
352
299
inline std::string DebugStr (const Align &A) {
0 commit comments