1 // RUN: %clang_cc1 %s -triple x86_64-apple-darwin10 -emit-llvm -o - | FileCheck %s
2
3 struct A {
4 virtual ~A();
5 };
6
7 struct B : A { };
8
9 struct C {
10 int i;
11 B b;
12 };
13
14 // CHECK: _Z15test_value_initv
test_value_init()15 void test_value_init() {
16 // This value initialization requires zero initialization of the 'B'
17 // subobject followed by a call to its constructor.
18 // PR5800
19
20 // CHECK: store i32 17
21 // CHECK: call void @llvm.memset.p0i8.i64
22 // CHECK: call void @_ZN1BC1Ev
23 C c = { 17 } ;
24 // CHECK: call void @_ZN1CD1Ev
25 }
26
27 enum enum_type { negative_number = -1, magic_number = 42 };
28
29 class enum_holder
30 {
31 enum_type m_enum;
32
33 public:
enum_holder()34 enum_holder() : m_enum(magic_number) { }
35 };
36
37 struct enum_holder_and_int
38 {
39 enum_holder e;
40 int i;
41 };
42
43 // CHECK: _Z24test_enum_holder_and_intv()
test_enum_holder_and_int()44 void test_enum_holder_and_int() {
45 // CHECK: alloca
46 // CHECK-NEXT: bitcast
47 // CHECK-NEXT: call void @llvm.memset
48 // CHECK-NEXT: call void @_ZN19enum_holder_and_intC1Ev
49 enum_holder_and_int();
50 // CHECK-NEXT: ret void
51 }
52
53 // PR7834: don't crash.
54 namespace test1 {
55 struct A {
56 int A::*f;
57 A();
58 A(const A&);
59 A &operator=(const A &);
60 };
61
62 struct B {
63 A base;
64 };
65
foo()66 void foo() {
67 B();
68 }
69 }
70
71 namespace ptrmem {
72 struct S {
73 int mem1;
74 int S::*mem2;
75 };
76
77 // CHECK-LABEL: define i32 @_ZN6ptrmem4testEPNS_1SE
test(S * s)78 int test(S *s) {
79 // CHECK: call void @llvm.memcpy.p0i8.p0i8.i64
80 // CHECK: getelementptr
81 // CHECK: ret
82 return s->*S().mem2;
83 }
84 }
85
86 namespace PR9801 {
87
88 struct Test {
TestPR9801::Test89 Test() : i(10) {}
TestPR9801::Test90 Test(int i) : i(i) {}
91 int i;
92 private:
93 int j;
94 };
95
96 struct Test2 {
97 Test t;
98 };
99
100 struct Test3 : public Test { };
101
102 // CHECK-LABEL: define void @_ZN6PR98011fEv
f()103 void f() {
104 // CHECK-NOT: call void @llvm.memset.p0i8.i64
105 // CHECK: call void @_ZN6PR98014TestC1Ei
106 // CHECK-NOT: call void @llvm.memset.p0i8.i64
107 // CHECK: call void @_ZN6PR98014TestC1Ev
108 Test partial[3] = { 1 };
109
110 // CHECK-NOT: call void @llvm.memset.p0i8.i64
111 // CHECK: call void @_ZN6PR98014TestC1Ev
112 // CHECK-NOT: call void @_ZN6PR98014TestC1Ev
113 Test empty[3] = {};
114
115 // CHECK: call void @llvm.memset.p0i8.i64
116 // CHECK-NOT: call void @llvm.memset.p0i8.i64
117 // CHECK: call void @_ZN6PR98015Test2C1Ev
118 // CHECK-NOT: call void @_ZN6PR98015Test2C1Ev
119 Test2 empty2[3] = {};
120
121 // CHECK: call void @llvm.memset.p0i8.i64
122 // CHECK-NOT: call void @llvm.memset.p0i8.i64
123 // CHECK: call void @_ZN6PR98015Test3C1Ev
124 // CHECK-NOT: call void @llvm.memset.p0i8.i64
125 // CHECK-NOT: call void @_ZN6PR98015Test3C1Ev
126 Test3 empty3[3] = {};
127 }
128
129 }
130
131 namespace zeroinit {
132 struct S { int i; };
133
134 // CHECK-LABEL: define i32 @_ZN8zeroinit4testEv()
test()135 int test() {
136 // CHECK: call void @llvm.memset.p0i8.i64
137 // CHECK: ret i32 0
138 return S().i;
139 }
140
141 struct X0 {
X0zeroinit::X0142 X0() { }
143 int x;
144 };
145
146 struct X1 : X0 {
147 int x1;
148 void f();
149 };
150
151 // CHECK-LABEL: define void @_ZN8zeroinit9testX0_X1Ev
testX0_X1()152 void testX0_X1() {
153 // CHECK: call void @llvm.memset.p0i8.i64
154 // CHECK-NEXT: call void @_ZN8zeroinit2X1C1Ev
155 // CHECK-NEXT: call void @_ZN8zeroinit2X11fEv
156 X1().f();
157 }
158
159 template<typename>
160 struct X2 : X0 {
161 int x2;
162 void f();
163 };
164
165 template<typename>
166 struct X3 : X2<int> {
X3zeroinit::X3167 X3() : X2<int>() { }
168 int i;
169 };
170
171
172 // CHECK-LABEL: define void @_ZN8zeroinit9testX0_X3Ev
testX0_X3()173 void testX0_X3() {
174 // CHECK-NOT: call void @llvm.memset
175 // CHECK: call void @_ZN8zeroinit2X3IiEC1Ev
176 // CHECK: call void @_ZN8zeroinit2X2IiE1fEv
177 // CHECK-NEXT: ret void
178 X3<int>().f();
179 }
180
181 // More checks at EOF
182 }
183
184 namespace PR8726 {
185 class C;
186 struct S {
187 const C &c1;
188 int i;
189 const C &c2;
190 };
f(const C & c)191 void f(const C& c) {
192 S s = {c, 42, c};
193 }
194
195 }
196
197 // rdar://problem/9355931
198 namespace test6 {
199 struct A { A(); A(int); };
200
test()201 void test() {
202 A arr[10][20] = { 5 };
203 };
204 // CHECK-LABEL: define void @_ZN5test64testEv()
205 // CHECK: [[ARR:%.*]] = alloca [10 x [20 x [[A:%.*]]]],
206
207 // CHECK-NEXT: [[INNER:%.*]] = getelementptr inbounds [10 x [20 x [[A]]]], [10 x [20 x [[A]]]]* [[ARR]], i64 0, i64 0
208 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [20 x [[A]]], [20 x [[A]]]* [[INNER]], i64 0, i64 0
209 // CHECK-NEXT: call void @_ZN5test61AC1Ei([[A]]* [[T0]], i32 5)
210 // CHECK-NEXT: [[BEGIN:%.*]] = getelementptr inbounds [[A]], [[A]]* [[T0]], i64 1
211 // CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [[A]], [[A]]* [[T0]], i64 20
212 // CHECK-NEXT: br label
213 // CHECK: [[CUR:%.*]] = phi [[A]]* [ [[BEGIN]], {{%.*}} ], [ [[NEXT:%.*]], {{%.*}} ]
214 // CHECK-NEXT: call void @_ZN5test61AC1Ev([[A]]* [[CUR]])
215 // CHECK-NEXT: [[NEXT]] = getelementptr inbounds [[A]], [[A]]* [[CUR]], i64 1
216 // CHECK-NEXT: [[T0:%.*]] = icmp eq [[A]]* [[NEXT]], [[END]]
217 // CHECK-NEXT: br i1
218
219 // CHECK: [[BEGIN:%.*]] = getelementptr inbounds [20 x [[A]]], [20 x [[A]]]* [[INNER]], i64 1
220 // CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [20 x [[A]]], [20 x [[A]]]* [[INNER]], i64 10
221 // CHECK-NEXT: br label
222 // CHECK: [[CUR:%.*]] = phi [20 x [[A]]]* [ [[BEGIN]], {{%.*}} ], [ [[NEXT:%.*]], {{%.*}} ]
223
224 // Inner loop.
225 // CHECK-NEXT: [[IBEGIN:%.*]] = getelementptr inbounds [20 x [[A]]], [20 x [[A]]]* [[CUR]], i32 0, i32 0
226 // CHECK-NEXT: [[IEND:%.*]] = getelementptr inbounds [[A]], [[A]]* [[IBEGIN]], i64 20
227 // CHECK-NEXT: br label
228 // CHECK: [[ICUR:%.*]] = phi [[A]]* [ [[IBEGIN]], {{%.*}} ], [ [[INEXT:%.*]], {{%.*}} ]
229 // CHECK-NEXT: call void @_ZN5test61AC1Ev([[A]]* [[ICUR]])
230 // CHECK-NEXT: [[INEXT:%.*]] = getelementptr inbounds [[A]], [[A]]* [[ICUR]], i64 1
231 // CHECK-NEXT: [[T0:%.*]] = icmp eq [[A]]* [[INEXT]], [[IEND]]
232 // CHECK-NEXT: br i1 [[T0]],
233
234 // CHECK: [[NEXT]] = getelementptr inbounds [20 x [[A]]], [20 x [[A]]]* [[CUR]], i64 1
235 // CHECK-NEXT: [[T0:%.*]] = icmp eq [20 x [[A]]]* [[NEXT]], [[END]]
236 // CHECK-NEXT: br i1 [[T0]]
237 // CHECK: ret void
238 }
239
240 namespace PR11124 {
241 // Make sure C::C doesn't overwrite parts of A while it is zero-initializing B
242 struct A { int a; A(); A(int); };
243 struct B : virtual A { int b; };
244 struct C : B { C(); };
C()245 C::C() : A(3), B() {}
246 // CHECK-LABEL: define void @_ZN7PR111241CC1Ev
247 // CHECK: call void @llvm.memset.p0i8.i64(i8* {{.*}}, i8 0, i64 12, i32 8, i1 false)
248 // CHECK-NEXT: call void @_ZN7PR111241BC2Ev
249 // Make sure C::C doesn't overwrite parts of A while it is zero-initializing B
250
251 struct B2 : virtual A { int B::*b; };
252 struct C2 : B2 { C2(); };
C2()253 C2::C2() : A(3), B2() {}
254 // CHECK-LABEL: define void @_ZN7PR111242C2C1Ev
255 // CHECK: call void @llvm.memcpy.p0i8.p0i8.i64(i8* %{{.*}}, i8* {{.*}}, i64 16, i32 8, i1 false)
256 // CHECK-NEXT: call void @_ZN7PR111242B2C2Ev
257 }
258
259 // Ensure we produce an i1 here, and don't assert.
260 // CHECK-LABEL: define void @_Z9r170806_bv(
261 // CHECK: call void @_Z9r170806_ab(i1 zeroext false)
262 void r170806_a(bool b = bool());
r170806_b()263 void r170806_b() { r170806_a(); }
264
265 namespace PR20256 {
266 struct data { int i; };
267
268 template<typename T = int>
g()269 data g() {
270 data d; // not value-init
271 return d;
272 }
273 template data g();
274 // CHECK-LABEL: define {{.*}} @_ZN7PR202561gIiEENS_4dataEv(
275 // CHECK-NOT: store
276 // CHECK-NOT: memset
277 // CHECK: }
278
279 template<typename ...T>
h(T...t)280 data h(T ...t) {
281 data d(t...); // value-init
282 return d;
283 }
284 template data h();
285 // CHECK-LABEL: define {{.*}} @_ZN7PR202561hIJEEENS_4dataEDpT_(
286 // CHECK: call void @llvm.memset
287 // CHECK: }
288
289
290 template<typename T = int>
j()291 data j() {
292 data d = {}; // value-init
293 return d;
294 }
295 template data j();
296 // CHECK-LABEL: define {{.*}} @_ZN7PR202561jIiEENS_4dataEv(
297 // CHECK: call void @llvm.memset
298 // CHECK: }
299
f()300 data f() {
301 data d; // not value-init
302 return d;
303 }
304 // CHECK-LABEL: define {{.*}} @_ZN7PR202561fEv(
305 // CHECK-NOT: store
306 // CHECK-NOT: memset
307 // CHECK: }
308
i()309 data i() {
310 data d = {}; // value-init
311 return d;
312 }
313 // CHECK-LABEL: define {{.*}} @_ZN7PR202561iEv(
314 // CHECK: call void @llvm.memset
315 // CHECK: }
316 }
317
318 // CHECK-LABEL: define linkonce_odr void @_ZN8zeroinit2X3IiEC2Ev(%"struct.zeroinit::X3"* %this) unnamed_addr
319 // CHECK: call void @llvm.memset.p0i8.i64
320 // CHECK-NEXT: call void @_ZN8zeroinit2X2IiEC2Ev
321 // CHECK-NEXT: ret void
322