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: missed add+load combination opportunity #42672

randall77 opened this issue Nov 17, 2020 · 0 comments

cmd/compile: missed add+load combination opportunity #42672

randall77 opened this issue Nov 17, 2020 · 0 comments


Copy link

@randall77 randall77 commented Nov 17, 2020

func f(a []int) int {
	s := 0
	for _, x := range a {
		s += x
	return s

Generates for the inner loop

		MOVQ	(AX)(DX*8), SI
		JGT	16

There's no reason we couldn't do ADDQ (AX)(DX*8), BX instead of the MOVQ/ADDQ combo.

There's actually code that does this combining optimization in the compiler, but it doesn't trigger. The difficulty is that the combined ADDQ instruction will clobber its input, so we don't combine when the non-load argument to the ADDQ is used somewhere else (see rewrite.go:canMergeLoadClobber). In this case the other use of BX is at the return statement. But that use shouldn't stop the optimization from happening, as it is in a separate branch. At the actual point where we're doing the optimization, the old value of BX is dead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants