Skip to content

Commit 0e7b754

Browse files
authored
[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.
1 parent 44d9bee commit 0e7b754

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

llvm/include/llvm/IR/CmpPredicate.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ class CmpPredicate {
5353
static std::optional<CmpPredicate> getMatching(CmpPredicate A,
5454
CmpPredicate B);
5555

56+
/// Attempts to return a signed CmpInst::Predicate from the CmpPredicate. If
57+
/// the CmpPredicate has samesign, return ICmpInst::getSignedPredicate,
58+
/// dropping samesign information. Otherwise, return the predicate, dropping
59+
/// samesign information.
60+
CmpInst::Predicate getPreferredSignedPredicate() const;
61+
5662
/// An operator== on the underlying Predicate.
5763
bool operator==(CmpInst::Predicate P) const { return Pred == P; }
5864
bool operator!=(CmpInst::Predicate P) const { return Pred != P; }

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9482,8 +9482,9 @@ isImpliedCondICmps(const ICmpInst *LHS, CmpPredicate RPred, const Value *R0,
94829482
// must be positive if X >= Y and no overflow".
94839483
// Take SGT as an example: L0:x > L1:y and C >= 0
94849484
// ==> R0:(x -nsw y) < R1:(-C) is false
9485-
if ((CmpPredicate::getMatching(LPred, ICmpInst::ICMP_SGT) ||
9486-
CmpPredicate::getMatching(LPred, ICmpInst::ICMP_SGE)) &&
9485+
CmpInst::Predicate SignedLPred = LPred.getPreferredSignedPredicate();
9486+
if ((SignedLPred == ICmpInst::ICMP_SGT ||
9487+
SignedLPred == ICmpInst::ICMP_SGE) &&
94879488
match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
94889489
if (match(R1, m_NonPositive()) &&
94899490
ICmpInst::isImpliedByMatchingCmp(LPred, RPred) == false)
@@ -9492,8 +9493,8 @@ isImpliedCondICmps(const ICmpInst *LHS, CmpPredicate RPred, const Value *R0,
94929493

94939494
// Take SLT as an example: L0:x < L1:y and C <= 0
94949495
// ==> R0:(x -nsw y) < R1:(-C) is true
9495-
if ((CmpPredicate::getMatching(LPred, ICmpInst::ICMP_SLT) ||
9496-
CmpPredicate::getMatching(LPred, ICmpInst::ICMP_SLE)) &&
9496+
if ((SignedLPred == ICmpInst::ICMP_SLT ||
9497+
SignedLPred == ICmpInst::ICMP_SLE) &&
94979498
match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
94989499
if (match(R1, m_NonNegative()) &&
94999500
ICmpInst::isImpliedByMatchingCmp(LPred, RPred) == true)

llvm/lib/IR/Instructions.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3950,6 +3950,10 @@ std::optional<CmpPredicate> CmpPredicate::getMatching(CmpPredicate A,
39503950
return {};
39513951
}
39523952

3953+
CmpInst::Predicate CmpPredicate::getPreferredSignedPredicate() const {
3954+
return HasSameSign ? ICmpInst::getSignedPredicate(Pred) : Pred;
3955+
}
3956+
39533957
CmpPredicate CmpPredicate::get(const CmpInst *Cmp) {
39543958
if (auto *ICI = dyn_cast<ICmpInst>(Cmp))
39553959
return ICI->getCmpPredicate();

0 commit comments

Comments
 (0)