@@ -6285,6 +6285,7 @@ SDValue TargetLowering::buildSDIVPow2WithCMov(
62856285// / Ref: "Hacker's Delight" or "The PowerPC Compiler Writer's Guide".
62866286SDValue TargetLowering::BuildSDIV (SDNode *N, SelectionDAG &DAG,
62876287 bool IsAfterLegalization,
6288+ bool IsAfterLegalTypes,
62886289 SmallVectorImpl<SDNode *> &Created) const {
62896290 SDLoc dl (N);
62906291 EVT VT = N->getValueType (0 );
@@ -6405,7 +6406,12 @@ SDValue TargetLowering::BuildSDIV(SDNode *N, SelectionDAG &DAG,
64056406 if (VT.isVector ())
64066407 WideVT = EVT::getVectorVT (*DAG.getContext (), WideVT,
64076408 VT.getVectorElementCount ());
6408- if (isOperationLegalOrCustom (ISD::MUL, WideVT)) {
6409+ // Some targets like AMDGPU try to go from SDIV to SDIVREM which is then
6410+ // custom lowered. This is very expensive so avoid it at all costs for
6411+ // constant divisors.
6412+ if ((!IsAfterLegalTypes && isOperationExpand (ISD::SDIV, VT) &&
6413+ isOperationCustom (ISD::SDIVREM, VT.getScalarType ())) ||
6414+ isOperationLegalOrCustom (ISD::MUL, WideVT)) {
64096415 X = DAG.getNode (ISD::SIGN_EXTEND, dl, WideVT, X);
64106416 Y = DAG.getNode (ISD::SIGN_EXTEND, dl, WideVT, Y);
64116417 Y = DAG.getNode (ISD::MUL, dl, WideVT, X, Y);
@@ -6447,6 +6453,7 @@ SDValue TargetLowering::BuildSDIV(SDNode *N, SelectionDAG &DAG,
64476453// / Ref: "Hacker's Delight" or "The PowerPC Compiler Writer's Guide".
64486454SDValue TargetLowering::BuildUDIV (SDNode *N, SelectionDAG &DAG,
64496455 bool IsAfterLegalization,
6456+ bool IsAfterLegalTypes,
64506457 SmallVectorImpl<SDNode *> &Created) const {
64516458 SDLoc dl (N);
64526459 EVT VT = N->getValueType (0 );
@@ -6588,7 +6595,12 @@ SDValue TargetLowering::BuildUDIV(SDNode *N, SelectionDAG &DAG,
65886595 if (VT.isVector ())
65896596 WideVT = EVT::getVectorVT (*DAG.getContext (), WideVT,
65906597 VT.getVectorElementCount ());
6591- if (isOperationLegalOrCustom (ISD::MUL, WideVT)) {
6598+ // Some targets like AMDGPU try to go from UDIV to UDIVREM which is then
6599+ // custom lowered. This is very expensive so avoid it at all costs for
6600+ // constant divisors.
6601+ if ((!IsAfterLegalTypes && isOperationExpand (ISD::UDIV, VT) &&
6602+ isOperationCustom (ISD::UDIVREM, VT.getScalarType ())) ||
6603+ isOperationLegalOrCustom (ISD::MUL, WideVT)) {
65926604 X = DAG.getNode (ISD::ZERO_EXTEND, dl, WideVT, X);
65936605 Y = DAG.getNode (ISD::ZERO_EXTEND, dl, WideVT, Y);
65946606 Y = DAG.getNode (ISD::MUL, dl, WideVT, X, Y);
0 commit comments