1 ; RUN: not llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-keep-registers -exception-model=wasm
2 ; 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
3 ; RUN: llc < %s -disable-wasm-fallthrough-return-opt -wasm-keep-registers -exception-model=wasm -mattr=+exception-handling
5 target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
6 target triple = "wasm32-unknown-unknown"
8 %struct.Temp = type { i8 }
10 @_ZTIi = external constant i8*
12 ; CHECK-LABEL: test_throw:
13 ; CHECK: throw __cpp_exception, $0
14 ; CHECK-NOT: unreachable
15 define void @test_throw(i8* %p) {
16 call void @llvm.wasm.throw(i32 0, i8* %p)
20 ; Simple test with a try-catch
30 ; CHECK-LABEL: test_catch:
31 ; CHECK: global.get ${{.+}}=, __stack_pointer
34 ; CHECK: catch $[[EXNREF:[0-9]+]]=
35 ; CHECK: global.set __stack_pointer
37 ; CHECK: br_on_exn 0, __cpp_exception, $[[EXNREF]]
38 ; CHECK: rethrow $[[EXNREF]]
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]]
46 ; CHECK: i32.call $drop=, __cxa_begin_catch
47 ; CHECK: call __cxa_end_catch
50 ; CHECK: rethrow $[[EXNREF]]
52 define void @test_catch() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
55 to label %try.cont unwind label %catch.dispatch
57 catch.dispatch: ; preds = %entry
58 %0 = catchswitch within none [label %catch.start] unwind to caller
60 catch.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
68 catch: ; 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
73 rethrow: ; preds = %catch.start
74 call void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %1) ]
77 try.cont: ; preds = %entry, %catch
81 ; Destructor (cleanup) test
87 ; void test_cleanup() {
92 ; CHECK-LABEL: test_cleanup:
95 ; CHECK: catch $[[EXNREF:[0-9]+]]=
96 ; CHECK: global.set __stack_pointer
97 ; CHECK: i32.call $drop=, _ZN4TempD2Ev
98 ; CHECK: rethrow $[[EXNREF]]
100 define void @test_cleanup() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
102 %t = alloca %struct.Temp, align 1
104 to label %invoke.cont unwind label %ehcleanup
106 invoke.cont: ; preds = %entry
107 %call = call %struct.Temp* @_ZN4TempD2Ev(%struct.Temp* %t)
110 ehcleanup: ; preds = %entry
111 %0 = cleanuppad within none []
112 %call1 = call %struct.Temp* @_ZN4TempD2Ev(%struct.Temp* %t) [ "funclet"(token %0) ]
113 cleanupret from %0 unwind to caller
116 ; Calling a function that may throw within a 'catch (...)' generates a
117 ; temrinatepad, because __cxa_end_catch() also can throw within 'catch (...)'.
120 ; void test_terminatepad() {
128 ; CHECK-LABEL: test_terminatepad
132 ; CHECK: i32.call $drop=, __cxa_begin_catch
137 ; CHECK: call __cxa_end_catch
140 ; CHECK: br_on_exn 0, __cpp_exception
141 ; CHECK: i32.const ${{.*}}=, 0
142 ; CHECK: call __clang_call_terminate
145 ; CHECK: call __clang_call_terminate
150 ; CHECK: call __cxa_end_catch
152 define void @test_terminatepad() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
155 to label %try.cont unwind label %catch.dispatch
157 catch.dispatch: ; preds = %entry
158 %0 = catchswitch within none [label %catch.start] unwind to caller
160 catch.start: ; preds = %catch.dispatch
161 %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
168 invoke.cont1: ; preds = %catch.start
169 call void @__cxa_end_catch() [ "funclet"(token %1) ]
170 catchret from %1 to label %try.cont
172 try.cont: ; preds = %entry, %invoke.cont1
175 ehcleanup: ; 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
180 invoke.cont2: ; preds = %ehcleanup
181 cleanupret from %5 unwind to caller
183 terminate: ; preds = %ehcleanup
184 %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) ]
190 ; 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.
196 ; void bar(int) noexcept;
197 ; void test_no_prolog_epilog_in_ehpad() {
207 ; CHECK-LABEL: test_no_prolog_epilog_in_ehpad
211 ; CHECK-NOT: global.get $push{{.+}}=, __stack_pointer
212 ; CHECK: global.set __stack_pointer
216 ; CHECK: i32.call $drop=, __cxa_begin_catch
220 ; CHECK-NOT: global.get $push{{.+}}=, __stack_pointer
221 ; CHECK: global.set __stack_pointer
222 ; CHECK: call __cxa_end_catch
224 ; CHECK-NOT: global.set __stack_pointer, $pop{{.+}}
229 ; CHECK-NOT: global.set __stack_pointer, $pop{{.+}}
230 ; CHECK: call __cxa_end_catch
232 define void @test_no_prolog_epilog_in_ehpad() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
234 %stack_var = alloca i32, align 4
235 call void @bar(i32* %stack_var)
237 to label %try.cont unwind label %catch.dispatch
239 catch.dispatch: ; preds = %entry
240 %0 = catchswitch within none [label %catch.start] unwind to caller
242 catch.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
250 catch: ; 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
257 invoke.cont1: ; preds = %catch
258 call void @__cxa_end_catch() [ "funclet"(token %1) ]
259 catchret from %1 to label %try.cont
261 rethrow: ; preds = %catch.start
262 call void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %1) ]
265 try.cont: ; preds = %entry, %invoke.cont1
268 ehcleanup: ; preds = %catch
269 %8 = cleanuppad within %1 []
270 call void @__cxa_end_catch() [ "funclet"(token %8) ]
271 cleanupret from %8 unwind to caller
274 ; 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.
278 ; void test_no_sp_writeback() {
285 ; CHECK-LABEL: test_no_sp_writeback
289 ; CHECK: i32.call $drop=, __cxa_begin_catch
290 ; CHECK: call __cxa_end_catch
292 ; CHECK-NOT: global.set __stack_pointer
294 define void @test_no_sp_writeback() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
297 to label %try.cont unwind label %catch.dispatch
299 catch.dispatch: ; preds = %entry
300 %0 = catchswitch within none [label %catch.start] unwind to caller
302 catch.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
310 try.cont: ; preds = %entry, %catch.start
314 ; 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.
316 define void @test_get_exception_wo_use() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
319 to label %try.cont unwind label %catch.dispatch
321 catch.dispatch: ; preds = %entry
322 %0 = catchswitch within none [label %catch.start] unwind to caller
324 catch.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
330 try.cont: ; preds = %entry, %catch.start
335 declare void @bar(i32*)
336 declare i32 @__gxx_wasm_personality_v0(...)
337 declare void @llvm.wasm.throw(i32, i8*)
338 declare i8* @llvm.wasm.get.exception(token)
339 declare i32 @llvm.wasm.get.ehselector(token)
340 declare void @llvm.wasm.rethrow.in.catch()
341 declare i32 @llvm.eh.typeid.for(i8*)
342 declare i8* @__cxa_begin_catch(i8*)
343 declare void @__cxa_end_catch()
344 declare void @__clang_call_terminate(i8*)
345 declare %struct.Temp* @_ZN4TempD2Ev(%struct.Temp* returned)
347 ; CHECK: __cpp_exception:
348 ; CHECK: .eventtype __cpp_exception i32