Skip to content

Commit 4f2c0e5

Browse files
Jorroporandall77
authored andcommitted
cmd/compile: compute Trunc's limits from argument's limits
Change-Id: I419faa781db085b98ea25008ca127d0317fb34e1 Reviewed-on: https://go-review.googlesource.com/c/go/+/605695 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]> Reviewed-by: Carlos Amedee <[email protected]>
1 parent 68c431e commit 4f2c0e5

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,6 +1643,11 @@ func (ft *factsTable) flowLimit(v *Value) bool {
16431643
case OpSignExt8to64, OpSignExt8to32, OpSignExt8to16, OpSignExt16to64, OpSignExt16to32, OpSignExt32to64:
16441644
a := ft.limits[v.Args[0].ID]
16451645
return ft.signedMinMax(v, a.min, a.max)
1646+
case OpTrunc64to8, OpTrunc64to16, OpTrunc64to32, OpTrunc32to8, OpTrunc32to16, OpTrunc16to8:
1647+
a := ft.limits[v.Args[0].ID]
1648+
if a.umax <= 1<<(uint64(v.Type.Size())*8)-1 {
1649+
return ft.unsignedMinMax(v, a.umin, a.umax)
1650+
}
16461651

16471652
// math/bits
16481653
case OpCtz64:

test/prove.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,6 +1586,26 @@ func div64s(a, b int64, ensureAllBranchesCouldHappen func() bool) int64 {
15861586
return z
15871587
}
15881588

1589+
func trunc64to16(a uint64, ensureAllBranchesCouldHappen func() bool) uint16 {
1590+
a &= 0xfff
1591+
a |= 0xff
1592+
1593+
z := uint16(a)
1594+
if ensureAllBranchesCouldHappen() && z > 0xfff { // ERROR "Disproved Less16U$"
1595+
return 42
1596+
}
1597+
if ensureAllBranchesCouldHappen() && z <= 0xfff { // ERROR "Proved Leq16U$"
1598+
return 1337
1599+
}
1600+
if ensureAllBranchesCouldHappen() && z < 0xff { // ERROR "Disproved Less16U$"
1601+
return 42
1602+
}
1603+
if ensureAllBranchesCouldHappen() && z >= 0xff { // ERROR "Proved Leq16U$"
1604+
return 1337
1605+
}
1606+
return z
1607+
}
1608+
15891609
//go:noinline
15901610
func useInt(a int) {
15911611
}

0 commit comments

Comments
 (0)