[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:47 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;
----------------
xortator wrote:

I don't like how this `IsSwapped` here get used so down below in the code. Better factor the whole thing out. Should be something like
```
    if (proveViaSignFlippedPredicate(Pred, LHS, RHS, FoundPred, ...))
      return true;
    if (proveViaSignFlippedPredicate(ICmpInst::getSwappedCmpPredicate(Pred), RHS, LHS, FoundPred ...))
      return true;
...
bool proveViaSignFlippedPredicate(...) {
  if (!IsSignFlippedPredicate(Pred, FoundPred))
    return false;
  // do all your stuff
}
```
No need to corrupt local data or track this new 'swapped' fact down below. Would that work?

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


More information about the llvm-branch-commits mailing list