@@ -680,60 +680,58 @@ MachineBasicBlock *SILowerControlFlow::process(MachineInstr &MI) {
680
680
}
681
681
682
682
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 ;
698
695
}
699
696
}
700
- return Ret ;
697
+ return S ;
701
698
};
702
- bool Redundant = true ;
699
+
703
700
for (auto &I : MBB.instrs ()) {
704
701
if (!I.isDebugInstr () && !I.isUnconditionalBranch ())
705
- Redundant = false ;
702
+ return false ;
706
703
}
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
- }
733
704
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);
735
715
}
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 ;
737
735
}
738
736
739
737
bool SILowerControlFlow::runOnMachineFunction (MachineFunction &MF) {
0 commit comments