1
0
mirror of https://github.com/golang/go synced 2024-09-29 07:24:32 -06:00

cmd/compile: remove NEG when used with SEQZ/SNEZ on riscv64

The negation does not change the comparison to zero.

Also remove unnecessary x.Uses == 1 condition from equivalent BEQZ/BNEZ rules.

Change-Id: I62dd8e383e42bfe5c46d11bbf78d8e5ff862a1d5
Reviewed-on: https://go-review.googlesource.com/c/go/+/426262
Reviewed-by: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Cherry Mui <cherryyz@google.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
This commit is contained in:
Joel Sing 2022-08-28 06:08:02 +10:00
parent 239115c3ef
commit 971373f56a
2 changed files with 51 additions and 21 deletions

View File

@ -601,9 +601,9 @@
(BNEZ (SEQZ x) yes no) => (BEQZ x yes no)
(BNEZ (SNEZ x) yes no) => (BNEZ x yes no)
// Absorb NEG into branch when possible.
(BEQZ x:(NEG y) yes no) && x.Uses == 1 => (BEQZ y yes no)
(BNEZ x:(NEG y) yes no) && x.Uses == 1 => (BNEZ y yes no)
// Absorb NEG into branch.
(BEQZ (NEG x) yes no) => (BEQZ x yes no)
(BNEZ (NEG x) yes no) => (BNEZ x yes no)
// Convert BEQZ/BNEZ into more optimal branch conditions.
(BEQZ (SUB x y) yes no) => (BEQ x y yes no)
@ -623,6 +623,10 @@
(BGE (MOVDconst [0]) cond yes no) => (BLEZ cond yes no)
(BGE cond (MOVDconst [0]) yes no) => (BGEZ cond yes no)
// Remove NEG when used with SEQZ/SNEZ.
(SEQZ (NEG x)) => (SEQZ x)
(SNEZ (NEG x)) => (SNEZ x)
// Store zero
(MOVBstore [off] {sym} ptr (MOVDconst [0]) mem) => (MOVBstorezero [off] {sym} ptr mem)
(MOVHstore [off] {sym} ptr (MOVDconst [0]) mem) => (MOVHstorezero [off] {sym} ptr mem)

View File

@ -505,6 +505,8 @@ func rewriteValueRISCV64(v *Value) bool {
return rewriteValueRISCV64_OpRISCV64OR(v)
case OpRISCV64ORI:
return rewriteValueRISCV64_OpRISCV64ORI(v)
case OpRISCV64SEQZ:
return rewriteValueRISCV64_OpRISCV64SEQZ(v)
case OpRISCV64SLL:
return rewriteValueRISCV64_OpRISCV64SLL(v)
case OpRISCV64SLLI:
@ -517,6 +519,8 @@ func rewriteValueRISCV64(v *Value) bool {
return rewriteValueRISCV64_OpRISCV64SLTIU(v)
case OpRISCV64SLTU:
return rewriteValueRISCV64_OpRISCV64SLTU(v)
case OpRISCV64SNEZ:
return rewriteValueRISCV64_OpRISCV64SNEZ(v)
case OpRISCV64SRA:
return rewriteValueRISCV64_OpRISCV64SRA(v)
case OpRISCV64SRAI:
@ -5000,6 +5004,21 @@ func rewriteValueRISCV64_OpRISCV64ORI(v *Value) bool {
}
return false
}
func rewriteValueRISCV64_OpRISCV64SEQZ(v *Value) bool {
v_0 := v.Args[0]
// match: (SEQZ (NEG x))
// result: (SEQZ x)
for {
if v_0.Op != OpRISCV64NEG {
break
}
x := v_0.Args[0]
v.reset(OpRISCV64SEQZ)
v.AddArg(x)
return true
}
return false
}
func rewriteValueRISCV64_OpRISCV64SLL(v *Value) bool {
v_1 := v.Args[1]
v_0 := v.Args[0]
@ -5102,6 +5121,21 @@ func rewriteValueRISCV64_OpRISCV64SLTU(v *Value) bool {
}
return false
}
func rewriteValueRISCV64_OpRISCV64SNEZ(v *Value) bool {
v_0 := v.Args[0]
// match: (SNEZ (NEG x))
// result: (SNEZ x)
for {
if v_0.Op != OpRISCV64NEG {
break
}
x := v_0.Args[0]
v.reset(OpRISCV64SNEZ)
v.AddArg(x)
return true
}
return false
}
func rewriteValueRISCV64_OpRISCV64SRA(v *Value) bool {
v_1 := v.Args[1]
v_0 := v.Args[0]
@ -6893,16 +6927,12 @@ func rewriteBlockRISCV64(b *Block) bool {
b.resetWithControl(BlockRISCV64BEQZ, x)
return true
}
// match: (BEQZ x:(NEG y) yes no)
// cond: x.Uses == 1
// result: (BEQZ y yes no)
// match: (BEQZ (NEG x) yes no)
// result: (BEQZ x yes no)
for b.Controls[0].Op == OpRISCV64NEG {
x := b.Controls[0]
y := x.Args[0]
if !(x.Uses == 1) {
break
}
b.resetWithControl(BlockRISCV64BEQZ, y)
v_0 := b.Controls[0]
x := v_0.Args[0]
b.resetWithControl(BlockRISCV64BEQZ, x)
return true
}
// match: (BEQZ (SUB x y) yes no)
@ -7018,16 +7048,12 @@ func rewriteBlockRISCV64(b *Block) bool {
b.resetWithControl(BlockRISCV64BNEZ, x)
return true
}
// match: (BNEZ x:(NEG y) yes no)
// cond: x.Uses == 1
// result: (BNEZ y yes no)
// match: (BNEZ (NEG x) yes no)
// result: (BNEZ x yes no)
for b.Controls[0].Op == OpRISCV64NEG {
x := b.Controls[0]
y := x.Args[0]
if !(x.Uses == 1) {
break
}
b.resetWithControl(BlockRISCV64BNEZ, y)
v_0 := b.Controls[0]
x := v_0.Args[0]
b.resetWithControl(BlockRISCV64BNEZ, x)
return true
}
// match: (BNEZ (SUB x y) yes no)