Index: include/llvm/IR/Instruction.h =================================================================== --- include/llvm/IR/Instruction.h +++ include/llvm/IR/Instruction.h @@ -665,6 +665,10 @@ /// instruction must be a terminator. void setSuccessor(unsigned Idx, BasicBlock *BB); + /// Replace specified successor OldBB to point at the provided block. + /// This instruction must be a terminator. + void replaceSuccessorWith(BasicBlock *OldBB, BasicBlock *NewBB); + /// Methods for support type inquiry through isa, cast, and dyn_cast: static bool classof(const Value *V) { return V->getValueID() >= Value::InstructionVal; Index: lib/IR/Instruction.cpp =================================================================== --- lib/IR/Instruction.cpp +++ lib/IR/Instruction.cpp @@ -675,6 +675,15 @@ llvm_unreachable("not a terminator"); } +void Instruction::replaceSuccessorWith(BasicBlock *OldBB, BasicBlock *NewBB) { + for (unsigned idx = 0, NumSuccessors = Instruction::getNumSuccessors(); + idx != NumSuccessors; ++idx) { + BasicBlock *CurrSuccessor = getSuccessor(idx); + if (CurrSuccessor == OldBB) + setSuccessor(idx, NewBB); + } +} + Instruction *Instruction::cloneImpl() const { llvm_unreachable("Subclass of Instruction failed to implement cloneImpl"); } Index: lib/Transforms/Utils/LoopSimplify.cpp =================================================================== --- lib/Transforms/Utils/LoopSimplify.cpp +++ lib/Transforms/Utils/LoopSimplify.cpp @@ -443,9 +443,7 @@ if (!LoopMD) LoopMD = TI->getMetadata(LoopMDKind); TI->setMetadata(LoopMDKind, nullptr); - for (unsigned Op = 0, e = TI->getNumSuccessors(); Op != e; ++Op) - if (TI->getSuccessor(Op) == Header) - TI->setSuccessor(Op, BEBlock); + TI->replaceSuccessorWith(Header, BEBlock); } BEBlock->getTerminator()->setMetadata(LoopMDKind, LoopMD);