Skip to content

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

Closed

Description

@cuishuang

Go version

Go tip (commit d758038)

Output of go env in your module/workspace:

go version go1.28-devel_d758038bdf Tue Aug 11 10:10:28 2026 -0700 darwin/arm64

This is unrelated to the environment variable; it's not a bug, but rather an optimization.


AR='ar'
CC='clang'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='clang++'
GCCGO='gccgo'
GO111MODULE='on'
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN='/Users/shuang/go/bin'
GOCACHE='/Users/shuang/Library/Caches/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/Users/shuang/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/tq/xxxxxxxx/T/go-build1xxxxxxxx=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/dev/null'
GOMODCACHE='/Users/shuang/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPACKAGESDRIVER=''
GOPATH='/Users/shuang/go'
GOPRIVATE=''
GOPROXY='https://goproxy.cn,direct'
GOROOT='/Users/shuang/2026/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/shuang/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/shuang/2026/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.28-devel_d758038bdf Tue Aug 11 10:10:28 2026 -0700'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

Compile the following function with go tool compile -S addsubcancel.go:

addsubcancel.go:

package main

//go:noinline
func addSubCancel(x, y, z uint64) uint64 {
	return x + (y - (x - z))
}

func main() {}

What did you see happen?

At Go tip, the arm64 compiler emits the following for addSubCancel:

ADD    R1, R2, R1
SUB    R0, R1, R1
ADD    R0, R1, R0
RET

The equivalent function:

//go:noinline
func addSubCancelOpt(x, y, z uint64) uint64 {
	return y + z
}

emits:

ADD    R2, R1, R0
RET

At the generic SSA level, the original expression has the form:

v1 = Sub64 x z
v2 = Sub64 y v1
v3 = Add64 x v2

The current generic rewrite rules do not reduce this form to y + z.
The same expression occurs in non-test production code in segmentio/encoding/json.encodeBytes:

https://github.com/segmentio/encoding/blob/7a0606c75807b35f13b92dcbb6bf7d098631865c/json/encode.go#L247-L252

n := base64.StdEncoding.EncodedLen(len(v)) + 2

if avail := cap(b) - len(b); avail < n {
	newB := make([]byte, cap(b)+(n-avail))
	// ...
}

The allocation length contains: cap(b) + (n - (cap(b) - len(b)))

which is equivalent to: n + len(b)

What did you expect to see?

The generic SSA optimizer should apply the following integer rewrite:

// 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)

The identity is valid modulo 2^W:

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

Therefore it remains valid when intermediate fixed-width integer operations overflow.

The rewrite only applies to integer Add8,Add16, Add32, and Add64 operations and their corresponding Sub operations. It does not apply to floating-point operations or carry/borrow-producing operations.

The cap(b) - len(b) computation in the motivating example must remain because it is also used by the branch condition; only the redundant arithmetic used to compute the allocation length should be removed.

This issue is related to #80320 and CL 798780, but this exact rewrite is not present in the current generic rules or in the current CL patchset.

Activity

  1. gopherbot commented on Aug 12, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/813660 mentions this issue: cmd/compile: fold x + (y - (x - z)) to y + z

  2. added
    FixPendingIssues that have a fix which has not yet been reviewed or submitted.
    on Aug 12, 2026
  3. added this to the Go1.28 milestone on Aug 12, 2026
  4. added a commit that references this issue on Aug 12, 2026
    82a8bf1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixPendingIssues that have a fix which has not yet been reviewed or submitted.Performancecompiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions