]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/ssa: optimise more branches with zero on riscv64
authorJoel Sing <joel@sing.id.au>
Mon, 19 Aug 2024 13:57:56 +0000 (23:57 +1000)
committerJoel Sing <joel@sing.id.au>
Fri, 28 Mar 2025 08:27:22 +0000 (01:27 -0700)
Optimise more branches with zero on riscv64. In particular, BLTU with
zero occurs with IsInBounds checks for index zero. This currently results
in two instructions and requires an additional register:

   li      t2, 0
   bltu    t2, t1, 0x174b4

This is equivalent to checking if the bounds is not equal to zero. With
this change:

   bnez    t1, 0x174c0

This removes more than 500 instructions from the Go binary on riscv64.

Change-Id: I6cd861d853e3ef270bd46dacecdfaa205b1c4644
Reviewed-on: https://go-review.googlesource.com/c/go/+/606715
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Meng Zhuo <mengzhuo1203@gmail.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
src/cmd/compile/internal/ssa/_gen/RISCV64.rules
src/cmd/compile/internal/ssa/rewriteRISCV64.go
test/codegen/compare_and_branch.go

index 96b9b11cf9d711bee700f7babe08c1950731ac3e..a5d4fb72ec687f23f0938d33e143f336aa97b531 100644 (file)
 (BNEZ (SLTIU [x] y) yes no) => (BLTU y (MOVDconst [x]) yes no)
 
 // Convert branch with zero to more optimal branch zero.
-(BEQ (MOVDconst [0]) cond yes no) => (BEQZ cond yes no)
-(BEQ cond (MOVDconst [0]) yes no) => (BEQZ cond yes no)
-(BNE (MOVDconst [0]) cond yes no) => (BNEZ cond yes no)
-(BNE cond (MOVDconst [0]) yes no) => (BNEZ cond yes no)
-(BLT (MOVDconst [0]) cond yes no) => (BGTZ cond yes no)
-(BLT cond (MOVDconst [0]) yes no) => (BLTZ cond yes no)
-(BGE (MOVDconst [0]) cond yes no) => (BLEZ cond yes no)
-(BGE cond (MOVDconst [0]) yes no) => (BGEZ cond yes no)
+(BEQ  (MOVDconst [0]) cond yes no) => (BEQZ cond yes no)
+(BEQ  cond (MOVDconst [0]) yes no) => (BEQZ cond yes no)
+(BNE  (MOVDconst [0]) cond yes no) => (BNEZ cond yes no)
+(BNE  cond (MOVDconst [0]) yes no) => (BNEZ cond yes no)
+(BLT  (MOVDconst [0]) cond yes no) => (BGTZ cond yes no)
+(BLT  cond (MOVDconst [0]) yes no) => (BLTZ cond yes no)
+(BLTU (MOVDconst [0]) cond yes no) => (BNEZ cond yes no)
+(BGE  (MOVDconst [0]) cond yes no) => (BLEZ cond yes no)
+(BGE  cond (MOVDconst [0]) yes no) => (BGEZ cond yes no)
+(BGEU (MOVDconst [0]) cond yes no) => (BEQZ cond yes no)
 
 // Remove redundant NEG from SEQZ/SNEZ.
 (SEQZ (NEG x)) => (SEQZ x)
index b2318e711b272f6802380d55b5e70f4c53f1c638..182ca2d3fd674fad6a4b83cb6d45a99b23eb3bbf 100644 (file)
@@ -9403,6 +9403,18 @@ func rewriteBlockRISCV64(b *Block) bool {
                        b.resetWithControl(BlockRISCV64BGEZ, cond)
                        return true
                }
+       case BlockRISCV64BGEU:
+               // match: (BGEU (MOVDconst [0]) cond yes no)
+               // result: (BEQZ cond yes no)
+               for b.Controls[0].Op == OpRISCV64MOVDconst {
+                       v_0 := b.Controls[0]
+                       if auxIntToInt64(v_0.AuxInt) != 0 {
+                               break
+                       }
+                       cond := b.Controls[1]
+                       b.resetWithControl(BlockRISCV64BEQZ, cond)
+                       return true
+               }
        case BlockRISCV64BLT:
                // match: (BLT (MOVDconst [0]) cond yes no)
                // result: (BGTZ cond yes no)
@@ -9426,6 +9438,18 @@ func rewriteBlockRISCV64(b *Block) bool {
                        b.resetWithControl(BlockRISCV64BLTZ, cond)
                        return true
                }
+       case BlockRISCV64BLTU:
+               // match: (BLTU (MOVDconst [0]) cond yes no)
+               // result: (BNEZ cond yes no)
+               for b.Controls[0].Op == OpRISCV64MOVDconst {
+                       v_0 := b.Controls[0]
+                       if auxIntToInt64(v_0.AuxInt) != 0 {
+                               break
+                       }
+                       cond := b.Controls[1]
+                       b.resetWithControl(BlockRISCV64BNEZ, cond)
+                       return true
+               }
        case BlockRISCV64BNE:
                // match: (BNE (MOVDconst [0]) cond yes no)
                // result: (BNEZ cond yes no)
index c121f1d2cc235a4b2b7eb9fb9387b5760fab74b3..759dd2635818854491c51a54429204d6ba9c1b0e 100644 (file)
@@ -241,4 +241,14 @@ func ui64x0(x chan uint64) {
        for <-x < 1 {
                dummy()
        }
+
+       // riscv64:"BNEZ"
+       for 0 < <-x {
+               dummy()
+       }
+
+       // riscv64:"BEQZ"
+       for 0 >= <-x {
+               dummy()
+       }
 }