Index: lib/Target/WebAssembly/WebAssemblyInstrControl.td =================================================================== --- lib/Target/WebAssembly/WebAssemblyInstrControl.td +++ lib/Target/WebAssembly/WebAssemblyInstrControl.td @@ -18,6 +18,9 @@ def BR_IF : I<(outs), (ins I32:$a, bb_op:$dst), [(brcond I32:$a, bb:$dst)], "br_if \t$a, $dst">; +let isCodeGenOnly = 1 in +def BR_UNLESS : I<(outs), (ins I32:$a, bb_op:$dst), [], + "br_unless\t$a, $dst">; let isBarrier = 1 in { def BR : I<(outs), (ins bb_op:$dst), [(br bb:$dst)], @@ -25,6 +28,15 @@ } // isBarrier = 1 } // isBranch = 1, isTerminator = 1, hasCtrlDep = 1 +} // Defs = [ARGUMENTS] + +def : Pat<(brcond (i32 (setne I32:$a, 0)), bb:$dst), + (BR_IF I32:$a, bb_op:$dst)>; +def : Pat<(brcond (i32 (seteq I32:$a, 0)), bb:$dst), + (BR_UNLESS I32:$a, bb_op:$dst)>; + +let Defs = [ARGUMENTS] in { + // TODO: SelectionDAG's lowering insists on using a pointer as the index for // jump tables, so in practice we don't ever use TABLESWITCH_I64 in wasm32 mode // currently. Index: lib/Target/WebAssembly/WebAssemblyInstrInfo.cpp =================================================================== --- lib/Target/WebAssembly/WebAssemblyInstrInfo.cpp +++ lib/Target/WebAssembly/WebAssemblyInstrInfo.cpp @@ -69,6 +69,15 @@ case WebAssembly::BR_IF: if (HaveCond) return true; + Cond.push_back(MachineOperand::CreateImm(true)); + Cond.push_back(MI.getOperand(0)); + TBB = MI.getOperand(1).getMBB(); + HaveCond = true; + break; + case WebAssembly::BR_UNLESS: + if (HaveCond) + return true; + Cond.push_back(MachineOperand::CreateImm(false)); Cond.push_back(MI.getOperand(0)); TBB = MI.getOperand(1).getMBB(); HaveCond = true; @@ -109,8 +118,6 @@ unsigned WebAssemblyInstrInfo::InsertBranch( MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, ArrayRef Cond, DebugLoc DL) const { - assert(Cond.size() <= 1); - if (Cond.empty()) { if (!TBB) return 0; @@ -119,9 +126,17 @@ return 1; } - BuildMI(&MBB, DL, get(WebAssembly::BR_IF)) - .addOperand(Cond[0]) - .addMBB(TBB); + assert(Cond.size() == 2); + + if (Cond[0].getImm()) { + BuildMI(&MBB, DL, get(WebAssembly::BR_IF)) + .addOperand(Cond[1]) + .addMBB(TBB); + } else { + BuildMI(&MBB, DL, get(WebAssembly::BR_UNLESS)) + .addOperand(Cond[1]) + .addMBB(TBB); + } if (!FBB) return 1; @@ -131,10 +146,7 @@ bool WebAssemblyInstrInfo::ReverseBranchCondition( SmallVectorImpl &Cond) const { - assert(Cond.size() == 1); - - // TODO: Add branch reversal here... And re-enable MachineBlockPlacementID - // when we do. - - return true; + assert(Cond.size() == 2); + Cond.front() = MachineOperand::CreateImm(!Cond.front().getImm()); + return false; } Index: lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp =================================================================== --- lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp +++ lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp @@ -176,10 +176,6 @@ // Fails with: should be run after register allocation. disablePass(&MachineCopyPropagationID); - // TODO: Until we get ReverseBranchCondition support, MachineBlockPlacement - // can create ugly-looking control flow. - disablePass(&MachineBlockPlacementID); - // Run the register coloring pass to reduce the total number of registers. addPass(createWebAssemblyRegColoring()); } Index: test/CodeGen/WebAssembly/cfg-stackify.ll =================================================================== --- test/CodeGen/WebAssembly/cfg-stackify.ll +++ test/CodeGen/WebAssembly/cfg-stackify.ll @@ -1,4 +1,5 @@ -; RUN: llc < %s -asm-verbose=false | FileCheck %s +; RUN: llc < %s -asm-verbose=false -disable-block-placement | FileCheck %s +; RUN: llc < %s -asm-verbose=false | FileCheck -check-prefix=OPT %s ; Test the CFG stackifier pass. @@ -12,10 +13,21 @@ ; CHECK-LABEL: test0: ; CHECK: loop ; CHECK: i32.add -; CHECK: br_if +; CHECK-NOT: br +; CHECK: br_unless +; CHECK-NOT: br ; CHECK: call ; CHECK: br BB0_1{{$}} ; CHECK: return{{$}} +; OPT-LABEL: test0: +; OPT: loop +; OPT: i32.add +; OPT-NOT: br +; OPT: br_unless +; OPT-NOT: br +; OPT: call +; OPT: br BB0_1{{$}} +; OPT: return{{$}} define void @test0(i32 %n) { entry: br label %header @@ -40,10 +52,21 @@ ; CHECK-LABEL: test1: ; CHECK: loop ; CHECK: i32.add -; CHECK: br_if +; CHECK-NOT: br +; CHECK: br_unless +; CHECK-NOT: br ; CHECK: call ; CHECK: br BB1_1{{$}} ; CHECK: return{{$}} +; OPT-LABEL: test1: +; OPT: loop +; OPT: i32.add +; OPT-NOT: br +; OPT: br_unless +; OPT-NOT: br +; OPT: call +; OPT: br BB1_1{{$}} +; OPT: return{{$}} define void @test1(i32 %n) { entry: br label %header @@ -69,9 +92,16 @@ ; CHECK: block BB2_2{{$}} ; CHECK: br_if {{.*}}, BB2_2{{$}} ; CHECK: BB2_1: -; CHECK: br_if $pop{{[0-9]+}}, BB2_1{{$}} +; CHECK: br_if ${{[0-9]+}}, BB2_1{{$}} ; CHECK: BB2_2: ; CHECK: return{{$}} +; OPT-LABEL: test2: +; OPT: block BB2_2{{$}} +; OPT: br_if {{.*}}, BB2_2{{$}} +; OPT: BB2_1: +; OPT: br_if ${{[0-9]+}}, BB2_1{{$}} +; OPT: BB2_2: +; OPT: return{{$}} define void @test2(double* nocapture %p, i32 %n) { entry: %cmp.4 = icmp sgt i32 %n, 0 @@ -100,13 +130,23 @@ ; CHECK-LABEL: doublediamond: ; CHECK: block BB3_5{{$}} ; CHECK: block BB3_2{{$}} -; CHECK: br_if $pop{{[0-9]+}}, BB3_2{{$}} +; CHECK: br_if $0, BB3_2{{$}} ; CHECK: block BB3_4{{$}} -; CHECK: br_if $pop{{[0-9]+}}, BB3_4{{$}} +; CHECK: br_if $1, BB3_4{{$}} ; CHECK: br BB3_5{{$}} ; CHECK: BB3_4: ; CHECK: BB3_5: ; CHECK: return ${{[0-9]+}}{{$}} +; OPT-LABEL: doublediamond: +; OPT: block BB3_5{{$}} +; OPT: block BB3_4{{$}} +; OPT: br_unless $0, BB3_4{{$}} +; OPT: block BB3_3{{$}} +; OPT: br_unless $1, BB3_3{{$}} +; OPT: br BB3_5{{$}} +; OPT: BB3_4: +; OPT: BB3_5: +; OPT: return ${{[0-9]+}}{{$}} define i32 @doublediamond(i32 %a, i32 %b, i32* %p) { entry: %c = icmp eq i32 %a, 0 @@ -132,9 +172,14 @@ ; CHECK-LABEL: triangle: ; CHECK: block BB4_2{{$}} -; CHECK: br_if $pop{{[0-9]+}}, BB4_2{{$}} +; CHECK: br_if $1, BB4_2{{$}} ; CHECK: BB4_2: ; CHECK: return ${{[0-9]+}}{{$}} +; OPT-LABEL: triangle: +; OPT: block BB4_2{{$}} +; OPT: br_if $1, BB4_2{{$}} +; OPT: BB4_2: +; OPT: return ${{[0-9]+}}{{$}} define i32 @triangle(i32* %p, i32 %a) { entry: %c = icmp eq i32 %a, 0 @@ -151,11 +196,19 @@ ; CHECK-LABEL: diamond: ; CHECK: block BB5_3{{$}} ; CHECK: block BB5_2{{$}} -; CHECK: br_if $pop{{[0-9]+}}, BB5_2{{$}} +; CHECK: br_if $1, BB5_2{{$}} ; CHECK: br BB5_3{{$}} ; CHECK: BB5_2: ; CHECK: BB5_3: ; CHECK: return ${{[0-9]+}}{{$}} +; OPT-LABEL: diamond: +; OPT: block BB5_3{{$}} +; OPT: block BB5_2{{$}} +; OPT: br_unless $1, BB5_2{{$}} +; OPT: br BB5_3{{$}} +; OPT: BB5_2: +; OPT: BB5_3: +; OPT: return ${{[0-9]+}}{{$}} define i32 @diamond(i32* %p, i32 %a) { entry: %c = icmp eq i32 %a, 0 @@ -175,6 +228,9 @@ ; CHECK-LABEL: single_block: ; CHECK-NOT: br ; CHECK: return $pop{{[0-9]+}}{{$}} +; OPT-LABEL: single_block: +; OPT-NOT: br +; OPT: return $pop{{[0-9]+}}{{$}} define i32 @single_block(i32* %p) { entry: store volatile i32 0, i32* %p @@ -187,6 +243,12 @@ ; CHECK: i32.store $discard=, $0, $pop{{[0-9]+}}{{$}} ; CHECK: br BB7_1{{$}} ; CHECK: BB7_2: +; OPT-LABEL: minimal_loop: +; OPT-NOT: br +; OPT: BB7_1: +; OPT: i32.store $discard=, $0, $pop{{[0-9]+}}{{$}} +; OPT: br BB7_1{{$}} +; OPT: BB7_2: define i32 @minimal_loop(i32* %p) { entry: store volatile i32 0, i32* %p @@ -200,9 +262,16 @@ ; CHECK-NOT: br ; CHECK: BB8_1: ; CHECK: loop BB8_2{{$}} -; CHECK: br_if $pop{{[0-9]+}}, BB8_1{{$}} +; CHECK: br_unless $1, BB8_1{{$}} ; CHECK: BB8_2: ; CHECK: return ${{[0-9]+}}{{$}} +; OPT-LABEL: simple_loop: +; OPT-NOT: br +; OPT: BB8_1: +; OPT: loop BB8_2{{$}} +; OPT: br_unless $1, BB8_1{{$}} +; OPT: BB8_2: +; OPT: return ${{[0-9]+}}{{$}} define i32 @simple_loop(i32* %p, i32 %a) { entry: %c = icmp eq i32 %a, 0 @@ -218,12 +287,20 @@ ; CHECK-LABEL: doubletriangle: ; CHECK: block BB9_4{{$}} -; CHECK: br_if $pop{{[0-9]+}}, BB9_4{{$}} +; CHECK: br_if $0, BB9_4{{$}} ; CHECK: block BB9_3{{$}} -; CHECK: br_if $pop{{[0-9]+}}, BB9_3{{$}} +; CHECK: br_if $1, BB9_3{{$}} ; CHECK: BB9_3: ; CHECK: BB9_4: ; CHECK: return ${{[0-9]+}}{{$}} +; OPT-LABEL: doubletriangle: +; OPT: block BB9_4{{$}} +; OPT: br_if $0, BB9_4{{$}} +; OPT: block BB9_3{{$}} +; OPT: br_if $1, BB9_3{{$}} +; OPT: BB9_3: +; OPT: BB9_4: +; OPT: return ${{[0-9]+}}{{$}} define i32 @doubletriangle(i32 %a, i32 %b, i32* %p) { entry: %c = icmp eq i32 %a, 0 @@ -247,12 +324,21 @@ ; CHECK-LABEL: ifelse_earlyexits: ; CHECK: block BB10_4{{$}} ; CHECK: block BB10_2{{$}} -; CHECK: br_if $pop{{[0-9]+}}, BB10_2{{$}} +; CHECK: br_if $0, BB10_2{{$}} ; CHECK: br BB10_4{{$}} ; CHECK: BB10_2: -; CHECK: br_if $pop{{[0-9]+}}, BB10_4{{$}} +; CHECK: br_if $1, BB10_4{{$}} ; CHECK: BB10_4: ; CHECK: return ${{[0-9]+}}{{$}} +; OPT-LABEL: ifelse_earlyexits: +; OPT: block BB10_4{{$}} +; OPT: block BB10_3{{$}} +; OPT: br_unless $0, BB10_3{{$}} +; OPT: br_if $1, BB10_4{{$}} +; OPT: br BB10_4{{$}} +; OPT: BB10_3: +; OPT: BB10_4: +; OPT: return ${{[0-9]+}}{{$}} define i32 @ifelse_earlyexits(i32 %a, i32 %b, i32* %p) { entry: %c = icmp eq i32 %a, 0 @@ -278,16 +364,31 @@ ; CHECK: loop BB11_7{{$}} ; CHECK: block BB11_6{{$}} ; CHECK: block BB11_3{{$}} -; CHECK: br_if $pop{{.*}}, BB11_3{{$}} +; CHECK: br_if $0, BB11_3{{$}} ; CHECK: br BB11_6{{$}} ; CHECK: BB11_3: ; CHECK: block BB11_5{{$}} -; CHECK: br_if $pop{{.*}}, BB11_5{{$}} +; CHECK: br_if $1, BB11_5{{$}} ; CHECK: br BB11_6{{$}} ; CHECK: BB11_5: ; CHECK: BB11_6: ; CHECK: br BB11_1{{$}} ; CHECK: BB11_7: +; OPT-LABEL: doublediamond_in_a_loop: +; OPT: BB11_1: +; OPT: loop BB11_7{{$}} +; OPT: block BB11_6{{$}} +; OPT: block BB11_5{{$}} +; OPT: br_unless $0, BB11_5{{$}} +; OPT: block BB11_4{{$}} +; OPT: br_unless $1, BB11_4{{$}} +; OPT: br BB11_6{{$}} +; OPT: BB11_4: +; OPT: br BB11_6{{$}} +; OPT: BB11_5: +; OPT: BB11_6: +; OPT: br BB11_1{{$}} +; OPT: BB11_7: define i32 @doublediamond_in_a_loop(i32 %a, i32 %b, i32* %p) { entry: br label %header Index: test/CodeGen/WebAssembly/switch.ll =================================================================== --- test/CodeGen/WebAssembly/switch.ll +++ test/CodeGen/WebAssembly/switch.ll @@ -1,6 +1,7 @@ -; RUN: llc < %s -asm-verbose=false | FileCheck %s +; RUN: llc < %s -asm-verbose=false -disable-block-placement | FileCheck %s -; Test switch instructions. +; Test switch instructions. Block placement is disabled because it reorders +; the blocks in a way that isn't interesting here. target datalayout = "e-p:32:32-i64:64-n32:64-S128" target triple = "wasm32-unknown-unknown"