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 <khr@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Joel Sing <joel@sing.id.au>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Keith Randall <khr@google.com>
This commit is contained in:
Meng Zhuo 2025-09-11 17:21:02 +08:00 committed by Gopher Robot
parent aa83aee7de
commit 2469e92d8c
3 changed files with 54 additions and 0 deletions

View file

@ -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 <t> [c] (ADD x x)) && c < t.Size() * 8 - 1 => (SLLI <t> [c+1] x)
(SLLI <t> [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))])

View file

@ -7185,6 +7185,42 @@ func rewriteValueRISCV64_OpRISCV64SLLI(v *Value) bool {
v.AuxInt = int64ToAuxInt(y << uint32(x))
return true
}
// match: (SLLI <t> [c] (ADD x x))
// cond: c < t.Size() * 8 - 1
// result: (SLLI <t> [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 <t> [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 {

View file

@ -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 //
// ------------------ //