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

Fold negation into addition/subtraction and avoid double negation.

file      before    after     Δ       %
addr2line 3742022   3741986   -36     -0.001%
asm       6668616   6668628   +12     +0.000%
buildid   3583786   3583630   -156    -0.004%
cgo       6020370   6019634   -736    -0.012%
compile   29416016  29417336  +1320   +0.004%
cover     6801903   6801675   -228    -0.003%
dist      4485916   4485816   -100    -0.002%
doc       10652787  10652251  -536    -0.005%
fix       4115988   4115560   -428    -0.010%
link      9002328   9001616   -712    -0.008%
nm        3733148   3732780   -368    -0.010%
objdump   6163292   6163068   -224    -0.004%
pack      2944768   2944604   -164    -0.006%
pprof     18909973  18908773  -1200   -0.006%
test2json 3394662   3394778   +116    +0.003%
trace     17350911  17349751  -1160   -0.007%
vet       10077727  10077527  -200    -0.002%
go        19118769  19118609  -160    -0.001%
total     166182982 166178022 -4960   -0.003%

Change-Id: Id55698800fd70f3cb2ff48393584456b87208921
Reviewed-on: https://go-review.googlesource.com/c/go/+/673556
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
This commit is contained in:
Julian Zhu 2025-05-17 00:27:37 +08:00 committed by Keith Randall
parent 972639fc4c
commit d52679006c
3 changed files with 47 additions and 0 deletions

View File

@ -614,11 +614,14 @@
// generic simplifications // generic simplifications
(ADD x (NEG y)) => (SUB x y) (ADD x (NEG y)) => (SUB x y)
(SUB x (NEG y)) => (ADD x y)
(SUB x x) => (MOVWconst [0]) (SUB x x) => (MOVWconst [0])
(SUB (MOVWconst [0]) x) => (NEG x) (SUB (MOVWconst [0]) x) => (NEG x)
(AND x x) => x (AND x x) => x
(OR x x) => x (OR x x) => x
(XOR x x) => (MOVWconst [0]) (XOR x x) => (MOVWconst [0])
(NEG (SUB x y)) => (SUB y x)
(NEG (NEG x)) => x
// miscellaneous patterns generated by dec64 // miscellaneous patterns generated by dec64
(AND (SGTUconst [1] x) (SGTUconst [1] y)) => (SGTUconst [1] (OR <x.Type> x y)) (AND (SGTUconst [1] x) (SGTUconst [1] y)) => (SGTUconst [1] (OR <x.Type> x y))

View File

