blob: 8e9933471b7896805c73be4b81a14e61e20a5579 [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 Ahn7fb68d22018-08-07 20:19:23 +00002
3target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
4target triple = "wasm32-unknown-unknown"
5
6@_ZTIi = external constant i8*
7@_ZTId = external constant i8*
8
9; Simple test case with two catch clauses
Heejin Ahn78297632019-02-26 03:29:59 +000010;
11; void foo();
Heejin Ahnd6f48782019-01-30 03:21:57 +000012; void test0() {
13; try {
14; foo();
Heejin Ahn78297632019-02-26 03:29:59 +000015; } catch (int) {
16; } catch (double) {
Heejin Ahnd6f48782019-01-30 03:21:57 +000017; }
18; }
Heejin Ahn7fb68d22018-08-07 20:19:23 +000019
20; CHECK-LABEL: test0
Heejin Ahncf699b42019-02-27 00:50:53 +000021; CHECK: try
22; CHECK: call foo
23; CHECK: catch
24; CHECK: block
25; CHECK: br_if 0, {{.*}} # 0: down to label2
26; CHECK: i32.call $drop=, __cxa_begin_catch
27; CHECK: call __cxa_end_catch
28; CHECK: br 1 # 1: down to label0
29; CHECK: end_block # label2:
30; CHECK: block
31; CHECK: br_if 0, {{.*}} # 0: down to label3
32; CHECK: i32.call $drop=, __cxa_begin_catch
33; CHECK: call __cxa_end_catch
34; CHECK: br 1 # 1: down to label0
35; CHECK: end_block # label3:
36; CHECK: call __cxa_rethrow
37; CHECK: end_try # label0:
Heejin Ahn7fb68d22018-08-07 20:19:23 +000038define void @test0() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
39entry:
40 invoke void @foo()
41 to label %try.cont unwind label %catch.dispatch
42
43catch.dispatch: ; preds = %entry
44 %0 = catchswitch within none [label %catch.start] unwind to caller
45
46catch.start: ; preds = %catch.dispatch
47 %1 = catchpad within %0 [i8* bitcast (i8** @_ZTIi to i8*), i8* bitcast (i8** @_ZTId to i8*)]
48 %2 = call i8* @llvm.wasm.get.exception(token %1)
49 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
50 %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
51 %matches = icmp eq i32 %3, %4
52 br i1 %matches, label %catch2, label %catch.fallthrough
53
54catch2: ; preds = %catch.start
55 %5 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
Heejin Ahn7fb68d22018-08-07 20:19:23 +000056 call void @__cxa_end_catch() [ "funclet"(token %1) ]
57 catchret from %1 to label %try.cont
58
59catch.fallthrough: ; preds = %catch.start
Heejin Ahn78297632019-02-26 03:29:59 +000060 %6 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTId to i8*))
61 %matches1 = icmp eq i32 %3, %6
Heejin Ahn7fb68d22018-08-07 20:19:23 +000062 br i1 %matches1, label %catch, label %rethrow
63
64catch: ; preds = %catch.fallthrough
Heejin Ahn78297632019-02-26 03:29:59 +000065 %7 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
Heejin Ahn7fb68d22018-08-07 20:19:23 +000066 call void @__cxa_end_catch() [ "funclet"(token %1) ]
67 catchret from %1 to label %try.cont
68
69rethrow: ; preds = %catch.fallthrough
70 call void @__cxa_rethrow() [ "funclet"(token %1) ]
71 unreachable
72
73try.cont: ; preds = %entry, %catch, %catch2
74 ret void
75}
76
77; Nested try-catches within a catch
Heejin Ahnd6f48782019-01-30 03:21:57 +000078; void test1() {
79; try {
80; foo();
Heejin Ahn78297632019-02-26 03:29:59 +000081; } catch (int) {
Heejin Ahnd6f48782019-01-30 03:21:57 +000082; try {
83; foo();
Heejin Ahn78297632019-02-26 03:29:59 +000084; } catch (int) {
Heejin Ahnd6f48782019-01-30 03:21:57 +000085; foo();
86; }
87; }
88; }
Heejin Ahn7fb68d22018-08-07 20:19:23 +000089
90; CHECK-LABEL: test1
Heejin Ahn78297632019-02-26 03:29:59 +000091; TODO Fill in CHECK lines once we fix ScopeTops info bug in D58605
Heejin Ahnd6f48782019-01-30 03:21:57 +000092define void @test1() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
Heejin Ahn7fb68d22018-08-07 20:19:23 +000093entry:
94 invoke void @foo()
95 to label %try.cont11 unwind label %catch.dispatch
96
97catch.dispatch: ; preds = %entry
98 %0 = catchswitch within none [label %catch.start] unwind to caller
99
100catch.start: ; preds = %catch.dispatch
101 %1 = catchpad within %0 [i8* bitcast (i8** @_ZTIi to i8*)]
102 %2 = call i8* @llvm.wasm.get.exception(token %1)
103 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
104 %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
105 %matches = icmp eq i32 %3, %4
106 br i1 %matches, label %catch, label %rethrow
107
108catch: ; preds = %catch.start
109 %5 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
110 %6 = bitcast i8* %5 to i32*
111 %7 = load i32, i32* %6, align 4
112 invoke void @foo() [ "funclet"(token %1) ]
113 to label %try.cont unwind label %catch.dispatch2
114
115catch.dispatch2: ; preds = %catch
116 %8 = catchswitch within %1 [label %catch.start3] unwind label %ehcleanup9
117
118catch.start3: ; preds = %catch.dispatch2
119 %9 = catchpad within %8 [i8* bitcast (i8** @_ZTIi to i8*)]
120 %10 = call i8* @llvm.wasm.get.exception(token %9)
121 %11 = call i32 @llvm.wasm.get.ehselector(token %9)
122 %12 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
123 %matches4 = icmp eq i32 %11, %12
124 br i1 %matches4, label %catch6, label %rethrow5
125
126catch6: ; preds = %catch.start3
127 %13 = call i8* @__cxa_begin_catch(i8* %10) [ "funclet"(token %9) ]
128 %14 = bitcast i8* %13 to i32*
129 %15 = load i32, i32* %14, align 4
130 invoke void @foo() [ "funclet"(token %9) ]
131 to label %invoke.cont8 unwind label %ehcleanup
132
133invoke.cont8: ; preds = %catch6
134 call void @__cxa_end_catch() [ "funclet"(token %9) ]
135 catchret from %9 to label %try.cont
136
137rethrow5: ; preds = %catch.start3
138 invoke void @__cxa_rethrow() [ "funclet"(token %9) ]
139 to label %unreachable unwind label %ehcleanup9
140
141try.cont: ; preds = %catch, %invoke.cont8
142 call void @__cxa_end_catch() [ "funclet"(token %1) ]
143 catchret from %1 to label %try.cont11
144
145rethrow: ; preds = %catch.start
146 call void @__cxa_rethrow() [ "funclet"(token %1) ]
147 unreachable
148
149try.cont11: ; preds = %entry, %try.cont
150 ret void
151
152ehcleanup: ; preds = %catch6
153 %16 = cleanuppad within %9 []
154 call void @__cxa_end_catch() [ "funclet"(token %16) ]
155 cleanupret from %16 unwind label %ehcleanup9
156
157ehcleanup9: ; preds = %ehcleanup, %rethrow5, %catch.dispatch2
158 %17 = cleanuppad within %1 []
159 call void @__cxa_end_catch() [ "funclet"(token %17) ]
160 cleanupret from %17 unwind to caller
161
162unreachable: ; preds = %rethrow5
163 unreachable
164}
165
166; Nested loop within a catch clause
Heejin Ahnd6f48782019-01-30 03:21:57 +0000167; void test2() {
168; try {
169; foo();
170; } catch (...) {
171; for (int i = 0; i < 50; i++)
172; foo();
173; }
174; }
Heejin Ahn7fb68d22018-08-07 20:19:23 +0000175
176; CHECK-LABEL: test2
Heejin Ahncf699b42019-02-27 00:50:53 +0000177; CHECK: try
178; CHECK: call foo
179; CHECK: catch
180; CHECK: i32.call $drop=, __cxa_begin_catch
181; CHECK: loop # label15:
182; CHECK: block
Heejin Ahn78297632019-02-26 03:29:59 +0000183; CHECK: block
Heejin Ahncf699b42019-02-27 00:50:53 +0000184; CHECK: br_if 0, {{.*}} # 0: down to label17
185; CHECK: try
186; CHECK: call foo
187; CHECK: br 2 # 2: down to label16
188; CHECK: catch
Heejin Ahn78297632019-02-26 03:29:59 +0000189; CHECK: try
Heejin Ahncf699b42019-02-27 00:50:53 +0000190; CHECK: call __cxa_end_catch
Heejin Ahn78297632019-02-26 03:29:59 +0000191; CHECK: catch
Heejin Ahncf699b42019-02-27 00:50:53 +0000192; CHECK: call __clang_call_terminate
193; CHECK: unreachable
Heejin Ahn78297632019-02-26 03:29:59 +0000194; CHECK: end_try
Heejin Ahncf699b42019-02-27 00:50:53 +0000195; CHECK: rethrow # to caller
196; CHECK: end_try
197; CHECK: end_block # label17:
198; CHECK: call __cxa_end_catch
199; CHECK: br 2 # 2: down to label13
200; CHECK: end_block # label16:
201; CHECK: br 0 # 0: up to label15
202; CHECK: end_loop
203; CHECK: end_try # label13:
Heejin Ahn7fb68d22018-08-07 20:19:23 +0000204define void @test2() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
205entry:
206 invoke void @foo()
207 to label %try.cont unwind label %catch.dispatch
208
209catch.dispatch: ; preds = %entry
210 %0 = catchswitch within none [label %catch.start] unwind to caller
211
212catch.start: ; preds = %catch.dispatch
213 %1 = catchpad within %0 [i8* null]
214 %2 = call i8* @llvm.wasm.get.exception(token %1)
215 %3 = call i32 @llvm.wasm.get.ehselector(token %1)
216 %4 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
217 br label %for.cond
218
219for.cond: ; preds = %for.inc, %catch.start
220 %i.0 = phi i32 [ 0, %catch.start ], [ %inc, %for.inc ]
221 %cmp = icmp slt i32 %i.0, 50
222 br i1 %cmp, label %for.body, label %for.end
223
224for.body: ; preds = %for.cond
225 invoke void @foo() [ "funclet"(token %1) ]
226 to label %for.inc unwind label %ehcleanup
227
228for.inc: ; preds = %for.body
229 %inc = add nsw i32 %i.0, 1
230 br label %for.cond
231
232for.end: ; preds = %for.cond
233 call void @__cxa_end_catch() [ "funclet"(token %1) ]
234 catchret from %1 to label %try.cont
235
236try.cont: ; preds = %for.end, %entry
237 ret void
238
239ehcleanup: ; preds = %for.body
240 %5 = cleanuppad within %1 []
241 invoke void @__cxa_end_catch() [ "funclet"(token %5) ]
242 to label %invoke.cont2 unwind label %terminate
243
244invoke.cont2: ; preds = %ehcleanup
245 cleanupret from %5 unwind to caller
246
247terminate: ; preds = %ehcleanup
248 %6 = cleanuppad within %5 []
249 %7 = call i8* @llvm.wasm.get.exception(token %6)
250 call void @__clang_call_terminate(i8* %7) [ "funclet"(token %6) ]
251 unreachable
252}
253
254declare void @foo()
255declare void @bar()
256declare i32 @__gxx_wasm_personality_v0(...)
257declare i8* @llvm.wasm.get.exception(token)
258declare i32 @llvm.wasm.get.ehselector(token)
259declare i32 @llvm.eh.typeid.for(i8*)
260declare i8* @__cxa_begin_catch(i8*)
261declare void @__cxa_end_catch()
262declare void @__cxa_rethrow()
263declare void @__clang_call_terminate(i8*)
264declare void @_ZSt9terminatev()