1 ; RUN: opt < %s -winehprepare -demote-catchswitch-only -wasmehprepare -S | FileCheck %s
2 ; RUN: opt < %s -winehprepare -demote-catchswitch-only -wasmehprepare -S --mattr=+atomics,+bulk-memory | FileCheck %s
4 target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
5 target triple = "wasm32-unknown-unknown"
7 ; CHECK: @__wasm_lpad_context = external thread_local global { i32, ptr, i32 }
9 @_ZTIi = external constant ptr
10 %struct.Temp = type { i8 }
12 ; A single 'catch (int)' clause.
13 ; A wasm.catch() call, wasm.lsda() call, and personality call to generate a
14 ; selector should all be genereated after the catchpad.
23 define void @test0() personality ptr @__gxx_wasm_personality_v0 {
24 ; CHECK-LABEL: @test0()
27 to label %try.cont unwind label %catch.dispatch
29 catch.dispatch: ; preds = %entry
30 %0 = catchswitch within none [label %catch.start] unwind to caller
32 catch.start: ; preds = %catch.dispatch
33 %1 = catchpad within %0 [ptr @_ZTIi]
34 %2 = call ptr @llvm.wasm.get.exception(token %1)
35 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
36 %4 = call i32 @llvm.eh.typeid.for(ptr @_ZTIi)
37 %matches = icmp eq i32 %3, %4
38 br i1 %matches, label %catch, label %rethrow
40 ; CHECK-NEXT: %[[CATCHPAD:.*]] = catchpad
41 ; CHECK-NEXT: %[[EXN:.*]] = call ptr @llvm.wasm.catch(i32 0)
42 ; CHECK-NEXT: call void @llvm.wasm.landingpad.index(token %[[CATCHPAD]], i32 0)
43 ; CHECK-NEXT: store i32 0, ptr @__wasm_lpad_context
44 ; CHECK-NEXT: %[[LSDA:.*]] = call ptr @llvm.wasm.lsda()
45 ; CHECK-NEXT: store ptr %[[LSDA]], ptr getelementptr inbounds ({ i32, ptr, i32 }, ptr @__wasm_lpad_context, i32 0, i32 1)
46 ; CHECK-NEXT: call i32 @_Unwind_CallPersonality(ptr %[[EXN]]) {{.*}} [ "funclet"(token %[[CATCHPAD]]) ]
47 ; CHECK-NEXT: %[[SELECTOR:.*]] = load i32, ptr getelementptr inbounds ({ i32, ptr, i32 }, ptr @__wasm_lpad_context, i32 0, i32 2)
48 ; CHECK: icmp eq i32 %[[SELECTOR]]
50 catch: ; preds = %catch.start
51 %5 = call ptr @__cxa_begin_catch(ptr %2) [ "funclet"(token %1) ]
52 call void @__cxa_end_catch() [ "funclet"(token %1) ]
53 catchret from %1 to label %try.cont
55 ; CHECK-NEXT: call ptr @__cxa_begin_catch(ptr %[[EXN]])
57 rethrow: ; preds = %catch.start
58 call void @llvm.wasm.rethrow() [ "funclet"(token %1) ]
61 try.cont: ; preds = %entry, %catch
66 ; For the catchpad with a single 'catch (...)', only a wasm.catch() call should
67 ; be generated after the catchpad; wasm.landingpad.index() and personality call
68 ; should NOT be generated. For the other catchpad, the argument of
69 ; wasm.landingpad.index() should be not 1 but 0.
82 define void @test1() personality ptr @__gxx_wasm_personality_v0 {
83 ; CHECK-LABEL: @test1()
86 to label %try.cont unwind label %catch.dispatch
88 catch.dispatch: ; preds = %entry
89 %0 = catchswitch within none [label %catch.start] unwind to caller
91 catch.start: ; preds = %catch.dispatch
92 %1 = catchpad within %0 [ptr null]
93 %2 = call ptr @llvm.wasm.get.exception(token %1)
94 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
95 %4 = call ptr @__cxa_begin_catch(ptr %2) [ "funclet"(token %1) ]
96 call void @__cxa_end_catch() [ "funclet"(token %1) ]
97 catchret from %1 to label %try.cont
99 ; CHECK-NEXT: catchpad within %0 [ptr null]
100 ; CHECK-NOT: call void @llvm.wasm.landingpad.index
101 ; CHECK-NOT: store {{.*}} @__wasm_lpad_context
102 ; CHECK-NOT: call ptr @llvm.wasm.lsda()
103 ; CHECK-NOT: call i32 @_Unwind_CallPersonality
104 ; CHECK-NOT: load {{.*}} @__wasm_lpad_context
106 try.cont: ; preds = %entry, %catch.start
108 to label %try.cont7 unwind label %catch.dispatch2
110 catch.dispatch2: ; preds = %try.cont
111 %5 = catchswitch within none [label %catch.start3] unwind to caller
113 catch.start3: ; preds = %catch.dispatch2
114 %6 = catchpad within %5 [ptr @_ZTIi]
115 %7 = call ptr @llvm.wasm.get.exception(token %6)
116 %8 = call i32 @llvm.wasm.get.ehselector(token %6)
117 %9 = call i32 @llvm.eh.typeid.for(ptr @_ZTIi)
118 %matches = icmp eq i32 %8, %9
119 br i1 %matches, label %catch4, label %rethrow
120 ; CHECK: catch.start3:
121 ; CHECK: call void @llvm.wasm.landingpad.index(token %{{.+}}, i32 0)
123 catch4: ; preds = %catch.start3
124 %10 = call ptr @__cxa_begin_catch(ptr %7) [ "funclet"(token %6) ]
125 call void @__cxa_end_catch() [ "funclet"(token %6) ]
126 catchret from %6 to label %try.cont7
128 rethrow: ; preds = %catch.start3
129 call void @llvm.wasm.rethrow() [ "funclet"(token %6) ]
132 try.cont7: ; preds = %try.cont, %catch4
136 ; PHI demotion test. Only the phi before catchswitch should be demoted; the phi
137 ; before cleanuppad should NOT.
140 ; int bar(int) noexcept;
165 define void @test2() personality ptr @__gxx_wasm_personality_v0 {
166 ; CHECK-LABEL: @test2
168 %t = alloca %struct.Temp, align 1
170 to label %invoke.cont unwind label %ehcleanup
172 invoke.cont: ; preds = %entry
174 to label %invoke.cont1 unwind label %ehcleanup
176 invoke.cont1: ; preds = %invoke.cont
177 %call = call ptr @_ZN4TempD2Ev(ptr %t)
180 ehcleanup: ; preds = %invoke.cont, %entry
181 %num.0 = phi i32 [ 2, %invoke.cont ], [ 1, %entry ]
182 %0 = cleanuppad within none []
183 %call2 = call ptr @_ZN4TempD2Ev(ptr %t) [ "funclet"(token %0) ]
184 cleanupret from %0 unwind label %catch.dispatch
188 catch.dispatch: ; preds = %ehcleanup
189 %1 = catchswitch within none [label %catch.start] unwind to caller
191 catch.start: ; preds = %catch.dispatch
192 %2 = catchpad within %1 [ptr null]
193 %3 = call ptr @llvm.wasm.get.exception(token %2)
194 %4 = call i32 @llvm.wasm.get.ehselector(token %2)
195 %5 = call ptr @__cxa_begin_catch(ptr %3) [ "funclet"(token %2) ]
196 call void @bar(i32 %num.0) [ "funclet"(token %2) ]
197 call void @__cxa_end_catch() [ "funclet"(token %2) ]
198 catchret from %2 to label %try.cont
200 try.cont: ; preds = %catch.start, %invoke.cont1
202 to label %invoke.cont3 unwind label %catch.dispatch5
204 invoke.cont3: ; preds = %try.cont
206 to label %try.cont10 unwind label %catch.dispatch5
208 catch.dispatch5: ; preds = %invoke.cont3, %try.cont
209 %num.1 = phi i32 [ 2, %invoke.cont3 ], [ 1, %try.cont ]
210 %6 = catchswitch within none [label %catch.start6] unwind to caller
211 ; CHECK: catch.dispatch5:
214 catch.start6: ; preds = %catch.dispatch5
215 %7 = catchpad within %6 [ptr null]
216 %8 = call ptr @llvm.wasm.get.exception(token %7)
217 %9 = call i32 @llvm.wasm.get.ehselector(token %7)
218 %10 = call ptr @__cxa_begin_catch(ptr %8) [ "funclet"(token %7) ]
219 call void @bar(i32 %num.1) [ "funclet"(token %7) ]
220 call void @__cxa_end_catch() [ "funclet"(token %7) ]
221 catchret from %7 to label %try.cont10
223 try.cont10: ; preds = %invoke.cont3, %catch.start6
227 ; Tests if instructions after a call to @llvm.wasm.throw are deleted and the
228 ; BB's dead children are deleted.
230 ; CHECK-LABEL: @test3
231 define i32 @test3(i1 %b, ptr %p) {
233 br i1 %b, label %bb.true, label %bb.false
236 ; CHECK-NEXT: call void @llvm.wasm.throw(i32 0, ptr %p)
237 ; CHECK-NEXT: unreachable
238 bb.true: ; preds = %entry
239 call void @llvm.wasm.throw(i32 0, ptr %p)
242 ; CHECK-NOT: bb.true.0
243 bb.true.0: ; preds = %bb.true
247 bb.false: ; preds = %entry
251 merge: ; preds = %bb.true.0, %bb.false
256 declare void @bar(i32)
257 declare ptr @_ZN4TempD2Ev(ptr returned)
258 declare i32 @__gxx_wasm_personality_v0(...)
259 ; Function Attrs: nounwind
260 declare ptr @llvm.wasm.get.exception(token) #0
261 ; Function Attrs: nounwind
262 declare i32 @llvm.wasm.get.ehselector(token) #0
263 ; Function Attrs: nounwind
264 declare i32 @llvm.eh.typeid.for(ptr) #0
265 ; Function Attrs: noreturn
266 declare void @llvm.wasm.throw(i32, ptr) #1
267 ; Function Attrs: noreturn
268 declare void @llvm.wasm.rethrow() #1
269 declare ptr @__cxa_begin_catch(ptr)
270 declare void @__cxa_end_catch()
271 declare void @_ZSt9terminatev()
273 attributes #0 = { nounwind }
274 attributes #1 = { noreturn }
276 ; CHECK-DAG: declare void @llvm.wasm.landingpad.index(token, i32 immarg)
277 ; CHECK-DAG: declare ptr @llvm.wasm.lsda()
278 ; CHECK-DAG: declare i32 @_Unwind_CallPersonality(ptr)