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 ptr @__cxa_allocate_exception(i64 8)
14 // UNALIGNED-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[EXNOBJ]], ptr align 8 @d1, i64 8, i1 false)
15 // ALIGNED-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 16 [[EXNOBJ]], ptr align 8 @d1, i64 8, i1 false)
16 // CHECK-NEXT: call void @__cxa_throw(ptr [[EXNOBJ]], ptr @_ZTI7test1_D, ptr null) [[NR:#[0-9]+]]
17 // CHECK-NEXT: unreachable
21 test2_D(const test2_D
&o
);
23 virtual void bar() { }
31 // CHECK-LABEL: define{{.*}} void @_Z5test2v()
32 // CHECK: [[EXNVAR:%.*]] = alloca ptr
33 // CHECK-NEXT: [[SELECTORVAR:%.*]] = alloca i32
34 // CHECK-NEXT: [[EXNOBJ:%.*]] = call ptr @__cxa_allocate_exception(i64 16)
35 // CHECK-NEXT: invoke void @_ZN7test2_DC1ERKS_(ptr {{[^,]*}} [[EXNOBJ]], ptr noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) @d2)
36 // CHECK-NEXT: to label %[[CONT:.*]] unwind label %{{.*}}
37 // : [[CONT]]: (can't check this in Release-Asserts builds)
38 // CHECK: call void @__cxa_throw(ptr [[EXNOBJ]], ptr @_ZTI7test2_D, ptr null) [[NR]]
39 // CHECK-NEXT: unreachable
44 test3_D(volatile test3_D
&o
);
49 throw (volatile test3_D
*)0;
52 // CHECK-LABEL: define{{.*}} void @_Z5test3v()
53 // CHECK: [[EXNOBJ:%.*]] = call ptr @__cxa_allocate_exception(i64 8)
54 // CHECK-NEXT: store ptr null, ptr [[EXNOBJ]]
55 // CHECK-NEXT: call void @__cxa_throw(ptr [[EXNOBJ]], ptr @_ZTIPV7test3_D, ptr null) [[NR]]
56 // CHECK-NEXT: unreachable
63 // CHECK-LABEL: define{{.*}} void @_Z5test4v()
64 // CHECK: call void @__cxa_rethrow() [[NR]]
65 // CHECK-NEXT: unreachable
75 try { throw A(); } catch (A
&x
) {}
77 // CHECK-LABEL: define{{.*}} void @_ZN5test54testEv()
78 // CHECK: [[EXNOBJ:%.*]] = call ptr @__cxa_allocate_exception(i64 1)
79 // CHECK: invoke void @_ZN5test51AC1Ev(ptr {{[^,]*}} [[EXNOBJ]])
80 // CHECK: invoke void @__cxa_throw(ptr [[EXNOBJ]], ptr @_ZTIN5test51AE, ptr @_ZN5test51AD1Ev) [[NR]]
81 // CHECK-NEXT: to label {{%.*}} unwind label %[[HANDLER:[^ ]*]]
82 // : [[HANDLER]]: (can't check this in Release-Asserts builds)
83 // CHECK: {{%.*}} = call i32 @llvm.eh.typeid.for(ptr @_ZTIN5test51AE)
87 template <class T
> struct allocator
{
88 ~allocator() throw() { }
98 // CHECK-LABEL: define{{.*}} i32 @_ZN5test73fooEv()
99 // CHECK-SAME: personality ptr @__gxx_personality_v0
101 // CHECK: [[CAUGHTEXNVAR:%.*]] = alloca ptr
102 // CHECK-NEXT: [[SELECTORVAR:%.*]] = alloca i32
103 // CHECK-NEXT: [[INTCATCHVAR:%.*]] = alloca i32
106 // CHECK-NEXT: [[EXNALLOC:%.*]] = call ptr @__cxa_allocate_exception
107 // CHECK-NEXT: store i32 1, ptr
108 // CHECK-NEXT: invoke void @__cxa_throw(ptr [[EXNALLOC]], ptr @_ZTIi, ptr null
112 // CHECK: [[CAUGHTVAL:%.*]] = landingpad { ptr, i32 }
113 // CHECK-NEXT: catch ptr @_ZTIi
114 // CHECK-NEXT: catch ptr null
115 // CHECK-NEXT: [[CAUGHTEXN:%.*]] = extractvalue { ptr, i32 } [[CAUGHTVAL]], 0
116 // CHECK-NEXT: store ptr [[CAUGHTEXN]], ptr [[CAUGHTEXNVAR]]
117 // CHECK-NEXT: [[SELECTOR:%.*]] = extractvalue { ptr, i32 } [[CAUGHTVAL]], 1
118 // CHECK-NEXT: store i32 [[SELECTOR]], ptr [[SELECTORVAR]]
119 // CHECK-NEXT: br label
120 // CHECK: [[SELECTOR:%.*]] = load i32, ptr [[SELECTORVAR]]
121 // CHECK-NEXT: [[T0:%.*]] = call i32 @llvm.eh.typeid.for(ptr @_ZTIi)
122 // CHECK-NEXT: icmp eq i32 [[SELECTOR]], [[T0]]
124 // CHECK: [[T0:%.*]] = load ptr, ptr [[CAUGHTEXNVAR]]
125 // CHECK-NEXT: [[T1:%.*]] = call ptr @__cxa_begin_catch(ptr [[T0]])
126 // CHECK-NEXT: [[T3:%.*]] = load i32, ptr [[T1]]
127 // CHECK-NEXT: store i32 [[T3]], ptr {{%.*}}, align 4
128 // CHECK-NEXT: invoke void @__cxa_rethrow
133 // CHECK: [[CAUGHTVAL:%.*]] = landingpad { ptr, i32 }
134 // CHECK-NEXT: catch ptr null
135 // CHECK-NEXT: [[CAUGHTEXN:%.*]] = extractvalue { ptr, i32 } [[CAUGHTVAL]], 0
136 // CHECK-NEXT: store ptr [[CAUGHTEXN]], ptr [[CAUGHTEXNVAR]]
137 // CHECK-NEXT: [[SELECTOR:%.*]] = extractvalue { ptr, i32 } [[CAUGHTVAL]], 1
138 // CHECK-NEXT: store i32 [[SELECTOR]], ptr [[SELECTORVAR]]
139 // CHECK-NEXT: call void @__cxa_end_catch()
140 // CHECK-NEXT: br label
141 // CHECK: load ptr, ptr [[CAUGHTEXNVAR]]
142 // CHECK-NEXT: call ptr @__cxa_begin_catch
143 // CHECK-NEXT: call void @__cxa_end_catch
151 // Ordering of destructors in a catch handler.
153 struct A
{ A(const A
&); ~A(); };
156 // CHECK-LABEL: define{{.*}} void @_ZN5test83fooEv()
159 // CHECK: invoke void @_ZN5test83barEv()
162 // CHECK: call ptr @__cxa_get_exception_ptr
163 // CHECK-NEXT: invoke void @_ZN5test81AC1ERKS0_(
164 // CHECK: call ptr @__cxa_begin_catch
165 // CHECK-NEXT: call void @_ZN5test81AD1Ev(
166 // CHECK: call void @__cxa_end_catch()
172 // Constructor function-try-block must rethrow on fallthrough.
179 // CHECK-LABEL: define{{.*}} void @_ZN5test91AC2Ev(ptr {{[^,]*}} %this) unnamed_addr
180 // CHECK-SAME: personality ptr @__gxx_personality_v0
182 // CHECK: invoke void @_ZN5test96opaqueEv()
185 // CHECK: landingpad { ptr, i32 }
186 // CHECK-NEXT: catch ptr @_ZTIi
188 // CHECK: call ptr @__cxa_begin_catch
189 // CHECK: invoke void @_ZN5test96opaqueEv()
190 // CHECK: invoke void @__cxa_rethrow()
192 // CHECK-LABEL: define{{.*}} void @_ZN5test91AC1Ev(ptr {{[^,]*}} %this) unnamed_addr
193 // CHECK: call void @_ZN5test91AC2Ev
194 // CHECK-NEXT: ret void
199 // __cxa_end_catch can throw for some kinds of caught exceptions.
206 // CHECK-LABEL: define{{.*}} void @_ZN6test103fooEv()
208 A a
; // force a cleanup context
211 // CHECK: invoke void @_ZN6test106opaqueEv()
214 // CHECK: call ptr @__cxa_begin_catch
215 // CHECK-NEXT: load i32, ptr
216 // CHECK-NEXT: store i32
217 // CHECK-NEXT: call void @__cxa_end_catch() [[NUW:#[0-9]+]]
219 // CHECK: call ptr @__cxa_begin_catch
220 // CHECK-NEXT: call void @llvm.memcpy
221 // CHECK-NEXT: invoke void @__cxa_end_catch()
223 // CHECK: call ptr @__cxa_begin_catch
224 // CHECK-NEXT: invoke void @__cxa_end_catch()
227 // CHECK: call void @_ZN6test101AD1Ev(
231 // __cxa_begin_catch returns pointers by value, even when catching by reference
235 // CHECK-LABEL: define{{.*}} void @_ZN6test113fooEv()
238 // CHECK: invoke void @_ZN6test116opaqueEv()
241 // CHECK: [[EXN:%.*]] = load ptr, ptr
242 // CHECK-NEXT: call ptr @__cxa_begin_catch(ptr [[EXN]]) [[NUW]]
243 // CHECK-NEXT: [[ADJ1:%.*]] = getelementptr i8, ptr [[EXN]], i32 32
244 // CHECK-NEXT: store ptr [[ADJ1]], ptr [[P:%.*]]
245 // CHECK-NEXT: call void @__cxa_end_catch() [[NUW]]
251 // CHECK-LABEL: define{{.*}} void @_ZN6test113barEv()
254 // CHECK: [[EXNSLOT:%.*]] = alloca ptr
255 // CHECK-NEXT: [[SELECTORSLOT:%.*]] = alloca i32
256 // CHECK-NEXT: [[P:%.*]] = alloca ptr,
257 // CHECK-NEXT: [[TMP:%.*]] = alloca ptr
258 // CHECK-NEXT: invoke void @_ZN6test116opaqueEv()
261 // CHECK: [[EXN:%.*]] = load ptr, ptr [[EXNSLOT]]
262 // CHECK-NEXT: [[ADJ1:%.*]] = call ptr @__cxa_begin_catch(ptr [[EXN]]) [[NUW]]
263 // CHECK-NEXT: store ptr [[ADJ1]], ptr [[TMP]]
264 // CHECK-NEXT: store ptr [[TMP]], ptr [[P]]
265 // CHECK-NEXT: call void @__cxa_end_catch() [[NUW]]
272 struct A
{ ~A() noexcept(false); };
273 bool opaque(const A
&);
275 // CHECK-LABEL: define{{.*}} void @_ZN6test124testEv()
277 // CHECK: [[X:%.*]] = alloca [[A:%.*]],
278 // CHECK: [[EHCLEANUPDEST:%.*]] = alloca i32
279 // CHECK: [[Y:%.*]] = alloca [[A:%.*]]
280 // CHECK: [[Z:%.*]] = alloca [[A]]
281 // CHECK: [[CLEANUPDEST:%.*]] = alloca i32
284 // CHECK: invoke noundef zeroext i1 @_ZN6test126opaqueERKNS_1AE(
289 // CHECK: invoke void @_ZN6test121AD1Ev(ptr {{[^,]*}} [[Z]])
290 // CHECK: invoke void @_ZN6test121AD1Ev(ptr {{[^,]*}} [[Y]])
298 // CHECK: call void @_ZN6test121AD1Ev(ptr {{[^,]*}} [[X]])
299 // CHECK-NEXT: ret void
303 // Reduced from some TableGen code that was causing a self-host crash.
352 // JumpDests shouldn't get confused by scopes that aren't normal cleanups.
358 // CHECK-LABEL: define{{.*}} void @_ZN6test153fooEv()
363 // CHECK: [[X:%.*]] = alloca i32
364 // CHECK: store i32 10, ptr [[X]]
365 // CHECK-NEXT: br label
370 // CHECK: load i32, ptr [[X]]
371 // CHECK-NEXT: [[COND:%.*]] = invoke noundef zeroext i1 @_ZN6test156opaqueEi
372 // CHECK: br i1 [[COND]]
382 // CHECK: call void @_ZN6test151AD1Ev
387 struct A
{ A(); ~A() noexcept(false); };
388 struct B
{ int x
; B(const A
&); ~B() noexcept(false); };
392 // CHECK-LABEL: define{{.*}} void @_ZN6test163barEv()
394 // CHECK: [[EXN_SAVE:%.*]] = alloca ptr
395 // CHECK-NEXT: [[EXN_ACTIVE:%.*]] = alloca i1
396 // CHECK-NEXT: [[TEMP:%.*]] = alloca [[A:%.*]],
397 // CHECK-NEXT: [[EXNSLOT:%.*]] = alloca ptr
398 // CHECK-NEXT: [[SELECTORSLOT:%.*]] = alloca i32
399 // CHECK-NEXT: [[TEMP_ACTIVE:%.*]] = alloca i1
401 cond() ? throw B(A()) : foo();
403 // CHECK-NEXT: [[COND:%.*]] = call noundef zeroext i1 @_ZN6test164condEv()
404 // CHECK-NEXT: store i1 false, ptr [[EXN_ACTIVE]]
405 // CHECK-NEXT: store i1 false, ptr [[TEMP_ACTIVE]]
406 // CHECK-NEXT: br i1 [[COND]],
408 // CHECK: [[EXN:%.*]] = call ptr @__cxa_allocate_exception(i64 4)
409 // CHECK-NEXT: store ptr [[EXN]], ptr [[EXN_SAVE]]
410 // CHECK-NEXT: store i1 true, ptr [[EXN_ACTIVE]]
411 // CHECK-NEXT: invoke void @_ZN6test161AC1Ev(ptr {{[^,]*}} [[TEMP]])
412 // CHECK: store i1 true, ptr [[TEMP_ACTIVE]]
413 // CHECK-NEXT: invoke void @_ZN6test161BC1ERKNS_1AE(ptr {{[^,]*}} [[EXN]], ptr noundef nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) [[TEMP]])
414 // CHECK: store i1 false, ptr [[EXN_ACTIVE]]
415 // CHECK-NEXT: invoke void @__cxa_throw(ptr [[EXN]],
417 // CHECK: invoke void @_ZN6test163fooEv()
420 // CHECK: invoke void @_ZN6test161AD1Ev(ptr {{[^,]*}} [[TEMP]])
423 // CHECK: [[T0:%.*]] = load i1, ptr [[EXN_ACTIVE]]
424 // CHECK-NEXT: br i1 [[T0]]
425 // CHECK: [[T1:%.*]] = load ptr, ptr [[EXN_SAVE]]
426 // CHECK-NEXT: call void @__cxa_free_exception(ptr [[T1]])
427 // CHECK-NEXT: br label
432 class BaseException
{
439 class DerivedException
: public BaseException
{
443 throw DerivedException();
444 // The alignment passed to memset is 16 on Darwin.
446 // CHECK: [[T0:%.*]] = call ptr @__cxa_allocate_exception(i64 16)
447 // UNALIGNED-NEXT: call void @llvm.memset.p0.i64(ptr align 8 [[T0]], i8 0, i64 16, i1 false)
448 // ALIGNED-NEXT: call void @llvm.memset.p0.i64(ptr align 16 [[T0]], i8 0, i64 16, i1 false)
452 // CHECK: attributes [[NUW]] = { nounwind }
453 // CHECK: attributes [[NR]] = { noreturn }