From d52679006c8e08875fac92aca1f723fce488d0d2 Mon Sep 17 00:00:00 2001 From: Julian Zhu Date: Sat, 17 May 2025 00:27:37 +0800 Subject: cmd/compile: fold negation into addition/subtraction on mipsx MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Keith Randall Reviewed-by: Keith Randall Reviewed-by: Cherry Mui --- src/cmd/compile/internal/ssa/_gen/MIPS.rules | 3 +++ src/cmd/compile/internal/ssa/rewriteMIPS.go | 34 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) (limited to 'src/cmd') diff --git a/src/cmd/compile/internal/ssa/_gen/MIPS.rules b/src/cmd/compile/internal/ssa/_gen/MIPS.rules index 4471763462..a4899ac24d 100644 --- a/src/cmd/compile/internal/ssa/_gen/MIPS.rules +++ b/src/cmd/compile/internal/ssa/_gen/MIPS.rules @@ -614,11 +614,14 @@ // generic simplifications (ADD x (NEG y)) => (SUB x y) +(SUB x (NEG y)) => (ADD x y) (SUB x x) => (MOVWconst [0]) (SUB (MOVWconst [0]) x) => (NEG x) (AND x x) => x (OR x x) => x (XOR x x) => (MOVWconst [0]) +(NEG (SUB x y)) => (SUB y x) +(NEG (NEG x)) => x // miscellaneous patterns generated by dec64 (AND (SGTUconst [1] x) (SGTUconst [1] y)) => (SGTUconst [1] (OR x y)) diff --git a/src/cmd/compile/internal/ssa/rewriteMIPS.go b/src/cmd/compile/internal/ssa/rewriteMIPS.go index 1bc2cb6e6d..fe24f0fd0f 100644 --- a/src/cmd/compile/internal/ssa/rewriteMIPS.go +++ b/src/cmd/compile/internal/ssa/rewriteMIPS.go @@ -4096,6 +4096,28 @@ func rewriteValueMIPS_OpMIPSMUL(v *Value) bool { } func rewriteValueMIPS_OpMIPSNEG(v *Value) bool { 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])) // result: (MOVWconst [-c]) for { @@ -4748,6 +4770,18 @@ func rewriteValueMIPS_OpMIPSSUB(v *Value) bool { v.AddArg(x) 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) // result: (MOVWconst [0]) for { -- cgit v1.3