Skip to content

Commit b611165

Browse files
committed
Split ReplaceNodeResults path out from LowerBITCAST
1 parent 6e16bb5 commit b611165

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,13 @@ VectorizePTXValueVTs(const SmallVectorImpl<EVT> &ValueVTs,
409409
return VectorInfo;
410410
}
411411

412+
static SDValue MaybeBitcast(SelectionDAG &DAG, SDLoc DL, EVT VT,
413+
SDValue Value) {
414+
if (Value->getValueType(0) == VT)
415+
return Value;
416+
return DAG.getNode(ISD::BITCAST, DL, VT, Value);
417+
}
418+
412419
// NVPTXTargetLowering Constructor.
413420
NVPTXTargetLowering::NVPTXTargetLowering(const NVPTXTargetMachine &TM,
414421
const NVPTXSubtarget &STI)
@@ -2316,30 +2323,14 @@ NVPTXTargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
23162323
}
23172324

23182325
SDValue NVPTXTargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
2319-
// Handle bitcasting to/from v2i8 without hitting the default promotion
2326+
// Handle bitcasting from v2i8 without hitting the default promotion
23202327
// strategy which goes through stack memory.
2321-
SDNode *Node = Op.getNode();
2322-
SDLoc DL(Node);
2323-
2324-
auto maybeBitcast = [&](EVT VT, SDValue Value) {
2325-
if (Value->getValueType(0) == VT)
2326-
return Value;
2327-
return DAG.getNode(ISD::BITCAST, DL, VT, Value);
2328-
};
2328+
SDLoc DL(Op);
23292329

23302330
EVT ToVT = Op->getValueType(0);
23312331
EVT FromVT = Op->getOperand(0)->getValueType(0);
23322332

2333-
if (ToVT == MVT::v2i8) {
2334-
// Bitcast to i16 and unpack elements into a vector
2335-
SDValue AsInt = maybeBitcast(MVT::i16, Op->getOperand(0));
2336-
SDValue Vec0 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, AsInt);
2337-
SDValue Const8 = DAG.getConstant(8, DL, MVT::i16);
2338-
SDValue Vec1 =
2339-
DAG.getNode(ISD::TRUNCATE, DL, MVT::i8,
2340-
DAG.getNode(ISD::SRL, DL, MVT::i16, {AsInt, Const8}));
2341-
return DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i8, {Vec0, Vec1});
2342-
} else if (FromVT == MVT::v2i8) {
2333+
if (FromVT == MVT::v2i8) {
23432334
// Pack vector elements into i16 and bitcast to final type
23442335
SDValue Vec0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i8,
23452336
Op->getOperand(0), DAG.getIntPtrConstant(0, DL));
@@ -2351,7 +2342,7 @@ SDValue NVPTXTargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
23512342
SDValue AsInt = DAG.getNode(
23522343
ISD::OR, DL, MVT::i16,
23532344
{Extend0, DAG.getNode(ISD::SHL, DL, MVT::i16, {Extend1, Const8})});
2354-
return maybeBitcast(ToVT, AsInt);
2345+
return MaybeBitcast(DAG, DL, ToVT, AsInt);
23552346
}
23562347
return Op;
23572348
}
@@ -6175,6 +6166,26 @@ SDValue NVPTXTargetLowering::PerformDAGCombine(SDNode *N,
61756166
return SDValue();
61766167
}
61776168

6169+
static void ReplaceBITCAST(SDNode *Node, SelectionDAG &DAG,
6170+
SmallVectorImpl<SDValue> &Results) {
6171+
// Handle bitcasting to v2i8 without hitting the default promotion
6172+
// strategy which goes through stack memory.
6173+
SDValue Op(Node, 0);
6174+
SDLoc DL(Node);
6175+
6176+
EVT ToVT = Op->getValueType(0);
6177+
if (ToVT == MVT::v2i8) {
6178+
SDValue AsInt = MaybeBitcast(DAG, DL, MVT::i16, Op->getOperand(0));
6179+
SDValue Vec0 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, AsInt);
6180+
SDValue Const8 = DAG.getConstant(8, DL, MVT::i16);
6181+
SDValue Vec1 =
6182+
DAG.getNode(ISD::TRUNCATE, DL, MVT::i8,
6183+
DAG.getNode(ISD::SRL, DL, MVT::i16, {AsInt, Const8}));
6184+
Results.push_back(
6185+
DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i8, {Vec0, Vec1}));
6186+
}
6187+
}
6188+
61786189
/// ReplaceVectorLoad - Convert vector loads into multi-output scalar loads.
61796190
static void ReplaceLoadVector(SDNode *N, SelectionDAG &DAG,
61806191
SmallVectorImpl<SDValue> &Results) {
@@ -6461,7 +6472,7 @@ void NVPTXTargetLowering::ReplaceNodeResults(
64616472
default:
64626473
report_fatal_error("Unhandled custom legalization");
64636474
case ISD::BITCAST:
6464-
Results.push_back(LowerBITCAST(SDValue(N, 0), DAG));
6475+
ReplaceBITCAST(N, DAG, Results);
64656476
return;
64666477
case ISD::LOAD:
64676478
ReplaceLoadVector(N, DAG, Results);

0 commit comments

Comments
 (0)