[llvm] 17af9ad - [DAG] Add SDPatternMatch m_ZExtOrSelf/m_SExtOrSelf/m_AExtOrSelf/m_TruncOrSelf matchers (#85480)

via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 20 13:19:02 PDT 2024


Author: Marc Auberer
Date: 2024-03-20T13:18:58-07:00
New Revision: 17af9addbbd0dc675c962bf034ea083b2b61a01a

URL: https://github.com/llvm/llvm-project/commit/17af9addbbd0dc675c962bf034ea083b2b61a01a
DIFF: https://github.com/llvm/llvm-project/commit/17af9addbbd0dc675c962bf034ea083b2b61a01a.diff

LOG: [DAG] Add SDPatternMatch m_ZExtOrSelf/m_SExtOrSelf/m_AExtOrSelf/m_TruncOrSelf matchers (#85480)

Fixes #85395

Added: 
    

Modified: 
    llvm/include/llvm/CodeGen/SDPatternMatch.h
    llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/CodeGen/SDPatternMatch.h b/llvm/include/llvm/CodeGen/SDPatternMatch.h
index 541c0ecb5be373..4cc7bb9c3b55a9 100644
--- a/llvm/include/llvm/CodeGen/SDPatternMatch.h
+++ b/llvm/include/llvm/CodeGen/SDPatternMatch.h
@@ -632,6 +632,38 @@ template <typename Opnd> inline UnaryOpc_match<Opnd> m_Trunc(const Opnd &Op) {
   return UnaryOpc_match<Opnd>(ISD::TRUNCATE, Op);
 }
 
+/// Match a zext or identity
+/// Allows to peek through optional extensions
+template <typename Opnd>
+inline Or<UnaryOpc_match<Opnd>, Opnd> m_ZExtOrSelf(Opnd &&Op) {
+  return Or<UnaryOpc_match<Opnd>, Opnd>(m_ZExt(std::forward<Opnd>(Op)),
+                                        std::forward<Opnd>(Op));
+}
+
+/// Match a sext or identity
+/// Allows to peek through optional extensions
+template <typename Opnd>
+inline Or<UnaryOpc_match<Opnd>, Opnd> m_SExtOrSelf(Opnd &&Op) {
+  return Or<UnaryOpc_match<Opnd>, Opnd>(m_SExt(std::forward<Opnd>(Op)),
+                                        std::forward<Opnd>(Op));
+}
+
+/// Match a aext or identity
+/// Allows to peek through optional extensions
+template <typename Opnd>
+inline Or<UnaryOpc_match<Opnd>, Opnd> m_AExtOrSelf(Opnd &&Op) {
+  return Or<UnaryOpc_match<Opnd>, Opnd>(m_AnyExt(std::forward<Opnd>(Op)),
+                                        std::forward<Opnd>(Op));
+}
+
+/// Match a trunc or identity
+/// Allows to peek through optional truncations
+template <typename Opnd>
+inline Or<UnaryOpc_match<Opnd>, Opnd> m_TruncOrSelf(Opnd &&Op) {
+  return Or<UnaryOpc_match<Opnd>, Opnd>(m_Trunc(std::forward<Opnd>(Op)),
+                                        std::forward<Opnd>(Op));
+}
+
 // === Constants ===
 struct ConstantInt_match {
   APInt *BindVal;
@@ -737,6 +769,7 @@ template <typename ValTy>
 inline BinaryOpc_match<ValTy, SpecificInt_match, true> m_Not(const ValTy &V) {
   return m_Xor(V, m_AllOnes());
 }
+
 } // namespace SDPatternMatch
 } // namespace llvm
 #endif

diff  --git a/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp b/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
index 1967a62bbf9d11..a7112cfac63de5 100644
--- a/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
+++ b/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
@@ -251,6 +251,38 @@ TEST_F(SelectionDAGPatternMatchTest, patternCombinators) {
   EXPECT_TRUE(sd_match(Add, m_AllOf(m_Opc(ISD::ADD), m_OneUse())));
 }
 
+TEST_F(SelectionDAGPatternMatchTest, optionalResizing) {
+  SDLoc DL;
+  auto Int32VT = EVT::getIntegerVT(Context, 32);
+  auto Int64VT = EVT::getIntegerVT(Context, 64);
+
+  SDValue Op32 = DAG->getCopyFromReg(DAG->getEntryNode(), DL, 1, Int32VT);
+  SDValue Op64 = DAG->getCopyFromReg(DAG->getEntryNode(), DL, 1, Int64VT);
+  SDValue ZExt = DAG->getNode(ISD::ZERO_EXTEND, DL, Int64VT, Op32);
+  SDValue SExt = DAG->getNode(ISD::SIGN_EXTEND, DL, Int64VT, Op32);
+  SDValue AExt = DAG->getNode(ISD::ANY_EXTEND, DL, Int64VT, Op32);
+  SDValue Trunc = DAG->getNode(ISD::TRUNCATE, DL, Int32VT, Op64);
+
+  using namespace SDPatternMatch;
+  SDValue A;
+  EXPECT_TRUE(sd_match(Op32, m_ZExtOrSelf(m_Value(A))));
+  EXPECT_TRUE(A == Op32);
+  EXPECT_TRUE(sd_match(ZExt, m_ZExtOrSelf(m_Value(A))));
+  EXPECT_TRUE(A == Op32);
+  EXPECT_TRUE(sd_match(Op64, m_SExtOrSelf(m_Value(A))));
+  EXPECT_TRUE(A == Op64);
+  EXPECT_TRUE(sd_match(SExt, m_SExtOrSelf(m_Value(A))));
+  EXPECT_TRUE(A == Op32);
+  EXPECT_TRUE(sd_match(Op32, m_AExtOrSelf(m_Value(A))));
+  EXPECT_TRUE(A == Op32);
+  EXPECT_TRUE(sd_match(AExt, m_AExtOrSelf(m_Value(A))));
+  EXPECT_TRUE(A == Op32);
+  EXPECT_TRUE(sd_match(Op64, m_TruncOrSelf(m_Value(A))));
+  EXPECT_TRUE(A == Op64);
+  EXPECT_TRUE(sd_match(Trunc, m_TruncOrSelf(m_Value(A))));
+  EXPECT_TRUE(A == Op64);
+}
+
 TEST_F(SelectionDAGPatternMatchTest, matchNode) {
   SDLoc DL;
   auto Int32VT = EVT::getIntegerVT(Context, 32);


        


More information about the llvm-commits mailing list