Closed
Description
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.
Metadata
Metadata
Assignees
Labels
Type
Projects
Relationships
Development
No branches or pull requests
Activity
randall77 commentedon Jan 23, 2019
The extra copy with unnamed return values is #14762.
The unneeded zeroing might be #25132 or #24926.
FiloSottile commentedon Jan 23, 2019
@randall77 close as duplicate then?