Skip to content

Commit 91fc4e0

Browse files
author
Aditya Nandakumar
committed
[GISel]: Add helpers for easy building G_FCONSTANT along with matchers
Added helpers to build G_FCONSTANT, along with matching ConstantFP and unit tests for the same. Sample usage. auto MIB = Builder.buildFConstant(s32, 0.5); // Build IEEESingle For Matching the above const ConstantFP* Tmp; mi_match(DstReg, MRI, m_GFCst(Tmp)); https://reviews.llvm.org/D44128 reviewed by: volkan llvm-svn: 327152
1 parent 2e55ee7 commit 91fc4e0

File tree

6 files changed

+80
-0
lines changed

6 files changed

+80
-0
lines changed

llvm/include/llvm/CodeGen/GlobalISel/MIPatternMatch.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,16 @@ template <> struct bind_helper<LLT> {
142142
}
143143
};
144144

145+
template <> struct bind_helper<const ConstantFP *> {
146+
static bool bind(const MachineRegisterInfo &MRI, const ConstantFP *&F,
147+
unsigned Reg) {
148+
F = getConstantFPVRegVal(Reg, MRI);
149+
if (F)
150+
return true;
151+
return false;
152+
}
153+
};
154+
145155
template <typename Class> struct bind_ty {
146156
Class &VR;
147157

@@ -156,6 +166,9 @@ inline bind_ty<unsigned> m_Reg(unsigned &R) { return R; }
156166
inline bind_ty<MachineInstr *> m_MInstr(MachineInstr *&MI) { return MI; }
157167
inline bind_ty<LLT> m_Type(LLT &Ty) { return Ty; }
158168

169+
// Helper for matching G_FCONSTANT
170+
inline bind_ty<const ConstantFP *> m_GFCst(const ConstantFP *&C) { return C; }
171+
159172
// General helper for all the binary generic MI such as G_ADD/G_SUB etc
160173
template <typename LHS_P, typename RHS_P, unsigned Opcode,
161174
bool Commutable = false>

llvm/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,12 @@ class MachineIRBuilder {
572572
}
573573
MachineInstrBuilder buildFConstant(unsigned Res, const ConstantFP &Val);
574574

575+
template <typename DstType>
576+
MachineInstrBuilder buildFConstant(DstType &&Res, double Val) {
577+
return buildFConstant(getDestFromArg(Res), Val);
578+
}
579+
MachineInstrBuilder buildFConstant(unsigned Res, double Val);
580+
575581
/// Build and insert \p Res = COPY Op
576582
///
577583
/// Register-to-register COPY sets \p Res to \p Op.

llvm/include/llvm/CodeGen/GlobalISel/Utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class TargetRegisterInfo;
3333
class TargetRegisterClass;
3434
class Twine;
3535
class ConstantFP;
36+
class APFloat;
3637

3738
/// Try to constrain Reg to the specified register class. If this fails,
3839
/// create a new virtual register in the correct class and insert a COPY before
@@ -99,5 +100,7 @@ const ConstantFP* getConstantFPVRegVal(unsigned VReg,
99100
MachineInstr *getOpcodeDef(unsigned Opcode, unsigned Reg,
100101
const MachineRegisterInfo &MRI);
101102

103+
/// Returns an APFloat from Val converted to the appropriate size.
104+
APFloat getAPFloatFromSize(double Val, unsigned Size);
102105
} // End namespace llvm.
103106
#endif

llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,14 @@ MachineInstrBuilder MachineIRBuilder::buildFConstant(unsigned Res,
284284
return buildInstr(TargetOpcode::G_FCONSTANT).addDef(Res).addFPImm(&Val);
285285
}
286286

287+
MachineInstrBuilder MachineIRBuilder::buildFConstant(unsigned Res, double Val) {
288+
LLT DstTy = MRI->getType(Res);
289+
auto &Ctx = MF->getFunction().getContext();
290+
auto *CFP =
291+
ConstantFP::get(Ctx, getAPFloatFromSize(Val, DstTy.getSizeInBits()));
292+
return buildFConstant(Res, *CFP);
293+
}
294+
287295
MachineInstrBuilder MachineIRBuilder::buildBrCond(unsigned Tst,
288296
MachineBasicBlock &Dest) {
289297
assert(MRI->getType(Tst).isScalar() && "invalid operand type");

llvm/lib/CodeGen/GlobalISel/Utils.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "llvm/CodeGen/GlobalISel/Utils.h"
14+
#include "llvm/ADT/APFloat.h"
1415
#include "llvm/ADT/Twine.h"
1516
#include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h"
1617
#include "llvm/CodeGen/MachineInstr.h"
@@ -212,3 +213,16 @@ llvm::MachineInstr *llvm::getOpcodeDef(unsigned Opcode, unsigned Reg,
212213
}
213214
return DefMI->getOpcode() == Opcode ? DefMI : nullptr;
214215
}
216+
217+
APFloat llvm::getAPFloatFromSize(double Val, unsigned Size) {
218+
if (Size == 32)
219+
return APFloat(float(Val));
220+
if (Size == 64)
221+
return APFloat(Val);
222+
if (Size != 16)
223+
llvm_unreachable("Unsupported FPConstant size");
224+
bool Ignored;
225+
APFloat APF(Val);
226+
APF.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &Ignored);
227+
return APF;
228+
}

llvm/unittests/CodeGen/GlobalISel/PatternMatchTest.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,42 @@ TEST(PatternMatchInstr, MatchFPUnaryOp) {
257257
match = mi_match(MIBFabs->getOperand(0).getReg(), MRI, m_GFabs(m_Reg(Src)));
258258
ASSERT_TRUE(match);
259259
ASSERT_EQ(Src, Copy0s32->getOperand(0).getReg());
260+
261+
// Build and match FConstant.
262+
auto MIBFCst = B.buildFConstant(s32, .5);
263+
const ConstantFP *TmpFP{};
264+
match = mi_match(MIBFCst->getOperand(0).getReg(), MRI, m_GFCst(TmpFP));
265+
ASSERT_TRUE(match);
266+
ASSERT_TRUE(TmpFP);
267+
APFloat APF((float).5);
268+
auto *CFP = ConstantFP::get(Context, APF);
269+
ASSERT_EQ(CFP, TmpFP);
270+
271+
// Build double float.
272+
LLT s64 = LLT::scalar(64);
273+
auto MIBFCst64 = B.buildFConstant(s64, .5);
274+
const ConstantFP *TmpFP64{};
275+
match = mi_match(MIBFCst64->getOperand(0).getReg(), MRI, m_GFCst(TmpFP64));
276+
ASSERT_TRUE(match);
277+
ASSERT_TRUE(TmpFP64);
278+
APFloat APF64(.5);
279+
auto CFP64 = ConstantFP::get(Context, APF64);
280+
ASSERT_EQ(CFP64, TmpFP64);
281+
ASSERT_NE(TmpFP64, TmpFP);
282+
283+
// Build half float.
284+
LLT s16 = LLT::scalar(16);
285+
auto MIBFCst16 = B.buildFConstant(s16, .5);
286+
const ConstantFP *TmpFP16{};
287+
match = mi_match(MIBFCst16->getOperand(0).getReg(), MRI, m_GFCst(TmpFP16));
288+
ASSERT_TRUE(match);
289+
ASSERT_TRUE(TmpFP16);
290+
bool Ignored;
291+
APFloat APF16(.5);
292+
APF16.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven, &Ignored);
293+
auto CFP16 = ConstantFP::get(Context, APF16);
294+
ASSERT_EQ(TmpFP16, CFP16);
295+
ASSERT_NE(TmpFP16, TmpFP);
260296
}
261297

262298
TEST(PatternMatchInstr, MatchExtendsTrunc) {

0 commit comments

Comments
 (0)