blob: 60dca99045ffbfbec5862c13ab08c8596804ac96 [file] [log] [blame]
Douglas Gregor16006c92009-12-16 18:50:27 +00001// RUN: %clang_cc1 %s -triple x86_64-apple-darwin10 -emit-llvm -o - | FileCheck %s
2
3struct A {
4 virtual ~A();
5};
6
7struct B : A { };
8
9struct C {
10 int i;
11 B b;
12};
13
14// CHECK: _Z15test_value_initv
15void 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
Mon P Wang3ecd7852010-04-04 03:10:52 +000021 // CHECK: call void @llvm.memset.p0i8.i64
Douglas Gregor7dd44902009-12-16 19:18:40 +000022 // CHECK: call void @_ZN1BC1Ev
Douglas Gregor16006c92009-12-16 18:50:27 +000023 C c = { 17 } ;
Douglas Gregor7dd44902009-12-16 19:18:40 +000024 // CHECK: call void @_ZN1CD1Ev
Douglas Gregor16006c92009-12-16 18:50:27 +000025}
Douglas Gregor1c63b9c2010-04-27 20:36:09 +000026
27enum enum_type { negative_number = -1, magic_number = 42 };
28
29class enum_holder
30{
31 enum_type m_enum;
32
33public:
34 enum_holder() : m_enum(magic_number) { }
35};
36
37struct enum_holder_and_int
38{
39 enum_holder e;
40 int i;
41};
42
43// CHECK: _Z24test_enum_holder_and_intv()
44void 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}
John McCall90217182010-08-07 08:21:30 +000052
53// PR7834: don't crash.
54namespace 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
66 void foo() {
67 B();
68 }
69}
Douglas Gregor452b7f22010-08-20 16:57:37 +000070
71namespace ptrmem {
72 struct S {
73 int mem1;
74 int S::*mem2;
75 };
76
77 // CHECK: define i32 @_ZN6ptrmem4testEPNS_1SE
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
Argyrios Kyrtzidis3b4d4902011-04-28 18:53:58 +000086namespace PR9801 {
87
88struct Test {
89 Test() : i(10) {}
90 Test(int i) : i(i) {}
91 int i;
92private:
93 int j;
94};
95
96struct Test2 {
97 Test t;
98};
99
100struct Test3 : public Test { };
101
102// CHECK: define void @_ZN6PR98011fEv
103void f() {
Argyrios Kyrtzidis657baf12011-04-28 22:57:55 +0000104 // CHECK-NOT: call void @llvm.memset.p0i8.i64
Argyrios Kyrtzidis3b4d4902011-04-28 18:53:58 +0000105 // CHECK: call void @_ZN6PR98014TestC1Ei
Argyrios Kyrtzidis657baf12011-04-28 22:57:55 +0000106 // CHECK-NOT: call void @llvm.memset.p0i8.i64
Argyrios Kyrtzidis3b4d4902011-04-28 18:53:58 +0000107 // CHECK: call void @_ZN6PR98014TestC1Ev
Argyrios Kyrtzidis3b4d4902011-04-28 18:53:58 +0000108 Test partial[3] = { 1 };
109
Argyrios Kyrtzidis657baf12011-04-28 22:57:55 +0000110 // CHECK-NOT: call void @llvm.memset.p0i8.i64
Argyrios Kyrtzidis3b4d4902011-04-28 18:53:58 +0000111 // CHECK: call void @_ZN6PR98014TestC1Ev
John McCallbdc4d802011-07-09 01:37:26 +0000112 // CHECK-NOT: call void @_ZN6PR98014TestC1Ev
Argyrios Kyrtzidis3b4d4902011-04-28 18:53:58 +0000113 Test empty[3] = {};
114
Argyrios Kyrtzidis657baf12011-04-28 22:57:55 +0000115 // CHECK: call void @llvm.memset.p0i8.i64
116 // CHECK-NOT: call void @llvm.memset.p0i8.i64
Argyrios Kyrtzidis3b4d4902011-04-28 18:53:58 +0000117 // CHECK: call void @_ZN6PR98015Test2C1Ev
John McCallbdc4d802011-07-09 01:37:26 +0000118 // CHECK-NOT: call void @_ZN6PR98015Test2C1Ev
Argyrios Kyrtzidis3b4d4902011-04-28 18:53:58 +0000119 Test2 empty2[3] = {};
120
Argyrios Kyrtzidis657baf12011-04-28 22:57:55 +0000121 // CHECK: call void @llvm.memset.p0i8.i64
122 // CHECK-NOT: call void @llvm.memset.p0i8.i64
Argyrios Kyrtzidis3b4d4902011-04-28 18:53:58 +0000123 // CHECK: call void @_ZN6PR98015Test3C1Ev
Argyrios Kyrtzidis657baf12011-04-28 22:57:55 +0000124 // CHECK-NOT: call void @llvm.memset.p0i8.i64
John McCallbdc4d802011-07-09 01:37:26 +0000125 // CHECK-NOT: call void @_ZN6PR98015Test3C1Ev
Argyrios Kyrtzidis3b4d4902011-04-28 18:53:58 +0000126 Test3 empty3[3] = {};
127}
128
129}
130
Douglas Gregor452b7f22010-08-20 16:57:37 +0000131namespace zeroinit {
132 struct S { int i; };
133
134 // CHECK: define i32 @_ZN8zeroinit4testEv()
135 int test() {
136 // CHECK: call void @llvm.memset.p0i8.i64
Richard Smith51201882011-12-30 21:15:51 +0000137 // CHECK: ret i32 0
Douglas Gregor452b7f22010-08-20 16:57:37 +0000138 return S().i;
139 }
Douglas Gregor759e41b2010-08-22 16:15:35 +0000140
141 struct X0 {
142 X0() { }
143 int x;
144 };
145
146 struct X1 : X0 {
147 int x1;
148 void f();
149 };
150
151 // CHECK: define void @_ZN8zeroinit9testX0_X1Ev
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 }
Douglas Gregor8c3e5542010-08-22 17:20:18 +0000158
159 template<typename>
160 struct X2 : X0 {
161 int x2;
162 void f();
163 };
164
165 template<typename>
166 struct X3 : X2<int> {
167 X3() : X2<int>() { }
Anders Carlsson1741d7b2011-04-17 21:49:47 +0000168 int i;
Douglas Gregor8c3e5542010-08-22 17:20:18 +0000169 };
170
171
172 // CHECK: define void @_ZN8zeroinit9testX0_X3Ev
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
John McCallbdc4d802011-07-09 01:37:26 +0000181 // More checks at EOF
Douglas Gregor452b7f22010-08-20 16:57:37 +0000182}
Chris Lattnerd1d56df2010-12-02 18:29:00 +0000183
184namespace PR8726 {
185class C;
186struct S {
187 const C &c1;
188 int i;
189 const C &c2;
190};
191void f(const C& c) {
192 S s = {c, 42, c};
193}
194
Chris Lattner124b3de2010-12-21 02:36:58 +0000195}
John McCallbdc4d802011-07-09 01:37:26 +0000196
197// rdar://problem/9355931
198namespace test6 {
199 struct A { A(); A(int); };
200
201 void test() {
202 A arr[10][20] = { 5 };
203 };
204 // CHECK: define void @_ZN5test64testEv()
205 // CHECK: [[ARR:%.*]] = alloca [10 x [20 x [[A:%.*]]]],
John McCallbdc4d802011-07-09 01:37:26 +0000206
207 // CHECK-NEXT: [[INNER:%.*]] = getelementptr inbounds [10 x [20 x [[A]]]]* [[ARR]], i64 0, i64 0
208 // CHECK-NEXT: [[T0:%.*]] = getelementptr inbounds [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]]* [[T0]], i64 1
211 // CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [[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]]* [[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]]]* [[INNER]], i64 1
220 // CHECK-NEXT: [[END:%.*]] = getelementptr inbounds [20 x [[A]]]* [[INNER]], i64 10
221 // CHECK-NEXT: br label
222 // CHECK: [[CUR:%.*]] = phi [20 x [[A]]]* [ [[BEGIN]], {{%.*}} ], [ [[NEXT:%.*]], {{%.*}} ]
John McCallbdc4d802011-07-09 01:37:26 +0000223
John McCallc3c07662011-07-13 06:10:41 +0000224 // Inner loop.
225 // CHECK-NEXT: [[IBEGIN:%.*]] = getelementptr inbounds [20 x [[A]]]* [[CUR]], i32 0, i32 0
226 // CHECK-NEXT: [[IEND:%.*]] = getelementptr inbounds [[A]]* [[IBEGIN]], i64 20
John McCallbdc4d802011-07-09 01:37:26 +0000227 // CHECK-NEXT: br label
John McCallc3c07662011-07-13 06:10:41 +0000228 // CHECK: [[ICUR:%.*]] = phi [[A]]* [ [[IBEGIN]], {{%.*}} ], [ [[INEXT:%.*]], {{%.*}} ]
229 // CHECK-NEXT: call void @_ZN5test61AC1Ev([[A]]* [[ICUR]])
230 // CHECK-NEXT: [[INEXT:%.*]] = getelementptr inbounds [[A]]* [[ICUR]], i64 1
231 // CHECK-NEXT: [[T0:%.*]] = icmp eq [[A]]* [[INEXT]], [[IEND]]
232 // CHECK-NEXT: br i1 [[T0]],
233
John McCallbdc4d802011-07-09 01:37:26 +0000234 // CHECK: [[NEXT]] = getelementptr inbounds [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
Eli Friedman2ed7cb62011-10-14 02:27:24 +0000240namespace 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(); };
245 C::C() : A(3), B() {}
246 // CHECK: 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(); };
253 C2::C2() : A(3), B2() {}
254 // CHECK: 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
Richard Smith0dbe2fb2012-12-21 03:17:28 +0000259// Ensure we produce an i1 here, and don't assert.
260// CHECK: define void @_Z9r170806_bv(
261// CHECK: call void @_Z9r170806_ab(i1 zeroext false)
262void r170806_a(bool b = bool());
263void r170806_b() { r170806_a(); }
264
John McCallbdc4d802011-07-09 01:37:26 +0000265// CHECK: define linkonce_odr void @_ZN8zeroinit2X3IiEC2Ev(%"struct.zeroinit::X3"* %this) unnamed_addr
266// CHECK: call void @llvm.memset.p0i8.i64
267// CHECK-NEXT: call void @_ZN8zeroinit2X2IiEC2Ev
268// CHECK-NEXT: ret void