[llvm] [SCEV] BECount to zero if `((-C + (C smax %x)) /u %x), C > 0` holds (PR #104580)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 4 12:04:48 PDT 2024


================
@@ -3547,6 +3547,25 @@ const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS,
     }
   }
 
+  // ((-C + (C smax %x)) /u %x) evaluates to zero, for any positive %x and any
+  // positive constant C.
+  if (const auto *AE = dyn_cast<SCEVAddExpr>(LHS);
+      AE && AE->getNumOperands() == 2) {
+    if (const auto *VC = dyn_cast<SCEVConstant>(AE->getOperand(0))) {
+      APInt NegC = VC->getAPInt();
+      if (NegC.isNegative() &&
+          NegC != APInt::getSignedMinValue(NegC.getBitWidth())) {
+        const auto *MME = dyn_cast<SCEVSMaxExpr>(AE->getOperand(1));
+        if (MME && MME->getNumOperands() == 2) {
+          if (isa<SCEVConstant>(MME->getOperand(0)) &&
+              cast<SCEVConstant>(MME->getOperand(0))->getAPInt() == -NegC)
+            if (MME->getOperand(1) == RHS)
----------------
nikic wrote:

Merge the three ifs.

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


More information about the llvm-commits mailing list