• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1; RUN: opt < %s -wasm-lower-em-ehsjlj -S | FileCheck %s
2
3target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
4target triple = "wasm32-unknown-unknown"
5
6%struct.__jmp_buf_tag = type { [6 x i32], i32, [32 x i32] }
7
8@global_var = hidden global i32 0, align 4
9; CHECK-DAG: __THREW__ = weak_odr global i32 0
10; CHECK-DAG: __threwValue = weak_odr global i32 0
11; CHECK-DAG: __tempRet0 = weak_odr global i32 0
12
13; Test a simple setjmp - longjmp sequence
14define hidden void @setjmp_longjmp() {
15; CHECK-LABEL: @setjmp_longjmp
16entry:
17  %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
18  %arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
19  %call = call i32 @setjmp(%struct.__jmp_buf_tag* %arraydecay) #0
20  %arraydecay1 = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
21  call void @longjmp(%struct.__jmp_buf_tag* %arraydecay1, i32 1) #1
22  unreachable
23; CHECK: entry:
24; CHECK-NEXT: %[[MALLOCCALL:.*]] = tail call i8* @malloc(i32 40)
25; CHECK-NEXT: %[[SETJMP_TABLE:.*]] = bitcast i8* %[[MALLOCCALL]] to i32*
26; CHECK-NEXT: store i32 0, i32* %[[SETJMP_TABLE]]
27; CHECK-NEXT: %[[SETJMP_TABLE_SIZE:.*]] = add i32 4, 0
28; CHECK-NEXT: %[[BUF:.*]] = alloca [1 x %struct.__jmp_buf_tag]
29; CHECK-NEXT: %[[ARRAYDECAY:.*]] = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %[[BUF]], i32 0, i32 0
30; CHECK-NEXT: %[[SETJMP_TABLE1:.*]] = call i32* @saveSetjmp(%struct.__jmp_buf_tag* %[[ARRAYDECAY]], i32 1, i32* %[[SETJMP_TABLE]], i32 %[[SETJMP_TABLE_SIZE]])
31; CHECK-NEXT: %[[SETJMP_TABLE_SIZE1:.*]] = load i32, i32* @__tempRet0
32; CHECK-NEXT: br label %entry.split
33
34; CHECK: entry.split:
35; CHECK-NEXT: phi i32 [ 0, %entry ], [ %[[LONGJMP_RESULT:.*]], %if.end ]
36; CHECK-NEXT: %[[ARRAYDECAY1:.*]] = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %[[BUF]], i32 0, i32 0
37; CHECK-NEXT: store i32 0, i32* @__THREW__
38; 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)
39; CHECK-NEXT: %[[__THREW__VAL:.*]] = load i32, i32* @__THREW__
40; CHECK-NEXT: store i32 0, i32* @__THREW__
41; CHECK-NEXT: %[[CMP0:.*]] = icmp ne i32 %__THREW__.val, 0
42; CHECK-NEXT: %[[THREWVALUE_VAL:.*]] = load i32, i32* @__threwValue
43; CHECK-NEXT: %[[CMP1:.*]] = icmp ne i32 %[[THREWVALUE_VAL]], 0
44; CHECK-NEXT: %[[CMP:.*]] = and i1 %[[CMP0]], %[[CMP1]]
45; CHECK-NEXT: br i1 %[[CMP]], label %if.then1, label %if.else1
46
47; CHECK: entry.split.split:
48; CHECK-NEXT: unreachable
49
50; CHECK: if.then1:
51; CHECK-NEXT: %[[__THREW__VAL_I32P:.*]] = inttoptr i32 %[[__THREW__VAL]] to i32*
52; CHECK-NEXT: %[[__THREW__VAL_I32P_LOADED:.*]] = load i32, i32* %[[__THREW__VAL_I32P]]
53; CHECK-NEXT: %[[LABEL:.*]] = call i32 @testSetjmp(i32 %[[__THREW__VAL_I32P_LOADED]], i32* %[[SETJMP_TABLE1]], i32 %[[SETJMP_TABLE_SIZE1]])
54; CHECK-NEXT: %[[CMP:.*]] = icmp eq i32 %[[LABEL]], 0
55; CHECK-NEXT: br i1 %[[CMP]], label %if.then2, label %if.end2
56
57; CHECK: if.else1:
58; CHECK-NEXT: br label %if.end
59
60; CHECK: if.end:
61; CHECK-NEXT: %[[LABEL_PHI:.*]] = phi i32 [ %[[LABEL:.*]], %if.end2 ], [ -1, %if.else1 ]
62; CHECK-NEXT: %[[LONGJMP_RESULT]] = load i32, i32* @__tempRet0
63; CHECK-NEXT: switch i32 %[[LABEL_PHI]], label %entry.split.split [
64; CHECK-NEXT:   i32 1, label %entry.split
65; CHECK-NEXT: ]
66
67; CHECK: if.then2:
68; CHECK-NEXT: call void @emscripten_longjmp(i32 %[[__THREW__VAL]], i32 %[[THREWVALUE_VAL]])
69; CHECK-NEXT: unreachable
70
71; CHECK: if.end2:
72; CHECK-NEXT: store i32 %[[THREWVALUE_VAL]], i32* @__tempRet0
73; CHECK-NEXT: br label %if.end
74}
75
76; Test a case of a function call (which is not longjmp) after a setjmp
77define hidden void @setjmp_other() {
78; CHECK-LABEL: @setjmp_other
79entry:
80  %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
81  %arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
82  %call = call i32 @setjmp(%struct.__jmp_buf_tag* %arraydecay) #0
83  call void @foo()
84  ret void
85; CHECK: entry:
86; CHECK: %[[SETJMP_TABLE:.*]] = call i32* @saveSetjmp(
87
88; CHECK: entry.split:
89; CHECK: call void @__invoke_void(void ()* @foo)
90
91; CHECK: entry.split.split:
92; CHECK-NEXT: %[[BUF:.*]] = bitcast i32* %[[SETJMP_TABLE]] to i8*
93; CHECK-NEXT: tail call void @free(i8* %[[BUF]])
94; CHECK-NEXT: ret void
95}
96
97; Test a case when a function call is within try-catch, after a setjmp
98define hidden void @exception_and_longjmp() #3 personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
99; CHECK-LABEL: @exception_and_longjmp
100entry:
101  %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
102  %arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
103  %call = call i32 @setjmp(%struct.__jmp_buf_tag* %arraydecay) #0
104  invoke void @foo()
105          to label %try.cont unwind label %lpad
106
107; CHECK: entry.split:
108; CHECK: store i32 0, i32* @__THREW__
109; CHECK-NEXT: call void @__invoke_void(void ()* @foo)
110; CHECK-NEXT: %[[__THREW__VAL:.*]] = load i32, i32* @__THREW__
111; CHECK-NEXT: store i32 0, i32* @__THREW__
112; CHECK-NEXT: %[[CMP0:.*]] = icmp ne i32 %[[__THREW__VAL]], 0
113; CHECK-NEXT: %[[THREWVALUE_VAL:.*]] = load i32, i32* @__threwValue
114; CHECK-NEXT: %[[CMP1:.*]] = icmp ne i32 %[[THREWVALUE_VAL]], 0
115; CHECK-NEXT: %[[CMP:.*]] = and i1 %[[CMP0]], %[[CMP1]]
116; CHECK-NEXT: br i1 %[[CMP]], label %if.then1, label %if.else1
117
118; CHECK: entry.split.split:
119; CHECK-NEXT: %[[CMP:.*]] = icmp eq i32 %[[__THREW__VAL]], 1
120; CHECK-NEXT: br i1 %[[CMP]], label %lpad, label %try.cont
121
122lpad:                                             ; preds = %entry
123  %0 = landingpad { i8*, i32 }
124          catch i8* null
125  %1 = extractvalue { i8*, i32 } %0, 0
126  %2 = extractvalue { i8*, i32 } %0, 1
127  %3 = call i8* @__cxa_begin_catch(i8* %1) #2
128  call void @__cxa_end_catch()
129  br label %try.cont
130
131try.cont:                                         ; preds = %entry, %lpad
132  ret void
133}
134
135; Test SSA validity
136define hidden void @ssa(i32 %n) {
137; CHECK-LABEL: @ssa
138entry:
139  %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
140  %cmp = icmp sgt i32 %n, 5
141  br i1 %cmp, label %if.then, label %if.end
142; CHECK: entry:
143; CHECK: %[[SETJMP_TABLE0:.*]] = bitcast i8*
144; CHECK: %[[SETJMP_TABLE_SIZE0:.*]] = add i32 4, 0
145
146if.then:                                          ; preds = %entry
147  %0 = load i32, i32* @global_var, align 4
148  %arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
149  %call = call i32 @setjmp(%struct.__jmp_buf_tag* %arraydecay) #0
150  store i32 %0, i32* @global_var, align 4
151  br label %if.end
152; CHECK: if.then:
153; CHECK: %[[VAR0:.*]] = load i32, i32* @global_var, align 4
154; CHECK: %[[SETJMP_TABLE1:.*]] = call i32* @saveSetjmp(
155; CHECK-NEXT: %[[SETJMP_TABLE_SIZE1:.*]] = load i32, i32* @__tempRet0
156
157; CHECK: if.then.split:
158; CHECK: %[[VAR1:.*]] = phi i32 [ %[[VAR0]], %if.then ], [ %[[VAR2:.*]], %if.end3 ]
159; CHECK: %[[SETJMP_TABLE_SIZE2:.*]] = phi i32 [ %[[SETJMP_TABLE_SIZE1]], %if.then ], [ %[[SETJMP_TABLE_SIZE3:.*]], %if.end3 ]
160; CHECK: %[[SETJMP_TABLE2:.*]] = phi i32* [ %[[SETJMP_TABLE1]], %if.then ], [ %[[SETJMP_TABLE3:.*]], %if.end3 ]
161; CHECK: store i32 %[[VAR1]], i32* @global_var, align 4
162
163if.end:                                           ; preds = %if.then, %entry
164  %arraydecay1 = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
165  call void @longjmp(%struct.__jmp_buf_tag* %arraydecay1, i32 5) #1
166  unreachable
167; CHECK: if.end:
168; CHECK: %[[VAR2]] = phi i32 [ %[[VAR1]], %if.then.split ], [ undef, %entry ]
169; CHECK: %[[SETJMP_TABLE_SIZE3]] = phi i32 [ %[[SETJMP_TABLE_SIZE2]], %if.then.split ], [ %[[SETJMP_TABLE_SIZE0]], %entry ]
170; CHECK: %[[SETJMP_TABLE3]] = phi i32* [ %[[SETJMP_TABLE2]], %if.then.split ], [ %[[SETJMP_TABLE0]], %entry ]
171}
172
173; Test a case when a function only calls other functions that are neither setjmp nor longjmp
174define hidden void @only_other_func() {
175entry:
176  call void @foo()
177  ret void
178; CHECK: call void @foo()
179}
180
181; Test a case when a function only calls longjmp and not setjmp
182define hidden void @only_longjmp() {
183entry:
184  %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
185  %arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
186  call void @longjmp(%struct.__jmp_buf_tag* %arraydecay, i32 5) #1
187  unreachable
188; CHECK: %[[ARRAYDECAY:.*]] = getelementptr inbounds
189; CHECK-NEXT: call void @emscripten_longjmp_jmpbuf(%struct.__jmp_buf_tag* %[[ARRAYDECAY]], i32 5) #1
190}
191
192declare void @foo()
193; Function Attrs: returns_twice
194declare i32 @setjmp(%struct.__jmp_buf_tag*) #0
195; Function Attrs: noreturn
196declare void @longjmp(%struct.__jmp_buf_tag*, i32) #1
197declare i32 @__gxx_personality_v0(...)
198declare i8* @__cxa_begin_catch(i8*)
199declare void @__cxa_end_catch()
200declare i8* @malloc(i32)
201declare void @free(i8*)
202
203; JS glue functions and invoke wrappers declaration
204; CHECK-DAG: declare i32* @saveSetjmp(%struct.__jmp_buf_tag*, i32, i32*, i32)
205; CHECK-DAG: declare i32 @testSetjmp(i32, i32*, i32)
206; CHECK-DAG: declare void @emscripten_longjmp_jmpbuf(%struct.__jmp_buf_tag*, i32)
207; CHECK-DAG: declare void @emscripten_longjmp(i32, i32)
208; CHECK-DAG: declare void @__invoke_void(void ()*)
209; CHECK-DAG: declare void @"__invoke_void_%struct.__jmp_buf_tag*_i32"(void (%struct.__jmp_buf_tag*, i32)*, %struct.__jmp_buf_tag*, i32)
210
211attributes #0 = { returns_twice }
212attributes #1 = { noreturn }
213attributes #2 = { nounwind }
214