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: similar returns not optimized #32554

Open
marigonzes opened this issue Jun 11, 2019 · 2 comments

Comments

@marigonzes
Copy link

commented Jun 11, 2019

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

$ go version
go version devel +323212b Sun Jun 9 16:23:11 2019 +0000 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What did you do?

I compiled the following function:

func f1(x int, b bool) int {
	if b {
		return x
	}
	return x
}

What did you expect to see?

I expected it to be compiled to a move and a return, given that the return value is always the same.

What did you see instead?

Instead, it was compiled to:

        movblzx "".b+16(SP), AX
        testb   AL, AL
        jeq     f1_pc20
        movq    "".x+8(SP), AX
        movq    AX, "".~r2+24(SP)
        ret
f1_pc20:
        movq    "".x+8(SP), AX
        movq    AX, "".~r2+24(SP)
        ret

I think the compiler should recognize both cases to be the same.

Note: if this function is called from another one, the optimization is applied. For example:

func f2(x int, b bool) int {
    return f1(x, b)
}

is compiled to

        movq    "".x+8(SP), AX
        movq    AX, "".~r2+24(SP)
        ret
@randall77

This comment has been minimized.

Copy link
Contributor

commented Jun 11, 2019

Yep, this is a TODO. I even have a CL for it: https://go-review.googlesource.com/c/go/+/33634
I never got around to actually finishing it up.

@randall77 randall77 added this to the Go1.14 milestone Jun 11, 2019
@MichaelTJones

This comment has been minimized.

Copy link
Contributor

commented Jun 13, 2019

Is the underlying issue different than the split/common approach in your CL? That seems a late stage remedy to what should already have been rewritten away by a "Moot Code Elimination" pass.

@rsc rsc modified the milestones: Go1.14, Backlog Oct 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.