[AA] Pass query info.
[llvm-project.git] / llvm / unittests / Support / AlignmentTest.cpp
blob5ef9cd4b2ac787dae24ca00e27b2283a5d183968
1 //=== - llvm/unittest/Support/Alignment.cpp - Alignment utility tests -----===//
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 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/Alignment.h"
10 #include "gtest/gtest.h"
12 #include <vector>
14 #ifdef _MSC_VER
15 // Disable warnings about potential divide by 0.
16 #pragma warning(push)
17 #pragma warning(disable : 4723)
18 #endif
20 using namespace llvm;
22 namespace {
24 TEST(AlignmentTest, AlignOfConstant) {
25 EXPECT_EQ(Align::Of<uint8_t>(), Align(alignof(uint8_t)));
26 EXPECT_EQ(Align::Of<uint16_t>(), Align(alignof(uint16_t)));
27 EXPECT_EQ(Align::Of<uint32_t>(), Align(alignof(uint32_t)));
28 EXPECT_EQ(Align::Of<uint64_t>(), Align(alignof(uint64_t)));
31 TEST(AlignmentTest, AlignConstant) {
32 EXPECT_EQ(Align::Constant<1>(), Align(1));
33 EXPECT_EQ(Align::Constant<2>(), Align(2));
34 EXPECT_EQ(Align::Constant<4>(), Align(4));
35 EXPECT_EQ(Align::Constant<8>(), Align(8));
36 EXPECT_EQ(Align::Constant<16>(), Align(16));
37 EXPECT_EQ(Align::Constant<32>(), Align(32));
38 EXPECT_EQ(Align::Constant<64>(), Align(64));
41 TEST(AlignmentTest, AlignConstexprConstant) {
42 constexpr Align kConstantAlign = Align::Of<uint64_t>();
43 EXPECT_EQ(Align(alignof(uint64_t)), kConstantAlign);
46 std::vector<uint64_t> getValidAlignments() {
47 std::vector<uint64_t> Out;
48 for (size_t Shift = 0; Shift < 64; ++Shift)
49 Out.push_back(1ULL << Shift);
50 return Out;
53 TEST(AlignmentTest, AlignDefaultCTor) {
54 EXPECT_EQ(Align().value(), 1ULL);
57 TEST(AlignmentTest, MaybeAlignDefaultCTor) {
58 EXPECT_FALSE(MaybeAlign().hasValue());
61 TEST(AlignmentTest, ValidCTors) {
62 for (uint64_t Value : getValidAlignments()) {
63 EXPECT_EQ(Align(Value).value(), Value);
64 EXPECT_EQ((*MaybeAlign(Value)).value(), Value);
68 TEST(AlignmentTest, CheckMaybeAlignHasValue) {
69 EXPECT_TRUE(MaybeAlign(1));
70 EXPECT_TRUE(MaybeAlign(1).hasValue());
71 EXPECT_FALSE(MaybeAlign(0));
72 EXPECT_FALSE(MaybeAlign(0).hasValue());
73 EXPECT_FALSE(MaybeAlign());
74 EXPECT_FALSE(MaybeAlign().hasValue());
77 TEST(AlignmentTest, Division) {
78 for (uint64_t Value : getValidAlignments()) {
79 if (Value > 1) {
80 EXPECT_EQ(Align(Value) / 2, Value / 2);
81 EXPECT_EQ(MaybeAlign(Value) / 2, Value / 2);
84 EXPECT_EQ(MaybeAlign(0) / 2, MaybeAlign(0));
87 TEST(AlignmentTest, AlignTo) {
88 struct {
89 uint64_t alignment;
90 uint64_t offset;
91 uint64_t rounded;
92 const void *forgedAddr() const {
93 // A value of any integral or enumeration type can be converted to a
94 // pointer type.
95 return reinterpret_cast<const void *>(offset);
97 } kTests[] = {
98 // MaybeAlign
99 {0, 0, 0},
100 {0, 1, 1},
101 {0, 5, 5},
102 // MaybeAlign / Align
103 {1, 0, 0},
104 {1, 1, 1},
105 {1, 5, 5},
106 {2, 0, 0},
107 {2, 1, 2},
108 {2, 2, 2},
109 {2, 7, 8},
110 {2, 16, 16},
111 {4, 0, 0},
112 {4, 1, 4},
113 {4, 4, 4},
114 {4, 6, 8},
116 for (const auto &T : kTests) {
117 MaybeAlign A(T.alignment);
118 // Test MaybeAlign
119 EXPECT_EQ(alignTo(T.offset, A), T.rounded);
120 // Test Align
121 if (A) {
122 EXPECT_EQ(alignTo(T.offset, A.getValue()), T.rounded);
123 EXPECT_EQ(alignAddr(T.forgedAddr(), A.getValue()), T.rounded);
128 TEST(AlignmentTest, AlignToWithSkew) {
129 EXPECT_EQ(alignTo(5, Align(8), 0), alignTo(5, Align(8)));
130 EXPECT_EQ(alignTo(5, Align(8), 7), 7U);
131 EXPECT_EQ(alignTo(17, Align(8), 1), 17U);
132 EXPECT_EQ(alignTo(~0LL, Align(8), 3), 3U);
135 TEST(AlignmentTest, Log2) {
136 for (uint64_t Value : getValidAlignments()) {
137 EXPECT_EQ(Log2(Align(Value)), Log2_64(Value));
141 TEST(AlignmentTest, MinAlign) {
142 struct {
143 uint64_t A;
144 uint64_t B;
145 uint64_t MinAlign;
146 } kTests[] = {
147 // MaybeAlign
148 {0, 0, 0},
149 {0, 8, 8},
150 {2, 0, 2},
151 // MaybeAlign / Align
152 {1, 2, 1},
153 {8, 4, 4},
155 for (const auto &T : kTests) {
156 EXPECT_EQ(commonAlignment(MaybeAlign(T.A), MaybeAlign(T.B)), T.MinAlign);
157 EXPECT_EQ(MinAlign(T.A, T.B), T.MinAlign);
158 if (T.A) {
159 EXPECT_EQ(commonAlignment(Align(T.A), MaybeAlign(T.B)), T.MinAlign);
161 if (T.B) {
162 EXPECT_EQ(commonAlignment(MaybeAlign(T.A), Align(T.B)), T.MinAlign);
164 if (T.A && T.B) {
165 EXPECT_EQ(commonAlignment(Align(T.A), Align(T.B)), T.MinAlign);
170 TEST(AlignmentTest, Encode_Decode) {
171 for (uint64_t Value : getValidAlignments()) {
173 Align Actual(Value);
174 Align Expected = decodeMaybeAlign(encode(Actual)).getValue();
175 EXPECT_EQ(Expected, Actual);
178 MaybeAlign Actual(Value);
179 MaybeAlign Expected = decodeMaybeAlign(encode(Actual));
180 EXPECT_EQ(Expected, Actual);
183 MaybeAlign Actual(0);
184 MaybeAlign Expected = decodeMaybeAlign(encode(Actual));
185 EXPECT_EQ(Expected, Actual);
188 TEST(AlignmentTest, isAligned_isAddrAligned) {
189 struct {
190 uint64_t alignment;
191 uint64_t offset;
192 bool isAligned;
193 const void *forgedAddr() const {
194 // A value of any integral or enumeration type can be converted to a
195 // pointer type.
196 return reinterpret_cast<const void *>(offset);
198 } kTests[] = {
199 {1, 0, true}, {1, 1, true}, {1, 5, true}, {2, 0, true},
200 {2, 1, false}, {2, 2, true}, {2, 7, false}, {2, 16, true},
201 {4, 0, true}, {4, 1, false}, {4, 4, true}, {4, 6, false},
203 for (const auto &T : kTests) {
204 MaybeAlign A(T.alignment);
205 // Test Align
206 if (A) {
207 EXPECT_EQ(isAligned(A.getValue(), T.offset), T.isAligned);
208 EXPECT_EQ(isAddrAligned(A.getValue(), T.forgedAddr()), T.isAligned);
213 TEST(AlignmentTest, offsetToAlignment) {
214 struct {
215 uint64_t alignment;
216 uint64_t offset;
217 uint64_t alignedOffset;
218 const void *forgedAddr() const {
219 // A value of any integral or enumeration type can be converted to a
220 // pointer type.
221 return reinterpret_cast<const void *>(offset);
223 } kTests[] = {
224 {1, 0, 0}, {1, 1, 0}, {1, 5, 0}, {2, 0, 0}, {2, 1, 1}, {2, 2, 0},
225 {2, 7, 1}, {2, 16, 0}, {4, 0, 0}, {4, 1, 3}, {4, 4, 0}, {4, 6, 2},
227 for (const auto &T : kTests) {
228 const Align A(T.alignment);
229 EXPECT_EQ(offsetToAlignment(T.offset, A), T.alignedOffset);
230 EXPECT_EQ(offsetToAlignedAddr(T.forgedAddr(), A), T.alignedOffset);
234 TEST(AlignmentTest, AlignComparisons) {
235 std::vector<uint64_t> ValidAlignments = getValidAlignments();
236 std::sort(ValidAlignments.begin(), ValidAlignments.end());
237 for (size_t I = 1; I < ValidAlignments.size(); ++I) {
238 assert(I >= 1);
239 const Align A(ValidAlignments[I - 1]);
240 const Align B(ValidAlignments[I]);
241 EXPECT_EQ(A, A);
242 EXPECT_NE(A, B);
243 EXPECT_LT(A, B);
244 EXPECT_GT(B, A);
245 EXPECT_LE(A, B);
246 EXPECT_GE(B, A);
247 EXPECT_LE(A, A);
248 EXPECT_GE(A, A);
250 EXPECT_EQ(A, A.value());
251 EXPECT_NE(A, B.value());
252 EXPECT_LT(A, B.value());
253 EXPECT_GT(B, A.value());
254 EXPECT_LE(A, B.value());
255 EXPECT_GE(B, A.value());
256 EXPECT_LE(A, A.value());
257 EXPECT_GE(A, A.value());
259 EXPECT_EQ(std::max(A, B), B);
260 EXPECT_EQ(std::min(A, B), A);
262 const MaybeAlign MA(ValidAlignments[I - 1]);
263 const MaybeAlign MB(ValidAlignments[I]);
264 EXPECT_EQ(MA, MA);
265 EXPECT_NE(MA, MB);
267 EXPECT_EQ(MA, MA ? (*MA).value() : 0);
268 EXPECT_NE(MA, MB ? (*MB).value() : 0);
270 EXPECT_EQ(std::max(A, B), B);
271 EXPECT_EQ(std::min(A, B), A);
275 TEST(AlignmentTest, Max) {
276 // We introduce std::max here to test ADL.
277 using std::max;
279 // Uses llvm::max.
280 EXPECT_EQ(max(MaybeAlign(), Align(2)), Align(2));
281 EXPECT_EQ(max(Align(2), MaybeAlign()), Align(2));
283 EXPECT_EQ(max(MaybeAlign(1), Align(2)), Align(2));
284 EXPECT_EQ(max(Align(2), MaybeAlign(1)), Align(2));
286 EXPECT_EQ(max(MaybeAlign(2), Align(2)), Align(2));
287 EXPECT_EQ(max(Align(2), MaybeAlign(2)), Align(2));
289 EXPECT_EQ(max(MaybeAlign(4), Align(2)), Align(4));
290 EXPECT_EQ(max(Align(2), MaybeAlign(4)), Align(4));
292 // Uses std::max.
293 EXPECT_EQ(max(Align(2), Align(4)), Align(4));
296 TEST(AlignmentTest, AssumeAligned) {
297 EXPECT_EQ(assumeAligned(0), Align(1));
298 EXPECT_EQ(assumeAligned(0), Align());
299 EXPECT_EQ(assumeAligned(1), Align(1));
300 EXPECT_EQ(assumeAligned(1), Align());
303 // Death tests reply on assert which is disabled in release mode.
304 #ifndef NDEBUG
306 // We use a subset of valid alignments for DEATH_TESTs as they are particularly
307 // slow.
308 std::vector<uint64_t> getValidAlignmentsForDeathTest() {
309 return {1, 1ULL << 31, 1ULL << 63};
312 std::vector<uint64_t> getNonPowerOfTwo() { return {3, 10, 15}; }
314 TEST(AlignmentDeathTest, CantConvertUnsetMaybe) {
315 EXPECT_DEATH((MaybeAlign(0).getValue()), ".*");
318 TEST(AlignmentDeathTest, Division) {
319 EXPECT_DEATH(Align(1) / 2, "Can't halve byte alignment");
320 EXPECT_DEATH(MaybeAlign(1) / 2, "Can't halve byte alignment");
322 EXPECT_DEATH(Align(8) / 0, "Divisor must be positive and a power of 2");
323 EXPECT_DEATH(Align(8) / 3, "Divisor must be positive and a power of 2");
326 TEST(AlignmentDeathTest, InvalidCTors) {
327 EXPECT_DEATH((Align(0)), "Value must not be 0");
328 for (uint64_t Value : getNonPowerOfTwo()) {
329 EXPECT_DEATH((Align(Value)), "Alignment is not a power of 2");
330 EXPECT_DEATH((MaybeAlign(Value)),
331 "Alignment is neither 0 nor a power of 2");
335 TEST(AlignmentDeathTest, ComparisonsWithZero) {
336 for (uint64_t Value : getValidAlignmentsForDeathTest()) {
337 EXPECT_DEATH((void)(Align(Value) == 0), ".* should be defined");
338 EXPECT_DEATH((void)(Align(Value) != 0), ".* should be defined");
339 EXPECT_DEATH((void)(Align(Value) >= 0), ".* should be defined");
340 EXPECT_DEATH((void)(Align(Value) <= 0), ".* should be defined");
341 EXPECT_DEATH((void)(Align(Value) > 0), ".* should be defined");
342 EXPECT_DEATH((void)(Align(Value) < 0), ".* should be defined");
346 TEST(AlignmentDeathTest, CompareMaybeAlignToZero) {
347 for (uint64_t Value : getValidAlignmentsForDeathTest()) {
348 // MaybeAlign is allowed to be == or != 0
349 (void)(MaybeAlign(Value) == 0);
350 (void)(MaybeAlign(Value) != 0);
354 TEST(AlignmentDeathTest, AlignAddr) {
355 const void *const unaligned_high_ptr =
356 reinterpret_cast<const void *>(std::numeric_limits<uintptr_t>::max() - 1);
357 EXPECT_DEATH(alignAddr(unaligned_high_ptr, Align(16)), "Overflow");
360 #endif // NDEBUG
362 } // end anonymous namespace
364 #ifdef _MSC_VER
365 #pragma warning(pop)
366 #endif