Skip to content

Commit ecd1f83

Browse files
alex-tmemfrob
authored andcommitted
[AMDGPU] SILowerControlFlow::removeMBBifRedundant. Refactoring plus fix for the null MBB pointer in MF->splice
Detailed description: This change addresses the refactoring adviced by foad. It also contain the fix for the case when getNextNode is null if the successor block is the last in MachineFunction. Reviewed By: foad Differential Revision: https://reviews.llvm.org/D90314
1 parent 43f616a commit ecd1f83

File tree

1 file changed

+44
-46
lines changed

1 file changed

+44
-46
lines changed

llvm/lib/Target/AMDGPU/SILowerControlFlow.cpp

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -680,60 +680,58 @@ MachineBasicBlock *SILowerControlFlow::process(MachineInstr &MI) {
680680
}
681681

682682
bool SILowerControlFlow::removeMBBifRedundant(MachineBasicBlock &MBB) {
683-
auto getFallThroughSucc = [=](MachineBasicBlock * MBB) {
684-
MachineBasicBlock *Ret = nullptr;
685-
for (auto S : MBB->successors()) {
686-
if (MBB->isLayoutSuccessor(S)) {
687-
// The only fallthrough candidate
688-
MachineBasicBlock::iterator I(MBB->getFirstInstrTerminator());
689-
while (I != MBB->end()) {
690-
if (I->isBranch() && TII->getBranchDestBlock(*I) == S)
691-
// We have unoptimized branch to layout successor
692-
break;
693-
I++;
694-
}
695-
if (I == MBB->end())
696-
Ret = S;
697-
break;
683+
auto GetFallThroughSucc = [=](MachineBasicBlock *B) -> MachineBasicBlock * {
684+
auto *S = B->getNextNode();
685+
if (!S)
686+
return nullptr;
687+
if (B->isSuccessor(S)) {
688+
// The only fallthrough candidate
689+
MachineBasicBlock::iterator I(B->getFirstInstrTerminator());
690+
MachineBasicBlock::iterator E = B->end();
691+
for (; I != E; I++) {
692+
if (I->isBranch() && TII->getBranchDestBlock(*I) == S)
693+
// We have unoptimized branch to layout successor
694+
return nullptr;
698695
}
699696
}
700-
return Ret;
697+
return S;
701698
};
702-
bool Redundant = true;
699+
703700
for (auto &I : MBB.instrs()) {
704701
if (!I.isDebugInstr() && !I.isUnconditionalBranch())
705-
Redundant = false;
702+
return false;
706703
}
707-
if (Redundant) {
708-
MachineBasicBlock *Succ = *MBB.succ_begin();
709-
SmallVector<MachineBasicBlock *, 2> Preds(MBB.predecessors());
710-
MachineBasicBlock *FallThrough = nullptr;
711-
for (auto P : Preds) {
712-
if (getFallThroughSucc(P) == &MBB)
713-
FallThrough = P;
714-
P->ReplaceUsesOfBlockWith(&MBB, Succ);
715-
}
716-
MBB.removeSuccessor(Succ);
717-
if (LIS) {
718-
for (auto &I : MBB.instrs())
719-
LIS->RemoveMachineInstrFromMaps(I);
720-
}
721-
MBB.clear();
722-
MBB.eraseFromParent();
723-
if (FallThrough && !FallThrough->isLayoutSuccessor(Succ)) {
724-
MachineFunction *MF = FallThrough->getParent();
725-
if (!getFallThroughSucc(Succ)) {
726-
MachineFunction::iterator InsertPt(FallThrough->getNextNode());
727-
MF->splice(InsertPt, Succ);
728-
} else
729-
BuildMI(*FallThrough, FallThrough->end(),
730-
FallThrough->findBranchDebugLoc(), TII->get(AMDGPU::S_BRANCH))
731-
.addMBB(Succ);
732-
}
733704

734-
return true;
705+
assert(MBB.succ_size() == 1 && "MBB has more than one successor");
706+
707+
MachineBasicBlock *Succ = *MBB.succ_begin();
708+
MachineBasicBlock *FallThrough = nullptr;
709+
710+
while (!MBB.predecessors().empty()) {
711+
MachineBasicBlock *P = *MBB.pred_begin();
712+
if (GetFallThroughSucc(P) == &MBB)
713+
FallThrough = P;
714+
P->ReplaceUsesOfBlockWith(&MBB, Succ);
735715
}
736-
return false;
716+
MBB.removeSuccessor(Succ);
717+
if (LIS) {
718+
for (auto &I : MBB.instrs())
719+
LIS->RemoveMachineInstrFromMaps(I);
720+
}
721+
MBB.clear();
722+
MBB.eraseFromParent();
723+
if (FallThrough && !FallThrough->isLayoutSuccessor(Succ)) {
724+
if (!GetFallThroughSucc(Succ)) {
725+
MachineFunction *MF = FallThrough->getParent();
726+
MachineFunction::iterator FallThroughPos(FallThrough);
727+
MF->splice(std::next(FallThroughPos), Succ);
728+
} else
729+
BuildMI(*FallThrough, FallThrough->end(),
730+
FallThrough->findBranchDebugLoc(), TII->get(AMDGPU::S_BRANCH))
731+
.addMBB(Succ);
732+
}
733+
734+
return true;
737735
}
738736

739737
bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) {

0 commit comments

Comments
 (0)