Skip to content

Commit 4cb7b54

Browse files
committed
[GlobalISel]: Simplify udiv lowering by determining known zeros
1 parent a73698e commit 4cb7b54

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5069,6 +5069,9 @@ MachineInstr *CombinerHelper::buildUDivUsingMul(MachineInstr &MI) {
50695069
const unsigned EltBits = ScalarTy.getScalarSizeInBits();
50705070
LLT ShiftAmtTy = getTargetLowering().getPreferredShiftAmountTy(Ty);
50715071
LLT ScalarShiftAmtTy = ShiftAmtTy.getScalarType();
5072+
5073+
unsigned KnownLeadingZeros =
5074+
KB ? KB->getKnownBits(LHS).countMinLeadingZeros() : 0;
50725075
auto &MIB = Builder;
50735076

50745077
bool UseNPQ = false;
@@ -5086,8 +5089,12 @@ MachineInstr *CombinerHelper::buildUDivUsingMul(MachineInstr &MI) {
50865089
// at the end.
50875090
// TODO: Use undef values for divisor of 1.
50885091
if (!Divisor.isOne()) {
5092+
5093+
// UnsignedDivisionByConstantInfo doesn't work correctly if leading zeros
5094+
// in the dividend exceeds the leading zeros for the divisor.
50895095
UnsignedDivisionByConstantInfo magics =
5090-
UnsignedDivisionByConstantInfo::get(Divisor);
5096+
UnsignedDivisionByConstantInfo::get(
5097+
Divisor, std::min(KnownLeadingZeros, Divisor.countl_zero()));
50915098

50925099
Magic = std::move(magics.Magic);
50935100

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)