[go] cmd/compile: fold x + (y - (x - z)) to y + z

0 views
Skip to first unread message

shuang cui (Gerrit)

unread,
Aug 11, 2026, 9:02:38 PM (6 hours ago) Aug 11
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

shuang cui has uploaded the change for review

Commit message

cmd/compile: fold x + (y - (x - z)) to y + z

Add a generic SSA rewrite for the fixed-width integer identity

x + (y - (x - z)) == y + z

The identity holds modulo 2^W, so it remains valid when intermediate
integer operations overflow.

For a minimal arm64 example, this reduces an ADD/SUB/ADD sequence
to one ADD.

The pattern occurs in segmentio/encoding's JSON byte encoder in the
allocation-length expression

cap(b) + (n - (cap(b) - len(b)))

where avail is cap(b) - len(b). The expression can be reduced to

len(b) + n

The avail computation remains because it is also used by the branch
condition. The rewrite only removes the redundant arithmetic in the
allocation-length expression.

Add code generation coverage for amd64, arm64, loong64, mips, mips64,
ppc64x, and riscv64.

Fixes golang/go#80837
Change-Id: I434fc7cfc6428686df9dfdcffac3938959a01776

Change diff

diff --git a/src/cmd/compile/internal/ssa/_gen/generic.rules b/src/cmd/compile/internal/ssa/_gen/generic.rules
index ea3611c..97a1165 100644
--- a/src/cmd/compile/internal/ssa/_gen/generic.rules
+++ b/src/cmd/compile/internal/ssa/_gen/generic.rules
@@ -810,6 +810,8 @@
(Sub(64|32|16|8) x (Add(64|32|16|8) x y)) => (Neg(64|32|16|8) y)
(Add(64|32|16|8) x (Sub(64|32|16|8) y x)) => y
(Add(64|32|16|8) x (Add(64|32|16|8) y (Sub(64|32|16|8) z x))) => (Add(64|32|16|8) y z)
+// x + (y - (x - z)) == y + z
+(Add(64|32|16|8) x (Sub(64|32|16|8) y (Sub(64|32|16|8) x z))) => (Add(64|32|16|8) y z)

// basic phi simplifications
(Phi (Const8 [c]) (Const8 [c])) => (Const8 [c])
diff --git a/src/cmd/compile/internal/ssa/rewritegeneric.go b/src/cmd/compile/internal/ssa/rewritegeneric.go
index 7d175ab..329005c 100644
--- a/src/cmd/compile/internal/ssa/rewritegeneric.go
+++ b/src/cmd/compile/internal/ssa/rewritegeneric.go
@@ -715,6 +715,30 @@
}
break
}
+ // match: (Add16 x (Sub16 y (Sub16 x z)))
+ // result: (Add16 y z)
+ for {
+ for _i0 := 0; _i0 <= 1; _i0, v_0, v_1 = _i0+1, v_1, v_0 {
+ x := v_0
+ if v_1.Op != OpSub16 {
+ continue
+ }
+ _ = v_1.Args[1]
+ y := v_1.Args[0]
+ v_1_1 := v_1.Args[1]
+ if v_1_1.Op != OpSub16 {
+ continue
+ }
+ z := v_1_1.Args[1]
+ if x != v_1_1.Args[0] {
+ continue
+ }
+ v.reset(OpAdd16)
+ v.AddArg2(y, z)
+ return true
+ }
+ break
+ }
// match: (Add16 (Add16 i:(Const16 <t>) z) x)
// cond: (z.Op != OpConst16 && x.Op != OpConst16)
// result: (Add16 i (Add16 <t> z x))
@@ -1341,6 +1365,30 @@
}
break
}
+ // match: (Add32 x (Sub32 y (Sub32 x z)))
+ // result: (Add32 y z)
+ for {
+ for _i0 := 0; _i0 <= 1; _i0, v_0, v_1 = _i0+1, v_1, v_0 {
+ x := v_0
+ if v_1.Op != OpSub32 {
+ continue
+ }
+ _ = v_1.Args[1]
+ y := v_1.Args[0]
+ v_1_1 := v_1.Args[1]
+ if v_1_1.Op != OpSub32 {
+ continue
+ }
+ z := v_1_1.Args[1]
+ if x != v_1_1.Args[0] {
+ continue
+ }
+ v.reset(OpAdd32)
+ v.AddArg2(y, z)
+ return true
+ }
+ break
+ }
// match: (Add32 (Add32 i:(Const32 <t>) z) x)
// cond: (z.Op != OpConst32 && x.Op != OpConst32)
// result: (Add32 i (Add32 <t> z x))
@@ -1994,6 +2042,30 @@
}
break
}
+ // match: (Add64 x (Sub64 y (Sub64 x z)))
+ // result: (Add64 y z)
+ for {
+ for _i0 := 0; _i0 <= 1; _i0, v_0, v_1 = _i0+1, v_1, v_0 {
+ x := v_0
+ if v_1.Op != OpSub64 {
+ continue
+ }
+ _ = v_1.Args[1]
+ y := v_1.Args[0]
+ v_1_1 := v_1.Args[1]
+ if v_1_1.Op != OpSub64 {
+ continue
+ }
+ z := v_1_1.Args[1]
+ if x != v_1_1.Args[0] {
+ continue
+ }
+ v.reset(OpAdd64)
+ v.AddArg2(y, z)
+ return true
+ }
+ break
+ }
// match: (Add64 (Add64 i:(Const64 <t>) z) x)
// cond: (z.Op != OpConst64 && x.Op != OpConst64)
// result: (Add64 i (Add64 <t> z x))
@@ -2685,6 +2757,30 @@
}
break
}
+ // match: (Add8 x (Sub8 y (Sub8 x z)))
+ // result: (Add8 y z)
+ for {
+ for _i0 := 0; _i0 <= 1; _i0, v_0, v_1 = _i0+1, v_1, v_0 {
+ x := v_0
+ if v_1.Op != OpSub8 {
+ continue
+ }
+ _ = v_1.Args[1]
+ y := v_1.Args[0]
+ v_1_1 := v_1.Args[1]
+ if v_1_1.Op != OpSub8 {
+ continue
+ }
+ z := v_1_1.Args[1]
+ if x != v_1_1.Args[0] {
+ continue
+ }
+ v.reset(OpAdd8)
+ v.AddArg2(y, z)
+ return true
+ }
+ break
+ }
// match: (Add8 (Add8 i:(Const8 <t>) z) x)
// cond: (z.Op != OpConst8 && x.Op != OpConst8)
// result: (Add8 i (Add8 <t> z x))
diff --git a/test/codegen/arithmetic.go b/test/codegen/arithmetic.go
index 71b4b88..b6a478e 100644
--- a/test/codegen/arithmetic.go
+++ b/test/codegen/arithmetic.go
@@ -220,6 +220,17 @@
return r
}

