[IR] Add helper CmpPredicate::dropSameSign (#134071)

Address review comment
https://github.com/llvm/llvm-project/pull/133711#discussion_r2024519641
This commit is contained in:
Yingwei Zheng 2025-04-02 22:25:01 +08:00 committed by GitHub
parent f186041553
commit 65ed35393c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 8 additions and 5 deletions

View File

@ -41,6 +41,10 @@ public:
/// Query samesign information, for optimizations.
bool hasSameSign() const { return HasSameSign; }
/// Drops samesign information. This is used when the samesign information
/// should be dropped explicitly.
CmpInst::Predicate dropSameSign() const { return Pred; }
/// Compares two CmpPredicates taking samesign into account and returns the
/// canonicalized CmpPredicate if they match. An alternative to operator==.
///

View File

@ -11622,7 +11622,7 @@ bool ScalarEvolution::isBasicBlockEntryGuardedByCond(const BasicBlock *BB,
// to prove non-equality and non-strict comparison separately.
CmpPredicate NonStrictPredicate = ICmpInst::getNonStrictCmpPredicate(Pred);
const bool ProvingStrictComparison =
(Pred != static_cast<CmpInst::Predicate>(NonStrictPredicate));
Pred != NonStrictPredicate.dropSameSign();
bool ProvedNonStrictComparison = false;
bool ProvedNonEquality = false;
@ -11792,9 +11792,8 @@ bool ScalarEvolution::isImpliedCond(CmpPredicate Pred, const SCEV *LHS,
const SCEV *TruncFoundLHS = getTruncateExpr(FoundLHS, NarrowType);
const SCEV *TruncFoundRHS = getTruncateExpr(FoundRHS, NarrowType);
// We cannot preserve samesign after truncation.
if (isImpliedCondBalancedTypes(
Pred, LHS, RHS, static_cast<ICmpInst::Predicate>(FoundPred),
TruncFoundLHS, TruncFoundRHS, CtxI))
if (isImpliedCondBalancedTypes(Pred, LHS, RHS, FoundPred.dropSameSign(),
TruncFoundLHS, TruncFoundRHS, CtxI))
return true;
}
}

View File

@ -5641,7 +5641,7 @@ Instruction *InstCombinerImpl::foldICmpWithMinMax(Instruction &I,
// execute comparisons. For example, `icmp samesign ult umax(X, -46), -32`
// cannot be decomposed into `(icmp samesign ult X, -46) or (icmp samesign ult
// -46, -32)`. `X` is allowed to be non-negative here.
Pred = static_cast<CmpInst::Predicate>(Pred);
Pred = Pred.dropSameSign();
auto CmpXZ = IsCondKnownTrue(simplifyICmpInst(Pred, X, Z, Q));
auto CmpYZ = IsCondKnownTrue(simplifyICmpInst(Pred, Y, Z, Q));
if (!CmpXZ.has_value() && !CmpYZ.has_value())