]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: convert branch with zero to more optimal branch zero on riscv64
authorJoel Sing <joel@sing.id.au>
Tue, 17 Aug 2021 09:01:52 +0000 (19:01 +1000)
committerJoel Sing <joel@sing.id.au>
Sat, 21 Aug 2021 11:22:07 +0000 (11:22 +0000)
Convert BLT and BGE with a zero valued constant to BGTZ/BLTZ/BLEZ/BGEZ as
appropriate.

Removes over 4,500 instructions from the go binary on riscv64.

Change-Id: Icc266e968b126ba04863ec88529630a9dd44498b
Reviewed-on: https://go-review.googlesource.com/c/go/+/342849
Trust: Joel Sing <joel@sing.id.au>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Run-TryBot: Cherry Mui <cherryyz@google.com>
TryBot-Result: Go Bot <gobot@golang.org>

src/cmd/compile/internal/ssa/gen/RISCV64.rules
src/cmd/compile/internal/ssa/rewriteRISCV64.go

index 1acef2a27395875979deb79bfe69e3bf2af06737..1414b2b34d5da51838182824e1ec564228b83d75 100644 (file)
 (BEQZ (SLTU x y) yes no) => (BGEU x y yes no)
 (BNEZ (SLTU x y) yes no) => (BLTU x y yes no)
 
-// Convert branch with zero to BEQZ/BNEZ.
+// 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)
 
 // Store zero
 (MOVBstore [off] {sym} ptr (MOVDconst [0]) mem) => (MOVBstorezero [off] {sym} ptr mem)
index e9f17206de673bf51f089260f0559e940fb8329d..9323cda55d2cef85f77bcbc594a1ceaae2bce427 100644 (file)
@@ -6129,6 +6129,52 @@ func rewriteBlockRISCV64(b *Block) bool {
                        b.resetWithControl2(BlockRISCV64BGEU, x, y)
                        return true
                }
+       case BlockRISCV64BGE:
+               // match: (BGE (MOVDconst [0]) cond yes no)
+               // result: (BLEZ 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(BlockRISCV64BLEZ, cond)
+                       return true
+               }
+               // match: (BGE cond (MOVDconst [0]) yes no)
+               // result: (BGEZ cond yes no)
+               for b.Controls[1].Op == OpRISCV64MOVDconst {
+                       cond := b.Controls[0]
+                       v_1 := b.Controls[1]
+                       if auxIntToInt64(v_1.AuxInt) != 0 {
+                               break
+                       }
+                       b.resetWithControl(BlockRISCV64BGEZ, cond)
+                       return true
+               }
+       case BlockRISCV64BLT:
+               // match: (BLT (MOVDconst [0]) cond yes no)
+               // result: (BGTZ 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(BlockRISCV64BGTZ, cond)
+                       return true
+               }
+               // match: (BLT cond (MOVDconst [0]) yes no)
+               // result: (BLTZ cond yes no)
+               for b.Controls[1].Op == OpRISCV64MOVDconst {
+                       cond := b.Controls[0]
+                       v_1 := b.Controls[1]
+                       if auxIntToInt64(v_1.AuxInt) != 0 {
+                               break
+                       }
+                       b.resetWithControl(BlockRISCV64BLTZ, cond)
+                       return true
+               }
        case BlockRISCV64BNE:
                // match: (BNE (MOVDconst [0]) cond yes no)
                // result: (BNEZ cond yes no)