From 7ad5d14f3a2c916277e25737de9ff3f605131d1a Mon Sep 17 00:00:00 2001 From: Roman Lebedev Date: Sun, 5 May 2019 18:59:22 +0000 Subject: [PATCH] [NFC] Instruction: introduce replaceSuccessorWith() function, use it Summary: There is `Instruction::getNumSuccessors()`, `Instruction::getSuccessor()` and `Instruction::setSuccessor()`, but no function to replace every specified `BasicBlock*` successor with some other specified `BasicBlock*`. I've found one place where it should clearly be used. Reviewers: chandlerc, craig.topper, spatel, danielcdh Reviewed By: craig.topper Subscribers: llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D61010 llvm-svn: 359994 --- llvm/include/llvm/IR/Instruction.h | 4 ++++ llvm/lib/IR/Instruction.cpp | 7 +++++++ llvm/lib/Transforms/Utils/LoopSimplify.cpp | 4 +--- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/llvm/include/llvm/IR/Instruction.h b/llvm/include/llvm/IR/Instruction.h index 499c816..6a9a74b 100644 --- a/llvm/include/llvm/IR/Instruction.h +++ b/llvm/include/llvm/IR/Instruction.h @@ -665,6 +665,10 @@ public: /// 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; diff --git a/llvm/lib/IR/Instruction.cpp b/llvm/lib/IR/Instruction.cpp index f15bd60..42a7a1c 100644 --- a/llvm/lib/IR/Instruction.cpp +++ b/llvm/lib/IR/Instruction.cpp @@ -675,6 +675,13 @@ void Instruction::setSuccessor(unsigned idx, BasicBlock *B) { llvm_unreachable("not a terminator"); } +void Instruction::replaceSuccessorWith(BasicBlock *OldBB, BasicBlock *NewBB) { + for (unsigned Idx = 0, NumSuccessors = Instruction::getNumSuccessors(); + Idx != NumSuccessors; ++Idx) + if (getSuccessor(Idx) == OldBB) + setSuccessor(Idx, NewBB); +} + Instruction *Instruction::cloneImpl() const { llvm_unreachable("Subclass of Instruction failed to implement cloneImpl"); } diff --git a/llvm/lib/Transforms/Utils/LoopSimplify.cpp b/llvm/lib/Transforms/Utils/LoopSimplify.cpp index b076e6ff..194cb5d 100644 --- a/llvm/lib/Transforms/Utils/LoopSimplify.cpp +++ b/llvm/lib/Transforms/Utils/LoopSimplify.cpp @@ -443,9 +443,7 @@ static BasicBlock *insertUniqueBackedgeBlock(Loop *L, BasicBlock *Preheader, 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); -- 2.7.4