653 lines · cpp
1// RUN: %clang_cc1 -w -fmerge-all-constants -triple x86_64-elf-gnu -emit-llvm -o - %s -std=c++11 | FileCheck %s2// RUN: %clang_cc1 -w -fmerge-all-constants -triple x86_64-elf-gnu -emit-llvm -o - %s -std=c++20 | FileCheck -check-prefix=CHECK20 %s3 4// FIXME: The padding in all these objects should be zero-initialized.5namespace 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;15 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) {}18 };19 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);22 23 // CHECK: @_ZN11StructUnion1bE ={{.*}} constant {{.*}} { i32 4, double 5.000000e+00, {{.*}} { ptr @{{.*}} } }24 extern constexpr A b(4, 5, "hello");25 26 struct B {27 int n;28 };29 30 // CHECK: @_ZN11StructUnion1cE ={{.*}} global {{.*}} zeroinitializer31 // CHECK: @_ZN11StructUnion2c2E ={{.*}} global {{.*}} zeroinitializer32 B c;33 B c2 = B();34 35 // CHECK: @_ZN11StructUnion1dE ={{.*}} global {{.*}} zeroinitializer36 B d[10];37 38 struct C {39 constexpr C() : c(0) {}40 int c;41 };42 43 // CHECK: @_ZN11StructUnion1eE ={{.*}} global {{.*}} zeroinitializer44 C e[10];45 46 struct D {47 constexpr D() : d(5) {}48 int d;49 };50 51 // CHECK: @_ZN11StructUnion1fE ={{.*}} global {{.*}} { i32 5 }52 D f;53 54 union E {55 int a;56 void *b = &f;57 };58 59 // CHECK: @_ZN11StructUnion1gE ={{.*}} global {{.*}} @_ZN11StructUnion1fE60 E g;61 62 // CHECK: @_ZN11StructUnion1hE ={{.*}} global {{.*}} @_ZN11StructUnion1fE63 E h = E();64}65 66namespace 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; };72 73 template<typename ...Ts>74 struct Test : Ts... { constexpr Test() : Ts()..., n(5) {} int n; };75 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 878 extern constexpr Test1 t1 = Test1();79 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();88 89 struct E {};90 struct Test2 : X<E,0>, X<E,1>, X<E,2>, X<E,3> {};91 // CHECK: @_ZN9BaseClass2t2E ={{.*}} constant {{.*}} zeroinitializer, align 192 extern constexpr Test2 t2 = Test2();93 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();98}99 100namespace Array {101 // CHECK: @_ZN5Array3arrE ={{.*}} constant [2 x i32] [i32 4, i32 0]102 extern constexpr int arr[2] = { 4 };103 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' };106 107 // CHECK: @_ZN5Array2ucE ={{.*}} constant [4 x i8] c"foo\00"108 extern constexpr unsigned char uc[] = { "foo" };109 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];114 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 };117 118 struct E {119 char c[4];120 char d[4];121 constexpr E() : c("foo"), d("x") {}122 };123 // CHECK: @_ZN5Array1eE ={{.*}} constant {{.*}} { [4 x i8] c"foo\00", [4 x i8] c"x\00\00\00" }124 extern constexpr E e = E();125 126 // PR13290127 struct F { constexpr F() : n(4) {} int n; };128 // CHECK: @_ZN5Array2f1E ={{.*}} global {{.*}} zeroinitializer129 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] = { };132}133 134namespace MemberPtr {135 struct B1 {136 int a, b;137 virtual void f();138 void g();139 };140 struct B2 {141 int c, d;142 virtual void h();143 void i();144 };145 struct C : B1 {146 int e;147 virtual void j();148 void k();149 };150 struct D : C, B2 {151 int z;152 virtual void l();153 void m();154 };155 156 // CHECK: @_ZN9MemberPtr2daE ={{.*}} constant i64 8157 // CHECK: @_ZN9MemberPtr2dbE ={{.*}} constant i64 12158 // CHECK: @_ZN9MemberPtr2dcE ={{.*}} constant i64 32159 // CHECK: @_ZN9MemberPtr2ddE ={{.*}} constant i64 36160 // CHECK: @_ZN9MemberPtr2deE ={{.*}} constant i64 16161 // CHECK: @_ZN9MemberPtr2dzE ={{.*}} constant i64 40162 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;168 169 // CHECK: @_ZN9MemberPtr2baE ={{.*}} constant i64 8170 // CHECK: @_ZN9MemberPtr2bbE ={{.*}} constant i64 12171 // CHECK: @_ZN9MemberPtr2bcE ={{.*}} constant i64 8172 // CHECK: @_ZN9MemberPtr2bdE ={{.*}} constant i64 12173 // CHECK: @_ZN9MemberPtr2beE ={{.*}} constant i64 16174 // CHECK: @_ZN9MemberPtr3b1zE ={{.*}} constant i64 40175 // CHECK: @_ZN9MemberPtr3b2zE ={{.*}} constant i64 16176 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;183 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;200 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;221}222 223namespace LiteralReference {224 struct Lit {225 constexpr Lit() : n(5) {}226 int n;227 };228 229 // This creates a non-const temporary and binds a reference to it.230 // CHECK: @[[TEMP:.*]] = internal global {{.*}} { i32 5 }, align 4231 // CHECK: @_ZN16LiteralReference3litE ={{.*}} constant {{.*}} @[[TEMP]], align 8232 const Lit &lit = Lit();233 234 // This creates a const temporary as part of the reference initialization.235 // CHECK: @[[TEMP:.*]] = internal constant {{.*}} { i32 5 }, align 4236 // CHECK: @_ZN16LiteralReference4lit2E ={{.*}} constant {{.*}} @[[TEMP]], align 8237 const Lit &lit2 = {};238 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 4243 // CHECK: @[[TEMP1:.*]] = internal constant i32 1, align 4244 // CHECK: @[[TEMPA1:.*]] = internal global {{.*}} { ptr @[[TEMP0]], ptr @[[TEMP1]] }, align 8245 // CHECK: @[[TEMP2:.*]] = internal global i32 2, align 4246 // CHECK: @[[TEMP3:.*]] = internal constant i32 3, align 4247 // CHECK: @[[TEMPA2:.*]] = internal constant {{.*}} { ptr @[[TEMP2]], ptr @[[TEMP3]] }, align 8248 // CHECK: @_ZN16LiteralReference1bE ={{.*}} global {{.*}} { ptr @[[TEMPA1]], ptr @[[TEMPA2]] }, align 8249 250 struct Subobj {251 int a, b, c;252 };253 // CHECK: @[[TEMP:.*]] = internal global {{.*}} { i32 1, i32 2, i32 3 }, align 4254 // CHECK: @_ZN16LiteralReference2soE ={{.*}} constant ptr getelementptr (i8, ptr @[[TEMP]], i64 4)255 constexpr int &&so = Subobj{ 1, 2, 3 }.b;256 257 struct Dummy { int padding; };258 struct Derived : Dummy, Subobj {259 constexpr Derived() : Dummy{200}, Subobj{4, 5, 6} {}260 };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);265 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{};269 270 // CHECK: @_ZN16LiteralReference7derivedE ={{.*}} constant {{.*}} @[[TEMPDERIVED]]271 constexpr Derived &derived = static_cast<Derived&>(base);272}273 274namespace NonLiteralConstexpr {275 constexpr int factorial(int n) {276 return n ? factorial(n-1) * n : 1;277 }278 extern void f(int *p);279 280 struct NonTrivialDtor {281 constexpr NonTrivialDtor() : n(factorial(5)), p(&n) {}282 ~NonTrivialDtor() {283 f(p);284 }285 286 int n;287 int *p;288 };289 static_assert(!__is_literal(NonTrivialDtor), "");290 // CHECK: @_ZN19NonLiteralConstexpr3ntdE ={{.*}} global {{.*}} { i32 120, ptr @291 NonTrivialDtor ntd;292 293 struct VolatileMember {294 constexpr VolatileMember() : n(5) {}295 volatile int n;296 };297 static_assert(!__is_literal(VolatileMember), "");298 // CHECK: @_ZN19NonLiteralConstexpr2vmE ={{.*}} global {{.*}} { i32 5 }299 VolatileMember vm;300 301 struct Both {302 constexpr Both() : n(10) {}303 ~Both();304 volatile int n;305 };306 // CHECK: @_ZN19NonLiteralConstexpr1bE ={{.*}} global {{.*}} { i32 10 }307 Both b;308 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;319 }320}321 322// PR12067323namespace VirtualMembers {324 struct A {325 constexpr A(double d) : d(d) {}326 virtual void f();327 double d;328 };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];334 };335 struct C {336 constexpr C() : n(64) {}337 int n;338 };339 struct D : C, A, B {340 constexpr D() : A(1.0), B(), s(5) {}341 short s;342 };343 struct E : D, B {344 constexpr E() : B(3), c{'b','y','e'} {}345 char c[3];346 };347 // CHECK: @_ZN14VirtualMembers1eE ={{.*}} global { ptr, double, i32, ptr, double, [5 x i8], i16, ptr, double, [5 x i8], [3 x i8] } { ptr getelementptr inbounds inrange(-16, 8) ({ [3 x ptr], [4 x ptr], [4 x ptr] }, ptr @_ZTVN14VirtualMembers1EE, i32 0, i32 0, i32 2), double 1.000000e+00, i32 64, ptr getelementptr inbounds inrange(-16, 16) ({ [3 x ptr], [4 x ptr], [4 x ptr] }, ptr @_ZTVN14VirtualMembers1EE, i32 0, i32 1, i32 2), double 2.000000e+00, [5 x i8] c"hello", i16 5, ptr getelementptr inbounds inrange(-16, 16) ({ [3 x ptr], [4 x ptr], [4 x ptr] }, ptr @_ZTVN14VirtualMembers1EE, i32 0, i32 2, i32 2), double 3.000000e+00, [5 x i8] c"world", [3 x i8] c"bye" }348 E e;349 350 struct nsMemoryImpl {351 virtual void f();352 };353 // CHECK: @_ZN14VirtualMembersL13sGlobalMemoryE = internal global %"struct.VirtualMembers::nsMemoryImpl" { ptr getelementptr inbounds inrange(-16, 8) ({ [3 x ptr] }, ptr @_ZTVN14VirtualMembers12nsMemoryImplE, i32 0, i32 0, i32 2) }354 __attribute__((used))355 static nsMemoryImpl sGlobalMemory;356 357 template<class T>358 struct TemplateClass {359 constexpr TemplateClass() : t{42} {}360 virtual void templateMethod() {}361 362 T t;363 };364 // CHECK: @_ZN14VirtualMembers1tE ={{.*}} global { ptr, i32 } { ptr getelementptr inbounds inrange(-16, 8) ({ [3 x ptr] }, ptr @_ZTVN14VirtualMembers13TemplateClassIiEE, i32 0, i32 0, i32 2), i32 42 }365 TemplateClass<int> t;366}367 368namespace PR13273 {369 struct U {370 int t;371 U() = default;372 };373 374 struct S : U {375 S() = default;376 };377 378 // CHECK: @_ZN7PR132731sE = {{.*}} zeroinitializer379 extern const S s {};380}381 382namespace 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 } } } };391}392 393namespace UnemittedTemporaryDecl {394 constexpr int &&ref = 0;395 extern constexpr int &ref2 = ref;396 // CHECK: @_ZGRN22UnemittedTemporaryDecl3refE_ = internal global i32 0397 398 // FIXME: This declaration should not be emitted -- it isn't odr-used.399 // CHECK: @_ZN22UnemittedTemporaryDecl3refE400 401 // CHECK: @_ZN22UnemittedTemporaryDecl4ref2E ={{.*}} constant ptr @_ZGRN22UnemittedTemporaryDecl3refE_402}403 404namespace DR2126 {405 struct A { int &&b; };406 constexpr const A &a = {42};407 // CHECK: @_ZGRN6DR21261aE0_ = internal global i32 42408 // 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;412 413 // Dynamically initialized: the temporary object bound to 'b' could be414 // modified (eg, by placement 'new') before the initializer of 's' runs.415 constexpr A &&b = {42};416 // CHECK: @_ZN6DR21261sE ={{.*}} global ptr null417 int &s = b.b;418}419 420// CHECK: @_ZZN12LocalVarInit3aggEvE1a = internal constant {{.*}} i32 101421// CHECK: @_ZZN12LocalVarInit4ctorEvE1a = internal constant {{.*}} i32 102422// CHECK: @__const._ZN12LocalVarInit8mutable_Ev.a = private unnamed_addr constant {{.*}} i32 103423// CHECK: @_ZGRN33ClassTemplateWithStaticDataMember1SIvE1aE_ = linkonce_odr constant i32 5, comdat424// CHECK: @_ZN33ClassTemplateWithStaticDataMember3useE ={{.*}} constant ptr @_ZGRN33ClassTemplateWithStaticDataMember1SIvE1aE_425// CHECK: @_ZGRN39ClassTemplateWithHiddenStaticDataMember1SIvE1aE_ = linkonce_odr hidden constant i32 5, comdat426// 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, comdat430// CHECK20: @_ZZN12LocalVarInit4dtorEvE1a = internal constant {{.*}} i32 103431 432// Constant initialization tests go before this point,433// dynamic initialization tests go after.434 435// We must emit a constant initializer for NonLiteralConstexpr::ntd, but also436// emit an initializer to register its destructor.437// CHECK: define {{.*}}cxx_global_var_init{{.*}}438// CHECK-NOT: NonLiteralConstexpr439// CHECK: call {{.*}}cxa_atexit(ptr @_ZN19NonLiteralConstexpr14NonTrivialDtorD1Ev, ptr @_ZN19NonLiteralConstexpr3ntdE440// CHECK-NEXT: ret void441 442// We don't need to emit any dynamic initialization for NonLiteralConstexpr::vm.443// CHECK-NOT: NonLiteralConstexpr2vm444 445// We must emit a constant initializer for NonLiteralConstexpr::b, but also446// emit an initializer to register its destructor.447// CHECK: define {{.*}}cxx_global_var_init{{.*}}448// CHECK-NOT: NonLiteralConstexpr449// CHECK: call {{.*}}cxa_atexit(ptr @_ZN19NonLiteralConstexpr4BothD1Ev, ptr @_ZN19NonLiteralConstexpr1bE450// CHECK-NEXT: ret void451 452// CHECK: define {{.*}}NonLiteralConstexpr10StaticVars453// CHECK-NOT: }454// CHECK: call {{.*}}cxa_atexit{{.*}}@_ZN19NonLiteralConstexpr14NonTrivialDtorD1Ev455// CHECK-NOT: }456// CHECK: call {{.*}}cxa_atexit{{.*}}@_ZN19NonLiteralConstexpr4BothD1Ev457 458// PR12848: Don't emit dynamic initializers for local constexpr variables.459namespace 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; };464 465 // CHECK: define {{.*}} @_ZN12LocalVarInit6scalarEv466 // CHECK-NOT: call467 // CHECK: store i32 100,468 // CHECK-NOT: call469 // CHECK: ret i32 100470 int scalar() { constexpr int a = { f(100) }; return a; }471 472 // CHECK: define {{.*}} @_ZN12LocalVarInit3aggEv473 // CHECK-NOT: call474 // CHECK: ret i32 101475 int agg() { constexpr Agg a = { f(101) }; return a.k; }476 477 // CHECK: define {{.*}} @_ZN12LocalVarInit4ctorEv478 // CHECK-NOT: call479 // CHECK: ret i32 102480 int ctor() { constexpr Ctor a = { f(102) }; return a.k; }481 482 // CHECK: define {{.*}} @_ZN12LocalVarInit8mutable_Ev483 // CHECK-NOT: call484 // CHECK: call {{.*}}memcpy{{.*}} @__const._ZN12LocalVarInit8mutable_Ev.a485 // CHECK-NOT: call486 // Can't fold return value due to 'mutable'.487 // CHECK-NOT: ret i32 103488 // CHECK: }489 int mutable_() { constexpr Mutable a = { f(103) }; return a.k; }490 491#if __cplusplus >= 202002L492 // CHECK20: define {{.*}} @_ZN12LocalVarInit4dtorEv493 // CHECK20-NOT: call494 // CHECK20: ret i32 103495 struct Dtor { constexpr Dtor(int n) : k(n) {} constexpr ~Dtor() {} int k; };496 int dtor() { constexpr Dtor a = { f(103) }; return a.k; }497#endif498}499 500namespace 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 8506}507 508// PR12012509namespace VirtualBase {510 struct B {};511 struct D : virtual B {};512 D d;513 // CHECK: call {{.*}}@_ZN11VirtualBase1DC1Ev514 515 template<typename T> struct X : T {516 constexpr X() : T() {}517 };518 X<D> x;519 // CHECK: call {{.*}}@_ZN11VirtualBase1XINS_1DEEC1Ev520}521 522// PR12145523namespace 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: _ZN12Unreferenced1pE529 // CHECK: = load i32, ptr @_ZN12Unreferenced1nE530 // CHECK-NEXT: store i32 {{.*}}, ptr @_ZN12Unreferenced1qE531 // CHECK-NOT: _ZN12Unreferenced1pE532 533 // Technically, we are not required to substitute variables of reference types534 // initialized by constant expressions, because the special case for odr-use535 // of variables in [basic.def.odr]p2 only applies to objects. But we do so536 // anyway.537 538 constexpr int &r = n;539 // CHECK-NOT: _ZN12Unreferenced1rE540 int s = r;541 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)547}548 549namespace InitFromConst {550 template<typename T> void consume(T);551 552 const bool b = true;553 const int n = 5;554 constexpr double d = 4.3;555 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 };562 563 void test() {564 // CHECK: call void @_ZN13InitFromConst7consumeIbEEvT_(i1 noundef zeroext true)565 consume(b);566 567 // CHECK: call void @_ZN13InitFromConst7consumeIiEEvT_(i32 noundef 5)568 consume(n);569 570 // CHECK: call void @_ZN13InitFromConst7consumeIdEEvT_(double noundef 4.300000e+00)571 consume(d);572 573 // CHECK: call void @_ZN13InitFromConst7consumeIRKNS_1SEEEvT_(ptr noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) @_ZN13InitFromConstL1sE)574 consume<const S&>(s);575 576 // CHECK: call void @_ZN13InitFromConst7consumeIRKNS_1SEEEvT_(ptr noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) @_ZN13InitFromConstL1sE)577 consume<const S&>(r);578 579 // CHECK: call void @_ZN13InitFromConst7consumeIPKNS_1SEEEvT_(ptr noundef @_ZN13InitFromConstL1sE)580 consume(p);581 582 // CHECK: call void @_ZN13InitFromConst7consumeIMNS_1SEiEEvT_(i64 0)583 consume(mp);584 585 // CHECK: call void @_ZN13InitFromConst7consumeIPKiEEvT_(ptr noundef @_ZN13InitFromConstL1aE)586 consume(a);587 }588}589 590namespace 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();597}598 599namespace InlineStaticConstRef {600 inline const int &fun() {601 static const int &i = 10;602 return i;603 // CHECK: ret ptr @_ZGRZN20InlineStaticConstRef3funEvE1i_604 }605 const int &use = fun();606}607 608namespace ClassTemplateWithStaticDataMember {609 template <typename T>610 struct S {611 static const int &a;612 };613 template <typename T>614 const int &S<T>::a = 5;615 const int &use = S<void>::a;616}617 618namespace ClassTemplateWithHiddenStaticDataMember {619 template <typename T>620 struct S {621 __attribute__((visibility("hidden"))) static const int &a;622 };623 template <typename T>624 const int &S<T>::a = 5;625 const int &use = S<void>::a;626}627 628namespace ClassWithStaticConstexprDataMember {629struct X {630 static constexpr const char &p = 'c';631};632 633// CHECK: @_ZGRN34ClassWithStaticConstexprDataMember1X1pE_634const char *f() { return &X::p; }635}636 637struct PR69979 {638 const char (&d)[9];639} e {"12345678"};640 641namespace GH147949 {642 struct Coordinate {};643 Coordinate Make();644 void TestBody() {645 // CHECK: call {{.*}} @_ZN8GH1479494MakeEv646 const Coordinate x{Make()};647 }648}649 650// VirtualMembers::TemplateClass::templateMethod() must be defined in this TU,651// not just declared.652// CHECK: define linkonce_odr void @_ZN14VirtualMembers13TemplateClassIiE14templateMethodEv(ptr {{[^,]*}} %this)653