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

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed May 28 09:54:26 PDT 2025


================
@@ -10748,6 +10748,22 @@ bool ScalarEvolution::SimplifyICmpOperands(CmpPredicate &Pred, const SCEV *&LHS,
   if (Depth >= 3)
     return false;
 
+  // (X * Z) icmp (Y * Z) ==> X icmp Y
+  //     when neither multiply wraps and Z is positive.
+  if (isa<SCEVMulExpr>(LHS) && isa<SCEVMulExpr>(RHS)) {
+    const SCEVMulExpr *LMul = cast<SCEVMulExpr>(LHS);
+    const SCEVMulExpr *RMul = cast<SCEVMulExpr>(RHS);
+
+    if (LMul->getNumOperands() == 2 && RMul->getNumOperands() == 2 &&
+        LMul->getOperand(1) == RMul->getOperand(1) &&
+        isKnownPositive(LMul->getOperand(1)) && ICmpInst::isUnsigned(Pred) &&
----------------
nikic wrote:

This isKnownPositive should be isKnownNonZero? https://alive2.llvm.org/ce/z/7jN-bG

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


More information about the llvm-commits mailing list