Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions llvm/include/llvm/IR/CmpPredicate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 getSignedPredicate() 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; }
Expand Down
9 changes: 5 additions & 4 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9495,8 +9495,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.getSignedPredicate();
if ((SignedLPred == ICmpInst::ICMP_SGT ||
SignedLPred == ICmpInst::ICMP_SGE) &&
match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
if (match(R1, m_NonPositive()) &&
isImpliedCondMatchingOperands(LPred, RPred) == false)
Expand All @@ -9505,8 +9506,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()) &&
isImpliedCondMatchingOperands(LPred, RPred) == true)
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/IR/Instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3939,6 +3939,10 @@ std::optional<CmpPredicate> CmpPredicate::getMatching(CmpPredicate A,
return {};
}

CmpInst::Predicate CmpPredicate::getSignedPredicate() const {
return HasSameSign ? ICmpInst::getSignedPredicate(Pred) : Pred;
}

CmpPredicate CmpPredicate::get(const CmpInst *Cmp) {
if (auto *ICI = dyn_cast<ICmpInst>(Cmp))
return ICI->getCmpPredicate();
Expand Down
Loading