[llvm-branch-commits] [llvm] [SCEV] Prove monotonic predicates from loop entry in isKnownViaInduction (PR #231697)

Max Kazantsev via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Oct 20 08:05:04 PDT 2026


================
@@ -11533,26 +11533,41 @@ ScalarEvolution::getMonotonicPredicateTypeImpl(const SCEVAddRecExpr *LHS,
   return std::nullopt;
 }
 
-std::optional<ScalarEvolution::LoopInvariantPredicate>
-ScalarEvolution::getLoopInvariantPredicate(CmpPredicate Pred, const SCEV *LHS,
-                                           const SCEV *RHS, const Loop *L,
-                                           const Instruction *CtxI) {
+std::optional<ScalarEvolution::MonotonicPredicate>
+ScalarEvolution::getMonotonicPredicate(CmpPredicate Pred, const SCEV *LHS,
+                                       const SCEV *RHS, const Loop *L) {
   // If there is a loop-invariant, force it into the RHS, otherwise bail out.
+  bool Swapped = false;
   if (!isLoopInvariant(RHS, L)) {
     if (!isLoopInvariant(LHS, L))
       return std::nullopt;
 
     std::swap(LHS, RHS);
     Pred = ICmpInst::getSwappedCmpPredicate(Pred);
+    Swapped = true;
   }
 
-  const SCEVAddRecExpr *ArLHS = dyn_cast<SCEVAddRecExpr>(LHS);
-  if (!ArLHS || ArLHS->getLoop() != L)
+  const auto *AR = dyn_cast<SCEVAddRecExpr>(LHS);
+  if (!AR || AR->getLoop() != L)
     return std::nullopt;
 
-  auto MonotonicType = getMonotonicPredicateType(ArLHS, Pred);
-  if (!MonotonicType)
+  auto Type = getMonotonicPredicateType(AR, Pred);
+  if (!Type)
+    return std::nullopt;
+  return MonotonicPredicate{Pred, AR, RHS, *Type, Swapped};
+}
+
+std::optional<ScalarEvolution::LoopInvariantPredicate>
+ScalarEvolution::getLoopInvariantPredicate(CmpPredicate Pred, const SCEV *LHS,
+                                           const SCEV *RHS, const Loop *L,
+                                           const Instruction *CtxI) {
+  auto MP = getMonotonicPredicate(Pred, LHS, RHS, L);
+  if (!MP)
     return std::nullopt;
+  Pred = MP->Pred;
+  const SCEVAddRecExpr *ArLHS = MP->AR;
+  LHS = ArLHS;
----------------
xortator wrote:

In old code, `ArLHS` was a dyn-casted `LHS`, and it was clear they are the same. Now ArLHS is computed and may differ from what it was.

Maybe it must be a follow-up, but only one of `ArLHS, LHS` should exist.

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


More information about the llvm-branch-commits mailing list