1; RUN: opt < %s -wasm-lower-em-ehsjlj -S | FileCheck %s
2; RUN: llc < %s -verify-machineinstrs
3
4; Tests for cases when exception handling and setjmp/longjmp handling are mixed.
5
6target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
7target triple = "wasm32-unknown-unknown"
8
9%struct.__jmp_buf_tag = type { [6 x i32], i32, [32 x i32] }
10
11; There is a function call (@foo) that can either throw an exception or longjmp
12; and there is also a setjmp call. When @foo throws, we have to check both for
13; exception and longjmp and jump to exception or longjmp handling BB depending
14; on the result.
15define void @setjmp_longjmp_exception() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
16; CHECK-LABEL: @setjmp_longjmp_exception
17entry:
18  %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
19  %arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
20  %call = call i32 @setjmp(%struct.__jmp_buf_tag* %arraydecay) #0
21  invoke void @foo()
22          to label %try.cont unwind label %lpad
23
24; CHECK:    entry.split:
25; CHECK:      %[[CMP0:.*]] = icmp ne i32 %__THREW__.val, 0
26; CHECK-NEXT: %__threwValue.val = load i32, i32* @__threwValue
27; CHECK-NEXT: %[[CMP1:.*]] = icmp ne i32 %__threwValue.val, 0
28; CHECK-NEXT: %[[CMP:.*]] = and i1 %[[CMP0]], %[[CMP1]]
29; CHECK-NEXT: br i1 %[[CMP]], label %if.then1, label %if.else1
30
31; This is exception checking part. %if.else1 leads here
32; CHECK:    entry.split.split:
33; CHECK-NEXT: %[[CMP:.*]] = icmp eq i32 %__THREW__.val, 1
34; CHECK-NEXT: br i1 %[[CMP]], label %lpad, label %try.cont
35
36; longjmp checking part
37; CHECK:    if.then1:
38; CHECK:      call i32 @testSetjmp
39
40lpad:                                             ; preds = %entry
41  %0 = landingpad { i8*, i32 }
42          catch i8* null
43  %1 = extractvalue { i8*, i32 } %0, 0
44  %2 = extractvalue { i8*, i32 } %0, 1
45  %3 = call i8* @__cxa_begin_catch(i8* %1) #2
46  call void @__cxa_end_catch()
47  br label %try.cont
48
49try.cont:                                         ; preds = %entry, %lpad
50  ret void
51}
52
53; @foo can either throw an exception or longjmp. Because this function doesn't
54; have any setjmp calls, we only handle exceptions in this function. But because
55; sjlj is enabled, we check if the thrown value is longjmp and if so rethrow it
56; by calling @emscripten_longjmp.
57define void @rethrow_longjmp() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
58; CHECK-LABEL: @rethrow_longjmp
59entry:
60  invoke void @foo()
61          to label %try.cont unwind label %lpad
62; CHECK:    entry:
63; CHECK:      %cmp.eq.one = icmp eq i32 %__THREW__.val, 1
64; CHECK-NEXT: %cmp.eq.zero = icmp eq i32 %__THREW__.val, 0
65; CHECK-NEXT: %or = or i1 %cmp.eq.zero, %cmp.eq.one
66; CHECK-NEXT: br i1 %or, label %tail, label %longjmp.rethrow
67
68; CHECK:    tail:
69; CHECK-NEXT: %cmp = icmp eq i32 %__THREW__.val, 1
70; CHECK-NEXT: br i1 %cmp, label %lpad, label %try.cont
71
72; CHECK:    longjmp.rethrow:
73; CHECK-NEXT: %__threwValue.val = load i32, i32* @__threwValue, align 4
74; CHECK-NEXT: call void @emscripten_longjmp(i32 %__THREW__.val, i32 %__threwValue.val)
75; CHECK-NEXT: unreachable
76
77lpad:                                             ; preds = %entry
78  %0 = landingpad { i8*, i32 }
79          catch i8* null
80  %1 = extractvalue { i8*, i32 } %0, 0
81  %2 = extractvalue { i8*, i32 } %0, 1
82  %3 = call i8* @__cxa_begin_catch(i8* %1) #5
83  call void @__cxa_end_catch()
84  br label %try.cont
85
86try.cont:                                         ; preds = %entry, %lpad
87  ret void
88}
89
90; This function contains a setjmp call and no invoke, so we only handle longjmp
91; here. But @foo can also throw an exception, so we check if an exception is
92; thrown and if so rethrow it by calling @__resumeException. Also we have to
93; free the setjmpTable buffer before calling @__resumeException.
94define void @rethrow_exception() {
95; CHECK-LABEL: @rethrow_exception
96entry:
97  %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
98  %arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
99  %call = call i32 @setjmp(%struct.__jmp_buf_tag* %arraydecay) #0
100  %cmp = icmp ne i32 %call, 0
101  br i1 %cmp, label %return, label %if.end
102
103if.end:                                           ; preds = %entry
104  call void @foo()
105  br label %return
106
107; CHECK:    if.end:
108; CHECK:      %cmp.eq.one = icmp eq i32 %__THREW__.val, 1
109; CHECK-NEXT: br i1 %cmp.eq.one, label %eh.rethrow, label %normal
110
111; CHECK:    normal:
112; CHECK-NEXT: icmp ne i32 %__THREW__.val, 0
113
114; CHECK:    eh.rethrow:
115; CHECK-NEXT: %exn = call i8* @__cxa_find_matching_catch_2()
116; CHECK-NEXT: %[[BUF:.*]] = bitcast i32* %setjmpTable1 to i8*
117; CHECK-NEXT: call void @free(i8* %[[BUF]])
118; CHECK-NEXT: call void @__resumeException(i8* %exn)
119; CHECK-NEXT: unreachable
120
121return:                                           ; preds = %entry, %if.end
122  ret void
123}
124
125; The same as 'rethrow_exception' but contains a __cxa_throw call. We have to
126; free the setjmpTable buffer before calling __cxa_throw.
127define void @rethrow_exception2() {
128; CHECK-LABEL: @rethrow_exception2
129entry:
130  %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
131  %arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
132  %call = call i32 @setjmp(%struct.__jmp_buf_tag* %arraydecay) #0
133  %cmp = icmp ne i32 %call, 0
134  br i1 %cmp, label %throw, label %if.end
135
136if.end:                                           ; preds = %entry
137  call void @foo()
138  br label %throw
139
140throw:                                            ; preds = %entry, %if.end
141  call void @__cxa_throw(i8* null, i8* null, i8* null) #1
142  unreachable
143
144; CHECK: throw:
145; CHECK:      %[[BUF:.*]] = bitcast i32* %setjmpTable5 to i8*
146; CHECK-NEXT: call void @free(i8* %[[BUF]])
147; CHECK-NEXT: call void @__cxa_throw(i8* null, i8* null, i8* null)
148; CHECK-NEXT: unreachable
149}
150
151declare void @foo()
152; Function Attrs: returns_twice
153declare i32 @setjmp(%struct.__jmp_buf_tag*)
154; Function Attrs: noreturn
155declare void @longjmp(%struct.__jmp_buf_tag*, i32)
156declare i32 @__gxx_personality_v0(...)
157declare i8* @__cxa_begin_catch(i8*)
158declare void @__cxa_end_catch()
159declare void @__cxa_throw(i8*, i8*, i8*)
160
161attributes #0 = { returns_twice }
162attributes #1 = { noreturn }
163