blob: f0f49344cfc7da6c524b664fc05484d52a8dd96b [file] [log] [blame]
Heejin Ahn78297632019-02-26 03:29:59 +00001; RUN: llc < %s -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -disable-block-placement -verify-machineinstrs -fast-isel=false -machine-sink-split-probability-threshold=0 -cgp-freq-ratio-to-skip-merge=1000 -exception-model=wasm -mattr=+exception-handling | FileCheck %s
Heejin Ahn44a5a4b2019-03-26 17:15:55 +00002; RUN: llc < %s -O0 -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -verify-machineinstrs -exception-model=wasm -mattr=+exception-handling | FileCheck %s --check-prefix=NOOPT
Heejin Ahn7fb68d22018-08-07 20:19:23 +00003
4target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
5target triple = "wasm32-unknown-unknown"
6
7@_ZTIi = external constant i8*
8@_ZTId = external constant i8*
9
10; Simple test case with two catch clauses
Heejin Ahn78297632019-02-26 03:29:59 +000011;
12; void foo();
Heejin Ahnd6f48782019-01-30 03:21:57 +000013; void test0() {
14; try {
15; foo();
Heejin Ahn78297632019-02-26 03:29:59 +000016; } catch (int) {
17; } catch (double) {
Heejin Ahnd6f48782019-01-30 03:21:57 +000018; }
19; }
Heejin Ahn7fb68d22018-08-07 20:19:23 +000020
21; CHECK-LABEL: test0
Heejin Ahncf699b42019-02-27 00:50:53 +000022; CHECK: try
23; CHECK: call foo
24; CHECK: catch
25; CHECK: block
26; CHECK: br_if 0, {{.*}} # 0: down to label2
27; CHECK: i32.call $drop=, __cxa_begin_catch
28; CHECK: call __cxa_end_catch
29; CHECK: br 1 # 1: down to label0
30; CHECK: end_block # label2:
31; CHECK: block
32; CHECK: br_if 0, {{.*}} # 0: down to label3
33; CHECK: i32.call $drop=, __cxa_begin_catch
34; CHECK: call __cxa_end_catch
35; CHECK: br 1 # 1: down to label0
36; CHECK: end_block # label3:
Heejin Ahn66ce4192019-03-16 05:38:57 +000037; CHECK: rethrow {{.*}} # to caller
Heejin Ahncf699b42019-02-27 00:50:53 +000038; CHECK: end_try # label0:
Heejin Ahn7fb68d22018-08-07 20:19:23 +000039define void @test0() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
40entry:
41 invoke void @foo()
42 to label %try.cont unwind label %catch.dispatch
43
44catch.dispatch: ; preds = %entry
45 %0 = catchswitch within none [label %catch.start] unwind to caller
46
47catch.start: ; preds = %catch.dispatch
48 %1 = catchpad within %0 [i8* bitcast (i8** @_ZTIi to i8*), i8* bitcast (i8** @_ZTId to i8*)]
49 %2 = call i8* @llvm.wasm.get.exception(token %1)
50 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
51 %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
52 %matches = icmp eq i32 %3, %4
53 br i1 %matches, label %catch2, label %catch.fallthrough
54
55catch2: ; preds = %catch.start
56 %5 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
Heejin Ahn7fb68d22018-08-07 20:19:23 +000057 call void @__cxa_end_catch() [ "funclet"(token %1) ]
58 catchret from %1 to label %try.cont
59
60catch.fallthrough: ; preds = %catch.start
Heejin Ahn78297632019-02-26 03:29:59 +000061 %6 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTId to i8*))
62 %matches1 = icmp eq i32 %3, %6
Heejin Ahn7fb68d22018-08-07 20:19:23 +000063 br i1 %matches1, label %catch, label %rethrow
64
65catch: ; preds = %catch.fallthrough
Heejin Ahn78297632019-02-26 03:29:59 +000066 %7 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
Heejin Ahn7fb68d22018-08-07 20:19:23 +000067 call void @__cxa_end_catch() [ "funclet"(token %1) ]
68 catchret from %1 to label %try.cont
69
70rethrow: ; preds = %catch.fallthrough
Heejin Ahn66ce4192019-03-16 05:38:57 +000071 call void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %1) ]
Heejin Ahn7fb68d22018-08-07 20:19:23 +000072 unreachable
73
Heejin Ahn44a5a4b2019-03-26 17:15:55 +000074try.cont: ; preds = %catch, %catch2, %entry
Heejin Ahn7fb68d22018-08-07 20:19:23 +000075 ret void
76}
77
78; Nested try-catches within a catch
Heejin Ahnd6f48782019-01-30 03:21:57 +000079; void test1() {
80; try {
81; foo();
Heejin Ahn78297632019-02-26 03:29:59 +000082; } catch (int) {
Heejin Ahnd6f48782019-01-30 03:21:57 +000083; try {
84; foo();
Heejin Ahn78297632019-02-26 03:29:59 +000085; } catch (int) {
Heejin Ahnd6f48782019-01-30 03:21:57 +000086; foo();
87; }
88; }
89; }
Heejin Ahn7fb68d22018-08-07 20:19:23 +000090
91; CHECK-LABEL: test1
Heejin Ahn82da1ff2019-02-27 01:35:14 +000092; CHECK: try
93; CHECK: call foo
94; CHECK: catch
95; CHECK: block
96; CHECK: block
97; CHECK: br_if 0, {{.*}} # 0: down to label7
98; CHECK: i32.call $drop=, __cxa_begin_catch
99; CHECK: try
100; CHECK: call foo
101; CHECK: br 2 # 2: down to label6
102; CHECK: catch
103; CHECK: try
104; CHECK: block
105; CHECK: br_if 0, {{.*}} # 0: down to label11
106; CHECK: i32.call $drop=, __cxa_begin_catch
107; CHECK: try
108; CHECK: call foo
Heejin Ahn195a62e2019-03-03 22:35:56 +0000109; CHECK: br 2 # 2: down to label9
Heejin Ahn82da1ff2019-02-27 01:35:14 +0000110; CHECK: catch
111; CHECK: call __cxa_end_catch
Heejin Ahn66ce4192019-03-16 05:38:57 +0000112; CHECK: rethrow {{.*}} # down to catch3
Heejin Ahn82da1ff2019-02-27 01:35:14 +0000113; CHECK: end_try
114; CHECK: end_block # label11:
Heejin Ahn66ce4192019-03-16 05:38:57 +0000115; CHECK: rethrow {{.*}} # down to catch3
Heejin Ahn82da1ff2019-02-27 01:35:14 +0000116; CHECK: catch {{.*}} # catch3:
117; CHECK: call __cxa_end_catch
Heejin Ahn66ce4192019-03-16 05:38:57 +0000118; CHECK: rethrow {{.*}} # to caller
Heejin Ahn195a62e2019-03-03 22:35:56 +0000119; CHECK: end_try # label9:
Heejin Ahn82da1ff2019-02-27 01:35:14 +0000120; CHECK: call __cxa_end_catch
121; CHECK: br 2 # 2: down to label6
122; CHECK: end_try
123; CHECK: end_block # label7:
Heejin Ahn66ce4192019-03-16 05:38:57 +0000124; CHECK: rethrow {{.*}} # to caller
Heejin Ahn82da1ff2019-02-27 01:35:14 +0000125; CHECK: end_block # label6:
126; CHECK: call __cxa_end_catch
127; CHECK: end_try
Heejin Ahnd6f48782019-01-30 03:21:57 +0000128define void @test1() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
Heejin Ahn7fb68d22018-08-07 20:19:23 +0000129entry:
130 invoke void @foo()
131 to label %try.cont11 unwind label %catch.dispatch
132
133catch.dispatch: ; preds = %entry
134 %0 = catchswitch within none [label %catch.start] unwind to caller
135
136catch.start: ; preds = %catch.dispatch
137 %1 = catchpad within %0 [i8* bitcast (i8** @_ZTIi to i8*)]
138 %2 = call i8* @llvm.wasm.get.exception(token %1)
139 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
140 %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
141 %matches = icmp eq i32 %3, %4
142 br i1 %matches, label %catch, label %rethrow
143
144catch: ; preds = %catch.start
145 %5 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
146 %6 = bitcast i8* %5 to i32*
147 %7 = load i32, i32* %6, align 4
148 invoke void @foo() [ "funclet"(token %1) ]
149 to label %try.cont unwind label %catch.dispatch2
150
151catch.dispatch2: ; preds = %catch
152 %8 = catchswitch within %1 [label %catch.start3] unwind label %ehcleanup9
153
154catch.start3: ; preds = %catch.dispatch2
155 %9 = catchpad within %8 [i8* bitcast (i8** @_ZTIi to i8*)]
156 %10 = call i8* @llvm.wasm.get.exception(token %9)
157 %11 = call i32 @llvm.wasm.get.ehselector(token %9)
158 %12 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
159 %matches4 = icmp eq i32 %11, %12
160 br i1 %matches4, label %catch6, label %rethrow5
161
162catch6: ; preds = %catch.start3
163 %13 = call i8* @__cxa_begin_catch(i8* %10) [ "funclet"(token %9) ]
164 %14 = bitcast i8* %13 to i32*
165 %15 = load i32, i32* %14, align 4
166 invoke void @foo() [ "funclet"(token %9) ]
167 to label %invoke.cont8 unwind label %ehcleanup
168
169invoke.cont8: ; preds = %catch6
170 call void @__cxa_end_catch() [ "funclet"(token %9) ]
171 catchret from %9 to label %try.cont
172
173rethrow5: ; preds = %catch.start3
Heejin Ahn66ce4192019-03-16 05:38:57 +0000174 invoke void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %9) ]
Heejin Ahn7fb68d22018-08-07 20:19:23 +0000175 to label %unreachable unwind label %ehcleanup9
176
Heejin Ahn44a5a4b2019-03-26 17:15:55 +0000177try.cont: ; preds = %invoke.cont8, %catch
Heejin Ahn7fb68d22018-08-07 20:19:23 +0000178 call void @__cxa_end_catch() [ "funclet"(token %1) ]
179 catchret from %1 to label %try.cont11
180
181rethrow: ; preds = %catch.start
Heejin Ahn66ce4192019-03-16 05:38:57 +0000182 call void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %1) ]
Heejin Ahn7fb68d22018-08-07 20:19:23 +0000183 unreachable
184
Heejin Ahn44a5a4b2019-03-26 17:15:55 +0000185try.cont11: ; preds = %try.cont, %entry
Heejin Ahn7fb68d22018-08-07 20:19:23 +0000186 ret void
187
188ehcleanup: ; preds = %catch6
189 %16 = cleanuppad within %9 []
190 call void @__cxa_end_catch() [ "funclet"(token %16) ]
191 cleanupret from %16 unwind label %ehcleanup9
192
193ehcleanup9: ; preds = %ehcleanup, %rethrow5, %catch.dispatch2
194 %17 = cleanuppad within %1 []
195 call void @__cxa_end_catch() [ "funclet"(token %17) ]
196 cleanupret from %17 unwind to caller
197
198unreachable: ; preds = %rethrow5
199 unreachable
200}
201
202; Nested loop within a catch clause
Heejin Ahnd6f48782019-01-30 03:21:57 +0000203; void test2() {
204; try {
205; foo();
206; } catch (...) {
207; for (int i = 0; i < 50; i++)
208; foo();
209; }
210; }
Heejin Ahn7fb68d22018-08-07 20:19:23 +0000211
212; CHECK-LABEL: test2
Heejin Ahncf699b42019-02-27 00:50:53 +0000213; CHECK: try
214; CHECK: call foo
215; CHECK: catch
216; CHECK: i32.call $drop=, __cxa_begin_catch
217; CHECK: loop # label15:
218; CHECK: block
Heejin Ahn78297632019-02-26 03:29:59 +0000219; CHECK: block
Heejin Ahncf699b42019-02-27 00:50:53 +0000220; CHECK: br_if 0, {{.*}} # 0: down to label17
221; CHECK: try
222; CHECK: call foo
223; CHECK: br 2 # 2: down to label16
224; CHECK: catch
Heejin Ahn78297632019-02-26 03:29:59 +0000225; CHECK: try
Heejin Ahncf699b42019-02-27 00:50:53 +0000226; CHECK: call __cxa_end_catch
Heejin Ahn78297632019-02-26 03:29:59 +0000227; CHECK: catch
Heejin Ahncf699b42019-02-27 00:50:53 +0000228; CHECK: call __clang_call_terminate
229; CHECK: unreachable
Heejin Ahn78297632019-02-26 03:29:59 +0000230; CHECK: end_try
Heejin Ahn66ce4192019-03-16 05:38:57 +0000231; CHECK: rethrow {{.*}} # to caller
Heejin Ahncf699b42019-02-27 00:50:53 +0000232; CHECK: end_try
233; CHECK: end_block # label17:
234; CHECK: call __cxa_end_catch
235; CHECK: br 2 # 2: down to label13
236; CHECK: end_block # label16:
237; CHECK: br 0 # 0: up to label15
238; CHECK: end_loop
239; CHECK: end_try # label13:
Heejin Ahn7fb68d22018-08-07 20:19:23 +0000240define void @test2() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
241entry:
242 invoke void @foo()
243 to label %try.cont unwind label %catch.dispatch
244
245catch.dispatch: ; preds = %entry
246 %0 = catchswitch within none [label %catch.start] unwind to caller
247
248catch.start: ; preds = %catch.dispatch
249 %1 = catchpad within %0 [i8* null]
250 %2 = call i8* @llvm.wasm.get.exception(token %1)
251 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
252 %4 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
253 br label %for.cond
254
255for.cond: ; preds = %for.inc, %catch.start
256 %i.0 = phi i32 [ 0, %catch.start ], [ %inc, %for.inc ]
257 %cmp = icmp slt i32 %i.0, 50
258 br i1 %cmp, label %for.body, label %for.end
259
260for.body: ; preds = %for.cond
261 invoke void @foo() [ "funclet"(token %1) ]
262 to label %for.inc unwind label %ehcleanup
263
264for.inc: ; preds = %for.body
265 %inc = add nsw i32 %i.0, 1
266 br label %for.cond
267
268for.end: ; preds = %for.cond
269 call void @__cxa_end_catch() [ "funclet"(token %1) ]
270 catchret from %1 to label %try.cont
271
272try.cont: ; preds = %for.end, %entry
273 ret void
274
275ehcleanup: ; preds = %for.body
276 %5 = cleanuppad within %1 []
277 invoke void @__cxa_end_catch() [ "funclet"(token %5) ]
278 to label %invoke.cont2 unwind label %terminate
279
280invoke.cont2: ; preds = %ehcleanup
281 cleanupret from %5 unwind to caller
282
283terminate: ; preds = %ehcleanup
284 %6 = cleanuppad within %5 []
285 %7 = call i8* @llvm.wasm.get.exception(token %6)
286 call void @__clang_call_terminate(i8* %7) [ "funclet"(token %6) ]
287 unreachable
288}
289
Heejin Ahn44a5a4b2019-03-26 17:15:55 +0000290; Tests if block and try markers are correctly placed. Even if two predecessors
291; of the EH pad are bb2 and bb3 and their nearest common dominator is bb1, the
292; TRY marker should be placed at bb0 because there's a branch from bb0 to bb2,
293; and scopes cannot be interleaved.
294
295; NOOPT-LABEL: test3
296; NOOPT: try
297; NOOPT: block
298; NOOPT: block
299; NOOPT: block
300; NOOPT: end_block
301; NOOPT: end_block
302; NOOPT: call foo
303; NOOPT: end_block
304; NOOPT: call bar
305; NOOPT: catch {{.*}}
306; NOOPT: end_try
307define void @test3() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
308bb0:
309 br i1 undef, label %bb1, label %bb2
310
311bb1: ; preds = %bb0
312 br i1 undef, label %bb3, label %bb4
313
314bb2: ; preds = %bb0
315 br label %try.cont
316
317bb3: ; preds = %bb1
318 invoke void @foo()
319 to label %try.cont unwind label %catch.dispatch
320
321bb4: ; preds = %bb1
322 invoke void @bar()
323 to label %try.cont unwind label %catch.dispatch
324
325catch.dispatch: ; preds = %bb4, %bb3
326 %0 = catchswitch within none [label %catch.start] unwind to caller
327
328catch.start: ; preds = %catch.dispatch
329 %1 = catchpad within %0 [i8* null]
330 %2 = call i8* @llvm.wasm.get.exception(token %1)
331 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
332 catchret from %1 to label %try.cont
333
334try.cont: ; preds = %catch.start, %bb4, %bb3, %bb2
335 ret void
336}
337
Heejin Ahn7fb68d22018-08-07 20:19:23 +0000338declare void @foo()
339declare void @bar()
340declare i32 @__gxx_wasm_personality_v0(...)
341declare i8* @llvm.wasm.get.exception(token)
342declare i32 @llvm.wasm.get.ehselector(token)
Heejin Ahn66ce4192019-03-16 05:38:57 +0000343declare void @llvm.wasm.rethrow.in.catch()
Heejin Ahn7fb68d22018-08-07 20:19:23 +0000344declare i32 @llvm.eh.typeid.for(i8*)
345declare i8* @__cxa_begin_catch(i8*)
346declare void @__cxa_end_catch()
Heejin Ahn7fb68d22018-08-07 20:19:23 +0000347declare void @__clang_call_terminate(i8*)
348declare void @_ZSt9terminatev()