cmd/compile: add rules to optimize go codes to constant 0 on loong64

goos: linux
goarch: loong64
pkg: test/bench/go1
cpu: Loongson-3A6000 @ 2500.00MHz
                      │  old.bench  │             new.bench              │
                      │   sec/op    │   sec/op     vs base               │
BinaryTree17             7.735 ± 1%    7.716 ± 1%  -0.23% (p=0.041 n=15)
Fannkuch11               2.645 ± 0%    2.646 ± 0%  +0.05% (p=0.013 n=15)
FmtFprintfEmpty         35.87n ± 0%   35.89n ± 0%  +0.06% (p=0.000 n=15)
FmtFprintfString        59.54n ± 0%   59.47n ± 0%       ~ (p=0.213 n=15)
FmtFprintfInt           62.23n ± 0%   62.06n ± 0%       ~ (p=0.212 n=15)
FmtFprintfIntInt        98.16n ± 0%   97.90n ± 0%  -0.26% (p=0.000 n=15)
FmtFprintfPrefixedInt   117.0n ± 0%   116.7n ± 0%  -0.26% (p=0.000 n=15)
FmtFprintfFloat         204.6n ± 0%   204.2n ± 0%  -0.20% (p=0.000 n=15)
FmtManyArgs             456.3n ± 0%   455.4n ± 0%  -0.20% (p=0.000 n=15)
GobDecode               7.210m ± 0%   7.156m ± 1%  -0.75% (p=0.000 n=15)
GobEncode               8.143m ± 1%   8.177m ± 1%       ~ (p=0.806 n=15)
Gzip                    280.2m ± 0%   279.7m ± 0%  -0.19% (p=0.005 n=15)
Gunzip                  32.71m ± 0%   32.65m ± 0%  -0.19% (p=0.000 n=15)
HTTPClientServer        53.76µ ± 0%   53.65µ ± 0%       ~ (p=0.083 n=15)
JSONEncode              9.297m ± 0%   9.295m ± 0%       ~ (p=0.806 n=15)
JSONDecode              46.97m ± 1%   47.07m ± 1%       ~ (p=0.683 n=15)
Mandelbrot200           4.602m ± 0%   4.600m ± 0%  -0.05% (p=0.001 n=15)
GoParse                 4.682m ± 0%   4.670m ± 1%  -0.25% (p=0.001 n=15)
RegexpMatchEasy0_32     59.80n ± 0%   59.63n ± 0%  -0.28% (p=0.000 n=15)
RegexpMatchEasy0_1K     458.3n ± 0%   457.3n ± 0%  -0.22% (p=0.001 n=15)
RegexpMatchEasy1_32     59.39n ± 0%   59.23n ± 0%  -0.27% (p=0.000 n=15)
RegexpMatchEasy1_1K     557.9n ± 0%   556.6n ± 0%  -0.23% (p=0.001 n=15)
RegexpMatchMedium_32    803.6n ± 0%   801.8n ± 0%  -0.22% (p=0.001 n=15)
RegexpMatchMedium_1K    27.32µ ± 0%   27.26µ ± 0%  -0.21% (p=0.000 n=15)
RegexpMatchHard_32      1.385µ ± 0%   1.382µ ± 0%  -0.22% (p=0.000 n=15)
RegexpMatchHard_1K      40.93µ ± 0%   40.83µ ± 0%  -0.24% (p=0.000 n=15)
Revcomp                 474.8m ± 0%   474.3m ± 0%       ~ (p=0.250 n=15)
Template                77.41m ± 1%   76.63m ± 1%  -1.01% (p=0.023 n=15)
TimeParse               271.1n ± 0%   271.2n ± 0%  +0.04% (p=0.022 n=15)
TimeFormat              290.0n ± 0%   289.8n ± 0%       ~ (p=0.118 n=15)
geomean                 51.73µ        51.64µ       -0.18%

