[PATCH] D61010: [NFC] Instruction: introduce changeSuccessor() function, use it

Roman Lebedev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 23 06:28:50 PDT 2019


lebedev.ri created this revision.
lebedev.ri added reviewers: chandlerc, craig.topper, spatel, danielcdh.
lebedev.ri added a project: LLVM.

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.


Repository:
  rL LLVM

https://reviews.llvm.org/D61010

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


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->changeSuccessor(Header, BEBlock);
   }
   BEBlock->getTerminator()->setMetadata(LoopMDKind, LoopMD);
 
Index: lib/IR/Instruction.cpp
===================================================================
--- lib/IR/Instruction.cpp
+++ lib/IR/Instruction.cpp
@@ -675,6 +675,15 @@
   llvm_unreachable("not a terminator");
 }
 
+void Instruction::changeSuccessor(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: 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);
 
+  /// Change specified successor OldBB to point at the provided block.
+  /// This instruction must be a terminator.
+  void changeSuccessor(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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D61010.196233.patch
Type: text/x-patch
Size: 1892 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190423/3899c3a5/attachment.bin>


More information about the llvm-commits mailing list