blob: b53cd0cb5444250bbb73cac403ee492c5fb877cb [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 Ahn095796a2018-11-16 00:47:18 +00002; 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
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
12declare void @llvm.wasm.throw(i32, i8*)
13
14; CHECK-LABEL: test_throw:
Heejin Ahn095796a2018-11-16 00:47:18 +000015; CHECK: get_local $push0=, 0
Heejin Ahnda419bd2018-11-14 02:46:21 +000016; CHECK-NEXT: throw __cpp_exception@EVENT, $pop0
Heejin Ahn095796a2018-11-16 00:47:18 +000017; CHECK-NOT: unreachable
18define void @test_throw(i8* %p) {
19 call void @llvm.wasm.throw(i32 0, i8* %p)
Heejin Ahnac62b052017-06-30 00:43:15 +000020 ret void
21}
22
Heejin Ahn4934f762018-06-25 01:07:11 +000023; CHECK-LABEL: test_catch_rethrow:
Heejin Ahn78d19102018-08-21 21:23:07 +000024; CHECK: get_global $push{{.+}}=, __stack_pointer@GLOBAL
Heejin Ahne76fa9e2018-08-16 23:50:59 +000025; CHECK: try
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000026; CHECK: call foo@FUNCTION
27; CHECK: i32.catch $push{{.+}}=, 0
Heejin Ahn78d19102018-08-21 21:23:07 +000028; CHECK: set_global __stack_pointer@GLOBAL
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000029; 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
Heejin Ahn4934f762018-06-25 01:07:11 +000035; CHECK-NEXT: rethrow
Heejin Ahne76fa9e2018-08-16 23:50:59 +000036; CHECK: end_try
Heejin Ahn4934f762018-06-25 01:07:11 +000037define void @test_catch_rethrow() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000038entry:
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
Heejin Ahnac62b052017-06-30 00:43:15 +000063 ret void
64}
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000065
66; CHECK-LABEL: test_cleanup:
Heejin Ahne76fa9e2018-08-16 23:50:59 +000067; CHECK: try
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000068; CHECK: call foo@FUNCTION
Heejin Ahn4934f762018-06-25 01:07:11 +000069; CHECK: catch_all
Heejin Ahn78d19102018-08-21 21:23:07 +000070; CHECK: set_global __stack_pointer@GLOBAL
Heejin Ahn283e1c12018-08-14 22:14:51 +000071; CHECK: i32.call $push{{.+}}=, _ZN7CleanupD1Ev@FUNCTION
Heejin Ahn4934f762018-06-25 01:07:11 +000072; CHECK: rethrow
Heejin Ahne76fa9e2018-08-16 23:50:59 +000073; CHECK: end_try
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000074define 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
Heejin Ahn4934f762018-06-25 01:07:11 +000090; - 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
Heejin Ahned5e06b2018-08-21 19:44:11 +0000158; 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
Heejin Ahn78d19102018-08-21 21:23:07 +0000169; CHECK: set_global __stack_pointer@GLOBAL
Heejin Ahned5e06b2018-08-21 19:44:11 +0000170; CHECK: try
171; CHECK: call foo@FUNCTION
172; CHECK: catch_all
Heejin Ahn9cd7f882018-08-21 20:04:42 +0000173; CHECK-NOT: get_global $push{{.+}}=, __stack_pointer@GLOBAL
Heejin Ahn78d19102018-08-21 21:23:07 +0000174; CHECK: set_global __stack_pointer@GLOBAL
Heejin Ahned5e06b2018-08-21 19:44:11 +0000175; 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
Heejin Ahn972fc352018-08-22 21:13:49 +0000222; 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
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000251declare void @foo()
Heejin Ahned5e06b2018-08-21 19:44:11 +0000252declare void @bar(i32*)
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000253declare 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*)
Heejin Ahn4934f762018-06-25 01:07:11 +0000261declare void @_ZSt9terminatev()
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000262declare %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* returned)
Heejin Ahnda419bd2018-11-14 02:46:21 +0000263
264; CHECK: __cpp_exception:
265; CHECK: .eventtype __cpp_exception, void, i32