From aef81a75516fa10338cf2e770914fad70037c49c Mon Sep 17 00:00:00 2001 From: Xiaolin Zhao Date: Thu, 10 Oct 2024 19:46:13 +0800 Subject: [PATCH] cmd/compile: add rules to optimize go codes to constant 0 on loong64 MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Meidan Li Reviewed-by: abner chenc Reviewed-by: Michael Pratt LUCI-TryBot-Result: Go LUCI Reviewed-by: Carlos Amedee --- .../compile/internal/ssa/_gen/LOONG64.rules | 10 +++ .../compile/internal/ssa/rewriteLOONG64.go | 90 +++++++++++++++++++ test/codegen/bitfield.go | 4 + 3 files changed, 104 insertions(+) diff --git a/src/cmd/compile/internal/ssa/_gen/LOONG64.rules b/src/cmd/compile/internal/ssa/_gen/LOONG64.rules index a5fa2ae48f..c1067c8272 100644 --- a/src/cmd/compile/internal/ssa/_gen/LOONG64.rules +++ b/src/cmd/compile/internal/ssa/_gen/LOONG64.rules @@ -646,6 +646,16 @@ (ROTR x (MOVVconst [c])) => (ROTRconst x [c&31]) (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 (MULV x (MOVVconst [-1])) => (NEGV x) (MULV _ (MOVVconst [0])) => (MOVVconst [0]) diff --git a/src/cmd/compile/internal/ssa/rewriteLOONG64.go b/src/cmd/compile/internal/ssa/rewriteLOONG64.go index 9f22c99172..95fa7cd30e 100644 --- a/src/cmd/compile/internal/ssa/rewriteLOONG64.go +++ b/src/cmd/compile/internal/ssa/rewriteLOONG64.go @@ -1989,6 +1989,21 @@ func rewriteValueLOONG64_OpLOONG64MOVBUreg(v *Value) bool { v.AddArg(x) 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])) // result: (MOVVconst [int64(uint8(c))]) for { @@ -3223,6 +3238,21 @@ func rewriteValueLOONG64_OpLOONG64MOVHUreg(v *Value) bool { v.AddArg(x) 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])) // result: (MOVVconst [int64(uint16(c))]) for { @@ -4415,6 +4445,21 @@ func rewriteValueLOONG64_OpLOONG64MOVWUreg(v *Value) bool { v.AddArg(x) 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])) // result: (MOVVconst [int64(uint32(c))]) for { @@ -5839,6 +5884,51 @@ func rewriteValueLOONG64_OpLOONG64SRLVconst(v *Value) bool { v.AddArg(x) 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])) // result: (MOVVconst [int64(uint64(d)>>uint64(c))]) for { diff --git a/test/codegen/bitfield.go b/test/codegen/bitfield.go index 3276af3de0..44cf09f86f 100644 --- a/test/codegen/bitfield.go +++ b/test/codegen/bitfield.go @@ -358,11 +358,15 @@ func rev16w(c uint32) (uint32, uint32, uint32) { func shift(x uint32, y uint16, z uint8) uint64 { // arm64:-`MOVWU`,-`LSR\t[$]32` + // loong64:-`MOVWU`,-`SRLV\t[$]32` a := uint64(x) >> 32 // arm64:-`MOVHU + // loong64:-`MOVHU`,-`SRLV\t[$]16` b := uint64(y) >> 16 // arm64:-`MOVBU` + // loong64:-`MOVBU`,-`SRLV\t[$]8` c := uint64(z) >> 8 // arm64:`MOVD\tZR`,-`ADD\tR[0-9]+>>16`,-`ADD\tR[0-9]+>>8`, + // loong64:`MOVV\t[$]0`,-`ADDVU` return a + b + c } -- 2.48.1