Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions llvm/include/llvm/CodeGen/SDPatternMatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,28 @@ inline SpecificInt_match m_SpecificInt(uint64_t V) {
return SpecificInt_match(APInt(64, V));
}

struct SpecificFP_match {
APFloat Val;

explicit SpecificFP_match(APFloat V) : Val(V) {}

template <typename MatchContext>
bool match(const MatchContext &Ctx, SDValue V) {
if (const auto *CFP = dyn_cast<ConstantFPSDNode>(V.getNode()))
return CFP->isExactlyValue(Val);
if (ConstantFPSDNode *C = isConstOrConstSplatFP(V, /*AllowUndefs=*/true))
return C->getValueAPF().compare(Val) == APFloat::cmpEqual;
return false;
}
};

/// Match a specific float constant.
inline SpecificFP_match m_SpecificFP(APFloat V) { return SpecificFP_match(V); }

inline SpecificFP_match m_SpecificFP(double V) {
return SpecificFP_match(APFloat(V));
}

struct Zero_match {
bool AllowUndefs;

Expand Down
29 changes: 29 additions & 0 deletions llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,35 @@ TEST_F(SelectionDAGPatternMatchTest, matchBinaryOp) {
sd_match(InsertELT, m_InsertElt(m_Value(), m_Value(), m_SpecificInt(1))));
}

TEST_F(SelectionDAGPatternMatchTest, matchSpecificFpOp) {
SDLoc DL;
APFloat Value(1.5f);
auto Float32VT = EVT::getFloatingPointVT(32);
SDValue Op0 = DAG->getCopyFromReg(DAG->getEntryNode(), DL, 1, Float32VT);
SDValue Op1 = DAG->getConstantFP(Value, DL, Float32VT);
SDValue FAdd = DAG->getNode(ISD::FADD, DL, Float32VT, Op0, Op1);

using namespace SDPatternMatch;

EXPECT_TRUE(sd_match(Op1, m_SpecificFP(Value)));
EXPECT_TRUE(
sd_match(FAdd, m_BinOp(ISD::FADD, m_Specific(Op0), m_SpecificFP(Value))));
EXPECT_TRUE(sd_match(
FAdd, m_c_BinOp(ISD::FADD, m_SpecificFP(Value), m_Specific(Op0))));

auto VFloat32VT = EVT::getVectorVT(Context, Float32VT, 2);
SDValue VOp0 = DAG->getSplat(VFloat32VT, DL, Op0);
SDValue VOp1 = DAG->getSplat(VFloat32VT, DL, Op1);

EXPECT_TRUE(sd_match(VOp1, m_SpecificFP(Value)));

SDValue VFAdd = DAG->getNode(ISD::FADD, DL, VFloat32VT, VOp0, VOp1);
EXPECT_TRUE(sd_match(
VFAdd, m_BinOp(ISD::FADD, m_Specific(VOp0), m_SpecificFP(Value))));
EXPECT_TRUE(sd_match(
VFAdd, m_c_BinOp(ISD::FADD, m_SpecificFP(Value), m_Specific(VOp0))));
}

TEST_F(SelectionDAGPatternMatchTest, matchGenericTernaryOp) {
SDLoc DL;
auto Float32VT = EVT::getFloatingPointVT(32);
Expand Down