Skip to content

Commit b185199

Browse files
committed
[DAG] Add SpecificFP matcher
1 parent 5430d7a commit b185199

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

llvm/include/llvm/CodeGen/SDPatternMatch.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,28 @@ inline SpecificInt_match m_SpecificInt(uint64_t V) {
11441144
return SpecificInt_match(APInt(64, V));
11451145
}
11461146

1147+
struct SpecificFP_match {
1148+
APFloat Val;
1149+
1150+
explicit SpecificFP_match(APFloat V) : Val(V) {}
1151+
1152+
template <typename MatchContext>
1153+
bool match(const MatchContext &Ctx, SDValue V) {
1154+
if (const auto *CFP = dyn_cast<ConstantFPSDNode>(V.getNode()))
1155+
return CFP->isExactlyValue(Val);
1156+
if (ConstantFPSDNode *C = isConstOrConstSplatFP(V, /*AllowUndefs=*/true))
1157+
return C->getValueAPF().compare(Val) == APFloat::cmpEqual;
1158+
return false;
1159+
}
1160+
};
1161+
1162+
/// Match a specific float constant.
1163+
inline SpecificFP_match m_SpecificFP(APFloat V) {
1164+
return SpecificFP_match(V);
1165+
}
1166+
1167+
inline SpecificFP_match m_SpecificFP(double V) { return SpecificFP_match(APFloat(V)); }
1168+
11471169
struct Zero_match {
11481170
bool AllowUndefs;
11491171

llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,35 @@ TEST_F(SelectionDAGPatternMatchTest, matchBinaryOp) {
354354
sd_match(InsertELT, m_InsertElt(m_Value(), m_Value(), m_SpecificInt(1))));
355355
}
356356

357+
TEST_F(SelectionDAGPatternMatchTest, matchSpecificFpOp) {
358+
SDLoc DL;
359+
APFloat Value(1.5f);
360+
auto Float32VT = EVT::getFloatingPointVT(32);
361+
SDValue Op0 = DAG->getCopyFromReg(DAG->getEntryNode(), DL, 1, Float32VT);
362+
SDValue Op1 = DAG->getConstantFP(Value, DL, Float32VT);
363+
SDValue FAdd = DAG->getNode(ISD::FADD, DL, Float32VT, Op0, Op1);
364+
365+
using namespace SDPatternMatch;
366+
367+
EXPECT_TRUE(sd_match(Op1, m_SpecificFP(Value)));
368+
EXPECT_TRUE(
369+
sd_match(FAdd, m_BinOp(ISD::FADD, m_Specific(Op0), m_SpecificFP(Value))));
370+
EXPECT_TRUE(sd_match(
371+
FAdd, m_c_BinOp(ISD::FADD, m_SpecificFP(Value), m_Specific(Op0))));
372+
373+
auto VFloat32VT = EVT::getVectorVT(Context, Float32VT, 2);
374+
SDValue VOp0 = DAG->getSplat(VFloat32VT, DL, Op0);
375+
SDValue VOp1 = DAG->getSplat(VFloat32VT, DL, Op1);
376+
377+
EXPECT_TRUE(sd_match(VOp1, m_SpecificFP(Value)));
378+
379+
SDValue VFAdd = DAG->getNode(ISD::FADD, DL, VFloat32VT, VOp0, VOp1);
380+
EXPECT_TRUE(sd_match(
381+
VFAdd, m_BinOp(ISD::FADD, m_Specific(VOp0), m_SpecificFP(Value))));
382+
EXPECT_TRUE(sd_match(
383+
VFAdd, m_c_BinOp(ISD::FADD, m_SpecificFP(Value), m_Specific(VOp0))));
384+
}
385+
357386
TEST_F(SelectionDAGPatternMatchTest, matchGenericTernaryOp) {
358387
SDLoc DL;
359388
auto Float32VT = EVT::getFloatingPointVT(32);

0 commit comments

Comments
 (0)