Skip to content

cmd/compile: unnecessary instructions generated #29892

Closed
@mariecurried

Description

@mariecurried

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.

Activity

randall77

randall77 commented on Jan 23, 2019

@randall77
Contributor

The extra copy with unnamed return values is #14762.

The unneeded zeroing might be #25132 or #24926.

added
NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
on Jan 23, 2019
added this to the Go1.13 milestone on Jan 23, 2019
FiloSottile

FiloSottile commented on Jan 23, 2019

@FiloSottile
Contributor

@randall77 close as duplicate then?

locked and limited conversation to collaborators on Jan 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @FiloSottile@randall77@gopherbot@mariecurried

        Issue actions

          cmd/compile: unnecessary instructions generated · Issue #29892 · golang/go