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
mariecurried opened this issue Jun 11, 2019 · 2 comments
Open

cmd/compile: similar returns not optimized #32554

mariecurried opened this issue Jun 11, 2019 · 2 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsFix The path to resolution is known, but the work has not been done. Performance
Milestone

Comments

@mariecurried
Copy link

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
Copy link
Contributor

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
Copy link
Contributor

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.

@FiloSottile FiloSottile added the NeedsFix The path to resolution is known, but the work has not been done. label Jun 14, 2019
@rsc rsc modified the milestones: Go1.14, Backlog Oct 9, 2019
@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. NeedsFix The path to resolution is known, but the work has not been done. Performance
Projects
Status: Triage Backlog
Development

No branches or pull requests

6 participants