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

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 22 22:53:32 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:

Yeah it should work for all constants. It even holds if we replace the `and` with any other instructions. I'd like to focus on the original motivating issue if you don't have a better idea to generalize the pattern. With this constraint, we can simplify `((X & Mask) op zext trunc X)` into `X` directly, instead of creating a temporary instruction and relying on later optimizations.


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


More information about the llvm-commits mailing list