Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CodeGenCXX / const-init-cxx11.cpp
blob3a12fe444f137bf96612337864f1be1141e70f95
1 // RUN: %clang_cc1 -w -fmerge-all-constants -triple x86_64-elf-gnu -emit-llvm -o - %s -std=c++11 | FileCheck %s
2 // RUN: %clang_cc1 -w -fmerge-all-constants -triple x86_64-elf-gnu -emit-llvm -o - %s -std=c++20 | FileCheck -check-prefix=CHECK20 %s
4 // FIXME: The padding in all these objects should be zero-initialized.
5 namespace StructUnion {
6 struct A {
7 int n;
8 double d;
9 union U {
10 constexpr U(int x) : x(x) {}
11 constexpr U(const char *y) : y(y) {}
12 int x;
13 const char *y;
14 } u;
16 constexpr A(int n, double d, int x) : n(n), d(d), u(x) {}
17 constexpr A(int n, double d, const char *y) : n(n), d(d), u(y) {}
20 // CHECK: @_ZN11StructUnion1aE ={{.*}} constant {{.*}} { i32 1, double 2.000000e+00, {{.*}} { i32 3, [4 x i8] undef } }
21 extern constexpr A a(1, 2.0, 3);
23 // CHECK: @_ZN11StructUnion1bE ={{.*}} constant {{.*}} { i32 4, double 5.000000e+00, {{.*}} { ptr @{{.*}} } }
24 extern constexpr A b(4, 5, "hello");
26 struct B {
27 int n;
30 // CHECK: @_ZN11StructUnion1cE ={{.*}} global {{.*}} zeroinitializer
31 // CHECK: @_ZN11StructUnion2c2E ={{.*}} global {{.*}} zeroinitializer
32 B c;
33 B c2 = B();
35 // CHECK: @_ZN11StructUnion1dE ={{.*}} global {{.*}} zeroinitializer
36 B d[10];
38 struct C {
39 constexpr C() : c(0) {}
40 int c;
43 // CHECK: @_ZN11StructUnion1eE ={{.*}} global {{.*}} zeroinitializer
44 C e[10];
46 struct D {
47 constexpr D() : d(5) {}
48 int d;
51 // CHECK: @_ZN11StructUnion1fE ={{.*}} global {{.*}} { i32 5 }
52 D f;
54 union E {
55 int a;
56 void *b = &f;
59 // CHECK: @_ZN11StructUnion1gE ={{.*}} global {{.*}} @_ZN11StructUnion1fE
60 E g;
62 // CHECK: @_ZN11StructUnion1hE ={{.*}} global {{.*}} @_ZN11StructUnion1fE
63 E h = E();
66 namespace BaseClass {
67 template<typename T, unsigned> struct X : T {};
68 struct C { char c = 1; };
69 template<unsigned... Ns> struct Cs : X<C,Ns>... {};
70 struct N { int n = 3; };
71 struct D { double d = 4.0; };
73 template<typename ...Ts>
74 struct Test : Ts... { constexpr Test() : Ts()..., n(5) {} int n; };
76 using Test1 = Test<N, C, Cs<1,2>, D, X<C,1>>;
77 // CHECK: @_ZN9BaseClass2t1E ={{.*}} constant {{.*}} { i32 3, i8 1, i8 1, i8 1, double 4.000000e+00, i8 1, i32 5 }, align 8
78 extern constexpr Test1 t1 = Test1();
80 struct DN : D, N {};
81 struct DND : DN, X<D,0> {};
82 struct DNN : DN, X<N,0> {};
83 // CHECK: @_ZN9BaseClass3dndE ={{.*}} constant {{.*}} { double 4.000000e+00, i32 3, double 4.000000e+00 }
84 extern constexpr DND dnd = DND();
85 // Note, N subobject is laid out in DN subobject's tail padding.
86 // CHECK: @_ZN9BaseClass3dnnE ={{.*}} constant {{.*}} { double 4.000000e+00, i32 3, i32 3 }
87 extern constexpr DNN dnn = DNN();
89 struct E {};
90 struct Test2 : X<E,0>, X<E,1>, X<E,2>, X<E,3> {};
91 // CHECK: @_ZN9BaseClass2t2E ={{.*}} constant {{.*}} zeroinitializer, align 1
92 extern constexpr Test2 t2 = Test2();
94 struct __attribute((packed)) PackedD { double y = 2; };
95 struct Test3 : C, PackedD { constexpr Test3() {} };
96 // CHECK: @_ZN9BaseClass2t3E ={{.*}} constant <{ i8, double }> <{ i8 1, double 2.000000e+00 }>
97 extern constexpr Test3 t3 = Test3();
100 namespace Array {
101 // CHECK: @_ZN5Array3arrE ={{.*}} constant [2 x i32] [i32 4, i32 0]
102 extern constexpr int arr[2] = { 4 };
104 // CHECK: @_ZN5Array1cE ={{.*}} constant [6 x [4 x i8]] [{{.*}} c"foo\00", [4 x i8] c"a\00\00\00", [4 x i8] c"bar\00", [4 x i8] c"xyz\00", [4 x i8] c"b\00\00\00", [4 x i8] c"123\00"]
105 extern constexpr char c[6][4] = { "foo", "a", { "bar" }, { 'x', 'y', 'z' }, { "b" }, '1', '2', '3' };
107 // CHECK: @_ZN5Array2ucE ={{.*}} constant [4 x i8] c"foo\00"
108 extern constexpr unsigned char uc[] = { "foo" };
110 struct C { constexpr C() : n(5) {} int n, m = 3 * n + 1; };
111 // CHECK: @_ZN5Array5ctorsE ={{.*}} constant [3 x {{.*}}] [{{.*}} { i32 5, i32 16 }, {{.*}} { i32 5, i32 16 }, {{.*}} { i32 5, i32 16 }]
112 extern const C ctors[3];
113 constexpr C ctors[3];
115 // CHECK: @_ZN5Array1dE ={{.*}} constant {{.*}} { [2 x i32] [i32 1, i32 2], [3 x i32] [i32 3, i32 4, i32 5] }
116 struct D { int n[2]; int m[3]; } extern constexpr d = { 1, 2, 3, 4, 5 };
118 struct E {
119 char c[4];
120 char d[4];
121 constexpr E() : c("foo"), d("x") {}
123 // CHECK: @_ZN5Array1eE ={{.*}} constant {{.*}} { [4 x i8] c"foo\00", [4 x i8] c"x\00\00\00" }
124 extern constexpr E e = E();
126 // PR13290
127 struct F { constexpr F() : n(4) {} int n; };
128 // CHECK: @_ZN5Array2f1E ={{.*}} global {{.*}} zeroinitializer
129 F f1[1][1][0] = { };
130 // CHECK: @_ZN5Array2f2E ={{.*}} global {{.* i32 4 .* i32 4 .* i32 4 .* i32 4 .* i32 4 .* i32 4 .* i32 4 .* i32 4}}
131 F f2[2][2][2] = { };
134 namespace MemberPtr {
135 struct B1 {
136 int a, b;
137 virtual void f();
138 void g();
140 struct B2 {
141 int c, d;
142 virtual void h();
143 void i();
145 struct C : B1 {
146 int e;
147 virtual void j();
148 void k();
150 struct D : C, B2 {
151 int z;
152 virtual void l();
153 void m();
156 // CHECK: @_ZN9MemberPtr2daE ={{.*}} constant i64 8
157 // CHECK: @_ZN9MemberPtr2dbE ={{.*}} constant i64 12
158 // CHECK: @_ZN9MemberPtr2dcE ={{.*}} constant i64 32
159 // CHECK: @_ZN9MemberPtr2ddE ={{.*}} constant i64 36
160 // CHECK: @_ZN9MemberPtr2deE ={{.*}} constant i64 16
161 // CHECK: @_ZN9MemberPtr2dzE ={{.*}} constant i64 40
162 extern constexpr int (D::*da) = &B1::a;
163 extern constexpr int (D::*db) = &C::b;
164 extern constexpr int (D::*dc) = &B2::c;
165 extern constexpr int (D::*dd) = &D::d;
166 extern constexpr int (D::*de) = &C::e;
167 extern constexpr int (D::*dz) = &D::z;
169 // CHECK: @_ZN9MemberPtr2baE ={{.*}} constant i64 8
170 // CHECK: @_ZN9MemberPtr2bbE ={{.*}} constant i64 12
171 // CHECK: @_ZN9MemberPtr2bcE ={{.*}} constant i64 8
172 // CHECK: @_ZN9MemberPtr2bdE ={{.*}} constant i64 12
173 // CHECK: @_ZN9MemberPtr2beE ={{.*}} constant i64 16
174 // CHECK: @_ZN9MemberPtr3b1zE ={{.*}} constant i64 40
175 // CHECK: @_ZN9MemberPtr3b2zE ={{.*}} constant i64 16
176 extern constexpr int (B1::*ba) = (int(B1::*))&B1::a;
177 extern constexpr int (B1::*bb) = (int(B1::*))&C::b;
178 extern constexpr int (B2::*bc) = (int(B2::*))&B2::c;
179 extern constexpr int (B2::*bd) = (int(B2::*))&D::d;
180 extern constexpr int (B1::*be) = (int(B1::*))&C::e;
181 extern constexpr int (B1::*b1z) = (int(B1::*))&D::z;
182 extern constexpr int (B2::*b2z) = (int(B2::*))&D::z;
184 // CHECK: @_ZN9MemberPtr2dfE ={{.*}} constant {{.*}} { i64 1, i64 0 }
185 // CHECK: @_ZN9MemberPtr2dgE ={{.*}} constant {{.*}} { i64 {{.*}}2B11gEv{{.*}}, i64 0 }
186 // CHECK: @_ZN9MemberPtr2dhE ={{.*}} constant {{.*}} { i64 1, i64 24 }
187 // CHECK: @_ZN9MemberPtr2diE ={{.*}} constant {{.*}} { i64 {{.*}}2B21iEv{{.*}}, i64 24 }
188 // CHECK: @_ZN9MemberPtr2djE ={{.*}} constant {{.*}} { i64 9, i64 0 }
189 // CHECK: @_ZN9MemberPtr2dkE ={{.*}} constant {{.*}} { i64 {{.*}}1C1kEv{{.*}}, i64 0 }
190 // CHECK: @_ZN9MemberPtr2dlE ={{.*}} constant {{.*}} { i64 17, i64 0 }
191 // CHECK: @_ZN9MemberPtr2dmE ={{.*}} constant {{.*}} { i64 {{.*}}1D1mEv{{.*}}, i64 0 }
192 extern constexpr void (D::*df)() = &C::f;
193 extern constexpr void (D::*dg)() = &B1::g;
194 extern constexpr void (D::*dh)() = &B2::h;
195 extern constexpr void (D::*di)() = &D::i;
196 extern constexpr void (D::*dj)() = &C::j;
197 extern constexpr void (D::*dk)() = &C::k;
198 extern constexpr void (D::*dl)() = &D::l;
199 extern constexpr void (D::*dm)() = &D::m;
201 // CHECK: @_ZN9MemberPtr2bfE ={{.*}} constant {{.*}} { i64 1, i64 0 }
202 // CHECK: @_ZN9MemberPtr2bgE ={{.*}} constant {{.*}} { i64 {{.*}}2B11gEv{{.*}}, i64 0 }
203 // CHECK: @_ZN9MemberPtr2bhE ={{.*}} constant {{.*}} { i64 1, i64 0 }
204 // CHECK: @_ZN9MemberPtr2biE ={{.*}} constant {{.*}} { i64 {{.*}}2B21iEv{{.*}}, i64 0 }
205 // CHECK: @_ZN9MemberPtr2bjE ={{.*}} constant {{.*}} { i64 9, i64 0 }
206 // CHECK: @_ZN9MemberPtr2bkE ={{.*}} constant {{.*}} { i64 {{.*}}1C1kEv{{.*}}, i64 0 }
207 // CHECK: @_ZN9MemberPtr3b1lE ={{.*}} constant {{.*}} { i64 17, i64 0 }
208 // CHECK: @_ZN9MemberPtr3b1mE ={{.*}} constant {{.*}} { i64 {{.*}}1D1mEv{{.*}}, i64 0 }
209 // CHECK: @_ZN9MemberPtr3b2lE ={{.*}} constant {{.*}} { i64 17, i64 -24 }
210 // CHECK: @_ZN9MemberPtr3b2mE ={{.*}} constant {{.*}} { i64 {{.*}}1D1mEv{{.*}}, i64 -24 }
211 extern constexpr void (B1::*bf)() = (void(B1::*)())&C::f;
212 extern constexpr void (B1::*bg)() = (void(B1::*)())&B1::g;
213 extern constexpr void (B2::*bh)() = (void(B2::*)())&B2::h;
214 extern constexpr void (B2::*bi)() = (void(B2::*)())&D::i;
215 extern constexpr void (B1::*bj)() = (void(B1::*)())&C::j;
216 extern constexpr void (B1::*bk)() = (void(B1::*)())&C::k;
217 extern constexpr void (B1::*b1l)() = (void(B1::*)())&D::l;
218 extern constexpr void (B1::*b1m)() = (void(B1::*)())&D::m;
219 extern constexpr void (B2::*b2l)() = (void(B2::*)())&D::l;
220 extern constexpr void (B2::*b2m)() = (void(B2::*)())&D::m;
223 namespace LiteralReference {
224 struct Lit {
225 constexpr Lit() : n(5) {}
226 int n;
229 // This creates a non-const temporary and binds a reference to it.
230 // CHECK: @[[TEMP:.*]] = internal global {{.*}} { i32 5 }, align 4
231 // CHECK: @_ZN16LiteralReference3litE ={{.*}} constant {{.*}} @[[TEMP]], align 8
232 const Lit &lit = Lit();
234 // This creates a const temporary as part of the reference initialization.
235 // CHECK: @[[TEMP:.*]] = internal constant {{.*}} { i32 5 }, align 4
236 // CHECK: @_ZN16LiteralReference4lit2E ={{.*}} constant {{.*}} @[[TEMP]], align 8
237 const Lit &lit2 = {};
239 struct A { int &&r1; const int &&r2; };
240 struct B { A &&a1; const A &&a2; };
241 B b = { { 0, 1 }, { 2, 3 } };
242 // CHECK: @[[TEMP0:.*]] = internal global i32 0, align 4
243 // CHECK: @[[TEMP1:.*]] = internal constant i32 1, align 4
244 // CHECK: @[[TEMPA1:.*]] = internal global {{.*}} { ptr @[[TEMP0]], ptr @[[TEMP1]] }, align 8
245 // CHECK: @[[TEMP2:.*]] = internal global i32 2, align 4
246 // CHECK: @[[TEMP3:.*]] = internal constant i32 3, align 4
247 // CHECK: @[[TEMPA2:.*]] = internal constant {{.*}} { ptr @[[TEMP2]], ptr @[[TEMP3]] }, align 8
248 // CHECK: @_ZN16LiteralReference1bE ={{.*}} global {{.*}} { ptr @[[TEMPA1]], ptr @[[TEMPA2]] }, align 8
250 struct Subobj {
251 int a, b, c;
253 // CHECK: @[[TEMP:.*]] = internal global {{.*}} { i32 1, i32 2, i32 3 }, align 4
254 // CHECK: @_ZN16LiteralReference2soE ={{.*}} constant ptr getelementptr (i8, ptr @[[TEMP]], i64 4)
255 constexpr int &&so = Subobj{ 1, 2, 3 }.b;
257 struct Dummy { int padding; };
258 struct Derived : Dummy, Subobj {
259 constexpr Derived() : Dummy{200}, Subobj{4, 5, 6} {}
261 using ConstDerived = const Derived;
262 // CHECK: @[[TEMPCOMMA:.*]] = internal constant {{.*}} { i32 200, i32 4, i32 5, i32 6 }
263 // CHECK: @_ZN16LiteralReference5commaE ={{.*}} constant {{.*}} getelementptr {{.*}} @[[TEMPCOMMA]]{{.*}}, i64 8)
264 constexpr const int &comma = (1, (2, ConstDerived{}).b);
266 // CHECK: @[[TEMPDERIVED:.*]] = internal global {{.*}} { i32 200, i32 4, i32 5, i32 6 }
267 // CHECK: @_ZN16LiteralReference4baseE ={{.*}} constant {{.*}} getelementptr {{.*}} @[[TEMPDERIVED]]{{.*}}, i64 4)
268 constexpr Subobj &&base = Derived{};
270 // CHECK: @_ZN16LiteralReference7derivedE ={{.*}} constant {{.*}} @[[TEMPDERIVED]]
271 constexpr Derived &derived = static_cast<Derived&>(base);
274 namespace NonLiteralConstexpr {
275 constexpr int factorial(int n) {
276 return n ? factorial(n-1) * n : 1;
278 extern void f(int *p);
280 struct NonTrivialDtor {
281 constexpr NonTrivialDtor() : n(factorial(5)), p(&n) {}
282 ~NonTrivialDtor() {
283 f(p);
286 int n;
287 int *p;
289 static_assert(!__is_literal(NonTrivialDtor), "");
290 // CHECK: @_ZN19NonLiteralConstexpr3ntdE ={{.*}} global {{.*}} { i32 120, ptr @
291 NonTrivialDtor ntd;
293 struct VolatileMember {
294 constexpr VolatileMember() : n(5) {}
295 volatile int n;
297 static_assert(!__is_literal(VolatileMember), "");
298 // CHECK: @_ZN19NonLiteralConstexpr2vmE ={{.*}} global {{.*}} { i32 5 }
299 VolatileMember vm;
301 struct Both {
302 constexpr Both() : n(10) {}
303 ~Both();
304 volatile int n;
306 // CHECK: @_ZN19NonLiteralConstexpr1bE ={{.*}} global {{.*}} { i32 10 }
307 Both b;
309 void StaticVars() {
310 // CHECK: @_ZZN19NonLiteralConstexpr10StaticVarsEvE3ntd = {{.*}} { i32 120, ptr @
311 // CHECK: @_ZGVZN19NonLiteralConstexpr10StaticVarsEvE3ntd =
312 static NonTrivialDtor ntd;
313 // CHECK: @_ZZN19NonLiteralConstexpr10StaticVarsEvE2vm = {{.*}} { i32 5 }
314 // CHECK-NOT: @_ZGVZN19NonLiteralConstexpr10StaticVarsEvE2vm =
315 static VolatileMember vm;
316 // CHECK: @_ZZN19NonLiteralConstexpr10StaticVarsEvE1b = {{.*}} { i32 10 }
317 // CHECK: @_ZGVZN19NonLiteralConstexpr10StaticVarsEvE1b =
318 static Both b;
322 // PR12067
323 namespace VirtualMembers {
324 struct A {
325 constexpr A(double d) : d(d) {}
326 virtual void f();
327 double d;
329 struct B : A {
330 constexpr B() : A(2.0), c{'h', 'e', 'l', 'l', 'o'} {}
331 constexpr B(int n) : A(n), c{'w', 'o', 'r', 'l', 'd'} {}
332 virtual void g();
333 char c[5];
335 struct C {
336 constexpr C() : n(64) {}
337 int n;
339 struct D : C, A, B {
340 constexpr D() : A(1.0), B(), s(5) {}
341 short s;
343 struct E : D, B {
344 constexpr E() : B(3), c{'b','y','e'} {}
345 char c[3];
347 // CHECK: @_ZN14VirtualMembers1eE ={{.*}} global { ptr, double, i32, ptr, double, [5 x i8], i16, ptr, double, [5 x i8], [3 x i8] } { ptr getelementptr inbounds ({ [3 x ptr], [4 x ptr], [4 x ptr] }, ptr @_ZTVN14VirtualMembers1EE, i32 0, inrange i32 0, i32 2), double 1.000000e+00, i32 64, ptr getelementptr inbounds ({ [3 x ptr], [4 x ptr], [4 x ptr] }, ptr @_ZTVN14VirtualMembers1EE, i32 0, inrange i32 1, i32 2), double 2.000000e+00, [5 x i8] c"hello", i16 5, ptr getelementptr inbounds ({ [3 x ptr], [4 x ptr], [4 x ptr] }, ptr @_ZTVN14VirtualMembers1EE, i32 0, inrange i32 2, i32 2), double 3.000000e+00, [5 x i8] c"world", [3 x i8] c"bye" }
348 E e;
350 struct nsMemoryImpl {
351 virtual void f();
353 // CHECK: @_ZN14VirtualMembersL13sGlobalMemoryE = internal global %"struct.VirtualMembers::nsMemoryImpl" { ptr getelementptr inbounds ({ [3 x ptr] }, ptr @_ZTVN14VirtualMembers12nsMemoryImplE, i32 0, inrange i32 0, i32 2) }
354 __attribute__((used))
355 static nsMemoryImpl sGlobalMemory;
357 template<class T>
358 struct TemplateClass {
359 constexpr TemplateClass() : t{42} {}
360 virtual void templateMethod() {}
362 T t;
364 // CHECK: @_ZN14VirtualMembers1tE ={{.*}} global { ptr, i32 } { ptr getelementptr inbounds ({ [3 x ptr] }, ptr @_ZTVN14VirtualMembers13TemplateClassIiEE, i32 0, inrange i32 0, i32 2), i32 42 }
365 TemplateClass<int> t;
368 namespace PR13273 {
369 struct U {
370 int t;
371 U() = default;
374 struct S : U {
375 S() = default;
378 // CHECK: @_ZN7PR132731sE = {{.*}} zeroinitializer
379 extern const S s {};
382 namespace ArrayTemporary {
383 struct A { const int (&x)[3]; };
384 struct B { const A (&x)[2]; };
385 // CHECK: @[[A1:_ZGRN14ArrayTemporary1bE.*]] = internal constant [3 x i32] [i32 1, i32 2, i32 3]
386 // CHECK: @[[A2:_ZGRN14ArrayTemporary1bE.*]] = internal constant [3 x i32] [i32 4, i32 5, i32 6]
387 // CHECK: @[[ARR:_ZGRN14ArrayTemporary1bE.*]] = internal constant [2 x {{.*}}] [{{.*}} { ptr @[[A1]] }, {{.*}} { ptr @[[A2]] }]
388 // CHECK: @[[B:_ZGRN14ArrayTemporary1bE.*]] = internal global {{.*}} { ptr @[[ARR]] }
389 // CHECK: @_ZN14ArrayTemporary1bE ={{.*}} constant ptr @[[B]]
390 B &&b = { { { { 1, 2, 3 } }, { { 4, 5, 6 } } } };
393 namespace UnemittedTemporaryDecl {
394 constexpr int &&ref = 0;
395 extern constexpr int &ref2 = ref;
396 // CHECK: @_ZGRN22UnemittedTemporaryDecl3refE_ = internal global i32 0
398 // FIXME: This declaration should not be emitted -- it isn't odr-used.
399 // CHECK: @_ZN22UnemittedTemporaryDecl3refE
401 // CHECK: @_ZN22UnemittedTemporaryDecl4ref2E ={{.*}} constant ptr @_ZGRN22UnemittedTemporaryDecl3refE_
404 namespace DR2126 {
405 struct A { int &&b; };
406 constexpr const A &a = {42};
407 // CHECK: @_ZGRN6DR21261aE0_ = internal global i32 42
408 // FIXME: This is unused and need not be emitted.
409 // CHECK: @_ZGRN6DR21261aE_ = internal constant {{.*}} { ptr @_ZGRN6DR21261aE0_ }
410 // CHECK: @_ZN6DR21261rE ={{.*}} constant ptr @_ZGRN6DR21261aE0_
411 int &r = a.b;
413 // Dynamically initialized: the temporary object bound to 'b' could be
414 // modified (eg, by placement 'new') before the initializer of 's' runs.
415 constexpr A &&b = {42};
416 // CHECK: @_ZN6DR21261sE ={{.*}} global ptr null
417 int &s = b.b;
420 // CHECK: @_ZZN12LocalVarInit3aggEvE1a = internal constant {{.*}} i32 101
421 // CHECK: @_ZZN12LocalVarInit4ctorEvE1a = internal constant {{.*}} i32 102
422 // CHECK: @__const._ZN12LocalVarInit8mutable_Ev.a = private unnamed_addr constant {{.*}} i32 103
423 // CHECK: @_ZGRN33ClassTemplateWithStaticDataMember1SIvE1aE_ = linkonce_odr constant i32 5, comdat
424 // CHECK: @_ZN33ClassTemplateWithStaticDataMember3useE ={{.*}} constant ptr @_ZGRN33ClassTemplateWithStaticDataMember1SIvE1aE_
425 // CHECK: @_ZGRN39ClassTemplateWithHiddenStaticDataMember1SIvE1aE_ = linkonce_odr hidden constant i32 5, comdat
426 // CHECK: @_ZN39ClassTemplateWithHiddenStaticDataMember3useE ={{.*}} constant ptr @_ZGRN39ClassTemplateWithHiddenStaticDataMember1SIvE1aE_
427 // CHECK: @.str.[[STR:[0-9]+]] ={{.*}} constant [9 x i8] c"12345678\00"
428 // CHECK-NEXT: @e = global %struct.PR69979 { ptr @.str.[[STR]] }
429 // CHECK: @_ZGRZN20InlineStaticConstRef3funEvE1i_ = linkonce_odr constant i32 10, comdat
430 // CHECK20: @_ZZN12LocalVarInit4dtorEvE1a = internal constant {{.*}} i32 103
432 // Constant initialization tests go before this point,
433 // dynamic initialization tests go after.
435 // We must emit a constant initializer for NonLiteralConstexpr::ntd, but also
436 // emit an initializer to register its destructor.
437 // CHECK: define {{.*}}cxx_global_var_init{{.*}}
438 // CHECK-NOT: NonLiteralConstexpr
439 // CHECK: call {{.*}}cxa_atexit(ptr @_ZN19NonLiteralConstexpr14NonTrivialDtorD1Ev, ptr @_ZN19NonLiteralConstexpr3ntdE
440 // CHECK-NEXT: ret void
442 // We don't need to emit any dynamic initialization for NonLiteralConstexpr::vm.
443 // CHECK-NOT: NonLiteralConstexpr2vm
445 // We must emit a constant initializer for NonLiteralConstexpr::b, but also
446 // emit an initializer to register its destructor.
447 // CHECK: define {{.*}}cxx_global_var_init{{.*}}
448 // CHECK-NOT: NonLiteralConstexpr
449 // CHECK: call {{.*}}cxa_atexit(ptr @_ZN19NonLiteralConstexpr4BothD1Ev, ptr @_ZN19NonLiteralConstexpr1bE
450 // CHECK-NEXT: ret void
452 // CHECK: define {{.*}}NonLiteralConstexpr10StaticVars
453 // CHECK-NOT: }
454 // CHECK: call {{.*}}cxa_atexit{{.*}}@_ZN19NonLiteralConstexpr14NonTrivialDtorD1Ev
455 // CHECK-NOT: }
456 // CHECK: call {{.*}}cxa_atexit{{.*}}@_ZN19NonLiteralConstexpr4BothD1Ev
458 // PR12848: Don't emit dynamic initializers for local constexpr variables.
459 namespace LocalVarInit {
460 constexpr int f(int n) { return n; }
461 struct Agg { int k; };
462 struct Ctor { constexpr Ctor(int n) : k(n) {} int k; };
463 struct Mutable { constexpr Mutable(int n) : k(n) {} mutable int k; };
465 // CHECK: define {{.*}} @_ZN12LocalVarInit6scalarEv
466 // CHECK-NOT: call
467 // CHECK: store i32 100,
468 // CHECK-NOT: call
469 // CHECK: ret i32 100
470 int scalar() { constexpr int a = { f(100) }; return a; }
472 // CHECK: define {{.*}} @_ZN12LocalVarInit3aggEv
473 // CHECK-NOT: call
474 // CHECK: ret i32 101
475 int agg() { constexpr Agg a = { f(101) }; return a.k; }
477 // CHECK: define {{.*}} @_ZN12LocalVarInit4ctorEv
478 // CHECK-NOT: call
479 // CHECK: ret i32 102
480 int ctor() { constexpr Ctor a = { f(102) }; return a.k; }
482 // CHECK: define {{.*}} @_ZN12LocalVarInit8mutable_Ev
483 // CHECK-NOT: call
484 // CHECK: call {{.*}}memcpy{{.*}} @__const._ZN12LocalVarInit8mutable_Ev.a
485 // CHECK-NOT: call
486 // Can't fold return value due to 'mutable'.
487 // CHECK-NOT: ret i32 103
488 // CHECK: }
489 int mutable_() { constexpr Mutable a = { f(103) }; return a.k; }
491 #if __cplusplus >= 202002L
492 // CHECK20: define {{.*}} @_ZN12LocalVarInit4dtorEv
493 // CHECK20-NOT: call
494 // CHECK20: ret i32 103
495 struct Dtor { constexpr Dtor(int n) : k(n) {} constexpr ~Dtor() {} int k; };
496 int dtor() { constexpr Dtor a = { f(103) }; return a.k; }
497 #endif
500 namespace CrossFuncLabelDiff {
501 // Make sure we refuse to constant-fold the variable b.
502 constexpr long a(bool x) { return x ? 0 : (long)&&lbl + (0 && ({lbl: 0;})); }
503 void test() { static long b = (long)&&lbl - a(false); lbl: return; }
504 // CHECK: sub nsw i64 ptrtoint (ptr blockaddress(@_ZN18CrossFuncLabelDiff4testEv, {{.*}}) to i64),
505 // CHECK: store i64 {{.*}}, ptr @_ZZN18CrossFuncLabelDiff4testEvE1b, align 8
508 // PR12012
509 namespace VirtualBase {
510 struct B {};
511 struct D : virtual B {};
512 D d;
513 // CHECK: call {{.*}}@_ZN11VirtualBase1DC1Ev
515 template<typename T> struct X : T {
516 constexpr X() : T() {}
518 X<D> x;
519 // CHECK: call {{.*}}@_ZN11VirtualBase1XINS_1DEEC1Ev
522 // PR12145
523 namespace Unreferenced {
524 int n;
525 constexpr int *p = &n;
526 // We must not emit a load of 'p' here, since it's not odr-used.
527 int q = *p;
528 // CHECK-NOT: _ZN12Unreferenced1pE
529 // CHECK: = load i32, ptr @_ZN12Unreferenced1nE
530 // CHECK-NEXT: store i32 {{.*}}, ptr @_ZN12Unreferenced1qE
531 // CHECK-NOT: _ZN12Unreferenced1pE
533 // Technically, we are not required to substitute variables of reference types
534 // initialized by constant expressions, because the special case for odr-use
535 // of variables in [basic.def.odr]p2 only applies to objects. But we do so
536 // anyway.
538 constexpr int &r = n;
539 // CHECK-NOT: _ZN12Unreferenced1rE
540 int s = r;
542 const int t = 1;
543 const int &rt = t;
544 int f(int);
545 int u = f(rt);
546 // CHECK: call noundef i32 @_ZN12Unreferenced1fEi(i32 noundef 1)
549 namespace InitFromConst {
550 template<typename T> void consume(T);
552 const bool b = true;
553 const int n = 5;
554 constexpr double d = 4.3;
556 struct S { int n = 7; S *p = 0; };
557 constexpr S s = S();
558 const S &r = s;
559 constexpr const S *p = &r;
560 constexpr int S::*mp = &S::n;
561 constexpr int a[3] = { 1, 4, 9 };
563 void test() {
564 // CHECK: call void @_ZN13InitFromConst7consumeIbEEvT_(i1 noundef zeroext true)
565 consume(b);
567 // CHECK: call void @_ZN13InitFromConst7consumeIiEEvT_(i32 noundef 5)
568 consume(n);
570 // CHECK: call void @_ZN13InitFromConst7consumeIdEEvT_(double noundef 4.300000e+00)
571 consume(d);
573 // CHECK: call void @_ZN13InitFromConst7consumeIRKNS_1SEEEvT_(ptr noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) @_ZN13InitFromConstL1sE)
574 consume<const S&>(s);
576 // CHECK: call void @_ZN13InitFromConst7consumeIRKNS_1SEEEvT_(ptr noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) @_ZN13InitFromConstL1sE)
577 consume<const S&>(r);
579 // CHECK: call void @_ZN13InitFromConst7consumeIPKNS_1SEEEvT_(ptr noundef @_ZN13InitFromConstL1sE)
580 consume(p);
582 // CHECK: call void @_ZN13InitFromConst7consumeIMNS_1SEiEEvT_(i64 0)
583 consume(mp);
585 // CHECK: call void @_ZN13InitFromConst7consumeIPKiEEvT_(ptr noundef @_ZN13InitFromConstL1aE)
586 consume(a);
590 namespace Null {
591 decltype(nullptr) null();
592 // CHECK: call {{.*}} @_ZN4Null4nullEv(
593 int *p = null();
594 struct S {};
595 // CHECK: call {{.*}} @_ZN4Null4nullEv(
596 int S::*q = null();
599 namespace InlineStaticConstRef {
600 inline const int &fun() {
601 static const int &i = 10;
602 return i;
603 // CHECK: ret ptr @_ZGRZN20InlineStaticConstRef3funEvE1i_
605 const int &use = fun();
608 namespace ClassTemplateWithStaticDataMember {
609 template <typename T>
610 struct S {
611 static const int &a;
613 template <typename T>
614 const int &S<T>::a = 5;
615 const int &use = S<void>::a;
618 namespace ClassTemplateWithHiddenStaticDataMember {
619 template <typename T>
620 struct S {
621 __attribute__((visibility("hidden"))) static const int &a;
623 template <typename T>
624 const int &S<T>::a = 5;
625 const int &use = S<void>::a;
628 namespace ClassWithStaticConstexprDataMember {
629 struct X {
630 static constexpr const char &p = 'c';
633 // CHECK: @_ZGRN34ClassWithStaticConstexprDataMember1X1pE_
634 const char *f() { return &X::p; }
637 struct PR69979 {
638 const char (&d)[9];
639 } e {"12345678"};
641 // VirtualMembers::TemplateClass::templateMethod() must be defined in this TU,
642 // not just declared.
643 // CHECK: define linkonce_odr void @_ZN14VirtualMembers13TemplateClassIiE14templateMethodEv(ptr {{[^,]*}} %this)