Commit 980fdb8d authored by Giovanni Bajo's avatar Giovanni Bajo

cmd/compile: improve testing of induction variables

Test both minimum and maximum bound, and prepare
formatting for more advanced tests (inclusive / esclusive bounds).

Change-Id: Ibe432916d9c938343bc07943798bc9709ad71845
Reviewed-on: https://go-review.googlesource.com/104040
Run-TryBot: Giovanni Bajo <rasky@develer.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent f49369b6
package ssa package ssa
import "fmt"
type indVar struct { type indVar struct {
ind *Value // induction variable ind *Value // induction variable
inc *Value // increment, a constant inc *Value // increment, a constant
...@@ -138,11 +140,22 @@ nextb: ...@@ -138,11 +140,22 @@ nextb:
} }
if f.pass.debug >= 1 { if f.pass.debug >= 1 {
if min.Op == OpConst64 { mlim1, mlim2 := fmt.Sprint(min.AuxInt), fmt.Sprint(max.AuxInt)
b.Func.Warnl(b.Pos, "Induction variable with minimum %d and increment %d", min.AuxInt, inc.AuxInt) if !min.isGenericIntConst() {
if f.pass.debug >= 2 {
mlim1 = fmt.Sprint(min)
} else { } else {
b.Func.Warnl(b.Pos, "Induction variable with non-const minimum and increment %d", inc.AuxInt) mlim1 = "?"
}
}
if !max.isGenericIntConst() {
if f.pass.debug >= 2 {
mlim2 = fmt.Sprint(max)
} else {
mlim2 = "?"
}
} }
b.Func.Warnl(b.Pos, "Induction variable: limits [%v,%v), increment %d", mlim1, mlim2, inc.AuxInt)
} }
iv = append(iv, indVar{ iv = append(iv, indVar{
......
...@@ -5,7 +5,7 @@ package main ...@@ -5,7 +5,7 @@ package main
func f0a(a []int) int { func f0a(a []int) int {
x := 0 x := 0
for i := range a { // ERROR "Induction variable with minimum 0 and increment 1$" for i := range a { // ERROR "Induction variable: limits \[0,\?\), increment 1$"
x += a[i] // ERROR "Proved IsInBounds$" x += a[i] // ERROR "Proved IsInBounds$"
} }
return x return x
...@@ -13,7 +13,7 @@ func f0a(a []int) int { ...@@ -13,7 +13,7 @@ func f0a(a []int) int {
func f0b(a []int) int { func f0b(a []int) int {
x := 0 x := 0
for i := range a { // ERROR "Induction variable with minimum 0 and increment 1$" for i := range a { // ERROR "Induction variable: limits \[0,\?\), increment 1$"
b := a[i:] // ERROR "Proved IsSliceInBounds$" b := a[i:] // ERROR "Proved IsSliceInBounds$"
x += b[0] x += b[0]
} }
...@@ -22,7 +22,7 @@ func f0b(a []int) int { ...@@ -22,7 +22,7 @@ func f0b(a []int) int {
func f0c(a []int) int { func f0c(a []int) int {
x := 0 x := 0
for i := range a { // ERROR "Induction variable with minimum 0 and increment 1$" for i := range a { // ERROR "Induction variable: limits \[0,\?\), increment 1$"
b := a[:i+1] // ERROR "Proved IsSliceInBounds$" b := a[:i+1] // ERROR "Proved IsSliceInBounds$"
x += b[0] x += b[0]
} }
...@@ -31,7 +31,7 @@ func f0c(a []int) int { ...@@ -31,7 +31,7 @@ func f0c(a []int) int {
func f1(a []int) int { func f1(a []int) int {
x := 0 x := 0
for _, i := range a { // ERROR "Induction variable with minimum 0 and increment 1" for _, i := range a { // ERROR "Induction variable: limits \[0,\?\), increment 1$"
x += i x += i
} }
return x return x
...@@ -39,7 +39,7 @@ func f1(a []int) int { ...@@ -39,7 +39,7 @@ func f1(a []int) int {
func f2(a []int) int { func f2(a []int) int {
x := 0 x := 0
for i := 1; i < len(a); i++ { // ERROR "Induction variable with minimum 1 and increment 1$" for i := 1; i < len(a); i++ { // ERROR "Induction variable: limits \[1,\?\), increment 1$"
x += a[i] // ERROR "Proved IsInBounds$" x += a[i] // ERROR "Proved IsInBounds$"
} }
return x return x
...@@ -47,7 +47,7 @@ func f2(a []int) int { ...@@ -47,7 +47,7 @@ func f2(a []int) int {
func f4(a [10]int) int { func f4(a [10]int) int {
x := 0 x := 0
for i := 0; i < len(a); i += 2 { // ERROR "Induction variable with minimum 0 and increment 2$" for i := 0; i < len(a); i += 2 { // ERROR "Induction variable: limits \[0,10\), increment 2$"
x += a[i] // ERROR "Proved IsInBounds$" x += a[i] // ERROR "Proved IsInBounds$"
} }
return x return x
...@@ -55,14 +55,14 @@ func f4(a [10]int) int { ...@@ -55,14 +55,14 @@ func f4(a [10]int) int {
func f5(a [10]int) int { func f5(a [10]int) int {
x := 0 x := 0
for i := -10; i < len(a); i += 2 { // ERROR "Induction variable with minimum -10 and increment 2$" for i := -10; i < len(a); i += 2 { // ERROR "Induction variable: limits \[-10,10\), increment 2$"
x += a[i] x += a[i]
} }
return x return x
} }
func f6(a []int) { func f6(a []int) {
for i := range a { // ERROR "Induction variable with minimum 0 and increment 1$" for i := range a { // ERROR "Induction variable: limits \[0,\?\), increment 1$"
b := a[0:i] // ERROR "Proved IsSliceInBounds$" b := a[0:i] // ERROR "Proved IsSliceInBounds$"
f6(b) f6(b)
} }
...@@ -70,7 +70,7 @@ func f6(a []int) { ...@@ -70,7 +70,7 @@ func f6(a []int) {
func g0a(a string) int { func g0a(a string) int {
x := 0 x := 0
for i := 0; i < len(a); i++ { // ERROR "Induction variable with minimum 0 and increment 1$" for i := 0; i < len(a); i++ { // ERROR "Induction variable: limits \[0,\?\), increment 1$"
x += int(a[i]) // ERROR "Proved IsInBounds$" x += int(a[i]) // ERROR "Proved IsInBounds$"
} }
return x return x
...@@ -78,7 +78,7 @@ func g0a(a string) int { ...@@ -78,7 +78,7 @@ func g0a(a string) int {
func g0b(a string) int { func g0b(a string) int {
x := 0 x := 0
for i := 0; len(a) > i; i++ { // ERROR "Induction variable with minimum 0 and increment 1$" for i := 0; len(a) > i; i++ { // ERROR "Induction variable: limits \[0,\?\), increment 1$"
x += int(a[i]) // ERROR "Proved IsInBounds$" x += int(a[i]) // ERROR "Proved IsInBounds$"
} }
return x return x
...@@ -87,7 +87,7 @@ func g0b(a string) int { ...@@ -87,7 +87,7 @@ func g0b(a string) int {
func g1() int { func g1() int {
a := "evenlength" a := "evenlength"
x := 0 x := 0
for i := 0; i < len(a); i += 2 { // ERROR "Induction variable with minimum 0 and increment 2$" for i := 0; i < len(a); i += 2 { // ERROR "Induction variable: limits \[0,10\), increment 2$"
x += int(a[i]) // ERROR "Proved IsInBounds$" x += int(a[i]) // ERROR "Proved IsInBounds$"
} }
return x return x
...@@ -96,7 +96,7 @@ func g1() int { ...@@ -96,7 +96,7 @@ func g1() int {
func g2() int { func g2() int {
a := "evenlength" a := "evenlength"
x := 0 x := 0
for i := 0; i < len(a); i += 2 { // ERROR "Induction variable with minimum 0 and increment 2$" for i := 0; i < len(a); i += 2 { // ERROR "Induction variable: limits \[0,10\), increment 2$"
j := i j := i
if a[i] == 'e' { // ERROR "Proved IsInBounds$" if a[i] == 'e' { // ERROR "Proved IsInBounds$"
j = j + 1 j = j + 1
...@@ -108,39 +108,39 @@ func g2() int { ...@@ -108,39 +108,39 @@ func g2() int {
func g3a() { func g3a() {
a := "this string has length 25" a := "this string has length 25"
for i := 0; i < len(a); i += 5 { // ERROR "Induction variable with minimum 0 and increment 5$" for i := 0; i < len(a); i += 5 { // ERROR "Induction variable: limits \[0,25\), increment 5$"
useString(a[i:]) // ERROR "Proved IsSliceInBounds$" useString(a[i:]) // ERROR "Proved IsSliceInBounds$"
useString(a[:i+3]) useString(a[:i+3])
} }
} }
func g3b(a string) { func g3b(a string) {
for i := 0; i < len(a); i++ { // ERROR "Induction variable with minimum 0 and increment 1$" for i := 0; i < len(a); i++ { // ERROR "Induction variable: limits \[0,\?\), increment 1$"
useString(a[i+1:]) // ERROR "Proved IsSliceInBounds$" useString(a[i+1:]) // ERROR "Proved IsSliceInBounds$"
} }
} }
func g3c(a string) { func g3c(a string) {
for i := 0; i < len(a); i++ { // ERROR "Induction variable with minimum 0 and increment 1$" for i := 0; i < len(a); i++ { // ERROR "Induction variable: limits \[0,\?\), increment 1$"
useString(a[:i+1]) // ERROR "Proved IsSliceInBounds$" useString(a[:i+1]) // ERROR "Proved IsSliceInBounds$"
} }
} }
func h1(a []byte) { func h1(a []byte) {
c := a[:128] c := a[:128]
for i := range c { // ERROR "Induction variable with minimum 0 and increment 1$" for i := range c { // ERROR "Induction variable: limits \[0,128\), increment 1$"
c[i] = byte(i) // ERROR "Proved IsInBounds$" c[i] = byte(i) // ERROR "Proved IsInBounds$"
} }
} }
func h2(a []byte) { func h2(a []byte) {
for i := range a[:128] { // ERROR "Induction variable with minimum 0 and increment 1$" for i := range a[:128] { // ERROR "Induction variable: limits \[0,128\), increment 1$"
a[i] = byte(i) a[i] = byte(i)
} }
} }
func k0(a [100]int) [100]int { func k0(a [100]int) [100]int {
for i := 10; i < 90; i++ { // ERROR "Induction variable with minimum 10 and increment 1$" for i := 10; i < 90; i++ { // ERROR "Induction variable: limits \[10,90\), increment 1$"
a[i-11] = i a[i-11] = i
a[i-10] = i // ERROR "Proved IsInBounds$" a[i-10] = i // ERROR "Proved IsInBounds$"
a[i-5] = i // ERROR "Proved IsInBounds$" a[i-5] = i // ERROR "Proved IsInBounds$"
...@@ -153,7 +153,7 @@ func k0(a [100]int) [100]int { ...@@ -153,7 +153,7 @@ func k0(a [100]int) [100]int {
} }
func k1(a [100]int) [100]int { func k1(a [100]int) [100]int {
for i := 10; i < 90; i++ { // ERROR "Induction variable with minimum 10 and increment 1$" for i := 10; i < 90; i++ { // ERROR "Induction variable: limits \[10,90\), increment 1$"
useSlice(a[:i-11]) useSlice(a[:i-11])
useSlice(a[:i-10]) // ERROR "Proved IsSliceInBounds$" useSlice(a[:i-10]) // ERROR "Proved IsSliceInBounds$"
useSlice(a[:i-5]) // ERROR "Proved IsSliceInBounds$" useSlice(a[:i-5]) // ERROR "Proved IsSliceInBounds$"
...@@ -168,7 +168,7 @@ func k1(a [100]int) [100]int { ...@@ -168,7 +168,7 @@ func k1(a [100]int) [100]int {
} }
func k2(a [100]int) [100]int { func k2(a [100]int) [100]int {
for i := 10; i < 90; i++ { // ERROR "Induction variable with minimum 10 and increment 1$" for i := 10; i < 90; i++ { // ERROR "Induction variable: limits \[10,90\), increment 1$"
useSlice(a[i-11:]) useSlice(a[i-11:])
useSlice(a[i-10:]) // ERROR "Proved IsSliceInBounds$" useSlice(a[i-10:]) // ERROR "Proved IsSliceInBounds$"
useSlice(a[i-5:]) // ERROR "Proved IsSliceInBounds$" useSlice(a[i-5:]) // ERROR "Proved IsSliceInBounds$"
...@@ -182,7 +182,7 @@ func k2(a [100]int) [100]int { ...@@ -182,7 +182,7 @@ func k2(a [100]int) [100]int {
} }
func k3(a [100]int) [100]int { func k3(a [100]int) [100]int {
for i := -10; i < 90; i++ { // ERROR "Induction variable with minimum -10 and increment 1$" for i := -10; i < 90; i++ { // ERROR "Induction variable: limits \[-10,90\), increment 1$"
a[i+9] = i a[i+9] = i
a[i+10] = i // ERROR "Proved IsInBounds$" a[i+10] = i // ERROR "Proved IsInBounds$"
a[i+11] = i a[i+11] = i
...@@ -192,7 +192,7 @@ func k3(a [100]int) [100]int { ...@@ -192,7 +192,7 @@ func k3(a [100]int) [100]int {
func k4(a [100]int) [100]int { func k4(a [100]int) [100]int {
min := (-1) << 63 min := (-1) << 63
for i := min; i < min+50; i++ { // ERROR "Induction variable with minimum -9223372036854775808 and increment 1$" for i := min; i < min+50; i++ { // ERROR "Induction variable: limits \[-9223372036854775808,-9223372036854775758\), increment 1$"
a[i-min] = i // ERROR "Proved IsInBounds$" a[i-min] = i // ERROR "Proved IsInBounds$"
} }
return a return a
...@@ -200,7 +200,7 @@ func k4(a [100]int) [100]int { ...@@ -200,7 +200,7 @@ func k4(a [100]int) [100]int {
func k5(a [100]int) [100]int { func k5(a [100]int) [100]int {
max := (1 << 63) - 1 max := (1 << 63) - 1
for i := max - 50; i < max; i++ { // ERROR "Induction variable with minimum 9223372036854775757 and increment 1$" for i := max - 50; i < max; i++ { // ERROR "Induction variable: limits \[9223372036854775757,9223372036854775807\), increment 1"
a[i-max+50] = i // ERROR "Proved IsInBounds$" a[i-max+50] = i // ERROR "Proved IsInBounds$"
a[i-(max-70)] = i // ERROR "Proved IsInBounds$" a[i-(max-70)] = i // ERROR "Proved IsInBounds$"
} }
...@@ -224,13 +224,13 @@ func nobce1() { ...@@ -224,13 +224,13 @@ func nobce1() {
} }
func nobce2(a string) { func nobce2(a string) {
for i := int64(0); i < int64(len(a)); i++ { // ERROR "Induction variable with minimum 0 and increment 1$" for i := int64(0); i < int64(len(a)); i++ { // ERROR "Induction variable: limits \[0,\?\), increment 1$"
useString(a[i:]) // ERROR "Proved IsSliceInBounds$" useString(a[i:]) // ERROR "Proved IsSliceInBounds$"
} }
for i := int64(0); i < int64(len(a))-31337; i++ { // ERROR "Induction variable with minimum 0 and increment 1$" for i := int64(0); i < int64(len(a))-31337; i++ { // ERROR "Induction variable: limits \[0,\?\), increment 1$"
useString(a[i:]) // ERROR "Proved IsSliceInBounds$" useString(a[i:]) // ERROR "Proved IsSliceInBounds$"
} }
for i := int64(0); i < int64(len(a))+int64(-1<<63); i++ { // ERROR "Induction variable with minimum 0 and increment 1$" for i := int64(0); i < int64(len(a))+int64(-1<<63); i++ { // ERROR "Induction variable: limits \[0,\?\), increment 1$"
// tests an overflow of StringLen-MinInt64 // tests an overflow of StringLen-MinInt64
useString(a[i:]) useString(a[i:])
} }
...@@ -239,7 +239,7 @@ func nobce2(a string) { ...@@ -239,7 +239,7 @@ func nobce2(a string) {
func nobce3(a [100]int64) [100]int64 { func nobce3(a [100]int64) [100]int64 {
min := int64((-1) << 63) min := int64((-1) << 63)
max := int64((1 << 63) - 1) max := int64((1 << 63) - 1)
for i := min; i < max; i++ { // ERROR "Induction variable with minimum -9223372036854775808 and increment 1$" for i := min; i < max; i++ { // ERROR "Induction variable: limits \[-9223372036854775808,9223372036854775807\), increment 1$"
a[i] = i a[i] = i
} }
return a return a
......
...@@ -62,7 +62,7 @@ func f1c(a []int, i int64) int { ...@@ -62,7 +62,7 @@ func f1c(a []int, i int64) int {
} }
func f2(a []int) int { func f2(a []int) int {
for i := range a { // ERROR "Induction variable with minimum 0 and increment 1" for i := range a { // ERROR "Induction variable: limits \[0,\?\), increment 1"
a[i+1] = i a[i+1] = i
a[i+1] = i // ERROR "Proved IsInBounds$" a[i+1] = i // ERROR "Proved IsInBounds$"
} }
...@@ -464,7 +464,7 @@ func f16(s []int) []int { ...@@ -464,7 +464,7 @@ func f16(s []int) []int {
} }
func f17(b []int) { func f17(b []int) {
for i := 0; i < len(b); i++ { // ERROR "Induction variable with minimum 0 and increment 1" for i := 0; i < len(b); i++ { // ERROR "Induction variable: limits \[0,\?\), increment 1"
// This tests for i <= cap, which we can only prove // This tests for i <= cap, which we can only prove
// using the derived relation between len and cap. // using the derived relation between len and cap.
// This depends on finding the contradiction, since we // This depends on finding the contradiction, since we
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment