]> granicus.if.org Git - llvm/commitdiff
[NFC] Instruction: introduce replaceSuccessorWith() function, use it
authorRoman Lebedev <lebedev.ri@gmail.com>
Sun, 5 May 2019 18:59:22 +0000 (18:59 +0000)
committerRoman Lebedev <lebedev.ri@gmail.com>
Sun, 5 May 2019 18:59:22 +0000 (18:59 +0000)
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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@359994 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/IR/Instruction.h
lib/IR/Instruction.cpp
lib/Transforms/Utils/LoopSimplify.cpp

index 499c8166dc3728f478d5999815618cb9a0fd6be7..6a9a74bd16f0f5f708d40b68bd6090f78fed2570 100644 (file)
@@ -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;
index f15bd6017276a7593fa8aa89edbaa0f8fe50d02b..42a7a1cce7fffefd5cc972816bb8567330d50caf 100644 (file)
@@ -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");
 }
index b076e6ffc5320aab83aea963653692da33eb819b..194cb5d302d5d836f3c85b74e74f26cc68cf7e2f 100644 (file)
@@ -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);