Skip to content
Open
Show file tree
Hide file tree
Changes from 14 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
45 changes: 45 additions & 0 deletions llvm/lib/Target/RISCV/RISCVISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9584,6 +9584,51 @@ SDValue RISCVTargetLowering::lowerSELECT(SDValue Op, SelectionDAG &DAG) const {
if (SDValue V = lowerSelectToBinOp(Op.getNode(), DAG, Subtarget))
return V;

// When there is no cost for GPR <-> FPR, we can use zicond select for
// floating value when CondV is int type
bool FPinGPR = Subtarget.hasStdExtZfinx();

// We can handle FGPR without spliting into hi/lo parts
bool FitsInGPR = TypeSize::isKnownLE(VT.getSizeInBits(),
Subtarget.getXLenVT().getSizeInBits());

bool UseZicondForFPSel = Subtarget.hasStdExtZicond() && FPinGPR &&
VT.isFloatingPoint() && FitsInGPR;

if (UseZicondForFPSel) {
MVT XLenIntVT = Subtarget.getXLenVT();

auto CastToInt = [&](SDValue V) -> SDValue {
// Treat +0.0 as int 0 to enable single 'czero' instruction generation.
if (isNullFPConstant(V))
return DAG.getConstant(0, DL, XLenIntVT);

if (VT == MVT::f16)
return DAG.getNode(RISCVISD::FMV_X_ANYEXTH, DL, XLenIntVT, V);

if (VT == MVT::f32 && Subtarget.is64Bit())
return DAG.getNode(RISCVISD::FMV_X_ANYEXTW_RV64, DL, XLenIntVT, V);

return DAG.getBitcast(XLenIntVT, V);
};

SDValue TrueVInt = CastToInt(TrueV);
SDValue FalseVInt = CastToInt(FalseV);

// Emit integer SELECT (lowers to Zicond)
SDValue ResultInt =
DAG.getNode(ISD::SELECT, DL, XLenIntVT, CondV, TrueVInt, FalseVInt);

// Convert back to floating VT
if (VT == MVT::f32 && Subtarget.is64Bit())
return DAG.getNode(RISCVISD::FMV_W_X_RV64, DL, VT, ResultInt);

if (VT == MVT::f16)
return DAG.getNode(RISCVISD::FMV_H_X, DL, VT, ResultInt);

return DAG.getBitcast(VT, ResultInt);
}

// When Zicond or XVentanaCondOps is present, emit CZERO_EQZ and CZERO_NEZ
// nodes to implement the SELECT. Performing the lowering here allows for
// greater control over when CZERO_{EQZ/NEZ} are used vs another branchless
Expand Down
Loading