1; REQUIRES: asserts 2; RUN: llc < %s -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -disable-block-placement -verify-machineinstrs -fast-isel=false -machine-sink-split-probability-threshold=0 -cgp-freq-ratio-to-skip-merge=1000 -exception-model=wasm -mattr=+exception-handling | FileCheck %s 3; RUN: llc < %s -disable-wasm-fallthrough-return-opt -disable-block-placement -verify-machineinstrs -fast-isel=false -machine-sink-split-probability-threshold=0 -cgp-freq-ratio-to-skip-merge=1000 -exception-model=wasm -mattr=+exception-handling 4; RUN: llc < %s -O0 -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -verify-machineinstrs -exception-model=wasm -mattr=+exception-handling | FileCheck %s --check-prefix=NOOPT 5; RUN: llc < %s -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -disable-block-placement -verify-machineinstrs -fast-isel=false -machine-sink-split-probability-threshold=0 -cgp-freq-ratio-to-skip-merge=1000 -exception-model=wasm -mattr=+exception-handling -wasm-disable-ehpad-sort | FileCheck %s --check-prefix=NOSORT 6; RUN: llc < %s -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -disable-block-placement -verify-machineinstrs -fast-isel=false -machine-sink-split-probability-threshold=0 -cgp-freq-ratio-to-skip-merge=1000 -exception-model=wasm -mattr=+exception-handling -wasm-disable-ehpad-sort -stats 2>&1 | FileCheck %s --check-prefix=NOSORT-STAT 7 8target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128" 9target triple = "wasm32-unknown-unknown" 10 11@_ZTIi = external constant i8* 12@_ZTId = external constant i8* 13 14; Simple test case with two catch clauses 15; 16; void foo(); 17; void test0() { 18; try { 19; foo(); 20; } catch (int) { 21; } catch (double) { 22; } 23; } 24 25; CHECK-LABEL: test0 26; CHECK: try 27; CHECK: call foo 28; CHECK: catch 29; CHECK: block 30; CHECK: br_if 0, {{.*}} # 0: down to label2 31; CHECK: call $drop=, __cxa_begin_catch 32; CHECK: call __cxa_end_catch 33; CHECK: br 1 # 1: down to label0 34; CHECK: end_block # label2: 35; CHECK: block 36; CHECK: br_if 0, {{.*}} # 0: down to label3 37; CHECK: call $drop=, __cxa_begin_catch 38; CHECK: call __cxa_end_catch 39; CHECK: br 1 # 1: down to label0 40; CHECK: end_block # label3: 41; CHECK: rethrow {{.*}} # to caller 42; CHECK: end_try # label0: 43define void @test0() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) { 44entry: 45 invoke void @foo() 46 to label %try.cont unwind label %catch.dispatch 47 48catch.dispatch: ; preds = %entry 49 %0 = catchswitch within none [label %catch.start] unwind to caller 50 51catch.start: ; preds = %catch.dispatch 52 %1 = catchpad within %0 [i8* bitcast (i8** @_ZTIi to i8*), i8* bitcast (i8** @_ZTId to i8*)] 53 %2 = call i8* @llvm.wasm.get.exception(token %1) 54 %3 = call i32 @llvm.wasm.get.ehselector(token %1) 55 %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*)) 56 %matches = icmp eq i32 %3, %4 57 br i1 %matches, label %catch2, label %catch.fallthrough 58 59catch2: ; preds = %catch.start 60 %5 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ] 61 call void @__cxa_end_catch() [ "funclet"(token %1) ] 62 catchret from %1 to label %try.cont 63 64catch.fallthrough: ; preds = %catch.start 65 %6 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTId to i8*)) 66 %matches1 = icmp eq i32 %3, %6 67 br i1 %matches1, label %catch, label %rethrow 68 69catch: ; preds = %catch.fallthrough 70 %7 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ] 71 call void @__cxa_end_catch() [ "funclet"(token %1) ] 72 catchret from %1 to label %try.cont 73 74rethrow: ; preds = %catch.fallthrough 75 call void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %1) ] 76 unreachable 77 78try.cont: ; preds = %catch, %catch2, %entry 79 ret void 80} 81 82; Nested try-catches within a catch 83; void test1() { 84; try { 85; foo(); 86; } catch (int) { 87; try { 88; foo(); 89; } catch (int) { 90; foo(); 91; } 92; } 93; } 94 95; CHECK-LABEL: test1 96; CHECK: try 97; CHECK: call foo 98; CHECK: catch 99; CHECK: block 100; CHECK: block 101; CHECK: br_if 0, {{.*}} # 0: down to label7 102; CHECK: call $drop=, __cxa_begin_catch 103; CHECK: try 104; CHECK: call foo 105; CHECK: br 2 # 2: down to label6 106; CHECK: catch 107; CHECK: try 108; CHECK: block 109; CHECK: br_if 0, {{.*}} # 0: down to label11 110; CHECK: call $drop=, __cxa_begin_catch 111; CHECK: try 112; CHECK: call foo 113; CHECK: br 2 # 2: down to label9 114; CHECK: catch 115; CHECK: call __cxa_end_catch 116; CHECK: rethrow {{.*}} # down to catch3 117; CHECK: end_try 118; CHECK: end_block # label11: 119; CHECK: rethrow {{.*}} # down to catch3 120; CHECK: catch {{.*}} # catch3: 121; CHECK: call __cxa_end_catch 122; CHECK: rethrow {{.*}} # to caller 123; CHECK: end_try # label9: 124; CHECK: call __cxa_end_catch 125; CHECK: br 2 # 2: down to label6 126; CHECK: end_try 127; CHECK: end_block # label7: 128; CHECK: rethrow {{.*}} # to caller 129; CHECK: end_block # label6: 130; CHECK: call __cxa_end_catch 131; CHECK: end_try 132define void @test1() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) { 133entry: 134 invoke void @foo() 135 to label %try.cont11 unwind label %catch.dispatch 136 137catch.dispatch: ; preds = %entry 138 %0 = catchswitch within none [label %catch.start] unwind to caller 139 140catch.start: ; preds = %catch.dispatch 141 %1 = catchpad within %0 [i8* bitcast (i8** @_ZTIi to i8*)] 142 %2 = call i8* @llvm.wasm.get.exception(token %1) 143 %3 = call i32 @llvm.wasm.get.ehselector(token %1) 144 %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*)) 145 %matches = icmp eq i32 %3, %4 146 br i1 %matches, label %catch, label %rethrow 147 148catch: ; preds = %catch.start 149 %5 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ] 150 %6 = bitcast i8* %5 to i32* 151 %7 = load i32, i32* %6, align 4 152 invoke void @foo() [ "funclet"(token %1) ] 153 to label %try.cont unwind label %catch.dispatch2 154 155catch.dispatch2: ; preds = %catch 156 %8 = catchswitch within %1 [label %catch.start3] unwind label %ehcleanup9 157 158catch.start3: ; preds = %catch.dispatch2 159 %9 = catchpad within %8 [i8* bitcast (i8** @_ZTIi to i8*)] 160 %10 = call i8* @llvm.wasm.get.exception(token %9) 161 %11 = call i32 @llvm.wasm.get.ehselector(token %9) 162 %12 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*)) 163 %matches4 = icmp eq i32 %11, %12 164 br i1 %matches4, label %catch6, label %rethrow5 165 166catch6: ; preds = %catch.start3 167 %13 = call i8* @__cxa_begin_catch(i8* %10) [ "funclet"(token %9) ] 168 %14 = bitcast i8* %13 to i32* 169 %15 = load i32, i32* %14, align 4 170 invoke void @foo() [ "funclet"(token %9) ] 171 to label %invoke.cont8 unwind label %ehcleanup 172 173invoke.cont8: ; preds = %catch6 174 call void @__cxa_end_catch() [ "funclet"(token %9) ] 175 catchret from %9 to label %try.cont 176 177rethrow5: ; preds = %catch.start3 178 invoke void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %9) ] 179 to label %unreachable unwind label %ehcleanup9 180 181try.cont: ; preds = %invoke.cont8, %catch 182 call void @__cxa_end_catch() [ "funclet"(token %1) ] 183 catchret from %1 to label %try.cont11 184 185rethrow: ; preds = %catch.start 186 call void @llvm.wasm.rethrow.in.catch() [ "funclet"(token %1) ] 187 unreachable 188 189try.cont11: ; preds = %try.cont, %entry 190 ret void 191 192ehcleanup: ; preds = %catch6 193 %16 = cleanuppad within %9 [] 194 call void @__cxa_end_catch() [ "funclet"(token %16) ] 195 cleanupret from %16 unwind label %ehcleanup9 196 197ehcleanup9: ; preds = %ehcleanup, %rethrow5, %catch.dispatch2 198 %17 = cleanuppad within %1 [] 199 call void @__cxa_end_catch() [ "funclet"(token %17) ] 200 cleanupret from %17 unwind to caller 201 202unreachable: ; preds = %rethrow5 203 unreachable 204} 205 206; Nested loop within a catch clause 207; void test2() { 208; try { 209; foo(); 210; } catch (...) { 211; for (int i = 0; i < 50; i++) 212; foo(); 213; } 214; } 215 216; CHECK-LABEL: test2 217; CHECK: try 218; CHECK: call foo 219; CHECK: catch 220; CHECK: call $drop=, __cxa_begin_catch 221; CHECK: loop # label15: 222; CHECK: block 223; CHECK: block 224; CHECK: br_if 0, {{.*}} # 0: down to label17 225; CHECK: try 226; CHECK: call foo 227; CHECK: br 2 # 2: down to label16 228; CHECK: catch 229; CHECK: try 230; CHECK: call __cxa_end_catch 231; CHECK: catch 232; CHECK: call __clang_call_terminate 233; CHECK: unreachable 234; CHECK: end_try 235; CHECK: rethrow {{.*}} # to caller 236; CHECK: end_try 237; CHECK: end_block # label17: 238; CHECK: call __cxa_end_catch 239; CHECK: br 2 # 2: down to label13 240; CHECK: end_block # label16: 241; CHECK: br 0 # 0: up to label15 242; CHECK: end_loop 243; CHECK: end_try # label13: 244define void @test2() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) { 245entry: 246 invoke void @foo() 247 to label %try.cont unwind label %catch.dispatch 248 249catch.dispatch: ; preds = %entry 250 %0 = catchswitch within none [label %catch.start] unwind to caller 251 252catch.start: ; preds = %catch.dispatch 253 %1 = catchpad within %0 [i8* null] 254 %2 = call i8* @llvm.wasm.get.exception(token %1) 255 %3 = call i32 @llvm.wasm.get.ehselector(token %1) 256 %4 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ] 257 br label %for.cond 258 259for.cond: ; preds = %for.inc, %catch.start 260 %i.0 = phi i32 [ 0, %catch.start ], [ %inc, %for.inc ] 261 %cmp = icmp slt i32 %i.0, 50 262 br i1 %cmp, label %for.body, label %for.end 263 264for.body: ; preds = %for.cond 265 invoke void @foo() [ "funclet"(token %1) ] 266 to label %for.inc unwind label %ehcleanup 267 268for.inc: ; preds = %for.body 269 %inc = add nsw i32 %i.0, 1 270 br label %for.cond 271 272for.end: ; preds = %for.cond 273 call void @__cxa_end_catch() [ "funclet"(token %1) ] 274 catchret from %1 to label %try.cont 275 276try.cont: ; preds = %for.end, %entry 277 ret void 278 279ehcleanup: ; preds = %for.body 280 %5 = cleanuppad within %1 [] 281 invoke void @__cxa_end_catch() [ "funclet"(token %5) ] 282 to label %invoke.cont2 unwind label %terminate 283 284invoke.cont2: ; preds = %ehcleanup 285 cleanupret from %5 unwind to caller 286 287terminate: ; preds = %ehcleanup 288 %6 = cleanuppad within %5 [] 289 %7 = call i8* @llvm.wasm.get.exception(token %6) 290 call void @__clang_call_terminate(i8* %7) [ "funclet"(token %6) ] 291 unreachable 292} 293 294; Tests if block and try markers are correctly placed. Even if two predecessors 295; of the EH pad are bb2 and bb3 and their nearest common dominator is bb1, the 296; TRY marker should be placed at bb0 because there's a branch from bb0 to bb2, 297; and scopes cannot be interleaved. 298 299; NOOPT-LABEL: test3 300; NOOPT: try 301; NOOPT: block 302; NOOPT: block 303; NOOPT: block 304; NOOPT: end_block 305; NOOPT: end_block 306; NOOPT: call foo 307; NOOPT: end_block 308; NOOPT: call bar 309; NOOPT: catch {{.*}} 310; NOOPT: end_try 311define void @test3() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) { 312bb0: 313 br i1 undef, label %bb1, label %bb2 314 315bb1: ; preds = %bb0 316 br i1 undef, label %bb3, label %bb4 317 318bb2: ; preds = %bb0 319 br label %try.cont 320 321bb3: ; preds = %bb1 322 invoke void @foo() 323 to label %try.cont unwind label %catch.dispatch 324 325bb4: ; preds = %bb1 326 invoke void @bar() 327 to label %try.cont unwind label %catch.dispatch 328 329catch.dispatch: ; preds = %bb4, %bb3 330 %0 = catchswitch within none [label %catch.start] unwind to caller 331 332catch.start: ; preds = %catch.dispatch 333 %1 = catchpad within %0 [i8* null] 334 %2 = call i8* @llvm.wasm.get.exception(token %1) 335 %3 = call i32 @llvm.wasm.get.ehselector(token %1) 336 catchret from %1 to label %try.cont 337 338try.cont: ; preds = %catch.start, %bb4, %bb3, %bb2 339 ret void 340} 341 342; Tests if try/end_try markers are placed correctly wrt loop/end_loop markers, 343; when try and loop markers are in the same BB and end_try and end_loop are in 344; another BB. 345; CHECK: loop 346; CHECK: try 347; CHECK: call foo 348; CHECK: catch 349; CHECK: end_try 350; CHECK: end_loop 351define void @test4(i32* %p) personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) { 352entry: 353 store volatile i32 0, i32* %p 354 br label %loop 355 356loop: ; preds = %try.cont, %entry 357 store volatile i32 1, i32* %p 358 invoke void @foo() 359 to label %try.cont unwind label %catch.dispatch 360 361catch.dispatch: ; preds = %loop 362 %0 = catchswitch within none [label %catch.start] unwind to caller 363 364catch.start: ; preds = %catch.dispatch 365 %1 = catchpad within %0 [i8* null] 366 %2 = call i8* @llvm.wasm.get.exception(token %1) 367 %3 = call i32 @llvm.wasm.get.ehselector(token %1) 368 catchret from %1 to label %try.cont 369 370try.cont: ; preds = %catch.start, %loop 371 br label %loop 372} 373 374; Some of test cases below are hand-tweaked by deleting some library calls to 375; simplify tests and changing the order of basic blocks to cause unwind 376; destination mismatches. And we use -wasm-disable-ehpad-sort to create maximum 377; number of mismatches in several tests below. 378 379; 'call bar''s original unwind destination was 'catch14', but after control flow 380; linearization, its unwind destination incorrectly becomes 'catch15'. We fix 381; this by wrapping the call with a nested try/catch/end_try and branching to the 382; right destination (label32). 383 384; NOSORT-LABEL: test5 385; NOSORT: block 386; NOSORT: try 387; NOSORT: try 388; NOSORT: call foo 389; --- Nested try/catch/end_try starts 390; NOSORT: try 391; NOSORT: call bar 392; NOSORT: catch $drop= 393; NOSORT: br 2 # 2: down to label32 394; NOSORT: end_try 395; --- Nested try/catch/end_try ends 396; NOSORT: br 2 # 2: down to label31 397; NOSORT: catch $drop= # catch15: 398; NOSORT: br 2 # 2: down to label31 399; NOSORT: end_try 400; NOSORT: catch $drop= # catch14: 401; NOSORT: end_try # label32: 402; NOSORT: end_block # label31: 403; NOSORT: return 404 405define void @test5() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) { 406bb0: 407 invoke void @foo() 408 to label %bb1 unwind label %catch.dispatch0 409 410bb1: ; preds = %bb0 411 invoke void @bar() 412 to label %try.cont unwind label %catch.dispatch1 413 414catch.dispatch0: ; preds = %bb0 415 %0 = catchswitch within none [label %catch.start0] unwind to caller 416 417catch.start0: ; preds = %catch.dispatch0 418 %1 = catchpad within %0 [i8* null] 419 %2 = call i8* @llvm.wasm.get.exception(token %1) 420 %3 = call i32 @llvm.wasm.get.ehselector(token %1) 421 catchret from %1 to label %try.cont 422 423catch.dispatch1: ; preds = %bb1 424 %4 = catchswitch within none [label %catch.start1] unwind to caller 425 426catch.start1: ; preds = %catch.dispatch1 427 %5 = catchpad within %4 [i8* null] 428 %6 = call i8* @llvm.wasm.get.exception(token %5) 429 %7 = call i32 @llvm.wasm.get.ehselector(token %5) 430 catchret from %5 to label %try.cont 431 432try.cont: ; preds = %catch.start1, %catch.start0, %bb1 433 ret void 434} 435 436; Two 'call bar''s original unwind destination was the caller, but after control 437; flow linearization, their unwind destination incorrectly becomes 'catch17'. We 438; fix this by wrapping the call with a nested try/catch/end_try and branching to 439; the right destination (label4), from which we rethrow the exception to the 440; caller. 441 442; And the return value of 'baz' should NOT be stackified because the BB is split 443; during fixing unwind mismatches. 444 445; NOSORT-LABEL: test6 446; NOSORT: try 447; NOSORT: call foo 448; --- Nested try/catch/end_try starts 449; NOSORT: try 450; NOSORT: call bar 451; NOSORT: call ${{[0-9]+}}=, baz 452; NOSORT-NOT: call $push{{.*}}=, baz 453; NOSORT: catch $[[REG:[0-9]+]]= 454; NOSORT: br 1 # 1: down to label35 455; NOSORT: end_try 456; --- Nested try/catch/end_try ends 457; NOSORT: return 458; NOSORT: catch $drop= # catch17: 459; NOSORT: return 460; NOSORT: end_try # label35: 461; NOSORT: rethrow $[[REG]] # to caller 462 463define void @test6() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) { 464bb0: 465 invoke void @foo() 466 to label %bb1 unwind label %catch.dispatch0 467 468bb1: ; preds = %bb0 469 call void @bar() 470 %call = call i32 @baz() 471 call void @nothrow(i32 %call) #0 472 ret void 473 474catch.dispatch0: ; preds = %bb0 475 %0 = catchswitch within none [label %catch.start0] unwind to caller 476 477catch.start0: ; preds = %catch.dispatch0 478 %1 = catchpad within %0 [i8* null] 479 %2 = call i8* @llvm.wasm.get.exception(token %1) 480 %3 = call i32 @llvm.wasm.get.ehselector(token %1) 481 catchret from %1 to label %try.cont 482 483try.cont: ; preds = %catch.start0 484 ret void 485} 486 487; Similar situation as @test6. Here 'call @qux''s original unwind destination 488; was the caller, but after control flow linearization, their unwind destination 489; incorrectly becomes another catch within the function. We fix this by wrapping 490; the call with a nested try/catch/end_try and branching to the right 491; destination, from which we rethrow the exception to the caller. 492 493; Because 'call @qux' pops an argument pushed by 'i32.const 5' from stack, the 494; nested 'try' should be placed before `i32.const 5', not between 'i32.const 5' 495; and 'call @qux'. 496 497; NOSORT-LABEL: test7 498; NOSORT: try 499; NOSORT: call foo 500; --- Nested try/catch/end_try starts 501; NOSORT: try 502; NOSORT-NEXT: i32.const $push{{[0-9]+}}=, 5 503; NOSORT-NEXT: call ${{[0-9]+}}=, qux 504; NOSORT: catch $[[REG:[0-9]+]]= 505; NOSORT: br 1 # 1: down to label37 506; NOSORT: end_try 507; --- Nested try/catch/end_try ends 508; NOSORT: return 509; NOSORT: catch $drop= # catch19: 510; NOSORT: return 511; NOSORT: end_try # label37: 512; NOSORT: rethrow $[[REG]] # to caller 513 514define i32 @test7() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) { 515bb0: 516 invoke void @foo() 517 to label %bb1 unwind label %catch.dispatch0 518 519bb1: ; preds = %bb0 520 %0 = call i32 @qux(i32 5) 521 ret i32 %0 522 523catch.dispatch0: ; preds = %bb0 524 %1 = catchswitch within none [label %catch.start0] unwind to caller 525 526catch.start0: ; preds = %catch.dispatch0 527 %2 = catchpad within %1 [i8* null] 528 %3 = call i8* @llvm.wasm.get.exception(token %2) 529 %j = call i32 @llvm.wasm.get.ehselector(token %2) 530 catchret from %2 to label %try.cont 531 532try.cont: ; preds = %catch.start0 533 ret i32 0 534} 535 536; If not for the unwind destination mismatch, the LOOP marker here would have an 537; i32 signature. But because we add a rethrow instruction at the end of the 538; appendix block, now the LOOP marker does not have a signature (= has a void 539; signature). Here the two calls two 'bar' are supposed to throw up to the 540; caller, but incorrectly unwind to 'catch19' after linearizing the CFG. 541 542; NOSORT-LABEL: test8 543; NOSORT: block 544; NOSORT-NOT: loop i32 545; NOSORT: loop # label40: 546; NOSORT: try 547; NOSORT: call foo 548; --- Nested try/catch/end_try starts 549; NOSORT: try 550; NOSORT: call bar 551; NOSORT: call bar 552; NOSORT: catch $[[REG:[0-9]+]]= 553; NOSORT: br 1 # 1: down to label41 554; NOSORT: end_try 555; --- Nested try/catch/end_try ends 556; NOSORT: return {{.*}} 557; NOSORT: catch $drop= # catch21: 558; NOSORT: br 1 # 1: up to label40 559; NOSORT: end_try # label41: 560; NOSORT: end_loop 561; NOSORT: end_block 562; NOSORT: rethrow $[[REG]] # to caller 563 564define i32 @test8(i32* %p) personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) { 565entry: 566 store volatile i32 0, i32* %p 567 br label %loop 568 569loop: ; preds = %try.cont, %entry 570 store volatile i32 1, i32* %p 571 invoke void @foo() 572 to label %bb unwind label %catch.dispatch 573 574bb: ; preds = %loop 575 call void @bar() 576 call void @bar() 577 ret i32 0 578 579catch.dispatch: ; preds = %loop 580 %0 = catchswitch within none [label %catch.start] unwind to caller 581 582catch.start: ; preds = %catch.dispatch 583 %1 = catchpad within %0 [i8* null] 584 %2 = call i8* @llvm.wasm.get.exception(token %1) 585 %3 = call i32 @llvm.wasm.get.ehselector(token %1) 586 catchret from %1 to label %try.cont 587 588try.cont: ; preds = %catch.start 589 br label %loop 590} 591 592; When we have both kinds of EH pad unwind mismatches: 593; - A may-throw instruction unwinds to an incorrect EH pad after linearizing the 594; CFG, when it is supposed to unwind to another EH pad. 595; - A may-throw instruction unwinds to an incorrect EH pad after linearizing the 596; CFG, when it is supposed to unwind to the caller. 597 598; NOSORT-LABEL: test9 599; NOSORT: block 600; NOSORT: block 601; NOSORT: try 602; NOSORT: try 603; NOSORT: call foo 604; --- Nested try/catch/end_try starts 605; NOSORT: try 606; NOSORT: call bar 607; NOSORT: catch $[[REG0:[0-9]+]]= 608; NOSORT: br 2 # 2: down to label45 609; NOSORT: end_try 610; --- Nested try/catch/end_try ends 611; NOSORT: br 2 # 2: down to label44 612; NOSORT: catch {{.*}} 613; NOSORT: block i32 614; NOSORT: br_on_exn 0, {{.*}} # 0: down to label48 615; --- Nested try/catch/end_try starts 616; NOSORT: try 617; NOSORT: rethrow {{.*}} # down to catch26 618; NOSORT: catch $[[REG1:[0-9]+]]= # catch26: 619; NOSORT: br 5 # 5: down to label43 620; NOSORT: end_try 621; --- Nested try/catch/end_try ends 622; NOSORT: end_block # label48: 623; NOSORT: call $drop=, __cxa_begin_catch 624; --- Nested try/catch/end_try starts 625; NOSORT: try 626; NOSORT: call __cxa_end_catch 627; NOSORT: catch $[[REG1]]= 628; NOSORT: br 4 # 4: down to label43 629; NOSORT: end_try 630; --- Nested try/catch/end_try ends 631; NOSORT: br 2 # 2: down to label44 632; NOSORT: end_try 633; NOSORT: catch $[[REG0]]= 634; NOSORT: end_try # label45: 635; NOSORT: call $drop=, __cxa_begin_catch 636; NOSORT: call __cxa_end_catch 637; NOSORT: end_block # label44: 638; NOSORT: return 639; NOSORT: end_block # label43: 640; NOSORT: rethrow $[[REG1]] # to caller 641define void @test9() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) { 642bb0: 643 invoke void @foo() 644 to label %bb1 unwind label %catch.dispatch0 645 646bb1: ; preds = %bb0 647 invoke void @bar() 648 to label %try.cont unwind label %catch.dispatch1 649 650catch.dispatch0: ; preds = %bb0 651 %0 = catchswitch within none [label %catch.start0] unwind to caller 652 653catch.start0: ; preds = %catch.dispatch0 654 %1 = catchpad within %0 [i8* null] 655 %2 = call i8* @llvm.wasm.get.exception(token %1) 656 %3 = call i32 @llvm.wasm.get.ehselector(token %1) 657 %4 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ] 658 call void @__cxa_end_catch() [ "funclet"(token %1) ] 659 catchret from %1 to label %try.cont 660 661catch.dispatch1: ; preds = %bb1 662 %5 = catchswitch within none [label %catch.start1] unwind to caller 663 664catch.start1: ; preds = %catch.dispatch1 665 %6 = catchpad within %5 [i8* null] 666 %7 = call i8* @llvm.wasm.get.exception(token %6) 667 %8 = call i32 @llvm.wasm.get.ehselector(token %6) 668 %9 = call i8* @__cxa_begin_catch(i8* %7) [ "funclet"(token %6) ] 669 call void @__cxa_end_catch() [ "funclet"(token %6) ] 670 catchret from %6 to label %try.cont 671 672try.cont: ; preds = %catch.start1, %catch.start0, %bb1 673 ret void 674} 675 676; In CFGSort, EH pads should be sorted as soon as it is available and 677; 'Preferred' queue and should NOT be entered into 'Ready' queue unless we are 678; in the middle of sorting another region that does not contain the EH pad. In 679; this example, 'catch.start' should be sorted right after 'if.then' is sorted 680; (before 'cont' is sorted) and there should not be any unwind destination 681; mismatches in CFGStackify. 682 683; NOOPT: block 684; NOOPT: try 685; NOOPT: call foo 686; NOOPT: catch 687; NOOPT: end_try 688; NOOPT: call foo 689; NOOPT: end_block 690; NOOPT: return 691define void @test10(i32 %arg) personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) { 692entry: 693 %tobool = icmp ne i32 %arg, 0 694 br i1 %tobool, label %if.then, label %if.end 695 696catch.dispatch: ; preds = %if.then 697 %0 = catchswitch within none [label %catch.start] unwind to caller 698 699catch.start: ; preds = %catch.dispatch 700 %1 = catchpad within %0 [i8* null] 701 %2 = call i8* @llvm.wasm.get.exception(token %1) 702 %3 = call i32 @llvm.wasm.get.ehselector(token %1) 703 %4 = call i8* @__cxa_begin_catch(i8* %2) [ "funclet"(token %1) ] 704 call void @__cxa_end_catch() [ "funclet"(token %1) ] 705 catchret from %1 to label %if.end 706 707if.then: ; preds = %entry 708 invoke void @foo() 709 to label %cont unwind label %catch.dispatch 710 711cont: ; preds = %if.then 712 call void @foo() 713 br label %if.end 714 715if.end: ; preds = %cont, %catch.start, %entry 716 ret void 717} 718 719%class.Object = type { i8 } 720 721; Intrinsics like memcpy, memmove, and memset don't throw and are lowered into 722; calls to external symbols (not global addresses) in instruction selection, 723; which will be eventually lowered to library function calls. 724; Because this test runs with -wasm-disable-ehpad-sort, these library calls in 725; invoke.cont BB fall within try~end_try, but they shouldn't cause crashes or 726; unwinding destination mismatches in CFGStackify. 727 728; NOSORT-LABEL: test11 729; NOSORT: try 730; NOSORT: call foo 731; NOSORT: call {{.*}} memcpy 732; NOSORT: call {{.*}} memmove 733; NOSORT: call {{.*}} memset 734; NOSORT: return 735; NOSORT: catch 736; NOSORT: rethrow 737; NOSORT: end_try 738define void @test11(i8* %a, i8* %b) personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) { 739entry: 740 %o = alloca %class.Object, align 1 741 invoke void @foo() 742 to label %invoke.cont unwind label %ehcleanup 743 744invoke.cont: ; preds = %entry 745 call void @llvm.memcpy.p0i8.p0i8.i32(i8* %a, i8* %b, i32 100, i1 false) 746 call void @llvm.memmove.p0i8.p0i8.i32(i8* %a, i8* %b, i32 100, i1 false) 747 call void @llvm.memset.p0i8.i32(i8* %a, i8 0, i32 100, i1 false) 748 %call = call %class.Object* @_ZN6ObjectD2Ev(%class.Object* %o) #1 749 ret void 750 751ehcleanup: ; preds = %entry 752 %0 = cleanuppad within none [] 753 %call2 = call %class.Object* @_ZN6ObjectD2Ev(%class.Object* %o) #1 [ "funclet"(token %0) ] 754 cleanupret from %0 unwind to caller 755} 756 757; Tests if 'try' marker is placed correctly. In this test, 'try' should be 758; placed before the call to 'nothrow_i32' and not between the call to 759; 'nothrow_i32' and 'fun', because the return value of 'nothrow_i32' is 760; stackified and pushed onto the stack to be consumed by the call to 'fun'. 761 762; CHECK-LABEL: test12 763; CHECK: try 764; CHECK: call $push{{.*}}=, nothrow_i32 765; CHECK: call fun, $pop{{.*}} 766define void @test12() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) { 767entry: 768 %call = call i32 @nothrow_i32() 769 invoke void @fun(i32 %call) 770 to label %invoke.cont unwind label %terminate 771 772invoke.cont: ; preds = %entry 773 ret void 774 775terminate: ; preds = %entry 776 %0 = cleanuppad within none [] 777 %1 = tail call i8* @llvm.wasm.get.exception(token %0) 778 call void @__clang_call_terminate(i8* %1) [ "funclet"(token %0) ] 779 unreachable 780} 781 782%class.MyClass = type { i32 } 783 784; This crashed on debug mode (= when NDEBUG is not defined) when the logic for 785; computing the innermost region was not correct, in which a loop region 786; contains an exception region. This should pass CFGSort without crashing. 787define void @test13() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) { 788entry: 789 %e = alloca %class.MyClass, align 4 790 br label %for.cond 791 792for.cond: ; preds = %for.inc, %entry 793 %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.inc ] 794 %cmp = icmp slt i32 %i.0, 9 795 br i1 %cmp, label %for.body, label %for.end 796 797for.body: ; preds = %for.cond 798 invoke void @quux(i32 %i.0) 799 to label %for.inc unwind label %catch.dispatch 800 801catch.dispatch: ; preds = %for.body 802 %0 = catchswitch within none [label %catch.start] unwind to caller 803 804catch.start: ; preds = %catch.dispatch 805 %1 = catchpad within %0 [i8* bitcast ({ i8*, i8* }* @_ZTI7MyClass to i8*)] 806 %2 = call i8* @llvm.wasm.get.exception(token %1) 807 %3 = call i32 @llvm.wasm.get.ehselector(token %1) 808 %4 = call i32 @llvm.eh.typeid.for(i8* bitcast ({ i8*, i8* }* @_ZTI7MyClass to i8*)) #3 809 %matches = icmp eq i32 %3, %4 810 br i1 %matches, label %catch, label %rethrow 811 812catch: ; preds = %catch.start 813 %5 = call i8* @__cxa_get_exception_ptr(i8* %2) #3 [ "funclet"(token %1) ] 814 %6 = bitcast i8* %5 to %class.MyClass* 815 %call = call %class.MyClass* @_ZN7MyClassC2ERKS_(%class.MyClass* %e, %class.MyClass* dereferenceable(4) %6) [ "funclet"(token %1) ] 816 %7 = call i8* @__cxa_begin_catch(i8* %2) #3 [ "funclet"(token %1) ] 817 %x = getelementptr inbounds %class.MyClass, %class.MyClass* %e, i32 0, i32 0 818 %8 = load i32, i32* %x, align 4 819 invoke void @quux(i32 %8) [ "funclet"(token %1) ] 820 to label %invoke.cont2 unwind label %ehcleanup 821 822invoke.cont2: ; preds = %catch 823 %call3 = call %class.MyClass* @_ZN7MyClassD2Ev(%class.MyClass* %e) #3 [ "funclet"(token %1) ] 824 call void @__cxa_end_catch() [ "funclet"(token %1) ] 825 catchret from %1 to label %for.inc 826 827rethrow: ; preds = %catch.start 828 call void @llvm.wasm.rethrow.in.catch() #6 [ "funclet"(token %1) ] 829 unreachable 830 831for.inc: ; preds = %invoke.cont2, %for.body 832 %inc = add nsw i32 %i.0, 1 833 br label %for.cond 834 835ehcleanup: ; preds = %catch 836 %9 = cleanuppad within %1 [] 837 %call4 = call %class.MyClass* @_ZN7MyClassD2Ev(%class.MyClass* %e) #3 [ "funclet"(token %9) ] 838 invoke void @__cxa_end_catch() [ "funclet"(token %9) ] 839 to label %invoke.cont6 unwind label %terminate7 840 841invoke.cont6: ; preds = %ehcleanup 842 cleanupret from %9 unwind to caller 843 844for.end: ; preds = %for.cond 845 ret void 846 847terminate7: ; preds = %ehcleanup 848 %10 = cleanuppad within %9 [] 849 %11 = call i8* @llvm.wasm.get.exception(token %10) 850 call void @__clang_call_terminate(i8* %11) #7 [ "funclet"(token %10) ] 851 unreachable 852} 853 854; We don't need to call placeBlockMarker after fixUnwindMismatches unless the 855; destination is the appendix BB at the very end. This should not crash. 856define void @test16(i32* %p, i32 %a, i32 %b) personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) { 857entry: 858 br label %loop 859 860loop: ; preds = %try.cont, %entry 861 invoke void @foo() 862 to label %bb0 unwind label %catch.dispatch0 863 864bb0: ; preds = %loop 865 %cmp = icmp ne i32 %a, %b 866 br i1 %cmp, label %bb1, label %last 867 868bb1: ; preds = %bb0 869 invoke void @bar() 870 to label %try.cont unwind label %catch.dispatch1 871 872catch.dispatch0: ; preds = %loop 873 %0 = catchswitch within none [label %catch.start0] unwind to caller 874 875catch.start0: ; preds = %catch.dispatch0 876 %1 = catchpad within %0 [i8* null] 877 %2 = call i8* @llvm.wasm.get.exception(token %1) 878 %3 = call i32 @llvm.wasm.get.ehselector(token %1) 879 catchret from %1 to label %try.cont 880 881catch.dispatch1: ; preds = %bb1 882 %4 = catchswitch within none [label %catch.start1] unwind to caller 883 884catch.start1: ; preds = %catch.dispatch1 885 %5 = catchpad within %4 [i8* null] 886 %6 = call i8* @llvm.wasm.get.exception(token %5) 887 %7 = call i32 @llvm.wasm.get.ehselector(token %5) 888 catchret from %5 to label %try.cont 889 890try.cont: ; preds = %catch.start1, %catch.start0, %bb1 891 br label %loop 892 893last: ; preds = %bb0 894 ret void 895} 896 897; Tests if CFGStackify's removeUnnecessaryInstrs() removes unnecessary branches 898; correctly. 899; CHECK-LABEL: test17 900define void @test17(i32 %n) personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) { 901entry: 902 invoke void @foo() 903 to label %for.body unwind label %catch.dispatch 904 905for.body: ; preds = %for.end, %entry 906 %i = phi i32 [ %inc, %for.end ], [ 0, %entry ] 907 invoke void @foo() 908 to label %for.end unwind label %catch.dispatch 909 910; Before going to CFGStackify, this BB will have a conditional branch followed 911; by an unconditional branch. CFGStackify should remove only the unconditional 912; one. 913for.end: ; preds = %for.body 914 %inc = add nuw nsw i32 %i, 1 915 %exitcond = icmp eq i32 %inc, %n 916 br i1 %exitcond, label %try.cont, label %for.body 917; CHECK: br_if 918; CHECK-NOT: br 919; CHECK: end_loop 920; CHECK: catch 921 922catch.dispatch: ; preds = %for.body, %entry 923 %0 = catchswitch within none [label %catch.start] unwind to caller 924 925catch.start: ; preds = %catch.dispatch 926 %1 = catchpad within %0 [i8* null] 927 %2 = call i8* @llvm.wasm.get.exception(token %1) 928 %3 = call i32 @llvm.wasm.get.ehselector(token %1) 929 %4 = call i8* @__cxa_begin_catch(i8* %2) #2 [ "funclet"(token %1) ] 930 call void @__cxa_end_catch() [ "funclet"(token %1) ] 931 catchret from %1 to label %try.cont 932 933try.cont: ; preds = %catch.start, %for.end 934 ret void 935} 936 937; Check if the unwind destination mismatch stats are correct 938; NOSORT-STAT: 16 wasm-cfg-stackify - Number of EH pad unwind mismatches found 939 940declare void @foo() 941declare void @bar() 942declare i32 @baz() 943declare i32 @qux(i32) 944declare void @quux(i32) 945declare void @fun(i32) 946; Function Attrs: nounwind 947declare void @nothrow(i32) #0 948declare i32 @nothrow_i32() #0 949 950; Function Attrs: nounwind 951declare %class.Object* @_ZN6ObjectD2Ev(%class.Object* returned) #0 952@_ZTI7MyClass = external constant { i8*, i8* }, align 4 953; Function Attrs: nounwind 954declare %class.MyClass* @_ZN7MyClassD2Ev(%class.MyClass* returned) #0 955; Function Attrs: nounwind 956declare %class.MyClass* @_ZN7MyClassC2ERKS_(%class.MyClass* returned, %class.MyClass* dereferenceable(4)) #0 957 958declare i32 @__gxx_wasm_personality_v0(...) 959declare i8* @llvm.wasm.get.exception(token) 960declare i32 @llvm.wasm.get.ehselector(token) 961declare void @llvm.wasm.rethrow.in.catch() 962declare i32 @llvm.eh.typeid.for(i8*) 963declare i8* @__cxa_begin_catch(i8*) 964declare void @__cxa_end_catch() 965declare i8* @__cxa_get_exception_ptr(i8*) 966declare void @__clang_call_terminate(i8*) 967declare void @_ZSt9terminatev() 968; Function Attrs: nounwind 969declare void @llvm.memcpy.p0i8.p0i8.i32(i8* noalias nocapture writeonly, i8* noalias nocapture readonly, i32, i1 immarg) #0 970; Function Attrs: nounwind 971declare void @llvm.memmove.p0i8.p0i8.i32(i8* nocapture, i8* nocapture readonly, i32, i1 immarg) #0 972; Function Attrs: nounwind 973declare void @llvm.memset.p0i8.i32(i8* nocapture writeonly, i8, i32, i1 immarg) #0 974 975attributes #0 = { nounwind } 976