From: Julian Zhu Date: Fri, 16 May 2025 16:14:31 +0000 (+0800) Subject: cmd/compile: fold negation into addition/subtraction on mips64x X-Git-Tag: go1.25rc1~245 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=8097cf14d20b547a615aae7d1b738a6aa563aa31;p=gostls13.git 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 Reviewed-by: Keith Randall Auto-Submit: Keith Randall LUCI-TryBot-Result: Go LUCI Reviewed-by: Michael Knyszek --- diff --git a/src/cmd/compile/internal/ssa/_gen/MIPS64.rules b/src/cmd/compile/internal/ssa/_gen/MIPS64.rules index 5b55c3bef7..cd82655ff3 100644 --- a/src/cmd/compile/internal/ssa/_gen/MIPS64.rules +++ b/src/cmd/compile/internal/ssa/_gen/MIPS64.rules @@ -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 diff --git a/src/cmd/compile/internal/ssa/rewriteMIPS64.go b/src/cmd/compile/internal/ssa/rewriteMIPS64.go index 10281e7e70..14b273f9aa 100644 --- a/src/cmd/compile/internal/ssa/rewriteMIPS64.go +++ b/src/cmd/compile/internal/ssa/rewriteMIPS64.go @@ -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 { diff --git a/test/codegen/arithmetic.go b/test/codegen/arithmetic.go index d02154bd3c..1023a62bee 100644 --- a/test/codegen/arithmetic.go +++ b/test/codegen/arithmetic.go @@ -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))