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: unnecessary instructions generated #29892

Closed
mariecurried opened this issue Jan 23, 2019 · 2 comments
Closed

cmd/compile: unnecessary instructions generated #29892

mariecurried opened this issue Jan 23, 2019 · 2 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@mariecurried
Copy link

mariecurried commented Jan 23, 2019

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

$ go version
go version go1.11.4 windows/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

I compiled the following code to assembly:

package test

import "encoding/binary"

func test(x uint64) [8]byte {
    var out [8]byte
    binary.LittleEndian.PutUint64(out[:], x)
    return out
}

Assembly code generated:

movq    $0, "".~r1+32(SP)
movq    $0, "".out(SP)
movq    "".x+24(SP), AX
movq    AX, "".out(SP)
movq    "".out(SP), AX
movq    AX, "".~r1+32(SP)
movq    8(SP), BP
addq    $16, SP
ret

What did you expect to see?

I was expecting the generated code to be much simpler

What did you see instead?

The assembly code shown above has many unnecessary instructions.
A solution I found was to use a named return. When I do that, the assembly code becomes:

movq    $0, "".out+16(SP)
movq    "".x+8(SP), AX
movq    AX, "".out+16(SP)
ret

In spite of being much better, it also has an extra instruction, the first one, where "out" is being zeroed.

@randall77
Copy link
Contributor

The extra copy with unnamed return values is #14762.

The unneeded zeroing might be #25132 or #24926.

@FiloSottile FiloSottile added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 23, 2019
@FiloSottile FiloSottile added this to the Go1.13 milestone Jan 23, 2019
@FiloSottile
Copy link
Contributor

@randall77 close as duplicate then?

@golang golang locked and limited conversation to collaborators Jan 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants