[PATCH] D61013: [NFC] BasicBlock: refactor changePhiUses() out of replaceSuccessorsPhiUsesWith(), use it

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


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

It is a common thing to loop over every `PHINode` in some `BasicBlock`
and change old `BasicBlock` incoming block to a new `BasicBlock` incoming block.
`replaceSuccessorsPhiUsesWith()` already had code to do that,
it just wasn't a function.
So outline it into a new function, and use it.


Repository:
  rL LLVM

https://reviews.llvm.org/D61013

Files:
  include/llvm/IR/BasicBlock.h
  lib/CodeGen/CodeGenPrepare.cpp
  lib/IR/BasicBlock.cpp
  lib/Transforms/Scalar/LoopInterchange.cpp


Index: lib/Transforms/Scalar/LoopInterchange.cpp
===================================================================
--- lib/Transforms/Scalar/LoopInterchange.cpp
+++ lib/Transforms/Scalar/LoopInterchange.cpp
@@ -1281,8 +1281,7 @@
 
 static void updateIncomingBlock(BasicBlock *CurrBlock, BasicBlock *OldPred,
                                 BasicBlock *NewPred) {
-  for (PHINode &PHI : CurrBlock->phis())
-    PHI.changeIncomingBlock(OldPred, NewPred);
+  CurrBlock->changePhiUses(OldPred, NewPred);
 }
 
 /// Update BI to jump to NewBB instead of OldBB. Records updates to
Index: lib/IR/BasicBlock.cpp
===================================================================
--- lib/IR/BasicBlock.cpp
+++ lib/IR/BasicBlock.cpp
@@ -437,22 +437,26 @@
   return New;
 }
 
+void BasicBlock::changePhiUses(BasicBlock *Old, BasicBlock *New) {
+  // N.B. This might not be a complete BasicBlock, so don't assume
+  // that it ends with a non-phi instruction.
+  for (iterator II = begin(), IE = end(); II != IE; ++II) {
+    PHINode *PN = dyn_cast<PHINode>(II);
+    if (!PN)
+      break;
+    PN->changeIncomingBlock(Old, New);
+  }
+}
+
 void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *New) {
   Instruction *TI = getTerminator();
   if (!TI)
     // Cope with being called on a BasicBlock that doesn't have a terminator
     // yet. Clang's CodeGenFunction::EmitReturnBlock() likes to do this.
     return;
-  for (BasicBlock *Succ : successors(TI)) {
-    // N.B. Succ might not be a complete BasicBlock, so don't assume
-    // that it ends with a non-phi instruction.
-    for (iterator II = Succ->begin(), IE = Succ->end(); II != IE; ++II) {
-      PHINode *PN = dyn_cast<PHINode>(II);
-      if (!PN)
-        break;
-      PN->changeIncomingBlock(this, New);
-    }
-  }
+  llvm::for_each(successors(TI), [this, New](BasicBlock *Succ) {
+    Succ->changePhiUses(this, New);
+  });
 }
 
 /// Return true if this basic block is a landing pad. I.e., it's
Index: lib/CodeGen/CodeGenPrepare.cpp
===================================================================
--- lib/CodeGen/CodeGenPrepare.cpp
+++ lib/CodeGen/CodeGenPrepare.cpp
@@ -7226,8 +7226,7 @@
       std::swap(TBB, FBB);
 
     // Replace the old BB with the new BB.
-    for (PHINode &PN : TBB->phis())
-      PN.changeIncomingBlock(&BB, TmpBB);
+    TBB->changePhiUses(&BB, TmpBB);
 
     // Add another incoming edge form the new BB.
     for (PHINode &PN : FBB->phis()) {
Index: include/llvm/IR/BasicBlock.h
===================================================================
--- include/llvm/IR/BasicBlock.h
+++ include/llvm/IR/BasicBlock.h
@@ -390,6 +390,10 @@
   /// direct branches, switches, etc. to it.
   bool hasAddressTaken() const { return getSubclassDataFromValue() != 0; }
 
+  /// Update all phi nodes in this basic block to refer to basic block \p New
+  /// instead of basic block \p Old.
+  void changePhiUses(BasicBlock *Old, BasicBlock *New);
+
   /// Update all phi nodes in this basic block's successors to refer to basic
   /// block \p New instead of to it.
   void replaceSuccessorsPhiUsesWith(BasicBlock *New);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D61013.196236.patch
Type: text/x-patch
Size: 3108 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190423/3101d436/attachment.bin>


More information about the llvm-commits mailing list