Skip to content

Commit 485284a

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

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

llvm/include/llvm/CodeGen/SDPatternMatch.h

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

1147+
struct SpecificFP_match {
1148+
double Val;
1149+
1150+
explicit SpecificFP_match(double 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+
if (CFP->isExactlyValue(Val))
1156+
return true;
1157+
return false;
1158+
}
1159+
};
1160+
1161+
/// Match a specific float constant.
1162+
inline SpecificFP_match m_SpecificFP(APFloat V) {
1163+
return SpecificFP_match(V.convertToDouble());
1164+
}
1165+
1166+
inline SpecificFP_match m_SpecificFP(double V) { return SpecificFP_match(V); }
1167+
11471168
struct Zero_match {
11481169
bool AllowUndefs;
11491170

llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,21 @@ 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(Op0, m_SpecificFP(Value)));
368+
EXPECT_TRUE(
369+
sd_match(FAdd, m_BinOp(ISD::FADD, m_Specific(Op1), m_SpecificFP(Value))));
370+
}
371+
357372
TEST_F(SelectionDAGPatternMatchTest, matchGenericTernaryOp) {
358373
SDLoc DL;
359374
auto Float32VT = EVT::getFloatingPointVT(32);

0 commit comments

Comments
 (0)