blob: 46ec46837af5cb521c70f0a432e8ffcc3dc198fe [file] [log] [blame]
Wouter van Oortmerssen8a9cb242018-08-27 15:45:51 +00001; RUN: not llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-keep-registers -exception-model=wasm
Heejin Ahnd6f48782019-01-30 03:21:57 +00002; 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
Heejin Ahn5b023e02018-11-02 18:38:52 +00003; RUN: llc < %s -disable-wasm-fallthrough-return-opt -wasm-keep-registers -exception-model=wasm -mattr=+exception-handling
Heejin Ahnac62b052017-06-30 00:43:15 +00004
5target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
Sam Clegga5908002018-05-10 17:49:11 +00006target triple = "wasm32-unknown-unknown"
Heejin Ahnac62b052017-06-30 00:43:15 +00007
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +00008%struct.Cleanup = type { i8 }
Heejin Ahnac62b052017-06-30 00:43:15 +00009
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000010@_ZTIi = external constant i8*
11
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000012; CHECK-LABEL: test_throw:
Heejin Ahnd6f48782019-01-30 03:21:57 +000013; CHECK: throw __cpp_exception@EVENT, $0
Heejin Ahn095796a2018-11-16 00:47:18 +000014; CHECK-NOT: unreachable
15define void @test_throw(i8* %p) {
16 call void @llvm.wasm.throw(i32 0, i8* %p)
Heejin Ahnac62b052017-06-30 00:43:15 +000017 ret void
18}
19
Heejin Ahnd6f48782019-01-30 03:21:57 +000020; CHECK-LABEL: test_rethrow:
21; CHECK: rethrow
22; CHECK-NOT: unreachable
23define void @test_rethrow(i8* %p) {
24 call void @llvm.wasm.rethrow()
25 ret void
26}
27
Heejin Ahn4934f762018-06-25 01:07:11 +000028; CHECK-LABEL: test_catch_rethrow:
Heejin Ahnd6f48782019-01-30 03:21:57 +000029; CHECK: global.get ${{.+}}=, __stack_pointer@GLOBAL
Heejin Ahne76fa9e2018-08-16 23:50:59 +000030; CHECK: try
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000031; CHECK: call foo@FUNCTION
Heejin Ahnd6f48782019-01-30 03:21:57 +000032; CHECK: catch $[[EXCEPT_REF:[0-9]+]]=
33; CHECK: block i32
34; CHECK: br_on_exn 0, __cpp_exception@EVENT, $[[EXCEPT_REF]]
35; CHECK: rethrow
36; CHECK: end_block
37; CHECK: extract_exception $[[EXN:[0-9]+]]=
Thomas Lively6a87dda2019-01-08 06:25:55 +000038; CHECK: global.set __stack_pointer@GLOBAL
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000039; CHECK-DAG: i32.store __wasm_lpad_context
40; CHECK-DAG: i32.store __wasm_lpad_context+4
Heejin Ahnd6f48782019-01-30 03:21:57 +000041; CHECK: i32.call $drop=, _Unwind_CallPersonality@FUNCTION, $[[EXN]]
42; CHECK: i32.call $drop=, __cxa_begin_catch@FUNCTION
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000043; CHECK: call __cxa_end_catch@FUNCTION
44; CHECK: call __cxa_rethrow@FUNCTION
Heejin Ahne76fa9e2018-08-16 23:50:59 +000045; CHECK: end_try
Heejin Ahn4934f762018-06-25 01:07:11 +000046define void @test_catch_rethrow() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000047entry:
48 invoke void @foo()
49 to label %try.cont unwind label %catch.dispatch
50
51catch.dispatch: ; preds = %entry
52 %0 = catchswitch within none [label %catch.start] unwind to caller
53
54catch.start: ; preds = %catch.dispatch
55 %1 = catchpad within %0 [i8* bitcast (i8** @_ZTIi to i8*)]
56 %2 = call i8* @llvm.wasm.get.exception(token %1)
57 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
58 %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
59 %matches = icmp eq i32 %3, %4
60 br i1 %matches, label %catch, label %rethrow
61
62catch: ; preds = %catch.start
63 %5 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
64 call void @__cxa_end_catch() [ "funclet"(token %1) ]
65 catchret from %1 to label %try.cont
66
67rethrow: ; preds = %catch.start
68 call void @__cxa_rethrow() [ "funclet"(token %1) ]
69 unreachable
70
71try.cont: ; preds = %entry, %catch
Heejin Ahnac62b052017-06-30 00:43:15 +000072 ret void
73}
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000074
75; CHECK-LABEL: test_cleanup:
Heejin Ahne76fa9e2018-08-16 23:50:59 +000076; CHECK: try
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000077; CHECK: call foo@FUNCTION
Heejin Ahnd6f48782019-01-30 03:21:57 +000078; CHECK: catch
Thomas Lively6a87dda2019-01-08 06:25:55 +000079; CHECK: global.set __stack_pointer@GLOBAL
Heejin Ahnd6f48782019-01-30 03:21:57 +000080; CHECK: i32.call $drop=, _ZN7CleanupD1Ev@FUNCTION
Heejin Ahn4934f762018-06-25 01:07:11 +000081; CHECK: rethrow
Heejin Ahne76fa9e2018-08-16 23:50:59 +000082; CHECK: end_try
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000083define void @test_cleanup() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
84entry:
85 %c = alloca %struct.Cleanup, align 1
86 invoke void @foo()
87 to label %invoke.cont unwind label %ehcleanup
88
89invoke.cont: ; preds = %entry
90 %call = call %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* %c)
91 ret void
92
93ehcleanup: ; preds = %entry
94 %0 = cleanuppad within none []
95 %call1 = call %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* %c) [ "funclet"(token %0) ]
96 cleanupret from %0 unwind to caller
97}
98
Heejin Ahn4934f762018-06-25 01:07:11 +000099; CHECK-LABEL: test_terminatepad
Heejin Ahnd6f48782019-01-30 03:21:57 +0000100; CHECK: catch
101; CHECK: block i32
102; CHECK: br_on_exn 0, __cpp_exception@EVENT
103; CHECK: call __clang_call_terminate@FUNCTION, 0
104; CHECK: unreachable
105; CHECK: end_block
106; CHECK: extract_exception
107; CHECK: call __clang_call_terminate@FUNCTION
108; CHECK: unreachable
109define void @test_terminatepad() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
Heejin Ahn4934f762018-06-25 01:07:11 +0000110entry:
Heejin Ahn4934f762018-06-25 01:07:11 +0000111 invoke void @foo()
Heejin Ahn4934f762018-06-25 01:07:11 +0000112 to label %try.cont unwind label %catch.dispatch
113
Heejin Ahnd6f48782019-01-30 03:21:57 +0000114catch.dispatch: ; preds = %entry
115 %0 = catchswitch within none [label %catch.start] unwind to caller
Heejin Ahn4934f762018-06-25 01:07:11 +0000116
117catch.start: ; preds = %catch.dispatch
Heejin Ahnd6f48782019-01-30 03:21:57 +0000118 %1 = catchpad within %0 [i8* null]
119 %2 = call i8* @llvm.wasm.get.exception(token %1)
120 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
121 %4 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
122 invoke void @foo() [ "funclet"(token %1) ]
123 to label %invoke.cont1 unwind label %ehcleanup
Heejin Ahn4934f762018-06-25 01:07:11 +0000124
Heejin Ahnd6f48782019-01-30 03:21:57 +0000125invoke.cont1: ; preds = %catch.start
126 call void @__cxa_end_catch() [ "funclet"(token %1) ]
127 catchret from %1 to label %try.cont
Heejin Ahn4934f762018-06-25 01:07:11 +0000128
Heejin Ahnd6f48782019-01-30 03:21:57 +0000129try.cont: ; preds = %entry, %invoke.cont1
130 ret void
Heejin Ahn4934f762018-06-25 01:07:11 +0000131
Heejin Ahnd6f48782019-01-30 03:21:57 +0000132ehcleanup: ; preds = %catch.start
133 %5 = cleanuppad within %1 []
134 invoke void @__cxa_end_catch() [ "funclet"(token %5) ]
135 to label %invoke.cont2 unwind label %terminate
Heejin Ahn4934f762018-06-25 01:07:11 +0000136
Heejin Ahnd6f48782019-01-30 03:21:57 +0000137invoke.cont2: ; preds = %ehcleanup
138 cleanupret from %5 unwind to caller
Heejin Ahn4934f762018-06-25 01:07:11 +0000139
140terminate: ; preds = %ehcleanup
Heejin Ahnd6f48782019-01-30 03:21:57 +0000141 %6 = cleanuppad within %5 []
142 %7 = call i8* @llvm.wasm.get.exception(token %6)
143 call void @__clang_call_terminate(i8* %7) [ "funclet"(token %6) ]
Heejin Ahn4934f762018-06-25 01:07:11 +0000144 unreachable
145}
146
Heejin Ahned5e06b2018-08-21 19:44:11 +0000147; Tests prologues and epilogues are not generated within EH scopes.
148; They should not be treated as funclets; BBs starting with a catch instruction
149; should not have a prologue, and BBs ending with a catchret/cleanupret should
150; not have an epilogue. This is separate from __stack_pointer restoring
151; instructions after a catch instruction.
152
153; CHECK-LABEL: test_no_prolog_epilog_in_ehpad
154; CHECK: try
155; CHECK: call foo@FUNCTION
Heejin Ahnd6f48782019-01-30 03:21:57 +0000156; CHECK: catch
Thomas Lively6a87dda2019-01-08 06:25:55 +0000157; CHECK-NOT: global.get $push{{.+}}=, __stack_pointer@GLOBAL
158; CHECK: global.set __stack_pointer@GLOBAL
Heejin Ahned5e06b2018-08-21 19:44:11 +0000159; CHECK: try
160; CHECK: call foo@FUNCTION
Heejin Ahnd6f48782019-01-30 03:21:57 +0000161; CHECK: catch
Thomas Lively6a87dda2019-01-08 06:25:55 +0000162; CHECK-NOT: global.get $push{{.+}}=, __stack_pointer@GLOBAL
163; CHECK: global.set __stack_pointer@GLOBAL
Heejin Ahned5e06b2018-08-21 19:44:11 +0000164; CHECK: call __cxa_end_catch@FUNCTION
Thomas Lively6a87dda2019-01-08 06:25:55 +0000165; CHECK-NOT: global.set __stack_pointer@GLOBAL, $pop{{.+}}
Heejin Ahned5e06b2018-08-21 19:44:11 +0000166; CHECK: end_try
Thomas Lively6a87dda2019-01-08 06:25:55 +0000167; CHECK-NOT: global.set __stack_pointer@GLOBAL, $pop{{.+}}
Heejin Ahned5e06b2018-08-21 19:44:11 +0000168; CHECK: end_try
169define void @test_no_prolog_epilog_in_ehpad() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
170entry:
171 %stack_var = alloca i32, align 4
172 call void @bar(i32* %stack_var)
173 invoke void @foo()
174 to label %try.cont unwind label %catch.dispatch
175
176catch.dispatch: ; preds = %entry
177 %0 = catchswitch within none [label %catch.start] unwind to caller
178
179catch.start: ; preds = %catch.dispatch
180 %1 = catchpad within %0 [i8* bitcast (i8** @_ZTIi to i8*)]
181 %2 = call i8* @llvm.wasm.get.exception(token %1)
182 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
183 %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
184 %matches = icmp eq i32 %3, %4
185 br i1 %matches, label %catch, label %rethrow
186
187catch: ; preds = %catch.start
188 %5 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
189 %6 = bitcast i8* %5 to float*
190 %7 = load float, float* %6, align 4
191 invoke void @foo() [ "funclet"(token %1) ]
192 to label %invoke.cont1 unwind label %ehcleanup
193
194invoke.cont1: ; preds = %catch
195 call void @__cxa_end_catch() [ "funclet"(token %1) ]
196 catchret from %1 to label %try.cont
197
198rethrow: ; preds = %catch.start
199 call void @__cxa_rethrow() [ "funclet"(token %1) ]
200 unreachable
201
202try.cont: ; preds = %entry, %invoke.cont1
203 ret void
204
205ehcleanup: ; preds = %catch
206 %8 = cleanuppad within %1 []
207 call void @__cxa_end_catch() [ "funclet"(token %8) ]
208 cleanupret from %8 unwind to caller
209}
210
Heejin Ahn972fc352018-08-22 21:13:49 +0000211; When a function does not have stack-allocated objects, it does not need to
212; store SP back to __stack_pointer global at the epilog.
213
214; CHECK-LABEL: no_sp_writeback
215; CHECK: try
216; CHECK: call foo@FUNCTION
217; CHECK: end_try
Thomas Lively6a87dda2019-01-08 06:25:55 +0000218; CHECK-NOT: global.set __stack_pointer@GLOBAL
Heejin Ahn972fc352018-08-22 21:13:49 +0000219; CHECK: return
220define void @no_sp_writeback() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
221entry:
222 invoke void @foo()
223 to label %try.cont unwind label %catch.dispatch
224
225catch.dispatch: ; preds = %entry
226 %0 = catchswitch within none [label %catch.start] unwind to caller
227
228catch.start: ; preds = %catch.dispatch
229 %1 = catchpad within %0 [i8* null]
230 %2 = call i8* @llvm.wasm.get.exception(token %1)
231 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
232 %4 = call i8* @__cxa_begin_catch(i8* %2) #2 [ "funclet"(token %1) ]
233 call void @__cxa_end_catch() [ "funclet"(token %1) ]
234 catchret from %1 to label %try.cont
235
236try.cont: ; preds = %entry, %catch.start
237 ret void
238}
239
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000240declare void @foo()
Heejin Ahned5e06b2018-08-21 19:44:11 +0000241declare void @bar(i32*)
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000242declare i32 @__gxx_wasm_personality_v0(...)
Heejin Ahnd6f48782019-01-30 03:21:57 +0000243declare void @llvm.wasm.throw(i32, i8*)
244declare void @llvm.wasm.rethrow()
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000245declare i8* @llvm.wasm.get.exception(token)
246declare i32 @llvm.wasm.get.ehselector(token)
247declare i32 @llvm.eh.typeid.for(i8*)
248declare i8* @__cxa_begin_catch(i8*)
249declare void @__cxa_end_catch()
250declare void @__cxa_rethrow()
251declare void @__clang_call_terminate(i8*)
252declare %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* returned)
Heejin Ahnda419bd2018-11-14 02:46:21 +0000253
254; CHECK: __cpp_exception:
Heejin Ahnbe5e5872018-12-11 01:11:04 +0000255; CHECK: .eventtype __cpp_exception i32