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: can len(n)>0 be optimized to len(n)!=0 #48054

Closed
xiyichan opened this issue Aug 30, 2021 · 3 comments
Closed

cmd/compile: can len(n)>0 be optimized to len(n)!=0 #48054

xiyichan opened this issue Aug 30, 2021 · 3 comments

Comments

@xiyichan
Copy link
Contributor

@xiyichan xiyichan commented Aug 30, 2021

What version of Go are you using (go version)?

$ go version
go version go1.17 linux/arm64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
O111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://goproxy.io,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_arm64"
GOVCS=""
GOVERSION="go1.17"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/root/go/src/test/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build426405088=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main
func a(n string ) bool {
	if len(n)!=0{
		return true
	}
	return false
}
func a2(n string) bool{
	if len(n)>0{
		return true
	}
	return false
}

What did you expect to see?

same genssa

What did you see instead?

func a genssa

# /root/go/src/test/main.go
00000 (8) TEXT "".a(SB), ABIInternal
00001 (8) FUNCDATA $0, gclocals·a36216b97439c93dafebe03e7f0808b5(SB)
00002 (8) FUNCDATA $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
00003 (+9) MOVD "".n+8(RSP), R0
00004 (9) CBZ R0, 8
00005 (+10) MOVD $1, R0
00006 (10) MOVB R0, "".~r1+16(RSP)
00007 (10) RET
00008 (+12) MOVB ZR, "".~r1+16(RSP)
00009 (12) RET
00010 (?) END

func a2 genssa

# /root/go/src/test/main.go
00000 (15) TEXT "".a2(SB), ABIInternal
00001 (15) FUNCDATA $0, gclocals·1a65e721a2ccc325b382662e7ffee780(SB)
00002 (15) FUNCDATA $1, gclocals·69c1753bd5f81501d95132d08af04464(SB)
00003 (15) FUNCDATA $5, "".a2.arginfo1(SB)
00004 (+16) MOVD "".n+8(RSP), R0
00005 (16) CMP $0, R0
00006 (16) BLE 10
00007 (+17) MOVD $1, R0
00008 (17) MOVB R0, "".~r1+16(RSP)
00009 (17) RET
00010 (+19) MOVB ZR, "".~r1+16(RSP)
00011 (19) RET
00012 (?) END
@gopherbot
Copy link

@gopherbot gopherbot commented Aug 30, 2021

Change https://golang.org/cl/346050 mentions this issue: cmd/compile: optimize len(s) > 0

Loading

@korzhao
Copy link
Contributor

@korzhao korzhao commented Aug 30, 2021

I found that len(x) can return a negative number.
https://play.golang.org/p/Ch54lAy2xTE

Loading

@randall77
Copy link
Contributor

@randall77 randall77 commented Aug 30, 2021

@korzhao , that kind of unsafe usage is not allowed. Our compiler assumes len is always nonnegative.

Loading

@randall77 randall77 added this to the Unplanned milestone Aug 30, 2021
@gopherbot gopherbot closed this in 21de6bc Sep 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants