[llvm] [DAG] Add SDPatternMatch m_ZExtOrSelf/m_SExtOrSelf/m_AExtOrSelf/m_TruncOrSelf matchers (PR #85480)
Marc Auberer via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 15 15:37:36 PDT 2024
https://github.com/marcauberer created https://github.com/llvm/llvm-project/pull/85480
Fixes #85395
>From e9d8ada9331439a8b8e8bc9bb91d66ef76ed8381 Mon Sep 17 00:00:00 2001
From: Marc Auberer <marc.auberer at chillibits.com>
Date: Fri, 15 Mar 2024 23:22:54 +0100
Subject: [PATCH] [DAG] Add SDPatternMatch
m_ZExtOrSelf/m_SExtOrSelf/m_AExtOrSelf/m_TruncOrSelf matchers
---
llvm/include/llvm/CodeGen/SDPatternMatch.h | 33 +++++++++++++++++++
.../CodeGen/SelectionDAGPatternMatchTest.cpp | 29 ++++++++++++++++
2 files changed, 62 insertions(+)
diff --git a/llvm/include/llvm/CodeGen/SDPatternMatch.h b/llvm/include/llvm/CodeGen/SDPatternMatch.h
index 96ab6b160a1c77..ba34f1cc81811c 100644
--- a/llvm/include/llvm/CodeGen/SDPatternMatch.h
+++ b/llvm/include/llvm/CodeGen/SDPatternMatch.h
@@ -725,6 +725,39 @@ inline auto m_False() {
},
m_Value()};
}
+
+/// Match a zext or identity
+/// Allows to peek through optional extensions
+template <typename LHS, typename RHS>
+inline BinaryOpc_match<LHS, RHS, true> m_ZExtOrSelf(const LHS &L,
+ const RHS &R) {
+ return m_Or(m_ZExt(L), R);
+}
+
+/// Match a sext or identity
+/// Allows to peek through optional extensions
+template <typename LHS, typename RHS>
+inline BinaryOpc_match<LHS, RHS, true> m_SExtOrSelf(const LHS &L,
+ const RHS &R) {
+ return m_Or(m_SExt(L), R);
+}
+
+/// Match a aext or identity
+/// Allows to peek through optional extensions
+template <typename LHS, typename RHS>
+inline BinaryOpc_match<LHS, RHS, true> m_AExtOrSelf(const LHS &L,
+ const RHS &R) {
+ return m_Or(m_AnyExt(L), R);
+}
+
+/// Match a trunc or identity
+/// Allows to peek through optional truncations
+template <typename LHS, typename RHS>
+inline BinaryOpc_match<LHS, RHS, true> m_TruncOrSelf(const LHS &L,
+ const RHS &R) {
+ return m_Or(m_Trunc(L), R);
+}
+
} // namespace SDPatternMatch
} // namespace llvm
#endif
diff --git a/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp b/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
index 180d4306a470f7..d803ac8bf3f450 100644
--- a/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
+++ b/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
@@ -241,6 +241,35 @@ 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);
+
+ SDValue Op0 = DAG->getCopyFromReg(DAG->getEntryNode(), DL, 1, Int32VT);
+
+ // zero extend
+ SDValue ZExt = DAG->getNode(ISD::ZERO_EXTEND, DL, Int32VT, Op0);
+ SDValue OptionalZExt = DAG->getNode(ISD::OR, DL, Int32VT, Op0, ZExt);
+
+ // sign extend
+ SDValue SExt = DAG->getNode(ISD::SIGN_EXTEND, DL, Int32VT, Op0);
+ SDValue OptionalSExt = DAG->getNode(ISD::OR, DL, Int32VT, Op0, SExt);
+
+ // any extend
+ SDValue AExt = DAG->getNode(ISD::ANY_EXTEND, DL, Int32VT, Op0);
+ SDValue OptionalAExt = DAG->getNode(ISD::OR, DL, Int32VT, Op0, AExt);
+
+ // truncate
+ SDValue Trunc = DAG->getNode(ISD::TRUNCATE, DL, Int32VT, Op0);
+ SDValue OptionalTrunc = DAG->getNode(ISD::OR, DL, Int32VT, Op0, Trunc);
+
+ using namespace SDPatternMatch;
+ EXPECT_TRUE(sd_match(OptionalZExt, m_ZExtOrSelf(m_Value(), m_Value())));
+ EXPECT_TRUE(sd_match(OptionalSExt, m_SExtOrSelf(m_Value(), m_Value())));
+ EXPECT_TRUE(sd_match(OptionalAExt, m_AExtOrSelf(m_Value(), m_Value())));
+ EXPECT_TRUE(sd_match(OptionalTrunc, m_TruncOrSelf(m_Value(), m_Value())));
+}
+
TEST_F(SelectionDAGPatternMatchTest, matchNode) {
SDLoc DL;
auto Int32VT = EVT::getIntegerVT(Context, 32);
More information about the llvm-commits
mailing list