================
@@ -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:

UPD: this code is *only* valid if `getMonotonicPredicate` does nothing else but 
returns original LHS, RHS, which is wrong in general. Assert on this or change 
the code below to not have this reliance.

https://github.com/llvm/llvm-project/pull/231697
_______________________________________________
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to