1; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -exception-model=wasm -mattr=+exception-handling -verify-machineinstrs | FileCheck --implicit-check-not=ehgcr -allow-deprecated-dag-overlap %s 2; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -exception-model=wasm -mattr=+exception-handling -verify-machineinstrs -O0 3; RUN: llc < %s -disable-wasm-fallthrough-return-opt -wasm-keep-registers -exception-model=wasm -mattr=+exception-handling 4 5target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128" 6target triple = "wasm32-unknown-unknown" 7 8%struct.Temp = type { i8 } 9 10@_ZTIi = external dso_local constant i8* 11 12; CHECK-LABEL: test_throw: 13; CHECK: throw __cpp_exception, $0 14; CHECK-NOT: unreachable 15define void @test_throw(i8* %p) { 16 call void @llvm.wasm.throw(i32 0, i8* %p) 17 ret void 18} 19 20; Simple test with a try-catch 21; 22; void foo(); 23; void test_catch() { 24; try { 25; foo(); 26; } catch (int) { 27; } 28; } 29 30; CHECK-LABEL: test_catch: 31; CHECK: global.get ${{.+}}=, __stack_pointer 32; CHECK: try 33; CHECK: call foo 34; CHECK: catch $[[EXN:[0-9]+]]=, __cpp_exception 35; CHECK: global.set __stack_pointer 36; CHECK: i32.{{store|const}} {{.*}} __wasm_lpad_context 37; CHECK: call $drop=, _Unwind_CallPersonality, $[[EXN]] 38; CHECK: block 39; CHECK: br_if 0 40; CHECK: call $drop=, __cxa_begin_catch 41; CHECK: call __cxa_end_catch 42; CHECK: br 1 43; CHECK: end_block 44; CHECK: rethrow 0 45; CHECK: end_try 46define void @test_catch() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) { 47entry: 48 invoke void @foo() 49 to label %try.cont unwind label %catch.dispatch 50 51catch.dispatch: ; preds = %entry 52 %0 = catchswitch within none [label %catch.start] unwind to caller 53 54catch.start: ; preds = %catch.dispatch 55 %1 = catchpad within %0 [i8* bitcast (i8** @_ZTIi to i8*)] 56 %2 = call i8* @llvm.wasm.get.exception(token %1) 57 %3 = call i32 @llvm.wasm.get.ehselector(token %1) 58 %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*)) 59 %matches = icmp eq i32 %3, %4 60 br i1 %matches, label %catch, label %rethrow 61 62catch: ; preds = %catch.start 63 %5 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ] 64 call void @__cxa_end_catch() [ "funclet"(token %1) ] 65 catchret from %1 to label %try.cont 66 67rethrow: ; preds = %catch.start 68 call void @llvm.wasm.rethrow() [ "funclet"(token %1) ] 69 unreachable 70 71try.cont: ; preds = %catch, %entry 72 ret void 73} 74 75; Destructor (cleanup) test 76; 77; void foo(); 78; struct Temp { 79; ~Temp() {} 80; }; 81; void test_cleanup() { 82; Temp t; 83; foo(); 84; } 85 86; CHECK-LABEL: test_cleanup: 87; CHECK: try 88; CHECK: call foo 89; CHECK: catch_all 90; CHECK: global.set __stack_pointer 91; CHECK: call $drop=, _ZN4TempD2Ev 92; CHECK: rethrow 0 93; CHECK: end_try 94define void @test_cleanup() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) { 95entry: 96 %t = alloca %struct.Temp, align 1 97 invoke void @foo() 98 to label %invoke.cont unwind label %ehcleanup 99 100invoke.cont: ; preds = %entry 101 %call = call %struct.Temp* @_ZN4TempD2Ev(%struct.Temp* %t) 102 ret void 103 104ehcleanup: ; preds = %entry 105 %0 = cleanuppad within none [] 106 %call1 = call %struct.Temp* @_ZN4TempD2Ev(%struct.Temp* %t) [ "funclet"(token %0) ] 107 cleanupret from %0 unwind to caller 108} 109 110; Calling a function that may throw within a 'catch (...)' generates a 111; temrinatepad, because __cxa_end_catch() also can throw within 'catch (...)'. 112; 113; void foo(); 114; void test_terminatepad() { 115; try { 116; foo(); 117; } catch (...) { 118; foo(); 119; } 120; } 121 122; CHECK-LABEL: test_terminatepad 123; CHECK: try 124; CHECK: call foo 125; CHECK: catch 126; CHECK: call $drop=, __cxa_begin_catch 127; CHECK: try 128; CHECK: call foo 129; CHECK: catch_all 130; CHECK: try 131; CHECK: call __cxa_end_catch 132; CHECK: catch_all 133; CHECK: call _ZSt9terminatev 134; CHECK: unreachable 135; CHECK: end_try 136; CHECK: rethrow 137; CHECK: end_try 138; CHECK: call __cxa_end_catch 139; CHECK: end_try 140define void @test_terminatepad() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) { 141entry: 142 invoke void @foo() 143 to label %try.cont unwind label %catch.dispatch 144 145catch.dispatch: ; preds = %entry 146 %0 = catchswitch within none [label %catch.start] unwind to caller 147 148catch.start: ; preds = %catch.dispatch 149 %1 = catchpad within %0 [i8* null] 150 %2 = call i8* @llvm.wasm.get.exception(token %1) 151 %3 = call i32 @llvm.wasm.get.ehselector(token %1) 152 %4 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ] 153 invoke void @foo() [ "funclet"(token %1) ] 154 to label %invoke.cont1 unwind label %ehcleanup 155 156invoke.cont1: ; preds = %catch.start 157 call void @__cxa_end_catch() [ "funclet"(token %1) ] 158 catchret from %1 to label %try.cont 159 160try.cont: ; preds = %invoke.cont1, %entry 161 ret void 162 163ehcleanup: ; preds = %catch.start 164 %5 = cleanuppad within %1 [] 165 invoke void @__cxa_end_catch() [ "funclet"(token %5) ] 166 to label %invoke.cont2 unwind label %terminate 167 168invoke.cont2: ; preds = %ehcleanup 169 cleanupret from %5 unwind to caller 170 171terminate: ; preds = %ehcleanup 172 %6 = cleanuppad within %5 [] 173 call void @_ZSt9terminatev() [ "funclet"(token %6) ] 174 unreachable 175} 176 177; Tests prologues and epilogues are not generated within EH scopes. 178; They should not be treated as funclets; BBs starting with a catch instruction 179; should not have a prologue, and BBs ending with a catchret/cleanupret should 180; not have an epilogue. This is separate from __stack_pointer restoring 181; instructions after a catch instruction. 182; 183; void bar(int) noexcept; 184; void test_no_prolog_epilog_in_ehpad() { 185; int stack_var = 0; 186; bar(stack_var); 187; try { 188; foo(); 189; } catch (int) { 190; foo(); 191; } 192; } 193 194; CHECK-LABEL: test_no_prolog_epilog_in_ehpad 195; CHECK: try 196; CHECK: call foo 197; CHECK: catch 198; CHECK-NOT: global.get $push{{.+}}=, __stack_pointer 199; CHECK: global.set __stack_pointer 200; CHECK: block 201; CHECK: block 202; CHECK: br_if 0 203; CHECK: call $drop=, __cxa_begin_catch 204; CHECK: try 205; CHECK: call foo 206; CHECK: catch 207; CHECK-NOT: global.get $push{{.+}}=, __stack_pointer 208; CHECK: global.set __stack_pointer 209; CHECK: call __cxa_end_catch 210; CHECK: rethrow 211; CHECK-NOT: global.set __stack_pointer, $pop{{.+}} 212; CHECK: end_try 213; CHECK: end_block 214; CHECK: rethrow 215; CHECK: end_block 216; CHECK-NOT: global.set __stack_pointer, $pop{{.+}} 217; CHECK: call __cxa_end_catch 218; CHECK: end_try 219define void @test_no_prolog_epilog_in_ehpad() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) { 220entry: 221 %stack_var = alloca i32, align 4 222 call void @bar(i32* %stack_var) 223 invoke void @foo() 224 to label %try.cont unwind label %catch.dispatch 225 226catch.dispatch: ; preds = %entry 227 %0 = catchswitch within none [label %catch.start] unwind to caller 228 229catch.start: ; preds = %catch.dispatch 230 %1 = catchpad within %0 [i8* bitcast (i8** @_ZTIi to i8*)] 231 %2 = call i8* @llvm.wasm.get.exception(token %1) 232 %3 = call i32 @llvm.wasm.get.ehselector(token %1) 233 %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*)) 234 %matches = icmp eq i32 %3, %4 235 br i1 %matches, label %catch, label %rethrow 236 237catch: ; preds = %catch.start 238 %5 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ] 239 %6 = bitcast i8* %5 to float* 240 %7 = load float, float* %6, align 4 241 invoke void @foo() [ "funclet"(token %1) ] 242 to label %invoke.cont1 unwind label %ehcleanup 243 244invoke.cont1: ; preds = %catch 245 call void @__cxa_end_catch() [ "funclet"(token %1) ] 246 catchret from %1 to label %try.cont 247 248rethrow: ; preds = %catch.start 249 call void @llvm.wasm.rethrow() [ "funclet"(token %1) ] 250 unreachable 251 252try.cont: ; preds = %invoke.cont1, %entry 253 ret void 254 255ehcleanup: ; preds = %catch 256 %8 = cleanuppad within %1 [] 257 call void @__cxa_end_catch() [ "funclet"(token %8) ] 258 cleanupret from %8 unwind to caller 259} 260 261; When a function does not have stack-allocated objects, it does not need to 262; store SP back to __stack_pointer global at the epilog. 263; 264; void foo(); 265; void test_no_sp_writeback() { 266; try { 267; foo(); 268; } catch (...) { 269; } 270; } 271 272; CHECK-LABEL: test_no_sp_writeback 273; CHECK: try 274; CHECK: call foo 275; CHECK: catch 276; CHECK: call $drop=, __cxa_begin_catch 277; CHECK: call __cxa_end_catch 278; CHECK: end_try 279; CHECK-NOT: global.set __stack_pointer 280; CHECK: return 281define void @test_no_sp_writeback() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) { 282entry: 283 invoke void @foo() 284 to label %try.cont unwind label %catch.dispatch 285 286catch.dispatch: ; preds = %entry 287 %0 = catchswitch within none [label %catch.start] unwind to caller 288 289catch.start: ; preds = %catch.dispatch 290 %1 = catchpad within %0 [i8* null] 291 %2 = call i8* @llvm.wasm.get.exception(token %1) 292 %3 = call i32 @llvm.wasm.get.ehselector(token %1) 293 %4 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ] 294 call void @__cxa_end_catch() [ "funclet"(token %1) ] 295 catchret from %1 to label %try.cont 296 297try.cont: ; preds = %catch.start, %entry 298 ret void 299} 300 301; When the result of @llvm.wasm.get.exception is not used. This is created to 302; fix a bug in LateEHPrepare and this should not crash. 303define void @test_get_exception_wo_use() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) { 304entry: 305 invoke void @foo() 306 to label %try.cont unwind label %catch.dispatch 307 308catch.dispatch: ; preds = %entry 309 %0 = catchswitch within none [label %catch.start] unwind to caller 310 311catch.start: ; preds = %catch.dispatch 312 %1 = catchpad within %0 [i8* null] 313 %2 = call i8* @llvm.wasm.get.exception(token %1) 314 %3 = call i32 @llvm.wasm.get.ehselector(token %1) 315 catchret from %1 to label %try.cont 316 317try.cont: ; preds = %catch.start, %entry 318 ret void 319} 320 321; Tests a case when a cleanup region (cleanuppad ~ clanupret) contains another 322; catchpad 323define void @test_complex_cleanup_region() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) { 324entry: 325 invoke void @foo() 326 to label %invoke.cont unwind label %ehcleanup 327 328invoke.cont: ; preds = %entry 329 ret void 330 331ehcleanup: ; preds = %entry 332 %0 = cleanuppad within none [] 333 invoke void @foo() [ "funclet"(token %0) ] 334 to label %ehcleanupret unwind label %catch.dispatch 335 336catch.dispatch: ; preds = %ehcleanup 337 %1 = catchswitch within %0 [label %catch.start] unwind label %ehcleanup.1 338 339catch.start: ; preds = %catch.dispatch 340 %2 = catchpad within %1 [i8* null] 341 %3 = call i8* @llvm.wasm.get.exception(token %2) 342 %4 = call i32 @llvm.wasm.get.ehselector(token %2) 343 catchret from %2 to label %ehcleanupret 344 345ehcleanup.1: ; preds = %catch.dispatch 346 %5 = cleanuppad within %0 [] 347 unreachable 348 349ehcleanupret: ; preds = %catch.start, %ehcleanup 350 cleanupret from %0 unwind to caller 351} 352 353declare void @foo() 354declare void @bar(i32*) 355declare i32 @__gxx_wasm_personality_v0(...) 356; Function Attrs: noreturn 357declare void @llvm.wasm.throw(i32, i8*) #1 358; Function Attrs: nounwind 359declare i8* @llvm.wasm.get.exception(token) #0 360; Function Attrs: nounwind 361declare i32 @llvm.wasm.get.ehselector(token) #0 362; Function Attrs: noreturn 363declare void @llvm.wasm.rethrow() #1 364; Function Attrs: nounwind 365declare i32 @llvm.eh.typeid.for(i8*) #0 366declare i8* @__cxa_begin_catch(i8*) 367declare void @__cxa_end_catch() 368declare void @_ZSt9terminatev() 369declare %struct.Temp* @_ZN4TempD2Ev(%struct.Temp* returned) 370 371attributes #0 = { nounwind } 372attributes #1 = { noreturn } 373 374; CHECK: __cpp_exception: 375; CHECK: .eventtype __cpp_exception i32 376