Skip to content

Commit 5523f7a

Browse files
committed
[RISCV] Support saturated truncate
Add support for `ISD::TRUNCATE_[US]SAT`.
1 parent e53f9bd commit 5523f7a

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,9 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
853853

854854
// Integer VTs are lowered as a series of "RISCVISD::TRUNCATE_VECTOR_VL"
855855
// nodes which truncate by one power of two at a time.
856-
setOperationAction(ISD::TRUNCATE, VT, Custom);
856+
setOperationAction({ISD::TRUNCATE, ISD::TRUNCATE_SSAT_S,
857+
ISD::TRUNCATE_SSAT_U, ISD::TRUNCATE_USAT_U},
858+
VT, Custom);
857859

858860
// Custom-lower insert/extract operations to simplify patterns.
859861
setOperationAction({ISD::INSERT_VECTOR_ELT, ISD::EXTRACT_VECTOR_ELT}, VT,
@@ -1168,7 +1170,9 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
11681170

11691171
setOperationAction(ISD::SELECT, VT, Custom);
11701172

1171-
setOperationAction(ISD::TRUNCATE, VT, Custom);
1173+
setOperationAction({ISD::TRUNCATE, ISD::TRUNCATE_SSAT_S,
1174+
ISD::TRUNCATE_SSAT_U, ISD::TRUNCATE_USAT_U},
1175+
VT, Custom);
11721176

11731177
setOperationAction(ISD::BITCAST, VT, Custom);
11741178

@@ -6395,6 +6399,9 @@ SDValue RISCVTargetLowering::LowerOperation(SDValue Op,
63956399
return DAG.getNode(RISCVISD::BREV8, DL, VT, BSwap);
63966400
}
63976401
case ISD::TRUNCATE:
6402+
case ISD::TRUNCATE_SSAT_S:
6403+
case ISD::TRUNCATE_SSAT_U:
6404+
case ISD::TRUNCATE_USAT_U:
63986405
// Only custom-lower vector truncates
63996406
if (!Op.getSimpleValueType().isVector())
64006407
return Op;
@@ -8234,7 +8241,8 @@ SDValue RISCVTargetLowering::lowerVectorMaskTruncLike(SDValue Op,
82348241

82358242
SDValue RISCVTargetLowering::lowerVectorTruncLike(SDValue Op,
82368243
SelectionDAG &DAG) const {
8237-
bool IsVPTrunc = Op.getOpcode() == ISD::VP_TRUNCATE;
8244+
unsigned Opc = Op.getOpcode();
8245+
bool IsVPTrunc = Opc == ISD::VP_TRUNCATE;
82388246
SDLoc DL(Op);
82398247

82408248
MVT VT = Op.getSimpleValueType();
@@ -8279,11 +8287,18 @@ SDValue RISCVTargetLowering::lowerVectorTruncLike(SDValue Op,
82798287
getDefaultVLOps(SrcVT, ContainerVT, DL, DAG, Subtarget);
82808288
}
82818289

8290+
unsigned NewOpc;
8291+
if (Opc == ISD::TRUNCATE_SSAT_S)
8292+
NewOpc = RISCVISD::TRUNCATE_VECTOR_VL_SSAT;
8293+
else if (Opc == ISD::TRUNCATE_SSAT_U || Opc == ISD::TRUNCATE_USAT_U)
8294+
NewOpc = RISCVISD::TRUNCATE_VECTOR_VL_USAT;
8295+
else
8296+
NewOpc = RISCVISD::TRUNCATE_VECTOR_VL;
8297+
82828298
do {
82838299
SrcEltVT = MVT::getIntegerVT(SrcEltVT.getSizeInBits() / 2);
82848300
MVT ResultVT = ContainerVT.changeVectorElementType(SrcEltVT);
8285-
Result = DAG.getNode(RISCVISD::TRUNCATE_VECTOR_VL, DL, ResultVT, Result,
8286-
Mask, VL);
8301+
Result = DAG.getNode(NewOpc, DL, ResultVT, Result, Mask, VL);
82878302
} while (SrcEltVT != DstEltVT);
82888303

82898304
if (SrcVT.isFixedLengthVector())

0 commit comments

Comments
 (0)