Skip to content

Commit e95892f

Browse files
committed
Implement the fold
1 parent 9623cb9 commit e95892f

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3560,7 +3560,9 @@ static Instruction *foldBitCeil(SelectInst &SI, IRBuilderBase &Builder) {
35603560

35613561
// This function tries to fold the following operations:
35623562
// (x < y) ? -1 : zext(x != y)
3563+
// (x < y) ? -1 : zext(x > y)
35633564
// (x > y) ? 1 : sext(x != y)
3565+
// (x > y) ? 1 : sext(x < y)
35643566
// Into ucmp/scmp(x, y), where signedness is determined by the signedness
35653567
// of the comparison in the original sequence.
35663568
Instruction *InstCombinerImpl::foldSelectToCmp(SelectInst &SI) {
@@ -3589,16 +3591,23 @@ Instruction *InstCombinerImpl::foldSelectToCmp(SelectInst &SI) {
35893591
ICmpInst::isSigned(Pred) ? Intrinsic::scmp : Intrinsic::ucmp;
35903592

35913593
bool Replace = false;
3594+
ICmpInst::Predicate ExtendedCmpPredicate;
35923595
// (x < y) ? -1 : zext(x != y)
3596+
// (x < y) ? -1 : zext(x > y)
35933597
if (ICmpInst::isLT(Pred) && match(TV, m_AllOnes()) &&
3594-
match(FV, m_ZExt(m_c_SpecificICmp(ICmpInst::ICMP_NE, m_Specific(LHS),
3595-
m_Specific(RHS)))))
3598+
match(FV, m_ZExt(m_c_ICmp(ExtendedCmpPredicate, m_Specific(LHS),
3599+
m_Specific(RHS)))) &&
3600+
(ExtendedCmpPredicate == ICmpInst::ICMP_NE ||
3601+
ICmpInst::getSwappedPredicate(ExtendedCmpPredicate) == Pred))
35963602
Replace = true;
35973603

35983604
// (x > y) ? 1 : sext(x != y)
3605+
// (x > y) ? 1 : sext(x < y)
35993606
if (ICmpInst::isGT(Pred) && match(TV, m_One()) &&
3600-
match(FV, m_SExt(m_c_SpecificICmp(ICmpInst::ICMP_NE, m_Specific(LHS),
3601-
m_Specific(RHS)))))
3607+
match(FV, m_SExt(m_c_ICmp(ExtendedCmpPredicate, m_Specific(LHS),
3608+
m_Specific(RHS)))) &&
3609+
(ExtendedCmpPredicate == ICmpInst::ICMP_NE ||
3610+
ICmpInst::getSwappedPredicate(ExtendedCmpPredicate) == Pred))
36023611
Replace = true;
36033612

36043613
if (Replace)

0 commit comments

Comments
 (0)