blob: e6005e34057ce2133aea9526f29c2fbc319cf7de [file] [log] [blame]
Heejin Ahn99d60e02018-05-31 22:02:34 +00001; RUN: opt < %s -winehprepare -demote-catchswitch-only -wasmehprepare -S | FileCheck %s
2
3target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
4target triple = "wasm32-unknown-unknown"
5
6; CHECK: @__wasm_lpad_context = external global { i32, i8*, i32 }
7
8@_ZTIi = external constant i8*
9%struct.Cleanup = type { i8 }
10
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.
14define void @test0() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
15; CHECK-LABEL: @test0()
16entry:
17 invoke void @foo()
18 to label %try.cont unwind label %catch.dispatch
19
20catch.dispatch: ; preds = %entry
21 %0 = catchswitch within none [label %catch.start] unwind to caller
22
23catch.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
30; CHECK: catch.start:
31; CHECK-NEXT: %[[CATCHPAD:.*]] = catchpad
32; CHECK-NEXT: %[[EXN:.*]] = call i8* @llvm.wasm.catch(i32 0)
33; CHECK-NEXT: call void @llvm.wasm.landingpad.index(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]]
40
41catch: ; 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
45; CHECK: catch:
46; CHECK-NEXT: call i8* @__cxa_begin_catch(i8* %[[EXN]])
47
48rethrow: ; preds = %catch.start
49 call void @__cxa_rethrow() [ "funclet"(token %1) ]
50 unreachable
51
52try.cont: ; preds = %entry, %catch
53 ret void
54}
55
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.
61define void @test1() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
62; CHECK-LABEL: @test1()
63entry:
64 invoke void @foo()
65 to label %try.cont unwind label %catch.dispatch
66
67catch.dispatch: ; preds = %entry
68 %0 = catchswitch within none [label %catch.start] unwind to caller
69
70catch.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
77; CHECK: catch.start:
78; CHECK-NEXT: catchpad within %0 [i8* null]
79; CHECK-NEXT: call i8* @llvm.wasm.catch(i32 0)
80; CHECK-NOT: call void @llvm.wasm.landingpad.index
81; CHECK-NOT: store {{.*}} @__wasm_lpad_context
82; CHECK-NOT: call i8* @llvm.wasm.lsda()
83; CHECK-NOT: call i32 @_Unwind_CallPersonality
84; CHECK-NOT: load {{.*}} @__wasm_lpad_context
85
86try.cont: ; preds = %entry, %catch.start
87 invoke void @foo()
88 to label %try.cont7 unwind label %catch.dispatch2
89
90catch.dispatch2: ; preds = %try.cont
91 %5 = catchswitch within none [label %catch.start3] unwind to caller
92
93catch.start3: ; preds = %catch.dispatch2
94 %6 = catchpad within %5 [i8* bitcast (i8** @_ZTIi to i8*)]
95 %7 = call i8* @llvm.wasm.get.exception(token %6)
96 %8 = call i32 @llvm.wasm.get.ehselector(token %6)
97 %9 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
98 %matches = icmp eq i32 %8, %9
99 br i1 %matches, label %catch4, label %rethrow
100; CHECK: catch.start3:
101; CHECK: call void @llvm.wasm.landingpad.index(i32 0)
102
103catch4: ; preds = %catch.start3
104 %10 = call i8* @__cxa_begin_catch(i8* %7) [ "funclet"(token %6) ]
105 call void @__cxa_end_catch() [ "funclet"(token %6) ]
106 catchret from %6 to label %try.cont7
107
108rethrow: ; preds = %catch.start3
109 call void @__cxa_rethrow() [ "funclet"(token %6) ]
110 unreachable
111
112try.cont7: ; preds = %try.cont, %catch4
113 ret void
114}
115
116; A nested try-catch within a catch. Within the nested catchpad, wasm.lsda()
117; call should NOT be generated.
118define void @test2() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
119; CHECK-LABEL: @test2()
120entry:
121 invoke void @foo()
122 to label %try.cont9 unwind label %catch.dispatch
123
124catch.dispatch: ; preds = %entry
125 %0 = catchswitch within none [label %catch.start] unwind to caller
126
127catch.start: ; preds = %catch.dispatch
128 %1 = catchpad within %0 [i8* bitcast (i8** @_ZTIi to i8*)]
129 %2 = call i8* @llvm.wasm.get.exception(token %1)
130 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
131 %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
132 %matches = icmp eq i32 %3, %4
133 br i1 %matches, label %catch, label %rethrow
134; CHECK: catch.start:
135; CHECK: call i8* @llvm.wasm.lsda()
136
137catch: ; preds = %catch.start
138 %5 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
139 invoke void @foo() [ "funclet"(token %1) ]
140 to label %try.cont unwind label %catch.dispatch2
141
142catch.dispatch2: ; preds = %catch
143 %6 = catchswitch within %1 [label %catch.start3] unwind label %ehcleanup
144
145catch.start3: ; preds = %catch.dispatch2
146 %7 = catchpad within %6 [i8* bitcast (i8** @_ZTIi to i8*)]
147 %8 = call i8* @llvm.wasm.get.exception(token %7)
148 %9 = call i32 @llvm.wasm.get.ehselector(token %7)
149 %10 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
150 %matches4 = icmp eq i32 %9, %10
151 br i1 %matches4, label %catch6, label %rethrow5
152; CHECK: catch.start3:
153; CHECK-NOT: call i8* @llvm.wasm.lsda()
154
155catch6: ; preds = %catch.start3
156 %11 = call i8* @__cxa_begin_catch(i8* %8) [ "funclet"(token %7) ]
157 call void @__cxa_end_catch() [ "funclet"(token %7) ]
158 catchret from %7 to label %try.cont
159
160rethrow5: ; preds = %catch.start3
161 invoke void @__cxa_rethrow() [ "funclet"(token %7) ]
162 to label %unreachable unwind label %ehcleanup
163
164try.cont: ; preds = %catch, %catch6
165 call void @__cxa_end_catch() [ "funclet"(token %1) ]
166 catchret from %1 to label %try.cont9
167
168rethrow: ; preds = %catch.start
169 call void @__cxa_rethrow() [ "funclet"(token %1) ]
170 unreachable
171
172try.cont9: ; preds = %entry, %try.cont
173 ret void
174
175ehcleanup: ; preds = %rethrow5, %catch.dispatch2
176 %12 = cleanuppad within %1 []
177 call void @__cxa_end_catch() [ "funclet"(token %12) ]
178 cleanupret from %12 unwind to caller
179; CHECK: ehcleanup:
180; CHECK-NEXT: cleanuppad
181; CHECK-NOT: call i8* @llvm.wasm.catch(i32 0)
182; CHECK-NOT: call void @llvm.wasm.landingpad.index
183; CHECK-NOT: store {{.*}} @__wasm_lpad_context
184; CHECK-NOT: call i8* @llvm.wasm.lsda()
185; CHECK-NOT: call i32 @_Unwind_CallPersonality
186; CHECK-NOT: load {{.*}} @__wasm_lpad_context
187
188unreachable: ; preds = %rethrow5
189 unreachable
190}
191
192; A cleanuppad with a call to __clang_call_terminate().
193; A call to wasm.catch() should be generated after the cleanuppad.
194define hidden void @test3() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
195; CHECK-LABEL: @test3
196entry:
197 invoke void @foo()
198 to label %try.cont unwind label %catch.dispatch
199
200catch.dispatch: ; preds = %entry
201 %0 = catchswitch within none [label %catch.start] unwind to caller
202
203catch.start: ; preds = %catch.dispatch
204 %1 = catchpad within %0 [i8* null]
205 %2 = call i8* @llvm.wasm.get.exception(token %1)
206 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
207 %4 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
208 invoke void @foo() [ "funclet"(token %1) ]
209 to label %invoke.cont1 unwind label %ehcleanup
210
211invoke.cont1: ; preds = %catch.start
212 call void @__cxa_end_catch() [ "funclet"(token %1) ]
213 catchret from %1 to label %try.cont
214
215try.cont: ; preds = %entry, %invoke.cont1
216 ret void
217
218ehcleanup: ; preds = %catch.start
219 %5 = cleanuppad within %1 []
220 invoke void @__cxa_end_catch() [ "funclet"(token %5) ]
221 to label %invoke.cont2 unwind label %terminate
222
223invoke.cont2: ; preds = %ehcleanup
224 cleanupret from %5 unwind to caller
225
226terminate: ; preds = %ehcleanup
227 %6 = cleanuppad within %5 []
228 %7 = call i8* @llvm.wasm.get.exception(token %6)
229 call void @__clang_call_terminate(i8* %7) [ "funclet"(token %6) ]
230 unreachable
231; CHECK: terminate:
232; CHECK-NEXT: cleanuppad
233; CHECK-NEXT: %[[EXN:.*]] = call i8* @llvm.wasm.catch(i32 0)
234; CHECK-NEXT: call void @__clang_call_terminate(i8* %[[EXN]])
235}
236
237; PHI demotion test. Only the phi before catchswitch should be demoted; the phi
238; before cleanuppad should NOT.
239define void @test5() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
240; CHECK-LABEL: @test5
241entry:
242 %c = alloca %struct.Cleanup, align 1
243 invoke void @foo()
244 to label %invoke.cont unwind label %ehcleanup
245
246invoke.cont: ; preds = %entry
247 invoke void @foo()
248 to label %invoke.cont1 unwind label %ehcleanup
249
250invoke.cont1: ; preds = %invoke.cont
251 %call = call %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* %c)
252 br label %try.cont
253
254ehcleanup: ; preds = %invoke.cont, %entry
255 %num.0 = phi i32 [ 2, %invoke.cont ], [ 1, %entry ]
256 %0 = cleanuppad within none []
257 %call2 = call %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* %c) [ "funclet"(token %0) ]
258 cleanupret from %0 unwind label %catch.dispatch
259; CHECK: ehcleanup:
260; CHECK-NEXT: = phi
261
262catch.dispatch: ; preds = %ehcleanup
263 %1 = catchswitch within none [label %catch.start] unwind to caller
264
265catch.start: ; preds = %catch.dispatch
266 %2 = catchpad within %1 [i8* null]
267 %3 = call i8* @llvm.wasm.get.exception(token %2)
268 %4 = call i32 @llvm.wasm.get.ehselector(token %2)
269 %5 = call i8* @__cxa_begin_catch(i8* %3) [ "funclet"(token %2) ]
270 call void @func(i32 %num.0) [ "funclet"(token %2) ]
271 call void @__cxa_end_catch() [ "funclet"(token %2) ]
272 catchret from %2 to label %try.cont
273
274try.cont: ; preds = %catch.start, %invoke.cont1
275 invoke void @foo()
276 to label %invoke.cont3 unwind label %catch.dispatch5
277
278invoke.cont3: ; preds = %try.cont
279 invoke void @foo()
280 to label %try.cont10 unwind label %catch.dispatch5
281
282catch.dispatch5: ; preds = %invoke.cont3, %try.cont
283 %num.1 = phi i32 [ 2, %invoke.cont3 ], [ 1, %try.cont ]
284 %6 = catchswitch within none [label %catch.start6] unwind to caller
285; CHECK: catch.dispatch5:
286; CHECK-NOT: = phi
287
288catch.start6: ; preds = %catch.dispatch5
289 %7 = catchpad within %6 [i8* null]
290 %8 = call i8* @llvm.wasm.get.exception(token %7)
291 %9 = call i32 @llvm.wasm.get.ehselector(token %7)
292 %10 = call i8* @__cxa_begin_catch(i8* %8) [ "funclet"(token %7) ]
293 call void @func(i32 %num.1) [ "funclet"(token %7) ]
294 call void @__cxa_end_catch() [ "funclet"(token %7) ]
295 catchret from %7 to label %try.cont10
296
297try.cont10: ; preds = %invoke.cont3, %catch.start6
298 ret void
299}
300
301declare void @foo()
302declare void @func(i32)
303declare %struct.Cleanup* @_ZN7CleanupD1Ev(%struct.Cleanup* returned)
304declare i32 @__gxx_wasm_personality_v0(...)
305declare i8* @llvm.wasm.get.exception(token)
306declare i32 @llvm.wasm.get.ehselector(token)
307declare i32 @llvm.eh.typeid.for(i8*)
308declare i8* @__cxa_begin_catch(i8*)
309declare void @__cxa_end_catch()
310declare void @__cxa_rethrow()
311declare void @__clang_call_terminate(i8*)
312
313; CHECK-DAG: declare i8* @llvm.wasm.catch(i32)
314; CHECK-DAG: declare void @llvm.wasm.landingpad.index(i32)
315; CHECK-DAG: declare i8* @llvm.wasm.lsda()
316; CHECK-DAG: declare i32 @_Unwind_CallPersonality(i8*)
317