1 ; RUN: opt < %s -wasm-lower-em-ehsjlj -enable-emscripten-sjlj -S | FileCheck %s -DPTR=i32
2 ; RUN: opt < %s -wasm-lower-em-ehsjlj -enable-emscripten-sjlj -S --mattr=+atomics,+bulk-memory | FileCheck %s -DPTR=i32
3 ; RUN: opt < %s -wasm-lower-em-ehsjlj -enable-emscripten-sjlj --mtriple=wasm64-unknown-unknown -data-layout="e-m:e-p:64:64-i64:64-n32:64-S128" -S | FileCheck %s -DPTR=i64
5 target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
6 target triple = "wasm32-unknown-unknown"
8 %struct.__jmp_buf_tag = type { [6 x i32], i32, [32 x i32] }
10 @global_var = global i32 0, align 4
11 @global_longjmp_ptr = global ptr @longjmp, align 4
12 ; CHECK-DAG: @__THREW__ = external thread_local global [[PTR]]
13 ; CHECK-DAG: @__threwValue = external thread_local global i32
14 ; CHECK-DAG: @global_longjmp_ptr = global ptr @emscripten_longjmp
16 ; Test a simple setjmp - longjmp sequence
17 define void @setjmp_longjmp() {
18 ; CHECK-LABEL: @setjmp_longjmp
20 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
21 %call = call i32 @setjmp(ptr %buf) #0
22 call void @longjmp(ptr %buf, i32 1) #1
25 ; CHECK-NEXT: %[[MALLOCCALL:.*]] = tail call ptr @malloc([[PTR]] 40)
26 ; CHECK-NEXT: store i32 0, ptr %[[MALLOCCALL]]
27 ; CHECK-NEXT: %[[SETJMP_TABLE_SIZE:.*]] = add i32 4, 0
28 ; CHECK-NEXT: br label %entry.split
31 ; CHECK-NEXT: %[[BUF:.*]] = alloca [1 x %struct.__jmp_buf_tag]
32 ; CHECK-NEXT: %[[SETJMP_TABLE1:.*]] = call ptr @saveSetjmp(ptr %[[BUF]], i32 1, ptr %[[MALLOCCALL]], i32 %[[SETJMP_TABLE_SIZE]])
33 ; CHECK-NEXT: %[[SETJMP_TABLE_SIZE1:.*]] = call i32 @getTempRet0()
34 ; CHECK-NEXT: br label %entry.split.split
36 ; CHECK: entry.split.split:
37 ; CHECK-NEXT: phi i32 [ 0, %entry.split ], [ %[[LONGJMP_RESULT:.*]], %if.end ]
38 ; CHECK-NEXT: %[[JMPBUF:.*]] = ptrtoint ptr %[[BUF]] to [[PTR]]
39 ; CHECK-NEXT: store [[PTR]] 0, ptr @__THREW__
40 ; CHECK-NEXT: call cc{{.*}} void @__invoke_void_[[PTR]]_i32(ptr @emscripten_longjmp, [[PTR]] %[[JMPBUF]], i32 1)
41 ; CHECK-NEXT: %[[__THREW__VAL:.*]] = load [[PTR]], ptr @__THREW__
42 ; CHECK-NEXT: store [[PTR]] 0, ptr @__THREW__
43 ; CHECK-NEXT: %[[CMP0:.*]] = icmp ne [[PTR]] %__THREW__.val, 0
44 ; CHECK-NEXT: %[[THREWVALUE_VAL:.*]] = load i32, ptr @__threwValue
45 ; CHECK-NEXT: %[[CMP1:.*]] = icmp ne i32 %[[THREWVALUE_VAL]], 0
46 ; CHECK-NEXT: %[[CMP:.*]] = and i1 %[[CMP0]], %[[CMP1]]
47 ; CHECK-NEXT: br i1 %[[CMP]], label %if.then1, label %if.else1
49 ; CHECK: entry.split.split.split:
50 ; CHECK-NEXT: unreachable
53 ; CHECK-NEXT: %[[__THREW__VAL_P:.*]] = inttoptr [[PTR]] %[[__THREW__VAL]] to ptr
54 ; CHECK-NEXT: %[[__THREW__VAL_P_LOADED:.*]] = load [[PTR]], ptr %[[__THREW__VAL_P]]
55 ; CHECK-NEXT: %[[LABEL:.*]] = call i32 @testSetjmp([[PTR]] %[[__THREW__VAL_P_LOADED]], ptr %[[SETJMP_TABLE1]], i32 %[[SETJMP_TABLE_SIZE1]])
56 ; CHECK-NEXT: %[[CMP:.*]] = icmp eq i32 %[[LABEL]], 0
57 ; CHECK-NEXT: br i1 %[[CMP]], label %call.em.longjmp, label %if.end2
60 ; CHECK-NEXT: br label %if.end
63 ; CHECK-NEXT: %[[LABEL_PHI:.*]] = phi i32 [ %[[LABEL:.*]], %if.end2 ], [ -1, %if.else1 ]
64 ; CHECK-NEXT: %[[LONGJMP_RESULT]] = call i32 @getTempRet0()
65 ; CHECK-NEXT: switch i32 %[[LABEL_PHI]], label %entry.split.split.split [
66 ; CHECK-NEXT: i32 1, label %entry.split.split
69 ; CHECK: call.em.longjmp:
70 ; CHECK-NEXT: %threw.phi = phi [[PTR]] [ %[[__THREW__VAL]], %if.then1 ]
71 ; CHECK-NEXT: %threwvalue.phi = phi i32 [ %[[THREWVALUE_VAL]], %if.then1 ]
72 ; CHECK-NEXT: tail call void @free(ptr %[[SETJMP_TABLE1]])
73 ; CHECK-NEXT: call void @emscripten_longjmp([[PTR]] %threw.phi, i32 %threwvalue.phi)
74 ; CHECK-NEXT: unreachable
77 ; CHECK-NEXT: call void @setTempRet0(i32 %[[THREWVALUE_VAL]])
78 ; CHECK-NEXT: br label %if.end
81 ; Test a case of a function call (which is not longjmp) after a setjmp
82 define void @setjmp_longjmpable_call() {
83 ; CHECK-LABEL: @setjmp_longjmpable_call
85 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
86 %call = call i32 @setjmp(ptr %buf) #0
90 ; CHECK: %[[SETJMP_TABLE:.*]] = call ptr @saveSetjmp(
92 ; CHECK: entry.split.split:
93 ; CHECK: @__invoke_void(ptr @foo)
95 ; CHECK: entry.split.split.split:
96 ; CHECK-NEXT: tail call void @free(ptr %[[SETJMP_TABLE]])
97 ; CHECK-NEXT: ret void
100 ; When there are multiple longjmpable calls after setjmp. In this test we
101 ; specifically check if 'call.em.longjmp' BB, which rethrows longjmps by calling
102 ; emscripten_longjmp for ones that are not for this function's setjmp, is
103 ; correctly created for multiple predecessors.
104 define void @setjmp_multiple_longjmpable_calls() {
105 ; CHECK-LABEL: @setjmp_multiple_longjmpable_calls
107 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
108 %call = call i32 @setjmp(ptr %buf) #0
112 ; CHECK: call.em.longjmp:
113 ; CHECK-NEXT: %threw.phi = phi [[PTR]] [ %__THREW__.val, %if.then1 ], [ %__THREW__.val4, %if.then15 ]
114 ; CHECK-NEXT: %threwvalue.phi = phi i32 [ %__threwValue.val, %if.then1 ], [ %__threwValue.val8, %if.then15 ]
115 ; CHECK-NEXT: tail call void @free(ptr %[[SETJMP_TABLE1]])
116 ; CHECK-NEXT: call void @emscripten_longjmp([[PTR]] %threw.phi, i32 %threwvalue.phi)
117 ; CHECK-NEXT: unreachable
120 ; Test a case where a function has a setjmp call but no other calls that can
121 ; longjmp. We don't need to do any transformation in this case.
122 define i32 @setjmp_only(ptr %ptr) {
123 ; CHECK-LABEL: @setjmp_only
125 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
126 %call = call i32 @setjmp(ptr %buf) #0
127 ; free cannot longjmp
128 call void @free(ptr %ptr)
132 ; CHECK-NOT: %setjmpTable
133 ; CHECK-NOT: @saveSetjmp
134 ; CHECK-NOT: @testSetjmp
135 ; The remaining setjmp call is converted to constant 0, because setjmp returns 0
136 ; when called directly.
141 define void @ssa(i32 %n) {
144 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
145 %cmp = icmp sgt i32 %n, 5
146 br i1 %cmp, label %if.then, label %if.end
148 ; CHECK: %[[SETJMP_TABLE_SIZE0:.*]] = add i32 4, 0
150 if.then: ; preds = %entry
151 %0 = load i32, ptr @global_var, align 4
152 %call = call i32 @setjmp(ptr %buf) #0
153 store i32 %0, ptr @global_var, align 4
156 ; CHECK: %[[VAR0:.*]] = load i32, ptr @global_var, align 4
157 ; CHECK: %[[SETJMP_TABLE1:.*]] = call ptr @saveSetjmp(
158 ; CHECK-NEXT: %[[SETJMP_TABLE_SIZE1:.*]] = call i32 @getTempRet0()
160 ; CHECK: if.then.split:
161 ; CHECK: %[[VAR1:.*]] = phi i32 [ %[[VAR2:.*]], %if.end3 ], [ %[[VAR0]], %if.then ]
162 ; CHECK: %[[SETJMP_TABLE_SIZE2:.*]] = phi i32 [ %[[SETJMP_TABLE_SIZE1]], %if.then ], [ %[[SETJMP_TABLE_SIZE3:.*]], %if.end3 ]
163 ; CHECK: %[[SETJMP_TABLE2:.*]] = phi ptr [ %[[SETJMP_TABLE1]], %if.then ], [ %[[SETJMP_TABLE3:.*]], %if.end3 ]
164 ; CHECK: store i32 %[[VAR1]], ptr @global_var, align 4
166 if.end: ; preds = %if.then, %entry
167 call void @longjmp(ptr %buf, i32 5) #1
170 ; CHECK: %[[VAR2]] = phi i32 [ %[[VAR1]], %if.then.split ], [ undef, %entry.split ]
171 ; CHECK: %[[SETJMP_TABLE_SIZE3]] = phi i32 [ %[[SETJMP_TABLE_SIZE2]], %if.then.split ], [ %[[SETJMP_TABLE_SIZE0]], %entry.split ]
172 ; CHECK: %[[SETJMP_TABLE3]] = phi ptr [ %[[SETJMP_TABLE2]], %if.then.split ], [ %setjmpTable, %entry.split ]
175 ; Test a case when a function only calls other functions that are neither setjmp nor longjmp
176 define void @other_func_only() {
177 ; CHECK-LABEL: @other_func_only
181 ; CHECK: call void @foo()
184 ; Test inline asm handling
185 define void @inline_asm() {
186 ; CHECK-LABEL: @inline_asm
188 %env = alloca [1 x %struct.__jmp_buf_tag], align 16
189 %call = call i32 @setjmp(ptr %env) #4
190 ; Inline assembly should not generate __invoke wrappers.
191 ; Doing so would fail as inline assembly cannot be passed as a function pointer.
192 ; CHECK: call void asm sideeffect "", ""()
193 ; CHECK-NOT: __invoke_void
194 call void asm sideeffect "", ""()
198 ; Test that the allocsize attribute is being transformed properly
199 declare ptr @allocator(i32, ptr) #3
200 define ptr @allocsize() {
201 ; CHECK-LABEL: @allocsize
203 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
204 %call = call i32 @setjmp(ptr %buf) #0
205 ; CHECK: call cc{{.*}} ptr @__invoke_ptr_i32_ptr([[ARGS:.*]]) #[[ALLOCSIZE_ATTR:[0-9]+]]
206 %alloc = call ptr @allocator(i32 20, ptr %buf) #3
210 ; Test a case when a function only calls longjmp and not setjmp
211 @buffer = global [1 x %struct.__jmp_buf_tag] zeroinitializer, align 16
212 define void @longjmp_only() {
213 ; CHECK-LABEL: @longjmp_only
215 ; CHECK: call void @emscripten_longjmp
216 call void @longjmp(ptr @buffer, i32 1) #1
220 ; Tests if SSA rewrite works when a use and its def are within the same BB.
221 define void @ssa_rewite_in_same_bb() {
222 ; CHECK-LABEL: @ssa_rewite_in_same_bb
227 for.cond: ; preds = %for.inc, %entry
228 ; CHECK: %{{.*}} = phi i32 [ %var[[VARNO:.*]], %for.inc.split ]
229 %0 = phi i32 [ %var, %for.inc ], [ undef, %entry ]
233 for.inc: ; preds = %for.cond
234 %call5 = call i32 @setjmp(ptr undef) #0
237 ; CHECK: for.inc.split:
238 ; CHECK: %var[[VARNO]] = phi i32 [ undef, %if.end ], [ %var, %for.inc ]
241 ; Tests cases where longjmp function pointer is used in other ways than direct
242 ; calls. longjmps should be replaced with
243 ; (void(*)(jmp_buf*, int))emscripten_longjmp.
244 declare void @take_longjmp(ptr %arg_ptr)
245 define void @indirect_longjmp() {
246 ; CHECK-LABEL: @indirect_longjmp
248 %local_longjmp_ptr = alloca ptr, align 4
249 %buf0 = alloca [1 x %struct.__jmp_buf_tag], align 16
250 %buf1 = alloca [1 x %struct.__jmp_buf_tag], align 16
252 ; Store longjmp in a local variable, load it, and call it
253 store ptr @longjmp, ptr %local_longjmp_ptr, align 4
254 ; CHECK: store ptr @emscripten_longjmp, ptr %local_longjmp_ptr, align 4
255 %longjmp_from_local_ptr = load ptr, ptr %local_longjmp_ptr, align 4
256 call void %longjmp_from_local_ptr(ptr %buf0, i32 0)
258 ; Load longjmp from a global variable and call it
259 %longjmp_from_global_ptr = load ptr, ptr @global_longjmp_ptr, align 4
260 call void %longjmp_from_global_ptr(ptr %buf1, i32 0)
262 ; Pass longjmp as a function argument. This is a call but longjmp is not a
263 ; callee but an argument.
264 call void @take_longjmp(ptr @longjmp)
265 ; CHECK: call void @take_longjmp(ptr @emscripten_longjmp)
269 ; Test if _setjmp and _longjmp calls are treated in the same way as setjmp and
271 define void @_setjmp__longjmp() {
272 ; CHECK-LABEL: @_setjmp__longjmp
273 ; These calls should have been transformed away
274 ; CHECK-NOT: call i32 @_setjmp
275 ; CHECK-NOT: call void @_longjmp
277 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
278 %call = call i32 @_setjmp(ptr %buf) #0
279 call void @_longjmp(ptr %buf, i32 1) #1
283 ; Function Attrs: nounwind
284 declare void @foo() #2
285 ; Function Attrs: returns_twice
286 declare i32 @setjmp(ptr) #0
287 declare i32 @_setjmp(ptr) #0
288 ; Function Attrs: noreturn
289 declare void @longjmp(ptr, i32) #1
290 declare void @_longjmp(ptr, i32) #1
291 declare i32 @__gxx_personality_v0(...)
292 declare ptr @__cxa_begin_catch(ptr)
293 declare void @__cxa_end_catch()
294 declare void @free(ptr)
296 ; JS glue functions and invoke wrappers declaration
297 ; CHECK-DAG: declare i32 @getTempRet0()
298 ; CHECK-DAG: declare void @setTempRet0(i32)
299 ; CHECK-DAG: declare ptr @saveSetjmp(ptr, i32, ptr, i32)
300 ; CHECK-DAG: declare i32 @testSetjmp([[PTR]], ptr, i32)
301 ; CHECK-DAG: declare void @emscripten_longjmp([[PTR]], i32)
302 ; CHECK-DAG: declare void @__invoke_void(ptr)
304 attributes #0 = { returns_twice }
305 attributes #1 = { noreturn }
306 attributes #2 = { nounwind }
307 attributes #3 = { allocsize(0) }
308 ; CHECK-DAG: attributes #{{[0-9]+}} = { nounwind "wasm-import-module"="env" "wasm-import-name"="getTempRet0" }
309 ; CHECK-DAG: attributes #{{[0-9]+}} = { nounwind "wasm-import-module"="env" "wasm-import-name"="setTempRet0" }
310 ; CHECK-DAG: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="__invoke_void" }
311 ; CHECK-DAG: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="saveSetjmp" }
312 ; CHECK-DAG: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="testSetjmp" }
313 ; CHECK-DAG: attributes #{{[0-9]+}} = { noreturn "wasm-import-module"="env" "wasm-import-name"="emscripten_longjmp" }
314 ; CHECK-DAG: attributes #{{[0-9]+}} = { "wasm-import-module"="env" "wasm-import-name"="__invoke_ptr_i32_ptr" }
315 ; CHECK-DAG: attributes #[[ALLOCSIZE_ATTR]] = { allocsize(1) }
318 !llvm.module.flags = !{!0}
320 !0 = !{i32 2, !"Debug Info Version", i32 3}
321 !1 = !DIFile(filename: "lower-em-sjlj.c", directory: "test")
322 !2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1)
323 !3 = distinct !DISubprogram(name: "setjmp_debug_info", unit:!2, file: !1, line: 1)
324 !4 = !DILocation(line:2, scope: !3)
325 !5 = !DILocation(line:3, scope: !3)
326 !6 = !DILocation(line:4, scope: !3)
327 !7 = !DILocation(line:5, scope: !3)
328 !8 = !DILocation(line:6, scope: !3)