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
4
5target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
6target triple = "wasm32-unknown-unknown"
7
8%struct.__jmp_buf_tag = type { [6 x i32], i32, [32 x i32] }
9
10@global_var = global i32 0, align 4
11; NO-TLS-DAG: __THREW__ = external global [[PTR]]
12; NO-TLS-DAG: __threwValue = external global [[PTR]]
13; TLS-DAG: __THREW__ = external thread_local(localexec) global i32
14; TLS-DAG: __threwValue = external thread_local(localexec) global i32
15@global_longjmp_ptr = global void (%struct.__jmp_buf_tag*, i32)* @longjmp, align 4
16; CHECK-DAG: @global_longjmp_ptr = global void (%struct.__jmp_buf_tag*, i32)* bitcast (void ([[PTR]], i32)* @emscripten_longjmp to void (%struct.__jmp_buf_tag*, i32)*)
17
18; Test a simple setjmp - longjmp sequence
19define void @setjmp_longjmp() {
20; CHECK-LABEL: @setjmp_longjmp
21entry:
22  %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
23  %arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
24  %call = call i32 @setjmp(%struct.__jmp_buf_tag* %arraydecay) #0
25  %arraydecay1 = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
26  call void @longjmp(%struct.__jmp_buf_tag* %arraydecay1, i32 1) #1
27  unreachable
28; CHECK: entry:
29; CHECK-NEXT: %[[MALLOCCALL:.*]] = tail call i8* @malloc(i32 40)
30; CHECK-NEXT: %[[SETJMP_TABLE:.*]] = bitcast i8* %[[MALLOCCALL]] to i32*
31; CHECK-NEXT: store i32 0, i32* %[[SETJMP_TABLE]]
32; CHECK-NEXT: %[[SETJMP_TABLE_SIZE:.*]] = add i32 4, 0
33; CHECK-NEXT: %[[BUF:.*]] = alloca [1 x %struct.__jmp_buf_tag]
34; CHECK-NEXT: %[[ARRAYDECAY:.*]] = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %[[BUF]], i32 0, i32 0
35; CHECK-NEXT: %[[SETJMP_TABLE1:.*]] = call i32* @saveSetjmp(%struct.__jmp_buf_tag* %[[ARRAYDECAY]], i32 1, i32* %[[SETJMP_TABLE]], i32 %[[SETJMP_TABLE_SIZE]])
36; CHECK-NEXT: %[[SETJMP_TABLE_SIZE1:.*]] = call i32 @getTempRet0()
37; CHECK-NEXT: br label %entry.split
38
39; CHECK: entry.split:
40; CHECK-NEXT: phi i32 [ 0, %entry ], [ %[[LONGJMP_RESULT:.*]], %if.end ]
41; CHECK-NEXT: %[[ARRAYDECAY1:.*]] = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %[[BUF]], i32 0, i32 0
42; CHECK-NEXT: %[[JMPBUF:.*]] = ptrtoint %struct.__jmp_buf_tag* %[[ARRAYDECAY1]] to [[PTR]]
43; CHECK-NEXT: store [[PTR]] 0, [[PTR]]* @__THREW__
44; CHECK-NEXT: call cc{{.*}} void @__invoke_void_[[PTR]]_i32(void ([[PTR]], i32)* @emscripten_longjmp, [[PTR]] %[[JMPBUF]], i32 1)
45; CHECK-NEXT: %[[__THREW__VAL:.*]] = load [[PTR]], [[PTR]]* @__THREW__
46; CHECK-NEXT: store [[PTR]] 0, [[PTR]]* @__THREW__
47; CHECK-NEXT: %[[CMP0:.*]] = icmp ne [[PTR]] %__THREW__.val, 0
48; CHECK-NEXT: %[[THREWVALUE_VAL:.*]] = load i32, i32* @__threwValue
49; CHECK-NEXT: %[[CMP1:.*]] = icmp ne i32 %[[THREWVALUE_VAL]], 0
50; CHECK-NEXT: %[[CMP:.*]] = and i1 %[[CMP0]], %[[CMP1]]
51; CHECK-NEXT: br i1 %[[CMP]], label %if.then1, label %if.else1
52
53; CHECK: entry.split.split:
54; CHECK-NEXT: unreachable
55
56; CHECK: if.then1:
57; CHECK-NEXT: %[[__THREW__VAL_P:.*]] = inttoptr [[PTR]] %[[__THREW__VAL]] to [[PTR]]*
58; CHECK-NEXT: %[[__THREW__VAL_P_LOADED:.*]] = load [[PTR]], [[PTR]]* %[[__THREW__VAL_P]]
59; CHECK-NEXT: %[[LABEL:.*]] = call i32 @testSetjmp([[PTR]] %[[__THREW__VAL_P_LOADED]], i32* %[[SETJMP_TABLE1]], i32 %[[SETJMP_TABLE_SIZE1]])
60; CHECK-NEXT: %[[CMP:.*]] = icmp eq i32 %[[LABEL]], 0
61; CHECK-NEXT: br i1 %[[CMP]], label %if.then2, label %if.end2
62
63; CHECK: if.else1:
64; CHECK-NEXT: br label %if.end
65
66; CHECK: if.end:
67; CHECK-NEXT: %[[LABEL_PHI:.*]] = phi i32 [ %[[LABEL:.*]], %if.end2 ], [ -1, %if.else1 ]
68; CHECK-NEXT: %[[LONGJMP_RESULT]] = call i32 @getTempRet0()
69; CHECK-NEXT: switch i32 %[[LABEL_PHI]], label %entry.split.split [
70; CHECK-NEXT:   i32 1, label %entry.split
71; CHECK-NEXT: ]
72
73; CHECK: if.then2:
74; CHECK-NEXT: call void @emscripten_longjmp([[PTR]] %[[__THREW__VAL]], i32 %[[THREWVALUE_VAL]])
75; CHECK-NEXT: unreachable
76
77; CHECK: if.end2:
78; CHECK-NEXT: call void  @setTempRet0(i32 %[[THREWVALUE_VAL]])
79; CHECK-NEXT: br label %if.end
80}
81
82; Test a case of a function call (which is not longjmp) after a setjmp
83define void @setjmp_other() {
84; CHECK-LABEL: @setjmp_other
85entry:
86  %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
87  %arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
88  %call = call i32 @setjmp(%struct.__jmp_buf_tag* %arraydecay) #0
89  call void @foo()
90  ret void
91; CHECK: entry:
92; CHECK: %[[SETJMP_TABLE:.*]] = call i32* @saveSetjmp(
93
94; CHECK: entry.split:
95; CHECK: @__invoke_void(void ()* @foo)
96
97; CHECK: entry.split.split:
98; CHECK-NEXT: %[[BUF:.*]] = bitcast i32* %[[SETJMP_TABLE]] to i8*
99; CHECK-NEXT: tail call void @free(i8* %[[BUF]])
100; CHECK-NEXT: ret void
101}
102
103; Test SSA validity
104define void @ssa(i32 %n) {
105; CHECK-LABEL: @ssa
106entry:
107  %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
108  %cmp = icmp sgt i32 %n, 5
109  br i1 %cmp, label %if.then, label %if.end
110; CHECK: entry:
111; CHECK: %[[SETJMP_TABLE0:.*]] = bitcast i8*
112; CHECK: %[[SETJMP_TABLE_SIZE0:.*]] = add i32 4, 0
113
114if.then:                                          ; preds = %entry
115  %0 = load i32, i32* @global_var, align 4
116  %arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
117  %call = call i32 @setjmp(%struct.__jmp_buf_tag* %arraydecay) #0
118  store i32 %0, i32* @global_var, align 4
119  br label %if.end
120; CHECK: if.then:
121; CHECK: %[[VAR0:.*]] = load i32, i32* @global_var, align 4
122; CHECK: %[[SETJMP_TABLE1:.*]] = call i32* @saveSetjmp(
123; CHECK-NEXT: %[[SETJMP_TABLE_SIZE1:.*]] = call i32 @getTempRet0()
124
125; CHECK: if.then.split:
126; CHECK: %[[VAR1:.*]] = phi i32 [ %[[VAR0]], %if.then ], [ %[[VAR2:.*]], %if.end3 ]
127; CHECK: %[[SETJMP_TABLE_SIZE2:.*]] = phi i32 [ %[[SETJMP_TABLE_SIZE1]], %if.then ], [ %[[SETJMP_TABLE_SIZE3:.*]], %if.end3 ]
128; CHECK: %[[SETJMP_TABLE2:.*]] = phi i32* [ %[[SETJMP_TABLE1]], %if.then ], [ %[[SETJMP_TABLE3:.*]], %if.end3 ]
129; CHECK: store i32 %[[VAR1]], i32* @global_var, align 4
130
131if.end:                                           ; preds = %if.then, %entry
132  %arraydecay1 = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
133  call void @longjmp(%struct.__jmp_buf_tag* %arraydecay1, i32 5) #1
134  unreachable
135; CHECK: if.end:
136; CHECK: %[[VAR2]] = phi i32 [ %[[VAR1]], %if.then.split ], [ undef, %entry ]
137; CHECK: %[[SETJMP_TABLE_SIZE3]] = phi i32 [ %[[SETJMP_TABLE_SIZE2]], %if.then.split ], [ %[[SETJMP_TABLE_SIZE0]], %entry ]
138; CHECK: %[[SETJMP_TABLE3]] = phi i32* [ %[[SETJMP_TABLE2]], %if.then.split ], [ %[[SETJMP_TABLE0]], %entry ]
139}
140
141; Test a case when a function only calls other functions that are neither setjmp nor longjmp
142define void @only_other_func() {
143entry:
144  call void @foo()
145  ret void
146; CHECK: call void @foo()
147}
148
149; Test a case when a function only calls longjmp and not setjmp
150define void @only_longjmp() {
151entry:
152  %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
153  %arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
154  call void @longjmp(%struct.__jmp_buf_tag* %arraydecay, i32 5) #1
155  unreachable
156; CHECK: %[[JMPBUF:.*]] = ptrtoint
157; CHECK-NEXT: call void @emscripten_longjmp([[PTR]] %[[JMPBUF]], i32 5)
158}
159
160; Test inline asm handling
161define void @inline_asm() {
162; CHECK-LABEL: @inline_asm
163entry:
164  %env = alloca [1 x %struct.__jmp_buf_tag], align 16
165  %arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %env, i32 0, i32 0
166  %call = call i32 @setjmp(%struct.__jmp_buf_tag* %arraydecay) #4
167; Inline assembly should not generate __invoke wrappers.
168; Doing so would fail as inline assembly cannot be passed as a function pointer.
169; CHECK: call void asm sideeffect "", ""()
170; CHECK-NOT: __invoke_void
171  call void asm sideeffect "", ""()
172  ret void
173}
174
175; Test that the allocsize attribute is being transformed properly
176declare i8 *@allocator(i32, %struct.__jmp_buf_tag*) #3
177define i8 *@allocsize() {
178; CHECK-LABEL: @allocsize
179entry:
180  %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
181  %arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
182  %call = call i32 @setjmp(%struct.__jmp_buf_tag* %arraydecay) #0
183; CHECK: call cc{{.*}} i8* @"__invoke_i8*_i32_%struct.__jmp_buf_tag*"([[ARGS:.*]]) #[[ALLOCSIZE_ATTR:[0-9]+]]
184  %alloc = call i8* @allocator(i32 20, %struct.__jmp_buf_tag* %arraydecay) #3
185  ret i8 *%alloc
186}
187
188; Tests if program does not crash when there's no setjmp function calls in the
189; module.
190@buffer = global [1 x %struct.__jmp_buf_tag] zeroinitializer, align 16
191define void @longjmp_only() {
192entry:
193  ; CHECK: call void @emscripten_longjmp
194  call void @longjmp(%struct.__jmp_buf_tag* getelementptr inbounds ([1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* @buffer, i32 0, i32 0), i32 1) #1
195  unreachable
196}
197
198; Tests if SSA rewrite works when a use and its def are within the same BB.
199define void @ssa_rewite_in_same_bb() {
200entry:
201  call void @foo()
202  br label %for.cond
203
204for.cond:                                         ; preds = %for.inc, %entry
205  ; CHECK: %{{.*}} = phi i32 [ %var[[VARNO:.*]], %for.inc.split ]
206  %0 = phi i32 [ %var, %for.inc ], [ undef, %entry ]
207  %var = add i32 0, 0
208  br label %for.inc
209
210for.inc:                                          ; preds = %for.cond
211  %call5 = call i32 @setjmp(%struct.__jmp_buf_tag* undef) #0
212  br label %for.cond
213
214; CHECK: for.inc.split:
215  ; CHECK: %var[[VARNO]] = phi i32 [ %var, %for.inc ]
216}
217
218; Tests cases where longjmp function pointer is used in other ways than direct
219; calls. longjmps should be replaced with
220; (int(*)(jmp_buf*, int))emscripten_longjmp.
221declare void @take_longjmp(void (%struct.__jmp_buf_tag*, i32)* %arg_ptr)
222define void @indirect_longjmp() {
223; CHECK-LABEL: @indirect_longjmp
224entry:
225  %local_longjmp_ptr = alloca void (%struct.__jmp_buf_tag*, i32)*, align 4
226  %buf0 = alloca [1 x %struct.__jmp_buf_tag], align 16
227  %buf1 = alloca [1 x %struct.__jmp_buf_tag], align 16
228
229  ; Store longjmp in a local variable, load it, and call it
230  store void (%struct.__jmp_buf_tag*, i32)* @longjmp, void (%struct.__jmp_buf_tag*, i32)** %local_longjmp_ptr, align 4
231  ; CHECK: store void (%struct.__jmp_buf_tag*, i32)* bitcast (void ([[PTR]], i32)* @emscripten_longjmp to void (%struct.__jmp_buf_tag*, i32)*), void (%struct.__jmp_buf_tag*, i32)** %local_longjmp_ptr, align 4
232  %longjmp_from_local_ptr = load void (%struct.__jmp_buf_tag*, i32)*, void (%struct.__jmp_buf_tag*, i32)** %local_longjmp_ptr, align 4
233  %arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf0, i32 0, i32 0
234  call void %longjmp_from_local_ptr(%struct.__jmp_buf_tag* %arraydecay, i32 0)
235
236  ; Load longjmp from a global variable and call it
237  %longjmp_from_global_ptr = load void (%struct.__jmp_buf_tag*, i32)*, void (%struct.__jmp_buf_tag*, i32)** @global_longjmp_ptr, align 4
238  %arraydecay1 = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf1, i32 0, i32 0
239  call void %longjmp_from_global_ptr(%struct.__jmp_buf_tag* %arraydecay1, i32 0)
240
241  ; Pass longjmp as a function argument. This is a call but longjmp is not a
242  ; callee but an argument.
243  call void @take_longjmp(void (%struct.__jmp_buf_tag*, i32)* @longjmp)
244  ; CHECK: call void @take_longjmp(void (%struct.__jmp_buf_tag*, i32)* bitcast (void ([[PTR]], i32)* @emscripten_longjmp to void (%struct.__jmp_buf_tag*, i32)*))
245  ret void
246}
247
248; Function Attrs: nounwind
249declare void @foo() #2
250; Function Attrs: returns_twice
251declare i32 @setjmp(%struct.__jmp_buf_tag*) #0
252; Function Attrs: noreturn
253declare void @longjmp(%struct.__jmp_buf_tag*, i32) #1
254declare i32 @__gxx_personality_v0(...)
255declare i8* @__cxa_begin_catch(i8*)
256declare void @__cxa_end_catch()
257declare i8* @malloc(i32)
258declare void @free(i8*)
259
260; JS glue functions and invoke wrappers declaration
261; CHECK-DAG: declare i32 @getTempRet0()
262; CHECK-DAG: declare void @setTempRet0(i32)
263; CHECK-DAG: declare i32* @saveSetjmp(%struct.__jmp_buf_tag*, i32, i32*, i32)
264; CHECK-DAG: declare i32 @testSetjmp([[PTR]], i32*, i32)
265; CHECK-DAG: declare void @emscripten_longjmp([[PTR]], i32)
266; CHECK-DAG: declare void @__invoke_void(void ()*)
267
268attributes #0 = { returns_twice }
269attributes #1 = { noreturn }
270attributes #2 = { nounwind }
271attributes #3 = { allocsize(0) }
272; CHECK-DAG: attributes #{{[0-9]+}} = { nounwind "wasm-import-module"="env" "wasm-import-name"="getTempRet0" }
273; CHECK-DAG: attributes #{{[0-9]+}} = { nounwind "wasm-import-module"="env" "wasm-import-name"="setTempRet0" }
274; CHECK-DAG: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="__resumeException" }
275; CHECK-DAG: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="llvm_eh_typeid_for" }
276; CHECK-DAG: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="__invoke_void" }
277; CHECK-DAG: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="__cxa_find_matching_catch_2" }
278; CHECK-DAG: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="saveSetjmp" }
279; CHECK-DAG: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="testSetjmp" }
280; CHECK-DAG: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="emscripten_longjmp" }
281; CHECK-DAG: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="__invoke_i8*_i32_%struct.__jmp_buf_tag*" }
282; CHECK-DAG: attributes #[[ALLOCSIZE_ATTR]] = { allocsize(1) }
283
284!llvm.dbg.cu = !{!2}
285!llvm.module.flags = !{!0}
286
287!0 = !{i32 2, !"Debug Info Version", i32 3}
288!1 = !DIFile(filename: "lower-em-sjlj.c", directory: "test")
289!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1)
290!3 = distinct !DISubprogram(name: "setjmp_debug_info", unit:!2, file: !1, line: 1)
291!4 = !DILocation(line:2, scope: !3)
292!5 = !DILocation(line:3, scope: !3)
293!6 = !DILocation(line:4, scope: !3)
294!7 = !DILocation(line:5, scope: !3)
295!8 = !DILocation(line:6, scope: !3)
296