Skip to content

Commit daae3e8

Browse files
committed
[GlobalISel]: Simplify udiv lowering by determining known zeros
1 parent 231beff commit daae3e8

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5069,6 +5069,14 @@ MachineInstr *CombinerHelper::buildUDivUsingMul(MachineInstr &MI) {
50695069
const unsigned EltBits = ScalarTy.getScalarSizeInBits();
50705070
LLT ShiftAmtTy = getTargetLowering().getPreferredShiftAmountTy(Ty);
50715071
LLT ScalarShiftAmtTy = ShiftAmtTy.getScalarType();
5072+
5073+
// UnsignedDivisionByConstantInfo doesn't work correctly if leading zeros in
5074+
// the dividend exceeds the leading zeros for the divisor.
5075+
unsigned KnownLeadingZeros =
5076+
KB ? std::min(KB->getKnownBits(LHS).countMinLeadingZeros(),
5077+
KB->getKnownBits(RHS).countMinLeadingZeros())
5078+
: 0;
5079+
50725080
auto &MIB = Builder;
50735081

50745082
bool UseNPQ = false;
@@ -5087,7 +5095,7 @@ MachineInstr *CombinerHelper::buildUDivUsingMul(MachineInstr &MI) {
50875095
// TODO: Use undef values for divisor of 1.
50885096
if (!Divisor.isOne()) {
50895097
UnsignedDivisionByConstantInfo magics =
5090-
UnsignedDivisionByConstantInfo::get(Divisor);
5098+
UnsignedDivisionByConstantInfo::get(Divisor, KnownLeadingZeros);
50915099

50925100
Magic = std::move(magics.Magic);
50935101

llvm/test/CodeGen/AArch64/GlobalISel/combine-udiv.ll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,12 @@ define i32 @udiv_div_by_180(i32 %x)
257257
;
258258
; GISEL-LABEL: udiv_div_by_180:
259259
; GISEL: // %bb.0:
260-
; GISEL-NEXT: ubfx w8, w0, #2, #6
261-
; GISEL-NEXT: mov w9, #27671 // =0x6c17
262-
; GISEL-NEXT: movk w9, #5825, lsl #16
260+
; GISEL-NEXT: uxtb w8, w0
261+
; GISEL-NEXT: mov w9, #5826 // =0x16c2
262+
; GISEL-NEXT: movk w9, #364, lsl #16
263263
; GISEL-NEXT: umull x8, w8, w9
264-
; GISEL-NEXT: lsr x8, x8, #32
265-
; GISEL-NEXT: lsr w0, w8, #2
264+
; GISEL-NEXT: lsr x0, x8, #32
265+
; GISEL-NEXT: // kill: def $w0 killed $w0 killed $x0
266266
; GISEL-NEXT: ret
267267
{
268268
%truncate = and i32 %x, 255

0 commit comments

Comments
 (0)