Skip to content

Commit 65ed353

Browse files
authored
[IR] Add helper CmpPredicate::dropSameSign (#134071)
Address review comment #133711 (comment)
1 parent f186041 commit 65ed353

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

llvm/include/llvm/IR/CmpPredicate.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ class CmpPredicate {
4141
/// Query samesign information, for optimizations.
4242
bool hasSameSign() const { return HasSameSign; }
4343

44+
/// Drops samesign information. This is used when the samesign information
45+
/// should be dropped explicitly.
46+
CmpInst::Predicate dropSameSign() const { return Pred; }
47+
4448
/// Compares two CmpPredicates taking samesign into account and returns the
4549
/// canonicalized CmpPredicate if they match. An alternative to operator==.
4650
///

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11622,7 +11622,7 @@ bool ScalarEvolution::isBasicBlockEntryGuardedByCond(const BasicBlock *BB,
1162211622
// to prove non-equality and non-strict comparison separately.
1162311623
CmpPredicate NonStrictPredicate = ICmpInst::getNonStrictCmpPredicate(Pred);
1162411624
const bool ProvingStrictComparison =
11625-
(Pred != static_cast<CmpInst::Predicate>(NonStrictPredicate));
11625+
Pred != NonStrictPredicate.dropSameSign();
1162611626
bool ProvedNonStrictComparison = false;
1162711627
bool ProvedNonEquality = false;
1162811628

@@ -11792,9 +11792,8 @@ bool ScalarEvolution::isImpliedCond(CmpPredicate Pred, const SCEV *LHS,
1179211792
const SCEV *TruncFoundLHS = getTruncateExpr(FoundLHS, NarrowType);
1179311793
const SCEV *TruncFoundRHS = getTruncateExpr(FoundRHS, NarrowType);
1179411794
// We cannot preserve samesign after truncation.
11795-
if (isImpliedCondBalancedTypes(
11796-
Pred, LHS, RHS, static_cast<ICmpInst::Predicate>(FoundPred),
11797-
TruncFoundLHS, TruncFoundRHS, CtxI))
11795+
if (isImpliedCondBalancedTypes(Pred, LHS, RHS, FoundPred.dropSameSign(),
11796+
TruncFoundLHS, TruncFoundRHS, CtxI))
1179811797
return true;
1179911798
}
1180011799
}

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5641,7 +5641,7 @@ Instruction *InstCombinerImpl::foldICmpWithMinMax(Instruction &I,
56415641
// execute comparisons. For example, `icmp samesign ult umax(X, -46), -32`
56425642
// cannot be decomposed into `(icmp samesign ult X, -46) or (icmp samesign ult
56435643
// -46, -32)`. `X` is allowed to be non-negative here.
5644-
Pred = static_cast<CmpInst::Predicate>(Pred);
5644+
Pred = Pred.dropSameSign();
56455645
auto CmpXZ = IsCondKnownTrue(simplifyICmpInst(Pred, X, Z, Q));
56465646
auto CmpYZ = IsCondKnownTrue(simplifyICmpInst(Pred, Y, Z, Q));
56475647
if (!CmpXZ.has_value() && !CmpYZ.has_value())

0 commit comments

Comments
 (0)