[llvm] [SCEV] Don't require NUW at first add when checking A+C1 < (A+C2)<nuw> (PR #149795)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 22 07:40:08 PDT 2025


================
@@ -11428,13 +11427,24 @@ bool ScalarEvolution::isKnownPredicateViaNoOverflow(CmpPredicate Pred,
       YFlagsPresent = ExpectedFlags;
     }
 
-    if (!isa<SCEVConstant>(YConstOp) ||
-        (YFlagsPresent & ExpectedFlags) != ExpectedFlags)
+    if (YNonConstOp != XNonConstOp)
       return false;
 
-    if (YNonConstOp != XNonConstOp)
+    if (!isa<SCEVConstant>(YConstOp))
       return false;
 
+    // When matching ADDs with NUW flags (and unsigned predicates), only the
+    // second ADD (with the larger constant) requires NUW.
+    if (YNonConstOp != Y && ExpectedFlags == SCEV::FlagNUW) {
----------------
nikic wrote:

Why is this `YNonConstOp != Y` check necessary? If they're the same (and YConstOp zero) then the flags are initialized to match and the check should pass anywa?

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


More information about the llvm-commits mailing list