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: omit zeroing of named return value when possible #4750

Open
rsc opened this issue Feb 3, 2013 · 8 comments
Open

cmd/compile: omit zeroing of named return value when possible #4750

rsc opened this issue Feb 3, 2013 · 8 comments
Milestone

Comments

@rsc
Copy link
Contributor

rsc commented Feb 3, 2013

In the program below, f1 and f2 should ideally generate the same code: if a return
variable name is never mentioned in the function and there are no returns without
arguments and no defer statements, the variable need not be cleared on entry:

g% cat x.go
package main

type T [16]int

func f1() (t T) {
    return g()
}

func f2() T {
    return g()
}

func g() T
g% go tool 6g -S x.go

--- prog list "f1" ---
0000 (x.go:5) TEXT    f1+0(SB),$256-128
0001 (x.go:5) LEAQ    t+0(FP),DI
0002 (x.go:5) MOVQ    $0,AX
0003 (x.go:5) MOVQ    $16,CX
0004 (x.go:5) REP     ,
0005 (x.go:5) STOSQ   ,
0006 (x.go:6) CALL    ,g+0(SB)
0007 (x.go:6) LEAQ    (SP),BX
0008 (x.go:6) LEAQ    autotmp_0000+-128(SP),BP
0009 (x.go:6) MOVQ    BP,DI
0010 (x.go:6) MOVQ    BX,SI
0011 (x.go:6) MOVQ    $16,CX
0012 (x.go:6) REP     ,
0013 (x.go:6) MOVSQ   ,
0014 (x.go:6) LEAQ    autotmp_0000+-128(SP),BX
0015 (x.go:6) LEAQ    t+0(FP),BP
0016 (x.go:6) MOVQ    BP,DI
0017 (x.go:6) MOVQ    BX,SI
0018 (x.go:6) MOVQ    $16,CX
0019 (x.go:6) REP     ,
0020 (x.go:6) MOVSQ   ,
0021 (x.go:6) RET     ,

--- prog list "f2" ---
0022 (x.go:9) TEXT    f2+0(SB),$256-128
0023 (x.go:10) CALL    ,g+0(SB)
0024 (x.go:10) LEAQ    (SP),BX
0025 (x.go:10) LEAQ    autotmp_0001+-128(SP),BP
0026 (x.go:10) MOVQ    BP,DI
0027 (x.go:10) MOVQ    BX,SI
0028 (x.go:10) MOVQ    $16,CX
0029 (x.go:10) REP     ,
0030 (x.go:10) MOVSQ   ,
0031 (x.go:10) LEAQ    autotmp_0001+-128(SP),BX
0032 (x.go:10) LEAQ    .noname+0(FP),BP
0033 (x.go:10) MOVQ    BP,DI
0034 (x.go:10) MOVQ    BX,SI
0035 (x.go:10) MOVQ    $16,CX
0036 (x.go:10) REP     ,
0037 (x.go:10) MOVSQ   ,
0038 (x.go:10) RET     ,

--- prog list "init" ---
0039 (x.go:13) TEXT    init+0(SB),$0-0
0040 (x.go:13) MOVBQZX initdone·+0(SB),AX
0041 (x.go:13) CMPB    AX,$0
0042 (x.go:13) JEQ     ,48
0043 (x.go:13) CMPB    AX,$2
0044 (x.go:13) JNE     ,46
0045 (x.go:13) RET     ,
0046 (x.go:13) CALL    ,runtime.throwinit+0(SB)
0047 (x.go:13) UNDEF   ,
0048 (x.go:13) MOVB    $2,initdone·+0(SB)
0049 (x.go:13) RET     ,
g%
@robpike
Copy link
Contributor

robpike commented Mar 7, 2013

Comment 1:

Labels changed: removed go1.1maybe.

@rsc
Copy link
Contributor Author

rsc commented Jul 30, 2013

Comment 2:

Labels changed: added go1.3.

@robpike
Copy link
Contributor

robpike commented Aug 20, 2013

Comment 3:

Labels changed: removed go1.3.

@rsc
Copy link
Contributor Author

rsc commented Nov 27, 2013

Comment 4:

Labels changed: added go1.3maybe.

@rsc
Copy link
Contributor Author

rsc commented Dec 4, 2013

Comment 5:

Labels changed: added release-none, removed go1.3maybe.

@rsc
Copy link
Contributor Author

rsc commented Dec 4, 2013

Comment 6:

Labels changed: added repo-main.

@rsc rsc added this to the Unplanned milestone Apr 10, 2015
@rsc rsc changed the title cmd/gc: omit zeroing of named return value when possible cmd/compile: omit zeroing of named return value when possible Jun 8, 2015
@navytux
Copy link
Contributor

navytux commented Mar 6, 2017

Current state:

