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 nil checks on address of return values #7994

Closed
randall77 opened this issue May 14, 2014 · 5 comments
Closed

cmd/compile: unnecessary nil checks on address of return values #7994

randall77 opened this issue May 14, 2014 · 5 comments

Comments

@randall77
Copy link
Contributor

package main
func foo(x int) (y int) {
     p := &x
     q := &y
     *q = *p
     return
}

compile with "go tool 6g -S foo.go"

The assembly is 

0005  MOVQ    $0,y+8(FP)
0006  LEAQ    x+0(FP),BX
0007  MOVQ    BX,CX
0008  LEAQ    y+8(FP),BX
0009  CMPQ    BX,$0
0010  JNE     $1,12
0011  MOVL    AX,(BX)
0012  NOP     ,
0013  MOVQ    (CX),R8
0014  MOVQ    R8,(BX)
0015  RET     ,

There is a nil check (CMPQ/JNE/MOVL/NOP) on the address of the return value, a value
that certainly non-nil.

Note that there is no nil check on the address of the argument.
@ianlancetaylor
Copy link
Contributor

Comment 1:

Labels changed: added repo-main.

@rsc
Copy link
Contributor

rsc commented Sep 15, 2014

Comment 2:

Labels changed: added release-go1.5, removed release-go1.4.

Status changed to Accepted.

@rsc
Copy link
Contributor

rsc commented Jun 8, 2015

Too late for Go 1.5. SSA may replace this code.

@rsc rsc modified the milestones: Unplanned, Go1.5 Jun 8, 2015
@rsc rsc changed the title cmd/gc: unnecessary nil checks on address of return values cmd/compile: unnecessary nil checks on address of return values Jun 8, 2015
@tzneal
Copy link
Member

tzneal commented Aug 21, 2015

dev.ssa currently generates:

00000  MOVQ    $0, AX
00002  MOVQ    AX, 16(SP) 
00007  MOVQ    8(SP), AX
00012  MOVQ    AX, 16(SP)
00017  RET

deadstore elimination doesn't work across blocks, or it would be even better.

@randall77
Copy link
Contributor Author

Fixed with SSA backend.

@golang golang locked and limited conversation to collaborators Apr 24, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants