Skip to content

cmd/compile: known bits and prove constant folding can impede memcombine #79195

Description

@Jorropo

Go version

go version go1.27-devel_53bcf27018 Tue May 5 01:08:30 2026 +0200 linux/amd64

Output of go env in your module/workspace:

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v3'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/tmp/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/hugo/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build2419463057=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/home/hugo/k/go/src/go.mod'
GOMODCACHE='/home/hugo/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/hugo/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/hugo/k/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/hugo/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='local'
GOTOOLDIR='/home/hugo/k/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.27-devel_53bcf27018 Tue May 5 01:08:30 2026 +0200'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

Add theses two tests to test/codegen/memcombine.go:

func store_le_byte_8_known_bits(b []byte, val uint64) {
	_ = b[8]
	val &= 0xff00ff00ff00ff00
	val |= 0x0012003400560078
	// arm64:`MOVD R[0-9]+, 1\(R[0-9]+\)` -`MOVB` -`MOVH` -`MOVW`
	// amd64:`MOVQ [A-Z]+, 1\([A-Z]+\)` -`MOVB` -`MOVW` -`MOVL`
	// ppc64le:`MOVD ` -`MOVW`
	// ppc64:`MOVDBR `
	b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8] = byte(val), byte(val>>8), byte(val>>16), byte(val>>24), byte(val>>32), byte(val>>40), byte(val>>48), byte(val>>56)
}

func store_be_byte_8_known_bits(b []byte, val uint64) {
	_ = b[8]
	val &= 0xff00ff00ff00ff00
	val |= 0x0012003400560078
	// arm64:`REV` `MOVD R[0-9]+, 1\(R[0-9]+\)` -`MOVB` -`MOVH` -`MOVW` -`REV16W` -`REVW`
	// amd64/v1,amd64/v2:`MOVQ [A-Z]+, 1\([A-Z]+\)` -`MOVB` -`MOVW` -`MOVL`
	// amd64/v3:`MOVBEQ [A-Z]+, 1\([A-Z]+\)` -`MOVBEL`
	// ppc64le:`MOVDBR`
	// ppc64:`MOVD`
	b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8] = byte(val>>56), byte(val>>48), byte(val>>40), byte(val>>32), byte(val>>24), byte(val>>16), byte(val>>8), byte(val)
}

The optimal solutions include a 64 bits store.

However the problem is that we run known bits before memcombine.
So known bits sees byte(...) operations it knows the result to and replace them with a byte store from a constant.

This removes the information memcombine needs to do her work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performancecompiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    Todo

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions