[llvm] [InstCombine] Improve folding of `icmp pred (and X, Mask/~Mask), Y)` (PR #81562)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Mar 9 16:41:23 PST 2024
================
@@ -4081,21 +4171,45 @@ Instruction *InstCombinerImpl::foldSelectICmp(ICmpInst::Predicate Pred,
/// The Mask can be a constant, too.
/// For some predicates, the operands are commutative.
/// For others, x can only be on a specific side.
-static Value *foldICmpWithLowBitMaskedVal(ICmpInst &I,
- InstCombiner::BuilderTy &Builder) {
+static Value *foldICmpWithLowBitMaskedVal(ICmpInst &I, const SimplifyQuery &Q,
+ InstCombiner &IC) {
+
+ Value *X, *M;
+ ICmpInst::Predicate Pred = I.getPredicate();
ICmpInst::Predicate SrcPred;
- Value *X, *M, *Y;
- auto m_VariableMask = m_CombineOr(
- m_CombineOr(m_Not(m_Shl(m_AllOnes(), m_Value())),
- m_Add(m_Shl(m_One(), m_Value()), m_AllOnes())),
- m_CombineOr(m_LShr(m_AllOnes(), m_Value()),
- m_LShr(m_Shl(m_AllOnes(), m_Value(Y)), m_Deferred(Y))));
- auto m_Mask = m_CombineOr(m_VariableMask, m_LowBitMask());
- if (!match(&I, m_c_ICmp(SrcPred,
- m_c_And(m_CombineAnd(m_Mask, m_Value(M)), m_Value(X)),
- m_Deferred(X))))
- return nullptr;
+ bool NeedsNot = false;
+
+ auto CheckMask = [&](Value *V, bool Not) {
+ if (!ICmpInst::isSigned(Pred))
+ return isMaskOrZero(V, Not, Q);
+ return Not ? match(V, m_NegatedPower2OrZero())
+ : match(V, m_LowBitMaskOrZero());
+ };
+
+ auto TryMatch = [&](unsigned OpNo) {
+ SrcPred = Pred;
+ if (match(I.getOperand(OpNo),
+ m_c_And(m_Specific(I.getOperand(1 - OpNo)), m_Value(M)))) {
+ X = I.getOperand(1 - OpNo);
+ if (OpNo)
+ SrcPred = ICmpInst::getSwappedPredicate(Pred);
+ return CheckMask(M, /*Not*/ false);
+ }
+ if (OpNo == 1 && match(I.getOperand(1), m_Zero()) &&
+ ICmpInst::isEquality(Pred) &&
+ match(I.getOperand(0), m_OneUse(m_And(m_Value(X), m_Value(M))))) {
+ NeedsNot = true;
+ if (IC.isFreeToInvert(X, X->hasOneUse()) && CheckMask(X, /*Not*/ true)) {
+ std::swap(X, M);
+ return true;
+ }
+ return IC.isFreeToInvert(M, M->hasOneUse()) && CheckMask(M, /*Not*/ true);
+ }
+ return false;
+ };
+ if (!TryMatch(0) && !TryMatch(1))
----------------
goldsteinn wrote:
Done, commit to do so is split into NFC.
https://github.com/llvm/llvm-project/pull/81562
More information about the llvm-commits
mailing list