Skip to content

Commit 0bab2ec

Browse files
author
Yeting Kuo
committed
[RISCV] Custom lower vector ISD::CTLZ to the minimum of ISD::CTLZ_ZERO_UNDEF and element size.
D111904 made RISC-V customized lower ISD::CTLZ_ZERO_UNDEF by converting to float and using the float result. The expected value of CTLZ with zero input is the element size of input type. Since the result of above method with zero input must be greater than the element size, for ISD::CTLZ, we could use the minimum of element size and the result of CTLZ_ZERO_UNDER with same input. Reviewed By: craig.topper Differential Revision: https://reviews.llvm.org/D141585
1 parent eb2db81 commit 0bab2ec

File tree

3 files changed

+304
-401
lines changed

3 files changed

+304
-401
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,9 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
681681
// of f32.
682682
EVT FloatVT = MVT::getVectorVT(MVT::f32, VT.getVectorElementCount());
683683
if (isTypeLegal(FloatVT)) {
684-
setOperationAction({ISD::CTLZ_ZERO_UNDEF, ISD::CTTZ_ZERO_UNDEF}, VT,
685-
Custom);
684+
setOperationAction(
685+
{ISD::CTLZ, ISD::CTLZ_ZERO_UNDEF, ISD::CTTZ_ZERO_UNDEF}, VT,
686+
Custom);
686687
}
687688
}
688689

@@ -912,8 +913,9 @@ RISCVTargetLowering::RISCVTargetLowering(const TargetMachine &TM,
912913
// range of f32.
913914
EVT FloatVT = MVT::getVectorVT(MVT::f32, VT.getVectorElementCount());
914915
if (isTypeLegal(FloatVT))
915-
setOperationAction({ISD::CTLZ_ZERO_UNDEF, ISD::CTTZ_ZERO_UNDEF}, VT,
916-
Custom);
916+
setOperationAction(
917+
{ISD::CTLZ, ISD::CTLZ_ZERO_UNDEF, ISD::CTTZ_ZERO_UNDEF}, VT,
918+
Custom);
917919
}
918920

919921
for (MVT VT : MVT::fp_fixedlen_vector_valuetypes()) {
@@ -3599,7 +3601,13 @@ RISCVTargetLowering::lowerCTLZ_CTTZ_ZERO_UNDEF(SDValue Op,
35993601
// For leading zeros, we need to remove the bias and convert from log2 to
36003602
// leading zeros. We can do this by subtracting from (Bias + (EltSize - 1)).
36013603
unsigned Adjust = ExponentBias + (EltSize - 1);
3602-
return DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(Adjust, DL, VT), Exp);
3604+
SDValue Res =
3605+
DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(Adjust, DL, VT), Exp);
3606+
// The above result with zero input equals to Adjust which is greater than
3607+
// EltSize. Hence, we can do min(Res, EltSize) for CTLZ.
3608+
if (Op.getOpcode() == ISD::CTLZ)
3609+
Res = DAG.getNode(ISD::UMIN, DL, VT, Res, DAG.getConstant(EltSize, DL, VT));
3610+
return Res;
36033611
}
36043612

36053613
// While RVV has alignment restrictions, we should always be able to load as a
@@ -4218,6 +4226,7 @@ SDValue RISCVTargetLowering::LowerOperation(SDValue Op,
42184226
case ISD::ABS:
42194227
case ISD::VP_ABS:
42204228
return lowerABS(Op, DAG);
4229+
case ISD::CTLZ:
42214230
case ISD::CTLZ_ZERO_UNDEF:
42224231
case ISD::CTTZ_ZERO_UNDEF:
42234232
return lowerCTLZ_CTTZ_ZERO_UNDEF(Op, DAG);

0 commit comments

Comments
 (0)