Skip to content

Commit a512c89

Browse files
committed
[NFC][InstCombine] Refactor '(-NSW x) pred x' fold
1 parent 1413576 commit a512c89

File tree

1 file changed

+7
-53
lines changed

1 file changed

+7
-53
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3725,60 +3725,14 @@ Instruction *foldICmpXNegX(ICmpInst &I) {
37253725
if (!match(&I, m_c_ICmp(Pred, m_NSWNeg(m_Value(X)), m_Deferred(X))))
37263726
return nullptr;
37273727

3728-
CmpInst::Predicate NewPred;
3729-
Constant *NewRHS;
3730-
switch (Pred) {
3731-
case ICmpInst::ICMP_SGT:
3732-
NewPred = ICmpInst::ICMP_SLT;
3733-
NewRHS = Constant::getNullValue(X->getType());
3734-
break;
3735-
3736-
case ICmpInst::ICMP_SGE:
3737-
NewPred = ICmpInst::ICMP_SLE;
3738-
NewRHS = Constant::getNullValue(X->getType());
3739-
break;
3740-
3741-
case ICmpInst::ICMP_SLT:
3742-
NewPred = ICmpInst::ICMP_SGT;
3743-
NewRHS = Constant::getNullValue(X->getType());
3744-
break;
3745-
3746-
case ICmpInst::ICMP_SLE:
3747-
NewPred = ICmpInst::ICMP_SGE;
3748-
NewRHS = Constant::getNullValue(X->getType());
3749-
break;
3750-
3751-
case ICmpInst::ICMP_UGT:
3752-
NewPred = ICmpInst::ICMP_SGT;
3753-
NewRHS = Constant::getNullValue(X->getType());
3754-
break;
3755-
3756-
case ICmpInst::ICMP_UGE:
3757-
NewPred = ICmpInst::ICMP_SGE;
3758-
NewRHS = Constant::getNullValue(X->getType());
3759-
break;
3760-
3761-
case ICmpInst::ICMP_ULT:
3762-
NewPred = ICmpInst::ICMP_SLT;
3763-
NewRHS = Constant::getNullValue(X->getType());
3764-
break;
3765-
3766-
case ICmpInst::ICMP_ULE:
3767-
NewPred = ICmpInst::ICMP_SLE;
3768-
NewRHS = Constant::getNullValue(X->getType());
3769-
break;
3770-
3771-
case ICmpInst::ICMP_EQ:
3772-
case ICmpInst::ICMP_NE:
3773-
NewPred = Pred;
3774-
NewRHS = Constant::getNullValue(X->getType());
3775-
break;
3776-
3777-
default:
3778-
return nullptr;
3779-
}
3728+
if (ICmpInst::isSigned(Pred))
3729+
Pred = ICmpInst::getSwappedPredicate(Pred);
3730+
else if (ICmpInst::isUnsigned(Pred))
3731+
Pred = ICmpInst::getSignedPredicate(Pred);
3732+
// else for equality-comparisons just keep the predicate.
37803733

3781-
return ICmpInst::Create(Instruction::ICmp, NewPred, X, NewRHS, I.getName());
3734+
return ICmpInst::Create(Instruction::ICmp, Pred, X,
3735+
Constant::getNullValue(X->getType()), I.getName());
37823736
}
37833737

37843738
/// Try to fold icmp (binop), X or icmp X, (binop).

0 commit comments

Comments
 (0)