1 // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -triple x86_64-apple-macosx10.13.99 -std=c++11 -emit-llvm -o - %s | FileCheck --check-prefix=CHECK --check-prefix=UNALIGNED %s
2 // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -triple x86_64-apple-macosx10.14 -std=c++11 -emit-llvm -o - %s | FileCheck --check-prefix=CHECK --check-prefix=ALIGNED %s
12 // CHECK-LABEL: 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 // UNALIGNED-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 [[EXN2]], i8* align 8 bitcast ([[DSTAR]] @d1 to i8*), i64 8, i1 false)
17 // ALIGNED-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 16 [[EXN2]], i8* align 8 bitcast ([[DSTAR]] @d1 to i8*), i64 8, i1 false)
18 // CHECK-NEXT: call void @__cxa_throw(i8* [[EXNOBJ]], i8* bitcast ({ i8*, i8* }* @_ZTI7test1_D to i8*), i8* null) [[NR:#[0-9]+]]
19 // CHECK-NEXT: unreachable
23 test2_D(const test2_D
&o
);
25 virtual void bar() { }
33 // CHECK-LABEL: define{{.*}} void @_Z5test2v()
34 // CHECK: [[EXNVAR:%.*]] = alloca i8*
35 // CHECK-NEXT: [[SELECTORVAR:%.*]] = alloca i32
36 // CHECK-NEXT: [[EXNOBJ:%.*]] = call i8* @__cxa_allocate_exception(i64 16)
37 // CHECK-NEXT: [[EXN:%.*]] = bitcast i8* [[EXNOBJ]] to [[DSTAR:%[^*]*\*]]
38 // CHECK-NEXT: invoke void @_ZN7test2_DC1ERKS_([[DSTAR]] {{[^,]*}} [[EXN]], [[DSTAR]] noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) @d2)
39 // CHECK-NEXT: to label %[[CONT:.*]] unwind label %{{.*}}
40 // : [[CONT]]: (can't check this in Release-Asserts builds)
41 // CHECK: call void @__cxa_throw(i8* [[EXNOBJ]], i8* bitcast ({{.*}}* @_ZTI7test2_D to i8*), i8* null) [[NR]]
42 // CHECK-NEXT: unreachable
47 test3_D(volatile test3_D
&o
);
52 throw (volatile test3_D
*)0;
55 // CHECK-LABEL: define{{.*}} void @_Z5test3v()
56 // CHECK: [[EXNOBJ:%.*]] = call i8* @__cxa_allocate_exception(i64 8)
57 // CHECK-NEXT: [[EXN:%.*]] = bitcast i8* [[EXNOBJ]] to [[D:%[^*]+]]**
58 // CHECK-NEXT: store [[D]]* null, [[D]]** [[EXN]]
59 // CHECK-NEXT: call void @__cxa_throw(i8* [[EXNOBJ]], i8* bitcast ({ i8*, i8*, i32, i8* }* @_ZTIPV7test3_D to i8*), i8* null) [[NR]]
60 // CHECK-NEXT: unreachable
67 // CHECK-LABEL: define{{.*}} void @_Z5test4v()
68 // CHECK: call void @__cxa_rethrow() [[NR]]
69 // CHECK-NEXT: unreachable
72 // rdar://problem/7696549
81 try { throw A(); } catch (A
&x
) {}
83 // CHECK-LABEL: define{{.*}} void @_ZN5test54testEv()
84 // CHECK: [[EXNOBJ:%.*]] = call i8* @__cxa_allocate_exception(i64 1)
85 // CHECK: [[EXNCAST:%.*]] = bitcast i8* [[EXNOBJ]] to [[A:%[^*]*]]*
86 // CHECK-NEXT: invoke void @_ZN5test51AC1Ev([[A]]* {{[^,]*}} [[EXNCAST]])
87 // CHECK: invoke void @__cxa_throw(i8* [[EXNOBJ]], i8* bitcast ({{.*}}* @_ZTIN5test51AE to i8*), i8* bitcast (void ([[A]]*)* @_ZN5test51AD1Ev to i8*)) [[NR]]
88 // CHECK-NEXT: to label {{%.*}} unwind label %[[HANDLER:[^ ]*]]
89 // : [[HANDLER]]: (can't check this in Release-Asserts builds)
90 // CHECK: {{%.*}} = call i32 @llvm.eh.typeid.for(i8* bitcast ({{.*}}* @_ZTIN5test51AE to i8*))
94 template <class T
> struct allocator
{
95 ~allocator() throw() { }
105 // CHECK-LABEL: define{{.*}} i32 @_ZN5test73fooEv()
106 // CHECK-SAME: personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*)
108 // CHECK: [[CAUGHTEXNVAR:%.*]] = alloca i8*
109 // CHECK-NEXT: [[SELECTORVAR:%.*]] = alloca i32
110 // CHECK-NEXT: [[INTCATCHVAR:%.*]] = alloca i32
113 // CHECK-NEXT: [[EXNALLOC:%.*]] = call i8* @__cxa_allocate_exception
114 // CHECK-NEXT: bitcast i8* [[EXNALLOC]] to i32*
115 // CHECK-NEXT: store i32 1, i32*
116 // CHECK-NEXT: invoke void @__cxa_throw(i8* [[EXNALLOC]], i8* bitcast (i8** @_ZTIi to i8*), i8* null
120 // CHECK: [[CAUGHTVAL:%.*]] = landingpad { i8*, i32 }
121 // CHECK-NEXT: catch i8* bitcast (i8** @_ZTIi to i8*)
122 // CHECK-NEXT: catch i8* null
123 // CHECK-NEXT: [[CAUGHTEXN:%.*]] = extractvalue { i8*, i32 } [[CAUGHTVAL]], 0
124 // CHECK-NEXT: store i8* [[CAUGHTEXN]], i8** [[CAUGHTEXNVAR]]
125 // CHECK-NEXT: [[SELECTOR:%.*]] = extractvalue { i8*, i32 } [[CAUGHTVAL]], 1
126 // CHECK-NEXT: store i32 [[SELECTOR]], i32* [[SELECTORVAR]]
127 // CHECK-NEXT: br label
128 // CHECK: [[SELECTOR:%.*]] = load i32, i32* [[SELECTORVAR]]
129 // CHECK-NEXT: [[T0:%.*]] = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
130 // CHECK-NEXT: icmp eq i32 [[SELECTOR]], [[T0]]
132 // CHECK: [[T0:%.*]] = load i8*, i8** [[CAUGHTEXNVAR]]
133 // CHECK-NEXT: [[T1:%.*]] = call i8* @__cxa_begin_catch(i8* [[T0]])
134 // CHECK-NEXT: [[T2:%.*]] = bitcast i8* [[T1]] to i32*
135 // CHECK-NEXT: [[T3:%.*]] = load i32, i32* [[T2]]
136 // CHECK-NEXT: store i32 [[T3]], i32* {{%.*}}, align 4
137 // CHECK-NEXT: invoke void @__cxa_rethrow
142 // CHECK: [[CAUGHTVAL:%.*]] = landingpad { i8*, i32 }
143 // CHECK-NEXT: catch i8* null
144 // CHECK-NEXT: [[CAUGHTEXN:%.*]] = extractvalue { i8*, i32 } [[CAUGHTVAL]], 0
145 // CHECK-NEXT: store i8* [[CAUGHTEXN]], i8** [[CAUGHTEXNVAR]]
146 // CHECK-NEXT: [[SELECTOR:%.*]] = extractvalue { i8*, i32 } [[CAUGHTVAL]], 1
147 // CHECK-NEXT: store i32 [[SELECTOR]], i32* [[SELECTORVAR]]
148 // CHECK-NEXT: call void @__cxa_end_catch()
149 // CHECK-NEXT: br label
150 // CHECK: load i8*, i8** [[CAUGHTEXNVAR]]
151 // CHECK-NEXT: call i8* @__cxa_begin_catch
152 // CHECK-NEXT: call void @__cxa_end_catch
160 // Ordering of destructors in a catch handler.
162 struct A
{ A(const A
&); ~A(); };
165 // CHECK-LABEL: define{{.*}} void @_ZN5test83fooEv()
168 // CHECK: invoke void @_ZN5test83barEv()
171 // CHECK: call i8* @__cxa_get_exception_ptr
172 // CHECK-NEXT: bitcast
173 // CHECK-NEXT: invoke void @_ZN5test81AC1ERKS0_(
174 // CHECK: call i8* @__cxa_begin_catch
175 // CHECK-NEXT: call void @_ZN5test81AD1Ev(
176 // CHECK: call void @__cxa_end_catch()
182 // Constructor function-try-block must rethrow on fallthrough.
183 // rdar://problem/7696603
190 // CHECK-LABEL: define{{.*}} void @_ZN5test91AC2Ev(%"struct.test9::A"* {{[^,]*}} %this) unnamed_addr
191 // CHECK-SAME: personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*)
193 // CHECK: invoke void @_ZN5test96opaqueEv()
196 // CHECK: landingpad { i8*, i32 }
197 // CHECK-NEXT: catch i8* bitcast (i8** @_ZTIi to i8*)
199 // CHECK: call i8* @__cxa_begin_catch
200 // CHECK: invoke void @_ZN5test96opaqueEv()
201 // CHECK: invoke void @__cxa_rethrow()
203 // CHECK-LABEL: define{{.*}} void @_ZN5test91AC1Ev(%"struct.test9::A"* {{[^,]*}} %this) unnamed_addr
204 // CHECK: call void @_ZN5test91AC2Ev
205 // CHECK-NEXT: ret void
210 // __cxa_end_catch can throw for some kinds of caught exceptions.
217 // CHECK-LABEL: define{{.*}} void @_ZN6test103fooEv()
219 A a
; // force a cleanup context
222 // CHECK: invoke void @_ZN6test106opaqueEv()
225 // CHECK: call i8* @__cxa_begin_catch
226 // CHECK-NEXT: bitcast
227 // CHECK-NEXT: load i32, i32*
228 // CHECK-NEXT: store i32
229 // CHECK-NEXT: call void @__cxa_end_catch() [[NUW:#[0-9]+]]
231 // CHECK: call i8* @__cxa_begin_catch
232 // CHECK-NEXT: bitcast
233 // CHECK-NEXT: bitcast
234 // CHECK-NEXT: bitcast
235 // CHECK-NEXT: call void @llvm.memcpy
236 // CHECK-NEXT: invoke void @__cxa_end_catch()
238 // CHECK: call i8* @__cxa_begin_catch
239 // CHECK-NEXT: invoke void @__cxa_end_catch()
242 // CHECK: call void @_ZN6test101AD1Ev(
246 // __cxa_begin_catch returns pointers by value, even when catching by reference
247 // <rdar://problem/8212123>
251 // CHECK-LABEL: define{{.*}} void @_ZN6test113fooEv()
254 // CHECK: invoke void @_ZN6test116opaqueEv()
257 // CHECK: [[EXN:%.*]] = load i8*, i8**
258 // CHECK-NEXT: call i8* @__cxa_begin_catch(i8* [[EXN]]) [[NUW]]
259 // CHECK-NEXT: [[ADJ1:%.*]] = getelementptr i8, i8* [[EXN]], i32 32
260 // CHECK-NEXT: [[ADJ2:%.*]] = bitcast i8* [[ADJ1]] to i32***
261 // CHECK-NEXT: store i32*** [[ADJ2]], i32**** [[P:%.*]]
262 // CHECK-NEXT: call void @__cxa_end_catch() [[NUW]]
268 // CHECK-LABEL: define{{.*}} void @_ZN6test113barEv()
271 // CHECK: [[EXNSLOT:%.*]] = alloca i8*
272 // CHECK-NEXT: [[SELECTORSLOT:%.*]] = alloca i32
273 // CHECK-NEXT: [[P:%.*]] = alloca [[A:%.*]]**,
274 // CHECK-NEXT: [[TMP:%.*]] = alloca [[A]]*
275 // CHECK-NEXT: invoke void @_ZN6test116opaqueEv()
278 // CHECK: [[EXN:%.*]] = load i8*, i8** [[EXNSLOT]]
279 // CHECK-NEXT: [[ADJ1:%.*]] = call i8* @__cxa_begin_catch(i8* [[EXN]]) [[NUW]]
280 // CHECK-NEXT: [[ADJ2:%.*]] = bitcast i8* [[ADJ1]] to [[A]]*
281 // CHECK-NEXT: store [[A]]* [[ADJ2]], [[A]]** [[TMP]]
282 // CHECK-NEXT: store [[A]]** [[TMP]], [[A]]*** [[P]]
283 // CHECK-NEXT: call void @__cxa_end_catch() [[NUW]]
290 struct A
{ ~A() noexcept(false); };
291 bool opaque(const A
&);
293 // CHECK-LABEL: define{{.*}} void @_ZN6test124testEv()
295 // CHECK: [[X:%.*]] = alloca [[A:%.*]],
296 // CHECK: [[EHCLEANUPDEST:%.*]] = alloca i32
297 // CHECK: [[Y:%.*]] = alloca [[A]]
298 // CHECK: [[Z:%.*]] = alloca [[A]]
299 // CHECK: [[CLEANUPDEST:%.*]] = alloca i32
302 // CHECK: invoke noundef zeroext i1 @_ZN6test126opaqueERKNS_1AE(
307 // CHECK: invoke void @_ZN6test121AD1Ev([[A]]* {{[^,]*}} [[Z]])
308 // CHECK: invoke void @_ZN6test121AD1Ev([[A]]* {{[^,]*}} [[Y]])
316 // CHECK: call void @_ZN6test121AD1Ev([[A]]* {{[^,]*}} [[X]])
317 // CHECK-NEXT: ret void
321 // Reduced from some TableGen code that was causing a self-host crash.
353 // rdar://problem/8231514
371 // rdar://problem/8231514
372 // JumpDests shouldn't get confused by scopes that aren't normal cleanups.
378 // CHECK-LABEL: define{{.*}} void @_ZN6test153fooEv()
383 // CHECK: [[X:%.*]] = alloca i32
384 // CHECK: store i32 10, i32* [[X]]
385 // CHECK-NEXT: br label
390 // CHECK: load i32, i32* [[X]]
391 // CHECK-NEXT: [[COND:%.*]] = invoke noundef zeroext i1 @_ZN6test156opaqueEi
392 // CHECK: br i1 [[COND]]
402 // CHECK: call void @_ZN6test151AD1Ev
407 struct A
{ A(); ~A() noexcept(false); };
408 struct B
{ int x
; B(const A
&); ~B() noexcept(false); };
412 // CHECK-LABEL: define{{.*}} void @_ZN6test163barEv()
414 // CHECK: [[EXN_SAVE:%.*]] = alloca i8*
415 // CHECK-NEXT: [[EXN_ACTIVE:%.*]] = alloca i1
416 // CHECK-NEXT: [[TEMP:%.*]] = alloca [[A:%.*]],
417 // CHECK-NEXT: [[EXNSLOT:%.*]] = alloca i8*
418 // CHECK-NEXT: [[SELECTORSLOT:%.*]] = alloca i32
419 // CHECK-NEXT: [[TEMP_ACTIVE:%.*]] = alloca i1
421 cond() ? throw B(A()) : foo();
423 // CHECK-NEXT: [[COND:%.*]] = call noundef zeroext i1 @_ZN6test164condEv()
424 // CHECK-NEXT: store i1 false, i1* [[EXN_ACTIVE]]
425 // CHECK-NEXT: store i1 false, i1* [[TEMP_ACTIVE]]
426 // CHECK-NEXT: br i1 [[COND]],
428 // CHECK: [[EXN:%.*]] = call i8* @__cxa_allocate_exception(i64 4)
429 // CHECK-NEXT: store i8* [[EXN]], i8** [[EXN_SAVE]]
430 // CHECK-NEXT: store i1 true, i1* [[EXN_ACTIVE]]
431 // CHECK-NEXT: [[T0:%.*]] = bitcast i8* [[EXN]] to [[B:%.*]]*
432 // CHECK-NEXT: invoke void @_ZN6test161AC1Ev([[A]]* {{[^,]*}} [[TEMP]])
433 // CHECK: store i1 true, i1* [[TEMP_ACTIVE]]
434 // CHECK-NEXT: invoke void @_ZN6test161BC1ERKNS_1AE([[B]]* {{[^,]*}} [[T0]], [[A]]* noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) [[TEMP]])
435 // CHECK: store i1 false, i1* [[EXN_ACTIVE]]
436 // CHECK-NEXT: invoke void @__cxa_throw(i8* [[EXN]],
438 // CHECK: invoke void @_ZN6test163fooEv()
441 // CHECK: invoke void @_ZN6test161AD1Ev([[A]]* {{[^,]*}} [[TEMP]])
444 // CHECK: [[T0:%.*]] = load i1, i1* [[EXN_ACTIVE]]
445 // CHECK-NEXT: br i1 [[T0]]
446 // CHECK: [[T1:%.*]] = load i8*, i8** [[EXN_SAVE]]
447 // CHECK-NEXT: call void @__cxa_free_exception(i8* [[T1]])
448 // CHECK-NEXT: br label
453 class BaseException
{
460 class DerivedException
: public BaseException
{
464 throw DerivedException();
465 // The alignment passed to memset is 16 on Darwin.
467 // CHECK: [[T0:%.*]] = call i8* @__cxa_allocate_exception(i64 16)
468 // CHECK-NEXT: [[T1:%.*]] = bitcast i8* [[T0]] to %"class.test17::DerivedException"*
469 // CHECK-NEXT: [[T2:%.*]] = bitcast %"class.test17::DerivedException"* [[T1]] to i8*
470 // UNALIGNED-NEXT: call void @llvm.memset.p0i8.i64(i8* align 8 [[T2]], i8 0, i64 16, i1 false)
471 // ALIGNED-NEXT: call void @llvm.memset.p0i8.i64(i8* align 16 [[T2]], i8 0, i64 16, i1 false)
475 // CHECK: attributes [[NUW]] = { nounwind }
476 // CHECK: attributes [[NR]] = { noreturn }