Skip to content

Commit 8688a77

Browse files
committed
Common loweri64tof16ITOFP
1 parent 31ba237 commit 8688a77

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7609,6 +7609,22 @@ LegalizerHelper::lowerU64ToF64BitFloatOps(MachineInstr &MI) {
76097609
return Legalized;
76107610
}
76117611

7612+
/// i64->fp16 itofp can be lowered to i64->f64,f64->f32,f32->f16. We cannot
7613+
/// convert fpround f64->f16 without double-rounding, so we manually perform the
7614+
/// lowering here where we know it is valid.
7615+
static LegalizerHelper::LegalizeResult
7616+
loweri64tof16ITOFP(MachineInstr &MI, Register Dst, LLT DstTy, Register Src,
7617+
LLT SrcTy, MachineIRBuilder &MIRBuilder) {
7618+
auto M1 = MI.getOpcode() == TargetOpcode::G_UITOFP
7619+
? MIRBuilder.buildUITOFP(SrcTy, Src)
7620+
: MIRBuilder.buildSITOFP(SrcTy, Src);
7621+
LLT S32Ty = SrcTy.changeElementSize(32);
7622+
auto M2 = MIRBuilder.buildFPTrunc(S32Ty, M1);
7623+
MIRBuilder.buildFPTrunc(Dst, M2);
7624+
MI.eraseFromParent();
7625+
return LegalizerHelper::Legalized;
7626+
}
7627+
76127628
LegalizerHelper::LegalizeResult LegalizerHelper::lowerUITOFP(MachineInstr &MI) {
76137629
auto [Dst, DstTy, Src, SrcTy] = MI.getFirst2RegLLTs();
76147630

@@ -7620,17 +7636,8 @@ LegalizerHelper::LegalizeResult LegalizerHelper::lowerUITOFP(MachineInstr &MI) {
76207636
return Legalized;
76217637
}
76227638

7623-
// i64->fp16 itofp can be lowered to i64->f64,f64->f32,f32->f16. We cannot
7624-
// just convert fpround f64->f16 without double-rounding, so we manually
7625-
// perform the lowering here where we know it is valid.
7626-
if (DstTy.getScalarSizeInBits() == 16 && SrcTy.getScalarSizeInBits() == 64) {
7627-
auto M1 = MIRBuilder.buildUITOFP(SrcTy, Src);
7628-
LLT S32Ty = SrcTy.changeElementSize(32);
7629-
auto M2 = MIRBuilder.buildFPTrunc(S32Ty, M1);
7630-
MIRBuilder.buildFPTrunc(Dst, M2);
7631-
MI.eraseFromParent();
7632-
return Legalized;
7633-
}
7639+
if (DstTy.getScalarSizeInBits() == 16 && SrcTy.getScalarSizeInBits() == 64)
7640+
return loweri64tof16ITOFP(MI, Dst, DstTy, Src, SrcTy, MIRBuilder);
76347641

76357642
if (SrcTy != LLT::scalar(64))
76367643
return UnableToLegalize;
@@ -7663,17 +7670,8 @@ LegalizerHelper::LegalizeResult LegalizerHelper::lowerSITOFP(MachineInstr &MI) {
76637670
return Legalized;
76647671
}
76657672

7666-
// i64->fp16 itofp can be lowered to i64->f64,f64->f32,f32->f16. We cannot
7667-
// just convert fpround f64->f16 without double-rounding, so we manually
7668-
// perform the lowering here where we know it is valid.
7669-
if (DstTy.getScalarSizeInBits() == 16 && SrcTy.getScalarSizeInBits() == 64) {
7670-
auto M1 = MIRBuilder.buildSITOFP(SrcTy, Src);
7671-
LLT S32Ty = SrcTy.changeElementSize(32);
7672-
auto M2 = MIRBuilder.buildFPTrunc(S32Ty, M1);
7673-
MIRBuilder.buildFPTrunc(Dst, M2);
7674-
MI.eraseFromParent();
7675-
return Legalized;
7676-
}
7673+
if (DstTy.getScalarSizeInBits() == 16 && SrcTy.getScalarSizeInBits() == 64)
7674+
return loweri64tof16ITOFP(MI, Dst, DstTy, Src, SrcTy, MIRBuilder);
76777675

76787676
if (SrcTy != S64)
76797677
return UnableToLegalize;

0 commit comments

Comments
 (0)