[llvm] [IndVars] Split the NumElimCmp into three pieces (PR #170514)

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 3 08:55:07 PST 2025


https://github.com/preames created https://github.com/llvm/llvm-project/pull/170514

Only one of the three update paths actual eliminate the comparison.

While here, use early return to clarify the code structure.

>From 6768610198eea0bb8ace41445f328988a6b6249d Mon Sep 17 00:00:00 2001
From: Philip Reames <preames at rivosinc.com>
Date: Wed, 3 Dec 2025 08:51:20 -0800
Subject: [PATCH] [IndVars] Split the NumElimCmp into three pieces

Only one of the three update paths actual eliminate the comparison.

While here, use early return to clarify the code structure.
---
 llvm/lib/Transforms/Utils/SimplifyIndVar.cpp | 30 +++++++++++++-------
 1 file changed, 20 insertions(+), 10 deletions(-)

diff --git a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
index f5b9cd78cb5d8..61acf3ab61a22 100644
--- a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
@@ -43,7 +43,9 @@ STATISTIC(
 STATISTIC(
     NumSimplifiedSRem,
     "Number of IV signed remainder operations converted to unsigned remainder");
-STATISTIC(NumElimCmp     , "Number of IV comparisons eliminated");
+STATISTIC(NumElimCmp, "Number of IV comparisons eliminated");
+STATISTIC(NumInvariantCmp, "Number of IV comparisons made loop invariant");
+STATISTIC(NumSameSign, "Number of IV comparisons with new samesign flags");
 
 namespace {
   /// This is a utility for simplifying induction variables
@@ -275,11 +277,20 @@ void SimplifyIndvar::eliminateIVComparison(ICmpInst *ICmp,
     ICmp->replaceAllUsesWith(ConstantInt::getBool(ICmp->getContext(), *Ev));
     DeadInsts.emplace_back(ICmp);
     LLVM_DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n');
-  } else if (makeIVComparisonInvariant(ICmp, IVOperand)) {
-    // fallthrough to end of function
-  } else if ((ICmpInst::isSigned(OriginalPred) ||
-              (ICmpInst::isUnsigned(OriginalPred) && !ICmp->hasSameSign())) &&
-             SE->haveSameSign(S, X)) {
+    ++NumElimCmp;
+    Changed = true;
+    return;
+  }
+
+  if (makeIVComparisonInvariant(ICmp, IVOperand)) {
+    ++NumInvariantCmp;
+    Changed = true;
+    return;
+  }
+
+  if ((ICmpInst::isSigned(OriginalPred) ||
+       (ICmpInst::isUnsigned(OriginalPred) && !ICmp->hasSameSign())) &&
+      SE->haveSameSign(S, X)) {
     // Set the samesign flag on the compare if legal, and canonicalize to
     // the unsigned variant (for signed compares) hoping that it will open
     // the doors for other optimizations.  Note that we cannot rely on Pred
@@ -289,11 +300,10 @@ void SimplifyIndvar::eliminateIVComparison(ICmpInst *ICmp,
                       << '\n');
     ICmp->setPredicate(ICmpInst::getUnsignedPredicate(OriginalPred));
     ICmp->setSameSign();
-  } else
+    NumSameSign++;
+    Changed = true;
     return;
-
-  ++NumElimCmp;
-  Changed = true;
+  }
 }
 
 bool SimplifyIndvar::eliminateSDiv(BinaryOperator *SDiv) {



More information about the llvm-commits mailing list