Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: inefficient compilation of (64 - x) #20677

josharian opened this issue Jun 14, 2017 · 1 comment


None yet
2 participants
Copy link

commented Jun 14, 2017

package p

var sink uint8

func BenchmarkSub(b *testing.B) {
	for i := 0; i < b.N; i++ {
		sink = 64 - sink

Currently, the core of the inner loop compiles to:

	0x0009 00009 (/Users/josh/src/mask_test.go:29)	MOVBLZX	"".sink(SB), DX
	0x0010 00016 (/Users/josh/src/mask_test.go:29)	ADDL	$-64, DX
	0x0013 00019 (/Users/josh/src/mask_test.go:29)	NEGL	DX
	0x0015 00021 (/Users/josh/src/mask_test.go:29)	MOVB	DL, "".sink(SB)

Disabling a few optimizations allows this to revert back to:

	0x0009 00009 (/Users/josh/src/mask_test.go:29)	MOVBLZX	"".sink(SB), DX
	0x0010 00016 (/Users/josh/src/mask_test.go:29)	MOVL	$64, BX
	0x0015 00021 (/Users/josh/src/mask_test.go:29)	SUBL	DX, BX
	0x0017 00023 (/Users/josh/src/mask_test.go:29)	MOVB	BL, "".sink(SB)

It is two bytes longer, but executes faster: 1.60 ns vs 2.06 ns. (Both measurements are extremely consistent.)

Perhaps we should remove the optimizations, or perhaps we should make them contingent on something (what?).

cc @randall77

@josharian josharian added this to the Go1.10 milestone Jun 14, 2017


This comment has been minimized.

Copy link
Contributor Author

commented Jun 14, 2017

I guess the existing code is not just shorter, it only requires a single register. I'll find a different attack on this code.

@josharian josharian closed this Jun 14, 2017

@golang golang locked and limited conversation to collaborators Jun 14, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.