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: optimize conditional pointer assignment #34020

Open
marigonzes opened this issue Sep 2, 2019 · 2 comments

Comments

@marigonzes
Copy link

commented Sep 2, 2019

What version of Go are you using (go version)?

$ go version
go version devel +d15dfdc Sun Sep 1 02:31:50 2019 +0000 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What did you do?

I compiled the following function (https://godbolt.org/z/rRkMVJ):

func fn(cond bool) int {
	a := new(int)
	if cond {
		*a = 1
	} else {
		*a = 2
	}
    return *a
}

What did you expect to see?

I expected that the generated instructions would be similar to https://godbolt.org/z/q0k3NT

What did you see instead?

Instead, there are MOV instructions to temporary stack variables and there is no use of the CMOV instruction.

@ALTree

This comment has been minimized.

Copy link
Member

commented Sep 2, 2019

Why would you write code like that? I mean, yeah, it could be compiled more efficiently... but optimizations generally 1) increase the complexity of the compiler's code 2) have the potential to slow down the compiler; so in general it's only worth to optimize reasonable code patterns that could appear in real-world code. Is this the case?

@marigonzes

This comment has been minimized.

Copy link
Author

commented Sep 2, 2019

You are probably right, but I found this while looking at an example published in Slack (https://godbolt.org/z/kofqr2). After playing around with the code, I ended up with the above function and opened this issue.
Please close it if you don't think this kind of code appears in production 😉

@ALTree ALTree added this to the Unplanned milestone Sep 2, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.