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