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 <khr@google.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Jorropo 2024-08-15 04:54:46 +02:00 committed by Keith Randall
parent 2f3165973f
commit 820f58a27f
2 changed files with 47 additions and 0 deletions

View File

@ -1787,6 +1787,10 @@ func (ft *factsTable) flowLimit(v *Value) bool {
a := ft.limits[v.Args[0].ID] a := ft.limits[v.Args[0].ID]
b := ft.limits[v.Args[1].ID] b := ft.limits[v.Args[1].ID]
return ft.newLimit(v, a.sub(b, 8)) 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: case OpMul64:
a := ft.limits[v.Args[0].ID] a := ft.limits[v.Args[0].ID]
b := ft.limits[v.Args[1].ID] b := ft.limits[v.Args[1].ID]

View File

@ -1627,6 +1627,49 @@ func com64(a uint64, ensureAllBranchesCouldHappen func() bool) uint64 {
return z 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 //go:noinline
func useInt(a int) { func useInt(a int) {
} }