[llvm] [InstCombine] Simplify associative binary ops when there is a select between them (PR #172662)

Gábor Spaits via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 18 04:10:13 PST 2025


================
@@ -1822,12 +1822,25 @@ Instruction *InstCombinerImpl::FoldOpIntoSelect(Instruction &Op, SelectInst *SI,
   Value *NewTV = simplifyOperationIntoSelectOperand(Op, SI, /*IsTrueArm=*/true);
   Value *NewFV =
       simplifyOperationIntoSelectOperand(Op, SI, /*IsTrueArm=*/false);
-  if (!NewTV && !NewFV)
-    return nullptr;
 
   if (SimplifyBothArms && !(NewTV && NewFV))
     return nullptr;
 
+  // Addressing a special case here, when a possible associative simplification
+  // is "abstracted" by a select instruction. When this pattern occurs it is
+  // worth to fold both arms of the select regardless of possible
+  // simplifications. Like the bellow case:
+  //   %1 = or i8 %in, C1
+  //   %2 = select i1 %cond, i8 %1, i8 %in
+  //   %3 = or i8 %2, C2
+  Value *Input;
+  if (!NewTV && !NewFV &&
+      (!Op.isBinaryOp() ||
+       !(match(SI, m_c_Select(m_Value(Input),
----------------
spaits wrote:

Check if the binary operator's right arg is a constant!

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


More information about the llvm-commits mailing list