1 ; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling -verify-machineinstrs | FileCheck --implicit-check-not=ehgcr -allow-deprecated-dag-overlap %s
2 ; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling -verify-machineinstrs -O0
3 ; RUN: llc < %s -disable-wasm-fallthrough-return-opt -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling
5 target triple = "wasm32-unknown-unknown"
7 %struct.Temp = type { i8 }
9 @_ZTIi = external dso_local constant ptr
11 ; CHECK: .tagtype __cpp_exception i32
13 ; CHECK-LABEL: test_throw:
14 ; CHECK: throw __cpp_exception, $0
15 ; CHECK-NOT: unreachable
16 define void @test_throw(ptr %p) {
17 call void @llvm.wasm.throw(i32 0, ptr %p)
21 ; Simple test with a try-catch
31 ; CHECK-LABEL: test_catch:
32 ; CHECK: global.get ${{.+}}=, __stack_pointer
35 ; CHECK: catch $[[EXN:[0-9]+]]=, __cpp_exception
36 ; CHECK: global.set __stack_pointer
37 ; CHECK: i32.{{store|const}} {{.*}} __wasm_lpad_context
38 ; CHECK: call $drop=, _Unwind_CallPersonality, $[[EXN]]
41 ; CHECK: call $drop=, __cxa_begin_catch
42 ; CHECK: call __cxa_end_catch
47 define void @test_catch() personality ptr @__gxx_wasm_personality_v0 {
50 to label %try.cont unwind label %catch.dispatch
52 catch.dispatch: ; preds = %entry
53 %0 = catchswitch within none [label %catch.start] unwind to caller
55 catch.start: ; preds = %catch.dispatch
56 %1 = catchpad within %0 [ptr @_ZTIi]
57 %2 = call ptr @llvm.wasm.get.exception(token %1)
58 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
59 %4 = call i32 @llvm.eh.typeid.for(ptr @_ZTIi)
60 %matches = icmp eq i32 %3, %4
61 br i1 %matches, label %catch, label %rethrow
63 catch: ; preds = %catch.start
64 %5 = call ptr @__cxa_begin_catch(ptr %2) [ "funclet"(token %1) ]
65 call void @__cxa_end_catch() [ "funclet"(token %1) ]
66 catchret from %1 to label %try.cont
68 rethrow: ; preds = %catch.start
69 call void @llvm.wasm.rethrow() [ "funclet"(token %1) ]
72 try.cont: ; preds = %catch, %entry
76 ; Destructor (cleanup) test
82 ; void test_cleanup() {
87 ; CHECK-LABEL: test_cleanup:
91 ; CHECK: global.set __stack_pointer
92 ; CHECK: call $drop=, _ZN4TempD2Ev
95 define void @test_cleanup() personality ptr @__gxx_wasm_personality_v0 {
97 %t = alloca %struct.Temp, align 1
99 to label %invoke.cont unwind label %ehcleanup
101 invoke.cont: ; preds = %entry
102 %call = call ptr @_ZN4TempD2Ev(ptr %t)
105 ehcleanup: ; preds = %entry
106 %0 = cleanuppad within none []
107 %call1 = call ptr @_ZN4TempD2Ev(ptr %t) [ "funclet"(token %0) ]
108 cleanupret from %0 unwind to caller
111 ; Calling a function that may throw within a 'catch (...)' generates a
112 ; temrinatepad, because __cxa_end_catch() also can throw within 'catch (...)'.
115 ; void test_terminatepad() {
123 ; CHECK-LABEL: test_terminatepad
127 ; CHECK: call $drop=, __cxa_begin_catch
132 ; CHECK: call __cxa_end_catch
134 ; CHECK: call _ZSt9terminatev
139 ; CHECK: call __cxa_end_catch
141 define void @test_terminatepad() personality ptr @__gxx_wasm_personality_v0 {
144 to label %try.cont unwind label %catch.dispatch
146 catch.dispatch: ; preds = %entry
147 %0 = catchswitch within none [label %catch.start] unwind to caller
149 catch.start: ; preds = %catch.dispatch
150 %1 = catchpad within %0 [ptr null]
151 %2 = call ptr @llvm.wasm.get.exception(token %1)
152 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
153 %4 = call ptr @__cxa_begin_catch(ptr %2) [ "funclet"(token %1) ]
154 invoke void @foo() [ "funclet"(token %1) ]
155 to label %invoke.cont1 unwind label %ehcleanup
157 invoke.cont1: ; preds = %catch.start
158 call void @__cxa_end_catch() [ "funclet"(token %1) ]
159 catchret from %1 to label %try.cont
161 try.cont: ; preds = %invoke.cont1, %entry
164 ehcleanup: ; preds = %catch.start
165 %5 = cleanuppad within %1 []
166 invoke void @__cxa_end_catch() [ "funclet"(token %5) ]
167 to label %invoke.cont2 unwind label %terminate
169 invoke.cont2: ; preds = %ehcleanup
170 cleanupret from %5 unwind to caller
172 terminate: ; preds = %ehcleanup
173 %6 = cleanuppad within %5 []
174 call void @_ZSt9terminatev() [ "funclet"(token %6) ]
178 ; Tests prologues and epilogues are not generated within EH scopes.
179 ; They should not be treated as funclets; BBs starting with a catch instruction
180 ; should not have a prologue, and BBs ending with a catchret/cleanupret should
181 ; not have an epilogue. This is separate from __stack_pointer restoring
182 ; instructions after a catch instruction.
184 ; void bar(int) noexcept;
185 ; void test_no_prolog_epilog_in_ehpad() {
195 ; CHECK-LABEL: test_no_prolog_epilog_in_ehpad
199 ; CHECK-NOT: global.get $push{{.+}}=, __stack_pointer
200 ; CHECK: global.set __stack_pointer
204 ; CHECK: call $drop=, __cxa_begin_catch
208 ; CHECK-NOT: global.get $push{{.+}}=, __stack_pointer
209 ; CHECK: global.set __stack_pointer
210 ; CHECK: call __cxa_end_catch
212 ; CHECK-NOT: global.set __stack_pointer, $pop{{.+}}
217 ; CHECK-NOT: global.set __stack_pointer, $pop{{.+}}
218 ; CHECK: call __cxa_end_catch
220 define void @test_no_prolog_epilog_in_ehpad() personality ptr @__gxx_wasm_personality_v0 {
222 %stack_var = alloca i32, align 4
223 call void @bar(ptr %stack_var)
225 to label %try.cont unwind label %catch.dispatch
227 catch.dispatch: ; preds = %entry
228 %0 = catchswitch within none [label %catch.start] unwind to caller
230 catch.start: ; preds = %catch.dispatch
231 %1 = catchpad within %0 [ptr @_ZTIi]
232 %2 = call ptr @llvm.wasm.get.exception(token %1)
233 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
234 %4 = call i32 @llvm.eh.typeid.for(ptr @_ZTIi)
235 %matches = icmp eq i32 %3, %4
236 br i1 %matches, label %catch, label %rethrow
238 catch: ; preds = %catch.start
239 %5 = call ptr @__cxa_begin_catch(ptr %2) [ "funclet"(token %1) ]
240 %6 = load float, ptr %5, align 4
241 invoke void @foo() [ "funclet"(token %1) ]
242 to label %invoke.cont1 unwind label %ehcleanup
244 invoke.cont1: ; preds = %catch
245 call void @__cxa_end_catch() [ "funclet"(token %1) ]
246 catchret from %1 to label %try.cont
248 rethrow: ; preds = %catch.start
249 call void @llvm.wasm.rethrow() [ "funclet"(token %1) ]
252 try.cont: ; preds = %invoke.cont1, %entry
255 ehcleanup: ; preds = %catch
256 %7 = cleanuppad within %1 []
257 call void @__cxa_end_catch() [ "funclet"(token %7) ]
258 cleanupret from %7 unwind to caller
261 ; When a function does not have stack-allocated objects, it does not need to
262 ; store SP back to __stack_pointer global at the epilog.
265 ; void test_no_sp_writeback() {
272 ; CHECK-LABEL: test_no_sp_writeback
276 ; CHECK: call $drop=, __cxa_begin_catch
277 ; CHECK: call __cxa_end_catch
279 ; CHECK-NOT: global.set __stack_pointer
281 define void @test_no_sp_writeback() personality ptr @__gxx_wasm_personality_v0 {
284 to label %try.cont unwind label %catch.dispatch
286 catch.dispatch: ; preds = %entry
287 %0 = catchswitch within none [label %catch.start] unwind to caller
289 catch.start: ; preds = %catch.dispatch
290 %1 = catchpad within %0 [ptr null]
291 %2 = call ptr @llvm.wasm.get.exception(token %1)
292 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
293 %4 = call ptr @__cxa_begin_catch(ptr %2) [ "funclet"(token %1) ]
294 call void @__cxa_end_catch() [ "funclet"(token %1) ]
295 catchret from %1 to label %try.cont
297 try.cont: ; preds = %catch.start, %entry
301 ; When the result of @llvm.wasm.get.exception is not used. This is created to
302 ; fix a bug in LateEHPrepare and this should not crash.
303 define void @test_get_exception_wo_use() personality ptr @__gxx_wasm_personality_v0 {
306 to label %try.cont unwind label %catch.dispatch
308 catch.dispatch: ; preds = %entry
309 %0 = catchswitch within none [label %catch.start] unwind to caller
311 catch.start: ; preds = %catch.dispatch
312 %1 = catchpad within %0 [ptr null]
313 %2 = call ptr @llvm.wasm.get.exception(token %1)
314 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
315 catchret from %1 to label %try.cont
317 try.cont: ; preds = %catch.start, %entry
321 ; Tests a case when a cleanup region (cleanuppad ~ clanupret) contains another
323 define void @test_complex_cleanup_region() personality ptr @__gxx_wasm_personality_v0 {
326 to label %invoke.cont unwind label %ehcleanup
328 invoke.cont: ; preds = %entry
331 ehcleanup: ; preds = %entry
332 %0 = cleanuppad within none []
333 invoke void @foo() [ "funclet"(token %0) ]
334 to label %ehcleanupret unwind label %catch.dispatch
336 catch.dispatch: ; preds = %ehcleanup
337 %1 = catchswitch within %0 [label %catch.start] unwind label %ehcleanup.1
339 catch.start: ; preds = %catch.dispatch
340 %2 = catchpad within %1 [ptr null]
341 %3 = call ptr @llvm.wasm.get.exception(token %2)
342 %4 = call i32 @llvm.wasm.get.ehselector(token %2)
343 catchret from %2 to label %ehcleanupret
345 ehcleanup.1: ; preds = %catch.dispatch
346 %5 = cleanuppad within %0 []
349 ehcleanupret: ; preds = %catch.start, %ehcleanup
350 cleanupret from %0 unwind to caller
354 declare void @bar(ptr)
355 declare i32 @__gxx_wasm_personality_v0(...)
356 ; Function Attrs: noreturn
357 declare void @llvm.wasm.throw(i32, ptr) #1
358 ; Function Attrs: nounwind
359 declare ptr @llvm.wasm.get.exception(token) #0
360 ; Function Attrs: nounwind
361 declare i32 @llvm.wasm.get.ehselector(token) #0
362 ; Function Attrs: noreturn
363 declare void @llvm.wasm.rethrow() #1
364 ; Function Attrs: nounwind
365 declare i32 @llvm.eh.typeid.for(ptr) #0
366 declare ptr @__cxa_begin_catch(ptr)
367 declare void @__cxa_end_catch()
368 declare void @_ZSt9terminatev()
369 declare ptr @_ZN4TempD2Ev(ptr returned)
371 attributes #0 = { nounwind }
372 attributes #1 = { noreturn }
374 ; CHECK: __cpp_exception: