Skip to content

Commit e57f9eb

Browse files
committed
CmpInst: merge functions; address review
1 parent 7bcf6c9 commit e57f9eb

File tree

3 files changed

+14
-28
lines changed

3 files changed

+14
-28
lines changed

llvm/include/llvm/IR/InstrTypes.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -912,15 +912,11 @@ class CmpInst : public Instruction {
912912
/// Determine if this is an equals/not equals predicate.
913913
bool isEquality() const { return isEquality(getPredicate()); }
914914

915-
/// Determine if this is an equals predicate that is also an equivalence. This
916-
/// is useful in GVN-like transformations, where we can replace RHS by LHS in
917-
/// the true branch of the CmpInst.
918-
bool isEqEquivalence() const;
919-
920-
/// Determine if this is a not-equals predicate that is also an equivalence.
921-
/// This is useful in GVN-like transformations, where we can replace RHS by
922-
/// LHS in the false branch of the CmpInst.
923-
bool isNeEquivalence() const;
915+
/// Determine if one operand of this compare can always be replaced by the
916+
/// other operand, ignoring provenance considerations. If \p Invert is false,
917+
/// check for equivalence with an equals predicate; otherwise, check for
918+
/// equivalence with a not-equals predicate.
919+
bool isEquivalence(bool Invert = false) const;
924920

925921
/// Return true if the predicate is relational (not EQ or NE).
926922
static bool isRelational(Predicate P) { return !isEquality(P); }

llvm/lib/IR/Instructions.cpp

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3487,34 +3487,24 @@ static bool hasNonZeroFPOperands(const CmpInst *Cmp) {
34873487
// Floating-point equality is not an equivalence when comparing +0.0 with
34883488
// -0.0, when comparing NaN with another value, or when flushing
34893489
// denormals-to-zero.
3490-
bool CmpInst::isEqEquivalence() const {
3490+
bool CmpInst::isEquivalence(bool Invert) const {
34913491
switch (getPredicate()) {
34923492
case CmpInst::Predicate::ICMP_EQ:
3493-
return true;
3493+
return !Invert;
3494+
case CmpInst::Predicate::ICMP_NE:
3495+
return Invert;
34943496
case CmpInst::Predicate::FCMP_UEQ:
34953497
if (!hasNoNaNs())
34963498
return false;
34973499
[[fallthrough]];
34983500
case CmpInst::Predicate::FCMP_OEQ:
3499-
return hasNonZeroFPOperands(this);
3500-
default:
3501-
return false;
3502-
}
3503-
}
3504-
3505-
// Floating-point equality is not an equivalence when comparing +0.0 with
3506-
// -0.0, when comparing NaN with another value, or when flushing
3507-
// denormals-to-zero.
3508-
bool CmpInst::isNeEquivalence() const {
3509-
switch (getPredicate()) {
3510-
case CmpInst::Predicate::ICMP_NE:
3511-
return true;
3501+
return !Invert && hasNonZeroFPOperands(this);
35123502
case CmpInst::Predicate::FCMP_ONE:
35133503
if (!hasNoNaNs())
35143504
return false;
35153505
[[fallthrough]];
35163506
case CmpInst::Predicate::FCMP_UNE:
3517-
return hasNonZeroFPOperands(this);
3507+
return Invert && hasNonZeroFPOperands(this);
35183508
default:
35193509
return false;
35203510
}

llvm/lib/Transforms/Scalar/GVN.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,7 +2090,7 @@ bool GVNPass::processAssumeIntrinsic(AssumeInst *IntrinsicI) {
20902090
// call void @llvm.assume(i1 %cmp)
20912091
// ret float %load ; will change it to ret float %0
20922092
if (auto *CmpI = dyn_cast<CmpInst>(V)) {
2093-
if (CmpI->isEqEquivalence()) {
2093+
if (CmpI->isEquivalence()) {
20942094
Value *CmpLHS = CmpI->getOperand(0);
20952095
Value *CmpRHS = CmpI->getOperand(1);
20962096
// Heuristically pick the better replacement -- the choice of heuristic
@@ -2514,8 +2514,8 @@ bool GVNPass::propagateEquality(Value *LHS, Value *RHS,
25142514
// If "A == B" is known true, or "A != B" is known false, then replace
25152515
// A with B everywhere in the scope. For floating point operations, we
25162516
// have to be careful since equality does not always imply equivalance.
2517-
if ((isKnownTrue && Cmp->isEqEquivalence()) ||
2518-
(isKnownFalse && Cmp->isNeEquivalence()))
2517+
if ((isKnownTrue && Cmp->isEquivalence()) ||
2518+
(isKnownFalse && Cmp->isEquivalence(/* Invert = */ true)))
25192519
Worklist.push_back(std::make_pair(Op0, Op1));
25202520

25212521
// If "A >= B" is known true, replace "A < B" with false everywhere.

0 commit comments

Comments
 (0)