[RISCV][GISel] Stop over promoting G_SITOFP/UITOFP libcalls on RV64. (#118597)
When we have legal instructions we want to promote to sXLen and let isel
pattern matching removing the and/sext_inreg.
When using a libcall we want to use a 'si' libcall for small types
instead of 'di'. To match the RV64 ABI, we need to sign extend `unsigned
int` arguments. We reuse the shouldSignExtendTypeInLibCall hook from
SelectionDAG.
diff --git a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
index d4323cd..cf835ad 100644
--- a/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
@@ -882,11 +882,20 @@
static LegalizerHelper::LegalizeResult
conversionLibcall(MachineInstr &MI, MachineIRBuilder &MIRBuilder, Type *ToType,
- Type *FromType, LostDebugLocObserver &LocObserver) {
+ Type *FromType, LostDebugLocObserver &LocObserver,
+ const TargetLowering &TLI, bool IsSigned = false) {
+ CallLowering::ArgInfo Arg = {MI.getOperand(1).getReg(), FromType, 0};
+ if (FromType->isIntegerTy()) {
+ if (TLI.shouldSignExtendTypeInLibCall(FromType, IsSigned))
+ Arg.Flags[0].setSExt();
+ else
+ Arg.Flags[0].setZExt();
+ }
+
RTLIB::Libcall Libcall = getConvRTLibDesc(MI.getOpcode(), ToType, FromType);
- return createLibcall(
- MIRBuilder, Libcall, {MI.getOperand(0).getReg(), ToType, 0},
- {{MI.getOperand(1).getReg(), FromType, 0}}, LocObserver, &MI);
+ return createLibcall(MIRBuilder, Libcall,
+ {MI.getOperand(0).getReg(), ToType, 0}, Arg, LocObserver,
+ &MI);
}
static RTLIB::Libcall
@@ -1308,7 +1317,7 @@
if (!FromTy || !ToTy)
return UnableToLegalize;
LegalizeResult Status =
- conversionLibcall(MI, MIRBuilder, ToTy, FromTy, LocObserver);
+ conversionLibcall(MI, MIRBuilder, ToTy, FromTy, LocObserver, TLI);
if (Status != Legalized)
return Status;
break;
@@ -1329,7 +1338,7 @@
if ((ToSize != 32 && ToSize != 64 && ToSize != 128) || !FromTy)
return UnableToLegalize;
LegalizeResult Status = conversionLibcall(
- MI, MIRBuilder, Type::getIntNTy(Ctx, ToSize), FromTy, LocObserver);
+ MI, MIRBuilder, Type::getIntNTy(Ctx, ToSize), FromTy, LocObserver, TLI);
if (Status != Legalized)
return Status;
break;
@@ -1341,8 +1350,10 @@
getFloatTypeForLLT(Ctx, MRI.getType(MI.getOperand(0).getReg()));
if ((FromSize != 32 && FromSize != 64 && FromSize != 128) || !ToTy)
return UnableToLegalize;
- LegalizeResult Status = conversionLibcall(
- MI, MIRBuilder, ToTy, Type::getIntNTy(Ctx, FromSize), LocObserver);
+ bool IsSigned = MI.getOpcode() == TargetOpcode::G_SITOFP;
+ LegalizeResult Status =
+ conversionLibcall(MI, MIRBuilder, ToTy, Type::getIntNTy(Ctx, FromSize),
+ LocObserver, TLI, IsSigned);
if (Status != Legalized)
return Status;
break;