1 ; RUN: opt < %s -wasm-lower-em-ehsjlj -S | FileCheck %s --check-prefixes=CHECK,NO-TLS -DPTR=i32
2 ; RUN: opt < %s -wasm-lower-em-ehsjlj -S --mattr=+atomics,+bulk-memory | FileCheck %s --check-prefixes=CHECK,TLS -DPTR=i32
3 ; RUN: opt < %s -wasm-lower-em-ehsjlj --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
5 target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
6 target triple = "wasm32-unknown-unknown"
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
15 ; Test invoke instruction with clauses (try-catch block)
16 define void @clause() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
17 ; CHECK-LABEL: @clause(
19 invoke void @foo(i32 3)
20 to label %invoke.cont unwind label %lpad
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
29 invoke.cont: ; preds = %entry
32 lpad: ; preds = %entry
33 %0 = landingpad { i8*, i32 }
34 catch i8* bitcast (i8** @_ZTIi to i8*)
36 %1 = extractvalue { i8*, i32 } %0, 0
37 %2 = extractvalue { i8*, i32 } %0, 1
38 br label %catch.dispatch
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
47 catch.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]]
55 catch1: ; 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()
62 try.cont: ; preds = %catch, %catch1, %invoke.cont
65 catch: ; preds = %catch.dispatch
66 %7 = call i8* @__cxa_begin_catch(i8* %1)
67 call void @__cxa_end_catch()
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.
75 define void @filter() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
76 ; CHECK-LABEL: @filter(
78 invoke void @foo(i32 3)
79 to label %invoke.cont unwind label %lpad
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
88 invoke.cont: ; preds = %entry
91 lpad: ; 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
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()
102 filter.dispatch: ; preds = %lpad
103 %ehspec.fails = icmp slt i32 %2, 0
104 br i1 %ehspec.fails, label %ehspec.unexpected, label %eh.resume
106 ehspec.unexpected: ; preds = %filter.dispatch
107 call void @__cxa_call_unexpected(i8* %1) #4
110 eh.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
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
122 ; Test if argument attributes indices in newly created call instructions are correct
123 define void @arg_attributes() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
124 ; CHECK-LABEL: @arg_attributes(
126 %0 = invoke noalias i8* @bar(i8 signext 1, i8 zeroext 2)
127 to label %invoke.cont unwind label %lpad
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)
132 invoke.cont: ; preds = %entry
135 lpad: ; preds = %entry
136 %1 = landingpad { i8*, i32 }
137 catch i8* bitcast (i8** @_ZTIi to i8*)
139 %2 = extractvalue { i8*, i32 } %1, 0
140 %3 = extractvalue { i8*, i32 } %1, 1
141 br label %catch.dispatch
143 catch.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
148 catch1: ; 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()
155 try.cont: ; preds = %catch, %catch1, %invoke.cont
158 catch: ; preds = %catch.dispatch
159 %8 = call i8* @__cxa_begin_catch(i8* %2)
160 call void @__cxa_end_catch()
164 declare void @foo(i32)
165 declare i8* @bar(i8, i8)
167 declare i32 @__gxx_personality_v0(...)
168 declare i32 @llvm.eh.typeid.for(i8*)
169 declare i8* @__cxa_begin_catch(i8*)
170 declare void @__cxa_end_catch()
171 declare void @__cxa_call_unexpected(i8*)
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*)