1 // +build amd64,!gcflags_noopt
2 // errorcheck -0 -d=ssa/check_bce/debug=3
4 // Copyright 2016 The Go Authors. All rights reserved.
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file.
8 // Test that the compiler does bounds check elimination as expected.
9 // This avoids accidental regressions.
13 import "encoding/binary"
16 a[0] = 1 // ERROR "Found IsInBounds$"
18 a[6] = 1 // ERROR "Found IsInBounds$"
24 func f1(a [256]int, i int) {
26 useInt(a[i]) // ERROR "Found IsInBounds$"
28 useInt(a[j]) // ERROR "Found IsInBounds$"
34 if 4 <= i && i < len(a) {
41 func f2(a [256]int, i uint) {
42 useInt(a[i]) // ERROR "Found IsInBounds$"
51 func f2a(a [35]int, i uint8) {
52 useInt(a[i]) // ERROR "Found IsInBounds$"
59 func f2b(a [35]int, i uint16) {
60 useInt(a[i]) // ERROR "Found IsInBounds$"
67 func f2c(a [35]int, i uint32) {
68 useInt(a[i]) // ERROR "Found IsInBounds$"
75 func f3(a [256]int, i uint8) {
81 func f4(a [27]int, i uint8) {
95 func f6(a [32]int, b [64]int, i int) {
96 useInt(a[uint32(i*0x07C4ACDD)>>27])
97 useInt(b[uint64(i*0x07C4ACDD)>>58])
98 useInt(a[uint(i*0x07C4ACDD)>>59])
100 // The following bounds should not be removed because they can overflow.
101 useInt(a[uint32(i*0x106297f105d0cc86)>>26]) // ERROR "Found IsInBounds$"
102 useInt(b[uint64(i*0x106297f105d0cc86)>>57]) // ERROR "Found IsInBounds$"
103 useInt(a[int32(i*0x106297f105d0cc86)>>26]) // ERROR "Found IsInBounds$"
104 useInt(b[int64(i*0x106297f105d0cc86)>>57]) // ERROR "Found IsInBounds$"
116 useInt(a[3]) // ERROR "Found IsInBounds$"
123 for i := range a[:256] { // ERROR "Found IsSliceInBounds$"
124 useInt(a[i]) // ERROR "Found IsInBounds$"
132 func g4(a [100]int) {
133 for i := 10; i < 50; i++ {
139 // The following are out of bounds.
140 if a[0] == 0xdeadbeef {
141 // This is a trick to prohibit sccp to optimize out the following out of bound check
144 useInt(a[i-11]) // ERROR "Found IsInBounds$"
145 useInt(a[i+51]) // ERROR "Found IsInBounds$"
149 func decode1(data []byte) (x uint64) {
150 for len(data) >= 32 {
151 x += binary.BigEndian.Uint64(data[:8])
152 x += binary.BigEndian.Uint64(data[8:16])
153 x += binary.BigEndian.Uint64(data[16:24])
154 x += binary.BigEndian.Uint64(data[24:32])
160 func decode2(data []byte) (x uint64) {
161 // TODO(rasky): this should behave like decode1 and compile to no
162 // boundchecks. We're currently not able to remove all of them.
163 for len(data) >= 32 {
164 x += binary.BigEndian.Uint64(data)
166 x += binary.BigEndian.Uint64(data) // ERROR "Found IsInBounds$"
168 x += binary.BigEndian.Uint64(data) // ERROR "Found IsInBounds$"
170 x += binary.BigEndian.Uint64(data) // ERROR "Found IsInBounds$"
181 func useSlice(a []int) {