@@ -6483,15 +6483,7 @@ SDValue TargetLowering::BuildUDIV(SDNode *N, SelectionDAG &DAG,
6483
6483
6484
6484
// Try to use leading zeros of the dividend to reduce the multiplier and
6485
6485
// avoid expensive fixups.
6486
- // TODO: Support vectors.
6487
- unsigned LeadingZeros = 0 ;
6488
- if (!VT.isVector () && isa<ConstantSDNode>(N1)) {
6489
- assert (!isOneConstant (N1) && " Unexpected divisor" );
6490
- LeadingZeros = DAG.computeKnownBits (N0).countMinLeadingZeros ();
6491
- // UnsignedDivisionByConstantInfo doesn't work correctly if leading zeros in
6492
- // the dividend exceeds the leading zeros for the divisor.
6493
- LeadingZeros = std::min (LeadingZeros, N1->getAsAPIntVal ().countl_zero ());
6494
- }
6486
+ unsigned KnownLeadingZeros = DAG.computeKnownBits (N0).countMinLeadingZeros ();
6495
6487
6496
6488
bool UseNPQ = false , UsePreShift = false , UsePostShift = false ;
6497
6489
SmallVector<SDValue, 16 > PreShifts, PostShifts, MagicFactors, NPQFactors;
@@ -6509,8 +6501,16 @@ SDValue TargetLowering::BuildUDIV(SDNode *N, SelectionDAG &DAG,
6509
6501
PreShift = PostShift = DAG.getUNDEF (ShSVT);
6510
6502
MagicFactor = NPQFactor = DAG.getUNDEF (SVT);
6511
6503
} else {
6504
+
6505
+ // Try to use leading zeros of the dividend to reduce the multiplier and
6506
+ // avoid expensive fixups.
6507
+ unsigned LeadingZeros = DAG.computeKnownBits (N0).countMinLeadingZeros ();
6508
+ // UnsignedDivisionByConstantInfo doesn't work correctly if leading zeros
6509
+ // in the dividend exceeds the leading zeros for the divisor.
6510
+ LeadingZeros = std::min (LeadingZeros, N1->getAsAPIntVal ().countl_zero ());
6512
6511
UnsignedDivisionByConstantInfo magics =
6513
- UnsignedDivisionByConstantInfo::get (Divisor, LeadingZeros);
6512
+ UnsignedDivisionByConstantInfo::get (
6513
+ Divisor, std::min (KnownLeadingZeros, Divisor.countl_zero ()));
6514
6514
6515
6515
MagicFactor = DAG.getConstant (magics.Magic , dl, SVT);
6516
6516
0 commit comments