go cmd/compile: flagalloc 不会删除死指令

mccptt67  于 4个月前  发布在  Go
关注(0)|答案(4)|浏览(52)

Go版本
go版本 devel go1.22-b7c630dc3a Tue Jan 9 01:36:54 2024 +0000 linux/amd64

在你的模块/工作区中go env的输出:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/dominikh/.cache/go-build'
GOENV='/home/dominikh/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/dominikh/prj/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/dominikh/prj'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/dominikh/prj/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/dominikh/prj/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='devel go1.22-b7c630dc3a Tue Jan 9 01:36:54 2024 +0000'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/dominikh/prj/src/example.com/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1294566987=/tmp/go-build -gno-record-gcc-switches'

你做了什么?

编译 https://go.dev/play/p/4RMX7fsEHw9

package pkg

import (
	"math/bits"
)

var Sink uint

func Foo(x uint64) {
	log2 := uint(63 - bits.LeadingZeros64(x))
	if x&(x-1) != 0 {
		log2++
	}
	Sink = log2
}

你看到了什么发生?

生成的汇编代码在0x0005处包含一个死掉的TESTQ指令。

command-line-arguments.Foo STEXT nosplit size=42 args=0x8 locals=0x0 funcid=0x0 align=0x0
	0x0000 00000 (/home/dominikh/prj/src/example.com/foo.go:9)	TEXT	command-line-arguments.Foo(SB), NOSPLIT|NOFRAME|ABIInternal, $0-8
	0x0000 00000 (/home/dominikh/prj/src/example.com/foo.go:9)	FUNCDATA	$0, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
	0x0000 00000 (/home/dominikh/prj/src/example.com/foo.go:9)	FUNCDATA	$1, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
	0x0000 00000 (/home/dominikh/prj/src/example.com/foo.go:9)	FUNCDATA	$5, command-line-arguments.Foo.arginfo1(SB)
	0x0000 00000 (/home/dominikh/prj/src/example.com/foo.go:9)	FUNCDATA	$6, command-line-arguments.Foo.argliveinfo(SB)
	0x0000 00000 (/home/dominikh/prj/src/example.com/foo.go:9)	PCDATA	$3, $1
	0x0000 00000 (/home/dominikh/prj/src/example.com/foo.go:11)	LEAQ	-1(AX), CX
	0x0004 00004 (/home/dominikh/prj/src/example.com/foo.go:10)	XCHGL	AX, AX
	0x0005 00005 (/home/dominikh/prj/src/example.com/foo.go:11)	TESTQ	AX, CX
	0x0008 00008 (/home/dominikh/prj/go/src/math/bits/bits.go:37)	BSRQ	AX, DX
	0x000c 00012 (/home/dominikh/prj/go/src/math/bits/bits.go:37)	MOVQ	$-1, BX
	0x0013 00019 (/home/dominikh/prj/go/src/math/bits/bits.go:37)	CMOVQEQ	BX, DX
	0x0017 00023 (/home/dominikh/prj/src/example.com/foo.go:12)	LEAQ	1(DX), BX
	0x001b 00027 (/home/dominikh/prj/src/example.com/foo.go:11)	TESTQ	AX, CX
	0x001e 00030 (/home/dominikh/prj/src/example.com/foo.go:14)	CMOVQNE	BX, DX
	0x0022 00034 (/home/dominikh/prj/src/example.com/foo.go:14)	MOVQ	DX, command-line-arguments.Sink(SB)
	0x0029 00041 (/home/dominikh/prj/src/example.com/foo.go:11)	RET

你期望看到什么?

CMOVQNE在0x001e处使用的ZF由TESTQ AX, CX设置。由TESTQ在0x0005处设置的ZF立即被随后的BSRQ清除。flagalloc在0x001b处插入了第二个TESTQ,但没有清理旧的TESTQ,现在它已经死了。

gojuced7

gojuced71#

相关:#24537 (评论)

50few1ms

50few1ms2#

CC @golang/compiler.

rqdpfwrv

rqdpfwrv3#

有人声称过这个吗?或者我可以尝试一下吗?

sigwle7e

sigwle7e4#

@kungfukennyg go for it.

相关问题