[PATCH] D134934: [AArch64] Lower multiplication by a negative constant to shl+sub+shl

Allen zhong via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 1 06:29:46 PDT 2022


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Allen marked an inline comment as done.
Closed by commit rG4a549be9c367: [AArch64] Lower multiplication by a negative constant to shl+sub+shl (authored by Allen).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D134934/new/

https://reviews.llvm.org/D134934

Files:
  llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
  llvm/test/CodeGen/AArch64/mul_pow2.ll


Index: llvm/test/CodeGen/AArch64/mul_pow2.ll
===================================================================
--- llvm/test/CodeGen/AArch64/mul_pow2.ll
+++ llvm/test/CodeGen/AArch64/mul_pow2.ll
@@ -524,8 +524,8 @@
 define i32 @ntest6(i32 %x) {
 ; CHECK-LABEL: ntest6:
 ; CHECK:       // %bb.0:
-; CHECK-NEXT:    mov w8, #-6
-; CHECK-NEXT:    mul w0, w0, w8
+; CHECK-NEXT:    lsl w8, w0, #1
+; CHECK-NEXT:    sub w0, w8, w0, lsl #3
 ; CHECK-NEXT:    ret
 ;
 ; GISEL-LABEL: ntest6:
@@ -623,8 +623,8 @@
 define i32 @ntest12(i32 %x) {
 ; CHECK-LABEL: ntest12:
 ; CHECK:       // %bb.0:
-; CHECK-NEXT:    mov w8, #-12
-; CHECK-NEXT:    mul w0, w0, w8
+; CHECK-NEXT:    lsl w8, w0, #2
+; CHECK-NEXT:    sub w0, w8, w0, lsl #4
 ; CHECK-NEXT:    ret
 ;
 ; GISEL-LABEL: ntest12:
@@ -656,8 +656,8 @@
 define i32 @ntest14(i32 %x) {
 ; CHECK-LABEL: ntest14:
 ; CHECK:       // %bb.0:
-; CHECK-NEXT:    mov w8, #-14
-; CHECK-NEXT:    mul w0, w0, w8
+; CHECK-NEXT:    lsl w8, w0, #1
+; CHECK-NEXT:    sub w0, w8, w0, lsl #4
 ; CHECK-NEXT:    ret
 ;
 ; GISEL-LABEL: ntest14:
Index: llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
===================================================================
--- llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -14833,8 +14833,7 @@
   // More aggressively, some multiplications N0 * C can be lowered to
   // shift+add+shift if the constant C = A * B where A = 2^N + 1 and B = 2^M,
   // e.g. 6=3*2=(2+1)*2.
-  // TODO: consider lowering more cases, e.g. C = -6, -14 or even 45
-  // which equals to (1+2)*16-(1+2).
+  // TODO: lower more cases, e.g. C = 45 which equals to (1+2)*16-(1+2).
 
   // TrailingZeroes is used to test if the mul can be lowered to
   // shift+add+shift.
@@ -14891,6 +14890,8 @@
   } else {
     // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
     // (mul x, -(2^N + 1)) => - (add (shl x, N), x)
+    // (mul x, -(2^(N-M) - 1) * 2^M) => (sub (shl x, M), (shl x, N))
+    APInt SCVPlus1 = -ShiftedConstValue + 1;
     APInt CVNegPlus1 = -ConstValue + 1;
     APInt CVNegMinus1 = -ConstValue - 1;
     if (CVNegPlus1.isPowerOf2()) {
@@ -14899,6 +14900,9 @@
     } else if (CVNegMinus1.isPowerOf2()) {
       ShiftAmt = CVNegMinus1.logBase2();
       return Negate(Add(Shl(N0, ShiftAmt), N0));
+    } else if (SCVPlus1.isPowerOf2()) {
+      ShiftAmt = SCVPlus1.logBase2() + TrailingZeroes;
+      return Sub(Shl(N0, TrailingZeroes), Shl(N0, ShiftAmt));
     }
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D134934.464497.patch
Type: text/x-patch
Size: 2472 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221001/0849a7e3/attachment.bin>


More information about the llvm-commits mailing list