Skip to content

Commit 04d4d45

Browse files
committed
[SelectionDAG] Move UREM Decomposition to Own Function; NFC
1 parent 5c02eca commit 04d4d45

File tree

1 file changed

+46
-33
lines changed

1 file changed

+46
-33
lines changed

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8011,25 +8011,12 @@ bool TargetLowering::expandMUL(SDNode *N, SDValue &Lo, SDValue &Hi, EVT HiLoVT,
80118011
// dividend and multiply by the multiplicative inverse of the shifted divisor.
80128012
// If we want the remainder, we shift the value left by the number of trailing
80138013
// zeros and add the bits that were shifted out of the dividend.
8014-
bool TargetLowering::expandDIVREMByConstant(SDNode *N,
8015-
SmallVectorImpl<SDValue> &Result,
8016-
EVT HiLoVT, SelectionDAG &DAG,
8017-
SDValue LL, SDValue LH) const {
8014+
static bool expandUDIVREMByConstantViaUREMDecomposition(
8015+
SDNode *N, APInt Divisor, SmallVectorImpl<SDValue> &Result, EVT HiLoVT,
8016+
SelectionDAG &DAG, SDValue LL, SDValue LH, const TargetLowering &TLI) {
80188017
unsigned Opcode = N->getOpcode();
80198018
EVT VT = N->getValueType(0);
80208019

8021-
// TODO: Support signed division/remainder.
8022-
if (Opcode == ISD::SREM || Opcode == ISD::SDIV || Opcode == ISD::SDIVREM)
8023-
return false;
8024-
assert(
8025-
(Opcode == ISD::UREM || Opcode == ISD::UDIV || Opcode == ISD::UDIVREM) &&
8026-
"Unexpected opcode");
8027-
8028-
auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(1));
8029-
if (!CN)
8030-
return false;
8031-
8032-
APInt Divisor = CN->getAPIntValue();
80338020
unsigned BitWidth = Divisor.getBitWidth();
80348021
unsigned HBitWidth = BitWidth / 2;
80358022
assert(VT.getScalarSizeInBits() == BitWidth &&
@@ -8040,20 +8027,6 @@ bool TargetLowering::expandDIVREMByConstant(SDNode *N,
80408027
if (Divisor.uge(HalfMaxPlus1))
80418028
return false;
80428029

8043-
// We depend on the UREM by constant optimization in DAGCombiner that requires
8044-
// high multiply.
8045-
if (!isOperationLegalOrCustom(ISD::MULHU, HiLoVT) &&
8046-
!isOperationLegalOrCustom(ISD::UMUL_LOHI, HiLoVT))
8047-
return false;
8048-
8049-
// Don't expand if optimizing for size.
8050-
if (DAG.shouldOptForSize())
8051-
return false;
8052-
8053-
// Early out for 0 or 1 divisors.
8054-
if (Divisor.ule(1))
8055-
return false;
8056-
80578030
// If the divisor is even, shift it until it becomes odd.
80588031
unsigned TrailingZeros = 0;
80598032
if (!Divisor[0]) {
@@ -8097,8 +8070,8 @@ bool TargetLowering::expandDIVREMByConstant(SDNode *N,
80978070

80988071
// Use uaddo_carry if we can, otherwise use a compare to detect overflow.
80998072
EVT SetCCType =
8100-
getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), HiLoVT);
8101-
if (isOperationLegalOrCustom(ISD::UADDO_CARRY, HiLoVT)) {
8073+
TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), HiLoVT);
8074+
if (TLI.isOperationLegalOrCustom(ISD::UADDO_CARRY, HiLoVT)) {
81028075
SDVTList VTList = DAG.getVTList(HiLoVT, SetCCType);
81038076
Sum = DAG.getNode(ISD::UADDO, dl, VTList, LL, LH);
81048077
Sum = DAG.getNode(ISD::UADDO_CARRY, dl, VTList, Sum,
@@ -8108,7 +8081,7 @@ bool TargetLowering::expandDIVREMByConstant(SDNode *N,
81088081
SDValue Carry = DAG.getSetCC(dl, SetCCType, Sum, LL, ISD::SETULT);
81098082
// If the boolean for the target is 0 or 1, we can add the setcc result
81108083
// directly.
8111-
if (getBooleanContents(HiLoVT) ==
8084+
if (TLI.getBooleanContents(HiLoVT) ==
81128085
TargetLoweringBase::ZeroOrOneBooleanContent)
81138086
Carry = DAG.getZExtOrTrunc(Carry, dl, HiLoVT);
81148087
else
@@ -8164,6 +8137,46 @@ bool TargetLowering::expandDIVREMByConstant(SDNode *N,
81648137
return true;
81658138
}
81668139

8140+
bool TargetLowering::expandDIVREMByConstant(SDNode *N,
8141+
SmallVectorImpl<SDValue> &Result,
8142+
EVT HiLoVT, SelectionDAG &DAG,
8143+
SDValue LL, SDValue LH) const {
8144+
unsigned Opcode = N->getOpcode();
8145+
8146+
// TODO: Support signed division/remainder.
8147+
if (Opcode == ISD::SREM || Opcode == ISD::SDIV || Opcode == ISD::SDIVREM)
8148+
return false;
8149+
assert(
8150+
(Opcode == ISD::UREM || Opcode == ISD::UDIV || Opcode == ISD::UDIVREM) &&
8151+
"Unexpected opcode");
8152+
8153+
auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(1));
8154+
if (!CN)
8155+
return false;
8156+
8157+
APInt Divisor = CN->getAPIntValue();
8158+
8159+
// We depend on the UREM by constant optimization in DAGCombiner that requires
8160+
// high multiply.
8161+
if (!isOperationLegalOrCustom(ISD::MULHU, HiLoVT) &&
8162+
!isOperationLegalOrCustom(ISD::UMUL_LOHI, HiLoVT))
8163+
return false;
8164+
8165+
// Don't expand if optimizing for size.
8166+
if (DAG.shouldOptForSize())
8167+
return false;
8168+
8169+
// Early out for 0 or 1 divisors.
8170+
if (Divisor.ule(1))
8171+
return false;
8172+
8173+
if (expandUDIVREMByConstantViaUREMDecomposition(N, Divisor, Result, HiLoVT,
8174+
DAG, LL, LH, *this))
8175+
return true;
8176+
8177+
return false;
8178+
}
8179+
81678180
// Check that (every element of) Z is undef or not an exact multiple of BW.
81688181
static bool isNonZeroModBitWidthOrUndef(SDValue Z, unsigned BW) {
81698182
return ISD::matchUnaryPredicate(

0 commit comments

Comments
 (0)