[llvm] [IVDescriptors] Implement MonotonicDescriptor (PR #214490)

Luke Lau via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 25 08:52:14 PDT 2026


================
@@ -482,6 +483,54 @@ class InductionDescriptor {
   SmallVector<const SCEVPredicate *, 2> NoWrapPredicates;
 };
 
+/// A struct for saving information about monotonic variables.
+/// Monotonic variable can be considered as a "conditional" induction variable:
+/// its update happens only on loop iterations for which a certain predicate is
+/// satisfied. The step of the monotonic variable must be loop-invariant.
+class MonotonicDescriptor {
+public:
+  MonotonicDescriptor() = default;
+
+  MonotonicDescriptor(PHINode *HeaderPHI, PHINode *BackedgePHI,
+                      Instruction *StepInst, const SCEVAddRecExpr *PhiSCEV)
+      : HeaderPHI(HeaderPHI), BackedgePHI(BackedgePHI), StepInst(StepInst),
+        PhiSCEV(PhiSCEV) {}
+
+  /// 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
+  /// describing the PHI.
+  static bool isMonotonicPHI(PHINode *PN, const Loop *L,
+                             MonotonicDescriptor &Desc, ScalarEvolution &SE);
+
+  /// Returns the header PHI described by this descriptor.
+  PHINode *getHeaderPHI() const { return HeaderPHI; }
+
+  /// Returns the backedge PHI that selects between StepInst and the HeaderPHI.
+  PHINode *getBackedgePHI() const { return BackedgePHI; }
+
+  /// Returns the instruction that updates the value of the monotonic PHI.
+  Instruction *getStepInst() const { return StepInst; }
+
+  /// Returns the expression that represents the monotonic PHI. Note: The
+  /// conditional update is represented with a plain SCEVAddRec. This only holds
+  /// on iterations where the monotonic PHI is updated by StepInst.
+  const SCEVAddRecExpr *getPhiSCEV() const { return PhiSCEV; }
----------------
lukel97 wrote:

Why do we need to store this as a SCEVAddRecExpr if it's not really a SCEVAddRec? From reading https://github.com/llvm/llvm-project/pull/214491 it sounds like we only really need to access the step. Can we get away with just storing that instead?

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


More information about the llvm-commits mailing list