Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 98bb3fb

Browse files
committed
[TargetLowering] Remove APInt divisor argument from BuildExactSDIV (NFCI).
As requested in D50392, this is a minor refactor to BuildExactSDIV to stop taking the uniform constant APInt divisor and instead extract it locally. I also cleanup the operands and valuetypes to better match BuildUDiv (and BuildSDIV in the near future). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@339246 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent f925727 commit 98bb3fb

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3432,22 +3432,32 @@ void TargetLowering::ComputeConstraintToUse(AsmOperandInfo &OpInfo,
34323432

34333433
/// Given an exact SDIV by a constant, create a multiplication
34343434
/// with the multiplicative inverse of the constant.
3435-
static SDValue BuildExactSDIV(const TargetLowering &TLI, SDValue Op1, APInt d,
3435+
static SDValue BuildExactSDIV(const TargetLowering &TLI, SDNode *N,
34363436
const SDLoc &dl, SelectionDAG &DAG,
34373437
SmallVectorImpl<SDNode *> &Created) {
3438+
SDValue Op0 = N->getOperand(0);
3439+
SDValue Op1 = N->getOperand(1);
3440+
EVT VT = N->getValueType(0);
3441+
EVT ShVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
3442+
3443+
ConstantSDNode *C = isConstOrConstSplat(Op1);
3444+
if (!C || C->isNullValue())
3445+
return SDValue();
3446+
3447+
APInt d = C->getAPIntValue();
34383448
assert(d != 0 && "Division by zero!");
34393449

3450+
SDValue Res = Op0;
3451+
34403452
// Shift the value upfront if it is even, so the LSB is one.
34413453
unsigned ShAmt = d.countTrailingZeros();
34423454
if (ShAmt) {
34433455
// TODO: For UDIV use SRL instead of SRA.
3444-
SDValue Amt =
3445-
DAG.getConstant(ShAmt, dl, TLI.getShiftAmountTy(Op1.getValueType(),
3446-
DAG.getDataLayout()));
3456+
SDValue Amt = DAG.getConstant(ShAmt, dl, ShVT);
34473457
SDNodeFlags Flags;
34483458
Flags.setExact(true);
3449-
Op1 = DAG.getNode(ISD::SRA, dl, Op1.getValueType(), Op1, Amt, Flags);
3450-
Created.push_back(Op1.getNode());
3459+
Res = DAG.getNode(ISD::SRA, dl, VT, Res, Amt, Flags);
3460+
Created.push_back(Res.getNode());
34513461
d.ashrInPlace(ShAmt);
34523462
}
34533463

@@ -3456,10 +3466,8 @@ static SDValue BuildExactSDIV(const TargetLowering &TLI, SDValue Op1, APInt d,
34563466
while ((t = d*xn) != 1)
34573467
xn *= APInt(d.getBitWidth(), 2) - t;
34583468

3459-
SDValue Op2 = DAG.getConstant(xn, dl, Op1.getValueType());
3460-
SDValue Mul = DAG.getNode(ISD::MUL, dl, Op1.getValueType(), Op1, Op2);
3461-
Created.push_back(Mul.getNode());
3462-
return Mul;
3469+
SDValue Mul = DAG.getConstant(xn, dl, VT);
3470+
return DAG.getNode(ISD::MUL, dl, VT, Res, Mul);
34633471
}
34643472

34653473
SDValue TargetLowering::BuildSDIVPow2(SDNode *N, const APInt &Divisor,
@@ -3487,16 +3495,16 @@ SDValue TargetLowering::BuildSDIV(SDNode *N, SelectionDAG &DAG,
34873495
if (!isTypeLegal(VT))
34883496
return SDValue();
34893497

3498+
// If the sdiv has an 'exact' bit we can use a simpler lowering.
3499+
if (N->getFlags().hasExact())
3500+
return BuildExactSDIV(*this, N, dl, DAG, Created);
3501+
34903502
// TODO: Add non-uniform constant support.
34913503
ConstantSDNode *C = isConstOrConstSplat(N->getOperand(1));
34923504
if (!C || C->isNullValue())
34933505
return SDValue();
34943506
const APInt &Divisor = C->getAPIntValue();
34953507

3496-
// If the sdiv has an 'exact' bit we can use a simpler lowering.
3497-
if (N->getFlags().hasExact())
3498-
return BuildExactSDIV(*this, N->getOperand(0), Divisor, dl, DAG, Created);
3499-
35003508
APInt::ms magics = Divisor.magic();
35013509

35023510
// Multiply the numerator (operand 0) by the magic value

0 commit comments

Comments
 (0)