Index: lib/CodeGen/TailDuplicator.cpp =================================================================== --- lib/CodeGen/TailDuplicator.cpp +++ lib/CodeGen/TailDuplicator.cpp @@ -895,6 +895,19 @@ !TailBB->hasAddressTaken()) { LLVM_DEBUG(dbgs() << "\nMerging into block: " << *PrevBB << "From MBB: " << *TailBB); + // If the BB to merge in has a fallthrough, insert an explicit branch, + // because these instructions will be moved into a different block now. + MachineBasicBlock *TailTBB, *TailFBB; + SmallVector TailCond; + if (!TII->analyzeBranch(*TailBB, TailTBB, TailFBB, TailCond)) { + bool FallsThrough = + (TailTBB == nullptr) || (TailFBB == nullptr && !TailCond.empty()); + MachineBasicBlock *FallThroughTarget = TailBB->getNextNode(); + if (FallsThrough && FallThroughTarget != nullptr) + if (TailBB->isSuccessor(FallThroughTarget)) + TII->insertUnconditionalBranch(*TailBB, FallThroughTarget, + DebugLoc()); + } // 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. @@ -922,7 +935,6 @@ } appendCopies(PrevBB, CopyInfos, Copies); } else { - TII->removeBranch(*PrevBB); // No PHIs to worry about, just splice the instructions over. PrevBB->splice(PrevBB->end(), TailBB, TailBB->begin(), TailBB->end()); }