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-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
4
5target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
6target triple = "wasm32-unknown-unknown"
7
8%struct.Cleanup = type { i8 }
9
10@_ZTIi = external constant i8*
11
12declare void @llvm.wasm.throw(i32, i8*)
13
14; CHECK-LABEL: test_throw:
15; CHECK:      get_local $push0=, 0
16; CHECK-NEXT: throw __cpp_exception@EVENT, $pop0
17; CHECK-NOT:  unreachable
18define void @test_throw(i8* %p) {
19  call void @llvm.wasm.throw(i32 0, i8* %p)
20  ret void
21}
22
23; CHECK-LABEL: test_catch_rethrow:
24; CHECK:   get_global  $push{{.+}}=, __stack_pointer@GLOBAL
25; CHECK:   try
26; CHECK:   call      foo@FUNCTION
27; CHECK:   i32.catch     $push{{.+}}=, 0
28; CHECK:   set_global  __stack_pointer@GLOBAL
29; CHECK-DAG:   i32.store  __wasm_lpad_context
30; CHECK-DAG:   i32.store  __wasm_lpad_context+4
31; CHECK:   i32.call  $push{{.+}}=, _Unwind_CallPersonality@FUNCTION
32; CHECK:   i32.call  $push{{.+}}=, __cxa_begin_catch@FUNCTION
33; CHECK:   call      __cxa_end_catch@FUNCTION
34; CHECK:   call      __cxa_rethrow@FUNCTION
35; CHECK-NEXT:   rethrow
36; CHECK:   end_try
37define void @test_catch_rethrow() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
38entry:
39  invoke void @foo()
40          to label %try.cont unwind label %catch.dispatch
41
42catch.dispatch:                                   ; preds = %entry
43  %0 = catchswitch within none [label %catch.start] unwind to caller
44
45catch.start:                                      ; preds = %catch.dispatch
46  %1 = catchpad within %0 [i8* bitcast (i8** @_ZTIi to i8*)]
47  %2 = call i8* @llvm.wasm.get.exception(token %1)
48  %3 = call i32 @llvm.wasm.get.ehselector(token %1)
49  %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
50  %matches = icmp eq i32 %3, %4
51  br i1 %matches, label %catch, label %rethrow
52
53catch:                                            ; preds = %catch.start
54  %5 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
55  call void @__cxa_end_catch() [ "funclet"(token %1) ]
56  catchret from %1 to label %try.cont
57
58rethrow:                                          ; preds = %catch.start
59  call void @__cxa_rethrow() [ "funclet"(token %1) ]
60  unreachable
61
62try.cont:                                         ; preds = %entry, %catch
63  ret void
64}
65
66; CHECK-LABEL: test_cleanup:
67; CHECK:   try
68; CHECK:   call      foo@FUNCTION
69; CHECK:   catch_all
70; CHECK:   set_global  __stack_pointer@GLOBAL
71; CHECK:   i32.call  $push{{.+}}=, _ZN7CleanupD1Ev@FUNCTION
72; CHECK:   rethrow
73; CHECK:   end_try
74define void @test_cleanup() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
75entry:
76  %c = alloca %struct.Cleanup, align 1
77  invoke void @foo()
78          to label %invoke.cont unwind label %ehcleanup
79
80invoke.cont:                                      ; preds = %entry
81  %call = call %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* %c)
82  ret void
83
84ehcleanup:                                        ; preds = %entry
85  %0 = cleanuppad within none []
86  %call1 = call %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* %c) [ "funclet"(token %0) ]
87  cleanupret from %0 unwind to caller
88}
89
90; - Tests multple terminate pads are merged into one
91; - Tests a catch_all terminate pad is created after a catch terminate pad
92
93; CHECK-LABEL: test_terminatepad
94; CHECK:  i32.catch
95; CHECK:  call      __clang_call_terminate@FUNCTION
96; CHECK:  unreachable
97; CHECK:  catch_all
98; CHECK:  call      _ZSt9terminatev@FUNCTION
99; CHECK-NOT:  call      __clang_call_terminate@FUNCTION
100define hidden i32 @test_terminatepad() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
101entry:
102  %c = alloca %struct.Cleanup, align 1
103  %c1 = alloca %struct.Cleanup, align 1
104  invoke void @foo()
105          to label %invoke.cont unwind label %ehcleanup
106
107invoke.cont:                                      ; preds = %entry
108  %call = invoke %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* %c1)
109          to label %try.cont unwind label %catch.dispatch
110
111ehcleanup:                                        ; preds = %entry
112  %0 = cleanuppad within none []
113  %call4 = invoke %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* %c1) [ "funclet"(token %0) ]
114          to label %invoke.cont3 unwind label %terminate
115
116invoke.cont3:                                     ; preds = %ehcleanup
117  cleanupret from %0 unwind label %catch.dispatch
118
119catch.dispatch:                                   ; preds = %invoke.cont3, %invoke.cont
120  %1 = catchswitch within none [label %catch.start] unwind label %ehcleanup7
121
122catch.start:                                      ; preds = %catch.dispatch
123  %2 = catchpad within %1 [i8* null]
124  %3 = call i8* @llvm.wasm.get.exception(token %2)
125  %4 = call i32 @llvm.wasm.get.ehselector(token %2)
126  %5 = call i8* @__cxa_begin_catch(i8* %3) [ "funclet"(token %2) ]
127  invoke void @__cxa_end_catch() [ "funclet"(token %2) ]
128          to label %invoke.cont5 unwind label %ehcleanup7
129
130invoke.cont5:                                     ; preds = %catch.start
131  catchret from %2 to label %try.cont
132
133try.cont:                                         ; preds = %invoke.cont5, %invoke.cont
134  %call6 = call %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* %c)
135  ret i32 0
136
137ehcleanup7:                                       ; preds = %catch.start, %catch.dispatch
138  %6 = cleanuppad within none []
139  %call9 = invoke %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* %c) [ "funclet"(token %6) ]
140          to label %invoke.cont8 unwind label %terminate10
141
142invoke.cont8:                                     ; preds = %ehcleanup7
143  cleanupret from %6 unwind to caller
144
145terminate:                                        ; preds = %ehcleanup
146  %7 = cleanuppad within %0 []
147  %8 = call i8* @llvm.wasm.get.exception(token %7)
148  call void @__clang_call_terminate(i8* %8) [ "funclet"(token %7) ]
149  unreachable
150
151terminate10:                                      ; preds = %ehcleanup7
152  %9 = cleanuppad within %6 []
153  %10 = call i8* @llvm.wasm.get.exception(token %9)
154  call void @__clang_call_terminate(i8* %10) [ "funclet"(token %9) ]
155  unreachable
156}
157
158; Tests prologues and epilogues are not generated within EH scopes.
159; They should not be treated as funclets; BBs starting with a catch instruction
160; should not have a prologue, and BBs ending with a catchret/cleanupret should
161; not have an epilogue. This is separate from __stack_pointer restoring
162; instructions after a catch instruction.
163
164; CHECK-LABEL: test_no_prolog_epilog_in_ehpad
165; CHECK:  try
166; CHECK:  call      foo@FUNCTION
167; CHECK:  i32.catch
168; CHECK-NOT:  get_global  $push{{.+}}=, __stack_pointer@GLOBAL
169; CHECK:  set_global  __stack_pointer@GLOBAL
170; CHECK:  try
171; CHECK:  call      foo@FUNCTION
172; CHECK:  catch_all
173; CHECK-NOT:  get_global  $push{{.+}}=, __stack_pointer@GLOBAL
174; CHECK:  set_global  __stack_pointer@GLOBAL
175; CHECK:  call      __cxa_end_catch@FUNCTION
176; CHECK-NOT:  set_global  __stack_pointer@GLOBAL, $pop{{.+}}
177; CHECK:  end_try
178; CHECK-NOT:  set_global  __stack_pointer@GLOBAL, $pop{{.+}}
179; CHECK:  end_try
180define void @test_no_prolog_epilog_in_ehpad() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
181entry:
182  %stack_var = alloca i32, align 4
183  call void @bar(i32* %stack_var)
184  invoke void @foo()
185          to label %try.cont unwind label %catch.dispatch
186
187catch.dispatch:                                   ; preds = %entry
188  %0 = catchswitch within none [label %catch.start] unwind to caller
189
190catch.start:                                      ; preds = %catch.dispatch
191  %1 = catchpad within %0 [i8* bitcast (i8** @_ZTIi to i8*)]
192  %2 = call i8* @llvm.wasm.get.exception(token %1)
193  %3 = call i32 @llvm.wasm.get.ehselector(token %1)
194  %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
195  %matches = icmp eq i32 %3, %4
196  br i1 %matches, label %catch, label %rethrow
197
198catch:                                            ; preds = %catch.start
199  %5 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
200  %6 = bitcast i8* %5 to float*
201  %7 = load float, float* %6, align 4
202  invoke void @foo() [ "funclet"(token %1) ]
203          to label %invoke.cont1 unwind label %ehcleanup
204
205invoke.cont1:                                     ; preds = %catch
206  call void @__cxa_end_catch() [ "funclet"(token %1) ]
207  catchret from %1 to label %try.cont
208
209rethrow:                                          ; preds = %catch.start
210  call void @__cxa_rethrow() [ "funclet"(token %1) ]
211  unreachable
212
213try.cont:                                         ; preds = %entry, %invoke.cont1
214  ret void
215
216ehcleanup:                                        ; preds = %catch
217  %8 = cleanuppad within %1 []
218  call void @__cxa_end_catch() [ "funclet"(token %8) ]
219  cleanupret from %8 unwind to caller
220}
221
222; When a function does not have stack-allocated objects, it does not need to
223; store SP back to __stack_pointer global at the epilog.
224
225; CHECK-LABEL: no_sp_writeback
226; CHECK:  try
227; CHECK:  call foo@FUNCTION
228; CHECK:  end_try
229; CHECK-NOT:  set_global  __stack_pointer@GLOBAL
230; CHECK:  return
231define void @no_sp_writeback() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
232entry:
233  invoke void @foo()
234          to label %try.cont unwind label %catch.dispatch
235
236catch.dispatch:                                   ; preds = %entry
237  %0 = catchswitch within none [label %catch.start] unwind to caller
238
239catch.start:                                      ; preds = %catch.dispatch
240  %1 = catchpad within %0 [i8* null]
241  %2 = call i8* @llvm.wasm.get.exception(token %1)
242  %3 = call i32 @llvm.wasm.get.ehselector(token %1)
243  %4 = call i8* @__cxa_begin_catch(i8* %2) #2 [ "funclet"(token %1) ]
244  call void @__cxa_end_catch() [ "funclet"(token %1) ]
245  catchret from %1 to label %try.cont
246
247try.cont:                                         ; preds = %entry, %catch.start
248  ret void
249}
250
251declare void @foo()
252declare void @bar(i32*)
253declare i32 @__gxx_wasm_personality_v0(...)
254declare i8* @llvm.wasm.get.exception(token)
255declare i32 @llvm.wasm.get.ehselector(token)
256declare i32 @llvm.eh.typeid.for(i8*)
257declare i8* @__cxa_begin_catch(i8*)
258declare void @__cxa_end_catch()
259declare void @__cxa_rethrow()
260declare void @__clang_call_terminate(i8*)
261declare void @_ZSt9terminatev()
262declare %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* returned)
263
264; CHECK: __cpp_exception:
265; CHECK: .eventtype  __cpp_exception i32
266