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: Improve BCE when using positive int as slice index #28885

Open
leitzler opened this Issue Nov 20, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@leitzler
Contributor

leitzler commented Nov 20, 2018

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

$ go version
go version go1.11.2 linux/amd64

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
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/leitzler/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/leitzler/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build416756199=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main  

// Should be enough with the first bounds check (?)
func foo(b []byte, n int) {  
    _ = b[n]  // Bounds check  
    _ = b[25] // Bounds check  
  
    for i := n; i <= 25; i++ {  
        b[i] = 123 // Bounds check  
    }  
}  

// prove should be able to eliminate bounds check in the for-loop here
func foo2(b []byte, n int) {  
    if n < 0 {  
        panic("")  
    }  
  
    _ = b[n]  // Bounds check  
    _ = b[25] // Bounds check  
  
    for i := n; i <= 25; i++ {  
        b[i] = 123 // Bounds check  
    }  
}  

// when using uint, it works as expected and eliminates bounds check in the loop  
func foo3(b []byte, n uint) {  
    _ = b[25] // Bounds check  
  
    for i := n; i <= 25; i++ {  
        b[i] = 123 // Bounds check elimintated  
    }  
}  

What did you expect to see?

BCE in all examples above.

What did you see instead?

BCE in the third example only.

@josharian

This comment has been minimized.

Contributor

josharian commented Nov 20, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment