1; RUN: llc < %s --mtriple=wasm32-unknown-unknown -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling | FileCheck %s -check-prefixes=CHECK,NOPIC -DPTR=32
2; RUN: llc < %s --mtriple=wasm64-unknown-unknown -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling | FileCheck %s -check-prefixes=CHECK,NOPIC -DPTR=64
3; RUN: llc < %s --mtriple=wasm32-unknown-emscripten -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling | FileCheck %s -check-prefixes=CHECK,NOPIC -DPTR=32
4; RUN: llc < %s --mtriple=wasm64-unknown-emscripten -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling | FileCheck %s -check-prefixes=CHECK,NOPIC -DPTR=64
5; RUN: llc < %s --mtriple=wasm32-unknown-emscripten -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling -relocation-model=pic | FileCheck %s -check-prefixes=CHECK,PIC -DPTR=32
6; RUN: llc < %s --mtriple=wasm64-unknown-emscripten -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling -relocation-model=pic | FileCheck %s -check-prefixes=CHECK,PIC -DPTR=64
7
8@_ZTIi = external constant i8*
9@_ZTIf = external constant i8*
10@_ZTId = external constant i8*
11
12; Single catch (...) does not need an exception table.
13;
14; try {
15;   may_throw();
16; } catch (...) {
17; }
18; CHECK-LABEL: test0:
19; CHECK-NOT: GCC_except_table
20define void @test0() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
21entry:
22  invoke void @may_throw()
23          to label %try.cont unwind label %catch.dispatch
24
25catch.dispatch:                                   ; preds = %entry
26  %0 = catchswitch within none [label %catch.start] unwind to caller
27
28catch.start:                                      ; preds = %catch.dispatch
29  %1 = catchpad within %0 [i8* null]
30  %2 = call i8* @llvm.wasm.get.exception(token %1)
31  %3 = call i32 @llvm.wasm.get.ehselector(token %1)
32  %4 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
33  call void @__cxa_end_catch() [ "funclet"(token %1) ]
34  catchret from %1 to label %try.cont
35
36try.cont:                                         ; preds = %entry, %catch.start
37  ret void
38}
39
40; Exception table generation + shared action test.
41;
42; try {
43;   may_throw();
44; } catch (int) {
45; } catch (float) {
46; } catch (double) {
47; } catch (...) {
48; }
49;
50; try {
51;   may_throw();
52; } catch (double) {
53; } catch (...) {
54; }
55;
56; try {
57;   may_throw();
58; } catch (int) {
59; } catch (float) {
60; }
61;
62; There are three landing pads. The second landing pad should share action table
63; entries with the first landing pad because they end with the same sequence
64; (double -> ...). But the third landing table cannot share action table entries
65; with others, so it should create its own entries.
66
67; CHECK-LABEL: test1:
68; In static linking, we load GCC_except_table as a constant directly.
69; NOPIC:      i[[PTR]].const  $push[[CONTEXT:.*]]=, __wasm_lpad_context
70; NOPIC-NEXT: i[[PTR]].const  $push[[EXCEPT_TABLE:.*]]=, GCC_except_table1
71; NOPIC-NEXT: i[[PTR]].store  {{[48]}}($pop[[CONTEXT]]), $pop[[EXCEPT_TABLE]]
72
73; In case of PIC, we make GCC_except_table symbols a relative on based on
74; __memory_base.
75; PIC:        global.get  $push[[CONTEXT:.*]]=, __wasm_lpad_context@GOT
76; PIC-NEXT:   local.tee  $push{{.*}}=, $[[CONTEXT_LOCAL:.*]]=, $pop[[CONTEXT]]
77; PIC:        global.get  $push[[MEMORY_BASE:.*]]=, __memory_base
78; PIC-NEXT:   i[[PTR]].const  $push[[EXCEPT_TABLE_REL:.*]]=, GCC_except_table1@MBREL
79; PIC-NEXT:   i[[PTR]].add   $push[[EXCEPT_TABLE:.*]]=, $pop[[MEMORY_BASE]], $pop[[EXCEPT_TABLE_REL]]
80; PIC-NEXT:   i[[PTR]].store  {{[48]}}($[[CONTEXT_LOCAL]]), $pop[[EXCEPT_TABLE]]
81
82; CHECK: .section  .rodata.gcc_except_table,"",@
83; CHECK-NEXT:   .p2align  2
84; CHECK-NEXT: GCC_except_table[[START:[0-9]+]]:
85; CHECK-NEXT: .Lexception0:
86; CHECK-NEXT:   .int8  255                     # @LPStart Encoding = omit
87; CHECK-NEXT:   .int8  0                       # @TType Encoding = absptr
88; CHECK-NEXT:   .uleb128 .Lttbase0-.Lttbaseref0
89; CHECK-NEXT: .Lttbaseref0:
90; CHECK-NEXT:   .int8  1                       # Call site Encoding = uleb128
91; CHECK-NEXT:   .uleb128 .Lcst_end0-.Lcst_begin0
92; CHECK-NEXT: .Lcst_begin0:
93; CHECK-NEXT:   .int8  0                       # >> Call Site 0 <<
94; CHECK-NEXT:                                  #   On exception at call site 0
95; CHECK-NEXT:   .int8  7                       #   Action: 4
96; CHECK-NEXT:   .int8  1                       # >> Call Site 1 <<
97; CHECK-NEXT:                                  #   On exception at call site 1
98; CHECK-NEXT:   .int8  3                       #   Action: 2
99; CHECK-NEXT:   .int8  2                       # >> Call Site 2 <<
100; CHECK-NEXT:                                  #   On exception at call site 2
101; CHECK-NEXT:   .int8  11                      #   Action: 6
102; CHECK-NEXT: .Lcst_end0:
103; CHECK-NEXT:   .int8  1                       # >> Action Record 1 <<
104; CHECK-NEXT:                                  #   Catch TypeInfo 1
105; CHECK-NEXT:   .int8  0                       #   No further actions
106; CHECK-NEXT:   .int8  2                       # >> Action Record 2 <<
107; CHECK-NEXT:                                  #   Catch TypeInfo 2
108; CHECK-NEXT:   .int8  125                     #   Continue to action 1
109; CHECK-NEXT:   .int8  3                       # >> Action Record 3 <<
110; CHECK-NEXT:                                  #   Catch TypeInfo 3
111; CHECK-NEXT:   .int8  125                     #   Continue to action 2
112; CHECK-NEXT:   .int8  4                       # >> Action Record 4 <<
113; CHECK-NEXT:                                  #   Catch TypeInfo 4
114; CHECK-NEXT:   .int8  125                     #   Continue to action 3
115; CHECK-NEXT:   .int8  3                       # >> Action Record 5 <<
116; CHECK-NEXT:                                  #   Catch TypeInfo 3
117; CHECK-NEXT:   .int8  0                       #   No further actions
118; CHECK-NEXT:   .int8  4                       # >> Action Record 6 <<
119; CHECK-NEXT:                                  #   Catch TypeInfo 4
120; CHECK-NEXT:   .int8  125                     #   Continue to action 5
121; CHECK-NEXT:   .p2align  2
122; CHECK-NEXT:                                  # >> Catch TypeInfos <<
123; CHECK-NEXT:   .int[[PTR]]  _ZTIi             # TypeInfo 4
124; CHECK-NEXT:   .int[[PTR]]  _ZTIf             # TypeInfo 3
125; CHECK-NEXT:   .int[[PTR]]  _ZTId             # TypeInfo 2
126; CHECK-NEXT:   .int[[PTR]]  0                 # TypeInfo 1
127; CHECK-NEXT: .Lttbase0:
128; CHECK-NEXT:   .p2align  2
129; CHECK-NEXT: .LGCC_except_table_end[[END:[0-9]+]]:
130; CHECK-NEXT:   .size  GCC_except_table[[START]], .LGCC_except_table_end[[END]]-GCC_except_table[[START]]
131define void @test1() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
132entry:
133  invoke void @may_throw()
134          to label %try.cont unwind label %catch.dispatch
135
136catch.dispatch:                                   ; preds = %entry
137  %0 = catchswitch within none [label %catch.start] unwind to caller
138
139catch.start:                                      ; preds = %catch.dispatch
140  %1 = catchpad within %0 [i8* bitcast (i8** @_ZTIi to i8*), i8* bitcast (i8** @_ZTIf to i8*), i8* bitcast (i8** @_ZTId to i8*), i8* null]
141  %2 = call i8* @llvm.wasm.get.exception(token %1)
142  %3 = call i32 @llvm.wasm.get.ehselector(token %1)
143  %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
144  %matches = icmp eq i32 %3, %4
145  br i1 %matches, label %catch10, label %catch.fallthrough
146
147catch10:                                          ; preds = %catch.start
148  %5 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
149  %6 = bitcast i8* %5 to i32*
150  %7 = load i32, i32* %6, align 4
151  call void @__cxa_end_catch() [ "funclet"(token %1) ]
152  catchret from %1 to label %try.cont
153
154try.cont:                                         ; preds = %entry, %catch, %catch4, %catch7, %catch10
155  invoke void @may_throw()
156          to label %try.cont23 unwind label %catch.dispatch14
157
158catch.dispatch14:                                 ; preds = %try.cont
159  %8 = catchswitch within none [label %catch.start15] unwind to caller
160
161catch.start15:                                    ; preds = %catch.dispatch14
162  %9 = catchpad within %8 [i8* bitcast (i8** @_ZTId to i8*), i8* null]
163  %10 = call i8* @llvm.wasm.get.exception(token %9)
164  %11 = call i32 @llvm.wasm.get.ehselector(token %9)
165  %12 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTId to i8*))
166  %matches16 = icmp eq i32 %11, %12
167  %13 = call i8* @__cxa_begin_catch(i8* %10) [ "funclet"(token %9) ]
168  br i1 %matches16, label %catch20, label %catch17
169
170catch20:                                          ; preds = %catch.start15
171  %14 = bitcast i8* %13 to double*
172  %15 = load double, double* %14, align 8
173  call void @__cxa_end_catch() [ "funclet"(token %9) ]
174  catchret from %9 to label %try.cont23
175
176try.cont23:                                       ; preds = %try.cont, %catch17, %catch20
177  invoke void @may_throw()
178          to label %try.cont36 unwind label %catch.dispatch25
179
180catch.dispatch25:                                 ; preds = %try.cont23
181  %16 = catchswitch within none [label %catch.start26] unwind to caller
182
183catch.start26:                                    ; preds = %catch.dispatch25
184  %17 = catchpad within %16 [i8* bitcast (i8** @_ZTIi to i8*), i8* bitcast (i8** @_ZTIf to i8*)]
185  %18 = call i8* @llvm.wasm.get.exception(token %17)
186  %19 = call i32 @llvm.wasm.get.ehselector(token %17)
187  %20 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
188  %matches27 = icmp eq i32 %19, %20
189  br i1 %matches27, label %catch33, label %catch.fallthrough28
190
191catch33:                                          ; preds = %catch.start26
192  %21 = call i8* @__cxa_begin_catch(i8* %18) [ "funclet"(token %17) ]
193  %22 = bitcast i8* %21 to i32*
194  %23 = load i32, i32* %22, align 4
195  call void @__cxa_end_catch() [ "funclet"(token %17) ]
196  catchret from %17 to label %try.cont36
197
198catch.fallthrough28:                              ; preds = %catch.start26
199  %24 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIf to i8*))
200  %matches29 = icmp eq i32 %19, %24
201  br i1 %matches29, label %catch30, label %rethrow
202
203catch30:                                          ; preds = %catch.fallthrough28
204  %25 = call i8* @__cxa_begin_catch(i8* %18) [ "funclet"(token %17) ]
205  %26 = bitcast i8* %25 to float*
206  %27 = load float, float* %26, align 4
207  call void @__cxa_end_catch() [ "funclet"(token %17) ]
208  catchret from %17 to label %try.cont36
209
210rethrow:                                          ; preds = %catch.fallthrough28
211  call void @__cxa_rethrow() [ "funclet"(token %17) ]
212  unreachable
213
214try.cont36:                                       ; preds = %try.cont23, %catch30, %catch33
215  ret void
216
217catch17:                                          ; preds = %catch.start15
218  call void @__cxa_end_catch() [ "funclet"(token %9) ]
219  catchret from %9 to label %try.cont23
220
221catch.fallthrough:                                ; preds = %catch.start
222  %28 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIf to i8*))
223  %matches1 = icmp eq i32 %3, %28
224  br i1 %matches1, label %catch7, label %catch.fallthrough2
225
226catch7:                                           ; preds = %catch.fallthrough
227  %29 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
228  %30 = bitcast i8* %29 to float*
229  %31 = load float, float* %30, align 4
230  call void @__cxa_end_catch() [ "funclet"(token %1) ]
231  catchret from %1 to label %try.cont
232
233catch.fallthrough2:                               ; preds = %catch.fallthrough
234  %32 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTId to i8*))
235  %matches3 = icmp eq i32 %3, %32
236  %33 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ]
237  br i1 %matches3, label %catch4, label %catch
238
239catch4:                                           ; preds = %catch.fallthrough2
240  %34 = bitcast i8* %33 to double*
241  %35 = load double, double* %34, align 8
242  call void @__cxa_end_catch() [ "funclet"(token %1) ]
243  catchret from %1 to label %try.cont
244
245catch:                                            ; preds = %catch.fallthrough2
246  call void @__cxa_end_catch() [ "funclet"(token %1) ]
247  catchret from %1 to label %try.cont
248}
249
250declare void @may_throw()
251; Function Attrs: nounwind
252declare i32 @llvm.eh.typeid.for(i8*) #0
253; Function Attrs: nounwind
254declare i8* @llvm.wasm.get.exception(token) #0
255; Function Attrs: nounwind
256declare i32 @llvm.wasm.get.ehselector(token) #0
257declare void @__cxa_rethrow()
258declare i8* @__cxa_begin_catch(i8*)
259declare void @__cxa_end_catch()
260declare i32 @__gxx_wasm_personality_v0(...)
261
262attributes #0 = { nounwind }
263