[llvm] [InstCombine] Fold reconstruction across select (PR #145102)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 22 01:36:23 PDT 2025


================
@@ -1349,6 +1349,40 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I,
     return nullptr;
   };
 
+  // Special case for reconstructing across a select:
+  // (Cond ? V1 : (X & Mask)) op
+  // zext (Cond ? V2 : trunc X)
+  // -> (Cond ? (V1 op zext V2) : ((X & Mask) op zext trunc X))
+  auto foldReconstruction = [&](Value *V1, Value *Masked,
+                                Value *ZExtSel) -> Value * {
+    if (Opcode != Instruction::Or)
+      return nullptr;
+
+    Value *X;
+    if (!match(Masked, m_OneUse(m_And(m_Value(X), m_Constant()))))
----------------
dtcxzyw wrote:

```suggestion
    if (!match(Masked, m_OneUse(m_And(m_Value(X), m_APInt(*C)))))
```
Missing check for the mask. It should be `APInt::getBitsSetFrom(X->getType()->getScalarSizeInBits(), Trunc->getType()->getScalarSizeInBits())`.


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


More information about the llvm-commits mailing list