Skip to content

Commit

Permalink
[NFC] Instruction: introduce replaceSuccessorWith() function, use it
Browse files Browse the repository at this point in the history
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
  • Loading branch information
LebedevRI authored and MrSidims committed May 24, 2019
1 parent ffb2f43 commit ae90774
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
4 changes: 4 additions & 0 deletions llvm/include/llvm/IR/Instruction.h
Expand Up @@ -665,6 +665,10 @@ class Instruction : public User,
/// 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;
Expand Down
7 changes: 7 additions & 0 deletions llvm/lib/IR/Instruction.cpp
Expand Up @@ -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");
}
Expand Down
4 changes: 1 addition & 3 deletions llvm/lib/Transforms/Utils/LoopSimplify.cpp
Expand Up @@ -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);

Expand Down

0 comments on commit ae90774

Please sign in to comment.