blob: 6c44c761296625b064d211c1abe66c616454aaa5 [file] [log] [blame]
Anders Carlssonabea9512011-02-28 00:40:07 +00001// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -triple x86_64-apple-darwin -std=c++0x -emit-llvm %s -o %t.ll
Mike Stumpa4463432009-11-20 18:51:28 +00002// RUN: FileCheck --input-file=%t.ll %s
3
4struct test1_D {
5 double d;
6} d1;
7
8void test1() {
9 throw d1;
10}
11
John McCallac418162010-04-22 01:10:34 +000012// CHECK: define void @_Z5test1v()
John McCall3ad32c82011-01-28 08:37:24 +000013// CHECK: [[EXNOBJ:%.*]] = call i8* @__cxa_allocate_exception(i64 8)
John McCallac418162010-04-22 01:10:34 +000014// CHECK-NEXT: [[EXN:%.*]] = bitcast i8* [[EXNOBJ]] to [[DSTAR:%[^*]*\*]]
15// CHECK-NEXT: [[EXN2:%.*]] = bitcast [[DSTAR]] [[EXN]] to i8*
16// CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* [[EXN2]], i8* bitcast ([[DSTAR]] @d1 to i8*), i64 8, i32 8, i1 false)
John McCallac418162010-04-22 01:10:34 +000017// CHECK-NEXT: call void @__cxa_throw(i8* [[EXNOBJ]], i8* bitcast (%0* @_ZTI7test1_D to i8*), i8* null) noreturn
Mike Stumpa4463432009-11-20 18:51:28 +000018// CHECK-NEXT: unreachable
19
20
21struct test2_D {
22 test2_D(const test2_D&o);
23 test2_D();
24 virtual void bar() { }
25 int i; int j;
26} d2;
27
28void test2() {
29 throw d2;
30}
31
John McCallac418162010-04-22 01:10:34 +000032// CHECK: define void @_Z5test2v()
John McCall3ad32c82011-01-28 08:37:24 +000033// CHECK: [[EXNSLOTVAR:%.*]] = alloca i8*
John McCallff8e1152010-07-23 21:56:41 +000034// CHECK-NEXT: [[CLEANUPDESTVAR:%.*]] = alloca i32
John McCallac418162010-04-22 01:10:34 +000035// CHECK-NEXT: [[EXNOBJ:%.*]] = call i8* @__cxa_allocate_exception(i64 16)
John McCallac418162010-04-22 01:10:34 +000036// CHECK-NEXT: [[EXN:%.*]] = bitcast i8* [[EXNOBJ]] to [[DSTAR:%[^*]*\*]]
37// CHECK-NEXT: invoke void @_ZN7test2_DC1ERKS_([[DSTAR]] [[EXN]], [[DSTAR]] @d2)
38// CHECK-NEXT: to label %[[CONT:.*]] unwind label %{{.*}}
John McCall7e2f1282010-04-22 03:27:09 +000039// : [[CONT]]: (can't check this in Release-Asserts builds)
John McCall3ad32c82011-01-28 08:37:24 +000040// CHECK: call void @__cxa_throw(i8* [[EXNOBJ]], i8* bitcast (%{{.*}}* @_ZTI7test2_D to i8*), i8* null) noreturn
Mike Stumpa4463432009-11-20 18:51:28 +000041// CHECK-NEXT: unreachable
42
43
44struct test3_D {
45 test3_D() { }
46 test3_D(volatile test3_D&o);
47 virtual void bar();
48};
49
50void test3() {
51 throw (volatile test3_D *)0;
52}
53
John McCallac418162010-04-22 01:10:34 +000054// CHECK: define void @_Z5test3v()
John McCall3ad32c82011-01-28 08:37:24 +000055// CHECK: [[EXNOBJ:%.*]] = call i8* @__cxa_allocate_exception(i64 8)
56// CHECK-NEXT: [[EXN:%.*]] = bitcast i8* [[EXNOBJ]] to [[D:%[^*]+]]**
57// CHECK-NEXT: store [[D]]* null, [[D]]** [[EXN]]
John McCallac418162010-04-22 01:10:34 +000058// CHECK-NEXT: call void @__cxa_throw(i8* [[EXNOBJ]], i8* bitcast (%1* @_ZTIPV7test3_D to i8*), i8* null) noreturn
59// CHECK-NEXT: unreachable
Mike Stumpa4463432009-11-20 18:51:28 +000060
61
62void test4() {
63 throw;
64}
65
John McCallac418162010-04-22 01:10:34 +000066// CHECK: define void @_Z5test4v()
Mike Stump53b48102010-01-13 21:23:04 +000067// CHECK: call void @__cxa_rethrow() noreturn
Mike Stumpa4463432009-11-20 18:51:28 +000068// CHECK-NEXT: unreachable
John McCallac418162010-04-22 01:10:34 +000069
70
71// rdar://problem/7696549
72namespace test5 {
73 struct A {
74 A();
75 A(const A&);
76 ~A();
77 };
78
79 void test() {
80 try { throw A(); } catch (A &x) {}
81 }
82// CHECK: define void @_ZN5test54testEv()
83// CHECK: [[EXNOBJ:%.*]] = call i8* @__cxa_allocate_exception(i64 1)
84// CHECK: [[EXNCAST:%.*]] = bitcast i8* [[EXNOBJ]] to [[A:%[^*]*]]*
85// CHECK-NEXT: invoke void @_ZN5test51AC1Ev([[A]]* [[EXNCAST]])
86// CHECK: invoke void @__cxa_throw(i8* [[EXNOBJ]], i8* bitcast ({{%.*}}* @_ZTIN5test51AE to i8*), i8* bitcast (void ([[A]]*)* @_ZN5test51AD1Ev to i8*)) noreturn
87// CHECK-NEXT: to label {{%.*}} unwind label %[[HANDLER:[^ ]*]]
John McCall7e2f1282010-04-22 03:27:09 +000088// : [[HANDLER]]: (can't check this in Release-Asserts builds)
John McCallac418162010-04-22 01:10:34 +000089// CHECK: {{%.*}} = call i32 @llvm.eh.typeid.for(i8* bitcast ({{%.*}}* @_ZTIN5test51AE to i8*))
90}
John McCallf1549f62010-07-06 01:34:17 +000091
92namespace test6 {
93 template <class T> struct allocator {
94 ~allocator() throw() { }
95 };
96
97 void foo() {
98 allocator<int> a;
99 }
100}
101
102// PR7127
103namespace test7 {
104// CHECK: define i32 @_ZN5test73fooEv()
105 int foo() {
John McCall3ad32c82011-01-28 08:37:24 +0000106// CHECK: [[CAUGHTEXNVAR:%.*]] = alloca i8*
John McCallf1549f62010-07-06 01:34:17 +0000107// CHECK-NEXT: [[INTCATCHVAR:%.*]] = alloca i32
John McCallff8e1152010-07-23 21:56:41 +0000108// CHECK-NEXT: [[EHCLEANUPDESTVAR:%.*]] = alloca i32
John McCallf1549f62010-07-06 01:34:17 +0000109 try {
110 try {
111// CHECK-NEXT: [[EXNALLOC:%.*]] = call i8* @__cxa_allocate_exception
John McCallf1549f62010-07-06 01:34:17 +0000112// CHECK-NEXT: bitcast i8* [[EXNALLOC]] to i32*
113// CHECK-NEXT: store i32 1, i32*
John McCallf1549f62010-07-06 01:34:17 +0000114// CHECK-NEXT: invoke void @__cxa_throw(i8* [[EXNALLOC]], i8* bitcast (i8** @_ZTIi to i8*), i8* null
115 throw 1;
116 }
John McCallf1549f62010-07-06 01:34:17 +0000117
118// CHECK: [[CAUGHTEXN:%.*]] = call i8* @llvm.eh.exception()
119// CHECK-NEXT: store i8* [[CAUGHTEXN]], i8** [[CAUGHTEXNVAR]]
120// CHECK-NEXT: call i32 (i8*, i8*, ...)* @llvm.eh.selector(i8* [[CAUGHTEXN]], i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*), i8* bitcast (i8** @_ZTIi to i8*), i8* null)
121// CHECK-NEXT: call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
122// CHECK-NEXT: icmp eq
123// CHECK-NEXT: br i1
124// CHECK: load i8** [[CAUGHTEXNVAR]]
125// CHECK-NEXT: call i8* @__cxa_begin_catch
126// CHECK: invoke void @__cxa_rethrow
127 catch (int) {
128 throw;
129 }
130 }
131// CHECK: [[CAUGHTEXN:%.*]] = call i8* @llvm.eh.exception()
132// CHECK-NEXT: store i8* [[CAUGHTEXN]], i8** [[CAUGHTEXNVAR]]
133// CHECK-NEXT: call i32 (i8*, i8*, ...)* @llvm.eh.selector(i8* [[CAUGHTEXN]], i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*), i8* null)
John McCallff8e1152010-07-23 21:56:41 +0000134// CHECK-NEXT: store i32 1, i32* [[EHCLEANUPDESTVAR]]
John McCallf1549f62010-07-06 01:34:17 +0000135// CHECK-NEXT: call void @__cxa_end_catch()
136// CHECK-NEXT: br label
137// CHECK: load i8** [[CAUGHTEXNVAR]]
138// CHECK-NEXT: call i8* @__cxa_begin_catch
139// CHECK-NEXT: call void @__cxa_end_catch
140 catch (...) {
141 }
142// CHECK: ret i32 0
143 return 0;
144 }
145}
146
147// Ordering of destructors in a catch handler.
148namespace test8 {
149 struct A { A(const A&); ~A(); };
150 void bar();
151
152 // CHECK: define void @_ZN5test83fooEv()
153 void foo() {
154 try {
155 // CHECK: invoke void @_ZN5test83barEv()
156 bar();
157 } catch (A a) {
158 // CHECK: call i8* @__cxa_get_exception_ptr
159 // CHECK-NEXT: bitcast
160 // CHECK-NEXT: invoke void @_ZN5test81AC1ERKS0_(
161 // CHECK: call i8* @__cxa_begin_catch
Sean Hunt1d780322011-05-18 20:57:11 +0000162 // CHECK-NEXT: invoke void @_ZN5test81AD1Ev(
John McCallf1549f62010-07-06 01:34:17 +0000163 // CHECK: call void @__cxa_end_catch()
John McCallf1549f62010-07-06 01:34:17 +0000164 // CHECK: ret void
165 }
166 }
167}
John McCall59a70002010-07-07 06:56:46 +0000168
169// Constructor function-try-block must rethrow on fallthrough.
170// rdar://problem/7696603
171namespace test9 {
172 void opaque();
173
174 struct A { A(); };
175
Rafael Espindola0691a5c2011-01-25 19:10:24 +0000176 // CHECK: define void @_ZN5test91AC1Ev(%"struct.test10::A"* %this) unnamed_addr
John McCall59a70002010-07-07 06:56:46 +0000177 // CHECK: call void @_ZN5test91AC2Ev
178 // CHECK-NEXT: ret void
179
Rafael Espindola0691a5c2011-01-25 19:10:24 +0000180 // CHECK: define void @_ZN5test91AC2Ev(%"struct.test10::A"* %this) unnamed_addr
John McCall59a70002010-07-07 06:56:46 +0000181 A::A() try {
182 // CHECK: invoke void @_ZN5test96opaqueEv()
183 opaque();
184 } catch (int x) {
185 // CHECK: call i8* @__cxa_begin_catch
186 // CHECK: invoke void @_ZN5test96opaqueEv()
187 // CHECK: invoke void @__cxa_rethrow()
188 opaque();
189 }
190
191 // landing pad from first call to invoke
192 // CHECK: call i8* @llvm.eh.exception
193 // CHECK: call i32 (i8*, i8*, ...)* @llvm.eh.selector(i8* {{.*}}, i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*), i8* bitcast (i8** @_ZTIi to i8*), i8* null)
194}
John McCall8e3f8612010-07-13 22:12:14 +0000195
196// __cxa_end_catch can throw for some kinds of caught exceptions.
197namespace test10 {
198 void opaque();
199
200 struct A { ~A(); };
201 struct B { int x; };
202
203 // CHECK: define void @_ZN6test103fooEv()
204 void foo() {
205 A a; // force a cleanup context
206
207 try {
208 // CHECK: invoke void @_ZN6test106opaqueEv()
209 opaque();
210 } catch (int i) {
211 // CHECK: call i8* @__cxa_begin_catch
212 // CHECK-NEXT: bitcast
213 // CHECK-NEXT: load i32*
214 // CHECK-NEXT: store i32
215 // CHECK-NEXT: call void @__cxa_end_catch() nounwind
216 } catch (B a) {
217 // CHECK: call i8* @__cxa_begin_catch
218 // CHECK-NEXT: bitcast
219 // CHECK-NEXT: bitcast
220 // CHECK-NEXT: bitcast
221 // CHECK-NEXT: call void @llvm.memcpy
222 // CHECK-NEXT: invoke void @__cxa_end_catch()
223 } catch (...) {
224 // CHECK: call i8* @__cxa_begin_catch
225 // CHECK-NEXT: invoke void @__cxa_end_catch()
226 }
227
228 // CHECK: call void @_ZN6test101AD1Ev(
229 }
230}
John McCall204b0752010-07-20 22:17:55 +0000231
232// __cxa_begin_catch returns pointers by value, even when catching by reference
233// <rdar://problem/8212123>
234namespace test11 {
235 void opaque();
236
237 // CHECK: define void @_ZN6test113fooEv()
238 void foo() {
239 try {
240 // CHECK: invoke void @_ZN6test116opaqueEv()
241 opaque();
242 } catch (int**&p) {
243 // CHECK: [[EXN:%.*]] = load i8**
244 // CHECK-NEXT: call i8* @__cxa_begin_catch(i8* [[EXN]]) nounwind
245 // CHECK-NEXT: [[ADJ1:%.*]] = getelementptr i8* [[EXN]], i32 32
246 // CHECK-NEXT: [[ADJ2:%.*]] = bitcast i8* [[ADJ1]] to i32***
247 // CHECK-NEXT: store i32*** [[ADJ2]], i32**** [[P:%.*]]
248 // CHECK-NEXT: call void @__cxa_end_catch() nounwind
249 }
250 }
251
252 struct A {};
253
254 // CHECK: define void @_ZN6test113barEv()
255 void bar() {
256 try {
257 // CHECK: [[EXNSLOT:%.*]] = alloca i8*
258 // CHECK-NEXT: [[P:%.*]] = alloca [[A:%.*]]**,
259 // CHECK-NEXT: [[TMP:%.*]] = alloca [[A]]*
260 // CHECK-NEXT: invoke void @_ZN6test116opaqueEv()
261 opaque();
262 } catch (A*&p) {
263 // CHECK: [[EXN:%.*]] = load i8** [[EXNSLOT]]
264 // CHECK-NEXT: [[ADJ1:%.*]] = call i8* @__cxa_begin_catch(i8* [[EXN]]) nounwind
265 // CHECK-NEXT: [[ADJ2:%.*]] = bitcast i8* [[ADJ1]] to [[A]]*
266 // CHECK-NEXT: store [[A]]* [[ADJ2]], [[A]]** [[TMP]]
267 // CHECK-NEXT: store [[A]]** [[TMP]], [[A]]*** [[P]]
268 // CHECK-NEXT: call void @__cxa_end_catch() nounwind
269 }
270 }
271}
John McCallff8e1152010-07-23 21:56:41 +0000272
273// PR7686
274namespace test12 {
Sean Hunt1d780322011-05-18 20:57:11 +0000275 struct A { ~A(); };
John McCallff8e1152010-07-23 21:56:41 +0000276 bool opaque(const A&);
277
278 // CHECK: define void @_ZN6test124testEv()
279 void test() {
280 // CHECK: [[X:%.*]] = alloca [[A:%.*]],
281 // CHECK: [[EHCLEANUPDEST:%.*]] = alloca i32
282 // CHECK: [[Y:%.*]] = alloca [[A]]
283 // CHECK: [[Z:%.*]] = alloca [[A]]
284 // CHECK: [[CLEANUPDEST:%.*]] = alloca i32
285
286 A x;
287 // CHECK: invoke zeroext i1 @_ZN6test126opaqueERKNS_1AE(
288 if (opaque(x)) {
289 A y;
290 A z;
291
292 // CHECK: invoke void @_ZN6test121AD1Ev([[A]]* [[Z]])
293 // CHECK: invoke void @_ZN6test121AD1Ev([[A]]* [[Y]])
294
295 // It'd be great if something eliminated this switch.
296 // CHECK: load i32* [[CLEANUPDEST]]
297 // CHECK-NEXT: switch i32
298 goto success;
299 }
300
301 success:
302 bool _ = true;
303
304 // CHECK: call void @_ZN6test121AD1Ev([[A]]* [[X]])
305 // CHECK-NEXT: ret void
306 }
307}
308
309// Reduced from some TableGen code that was causing a self-host crash.
310namespace test13 {
311 struct A { ~A(); };
312
313 void test0(int x) {
314 try {
315 switch (x) {
316 case 0:
317 break;
318 case 1:{
319 A a;
320 break;
321 }
322 default:
323 return;
324 }
325 return;
326 } catch (int x) {
327 }
328 return;
329 }
330
331 void test1(int x) {
332 A y;
333 try {
334 switch (x) {
335 default: break;
336 }
337 } catch (int x) {}
338 }
339}
John McCall7cd4b062010-07-26 22:44:58 +0000340
341// rdar://problem/8231514
342namespace test14 {
343 struct A { ~A(); };
344 struct B { ~B(); };
345
346 B b();
347 void opaque();
348
349 void foo() {
350 A a;
351 try {
352 B str = b();
353 opaque();
354 } catch (int x) {
355 }
356 }
357}
John McCall413e6772010-07-28 01:07:35 +0000358
359// rdar://problem/8231514
360// JumpDests shouldn't get confused by scopes that aren't normal cleanups.
361namespace test15 {
362 struct A { ~A(); };
363
364 bool opaque(int);
365
366 // CHECK: define void @_ZN6test153fooEv()
367 void foo() {
368 A a;
369
370 try {
371 // CHECK: [[X:%.*]] = alloca i32
372 // CHECK: store i32 10, i32* [[X]]
373 // CHECK-NEXT: br label
374 // -> while.cond
375 int x = 10;
376
377 while (true) {
378 // CHECK: load i32* [[X]]
379 // CHECK-NEXT: [[COND:%.*]] = invoke zeroext i1 @_ZN6test156opaqueEi
380 // CHECK: br i1 [[COND]]
381 if (opaque(x))
382 // CHECK: br label
383 break;
384
385 // CHECK: br label
386 }
387 // CHECK: br label
388 } catch (int x) { }
389
390 // CHECK: call void @_ZN6test151AD1Ev
391 }
392}
John McCall3ad32c82011-01-28 08:37:24 +0000393
394namespace test16 {
Sean Hunt1d780322011-05-18 20:57:11 +0000395 struct A { A(); ~A(); };
396 struct B { int x; B(const A &); ~B(); };
John McCall3ad32c82011-01-28 08:37:24 +0000397 void foo();
398 bool cond();
399
400 // CHECK: define void @_ZN6test163barEv()
401 void bar() {
402 // CHECK: [[EXN_SAVE:%.*]] = alloca i8*
403 // CHECK-NEXT: [[EXN_ACTIVE:%.*]] = alloca i1
404 // CHECK-NEXT: [[TEMP:%.*]] = alloca [[A:%.*]],
405 // CHECK-NEXT: [[EXNSLOT:%.*]] = alloca i8*
406 // CHECK-NEXT: [[EHDEST:%.*]] = alloca i32
407 // CHECK-NEXT: [[TEMP_ACTIVE:%.*]] = alloca i1
408
409 cond() ? throw B(A()) : foo();
410
411 // CHECK-NEXT: [[COND:%.*]] = call zeroext i1 @_ZN6test164condEv()
412 // CHECK-NEXT: store i1 false, i1* [[EXN_ACTIVE]]
413 // CHECK-NEXT: store i1 false, i1* [[TEMP_ACTIVE]]
414 // CHECK-NEXT: br i1 [[COND]],
415
416 // CHECK: [[EXN:%.*]] = call i8* @__cxa_allocate_exception(i64 4)
417 // CHECK-NEXT: store i8* [[EXN]], i8** [[EXN_SAVE]]
418 // CHECK-NEXT: store i1 true, i1* [[EXN_ACTIVE]]
419 // CHECK-NEXT: [[T0:%.*]] = bitcast i8* [[EXN]] to [[B:%.*]]*
420 // CHECK-NEXT: invoke void @_ZN6test161AC1Ev([[A]]* [[TEMP]])
421 // CHECK: store i1 true, i1* [[TEMP_ACTIVE]]
422 // CHECK-NEXT: invoke void @_ZN6test161BC1ERKNS_1AE([[B]]* [[T0]], [[A]]* [[TEMP]])
423 // CHECK: store i1 false, i1* [[EXN_ACTIVE]]
424 // CHECK-NEXT: invoke void @__cxa_throw(i8* [[EXN]],
425
426 // CHECK: invoke void @_ZN6test163fooEv()
427 // CHECK: br label
428
429 // CHECK: invoke void @_ZN6test161AD1Ev([[A]]* [[TEMP]])
430 // CHECK: ret void
431
432 // CHECK: [[T0:%.*]] = load i1* [[EXN_ACTIVE]]
433 // CHECK-NEXT: br i1 [[T0]]
434 // CHECK: [[T1:%.*]] = load i8** [[EXN_SAVE]]
435 // CHECK-NEXT: call void @__cxa_free_exception(i8* [[T1]])
436 // CHECK-NEXT: br label
437 }
438}