@ -4096,6 +4096,28 @@ func rewriteValueMIPS_OpMIPSMUL(v *Value) bool {
} }
func rewriteValueMIPS_OpMIPSNEG(v *Value) bool { func rewriteValueMIPS_OpMIPSNEG(v *Value) bool {
v_0 := v.Args[0] v_0 := v.Args[0]
// match: (NEG (SUB x y))
// result: (SUB y x)
for {
if v_0.Op != OpMIPSSUB {
break
}
y := v_0.Args[1]
x := v_0.Args[0]
v.reset(OpMIPSSUB)
v.AddArg2(y, x)
return true
}
// match: (NEG (NEG x))
// result: x
for {
if v_0.Op != OpMIPSNEG {
break
}
x := v_0.Args[0]
v.copyOf(x)
return true
}
// match: (NEG (MOVWconst [c])) // match: (NEG (MOVWconst [c]))
// result: (MOVWconst [-c]) // result: (MOVWconst [-c])
for { for {
@ -4748,6 +4770,18 @@ func rewriteValueMIPS_OpMIPSSUB(v *Value) bool {
v.AddArg(x) v.AddArg(x)
return true return true
} }
// match: (SUB x (NEG y))
// result: (ADD x y)
for {
x := v_0
if v_1.Op != OpMIPSNEG {
break
}
y := v_1.Args[0]
v.reset(OpMIPSADD)
v.AddArg2(x, y)
return true
}
// match: (SUB x x) // match: (SUB x x)
// result: (MOVWconst [0]) // result: (MOVWconst [0])
for { for {

View File

@ -92,6 +92,7 @@ func SubFromConst(a int) int {
func SubFromConstNeg(a int) int { func SubFromConstNeg(a int) int {
// loong64: "ADDV[U]\t\\$40" // loong64: "ADDV[U]\t\\$40"
// mips: "ADD[U]\t\\$40"
// mips64: "ADDV[U]\t\\$40" // mips64: "ADDV[U]\t\\$40"
// ppc64x: `ADD\t[$]40,\sR[0-9]+,\sR` // ppc64x: `ADD\t[$]40,\sR[0-9]+,\sR`
// riscv64: "ADDI\t\\$40",-"NEG" // riscv64: "ADDI\t\\$40",-"NEG"
@ -101,6 +102,7 @@ func SubFromConstNeg(a int) int {
func SubSubFromConst(a int) int { func SubSubFromConst(a int) int {
// loong64: "ADDV[U]\t\\$20" // loong64: "ADDV[U]\t\\$20"
// mips: "ADD[U]\t\\$20"
// mips64: "ADDV[U]\t\\$20" // mips64: "ADDV[U]\t\\$20"
// ppc64x: `ADD\t[$]20,\sR[0-9]+,\sR` // ppc64x: `ADD\t[$]20,\sR[0-9]+,\sR`
// riscv64: "ADDI\t\\$20",-"NEG" // riscv64: "ADDI\t\\$20",-"NEG"
@ -117,6 +119,7 @@ func AddSubFromConst(a int) int {
func NegSubFromConst(a int) int { func NegSubFromConst(a int) int {
// loong64: "ADDV[U]\t\\$-20" // loong64: "ADDV[U]\t\\$-20"
// mips: "ADD[U]\t\\$-20"
// mips64: "ADDV[U]\t\\$-20" // mips64: "ADDV[U]\t\\$-20"
// ppc64x: `ADD\t[$]-20,\sR[0-9]+,\sR` // ppc64x: `ADD\t[$]-20,\sR[0-9]+,\sR`
// riscv64: "ADDI\t\\$-20" // riscv64: "ADDI\t\\$-20"
@ -126,6 +129,7 @@ func NegSubFromConst(a int) int {
func NegAddFromConstNeg(a int) int { func NegAddFromConstNeg(a int) int {
// loong64: "ADDV[U]\t\\$-40","SUBV" // loong64: "ADDV[U]\t\\$-40","SUBV"
// mips: "ADD[U]\t\\$-40","SUB"
// mips64: "ADDV[U]\t\\$-40","SUBV" // mips64: "ADDV[U]\t\\$-40","SUBV"
// ppc64x: `SUBC\tR[0-9]+,\s[$]40,\sR` // ppc64x: `SUBC\tR[0-9]+,\s[$]40,\sR`
// riscv64: "ADDI\t\\$-40","NEG" // riscv64: "ADDI\t\\$-40","NEG"
@ -136,6 +140,7 @@ func NegAddFromConstNeg(a int) int {
func SubSubNegSimplify(a, b int) int { func SubSubNegSimplify(a, b int) int {
// amd64:"NEGQ" // amd64:"NEGQ"
// loong64:"SUBV" // loong64:"SUBV"
// mips:"SUB"
// mips64:"SUBV" // mips64:"SUBV"
// ppc64x:"NEG" // ppc64x:"NEG"
// riscv64:"NEG",-"SUB" // riscv64:"NEG",-"SUB"
@ -146,6 +151,7 @@ func SubSubNegSimplify(a, b int) int {
func SubAddSimplify(a, b int) int { func SubAddSimplify(a, b int) int {
// amd64:-"SUBQ",-"ADDQ" // amd64:-"SUBQ",-"ADDQ"
// loong64:-"SUBV",-"ADDV" // loong64:-"SUBV",-"ADDV"
// mips:-"SUB",-"ADD"
// mips64:-"SUBV",-"ADDV" // mips64:-"SUBV",-"ADDV"
// ppc64x:-"SUB",-"ADD" // ppc64x:-"SUB",-"ADD"
// riscv64:-"SUB",-"ADD" // riscv64:-"SUB",-"ADD"
@ -155,6 +161,7 @@ func SubAddSimplify(a, b int) int {
func SubAddSimplify2(a, b, c int) (int, int, int, int, int, int) { func SubAddSimplify2(a, b, c int) (int, int, int, int, int, int) {
// amd64:-"ADDQ" // amd64:-"ADDQ"
// mips:"SUB",-"ADD"
// mips64:"SUBV",-"ADDV" // mips64:"SUBV",-"ADDV"
// loong64:"SUBV",-"ADDV" // loong64:"SUBV",-"ADDV"
r := (a + b) - (a + c) r := (a + b) - (a + c)
@ -165,6 +172,7 @@ func SubAddSimplify2(a, b, c int) (int, int, int, int, int, int) {
// amd64:-"ADDQ" // amd64:-"ADDQ"
r3 := (b + a) - (c + a) r3 := (b + a) - (c + a)
// amd64:-"SUBQ" // amd64:-"SUBQ"
// mips:"ADD",-"SUB"
// mips64:"ADDV",-"SUBV" // mips64:"ADDV",-"SUBV"
// loong64:"ADDV",-"SUBV" // loong64:"ADDV",-"SUBV"
r4 := (a - c) + (c + b) r4 := (a - c) + (c + b)
@ -176,6 +184,7 @@ func SubAddSimplify2(a, b, c int) (int, int, int, int, int, int) {
func SubAddNegSimplify(a, b int) int { func SubAddNegSimplify(a, b int) int {
// amd64:"NEGQ",-"ADDQ",-"SUBQ" // amd64:"NEGQ",-"ADDQ",-"SUBQ"
// loong64:"SUBV",-"ADDV" // loong64:"SUBV",-"ADDV"
// mips:"SUB",-"ADD"
// mips64:"SUBV",-"ADDV" // mips64:"SUBV",-"ADDV"
// ppc64x:"NEG",-"ADD",-"SUB" // ppc64x:"NEG",-"ADD",-"SUB"
// riscv64:"NEG",-"ADD",-"SUB" // riscv64:"NEG",-"ADD",-"SUB"
@ -186,6 +195,7 @@ func SubAddNegSimplify(a, b int) int {
func AddAddSubSimplify(a, b, c int) int { func AddAddSubSimplify(a, b, c int) int {
// amd64:-"SUBQ" // amd64:-"SUBQ"
// loong64:"ADDV",-"SUBV" // loong64:"ADDV",-"SUBV"
// mips:"ADD",-"SUB"
// mips64:"ADDV",-"SUBV" // mips64:"ADDV",-"SUBV"
// ppc64x:-"SUB" // ppc64x:-"SUB"
// riscv64:"ADD","ADD",-"SUB" // riscv64:"ADD","ADD",-"SUB"