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

Mitch Briles via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 7 01:34:05 PDT 2026


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

>From 5b77a8f29b1943676730d7fb196b6e5c1741eef1 Mon Sep 17 00:00:00 2001
From: Mitch <mitchbriles at gmail.com>
Date: Thu, 19 Mar 2026 04:11:10 -0600
Subject: [PATCH 01/16] Pre-patch tests

---
 .../commutative-operation-over-minmax.ll      | 114 ++++++++++++++++++
 1 file changed, 114 insertions(+)
 create mode 100644 llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll

diff --git a/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll b/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
new file mode 100644
index 0000000000000..239946ad1a4e4
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
@@ -0,0 +1,114 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -passes=instcombine -S < %s | FileCheck %s
+
+define i32 @sadd_min_max_assoc(i32 %0, i32 %1, i32 %2) {
+; CHECK-LABEL: define i32 @sadd_min_max_assoc(
+; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]]) {
+; CHECK-NEXT:    [[TMP4:%.*]] = call i32 @llvm.smin.i32(i32 [[TMP0]], i32 [[TMP1]])
+; CHECK-NEXT:    [[TMP5:%.*]] = add i32 [[TMP2]], [[TMP4]]
+; CHECK-NEXT:    [[TMP6:%.*]] = call i32 @llvm.smax.i32(i32 [[TMP0]], i32 [[TMP1]])
+; CHECK-NEXT:    [[TMP7:%.*]] = add i32 [[TMP5]], [[TMP6]]
+; CHECK-NEXT:    ret i32 [[TMP7]]
+;
+  %4 = call i32 @llvm.smin.i32(i32 %0, i32 %1)
+  %5 = add i32 %2, %4
+  %6 = call i32 @llvm.smax.i32(i32 %0, i32 %1)
+  %7 = add i32 %5, %6
+  ret i32 %7
+}
+
+define i32 @or_min_max_assoc(i32 %0, i32 %1, i32 %2) {
+; CHECK-LABEL: define i32 @or_min_max_assoc(
+; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]]) {
+; CHECK-NEXT:    [[TMP4:%.*]] = call i32 @llvm.smin.i32(i32 [[TMP0]], i32 [[TMP1]])
+; CHECK-NEXT:    [[TMP5:%.*]] = or i32 [[TMP2]], [[TMP4]]
+; CHECK-NEXT:    [[TMP6:%.*]] = call i32 @llvm.smax.i32(i32 [[TMP0]], i32 [[TMP1]])
+; CHECK-NEXT:    [[TMP7:%.*]] = or i32 [[TMP5]], [[TMP6]]
+; CHECK-NEXT:    ret i32 [[TMP7]]
+;
+  %4 = call i32 @llvm.smin.i32(i32 %0, i32 %1)
+  %5 = or i32 %2, %4
+  %6 = call i32 @llvm.smax.i32(i32 %0, i32 %1)
+  %7 = or i32 %5, %6
+  ret i32 %7
+}
+
+define i32 @smul_min_max_assoc(i32 %0, i32 %1, i32 %2) {
+; CHECK-LABEL: define i32 @smul_min_max_assoc(
+; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]]) {
+; CHECK-NEXT:    [[TMP4:%.*]] = call i32 @llvm.smin.i32(i32 [[TMP0]], i32 [[TMP1]])
+; CHECK-NEXT:    [[TMP5:%.*]] = mul i32 [[TMP2]], [[TMP4]]
+; CHECK-NEXT:    [[TMP6:%.*]] = call i32 @llvm.smax.i32(i32 [[TMP0]], i32 [[TMP1]])
+; CHECK-NEXT:    [[TMP7:%.*]] = mul i32 [[TMP5]], [[TMP6]]
+; CHECK-NEXT:    ret i32 [[TMP7]]
+;
+  %4 = call i32 @llvm.smin.i32(i32 %0, i32 %1)
+  %5 = mul i32 %2, %4
+  %6 = call i32 @llvm.smax.i32(i32 %0, i32 %1)
+  %7 = mul i32 %5, %6
+  ret i32 %7
+}
+
+define i32 @uadd_min_max_assoc(i32 %0, i32 %1, i32 %2) {
+; CHECK-LABEL: define i32 @uadd_min_max_assoc(
+; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]]) {
+; CHECK-NEXT:    [[TMP4:%.*]] = call i32 @llvm.umin.i32(i32 [[TMP0]], i32 [[TMP1]])
+; CHECK-NEXT:    [[TMP5:%.*]] = add i32 [[TMP2]], [[TMP4]]
+; CHECK-NEXT:    [[TMP6:%.*]] = call i32 @llvm.umax.i32(i32 [[TMP0]], i32 [[TMP1]])
+; CHECK-NEXT:    [[TMP7:%.*]] = add i32 [[TMP5]], [[TMP6]]
+; CHECK-NEXT:    ret i32 [[TMP7]]
+;
+  %4 = call i32 @llvm.umin.i32(i32 %0, i32 %1)
+  %5 = add i32 %2, %4
+  %6 = call i32 @llvm.umax.i32(i32 %0, i32 %1)
+  %7 = add i32 %5, %6
+  ret i32 %7
+}
+
+define i32 @umul_min_max_assoc(i32 %0, i32 %1, i32 %2) {
+; CHECK-LABEL: define i32 @umul_min_max_assoc(
+; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]]) {
+; CHECK-NEXT:    [[TMP4:%.*]] = call i32 @llvm.umin.i32(i32 [[TMP0]], i32 [[TMP1]])
+; CHECK-NEXT:    [[TMP5:%.*]] = mul i32 [[TMP2]], [[TMP4]]
+; CHECK-NEXT:    [[TMP6:%.*]] = call i32 @llvm.umax.i32(i32 [[TMP0]], i32 [[TMP1]])
+; CHECK-NEXT:    [[TMP7:%.*]] = mul i32 [[TMP5]], [[TMP6]]
+; CHECK-NEXT:    ret i32 [[TMP7]]
+;
+  %4 = call i32 @llvm.umin.i32(i32 %0, i32 %1)
+  %5 = mul i32 %2, %4
+  %6 = call i32 @llvm.umax.i32(i32 %0, i32 %1)
+  %7 = mul i32 %5, %6
+  ret i32 %7
+}
+
+define i32 @sadd_min_max_assoc_wrong1(i32 %0, i32 %1, i32 %2) {
+; CHECK-LABEL: define i32 @sadd_min_max_assoc_wrong1(
+; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]]) {
+; CHECK-NEXT:    [[TMP4:%.*]] = call i32 @llvm.smin.i32(i32 [[TMP0]], i32 [[TMP1]])
+; CHECK-NEXT:    [[TMP5:%.*]] = add i32 [[TMP2]], [[TMP4]]
+; CHECK-NEXT:    [[TMP6:%.*]] = call i32 @llvm.umax.i32(i32 [[TMP0]], i32 [[TMP1]])
+; CHECK-NEXT:    [[TMP7:%.*]] = add i32 [[TMP5]], [[TMP6]]
+; CHECK-NEXT:    ret i32 [[TMP7]]
+;
+  %4 = call i32 @llvm.smin.i32(i32 %0, i32 %1)
+  %5 = add i32 %2, %4
+  %6 = call i32 @llvm.umax.i32(i32 %0, i32 %1)
+  %7 = add i32 %5, %6
+  ret i32 %7
+}
+
+define i32 @sadd_min_max_assoc_wrong2(i32 %0, i32 %1, i32 %2) {
+; CHECK-LABEL: define i32 @sadd_min_max_assoc_wrong2(
+; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]]) {
+; CHECK-NEXT:    [[TMP4:%.*]] = call i32 @llvm.smin.i32(i32 [[TMP0]], i32 [[TMP1]])
+; CHECK-NEXT:    [[TMP5:%.*]] = mul i32 [[TMP2]], [[TMP4]]
+; CHECK-NEXT:    [[TMP6:%.*]] = call i32 @llvm.umax.i32(i32 [[TMP0]], i32 [[TMP1]])
+; CHECK-NEXT:    [[TMP7:%.*]] = add i32 [[TMP5]], [[TMP6]]
+; CHECK-NEXT:    ret i32 [[TMP7]]
+;
+  %4 = call i32 @llvm.smin.i32(i32 %0, i32 %1)
+  %5 = mul i32 %2, %4
+  %6 = call i32 @llvm.umax.i32(i32 %0, i32 %1)
+  %7 = add i32 %5, %6
+  ret i32 %7
+}

>From 43df0d37787dd3257f1b4c618f69667776bd0a8e Mon Sep 17 00:00:00 2001
From: Mitch <mitchbriles at gmail.com>
Date: Thu, 19 Mar 2026 06:04:36 -0600
Subject: [PATCH 02/16] [InstCombine] Reassociate commutative binops over
 select/phi/minmax

