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

Gaƫtan Bossu via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 11 02:50:06 PDT 2026


================
@@ -1696,3 +1696,125 @@ bool InductionDescriptor::isInductionPHI(
                           /*InductionBinOp=*/nullptr, /*Casts=*/nullptr, Preds);
   return true;
 }
+
+bool MonotonicDescriptor::setSCEV(const SCEV *NewExpr) {
+  auto *AddRec = dyn_cast<SCEVAddRecExpr>(NewExpr);
+  if (!AddRec || !AddRec->isAffine())
+    return false;
+  Expr = AddRec;
+  return true;
+}
+
+// Recognize monotonic phi variable by matching the following pattern:
+// loop_header:
+//   %monotonic_phi = [%start, %preheader], [%chain_phi0, %latch]
+//
+// step_bb:
+//   %step = add/gep %monotonic_phi, %step_val
+//
+// bbN:
+//   %chain_phiN = [%monotonic_phi, ], [%step, ]
+//
+// ...
+//
+// bb1:
+//   %chain_phi1 = [%monotonic_phi, ], [%chain_phi2, ]
+//
+// latch:
+//   %chain_phi0 = [%monotonic_phi, %pred], [%chain_phi1, %pred]
+//
+// For this pattern, monotonic phi is described by {%start, +, %step} recurrence
+// and predicate is CFG edge %step_bb -> %bbN.
+bool MonotonicDescriptor::isMonotonicPHI(PHINode *PN, const Loop *L,
+                                         MonotonicDescriptor &Desc,
+                                         ScalarEvolution &SE) {
----------------
gbossu wrote:

One of the things which makes the code harder to read is that we start off with a "blank" `MonotonicDescriptor` and then we update its state as we process instructions.

I think constructing `MonotonicDescriptor` should be the final step, once we've done all the analysis to identify the `Chain`, `StepInst`, `PredEdge` and SCEV. Those parameters should be passed to a non-default `MonotonicDescriptor` constructor.

Maybe rework this function into a `static MonotonicDescriptor MonotonicDescriptor::get(...)`, which would either return a default-constructed `MonotonicDescriptor`, or a valid one with all the data. AFAIK this is similar to what is done for `RecurrenceDescriptor`.

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


More information about the llvm-commits mailing list