[llvm] [LLVM][SCEV] Look through common multiplicand when simplifying compares. (PR #141798)

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 5 11:15:03 PDT 2025


================
@@ -10748,6 +10748,63 @@ bool ScalarEvolution::SimplifyICmpOperands(CmpPredicate &Pred, const SCEV *&LHS,
   if (Depth >= 3)
     return false;
 
+  if (isa<SCEVMulExpr>(LHS) && isa<SCEVMulExpr>(RHS)) {
+    const SCEVMulExpr *LMul = cast<SCEVMulExpr>(LHS);
+    const SCEVMulExpr *RMul = cast<SCEVMulExpr>(RHS);
+
+    auto FindCommonFactor =
+        [&](const SCEVMulExpr *LHS, const SCEVMulExpr *RHS,
+            bool (ScalarEvolution::*Predicate)(
+                const SCEV *)) -> std::optional<std::pair<int, int>> {
+      for (int i = 0, e = LHS->getNumOperands(); i != e; ++i)
+        for (int j = 0, e = RHS->getNumOperands(); j != e; ++j)
+          if (LHS->getOperand(i) == RHS->getOperand(j) &&
+              (this->*Predicate)(LHS->getOperand(i)))
+            return std::make_pair(i, j);
----------------
artagnon wrote:

I'd say: match the pattern and simplify the patch.

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


More information about the llvm-commits mailing list