1 // RUN: %clang_cc1 -fexceptions -triple x86_64-apple-darwin -std=c++0x -emit-llvm %s -o %t.ll
2 // RUN: FileCheck --input-file=%t.ll %s
12 // CHECK: define void @_Z5test1v()
13 // CHECK: [[EXNOBJ:%.*]] = call i8* @__cxa_allocate_exception(i64 8)
14 // 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)
17 // CHECK-NEXT: call void @__cxa_throw(i8* [[EXNOBJ]], i8* bitcast (%0* @_ZTI7test1_D to i8*), i8* null) noreturn
18 // CHECK-NEXT: unreachable
22 test2_D(const test2_D
&o
);
24 virtual void bar() { }
32 // CHECK: define void @_Z5test2v()
33 // CHECK: [[EXNSLOTVAR:%.*]] = alloca i8*
34 // CHECK-NEXT: [[CLEANUPDESTVAR:%.*]] = alloca i32
35 // CHECK-NEXT: [[EXNOBJ:%.*]] = call i8* @__cxa_allocate_exception(i64 16)
36 // 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 %{{.*}}
39 // : [[CONT]]: (can't check this in Release-Asserts builds)
40 // CHECK: call void @__cxa_throw(i8* [[EXNOBJ]], i8* bitcast (%{{.*}}* @_ZTI7test2_D to i8*), i8* null) noreturn
41 // CHECK-NEXT: unreachable
46 test3_D(volatile test3_D
&o
);
51 throw (volatile test3_D
*)0;
54 // CHECK: define void @_Z5test3v()
55 // 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]]
58 // CHECK-NEXT: call void @__cxa_throw(i8* [[EXNOBJ]], i8* bitcast (%1* @_ZTIPV7test3_D to i8*), i8* null) noreturn
59 // CHECK-NEXT: unreachable
66 // CHECK: define void @_Z5test4v()
67 // CHECK: call void @__cxa_rethrow() noreturn
68 // CHECK-NEXT: unreachable
71 // rdar://problem/7696549
80 try { throw A(); } catch (A
&x
) {}
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:[^ ]*]]
88 // : [[HANDLER]]: (can't check this in Release-Asserts builds)
89 // CHECK: {{%.*}} = call i32 @llvm.eh.typeid.for(i8* bitcast ({{%.*}}* @_ZTIN5test51AE to i8*))
93 template <class T
> struct allocator
{
94 ~allocator() throw() { }
104 // CHECK: define i32 @_ZN5test73fooEv()
106 // CHECK: [[CAUGHTEXNVAR:%.*]] = alloca i8*
107 // CHECK-NEXT: [[INTCATCHVAR:%.*]] = alloca i32
108 // CHECK-NEXT: [[EHCLEANUPDESTVAR:%.*]] = alloca i32
111 // CHECK-NEXT: [[EXNALLOC:%.*]] = call i8* @__cxa_allocate_exception
112 // CHECK-NEXT: bitcast i8* [[EXNALLOC]] to i32*
113 // CHECK-NEXT: store i32 1, i32*
114 // CHECK-NEXT: invoke void @__cxa_throw(i8* [[EXNALLOC]], i8* bitcast (i8** @_ZTIi to i8*), i8* null
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
124 // CHECK: load i8** [[CAUGHTEXNVAR]]
125 // CHECK-NEXT: call i8* @__cxa_begin_catch
126 // CHECK: invoke void @__cxa_rethrow
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)
134 // CHECK-NEXT: store i32 1, i32* [[EHCLEANUPDESTVAR]]
135 // 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
147 // Ordering of destructors in a catch handler.
149 struct A
{ A(const A
&); ~A(); };
152 // CHECK: define void @_ZN5test83fooEv()
155 // CHECK: invoke void @_ZN5test83barEv()
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
162 // CHECK-NEXT: invoke void @_ZN5test81AD1Ev(
163 // CHECK: call void @__cxa_end_catch()
169 // Constructor function-try-block must rethrow on fallthrough.
170 // rdar://problem/7696603
176 // CHECK: define void @_ZN5test91AC1Ev(%"struct.test10::A"* %this) unnamed_addr
177 // CHECK: call void @_ZN5test91AC2Ev
178 // CHECK-NEXT: ret void
180 // CHECK: define void @_ZN5test91AC2Ev(%"struct.test10::A"* %this) unnamed_addr
182 // CHECK: invoke void @_ZN5test96opaqueEv()
185 // CHECK: call i8* @__cxa_begin_catch
186 // CHECK: invoke void @_ZN5test96opaqueEv()
187 // CHECK: invoke void @__cxa_rethrow()
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)
196 // __cxa_end_catch can throw for some kinds of caught exceptions.
203 // CHECK: define void @_ZN6test103fooEv()
205 A a
; // force a cleanup context
208 // CHECK: invoke void @_ZN6test106opaqueEv()
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
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()
224 // CHECK: call i8* @__cxa_begin_catch
225 // CHECK-NEXT: invoke void @__cxa_end_catch()
228 // CHECK: call void @_ZN6test101AD1Ev(
232 // __cxa_begin_catch returns pointers by value, even when catching by reference
233 // <rdar://problem/8212123>
237 // CHECK: define void @_ZN6test113fooEv()
240 // CHECK: invoke void @_ZN6test116opaqueEv()
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
254 // CHECK: define void @_ZN6test113barEv()
257 // CHECK: [[EXNSLOT:%.*]] = alloca i8*
258 // CHECK-NEXT: [[P:%.*]] = alloca [[A:%.*]]**,
259 // CHECK-NEXT: [[TMP:%.*]] = alloca [[A]]*
260 // CHECK-NEXT: invoke void @_ZN6test116opaqueEv()
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
276 bool opaque(const A
&);
278 // CHECK: define void @_ZN6test124testEv()
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
287 // CHECK: invoke zeroext i1 @_ZN6test126opaqueERKNS_1AE(
292 // CHECK: invoke void @_ZN6test121AD1Ev([[A]]* [[Z]])
293 // CHECK: invoke void @_ZN6test121AD1Ev([[A]]* [[Y]])
295 // It'd be great if something eliminated this switch.
296 // CHECK: load i32* [[CLEANUPDEST]]
297 // CHECK-NEXT: switch i32
304 // CHECK: call void @_ZN6test121AD1Ev([[A]]* [[X]])
305 // CHECK-NEXT: ret void
309 // Reduced from some TableGen code that was causing a self-host crash.
341 // rdar://problem/8231514
359 // rdar://problem/8231514
360 // JumpDests shouldn't get confused by scopes that aren't normal cleanups.
366 // CHECK: define void @_ZN6test153fooEv()
371 // CHECK: [[X:%.*]] = alloca i32
372 // CHECK: store i32 10, i32* [[X]]
373 // CHECK-NEXT: br label
378 // CHECK: load i32* [[X]]
379 // CHECK-NEXT: [[COND:%.*]] = invoke zeroext i1 @_ZN6test156opaqueEi
380 // CHECK: br i1 [[COND]]
390 // CHECK: call void @_ZN6test151AD1Ev
395 struct A
{ A(); ~A(); };
396 struct B
{ int x
; B(const A
&); ~B(); };
400 // CHECK: define void @_ZN6test163barEv()
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
409 cond() ? throw B(A()) : foo();
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]],
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]],
426 // CHECK: invoke void @_ZN6test163fooEv()
429 // CHECK: invoke void @_ZN6test161AD1Ev([[A]]* [[TEMP]])
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