[llvm] [IVDescriptors] Address MonotonicDescriptor post-commit comments (NFC) (PR #223346)

via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 14 02:51:03 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-analysis

Author: Benjamin Maxwell (MacDue)

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/223346.diff


3 Files Affected:

- (modified) llvm/include/llvm/Analysis/IVDescriptors.h (+6-6) 
- (modified) llvm/lib/Analysis/IVDescriptors.cpp (+19-20) 
- (modified) llvm/unittests/Analysis/IVDescriptorsTest.cpp (+3-4) 


``````````diff
diff --git a/llvm/include/llvm/Analysis/IVDescriptors.h b/llvm/include/llvm/Analysis/IVDescriptors.h
index 7748917ae1868..837abbaf3ee22 100644
--- a/llvm/include/llvm/Analysis/IVDescriptors.h
+++ b/llvm/include/llvm/Analysis/IVDescriptors.h
@@ -30,6 +30,7 @@ class ScalarEvolution;
 class SCEV;
 class SCEVPredicate;
 class StoreInst;
+enum class SCEVNoWrapFlags;
 
 /// These are the kinds of recurrences that we support.
 enum class RecurKind {
@@ -491,7 +492,7 @@ class MonotonicDescriptor {
   MonotonicDescriptor() = default;
 
   /// Returns true if \p PN is a monotonic variable in the loop \p L. If \p PN
-  /// is monotonic, the monotonic descriptor \p D will contain the data
+  /// is monotonic, the monotonic descriptor \p Desc will contain the data
   /// describing the PHI.
   LLVM_ABI static bool isMonotonicPHI(PHINode *PN, const Loop *L,
                                       MonotonicDescriptor &Desc,
@@ -515,15 +516,14 @@ class MonotonicDescriptor {
   const SCEV *getStepSCEV() const { return StepSCEV; }
 
   /// Returns the SCEV no-wrap flags that apply to StepInst.
-  unsigned getSCEVNoWrapFlags() const { return SCEVNoWrapFlags; }
+  SCEVNoWrapFlags getSCEVNoWrapFlags() const { return NoWrapFlags; }
 
 private:
   MonotonicDescriptor(PHINode *HeaderPHI, PHINode *BackedgePHI,
                       Instruction *StepInst, const SCEV *StartSCEV,
-                      const SCEV *StepSCEV, unsigned SCEVNoWrapFlags)
+                      const SCEV *StepSCEV, SCEVNoWrapFlags NoWrapFlags)
       : HeaderPHI(HeaderPHI), BackedgePHI(BackedgePHI), StepInst(StepInst),
-        StartSCEV(StartSCEV), StepSCEV(StepSCEV),
-        SCEVNoWrapFlags(SCEVNoWrapFlags) {}
+        StartSCEV(StartSCEV), StepSCEV(StepSCEV), NoWrapFlags(NoWrapFlags) {}
 
   /// The header PHI (this is the PHI described by the descriptor).
   PHINode *HeaderPHI = nullptr;
@@ -541,7 +541,7 @@ class MonotonicDescriptor {
   const SCEV *StepSCEV = nullptr;
 
   /// The SCEV no-wrap flags that apply to StepInst.
-  unsigned SCEVNoWrapFlags = 0;
+  SCEVNoWrapFlags NoWrapFlags{};
 };
 
 } // end namespace llvm
diff --git a/llvm/lib/Analysis/IVDescriptors.cpp b/llvm/lib/Analysis/IVDescriptors.cpp
index 040cabd58a53f..d9e9fabcebb77 100644
--- a/llvm/lib/Analysis/IVDescriptors.cpp
+++ b/llvm/lib/Analysis/IVDescriptors.cpp
@@ -1709,7 +1709,7 @@ bool InductionDescriptor::isInductionPHI(
 // latch:
 //   %latch_phi = phi [ %monotonic_phi, %loop_header ], [ %step, %step_bb ]
 //   br label %loop_header
-bool MonotonicDescriptor::isMonotonicPHI(PHINode *HeaderPHI, const Loop *L,
+bool MonotonicDescriptor::isMonotonicPHI(PHINode *PN, const Loop *L,
                                          MonotonicDescriptor &Desc,
                                          ScalarEvolution &SE) {
   BasicBlock *Preheader = L->getLoopPreheader();
@@ -1717,30 +1717,29 @@ bool MonotonicDescriptor::isMonotonicPHI(PHINode *HeaderPHI, const Loop *L,
     return false;
 
   BasicBlock *Latch = L->getLoopLatch();
-  if (!Latch || !HeaderPHI->getType()->isIntOrPtrTy() ||
-      HeaderPHI->getParent() != L->getHeader())
+  if (!Latch || !PN->getType()->isIntOrPtrTy() ||
+      PN->getParent() != L->getHeader())
     return false;
 
-  auto *BackedgePHI =
-      dyn_cast<PHINode>(HeaderPHI->getIncomingValueForBlock(Latch));
+  auto *BackedgePHI = dyn_cast<PHINode>(PN->getIncomingValueForBlock(Latch));
   if (!BackedgePHI)
     return false;
 
   // Ensure the only users of the backedge PHI are outside the loop or the
-  // header PHI.
+  // header PHI (PN).
   for (User *U : BackedgePHI->users()) {
     auto *UI = cast<Instruction>(U);
-    if (UI != HeaderPHI && L->contains(UI))
+    if (UI != PN && L->contains(UI))
       return false;
   }
 
   // Find the step operation used to increment the value of the monotonic PHI.
   // TODO: Support chains of PHIs.
-  Value *StepOp = find_singleton<Value>(
-      BackedgePHI->incoming_values(),
-      [&](Use &Incoming, bool /*AllowRepeats*/) {
-        return Incoming != HeaderPHI ? Incoming.get() : nullptr;
-      });
+  Value *StepOp =
+      find_singleton<Value>(BackedgePHI->incoming_values(),
+                            [&](Use &Incoming, bool /*AllowRepeats*/) {
+                              return Incoming != PN ? Incoming.get() : nullptr;
+                            });
   if (!StepOp || !StepOp->hasOneUse())
     return false;
 
@@ -1750,20 +1749,20 @@ bool MonotonicDescriptor::isMonotonicPHI(PHINode *HeaderPHI, const Loop *L,
 
   Value *Step = nullptr;
   bool StepMatch =
-      HeaderPHI->getType()->isPointerTy()
-          ? match(StepInst, m_PtrAdd(m_Specific(HeaderPHI), m_Value(Step)))
-          : match(StepInst, m_c_Add(m_Specific(HeaderPHI), m_Value(Step)));
+      PN->getType()->isPointerTy()
+          ? match(StepInst, m_PtrAdd(m_Specific(PN), m_Value(Step)))
+          : match(StepInst, m_c_Add(m_Specific(PN), m_Value(Step)));
   if (!StepMatch || !L->isLoopInvariant(Step))
     return false;
 
   // Ensure GEP offsets are extended to the size of the PHI.
   const SCEV *StepSCEV = SE.getTruncateOrSignExtend(
-      SE.getSCEV(Step), SE.getEffectiveSCEVType(HeaderPHI->getType()));
+      SE.getSCEV(Step), SE.getEffectiveSCEVType(PN->getType()));
 
   if (StepSCEV->isZero())
     return false;
 
-  Value *Start = HeaderPHI->getIncomingValueForBlock(Preheader);
+  Value *Start = PN->getIncomingValueForBlock(Preheader);
   const SCEV *StartSCEV = SE.getSCEV(Start);
 
   SCEV::NoWrapFlags NoWrapFlags = SCEV::FlagAnyWrap;
@@ -1780,10 +1779,10 @@ bool MonotonicDescriptor::isMonotonicPHI(PHINode *HeaderPHI, const Loop *L,
       NoWrapFlags = ScalarEvolution::setFlags(NoWrapFlags, SCEV::FlagNSW);
   }
 
-  LLVM_DEBUG(dbgs() << "LV: Found a monotonic phi: HeaderPHI: " << *HeaderPHI
+  LLVM_DEBUG(dbgs() << "LV: Found a monotonic phi: HeaderPHI: " << *PN
                     << ", StepInst: " << *StepInst << "\n");
 
-  Desc = MonotonicDescriptor(HeaderPHI, BackedgePHI, StepInst, StartSCEV,
-                             StepSCEV, to_underlying(NoWrapFlags));
+  Desc = MonotonicDescriptor(PN, BackedgePHI, StepInst, StartSCEV, StepSCEV,
+                             NoWrapFlags);
   return true;
 }
diff --git a/llvm/unittests/Analysis/IVDescriptorsTest.cpp b/llvm/unittests/Analysis/IVDescriptorsTest.cpp
index 07677ee49c677..6b09354df8ced 100644
--- a/llvm/unittests/Analysis/IVDescriptorsTest.cpp
+++ b/llvm/unittests/Analysis/IVDescriptorsTest.cpp
@@ -500,7 +500,7 @@ for.end:
         auto *AR = cast<SCEVAddRecExpr>(OrigInSCEV);
         EXPECT_EQ(AR->getNoWrapFlags(), SCEV::FlagNUW | SCEV::FlagNW);
         // Check the expressions and wrap flags for the monotonic induction.
-        EXPECT_EQ(SCEV::NoWrapFlags(Desc.getSCEVNoWrapFlags()), SCEV::FlagNSW);
+        EXPECT_EQ(Desc.getSCEVNoWrapFlags(), SCEV::FlagNSW);
         EXPECT_EQ(Desc.getStartSCEV(), AR->getStart());
         EXPECT_EQ(Desc.getStepSCEV(), AR->getStepRecurrence(SE));
       });
@@ -560,7 +560,7 @@ for.end:
         auto *StepSCEV = SE.getConstant(StartSCEV->getType(), 4);
         EXPECT_EQ(Desc.getStartSCEV(), StartSCEV);
         EXPECT_EQ(Desc.getStepSCEV(), StepSCEV);
-        EXPECT_EQ(SCEV::NoWrapFlags(Desc.getSCEVNoWrapFlags()), SCEV::FlagNUW);
+        EXPECT_EQ(Desc.getSCEVNoWrapFlags(), SCEV::FlagNUW);
       });
 }
 
@@ -673,7 +673,6 @@ for.end:
         EXPECT_EQ(Desc.getStepSCEV(), StepSCEV);
 
         // Check we don't add `nuw` when we have a negative GEP step.
-        EXPECT_EQ(SCEV::NoWrapFlags(Desc.getSCEVNoWrapFlags()),
-                  SCEV::FlagAnyWrap);
+        EXPECT_EQ(Desc.getSCEVNoWrapFlags(), SCEV::FlagAnyWrap);
       });
 }

``````````

</details>


https://github.com/llvm/llvm-project/pull/223346


More information about the llvm-commits mailing list