Skip to content

Commit 7389545

Browse files
authored
Reapply "[AMDGPU] Always lower s/udiv64 by constant to MUL" (llvm#101942)
Reland llvm#100723, fixing the ARM issue at the cost of a small loss of optimization in `test/CodeGen/AMDGPU/fshr.ll` Solves llvm#100383
1 parent cb372bd commit 7389545

File tree

9 files changed

+1958
-1514
lines changed

9 files changed

+1958
-1514
lines changed

llvm/include/llvm/CodeGen/TargetLowering.h

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

50845084
SDValue BuildSDIV(SDNode *N, SelectionDAG &DAG, bool IsAfterLegalization,
5085+
bool IsAfterLegalTypes,
50855086
SmallVectorImpl<SDNode *> &Created) const;
50865087
SDValue BuildUDIV(SDNode *N, SelectionDAG &DAG, bool IsAfterLegalization,
5088+
bool IsAfterLegalTypes,
50875089
SmallVectorImpl<SDNode *> &Created) const;
50885090
// Build sdiv by power-of-2 with conditional move instructions
50895091
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
@@ -27800,7 +27800,7 @@ SDValue DAGCombiner::BuildSDIV(SDNode *N) {
2780027800
return SDValue();
2780127801

2780227802
SmallVector<SDNode *, 8> Built;
27803-
if (SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, Built)) {
27803+
if (SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, LegalTypes, Built)) {
2780427804
for (SDNode *N : Built)
2780527805
AddToWorklist(N);
2780627806
return S;
@@ -27841,7 +27841,7 @@ SDValue DAGCombiner::BuildUDIV(SDNode *N) {
2784127841
return SDValue();
2784227842

2784327843
SmallVector<SDNode *, 8> Built;
27844-
if (SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, Built)) {
27844+
if (SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, LegalTypes, Built)) {
2784527845
for (SDNode *N : Built)
2784627846
AddToWorklist(N);
2784727847
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)