[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
Thu Sep 29 19:46:13 PDT 2022
Allen created this revision.
Allen added reviewers: efriedma, dmgreen.
Herald added subscribers: hiraditya, kristof.beyls.
Herald added a project: All.
Allen requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
Change the costmodel to lower a = b * C where C = -(2^n - 2^m) to
lsl w8, w0, m
sub w0, w8, w0, lsl n
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
@@ -14799,7 +14799,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
+ // TODO: consider lowering 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
@@ -14857,6 +14857,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()) {
@@ -14865,6 +14867,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.464131.patch
Type: text/x-patch
Size: 2429 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220930/b8e55905/attachment.bin>
More information about the llvm-commits
mailing list