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

Open
randall77 opened this issue Nov 17, 2020 · 0 comments
Open

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

randall77 opened this issue Nov 17, 2020 · 0 comments
Labels
Milestone

Comments

@randall77
Copy link
Contributor

@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
		INCQ	DX
		ADDQ	SI, BX
		CMPQ	CX, DX
		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.

@randall77 randall77 added this to the Unplanned milestone Nov 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.