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.
93define void @rethrow_exception() {
94; CHECK-LABEL: @rethrow_exception
95entry:
96  %buf = alloca [1 x %struct.__jmp_buf_tag], align 16
97  %arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0
98  %call = call i32 @setjmp(%struct.__jmp_buf_tag* %arraydecay) #0
99  %cmp = icmp ne i32 %call, 0
100  br i1 %cmp, label %return, label %if.end
101
102if.end:                                           ; preds = %entry
103  call void @foo()
104  br label %return
105
106; CHECK:    if.end:
107; CHECK:      %cmp.eq.one = icmp eq i32 %__THREW__.val, 1
108; CHECK-NEXT: br i1 %cmp.eq.one, label %eh.rethrow, label %normal
109
110; CHECK:    normal:
111; CHECK-NEXT: icmp ne i32 %__THREW__.val, 0
112
113; CHECK:    eh.rethrow:
114; CHECK-NEXT: %exn = call i8* @__cxa_find_matching_catch_2()
115; CHECK-NEXT: call void @__resumeException(i8* %exn)
116; CHECK-NEXT: unreachable
117
118return:                                           ; preds = %entry, %if.end
119  ret void
120}
121
122declare void @foo()
123; Function Attrs: returns_twice
124declare i32 @setjmp(%struct.__jmp_buf_tag*)
125; Function Attrs: noreturn
126declare void @longjmp(%struct.__jmp_buf_tag*, i32)
127declare i32 @__gxx_personality_v0(...)
128declare i8* @__cxa_begin_catch(i8*)
129declare void @__cxa_end_catch()
130
131attributes #0 = { returns_twice }
132attributes #1 = { noreturn }
133