blob: 0577cd43b9778c90781a8d38a14cb5563d42996e [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
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
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 Ahnac62b052017-06-30 00:43:15 +000015; CHECK-NEXT: i32.const $push0=, 0
Heejin Ahnda419bd2018-11-14 02:46:21 +000016; CHECK-NEXT: throw __cpp_exception@EVENT, $pop0
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000017define void @test_throw() {
Heejin Ahnac62b052017-06-30 00:43:15 +000018 call void @llvm.wasm.throw(i32 0, i8* null)
19 ret void
20}
21
Heejin Ahn4934f762018-06-25 01:07:11 +000022; CHECK-LABEL: test_catch_rethrow:
Heejin Ahn78d19102018-08-21 21:23:07 +000023; CHECK: get_global $push{{.+}}=, __stack_pointer@GLOBAL
Heejin Ahne76fa9e2018-08-16 23:50:59 +000024; CHECK: try
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000025; CHECK: call foo@FUNCTION
26; CHECK: i32.catch $push{{.+}}=, 0
Heejin Ahn78d19102018-08-21 21:23:07 +000027; CHECK: set_global __stack_pointer@GLOBAL
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000028; 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
Heejin Ahn4934f762018-06-25 01:07:11 +000034; CHECK-NEXT: rethrow
Heejin Ahne76fa9e2018-08-16 23:50:59 +000035; CHECK: end_try
Heejin Ahn4934f762018-06-25 01:07:11 +000036define void @test_catch_rethrow() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000037entry:
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
Heejin Ahnac62b052017-06-30 00:43:15 +000062 ret void
63}
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000064
65; CHECK-LABEL: test_cleanup:
Heejin Ahne76fa9e2018-08-16 23:50:59 +000066; CHECK: try
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000067; CHECK: call foo@FUNCTION
Heejin Ahn4934f762018-06-25 01:07:11 +000068; CHECK: catch_all
Heejin Ahn78d19102018-08-21 21:23:07 +000069; CHECK: set_global __stack_pointer@GLOBAL
Heejin Ahn283e1c12018-08-14 22:14:51 +000070; CHECK: i32.call $push{{.+}}=, _ZN7CleanupD1Ev@FUNCTION
Heejin Ahn4934f762018-06-25 01:07:11 +000071; CHECK: rethrow
Heejin Ahne76fa9e2018-08-16 23:50:59 +000072; CHECK: end_try
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +000073define 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
Heejin Ahn4934f762018-06-25 01:07:11 +000089; - 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
Heejin Ahned5e06b2018-08-21 19:44:11 +0000157; 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
Heejin Ahn78d19102018-08-21 21:23:07 +0000168; CHECK: set_global __stack_pointer@GLOBAL
Heejin Ahned5e06b2018-08-21 19:44:11 +0000169; CHECK: try
170; CHECK: call foo@FUNCTION
171; CHECK: catch_all
Heejin Ahn9cd7f882018-08-21 20:04:42 +0000172; CHECK-NOT: get_global $push{{.+}}=, __stack_pointer@GLOBAL
Heejin Ahn78d19102018-08-21 21:23:07 +0000173; CHECK: set_global __stack_pointer@GLOBAL
Heejin Ahned5e06b2018-08-21 19:44:11 +0000174; 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
Heejin Ahn972fc352018-08-22 21:13:49 +0000221; 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
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000250declare void @foo()
Heejin Ahned5e06b2018-08-21 19:44:11 +0000251declare void @bar(i32*)
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000252declare 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*)
Heejin Ahn4934f762018-06-25 01:07:11 +0000260declare void @_ZSt9terminatev()
Heejin Ahn5ef4d5f2018-05-31 22:25:54 +0000261declare %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* returned)
Heejin Ahnda419bd2018-11-14 02:46:21 +0000262
263; CHECK: __cpp_exception:
264; CHECK: .eventtype __cpp_exception, void, i32