blob: af470b27e89de2fc61fbbcf237a594388ec2371c [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
Heejin Ahn831efe02019-08-11 06:24:07 +0000141; CHECK: i32.const ${{.*}}=, 0
142; CHECK: call __clang_call_terminate
Heejin Ahncf699b42019-02-27 00:50:53 +0000143; CHECK: unreachable
144; CHECK: end_block
145; CHECK: call __clang_call_terminate
146; CHECK: unreachable
147; CHECK: end_try
148; CHECK: rethrow
Heejin Ahn78297632019-02-26 03:29:59 +0000149; CHECK: end_try
Heejin Ahncf699b42019-02-27 00:50:53 +0000150; CHECK: call __cxa_end_catch
151; CHECK: end_try
Heejin Ahnd6f48782019-01-30 03:21:57 +0000152define void @test_terminatepad() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
Heejin Ahn4934f762018-06-25 01:07:11 +0000153entry:
Heejin Ahn4934f762018-06-25 01:07:11 +0000154 invoke void @foo()
Heejin Ahn4934f762018-06-25 01:07:11 +0000155 to label %try.cont unwind label %catch.dispatch
156
Heejin Ahnd6f48782019-01-30 03:21:57 +0000157catch.dispatch: ; preds = %entry
158 %0 = catchswitch within none [label %catch.start] unwind to caller
Heejin Ahn4934f762018-06-25 01:07:11 +0000159
160catch.start: ; preds = %catch.dispatch
Heejin Ahnd6f48782019-01-30 03:21:57 +0000161 %1 = catchpad within %0 [i8* null]
162 %2 = call i8* @llvm.wasm.get.exception(token %1)
163 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
164 %4 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
165 invoke void @foo() [ "funclet"(token %1) ]
166 to label %invoke.cont1 unwind label %ehcleanup
Heejin Ahn4934f762018-06-25 01:07:11 +0000167
Heejin Ahnd6f48782019-01-30 03:21:57 +0000168invoke.cont1: ; preds = %catch.start
169 call void @__cxa_end_catch() [ "funclet"(token %1) ]
170 catchret from %1 to label %try.cont
Heejin Ahn4934f762018-06-25 01:07:11 +0000171
Heejin Ahnd6f48782019-01-30 03:21:57 +0000172try.cont: ; preds = %entry, %invoke.cont1
173 ret void
Heejin Ahn4934f762018-06-25 01:07:11 +0000174
Heejin Ahnd6f48782019-01-30 03:21:57 +0000175ehcleanup: ; preds = %catch.start
176 %5 = cleanuppad within %1 []
177 invoke void @__cxa_end_catch() [ "funclet"(token %5) ]
178 to label %invoke.cont2 unwind label %terminate
Heejin Ahn4934f762018-06-25 01:07:11 +0000179
Heejin Ahnd6f48782019-01-30 03:21:57 +0000180invoke.cont2: ; preds = %ehcleanup
181 cleanupret from %5 unwind to caller
Heejin Ahn4934f762018-06-25 01:07:11 +0000182
183terminate: ; preds = %ehcleanup
Heejin Ahnd6f48782019-01-30 03:21:57 +0000184 %6 = cleanuppad within %5 []
185 %7 = call i8* @llvm.wasm.get.exception(token %6)
186 call void @__clang_call_terminate(i8* %7) [ "funclet"(token %6) ]
Heejin Ahn4934f762018-06-25 01:07:11 +0000187 unreachable
188}
189
Heejin Ahned5e06b2018-08-21 19:44:11 +0000190; Tests prologues and epilogues are not generated within EH scopes.
191; They should not be treated as funclets; BBs starting with a catch instruction
192; should not have a prologue, and BBs ending with a catchret/cleanupret should
193; not have an epilogue. This is separate from __stack_pointer restoring
194; instructions after a catch instruction.
Heejin Ahn78297632019-02-26 03:29:59 +0000195;
196; void bar(int) noexcept;
197; void test_no_prolog_epilog_in_ehpad() {
198; int stack_var = 0;
199; bar(stack_var);
200; try {
201; foo();
202; } catch (int) {
203; foo();
204; }
205; }
Heejin Ahned5e06b2018-08-21 19:44:11 +0000206
207; CHECK-LABEL: test_no_prolog_epilog_in_ehpad
Heejin Ahncf699b42019-02-27 00:50:53 +0000208; CHECK: try
209; CHECK: call foo
210; CHECK: catch
211; CHECK-NOT: global.get $push{{.+}}=, __stack_pointer
212; CHECK: global.set __stack_pointer
213; CHECK: block
Heejin Ahn78297632019-02-26 03:29:59 +0000214; CHECK: block
Heejin Ahncf699b42019-02-27 00:50:53 +0000215; CHECK: br_if 0
216; CHECK: i32.call $drop=, __cxa_begin_catch
217; CHECK: try
218; CHECK: call foo
219; CHECK: catch
220; CHECK-NOT: global.get $push{{.+}}=, __stack_pointer
221; CHECK: global.set __stack_pointer
222; CHECK: call __cxa_end_catch
223; CHECK: rethrow
224; CHECK-NOT: global.set __stack_pointer, $pop{{.+}}
225; CHECK: end_try
Heejin Ahn78297632019-02-26 03:29:59 +0000226; CHECK: end_block
Heejin Ahn66ce4192019-03-16 05:38:57 +0000227; CHECK: rethrow
Heejin Ahncf699b42019-02-27 00:50:53 +0000228; CHECK: end_block
229; CHECK-NOT: global.set __stack_pointer, $pop{{.+}}
230; CHECK: call __cxa_end_catch
231; CHECK: end_try
Heejin Ahned5e06b2018-08-21 19:44:11 +0000232define void @test_no_prolog_epilog_in_ehpad() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
233entry:
234 %stack_var = alloca i32, align 4
235 call void @bar(i32* %stack_var)
236 invoke void @foo()
237 to label %try.cont unwind label %catch.dispatch
238
239catch.dispatch: ; preds = %entry
240 %0 = catchswitch within none [label %catch.start] unwind to caller
241
242catch.start: ; preds = %catch.dispatch
243 %1 = catchpad within %0 [i8* bitcast (i8** @_ZTIi to i8*)]
244 %2 = call i8* @llvm.wasm.get.exception(token %1)
245 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
246 %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
247 %matches = icmp eq i32 %3, %4
248 br i1 %matches, label %catch, label %rethrow
249
250catch: ; preds = %catch.start
251 %5 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
252 %6 = bitcast i8* %5 to float*
253 %7 = load float, float* %6, align 4
254 invoke void @foo() [ "funclet"(token %1) ]
255 to label %invoke.cont1 unwind label %ehcleanup
256
257invoke.cont1: ; preds = %catch
258 call void @__cxa_end_catch() [ "funclet"(token %1) ]
259 catchret from %1 to label %try.cont
260
261rethrow: ; preds = %catch.start
Heejin Ahn66ce4192019-03-16 05:38:57 +0000262 call void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %1) ]
Heejin Ahned5e06b2018-08-21 19:44:11 +0000263 unreachable
264
265try.cont: ; preds = %entry, %invoke.cont1
266 ret void
267
268ehcleanup: ; preds = %catch
269 %8 = cleanuppad within %1 []
270 call void @__cxa_end_catch() [ "funclet"(token %8) ]
271 cleanupret from %8 unwind to caller
272}
273
Heejin Ahn972fc352018-08-22 21:13:49 +0000274; When a function does not have stack-allocated objects, it does not need to
275; store SP back to __stack_pointer global at the epilog.
Heejin Ahn78297632019-02-26 03:29:59 +0000276;
277; void foo();
278; void test_no_sp_writeback() {
279; try {
280; foo();
281; } catch (...) {
282; }
283; }
Heejin Ahn972fc352018-08-22 21:13:49 +0000284
Heejin Ahn78297632019-02-26 03:29:59 +0000285; CHECK-LABEL: test_no_sp_writeback
Heejin Ahncf699b42019-02-27 00:50:53 +0000286; CHECK: try
287; CHECK: call foo
288; CHECK: catch
289; CHECK: i32.call $drop=, __cxa_begin_catch
290; CHECK: call __cxa_end_catch
291; CHECK: end_try
Heejin Ahn78297632019-02-26 03:29:59 +0000292; CHECK-NOT: global.set __stack_pointer
293; CHECK: return
294define void @test_no_sp_writeback() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
Heejin Ahn972fc352018-08-22 21:13:49 +0000295entry:
296 invoke void @foo()
297 to label %try.cont unwind label %catch.dispatch
298
299catch.dispatch: ; preds = %entry
300 %0 = catchswitch within none [label %catch.start] unwind to caller
301
302catch.start: ; preds = %catch.dispatch
303 %1 = catchpad within %0 [i8* null]
304 %2 = call i8* @llvm.wasm.get.exception(token %1)
305 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
306 %4 = call i8* @__cxa_begin_catch(i8* %2) #2 [ "funclet"(token %1) ]
307 call void @__cxa_end_catch() [ "funclet"(token %1) ]
308 catchret from %1 to label %try.cont
309
310try.cont: ; preds = %entry, %catch.start
311 ret void
312}
313
Heejin Ahnd2a56ac2019-02-26 04:08:49 +0000314; When the result of @llvm.wasm.get.exception is not used. This is created to
315; fix a bug in LateEHPrepare and this should not crash.
316define void @test_get_exception_wo_use() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
317entry:
318 invoke void @foo()
319 to label %try.cont unwind label %catch.dispatch
320
321catch.dispatch: ; preds = %entry
322 %0 = catchswitch within none [label %catch.start] unwind to caller
323
324catch.start: ; preds = %catch.dispatch
325 %1 = catchpad within %0 [i8* null]
326 %2 = call i8* @llvm.wasm.get.exception(token %1)
327 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
328 catchret from %1 to label %try.cont
329
330try.cont: ; preds = %entry, %catch.start
331 ret void
332}
333
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000334declare void @foo()
Heejin Ahned5e06b2018-08-21 19:44:11 +0000335declare void @bar(i32*)
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000336declare i32 @__gxx_wasm_personality_v0(...)
Heejin Ahnd6f48782019-01-30 03:21:57 +0000337declare void @llvm.wasm.throw(i32, i8*)
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000338declare i8* @llvm.wasm.get.exception(token)
339declare i32 @llvm.wasm.get.ehselector(token)
Heejin Ahn66ce4192019-03-16 05:38:57 +0000340declare void @llvm.wasm.rethrow.in.catch()
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000341declare i32 @llvm.eh.typeid.for(i8*)
342declare i8* @__cxa_begin_catch(i8*)
343declare void @__cxa_end_catch()
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000344declare void @__clang_call_terminate(i8*)
Heejin Ahn78297632019-02-26 03:29:59 +0000345declare %struct.Temp* @_ZN4TempD2Ev(%struct.Temp* returned)
Heejin Ahnda419bd2018-11-14 02:46:21 +0000346
347; CHECK: __cpp_exception:
Heejin Ahnbe5e5872018-12-11 01:11:04 +0000348; CHECK: .eventtype __cpp_exception i32