---
 .../InstCombine/InstructionCombining.cpp      | 14 +++++++++
 .../commutative-operation-over-minmax.ll      | 30 +++++++------------
 2 files changed, 24 insertions(+), 20 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 9182454b74bfb..74eacd5907400 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -517,6 +517,20 @@ bool InstCombinerImpl::SimplifyAssociativeOrCommutative(BinaryOperator &I) {
         Value *B = Op0->getOperand(1);
         Value *C = I.getOperand(1);
 
+        // Form "A op V" when "B op C" is a symmetric pair
+        if (I.isCommutative()) {
+          if (auto Pair = matchSymmetricPair(B, C)) {
+            replaceOperand(*Op0, 0, Pair->first);
+            replaceOperand(*Op0, 1, Pair->second);
+            replaceOperand(I, 1, A);
+            I.swapOperands();
+            ClearSubclassDataAfterReassociation(I);
+            Changed = true;
+            ++NumReassoc;
+            continue;
+          }
+        }
+
         // Does "B op C" simplify?
         if (Value *V = simplifyBinOp(Opcode, B, C, SQ.getWithInstruction(&I))) {
           // It simplifies to V.  Form "A op V".
diff --git a/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll b/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
index 239946ad1a4e4..dc85d3984c65b 100644
--- a/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
+++ b/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
@@ -4,11 +4,9 @@
 define i32 @sadd_min_max_assoc(i32 %0, i32 %1, i32 %2) {
 ; CHECK-LABEL: define i32 @sadd_min_max_assoc(
 ; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]]) {
-; CHECK-NEXT:    [[TMP4:%.*]] = call i32 @llvm.smin.i32(i32 [[TMP0]], i32 [[TMP1]])
+; CHECK-NEXT:    [[TMP4:%.*]] = add i32 [[TMP0]], [[TMP1]]
 ; CHECK-NEXT:    [[TMP5:%.*]] = add i32 [[TMP2]], [[TMP4]]
-; CHECK-NEXT:    [[TMP6:%.*]] = call i32 @llvm.smax.i32(i32 [[TMP0]], i32 [[TMP1]])
-; CHECK-NEXT:    [[TMP7:%.*]] = add i32 [[TMP5]], [[TMP6]]
-; CHECK-NEXT:    ret i32 [[TMP7]]
+; CHECK-NEXT:    ret i32 [[TMP5]]
 ;
   %4 = call i32 @llvm.smin.i32(i32 %0, i32 %1)
   %5 = add i32 %2, %4
@@ -20,11 +18,9 @@ define i32 @sadd_min_max_assoc(i32 %0, i32 %1, i32 %2) {
 define i32 @or_min_max_assoc(i32 %0, i32 %1, i32 %2) {
 ; CHECK-LABEL: define i32 @or_min_max_assoc(
 ; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]]) {
-; CHECK-NEXT:    [[TMP4:%.*]] = call i32 @llvm.smin.i32(i32 [[TMP0]], i32 [[TMP1]])
+; CHECK-NEXT:    [[TMP4:%.*]] = or i32 [[TMP0]], [[TMP1]]
 ; CHECK-NEXT:    [[TMP5:%.*]] = or i32 [[TMP2]], [[TMP4]]
-; CHECK-NEXT:    [[TMP6:%.*]] = call i32 @llvm.smax.i32(i32 [[TMP0]], i32 [[TMP1]])
-; CHECK-NEXT:    [[TMP7:%.*]] = or i32 [[TMP5]], [[TMP6]]
-; CHECK-NEXT:    ret i32 [[TMP7]]
+; CHECK-NEXT:    ret i32 [[TMP5]]
 ;
   %4 = call i32 @llvm.smin.i32(i32 %0, i32 %1)
   %5 = or i32 %2, %4
@@ -36,11 +32,9 @@ define i32 @or_min_max_assoc(i32 %0, i32 %1, i32 %2) {
 define i32 @smul_min_max_assoc(i32 %0, i32 %1, i32 %2) {
 ; CHECK-LABEL: define i32 @smul_min_max_assoc(
 ; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]]) {
-; CHECK-NEXT:    [[TMP4:%.*]] = call i32 @llvm.smin.i32(i32 [[TMP0]], i32 [[TMP1]])
+; CHECK-NEXT:    [[TMP4:%.*]] = mul i32 [[TMP0]], [[TMP1]]
 ; CHECK-NEXT:    [[TMP5:%.*]] = mul i32 [[TMP2]], [[TMP4]]
-; CHECK-NEXT:    [[TMP6:%.*]] = call i32 @llvm.smax.i32(i32 [[TMP0]], i32 [[TMP1]])
-; CHECK-NEXT:    [[TMP7:%.*]] = mul i32 [[TMP5]], [[TMP6]]
-; CHECK-NEXT:    ret i32 [[TMP7]]
+; CHECK-NEXT:    ret i32 [[TMP5]]
 ;
   %4 = call i32 @llvm.smin.i32(i32 %0, i32 %1)
   %5 = mul i32 %2, %4
@@ -52,11 +46,9 @@ define i32 @smul_min_max_assoc(i32 %0, i32 %1, i32 %2) {
 define i32 @uadd_min_max_assoc(i32 %0, i32 %1, i32 %2) {
 ; CHECK-LABEL: define i32 @uadd_min_max_assoc(
 ; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]]) {
-; CHECK-NEXT:    [[TMP4:%.*]] = call i32 @llvm.umin.i32(i32 [[TMP0]], i32 [[TMP1]])
+; CHECK-NEXT:    [[TMP4:%.*]] = add i32 [[TMP0]], [[TMP1]]
 ; CHECK-NEXT:    [[TMP5:%.*]] = add i32 [[TMP2]], [[TMP4]]
-; CHECK-NEXT:    [[TMP6:%.*]] = call i32 @llvm.umax.i32(i32 [[TMP0]], i32 [[TMP1]])
-; CHECK-NEXT:    [[TMP7:%.*]] = add i32 [[TMP5]], [[TMP6]]
-; CHECK-NEXT:    ret i32 [[TMP7]]
+; CHECK-NEXT:    ret i32 [[TMP5]]
 ;
   %4 = call i32 @llvm.umin.i32(i32 %0, i32 %1)
   %5 = add i32 %2, %4
@@ -68,11 +60,9 @@ define i32 @uadd_min_max_assoc(i32 %0, i32 %1, i32 %2) {
 define i32 @umul_min_max_assoc(i32 %0, i32 %1, i32 %2) {
 ; CHECK-LABEL: define i32 @umul_min_max_assoc(
 ; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]]) {
-; CHECK-NEXT:    [[TMP4:%.*]] = call i32 @llvm.umin.i32(i32 [[TMP0]], i32 [[TMP1]])
+; CHECK-NEXT:    [[TMP4:%.*]] = mul i32 [[TMP0]], [[TMP1]]
 ; CHECK-NEXT:    [[TMP5:%.*]] = mul i32 [[TMP2]], [[TMP4]]
-; CHECK-NEXT:    [[TMP6:%.*]] = call i32 @llvm.umax.i32(i32 [[TMP0]], i32 [[TMP1]])
-; CHECK-NEXT:    [[TMP7:%.*]] = mul i32 [[TMP5]], [[TMP6]]
-; CHECK-NEXT:    ret i32 [[TMP7]]
+; CHECK-NEXT:    ret i32 [[TMP5]]
 ;
   %4 = call i32 @llvm.umin.i32(i32 %0, i32 %1)
   %5 = mul i32 %2, %4

>From c39715315e5440e172dda29f9b26711a76e9ec10 Mon Sep 17 00:00:00 2001
From: Mitch <mitchbriles at gmail.com>
Date: Thu, 19 Mar 2026 06:49:32 -0600
Subject: [PATCH 03/16] Fix test names

---
 .../InstCombine/commutative-operation-over-minmax.ll      | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll b/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
index dc85d3984c65b..0f29322c5b174 100644
--- a/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
+++ b/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
@@ -71,8 +71,8 @@ define i32 @umul_min_max_assoc(i32 %0, i32 %1, i32 %2) {
   ret i32 %7
 }
 
-define i32 @sadd_min_max_assoc_wrong1(i32 %0, i32 %1, i32 %2) {
-; CHECK-LABEL: define i32 @sadd_min_max_assoc_wrong1(
+define i32 @add_min_max_assoc_wrong(i32 %0, i32 %1, i32 %2) {
+; CHECK-LABEL: define i32 @add_min_max_assoc_wrong(
 ; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]]) {
 ; CHECK-NEXT:    [[TMP4:%.*]] = call i32 @llvm.smin.i32(i32 [[TMP0]], i32 [[TMP1]])
 ; CHECK-NEXT:    [[TMP5:%.*]] = add i32 [[TMP2]], [[TMP4]]
@@ -87,8 +87,8 @@ define i32 @sadd_min_max_assoc_wrong1(i32 %0, i32 %1, i32 %2) {
   ret i32 %7
 }
 
-define i32 @sadd_min_max_assoc_wrong2(i32 %0, i32 %1, i32 %2) {
-; CHECK-LABEL: define i32 @sadd_min_max_assoc_wrong2(
+define i32 @mul_min_max_assoc_wrong(i32 %0, i32 %1, i32 %2) {
+; CHECK-LABEL: define i32 @mul_min_max_assoc_wrong(
 ; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]]) {
 ; CHECK-NEXT:    [[TMP4:%.*]] = call i32 @llvm.smin.i32(i32 [[TMP0]], i32 [[TMP1]])
 ; CHECK-NEXT:    [[TMP5:%.*]] = mul i32 [[TMP2]], [[TMP4]]

>From 30b1e9439f0fbb53ac6870c071b21b4b3b4ba908 Mon Sep 17 00:00:00 2001
From: Mitch <mitchbriles at gmail.com>
Date: Tue, 24 Mar 2026 17:33:53 -0600
Subject: [PATCH 04/16] Check hasOneUse

---
 .../InstCombine/InstructionCombining.cpp       |  2 +-
 .../commutative-operation-over-minmax.ll       | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 74eacd5907400..21a4d92d884f1 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -518,7 +518,7 @@ bool InstCombinerImpl::SimplifyAssociativeOrCommutative(BinaryOperator &I) {
         Value *C = I.getOperand(1);
 
         // Form "A op V" when "B op C" is a symmetric pair
-        if (I.isCommutative()) {
+        if (I.isCommutative() && Op0->hasOneUse()) {
           if (auto Pair = matchSymmetricPair(B, C)) {
             replaceOperand(*Op0, 0, Pair->first);
             replaceOperand(*Op0, 1, Pair->second);
diff --git a/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll b/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
index 0f29322c5b174..2878b1dae4ffa 100644
--- a/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
+++ b/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
@@ -87,6 +87,24 @@ define i32 @add_min_max_assoc_wrong(i32 %0, i32 %1, i32 %2) {
   ret i32 %7
 }
 
+define i32 @add_min_max_assoc_wrong2(i32 %0, i32 %1, i32 %2, ptr %p) {
+; CHECK-LABEL: define i32 @add_min_max_assoc_wrong2(
+; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]], ptr [[P:%.*]]) {
+; CHECK-NEXT:    [[TMP4:%.*]] = call i32 @llvm.smin.i32(i32 [[TMP0]], i32 [[TMP1]])
+; CHECK-NEXT:    [[TMP5:%.*]] = add i32 [[TMP2]], [[TMP4]]
+; CHECK-NEXT:    [[TMP6:%.*]] = call i32 @llvm.smax.i32(i32 [[TMP0]], i32 [[TMP1]])
+; CHECK-NEXT:    [[TMP7:%.*]] = add i32 [[TMP5]], [[TMP6]]
+; CHECK-NEXT:    store i32 [[TMP5]], ptr [[P]], align 4
+; CHECK-NEXT:    ret i32 [[TMP7]]
+;
+  %4 = call i32 @llvm.smin.i32(i32 %0, i32 %1)
+  %5 = add i32 %2, %4
+  %6 = call i32 @llvm.smax.i32(i32 %0, i32 %1)
+  %7 = add i32 %5, %6
+  store i32 %5, ptr %p
+  ret i32 %7
+}
+
 define i32 @mul_min_max_assoc_wrong(i32 %0, i32 %1, i32 %2) {
 ; CHECK-LABEL: define i32 @mul_min_max_assoc_wrong(
 ; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]]) {

>From b7f613bddbfa6a45fcfbd76347aba0493702451b Mon Sep 17 00:00:00 2001
From: Mitch <mitchbriles at gmail.com>
Date: Thu, 9 Apr 2026 02:03:09 -0600
Subject: [PATCH 05/16] Drop `nuw`/`nsw` + fixup tests

---
 .../InstCombine/InstructionCombining.cpp      |   4 +
 .../commutative-operation-over-minmax.ll      | 180 ++++++++++--------
 2 files changed, 108 insertions(+), 76 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 21a4d92d884f1..2de3a5b370535 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -522,6 +522,10 @@ bool InstCombinerImpl::SimplifyAssociativeOrCommutative(BinaryOperator &I) {
           if (auto Pair = matchSymmetricPair(B, C)) {
             replaceOperand(*Op0, 0, Pair->first);
             replaceOperand(*Op0, 1, Pair->second);
+            if (hasNoUnsignedWrap(*Op0))
+              Op0->setHasNoUnsignedWrap(false);
+            if (hasNoSignedWrap(*Op0))
+              Op0->setHasNoSignedWrap(false);
             replaceOperand(I, 1, A);
             I.swapOperands();
             ClearSubclassDataAfterReassociation(I);
diff --git a/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll b/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
index 2878b1dae4ffa..547aad7c6b164 100644
--- a/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
+++ b/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
@@ -1,122 +1,150 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
 ; RUN: opt -passes=instcombine -S < %s | FileCheck %s
 
-define i32 @sadd_min_max_assoc(i32 %0, i32 %1, i32 %2) {
+define i32 @sadd_min_max_assoc(i32 %x, i32 %y, i32 %z) {
 ; CHECK-LABEL: define i32 @sadd_min_max_assoc(
-; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]]) {
-; CHECK-NEXT:    [[TMP4:%.*]] = add i32 [[TMP0]], [[TMP1]]
-; CHECK-NEXT:    [[TMP5:%.*]] = add i32 [[TMP2]], [[TMP4]]
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[LHS:%.*]] = add i32 [[X]], [[Y]]
+; CHECK-NEXT:    [[TMP5:%.*]] = add i32 [[Z]], [[LHS]]
 ; CHECK-NEXT:    ret i32 [[TMP5]]
 ;
-  %4 = call i32 @llvm.smin.i32(i32 %0, i32 %1)
-  %5 = add i32 %2, %4
-  %6 = call i32 @llvm.smax.i32(i32 %0, i32 %1)
-  %7 = add i32 %5, %6
-  ret i32 %7
+  %min = call i32 @llvm.smin.i32(i32 %x, i32 %y)
+  %lhs = add i32 %z, %min
+  %max = call i32 @llvm.smax.i32(i32 %x, i32 %y)
+  %ret = add i32 %lhs, %max
+  ret i32 %ret
 }
 
-define i32 @or_min_max_assoc(i32 %0, i32 %1, i32 %2) {
+define i32 @or_min_max_assoc(i32 %x, i32 %y, i32 %z) {
 ; CHECK-LABEL: define i32 @or_min_max_assoc(
-; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]]) {
-; CHECK-NEXT:    [[TMP4:%.*]] = or i32 [[TMP0]], [[TMP1]]
-; CHECK-NEXT:    [[TMP5:%.*]] = or i32 [[TMP2]], [[TMP4]]
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[LHS:%.*]] = or i32 [[X]], [[Y]]
+; CHECK-NEXT:    [[TMP5:%.*]] = or i32 [[Z]], [[LHS]]
 ; CHECK-NEXT:    ret i32 [[TMP5]]
 ;
-  %4 = call i32 @llvm.smin.i32(i32 %0, i32 %1)
-  %5 = or i32 %2, %4
-  %6 = call i32 @llvm.smax.i32(i32 %0, i32 %1)
-  %7 = or i32 %5, %6
-  ret i32 %7
+  %min = call i32 @llvm.smin.i32(i32 %x, i32 %y)
+  %lhs = or i32 %z, %min
+  %max = call i32 @llvm.smax.i32(i32 %x, i32 %y)
+  %ret = or i32 %lhs, %max
+  ret i32 %ret
 }
 
-define i32 @smul_min_max_assoc(i32 %0, i32 %1, i32 %2) {
+define i32 @smul_min_max_assoc(i32 %x, i32 %y, i32 %z) {
 ; CHECK-LABEL: define i32 @smul_min_max_assoc(
-; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]]) {
-; CHECK-NEXT:    [[TMP4:%.*]] = mul i32 [[TMP0]], [[TMP1]]
-; CHECK-NEXT:    [[TMP5:%.*]] = mul i32 [[TMP2]], [[TMP4]]
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[LHS:%.*]] = mul i32 [[X]], [[Y]]
+; CHECK-NEXT:    [[TMP5:%.*]] = mul i32 [[Z]], [[LHS]]
 ; CHECK-NEXT:    ret i32 [[TMP5]]
 ;
-  %4 = call i32 @llvm.smin.i32(i32 %0, i32 %1)
-  %5 = mul i32 %2, %4
-  %6 = call i32 @llvm.smax.i32(i32 %0, i32 %1)
-  %7 = mul i32 %5, %6
-  ret i32 %7
+  %min = call i32 @llvm.smin.i32(i32 %x, i32 %y)
+  %lhs = mul i32 %z, %min
+  %max = call i32 @llvm.smax.i32(i32 %x, i32 %y)
+  %ret = mul i32 %lhs, %max
+  ret i32 %ret
 }
 
-define i32 @uadd_min_max_assoc(i32 %0, i32 %1, i32 %2) {
+define i32 @uadd_min_max_assoc(i32 %x, i32 %y, i32 %z) {
 ; CHECK-LABEL: define i32 @uadd_min_max_assoc(
-; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]]) {
-; CHECK-NEXT:    [[TMP4:%.*]] = add i32 [[TMP0]], [[TMP1]]
-; CHECK-NEXT:    [[TMP5:%.*]] = add i32 [[TMP2]], [[TMP4]]
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[LHS:%.*]] = add i32 [[X]], [[Y]]
+; CHECK-NEXT:    [[TMP5:%.*]] = add i32 [[Z]], [[LHS]]
 ; CHECK-NEXT:    ret i32 [[TMP5]]
 ;
-  %4 = call i32 @llvm.umin.i32(i32 %0, i32 %1)
-  %5 = add i32 %2, %4
-  %6 = call i32 @llvm.umax.i32(i32 %0, i32 %1)
-  %7 = add i32 %5, %6
-  ret i32 %7
+  %min = call i32 @llvm.umin.i32(i32 %x, i32 %y)
+  %lhs = add i32 %z, %min
+  %max = call i32 @llvm.umax.i32(i32 %x, i32 %y)
+  %ret = add i32 %lhs, %max
+  ret i32 %ret
 }
 
-define i32 @umul_min_max_assoc(i32 %0, i32 %1, i32 %2) {
+define i32 @umul_min_max_assoc(i32 %x, i32 %y, i32 %z) {
 ; CHECK-LABEL: define i32 @umul_min_max_assoc(
-; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]]) {
-; CHECK-NEXT:    [[TMP4:%.*]] = mul i32 [[TMP0]], [[TMP1]]
-; CHECK-NEXT:    [[TMP5:%.*]] = mul i32 [[TMP2]], [[TMP4]]
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[LHS:%.*]] = mul i32 [[X]], [[Y]]
+; CHECK-NEXT:    [[TMP5:%.*]] = mul i32 [[Z]], [[LHS]]
 ; CHECK-NEXT:    ret i32 [[TMP5]]
 ;
-  %4 = call i32 @llvm.umin.i32(i32 %0, i32 %1)
-  %5 = mul i32 %2, %4
-  %6 = call i32 @llvm.umax.i32(i32 %0, i32 %1)
-  %7 = mul i32 %5, %6
-  ret i32 %7
+  %min = call i32 @llvm.umin.i32(i32 %x, i32 %y)
+  %lhs = mul i32 %z, %min
+  %max = call i32 @llvm.umax.i32(i32 %x, i32 %y)
+  %ret = mul i32 %lhs, %max
+  ret i32 %ret
 }
 
-define i32 @add_min_max_assoc_wrong(i32 %0, i32 %1, i32 %2) {
+define i32 @add_min_max_assoc_wrong(i32 %x, i32 %y, i32 %z) {
 ; CHECK-LABEL: define i32 @add_min_max_assoc_wrong(
-; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]]) {
-; CHECK-NEXT:    [[TMP4:%.*]] = call i32 @llvm.smin.i32(i32 [[TMP0]], i32 [[TMP1]])
-; CHECK-NEXT:    [[TMP5:%.*]] = add i32 [[TMP2]], [[TMP4]]
-; CHECK-NEXT:    [[TMP6:%.*]] = call i32 @llvm.umax.i32(i32 [[TMP0]], i32 [[TMP1]])
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[MIN:%.*]] = call i32 @llvm.smin.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    [[TMP5:%.*]] = add i32 [[Z]], [[MIN]]
+; CHECK-NEXT:    [[TMP6:%.*]] = call i32 @llvm.umax.i32(i32 [[X]], i32 [[Y]])
 ; CHECK-NEXT:    [[TMP7:%.*]] = add i32 [[TMP5]], [[TMP6]]
 ; CHECK-NEXT:    ret i32 [[TMP7]]
 ;
-  %4 = call i32 @llvm.smin.i32(i32 %0, i32 %1)
-  %5 = add i32 %2, %4
-  %6 = call i32 @llvm.umax.i32(i32 %0, i32 %1)
-  %7 = add i32 %5, %6
-  ret i32 %7
+  %min = call i32 @llvm.smin.i32(i32 %x, i32 %y)
+  %lhs = add i32 %z, %min
+  %max = call i32 @llvm.umax.i32(i32 %x, i32 %y)
+  %ret = add i32 %lhs, %max
+  ret i32 %ret
 }
 
-define i32 @add_min_max_assoc_wrong2(i32 %0, i32 %1, i32 %2, ptr %p) {
+define i32 @add_min_max_assoc_wrong2(i32 %x, i32 %y, i32 %z, ptr %p) {
 ; CHECK-LABEL: define i32 @add_min_max_assoc_wrong2(
-; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]], ptr [[P:%.*]]) {
-; CHECK-NEXT:    [[TMP4:%.*]] = call i32 @llvm.smin.i32(i32 [[TMP0]], i32 [[TMP1]])
-; CHECK-NEXT:    [[TMP5:%.*]] = add i32 [[TMP2]], [[TMP4]]
-; CHECK-NEXT:    [[TMP6:%.*]] = call i32 @llvm.smax.i32(i32 [[TMP0]], i32 [[TMP1]])
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]], ptr [[P:%.*]]) {
+; CHECK-NEXT:    [[MIN:%.*]] = call i32 @llvm.smin.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    [[TMP5:%.*]] = add i32 [[Z]], [[MIN]]
+; CHECK-NEXT:    [[TMP6:%.*]] = call i32 @llvm.smax.i32(i32 [[X]], i32 [[Y]])
 ; CHECK-NEXT:    [[TMP7:%.*]] = add i32 [[TMP5]], [[TMP6]]
 ; CHECK-NEXT:    store i32 [[TMP5]], ptr [[P]], align 4
 ; CHECK-NEXT:    ret i32 [[TMP7]]
 ;
-  %4 = call i32 @llvm.smin.i32(i32 %0, i32 %1)
-  %5 = add i32 %2, %4
-  %6 = call i32 @llvm.smax.i32(i32 %0, i32 %1)
-  %7 = add i32 %5, %6
-  store i32 %5, ptr %p
-  ret i32 %7
+  %min = call i32 @llvm.smin.i32(i32 %x, i32 %y)
+  %lhs = add i32 %z, %min
+  %max = call i32 @llvm.smax.i32(i32 %x, i32 %y)
+  %ret = add i32 %lhs, %max
+  store i32 %lhs, ptr %p
+  ret i32 %ret
 }
 
-define i32 @mul_min_max_assoc_wrong(i32 %0, i32 %1, i32 %2) {
+define i32 @mul_min_max_assoc_wrong(i32 %x, i32 %y, i32 %z) {
 ; CHECK-LABEL: define i32 @mul_min_max_assoc_wrong(
-; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]]) {
-; CHECK-NEXT:    [[TMP4:%.*]] = call i32 @llvm.smin.i32(i32 [[TMP0]], i32 [[TMP1]])
-; CHECK-NEXT:    [[TMP5:%.*]] = mul i32 [[TMP2]], [[TMP4]]
-; CHECK-NEXT:    [[TMP6:%.*]] = call i32 @llvm.umax.i32(i32 [[TMP0]], i32 [[TMP1]])
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[MIN:%.*]] = call i32 @llvm.smin.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    [[TMP5:%.*]] = mul i32 [[Z]], [[MIN]]
+; CHECK-NEXT:    [[TMP6:%.*]] = call i32 @llvm.umax.i32(i32 [[X]], i32 [[Y]])
 ; CHECK-NEXT:    [[TMP7:%.*]] = add i32 [[TMP5]], [[TMP6]]
 ; CHECK-NEXT:    ret i32 [[TMP7]]
 ;
-  %4 = call i32 @llvm.smin.i32(i32 %0, i32 %1)
-  %5 = mul i32 %2, %4
-  %6 = call i32 @llvm.umax.i32(i32 %0, i32 %1)
-  %7 = add i32 %5, %6
-  ret i32 %7
+  %min = call i32 @llvm.smin.i32(i32 %x, i32 %y)
+  %lhs = mul i32 %z, %min
+  %max = call i32 @llvm.umax.i32(i32 %x, i32 %y)
+  %ret = add i32 %lhs, %max
+  ret i32 %ret
+}
+
+define i32 @sadd_nsw_min_max_assoc(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: define i32 @sadd_nsw_min_max_assoc(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[LHS:%.*]] = add i32 [[Y]], [[X]]
+; CHECK-NEXT:    [[RET:%.*]] = add i32 [[Z]], [[LHS]]
+; CHECK-NEXT:    ret i32 [[RET]]
+;
+  %min = call i32 @llvm.smin.i32(i32 %y, i32 %x)
+  %lhs = add nsw i32 %z, %min
+  %max = call i32 @llvm.smax.i32(i32 %x, i32 %y)
+  %ret = add i32 %lhs, %max
+  ret i32 %ret
+}
+
+define i32 @sadd_nuw_min_max_assoc(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: define i32 @sadd_nuw_min_max_assoc(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[LHS:%.*]] = add i32 [[Y]], [[X]]
+; CHECK-NEXT:    [[RET:%.*]] = add i32 [[Z]], [[LHS]]
+; CHECK-NEXT:    ret i32 [[RET]]
+;
+  %min = call i32 @llvm.smin.i32(i32 %y, i32 %x)
+  %lhs = add nuw i32 %z, %min
+  %max = call i32 @llvm.smax.i32(i32 %x, i32 %y)
+  %ret = add i32 %lhs, %max
+  ret i32 %ret
 }

>From 04f78e188c16d64da91a309aa2ed61dd9360d083 Mon Sep 17 00:00:00 2001
From: Mitch <mitchbriles at gmail.com>
Date: Thu, 9 Apr 2026 18:28:42 -0600
Subject: [PATCH 06/16] Use `dropPoisonGeneratingFlags`

---
 .../InstCombine/InstructionCombining.cpp           |  5 +----
 .../commutative-operation-over-minmax.ll           | 14 ++++++++++++++
 2 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 2de3a5b370535..6e45663512c31 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -522,10 +522,7 @@ bool InstCombinerImpl::SimplifyAssociativeOrCommutative(BinaryOperator &I) {
           if (auto Pair = matchSymmetricPair(B, C)) {
             replaceOperand(*Op0, 0, Pair->first);
             replaceOperand(*Op0, 1, Pair->second);
-            if (hasNoUnsignedWrap(*Op0))
-              Op0->setHasNoUnsignedWrap(false);
-            if (hasNoSignedWrap(*Op0))
-              Op0->setHasNoSignedWrap(false);
+            Op0->dropPoisonGeneratingFlags();
             replaceOperand(I, 1, A);
             I.swapOperands();
             ClearSubclassDataAfterReassociation(I);
diff --git a/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll b/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
index 547aad7c6b164..870b1244ad66e 100644
--- a/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
+++ b/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
@@ -29,6 +29,20 @@ define i32 @or_min_max_assoc(i32 %x, i32 %y, i32 %z) {
   ret i32 %ret
 }
 
+define i32 @or_disjoint_min_max_assoc(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: define i32 @or_disjoint_min_max_assoc(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[LHS:%.*]] = or i32 [[X]], [[Y]]
+; CHECK-NEXT:    [[TMP5:%.*]] = or i32 [[Z]], [[LHS]]
+; CHECK-NEXT:    ret i32 [[TMP5]]
+;
+  %min = call i32 @llvm.smin.i32(i32 %x, i32 %y)
+  %lhs = or disjoint i32 %z, %min
+  %max = call i32 @llvm.smax.i32(i32 %x, i32 %y)
+  %ret = or i32 %lhs, %max
+  ret i32 %ret
+}
+
 define i32 @smul_min_max_assoc(i32 %x, i32 %y, i32 %z) {
 ; CHECK-LABEL: define i32 @smul_min_max_assoc(
 ; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {

>From b7184aca5e3de5a8404298565239ddfd128bf42c Mon Sep 17 00:00:00 2001
From: Mitch <mitchbriles at gmail.com>
Date: Tue, 21 Apr 2026 01:54:06 -0600
Subject: [PATCH 07/16] Handle commuted pattern

---
 .../InstCombine/InstructionCombining.cpp           | 14 ++++++++++++++
 .../commutative-operation-over-minmax.ll           | 14 ++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 6e45663512c31..4ec4ea23513b0 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -615,6 +615,20 @@ bool InstCombinerImpl::SimplifyAssociativeOrCommutative(BinaryOperator &I) {
         Value *B = Op1->getOperand(0);
         Value *C = Op1->getOperand(1);
 
+        // Form "B op V" when "C op A" is a symmetric pair.
+        if (Op1->hasOneUse()) {
+          if (auto Pair = matchSymmetricPair(C, A)) {
+            replaceOperand(*Op1, 0, Pair->first);
+            replaceOperand(*Op1, 1, Pair->second);
+            Op1->dropPoisonGeneratingFlags();
+            replaceOperand(I, 0, B);
+            ClearSubclassDataAfterReassociation(I);
+            Changed = true;
+            ++NumReassoc;
+            continue;
+          }
+        }
+
         // Does "C op A" simplify?
         if (Value *V = simplifyBinOp(Opcode, C, A, SQ.getWithInstruction(&I))) {
           // It simplifies to V.  Form "B op V".
diff --git a/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll b/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
index 870b1244ad66e..794d684fda63f 100644
--- a/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
+++ b/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
@@ -15,6 +15,20 @@ define i32 @sadd_min_max_assoc(i32 %x, i32 %y, i32 %z) {
   ret i32 %ret
 }
 
+define i32 @sadd_min_max_assoc_comm(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: define i32 @sadd_min_max_assoc_comm(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[RHS:%.*]] = add i32 [[X]], [[Y]]
+; CHECK-NEXT:    [[RET:%.*]] = add i32 [[Z]], [[RHS]]
+; CHECK-NEXT:    ret i32 [[RET]]
+;
+  %min = call i32 @llvm.smin.i32(i32 %x, i32 %y)
+  %rhs = add i32 %z, %min
+  %max = call i32 @llvm.smax.i32(i32 %x, i32 %y)
+  %ret = add i32 %max, %rhs
+  ret i32 %ret
+}
+
 define i32 @or_min_max_assoc(i32 %x, i32 %y, i32 %z) {
 ; CHECK-LABEL: define i32 @or_min_max_assoc(
 ; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {

>From c64637feffe8effe23c9feedbd7671a7749900fa Mon Sep 17 00:00:00 2001
From: Mitch <mitchbriles at gmail.com>
Date: Mon, 27 Apr 2026 02:57:04 -0600
Subject: [PATCH 08/16] Last commuted case

---
 .../InstCombine/InstructionCombining.cpp           | 14 ++++++++++++++
 .../commutative-operation-over-minmax.ll           | 14 ++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 4ec4ea23513b0..bc0a71652799d 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -593,6 +593,20 @@ bool InstCombinerImpl::SimplifyAssociativeOrCommutative(BinaryOperator &I) {
         Value *A = Op0->getOperand(0);
         Value *B = Op0->getOperand(1);
         Value *C = I.getOperand(1);
+        
+        // Form "V op B" when "C op A" is a symmetric pair
+        if (Op0->hasOneUse()) {
+          if (auto Pair = matchSymmetricPair(C, A)) {
+            replaceOperand(*Op0, 0, Pair->first);
+            replaceOperand(*Op0, 1, Pair->second);
+            Op0->dropPoisonGeneratingFlags();
+            replaceOperand(I, 1, B);
+            ClearSubclassDataAfterReassociation(I);
+            Changed = true;
+            ++NumReassoc;
+            continue;
+          }
+        }
 
         // Does "C op A" simplify?
         if (Value *V = simplifyBinOp(Opcode, C, A, SQ.getWithInstruction(&I))) {
diff --git a/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll b/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
index 794d684fda63f..3f6677927db1f 100644
--- a/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
+++ b/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
@@ -71,6 +71,20 @@ define i32 @smul_min_max_assoc(i32 %x, i32 %y, i32 %z) {
   ret i32 %ret
 }
 
+define i32 @smul_min_max_assoc_comm(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: define i32 @smul_min_max_assoc_comm(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[LHS:%.*]] = mul i32 [[X]], [[Y]]
+; CHECK-NEXT:    [[RET:%.*]] = mul i32 [[LHS]], [[Z]]
+; CHECK-NEXT:    ret i32 [[RET]]
+;
+  %min = call i32 @llvm.smin.i32(i32 %x, i32 %y)
+  %lhs = mul i32 %min, %z
+  %max = call i32 @llvm.smax.i32(i32 %x, i32 %y)
+  %ret = mul i32 %lhs, %max
+  ret i32 %ret
+}
+
 define i32 @uadd_min_max_assoc(i32 %x, i32 %y, i32 %z) {
 ; CHECK-LABEL: define i32 @uadd_min_max_assoc(
 ; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {

>From f972246363c56419c83b0436f22618422d4ebefe Mon Sep 17 00:00:00 2001
From: Mitch <mitchbriles at gmail.com>
Date: Fri, 22 May 2026 05:28:54 -0600
Subject: [PATCH 09/16] Refactor into helper + add test

---
 .../InstCombine/InstCombineInternal.h         | 11 +++
 .../InstCombine/InstructionCombining.cpp      | 81 ++++++++++---------
 .../commutative-operation-over-minmax.ll      | 18 ++++-
 3 files changed, 70 insertions(+), 40 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
index 4a09f870ccc33..bd7a207f857b6 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
+++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
@@ -522,6 +522,17 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
   /// or commutative.
   bool SimplifyAssociativeOrCommutative(BinaryOperator &I);
 
+  /// If InnerVal1 and OuterVal form a symmetric pair, and "op" is commutative:
+  /// "(InnerVal0 op InnerVal1) op OuterVal" ==> "(X op' Y) op InnerVal0".
+  /// "OuterVal op (InnerVal0 op InnerVal1)" ==> "InnerVal0 op (X op' Y)".
+  /// X and Y are the inputs to the symmetric pair. "op'" indicates that
+  /// InnerOp will have its poison-generating flags dropped. Warning:
+  /// associativity not checked.
+  bool tryReassociateAndFoldSymmetricPair(BinaryOperator &OuterOp,
+                                          BinaryOperator &InnerOp,
+                                          Value *InnerVal0, Value *InnerVal1,
+                                          Value *OuterVal);
+
   /// Tries to simplify binary operations which some other binary
   /// operation distributes over.
   ///
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index bc0a71652799d..a176a99d89a01 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -517,19 +517,11 @@ bool InstCombinerImpl::SimplifyAssociativeOrCommutative(BinaryOperator &I) {
         Value *B = Op0->getOperand(1);
         Value *C = I.getOperand(1);
 
-        // Form "A op V" when "B op C" is a symmetric pair
-        if (I.isCommutative() && Op0->hasOneUse()) {
-          if (auto Pair = matchSymmetricPair(B, C)) {
-            replaceOperand(*Op0, 0, Pair->first);
-            replaceOperand(*Op0, 1, Pair->second);
-            Op0->dropPoisonGeneratingFlags();
-            replaceOperand(I, 1, A);
-            I.swapOperands();
-            ClearSubclassDataAfterReassociation(I);
-            Changed = true;
-            ++NumReassoc;
-            continue;
-          }
+        if (tryReassociateAndFoldSymmetricPair(I, *Op0, A, B, C)) {
+          I.swapOperands();
+          Changed = true;
+          ++NumReassoc;
+          continue;
         }
 
         // Does "B op C" simplify?
@@ -565,6 +557,13 @@ bool InstCombinerImpl::SimplifyAssociativeOrCommutative(BinaryOperator &I) {
         Value *B = Op1->getOperand(0);
         Value *C = Op1->getOperand(1);
 
+        if (tryReassociateAndFoldSymmetricPair(I, *Op1, C, A, B)) {
+          I.swapOperands();
+          Changed = true;
+          ++NumReassoc;
+          continue;
+        }
+
         // Does "A op B" simplify?
         if (Value *V = simplifyBinOp(Opcode, A, B, SQ.getWithInstruction(&I))) {
           // It simplifies to V.  Form "V op C".
@@ -593,19 +592,11 @@ bool InstCombinerImpl::SimplifyAssociativeOrCommutative(BinaryOperator &I) {
         Value *A = Op0->getOperand(0);
         Value *B = Op0->getOperand(1);
         Value *C = I.getOperand(1);
-        
-        // Form "V op B" when "C op A" is a symmetric pair
-        if (Op0->hasOneUse()) {
-          if (auto Pair = matchSymmetricPair(C, A)) {
-            replaceOperand(*Op0, 0, Pair->first);
-            replaceOperand(*Op0, 1, Pair->second);
-            Op0->dropPoisonGeneratingFlags();
-            replaceOperand(I, 1, B);
-            ClearSubclassDataAfterReassociation(I);
-            Changed = true;
-            ++NumReassoc;
-            continue;
-          }
+
+        if (tryReassociateAndFoldSymmetricPair(I, *Op0, B, C, A)) {
+          Changed = true;
+          ++NumReassoc;
+          continue;
         }
 
         // Does "C op A" simplify?
@@ -629,18 +620,10 @@ bool InstCombinerImpl::SimplifyAssociativeOrCommutative(BinaryOperator &I) {
         Value *B = Op1->getOperand(0);
         Value *C = Op1->getOperand(1);
 
-        // Form "B op V" when "C op A" is a symmetric pair.
-        if (Op1->hasOneUse()) {
-          if (auto Pair = matchSymmetricPair(C, A)) {
-            replaceOperand(*Op1, 0, Pair->first);
-            replaceOperand(*Op1, 1, Pair->second);
-            Op1->dropPoisonGeneratingFlags();
-            replaceOperand(I, 0, B);
-            ClearSubclassDataAfterReassociation(I);
-            Changed = true;
-            ++NumReassoc;
-            continue;
-          }
+        if (tryReassociateAndFoldSymmetricPair(I, *Op1, B, C, A)) {
+          Changed = true;
+          ++NumReassoc;
+          continue;
         }
 
         // Does "C op A" simplify?
@@ -1388,6 +1371,28 @@ InstCombinerImpl::matchSymmetricPair(Value *LHS, Value *RHS) {
   }
 }
 
+bool InstCombinerImpl::tryReassociateAndFoldSymmetricPair(
+    BinaryOperator &OuterOp, BinaryOperator &InnerOp, Value *InnerVal0,
+    Value *InnerVal1, Value *OuterVal) {
+  assert(OuterOp.getOpcode() == InnerOp.getOpcode() &&
+         "Outer and inner operation must have same opcode!");
+  if (!OuterOp.isCommutative() || !InnerOp.hasOneUse())
+    return false;
+
+  auto Pair = matchSymmetricPair(InnerVal1, OuterVal);
+  if (Pair) {
+    replaceOperand(InnerOp, 0, Pair->first);
+    replaceOperand(InnerOp, 1, Pair->second);
+    InnerOp.dropPoisonGeneratingFlags();
+    replaceOperand(OuterOp, OuterOp.getOperand(1) == OuterVal ? 1 : 0,
+                   InnerVal0);
+    ClearSubclassDataAfterReassociation(OuterOp);
+    return true;
+  }
+
+  return false;
+}
+
 Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I,
                                                         Value *LHS,
                                                         Value *RHS) {
diff --git a/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll b/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
index 3f6677927db1f..3c461016ad115 100644
--- a/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
+++ b/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
@@ -15,6 +15,20 @@ define i32 @sadd_min_max_assoc(i32 %x, i32 %y, i32 %z) {
   ret i32 %ret
 }
 
+define i32 @sadd_min_max_assoc2(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: define i32 @sadd_min_max_assoc2(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[RHS:%.*]] = add i32 [[X]], [[Y]]
+; CHECK-NEXT:    [[RET:%.*]] = add i32 [[RHS]], [[Z]]
+; CHECK-NEXT:    ret i32 [[RET]]
+;
+  %min = call i32 @llvm.smin.i32(i32 %x, i32 %y)
+  %rhs = add i32 %min, %z
+  %max = call i32 @llvm.smax.i32(i32 %x, i32 %y)
+  %ret = add i32 %max, %rhs
+  ret i32 %ret
+}
+
 define i32 @sadd_min_max_assoc_comm(i32 %x, i32 %y, i32 %z) {
 ; CHECK-LABEL: define i32 @sadd_min_max_assoc_comm(
 ; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
@@ -74,8 +88,8 @@ define i32 @smul_min_max_assoc(i32 %x, i32 %y, i32 %z) {
 define i32 @smul_min_max_assoc_comm(i32 %x, i32 %y, i32 %z) {
 ; CHECK-LABEL: define i32 @smul_min_max_assoc_comm(
 ; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
-; CHECK-NEXT:    [[LHS:%.*]] = mul i32 [[X]], [[Y]]
-; CHECK-NEXT:    [[RET:%.*]] = mul i32 [[LHS]], [[Z]]
+; CHECK-NEXT:    [[MAX:%.*]] = call i32 @llvm.smax.i32(i32 [[X]], i32 [[Y]])
+; CHECK-NEXT:    [[RET:%.*]] = mul i32 [[Z]], [[MAX]]
 ; CHECK-NEXT:    ret i32 [[RET]]
 ;
   %min = call i32 @llvm.smin.i32(i32 %x, i32 %y)

>From 55fbcabdaf439659430e362a53c8da3366866416 Mon Sep 17 00:00:00 2001
From: Mitch <mitchbriles at gmail.com>
Date: Fri, 22 May 2026 05:55:55 -0600
Subject: [PATCH 10/16] Be more careful about operand indices

---
 .../InstCombine/InstructionCombining.cpp        | 17 +++++++++++++----
 .../commutative-operation-over-minmax.ll        |  4 ++--
 2 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index a176a99d89a01..6364f28a39a47 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -557,7 +557,7 @@ bool InstCombinerImpl::SimplifyAssociativeOrCommutative(BinaryOperator &I) {
         Value *B = Op1->getOperand(0);
         Value *C = Op1->getOperand(1);
 
-        if (tryReassociateAndFoldSymmetricPair(I, *Op1, C, A, B)) {
+        if (tryReassociateAndFoldSymmetricPair(I, *Op1, C, B, A)) {
           I.swapOperands();
           Changed = true;
           ++NumReassoc;
@@ -593,7 +593,7 @@ bool InstCombinerImpl::SimplifyAssociativeOrCommutative(BinaryOperator &I) {
         Value *B = Op0->getOperand(1);
         Value *C = I.getOperand(1);
 
-        if (tryReassociateAndFoldSymmetricPair(I, *Op0, B, C, A)) {
+        if (tryReassociateAndFoldSymmetricPair(I, *Op0, B, A, C)) {
           Changed = true;
           ++NumReassoc;
           continue;
@@ -1379,13 +1379,22 @@ bool InstCombinerImpl::tryReassociateAndFoldSymmetricPair(
   if (!OuterOp.isCommutative() || !InnerOp.hasOneUse())
     return false;
 
+  unsigned OuterValIdx;
+  if (OuterOp.getOperand(0) == OuterVal)
+    OuterValIdx = 0;
+  else if (OuterOp.getOperand(1) == OuterVal)
+    OuterValIdx = 1;
+  else {
+    assert(false && "OuterVal must be an operand of OuterOp");
+    return false;
+  }
+
   auto Pair = matchSymmetricPair(InnerVal1, OuterVal);
   if (Pair) {
     replaceOperand(InnerOp, 0, Pair->first);
     replaceOperand(InnerOp, 1, Pair->second);
     InnerOp.dropPoisonGeneratingFlags();
-    replaceOperand(OuterOp, OuterOp.getOperand(1) == OuterVal ? 1 : 0,
-                   InnerVal0);
+    replaceOperand(OuterOp, OuterValIdx, InnerVal0);
     ClearSubclassDataAfterReassociation(OuterOp);
     return true;
   }
diff --git a/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll b/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
index 3c461016ad115..0bbd861673627 100644
--- a/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
+++ b/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
@@ -88,8 +88,8 @@ define i32 @smul_min_max_assoc(i32 %x, i32 %y, i32 %z) {
 define i32 @smul_min_max_assoc_comm(i32 %x, i32 %y, i32 %z) {
 ; CHECK-LABEL: define i32 @smul_min_max_assoc_comm(
 ; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
-; CHECK-NEXT:    [[MAX:%.*]] = call i32 @llvm.smax.i32(i32 [[X]], i32 [[Y]])
-; CHECK-NEXT:    [[RET:%.*]] = mul i32 [[Z]], [[MAX]]
+; CHECK-NEXT:    [[LHS:%.*]] = mul i32 [[X]], [[Y]]
+; CHECK-NEXT:    [[RET:%.*]] = mul i32 [[LHS]], [[Z]]
 ; CHECK-NEXT:    ret i32 [[RET]]
 ;
   %min = call i32 @llvm.smin.i32(i32 %x, i32 %y)

>From 3fd30651bf11b2ab2e3d30ce9cca599a75034cf7 Mon Sep 17 00:00:00 2001
From: Mitch <mitchbriles at gmail.com>
Date: Sat, 23 May 2026 17:26:36 -0600
Subject: [PATCH 11/16] Mixed associativity tests (pre-patch)

---
 .../commutative-operation-over-minmax.ll      | 28 +++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll b/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
index 0bbd861673627..834a8c1f7f0cd 100644
--- a/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
+++ b/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
@@ -204,3 +204,31 @@ define i32 @sadd_nuw_min_max_assoc(i32 %x, i32 %y, i32 %z) {
   %ret = add i32 %lhs, %max
   ret i32 %ret
 }
+
+define float @fp_inner_no_reassoc_cmp(i1 %c, float %a, float %x, float %y) {
+; CHECK-LABEL: define float @fp_inner_no_reassoc_cmp(
+; CHECK-SAME: i1 [[C:%.*]], float [[A:%.*]], float [[X:%.*]], float [[Y:%.*]]) {
+; CHECK-NEXT:    [[INNER:%.*]] = fadd float [[X]], [[Y]]
+; CHECK-NEXT:    [[RETF:%.*]] = fadd reassoc nsz float [[A]], [[INNER]]
+; CHECK-NEXT:    ret float [[RETF]]
+;
+  %s0 = select i1 %c, float %x, float %y
+  %s1 = select i1 %c, float %y, float %x
+  %inner = fadd float %a, %s0
+  %retf = fadd reassoc nsz float %inner, %s1
+  ret float %retf
+}
+
+define float @fp_mixed_fmf_cmp(i1 %c, float %a, float %x, float %y) {
+; CHECK-LABEL: define float @fp_mixed_fmf_cmp(
+; CHECK-SAME: i1 [[C:%.*]], float [[A:%.*]], float [[X:%.*]], float [[Y:%.*]]) {
+; CHECK-NEXT:    [[INNER:%.*]] = fadd float [[X]], [[Y]]
+; CHECK-NEXT:    [[RETF:%.*]] = fadd reassoc nnan ninf nsz float [[A]], [[INNER]]
+; CHECK-NEXT:    ret float [[RETF]]
+;
+  %s0 = select i1 %c, float %x, float %y
+  %s1 = select i1 %c, float %y, float %x
+  %inner = fadd nnan float %a, %s0
+  %retf = fadd reassoc nsz nnan ninf float %inner, %s1
+  ret float %retf
+}

>From baf3fdf7ee30a23005d0769b0d0bdf06c722ffb7 Mon Sep 17 00:00:00 2001
From: Mitch <mitchbriles at gmail.com>
Date: Sat, 23 May 2026 18:01:00 -0600
Subject: [PATCH 12/16] Account for mixed associativity

Also no longer crashes when helper fails. Wasn't causing issues here, just a self nit.
---
 .../Transforms/InstCombine/InstructionCombining.cpp  | 12 +++++-------
 .../InstCombine/commutative-operation-over-minmax.ll | 12 ++++++++----
 2 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 6364f28a39a47..7b6a1e390e32f 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1374,9 +1374,9 @@ InstCombinerImpl::matchSymmetricPair(Value *LHS, Value *RHS) {
 bool InstCombinerImpl::tryReassociateAndFoldSymmetricPair(
     BinaryOperator &OuterOp, BinaryOperator &InnerOp, Value *InnerVal0,
     Value *InnerVal1, Value *OuterVal) {
-  assert(OuterOp.getOpcode() == InnerOp.getOpcode() &&
-         "Outer and inner operation must have same opcode!");
-  if (!OuterOp.isCommutative() || !InnerOp.hasOneUse())
+  if (!OuterOp.isCommutative() || !OuterOp.isAssociative() ||
+      !InnerOp.isCommutative() || !InnerOp.isAssociative() ||
+      !InnerOp.hasOneUse() || OuterOp.getOpcode() != InnerOp.getOpcode())
     return false;
 
   unsigned OuterValIdx;
@@ -1384,10 +1384,8 @@ bool InstCombinerImpl::tryReassociateAndFoldSymmetricPair(
     OuterValIdx = 0;
   else if (OuterOp.getOperand(1) == OuterVal)
     OuterValIdx = 1;
-  else {
-    assert(false && "OuterVal must be an operand of OuterOp");
-    return false;
-  }
+  else 
+    return false; // OuterVal must be an operand of OuterOp
 
   auto Pair = matchSymmetricPair(InnerVal1, OuterVal);
   if (Pair) {
diff --git a/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll b/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
index 834a8c1f7f0cd..504af5db678fc 100644
--- a/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
+++ b/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
@@ -208,8 +208,10 @@ define i32 @sadd_nuw_min_max_assoc(i32 %x, i32 %y, i32 %z) {
 define float @fp_inner_no_reassoc_cmp(i1 %c, float %a, float %x, float %y) {
 ; CHECK-LABEL: define float @fp_inner_no_reassoc_cmp(
 ; CHECK-SAME: i1 [[C:%.*]], float [[A:%.*]], float [[X:%.*]], float [[Y:%.*]]) {
-; CHECK-NEXT:    [[INNER:%.*]] = fadd float [[X]], [[Y]]
-; CHECK-NEXT:    [[RETF:%.*]] = fadd reassoc nsz float [[A]], [[INNER]]
+; CHECK-NEXT:    [[S0:%.*]] = select i1 [[C]], float [[X]], float [[Y]]
+; CHECK-NEXT:    [[S1:%.*]] = select i1 [[C]], float [[Y]], float [[X]]
+; CHECK-NEXT:    [[INNER:%.*]] = fadd float [[A]], [[S0]]
+; CHECK-NEXT:    [[RETF:%.*]] = fadd reassoc nsz float [[INNER]], [[S1]]
 ; CHECK-NEXT:    ret float [[RETF]]
 ;
   %s0 = select i1 %c, float %x, float %y
@@ -222,8 +224,10 @@ define float @fp_inner_no_reassoc_cmp(i1 %c, float %a, float %x, float %y) {
 define float @fp_mixed_fmf_cmp(i1 %c, float %a, float %x, float %y) {
 ; CHECK-LABEL: define float @fp_mixed_fmf_cmp(
 ; CHECK-SAME: i1 [[C:%.*]], float [[A:%.*]], float [[X:%.*]], float [[Y:%.*]]) {
-; CHECK-NEXT:    [[INNER:%.*]] = fadd float [[X]], [[Y]]
-; CHECK-NEXT:    [[RETF:%.*]] = fadd reassoc nnan ninf nsz float [[A]], [[INNER]]
+; CHECK-NEXT:    [[S0:%.*]] = select i1 [[C]], float [[X]], float [[Y]]
+; CHECK-NEXT:    [[S1:%.*]] = select i1 [[C]], float [[Y]], float [[X]]
+; CHECK-NEXT:    [[INNER:%.*]] = fadd nnan float [[A]], [[S0]]
+; CHECK-NEXT:    [[RETF:%.*]] = fadd reassoc nnan ninf nsz float [[INNER]], [[S1]]
 ; CHECK-NEXT:    ret float [[RETF]]
 ;
   %s0 = select i1 %c, float %x, float %y

>From 40c96c5393c89ad56162c99a03523a1b26f54023 Mon Sep 17 00:00:00 2001
From: Mitch <mitchbriles at gmail.com>
Date: Sat, 23 May 2026 18:04:57 -0600
Subject: [PATCH 13/16] Formatting somehow slipped through

---
 llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 7b6a1e390e32f..36cc41e472f76 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1384,7 +1384,7 @@ bool InstCombinerImpl::tryReassociateAndFoldSymmetricPair(
     OuterValIdx = 0;
   else if (OuterOp.getOperand(1) == OuterVal)
     OuterValIdx = 1;
-  else 
+  else
     return false; // OuterVal must be an operand of OuterOp
 
   auto Pair = matchSymmetricPair(InnerVal1, OuterVal);

>From 391568440471f7ff1c99c53f905ff4acd58e5e5d Mon Sep 17 00:00:00 2001
From: Mitch <mitchbriles at gmail.com>
Date: Sun, 24 May 2026 17:42:42 -0600
Subject: [PATCH 14/16] Use assertions

---
 .../InstCombine/InstCombineInternal.h         |  3 +--
 .../InstCombine/InstructionCombining.cpp      | 27 ++++++++++---------
 2 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
index bd7a207f857b6..1eb05d7e56f3d 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
+++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
@@ -526,8 +526,7 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
   /// "(InnerVal0 op InnerVal1) op OuterVal" ==> "(X op' Y) op InnerVal0".
   /// "OuterVal op (InnerVal0 op InnerVal1)" ==> "InnerVal0 op (X op' Y)".
   /// X and Y are the inputs to the symmetric pair. "op'" indicates that
-  /// InnerOp will have its poison-generating flags dropped. Warning:
-  /// associativity not checked.
+  /// InnerOp will have its poison-generating flags dropped.
   bool tryReassociateAndFoldSymmetricPair(BinaryOperator &OuterOp,
                                           BinaryOperator &InnerOp,
                                           Value *InnerVal0, Value *InnerVal1,
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 36cc41e472f76..c730b0ac5ccbf 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1374,9 +1374,11 @@ 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())
+  assert(OuterOp.isCommutative() && OuterOp.isAssociative() &&
+         "OuterOp must be commutative and associative");
+  assert(OuterOp.getOpcode() == InnerOp.getOpcode() &&
+         "OuterOp and InnerOp must have same opcode");
+  if (!InnerOp.isAssociative() || !InnerOp.hasOneUse())
     return false;
 
   unsigned OuterValIdx;
@@ -1385,19 +1387,18 @@ bool InstCombinerImpl::tryReassociateAndFoldSymmetricPair(
   else if (OuterOp.getOperand(1) == OuterVal)
     OuterValIdx = 1;
   else
-    return false; // OuterVal must be an operand of OuterOp
+    assert(false && "OuterVal must be an operand of OuterOp");
 
   auto Pair = matchSymmetricPair(InnerVal1, OuterVal);
-  if (Pair) {
-    replaceOperand(InnerOp, 0, Pair->first);
-    replaceOperand(InnerOp, 1, Pair->second);
-    InnerOp.dropPoisonGeneratingFlags();
-    replaceOperand(OuterOp, OuterValIdx, InnerVal0);
-    ClearSubclassDataAfterReassociation(OuterOp);
-    return true;
-  }
+  if (!Pair)
+    return false;
 
-  return false;
+  replaceOperand(InnerOp, 0, Pair->first);
+  replaceOperand(InnerOp, 1, Pair->second);
+  InnerOp.dropPoisonGeneratingFlags();
+  replaceOperand(OuterOp, OuterValIdx, InnerVal0);
+  ClearSubclassDataAfterReassociation(OuterOp);
+  return true;
 }
 
 Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I,

>From 3c478a3b342b29b138e9fa7798e154474806d328 Mon Sep 17 00:00:00 2001
From: Mitch <mitchbriles at gmail.com>
Date: Sat, 6 Jun 2026 22:45:56 -0600
Subject: [PATCH 15/16] Drop annotations

---
 .../InstCombine/InstructionCombining.cpp      | 14 +++---
 .../commutative-operation-over-minmax.ll      | 44 +++++++++++++++++++
 2 files changed, 50 insertions(+), 8 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index c730b0ac5ccbf..d743ada01cb97 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1381,13 +1381,9 @@ bool InstCombinerImpl::tryReassociateAndFoldSymmetricPair(
   if (!InnerOp.isAssociative() || !InnerOp.hasOneUse())
     return false;
 
-  unsigned OuterValIdx;
-  if (OuterOp.getOperand(0) == OuterVal)
-    OuterValIdx = 0;
-  else if (OuterOp.getOperand(1) == OuterVal)
-    OuterValIdx = 1;
-  else
-    assert(false && "OuterVal must be an operand of OuterOp");
+  unsigned OuterValIdx = OuterOp.getOperand(0) == OuterVal ? 0 : 1;
+  assert(OuterOp.getOperand(OuterValIdx) == OuterVal &&
+         "OuterVal must be an operand of OuterOp");
 
   auto Pair = matchSymmetricPair(InnerVal1, OuterVal);
   if (!Pair)
@@ -1395,9 +1391,11 @@ bool InstCombinerImpl::tryReassociateAndFoldSymmetricPair(
 
   replaceOperand(InnerOp, 0, Pair->first);
   replaceOperand(InnerOp, 1, Pair->second);
-  InnerOp.dropPoisonGeneratingFlags();
+  InnerOp.dropPoisonGeneratingAnnotations();
+  InnerOp.dropUBImplyingAttrsAndMetadata();
   replaceOperand(OuterOp, OuterValIdx, InnerVal0);
   ClearSubclassDataAfterReassociation(OuterOp);
+  OuterOp.dropUnknownNonDebugMetadata();
   return true;
 }
 
diff --git a/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll b/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
index 504af5db678fc..7d964c42bb349 100644
--- a/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
+++ b/llvm/test/Transforms/InstCombine/commutative-operation-over-minmax.ll
@@ -71,6 +71,20 @@ define i32 @or_disjoint_min_max_assoc(i32 %x, i32 %y, i32 %z) {
   ret i32 %ret
 }
 
+define i32 @or_outer_disjoint_min_max_assoc(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: define i32 @or_outer_disjoint_min_max_assoc(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[LHS:%.*]] = or i32 [[X]], [[Y]]
+; CHECK-NEXT:    [[RET:%.*]] = or i32 [[Z]], [[LHS]]
+; CHECK-NEXT:    ret i32 [[RET]]
+;
+  %min = call i32 @llvm.smin.i32(i32 %x, i32 %y)
+  %lhs = or i32 %z, %min
+  %max = call i32 @llvm.smax.i32(i32 %x, i32 %y)
+  %ret = or disjoint i32 %lhs, %max
+  ret i32 %ret
+}
+
 define i32 @smul_min_max_assoc(i32 %x, i32 %y, i32 %z) {
 ; CHECK-LABEL: define i32 @smul_min_max_assoc(
 ; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
@@ -205,6 +219,20 @@ define i32 @sadd_nuw_min_max_assoc(i32 %x, i32 %y, i32 %z) {
   ret i32 %ret
 }
 
+define i32 @sadd_inner_metadata_outer_annotations(i32 %x, i32 %y, i32 %z) {
+; CHECK-LABEL: define i32 @sadd_inner_metadata_outer_annotations(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]]) {
+; CHECK-NEXT:    [[LHS:%.*]] = add i32 [[Y]], [[X]]
+; CHECK-NEXT:    [[RET:%.*]] = add i32 [[Z]], [[LHS]]
+; CHECK-NEXT:    ret i32 [[RET]]
+;
+  %min = call i32 @llvm.smin.i32(i32 %y, i32 %x)
+  %lhs = add i32 %z, %min, !noundef !0
+  %max = call i32 @llvm.smax.i32(i32 %x, i32 %y)
+  %ret = add nuw nsw i32 %lhs, %max, !noundef !0
+  ret i32 %ret
+}
+
 define float @fp_inner_no_reassoc_cmp(i1 %c, float %a, float %x, float %y) {
 ; CHECK-LABEL: define float @fp_inner_no_reassoc_cmp(
 ; CHECK-SAME: i1 [[C:%.*]], float [[A:%.*]], float [[X:%.*]], float [[Y:%.*]]) {
@@ -236,3 +264,19 @@ define float @fp_mixed_fmf_cmp(i1 %c, float %a, float %x, float %y) {
   %retf = fadd reassoc nsz nnan ninf float %inner, %s1
   ret float %retf
 }
+
+define float @fp_outer_fmf_metadata(i1 %c, float %a, float %x, float %y) {
+; CHECK-LABEL: define float @fp_outer_fmf_metadata(
+; CHECK-SAME: i1 [[C:%.*]], float [[A:%.*]], float [[X:%.*]], float [[Y:%.*]]) {
+; CHECK-NEXT:    [[INNER:%.*]] = fadd reassoc nsz float [[X]], [[Y]]
+; CHECK-NEXT:    [[RETF:%.*]] = fadd reassoc nnan ninf nsz float [[A]], [[INNER]]
+; CHECK-NEXT:    ret float [[RETF]]
+;
+  %s0 = select i1 %c, float %x, float %y
+  %s1 = select i1 %c, float %y, float %x
+  %inner = fadd reassoc nnan ninf nsz float %a, %s0, !noundef !0
+  %retf = fadd reassoc nnan ninf nsz float %inner, %s1, !noundef !0
+  ret float %retf
+}
+
+!0 = !{}

>From e82f3c6839cc49552c2b2d8abef7274ea4827f00 Mon Sep 17 00:00:00 2001
From: Mitch <mitchbriles at gmail.com>
Date: Sun, 7 Jun 2026 02:31:08 -0600
Subject: [PATCH 16/16] Update for new API

---
 llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index d743ada01cb97..23e476ea3a6be 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1394,8 +1394,9 @@ bool InstCombinerImpl::tryReassociateAndFoldSymmetricPair(
   InnerOp.dropPoisonGeneratingAnnotations();
   InnerOp.dropUBImplyingAttrsAndMetadata();
   replaceOperand(OuterOp, OuterValIdx, InnerVal0);
-  ClearSubclassDataAfterReassociation(OuterOp);
-  OuterOp.dropUnknownNonDebugMetadata();
+  if (!isa<FPMathOperator>(OuterOp))
+    OuterOp.dropPoisonGeneratingFlags();
+  OuterOp.dropUBImplyingAttrsAndMetadata();
   return true;
 }
 



More information about the llvm-commits mailing list