Skip to content

[IR] Add helper CmpPredicate::dropSameSign #134071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions llvm/include/llvm/IR/CmpPredicate.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class CmpPredicate {
/// 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==.
///
Expand Down
7 changes: 3 additions & 4 deletions llvm/lib/Analysis/ScalarEvolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Loading