Skip to content

Commit a552737

Browse files
sophie-zhaoabner-chenc
authored andcommitted
cmd/compile: fold negation into multiplication on loong64
This change also add corresponding benchmark tests and codegen tests. The performance improvement on CPU Loongson-3A6000-HV is as follows: goos: linux goarch: loong64 pkg: cmd/compile/internal/test cpu: Loongson-3A6000-HV @ 2500.00MHz | bench.old | bench.new | | sec/op | sec/op vs base | MulNeg 828.4n ± 0% 655.9n ± 0% -20.82% (p=0.000 n=10) Mul2Neg 1062.0n ± 0% 826.8n ± 0% -22.15% (p=0.000 n=10) geomean 938.0n 736.4n -21.49% Change-Id: Ia999732880ec65be0c66cddc757a4868847e5b15 Reviewed-on: https://go-review.googlesource.com/c/go/+/682535 Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: abner chenc <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Mark Freeman <[email protected]>
1 parent e1fd4fa commit a552737

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

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

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

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

758+
(MULV (NEGV x) (MOVVconst [c])) => (MULV x (MOVVconst [-c]))
759+
(MULV (NEGV x) (NEGV y)) => (MULV x y)
760+
758761
// div by constant
759762
(DIVVU x (MOVVconst [1])) => x
760763
(DIVVU x (MOVVconst [c])) && isPowerOfTwo(c) => (SRLVconst [log64(c)] x)

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

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

src/cmd/compile/internal/test/bench_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,26 @@ func BenchmarkBitToggleConst(b *testing.B) {
122122
}
123123
}
124124
}
125+
126+
func BenchmarkMulNeg(b *testing.B) {
127+
x := make([]int64, 1024)
128+
for i := 0; i < b.N; i++ {
129+
var s int64
130+
for i := range x {
131+
s = (-x[i]) * 11
132+
}
133+
globl = s
134+
}
135+
}
136+
137+
func BenchmarkMul2Neg(b *testing.B) {
138+
x := make([]int64, 1024)
139+
y := make([]int64, 1024)
140+
for i := 0; i < b.N; i++ {
141+
var s int64
142+
for i := range x {
143+
s = (-x[i]) * (-y[i])
144+
}
145+
globl = s
146+
}
147+
}

test/codegen/arithmetic.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,18 @@ func MergeMuls5(a, n int) int {
314314
return a*n - 19*n // (a-19)n
315315
}
316316

317+
// Multiplications folded negation
318+
319+
func FoldNegMul(a int) int {
320+
// loong64:"MULV","MOVV\t[$]-11",-"SUBVU\tR[0-9], R0,"
321+
return (-a) * 11
322+
}
323+
324+
func Fold2NegMul(a, b int) int {
325+
// loong64:"MULV",-"SUBVU\tR[0-9], R0,"
326+
return (-a) * (-b)
327+
}
328+
317329
// -------------- //
318330
// Division //
319331
// -------------- //

0 commit comments

Comments
 (0)