[llvm] [IVDescriptors] Implement MonotonicDescriptor (PR #214490)
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 24 04:06:03 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; }
+
+private:
+ /// The header PHI (this is the PHI described by the descriptor).
+ PHINode *HeaderPHI = nullptr;
+
+ /// The backedge PHI that selects between StepInst and the HeaderPHI.
+ PHINode *BackedgePHI = nullptr;
+
+ /// The instruction that updates the value of the monotonic PHI.
+ Instruction *StepInst = nullptr;
+
+ /// Expression that represents the monotonic PHI. Within the expression, the
----------------
fhahn wrote:
```suggestion
/// Expression that over-approximates the monotonic PHI as SCEVAddRec assuming the condition is always true.
```
Slightly clearer?
https://github.com/llvm/llvm-project/pull/214490
More information about the llvm-commits
mailing list