1; RUN: opt < %s -wasm-lower-em-ehsjlj -enable-emscripten-cxx-exceptions -S | FileCheck %s --check-prefixes=CHECK,NO-TLS -DPTR=i32
2; RUN: opt < %s -wasm-lower-em-ehsjlj -enable-emscripten-cxx-exceptions -S --mattr=+atomics,+bulk-memory | FileCheck %s --check-prefixes=CHECK,TLS -DPTR=i32
3; RUN: opt < %s -wasm-lower-em-ehsjlj -enable-emscripten-cxx-exceptions --mtriple=wasm64-unknown-unknown -data-layout="e-m:e-p:64:64-i64:64-n32:64-S128" -S | FileCheck %s --check-prefixes=CHECK -DPTR=i64
4
5target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
6target triple = "wasm32-unknown-unknown"
7
8@_ZTIi = external constant i8*
9@_ZTIc = external constant i8*
10; NO-TLS-DAG: __THREW__ = external global [[PTR]]
11; NO-TLS-DAG: __threwValue = external global i32
12; TLS-DAG: __THREW__ = external thread_local(localexec) global [[PTR]]
13; TLS-DAG: __threwValue = external thread_local(localexec) global i32
14
15; Test invoke instruction with clauses (try-catch block)
16define void @clause() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
17; CHECK-LABEL: @clause(
18entry:
19  invoke void @foo(i32 3)
20          to label %invoke.cont unwind label %lpad
21; CHECK: entry:
22; CHECK-NEXT: store [[PTR]] 0, [[PTR]]* @__THREW__
23; CHECK-NEXT: call cc{{.*}} void @__invoke_void_i32(void (i32)* @foo, i32 3)
24; CHECK-NEXT: %[[__THREW__VAL:.*]] = load [[PTR]], [[PTR]]* @__THREW__
25; CHECK-NEXT: store [[PTR]] 0, [[PTR]]* @__THREW__
26; CHECK-NEXT: %cmp = icmp eq [[PTR]] %[[__THREW__VAL]], 1
27; CHECK-NEXT: br i1 %cmp, label %lpad, label %invoke.cont
28
29invoke.cont:                                      ; preds = %entry
30  br label %try.cont
31
32lpad:                                             ; preds = %entry
33  %0 = landingpad { i8*, i32 }
34          catch i8* bitcast (i8** @_ZTIi to i8*)
35          catch i8* null
36  %1 = extractvalue { i8*, i32 } %0, 0
37  %2 = extractvalue { i8*, i32 } %0, 1
38  br label %catch.dispatch
39; CHECK: lpad:
40; CHECK-NEXT: %[[FMC:.*]] = call i8* @__cxa_find_matching_catch_4(i8* bitcast (i8** @_ZTIi to i8*), i8* null)
41; CHECK-NEXT: %[[IVI1:.*]] = insertvalue { i8*, i32 } undef, i8* %[[FMC]], 0
42; CHECK-NEXT: %[[TEMPRET0_VAL:.*]] = call i32 @getTempRet0()
43; CHECK-NEXT: %[[IVI2:.*]] = insertvalue { i8*, i32 } %[[IVI1]], i32 %[[TEMPRET0_VAL]], 1
44; CHECK-NEXT: extractvalue { i8*, i32 } %[[IVI2]], 0
45; CHECK-NEXT: %[[CDR:.*]] = extractvalue { i8*, i32 } %[[IVI2]], 1
46
47catch.dispatch:                                   ; preds = %lpad
48  %3 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
49  %matches = icmp eq i32 %2, %3
50  br i1 %matches, label %catch1, label %catch
51; CHECK: catch.dispatch:
52; CHECK-NEXT: %[[TYPEID:.*]] = call i32 @llvm_eh_typeid_for(i8* bitcast (i8** @_ZTIi to i8*))
53; CHECK-NEXT: %matches = icmp eq i32 %[[CDR]], %[[TYPEID]]
54
55catch1:                                           ; preds = %catch.dispatch
56  %4 = call i8* @__cxa_begin_catch(i8* %1)
57  %5 = bitcast i8* %4 to i32*
58  %6 = load i32, i32* %5, align 4
59  call void @__cxa_end_catch()
60  br label %try.cont
61
62try.cont:                                         ; preds = %catch, %catch1, %invoke.cont
63  ret void
64
65catch:                                            ; preds = %catch.dispatch
66  %7 = call i8* @__cxa_begin_catch(i8* %1)
67  call void @__cxa_end_catch()
68  br label %try.cont
69}
70
71; Test invoke instruction with filters (functions with throw(...) declaration)
72; Currently we don't support exception specifications correctly in JS glue code,
73; so we ignore all filters here.
74; See https://bugs.llvm.org/show_bug.cgi?id=50396.
75define void @filter() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
76; CHECK-LABEL: @filter(
77entry:
78  invoke void @foo(i32 3)
79          to label %invoke.cont unwind label %lpad
80; CHECK: entry:
81; CHECK-NEXT: store [[PTR]] 0, [[PTR]]* @__THREW__
82; CHECK-NEXT: call cc{{.*}} void @__invoke_void_i32(void (i32)* @foo, i32 3)
83; CHECK-NEXT: %[[__THREW__VAL:.*]] = load [[PTR]], [[PTR]]* @__THREW__
84; CHECK-NEXT: store [[PTR]] 0, [[PTR]]* @__THREW__
85; CHECK-NEXT: %cmp = icmp eq [[PTR]] %[[__THREW__VAL]], 1
86; CHECK-NEXT: br i1 %cmp, label %lpad, label %invoke.cont
87
88invoke.cont:                                      ; preds = %entry
89  ret void
90
91lpad:                                             ; preds = %entry
92  %0 = landingpad { i8*, i32 }
93          filter [2 x i8*] [i8* bitcast (i8** @_ZTIi to i8*), i8* bitcast (i8** @_ZTIc to i8*)]
94  %1 = extractvalue { i8*, i32 } %0, 0
95  %2 = extractvalue { i8*, i32 } %0, 1
96  br label %filter.dispatch
97; CHECK: lpad:
98; We now temporarily ignore filters because of the bug, so we pass nothing to
99; __cxa_find_matching_catch
100; CHECK-NEXT: %[[FMC:.*]] = call i8* @__cxa_find_matching_catch_2()
101
102filter.dispatch:                                  ; preds = %lpad
103  %ehspec.fails = icmp slt i32 %2, 0
104  br i1 %ehspec.fails, label %ehspec.unexpected, label %eh.resume
105
106ehspec.unexpected:                                ; preds = %filter.dispatch
107  call void @__cxa_call_unexpected(i8* %1) #4
108  unreachable
109
110eh.resume:                                        ; preds = %filter.dispatch
111  %lpad.val = insertvalue { i8*, i32 } undef, i8* %1, 0
112  %lpad.val3 = insertvalue { i8*, i32 } %lpad.val, i32 %2, 1
113  resume { i8*, i32 } %lpad.val3
114; CHECK: eh.resume:
115; CHECK-NEXT: insertvalue
116; CHECK-NEXT: %[[LPAD_VAL:.*]] = insertvalue
117; CHECK-NEXT: %[[LOW:.*]] = extractvalue { i8*, i32 } %[[LPAD_VAL]], 0
118; CHECK-NEXT: call void @__resumeException(i8* %[[LOW]])
119; CHECK-NEXT: unreachable
120}
121
122; Test if argument attributes indices in newly created call instructions are correct
123define void @arg_attributes() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
124; CHECK-LABEL: @arg_attributes(
125entry:
126  %0 = invoke noalias i8* @bar(i8 signext 1, i8 zeroext 2)
127          to label %invoke.cont unwind label %lpad
128; CHECK: entry:
129; CHECK-NEXT: store [[PTR]] 0, [[PTR]]* @__THREW__
130; CHECK-NEXT: %0 = call cc{{.*}} noalias i8* @"__invoke_i8*_i8_i8"(i8* (i8, i8)* @bar, i8 signext 1, i8 zeroext 2)
131
132invoke.cont:                                      ; preds = %entry
133  br label %try.cont
134
135lpad:                                             ; preds = %entry
136  %1 = landingpad { i8*, i32 }
137          catch i8* bitcast (i8** @_ZTIi to i8*)
138          catch i8* null
139  %2 = extractvalue { i8*, i32 } %1, 0
140  %3 = extractvalue { i8*, i32 } %1, 1
141  br label %catch.dispatch
142
143catch.dispatch:                                   ; preds = %lpad
144  %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
145  %matches = icmp eq i32 %3, %4
146  br i1 %matches, label %catch1, label %catch
147
148catch1:                                           ; preds = %catch.dispatch
149  %5 = call i8* @__cxa_begin_catch(i8* %2)
150  %6 = bitcast i8* %5 to i32*
151  %7 = load i32, i32* %6, align 4
152  call void @__cxa_end_catch()
153  br label %try.cont
154
155try.cont:                                         ; preds = %catch, %catch1, %invoke.cont
156  ret void
157
158catch:                                            ; preds = %catch.dispatch
159  %8 = call i8* @__cxa_begin_catch(i8* %2)
160  call void @__cxa_end_catch()
161  br label %try.cont
162}
163
164declare void @foo(i32)
165declare i8* @bar(i8, i8)
166
167declare i32 @__gxx_personality_v0(...)
168declare i32 @llvm.eh.typeid.for(i8*)
169declare i8* @__cxa_begin_catch(i8*)
170declare void @__cxa_end_catch()
171declare void @__cxa_call_unexpected(i8*)
172
173; JS glue functions and invoke wrappers declaration
174; CHECK-DAG: declare i32 @getTempRet0()
175; CHECK-DAG: declare void @setTempRet0(i32)
176; CHECK-DAG: declare void @__resumeException(i8*)
177; CHECK-DAG: declare void @__invoke_void_i32(void (i32)*, i32)
178; CHECK-DAG: declare i8* @__cxa_find_matching_catch_4(i8*, i8*)
179