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 %struct.__jmp_buf_tag = type { [6 x i32], i32, [32 x i32] }
8 @global_var = hidden global i32 0, align 4
9 ; CHECK-DAG: __THREW__ = external global i32
10 ; CHECK-DAG: __threwValue = external global i32
12 ; Test a simple setjmp - longjmp sequence
13 define hidden void @setjmp_longjmp() {
14 ; CHECK-LABEL: @setjmp_longjmp
16 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
17 %arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
18 %call = call i32 @setjmp(%struct.__jmp_buf_tag* %arraydecay) #0
19 %arraydecay1 = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
20 call void @longjmp(%struct.__jmp_buf_tag* %arraydecay1, i32 1) #1
23 ; CHECK-NEXT: %[[MALLOCCALL:.*]] = tail call i8* @malloc(i32 40)
24 ; CHECK-NEXT: %[[SETJMP_TABLE:.*]] = bitcast i8* %[[MALLOCCALL]] to i32*
25 ; CHECK-NEXT: store i32 0, i32* %[[SETJMP_TABLE]]
26 ; CHECK-NEXT: %[[SETJMP_TABLE_SIZE:.*]] = add i32 4, 0
27 ; CHECK-NEXT: %[[BUF:.*]] = alloca [1 x %struct.__jmp_buf_tag]
28 ; CHECK-NEXT: %[[ARRAYDECAY:.*]] = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %[[BUF]], i32 0, i32 0
29 ; CHECK-NEXT: %[[SETJMP_TABLE1:.*]] = call i32* @saveSetjmp(%struct.__jmp_buf_tag* %[[ARRAYDECAY]], i32 1, i32* %[[SETJMP_TABLE]], i32 %[[SETJMP_TABLE_SIZE]])
30 ; CHECK-NEXT: %[[SETJMP_TABLE_SIZE1:.*]] = call i32 @getTempRet0()
31 ; CHECK-NEXT: br label %entry.split
34 ; CHECK-NEXT: phi i32 [ 0, %entry ], [ %[[LONGJMP_RESULT:.*]], %if.end ]
35 ; CHECK-NEXT: %[[ARRAYDECAY1:.*]] = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %[[BUF]], i32 0, i32 0
36 ; CHECK-NEXT: store i32 0, i32* @__THREW__
37 ; CHECK-NEXT: call void @"__invoke_void_%struct.__jmp_buf_tag*_i32"(void (%struct.__jmp_buf_tag*, i32)* @emscripten_longjmp_jmpbuf, %struct.__jmp_buf_tag* %[[ARRAYDECAY1]], i32 1)
38 ; CHECK-NEXT: %[[__THREW__VAL:.*]] = load i32, i32* @__THREW__
39 ; CHECK-NEXT: store i32 0, i32* @__THREW__
40 ; CHECK-NEXT: %[[CMP0:.*]] = icmp ne i32 %__THREW__.val, 0
41 ; CHECK-NEXT: %[[THREWVALUE_VAL:.*]] = load i32, i32* @__threwValue
42 ; CHECK-NEXT: %[[CMP1:.*]] = icmp ne i32 %[[THREWVALUE_VAL]], 0
43 ; CHECK-NEXT: %[[CMP:.*]] = and i1 %[[CMP0]], %[[CMP1]]
44 ; CHECK-NEXT: br i1 %[[CMP]], label %if.then1, label %if.else1
46 ; CHECK: entry.split.split:
47 ; CHECK-NEXT: unreachable
50 ; CHECK-NEXT: %[[__THREW__VAL_I32P:.*]] = inttoptr i32 %[[__THREW__VAL]] to i32*
51 ; CHECK-NEXT: %[[__THREW__VAL_I32P_LOADED:.*]] = load i32, i32* %[[__THREW__VAL_I32P]]
52 ; CHECK-NEXT: %[[LABEL:.*]] = call i32 @testSetjmp(i32 %[[__THREW__VAL_I32P_LOADED]], i32* %[[SETJMP_TABLE1]], i32 %[[SETJMP_TABLE_SIZE1]])
53 ; CHECK-NEXT: %[[CMP:.*]] = icmp eq i32 %[[LABEL]], 0
54 ; CHECK-NEXT: br i1 %[[CMP]], label %if.then2, label %if.end2
57 ; CHECK-NEXT: br label %if.end
60 ; CHECK-NEXT: %[[LABEL_PHI:.*]] = phi i32 [ %[[LABEL:.*]], %if.end2 ], [ -1, %if.else1 ]
61 ; CHECK-NEXT: %[[LONGJMP_RESULT]] = call i32 @getTempRet0()
62 ; CHECK-NEXT: switch i32 %[[LABEL_PHI]], label %entry.split.split [
63 ; CHECK-NEXT: i32 1, label %entry.split
67 ; CHECK-NEXT: call void @emscripten_longjmp(i32 %[[__THREW__VAL]], i32 %[[THREWVALUE_VAL]])
68 ; CHECK-NEXT: unreachable
71 ; CHECK-NEXT: call void @setTempRet0(i32 %[[THREWVALUE_VAL]])
72 ; CHECK-NEXT: br label %if.end
75 ; Test a case of a function call (which is not longjmp) after a setjmp
76 define hidden void @setjmp_other() {
77 ; CHECK-LABEL: @setjmp_other
79 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
80 %arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
81 %call = call i32 @setjmp(%struct.__jmp_buf_tag* %arraydecay) #0
85 ; CHECK: %[[SETJMP_TABLE:.*]] = call i32* @saveSetjmp(
88 ; CHECK: call void @__invoke_void(void ()* @foo)
90 ; CHECK: entry.split.split:
91 ; CHECK-NEXT: %[[BUF:.*]] = bitcast i32* %[[SETJMP_TABLE]] to i8*
92 ; CHECK-NEXT: tail call void @free(i8* %[[BUF]])
93 ; CHECK-NEXT: ret void
96 ; Test a case when a function call is within try-catch, after a setjmp
97 define hidden void @exception_and_longjmp() #3 personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
98 ; CHECK-LABEL: @exception_and_longjmp
100 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
101 %arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
102 %call = call i32 @setjmp(%struct.__jmp_buf_tag* %arraydecay) #0
104 to label %try.cont unwind label %lpad
106 ; CHECK: entry.split:
107 ; CHECK: store i32 0, i32* @__THREW__
108 ; CHECK-NEXT: call void @__invoke_void(void ()* @foo)
109 ; CHECK-NEXT: %[[__THREW__VAL:.*]] = load i32, i32* @__THREW__
110 ; CHECK-NEXT: store i32 0, i32* @__THREW__
111 ; CHECK-NEXT: %[[CMP0:.*]] = icmp ne i32 %[[__THREW__VAL]], 0
112 ; CHECK-NEXT: %[[THREWVALUE_VAL:.*]] = load i32, i32* @__threwValue
113 ; CHECK-NEXT: %[[CMP1:.*]] = icmp ne i32 %[[THREWVALUE_VAL]], 0
114 ; CHECK-NEXT: %[[CMP:.*]] = and i1 %[[CMP0]], %[[CMP1]]
115 ; CHECK-NEXT: br i1 %[[CMP]], label %if.then1, label %if.else1
117 ; CHECK: entry.split.split:
118 ; CHECK-NEXT: %[[CMP:.*]] = icmp eq i32 %[[__THREW__VAL]], 1
119 ; CHECK-NEXT: br i1 %[[CMP]], label %lpad, label %try.cont
121 lpad: ; preds = %entry
122 %0 = landingpad { i8*, i32 }
124 %1 = extractvalue { i8*, i32 } %0, 0
125 %2 = extractvalue { i8*, i32 } %0, 1
126 %3 = call i8* @__cxa_begin_catch(i8* %1) #2
127 call void @__cxa_end_catch()
130 try.cont: ; preds = %entry, %lpad
135 define hidden void @ssa(i32 %n) {
138 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
139 %cmp = icmp sgt i32 %n, 5
140 br i1 %cmp, label %if.then, label %if.end
142 ; CHECK: %[[SETJMP_TABLE0:.*]] = bitcast i8*
143 ; CHECK: %[[SETJMP_TABLE_SIZE0:.*]] = add i32 4, 0
145 if.then: ; preds = %entry
146 %0 = load i32, i32* @global_var, align 4
147 %arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
148 %call = call i32 @setjmp(%struct.__jmp_buf_tag* %arraydecay) #0
149 store i32 %0, i32* @global_var, align 4
152 ; CHECK: %[[VAR0:.*]] = load i32, i32* @global_var, align 4
153 ; CHECK: %[[SETJMP_TABLE1:.*]] = call i32* @saveSetjmp(
154 ; CHECK-NEXT: %[[SETJMP_TABLE_SIZE1:.*]] = call i32 @getTempRet0()
156 ; CHECK: if.then.split:
157 ; CHECK: %[[VAR1:.*]] = phi i32 [ %[[VAR0]], %if.then ], [ %[[VAR2:.*]], %if.end3 ]
158 ; CHECK: %[[SETJMP_TABLE_SIZE2:.*]] = phi i32 [ %[[SETJMP_TABLE_SIZE1]], %if.then ], [ %[[SETJMP_TABLE_SIZE3:.*]], %if.end3 ]
159 ; CHECK: %[[SETJMP_TABLE2:.*]] = phi i32* [ %[[SETJMP_TABLE1]], %if.then ], [ %[[SETJMP_TABLE3:.*]], %if.end3 ]
160 ; CHECK: store i32 %[[VAR1]], i32* @global_var, align 4
162 if.end: ; preds = %if.then, %entry
163 %arraydecay1 = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
164 call void @longjmp(%struct.__jmp_buf_tag* %arraydecay1, i32 5) #1
167 ; CHECK: %[[VAR2]] = phi i32 [ %[[VAR1]], %if.then.split ], [ undef, %entry ]
168 ; CHECK: %[[SETJMP_TABLE_SIZE3]] = phi i32 [ %[[SETJMP_TABLE_SIZE2]], %if.then.split ], [ %[[SETJMP_TABLE_SIZE0]], %entry ]
169 ; CHECK: %[[SETJMP_TABLE3]] = phi i32* [ %[[SETJMP_TABLE2]], %if.then.split ], [ %[[SETJMP_TABLE0]], %entry ]
172 ; Test a case when a function only calls other functions that are neither setjmp nor longjmp
173 define hidden void @only_other_func() {
177 ; CHECK: call void @foo()
180 ; Test a case when a function only calls longjmp and not setjmp
181 define hidden void @only_longjmp() {
183 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
184 %arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
185 call void @longjmp(%struct.__jmp_buf_tag* %arraydecay, i32 5) #1
187 ; CHECK: %[[ARRAYDECAY:.*]] = getelementptr inbounds
188 ; CHECK-NEXT: call void @emscripten_longjmp_jmpbuf(%struct.__jmp_buf_tag* %[[ARRAYDECAY]], i32 5) #1
191 ; Test inline asm handling
192 define hidden void @inline_asm() {
193 ; CHECK-LABEL: @inline_asm
195 %env = alloca [1 x %struct.__jmp_buf_tag], align 16
196 %arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %env, i32 0, i32 0
197 %call = call i32 @setjmp(%struct.__jmp_buf_tag* %arraydecay) #4
198 ; Inline assembly should not generate __invoke wrappers.
199 ; Doing so would fail as inline assembly cannot be passed as a function pointer.
200 ; CHECK: call void asm sideeffect "", ""()
201 ; CHECK-NOT: __invoke_void
202 call void asm sideeffect "", ""()
207 ; Function Attrs: returns_twice
208 declare i32 @setjmp(%struct.__jmp_buf_tag*) #0
209 ; Function Attrs: noreturn
210 declare void @longjmp(%struct.__jmp_buf_tag*, i32) #1
211 declare i32 @__gxx_personality_v0(...)
212 declare i8* @__cxa_begin_catch(i8*)
213 declare void @__cxa_end_catch()
214 declare i8* @malloc(i32)
215 declare void @free(i8*)
217 ; JS glue functions and invoke wrappers declaration
218 ; CHECK-DAG: declare i32 @getTempRet0()
219 ; CHECK-DAG: declare void @setTempRet0(i32)
220 ; CHECK-DAG: declare i32* @saveSetjmp(%struct.__jmp_buf_tag*, i32, i32*, i32)
221 ; CHECK-DAG: declare i32 @testSetjmp(i32, i32*, i32)
222 ; CHECK-DAG: declare void @emscripten_longjmp_jmpbuf(%struct.__jmp_buf_tag*, i32)
223 ; CHECK-DAG: declare void @emscripten_longjmp(i32, i32)
224 ; CHECK-DAG: declare void @__invoke_void(void ()*)
225 ; CHECK-DAG: declare void @"__invoke_void_%struct.__jmp_buf_tag*_i32"(void (%struct.__jmp_buf_tag*, i32)*, %struct.__jmp_buf_tag*, i32)
227 attributes #0 = { returns_twice }
228 attributes #1 = { noreturn }
229 attributes #2 = { nounwind }