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

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


================
@@ -1696,3 +1696,88 @@ bool InductionDescriptor::isInductionPHI(
                           /*InductionBinOp=*/nullptr, /*Casts=*/nullptr, Preds);
   return true;
 }
+
+// Recognize monotonic phi variable by matching the following pattern:
+// loop_header:
+//   %monotonic_phi = phi [ %start, %preheader ], [ %chain_phi0, %latch ]
+//   br i1 %do_step, label %step_bb, label %latch
+//
+// step_bb:
+//   %step = add/gep %monotonic_phi, %step_val
+//   br label %latch
+//
+// latch:
+//   %chain_phi0 = phi [ %monotonic_phi, %loop_header ], [ %step, %step_bb ]
+//   br label %loop_header
+//
+// For this pattern, monotonic phi is described by {%start, +, %step}
+// recurrence and predicate is CFG edge %step_bb -> %latch.
+bool MonotonicDescriptor::isMonotonicPHI(PHINode *HeaderPHI, const Loop *L,
+                                         MonotonicDescriptor &Desc,
+                                         ScalarEvolution &SE) {
+  if (!L->getLoopPreheader() || !HeaderPHI->getType()->isIntOrPtrTy() ||
+      HeaderPHI->getParent() != L->getHeader())
+    return false;
+  auto *BackedgePHI =
+      dyn_cast<PHINode>(HeaderPHI->getIncomingValueForBlock(L->getLoopLatch()));
----------------
lukel97 wrote:

Are we checking anywhere that the loop only has one latch? I think this will crash on a multi latch loop

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


More information about the llvm-commits mailing list