- UpdateTerminator(prior(Begin));
- UpdateTerminator(OldBeginPrior);
- UpdateTerminator(OldEndPrior);
-}
-
-/// UpdateTerminator - Update the terminator instructions in MBB to account
-/// for changes to the layout. If the block previously used a fallthrough,
-/// it may now need a branch, and if it previously used branching it may now
-/// be able to use a fallthrough.
-///
-void CodePlacementOpt::UpdateTerminator(MachineBasicBlock *MBB) {
- // A block with no successors has no concerns with fall-through edges.
- if (MBB->succ_empty()) return;
-
- MachineBasicBlock *TBB = 0, *FBB = 0;
- SmallVector<MachineOperand, 4> Cond;
- assert(!TII->AnalyzeBranch(*MBB, TBB, FBB, Cond) &&
- "UpdateTerminators requires analyzable predecessors!");
- if (Cond.empty()) {
- if (TBB) {
- // The block has an unconditional branch. If its successor is now
- // its layout successor, delete the branch.
- if (MBB->isLayoutSuccessor(TBB))
- TII->RemoveBranch(*MBB);
- } else {
- // The block has an unconditional fallthrough. If its successor is not
- // its layout successor, insert a branch.
- TBB = *MBB->succ_begin();
- if (!MBB->isLayoutSuccessor(TBB))
- TII->InsertBranch(*MBB, TBB, 0, Cond);
- }
- } else {
- if (FBB) {
- // The block has a non-fallthrough conditional branch. If one of its
- // successors is its layout successor, rewrite it to a fallthrough
- // conditional branch.
- if (MBB->isLayoutSuccessor(TBB)) {
- TII->RemoveBranch(*MBB);
- TII->ReverseBranchCondition(Cond);
- TII->InsertBranch(*MBB, FBB, 0, Cond);
- } else if (MBB->isLayoutSuccessor(FBB)) {
- TII->RemoveBranch(*MBB);
- TII->InsertBranch(*MBB, TBB, 0, Cond);
- }
- } else {
- // The block has a fallthrough conditional branch.
- MachineBasicBlock *MBBA = *MBB->succ_begin();
- MachineBasicBlock *MBBB = *next(MBB->succ_begin());
- if (MBBA == TBB) std::swap(MBBB, MBBA);
- if (MBB->isLayoutSuccessor(TBB)) {
- TII->RemoveBranch(*MBB);
- TII->ReverseBranchCondition(Cond);
- TII->InsertBranch(*MBB, MBBA, 0, Cond);
- } else if (!MBB->isLayoutSuccessor(MBBA)) {
- TII->RemoveBranch(*MBB);
- TII->InsertBranch(*MBB, TBB, MBBA, Cond);
- }
- }
- }
-}
-
-/// OptimizeIntraLoopEdges - Reposition loop blocks to minimize
-/// intra-loop branching and to form contiguous loops.
-///
-bool CodePlacementOpt::OptimizeIntraLoopEdges(MachineFunction &MF) {
- bool Changed = false;
-
- if (!TLI->shouldOptimizeCodePlacement())
- return Changed;
-
- for (MachineLoopInfo::iterator I = MLI->begin(), E = MLI->end();
- I != E; ++I)
- Changed |= OptimizeIntraLoopEdgesInLoop(MF, *I);
-
- return Changed;