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