1; RUN: opt < %s -wasm-lower-em-ehsjlj -enable-emscripten-cxx-exceptions -enable-emscripten-sjlj -S | FileCheck %s 2; RUN: llc < %s -enable-emscripten-cxx-exceptions -enable-emscripten-sjlj -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.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.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 = %lpad, %entry 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 %rethrow.longjmp 67 68; CHECK: try.cont: 69; CHECK-NEXT: %phi = phi i32 [ undef, %tail ], [ undef, %lpad ] 70; CHECK-NEXT: ret void 71 72; CHECK: rethrow.longjmp: 73; CHECK-NEXT: %threw.phi = phi i32 [ %__THREW__.val, %entry ] 74; CHECK-NEXT: %__threwValue.val = load i32, i32* @__threwValue, align 4 75; CHECK-NEXT: call void @emscripten_longjmp(i32 %threw.phi, i32 %__threwValue.val 76; CHECK-NEXT: unreachable 77 78; CHECK: tail: 79; CHECK-NEXT: %cmp = icmp eq i32 %__THREW__.val, 1 80; CHECK-NEXT: br i1 %cmp, label %lpad, label %try.cont 81 82lpad: ; preds = %entry 83 %0 = landingpad { i8*, i32 } 84 catch i8* null 85 %1 = extractvalue { i8*, i32 } %0, 0 86 %2 = extractvalue { i8*, i32 } %0, 1 87 %3 = call i8* @__cxa_begin_catch(i8* %1) #5 88 call void @__cxa_end_catch() 89 br label %try.cont 90 91try.cont: ; preds = %lpad, %entry 92 %phi = phi i32 [ undef, %entry ], [ undef, %lpad ] 93 ret void 94} 95 96; This function contains a setjmp call and no invoke, so we only handle longjmp 97; here. But @foo can also throw an exception, so we check if an exception is 98; thrown and if so rethrow it by calling @__resumeException. Also we have to 99; free the setjmpTable buffer before calling @__resumeException. 100define void @rethrow_exception() { 101; CHECK-LABEL: @rethrow_exception 102entry: 103 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16 104 %arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0 105 %call = call i32 @setjmp(%struct.__jmp_buf_tag* %arraydecay) #0 106 %cmp = icmp ne i32 %call, 0 107 br i1 %cmp, label %return, label %if.end 108 109if.end: ; preds = %entry 110 call void @foo() 111 br label %return 112 113; CHECK: if.end: 114; CHECK: %cmp.eq.one = icmp eq i32 %__THREW__.val, 1 115; CHECK-NEXT: br i1 %cmp.eq.one, label %rethrow.exn, label %normal 116 117; CHECK: rethrow.exn: 118; CHECK-NEXT: %exn = call i8* @__cxa_find_matching_catch_2() 119; CHECK-NEXT: %[[BUF:.*]] = bitcast i32* %setjmpTable{{.*}} to i8* 120; CHECK-NEXT: call void @free(i8* %[[BUF]]) 121; CHECK-NEXT: call void @__resumeException(i8* %exn) 122; CHECK-NEXT: unreachable 123 124; CHECK: normal: 125; CHECK-NEXT: icmp ne i32 %__THREW__.val, 0 126 127return: ; preds = %if.end, %entry 128 ret void 129} 130 131; The same as 'rethrow_exception' but contains a __cxa_throw call. We have to 132; free the setjmpTable buffer before calling __cxa_throw. 133define void @rethrow_exception2() { 134; CHECK-LABEL: @rethrow_exception2 135entry: 136 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16 137 %arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0 138 %call = call i32 @setjmp(%struct.__jmp_buf_tag* %arraydecay) #0 139 %cmp = icmp ne i32 %call, 0 140 br i1 %cmp, label %throw, label %if.end 141 142if.end: ; preds = %entry 143 call void @foo() 144 br label %throw 145 146throw: ; preds = %if.end, %entry 147 call void @__cxa_throw(i8* null, i8* null, i8* null) #1 148 unreachable 149 150; CHECK: throw: 151; CHECK: %[[BUF:.*]] = bitcast i32* %setjmpTable{{.*}} to i8* 152; CHECK-NEXT: call void @free(i8* %[[BUF]]) 153; CHECK-NEXT: call void @__cxa_throw(i8* null, i8* null, i8* null) 154; CHECK-NEXT: unreachable 155} 156 157; The same case with @rethrow_longjmp, but there are multiple function calls 158; that can possibly longjmp (instead of throwing exception) so we have to 159; rethrow them. Here we test if we correclty generate only one 'rethrow.longjmp' 160; BB and share it for multiple calls. 161define void @rethrow_longjmp_multi() personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) { 162; CHECK-LABEL: @rethrow_longjmp_multi 163entry: 164 invoke void @foo() 165 to label %bb unwind label %lpad 166 167bb: ; preds = %entry 168 invoke void @foo() 169 to label %try.cont unwind label %lpad 170 171lpad: ; preds = %bb, %entry 172 %0 = landingpad { i8*, i32 } 173 catch i8* null 174 %1 = extractvalue { i8*, i32 } %0, 0 175 %2 = extractvalue { i8*, i32 } %0, 1 176 %3 = call i8* @__cxa_begin_catch(i8* %1) #5 177 call void @__cxa_end_catch() 178 br label %try.cont 179 180try.cont: ; preds = %lpad, %bb 181 %phi = phi i32 [ undef, %bb ], [ undef, %lpad ] 182 ret void 183 184; CHECK: rethrow.longjmp: 185; CHECK-NEXT: %threw.phi = phi i32 [ %__THREW__.val, %entry ], [ %__THREW__.val1, %bb ] 186; CHECK-NEXT: %__threwValue.val = load i32, i32* @__threwValue, align 4 187; CHECK-NEXT: call void @emscripten_longjmp(i32 %threw.phi, i32 %__threwValue.val) 188; CHECK-NEXT: unreachable 189} 190 191; The same case with @rethrow_exception, but there are multiple function calls 192; that can possibly throw (instead of longjmping) so we have to rethrow them. 193; Here we test if correctly we generate only one 'rethrow.exn' BB and share it 194; for multiple calls. 195define void @rethrow_exception_multi() { 196; CHECK-LABEL: @rethrow_exception_multi 197entry: 198 %buf = alloca [1 x %struct.__jmp_buf_tag], align 16 199 %arraydecay = getelementptr inbounds [1 x %struct.__jmp_buf_tag], [1 x %struct.__jmp_buf_tag]* %buf, i32 0, i32 0 200 %call = call i32 @setjmp(%struct.__jmp_buf_tag* %arraydecay) #0 201 %cmp = icmp ne i32 %call, 0 202 br i1 %cmp, label %return, label %if.end 203 204if.end: ; preds = %entry 205 call void @foo() 206 call void @foo() 207 br label %return 208 209return: ; preds = %entry, %if.end 210 ret void 211 212; CHECK: rethrow.exn: 213; CHECK-NEXT: %setjmpTable{{.*}} = phi i32* [ %setjmpTable{{.*}}, %if.end.split ], [ %setjmpTable{{.*}}, %if.end ] 214; CHECK-NEXT: %exn = call i8* @__cxa_find_matching_catch_2() 215; CHECK-NEXT: %{{.*}} = bitcast i32* %setjmpTable{{.*}} to i8* 216; CHECK-NEXT: tail call void @free(i8* %{{.*}}) 217; CHECK-NEXT: call void @__resumeException(i8* %exn) 218; CHECK-NEXT: unreachable 219} 220 221declare void @foo() 222; Function Attrs: returns_twice 223declare i32 @setjmp(%struct.__jmp_buf_tag*) 224; Function Attrs: noreturn 225declare void @longjmp(%struct.__jmp_buf_tag*, i32) 226declare i32 @__gxx_personality_v0(...) 227declare i8* @__cxa_begin_catch(i8*) 228declare void @__cxa_end_catch() 229declare void @__cxa_throw(i8*, i8*, i8*) 230 231attributes #0 = { returns_twice } 232attributes #1 = { noreturn } 233