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