cmd/compile: fold negation into addition/subtraction on mips64x

Fold negation into addition/subtraction and avoid double negation.

file      before    after     Δ       %
addr2line 4007310   4007470   +160    +0.004%
asm       7007636   7007436   -200    -0.003%
buildid   3839268   3838972   -296    -0.008%
cgo       6353466   6352738   -728    -0.011%
compile   30426920  30426896  -24     -0.000%
cover     7005408   7004744   -664    -0.009%
dist      4651192   4650872   -320    -0.007%
doc       10606050  10606034  -16     -0.000%
fix       4446414   4446390   -24     -0.001%
link      9237736   9237024   -712    -0.008%
nm        3999107   3999323   +216    +0.005%
objdump   6762424   6762144   -280    -0.004%
pack      3270757   3270493   -264    -0.008%
pprof     19428299  19361939  -66360  -0.342%
test2json 3717345   3717217   -128    -0.003%
trace     17382273  17381657  -616    -0.004%
vet       10689481  10688985  -496    -0.005%
go        19118769  19118609  -160    -0.001%
total     171949855 171878943 -70912  -0.041%

Change-Id: I35c1f264d216c214ea3f56252a9ddab8ea850fa6
Reviewed-on: https://go-review.googlesource.com/c/go/+/673555
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Auto-Submit: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
This commit is contained in:
Julian Zhu 2025-05-17 00:14:31 +08:00 committed by Gopher Robot
parent 07a279794d
commit 8097cf14d2
3 changed files with 47 additions and 0 deletions

View File

@ -719,11 +719,14 @@
// generic simplifications
(ADDV x (NEGV y)) => (SUBV x y)
(SUBV x (NEGV y)) => (ADDV x y)
(SUBV x x) => (MOVVconst [0])
(SUBV (MOVVconst [0]) x) => (NEGV x)
(AND x x) => x
(OR x x) => x
(XOR x x) => (MOVVconst [0])
(NEGV (SUBV x y)) => (SUBV y x)
(NEGV (NEGV x)) => x
// remove redundant *const ops
(ADDVconst [0] x) => x

View File

@ -4673,6 +4673,28 @@ func rewriteValueMIPS64_OpMIPS64MOVWstorezero(v *Value) bool {
}
func rewriteValueMIPS64_OpMIPS64NEGV(v *Value) bool {
v_0 := v.Args[0]
// match: (NEGV (SUBV x y))
// result: (SUBV y x)
for {
if v_0.Op != OpMIPS64SUBV {
break
}
y := v_0.Args[1]
x := v_0.Args[0]
v.reset(OpMIPS64SUBV)
v.AddArg2(y, x)
return true
}
// match: (NEGV (NEGV x))
// result: x
for {
if v_0.Op != OpMIPS64NEGV {
break
}
x := v_0.Args[0]
v.copyOf(x)
return true
}
// match: (NEGV (MOVVconst [c]))
// result: (MOVVconst [-c])
for {
@ -5319,6 +5341,18 @@ func rewriteValueMIPS64_OpMIPS64SUBV(v *Value) bool {
v.AddArg(x)
return true
}
// match: (SUBV x (NEGV y))
// result: (ADDV x y)
for {
x := v_0
if v_1.Op != OpMIPS64NEGV {
break
}
y := v_1.Args[0]
v.reset(OpMIPS64ADDV)
v.AddArg2(x, y)
return true
}
// match: (SUBV x x)
// result: (MOVVconst [0])
for {

View File

@ -92,6 +92,7 @@ func SubFromConst(a int) int {
func SubFromConstNeg(a int) int {
// loong64: "ADDV[U]\t\\$40"
// mips64: "ADDV[U]\t\\$40"
// ppc64x: `ADD\t[$]40,\sR[0-9]+,\sR`
// riscv64: "ADDI\t\\$40",-"NEG"
c := 40 - (-a)
@ -100,6 +101,7 @@ func SubFromConstNeg(a int) int {
func SubSubFromConst(a int) int {
// loong64: "ADDV[U]\t\\$20"
// mips64: "ADDV[U]\t\\$20"
// ppc64x: `ADD\t[$]20,\sR[0-9]+,\sR`
// riscv64: "ADDI\t\\$20",-"NEG"
c := 40 - (20 - a)
@ -115,6 +117,7 @@ func AddSubFromConst(a int) int {
func NegSubFromConst(a int) int {
// loong64: "ADDV[U]\t\\$-20"
// mips64: "ADDV[U]\t\\$-20"
// ppc64x: `ADD\t[$]-20,\sR[0-9]+,\sR`
// riscv64: "ADDI\t\\$-20"
c := -(20 - a)
@ -123,6 +126,7 @@ func NegSubFromConst(a int) int {
func NegAddFromConstNeg(a int) int {
// loong64: "ADDV[U]\t\\$-40","SUBV"
// mips64: "ADDV[U]\t\\$-40","SUBV"
// ppc64x: `SUBC\tR[0-9]+,\s[$]40,\sR`
// riscv64: "ADDI\t\\$-40","NEG"
c := -(-40 + a)
@ -132,6 +136,7 @@ func NegAddFromConstNeg(a int) int {
func SubSubNegSimplify(a, b int) int {
// amd64:"NEGQ"
// loong64:"SUBV"
// mips64:"SUBV"
// ppc64x:"NEG"
// riscv64:"NEG",-"SUB"
r := (a - b) - a
@ -141,6 +146,7 @@ func SubSubNegSimplify(a, b int) int {
func SubAddSimplify(a, b int) int {
// amd64:-"SUBQ",-"ADDQ"
// loong64:-"SUBV",-"ADDV"
// mips64:-"SUBV",-"ADDV"
// ppc64x:-"SUB",-"ADD"
// riscv64:-"SUB",-"ADD"
r := a + (b - a)
@ -149,6 +155,7 @@ func SubAddSimplify(a, b int) int {
func SubAddSimplify2(a, b, c int) (int, int, int, int, int, int) {
// amd64:-"ADDQ"
// mips64:"SUBV",-"ADDV"
// loong64:"SUBV",-"ADDV"
r := (a + b) - (a + c)
// amd64:-"ADDQ"
@ -158,6 +165,7 @@ func SubAddSimplify2(a, b, c int) (int, int, int, int, int, int) {
// amd64:-"ADDQ"
r3 := (b + a) - (c + a)
// amd64:-"SUBQ"
// mips64:"ADDV",-"SUBV"
// loong64:"ADDV",-"SUBV"
r4 := (a - c) + (c + b)
// amd64:-"SUBQ"
@ -168,6 +176,7 @@ func SubAddSimplify2(a, b, c int) (int, int, int, int, int, int) {
func SubAddNegSimplify(a, b int) int {
// amd64:"NEGQ",-"ADDQ",-"SUBQ"
// loong64:"SUBV",-"ADDV"
// mips64:"SUBV",-"ADDV"
// ppc64x:"NEG",-"ADD",-"SUB"
// riscv64:"NEG",-"ADD",-"SUB"
r := a - (b + a)
@ -177,6 +186,7 @@ func SubAddNegSimplify(a, b int) int {
func AddAddSubSimplify(a, b, c int) int {
// amd64:-"SUBQ"
// loong64:"ADDV",-"SUBV"
// mips64:"ADDV",-"SUBV"
// ppc64x:-"SUB"
// riscv64:"ADD","ADD",-"SUB"
r := a + (b + (c - a))