Skip to content

cmd/compile: pointless static copy code #17113

@josharian

Description

@josharian
package main

type I int

var (
    i int
    x = I(i)
)

compiles to:

".init t=1 size=72 args=0x0 locals=0x0
    0x0000 00000 (issue17111.go:17) TEXT    "".init(SB), $0-0
    0x0000 00000 (issue17111.go:17) MOVQ    (TLS), CX
    0x0009 00009 (issue17111.go:17) CMPQ    SP, 16(CX)
    0x000d 00013 (issue17111.go:17) JLS 65
    0x000f 00015 (issue17111.go:17) FUNCDATA    $0, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
    0x000f 00015 (issue17111.go:17) FUNCDATA    $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
    0x000f 00015 (issue17111.go:17) MOVBLZX "".initdone·(SB), AX
    0x0016 00022 (issue17111.go:17) CMPB    AL, $1
    0x0018 00024 (issue17111.go:17) JLS $0, 27
    0x001a 00026 (issue17111.go:17) RET
    0x001b 00027 (issue17111.go:17) JNE $0, 36
    0x001d 00029 (issue17111.go:17) PCDATA  $0, $0
    0x001d 00029 (issue17111.go:17) CALL    runtime.throwinit(SB)
    0x0022 00034 (issue17111.go:17) UNDEF
    0x0024 00036 (issue17111.go:17) MOVB    $1, "".initdone·(SB)
    0x002b 00043 (issue17111.go:13) MOVQ    "".i(SB), AX
    0x0032 00050 (issue17111.go:13) MOVQ    AX, "".x(SB)
    0x0039 00057 (issue17111.go:17) MOVB    $2, "".initdone·(SB)
    0x0040 00064 (issue17111.go:17) RET
    0x0041 00065 (issue17111.go:17) NOP
    0x0041 00065 (issue17111.go:17) CALL    runtime.morestack_noctxt(SB)
    0x0046 00070 (issue17111.go:17) JMP 0
    0x0000 65 48 8b 0c 25 00 00 00 00 48 3b 61 10 76 32 0f  eH..%....H;a.v2.
    0x0010 b6 05 00 00 00 00 3c 01 76 01 c3 75 07 e8 00 00  ......<.v..u....
    0x0020 00 00 0f 0b c6 05 00 00 00 00 01 48 8b 05 00 00  ...........H....
    0x0030 00 00 48 89 05 00 00 00 00 c6 05 00 00 00 00 02  ..H.............
    0x0040 c3 e8 00 00 00 00 eb b8                          ........
    rel 5+4 t=15 TLS+0
    rel 18+4 t=14 "".initdone·+0
    rel 30+4 t=7 runtime.throwinit+0
    rel 38+4 t=14 "".initdone·+-1
    rel 46+4 t=14 "".i+0
    rel 53+4 t=14 "".x+0
    rel 59+4 t=14 "".initdone·+-1
    rel 66+4 t=7 runtime.morestack_noctxt+0
gclocals·33cdeccccebe80329f1fdbee7f5874cb t=9 dupok size=8
    0x0000 01 00 00 00 00 00 00 00                          ........
go.info."".init t=47 size=27
    0x0000 02 22 22 2e 69 6e 69 74 00 00 00 00 00 00 00 00  ."".init........
    0x0010 00 00 00 00 00 00 00 00 00 01 00                 ...........
    rel 9+8 t=1 "".init+0
    rel 17+8 t=1 "".init+72
"".i t=34 size=8
"".x t=34 size=8
"".initdone· t=34 size=1
"".init·f t=9 dupok size=8
    0x0000 00 00 00 00 00 00 00 00                          ........
    rel 0+8 t=1 "".init+0
runtime.gcbits.01 t=9 dupok size=1
    0x0000 01                                               .
type..namedata.*main.I. t=9 dupok size=10
    0x0000 01 00 07 2a 6d 61 69 6e 2e 49                    ...*main.I
type.*"".I t=9 size=56
    0x0000 08 00 00 00 00 00 00 00 08 00 00 00 00 00 00 00  ................
    0x0010 d3 75 f9 8b 00 08 08 36 00 00 00 00 00 00 00 00  .u.....6........
    0x0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x0030 00 00 00 00 00 00 00 00                          ........
    rel 24+8 t=1 runtime.algarray+80
    rel 32+8 t=1 runtime.gcbits.01+0
    rel 40+4 t=5 type..namedata.*main.I.+0
    rel 48+8 t=1 type."".I+0
go.typelink.*"".I t=9 size=4
    0x0000 00 00 00 00                                      ....
    rel 0+4 t=5 type.*"".I+0
runtime.gcbits. t=9 dupok size=0
type."".I t=9 size=64
    0x0000 08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x0010 d1 cd b2 de 07 08 08 82 00 00 00 00 00 00 00 00  ................
    0x0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    0x0030 00 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00  ................
    rel 24+8 t=1 runtime.algarray+80
    rel 32+8 t=1 runtime.gcbits.+0
    rel 40+4 t=5 type..namedata.*main.I.+0
    rel 44+4 t=5 type.*"".I+0
    rel 48+4 t=5 type..importpath."".+0

Observe that we generate init code to copy i to x, even though x could be entirely staticly initialized.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions