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: s[:0] has unnecessary comparison #14872

Closed
randall77 opened this issue Mar 18, 2016 · 1 comment
Closed

cmd/compile: s[:0] has unnecessary comparison #14872

randall77 opened this issue Mar 18, 2016 · 1 comment
Milestone

Comments

@randall77
Copy link
Contributor

func f(s []int) []int {
    return s[:0]
}

compiles to

    0x0000 00000 (slice0.go:4)  MOVQ    "".s+24(FP), CX
    0x0005 00005 (slice0.go:4)  CMPQ    CX, $0
    0x0009 00009 (slice0.go:4)  JEQ $0, 11
    0x000b 00011 (slice0.go:4)  MOVQ    "".s+8(FP), DX
    0x0010 00016 (slice0.go:4)  MOVQ    DX, "".~r1+32(FP)
    0x0015 00021 (slice0.go:4)  MOVQ    $0, "".~r1+40(FP)
    0x001e 00030 (slice0.go:4)  MOVQ    CX, "".~r1+48(FP)

The comparison is unnecessary, [:0] is always legal. And the branch doesn't go anywhere.

Might be a dup of #14849

@brtzsnr

@bradfitz bradfitz changed the title s[:0] has unnecessary comparison cmd/compile: s[:0] has unnecessary comparison Mar 18, 2016
@ianlancetaylor ianlancetaylor added this to the Go1.7 milestone Mar 19, 2016
@brtzsnr
Copy link
Contributor

brtzsnr commented Mar 20, 2016

Looks like a dup of #14849, but let me know if you find more of these cases. The generated code is now

v22 00074 (a.go:23) MOVQ "".s(FP), AX
v10 00075 (a.go:23) MOVQ AX, "".~r1+24(FP)
v13 00076 (a.go:23) MOVQ $0, "".~r1+32(FP)
v21 00077 (a.go:23) MOVQ "".s+16(FP), AX
v27 00078 (a.go:23) MOVQ AX, "".~r1+40(FP)

@golang golang locked and limited conversation to collaborators Mar 20, 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

4 participants