Change-Id: I45a1e6c85bb3cea0f62766ec932432803e9af10a
Reviewed-on: https://go-review.googlesource.com/c/go/+/619315
Reviewed-by: Qiqi Huang <huangqiqi@loongson.cn>
Reviewed-by: Meidan Li <limeidan@loongson.cn>
Reviewed-by: abner chenc <chenguoqi@loongson.cn>
Reviewed-by: Michael Pratt <mpratt@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
This commit is contained in:
Xiaolin Zhao 2024-10-10 19:46:13 +08:00 committed by abner chenc
parent 98b3be702b
commit aef81a7551
3 changed files with 104 additions and 0 deletions

View File

@ -646,6 +646,16 @@
(ROTR x (MOVVconst [c])) => (ROTRconst x [c&31]) (ROTR x (MOVVconst [c])) => (ROTRconst x [c&31])
(ROTRV x (MOVVconst [c])) => (ROTRVconst x [c&63]) (ROTRV x (MOVVconst [c])) => (ROTRVconst x [c&63])
// If the shift amount is larger than the datasize(32, 16, 8), we can optimize to constant 0.
(MOVWUreg (SLLVconst [lc] x)) && lc >= 32 => (MOVVconst [0])
(MOVHUreg (SLLVconst [lc] x)) && lc >= 16 => (MOVVconst [0])
(MOVBUreg (SLLVconst [lc] x)) && lc >= 8 => (MOVVconst [0])
// After zero extension, the upper (64-datasize(32|16|8)) bits are zero, we can optimize to constant 0.
(SRLVconst [rc] (MOVWUreg x)) && rc >= 32 => (MOVVconst [0])
(SRLVconst [rc] (MOVHUreg x)) && rc >= 16 => (MOVVconst [0])
(SRLVconst [rc] (MOVBUreg x)) && rc >= 8 => (MOVVconst [0])
// mul by constant // mul by constant
(MULV x (MOVVconst [-1])) => (NEGV x) (MULV x (MOVVconst [-1])) => (NEGV x)
(MULV _ (MOVVconst [0])) => (MOVVconst [0]) (MULV _ (MOVVconst [0])) => (MOVVconst [0])

View File

