[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:27 PST 2026
================
@@ -8155,6 +8218,88 @@ Instruction *InstCombinerImpl::foldICmpCommutative(CmpPredicate Pred,
}
}
+// Folds that each require Op0 to be a particular operator.
+Instruction *InstCombinerImpl::foldICmpByOp0(ICmpInst &I) {
+ Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
+ ICmpInst::Predicate Pred = I.getPredicate();
+ switch (Operator::getOpcode(Op0)) {
+ case Instruction::BitCast:
+ if (Instruction *Res = foldICmpBitCast(I))
+ return Res;
+ if (Instruction *Res = foldICmpWithCastOp(I))
+ return Res;
+ return foldReductionIdiom(I, Builder, DL);
+
+ case Instruction::Trunc:
+ case Instruction::ZExt:
+ case Instruction::SExt:
+ case Instruction::FPToUI:
+ case Instruction::FPToSI:
+ case Instruction::UIToFP:
+ case Instruction::SIToFP:
+ case Instruction::FPTrunc:
+ case Instruction::FPExt:
+ case Instruction::PtrToInt:
+ case Instruction::PtrToAddr:
+ case Instruction::IntToPtr:
+ case Instruction::AddrSpaceCast:
+ // TODO: Hoist this above the min/max bailout.
+ return foldICmpWithCastOp(I);
+
+ case Instruction::And: {
+ // icmp (and (trunc Y), Mask), (zext X) -> icmp trunc Y, X IFF Mask exactly
+ // covers the bits of X
+ Value *X, *Y;
+ const APInt *Mask;
+ if (match(Op1, m_ZExt(m_Value(X))) &&
+ match(Op0, m_OneUse(m_And(m_Trunc(m_Value(Y)), m_APInt(Mask))))) {
+ Type *SmallType = X->getType();
+ unsigned SmallWidth = SmallType->getScalarSizeInBits();
+ if (Mask->isMask(SmallWidth) &&
+ shouldChangeType(Op0->getType(), SmallType)) {
+ Value *NewTrunc = Builder.CreateTrunc(Y, SmallType);
+ return new ICmpInst(I.getUnsignedPredicate(), NewTrunc, X);
+ }
+ }
+ return nullptr;
+ }
+
+ case Instruction::ExtractValue:
+ // icmp X, (inttoptr (ptrtoint P)) --> icmp X, P
+ if (isa<IntToPtrInst>(Op1))
+ if (Instruction *Res = foldICmpWithCastOp(I))
+ return Res;
+
+ // The 'cmpxchg' instruction returns an aggregate containing the old value
+ // and an i1 which indicates whether or not we successfully did the swap.
+ //
+ // Replace comparisons between the old value and the expected value with
+ // the indicator that 'cmpxchg' returns.
+ //
+ // N.B. This transform is only valid when the 'cmpxchg' is not permitted to
+ // spuriously fail. In those cases, the old value may equal the expected
+ // value but it is possible for the swap to not occur.
+ if (Pred == ICmpInst::ICMP_EQ)
+ if (auto *EVI = dyn_cast<ExtractValueInst>(Op0))
+ if (auto *ACXI =
+ dyn_cast<AtomicCmpXchgInst>(EVI->getAggregateOperand()))
+ if (EVI->getIndices()[0] == 0 && ACXI->getCompareOperand() == Op1 &&
+ !ACXI->isWeak())
+ return ExtractValueInst::Create(ACXI, 1);
+ return nullptr;
+
+ case Instruction::Call:
+ if (Instruction *Res = foldICmpInvariantGroup(I))
----------------
nikic wrote:
Another fold with zero RHS.
https://github.com/llvm/llvm-project/pull/244766
More information about the llvm-branch-commits
mailing list