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

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 12 02:03:37 PDT 2026


================
@@ -482,6 +482,68 @@ 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 SCEV *StartSCEV,
+                      const SCEV *StepSCEV, unsigned SCEVNoWrapFlags)
+      : HeaderPHI(HeaderPHI), BackedgePHI(BackedgePHI), StepInst(StepInst),
+        StartSCEV(StartSCEV), StepSCEV(StepSCEV),
+        SCEVNoWrapFlags(SCEVNoWrapFlags) {}
+
+  /// 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.
+  LLVM_ABI 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 a SCEV expression for the initial value of the monotonic PHI.
+  const SCEV *getStartSCEV() const { return StartSCEV; }
+
+  /// Returns a SCEV expression for the step of the monotonic PHI. This is
+  /// the value the monotonic PHI increments by on loop iterations where the
+  /// predicate is satisfied.
+  const SCEV *getStepSCEV() const { return StepSCEV; }
+
+  /// Returns the SCEV no-wrap flags that apply to StepInst.
+  unsigned getSCEVNoWrapFlags() const { return SCEVNoWrapFlags; }
----------------
fhahn wrote:

Fair enough, although it makes it a bit more difficult to review the logic regarding the flags without seeing how it will be used in practice.

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


More information about the llvm-commits mailing list