@@ -6285,6 +6285,7 @@ SDValue TargetLowering::buildSDIVPow2WithCMov(
6285
6285
/// Ref: "Hacker's Delight" or "The PowerPC Compiler Writer's Guide".
6286
6286
SDValue TargetLowering::BuildSDIV(SDNode *N, SelectionDAG &DAG,
6287
6287
bool IsAfterLegalization,
6288
+ bool IsAfterLegalTypes,
6288
6289
SmallVectorImpl<SDNode *> &Created) const {
6289
6290
SDLoc dl(N);
6290
6291
EVT VT = N->getValueType(0);
@@ -6405,7 +6406,12 @@ SDValue TargetLowering::BuildSDIV(SDNode *N, SelectionDAG &DAG,
6405
6406
if (VT.isVector())
6406
6407
WideVT = EVT::getVectorVT(*DAG.getContext(), WideVT,
6407
6408
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)) {
6409
6415
X = DAG.getNode(ISD::SIGN_EXTEND, dl, WideVT, X);
6410
6416
Y = DAG.getNode(ISD::SIGN_EXTEND, dl, WideVT, Y);
6411
6417
Y = DAG.getNode(ISD::MUL, dl, WideVT, X, Y);
@@ -6447,6 +6453,7 @@ SDValue TargetLowering::BuildSDIV(SDNode *N, SelectionDAG &DAG,
6447
6453
/// Ref: "Hacker's Delight" or "The PowerPC Compiler Writer's Guide".
6448
6454
SDValue TargetLowering::BuildUDIV(SDNode *N, SelectionDAG &DAG,
6449
6455
bool IsAfterLegalization,
6456
+ bool IsAfterLegalTypes,
6450
6457
SmallVectorImpl<SDNode *> &Created) const {
6451
6458
SDLoc dl(N);
6452
6459
EVT VT = N->getValueType(0);
@@ -6588,7 +6595,12 @@ SDValue TargetLowering::BuildUDIV(SDNode *N, SelectionDAG &DAG,
6588
6595
if (VT.isVector())
6589
6596
WideVT = EVT::getVectorVT(*DAG.getContext(), WideVT,
6590
6597
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)) {
6592
6604
X = DAG.getNode(ISD::ZERO_EXTEND, dl, WideVT, X);
6593
6605
Y = DAG.getNode(ISD::ZERO_EXTEND, dl, WideVT, Y);
6594
6606
Y = DAG.getNode(ISD::MUL, dl, WideVT, X, Y);
0 commit comments