[llvm] [DAG] SDPatternMatch - Fix m_Reassociatable mismatching (PR #170061)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 3 01:04:02 PST 2025


================
@@ -842,6 +842,16 @@ TEST_F(SelectionDAGPatternMatchTest, matchReassociatableOp) {
   EXPECT_TRUE(sd_match(
       MUL, m_ReassociatableMul(m_Value(), m_Value(), m_Value(), m_Value())));
 
+  // (Op0 + Op1) + Op0 binds correctly, allowing commutation
+  SDValue ADD010 = DAG->getNode(ISD::ADD, DL, Int32VT, ADD01, Op0);
+  SDValue A, B;
+  EXPECT_TRUE(sd_match(
+      ADD010, m_ReassociatableAdd(m_Value(A), m_Value(B), m_Deferred(A))));
+  EXPECT_TRUE(sd_match(
+      ADD010, m_ReassociatableAdd(m_Value(A), m_Value(B), m_Deferred(B))));
----------------
RKSimon wrote:

The problem I was often seeing wasn't that m_ReassociatableAdd etc. didn't return true - it was that the values of A + B weren't correct - please can you add additional checks that A and B are correctly initialized to the correct ADD01/Op0 pairs?

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


More information about the llvm-commits mailing list