Skip to content

Commit 2cdcc41

Browse files
mengzhuogopherbot
authored andcommitted
cmd/compile: fold negation into multiplication
goos: linux goarch: riscv64 pkg: cmd/compile/internal/test cpu: Spacemit(R) X60 │ /root/mul.base.log │ /root/mul.new.log │ │ sec/op │ sec/op vs base │ MulNeg 6.426µ ± 0% 4.501µ ± 0% -29.96% (p=0.000 n=10) Mul2Neg 9.000µ ± 0% 6.431µ ± 0% -28.54% (p=0.000 n=10) Mul2 1.263µ ± 0% 1.263µ ± 0% ~ (p=1.000 n=10) MulNeg2 1.577µ ± 0% 1.577µ ± 0% ~ (p=0.211 n=10) geomean 3.276µ 2.756µ -15.89% goos: linux goarch: amd64 pkg: cmd/compile/internal/test cpu: AMD EPYC 7532 32-Core Processor │ /root/base │ /root/new │ │ sec/op │ sec/op vs base │ MulNeg 691.9n ± 1% 319.4n ± 0% -53.83% (p=0.000 n=10) Mul2Neg 630.0n ± 0% 629.6n ± 0% -0.07% (p=0.000 n=10) Mul2 438.1n ± 0% 438.1n ± 0% ~ (p=0.728 n=10) MulNeg2 439.3n ± 0% 439.4n ± 0% ~ (p=0.656 n=10) geomean 538.2n 443.6n -17.58% Change-Id: Ice8e6c8d1e8e3009ba8a0b1b689205174e199019 Reviewed-on: https://go-review.googlesource.com/c/go/+/720180 Reviewed-by: abner chenc <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Junyang Shao <[email protected]> Reviewed-by: Joel Sing <[email protected]> Reviewed-by: Keith Randall <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Keith Randall <[email protected]>
1 parent b57962b commit 2cdcc41

File tree

5 files changed

+171
-46
lines changed

5 files changed

+171
-46
lines changed

src/cmd/compile/internal/ssa/_gen/LOONG64.rules

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -743,9 +743,6 @@
743743

744744
(MULV x (MOVVconst [c])) && canMulStrengthReduce(config, c) => {mulStrengthReduce(v, x, c)}
745745

746-
(MULV (NEGV x) (MOVVconst [c])) => (MULV x (MOVVconst [-c]))
747-
(MULV (NEGV x) (NEGV y)) => (MULV x y)
748-
749746
(ADDV x0 x1:(SLLVconst [c] y)) && x1.Uses == 1 && c > 0 && c <= 4 => (ADDshiftLLV x0 y [c])
750747

751748
// fold constant in ADDshift op

src/cmd/compile/internal/ssa/_gen/generic.rules

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@
195195
// Convert x * -1 to -x.
196196
(Mul(8|16|32|64) (Const(8|16|32|64) [-1]) x) => (Neg(8|16|32|64) x)
197197

198+
// Convert -x * c to x * -c
199+
(Mul(8|16|32|64) (Const(8|16|32|64) <t> [c]) (Neg(8|16|32|64) x)) => (Mul(8|16|32|64) x (Const(8|16|32|64) <t> [-c]))
200+
201+
(Mul(8|16|32|64) (Neg(8|16|32|64) x) (Neg(8|16|32|64) y)) => (Mul(8|16|32|64) x y)
202+
198203
// DeMorgan's Laws
199204
(And(8|16|32|64) <t> (Com(8|16|32|64) x) (Com(8|16|32|64) y)) => (Com(8|16|32|64) (Or(8|16|32|64) <t> x y))
200205
(Or(8|16|32|64) <t> (Com(8|16|32|64) x) (Com(8|16|32|64) y)) => (Com(8|16|32|64) (And(8|16|32|64) <t> x y))
@@ -2228,4 +2233,4 @@
22282233
(Neq(64|32|16) (SignExt8to(64|32|16) (CvtBoolToUint8 x)) (Const(64|32|16) [0])) => x
22292234
(Neq(64|32|16) (SignExt8to(64|32|16) (CvtBoolToUint8 x)) (Const(64|32|16) [1])) => (Not x)
22302235
(Eq(64|32|16) (SignExt8to(64|32|16) (CvtBoolToUint8 x)) (Const(64|32|16) [1])) => x
2231-
(Eq(64|32|16) (SignExt8to(64|32|16) (CvtBoolToUint8 x)) (Const(64|32|16) [0])) => (Not x)
2236+
(Eq(64|32|16) (SignExt8to(64|32|16) (CvtBoolToUint8 x)) (Const(64|32|16) [0])) => (Not x)

src/cmd/compile/internal/ssa/rewriteLOONG64.go

Lines changed: 0 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/compile/internal/ssa/rewritegeneric.go

Lines changed: 156 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/codegen/arithmetic.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,13 +318,19 @@ func MergeMuls5(a, n int) int {
318318
// Multiplications folded negation
319319

320320
func FoldNegMul(a int) int {
321-
// loong64:"SUBVU" "ALSLV [$]2" "ALSLV [$]1"
322-
return (-a) * 11
321+
// amd64:"IMUL3Q [$]-11" -"NEGQ"
322+
// arm64:"MOVD [$]-11" "MUL" -"NEG"
323+
// loong64:"ALSLV [$]2" "SUBVU" "ALSLV [$]4"
324+
// riscv64:"MOV [$]-11" "MUL" -"NEG"
325+
return -a * 11
323326
}
324327

325328
func Fold2NegMul(a, b int) int {
329+
// amd64:"IMULQ" -"NEGQ"
330+
// arm64:"MUL" -"NEG"
326331
// loong64:"MULV" -"SUBVU R[0-9], R0,"
327-
return (-a) * (-b)
332+
// riscv64:"MUL" -"NEG"
333+
return -a * -b
328334
}
329335

330336
// -------------- //

0 commit comments

Comments
 (0)