go version
go version devel +2ec77d3457 Mon Mar 6 05:07:06 2017 +0000 linux/amd64
$ go tool compile -S x.go
"".f1 t=1 size=176 args=0x80 locals=0x108
        0x0000 00000 (x.go:5)   TEXT    "".f1(SB), $264-128
        0x0000 00000 (x.go:5)   MOVQ    (TLS), CX
        0x0009 00009 (x.go:5)   LEAQ    -136(SP), AX
        0x0011 00017 (x.go:5)   CMPQ    AX, 16(CX)
        0x0015 00021 (x.go:5)   JLS     166
        0x001b 00027 (x.go:5)   SUBQ    $264, SP
        0x0022 00034 (x.go:5)   MOVQ    BP, 256(SP)
        0x002a 00042 (x.go:5)   LEAQ    256(SP), BP
        0x0032 00050 (x.go:5)   FUNCDATA        $0, gclocals·8658ec02c587fb17d31955e2d572c2ff(SB)
        0x0032 00050 (x.go:5)   FUNCDATA        $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
        0x0032 00050 (x.go:5)   LEAQ    "".t+272(SP), DI
        0x003a 00058 (x.go:5)   XORPS   X0, X0
        0x003d 00061 (x.go:5)   DUFFZERO        $266
        0x0050 00080 (x.go:6)   PCDATA  $0, $0
        0x0050 00080 (x.go:6)   CALL    "".g(SB)
        0x0055 00085 (x.go:6)   LEAQ    ""..autotmp_0+128(SP), DI
        0x005d 00093 (x.go:6)   MOVQ    SP, SI
        0x0060 00096 (x.go:6)   DUFFCOPY        $784
        0x0073 00115 (x.go:5)   LEAQ    "".t+272(SP), DI
        0x007b 00123 (x.go:6)   LEAQ    ""..autotmp_0+128(SP), SI
        0x0083 00131 (x.go:6)   DUFFCOPY        $784
        0x0096 00150 (x.go:6)   MOVQ    256(SP), BP
        0x009e 00158 (x.go:6)   ADDQ    $264, SP
        0x00a5 00165 (x.go:6)   RET
        0x00a6 00166 (x.go:6)   NOP
        0x00a6 00166 (x.go:5)   PCDATA  $0, $-1
        0x00a6 00166 (x.go:5)   CALL    runtime.morestack_noctxt(SB)
        0x00ab 00171 (x.go:5)   JMP     0
...
"".f2 t=1 size=176 args=0x80 locals=0x108
        0x0000 00000 (x.go:9)   TEXT    "".f2(SB), $264-128
        0x0000 00000 (x.go:9)   MOVQ    (TLS), CX
        0x0009 00009 (x.go:9)   LEAQ    -136(SP), AX
        0x0011 00017 (x.go:9)   CMPQ    AX, 16(CX)
        0x0015 00021 (x.go:9)   JLS     166
        0x001b 00027 (x.go:9)   SUBQ    $264, SP
        0x0022 00034 (x.go:9)   MOVQ    BP, 256(SP)
        0x002a 00042 (x.go:9)   LEAQ    256(SP), BP
        0x0032 00050 (x.go:9)   FUNCDATA        $0, gclocals·8658ec02c587fb17d31955e2d572c2ff(SB)
        0x0032 00050 (x.go:9)   FUNCDATA        $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
        0x0032 00050 (x.go:9)   LEAQ    "".~r0+272(SP), DI
        0x003a 00058 (x.go:9)   XORPS   X0, X0
        0x003d 00061 (x.go:9)   DUFFZERO        $266
        0x0050 00080 (x.go:10)  PCDATA  $0, $0
        0x0050 00080 (x.go:10)  CALL    "".g(SB)
        0x0055 00085 (x.go:10)  LEAQ    ""..autotmp_1+128(SP), DI
        0x005d 00093 (x.go:10)  MOVQ    SP, SI
        0x0060 00096 (x.go:10)  DUFFCOPY        $784
        0x0073 00115 (x.go:9)   LEAQ    "".~r0+272(SP), DI
        0x007b 00123 (x.go:10)  LEAQ    ""..autotmp_1+128(SP), SI
        0x0083 00131 (x.go:10)  DUFFCOPY        $784
        0x0096 00150 (x.go:10)  MOVQ    256(SP), BP
        0x009e 00158 (x.go:10)  ADDQ    $264, SP
        0x00a5 00165 (x.go:10)  RET
        0x00a6 00166 (x.go:10)  NOP
        0x00a6 00166 (x.go:9)   PCDATA  $0, $-1
        0x00a6 00166 (x.go:9)   CALL    runtime.morestack_noctxt(SB)
        0x00ab 00171 (x.go:9)   JMP     0
...

ie both f1 and f2 now unneccessarily pre-zero return. Possibly related: #15925

/cc @randall77

@mdempsky
Copy link
Member

mdempsky commented Mar 6, 2017

At least we fixed the bug that f1 and f2 compiled to different code. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants