[llvm] 0e7b754 - [ValueTracking] Squash compile-time regression from 66badf2 (#122700)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 14 11:57:40 PST 2025
Author: Ramkumar Ramachandra
Date: 2025-01-14T19:57:36Z
New Revision: 0e7b754ecc2ccb1d88fd929c6a198ba0f693d098
URL: https://github.com/llvm/llvm-project/commit/0e7b754ecc2ccb1d88fd929c6a198ba0f693d098
DIFF: https://github.com/llvm/llvm-project/commit/0e7b754ecc2ccb1d88fd929c6a198ba0f693d098.diff
LOG: [ValueTracking] Squash compile-time regression from 66badf2 (#122700)
66badf2 (VT: teach a special-case optz about samesign) introduced a
compile-time regression due to the use of CmpPredicate::getMatching,
which is unnecessarily inefficient. Introduce
CmpPredicate::getPreferredSignedPredicate, which alleviates the
inefficiency problem and squashes the compile-time regression.
Added:
Modified:
llvm/include/llvm/IR/CmpPredicate.h
llvm/lib/Analysis/ValueTracking.cpp
llvm/lib/IR/Instructions.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/IR/CmpPredicate.h b/llvm/include/llvm/IR/CmpPredicate.h
index 9aa1449465f5ff..6921afbc58d252 100644
--- a/llvm/include/llvm/IR/CmpPredicate.h
+++ b/llvm/include/llvm/IR/CmpPredicate.h
@@ -53,6 +53,12 @@ class CmpPredicate {
static std::optional<CmpPredicate> getMatching(CmpPredicate A,
CmpPredicate B);
+ /// Attempts to return a signed CmpInst::Predicate from the CmpPredicate. If
+ /// the CmpPredicate has samesign, return ICmpInst::getSignedPredicate,
+ /// dropping samesign information. Otherwise, return the predicate, dropping
+ /// samesign information.
+ CmpInst::Predicate getPreferredSignedPredicate() const;
+
/// An operator== on the underlying Predicate.
bool operator==(CmpInst::Predicate P) const { return Pred == P; }
bool operator!=(CmpInst::Predicate P) const { return Pred != P; }
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index d03e6f5a5754d5..1bab9b32525c33 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -9482,8 +9482,9 @@ isImpliedCondICmps(const ICmpInst *LHS, CmpPredicate RPred, const Value *R0,
// must be positive if X >= Y and no overflow".
// Take SGT as an example: L0:x > L1:y and C >= 0
// ==> R0:(x -nsw y) < R1:(-C) is false
- if ((CmpPredicate::getMatching(LPred, ICmpInst::ICMP_SGT) ||
- CmpPredicate::getMatching(LPred, ICmpInst::ICMP_SGE)) &&
+ CmpInst::Predicate SignedLPred = LPred.getPreferredSignedPredicate();
+ if ((SignedLPred == ICmpInst::ICMP_SGT ||
+ SignedLPred == ICmpInst::ICMP_SGE) &&
match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
if (match(R1, m_NonPositive()) &&
ICmpInst::isImpliedByMatchingCmp(LPred, RPred) == false)
@@ -9492,8 +9493,8 @@ isImpliedCondICmps(const ICmpInst *LHS, CmpPredicate RPred, const Value *R0,
// Take SLT as an example: L0:x < L1:y and C <= 0
// ==> R0:(x -nsw y) < R1:(-C) is true
- if ((CmpPredicate::getMatching(LPred, ICmpInst::ICMP_SLT) ||
- CmpPredicate::getMatching(LPred, ICmpInst::ICMP_SLE)) &&
+ if ((SignedLPred == ICmpInst::ICMP_SLT ||
+ SignedLPred == ICmpInst::ICMP_SLE) &&
match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
if (match(R1, m_NonNegative()) &&
ICmpInst::isImpliedByMatchingCmp(LPred, RPred) == true)
diff --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp
index 2408da410b4019..50560e9cf218e4 100644
--- a/llvm/lib/IR/Instructions.cpp
+++ b/llvm/lib/IR/Instructions.cpp
@@ -3950,6 +3950,10 @@ std::optional<CmpPredicate> CmpPredicate::getMatching(CmpPredicate A,
return {};
}
+CmpInst::Predicate CmpPredicate::getPreferredSignedPredicate() const {
+ return HasSameSign ? ICmpInst::getSignedPredicate(Pred) : Pred;
+}
+
CmpPredicate CmpPredicate::get(const CmpInst *Cmp) {
if (auto *ICI = dyn_cast<ICmpInst>(Cmp))
return ICI->getCmpPredicate();
More information about the llvm-commits
mailing list