Skip to content

Commit d383ade

Browse files
committed
[BranchRelaxation] Fall through only if block has no unconditional branches
Prior to inserting an unconditional branch from X to its fall through basic block, check if X has any terminators to avoid inserting additional branches. Reviewed By: arsenm Differential Revision: https://reviews.llvm.org/D134557
1 parent f59f116 commit d383ade

File tree

2 files changed

+189
-6
lines changed

2 files changed

+189
-6
lines changed

llvm/lib/CodeGen/BranchRelaxation.cpp

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "llvm/Pass.h"
2424
#include "llvm/Support/Compiler.h"
2525
#include "llvm/Support/Debug.h"
26+
#include "llvm/Support/ErrorHandling.h"
2627
#include "llvm/Support/Format.h"
2728
#include "llvm/Support/raw_ostream.h"
2829
#include <cassert>
@@ -431,7 +432,8 @@ bool BranchRelaxation::fixupConditionalBranch(MachineInstr &MI) {
431432

432433
bool BranchRelaxation::fixupUnconditionalBranch(MachineInstr &MI) {
433434
MachineBasicBlock *MBB = MI.getParent();
434-
435+
MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
436+
SmallVector<MachineOperand, 4> Cond;
435437
unsigned OldBrSize = TII->getInstSizeInBytes(MI);
436438
MachineBasicBlock *DestBB = TII->getBranchDestBlock(MI);
437439

@@ -444,6 +446,20 @@ bool BranchRelaxation::fixupUnconditionalBranch(MachineInstr &MI) {
444446

445447
MachineBasicBlock *BranchBB = MBB;
446448

449+
auto RemoveBranch = [&](MachineBasicBlock *MBB) {
450+
unsigned &BBSize = BlockInfo[MBB->getNumber()].Size;
451+
int RemovedSize = 0;
452+
TII->removeBranch(*MBB, &RemovedSize);
453+
BBSize -= RemovedSize;
454+
};
455+
456+
auto InsertUncondBranch = [&](MachineBasicBlock *MBB,
457+
MachineBasicBlock *Dst) {
458+
TII->insertUnconditionalBranch(*MBB, Dst, DebugLoc());
459+
// Recalculate the block size.
460+
BlockInfo[MBB->getNumber()].Size = computeBlockSize(*MBB);
461+
};
462+
447463
// If this was an expanded conditional branch, there is already a single
448464
// unconditional branch in a block.
449465
if (!MBB->empty()) {
@@ -482,11 +498,15 @@ bool BranchRelaxation::fixupUnconditionalBranch(MachineInstr &MI) {
482498
// restore blocks are just duplicated for each far branch.
483499
assert(!DestBB->isEntryBlock());
484500
MachineBasicBlock *PrevBB = &*std::prev(DestBB->getIterator());
485-
if (auto *FT = PrevBB->getFallThrough()) {
486-
assert(FT == DestBB);
487-
TII->insertUnconditionalBranch(*PrevBB, FT, DebugLoc());
488-
// Recalculate the block size.
489-
BlockInfo[PrevBB->getNumber()].Size = computeBlockSize(*PrevBB);
501+
// Fall through only if PrevBB has no unconditional branch as one of its
502+
// terminators.
503+
if (TII->analyzeBranch(*PrevBB, TBB, FBB, Cond))
504+
report_fatal_error("Could not analyze terminators.");
505+
if (!FBB) {
506+
if (!Cond.empty() && TBB && TBB == DestBB)
507+
RemoveBranch(PrevBB);
508+
if (!TBB || (TBB && !Cond.empty()))
509+
InsertUncondBranch(PrevBB, DestBB);
490510
}
491511
// Now, RestoreBB could be placed directly before DestBB.
492512
MF->splice(DestBB->getIterator(), RestoreBB->getIterator());

0 commit comments

Comments
 (0)