Index: lib/CodeGen/TailDuplicator.cpp =================================================================== --- lib/CodeGen/TailDuplicator.cpp +++ lib/CodeGen/TailDuplicator.cpp @@ -785,6 +785,21 @@ return true; } +/// If the block \p MBB contains an implicit fallthrough to another block add an +/// explicit unconditional branch. If the block is unanalyzable do nothing. +static void addBranchForFallthrough(const TargetInstrInfo *TII, + MachineBasicBlock &MBB) { + MachineBasicBlock *TBB, *FBB; + SmallVector TailCond; + if (!TII->analyzeBranch(MBB, TBB, FBB, TailCond)) { + bool FallsThrough = !TBB || (!FBB && !TailCond.empty()); + MachineBasicBlock *FallThroughTarget = MBB.getNextNode(); + if (FallsThrough && FallThroughTarget) + if (MBB.isSuccessor(FallThroughTarget)) + TII->insertUnconditionalBranch(MBB, FallThroughTarget, DebugLoc()); + } +} + /// If it is profitable, duplicate TailBB's contents in each /// of its predecessors. /// \p IsSimple result of isSimpleBB @@ -835,6 +850,10 @@ TDBBs.push_back(PredBB); + // If the BB to merge in has a fallthrough, insert an explicit branch, + // because these instructions will be moved into a different block now. + addBranchForFallthrough(TII, *TailBB); + // Remove PredBB's unconditional branch. TII->removeBranch(*PredBB); @@ -895,6 +914,7 @@ !TailBB->hasAddressTaken()) { LLVM_DEBUG(dbgs() << "\nMerging into block: " << *PrevBB << "From MBB: " << *TailBB); + addBranchForFallthrough(TII, *TailBB); // There may be a branch to the layout successor. This is unlikely but it // happens. The correct thing to do is to remove the branch before // duplicating the instructions in all cases.