Skip to content

Commit 00f0381

Browse files
committed
[InstCombine] Refactor foldSelectICmpAndOr to use decomposeBitTestICmp instead of bespoke logic
This is essentially NFC as the cases `decomposeBitTestICmp` covers that weren't already covered explicitly, will be canonicalized into the cases explicitly covered. As well the unsigned cases don't apply as the `Mask` is not a power of 2. That being said, using a well established helper is less bug prone and if some canonicalization changes, will prevent regressions here. Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D148744
1 parent 82292d1 commit 00f0381

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -714,8 +714,8 @@ static Value *foldSelectICmpAndOr(const ICmpInst *IC, Value *TrueVal,
714714
Value *CmpRHS = IC->getOperand(1);
715715

716716
unsigned C1Log;
717-
bool IsEqualZero;
718717
bool NeedAnd = false;
718+
CmpInst::Predicate Pred = IC->getPredicate();
719719
if (IC->isEquality()) {
720720
if (!match(CmpRHS, m_Zero()))
721721
return nullptr;
@@ -725,17 +725,13 @@ static Value *foldSelectICmpAndOr(const ICmpInst *IC, Value *TrueVal,
725725
return nullptr;
726726

727727
C1Log = C1->logBase2();
728-
IsEqualZero = IC->getPredicate() == ICmpInst::ICMP_EQ;
729728
} else {
730-
// We also need to recognize (icmp slt X, 0) and (icmp sgt X, -1).
731-
if (IC->getPredicate() == ICmpInst::ICMP_SGT && match(CmpRHS, m_AllOnes()))
732-
IsEqualZero = true;
733-
if (IC->getPredicate() == ICmpInst::ICMP_SLT && match(CmpRHS, m_Zero()))
734-
IsEqualZero = false;
735-
else
729+
APInt C1;
730+
if (!decomposeBitTestICmp(CmpLHS, CmpRHS, Pred, CmpLHS, C1) ||
731+
!C1.isPowerOf2())
736732
return nullptr;
737733

738-
C1Log = CmpLHS->getType()->getScalarSizeInBits() - 1;
734+
C1Log = C1.logBase2();
739735
NeedAnd = true;
740736
}
741737

@@ -745,11 +741,11 @@ static Value *foldSelectICmpAndOr(const ICmpInst *IC, Value *TrueVal,
745741
if (match(FalseVal, m_Or(m_Specific(TrueVal), m_Power2(C2)))) {
746742
Y = TrueVal;
747743
Or = FalseVal;
748-
NeedXor = !IsEqualZero;
744+
NeedXor = Pred == ICmpInst::ICMP_NE;
749745
} else if (match(TrueVal, m_Or(m_Specific(FalseVal), m_Power2(C2)))) {
750746
Y = FalseVal;
751747
Or = TrueVal;
752-
NeedXor = IsEqualZero;
748+
NeedXor = Pred == ICmpInst::ICMP_EQ;
753749
} else {
754750
return nullptr;
755751
}

0 commit comments

Comments
 (0)