[llvm] [SCEV] Sharpen BTC computations with minus-one overflow trick (PR #222939)
Ramkumar Ramachandra via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 11 07:01:06 PDT 2026
https://github.com/artagnon updated https://github.com/llvm/llvm-project/pull/222939
>From 770f6d8c91943a0fe54ec9269d891ebfb5ec3055 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Fri, 11 Sep 2026 13:16:07 +0100
Subject: [PATCH] [SCEV] Use equiv of X >= Y and X > Y - 1 to sharpen
known-pred
Add a special case for LHS >= RHS in isKnownPredicateWithNoOverflow in
order to sharpen the result.
---
llvm/lib/Analysis/ScalarEvolution.cpp | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 06f350fd2179b..1b9a652eb899a 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -11754,6 +11754,12 @@ bool ScalarEvolution::isKnownPredicateViaNoOverflow(CmpPredicate Pred,
break;
case ICmpInst::ICMP_SGE:
+ // X s>= Y is equivalent to X s> Y - 1, and this holds trivially when Y - 1
+ // does not overflow. When it does overflow, it must be SINT_MAX, and the
+ // comparison would evaluate to false anyway.
+ if (MatchBinaryAddToConst(LHS, RHS, C1, C2, SCEV::FlagNSW) && C2.isZero() &&
+ C1.sgt(APInt::getAllOnes(C2.getBitWidth())))
+ return true;
std::swap(LHS, RHS);
[[fallthrough]];
case ICmpInst::ICMP_SLE:
@@ -11774,6 +11780,12 @@ bool ScalarEvolution::isKnownPredicateViaNoOverflow(CmpPredicate Pred,
break;
case ICmpInst::ICMP_UGE:
+ // X u>= Y is equivalent to X u> Y - 1, and this holds trivially when Y - 1
+ // does not overflow. When it does overflow, it must be UINT_MAX, and the
+ // comparison would evaluate to false anyway.
+ if (MatchBinaryAddToConst(LHS, RHS, C1, C2, SCEV::FlagNUW) && C2.isZero() &&
+ C1.ugt(APInt::getAllOnes(C2.getBitWidth())))
+ return true;
std::swap(LHS, RHS);
[[fallthrough]];
case ICmpInst::ICMP_ULE:
More information about the llvm-commits
mailing list