[llvm] [GlobalISel]: Simplify udiv lowering by determining known zeros (PR #89678)

via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 23 07:06:03 PDT 2024


https://github.com/AtariDreams updated https://github.com/llvm/llvm-project/pull/89678

>From 231beffad283ecabd8bc1d07a36eba511febf764 Mon Sep 17 00:00:00 2001
From: Rose <gfunni234 at gmail.com>
Date: Mon, 22 Apr 2024 17:36:07 -0400
Subject: [PATCH 1/2] Pre-commit test (NFC)

---
 .../AArch64/GlobalISel/combine-udiv.ll        | 26 +++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/combine-udiv.ll b/llvm/test/CodeGen/AArch64/GlobalISel/combine-udiv.ll
index 9a525151ca328b..a49e2fc6038d41 100644
--- a/llvm/test/CodeGen/AArch64/GlobalISel/combine-udiv.ll
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/combine-udiv.ll
@@ -243,3 +243,29 @@ define <8 x i16> @pr38477(<8 x i16> %a0) {
   %1 = udiv <8 x i16> %a0, <i16 1, i16 119, i16 73, i16 -111, i16 -3, i16 118, i16 32, i16 31>
   ret <8 x i16> %1
 }
+
+define i32 @udiv_div_by_180(i32 %x)
+; SDAG-LABEL: udiv_div_by_180:
+; SDAG:       // %bb.0:
+; SDAG-NEXT:    mov w8, #5826 // =0x16c2
+; SDAG-NEXT:    and w9, w0, #0xff
+; SDAG-NEXT:    movk w8, #364, lsl #16
+; SDAG-NEXT:    umull x8, w9, w8
+; SDAG-NEXT:    lsr x0, x8, #32
+; SDAG-NEXT:    // kill: def $w0 killed $w0 killed $x0
+; SDAG-NEXT:    ret
+;
+; GISEL-LABEL: udiv_div_by_180:
+; GISEL:       // %bb.0:
+; GISEL-NEXT:    ubfx w8, w0, #2, #6
+; GISEL-NEXT:    mov w9, #27671 // =0x6c17
+; GISEL-NEXT:    movk w9, #5825, lsl #16
+; GISEL-NEXT:    umull x8, w8, w9
+; GISEL-NEXT:    lsr x8, x8, #32
+; GISEL-NEXT:    lsr w0, w8, #2
+; GISEL-NEXT:    ret
+{
+  %truncate = and i32 %x, 255
+  %udiv = udiv i32 %truncate, 180
+  ret i32 %udiv
+}

>From daae3e8cd914cd24d6dc40125038ff65d67d4747 Mon Sep 17 00:00:00 2001
From: Rose <gfunni234 at gmail.com>
Date: Mon, 22 Apr 2024 17:24:40 -0400
Subject: [PATCH 2/2] [GlobalISel]: Simplify udiv lowering by determining known
 zeros

---
 llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp       | 10 +++++++++-
 llvm/test/CodeGen/AArch64/GlobalISel/combine-udiv.ll | 10 +++++-----
 2 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
index 5545ec3b3ed0c6..f17bd6508d6fe3 100644
--- a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
@@ -5069,6 +5069,14 @@ MachineInstr *CombinerHelper::buildUDivUsingMul(MachineInstr &MI) {
   const unsigned EltBits = ScalarTy.getScalarSizeInBits();
   LLT ShiftAmtTy = getTargetLowering().getPreferredShiftAmountTy(Ty);
   LLT ScalarShiftAmtTy = ShiftAmtTy.getScalarType();
+
+  // UnsignedDivisionByConstantInfo doesn't work correctly if leading zeros in
+  // the dividend exceeds the leading zeros for the divisor.
+  unsigned KnownLeadingZeros =
+      KB ? std::min(KB->getKnownBits(LHS).countMinLeadingZeros(),
+                    KB->getKnownBits(RHS).countMinLeadingZeros())
+         : 0;
+
   auto &MIB = Builder;
 
   bool UseNPQ = false;
@@ -5087,7 +5095,7 @@ MachineInstr *CombinerHelper::buildUDivUsingMul(MachineInstr &MI) {
     // TODO: Use undef values for divisor of 1.
     if (!Divisor.isOne()) {
       UnsignedDivisionByConstantInfo magics =
-          UnsignedDivisionByConstantInfo::get(Divisor);
+          UnsignedDivisionByConstantInfo::get(Divisor, KnownLeadingZeros);
 
       Magic = std::move(magics.Magic);
 
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/combine-udiv.ll b/llvm/test/CodeGen/AArch64/GlobalISel/combine-udiv.ll
index a49e2fc6038d41..c97a00ccdd4557 100644
--- a/llvm/test/CodeGen/AArch64/GlobalISel/combine-udiv.ll
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/combine-udiv.ll
@@ -257,12 +257,12 @@ define i32 @udiv_div_by_180(i32 %x)
 ;
 ; GISEL-LABEL: udiv_div_by_180:
 ; GISEL:       // %bb.0:
-; GISEL-NEXT:    ubfx w8, w0, #2, #6
-; GISEL-NEXT:    mov w9, #27671 // =0x6c17
-; GISEL-NEXT:    movk w9, #5825, lsl #16
+; GISEL-NEXT:    uxtb w8, w0
+; GISEL-NEXT:    mov w9, #5826 // =0x16c2
+; GISEL-NEXT:    movk w9, #364, lsl #16
 ; GISEL-NEXT:    umull x8, w8, w9
-; GISEL-NEXT:    lsr x8, x8, #32
-; GISEL-NEXT:    lsr w0, w8, #2
+; GISEL-NEXT:    lsr x0, x8, #32
+; GISEL-NEXT:    // kill: def $w0 killed $w0 killed $x0
 ; GISEL-NEXT:    ret
 {
   %truncate = and i32 %x, 255



More information about the llvm-commits mailing list