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

Benjamin Maxwell via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 28 03:35:45 PDT 2026


================
@@ -1696,3 +1696,95 @@ bool InductionDescriptor::isInductionPHI(
                           /*InductionBinOp=*/nullptr, /*Casts=*/nullptr, Preds);
   return true;
 }
+
+// Recognize a monotonic PHI variable by matching the following pattern:
+// loop_header:
+//   %monotonic_phi = phi [ %start, %preheader ], [ %latch_phi, %latch ]
+//   br i1 %do_step, label %step_bb, label %latch
+//
+// step_bb:
+//   %step = add/gep %monotonic_phi, %step_val
+//   br label %latch
+//
+// latch:
+//   %latch_phi = phi [ %monotonic_phi, %loop_header ], [ %step, %step_bb ]
+//   br label %loop_header
+bool MonotonicDescriptor::isMonotonicPHI(PHINode *HeaderPHI, const Loop *L,
+                                         MonotonicDescriptor &Desc,
+                                         ScalarEvolution &SE) {
+  BasicBlock *Preheader = L->getLoopPreheader();
+  if (!Preheader)
+    return false;
+
+  BasicBlock *Latch = L->getLoopLatch();
+  if (!Latch || !HeaderPHI->getType()->isIntOrPtrTy() ||
+      HeaderPHI->getParent() != L->getHeader())
+    return false;
+
+  auto *BackedgePHI =
+      dyn_cast<PHINode>(HeaderPHI->getIncomingValueForBlock(Latch));
+  if (!BackedgePHI)
+    return false;
+
+  // Ensure the only users of the backedge PHI are outside the loop or the
+  // header PHI.
+  for (User *U : BackedgePHI->users()) {
+    auto *UI = dyn_cast<Instruction>(U);
+    if (UI && UI != HeaderPHI && L->contains(UI))
+      return false;
+  }
+
+  // Find the step operation used to increment the value of the monotonic PHI.
+  // TODO: Support chains of PHIs.
+  Value *StepOp = find_singleton<Value>(
+      BackedgePHI->incoming_values(),
+      [&](Use &Incoming, bool /*AllowRepeats*/) {
+        return Incoming != HeaderPHI ? Incoming.get() : nullptr;
+      });
----------------
MacDue wrote:

More the two predecessors should be allowed so long as there's only a single incoming value that's not the HeaderPHI. 

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


More information about the llvm-commits mailing list