[llvm] b1bfe22 - [VPlan] Remove unneeded getNumUsers calls in replaceAllUsesWith (NFC).

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 15 05:43:24 PST 2023


Author: Florian Hahn
Date: 2023-12-15T13:43:15Z
New Revision: b1bfe221e6cc36c2b4bdf80466aaf8935557e45f

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

LOG: [VPlan] Remove unneeded getNumUsers calls in replaceAllUsesWith (NFC).

As suggested post-commit for a00227197, replace unnecessary getNumUsers
calls by boolean variable to indicate if users changed. Note that this
also requires an early exit to detect the case where a value is replaced
by itself.

Added: 
    

Modified: 
    llvm/lib/Transforms/Vectorize/VPlan.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp b/llvm/lib/Transforms/Vectorize/VPlan.cpp
index 263d9938d1f0f4..1d7df9c9575af0 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -1135,16 +1135,20 @@ void VPlanIngredient::print(raw_ostream &O) const {
 template void DomTreeBuilder::Calculate<VPDominatorTree>(VPDominatorTree &DT);
 
 void VPValue::replaceAllUsesWith(VPValue *New) {
+  if (this == New)
+    return;
   for (unsigned J = 0; J < getNumUsers();) {
     VPUser *User = Users[J];
-    unsigned NumUsers = getNumUsers();
+    bool RemovedUser = false;
     for (unsigned I = 0, E = User->getNumOperands(); I < E; ++I)
-      if (User->getOperand(I) == this)
+      if (User->getOperand(I) == this) {
         User->setOperand(I, New);
+        RemovedUser = true;
+      }
     // If a user got removed after updating the current user, the next user to
     // update will be moved to the current position, so we only need to
     // increment the index if the number of users did not change.
-    if (NumUsers == getNumUsers())
+    if (!RemovedUser)
       J++;
   }
 }
@@ -1152,19 +1156,22 @@ void VPValue::replaceAllUsesWith(VPValue *New) {
 void VPValue::replaceUsesWithIf(
     VPValue *New,
     llvm::function_ref<bool(VPUser &U, unsigned Idx)> ShouldReplace) {
+  if (this == New)
+    return;
   for (unsigned J = 0; J < getNumUsers();) {
     VPUser *User = Users[J];
-    unsigned NumUsers = getNumUsers();
+    bool RemovedUser = false;
     for (unsigned I = 0, E = User->getNumOperands(); I < E; ++I) {
       if (User->getOperand(I) != this || !ShouldReplace(*User, I))
         continue;
 
+      RemovedUser = true;
       User->setOperand(I, New);
     }
     // If a user got removed after updating the current user, the next user to
     // update will be moved to the current position, so we only need to
     // increment the index if the number of users did not change.
-    if (NumUsers == getNumUsers())
+    if (!RemovedUser)
       J++;
   }
 }


        


More information about the llvm-commits mailing list