1 ; RUN: opt < %s -wasm-lower-em-ehsjlj -S | FileCheck %s
3 target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
4 target triple = "wasm32-unknown-unknown"
6 @_ZTIi = external constant i8*
7 @_ZTIc = external constant i8*
8 ; CHECK-DAG: __THREW__ = external global i32
9 ; CHECK-DAG: __threwValue = external global i32
11 ; Test invoke instruction with clauses (try-catch block)
12 define void @clause() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
13 ; CHECK-LABEL: @clause(
15 invoke void @foo(i32 3)
16 to label %invoke.cont unwind label %lpad
18 ; CHECK-NEXT: store i32 0, i32* @__THREW__
19 ; CHECK-NEXT: call void @__invoke_void_i32(void (i32)* @foo, i32 3)
20 ; CHECK-NEXT: %[[__THREW__VAL:.*]] = load i32, i32* @__THREW__
21 ; CHECK-NEXT: store i32 0, i32* @__THREW__
22 ; CHECK-NEXT: %cmp = icmp eq i32 %[[__THREW__VAL]], 1
23 ; CHECK-NEXT: br i1 %cmp, label %lpad, label %invoke.cont
25 invoke.cont: ; preds = %entry
28 lpad: ; preds = %entry
29 %0 = landingpad { i8*, i32 }
30 catch i8* bitcast (i8** @_ZTIi to i8*)
32 %1 = extractvalue { i8*, i32 } %0, 0
33 %2 = extractvalue { i8*, i32 } %0, 1
34 br label %catch.dispatch
36 ; CHECK-NEXT: %[[FMC:.*]] = call i8* @__cxa_find_matching_catch_4(i8* bitcast (i8** @_ZTIi to i8*), i8* null)
37 ; CHECK-NEXT: %[[IVI1:.*]] = insertvalue { i8*, i32 } undef, i8* %[[FMC]], 0
38 ; CHECK-NEXT: %[[TEMPRET0_VAL:.*]] = call i32 @getTempRet0()
39 ; CHECK-NEXT: %[[IVI2:.*]] = insertvalue { i8*, i32 } %[[IVI1]], i32 %[[TEMPRET0_VAL]], 1
40 ; CHECK-NEXT: extractvalue { i8*, i32 } %[[IVI2]], 0
41 ; CHECK-NEXT: %[[CDR:.*]] = extractvalue { i8*, i32 } %[[IVI2]], 1
43 catch.dispatch: ; preds = %lpad
44 %3 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*))
45 %matches = icmp eq i32 %2, %3
46 br i1 %matches, label %catch1, label %catch
47 ; CHECK: catch.dispatch:
48 ; CHECK-NEXT: %[[TYPEID:.*]] = call i32 @llvm_eh_typeid_for(i8* bitcast (i8** @_ZTIi to i8*))
49 ; CHECK-NEXT: %matches = icmp eq i32 %[[CDR]], %[[TYPEID]]
51 catch1: ; preds = %catch.dispatch
52 %4 = call i8* @__cxa_begin_catch(i8* %1)
53 %5 = bitcast i8* %4 to i32*
54 %6 = load i32, i32* %5, align 4
55 call void @__cxa_end_catch()
58 try.cont: ; preds = %catch, %catch1, %invoke.cont
61 catch: ; preds = %catch.dispatch
62 %7 = call i8* @__cxa_begin_catch(i8* %1)
63 call void @__cxa_end_catch()
67 ; Test invoke instruction with filters (functions with throw(...) declaration)
68 define void @filter() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
69 ; CHECK-LABEL: @filter(
71 invoke void @foo(i32 3)
72 to label %invoke.cont unwind label %lpad
74 ; CHECK-NEXT: store i32 0, i32* @__THREW__
75 ; CHECK-NEXT: call void @__invoke_void_i32(void (i32)* @foo, i32 3)
76 ; CHECK-NEXT: %[[__THREW__VAL:.*]] = load i32, i32* @__THREW__
77 ; CHECK-NEXT: store i32 0, i32* @__THREW__
78 ; CHECK-NEXT: %cmp = icmp eq i32 %[[__THREW__VAL]], 1
79 ; CHECK-NEXT: br i1 %cmp, label %lpad, label %invoke.cont
81 invoke.cont: ; preds = %entry
84 lpad: ; preds = %entry
85 %0 = landingpad { i8*, i32 }
86 filter [2 x i8*] [i8* bitcast (i8** @_ZTIi to i8*), i8* bitcast (i8** @_ZTIc to i8*)]
87 %1 = extractvalue { i8*, i32 } %0, 0
88 %2 = extractvalue { i8*, i32 } %0, 1
89 br label %filter.dispatch
91 ; CHECK-NEXT: %[[FMC:.*]] = call i8* @__cxa_find_matching_catch_4(i8* bitcast (i8** @_ZTIi to i8*), i8* bitcast (i8** @_ZTIc to i8*))
92 ; CHECK-NEXT: %[[IVI1:.*]] = insertvalue { i8*, i32 } undef, i8* %[[FMC]], 0
93 ; CHECK-NEXT: %[[TEMPRET0_VAL:.*]] = call i32 @getTempRet0()
94 ; CHECK-NEXT: %[[IVI2:.*]] = insertvalue { i8*, i32 } %[[IVI1]], i32 %[[TEMPRET0_VAL]], 1
95 ; CHECK-NEXT: extractvalue { i8*, i32 } %[[IVI2]], 0
96 ; CHECK-NEXT: extractvalue { i8*, i32 } %[[IVI2]], 1
98 filter.dispatch: ; preds = %lpad
99 %ehspec.fails = icmp slt i32 %2, 0
100 br i1 %ehspec.fails, label %ehspec.unexpected, label %eh.resume
102 ehspec.unexpected: ; preds = %filter.dispatch
103 call void @__cxa_call_unexpected(i8* %1) #4
106 eh.resume: ; preds = %filter.dispatch
107 %lpad.val = insertvalue { i8*, i32 } undef, i8* %1, 0
108 %lpad.val3 = insertvalue { i8*, i32 } %lpad.val, i32 %2, 1
109 resume { i8*, i32 } %lpad.val3
111 ; CHECK-NEXT: insertvalue
112 ; CHECK-NEXT: %[[LPAD_VAL:.*]] = insertvalue
113 ; CHECK-NEXT: %[[LOW:.*]] = extractvalue { i8*, i32 } %[[LPAD_VAL]], 0
114 ; CHECK-NEXT: call void @__resumeException(i8* %[[LOW]])
115 ; CHECK-NEXT: unreachable
118 ; Test if argument attributes indices in newly created call instructions are correct
119 define void @arg_attributes() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
120 ; CHECK-LABEL: @arg_attributes(
122 %0 = invoke noalias i8* @bar(i8 signext 1, i8 zeroext 2)
123 to label %invoke.cont unwind label %lpad
125 ; CHECK-NEXT: store i32 0, i32* @__THREW__
126 ; CHECK-NEXT: %0 = call noalias i8* @"__invoke_i8*_i8_i8"(i8* (i8, i8)* @bar, i8 signext 1, i8 zeroext 2)
128 invoke.cont: ; preds = %entry
131 lpad: ; preds = %entry
132 %1 = landingpad { i8*, i32 }
133 catch i8* bitcast (i8** @_ZTIi to i8*)
135 %2 = extractvalue { i8*, i32 } %1, 0
136 %3 = extractvalue { i8*, i32 } %1, 1
137 br label %catch.dispatch
139 catch.dispatch: ; preds = %lpad
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 %catch1, label %catch
144 catch1: ; preds = %catch.dispatch
145 %5 = call i8* @__cxa_begin_catch(i8* %2)
146 %6 = bitcast i8* %5 to i32*
147 %7 = load i32, i32* %6, align 4
148 call void @__cxa_end_catch()
151 try.cont: ; preds = %catch, %catch1, %invoke.cont
154 catch: ; preds = %catch.dispatch
155 %8 = call i8* @__cxa_begin_catch(i8* %2)
156 call void @__cxa_end_catch()
160 declare void @foo(i32)
161 declare i8* @bar(i8, i8)
163 declare i32 @__gxx_personality_v0(...)
164 declare i32 @llvm.eh.typeid.for(i8*)
165 declare i8* @__cxa_begin_catch(i8*)
166 declare void @__cxa_end_catch()
167 declare void @__cxa_call_unexpected(i8*)
169 ; JS glue functions and invoke wrappers declaration
170 ; CHECK-DAG: declare i32 @getTempRet0()
171 ; CHECK-DAG: declare void @setTempRet0(i32)
172 ; CHECK-DAG: declare void @__resumeException(i8*)
173 ; CHECK-DAG: declare void @__invoke_void_i32(void (i32)*, i32)
174 ; CHECK-DAG: declare i8* @__cxa_find_matching_catch_4(i8*, i8*)