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
marigonzes opened this Issue Jan 23, 2019 · 2 comments

Comments

Projects
None yet
3 participants
@marigonzes
Copy link

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?

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

This comment has been minimized.

Copy link
Contributor

randall77 commented Jan 23, 2019

The extra copy with unnamed return values is #14762.

The unneeded zeroing might be #25132 or #24926.

@FiloSottile

This comment has been minimized.

Copy link
Member

FiloSottile commented Jan 23, 2019

@randall77 close as duplicate then?

@randall77 randall77 closed this Jan 23, 2019

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