Skip to content

Commit 7ffc5d8

Browse files
committed
Reapply "[AMDGPU] Always lower s/udiv64 by constant to MUL"
Reland #100723, fixing the ARM issue at the cost of a small regression in AMDGPU. Solves #100383
1 parent 4cfbd49 commit 7ffc5d8

File tree

9 files changed

+1962
-1514
lines changed

9 files changed

+1962
-1514
lines changed

llvm/include/llvm/CodeGen/TargetLowering.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5078,8 +5078,10 @@ class TargetLowering : public TargetLoweringBase {
50785078
//
50795079

50805080
SDValue BuildSDIV(SDNode *N, SelectionDAG &DAG, bool IsAfterLegalization,
5081+
bool IsAfterLegalTypes,
50815082
SmallVectorImpl<SDNode *> &Created) const;
50825083
SDValue BuildUDIV(SDNode *N, SelectionDAG &DAG, bool IsAfterLegalization,
5084+
bool IsAfterLegalTypes,
50835085
SmallVectorImpl<SDNode *> &Created) const;
50845086
// Build sdiv by power-of-2 with conditional move instructions
50855087
SDValue buildSDIVPow2WithCMov(SDNode *N, const APInt &Divisor,

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27805,7 +27805,7 @@ SDValue DAGCombiner::BuildSDIV(SDNode *N) {
2780527805
return SDValue();
2780627806

2780727807
SmallVector<SDNode *, 8> Built;
27808-
if (SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, Built)) {
27808+
if (SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, LegalTypes, Built)) {
2780927809
for (SDNode *N : Built)
2781027810
AddToWorklist(N);
2781127811
return S;
@@ -27846,7 +27846,7 @@ SDValue DAGCombiner::BuildUDIV(SDNode *N) {
2784627846
return SDValue();
2784727847

2784827848
SmallVector<SDNode *, 8> Built;
27849-
if (SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, Built)) {
27849+
if (SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, LegalTypes, Built)) {
2785027850
for (SDNode *N : Built)
2785127851
AddToWorklist(N);
2785227852
return S;

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6285,6 +6285,7 @@ SDValue TargetLowering::buildSDIVPow2WithCMov(
62856285
/// Ref: "Hacker's Delight" or "The PowerPC Compiler Writer's Guide".
62866286
SDValue 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".
64486454
SDValue 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

Comments
 (0)