Skip to content

Commit 820f58a

Browse files
Jorroporandall77
authored andcommitted
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 <[email protected]> Reviewed-by: David Chase <[email protected]> Reviewed-by: Keith Randall <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 2f31659 commit 820f58a

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -1787,6 +1787,10 @@ func (ft *factsTable) flowLimit(v *Value) bool {
17871787
a := ft.limits[v.Args[0].ID]
17881788
b := ft.limits[v.Args[1].ID]
17891789
return ft.newLimit(v, a.sub(b, 8))
1790+
case OpNeg64, OpNeg32, OpNeg16, OpNeg8:
1791+
a := ft.limits[v.Args[0].ID]
1792+
bitsize := uint(v.Type.Size()) * 8
1793+
return ft.newLimit(v, a.com(bitsize).add(limit{min: 1, max: 1, umin: 1, umax: 1}, bitsize))
17901794
case OpMul64:
17911795
a := ft.limits[v.Args[0].ID]
17921796
b := ft.limits[v.Args[1].ID]

test/prove.go

+43
Original file line numberDiff line numberDiff line change
@@ -1627,6 +1627,49 @@ func com64(a uint64, ensureAllBranchesCouldHappen func() bool) uint64 {
16271627
return z
16281628
}
16291629

1630+
func neg64(a uint64, ensureAllBranchesCouldHappen func() bool) uint64 {
1631+
var lo, hi uint64 = 0xff, 0xfff
1632+
a &= hi
1633+
a |= lo
1634+
1635+
z := -a
1636+
1637+
if ensureAllBranchesCouldHappen() && z > -lo { // ERROR "Disproved Less64U$"
1638+
return 42
1639+
}
1640+
if ensureAllBranchesCouldHappen() && z <= -lo { // ERROR "Proved Leq64U$"
1641+
return 1337
1642+
}
1643+
if ensureAllBranchesCouldHappen() && z < -hi { // ERROR "Disproved Less64U$"
1644+
return 42
1645+
}
1646+
if ensureAllBranchesCouldHappen() && z >= -hi { // ERROR "Proved Leq64U$"
1647+
return 1337
1648+
}
1649+
return z
1650+
}
1651+
func neg64mightOverflowDuringNeg(a uint64, ensureAllBranchesCouldHappen func() bool) uint64 {
1652+
var lo, hi uint64 = 0, 0xfff
1653+
a &= hi
1654+
a |= lo
1655+
1656+
z := -a
1657+
1658+
if ensureAllBranchesCouldHappen() && z > -lo {
1659+
return 42
1660+
}
1661+
if ensureAllBranchesCouldHappen() && z <= -lo {
1662+
return 1337
1663+
}
1664+
if ensureAllBranchesCouldHappen() && z < -hi {
1665+
return 42
1666+
}
1667+
if ensureAllBranchesCouldHappen() && z >= -hi {
1668+
return 1337
1669+
}
1670+
return z
1671+
}
1672+
16301673
//go:noinline
16311674
func useInt(a int) {
16321675
}

0 commit comments

Comments
 (0)