Skip to content

Commit 18c7bf0

Browse files
committed
[RISCV] Refactor selectVSplat. NFCI
This patch shares the logic between the various splat ComplexPatterns to help the diff in some upcoming patches. It's worth noting that the uimm splat pattern now takes into account the implicit truncation + sign extend semantics of vmv_v_x_vl, but that doesn't seem to affect the result since it always took the sext value anyway. Reviewed By: craig.topper Differential Revision: https://reviews.llvm.org/D158741
1 parent 998c323 commit 18c7bf0

File tree

1 file changed

+33
-39
lines changed

1 file changed

+33
-39
lines changed

llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2955,27 +2955,35 @@ bool RISCVDAGToDAGISel::selectVLOp(SDValue N, SDValue &VL) {
29552955
return true;
29562956
}
29572957

2958+
static SDValue findVSplat(SDValue N) {
2959+
SDValue Splat = N;
2960+
if (Splat.getOpcode() != RISCVISD::VMV_V_X_VL ||
2961+
!Splat.getOperand(0).isUndef())
2962+
return SDValue();
2963+
assert(Splat.getNumOperands() == 3 && "Unexpected number of operands");
2964+
return Splat;
2965+
}
2966+
29582967
bool RISCVDAGToDAGISel::selectVSplat(SDValue N, SDValue &SplatVal) {
2959-
if (N.getOpcode() != RISCVISD::VMV_V_X_VL || !N.getOperand(0).isUndef())
2968+
SDValue Splat = findVSplat(N);
2969+
if (!Splat)
29602970
return false;
2961-
assert(N.getNumOperands() == 3 && "Unexpected number of operands");
2962-
SplatVal = N.getOperand(1);
2971+
2972+
SplatVal = Splat.getOperand(1);
29632973
return true;
29642974
}
29652975

2966-
using ValidateFn = bool (*)(int64_t);
2967-
2968-
static bool selectVSplatSimmHelper(SDValue N, SDValue &SplatVal,
2969-
SelectionDAG &DAG,
2970-
const RISCVSubtarget &Subtarget,
2971-
ValidateFn ValidateImm) {
2972-
if (N.getOpcode() != RISCVISD::VMV_V_X_VL || !N.getOperand(0).isUndef() ||
2973-
!isa<ConstantSDNode>(N.getOperand(1)))
2976+
static bool selectVSplatImmHelper(SDValue N, SDValue &SplatVal,
2977+
SelectionDAG &DAG,
2978+
const RISCVSubtarget &Subtarget,
2979+
std::function<bool(int64_t)> ValidateImm) {
2980+
SDValue Splat = findVSplat(N);
2981+
if (!Splat || !isa<ConstantSDNode>(Splat.getOperand(1)))
29742982
return false;
2975-
assert(N.getNumOperands() == 3 && "Unexpected number of operands");
29762983

2977-
int64_t SplatImm =
2978-
cast<ConstantSDNode>(N.getOperand(1))->getSExtValue();
2984+
const unsigned SplatEltSize = Splat.getScalarValueSizeInBits();
2985+
assert(Subtarget.getXLenVT() == Splat.getOperand(1).getSimpleValueType() &&
2986+
"Unexpected splat operand type");
29792987

29802988
// The semantics of RISCVISD::VMV_V_X_VL is that when the operand
29812989
// type is wider than the resulting vector element type: an implicit
@@ -2984,55 +2992,41 @@ static bool selectVSplatSimmHelper(SDValue N, SDValue &SplatVal,
29842992
// any zero-extended immediate.
29852993
// For example, we wish to match (i8 -1) -> (XLenVT 255) as a simm5 by first
29862994
// sign-extending to (XLenVT -1).
2987-
MVT XLenVT = Subtarget.getXLenVT();
2988-
assert(XLenVT == N.getOperand(1).getSimpleValueType() &&
2989-
"Unexpected splat operand type");
2990-
MVT EltVT = N.getSimpleValueType().getVectorElementType();
2991-
if (EltVT.bitsLT(XLenVT))
2992-
SplatImm = SignExtend64(SplatImm, EltVT.getSizeInBits());
2995+
APInt SplatConst = Splat.getConstantOperandAPInt(1).sextOrTrunc(SplatEltSize);
2996+
2997+
int64_t SplatImm = SplatConst.getSExtValue();
29932998

29942999
if (!ValidateImm(SplatImm))
29953000
return false;
29963001

2997-
SplatVal = DAG.getTargetConstant(SplatImm, SDLoc(N), XLenVT);
3002+
SplatVal = DAG.getTargetConstant(SplatImm, SDLoc(N), Subtarget.getXLenVT());
29983003
return true;
29993004
}
30003005

30013006
bool RISCVDAGToDAGISel::selectVSplatSimm5(SDValue N, SDValue &SplatVal) {
3002-
return selectVSplatSimmHelper(N, SplatVal, *CurDAG, *Subtarget,
3003-
[](int64_t Imm) { return isInt<5>(Imm); });
3007+
return selectVSplatImmHelper(N, SplatVal, *CurDAG, *Subtarget,
3008+
[](int64_t Imm) { return isInt<5>(Imm); });
30043009
}
30053010

30063011
bool RISCVDAGToDAGISel::selectVSplatSimm5Plus1(SDValue N, SDValue &SplatVal) {
3007-
return selectVSplatSimmHelper(
3012+
return selectVSplatImmHelper(
30083013
N, SplatVal, *CurDAG, *Subtarget,
30093014
[](int64_t Imm) { return (isInt<5>(Imm) && Imm != -16) || Imm == 16; });
30103015
}
30113016

30123017
bool RISCVDAGToDAGISel::selectVSplatSimm5Plus1NonZero(SDValue N,
30133018
SDValue &SplatVal) {
3014-
return selectVSplatSimmHelper(
3019+
return selectVSplatImmHelper(
30153020
N, SplatVal, *CurDAG, *Subtarget, [](int64_t Imm) {
30163021
return Imm != 0 && ((isInt<5>(Imm) && Imm != -16) || Imm == 16);
30173022
});
30183023
}
30193024

30203025
bool RISCVDAGToDAGISel::selectVSplatUimm(SDValue N, unsigned Bits,
30213026
SDValue &SplatVal) {
3022-
if (N.getOpcode() != RISCVISD::VMV_V_X_VL || !N.getOperand(0).isUndef() ||
3023-
!isa<ConstantSDNode>(N.getOperand(1)))
3024-
return false;
3025-
3026-
int64_t SplatImm =
3027-
cast<ConstantSDNode>(N.getOperand(1))->getSExtValue();
3028-
3029-
if (!isUIntN(Bits, SplatImm))
3030-
return false;
3031-
3032-
SplatVal =
3033-
CurDAG->getTargetConstant(SplatImm, SDLoc(N), Subtarget->getXLenVT());
3034-
3035-
return true;
3027+
return selectVSplatImmHelper(
3028+
N, SplatVal, *CurDAG, *Subtarget,
3029+
[Bits](int64_t Imm) { return isUIntN(Bits, Imm); });
30363030
}
30373031

30383032
bool RISCVDAGToDAGISel::selectLow8BitsVSplat(SDValue N, SDValue &SplatVal) {

0 commit comments

Comments
 (0)