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

marigonzes opened this issue Jan 23, 2019 · 2 comments

cmd/compile: unnecessary instructions generated #29892

marigonzes opened this issue Jan 23, 2019 · 2 comments


Copy link

@marigonzes marigonzes 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?


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

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)

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

Copy link

@randall77 randall77 commented Jan 23, 2019

The extra copy with unnamed return values is #14762.

The unneeded zeroing might be #25132 or #24926.

Copy link

@FiloSottile FiloSottile commented Jan 23, 2019

@randall77 close as duplicate then?

@randall77 randall77 closed this Jan 23, 2019
@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.
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants