[llvm] [SCEV] BECount to zero if `((-1 + (1 smax %x))<nsw> /u %x)` holds (PR #104580)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 16 05:53:29 PDT 2024


================
@@ -3547,6 +3547,25 @@ const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS,
     }
   }
 
+  // ((-1 + (1 smax %x))<nsw> /u %x) evaluates to zero, for any positive %x.
+  if (const auto *AE = dyn_cast<SCEVAddExpr>(LHS);
+      AE && AE->getNumOperands() == 2 && AE->hasNoSignedWrap()) {
+    bool FoundMinusOne = false, FoundOneSMaxRHS = false;
+    for (const SCEV *Op : AE->operands()) {
+      if (Op->isAllOnesValue()) {
+        FoundMinusOne = true;
+      } else if (const auto *MME = dyn_cast<SCEVMinMaxExpr>(Op)) {
----------------
nikic wrote:

Directly check SCEVSMinExpr?

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


More information about the llvm-commits mailing list