Repository navigation
Description
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
RETThe equivalent function:
//go:noinline
func addSubCancelOpt(x, y, z uint64) uint64 {
return y + z
}emits:
ADD R2, R1, R0
RETAt the generic SSA level, the original expression has the form:
v1 = Sub64 x z
v2 = Sub64 y v1
v3 = Add64 x v2The 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:
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 + zTherefore 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.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
gopherbot commentedon Aug 12, 2026
Change https://go.dev/cl/813660 mentions this issue:
cmd/compile: fold x + (y - (x - z)) to y + zgabyhelp commentedon Aug 12, 2026
Related Issues
Related Code Changes
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
cmd/compile: fold x + (y - (x - z)) to y + z