[llvm] [InstCombine] Combine and->cmp->sel->or-disjoint into and->mul (PR #135274)

Andreas Jonson via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 15 14:13:14 PDT 2025


================
@@ -3643,6 +3643,58 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
             foldAddLikeCommutative(I.getOperand(1), I.getOperand(0),
                                    /*NSW=*/true, /*NUW=*/true))
       return R;
+
+    Value *Cond0 = nullptr, *Cond1 = nullptr;
+    const APInt *Op0Eq = nullptr, *Op0Ne = nullptr;
+    const APInt *Op1Eq = nullptr, *Op1Ne = nullptr;
+
+    //  (!(A & N) ? 0 : N * C) + (!(A & M) ? 0 : M * C) -> A & (N + M) * C
+    if (match(I.getOperand(0),
+              m_Select(m_Value(Cond0), m_APInt(Op0Eq), m_APInt(Op0Ne))) &&
+        match(I.getOperand(1),
+              m_Select(m_Value(Cond1), m_APInt(Op1Eq), m_APInt(Op1Ne)))) {
+      CmpPredicate Pred0, Pred1;
+
+      if (ICmpInst *ICL = dyn_cast<ICmpInst>(Cond0);
+          ICmpInst *ICR = dyn_cast<ICmpInst>(Cond1)) {
----------------
andjo403 wrote:

this do not check that Cond0 is a ICmpInst as it is only the condition expression that is converted to bool not the init-statement see https://en.cppreference.com/w/cpp/language/if and https://godbolt.org/z/sE86Wzrbx

but also do not think that this if shall be here and decomposeBitTest to be called instead of decomposeBitTestICmp as it will handle the trunc to i1 case also see this example https://alive2.llvm.org/ce/z/wKAGdb

https://github.com/llvm/llvm-project/pull/135274


More information about the llvm-commits mailing list