Skip to content

Commit 17af9ad

Browse files
authored
[DAG] Add SDPatternMatch m_ZExtOrSelf/m_SExtOrSelf/m_AExtOrSelf/m_TruncOrSelf matchers (#85480)
Fixes #85395
1 parent 8411549 commit 17af9ad

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

llvm/include/llvm/CodeGen/SDPatternMatch.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,38 @@ template <typename Opnd> inline UnaryOpc_match<Opnd> m_Trunc(const Opnd &Op) {
632632
return UnaryOpc_match<Opnd>(ISD::TRUNCATE, Op);
633633
}
634634

635+
/// Match a zext or identity
636+
/// Allows to peek through optional extensions
637+
template <typename Opnd>
638+
inline Or<UnaryOpc_match<Opnd>, Opnd> m_ZExtOrSelf(Opnd &&Op) {
639+
return Or<UnaryOpc_match<Opnd>, Opnd>(m_ZExt(std::forward<Opnd>(Op)),
640+
std::forward<Opnd>(Op));
641+
}
642+
643+
/// Match a sext or identity
644+
/// Allows to peek through optional extensions
645+
template <typename Opnd>
646+
inline Or<UnaryOpc_match<Opnd>, Opnd> m_SExtOrSelf(Opnd &&Op) {
647+
return Or<UnaryOpc_match<Opnd>, Opnd>(m_SExt(std::forward<Opnd>(Op)),
648+
std::forward<Opnd>(Op));
649+
}
650+
651+
/// Match a aext or identity
652+
/// Allows to peek through optional extensions
653+
template <typename Opnd>
654+
inline Or<UnaryOpc_match<Opnd>, Opnd> m_AExtOrSelf(Opnd &&Op) {
655+
return Or<UnaryOpc_match<Opnd>, Opnd>(m_AnyExt(std::forward<Opnd>(Op)),
656+
std::forward<Opnd>(Op));
657+
}
658+
659+
/// Match a trunc or identity
660+
/// Allows to peek through optional truncations
661+
template <typename Opnd>
662+
inline Or<UnaryOpc_match<Opnd>, Opnd> m_TruncOrSelf(Opnd &&Op) {
663+
return Or<UnaryOpc_match<Opnd>, Opnd>(m_Trunc(std::forward<Opnd>(Op)),
664+
std::forward<Opnd>(Op));
665+
}
666+
635667
// === Constants ===
636668
struct ConstantInt_match {
637669
APInt *BindVal;
@@ -737,6 +769,7 @@ template <typename ValTy>
737769
inline BinaryOpc_match<ValTy, SpecificInt_match, true> m_Not(const ValTy &V) {
738770
return m_Xor(V, m_AllOnes());
739771
}
772+
740773
} // namespace SDPatternMatch
741774
} // namespace llvm
742775
#endif

llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,38 @@ TEST_F(SelectionDAGPatternMatchTest, patternCombinators) {
251251
EXPECT_TRUE(sd_match(Add, m_AllOf(m_Opc(ISD::ADD), m_OneUse())));
252252
}
253253

254+
TEST_F(SelectionDAGPatternMatchTest, optionalResizing) {
255+
SDLoc DL;
256+
auto Int32VT = EVT::getIntegerVT(Context, 32);
257+
auto Int64VT = EVT::getIntegerVT(Context, 64);
258+
259+
SDValue Op32 = DAG->getCopyFromReg(DAG->getEntryNode(), DL, 1, Int32VT);
260+
SDValue Op64 = DAG->getCopyFromReg(DAG->getEntryNode(), DL, 1, Int64VT);
261+
SDValue ZExt = DAG->getNode(ISD::ZERO_EXTEND, DL, Int64VT, Op32);
262+
SDValue SExt = DAG->getNode(ISD::SIGN_EXTEND, DL, Int64VT, Op32);
263+
SDValue AExt = DAG->getNode(ISD::ANY_EXTEND, DL, Int64VT, Op32);
264+
SDValue Trunc = DAG->getNode(ISD::TRUNCATE, DL, Int32VT, Op64);
265+
266+
using namespace SDPatternMatch;
267+
SDValue A;
268+
EXPECT_TRUE(sd_match(Op32, m_ZExtOrSelf(m_Value(A))));
269+
EXPECT_TRUE(A == Op32);
270+
EXPECT_TRUE(sd_match(ZExt, m_ZExtOrSelf(m_Value(A))));
271+
EXPECT_TRUE(A == Op32);
272+
EXPECT_TRUE(sd_match(Op64, m_SExtOrSelf(m_Value(A))));
273+
EXPECT_TRUE(A == Op64);
274+
EXPECT_TRUE(sd_match(SExt, m_SExtOrSelf(m_Value(A))));
275+
EXPECT_TRUE(A == Op32);
276+
EXPECT_TRUE(sd_match(Op32, m_AExtOrSelf(m_Value(A))));
277+
EXPECT_TRUE(A == Op32);
278+
EXPECT_TRUE(sd_match(AExt, m_AExtOrSelf(m_Value(A))));
279+
EXPECT_TRUE(A == Op32);
280+
EXPECT_TRUE(sd_match(Op64, m_TruncOrSelf(m_Value(A))));
281+
EXPECT_TRUE(A == Op64);
282+
EXPECT_TRUE(sd_match(Trunc, m_TruncOrSelf(m_Value(A))));
283+
EXPECT_TRUE(A == Op64);
284+
}
285+
254286
TEST_F(SelectionDAGPatternMatchTest, matchNode) {
255287
SDLoc DL;
256288
auto Int32VT = EVT::getIntegerVT(Context, 32);

0 commit comments

Comments
 (0)