[llvm] [InstCombine] Reassociate commutative binops over select/phi/minmax (PR #187493)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sun May 24 02:48:34 PDT 2026


================
@@ -1356,6 +1382,35 @@ InstCombinerImpl::matchSymmetricPair(Value *LHS, Value *RHS) {
   }
 }
 
+bool InstCombinerImpl::tryReassociateAndFoldSymmetricPair(
+    BinaryOperator &OuterOp, BinaryOperator &InnerOp, Value *InnerVal0,
+    Value *InnerVal1, Value *OuterVal) {
+  if (!OuterOp.isCommutative() || !OuterOp.isAssociative() ||
+      !InnerOp.isCommutative() || !InnerOp.isAssociative() ||
+      !InnerOp.hasOneUse() || OuterOp.getOpcode() != InnerOp.getOpcode())
+    return false;
+
+  unsigned OuterValIdx;
+  if (OuterOp.getOperand(0) == OuterVal)
+    OuterValIdx = 0;
+  else if (OuterOp.getOperand(1) == OuterVal)
+    OuterValIdx = 1;
+  else
+    return false; // OuterVal must be an operand of OuterOp
+
+  auto Pair = matchSymmetricPair(InnerVal1, OuterVal);
+  if (Pair) {
----------------
dtcxzyw wrote:

```suggestion
  if (auto Pair = matchSymmetricPair(InnerVal1, OuterVal)) {
```

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


More information about the llvm-commits mailing list