Skip to content

Commit 73ef27c

Browse files
[DAGCombiner] Handle type-promoted constants in UDIV exact lowering (#169949)
Builds up on the solution proposed for #169491 and applies it for UDIV exact as well.
1 parent 0e11a92 commit 73ef27c

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6403,7 +6403,6 @@ static SDValue BuildExactUDIV(const TargetLowering &TLI, SDNode *N,
64036403
const SDLoc &dl, SelectionDAG &DAG,
64046404
SmallVectorImpl<SDNode *> &Created) {
64056405
EVT VT = N->getValueType(0);
6406-
EVT SVT = VT.getScalarType();
64076406
EVT ShVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
64086407
EVT ShSVT = ShVT.getScalarType();
64096408

@@ -6413,6 +6412,8 @@ static SDValue BuildExactUDIV(const TargetLowering &TLI, SDNode *N,
64136412
auto BuildUDIVPattern = [&](ConstantSDNode *C) {
64146413
if (C->isZero())
64156414
return false;
6415+
6416+
EVT CT = C->getValueType(0);
64166417
APInt Divisor = C->getAPIntValue();
64176418
unsigned Shift = Divisor.countr_zero();
64186419
if (Shift) {
@@ -6422,14 +6423,15 @@ static SDValue BuildExactUDIV(const TargetLowering &TLI, SDNode *N,
64226423
// Calculate the multiplicative inverse modulo BW.
64236424
APInt Factor = Divisor.multiplicativeInverse();
64246425
Shifts.push_back(DAG.getConstant(Shift, dl, ShSVT));
6425-
Factors.push_back(DAG.getConstant(Factor, dl, SVT));
6426+
Factors.push_back(DAG.getConstant(Factor, dl, CT));
64266427
return true;
64276428
};
64286429

64296430
SDValue Op1 = N->getOperand(1);
64306431

64316432
// Collect all magic values from the build vector.
6432-
if (!ISD::matchUnaryPredicate(Op1, BuildUDIVPattern))
6433+
if (!ISD::matchUnaryPredicate(Op1, BuildUDIVPattern, /*AllowUndefs=*/false,
6434+
/*AllowTruncation=*/true))
64336435
return SDValue();
64346436

64356437
SDValue Shift, Factor;

llvm/test/CodeGen/AArch64/udiv-by-const-promoted-ops.ll

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,24 @@ define <16 x i16> @urem_v16i16_by_255(<16 x i16> %x) {
7676
%rem = urem <16 x i16> %x, splat (i16 255)
7777
ret <16 x i16> %rem
7878
}
79+
80+
define <8 x i16> @udiv_exact_v8i16_by_255(<8 x i16> %x) {
81+
; CHECK-LABEL: udiv_exact_v8i16_by_255:
82+
; CHECK: // %bb.0:
83+
; CHECK-NEXT: mvni v1.8h, #1, lsl #8
84+
; CHECK-NEXT: mul v0.8h, v0.8h, v1.8h
85+
; CHECK-NEXT: ret
86+
%div = udiv exact <8 x i16> %x, splat (i16 255)
87+
ret <8 x i16> %div
88+
}
89+
90+
define <16 x i16> @udiv_exact_v16i16_by_255(<16 x i16> %x) {
91+
; CHECK-LABEL: udiv_exact_v16i16_by_255:
92+
; CHECK: // %bb.0:
93+
; CHECK-NEXT: mvni v2.8h, #1, lsl #8
94+
; CHECK-NEXT: mul v0.8h, v0.8h, v2.8h
95+
; CHECK-NEXT: mul v1.8h, v1.8h, v2.8h
96+
; CHECK-NEXT: ret
97+
%div = udiv exact <16 x i16> %x, splat (i16 255)
98+
ret <16 x i16> %div
99+
}

0 commit comments

Comments
 (0)