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

Macsen Casaus via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 23 04:13:20 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()))))
----------------
macsencasaus wrote:

To generalize, this optimization already occurs when the condition is arbitrary:
https://godbolt.org/z/8EPEP81TP

It just doesn't work when the condition is an `icmp` because of  this check:
https://github.com/llvm/llvm-project/blob/cfcb7888c71c7b5468e3fc55b6de0804403dc3fe/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp#L178-L186

I didn't know how to implement the optimization from here, but this may be an area where a more general optimization might occur.

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


More information about the llvm-commits mailing list