[llvm-branch-commits] [llvm] [SCEV] Derive x >=s 0 from x <u y and y <=u 2^(n-1) (PR #231631)

Max Kazantsev via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Oct 21 17:57:48 PDT 2026


================
@@ -12239,15 +12239,28 @@ bool ScalarEvolution::isImpliedCondBalancedTypes(
     return CmpInst::isRelational(P2) &&
            P1 == ICmpInst::getFlippedSignednessPredicate(P2);
   };
-  if (IsSignFlippedPredicate(Pred, FoundPred)) {
+  // If swapping query's operands makes the predicates sign-flipped, do it. E.g.
+  // the query "x >s -1" becomes "-1 <s x" for the found condition "x <u y".
+  CmpPredicate SignFlippedPred = Pred;
+  const SCEV *SignFlippedLHS = LHS, *SignFlippedRHS = RHS;
+  bool IsSwapped = false;
+  if (!IsSignFlippedPredicate(Pred, FoundPred) &&
+      IsSignFlippedPredicate(ICmpInst::getSwappedCmpPredicate(Pred),
+                             FoundPred)) {
+    SignFlippedPred = ICmpInst::getSwappedCmpPredicate(Pred);
+    std::swap(SignFlippedLHS, SignFlippedRHS);
+    IsSwapped = true;
+  }
+  if (IsSignFlippedPredicate(SignFlippedPred, FoundPred)) {
     // Unsigned comparison is the same as signed comparison when both the
     // operands are non-negative or negative.
-    if (haveSameSign(FoundLHS, FoundRHS))
+    if (!IsSwapped && haveSameSign(FoundLHS, FoundRHS))
----------------
xortator wrote:

Why reject swapped case?

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


More information about the llvm-branch-commits mailing list