[InstCombine] Signed saturation patterns
[llvm-core.git] / unittests / Support / AlignOfTest.cpp
blobd8cabde6c727ae4da90f25e6fa4b534a3a19c513
1 //=== - llvm/unittest/Support/AlignOfTest.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 #ifdef _MSC_VER
10 // Disable warnings about alignment-based structure padding.
11 // This must be above the includes to suppress warnings in included templates.
12 #pragma warning(disable:4324)
13 #endif
15 #include "llvm/Support/AlignOf.h"
16 #include "llvm/Support/Compiler.h"
17 #include "gtest/gtest.h"
19 using namespace llvm;
21 namespace {
22 // Disable warnings about questionable type definitions.
23 // We're testing that even questionable types work with the alignment utilities.
24 #ifdef _MSC_VER
25 #pragma warning(disable:4584)
26 #endif
28 // Suppress direct base '{anonymous}::S1' inaccessible in '{anonymous}::D9'
29 // due to ambiguity warning.
30 #ifdef __clang__
31 #pragma clang diagnostic ignored "-Wunknown-pragmas"
32 #pragma clang diagnostic ignored "-Winaccessible-base"
33 #elif ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
34 // Pragma based warning suppression was introduced in GGC 4.2. Additionally
35 // this warning is "enabled by default". The warning still appears if -Wall is
36 // suppressed. Apparently GCC suppresses it when -w is specifed, which is odd.
37 #pragma GCC diagnostic warning "-w"
38 #endif
40 // Define some fixed alignment types to use in these tests.
41 struct alignas(1) A1 {};
42 struct alignas(2) A2 {};
43 struct alignas(4) A4 {};
44 struct alignas(8) A8 {};
46 struct S1 {};
47 struct S2 { char a; };
48 struct S3 { int x; };
49 struct S4 { double y; };
50 struct S5 { A1 a1; A2 a2; A4 a4; A8 a8; };
51 struct S6 { double f(); };
52 struct D1 : S1 {};
53 struct D2 : S6 { float g(); };
54 struct D3 : S2 {};
55 struct D4 : S2 { int x; };
56 struct D5 : S3 { char c; };
57 struct D6 : S2, S3 {};
58 struct D7 : S1, S3 {};
59 struct D8 : S1, D4, D5 { double x[2]; };
60 struct D9 : S1, D1 { S1 s1; };
61 struct V1 { virtual ~V1(); };
62 struct V2 { int x; virtual ~V2(); };
63 struct V3 : V1 {
64 ~V3() override;
66 struct V4 : virtual V2 { int y;
67 ~V4() override;
69 struct V5 : V4, V3 { double z;
70 ~V5() override;
72 struct V6 : S1 { virtual ~V6(); };
73 struct V7 : virtual V2, virtual V6 {
74 ~V7() override;
76 struct V8 : V5, virtual V6, V7 { double zz;
77 ~V8() override;
80 double S6::f() { return 0.0; }
81 float D2::g() { return 0.0f; }
82 V1::~V1() {}
83 V2::~V2() {}
84 V3::~V3() {}
85 V4::~V4() {}
86 V5::~V5() {}
87 V6::~V6() {}
88 V7::~V7() {}
89 V8::~V8() {}
91 template <typename M> struct T { M m; };
93 TEST(AlignOfTest, BasicAlignedArray) {
94 EXPECT_LE(1u, alignof(AlignedCharArrayUnion<A1>));
95 EXPECT_LE(2u, alignof(AlignedCharArrayUnion<A2>));
96 EXPECT_LE(4u, alignof(AlignedCharArrayUnion<A4>));
97 EXPECT_LE(8u, alignof(AlignedCharArrayUnion<A8>));
99 EXPECT_LE(1u, sizeof(AlignedCharArrayUnion<A1>));
100 EXPECT_LE(2u, sizeof(AlignedCharArrayUnion<A2>));
101 EXPECT_LE(4u, sizeof(AlignedCharArrayUnion<A4>));
102 EXPECT_LE(8u, sizeof(AlignedCharArrayUnion<A8>));
104 EXPECT_EQ(1u, (alignof(AlignedCharArrayUnion<A1>)));
105 EXPECT_EQ(2u, (alignof(AlignedCharArrayUnion<A1, A2>)));
106 EXPECT_EQ(4u, (alignof(AlignedCharArrayUnion<A1, A2, A4>)));
107 EXPECT_EQ(8u, (alignof(AlignedCharArrayUnion<A1, A2, A4, A8>)));
109 EXPECT_EQ(1u, sizeof(AlignedCharArrayUnion<A1>));
110 EXPECT_EQ(2u, sizeof(AlignedCharArrayUnion<A1, A2>));
111 EXPECT_EQ(4u, sizeof(AlignedCharArrayUnion<A1, A2, A4>));
112 EXPECT_EQ(8u, sizeof(AlignedCharArrayUnion<A1, A2, A4, A8>));
114 EXPECT_EQ(1u, (alignof(AlignedCharArrayUnion<A1[1]>)));
115 EXPECT_EQ(2u, (alignof(AlignedCharArrayUnion<A1[2], A2[1]>)));
116 EXPECT_EQ(4u, (alignof(AlignedCharArrayUnion<A1[42], A2[55], A4[13]>)));
117 EXPECT_EQ(8u, (alignof(AlignedCharArrayUnion<A1[2], A2[1], A4, A8>)));
119 EXPECT_EQ(1u, sizeof(AlignedCharArrayUnion<A1[1]>));
120 EXPECT_EQ(2u, sizeof(AlignedCharArrayUnion<A1[2], A2[1]>));
121 EXPECT_EQ(4u, sizeof(AlignedCharArrayUnion<A1[3], A2[2], A4>));
122 EXPECT_EQ(16u, sizeof(AlignedCharArrayUnion<A1, A2[3],
123 A4[3], A8>));
125 // For other tests we simply assert that the alignment of the union mathes
126 // that of the fundamental type and hope that we have any weird type
127 // productions that would trigger bugs.
128 EXPECT_EQ(alignof(T<char>), alignof(AlignedCharArrayUnion<char>));
129 EXPECT_EQ(alignof(T<short>), alignof(AlignedCharArrayUnion<short>));
130 EXPECT_EQ(alignof(T<int>), alignof(AlignedCharArrayUnion<int>));
131 EXPECT_EQ(alignof(T<long>), alignof(AlignedCharArrayUnion<long>));
132 EXPECT_EQ(alignof(T<long long>), alignof(AlignedCharArrayUnion<long long>));
133 EXPECT_EQ(alignof(T<float>), alignof(AlignedCharArrayUnion<float>));
134 EXPECT_EQ(alignof(T<double>), alignof(AlignedCharArrayUnion<double>));
135 EXPECT_EQ(alignof(T<long double>),
136 alignof(AlignedCharArrayUnion<long double>));
137 EXPECT_EQ(alignof(T<void *>), alignof(AlignedCharArrayUnion<void *>));
138 EXPECT_EQ(alignof(T<int *>), alignof(AlignedCharArrayUnion<int *>));
139 EXPECT_EQ(alignof(T<double (*)(double)>),
140 alignof(AlignedCharArrayUnion<double (*)(double)>));
141 EXPECT_EQ(alignof(T<double (S6::*)()>),
142 alignof(AlignedCharArrayUnion<double (S6::*)()>));
143 EXPECT_EQ(alignof(S1), alignof(AlignedCharArrayUnion<S1>));
144 EXPECT_EQ(alignof(S2), alignof(AlignedCharArrayUnion<S2>));
145 EXPECT_EQ(alignof(S3), alignof(AlignedCharArrayUnion<S3>));
146 EXPECT_EQ(alignof(S4), alignof(AlignedCharArrayUnion<S4>));
147 EXPECT_EQ(alignof(S5), alignof(AlignedCharArrayUnion<S5>));
148 EXPECT_EQ(alignof(S6), alignof(AlignedCharArrayUnion<S6>));
149 EXPECT_EQ(alignof(D1), alignof(AlignedCharArrayUnion<D1>));
150 EXPECT_EQ(alignof(D2), alignof(AlignedCharArrayUnion<D2>));
151 EXPECT_EQ(alignof(D3), alignof(AlignedCharArrayUnion<D3>));
152 EXPECT_EQ(alignof(D4), alignof(AlignedCharArrayUnion<D4>));
153 EXPECT_EQ(alignof(D5), alignof(AlignedCharArrayUnion<D5>));
154 EXPECT_EQ(alignof(D6), alignof(AlignedCharArrayUnion<D6>));
155 EXPECT_EQ(alignof(D7), alignof(AlignedCharArrayUnion<D7>));
156 EXPECT_EQ(alignof(D8), alignof(AlignedCharArrayUnion<D8>));
157 EXPECT_EQ(alignof(D9), alignof(AlignedCharArrayUnion<D9>));
158 EXPECT_EQ(alignof(V1), alignof(AlignedCharArrayUnion<V1>));
159 EXPECT_EQ(alignof(V2), alignof(AlignedCharArrayUnion<V2>));
160 EXPECT_EQ(alignof(V3), alignof(AlignedCharArrayUnion<V3>));
161 EXPECT_EQ(alignof(V4), alignof(AlignedCharArrayUnion<V4>));
162 EXPECT_EQ(alignof(V5), alignof(AlignedCharArrayUnion<V5>));
163 EXPECT_EQ(alignof(V6), alignof(AlignedCharArrayUnion<V6>));
164 EXPECT_EQ(alignof(V7), alignof(AlignedCharArrayUnion<V7>));
166 // Some versions of MSVC get this wrong somewhat disturbingly. The failure
167 // appears to be benign: alignof(V8) produces a preposterous value: 12
168 #ifndef _MSC_VER
169 EXPECT_EQ(alignof(V8), alignof(AlignedCharArrayUnion<V8>));
170 #endif
172 EXPECT_EQ(sizeof(char), sizeof(AlignedCharArrayUnion<char>));
173 EXPECT_EQ(sizeof(char[1]), sizeof(AlignedCharArrayUnion<char[1]>));
174 EXPECT_EQ(sizeof(char[2]), sizeof(AlignedCharArrayUnion<char[2]>));
175 EXPECT_EQ(sizeof(char[3]), sizeof(AlignedCharArrayUnion<char[3]>));
176 EXPECT_EQ(sizeof(char[4]), sizeof(AlignedCharArrayUnion<char[4]>));
177 EXPECT_EQ(sizeof(char[5]), sizeof(AlignedCharArrayUnion<char[5]>));
178 EXPECT_EQ(sizeof(char[8]), sizeof(AlignedCharArrayUnion<char[8]>));
179 EXPECT_EQ(sizeof(char[13]), sizeof(AlignedCharArrayUnion<char[13]>));
180 EXPECT_EQ(sizeof(char[16]), sizeof(AlignedCharArrayUnion<char[16]>));
181 EXPECT_EQ(sizeof(char[21]), sizeof(AlignedCharArrayUnion<char[21]>));
182 EXPECT_EQ(sizeof(char[32]), sizeof(AlignedCharArrayUnion<char[32]>));
183 EXPECT_EQ(sizeof(short), sizeof(AlignedCharArrayUnion<short>));
184 EXPECT_EQ(sizeof(int), sizeof(AlignedCharArrayUnion<int>));
185 EXPECT_EQ(sizeof(long), sizeof(AlignedCharArrayUnion<long>));
186 EXPECT_EQ(sizeof(long long),
187 sizeof(AlignedCharArrayUnion<long long>));
188 EXPECT_EQ(sizeof(float), sizeof(AlignedCharArrayUnion<float>));
189 EXPECT_EQ(sizeof(double), sizeof(AlignedCharArrayUnion<double>));
190 EXPECT_EQ(sizeof(long double),
191 sizeof(AlignedCharArrayUnion<long double>));
192 EXPECT_EQ(sizeof(void *), sizeof(AlignedCharArrayUnion<void *>));
193 EXPECT_EQ(sizeof(int *), sizeof(AlignedCharArrayUnion<int *>));
194 EXPECT_EQ(sizeof(double (*)(double)),
195 sizeof(AlignedCharArrayUnion<double (*)(double)>));
196 EXPECT_EQ(sizeof(double (S6::*)()),
197 sizeof(AlignedCharArrayUnion<double (S6::*)()>));
198 EXPECT_EQ(sizeof(S1), sizeof(AlignedCharArrayUnion<S1>));
199 EXPECT_EQ(sizeof(S2), sizeof(AlignedCharArrayUnion<S2>));
200 EXPECT_EQ(sizeof(S3), sizeof(AlignedCharArrayUnion<S3>));
201 EXPECT_EQ(sizeof(S4), sizeof(AlignedCharArrayUnion<S4>));
202 EXPECT_EQ(sizeof(S5), sizeof(AlignedCharArrayUnion<S5>));
203 EXPECT_EQ(sizeof(S6), sizeof(AlignedCharArrayUnion<S6>));
204 EXPECT_EQ(sizeof(D1), sizeof(AlignedCharArrayUnion<D1>));
205 EXPECT_EQ(sizeof(D2), sizeof(AlignedCharArrayUnion<D2>));
206 EXPECT_EQ(sizeof(D3), sizeof(AlignedCharArrayUnion<D3>));
207 EXPECT_EQ(sizeof(D4), sizeof(AlignedCharArrayUnion<D4>));
208 EXPECT_EQ(sizeof(D5), sizeof(AlignedCharArrayUnion<D5>));
209 EXPECT_EQ(sizeof(D6), sizeof(AlignedCharArrayUnion<D6>));
210 EXPECT_EQ(sizeof(D7), sizeof(AlignedCharArrayUnion<D7>));
211 EXPECT_EQ(sizeof(D8), sizeof(AlignedCharArrayUnion<D8>));
212 EXPECT_EQ(sizeof(D9), sizeof(AlignedCharArrayUnion<D9>));
213 EXPECT_EQ(sizeof(D9[1]), sizeof(AlignedCharArrayUnion<D9[1]>));
214 EXPECT_EQ(sizeof(D9[2]), sizeof(AlignedCharArrayUnion<D9[2]>));
215 EXPECT_EQ(sizeof(D9[3]), sizeof(AlignedCharArrayUnion<D9[3]>));
216 EXPECT_EQ(sizeof(D9[4]), sizeof(AlignedCharArrayUnion<D9[4]>));
217 EXPECT_EQ(sizeof(D9[5]), sizeof(AlignedCharArrayUnion<D9[5]>));
218 EXPECT_EQ(sizeof(D9[8]), sizeof(AlignedCharArrayUnion<D9[8]>));
219 EXPECT_EQ(sizeof(D9[13]), sizeof(AlignedCharArrayUnion<D9[13]>));
220 EXPECT_EQ(sizeof(D9[16]), sizeof(AlignedCharArrayUnion<D9[16]>));
221 EXPECT_EQ(sizeof(D9[21]), sizeof(AlignedCharArrayUnion<D9[21]>));
222 EXPECT_EQ(sizeof(D9[32]), sizeof(AlignedCharArrayUnion<D9[32]>));
223 EXPECT_EQ(sizeof(V1), sizeof(AlignedCharArrayUnion<V1>));
224 EXPECT_EQ(sizeof(V2), sizeof(AlignedCharArrayUnion<V2>));
225 EXPECT_EQ(sizeof(V3), sizeof(AlignedCharArrayUnion<V3>));
226 EXPECT_EQ(sizeof(V4), sizeof(AlignedCharArrayUnion<V4>));
227 EXPECT_EQ(sizeof(V5), sizeof(AlignedCharArrayUnion<V5>));
228 EXPECT_EQ(sizeof(V6), sizeof(AlignedCharArrayUnion<V6>));
229 EXPECT_EQ(sizeof(V7), sizeof(AlignedCharArrayUnion<V7>));
231 // Some versions of MSVC also get this wrong. The failure again appears to be
232 // benign: sizeof(V8) is only 52 bytes, but our array reserves 56.
233 #ifndef _MSC_VER
234 EXPECT_EQ(sizeof(V8), sizeof(AlignedCharArrayUnion<V8>));
235 #endif
237 } // end anonymous namespace