Skip to content

Commit

Permalink
[WebAssembly] Remove loop's bottom label.
Browse files Browse the repository at this point in the history
Per spec changes, loop constructs no longer have a bottom label.

https://reviews.llvm.org/D25118


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@283502 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Dan Gohman committed Oct 6, 2016
1 parent a5e77a0 commit d8d953e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 43 deletions.
9 changes: 2 additions & 7 deletions lib/Target/WebAssembly/InstPrinter/WebAssemblyInstPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,15 @@ void WebAssemblyInstPrinter::printInst(const MCInst *MI, raw_ostream &OS,
default:
break;
case WebAssembly::LOOP: {
// Grab the TopLabel value first so that labels print in numeric order.
uint64_t TopLabel = ControlFlowCounter++;
ControlFlowStack.push_back(std::make_pair(ControlFlowCounter++, false));
printAnnotation(OS, "label" + utostr(TopLabel) + ':');
ControlFlowStack.push_back(std::make_pair(TopLabel, true));
printAnnotation(OS, "label" + utostr(ControlFlowCounter) + ':');
ControlFlowStack.push_back(std::make_pair(ControlFlowCounter++, true));
break;
}
case WebAssembly::BLOCK:
ControlFlowStack.push_back(std::make_pair(ControlFlowCounter++, false));
break;
case WebAssembly::END_LOOP:
ControlFlowStack.pop_back();
printAnnotation(
OS, "label" + utostr(ControlFlowStack.pop_back_val().first) + ':');
break;
case WebAssembly::END_BLOCK:
printAnnotation(
Expand Down
33 changes: 14 additions & 19 deletions lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,14 @@ static bool IsChild(const MachineInstr &MI,
}

/// Insert a BLOCK marker for branches to MBB (if needed).
static void PlaceBlockMarker(MachineBasicBlock &MBB, MachineFunction &MF,
SmallVectorImpl<MachineBasicBlock *> &ScopeTops,
const WebAssemblyInstrInfo &TII,
const MachineLoopInfo &MLI,
MachineDominatorTree &MDT,
WebAssemblyFunctionInfo &MFI) {
static void PlaceBlockMarker(
MachineBasicBlock &MBB, MachineFunction &MF,
SmallVectorImpl<MachineBasicBlock *> &ScopeTops,
DenseMap<const MachineInstr *, const MachineBasicBlock *> &LoopTops,
const WebAssemblyInstrInfo &TII,
const MachineLoopInfo &MLI,
MachineDominatorTree &MDT,
WebAssemblyFunctionInfo &MFI) {
// First compute the nearest common dominator of all forward non-fallthrough
// predecessors so that we minimize the time that the BLOCK is on the stack,
// which reduces overall stack height.
Expand Down Expand Up @@ -347,21 +349,15 @@ static void PlaceBlockMarker(MachineBasicBlock &MBB, MachineFunction &MF,
}
}

// If there's a loop which ends just before MBB which contains Header, we can
// reuse its label instead of inserting a new BLOCK.
for (MachineLoop *Loop = MLI.getLoopFor(LayoutPred);
Loop && Loop->contains(LayoutPred); Loop = Loop->getParentLoop())
if (Loop && LoopBottom(Loop) == LayoutPred && Loop->contains(Header))
return;

// Decide where in Header to put the BLOCK.
MachineBasicBlock::iterator InsertPos;
MachineLoop *HeaderLoop = MLI.getLoopFor(Header);
if (HeaderLoop && MBB.getNumber() > LoopBottom(HeaderLoop)->getNumber()) {
// Header is the header of a loop that does not lexically contain MBB, so
// the BLOCK needs to be above the LOOP, after any END constructs.
InsertPos = Header->begin();
while (InsertPos->getOpcode() != WebAssembly::LOOP)
while (InsertPos->getOpcode() == WebAssembly::END_BLOCK ||
InsertPos->getOpcode() == WebAssembly::END_LOOP)
++InsertPos;
} else {
// Otherwise, insert the BLOCK as late in Header as we can, but before the
Expand All @@ -381,7 +377,8 @@ static void PlaceBlockMarker(MachineBasicBlock &MBB, MachineFunction &MF,
// Mark the end of the block.
InsertPos = MBB.begin();
while (InsertPos != MBB.end() &&
InsertPos->getOpcode() == WebAssembly::END_LOOP)
InsertPos->getOpcode() == WebAssembly::END_LOOP &&
LoopTops[&*InsertPos]->getNumber() >= Header->getNumber())
++InsertPos;
BuildMI(MBB, InsertPos, DebugLoc(), TII.get(WebAssembly::END_BLOCK));

Expand Down Expand Up @@ -468,7 +465,7 @@ static void PlaceMarkers(MachineFunction &MF, const MachineLoopInfo &MLI,
PlaceLoopMarker(MBB, MF, ScopeTops, LoopTops, TII, MLI);

// Place the BLOCK for MBB if MBB is branched to from above.
PlaceBlockMarker(MBB, MF, ScopeTops, TII, MLI, MDT, MFI);
PlaceBlockMarker(MBB, MF, ScopeTops, LoopTops, TII, MLI, MDT, MFI);
}

// Now rewrite references to basic blocks to be depth immediates.
Expand All @@ -477,20 +474,18 @@ static void PlaceMarkers(MachineFunction &MF, const MachineLoopInfo &MLI,
for (auto &MI : reverse(MBB)) {
switch (MI.getOpcode()) {
case WebAssembly::BLOCK:
assert(ScopeTops[Stack.back()->getNumber()] == &MBB &&
assert(ScopeTops[Stack.back()->getNumber()]->getNumber() <= MBB.getNumber() &&
"Block should be balanced");
Stack.pop_back();
break;
case WebAssembly::LOOP:
assert(Stack.back() == &MBB && "Loop top should be balanced");
Stack.pop_back();
Stack.pop_back();
break;
case WebAssembly::END_BLOCK:
Stack.push_back(&MBB);
break;
case WebAssembly::END_LOOP:
Stack.push_back(&MBB);
Stack.push_back(LoopTops[&MI]);
break;
default:
Expand Down
48 changes: 31 additions & 17 deletions test/CodeGen/WebAssembly/cfg-stackify.ll
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ default:
; CHECK: .LBB14_1:
; CHECK-NEXT: block{{$}}
; CHECK-NEXT: loop{{$}}
; CHECK: br_if 2, {{[^,]+}}{{$}}
; CHECK: br_if 1, {{[^,]+}}{{$}}
; CHECK: br_if 0, {{[^,]+}}{{$}}
; CHECK-NEXT: end_loop{{$}}
; CHECK: return{{$}}
Expand All @@ -586,7 +586,7 @@ default:
; OPT: .LBB14_1:
; OPT-NEXT: block{{$}}
; OPT-NEXT: loop{{$}}
; OPT: br_if 2, {{[^,]+}}{{$}}
; OPT: br_if 1, {{[^,]+}}{{$}}
; OPT: br_if 0, {{[^,]+}}{{$}}
; OPT-NEXT: end_loop{{$}}
; OPT: return{{$}}
Expand Down Expand Up @@ -623,10 +623,10 @@ return:
; CHECK-NEXT: block{{$}}
; CHECK-NEXT: loop{{$}}
; CHECK-NOT: block
; CHECK: br_if 3, {{[^,]+}}{{$}}
; CHECK-NOT: block
; CHECK: br_if 2, {{[^,]+}}{{$}}
; CHECK-NOT: block
; CHECK: br_if 1, {{[^,]+}}{{$}}
; CHECK-NOT: block
; CHECK: br_if 0, {{[^,]+}}{{$}}
; CHECK-NEXT: end_loop{{$}}
; CHECK-NOT: block
Expand All @@ -644,10 +644,10 @@ return:
; OPT-NEXT: block{{$}}
; OPT-NEXT: loop{{$}}
; OPT-NOT: block
; OPT: br_if 3, {{[^,]+}}{{$}}
; OPT-NOT: block
; OPT: br_if 2, {{[^,]+}}{{$}}
; OPT-NOT: block
; OPT: br_if 1, {{[^,]+}}{{$}}
; OPT-NOT: block
; OPT: br_if 0, {{[^,]+}}{{$}}
; OPT-NEXT: end_loop{{$}}
; OPT-NOT: block
Expand Down Expand Up @@ -718,7 +718,7 @@ second:
; OPT: br_if 0, {{[^,]+}}{{$}}
; OPT-NOT: block
; OPT: br_if 1, {{[^,]+}}{{$}}
; OPT: br 3{{$}}
; OPT: br 2{{$}}
; OPT-NEXT: .LBB16_3:
; OPT-NEXT: end_block
; OPT-NOT: block
Expand Down Expand Up @@ -796,6 +796,7 @@ bb3:

; CHECK-LABEL: test9:
; CHECK: .LBB18_1:
; CHECK-NEXT: block{{$}}
; CHECK-NEXT: loop{{$}}
; CHECK-NOT: block
; CHECK: br_if 1, {{[^,]+}}{{$}}
Expand All @@ -806,18 +807,21 @@ bb3:
; CHECK-NOT: block
; CHECK: br_if 0, {{[^,]+}}{{$}}
; CHECK-NOT: block
; CHECK: br_if 3, {{[^,]+}}{{$}}
; CHECK: br_if 2, {{[^,]+}}{{$}}
; CHECK-NEXT: br 1{{$}}
; CHECK-NEXT: .LBB18_4:
; CHECK-NEXT: end_block{{$}}
; CHECK-NOT: block
; CHECK: br_if 2, {{[^,]+}}{{$}}
; CHECK: br_if 1, {{[^,]+}}{{$}}
; CHECK-NEXT: br 0{{$}}
; CHECK-NEXT: .LBB18_5:
; CHECK-NOT: block
; CHECK: end_block
; CHECK-NOT: block
; CHECK: return{{$}}
; OPT-LABEL: test9:
; OPT: .LBB18_1:
; OPT-NEXT: block{{$}}
; OPT-NEXT: loop{{$}}
; OPT-NOT: block
; OPT: br_if 1, {{[^,]+}}{{$}}
Expand All @@ -829,14 +833,16 @@ bb3:
; OPT: br_if 0, {{[^,]+}}{{$}}
; OPT-NOT: block
; OPT: br_if 1, {{[^,]+}}{{$}}
; OPT-NEXT: br 3{{$}}
; OPT-NEXT: br 2{{$}}
; OPT-NEXT: .LBB18_4:
; OPT-NEXT: end_block{{$}}
; OPT-NOT: block
; OPT: br_if 0, {{[^,]+}}{{$}}
; OPT-NEXT: br 2{{$}}
; OPT-NEXT: br 1{{$}}
; OPT-NEXT: .LBB18_5:
; OPT-NOT: block
; OPT: end_block
; OPT-NOT: block
; OPT: return{{$}}
declare i1 @a()
define void @test9() {
Expand Down Expand Up @@ -884,10 +890,11 @@ end:
; CHECK: .LBB19_4:
; CHECK-NEXT: loop{{$}}
; CHECK-NOT: block
; CHECK: br_if 5, {{[^,]+}}{{$}}
; CHECK-NOT: block
; CHECK: br_table {{[^,]+}}, 0, 1, 5, 2, 4, 0{{$}}
; CHECK: br_if 3, {{[^,]+}}{{$}}
; CHECK: block{{$}}
; CHECK: br_table {{[^,]+}}, 1, 0, 4, 2, 3, 1{{$}}
; CHECK-NEXT: .LBB19_6:
; CHECK-NEXT: end_block{{$}}
; CHECK-NEXT: end_loop{{$}}
; CHECK-NEXT: end_loop{{$}}
; CHECK-NEXT: return{{$}}
Expand All @@ -908,10 +915,11 @@ end:
; OPT: .LBB19_4:
; OPT-NEXT: loop{{$}}
; OPT-NOT: block
; OPT: br_if 5, {{[^,]+}}{{$}}
; OPT-NOT: block
; OPT: br_table {{[^,]+}}, 0, 1, 5, 2, 4, 0{{$}}
; OPT: br_if 3, {{[^,]+}}{{$}}
; OPT: block
; OPT: br_table {{[^,]+}}, 1, 0, 4, 2, 3, 1{{$}}
; OPT-NEXT: .LBB19_6:
; OPT-NEXT: end_block{{$}}
; OPT-NEXT: end_loop{{$}}
; OPT-NEXT: end_loop{{$}}
; OPT-NEXT: return{{$}}
Expand Down Expand Up @@ -1055,6 +1063,7 @@ bb8:

; CHECK-LABEL: test12:
; CHECK: .LBB21_1:
; CHECK-NEXT: block{{$}}
; CHECK-NEXT: loop{{$}}
; CHECK-NOT: block
; CHECK: block{{$}}
Expand All @@ -1077,9 +1086,11 @@ bb8:
; CHECK: br 0{{$}}
; CHECK-NEXT: .LBB21_7:
; CHECK-NEXT: end_loop{{$}}
; CHECK-NEXT: end_block{{$}}
; CHECK-NEXT: return{{$}}
; OPT-LABEL: test12:
; OPT: .LBB21_1:
; OPT-NEXT: block{{$}}
; OPT-NEXT: loop{{$}}
; OPT-NOT: block
; OPT: block{{$}}
Expand All @@ -1101,6 +1112,7 @@ bb8:
; OPT: br 0{{$}}
; OPT-NEXT: .LBB21_7:
; OPT-NEXT: end_loop{{$}}
; OPT-NEXT: end_block{{$}}
; OPT-NEXT: return{{$}}
define void @test12(i8* %arg) {
bb:
Expand Down Expand Up @@ -1250,6 +1262,7 @@ bb50:
; CHECK: br_if 0, $pop{{.*}}{{$}}
; CHECK: .LBB24_2:
; CHECK-NEXT: block{{$}}
; CHECK-NEXT: block{{$}}
; CHECK-NEXT: loop{{$}}
; CHECK: br_if 1, $pop{{.*}}{{$}}
; CHECK: br_if 0, ${{.*}}{{$}}
Expand Down Expand Up @@ -1278,6 +1291,7 @@ bb50:
; OPT-NEXT: i32.const
; OPT-NEXT: .LBB24_3:
; OPT-NEXT: block
; OPT-NEXT: block
; OPT-NEXT: loop
%0 = type { i8, i32 }
declare void @test15_callee0()
Expand Down

0 comments on commit d8d953e

Please sign in to comment.