Skip to content

Commit 0c9d220

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

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

llvm/include/llvm/CodeGen/SDPatternMatch.h

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

1147+
struct SpecificFP_match {
1148+
std::optional<double> Val = std::nullopt;
1149+
1150+
explicit SpecificFP_match(std::optional<double> V) : Val(V) {}
1151+
1152+
template <typename MatchContext>
1153+
bool match(const MatchContext &Ctx, SDValue V) {
1154+
if (!Val.has_value())
1155+
return false;
1156+
if (const auto *CFP = dyn_cast<ConstantFPSDNode>(V.getNode()))
1157+
return CFP->isExactlyValue(Val.value());
1158+
if (ConstantFPSDNode *C = isConstOrConstSplatFP(V, true))
1159+
return C->getValueAPF().isExactlyValue(Val.value());
1160+
return false;
1161+
}
1162+
};
1163+
1164+
/// Match a specific float constant.
1165+
inline SpecificFP_match m_SpecificFP(APFloat V) {
1166+
return SpecificFP_match(V.convertToDouble());
1167+
}
1168+
1169+
inline SpecificFP_match m_SpecificFP(double V) { return SpecificFP_match(V); }
1170+
1171+
inline SpecificFP_match m_SpecificFP(SDValue V) {
1172+
if (ConstantFPSDNode *C = isConstOrConstSplatFP(V, true))
1173+
return SpecificFP_match(C->getValueAPF().convertToDouble());
1174+
return SpecificFP_match(std::nullopt);
1175+
}
1176+
11471177
struct Zero_match {
11481178
bool AllowUndefs;
11491179

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(VOp1)));
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(VOp1))));
382+
EXPECT_TRUE(sd_match(
383+
VFAdd, m_c_BinOp(ISD::FADD, m_SpecificFP(VOp1), m_Specific(VOp0))));
384+
}
385+
357386
TEST_F(SelectionDAGPatternMatchTest, matchGenericTernaryOp) {
358387
SDLoc DL;
359388
auto Float32VT = EVT::getFloatingPointVT(32);

0 commit comments

Comments
 (0)