Skip to content

Commit 5073a62

Browse files
committed
[MachineBasicBlock] Explicit FT branching param
Introduce a parameter in getFallThrough() to optionally allow returning the fall through basic block in spite of an explicit branch instruction to it. This parameter is set to false by default. Introduce getLogicalFallThrough() which calls getFallThrough(false) to obtain the block while avoiding insertion of a jump instruction to its immediate successor. This patch also reverts the changes made by D134557 and solves the case where a jump is inserted after another jump (branch-relax-no-terminators.mir). Reviewed By: arsenm Differential Revision: https://reviews.llvm.org/D140790
1 parent c43f38e commit 5073a62

File tree

4 files changed

+196
-29
lines changed

4 files changed

+196
-29
lines changed

llvm/include/llvm/CodeGen/MachineBasicBlock.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -793,10 +793,15 @@ class MachineBasicBlock
793793

794794
/// Return the fallthrough block if the block can implicitly
795795
/// transfer control to the block after it by falling off the end of
796-
/// it. This should return null if it can reach the block after
797-
/// it, but it uses an explicit branch to do so (e.g., a table
798-
/// jump). Non-null return is a conservative answer.
799-
MachineBasicBlock *getFallThrough();
796+
/// it. If an explicit branch to the fallthrough block is not allowed,
797+
/// set JumpToFallThrough to be false. Non-null return is a conservative
798+
/// answer.
799+
MachineBasicBlock *getFallThrough(bool JumpToFallThrough = false);
800+
801+
/// Return the fallthrough block if the block can implicitly
802+
/// transfer control to it's successor, whether by a branch or
803+
/// a fallthrough. Non-null return is a conservative answer.
804+
MachineBasicBlock *getLogicalFallThrough() { return getFallThrough(true); }
800805

801806
/// Return true if the block can implicitly transfer control to the
802807
/// block after it by falling off the end of it. This should return

llvm/lib/CodeGen/BranchRelaxation.cpp

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,6 @@ bool BranchRelaxation::fixupConditionalBranch(MachineInstr &MI) {
442442

443443
bool BranchRelaxation::fixupUnconditionalBranch(MachineInstr &MI) {
444444
MachineBasicBlock *MBB = MI.getParent();
445-
MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
446445
SmallVector<MachineOperand, 4> Cond;
447446
unsigned OldBrSize = TII->getInstSizeInBytes(MI);
448447
MachineBasicBlock *DestBB = TII->getBranchDestBlock(MI);
@@ -456,20 +455,6 @@ bool BranchRelaxation::fixupUnconditionalBranch(MachineInstr &MI) {
456455

457456
MachineBasicBlock *BranchBB = MBB;
458457

459-
auto RemoveBranch = [&](MachineBasicBlock *MBB) {
460-
unsigned &BBSize = BlockInfo[MBB->getNumber()].Size;
461-
int RemovedSize = 0;
462-
TII->removeBranch(*MBB, &RemovedSize);
463-
BBSize -= RemovedSize;
464-
};
465-
466-
auto InsertUncondBranch = [&](MachineBasicBlock *MBB,
467-
MachineBasicBlock *Dst) {
468-
TII->insertUnconditionalBranch(*MBB, Dst, DebugLoc());
469-
// Recalculate the block size.
470-
BlockInfo[MBB->getNumber()].Size = computeBlockSize(*MBB);
471-
};
472-
473458
// If this was an expanded conditional branch, there is already a single
474459
// unconditional branch in a block.
475460
if (!MBB->empty()) {
@@ -511,13 +496,10 @@ bool BranchRelaxation::fixupUnconditionalBranch(MachineInstr &MI) {
511496
MachineBasicBlock *PrevBB = &*std::prev(DestBB->getIterator());
512497
// Fall through only if PrevBB has no unconditional branch as one of its
513498
// terminators.
514-
if (TII->analyzeBranch(*PrevBB, TBB, FBB, Cond))
515-
report_fatal_error("Could not analyze terminators.");
516-
if (!FBB) {
517-
if (!Cond.empty() && TBB && TBB == DestBB)
518-
RemoveBranch(PrevBB);
519-
if (!TBB || (TBB && !Cond.empty()))
520-
InsertUncondBranch(PrevBB, DestBB);
499+
if (auto *FT = PrevBB->getLogicalFallThrough()) {
500+
assert(FT == DestBB);
501+
TII->insertUnconditionalBranch(*PrevBB, FT, DebugLoc());
502+
BlockInfo[PrevBB->getNumber()].Size = computeBlockSize(*PrevBB);
521503
}
522504
// Now, RestoreBB could be placed directly before DestBB.
523505
MF->splice(DestBB->getIterator(), RestoreBB->getIterator());

llvm/lib/CodeGen/MachineBasicBlock.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ const MachineBasicBlock *MachineBasicBlock::getSingleSuccessor() const {
944944
return Successors.size() == 1 ? Successors[0] : nullptr;
945945
}
946946

947-
MachineBasicBlock *MachineBasicBlock::getFallThrough() {
947+
MachineBasicBlock *MachineBasicBlock::getFallThrough(bool JumpToFallThrough) {
948948
MachineFunction::iterator Fallthrough = getIterator();
949949
++Fallthrough;
950950
// If FallthroughBlock is off the end of the function, it can't fall through.
@@ -975,8 +975,8 @@ MachineBasicBlock *MachineBasicBlock::getFallThrough() {
975975

976976
// If there is some explicit branch to the fallthrough block, it can obviously
977977
// reach, even though the branch should get folded to fall through implicitly.
978-
if (MachineFunction::iterator(TBB) == Fallthrough ||
979-
MachineFunction::iterator(FBB) == Fallthrough)
978+
if (!JumpToFallThrough && (MachineFunction::iterator(TBB) == Fallthrough ||
979+
MachineFunction::iterator(FBB) == Fallthrough))
980980
return &*Fallthrough;
981981

982982
// If it's an unconditional branch to some block not the fall through, it

0 commit comments

Comments
 (0)