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
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance
Milestone

Comments

@randall77
Copy link
Contributor

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
@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 30, 2020
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance
Projects
None yet
Development

No branches or pull requests

3 participants