[llvm] [InstCombine] Fold `(select C, (x bin_op a), x) bin_op b` into `x bin_op select C, (a bin_op b), b` (PR #173511)

Gábor Spaits via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 26 23:40:10 PST 2025


================
@@ -1876,6 +1876,47 @@ static Value *simplifyInstructionWithPHI(Instruction &I, PHINode *PN,
   return nullptr;
 }
 
+// In some cases it is beneficial to fold a select into a binary operator.
+// For example:
+//   %1 = or %in, 4
+//   %2 = select %cond, %1, %in
+//   %3 = or %2, 1
+// =>
+//   %1 = select i1 %cond, 5, 1
+//   %2 = or %1, %in
+Instruction *InstCombinerImpl::foldSelectIntoBinOp(BinaryOperator &Op) {
+  assert(Op.isAssociative() && "The operation must be associative!");
+
+  SelectInst *SI = dyn_cast<SelectInst>(Op.getOperand(0));
+  Constant *Const = dyn_cast<Constant>(Op.getOperand(1));
+  if (!SI || !Const || !Op.hasOneUse() || !SI->hasOneUse())
+    return nullptr;
+
+  // TODO: Maybe hasOneUse the other bin op too?
----------------
spaits wrote:

I quickly checked a version without the one use check. Here is the regression for test case `test_chr_7`.
<img width="1636" height="422" alt="image" src="https://github.com/user-attachments/assets/9aa10c36-3b62-4acc-835d-d1f778eccf40" />

So for this patch I would prefer to keep all of the one use check.

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


More information about the llvm-commits mailing list