From: Meng Zhuo Date: Thu, 11 Sep 2025 09:21:02 +0000 (+0800) Subject: cmd/compile: combine doubling with shift on riscv64 X-Git-Tag: go1.26rc1~865 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=2469e92d8c;p=gostls13.git cmd/compile: combine doubling with shift on riscv64 Change-Id: I4bee2770fedf97e35b5a5b9187a8ba3c41f9ec2e Reviewed-on: https://go-review.googlesource.com/c/go/+/702697 Reviewed-by: Keith Randall Reviewed-by: Michael Knyszek Reviewed-by: Joel Sing LUCI-TryBot-Result: Go LUCI Auto-Submit: Keith Randall --- diff --git a/src/cmd/compile/internal/ssa/_gen/RISCV64.rules b/src/cmd/compile/internal/ssa/_gen/RISCV64.rules index e14de328ea..7059273eb2 100644 --- a/src/cmd/compile/internal/ssa/_gen/RISCV64.rules +++ b/src/cmd/compile/internal/ssa/_gen/RISCV64.rules @@ -782,6 +782,10 @@ (SRLI [x] (MOVDconst [y])) => (MOVDconst [int64(uint64(y) >> uint32(x))]) (SRAI [x] (MOVDconst [y])) => (MOVDconst [int64(y) >> uint32(x)]) +// Combine doubling via addition with shift. +(SLLI [c] (ADD x x)) && c < t.Size() * 8 - 1 => (SLLI [c+1] x) +(SLLI [c] (ADD x x)) && c >= t.Size() * 8 - 1 => (MOVDconst [0]) + // SLTI/SLTIU with constants. (SLTI [x] (MOVDconst [y])) => (MOVDconst [b2i(int64(y) < int64(x))]) (SLTIU [x] (MOVDconst [y])) => (MOVDconst [b2i(uint64(y) < uint64(x))]) diff --git a/src/cmd/compile/internal/ssa/rewriteRISCV64.go b/src/cmd/compile/internal/ssa/rewriteRISCV64.go index 5723327bc9..a7b4cf1bc4 100644 --- a/src/cmd/compile/internal/ssa/rewriteRISCV64.go +++ b/src/cmd/compile/internal/ssa/rewriteRISCV64.go @@ -7185,6 +7185,42 @@ func rewriteValueRISCV64_OpRISCV64SLLI(v *Value) bool { v.AuxInt = int64ToAuxInt(y << uint32(x)) return true } + // match: (SLLI [c] (ADD x x)) + // cond: c < t.Size() * 8 - 1 + // result: (SLLI [c+1] x) + for { + t := v.Type + c := auxIntToInt64(v.AuxInt) + if v_0.Op != OpRISCV64ADD { + break + } + x := v_0.Args[1] + if x != v_0.Args[0] || !(c < t.Size()*8-1) { + break + } + v.reset(OpRISCV64SLLI) + v.Type = t + v.AuxInt = int64ToAuxInt(c + 1) + v.AddArg(x) + return true + } + // match: (SLLI [c] (ADD x x)) + // cond: c >= t.Size() * 8 - 1 + // result: (MOVDconst [0]) + for { + t := v.Type + c := auxIntToInt64(v.AuxInt) + if v_0.Op != OpRISCV64ADD { + break + } + x := v_0.Args[1] + if x != v_0.Args[0] || !(c >= t.Size()*8-1) { + break + } + v.reset(OpRISCV64MOVDconst) + v.AuxInt = int64ToAuxInt(0) + return true + } return false } func rewriteValueRISCV64_OpRISCV64SLLW(v *Value) bool { diff --git a/test/codegen/shift.go b/test/codegen/shift.go index 1c71b0f3ef..7385058726 100644 --- a/test/codegen/shift.go +++ b/test/codegen/shift.go @@ -122,27 +122,41 @@ func rshConst64x32(v int64) int64 { func lshConst32x1Add(x int32) int32 { // amd64:"SHLL\t[$]2" // loong64:"SLL\t[$]2" + // riscv64:"SLLI\t[$]2" return (x + x) << 1 } func lshConst64x1Add(x int64) int64 { // amd64:"SHLQ\t[$]2" // loong64:"SLLV\t[$]2" + // riscv64:"SLLI\t[$]2" return (x + x) << 1 } func lshConst32x2Add(x int32) int32 { // amd64:"SHLL\t[$]3" // loong64:"SLL\t[$]3" + // riscv64:"SLLI\t[$]3" return (x + x) << 2 } func lshConst64x2Add(x int64) int64 { // amd64:"SHLQ\t[$]3" // loong64:"SLLV\t[$]3" + // riscv64:"SLLI\t[$]3" return (x + x) << 2 } +func lshConst32x31Add(x int32) int32 { + // riscv64:-"SLLI","MOV\t[$]0" + return (x + x) << 31 +} + +func lshConst64x63Add(x int64) int64 { + // riscv64:-"SLLI","MOV\t[$]0" + return (x + x) << 63 +} + // ------------------ // // masked shifts // // ------------------ //