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