1 ; RUN: opt < %s -wasm-lower-em-ehsjlj -S | FileCheck %s
2 ; RUN: llc < %s -verify-machineinstrs
4 ; Tests for cases when exception handling and setjmp/longjmp handling are mixed.
6 target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
7 target triple = "wasm32-unknown-unknown"
9 %struct.__jmp_buf_tag = type { [6 x i32], i32, [32 x i32] }
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
15 define void @setjmp_longjmp_exception() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
16 ; CHECK-LABEL: @setjmp_longjmp_exception
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
22 to label %try.cont unwind label %lpad
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
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
36 ; longjmp checking part
38 ; CHECK: call i32 @testSetjmp
40 lpad: ; preds = %entry
41 %0 = landingpad { i8*, i32 }
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()
49 try.cont: ; preds = %entry, %lpad
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.
57 define void @rethrow_longjmp() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
58 ; CHECK-LABEL: @rethrow_longjmp
61 to label %try.cont unwind label %lpad
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
69 ; CHECK-NEXT: %cmp = icmp eq i32 %__THREW__.val, 1
70 ; CHECK-NEXT: br i1 %cmp, label %lpad, label %try.cont
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
77 lpad: ; preds = %entry
78 %0 = landingpad { i8*, i32 }
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()
86 try.cont: ; preds = %entry, %lpad
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.
94 define void @rethrow_exception() {
95 ; CHECK-LABEL: @rethrow_exception
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
103 if.end: ; preds = %entry
108 ; CHECK: %cmp.eq.one = icmp eq i32 %__THREW__.val, 1
109 ; CHECK-NEXT: br i1 %cmp.eq.one, label %eh.rethrow, label %normal
112 ; CHECK-NEXT: icmp ne i32 %__THREW__.val, 0
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
121 return: ; preds = %entry, %if.end
125 ; The same as 'rethrow_exception' but contains a __cxa_throw call. We have to
126 ; free the setjmpTable buffer before calling __cxa_throw.
127 define void @rethrow_exception2() {
128 ; CHECK-LABEL: @rethrow_exception2
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
136 if.end: ; preds = %entry
140 throw: ; preds = %entry, %if.end
141 call void @__cxa_throw(i8* null, i8* null, i8* null) #1
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
152 ; Function Attrs: returns_twice
153 declare i32 @setjmp(%struct.__jmp_buf_tag*)
154 ; Function Attrs: noreturn
155 declare void @longjmp(%struct.__jmp_buf_tag*, i32)
156 declare i32 @__gxx_personality_v0(...)
157 declare i8* @__cxa_begin_catch(i8*)
158 declare void @__cxa_end_catch()
159 declare void @__cxa_throw(i8*, i8*, i8*)
161 attributes #0 = { returns_twice }
162 attributes #1 = { noreturn }