Index: llvm/lib/CodeGen/MachineBlockPlacement.cpp =================================================================== --- llvm/lib/CodeGen/MachineBlockPlacement.cpp +++ llvm/lib/CodeGen/MachineBlockPlacement.cpp @@ -2755,6 +2755,7 @@ // cannot because all branches may not be analyzable. // E.g., the target may be able to remove an unconditional branch to // a fallthrough when it occurs after predicated terminators. + SmallVector EmptyBB; for (MachineBasicBlock *ChainBB : FunctionChain) { Cond.clear(); MachineBasicBlock *TBB = nullptr, *FBB = nullptr; // For AnalyzeBranch. @@ -2774,9 +2775,41 @@ TII->removeBranch(*ChainBB); TII->insertBranch(*ChainBB, FBB, TBB, Cond, dl); ChainBB->updateTerminator(); + } else if (Cond.empty() && TBB && ChainBB != TBB && + !TBB->canFallThrough()) { + // When ChainBB is unconditional branch to the TBB, and TBB has no + // fallthrough predecessor and fallthrough successor, try to merge + // ChainBB and TBB. This is legal under the one of following conditions: + // 1. ChainBB is empty except for an unconditional branch. + // 2. TBB has only one predecessor. + MachineFunction::iterator I(TBB); + if (((TBB == &*F->begin()) || !std::prev(I)->canFallThrough()) && + (TailDup.isSimpleBB(ChainBB) || (TBB->pred_size() == 1))) { + TII->removeBranch(*ChainBB); + ChainBB->removeSuccessor(TBB); + + // Update the CFG. + for (MachineBasicBlock::pred_iterator PI = TBB->pred_begin(), + PE = TBB->pred_end(); PI != PE; PI++) + (*PI)->ReplaceUsesOfBlockWith(TBB, ChainBB); + + for (MachineBasicBlock *Succ : TBB->successors()) + ChainBB->addSuccessor(Succ, MBPI->getEdgeProbability(TBB, Succ)); + + // Move all the instructions of TBB to ChainBB. + ChainBB->splice(ChainBB->end(), TBB, TBB->begin(), TBB->end()); + EmptyBB.push_back(TBB); + } } } } + + for (auto BB: EmptyBB) { + MLI->removeBlock(BB); + FunctionChain.remove(BB); + BlockToChain.erase(BB); + F->erase(BB); + } } void MachineBlockPlacement::alignBlocks() { @@ -3096,6 +3129,9 @@ } } + // optimizeBranches() may change the blocks, but we haven't updated the + // post-dominator tree. Because the post-dominator tree won't be used after + // this function and this pass don't preserve the post-dominator tree. optimizeBranches(); alignBlocks(); Index: llvm/test/CodeGen/PowerPC/block-placement-1.mir =================================================================== --- llvm/test/CodeGen/PowerPC/block-placement-1.mir +++ llvm/test/CodeGen/PowerPC/block-placement-1.mir @@ -299,14 +299,8 @@ bb.11.unreachable: ; CHECK: bb.4.catch4: - ; CHECK: successors: %bb.11(0x7ffff800), %bb.6(0x00000800) - ; CHECK: B %bb.11 + ; CHECK: successors: %bb.6(0x00000800) ; CHECK: bb.1.for.body (align 4): - ; CHECK: successors: %bb.2(0x7ffff800), %bb.3(0x00000800) - ; CHECK: B %bb.2 - - ; CHECK: bb.2..noexc: - - ; CHECK: bb.11.unreachable: + ; CHECK: successors: %bb.3(0x00000800) ... Index: llvm/test/CodeGen/PowerPC/block-placement.mir =================================================================== --- llvm/test/CodeGen/PowerPC/block-placement.mir +++ llvm/test/CodeGen/PowerPC/block-placement.mir @@ -209,14 +209,10 @@ BLR8 implicit $lr8, implicit $rm, implicit killed $x3 ; CHECK: bb.5.if.else.i: - ; CHECK: successors: %bb.11(0x80000000) - ; CHECK: B %bb.11 + ; CHECK-NEXT: renamable $x3 = LI8 1 + ; CHECK-NEXT: BLR8 implicit $lr8, implicit $rm, implicit killed $x3 ; CHECK: bb.8.while.body.i (align 4): - ; CHECK: successors: %bb.11(0x04000000), %bb.9(0x7c000000) - ; CHECK: BCC 76, killed renamable $cr0, %bb.11 - - ; CHECK: bb.11: - ; CHECK: renamable $x3 = LI8 1 - ; CHECK-NEXT: BLR8 implicit $lr8, implicit $rm, implicit killed $x3 + ; CHECK: successors: %bb.5(0x04000000), %bb.9(0x7c000000) + ; CHECK: BCC 76, killed renamable $cr0, %bb.5 ...