1; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -exception-model=wasm | FileCheck %s
2
3target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
4target triple = "wasm32-unknown-unknown"
5
6%struct.Cleanup = type { i8 }
7
8@_ZTIi = external constant i8*
9
10declare void @llvm.wasm.throw(i32, i8*)
11
12; CHECK-LABEL: test_throw:
13; CHECK-NEXT: i32.const $push0=, 0
14; CHECK-NEXT: throw 0, $pop0
15define void @test_throw() {
16  call void @llvm.wasm.throw(i32 0, i8* null)
17  ret void
18}
19
20; CHECK-LABEL: test_catch_rethrow:
21; CHECK:   call      foo@FUNCTION
22; CHECK:   i32.catch     $push{{.+}}=, 0
23; CHECK-DAG:   i32.store  __wasm_lpad_context
24; CHECK-DAG:   i32.store  __wasm_lpad_context+4
25; CHECK:   i32.call  $push{{.+}}=, _Unwind_CallPersonality@FUNCTION
26; CHECK:   i32.call  $push{{.+}}=, __cxa_begin_catch@FUNCTION
27; CHECK:   call      __cxa_end_catch@FUNCTION
28; CHECK:   call      __cxa_rethrow@FUNCTION
29; CHECK-NEXT:   rethrow
30define void @test_catch_rethrow() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
31entry:
32  invoke void @foo()
33          to label %try.cont unwind label %catch.dispatch
34
35catch.dispatch:                                   ; preds = %entry
36  %0 = catchswitch within none [label %catch.start] unwind to caller
37
38catch.start:                                      ; preds = %catch.dispatch
39  %1 = catchpad within %0 [i8* bitcast (i8** @_ZTIi to i8*)]
40  %2 = call i8* @llvm.wasm.get.exception(token %1)
41  %3 = call i32 @llvm.wasm.get.ehselector(token %1)
42  %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
43  %matches = icmp eq i32 %3, %4
44  br i1 %matches, label %catch, label %rethrow
45
46catch:                                            ; preds = %catch.start
47  %5 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
48  call void @__cxa_end_catch() [ "funclet"(token %1) ]
49  catchret from %1 to label %try.cont
50
51rethrow:                                          ; preds = %catch.start
52  call void @__cxa_rethrow() [ "funclet"(token %1) ]
53  unreachable
54
55try.cont:                                         ; preds = %entry, %catch
56  ret void
57}
58
59; CHECK-LABEL: test_cleanup:
60; CHECK:   call      foo@FUNCTION
61; CHECK:   catch_all
62; CHECK:   i32.call  $push20=, _ZN7CleanupD1Ev@FUNCTION
63; CHECK:   rethrow
64define void @test_cleanup() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
65entry:
66  %c = alloca %struct.Cleanup, align 1
67  invoke void @foo()
68          to label %invoke.cont unwind label %ehcleanup
69
70invoke.cont:                                      ; preds = %entry
71  %call = call %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* %c)
72  ret void
73
74ehcleanup:                                        ; preds = %entry
75  %0 = cleanuppad within none []
76  %call1 = call %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* %c) [ "funclet"(token %0) ]
77  cleanupret from %0 unwind to caller
78}
79
80; - Tests multple terminate pads are merged into one
81; - Tests a catch_all terminate pad is created after a catch terminate pad
82
83; CHECK-LABEL: test_terminatepad
84; CHECK:  i32.catch
85; CHECK:  call      __clang_call_terminate@FUNCTION
86; CHECK:  unreachable
87; CHECK:  catch_all
88; CHECK:  call      _ZSt9terminatev@FUNCTION
89; CHECK-NOT:  call      __clang_call_terminate@FUNCTION
90define hidden i32 @test_terminatepad() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
91entry:
92  %c = alloca %struct.Cleanup, align 1
93  %c1 = alloca %struct.Cleanup, align 1
94  invoke void @foo()
95          to label %invoke.cont unwind label %ehcleanup
96
97invoke.cont:                                      ; preds = %entry
98  %call = invoke %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* %c1)
99          to label %try.cont unwind label %catch.dispatch
100
101ehcleanup:                                        ; preds = %entry
102  %0 = cleanuppad within none []
103  %call4 = invoke %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* %c1) [ "funclet"(token %0) ]
104          to label %invoke.cont3 unwind label %terminate
105
106invoke.cont3:                                     ; preds = %ehcleanup
107  cleanupret from %0 unwind label %catch.dispatch
108
109catch.dispatch:                                   ; preds = %invoke.cont3, %invoke.cont
110  %1 = catchswitch within none [label %catch.start] unwind label %ehcleanup7
111
112catch.start:                                      ; preds = %catch.dispatch
113  %2 = catchpad within %1 [i8* null]
114  %3 = call i8* @llvm.wasm.get.exception(token %2)
115  %4 = call i32 @llvm.wasm.get.ehselector(token %2)
116  %5 = call i8* @__cxa_begin_catch(i8* %3) [ "funclet"(token %2) ]
117  invoke void @__cxa_end_catch() [ "funclet"(token %2) ]
118          to label %invoke.cont5 unwind label %ehcleanup7
119
120invoke.cont5:                                     ; preds = %catch.start
121  catchret from %2 to label %try.cont
122
123try.cont:                                         ; preds = %invoke.cont5, %invoke.cont
124  %call6 = call %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* %c)
125  ret i32 0
126
127ehcleanup7:                                       ; preds = %catch.start, %catch.dispatch
128  %6 = cleanuppad within none []
129  %call9 = invoke %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* %c) [ "funclet"(token %6) ]
130          to label %invoke.cont8 unwind label %terminate10
131
132invoke.cont8:                                     ; preds = %ehcleanup7
133  cleanupret from %6 unwind to caller
134
135terminate:                                        ; preds = %ehcleanup
136  %7 = cleanuppad within %0 []
137  %8 = call i8* @llvm.wasm.get.exception(token %7)
138  call void @__clang_call_terminate(i8* %8) [ "funclet"(token %7) ]
139  unreachable
140
141terminate10:                                      ; preds = %ehcleanup7
142  %9 = cleanuppad within %6 []
143  %10 = call i8* @llvm.wasm.get.exception(token %9)
144  call void @__clang_call_terminate(i8* %10) [ "funclet"(token %9) ]
145  unreachable
146}
147
148declare void @foo()
149declare i32 @__gxx_wasm_personality_v0(...)
150declare i8* @llvm.wasm.get.exception(token)
151declare i32 @llvm.wasm.get.ehselector(token)
152declare i32 @llvm.eh.typeid.for(i8*)
153declare i8* @__cxa_begin_catch(i8*)
154declare void @__cxa_end_catch()
155declare void @__cxa_rethrow()
156declare void @__clang_call_terminate(i8*)
157declare void @_ZSt9terminatev()
158declare %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* returned)
159