const-init-cxx11.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. // RUN: not %clang_cc1 -verify -triple x86_64-apple-darwin -emit-llvm -o - %s -std=c++11 | FileCheck %s
  2. // FIXME: The padding in all these objects should be zero-initialized.
  3. namespace StructUnion {
  4. struct A {
  5. int n;
  6. double d;
  7. union U {
  8. constexpr U(int x) : x(x) {}
  9. constexpr U(const char *y) : y(y) {}
  10. int x;
  11. const char *y;
  12. } u;
  13. constexpr A(int n, double d, int x) : n(n), d(d), u(x) {}
  14. constexpr A(int n, double d, const char *y) : n(n), d(d), u(y) {}
  15. };
  16. // CHECK: @_ZN11StructUnion1aE = constant {{.*}} { i32 1, double 2.000000e+00, {{.*}} { i32 3, [4 x i8] undef } }
  17. extern constexpr A a(1, 2.0, 3);
  18. // CHECK: @_ZN11StructUnion1bE = constant {{.*}} { i32 4, double 5.000000e+00, {{.*}} { i8* getelementptr inbounds ([6 x i8]* @{{.*}}, i32 0, i32 0) } }
  19. extern constexpr A b(4, 5, "hello");
  20. struct B {
  21. int n;
  22. };
  23. // CHECK: @_ZN11StructUnion1cE = global {{.*}} zeroinitializer
  24. // CHECK: @_ZN11StructUnion2c2E = global {{.*}} zeroinitializer
  25. B c;
  26. B c2 = B();
  27. // CHECK: @_ZN11StructUnion1dE = global {{.*}} zeroinitializer
  28. B d[10];
  29. struct C {
  30. constexpr C() : c(0) {}
  31. int c;
  32. };
  33. // CHECK: @_ZN11StructUnion1eE = global {{.*}} zeroinitializer
  34. C e[10];
  35. struct D {
  36. constexpr D() : d(5) {}
  37. int d;
  38. };
  39. // CHECK: @_ZN11StructUnion1fE = global {{.*}} { i32 5 }
  40. D f;
  41. union E {
  42. int a;
  43. void *b = &f;
  44. };
  45. // CHECK: @_ZN11StructUnion1gE = global {{.*}} @_ZN11StructUnion1fE
  46. E g;
  47. // CHECK: @_ZN11StructUnion1hE = global {{.*}} @_ZN11StructUnion1fE
  48. E h = E();
  49. }
  50. namespace BaseClass {
  51. template<typename T, unsigned> struct X : T {};
  52. struct C { char c = 1; };
  53. template<unsigned... Ns> struct Cs : X<C,Ns>... {};
  54. struct N { int n = 3; };
  55. struct D { double d = 4.0; };
  56. template<typename ...Ts>
  57. struct Test : Ts... { constexpr Test() : Ts()..., n(5) {} int n; };
  58. using Test1 = Test<N, C, Cs<1,2>, D, X<C,1>>;
  59. // CHECK: @_ZN9BaseClass2t1E = constant {{.*}} { i32 3, i8 1, i8 1, i8 1, double 4.000000e+00, i8 1, i32 5 }, align 8
  60. extern constexpr Test1 t1 = Test1();
  61. struct DN : D, N {};
  62. struct DND : DN, X<D,0> {};
  63. struct DNN : DN, X<N,0> {};
  64. // CHECK: @_ZN9BaseClass3dndE = constant {{.*}} { double 4.000000e+00, i32 3, double 4.000000e+00 }
  65. extern constexpr DND dnd = DND();
  66. // Note, N subobject is laid out in DN subobject's tail padding.
  67. // CHECK: @_ZN9BaseClass3dnnE = constant {{.*}} { double 4.000000e+00, i32 3, i32 3 }
  68. extern constexpr DNN dnn = DNN();
  69. struct E {};
  70. struct Test2 : X<E,0>, X<E,1>, X<E,2>, X<E,3> {};
  71. // CHECK: @_ZN9BaseClass2t2E = constant {{.*}} undef
  72. extern constexpr Test2 t2 = Test2();
  73. struct __attribute((packed)) PackedD { double y = 2; };
  74. struct Test3 : C, PackedD { constexpr Test3() {} };
  75. // CHECK: @_ZN9BaseClass2t3E = constant <{ i8, double }> <{ i8 1, double 2.000000e+00 }>
  76. extern constexpr Test3 t3 = Test3();
  77. }
  78. namespace Array {
  79. // CHECK: @_ZN5Array3arrE = constant [2 x i32] [i32 4, i32 0]
  80. extern constexpr int arr[2] = { 4 };
  81. // 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"]
  82. extern constexpr char c[6][4] = { "foo", "a", { "bar" }, { 'x', 'y', 'z' }, { "b" }, '1', '2', '3' };
  83. // CHECK: @_ZN5Array2ucE = constant [4 x i8] c"foo\00"
  84. extern constexpr unsigned char uc[] = { "foo" };
  85. struct C { constexpr C() : n(5) {} int n, m = 3 * n + 1; };
  86. // CHECK: @_ZN5Array5ctorsE = constant [3 x {{.*}}] [{{.*}} { i32 5, i32 16 }, {{.*}} { i32 5, i32 16 }, {{.*}} { i32 5, i32 16 }]
  87. extern const C ctors[3];
  88. constexpr C ctors[3];
  89. // CHECK: @_ZN5Array1dE = constant {{.*}} { [2 x i32] [i32 1, i32 2], [3 x i32] [i32 3, i32 4, i32 5] }
  90. struct D { int n[2]; int m[3]; } extern constexpr d = { 1, 2, 3, 4, 5 };
  91. struct E {
  92. char c[4];
  93. char d[4];
  94. constexpr E() : c("foo"), d("x") {}
  95. };
  96. // CHECK: @_ZN5Array1eE = constant {{.*}} { [4 x i8] c"foo\00", [4 x i8] c"x\00\00\00" }
  97. extern constexpr E e = E();
  98. // PR13290
  99. struct F { constexpr F() : n(4) {} int n; };
  100. // CHECK: @_ZN5Array2f1E = global {{.*}} zeroinitializer
  101. F f1[1][1][0] = { };
  102. // CHECK: @_ZN5Array2f2E = global {{.* i32 4 .* i32 4 .* i32 4 .* i32 4 .* i32 4 .* i32 4 .* i32 4 .* i32 4}}
  103. F f2[2][2][2] = { };
  104. }
  105. namespace MemberPtr {
  106. struct B1 {
  107. int a, b;
  108. virtual void f();
  109. void g();
  110. };
  111. struct B2 {
  112. int c, d;
  113. virtual void h();
  114. void i();
  115. };
  116. struct C : B1 {
  117. int e;
  118. virtual void j();
  119. void k();
  120. };
  121. struct D : C, B2 {
  122. int z;
  123. virtual void l();
  124. void m();
  125. };
  126. // CHECK: @_ZN9MemberPtr2daE = constant i64 8
  127. // CHECK: @_ZN9MemberPtr2dbE = constant i64 12
  128. // CHECK: @_ZN9MemberPtr2dcE = constant i64 32
  129. // CHECK: @_ZN9MemberPtr2ddE = constant i64 36
  130. // CHECK: @_ZN9MemberPtr2deE = constant i64 16
  131. // CHECK: @_ZN9MemberPtr2dzE = constant i64 40
  132. extern constexpr int (D::*da) = &B1::a;
  133. extern constexpr int (D::*db) = &C::b;
  134. extern constexpr int (D::*dc) = &B2::c;
  135. extern constexpr int (D::*dd) = &D::d;
  136. extern constexpr int (D::*de) = &C::e;
  137. extern constexpr int (D::*dz) = &D::z;
  138. // CHECK: @_ZN9MemberPtr2baE = constant i64 8
  139. // CHECK: @_ZN9MemberPtr2bbE = constant i64 12
  140. // CHECK: @_ZN9MemberPtr2bcE = constant i64 8
  141. // CHECK: @_ZN9MemberPtr2bdE = constant i64 12
  142. // CHECK: @_ZN9MemberPtr2beE = constant i64 16
  143. // CHECK: @_ZN9MemberPtr3b1zE = constant i64 40
  144. // CHECK: @_ZN9MemberPtr3b2zE = constant i64 16
  145. extern constexpr int (B1::*ba) = (int(B1::*))&B1::a;
  146. extern constexpr int (B1::*bb) = (int(B1::*))&C::b;
  147. extern constexpr int (B2::*bc) = (int(B2::*))&B2::c;
  148. extern constexpr int (B2::*bd) = (int(B2::*))&D::d;
  149. extern constexpr int (B1::*be) = (int(B1::*))&C::e;
  150. extern constexpr int (B1::*b1z) = (int(B1::*))&D::z;
  151. extern constexpr int (B2::*b2z) = (int(B2::*))&D::z;
  152. // CHECK: @_ZN9MemberPtr2dfE = constant {{.*}} { i64 1, i64 0 }
  153. // CHECK: @_ZN9MemberPtr2dgE = constant {{.*}} { i64 {{.*}}2B11gEv{{.*}}, i64 0 }
  154. // CHECK: @_ZN9MemberPtr2dhE = constant {{.*}} { i64 1, i64 24 }
  155. // CHECK: @_ZN9MemberPtr2diE = constant {{.*}} { i64 {{.*}}2B21iEv{{.*}}, i64 24 }
  156. // CHECK: @_ZN9MemberPtr2djE = constant {{.*}} { i64 9, i64 0 }
  157. // CHECK: @_ZN9MemberPtr2dkE = constant {{.*}} { i64 {{.*}}1C1kEv{{.*}}, i64 0 }
  158. // CHECK: @_ZN9MemberPtr2dlE = constant {{.*}} { i64 17, i64 0 }
  159. // CHECK: @_ZN9MemberPtr2dmE = constant {{.*}} { i64 {{.*}}1D1mEv{{.*}}, i64 0 }
  160. extern constexpr void (D::*df)() = &C::f;
  161. extern constexpr void (D::*dg)() = &B1::g;
  162. extern constexpr void (D::*dh)() = &B2::h;
  163. extern constexpr void (D::*di)() = &D::i;
  164. extern constexpr void (D::*dj)() = &C::j;
  165. extern constexpr void (D::*dk)() = &C::k;
  166. extern constexpr void (D::*dl)() = &D::l;
  167. extern constexpr void (D::*dm)() = &D::m;
  168. // CHECK: @_ZN9MemberPtr2bfE = constant {{.*}} { i64 1, i64 0 }
  169. // CHECK: @_ZN9MemberPtr2bgE = constant {{.*}} { i64 {{.*}}2B11gEv{{.*}}, i64 0 }
  170. // CHECK: @_ZN9MemberPtr2bhE = constant {{.*}} { i64 1, i64 0 }
  171. // CHECK: @_ZN9MemberPtr2biE = constant {{.*}} { i64 {{.*}}2B21iEv{{.*}}, i64 0 }
  172. // CHECK: @_ZN9MemberPtr2bjE = constant {{.*}} { i64 9, i64 0 }
  173. // CHECK: @_ZN9MemberPtr2bkE = constant {{.*}} { i64 {{.*}}1C1kEv{{.*}}, i64 0 }
  174. // CHECK: @_ZN9MemberPtr3b1lE = constant {{.*}} { i64 17, i64 0 }
  175. // CHECK: @_ZN9MemberPtr3b1mE = constant {{.*}} { i64 {{.*}}1D1mEv{{.*}}, i64 0 }
  176. // CHECK: @_ZN9MemberPtr3b2lE = constant {{.*}} { i64 17, i64 -24 }
  177. // CHECK: @_ZN9MemberPtr3b2mE = constant {{.*}} { i64 {{.*}}1D1mEv{{.*}}, i64 -24 }
  178. extern constexpr void (B1::*bf)() = (void(B1::*)())&C::f;
  179. extern constexpr void (B1::*bg)() = (void(B1::*)())&B1::g;
  180. extern constexpr void (B2::*bh)() = (void(B2::*)())&B2::h;
  181. extern constexpr void (B2::*bi)() = (void(B2::*)())&D::i;
  182. extern constexpr void (B1::*bj)() = (void(B1::*)())&C::j;
  183. extern constexpr void (B1::*bk)() = (void(B1::*)())&C::k;
  184. extern constexpr void (B1::*b1l)() = (void(B1::*)())&D::l;
  185. extern constexpr void (B1::*b1m)() = (void(B1::*)())&D::m;
  186. extern constexpr void (B2::*b2l)() = (void(B2::*)())&D::l;
  187. extern constexpr void (B2::*b2m)() = (void(B2::*)())&D::m;
  188. }
  189. namespace LiteralReference {
  190. struct Lit {
  191. constexpr Lit() : n(5) {}
  192. int n;
  193. };
  194. // This creates a non-const temporary and binds a reference to it.
  195. // CHECK: @[[TEMP:.*]] = private global {{.*}} { i32 5 }, align 4
  196. // CHECK: @_ZN16LiteralReference3litE = constant {{.*}} @[[TEMP]], align 8
  197. const Lit &lit = Lit();
  198. // This creates a const temporary as part of the reference initialization.
  199. // CHECK: @[[TEMP:.*]] = private constant {{.*}} { i32 5 }, align 4
  200. // CHECK: @_ZN16LiteralReference4lit2E = constant {{.*}} @[[TEMP]], align 8
  201. const Lit &lit2 = {};
  202. struct A { int &&r1; const int &&r2; };
  203. struct B { A &&a1; const A &&a2; };
  204. B b = { { 0, 1 }, { 2, 3 } };
  205. // CHECK: @[[TEMP0:.*]] = private global i32 0, align 4
  206. // CHECK: @[[TEMP1:.*]] = private constant i32 1, align 4
  207. // CHECK: @[[TEMPA1:.*]] = private global {{.*}} { i32* @[[TEMP0]], i32* @[[TEMP1]] }, align 8
  208. // CHECK: @[[TEMP2:.*]] = private global i32 2, align 4
  209. // CHECK: @[[TEMP3:.*]] = private constant i32 3, align 4
  210. // CHECK: @[[TEMPA2:.*]] = private constant {{.*}} { i32* @[[TEMP2]], i32* @[[TEMP3]] }, align 8
  211. // CHECK: @_ZN16LiteralReference1bE = global {{.*}} { {{.*}}* @[[TEMPA1]], {{.*}}* @[[TEMPA2]] }, align 8
  212. struct Subobj {
  213. int a, b, c;
  214. };
  215. // CHECK: @[[TEMP:.*]] = private global {{.*}} { i32 1, i32 2, i32 3 }, align 4
  216. // CHECK: @_ZN16LiteralReference2soE = constant {{.*}} (i8* getelementptr {{.*}} @[[TEMP]]{{.*}}, i64 4)
  217. constexpr int &&so = Subobj{ 1, 2, 3 }.b;
  218. struct Dummy { int padding; };
  219. struct Derived : Dummy, Subobj {
  220. constexpr Derived() : Dummy{200}, Subobj{4, 5, 6} {}
  221. };
  222. using ConstDerived = const Derived;
  223. // CHECK: @[[TEMPCOMMA:.*]] = private constant {{.*}} { i32 200, i32 4, i32 5, i32 6 }
  224. // CHECK: @_ZN16LiteralReference5commaE = constant {{.*}} getelementptr {{.*}} @[[TEMPCOMMA]]{{.*}}, i64 8)
  225. constexpr const int &comma = (1, (2, ConstDerived{}).b);
  226. // CHECK: @[[TEMPDERIVED:.*]] = private global {{.*}} { i32 200, i32 4, i32 5, i32 6 }
  227. // CHECK: @_ZN16LiteralReference4baseE = constant {{.*}} getelementptr {{.*}} @[[TEMPDERIVED]]{{.*}}, i64 4)
  228. constexpr Subobj &&base = Derived{};
  229. // CHECK: @_ZN16LiteralReference7derivedE = constant {{.*}} @[[TEMPDERIVED]]
  230. constexpr Derived &derived = static_cast<Derived&>(base);
  231. }
  232. namespace NonLiteralConstexpr {
  233. constexpr int factorial(int n) {
  234. return n ? factorial(n-1) * n : 1;
  235. }
  236. extern void f(int *p);
  237. struct NonTrivialDtor {
  238. constexpr NonTrivialDtor() : n(factorial(5)), p(&n) {}
  239. ~NonTrivialDtor() {
  240. f(p);
  241. }
  242. int n;
  243. int *p;
  244. };
  245. static_assert(!__is_literal(NonTrivialDtor), "");
  246. // CHECK: @_ZN19NonLiteralConstexpr3ntdE = global {{.*}} { i32 120, i32* getelementptr
  247. NonTrivialDtor ntd;
  248. struct VolatileMember {
  249. constexpr VolatileMember() : n(5) {}
  250. volatile int n;
  251. };
  252. static_assert(!__is_literal(VolatileMember), "");
  253. // CHECK: @_ZN19NonLiteralConstexpr2vmE = global {{.*}} { i32 5 }
  254. VolatileMember vm;
  255. struct Both {
  256. constexpr Both() : n(10) {}
  257. ~Both();
  258. volatile int n;
  259. };
  260. // CHECK: @_ZN19NonLiteralConstexpr1bE = global {{.*}} { i32 10 }
  261. Both b;
  262. void StaticVars() {
  263. // CHECK: @_ZZN19NonLiteralConstexpr10StaticVarsEvE3ntd = {{.*}} { i32 120, i32* getelementptr {{.*}}
  264. // CHECK: @_ZGVZN19NonLiteralConstexpr10StaticVarsEvE3ntd =
  265. static NonTrivialDtor ntd;
  266. // CHECK: @_ZZN19NonLiteralConstexpr10StaticVarsEvE2vm = {{.*}} { i32 5 }
  267. // CHECK-NOT: @_ZGVZN19NonLiteralConstexpr10StaticVarsEvE2vm =
  268. static VolatileMember vm;
  269. // CHECK: @_ZZN19NonLiteralConstexpr10StaticVarsEvE1b = {{.*}} { i32 10 }
  270. // CHECK: @_ZGVZN19NonLiteralConstexpr10StaticVarsEvE1b =
  271. static Both b;
  272. }
  273. }
  274. // PR12067
  275. namespace VirtualMembers {
  276. struct A {
  277. constexpr A(double d) : d(d) {}
  278. virtual void f();
  279. double d;
  280. };
  281. struct B : A {
  282. constexpr B() : A(2.0), c{'h', 'e', 'l', 'l', 'o'} {}
  283. constexpr B(int n) : A(n), c{'w', 'o', 'r', 'l', 'd'} {}
  284. virtual void g();
  285. char c[5];
  286. };
  287. struct C {
  288. constexpr C() : n(64) {}
  289. int n;
  290. };
  291. struct D : C, A, B {
  292. constexpr D() : A(1.0), B(), s(5) {}
  293. short s;
  294. };
  295. struct E : D, B {
  296. constexpr E() : B(3), c{'b','y','e'} {}
  297. char c[3];
  298. };
  299. // CHECK: @_ZN14VirtualMembers1eE = global { i8**, double, i32, i8**, double, [5 x i8], i16, i8**, double, [5 x i8], [3 x i8] } { i8** getelementptr inbounds ([11 x i8*]* @_ZTVN14VirtualMembers1EE, i64 0, i64 2), double 1.000000e+00, i32 64, i8** getelementptr inbounds ([11 x i8*]* @_ZTVN14VirtualMembers1EE, i64 0, i64 5), double 2.000000e+00, [5 x i8] c"hello", i16 5, i8** getelementptr inbounds ([11 x i8*]* @_ZTVN14VirtualMembers1EE, i64 0, i64 9), double 3.000000e+00, [5 x i8] c"world", [3 x i8] c"bye" }
  300. E e;
  301. struct nsMemoryImpl {
  302. virtual void f();
  303. };
  304. // CHECK: @_ZN14VirtualMembersL13sGlobalMemoryE = internal global { i8** } { i8** getelementptr inbounds ([3 x i8*]* @_ZTVN14VirtualMembers12nsMemoryImplE, i64 0, i64 2) }
  305. static nsMemoryImpl sGlobalMemory;
  306. }
  307. namespace PR13273 {
  308. struct U {
  309. int t;
  310. U() = default;
  311. };
  312. struct S : U {
  313. S() = default;
  314. };
  315. // CHECK: @_ZN7PR132731sE = {{.*}} zeroinitializer
  316. extern const S s {};
  317. }
  318. namespace ArrayTemporary {
  319. struct A { const int (&x)[3]; };
  320. struct B { const A (&x)[2]; };
  321. // CHECK: @[[A1:_ZGRN14ArrayTemporary1bE.*]] = private constant [3 x i32] [i32 1, i32 2, i32 3]
  322. // CHECK: @[[A2:_ZGRN14ArrayTemporary1bE.*]] = private constant [3 x i32] [i32 4, i32 5, i32 6]
  323. // CHECK: @[[ARR:_ZGRN14ArrayTemporary1bE.*]] = private constant [2 x {{.*}}] [{{.*}} { [3 x i32]* @[[A1]] }, {{.*}} { [3 x i32]* @[[A2]] }]
  324. // CHECK: @[[B:_ZGRN14ArrayTemporary1bE.*]] = private global {{.*}} { [2 x {{.*}}]* @[[ARR]] }
  325. // CHECK: @_ZN14ArrayTemporary1bE = constant {{.*}}* @[[B]]
  326. B &&b = { { { { 1, 2, 3 } }, { { 4, 5, 6 } } } };
  327. }
  328. namespace UnemittedTemporaryDecl {
  329. constexpr int &&ref = 0;
  330. extern constexpr int &ref2 = ref;
  331. // CHECK: @_ZGRN22UnemittedTemporaryDecl3refE = private global i32 0
  332. // FIXME: This declaration should not be emitted -- it isn't odr-used.
  333. // CHECK: @_ZN22UnemittedTemporaryDecl3refE
  334. // CHECK: @_ZN22UnemittedTemporaryDecl4ref2E = constant i32* @_ZGRN22UnemittedTemporaryDecl3refE
  335. }
  336. // CHECK: @_ZZN12LocalVarInit3aggEvE1a = internal constant {{.*}} i32 101
  337. // CHECK: @_ZZN12LocalVarInit4ctorEvE1a = internal constant {{.*}} i32 102
  338. // CHECK: @_ZZN12LocalVarInit8mutable_EvE1a = private unnamed_addr constant {{.*}} i32 103
  339. // Constant initialization tests go before this point,
  340. // dynamic initialization tests go after.
  341. // We must emit a constant initializer for NonLiteralConstexpr::ntd, but also
  342. // emit an initializer to register its destructor.
  343. // CHECK: define {{.*}}cxx_global_var_init{{.*}}
  344. // CHECK-NOT: NonLiteralConstexpr
  345. // CHECK: call {{.*}}cxa_atexit{{.*}} @_ZN19NonLiteralConstexpr14NonTrivialDtorD1Ev {{.*}} @_ZN19NonLiteralConstexpr3ntdE
  346. // CHECK-NEXT: ret void
  347. // We don't need to emit any dynamic initialization for NonLiteralConstexpr::vm.
  348. // CHECK-NOT: NonLiteralConstexpr2vm
  349. // We must emit a constant initializer for NonLiteralConstexpr::b, but also
  350. // emit an initializer to register its destructor.
  351. // CHECK: define {{.*}}cxx_global_var_init{{.*}}
  352. // CHECK-NOT: NonLiteralConstexpr
  353. // CHECK: call {{.*}}cxa_atexit{{.*}} @_ZN19NonLiteralConstexpr4BothD1Ev {{.*}} @_ZN19NonLiteralConstexpr1bE
  354. // CHECK-NEXT: ret void
  355. // CHECK: define {{.*}}NonLiteralConstexpr10StaticVars
  356. // CHECK-NOT: }
  357. // CHECK: call {{.*}}cxa_atexit{{.*}}@_ZN19NonLiteralConstexpr14NonTrivialDtorD1Ev
  358. // CHECK-NOT: }
  359. // CHECK: call {{.*}}cxa_atexit{{.*}}@_ZN19NonLiteralConstexpr4BothD1Ev
  360. // PR12848: Don't emit dynamic initializers for local constexpr variables.
  361. namespace LocalVarInit {
  362. constexpr int f(int n) { return n; }
  363. struct Agg { int k; };
  364. struct Ctor { constexpr Ctor(int n) : k(n) {} int k; };
  365. struct Mutable { constexpr Mutable(int n) : k(n) {} mutable int k; };
  366. // CHECK: define {{.*}} @_ZN12LocalVarInit6scalarEv
  367. // CHECK-NOT: call
  368. // CHECK: store i32 100,
  369. // CHECK-NOT: call
  370. // CHECK: ret i32 100
  371. int scalar() { constexpr int a = { f(100) }; return a; }
  372. // CHECK: define {{.*}} @_ZN12LocalVarInit3aggEv
  373. // CHECK-NOT: call
  374. // CHECK: ret i32 101
  375. int agg() { constexpr Agg a = { f(101) }; return a.k; }
  376. // CHECK: define {{.*}} @_ZN12LocalVarInit4ctorEv
  377. // CHECK-NOT: call
  378. // CHECK: ret i32 102
  379. int ctor() { constexpr Ctor a = { f(102) }; return a.k; }
  380. // CHECK: define {{.*}} @_ZN12LocalVarInit8mutable_Ev
  381. // CHECK-NOT: call
  382. // CHECK: call {{.*}}memcpy{{.*}} @_ZZN12LocalVarInit8mutable_EvE1a
  383. // CHECK-NOT: call
  384. // Can't fold return value due to 'mutable'.
  385. // CHECK-NOT: ret i32 103
  386. // CHECK: }
  387. int mutable_() { constexpr Mutable a = { f(103) }; return a.k; }
  388. }
  389. namespace CrossFuncLabelDiff {
  390. // Make sure we refuse to constant-fold the variable b.
  391. constexpr long a(bool x) { return x ? 0 : (long)&&lbl + (0 && ({lbl: 0;})); }
  392. void test() { static long b = (long)&&lbl - a(false); lbl: return; }
  393. // CHECK: sub nsw i64 ptrtoint (i8* blockaddress(@_ZN18CrossFuncLabelDiff4testEv, {{.*}}) to i64),
  394. // CHECK: store i64 {{.*}}, i64* @_ZZN18CrossFuncLabelDiff4testEvE1b, align 8
  395. }
  396. // PR12012
  397. namespace VirtualBase {
  398. struct B {};
  399. struct D : virtual B {};
  400. D d;
  401. // CHECK: call {{.*}}@_ZN11VirtualBase1DC1Ev
  402. template<typename T> struct X : T {
  403. constexpr X() : T() {}
  404. };
  405. X<D> x;
  406. // CHECK: call {{.*}}@_ZN11VirtualBase1XINS_1DEEC1Ev
  407. }
  408. // PR12145
  409. namespace Unreferenced {
  410. int n;
  411. constexpr int *p = &n;
  412. // We must not emit a load of 'p' here, since it's not odr-used.
  413. int q = *p;
  414. // CHECK-NOT: _ZN12Unreferenced1pE
  415. // CHECK: = load i32* @_ZN12Unreferenced1nE
  416. // CHECK-NEXT: store i32 {{.*}}, i32* @_ZN12Unreferenced1qE
  417. // CHECK-NOT: _ZN12Unreferenced1pE
  418. // Technically, we are not required to substitute variables of reference types
  419. // initialized by constant expressions, because the special case for odr-use
  420. // of variables in [basic.def.odr]p2 only applies to objects. But we do so
  421. // anyway.
  422. constexpr int &r = n;
  423. // CHECK-NOT: _ZN12Unreferenced1rE
  424. int s = r;
  425. const int t = 1;
  426. const int &rt = t;
  427. int f(int);
  428. int u = f(rt);
  429. // CHECK: call i32 @_ZN12Unreferenced1fEi(i32 1)
  430. }
  431. namespace InitFromConst {
  432. template<typename T> void consume(T);
  433. const bool b = true;
  434. const int n = 5;
  435. constexpr double d = 4.3;
  436. struct S { int n = 7; S *p = 0; };
  437. constexpr S s = S();
  438. const S &r = s;
  439. constexpr const S *p = &r;
  440. constexpr int S::*mp = &S::n;
  441. constexpr int a[3] = { 1, 4, 9 };
  442. void test() {
  443. // CHECK: call void @_ZN13InitFromConst7consumeIbEEvT_(i1 zeroext true)
  444. consume(b);
  445. // CHECK: call void @_ZN13InitFromConst7consumeIiEEvT_(i32 5)
  446. consume(n);
  447. // CHECK: call void @_ZN13InitFromConst7consumeIdEEvT_(double 4.300000e+00)
  448. consume(d);
  449. // CHECK: call void @_ZN13InitFromConst7consumeIRKNS_1SEEEvT_(%"struct.InitFromConst::S"* @_ZN13InitFromConstL1sE)
  450. consume<const S&>(s);
  451. // CHECK: call void @_ZN13InitFromConst7consumeIRKNS_1SEEEvT_(%"struct.InitFromConst::S"* @_ZN13InitFromConstL1sE)
  452. consume<const S&>(r);
  453. // CHECK: call void @_ZN13InitFromConst7consumeIPKNS_1SEEEvT_(%"struct.InitFromConst::S"* @_ZN13InitFromConstL1sE)
  454. consume(p);
  455. // CHECK: call void @_ZN13InitFromConst7consumeIMNS_1SEiEEvT_(i64 0)
  456. consume(mp);
  457. // CHECK: call void @_ZN13InitFromConst7consumeIPKiEEvT_(i32* getelementptr inbounds ([3 x i32]* @_ZN13InitFromConstL1aE, i32 0, i32 0))
  458. consume(a);
  459. }
  460. }
  461. namespace Null {
  462. decltype(nullptr) null();
  463. // CHECK: call {{.*}} @_ZN4Null4nullEv(
  464. int *p = null();
  465. struct S {};
  466. // CHECK: call {{.*}} @_ZN4Null4nullEv(
  467. int S::*q = null();
  468. }