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