Skip to content

Commit 7c1e778

Browse files
committed
cmd/compile: make prove use non-equality in subtraction for a stronger bound
Given: s := /* slice */ k := /* proved valid index in s (0 <= k < len(s)) */ v := s[k:] len(v) >= 1, so v[0] needs no bounds check. However, for len(v) = len(s) - k, we only checked if len(s) >= k and so could only prove len(v) >= 0, thus the bounds check wasn't removed. As far as I can tell these checks were commented out for performance, but after benchmarking prove I see no difference. Fixes: #76429
1 parent cead111 commit 7c1e778

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

src/cmd/compile/internal/ssa/prove.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,24 +2162,22 @@ func (ft *factsTable) detectSubRelations(v *Value) {
21622162
return // x-y might overflow
21632163
}
21642164

2165-
// Subtracting a positive number only makes
2166-
// things smaller.
2167-
if yLim.min >= 0 {
2165+
// Subtracting a positive non-zero number only makes
2166+
// things smaller. If it's positive or zero, it might
2167+
// also do nothing (x-0 == v).
2168+
if yLim.min > 0 {
2169+
ft.update(v.Block, v, x, signed, lt)
2170+
} else if yLim.min == 0 {
21682171
ft.update(v.Block, v, x, signed, lt|eq)
2169-
// TODO: is this worth it?
2170-
//if yLim.min > 0 {
2171-
// ft.update(v.Block, v, x, signed, lt)
2172-
//}
21732172
}
21742173

21752174
// Subtracting a number from a bigger one
2176-
// can't go below 0.
2177-
if ft.orderS.OrderedOrEqual(y, x) {
2175+
// can't go below 1. If the numbers might be
2176+
// equal, then it can't go below 0.
2177+
if ft.orderS.Ordered(y, x) {
2178+
ft.signedMin(v, 1)
2179+
} else if ft.orderS.OrderedOrEqual(y, x) {
21782180
ft.setNonNegative(v)
2179-
// TODO: is this worth it?
2180-
//if ft.orderS.Ordered(y, x) {
2181-
// ft.signedMin(v, 1)
2182-
//}
21832181
}
21842182
}
21852183

0 commit comments

Comments
 (0)