]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: combine doubling with shift on riscv64
authorMeng Zhuo <mengzhuo@iscas.ac.cn>
Thu, 11 Sep 2025 09:21:02 +0000 (17:21 +0800)
committerGopher Robot <gobot@golang.org>
Tue, 16 Sep 2025 00:31:56 +0000 (17:31 -0700)
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>

src/cmd/compile/internal/ssa/_gen/RISCV64.rules
src/cmd/compile/internal/ssa/rewriteRISCV64.go
test/codegen/shift.go

index e14de328ea47d3c6776d78821106d7c43711ee75..7059273eb2fd4c598cf83d8927ec65060233ece0 100644 (file)
 (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))])
index 5723327bc920eb8381d39a4a0e24967acc597621..a7b4cf1bc402d1e93c9c831a20bcfe92ac96ed92 100644 (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 {
index 1c71b0f3efd90a800fd12599cb753b4c254691a5..738505872671dd81bc84be24f6bcf1c5783aa325 100644 (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    //
 // ------------------ //