blob: fc61f4099e0edc7c99415e784c3e1de493bb2ef7 [file] [log] [blame]
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +00001; RUN: not llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-keep-registers -exception-model=wasm
Heejin Ahnd6f48782019-01-30 03:21:57 +00002; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -exception-model=wasm -mattr=+exception-handling -verify-machineinstrs | FileCheck -allow-deprecated-dag-overlap %s
Heejin Ahn5b023e02018-11-02 18:38:52 +00003; RUN: llc < %s -disable-wasm-fallthrough-return-opt -wasm-keep-registers -exception-model=wasm -mattr=+exception-handling
Heejin Ahnac62b052017-06-30 00:43:15 +00004
5target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
Sam Clegga5908002018-05-10 17:49:11 +00006target triple = "wasm32-unknown-unknown"
Heejin Ahnac62b052017-06-30 00:43:15 +00007
Heejin Ahn78297632019-02-26 03:29:59 +00008%struct.Temp = type { i8 }
Heejin Ahnac62b052017-06-30 00:43:15 +00009
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000010@_ZTIi = external constant i8*
11
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000012; CHECK-LABEL: test_throw:
Heejin Ahn78297632019-02-26 03:29:59 +000013; CHECK: throw __cpp_exception, $0
14; CHECK-NOT: unreachable
Heejin Ahn095796a2018-11-16 00:47:18 +000015define void @test_throw(i8* %p) {
16 call void @llvm.wasm.throw(i32 0, i8* %p)
Heejin Ahnac62b052017-06-30 00:43:15 +000017 ret void
18}
19
Heejin Ahn78297632019-02-26 03:29:59 +000020; Simple test with a try-catch
21;
22; void foo();
23; void test_catch() {
24; try {
25; foo();
26; } catch (int) {
27; }
28; }
29
30; CHECK-LABEL: test_catch:
31; CHECK: global.get ${{.+}}=, __stack_pointer
Heejin Ahncf699b42019-02-27 00:50:53 +000032; CHECK: try
33; CHECK: call foo
Heejin Ahn9f96a582019-07-15 22:49:25 +000034; CHECK: catch $[[EXNREF:[0-9]+]]=
Heejin Ahncf699b42019-02-27 00:50:53 +000035; CHECK: global.set __stack_pointer
36; CHECK: block i32
Heejin Ahn9f96a582019-07-15 22:49:25 +000037; CHECK: br_on_exn 0, __cpp_exception, $[[EXNREF]]
38; CHECK: rethrow $[[EXNREF]]
Heejin Ahncf699b42019-02-27 00:50:53 +000039; CHECK: end_block
40; CHECK: extract_exception $[[EXN:[0-9]+]]=
41; CHECK-DAG: i32.store __wasm_lpad_context
42; CHECK-DAG: i32.store __wasm_lpad_context+4
43; CHECK: i32.call $drop=, _Unwind_CallPersonality, $[[EXN]]
44; CHECK: block
45; CHECK: br_if 0
46; CHECK: i32.call $drop=, __cxa_begin_catch
47; CHECK: call __cxa_end_catch
48; CHECK: br 1
49; CHECK: end_block
Heejin Ahn9f96a582019-07-15 22:49:25 +000050; CHECK: rethrow $[[EXNREF]]
Heejin Ahncf699b42019-02-27 00:50:53 +000051; CHECK: end_try
Heejin Ahn78297632019-02-26 03:29:59 +000052define void @test_catch() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000053entry:
54 invoke void @foo()
55 to label %try.cont unwind label %catch.dispatch
56
57catch.dispatch: ; preds = %entry
58 %0 = catchswitch within none [label %catch.start] unwind to caller
59
60catch.start: ; preds = %catch.dispatch
61 %1 = catchpad within %0 [i8* bitcast (i8** @_ZTIi to i8*)]
62 %2 = call i8* @llvm.wasm.get.exception(token %1)
63 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
64 %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
65 %matches = icmp eq i32 %3, %4
66 br i1 %matches, label %catch, label %rethrow
67
68catch: ; preds = %catch.start
69 %5 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
70 call void @__cxa_end_catch() [ "funclet"(token %1) ]
71 catchret from %1 to label %try.cont
72
73rethrow: ; preds = %catch.start
Heejin Ahn66ce4192019-03-16 05:38:57 +000074 call void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %1) ]
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000075 unreachable
76
77try.cont: ; preds = %entry, %catch
Heejin Ahnac62b052017-06-30 00:43:15 +000078 ret void
79}
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000080
Heejin Ahn78297632019-02-26 03:29:59 +000081; Destructor (cleanup) test
82;
83; void foo();
84; struct Temp {
85; ~Temp() {}
86; };
87; void test_cleanup() {
88; Temp t;
89; foo();
90; }
91
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000092; CHECK-LABEL: test_cleanup:
Heejin Ahncf699b42019-02-27 00:50:53 +000093; CHECK: try
94; CHECK: call foo
Heejin Ahn9f96a582019-07-15 22:49:25 +000095; CHECK: catch $[[EXNREF:[0-9]+]]=
Heejin Ahncf699b42019-02-27 00:50:53 +000096; CHECK: global.set __stack_pointer
97; CHECK: i32.call $drop=, _ZN4TempD2Ev
Heejin Ahn9f96a582019-07-15 22:49:25 +000098; CHECK: rethrow $[[EXNREF]]
Heejin Ahncf699b42019-02-27 00:50:53 +000099; CHECK: end_try
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000100define void @test_cleanup() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
101entry:
Heejin Ahn78297632019-02-26 03:29:59 +0000102 %t = alloca %struct.Temp, align 1
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000103 invoke void @foo()
104 to label %invoke.cont unwind label %ehcleanup
105
106invoke.cont: ; preds = %entry
Heejin Ahn78297632019-02-26 03:29:59 +0000107 %call = call %struct.Temp* @_ZN4TempD2Ev(%struct.Temp* %t)
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000108 ret void
109
110ehcleanup: ; preds = %entry
111 %0 = cleanuppad within none []
Heejin Ahn78297632019-02-26 03:29:59 +0000112 %call1 = call %struct.Temp* @_ZN4TempD2Ev(%struct.Temp* %t) [ "funclet"(token %0) ]
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000113 cleanupret from %0 unwind to caller
114}
115
Heejin Ahn78297632019-02-26 03:29:59 +0000116; Calling a function that may throw within a 'catch (...)' generates a
117; temrinatepad, because __cxa_end_catch() also can throw within 'catch (...)'.
118;
119; void foo();
120; void test_terminatepad() {
121; try {
122; foo();
123; } catch (...) {
124; foo();
125; }
126; }
127
Heejin Ahn4934f762018-06-25 01:07:11 +0000128; CHECK-LABEL: test_terminatepad
Heejin Ahncf699b42019-02-27 00:50:53 +0000129; CHECK: try
130; CHECK: call foo
131; CHECK: catch
132; CHECK: i32.call $drop=, __cxa_begin_catch
Heejin Ahn78297632019-02-26 03:29:59 +0000133; CHECK: try
134; CHECK: call foo
Heejin Ahnd6f48782019-01-30 03:21:57 +0000135; CHECK: catch
Heejin Ahncf699b42019-02-27 00:50:53 +0000136; CHECK: try
137; CHECK: call __cxa_end_catch
138; CHECK: catch
139; CHECK: block i32
140; CHECK: br_on_exn 0, __cpp_exception
141; CHECK: call __clang_call_terminate, 0
142; CHECK: unreachable
143; CHECK: end_block
144; CHECK: call __clang_call_terminate
145; CHECK: unreachable
146; CHECK: end_try
147; CHECK: rethrow
Heejin Ahn78297632019-02-26 03:29:59 +0000148; CHECK: end_try
Heejin Ahncf699b42019-02-27 00:50:53 +0000149; CHECK: call __cxa_end_catch
150; CHECK: end_try
Heejin Ahnd6f48782019-01-30 03:21:57 +0000151define void @test_terminatepad() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
Heejin Ahn4934f762018-06-25 01:07:11 +0000152entry:
Heejin Ahn4934f762018-06-25 01:07:11 +0000153 invoke void @foo()
Heejin Ahn4934f762018-06-25 01:07:11 +0000154 to label %try.cont unwind label %catch.dispatch
155
Heejin Ahnd6f48782019-01-30 03:21:57 +0000156catch.dispatch: ; preds = %entry
157 %0 = catchswitch within none [label %catch.start] unwind to caller
Heejin Ahn4934f762018-06-25 01:07:11 +0000158
159catch.start: ; preds = %catch.dispatch
Heejin Ahnd6f48782019-01-30 03:21:57 +0000160 %1 = catchpad within %0 [i8* null]
161 %2 = call i8* @llvm.wasm.get.exception(token %1)
162 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
163 %4 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
164 invoke void @foo() [ "funclet"(token %1) ]
165 to label %invoke.cont1 unwind label %ehcleanup
Heejin Ahn4934f762018-06-25 01:07:11 +0000166
Heejin Ahnd6f48782019-01-30 03:21:57 +0000167invoke.cont1: ; preds = %catch.start
168 call void @__cxa_end_catch() [ "funclet"(token %1) ]
169 catchret from %1 to label %try.cont
Heejin Ahn4934f762018-06-25 01:07:11 +0000170
Heejin Ahnd6f48782019-01-30 03:21:57 +0000171try.cont: ; preds = %entry, %invoke.cont1
172 ret void
Heejin Ahn4934f762018-06-25 01:07:11 +0000173
Heejin Ahnd6f48782019-01-30 03:21:57 +0000174ehcleanup: ; preds = %catch.start
175 %5 = cleanuppad within %1 []
176 invoke void @__cxa_end_catch() [ "funclet"(token %5) ]
177 to label %invoke.cont2 unwind label %terminate
Heejin Ahn4934f762018-06-25 01:07:11 +0000178
Heejin Ahnd6f48782019-01-30 03:21:57 +0000179invoke.cont2: ; preds = %ehcleanup
180 cleanupret from %5 unwind to caller
Heejin Ahn4934f762018-06-25 01:07:11 +0000181
182terminate: ; preds = %ehcleanup
Heejin Ahnd6f48782019-01-30 03:21:57 +0000183 %6 = cleanuppad within %5 []
184 %7 = call i8* @llvm.wasm.get.exception(token %6)
185 call void @__clang_call_terminate(i8* %7) [ "funclet"(token %6) ]
Heejin Ahn4934f762018-06-25 01:07:11 +0000186 unreachable
187}
188
Heejin Ahned5e06b2018-08-21 19:44:11 +0000189; Tests prologues and epilogues are not generated within EH scopes.
190; They should not be treated as funclets; BBs starting with a catch instruction
191; should not have a prologue, and BBs ending with a catchret/cleanupret should
192; not have an epilogue. This is separate from __stack_pointer restoring
193; instructions after a catch instruction.
Heejin Ahn78297632019-02-26 03:29:59 +0000194;
195; void bar(int) noexcept;
196; void test_no_prolog_epilog_in_ehpad() {
197; int stack_var = 0;
198; bar(stack_var);
199; try {
200; foo();
201; } catch (int) {
202; foo();
203; }
204; }
Heejin Ahned5e06b2018-08-21 19:44:11 +0000205
206; CHECK-LABEL: test_no_prolog_epilog_in_ehpad
Heejin Ahncf699b42019-02-27 00:50:53 +0000207; CHECK: try
208; CHECK: call foo
209; CHECK: catch
210; CHECK-NOT: global.get $push{{.+}}=, __stack_pointer
211; CHECK: global.set __stack_pointer
212; CHECK: block
Heejin Ahn78297632019-02-26 03:29:59 +0000213; CHECK: block
Heejin Ahncf699b42019-02-27 00:50:53 +0000214; CHECK: br_if 0
215; CHECK: i32.call $drop=, __cxa_begin_catch
216; CHECK: try
217; CHECK: call foo
218; CHECK: catch
219; CHECK-NOT: global.get $push{{.+}}=, __stack_pointer
220; CHECK: global.set __stack_pointer
221; CHECK: call __cxa_end_catch
222; CHECK: rethrow
223; CHECK-NOT: global.set __stack_pointer, $pop{{.+}}
224; CHECK: end_try
Heejin Ahn78297632019-02-26 03:29:59 +0000225; CHECK: end_block
Heejin Ahn66ce4192019-03-16 05:38:57 +0000226; CHECK: rethrow
Heejin Ahncf699b42019-02-27 00:50:53 +0000227; CHECK: end_block
228; CHECK-NOT: global.set __stack_pointer, $pop{{.+}}
229; CHECK: call __cxa_end_catch
230; CHECK: end_try
Heejin Ahned5e06b2018-08-21 19:44:11 +0000231define void @test_no_prolog_epilog_in_ehpad() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
232entry:
233 %stack_var = alloca i32, align 4
234 call void @bar(i32* %stack_var)
235 invoke void @foo()
236 to label %try.cont unwind label %catch.dispatch
237
238catch.dispatch: ; preds = %entry
239 %0 = catchswitch within none [label %catch.start] unwind to caller
240
241catch.start: ; preds = %catch.dispatch
242 %1 = catchpad within %0 [i8* bitcast (i8** @_ZTIi to i8*)]
243 %2 = call i8* @llvm.wasm.get.exception(token %1)
244 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
245 %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
246 %matches = icmp eq i32 %3, %4
247 br i1 %matches, label %catch, label %rethrow
248
249catch: ; preds = %catch.start
250 %5 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
251 %6 = bitcast i8* %5 to float*
252 %7 = load float, float* %6, align 4
253 invoke void @foo() [ "funclet"(token %1) ]
254 to label %invoke.cont1 unwind label %ehcleanup
255
256invoke.cont1: ; preds = %catch
257 call void @__cxa_end_catch() [ "funclet"(token %1) ]
258 catchret from %1 to label %try.cont
259
260rethrow: ; preds = %catch.start
Heejin Ahn66ce4192019-03-16 05:38:57 +0000261 call void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %1) ]
Heejin Ahned5e06b2018-08-21 19:44:11 +0000262 unreachable
263
264try.cont: ; preds = %entry, %invoke.cont1
265 ret void
266
267ehcleanup: ; preds = %catch
268 %8 = cleanuppad within %1 []
269 call void @__cxa_end_catch() [ "funclet"(token %8) ]
270 cleanupret from %8 unwind to caller
271}
272
Heejin Ahn972fc352018-08-22 21:13:49 +0000273; When a function does not have stack-allocated objects, it does not need to
274; store SP back to __stack_pointer global at the epilog.
Heejin Ahn78297632019-02-26 03:29:59 +0000275;
276; void foo();
277; void test_no_sp_writeback() {
278; try {
279; foo();
280; } catch (...) {
281; }
282; }
Heejin Ahn972fc352018-08-22 21:13:49 +0000283
Heejin Ahn78297632019-02-26 03:29:59 +0000284; CHECK-LABEL: test_no_sp_writeback
Heejin Ahncf699b42019-02-27 00:50:53 +0000285; CHECK: try
286; CHECK: call foo
287; CHECK: catch
288; CHECK: i32.call $drop=, __cxa_begin_catch
289; CHECK: call __cxa_end_catch
290; CHECK: end_try
Heejin Ahn78297632019-02-26 03:29:59 +0000291; CHECK-NOT: global.set __stack_pointer
292; CHECK: return
293define void @test_no_sp_writeback() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
Heejin Ahn972fc352018-08-22 21:13:49 +0000294entry:
295 invoke void @foo()
296 to label %try.cont unwind label %catch.dispatch
297
298catch.dispatch: ; preds = %entry
299 %0 = catchswitch within none [label %catch.start] unwind to caller
300
301catch.start: ; preds = %catch.dispatch
302 %1 = catchpad within %0 [i8* null]
303 %2 = call i8* @llvm.wasm.get.exception(token %1)
304 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
305 %4 = call i8* @__cxa_begin_catch(i8* %2) #2 [ "funclet"(token %1) ]
306 call void @__cxa_end_catch() [ "funclet"(token %1) ]
307 catchret from %1 to label %try.cont
308
309try.cont: ; preds = %entry, %catch.start
310 ret void
311}
312
Heejin Ahnd2a56ac2019-02-26 04:08:49 +0000313; When the result of @llvm.wasm.get.exception is not used. This is created to
314; fix a bug in LateEHPrepare and this should not crash.
315define void @test_get_exception_wo_use() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
316entry:
317 invoke void @foo()
318 to label %try.cont unwind label %catch.dispatch
319
320catch.dispatch: ; preds = %entry
321 %0 = catchswitch within none [label %catch.start] unwind to caller
322
323catch.start: ; preds = %catch.dispatch
324 %1 = catchpad within %0 [i8* null]
325 %2 = call i8* @llvm.wasm.get.exception(token %1)
326 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
327 catchret from %1 to label %try.cont
328
329try.cont: ; preds = %entry, %catch.start
330 ret void
331}
332
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000333declare void @foo()
Heejin Ahned5e06b2018-08-21 19:44:11 +0000334declare void @bar(i32*)
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000335declare i32 @__gxx_wasm_personality_v0(...)
Heejin Ahnd6f48782019-01-30 03:21:57 +0000336declare void @llvm.wasm.throw(i32, i8*)
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000337declare i8* @llvm.wasm.get.exception(token)
338declare i32 @llvm.wasm.get.ehselector(token)
Heejin Ahn66ce4192019-03-16 05:38:57 +0000339declare void @llvm.wasm.rethrow.in.catch()
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000340declare i32 @llvm.eh.typeid.for(i8*)
341declare i8* @__cxa_begin_catch(i8*)
342declare void @__cxa_end_catch()
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000343declare void @__clang_call_terminate(i8*)
Heejin Ahn78297632019-02-26 03:29:59 +0000344declare %struct.Temp* @_ZN4TempD2Ev(%struct.Temp* returned)
Heejin Ahnda419bd2018-11-14 02:46:21 +0000345
346; CHECK: __cpp_exception:
Heejin Ahnbe5e5872018-12-11 01:11:04 +0000347; CHECK: .eventtype __cpp_exception i32