578 lines · plain
1; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -wasm-use-legacy-eh -exception-model=wasm -mattr=+exception-handling -verify-machineinstrs | FileCheck --implicit-check-not=ehgcr -allow-deprecated-dag-overlap %s2; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -wasm-enable-eh -wasm-use-legacy-eh -exception-model=wasm -mattr=+exception-handling -verify-machineinstrs -O03; RUN: llc < %s -disable-wasm-fallthrough-return-opt -wasm-keep-registers -wasm-enable-eh -wasm-use-legacy-eh -exception-model=wasm -mattr=+exception-handling4 5target triple = "wasm32-unknown-unknown"6 7%struct.Temp = type { i8 }8 9@_ZTIi = external dso_local constant ptr10 11; CHECK: .tagtype __cpp_exception i3212 13; CHECK-LABEL: throw:14; CHECK: throw __cpp_exception, $015; CHECK-NOT: unreachable16define void @throw(ptr %p) {17 call void @llvm.wasm.throw(i32 0, ptr %p)18 ret void19}20 21; Simple test with a try-catch22;23; void foo();24; void catch() {25; try {26; foo();27; } catch (int) {28; }29; }30 31; CHECK-LABEL: catch:32; CHECK: global.get ${{.+}}=, __stack_pointer33; CHECK: try34; CHECK: call foo35; CHECK: catch $[[EXN:[0-9]+]]=, __cpp_exception36; CHECK: global.set __stack_pointer37; CHECK: i32.store __wasm_lpad_context38; CHECK: call $drop=, _Unwind_CallPersonality, $[[EXN]]39; CHECK: block40; CHECK: br_if 041; CHECK: call $drop=, __cxa_begin_catch42; CHECK: call __cxa_end_catch43; CHECK: br 144; CHECK: end_block45; CHECK: rethrow 046; CHECK: end_try47define void @catch() personality ptr @__gxx_wasm_personality_v0 {48entry:49 invoke void @foo()50 to label %try.cont unwind label %catch.dispatch51 52catch.dispatch: ; preds = %entry53 %0 = catchswitch within none [label %catch.start] unwind to caller54 55catch.start: ; preds = %catch.dispatch56 %1 = catchpad within %0 [ptr @_ZTIi]57 %2 = call ptr @llvm.wasm.get.exception(token %1)58 %3 = call i32 @llvm.wasm.get.ehselector(token %1)59 %4 = call i32 @llvm.eh.typeid.for(ptr @_ZTIi)60 %matches = icmp eq i32 %3, %461 br i1 %matches, label %catch, label %rethrow62 63catch: ; preds = %catch.start64 %5 = call ptr @__cxa_begin_catch(ptr %2) [ "funclet"(token %1) ]65 call void @__cxa_end_catch() [ "funclet"(token %1) ]66 catchret from %1 to label %try.cont67 68rethrow: ; preds = %catch.start69 call void @llvm.wasm.rethrow() [ "funclet"(token %1) ]70 unreachable71 72try.cont: ; preds = %catch, %entry73 ret void74}75 76; Destructor (cleanup) test77;78; void foo();79; struct Temp {80; ~Temp() {}81; };82; void cleanup() {83; Temp t;84; foo();85; }86 87; CHECK-LABEL: cleanup:88; CHECK: try89; CHECK: call foo90; CHECK: catch_all91; CHECK: global.set __stack_pointer92; CHECK: call $drop=, _ZN4TempD2Ev93; CHECK: rethrow 094; CHECK: end_try95define void @cleanup() personality ptr @__gxx_wasm_personality_v0 {96entry:97 %t = alloca %struct.Temp, align 198 invoke void @foo()99 to label %invoke.cont unwind label %ehcleanup100 101invoke.cont: ; preds = %entry102 %call = call ptr @_ZN4TempD2Ev(ptr %t)103 ret void104 105ehcleanup: ; preds = %entry106 %0 = cleanuppad within none []107 %call1 = call ptr @_ZN4TempD2Ev(ptr %t) [ "funclet"(token %0) ]108 cleanupret from %0 unwind to caller109}110 111; Calling a function that may throw within a 'catch (...)' generates a112; terminatepad, because __cxa_end_catch() also can throw within 'catch (...)'.113;114; void foo();115; void terminatepad() {116; try {117; foo();118; } catch (...) {119; foo();120; }121; }122 123; CHECK-LABEL: terminatepad124; CHECK: try125; CHECK: call foo126; CHECK: catch127; CHECK: call $drop=, __cxa_begin_catch128; CHECK: try129; CHECK: call foo130; CHECK: catch_all131; CHECK: try132; CHECK: call __cxa_end_catch133; CHECK: catch_all134; CHECK: call _ZSt9terminatev135; CHECK: unreachable136; CHECK: end_try137; CHECK: rethrow138; CHECK: end_try139; CHECK: call __cxa_end_catch140; CHECK: end_try141define void @terminatepad() personality ptr @__gxx_wasm_personality_v0 {142entry:143 invoke void @foo()144 to label %try.cont unwind label %catch.dispatch145 146catch.dispatch: ; preds = %entry147 %0 = catchswitch within none [label %catch.start] unwind to caller148 149catch.start: ; preds = %catch.dispatch150 %1 = catchpad within %0 [ptr null]151 %2 = call ptr @llvm.wasm.get.exception(token %1)152 %3 = call i32 @llvm.wasm.get.ehselector(token %1)153 %4 = call ptr @__cxa_begin_catch(ptr %2) [ "funclet"(token %1) ]154 invoke void @foo() [ "funclet"(token %1) ]155 to label %invoke.cont1 unwind label %ehcleanup156 157invoke.cont1: ; preds = %catch.start158 call void @__cxa_end_catch() [ "funclet"(token %1) ]159 catchret from %1 to label %try.cont160 161try.cont: ; preds = %invoke.cont1, %entry162 ret void163 164ehcleanup: ; preds = %catch.start165 %5 = cleanuppad within %1 []166 invoke void @__cxa_end_catch() [ "funclet"(token %5) ]167 to label %invoke.cont2 unwind label %terminate168 169invoke.cont2: ; preds = %ehcleanup170 cleanupret from %5 unwind to caller171 172terminate: ; preds = %ehcleanup173 %6 = cleanuppad within %5 []174 call void @_ZSt9terminatev() #2 [ "funclet"(token %6) ]175 unreachable176}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 instruction180; should not have a prologue, and BBs ending with a catchret/cleanupret should181; not have an epilogue. This is separate from __stack_pointer restoring182; instructions after a catch instruction.183;184; void bar(int) noexcept;185; void 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: no_prolog_epilog_in_ehpad196; CHECK: try197; CHECK: call foo198; CHECK: catch199; CHECK-NOT: global.get $push{{.+}}=, __stack_pointer200; CHECK: global.set __stack_pointer201; CHECK: block202; CHECK: block203; CHECK: br_if 0204; CHECK: call $drop=, __cxa_begin_catch205; CHECK: try206; CHECK: call foo207; CHECK: catch208; CHECK-NOT: global.get $push{{.+}}=, __stack_pointer209; CHECK: global.set __stack_pointer210; CHECK: call __cxa_end_catch211; CHECK: rethrow212; CHECK-NOT: global.set __stack_pointer, $pop{{.+}}213; CHECK: end_try214; CHECK: end_block215; CHECK: rethrow216; CHECK: end_block217; CHECK-NOT: global.set __stack_pointer, $pop{{.+}}218; CHECK: call __cxa_end_catch219; CHECK: end_try220define void @no_prolog_epilog_in_ehpad() personality ptr @__gxx_wasm_personality_v0 {221entry:222 %stack_var = alloca i32, align 4223 call void @bar(ptr %stack_var)224 invoke void @foo()225 to label %try.cont unwind label %catch.dispatch226 227catch.dispatch: ; preds = %entry228 %0 = catchswitch within none [label %catch.start] unwind to caller229 230catch.start: ; preds = %catch.dispatch231 %1 = catchpad within %0 [ptr @_ZTIi]232 %2 = call ptr @llvm.wasm.get.exception(token %1)233 %3 = call i32 @llvm.wasm.get.ehselector(token %1)234 %4 = call i32 @llvm.eh.typeid.for(ptr @_ZTIi)235 %matches = icmp eq i32 %3, %4236 br i1 %matches, label %catch, label %rethrow237 238catch: ; preds = %catch.start239 %5 = call ptr @__cxa_begin_catch(ptr %2) [ "funclet"(token %1) ]240 %6 = load float, ptr %5, align 4241 invoke void @foo() [ "funclet"(token %1) ]242 to label %invoke.cont1 unwind label %ehcleanup243 244invoke.cont1: ; preds = %catch245 call void @__cxa_end_catch() [ "funclet"(token %1) ]246 catchret from %1 to label %try.cont247 248rethrow: ; preds = %catch.start249 call void @llvm.wasm.rethrow() [ "funclet"(token %1) ]250 unreachable251 252try.cont: ; preds = %invoke.cont1, %entry253 ret void254 255ehcleanup: ; preds = %catch256 %7 = cleanuppad within %1 []257 call void @__cxa_end_catch() [ "funclet"(token %7) ]258 cleanupret from %7 unwind to caller259}260 261; When a function does not have stack-allocated objects, it does not need to262; store SP back to __stack_pointer global at the epilog.263;264; void foo();265; void no_sp_writeback() {266; try {267; foo();268; } catch (...) {269; }270; }271 272; CHECK-LABEL: no_sp_writeback273; CHECK: try274; CHECK: call foo275; CHECK: catch276; CHECK: call $drop=, __cxa_begin_catch277; CHECK: call __cxa_end_catch278; CHECK: end_try279; CHECK-NOT: global.set __stack_pointer280; CHECK: return281define void @no_sp_writeback() personality ptr @__gxx_wasm_personality_v0 {282entry:283 invoke void @foo()284 to label %try.cont unwind label %catch.dispatch285 286catch.dispatch: ; preds = %entry287 %0 = catchswitch within none [label %catch.start] unwind to caller288 289catch.start: ; preds = %catch.dispatch290 %1 = catchpad within %0 [ptr null]291 %2 = call ptr @llvm.wasm.get.exception(token %1)292 %3 = call i32 @llvm.wasm.get.ehselector(token %1)293 %4 = call ptr @__cxa_begin_catch(ptr %2) [ "funclet"(token %1) ]294 call void @__cxa_end_catch() [ "funclet"(token %1) ]295 catchret from %1 to label %try.cont296 297try.cont: ; preds = %catch.start, %entry298 ret void299}300 301; When the result of @llvm.wasm.get.exception is not used. This is created to302; fix a bug in LateEHPrepare and this should not crash.303define void @get_exception_wo_use() personality ptr @__gxx_wasm_personality_v0 {304entry:305 invoke void @foo()306 to label %try.cont unwind label %catch.dispatch307 308catch.dispatch: ; preds = %entry309 %0 = catchswitch within none [label %catch.start] unwind to caller310 311catch.start: ; preds = %catch.dispatch312 %1 = catchpad within %0 [ptr null]313 %2 = call ptr @llvm.wasm.get.exception(token %1)314 %3 = call i32 @llvm.wasm.get.ehselector(token %1)315 catchret from %1 to label %try.cont316 317try.cont: ; preds = %catch.start, %entry318 ret void319}320 321; Tests a case when a cleanup region (cleanuppad ~ clanupret) contains another322; catchpad323define void @complex_cleanup_region() personality ptr @__gxx_wasm_personality_v0 {324entry:325 invoke void @foo()326 to label %invoke.cont unwind label %ehcleanup327 328invoke.cont: ; preds = %entry329 ret void330 331ehcleanup: ; preds = %entry332 %0 = cleanuppad within none []333 invoke void @foo() [ "funclet"(token %0) ]334 to label %ehcleanupret unwind label %catch.dispatch335 336catch.dispatch: ; preds = %ehcleanup337 %1 = catchswitch within %0 [label %catch.start] unwind label %ehcleanup.1338 339catch.start: ; preds = %catch.dispatch340 %2 = catchpad within %1 [ptr null]341 %3 = call ptr @llvm.wasm.get.exception(token %2)342 %4 = call i32 @llvm.wasm.get.ehselector(token %2)343 catchret from %2 to label %ehcleanupret344 345ehcleanup.1: ; preds = %catch.dispatch346 %5 = cleanuppad within %0 []347 unreachable348 349ehcleanupret: ; preds = %catch.start, %ehcleanup350 cleanupret from %0 unwind to caller351}352 353; Regression test for the bug that 'rethrow' was not treated correctly as a354; terminator in isel.355define void @rethrow_terminator() personality ptr @__gxx_wasm_personality_v0 {356entry:357 invoke void @foo()358 to label %try.cont unwind label %catch.dispatch359 360catch.dispatch: ; preds = %entry361 %0 = catchswitch within none [label %catch.start] unwind label %ehcleanup362 363catch.start: ; preds = %catch.dispatch364 %1 = catchpad within %0 [ptr @_ZTIi]365 %2 = call ptr @llvm.wasm.get.exception(token %1)366 %3 = call i32 @llvm.wasm.get.ehselector(token %1)367 %4 = call i32 @llvm.eh.typeid.for.p0(ptr @_ZTIi)368 %matches = icmp eq i32 %3, %4369 br i1 %matches, label %catch, label %rethrow370 371catch: ; preds = %catch.start372 %5 = call ptr @__cxa_begin_catch(ptr %2) [ "funclet"(token %1) ]373 %6 = load i32, ptr %5, align 4374 call void @__cxa_end_catch() [ "funclet"(token %1) ]375 catchret from %1 to label %try.cont376 377rethrow: ; preds = %catch.start378 invoke void @llvm.wasm.rethrow() #1 [ "funclet"(token %1) ]379 to label %unreachable unwind label %ehcleanup380 381try.cont: ; preds = %entry, %catch382 ret void383 384ehcleanup: ; preds = %rethrow, %catch.dispatch385 ; 'rethrow' BB is this BB's predecessor, and its386 ; 'invoke void @llvm.wasm.rethrow()' is lowered down to a 'RETHROW' in Wasm387 ; MIR. And this 'phi' creates 'CONST_I32' instruction in the predecessor388 ; 'rethrow' BB. If 'RETHROW' is not treated correctly as a terminator, it can389 ; create a BB like390 ; bb.3.rethrow:391 ; RETHROW 0392 ; %0 = CONST_I32 20393 ; BR ...394 %tmp = phi i32 [ 10, %catch.dispatch ], [ 20, %rethrow ]395 %7 = cleanuppad within none []396 call void @take_i32(i32 %tmp) [ "funclet"(token %7) ]397 cleanupret from %7 unwind to caller398 399unreachable: ; preds = %rethrow400 unreachable401}402 403; The bitcode below is generated when the code below is compiled and404; Temp::~Temp() is inlined into inlined_cleanupret():405;406; void inlined_cleanupret() {407; try {408; Temp t;409; throw 2;410; } catch (...)411; }412;413; Temp::~Temp() {414; try {415; throw 1;416; } catch (...) {417; }418; }419;420; ~Temp() generates cleanupret, which is lowered to a 'rethrow' later. That421; rethrow's immediate argument should correctly target the top-level cleanuppad422; (catch_all). This is a regression test for the bug where we did not compute423; rethrow's argument correctly.424 425; CHECK-LABEL: inlined_cleanupret:426; CHECK: try427; CHECK: call __cxa_throw428; CHECK: catch_all429; CHECK: try430; CHECK: try431; CHECK: call __cxa_throw432; CHECK: catch433; CHECK: call __cxa_end_catch434; CHECK: try435; CHECK: try436; Note that this rethrow targets the top-level catch_all437; CHECK: rethrow 4438; CHECK: catch439; CHECK: try440; CHECK: call __cxa_end_catch441; CHECK: delegate 5442; CHECK: return443; CHECK: end_try444; CHECK: delegate 3445; CHECK: end_try446; CHECK: catch_all447; CHECK: call _ZSt9terminatev448; CHECK: end_try449; CHECK: end_try450define void @inlined_cleanupret() personality ptr @__gxx_wasm_personality_v0 {451entry:452 %exception = tail call ptr @__cxa_allocate_exception(i32 4)453 store i32 2, ptr %exception, align 16454 invoke void @__cxa_throw(ptr nonnull %exception, ptr nonnull @_ZTIi, ptr null)455 to label %unreachable unwind label %ehcleanup456 457ehcleanup: ; preds = %entry458 %0 = cleanuppad within none []459 %exception.i = call ptr @__cxa_allocate_exception(i32 4) [ "funclet"(token %0) ]460 store i32 1, ptr %exception.i, align 16461 invoke void @__cxa_throw(ptr nonnull %exception.i, ptr nonnull @_ZTIi, ptr null) [ "funclet"(token %0) ]462 to label %unreachable unwind label %catch.dispatch.i463 464catch.dispatch.i: ; preds = %ehcleanup465 %1 = catchswitch within %0 [label %catch.start.i] unwind label %terminate.i466 467catch.start.i: ; preds = %catch.dispatch.i468 %2 = catchpad within %1 [ptr null]469 %3 = tail call ptr @llvm.wasm.get.exception(token %2)470 %4 = tail call i32 @llvm.wasm.get.ehselector(token %2)471 %5 = call ptr @__cxa_begin_catch(ptr %3) [ "funclet"(token %2) ]472 invoke void @__cxa_end_catch() [ "funclet"(token %2) ]473 to label %invoke.cont.i unwind label %terminate.i474 475invoke.cont.i: ; preds = %catch.start.i476 catchret from %2 to label %_ZN4TempD2Ev.exit477 478terminate.i: ; preds = %catch.start.i, %catch.dispatch.i479 %6 = cleanuppad within %0 []480 call void @_ZSt9terminatev() #2 [ "funclet"(token %6) ]481 unreachable482 483_ZN4TempD2Ev.exit: ; preds = %invoke.cont.i484 cleanupret from %0 unwind label %catch.dispatch485 486catch.dispatch: ; preds = %_ZN4TempD2Ev.exit487 %7 = catchswitch within none [label %catch.start] unwind to caller488 489catch.start: ; preds = %catch.dispatch490 %8 = catchpad within %7 [ptr null]491 %9 = tail call ptr @llvm.wasm.get.exception(token %8)492 %10 = tail call i32 @llvm.wasm.get.ehselector(token %8)493 %11 = call ptr @__cxa_begin_catch(ptr %9) #8 [ "funclet"(token %8) ]494 call void @__cxa_end_catch() [ "funclet"(token %8) ]495 catchret from %8 to label %try.cont496 497try.cont: ; preds = %catch.start498 ret void499 500unreachable: ; preds = %entry501 unreachable502}503 504; Regression test for a bug where, when an invoke unwinds to a catchswitch, the505; catchswitch's unwind destination was not included in the invoke's unwind506; destination when there was no direct link from catch.start to there.507 508; CHECK-LABEL: unwind_destinations:509; CHECK: try510; CHECK: try511; CHECK: call foo512; CHECK: catch $0=, __cpp_exception513; CHECK: call _ZSt9terminatev514; CHECK: unreachable515; CHECK: end_try516; Note the below is "terminate" BB and should not be DCE'd517; CHECK: catch_all518; CHECK: call _ZSt9terminatev519; CHECK: unreachable520; CHECK: end_try521; CHECK: return522define void @unwind_destinations() personality ptr @__gxx_wasm_personality_v0 {523entry:524 invoke void @foo()525 to label %try.cont unwind label %catch.dispatch526 527catch.dispatch: ; preds = %entry528 %0 = catchswitch within none [label %catch.start] unwind label %terminate529 530catch.start: ; preds = %catch.dispatch531 %1 = catchpad within %0 [ptr null]532 %2 = call ptr @llvm.wasm.get.exception(token %1)533 %3 = call ptr @__cxa_begin_catch(ptr %2) #5 [ "funclet"(token %1) ]534 call void @_ZSt9terminatev() #2 [ "funclet"(token %1) ]535 unreachable536 537; Even if there is no link from catch.start to this terminate BB, when there is538; an exception that catch.start does not catch (e.g. a foreign exception), it539; should end up here, so this BB should NOT be DCE'ed540terminate: ; preds = %catch.dispatch541 %4 = cleanuppad within none []542 call void @_ZSt9terminatev() #2 [ "funclet"(token %4) ]543 unreachable544 545try.cont: ; preds = %entry546 ret void547}548 549declare void @foo()550declare void @bar(ptr)551declare void @take_i32(i32)552declare i32 @__gxx_wasm_personality_v0(...)553; Function Attrs: noreturn554declare void @llvm.wasm.throw(i32, ptr) #1555; Function Attrs: nounwind556declare ptr @llvm.wasm.get.exception(token) #0557; Function Attrs: nounwind558declare i32 @llvm.wasm.get.ehselector(token) #0559; Function Attrs: noreturn560declare void @llvm.wasm.rethrow() #1561; Function Attrs: nounwind562declare i32 @llvm.eh.typeid.for(ptr) #0563; Function Attrs: nounwind564declare ptr @__cxa_allocate_exception(i32) #0565declare ptr @__cxa_begin_catch(ptr)566declare void @__cxa_end_catch()567; Function Attrs: noreturn568declare void @__cxa_throw(ptr, ptr, ptr) #1569declare void @_ZSt9terminatev()570declare ptr @_ZN4TempD2Ev(ptr returned)571 572attributes #0 = { nounwind }573attributes #1 = { noreturn }574attributes #2 = { noreturn nounwind }575 576;; The exception tag should not be defined locally577; CHECK-NOT: __cpp_exception:578