1 // RUN: %clang_cc1 -std=c++11 -S -emit-llvm -o - %s -triple x86_64-linux-gnu | FileCheck %s
2 // RUN: %clang_cc1 -std=c++17 -S -emit-llvm -o - %s -triple x86_64-linux-gnu | FileCheck %s
3 
4 struct A { int a, b; int f(); };
5 
6 namespace NonAggregateCopyInAggregateInit { // PR32044
ANonAggregateCopyInAggregateInit::A7   struct A { constexpr A(int n) : x(n), y() {} int x, y; } extern a;
8   // CHECK-DAG: @_ZN31NonAggregateCopyInAggregateInit1bE ={{.*}} global %{{.*}} { %[[A:.*]]* @_ZN31NonAggregateCopyInAggregateInit1aE }
9   struct B { A &p; } b{{a}};
10   // CHECK-DAG: @_ZGRN31NonAggregateCopyInAggregateInit1cE_ = internal global %[[A]] { i32 1, i32 0 }
11   // CHECK-DAG: @_ZN31NonAggregateCopyInAggregateInit1cE ={{.*}} global %{{.*}} { %{{.*}}* @_ZGRN31NonAggregateCopyInAggregateInit1cE_ }
12   struct C { A &&p; } c{{1}};
13 }
14 
15 namespace NearlyZeroInit {
16   // CHECK-DAG: @_ZN14NearlyZeroInit1aE ={{.*}} global {{.*}} <{ i32 1, i32 2, i32 3, [120 x i32] zeroinitializer }>
17   int a[123] = {1, 2, 3};
18   // CHECK-DAG: @_ZN14NearlyZeroInit1bE ={{.*}} global {{.*}} { i32 1, <{ i32, [2147483647 x i32] }> <{ i32 2, [2147483647 x i32] zeroinitializer }> }
19   struct B { int n; int arr[1024 * 1024 * 1024 * 2u]; } b = {1, {2}};
20 }
21 
22 namespace PR37560 {
23   union U {
24     char x;
25     int a;
26   };
27   // FIXME: [dcl.init]p2, the padding bits of the union object should be
28   // initialized to 0, not undef, which would allow us to collapse the tail
29   // of these arrays to zeroinitializer.
30   // CHECK-DAG: @_ZN7PR375601cE ={{.*}} global <{ { i8, [3 x i8] } }> <{ { i8, [3 x i8] } { i8 0, [3 x i8] undef } }>
31   U c[1] = {};
32   // CHECK-DAG: @_ZN7PR375601dE ={{.*}} global {{.*}} <{ { i8, [3 x i8] } { i8 97, [3 x i8] undef }, %"{{[^"]*}}" { i32 123 }, { i8, [3 x i8] } { i8 98, [3 x i8] undef }, { i8, [3 x i8] } { i8 0, [3 x i8] undef },
33   U d[16] = {'a', {.a = 123}, 'b'};
34   // CHECK-DAG: @_ZN7PR375601eE ={{.*}} global {{.*}} <{ %"{{[^"]*}}" { i32 123 }, %"{{[^"]*}}" { i32 456 }, { i8, [3 x i8] } { i8 0, [3 x i8] undef },
35   U e[16] = {{.a = 123}, {.a = 456}};
36 
37   union V {
38     int a;
39     char x;
40   };
41   // CHECK-DAG: @_ZN7PR375601fE ={{.*}} global [1 x %"{{[^"]*}}"] zeroinitializer
42   V f[1] = {};
43   // CHECK-DAG: @_ZN7PR375601gE ={{.*}} global {{.*}} <{ { i8, [3 x i8] } { i8 97, [3 x i8] undef }, %"{{[^"]*}}" { i32 123 }, { i8, [3 x i8] } { i8 98, [3 x i8] undef }, [13 x %"{{[^"]*}}"] zeroinitializer }>
44   V g[16] = {{.x = 'a'}, {.a = 123}, {.x = 'b'}};
45   // CHECK-DAG: @_ZN7PR375601hE ={{.*}} global {{.*}} <{ %"{{[^"]*}}" { i32 123 }, %"{{[^"]*}}" { i32 456 }, [14 x %"{{[^"]*}}"] zeroinitializer }>
46   V h[16] = {{.a = 123}, {.a = 456}};
47   // CHECK-DAG: @_ZN7PR375601iE ={{.*}} global [4 x %"{{[^"]*}}"] [%"{{[^"]*}}" { i32 123 }, %"{{[^"]*}}" { i32 456 }, %"{{[^"]*}}" zeroinitializer, %"{{[^"]*}}" zeroinitializer]
48   V i[4] = {{.a = 123}, {.a = 456}};
49 }
50 
51 // CHECK-LABEL: define {{.*}}@_Z3fn1i(
fn1(int x)52 int fn1(int x) {
53   // CHECK: %[[INITLIST:.*]] = alloca %struct.A
54   // CHECK: %[[A:.*]] = getelementptr inbounds %struct.A, %struct.A* %[[INITLIST]], i32 0, i32 0
55   // CHECK: store i32 %{{.*}}, i32* %[[A]], align 4
56   // CHECK: %[[B:.*]] = getelementptr inbounds %struct.A, %struct.A* %[[INITLIST]], i32 0, i32 1
57   // CHECK: store i32 5, i32* %[[B]], align 4
58   // CHECK: call i32 @_ZN1A1fEv(%struct.A* {{[^,]*}} %[[INITLIST]])
59   return A{x, 5}.f();
60 }
61 
fB62 struct B { int &r; int &f() { return r; } };
63 
64 // CHECK-LABEL: define {{.*}}@_Z3fn2Ri(
fn2(int & v)65 int &fn2(int &v) {
66   // CHECK: %[[INITLIST2:.*]] = alloca %struct.B, align 8
67   // CHECK: %[[R:.*]] = getelementptr inbounds %struct.B, %struct.B* %[[INITLIST2:.*]], i32 0, i32 0
68   // CHECK: store i32* %{{.*}}, i32** %[[R]], align 8
69   // CHECK: call nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) i32* @_ZN1B1fEv(%struct.B* {{[^,]*}} %[[INITLIST2:.*]])
70   return B{v}.f();
71 }
72 
73 // CHECK-LABEL: define {{.*}}@__cxx_global_var_init(
74 //
75 // CHECK: call {{.*}}@_ZN14NonTrivialInit1AC1Ev(
76 // CHECK: getelementptr inbounds {{.*}}, i64 1
77 // CHECK: br i1
78 //
79 // CHECK: getelementptr inbounds {{.*}}, i64 1
80 // CHECK: icmp eq {{.*}}, i64 30
81 // CHECK: br i1
82 //
83 // CHECK: call i32 @__cxa_atexit(
84 namespace NonTrivialInit {
85   struct A { A(); A(const A&) = delete; ~A(); };
86   struct B { A a[20]; };
87   // NB, this must be large enough to be worth memsetting for this test to be
88   // meaningful.
89   B b[30] = {};
90 }
91 
92 namespace ZeroInit {
93   enum { Zero, One };
zero()94   constexpr int zero() { return 0; }
null()95   constexpr int *null() { return nullptr; }
96   struct Filler {
97     int x;
98     Filler();
99   };
100   struct S1 {
101     int x;
102   };
103 
104   // These declarations, if implemented elementwise, require huge
105   // amout of memory and compiler time.
106   unsigned char data_1[1024 * 1024 * 1024 * 2u] = { 0 };
107   unsigned char data_2[1024 * 1024 * 1024 * 2u] = { Zero };
108   unsigned char data_3[1024][1024][1024] = {{{0}}};
109   unsigned char data_4[1024 * 1024 * 1024 * 2u] = { zero() };
110   int *data_5[1024 * 1024 * 512] = { nullptr };
111   int *data_6[1024 * 1024 * 512] = { null() };
112   struct S1 data_7[1024 * 1024 * 512] = {{0}};
113   char data_8[1000 * 1000 * 1000] = {};
114   int (&&data_9)[1000 * 1000 * 1000] = {0};
115   unsigned char data_10[1024 * 1024 * 1024 * 2u] = { 1 };
116   unsigned char data_11[1024 * 1024 * 1024 * 2u] = { One };
117   unsigned char data_12[1024][1024][1024] = {{{1}}};
118 
119   // This variable must be initialized elementwise.
120   Filler data_e1[1024] = {};
121   // CHECK: getelementptr inbounds {{.*}} @_ZN8ZeroInit7data_e1E
122 
123   struct Largeish {
124     long a, b, c;
125   };
126   // CHECK: define {{.*}}@_ZN8ZeroInit9largeish1Ev(
127   // CHECK-NOT }
128   // CHECK: call {{.*}}memset
largeish1()129   Largeish largeish1() { return {}; }
130   // CHECK: define {{.*}}@_ZN8ZeroInit9largeish2Ev(
131   // CHECK-NOT }
132   // CHECK: call {{.*}}memset
largeish2()133   Largeish largeish2() { return Largeish(); }
134   // CHECK: define {{.*}}@_ZN8ZeroInit9largeish3Ev(
135   // CHECK-NOT }
136   // CHECK: call {{.*}}memset
largeish3()137   Largeish largeish3() { return Largeish{}; }
138   // CHECK: define {{.*}}@_ZN8ZeroInit9largeish4Ev(
139   // CHECK-NOT }
140   // CHECK: call {{.*}}memset
largeish4()141   Largeish largeish4() { return (Largeish){}; }
142   // CHECK: define {{.*}}@_ZN8ZeroInit9largeish5Ev(
143   // CHECK-NOT }
144   // CHECK: call {{.*}}memset
largeish5()145   Largeish largeish5() { return {0, 0, 0}; }
146 
147   typedef __attribute__((ext_vector_type(4))) char CI4;
148   struct Conversions {
149     _Complex int a;
150     _Complex float b;
151     short c;
152     long double d;
153     CI4 e;
154     char f;
155     char g;
156     int *h;
157     long i;
158   };
159   // CHECK: define {{.*}}@_ZN8ZeroInit11conversionsEv(
160   // CHECK-NOT }
161   // CHECK: call {{.*}}memset
conversions()162   Conversions conversions() {
163     return {0,
164             0,
165             0,
166             0,
167             CI4(0),
168             static_cast<char>(0.0),
169             char(0 + 0i),
170             reinterpret_cast<int *>(0),
171             reinterpret_cast<long>((int *)nullptr)};
172   }
173 }
174