[llvm-branch-commits] [llvm] ce134da - [IR] simplify code in removePredecessor(); NFCI

Sanjay Patel via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sun Nov 29 07:04:30 PST 2020


Author: Sanjay Patel
Date: 2020-11-29T09:55:04-05:00
New Revision: ce134da4b18c27bbeba4e32f5813b1a3b043066e

URL: https://github.com/llvm/llvm-project/commit/ce134da4b18c27bbeba4e32f5813b1a3b043066e
DIFF: https://github.com/llvm/llvm-project/commit/ce134da4b18c27bbeba4e32f5813b1a3b043066e.diff

LOG: [IR] simplify code in removePredecessor(); NFCI

As suggested in D92247 (and independent of whatever we decide to do there),
this code is confusing as-is. Hopefully, this is at least mildly better.

We might be able to do better still, but we have a function called
"removePredecessor" with this behavior:
"Note that this function does not actually remove the predecessor." (!)

Added: 
    

Modified: 
    llvm/lib/IR/BasicBlock.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/BasicBlock.cpp b/llvm/lib/IR/BasicBlock.cpp
index 31666265b504..3268641ddf19 100644
--- a/llvm/lib/IR/BasicBlock.cpp
+++ b/llvm/lib/IR/BasicBlock.cpp
@@ -327,21 +327,19 @@ void BasicBlock::removePredecessor(BasicBlock *Pred,
   // Return early if there are no PHI nodes to update.
   if (!isa<PHINode>(begin()))
     return;
-  unsigned NumPreds = cast<PHINode>(front()).getNumIncomingValues();
 
-  // Update all PHI nodes.
-  for (iterator II = begin(); isa<PHINode>(II);) {
-    PHINode *PN = cast<PHINode>(II++);
-    PN->removeIncomingValue(Pred, !KeepOneInputPHIs);
-    if (!KeepOneInputPHIs) {
-      // If we have a single predecessor, removeIncomingValue erased the PHI
-      // node itself.
-      if (NumPreds > 1) {
-        if (Value *PNV = PN->hasConstantValue()) {
-          // Replace the PHI node with its constant value.
-          PN->replaceAllUsesWith(PNV);
-          PN->eraseFromParent();
-        }
+  unsigned NumPreds = cast<PHINode>(front()).getNumIncomingValues();
+  for (PHINode &Phi : make_early_inc_range(phis())) {
+    Phi.removeIncomingValue(Pred, !KeepOneInputPHIs);
+    if (KeepOneInputPHIs)
+      continue;
+    // If we have a single predecessor, removeIncomingValue erased the PHI
+    // node itself.
+    // Try to replace the PHI node with a constant value.
+    if (NumPreds > 1) {
+      if (Value *PhiConstant = Phi.hasConstantValue()) {
+        Phi.replaceAllUsesWith(PhiConstant);
+        Phi.eraseFromParent();
       }
     }
   }


        


More information about the llvm-branch-commits mailing list