Skip to content

Commit e1b6fce

Browse files
committed
Ported LowerBITCAST from NVPTXISelLowering.cpp to SelectionDAG/LegalizeTypes.cpp.
1 parent d486b76 commit e1b6fce

File tree

5 files changed

+42
-29
lines changed

5 files changed

+42
-29
lines changed

llvm/lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2174,8 +2174,13 @@ SDValue DAGTypeLegalizer::PromoteIntOp_ATOMIC_STORE(AtomicSDNode *N) {
21742174
}
21752175

21762176
SDValue DAGTypeLegalizer::PromoteIntOp_BITCAST(SDNode *N) {
2177-
// This should only occur in unusual situations like bitcasting to an
2178-
// x86_fp80, so just turn it into a store+load
2177+
2178+
// Use the custom lowering.
2179+
if (const auto Res = LowerBitcast(N)) {
2180+
return Res;
2181+
}
2182+
2183+
// If it fails fall back to the default method
21792184
return CreateStackStoreLoad(N->getOperand(0), N->getValueType(0));
21802185
}
21812186

llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,39 @@ SDValue DAGTypeLegalizer::CreateStackStoreLoad(SDValue Op,
910910
return DAG.getLoad(DestVT, dl, Store, StackPtr, MachinePointerInfo(), Align);
911911
}
912912

913+
static SDValue MaybeBitcast(SelectionDAG &DAG, SDLoc DL, EVT VT,
914+
SDValue Value) {
915+
if (Value->getValueType(0) == VT)
916+
return Value;
917+
return DAG.getNode(ISD::BITCAST, DL, VT, Value);
918+
}
919+
920+
SDValue DAGTypeLegalizer::LowerBitcast(SDNode *Node) const {
921+
assert(Node->getOpcode() == ISD::BITCAST ||
922+
Node->getOpcode() == ISD::FP_ROUND && "Unexpected opcode!");
923+
// Handle bitcasting from v2i8 without hitting the default promotion
924+
// strategy which goes through stack memory.
925+
EVT FromVT = Node->getOperand(0)->getValueType(0);
926+
if (FromVT != MVT::v2i8) {
927+
return SDValue();
928+
}
929+
930+
// Pack vector elements into i16 and bitcast to final type
931+
SDLoc DL(Node);
932+
SDValue Vec0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i8,
933+
Node->getOperand(0), DAG.getIntPtrConstant(0, DL));
934+
SDValue Vec1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i8,
935+
Node->getOperand(0), DAG.getIntPtrConstant(1, DL));
936+
SDValue Extend0 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i16, Vec0);
937+
SDValue Extend1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i16, Vec1);
938+
SDValue Const8 = DAG.getConstant(8, DL, MVT::i16);
939+
SDValue AsInt = DAG.getNode(
940+
ISD::OR, DL, MVT::i16,
941+
{Extend0, DAG.getNode(ISD::SHL, DL, MVT::i16, {Extend1, Const8})});
942+
EVT ToVT = Node->getValueType(0);
943+
return MaybeBitcast(DAG, DL, ToVT, AsInt);
944+
}
945+
913946
/// Replace the node's results with custom code provided by the target and
914947
/// return "true", or do nothing and return "false".
915948
/// The last parameter is FALSE if we are dealing with a node with legal

llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ class LLVM_LIBRARY_VISIBILITY DAGTypeLegalizer {
216216
SDValue BitConvertToInteger(SDValue Op);
217217
SDValue BitConvertVectorToIntegerVector(SDValue Op);
218218
SDValue CreateStackStoreLoad(SDValue Op, EVT DestVT);
219+
SDValue LowerBitcast(SDNode *N) const;
219220
bool CustomLowerNode(SDNode *N, EVT VT, bool LegalizeResult);
220221
bool CustomWidenLowerNode(SDNode *N, EVT VT);
221222

llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,30 +2086,6 @@ NVPTXTargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
20862086
return DAG.getBuildVector(Node->getValueType(0), dl, Ops);
20872087
}
20882088

2089-
SDValue NVPTXTargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
2090-
// Handle bitcasting from v2i8 without hitting the default promotion
2091-
// strategy which goes through stack memory.
2092-
EVT FromVT = Op->getOperand(0)->getValueType(0);
2093-
if (FromVT != MVT::v2i8) {
2094-
return Op;
2095-
}
2096-
2097-
// Pack vector elements into i16 and bitcast to final type
2098-
SDLoc DL(Op);
2099-
SDValue Vec0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i8,
2100-
Op->getOperand(0), DAG.getIntPtrConstant(0, DL));
2101-
SDValue Vec1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i8,
2102-
Op->getOperand(0), DAG.getIntPtrConstant(1, DL));
2103-
SDValue Extend0 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i16, Vec0);
2104-
SDValue Extend1 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i16, Vec1);
2105-
SDValue Const8 = DAG.getConstant(8, DL, MVT::i16);
2106-
SDValue AsInt = DAG.getNode(
2107-
ISD::OR, DL, MVT::i16,
2108-
{Extend0, DAG.getNode(ISD::SHL, DL, MVT::i16, {Extend1, Const8})});
2109-
EVT ToVT = Op->getValueType(0);
2110-
return MaybeBitcast(DAG, DL, ToVT, AsInt);
2111-
}
2112-
21132089
// We can init constant f16x2/v2i16/v4i8 with a single .b32 move. Normally it
21142090
// would get lowered as two constant loads and vector-packing move.
21152091
// Instead we want just a constant move:
@@ -2619,7 +2595,7 @@ NVPTXTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
26192595
case ISD::BUILD_VECTOR:
26202596
return LowerBUILD_VECTOR(Op, DAG);
26212597
case ISD::BITCAST:
2622-
return LowerBITCAST(Op, DAG);
2598+
return SDValue();
26232599
case ISD::EXTRACT_SUBVECTOR:
26242600
return Op;
26252601
case ISD::EXTRACT_VECTOR_ELT:

llvm/lib/Target/NVPTX/NVPTXISelLowering.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,6 @@ class NVPTXTargetLowering : public TargetLowering {
265265
const NVPTXSubtarget &STI; // cache the subtarget here
266266
SDValue getParamSymbol(SelectionDAG &DAG, int idx, EVT) const;
267267

268-
SDValue LowerBITCAST(SDValue Op, SelectionDAG &DAG) const;
269-
270268
SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const;
271269
SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const;
272270
SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const;

0 commit comments

Comments
 (0)