[llvm-branch-commits] [llvm] [InstCombine] Move more icmp folds keyed on Op0 into the switch (PR #244766)
Nikita Popov via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sun Nov 22 12:18:26 PST 2026
================
@@ -4556,6 +4562,63 @@ Instruction *InstCombinerImpl::foldICmpInstWithConstantNotInt(ICmpInst &I) {
cast<LoadInst>(LHSI), I.getPredicate(), RHSC, I))
return Res;
break;
+
+ case Instruction::And: {
+ Value *X, *Y;
+ // Transform (X & ~Y) == 0 --> (X & Y) != 0
+ // and (X & ~Y) != 0 --> (X & Y) == 0
+ // if A is a power of 2.
+ if (match(Op0, m_And(m_Value(X), m_Not(m_Value(Y)))) &&
+ match(Op1, m_Zero()) && isKnownToBeAPowerOfTwo(X, false, &I) &&
+ I.isEquality())
+ return new ICmpInst(I.getInversePredicate(), Builder.CreateAnd(X, Y),
+ Op1);
+
+ Value *A;
+ const APInt *C1, *C2;
+ ICmpInst::Predicate Pred = I.getPredicate();
+ if (ICmpInst::isEquality(Pred)) {
+ // sext(a) & c1 == c2 --> a & c3 == trunc(c2)
+ // sext(a) & c1 != c2 --> a & c3 != trunc(c2)
+ if (match(Op0, m_And(m_SExt(m_Value(A)), m_APInt(C1))) &&
+ match(Op1, m_APInt(C2))) {
+ Type *InputTy = A->getType();
+ unsigned InputBitWidth = InputTy->getScalarSizeInBits();
+ // c2 must be non-negative at the bitwidth of a.
+ if (C2->getActiveBits() < InputBitWidth) {
+ APInt TruncC1 = C1->trunc(InputBitWidth);
+ // Check if there are 1s in C1 high bits of size InputBitWidth.
+ if (C1->uge(APInt::getOneBitSet(C1->getBitWidth(), InputBitWidth)))
+ TruncC1.setBit(InputBitWidth - 1);
+ Value *AndInst = Builder.CreateAnd(A, TruncC1);
+ return new ICmpInst(
+ Pred, AndInst,
+ ConstantInt::get(InputTy, C2->trunc(InputBitWidth)));
+ }
+ }
+ }
+ break;
+ }
+
+ case Instruction::Add:
+ case Instruction::Mul: {
+ // (zext X) + (zext Y) --> add + overflow check.
+ // (zext X) * (zext Y) --> llvm.umul.with.overflow.
+ Value *X, *Y;
+ const APInt *C;
+ if ((match(Op0, m_NUWAdd(m_ZExt(m_Value(X)), m_ZExt(m_Value(Y)))) ||
+ match(Op0, m_NUWMul(m_ZExt(m_Value(X)), m_ZExt(m_Value(Y))))) &&
+ match(Op1, m_APInt(C))) {
----------------
nikic wrote:
Why is this in foldICmpInstWithConstantNotInt? This looks like it should be in foldICmpInstWithConstant to me?
Similar for the others, with foldICmpInstWithConstantAllowPoison also being an option, depending.
https://github.com/llvm/llvm-project/pull/244766
More information about the llvm-branch-commits
mailing list