[llvm] [InstCombine] Enable folding of (select C, (x bin_op a), x) bin_op b -> x bin_op select C, (a bin_op b), b (PR #172662)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 20 08:02:33 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 &&
----------------
dtcxzyw wrote:

Can you add some multi-use tests? We may create more instructions when %1 has extra users.

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


More information about the llvm-commits mailing list