@ -1989,6 +1989,21 @@ func rewriteValueLOONG64_OpLOONG64MOVBUreg(v *Value) bool {
v.AddArg(x) v.AddArg(x)
return true return true
} }
// match: (MOVBUreg (SLLVconst [lc] x))
// cond: lc >= 8
// result: (MOVVconst [0])
for {
if v_0.Op != OpLOONG64SLLVconst {
break
}
lc := auxIntToInt64(v_0.AuxInt)
if !(lc >= 8) {
break
}
v.reset(OpLOONG64MOVVconst)
v.AuxInt = int64ToAuxInt(0)
return true
}
// match: (MOVBUreg (MOVVconst [c])) // match: (MOVBUreg (MOVVconst [c]))
// result: (MOVVconst [int64(uint8(c))]) // result: (MOVVconst [int64(uint8(c))])
for { for {
@ -3223,6 +3238,21 @@ func rewriteValueLOONG64_OpLOONG64MOVHUreg(v *Value) bool {
v.AddArg(x) v.AddArg(x)
return true return true
} }
// match: (MOVHUreg (SLLVconst [lc] x))
// cond: lc >= 16
// result: (MOVVconst [0])
for {
if v_0.Op != OpLOONG64SLLVconst {
break
}
lc := auxIntToInt64(v_0.AuxInt)
if !(lc >= 16) {
break
}
v.reset(OpLOONG64MOVVconst)
v.AuxInt = int64ToAuxInt(0)
return true
}
// match: (MOVHUreg (MOVVconst [c])) // match: (MOVHUreg (MOVVconst [c]))
// result: (MOVVconst [int64(uint16(c))]) // result: (MOVVconst [int64(uint16(c))])
for { for {
@ -4415,6 +4445,21 @@ func rewriteValueLOONG64_OpLOONG64MOVWUreg(v *Value) bool {
v.AddArg(x) v.AddArg(x)
return true return true
} }
// match: (MOVWUreg (SLLVconst [lc] x))
// cond: lc >= 32
// result: (MOVVconst [0])
for {
if v_0.Op != OpLOONG64SLLVconst {
break
}
lc := auxIntToInt64(v_0.AuxInt)
if !(lc >= 32) {
break
}
v.reset(OpLOONG64MOVVconst)
v.AuxInt = int64ToAuxInt(0)
return true
}
// match: (MOVWUreg (MOVVconst [c])) // match: (MOVWUreg (MOVVconst [c]))
// result: (MOVVconst [int64(uint32(c))]) // result: (MOVVconst [int64(uint32(c))])
for { for {
@ -5839,6 +5884,51 @@ func rewriteValueLOONG64_OpLOONG64SRLVconst(v *Value) bool {
v.AddArg(x) v.AddArg(x)
return true return true
} }
// match: (SRLVconst [rc] (MOVWUreg x))
// cond: rc >= 32
// result: (MOVVconst [0])
for {
rc := auxIntToInt64(v.AuxInt)
if v_0.Op != OpLOONG64MOVWUreg {
break
}
if !(rc >= 32) {
break
}
v.reset(OpLOONG64MOVVconst)
v.AuxInt = int64ToAuxInt(0)
return true
}
// match: (SRLVconst [rc] (MOVHUreg x))
// cond: rc >= 16
// result: (MOVVconst [0])
for {
rc := auxIntToInt64(v.AuxInt)
if v_0.Op != OpLOONG64MOVHUreg {
break
}
if !(rc >= 16) {
break
}
v.reset(OpLOONG64MOVVconst)
v.AuxInt = int64ToAuxInt(0)
return true
}
// match: (SRLVconst [rc] (MOVBUreg x))
// cond: rc >= 8
// result: (MOVVconst [0])
for {
rc := auxIntToInt64(v.AuxInt)
if v_0.Op != OpLOONG64MOVBUreg {
break
}
if !(rc >= 8) {
break
}
v.reset(OpLOONG64MOVVconst)
v.AuxInt = int64ToAuxInt(0)
return true
}
// match: (SRLVconst [c] (MOVVconst [d])) // match: (SRLVconst [c] (MOVVconst [d]))
// result: (MOVVconst [int64(uint64(d)>>uint64(c))]) // result: (MOVVconst [int64(uint64(d)>>uint64(c))])
for { for {

View File

@ -358,11 +358,15 @@ func rev16w(c uint32) (uint32, uint32, uint32) {
func shift(x uint32, y uint16, z uint8) uint64 { func shift(x uint32, y uint16, z uint8) uint64 {
// arm64:-`MOVWU`,-`LSR\t[$]32` // arm64:-`MOVWU`,-`LSR\t[$]32`
// loong64:-`MOVWU`,-`SRLV\t[$]32`
a := uint64(x) >> 32 a := uint64(x) >> 32
// arm64:-`MOVHU // arm64:-`MOVHU
// loong64:-`MOVHU`,-`SRLV\t[$]16`
b := uint64(y) >> 16 b := uint64(y) >> 16
// arm64:-`MOVBU` // arm64:-`MOVBU`
// loong64:-`MOVBU`,-`SRLV\t[$]8`
c := uint64(z) >> 8 c := uint64(z) >> 8
// arm64:`MOVD\tZR`,-`ADD\tR[0-9]+>>16`,-`ADD\tR[0-9]+>>8`, // arm64:`MOVD\tZR`,-`ADD\tR[0-9]+>>16`,-`ADD\tR[0-9]+>>8`,
// loong64:`MOVV\t[$]0`,-`ADDVU`
return a + b + c return a + b + c
} }