Skip to content

Commit ed57ea5

Browse files
committed
[InstCombine] fold icmp ne (and X, 1), 0 --> trunc X to N x i1
1 parent 21f04b1 commit ed57ea5

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -1768,11 +1768,8 @@ Instruction *InstCombinerImpl::foldICmpAndConstConst(ICmpInst &Cmp,
17681768
const APInt &C1) {
17691769
bool isICMP_NE = Cmp.getPredicate() == ICmpInst::ICMP_NE;
17701770

1771-
// For vectors: icmp ne (and X, 1), 0 --> trunc X to N x i1
1772-
// TODO: We canonicalize to the longer form for scalars because we have
1773-
// better analysis/folds for icmp, and codegen may be better with icmp.
1774-
if (isICMP_NE && Cmp.getType()->isVectorTy() && C1.isZero() &&
1775-
match(And->getOperand(1), m_One()))
1771+
// icmp ne (and X, 1), 0 --> trunc X to N x i1
1772+
if (isICMP_NE && C1.isZero() && match(And->getOperand(1), m_One()))
17761773
return new TruncInst(And->getOperand(0), Cmp.getType());
17771774

17781775
const APInt *C2;
@@ -6778,6 +6775,13 @@ Instruction *InstCombinerImpl::foldICmpUsingKnownBits(ICmpInst &I) {
67786775
break;
67796776
case ICmpInst::ICMP_EQ:
67806777
case ICmpInst::ICMP_NE: {
6778+
if (Ty->isIntOrIntVectorTy() && Op0Known.countMaxActiveBits() <= 1)
6779+
if ((Pred == CmpInst::ICMP_NE && Op1Known.isZero()) ||
6780+
(Pred == CmpInst::ICMP_EQ && Op1Known.isConstant() &&
6781+
Op1Known.getConstant().isOne()))
6782+
return replaceInstUsesWith(
6783+
I, Builder.CreateTrunc(Op0, I.getType(), "", /*IsNUW=*/true));
6784+
67816785
// If all bits are known zero except for one, then we know at most one bit
67826786
// is set. If the comparison is against zero, then this is a check to see if
67836787
// *that* bit is set.

0 commit comments

Comments
 (0)