From: Jorropo Date: Thu, 15 Aug 2024 02:54:46 +0000 (+0200) Subject: cmd/compile: compute Negation's limits from argument's limits X-Git-Tag: go1.24rc1~1039 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=820f58a27f7f64f21353db6a071bf5dbf658924c;p=gostls13.git cmd/compile: compute Negation's limits from argument's limits Change-Id: I2e4d74a86faa95321e847a061e06c3efff7f20df Reviewed-on: https://go-review.googlesource.com/c/go/+/605775 Reviewed-by: Keith Randall Reviewed-by: David Chase Reviewed-by: Keith Randall LUCI-TryBot-Result: Go LUCI --- diff --git a/src/cmd/compile/internal/ssa/prove.go b/src/cmd/compile/internal/ssa/prove.go index db0ad97ad0..415d627784 100644 --- a/src/cmd/compile/internal/ssa/prove.go +++ b/src/cmd/compile/internal/ssa/prove.go @@ -1787,6 +1787,10 @@ func (ft *factsTable) flowLimit(v *Value) bool { a := ft.limits[v.Args[0].ID] b := ft.limits[v.Args[1].ID] return ft.newLimit(v, a.sub(b, 8)) + case OpNeg64, OpNeg32, OpNeg16, OpNeg8: + a := ft.limits[v.Args[0].ID] + bitsize := uint(v.Type.Size()) * 8 + return ft.newLimit(v, a.com(bitsize).add(limit{min: 1, max: 1, umin: 1, umax: 1}, bitsize)) case OpMul64: a := ft.limits[v.Args[0].ID] b := ft.limits[v.Args[1].ID] diff --git a/test/prove.go b/test/prove.go index fd709f119e..2265b637ba 100644 --- a/test/prove.go +++ b/test/prove.go @@ -1627,6 +1627,49 @@ func com64(a uint64, ensureAllBranchesCouldHappen func() bool) uint64 { return z } +func neg64(a uint64, ensureAllBranchesCouldHappen func() bool) uint64 { + var lo, hi uint64 = 0xff, 0xfff + a &= hi + a |= lo + + z := -a + + if ensureAllBranchesCouldHappen() && z > -lo { // ERROR "Disproved Less64U$" + return 42 + } + if ensureAllBranchesCouldHappen() && z <= -lo { // ERROR "Proved Leq64U$" + return 1337 + } + if ensureAllBranchesCouldHappen() && z < -hi { // ERROR "Disproved Less64U$" + return 42 + } + if ensureAllBranchesCouldHappen() && z >= -hi { // ERROR "Proved Leq64U$" + return 1337 + } + return z +} +func neg64mightOverflowDuringNeg(a uint64, ensureAllBranchesCouldHappen func() bool) uint64 { + var lo, hi uint64 = 0, 0xfff + a &= hi + a |= lo + + z := -a + + if ensureAllBranchesCouldHappen() && z > -lo { + return 42 + } + if ensureAllBranchesCouldHappen() && z <= -lo { + return 1337 + } + if ensureAllBranchesCouldHappen() && z < -hi { + return 42 + } + if ensureAllBranchesCouldHappen() && z >= -hi { + return 1337 + } + return z +} + //go:noinline func useInt(a int) { }