[llvm] r359994 - [NFC] Instruction: introduce replaceSuccessorWith() function, use it
Roman Lebedev via llvm-commits
llvm-commits at lists.llvm.org
Sun May 5 11:59:22 PDT 2019
Author: lebedevri
Date: Sun May 5 11:59:22 2019
New Revision: 359994
URL: http://llvm.org/viewvc/llvm-project?rev=359994&view=rev
Log:
[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
Modified:
llvm/trunk/include/llvm/IR/Instruction.h
llvm/trunk/lib/IR/Instruction.cpp
llvm/trunk/lib/Transforms/Utils/LoopSimplify.cpp
Modified: llvm/trunk/include/llvm/IR/Instruction.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/Instruction.h?rev=359994&r1=359993&r2=359994&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/Instruction.h (original)
+++ llvm/trunk/include/llvm/IR/Instruction.h Sun May 5 11:59:22 2019
@@ -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;
Modified: llvm/trunk/lib/IR/Instruction.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Instruction.cpp?rev=359994&r1=359993&r2=359994&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Instruction.cpp (original)
+++ llvm/trunk/lib/IR/Instruction.cpp Sun May 5 11:59:22 2019
@@ -675,6 +675,13 @@ void Instruction::setSuccessor(unsigned
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");
}
Modified: llvm/trunk/lib/Transforms/Utils/LoopSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/LoopSimplify.cpp?rev=359994&r1=359993&r2=359994&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/LoopSimplify.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/LoopSimplify.cpp Sun May 5 11:59:22 2019
@@ -443,9 +443,7 @@ static BasicBlock *insertUniqueBackedgeB
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);
More information about the llvm-commits
mailing list