From 7589e9604237ad94869c5803af3bf428f108fefa Mon Sep 17 00:00:00 2001 From: Julian Zhu Date: Sat, 17 May 2025 01:55:12 +0800 Subject: [PATCH] cmd/compile: fold negation into addition/subtraction on s390x MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Fold negation into addition/subtraction and avoid double negation. file before after Δ % addr2line 3909260 3909204 -56 -0.001% asm 6714513 6714505 -8 -0.000% buildid 3680344 3679504 -840 -0.023% cgo 6219857 6219521 -336 -0.005% compile 29527941 29528037 +96 +0.000% cover 6869451 6868731 -720 -0.010% dist 4498817 4498769 -48 -0.001% doc 10483319 10481719 -1600 -0.015% fix 4356204 4355932 -272 -0.006% link 9080951 9080383 -568 -0.006% nm 3899682 3833674 -66008 -1.693% objdump 6347837 6347605 -232 -0.004% pack 3103750 3103454 -296 -0.010% pprof 18849998 18849478 -520 -0.003% test2json 3619671 3619511 -160 -0.004% trace 17164007 17161463 -2544 -0.015% vet 10465861 10465173 -688 -0.007% total 167058409 166983609 -74800 -0.045% Change-Id: I1b8cf3939b433e1765682196b8fc1aa07d37f895 Reviewed-on: https://go-review.googlesource.com/c/go/+/673476 Auto-Submit: Keith Randall Reviewed-by: Keith Randall LUCI-TryBot-Result: Go LUCI Reviewed-by: Keith Randall Reviewed-by: Dmitri Shuralyov --- src/cmd/compile/internal/ssa/_gen/S390X.rules | 3 ++ src/cmd/compile/internal/ssa/rewriteS390X.go | 34 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/cmd/compile/internal/ssa/_gen/S390X.rules b/src/cmd/compile/internal/ssa/_gen/S390X.rules index 78de5bb5a2..231ad0615d 100644 --- a/src/cmd/compile/internal/ssa/_gen/S390X.rules +++ b/src/cmd/compile/internal/ssa/_gen/S390X.rules @@ -1188,6 +1188,8 @@ // TODO: more of this (ADD x (NEG y)) => (SUB x y) (ADDW x (NEGW y)) => (SUBW x y) +(SUB x (NEG y)) => (ADD x y) +(SUBW x (NEGW y)) => (ADDW x y) (SUB x x) => (MOVDconst [0]) (SUBW x x) => (MOVDconst [0]) (AND x x) => x @@ -1196,6 +1198,7 @@ (ORW x x) => x (XOR x x) => (MOVDconst [0]) (XORW x x) => (MOVDconst [0]) +(NEG (NEG x)) => x (NEG (ADDconst [c] (NEG x))) && c != -(1<<31) => (ADDconst [-c] x) (MOVBZreg (ANDWconst [m] x)) => (MOVWZreg (ANDWconst [int32( uint8(m))] x)) (MOVHZreg (ANDWconst [m] x)) => (MOVWZreg (ANDWconst [int32(uint16(m))] x)) diff --git a/src/cmd/compile/internal/ssa/rewriteS390X.go b/src/cmd/compile/internal/ssa/rewriteS390X.go index 7e652a19bc..2e7492501a 100644 --- a/src/cmd/compile/internal/ssa/rewriteS390X.go +++ b/src/cmd/compile/internal/ssa/rewriteS390X.go @@ -11292,6 +11292,16 @@ func rewriteValueS390X_OpS390XNEG(v *Value) bool { v.AuxInt = int64ToAuxInt(-c) return true } + // match: (NEG (NEG x)) + // result: x + for { + if v_0.Op != OpS390XNEG { + break + } + x := v_0.Args[0] + v.copyOf(x) + return true + } // match: (NEG (ADDconst [c] (NEG x))) // cond: c != -(1<<31) // result: (ADDconst [-c] x) @@ -13326,6 +13336,18 @@ func rewriteValueS390X_OpS390XSUB(v *Value) bool { v.AddArg(v0) return true } + // match: (SUB x (NEG y)) + // result: (ADD x y) + for { + x := v_0 + if v_1.Op != OpS390XNEG { + break + } + y := v_1.Args[0] + v.reset(OpS390XADD) + v.AddArg2(x, y) + return true + } // match: (SUB x x) // result: (MOVDconst [0]) for { @@ -13467,6 +13489,18 @@ func rewriteValueS390X_OpS390XSUBW(v *Value) bool { v.AddArg(v0) return true } + // match: (SUBW x (NEGW y)) + // result: (ADDW x y) + for { + x := v_0 + if v_1.Op != OpS390XNEGW { + break + } + y := v_1.Args[0] + v.reset(OpS390XADDW) + v.AddArg2(x, y) + return true + } // match: (SUBW x x) // result: (MOVDconst [0]) for { -- 2.50.0