[llvm] [DAG] SDPatternMatch - add a m_SpecificNeg() matcher (PR #173807)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 2 04:18:51 PST 2026


================
@@ -1062,3 +1062,52 @@ TEST_F(SelectionDAGPatternMatchTest, MatchSelectCC) {
                                           m_Specific(TVal), m_Specific(FVal),
                                           m_CondCode(CC))));
 }
+
+TEST_F(SelectionDAGPatternMatchTest, MatchSpecificNeg) {
+  SDLoc DL;
+  auto Int32VT = EVT::getIntegerVT(Context, 32);
+  auto VecVT = EVT::getVectorVT(Context, Int32VT, 4);
+
+  SDValue Op0 = DAG->getCopyFromReg(DAG->getEntryNode(), DL, 1, Int32VT);
+
+  using namespace SDPatternMatch;
+
+  SDValue Neg = DAG->getNegative(Op0, DL, Int32VT);
+  EXPECT_TRUE(sd_match(Neg, m_SpecificNeg(Op0)));
+  EXPECT_TRUE(sd_match(Neg, m_Neg(m_Specific(Op0))));
+
+  SDValue Op1 = DAG->getCopyFromReg(DAG->getEntryNode(), DL, 2, Int32VT);
+  EXPECT_FALSE(sd_match(Neg, m_SpecificNeg(Op1)));
+
+  SDValue Const5 = DAG->getConstant(5, DL, Int32VT);
+  SDValue ConstNeg5 = DAG->getConstant(APInt(32, -5, true), DL, Int32VT);
+  EXPECT_TRUE(sd_match(ConstNeg5, m_SpecificNeg(Const5)));
+  EXPECT_TRUE(sd_match(Const5, m_SpecificNeg(ConstNeg5)));
+
+  SDValue Const3 = DAG->getConstant(3, DL, Int32VT);
+  EXPECT_FALSE(sd_match(ConstNeg5, m_SpecificNeg(Const3)));
+
+  SDValue VecConst5 = DAG->getSplatBuildVector(VecVT, DL, Const5);
+  SDValue VecConstNeg5 = DAG->getSplatBuildVector(VecVT, DL, ConstNeg5);
+  EXPECT_TRUE(sd_match(VecConstNeg5, m_SpecificNeg(VecConst5)));
+  EXPECT_TRUE(sd_match(VecConst5, m_SpecificNeg(VecConstNeg5)));
+
+  SDValue Const1 = DAG->getConstant(1, DL, Int32VT);
+  SDValue Const2 = DAG->getConstant(2, DL, Int32VT);
+  SDValue ConstNeg1 = DAG->getConstant(APInt(32, -1, true), DL, Int32VT);
+  SDValue ConstNeg2 = DAG->getConstant(APInt(32, -2, true), DL, Int32VT);
+  SDValue ConstNeg3 = DAG->getConstant(APInt(32, -3, true), DL, Int32VT);
+  SmallVector<SDValue, 4> PosOps = {Const1, Const2, Const5, Const3};
+  SmallVector<SDValue, 4> NegOps = {ConstNeg1, ConstNeg2, ConstNeg5, ConstNeg3};
+  SDValue VecPos = DAG->getBuildVector(VecVT, DL, PosOps);
+  SDValue VecNeg = DAG->getBuildVector(VecVT, DL, NegOps);
+  EXPECT_TRUE(sd_match(VecNeg, m_SpecificNeg(VecPos)));
+  EXPECT_TRUE(sd_match(VecPos, m_SpecificNeg(VecNeg)));
----------------
RKSimon wrote:

Please can you add test coverage with buildvectors with different scalar constant widths (they are allowed to be implicitly truncated)

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


More information about the llvm-commits mailing list