Index: lib/Target/WebAssembly/WebAssemblyISelLowering.cpp =================================================================== --- lib/Target/WebAssembly/WebAssemblyISelLowering.cpp +++ lib/Target/WebAssembly/WebAssemblyISelLowering.cpp @@ -270,6 +270,11 @@ MaxStoresPerMemset = 1; MaxStoresPerMemsetOptSize = 1; } + + // Always convert switches to br_tables unless there is only one case, which + // is equivalent to a simple branch. This reduces code size for wasm, and we + // defer possible jump table optimizations to the VM. + setMinimumJumpTableEntries(2); } TargetLowering::AtomicExpansionKind Index: test/CodeGen/WebAssembly/cfg-stackify.ll =================================================================== --- test/CodeGen/WebAssembly/cfg-stackify.ll +++ test/CodeGen/WebAssembly/cfg-stackify.ll @@ -384,18 +384,12 @@ ; CHECK-NEXT: .functype test4 (i32) -> (){{$}} ; CHECK: block {{$}} ; CHECK-NEXT: block {{$}} -; CHECK: br_if 0, $pop{{[0-9]+}}{{$}} -; CHECK: br_if 1, $pop{{[0-9]+}}{{$}} -; CHECK: br 1{{$}} -; CHECK-NEXT: .LBB{{[0-9]+}}_3: -; CHECK-NEXT: end_block{{$}} -; CHECK-NEXT: block {{$}} -; CHECK: br_if 0, $pop{{[0-9]+}}{{$}} -; CHECK: br_if 1, $pop{{[0-9]+}}{{$}} -; CHECK-NEXT: .LBB{{[0-9]+}}_5: +; CHECK: br_if 0, $pop{{[0-9]+}}{{$}} +; CHECK: br 1{{$}} +; CHECK-NEXT: .LBB{{[0-9]+}}_2: ; CHECK-NEXT: end_block{{$}} -; CHECK-NEXT: return{{$}} -; CHECK-NEXT: .LBB{{[0-9]+}}_6: +; CHECK-NEXT: br_table $0, 0, 0, 0, 0, 0, 0{{$}} +; CHECK-NEXT: .LBB{{[0-9]+}}_3: ; CHECK-NEXT: end_block{{$}} ; CHECK-NEXT: return{{$}} define void @test4(i32 %t) { @@ -646,29 +640,30 @@ ; CHECK-LABEL: test10: ; CHECK: .LBB{{[0-9]+}}_1: ; CHECK-NEXT: loop {{$}} -; CHECK-NOT: block -; CHECK: br_if 0, {{[^,]+}}{{$}} +; CHECK: br_if 0, {{[^,]+}}{{$}} ; CHECK: .LBB{{[0-9]+}}_3: ; CHECK-NEXT: block {{$}} ; CHECK-NEXT: loop {{$}} -; CHECK-NOT: block ; CHECK: .LBB{{[0-9]+}}_4: ; CHECK-NEXT: loop {{$}} -; CHECK-NOT: block -; CHECK: br_if 0, {{[^,]+}}{{$}} +; CHECK: br_if 0, {{[^,]+}}{{$}} ; CHECK-NEXT: end_loop{{$}} -; CHECK: br_if 1, {{[^,]+}}{{$}} -; CHECK-NOT: block -; CHECK: br_if 0, {{[^,]+}}{{$}} +; CHECK-NEXT: block {{$}} +; CHECK: br_if 0, {{[^,]+}}{{$}} +; CHECK: br 3{{$}} +; CHECK-NEXT: .LBB{{[0-9]+}}_7: +; CHECK-NEXT: end_block{{$}} +; CHECK: block {{$}} +; CHECK-NEXT: br_table $0, 0, 3, 1, 2, 0 +; CHECK-NEXT: .LBB{{[0-9]+}}_8: +; CHECK-NEXT: end_block{{$}} ; CHECK-NEXT: end_loop{{$}} -; CHECK-NOT: block -; CHECK: br_if 1, {{[^,]+}}{{$}} ; CHECK-NEXT: return{{$}} ; CHECK-NEXT: .LBB{{[0-9]+}}_9: ; CHECK-NEXT: end_block{{$}} -; CHECK-NOT: block -; CHECK: br 0{{$}} +; CHECK: br 0{{$}} ; CHECK-NEXT: .LBB{{[0-9]+}}_10: +; CHECK-NEXT: end_loop{{$}} define void @test10() { bb0: br label %bb1 @@ -772,31 +767,25 @@ ; CHECK-LABEL: test12: ; CHECK: .LBB{{[0-9]+}}_1: -; CHECK-NEXT: block {{$}} ; CHECK-NEXT: loop {{$}} -; CHECK-NOT: block -; CHECK: block {{$}} ; CHECK-NEXT: block {{$}} -; CHECK: br_if 0, {{[^,]+}}{{$}} -; CHECK-NOT: block -; CHECK: br_if 1, {{[^,]+}}{{$}} -; CHECK-NOT: block -; CHECK: br_if 1, {{[^,]+}}{{$}} -; CHECK-NEXT: br 3{{$}} +; CHECK-NEXT: block {{$}} +; CHECK-NEXT: block {{$}} +; CHECK: br_if 0, {{[^,]+}}{{$}} +; CHECK: br_if 2, {{[^,]+}}{{$}} +; CHECK: br_if 1, {{[^,]+}}{{$}} +; CHECK-NEXT: br 2{{$}} ; CHECK-NEXT: .LBB{{[0-9]+}}_4: ; CHECK-NEXT: end_block{{$}} -; CHECK-NOT: block -; CHECK: br_if 0, {{[^,]+}}{{$}} -; CHECK-NOT: block -; CHECK: br_if 2, {{[^,]+}}{{$}} +; CHECK-NEXT: br_table $2, 1, 0, 0, 0, 1, 1{{$}} +; CHECK-NEXT: .LBB{{[0-9]+}}_5: +; CHECK-NEXT: end_block{{$}} +; CHECK-NEXT: return{{$}} ; CHECK-NEXT: .LBB{{[0-9]+}}_6: ; CHECK-NEXT: end_block{{$}} -; CHECK-NOT: block -; CHECK: br 0{{$}} +; CHECK: br 0{{$}} ; CHECK-NEXT: .LBB{{[0-9]+}}_7: ; CHECK-NEXT: end_loop{{$}} -; CHECK-NEXT: end_block{{$}} -; CHECK-NEXT: return{{$}} define void @test12(i8* %arg) { bb: br label %bb1