Skip to content

Commit 424f82c

Browse files
committed
[RISCV] Refactor combineTruncToVnclipu to prepare for adding signed vnclip support. NFC
Reviewed as part of #93728.
1 parent f4681be commit 424f82c

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16185,24 +16185,24 @@ static SDValue combineTruncOfSraSext(SDNode *N, SelectionDAG &DAG) {
1618516185

1618616186
// Combine (truncate_vector_vl (umin X, C)) -> (vnclipu_vl X) if C is maximum
1618716187
// value for the truncated type.
16188-
static SDValue combineTruncToVnclipu(SDNode *N, SelectionDAG &DAG,
16189-
const RISCVSubtarget &Subtarget) {
16188+
static SDValue combineTruncToVnclip(SDNode *N, SelectionDAG &DAG,
16189+
const RISCVSubtarget &Subtarget) {
1619016190
assert(N->getOpcode() == RISCVISD::TRUNCATE_VECTOR_VL);
1619116191

1619216192
MVT VT = N->getSimpleValueType(0);
1619316193

1619416194
SDValue Mask = N->getOperand(1);
1619516195
SDValue VL = N->getOperand(2);
1619616196

16197-
SDValue Src = N->getOperand(0);
16197+
auto MatchMinMax = [&VL, &Mask](SDValue V, unsigned Opc, unsigned OpcVL,
16198+
APInt &SplatVal) {
16199+
if (V.getOpcode() != Opc &&
16200+
!(V.getOpcode() == OpcVL && V.getOperand(2).isUndef() &&
16201+
V.getOperand(3) == Mask && V.getOperand(4) == VL))
16202+
return SDValue();
1619816203

16199-
// Src must be a UMIN or UMIN_VL.
16200-
if (Src.getOpcode() != ISD::UMIN &&
16201-
!(Src.getOpcode() == RISCVISD::UMIN_VL && Src.getOperand(2).isUndef() &&
16202-
Src.getOperand(3) == Mask && Src.getOperand(4) == VL))
16203-
return SDValue();
16204+
SDValue Op = V.getOperand(1);
1620416205

16205-
auto IsSplat = [&VL](SDValue Op, APInt &SplatVal) {
1620616206
// Peek through conversion between fixed and scalable vectors.
1620716207
if (Op.getOpcode() == ISD::INSERT_SUBVECTOR && Op.getOperand(0).isUndef() &&
1620816208
isNullConstant(Op.getOperand(2)) &&
@@ -16213,32 +16213,45 @@ static SDValue combineTruncToVnclipu(SDNode *N, SelectionDAG &DAG,
1621316213
Op = Op.getOperand(1).getOperand(0);
1621416214

1621516215
if (ISD::isConstantSplatVector(Op.getNode(), SplatVal))
16216-
return true;
16216+
return V.getOperand(0);
1621716217

1621816218
if (Op.getOpcode() == RISCVISD::VMV_V_X_VL && Op.getOperand(0).isUndef() &&
1621916219
Op.getOperand(2) == VL) {
1622016220
if (auto *Op1 = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
1622116221
SplatVal =
1622216222
Op1->getAPIntValue().sextOrTrunc(Op.getScalarValueSizeInBits());
16223-
return true;
16223+
return V.getOperand(0);
1622416224
}
1622516225
}
1622616226

16227-
return false;
16227+
return SDValue();
1622816228
};
1622916229

16230-
APInt C;
16231-
if (!IsSplat(Src.getOperand(1), C))
16232-
return SDValue();
16230+
auto DetectUSatPattern = [&](SDValue V) {
16231+
// Src must be a UMIN or UMIN_VL.
16232+
APInt C;
16233+
SDValue UMin = MatchMinMax(V, ISD::UMIN, RISCVISD::UMIN_VL, C);
16234+
if (!UMin)
16235+
return SDValue();
16236+
16237+
if (!C.isMask(VT.getScalarSizeInBits()))
16238+
return SDValue();
1623316239

16234-
if (!C.isMask(VT.getScalarSizeInBits()))
16240+
return UMin;
16241+
};
16242+
16243+
SDValue Val;
16244+
unsigned ClipOpc;
16245+
if ((Val = DetectUSatPattern(N->getOperand(0))))
16246+
ClipOpc = RISCVISD::VNCLIPU_VL;
16247+
else
1623516248
return SDValue();
1623616249

1623716250
SDLoc DL(N);
1623816251
// Rounding mode here is arbitrary since we aren't shifting out any bits.
1623916252
return DAG.getNode(
16240-
RISCVISD::VNCLIPU_VL, DL, VT,
16241-
{Src.getOperand(0), DAG.getConstant(0, DL, VT), DAG.getUNDEF(VT), Mask,
16253+
ClipOpc, DL, VT,
16254+
{Val, DAG.getConstant(0, DL, VT), DAG.getUNDEF(VT), Mask,
1624216255
DAG.getTargetConstant(RISCVVXRndMode::RNU, DL, Subtarget.getXLenVT()),
1624316256
VL});
1624416257
}
@@ -16462,7 +16475,7 @@ SDValue RISCVTargetLowering::PerformDAGCombine(SDNode *N,
1646216475
case RISCVISD::TRUNCATE_VECTOR_VL:
1646316476
if (SDValue V = combineTruncOfSraSext(N, DAG))
1646416477
return V;
16465-
return combineTruncToVnclipu(N, DAG, Subtarget);
16478+
return combineTruncToVnclip(N, DAG, Subtarget);
1646616479
case ISD::TRUNCATE:
1646716480
return performTRUNCATECombine(N, DAG, Subtarget);
1646816481
case ISD::SELECT:

0 commit comments

Comments
 (0)