+func AddSubSubSimplify(a, b, c int) int {
+ // amd64:"LEAQ" -"SUBQ"
+ // arm64:"ADD" -"SUB"
+ // loong64:"ADDV" -"SUBV"
+ // mips:"ADD" -"SUB"
+ // mips64:"ADDV" -"SUBV"
+ // ppc64x:-"SUB"
+ // riscv64:"ADD" -"SUB"
+ return a + (b - (a - c))
+}
+
func NegToInt32(a int) int {
// riscv64: "NEGW" -"MOVW"
r := int(int32(-a))

Change information

Files:
  • M src/cmd/compile/internal/ssa/_gen/generic.rules
  • M src/cmd/compile/internal/ssa/rewritegeneric.go
  • M test/codegen/arithmetic.go
Change size: M
Delta: 3 files changed, 109 insertions(+), 0 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I434fc7cfc6428686df9dfdcffac3938959a01776
Gerrit-Change-Number: 813660
Gerrit-PatchSet: 1
Gerrit-Owner: shuang cui <imc...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

shuang cui (Gerrit)

unread,
Aug 11, 2026, 9:04:21 PM (6 hours ago) Aug 11
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

shuang cui voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I434fc7cfc6428686df9dfdcffac3938959a01776
Gerrit-Change-Number: 813660
Gerrit-PatchSet: 1
Gerrit-Owner: shuang cui <imc...@gmail.com>
Gerrit-Reviewer: shuang cui <imc...@gmail.com>
Gerrit-Comment-Date: Wed, 12 Aug 2026 01:04:11 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Jorropo (Gerrit)

unread,
Aug 11, 2026, 9:40:08 PM (6 hours ago) Aug 11
to shuang cui, goph...@pubsubhelper.golang.org, golang...@luci-project-accounts.iam.gserviceaccount.com, Keith Randall, Martin Möhrmann, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Keith Randall, Martin Möhrmann and shuang cui

Jorropo voted

Auto-Submit+1
Code-Review+2
Open in Gerrit

Related details

Attention is currently required from:
  • Keith Randall
  • Martin Möhrmann
  • shuang cui
Submit Requirements:
  • requirement satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I434fc7cfc6428686df9dfdcffac3938959a01776
Gerrit-Change-Number: 813660
Gerrit-PatchSet: 1
Gerrit-Owner: shuang cui <imc...@gmail.com>
Gerrit-Reviewer: Jorropo <jorro...@gmail.com>
Gerrit-Reviewer: Keith Randall <k...@golang.org>
Gerrit-Reviewer: Martin Möhrmann <moeh...@google.com>
Gerrit-Reviewer: shuang cui <imc...@gmail.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: Keith Randall <k...@golang.org>
Gerrit-Attention: Martin Möhrmann <moeh...@google.com>
Gerrit-Attention: shuang cui <imc...@gmail.com>
Gerrit-Comment-Date: Wed, 12 Aug 2026 01:40:00 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
satisfied_requirement
unsatisfied_requirement
open
diffy

Jorropo (Gerrit)

unread,
Aug 11, 2026, 11:40:04 PM (4 hours ago) Aug 11
to shuang cui, goph...@pubsubhelper.golang.org, golang...@luci-project-accounts.iam.gserviceaccount.com, Keith Randall, Martin Möhrmann, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Keith Randall, Martin Möhrmann and shuang cui

Jorropo added 1 comment

Commit Message
Line 35, Patchset 1 (Latest):Fixes golang/go#80837
Jorropo . unresolved

```suggestion
Fixes #80837
```

Open in Gerrit

Related details

Attention is currently required from:
  • Keith Randall
  • Martin Möhrmann
  • shuang cui
Submit Requirements:
  • requirement satisfiedCode-Review
  • requirement is not satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I434fc7cfc6428686df9dfdcffac3938959a01776
Gerrit-Change-Number: 813660
Gerrit-PatchSet: 1
Gerrit-Owner: shuang cui <imc...@gmail.com>
Gerrit-Reviewer: Jorropo <jorro...@gmail.com>
Gerrit-Reviewer: Keith Randall <k...@golang.org>
Gerrit-Reviewer: Martin Möhrmann <moeh...@google.com>
Gerrit-Reviewer: shuang cui <imc...@gmail.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: Keith Randall <k...@golang.org>
Gerrit-Attention: Martin Möhrmann <moeh...@google.com>
Gerrit-Attention: shuang cui <imc...@gmail.com>
Gerrit-Comment-Date: Wed, 12 Aug 2026 03:39:55 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
satisfied_requirement
unsatisfied_requirement
open
diffy

shuang cui (Gerrit)

unread,
Aug 11, 2026, 11:47:27 PM (4 hours ago) Aug 11
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
Attention needed from Jorropo, Keith Randall, Martin Möhrmann and shuang cui

shuang cui uploaded new patchset

shuang cui uploaded patch set #2 to this change.
Open in Gerrit

Related details

Attention is currently required from:
  • Jorropo
  • Keith Randall
  • Martin Möhrmann
  • shuang cui
Submit Requirements:
  • requirement satisfiedCode-Review
  • requirement is not satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newpatchset
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I434fc7cfc6428686df9dfdcffac3938959a01776
Gerrit-Change-Number: 813660
Gerrit-PatchSet: 2
Gerrit-Owner: shuang cui <imc...@gmail.com>
Gerrit-Reviewer: Jorropo <jorro...@gmail.com>
Gerrit-Reviewer: Keith Randall <k...@golang.org>
Gerrit-Reviewer: Martin Möhrmann <moeh...@google.com>
Gerrit-Reviewer: shuang cui <imc...@gmail.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: Keith Randall <k...@golang.org>
Gerrit-Attention: Jorropo <jorro...@gmail.com>
satisfied_requirement
unsatisfied_requirement
open
diffy

shuang cui (Gerrit)

unread,
Aug 11, 2026, 11:48:53 PM (4 hours ago) Aug 11
to goph...@pubsubhelper.golang.org, Jorropo, golang...@luci-project-accounts.iam.gserviceaccount.com, Keith Randall, Martin Möhrmann, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Jorropo, Keith Randall and Martin Möhrmann

shuang cui added 1 comment

Commit Message
Line 35, Patchset 1:Fixes golang/go#80837
Jorropo . resolved

```suggestion
Fixes #80837
```

shuang cui

Thanks. Modified.

Open in Gerrit

Related details

Attention is currently required from:
  • Jorropo
  • Keith Randall
  • Martin Möhrmann
Submit Requirements:
  • requirement satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I434fc7cfc6428686df9dfdcffac3938959a01776
Gerrit-Change-Number: 813660
Gerrit-PatchSet: 2
Gerrit-Owner: shuang cui <imc...@gmail.com>
Gerrit-Reviewer: Jorropo <jorro...@gmail.com>
Gerrit-Reviewer: Keith Randall <k...@golang.org>
Gerrit-Reviewer: Martin Möhrmann <moeh...@google.com>
Gerrit-Reviewer: shuang cui <imc...@gmail.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: Keith Randall <k...@golang.org>
Gerrit-Attention: Jorropo <jorro...@gmail.com>
Gerrit-Attention: Martin Möhrmann <moeh...@google.com>
Gerrit-Comment-Date: Wed, 12 Aug 2026 03:48:41 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Jorropo <jorro...@gmail.com>
satisfied_requirement
unsatisfied_requirement
open
diffy

Jorropo (Gerrit)

unread,
12:07 AM (3 hours ago) 12:07 AM
to shuang cui, goph...@pubsubhelper.golang.org, golang...@luci-project-accounts.iam.gserviceaccount.com, Keith Randall, Martin Möhrmann, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Keith Randall, Martin Möhrmann and shuang cui

Jorropo voted

Auto-Submit+1
Code-Review+2
Open in Gerrit

Related details

Attention is currently required from:
  • Keith Randall
  • Martin Möhrmann
  • shuang cui
Submit Requirements:
  • requirement satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I434fc7cfc6428686df9dfdcffac3938959a01776
Gerrit-Change-Number: 813660
Gerrit-PatchSet: 2
Gerrit-Owner: shuang cui <imc...@gmail.com>
Gerrit-Reviewer: Jorropo <jorro...@gmail.com>
Gerrit-Reviewer: Keith Randall <k...@golang.org>
Gerrit-Reviewer: Martin Möhrmann <moeh...@google.com>
Gerrit-Reviewer: shuang cui <imc...@gmail.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: Keith Randall <k...@golang.org>
Gerrit-Attention: Martin Möhrmann <moeh...@google.com>
Gerrit-Attention: shuang cui <imc...@gmail.com>
Gerrit-Comment-Date: Wed, 12 Aug 2026 04:07:02 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
satisfied_requirement
unsatisfied_requirement
open
diffy
Reply all
Reply to author
Forward
0 new messages