@@ -576,3 +576,88 @@ TEST_F(SelectionDAGPatternMatchTest, matchAdvancedProperties) {
576576 EXPECT_TRUE (sd_match (Add, DAG.get (),
577577 m_LegalOp (m_IntegerVT (m_Add (m_Value (), m_Value ())))));
578578}
579+
580+ TEST_F (SelectionDAGPatternMatchTest, matchReassociatableOp) {
581+ using namespace SDPatternMatch ;
582+
583+ SDLoc DL;
584+ auto Int32VT = EVT::getIntegerVT (Context, 32 );
585+ auto Float32VT = EVT::getFloatingPointVT (32 );
586+
587+ SDValue Op0 = DAG->getCopyFromReg (DAG->getEntryNode (), DL, 1 , Int32VT);
588+ SDValue Op1 = DAG->getCopyFromReg (DAG->getEntryNode (), DL, 2 , Int32VT);
589+ SDValue Op2 = DAG->getCopyFromReg (DAG->getEntryNode (), DL, 3 , Float32VT);
590+ SDValue Op3 = DAG->getCopyFromReg (DAG->getEntryNode (), DL, 8 , Int32VT);
591+
592+ // (Op0 + Op1) + (Op2 + Op3)
593+ SDValue ADD01 = DAG->getNode (ISD::ADD, DL, Int32VT, Op0, Op1);
594+ SDValue ADD23 = DAG->getNode (ISD::ADD, DL, Int32VT, Op2, Op3);
595+ SDValue ADD = DAG->getNode (ISD::ADD, DL, Int32VT, ADD01, ADD23);
596+
597+ EXPECT_TRUE (sd_match (ADD01, m_ReassociatableAdd (m_Value (), m_Value ())));
598+ EXPECT_TRUE (sd_match (ADD23, m_ReassociatableAdd (m_Value (), m_Value ())));
599+ EXPECT_TRUE (sd_match (
600+ ADD, m_ReassociatableAdd (m_Value (), m_Value (), m_Value (), m_Value ())));
601+
602+ // Op0 + (Op1 + (Op2 + Op3))
603+ SDValue ADD123 = DAG->getNode (ISD::ADD, DL, Int32VT, Op2, ADD23);
604+ SDValue ADD0123 = DAG->getNode (ISD::ADD, DL, Int32VT, Op0, ADD123);
605+ EXPECT_TRUE (
606+ sd_match (ADD123, m_ReassociatableAdd (m_Value (), m_Value (), m_Value ())));
607+ EXPECT_TRUE (sd_match (ADD0123, m_ReassociatableAdd (m_Value (), m_Value (),
608+ m_Value (), m_Value ())));
609+
610+ // (Op0 * Op1) * (Op2 * Op3)
611+ SDValue MUL01 = DAG->getNode (ISD::MUL, DL, Int32VT, Op0, Op1);
612+ SDValue MUL23 = DAG->getNode (ISD::MUL, DL, Int32VT, Op2, Op3);
613+ SDValue MUL = DAG->getNode (ISD::MUL, DL, Int32VT, MUL01, MUL23);
614+
615+ EXPECT_TRUE (sd_match (MUL01, m_ReassociatableMul (m_Value (), m_Value ())));
616+ EXPECT_TRUE (sd_match (MUL23, m_ReassociatableMul (m_Value (), m_Value ())));
617+ EXPECT_TRUE (sd_match (
618+ MUL, m_ReassociatableMul (m_Value (), m_Value (), m_Value (), m_Value ())));
619+
620+ // Op0 * (Op1 * (Op2 * Op3))
621+ SDValue MUL123 = DAG->getNode (ISD::MUL, DL, Int32VT, Op2, MUL23);
622+ SDValue MUL0123 = DAG->getNode (ISD::MUL, DL, Int32VT, Op0, MUL123);
623+ EXPECT_TRUE (
624+ sd_match (MUL123, m_ReassociatableMul (m_Value (), m_Value (), m_Value ())));
625+ EXPECT_TRUE (sd_match (MUL0123, m_ReassociatableMul (m_Value (), m_Value (),
626+ m_Value (), m_Value ())));
627+
628+ // (Op0 && Op1) && (Op2 && Op3)
629+ SDValue AND01 = DAG->getNode (ISD::AND, DL, Int32VT, Op0, Op1);
630+ SDValue AND23 = DAG->getNode (ISD::AND, DL, Int32VT, Op2, Op3);
631+ SDValue AND = DAG->getNode (ISD::AND, DL, Int32VT, AND01, AND23);
632+
633+ EXPECT_TRUE (sd_match (AND01, m_ReassociatableAnd (m_Value (), m_Value ())));
634+ EXPECT_TRUE (sd_match (AND23, m_ReassociatableAnd (m_Value (), m_Value ())));
635+ EXPECT_TRUE (sd_match (
636+ AND, m_ReassociatableAnd (m_Value (), m_Value (), m_Value (), m_Value ())));
637+
638+ // Op0 && (Op1 && (Op2 && Op3))
639+ SDValue AND123 = DAG->getNode (ISD::AND, DL, Int32VT, Op2, AND23);
640+ SDValue AND0123 = DAG->getNode (ISD::AND, DL, Int32VT, Op0, AND123);
641+ EXPECT_TRUE (
642+ sd_match (AND123, m_ReassociatableAnd (m_Value (), m_Value (), m_Value ())));
643+ EXPECT_TRUE (sd_match (AND0123, m_ReassociatableAnd (m_Value (), m_Value (),
644+ m_Value (), m_Value ())));
645+
646+ // (Op0 || Op1) || (Op2 || Op3)
647+ SDValue OR01 = DAG->getNode (ISD::OR, DL, Int32VT, Op0, Op1);
648+ SDValue OR23 = DAG->getNode (ISD::OR, DL, Int32VT, Op2, Op3);
649+ SDValue OR = DAG->getNode (ISD::OR, DL, Int32VT, OR01, OR23);
650+
651+ EXPECT_TRUE (sd_match (OR01, m_ReassociatableOr (m_Value (), m_Value ())));
652+ EXPECT_TRUE (sd_match (OR23, m_ReassociatableOr (m_Value (), m_Value ())));
653+ EXPECT_TRUE (sd_match (
654+ OR, m_ReassociatableOr (m_Value (), m_Value (), m_Value (), m_Value ())));
655+
656+ // Op0 || (Op1 || (Op2 || Op3))
657+ SDValue OR123 = DAG->getNode (ISD::OR, DL, Int32VT, Op2, OR23);
658+ SDValue OR0123 = DAG->getNode (ISD::OR, DL, Int32VT, Op0, OR123);
659+ EXPECT_TRUE (
660+ sd_match (OR123, m_ReassociatableOr (m_Value (), m_Value (), m_Value ())));
661+ EXPECT_TRUE (sd_match (
662+ OR0123, m_ReassociatableOr (m_Value (), m_Value (), m_Value (), m_Value ())));
663+ }
0 commit comments