[llvm] [SCEV][LAA] Support multiplication overflow computation (PR #155236)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 25 05:56:10 PDT 2025


================
@@ -2337,15 +2337,21 @@ bool ScalarEvolution::willNotOverflow(Instruction::BinaryOps BinOp, bool Signed,
   // Can we use context to prove the fact we need?
   if (!CtxI)
     return false;
-  // TODO: Support mul.
-  if (BinOp == Instruction::Mul)
-    return false;
   auto *RHSC = dyn_cast<SCEVConstant>(RHS);
   // TODO: Lift this limitation.
   if (!RHSC)
     return false;
   APInt C = RHSC->getAPInt();
   unsigned NumBits = C.getBitWidth();
+  if (BinOp == Instruction::Mul) {
+    // Multiplying by 0 or 1 never overflows
+    if (C.isZero() || C.isOne())
+      return true;
+    auto Pred = Signed ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
----------------
nikic wrote:

This is not correct for signed (you'd allow a large negative number).

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


More information about the llvm-commits mailing list