1 ; RUN: opt < %s -winehprepare -demote-catchswitch-only -wasmehprepare -S | FileCheck %s
3 target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
4 target triple = "wasm32-unknown-unknown"
6 ; CHECK: @__wasm_lpad_context = external global { i32, i8*, i32 }
8 @_ZTIi = external constant i8*
9 %struct.Cleanup = type { i8 }
11 ; A single 'catch (int)' clause.
12 ; A wasm.catch() call, wasm.lsda() call, and personality call to generate a
13 ; selector should all be genereated after the catchpad.
14 define void @test0() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
15 ; CHECK-LABEL: @test0()
18 to label %try.cont unwind label %catch.dispatch
20 catch.dispatch: ; preds = %entry
21 %0 = catchswitch within none [label %catch.start] unwind to caller
23 catch.start: ; preds = %catch.dispatch
24 %1 = catchpad within %0 [i8* bitcast (i8** @_ZTIi to i8*)]
25 %2 = call i8* @llvm.wasm.get.exception(token %1)
26 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
27 %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
28 %matches = icmp eq i32 %3, %4
29 br i1 %matches, label %catch, label %rethrow
31 ; CHECK-NEXT: %[[CATCHPAD:.*]] = catchpad
32 ; CHECK-NEXT: %[[EXN:.*]] = call i8* @llvm.wasm.extract.exception()
33 ; CHECK-NEXT: call void @llvm.wasm.landingpad.index(token %[[CATCHPAD]], i32 0)
34 ; CHECK-NEXT: store i32 0, i32* getelementptr inbounds ({ i32, i8*, i32 }, { i32, i8*, i32 }* @__wasm_lpad_context, i32 0, i32 0)
35 ; CHECK-NEXT: %[[LSDA:.*]] = call i8* @llvm.wasm.lsda()
36 ; CHECK-NEXT: store i8* %[[LSDA]], i8** getelementptr inbounds ({ i32, i8*, i32 }, { i32, i8*, i32 }* @__wasm_lpad_context, i32 0, i32 1)
37 ; CHECK-NEXT: call i32 @_Unwind_CallPersonality(i8* %[[EXN]]) {{.*}} [ "funclet"(token %[[CATCHPAD]]) ]
38 ; CHECK-NEXT: %[[SELECTOR:.*]] = load i32, i32* getelementptr inbounds ({ i32, i8*, i32 }, { i32, i8*, i32 }* @__wasm_lpad_context, i32 0, i32 2)
39 ; CHECK: icmp eq i32 %[[SELECTOR]]
41 catch: ; preds = %catch.start
42 %5 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
43 call void @__cxa_end_catch() [ "funclet"(token %1) ]
44 catchret from %1 to label %try.cont
46 ; CHECK-NEXT: call i8* @__cxa_begin_catch(i8* %[[EXN]])
48 rethrow: ; preds = %catch.start
49 call void @__cxa_rethrow() [ "funclet"(token %1) ]
52 try.cont: ; preds = %entry, %catch
56 ; Two try-catches, one of them is with a single 'catch (...)' clause.
57 ; For the catchpad with a single 'catch (...)', only a wasm.catch() call should
58 ; be generated after the catchpad; wasm.landingpad.index() and personality call
59 ; should NOT be generated. For the other catchpad, the argument of
60 ; wasm.landingpad.index() should be not 1 but 0.
61 define void @test1() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
62 ; CHECK-LABEL: @test1()
65 to label %try.cont unwind label %catch.dispatch
67 catch.dispatch: ; preds = %entry
68 %0 = catchswitch within none [label %catch.start] unwind to caller
70 catch.start: ; preds = %catch.dispatch
71 %1 = catchpad within %0 [i8* null]
72 %2 = call i8* @llvm.wasm.get.exception(token %1)
73 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
74 %4 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
75 call void @__cxa_end_catch() [ "funclet"(token %1) ]
76 catchret from %1 to label %try.cont
78 ; CHECK-NEXT: catchpad within %0 [i8* null]
79 ; CHECK-NOT: call void @llvm.wasm.landingpad.index
80 ; CHECK-NOT: store {{.*}} @__wasm_lpad_context
81 ; CHECK-NOT: call i8* @llvm.wasm.lsda()
82 ; CHECK-NOT: call i32 @_Unwind_CallPersonality
83 ; CHECK-NOT: load {{.*}} @__wasm_lpad_context
85 try.cont: ; preds = %entry, %catch.start
87 to label %try.cont7 unwind label %catch.dispatch2
89 catch.dispatch2: ; preds = %try.cont
90 %5 = catchswitch within none [label %catch.start3] unwind to caller
92 catch.start3: ; preds = %catch.dispatch2
93 %6 = catchpad within %5 [i8* bitcast (i8** @_ZTIi to i8*)]
94 %7 = call i8* @llvm.wasm.get.exception(token %6)
95 %8 = call i32 @llvm.wasm.get.ehselector(token %6)
96 %9 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
97 %matches = icmp eq i32 %8, %9
98 br i1 %matches, label %catch4, label %rethrow
99 ; CHECK: catch.start3:
100 ; CHECK: call void @llvm.wasm.landingpad.index(token %{{.+}}, i32 0)
102 catch4: ; preds = %catch.start3
103 %10 = call i8* @__cxa_begin_catch(i8* %7) [ "funclet"(token %6) ]
104 call void @__cxa_end_catch() [ "funclet"(token %6) ]
105 catchret from %6 to label %try.cont7
107 rethrow: ; preds = %catch.start3
108 call void @__cxa_rethrow() [ "funclet"(token %6) ]
111 try.cont7: ; preds = %try.cont, %catch4
115 ; A nested try-catch within a catch. Within the nested catchpad, wasm.lsda()
116 ; call should NOT be generated.
117 define void @test2() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
118 ; CHECK-LABEL: @test2()
121 to label %try.cont9 unwind label %catch.dispatch
123 catch.dispatch: ; preds = %entry
124 %0 = catchswitch within none [label %catch.start] unwind to caller
126 catch.start: ; preds = %catch.dispatch
127 %1 = catchpad within %0 [i8* bitcast (i8** @_ZTIi to i8*)]
128 %2 = call i8* @llvm.wasm.get.exception(token %1)
129 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
130 %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
131 %matches = icmp eq i32 %3, %4
132 br i1 %matches, label %catch, label %rethrow
133 ; CHECK: catch.start:
134 ; CHECK: call i8* @llvm.wasm.lsda()
136 catch: ; preds = %catch.start
137 %5 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
138 invoke void @foo() [ "funclet"(token %1) ]
139 to label %try.cont unwind label %catch.dispatch2
141 catch.dispatch2: ; preds = %catch
142 %6 = catchswitch within %1 [label %catch.start3] unwind label %ehcleanup
144 catch.start3: ; preds = %catch.dispatch2
145 %7 = catchpad within %6 [i8* bitcast (i8** @_ZTIi to i8*)]
146 %8 = call i8* @llvm.wasm.get.exception(token %7)
147 %9 = call i32 @llvm.wasm.get.ehselector(token %7)
148 %10 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
149 %matches4 = icmp eq i32 %9, %10
150 br i1 %matches4, label %catch6, label %rethrow5
151 ; CHECK: catch.start3:
152 ; CHECK-NOT: call i8* @llvm.wasm.lsda()
154 catch6: ; preds = %catch.start3
155 %11 = call i8* @__cxa_begin_catch(i8* %8) [ "funclet"(token %7) ]
156 call void @__cxa_end_catch() [ "funclet"(token %7) ]
157 catchret from %7 to label %try.cont
159 rethrow5: ; preds = %catch.start3
160 invoke void @__cxa_rethrow() [ "funclet"(token %7) ]
161 to label %unreachable unwind label %ehcleanup
163 try.cont: ; preds = %catch, %catch6
164 call void @__cxa_end_catch() [ "funclet"(token %1) ]
165 catchret from %1 to label %try.cont9
167 rethrow: ; preds = %catch.start
168 call void @__cxa_rethrow() [ "funclet"(token %1) ]
171 try.cont9: ; preds = %entry, %try.cont
174 ehcleanup: ; preds = %rethrow5, %catch.dispatch2
175 %12 = cleanuppad within %1 []
176 call void @__cxa_end_catch() [ "funclet"(token %12) ]
177 cleanupret from %12 unwind to caller
179 ; CHECK-NEXT: cleanuppad
180 ; CHECK-NOT: call void @llvm.wasm.landingpad.index
181 ; CHECK-NOT: store {{.*}} @__wasm_lpad_context
182 ; CHECK-NOT: call i8* @llvm.wasm.lsda()
183 ; CHECK-NOT: call i32 @_Unwind_CallPersonality
184 ; CHECK-NOT: load {{.*}} @__wasm_lpad_context
186 unreachable: ; preds = %rethrow5
190 ; A cleanuppad with a call to __clang_call_terminate().
191 ; A call to wasm.catch() should be generated after the cleanuppad.
192 define void @test3() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
193 ; CHECK-LABEL: @test3
196 to label %try.cont unwind label %catch.dispatch
198 catch.dispatch: ; preds = %entry
199 %0 = catchswitch within none [label %catch.start] unwind to caller
201 catch.start: ; preds = %catch.dispatch
202 %1 = catchpad within %0 [i8* null]
203 %2 = call i8* @llvm.wasm.get.exception(token %1)
204 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
205 %4 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
206 invoke void @foo() [ "funclet"(token %1) ]
207 to label %invoke.cont1 unwind label %ehcleanup
209 invoke.cont1: ; preds = %catch.start
210 call void @__cxa_end_catch() [ "funclet"(token %1) ]
211 catchret from %1 to label %try.cont
213 try.cont: ; preds = %entry, %invoke.cont1
216 ehcleanup: ; preds = %catch.start
217 %5 = cleanuppad within %1 []
218 invoke void @__cxa_end_catch() [ "funclet"(token %5) ]
219 to label %invoke.cont2 unwind label %terminate
221 invoke.cont2: ; preds = %ehcleanup
222 cleanupret from %5 unwind to caller
224 terminate: ; preds = %ehcleanup
225 %6 = cleanuppad within %5 []
226 %7 = call i8* @llvm.wasm.get.exception(token %6)
227 call void @__clang_call_terminate(i8* %7) [ "funclet"(token %6) ]
230 ; CHECK-NEXT: cleanuppad
231 ; CHECK-NEXT: %[[EXN:.*]] = call i8* @llvm.wasm.extract.exception
232 ; CHECK-NEXT: call void @__clang_call_terminate(i8* %[[EXN]])
235 ; PHI demotion test. Only the phi before catchswitch should be demoted; the phi
236 ; before cleanuppad should NOT.
237 define void @test4() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
238 ; CHECK-LABEL: @test4
240 %c = alloca %struct.Cleanup, align 1
242 to label %invoke.cont unwind label %ehcleanup
244 invoke.cont: ; preds = %entry
246 to label %invoke.cont1 unwind label %ehcleanup
248 invoke.cont1: ; preds = %invoke.cont
249 %call = call %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* %c)
252 ehcleanup: ; preds = %invoke.cont, %entry
253 %num.0 = phi i32 [ 2, %invoke.cont ], [ 1, %entry ]
254 %0 = cleanuppad within none []
255 %call2 = call %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* %c) [ "funclet"(token %0) ]
256 cleanupret from %0 unwind label %catch.dispatch
260 catch.dispatch: ; preds = %ehcleanup
261 %1 = catchswitch within none [label %catch.start] unwind to caller
263 catch.start: ; preds = %catch.dispatch
264 %2 = catchpad within %1 [i8* null]
265 %3 = call i8* @llvm.wasm.get.exception(token %2)
266 %4 = call i32 @llvm.wasm.get.ehselector(token %2)
267 %5 = call i8* @__cxa_begin_catch(i8* %3) [ "funclet"(token %2) ]
268 call void @func(i32 %num.0) [ "funclet"(token %2) ]
269 call void @__cxa_end_catch() [ "funclet"(token %2) ]
270 catchret from %2 to label %try.cont
272 try.cont: ; preds = %catch.start, %invoke.cont1
274 to label %invoke.cont3 unwind label %catch.dispatch5
276 invoke.cont3: ; preds = %try.cont
278 to label %try.cont10 unwind label %catch.dispatch5
280 catch.dispatch5: ; preds = %invoke.cont3, %try.cont
281 %num.1 = phi i32 [ 2, %invoke.cont3 ], [ 1, %try.cont ]
282 %6 = catchswitch within none [label %catch.start6] unwind to caller
283 ; CHECK: catch.dispatch5:
286 catch.start6: ; preds = %catch.dispatch5
287 %7 = catchpad within %6 [i8* null]
288 %8 = call i8* @llvm.wasm.get.exception(token %7)
289 %9 = call i32 @llvm.wasm.get.ehselector(token %7)
290 %10 = call i8* @__cxa_begin_catch(i8* %8) [ "funclet"(token %7) ]
291 call void @func(i32 %num.1) [ "funclet"(token %7) ]
292 call void @__cxa_end_catch() [ "funclet"(token %7) ]
293 catchret from %7 to label %try.cont10
295 try.cont10: ; preds = %invoke.cont3, %catch.start6
299 ; Tests if instructions after a call to @llvm.wasm.throw are deleted and the
300 ; BB's dead children are deleted.
302 ; CHECK-LABEL: @test5
303 define i32 @test5(i1 %b, i8* %p) {
305 br i1 %b, label %bb.true, label %bb.false
308 ; CHECK-NEXT: call void @llvm.wasm.throw(i32 0, i8* %p)
309 ; CHECK-NEXT: unreachable
310 bb.true: ; preds = %entry
311 call void @llvm.wasm.throw(i32 0, i8* %p)
314 ; CHECK-NOT: bb.true.0
315 bb.true.0: ; preds = %bb.true
319 bb.false: ; preds = %entry
323 merge: ; preds = %bb.true.0, %bb.false
327 ; Tests if instructions after a call to @llvm.wasm.rethrow are deleted and the
328 ; BB's dead children are deleted.
330 ; CHECK-LABEL: @test6
331 define i32 @test6(i1 %b, i8* %p) {
333 br i1 %b, label %bb.true, label %bb.false
336 ; CHECK-NEXT: call void @llvm.wasm.rethrow()
337 ; CHECK-NEXT: unreachable
338 bb.true: ; preds = %entry
339 call void @llvm.wasm.rethrow()
342 ; CHECK-NOT: bb.true.0
343 bb.true.0: ; preds = %bb.true
347 bb.false: ; preds = %entry
351 merge: ; preds = %bb.true.0, %bb.false
356 declare void @func(i32)
357 declare %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* returned)
358 declare i32 @__gxx_wasm_personality_v0(...)
359 declare i8* @llvm.wasm.get.exception(token)
360 declare i32 @llvm.wasm.get.ehselector(token)
361 declare i32 @llvm.eh.typeid.for(i8*)
362 declare void @llvm.wasm.throw(i32, i8*)
363 declare void @llvm.wasm.rethrow()
364 declare i8* @__cxa_begin_catch(i8*)
365 declare void @__cxa_end_catch()
366 declare void @__cxa_rethrow()
367 declare void @__clang_call_terminate(i8*)
369 ; CHECK-DAG: declare void @llvm.wasm.landingpad.index(token, i32)
370 ; CHECK-DAG: declare i8* @llvm.wasm.lsda()
371 ; CHECK-DAG: declare i32 @_Unwind_CallPersonality(i8*)