1
0
mirror of https://github.com/golang/go synced 2024-11-23 11:30:06 -07:00

cmd/compile: Fix constant-folding of unsigned shifts

Make sure the results of unsigned constant-folded
shifts are sign-extended into the AuxInt field.

Fixes #15175

Change-Id: I3490d1bc3d9b2e1578ed30964645508577894f58
Reviewed-on: https://go-review.googlesource.com/21586
Reviewed-by: Alexandru Moșoi <alexandru@mosoi.ro>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Keith Randall 2016-04-07 10:21:35 -07:00
parent dfded57819
commit 68ac1f7746
3 changed files with 75 additions and 9 deletions

View File

@ -81,13 +81,13 @@
(Rsh64Ux64 (Const64 [c]) (Const64 [d])) -> (Const64 [int64(uint64(c) >> uint64(d))])
(Lsh32x64 (Const32 [c]) (Const64 [d])) -> (Const32 [int64(int32(c) << uint64(d))])
(Rsh32x64 (Const32 [c]) (Const64 [d])) -> (Const32 [int64(int32(c) >> uint64(d))])
(Rsh32Ux64 (Const32 [c]) (Const64 [d])) -> (Const32 [int64(uint32(c) >> uint64(d))])
(Rsh32Ux64 (Const32 [c]) (Const64 [d])) -> (Const32 [int64(int32(uint32(c) >> uint64(d)))])
(Lsh16x64 (Const16 [c]) (Const64 [d])) -> (Const16 [int64(int16(c) << uint64(d))])
(Rsh16x64 (Const16 [c]) (Const64 [d])) -> (Const16 [int64(int16(c) >> uint64(d))])
(Rsh16Ux64 (Const16 [c]) (Const64 [d])) -> (Const16 [int64(uint16(c) >> uint64(d))])
(Rsh16Ux64 (Const16 [c]) (Const64 [d])) -> (Const16 [int64(int16(uint16(c) >> uint64(d)))])
(Lsh8x64 (Const8 [c]) (Const64 [d])) -> (Const8 [int64(int8(c) << uint64(d))])
(Rsh8x64 (Const8 [c]) (Const64 [d])) -> (Const8 [int64(int8(c) >> uint64(d))])
(Rsh8Ux64 (Const8 [c]) (Const64 [d])) -> (Const8 [int64(uint8(c) >> uint64(d))])
(Rsh8Ux64 (Const8 [c]) (Const64 [d])) -> (Const8 [int64(int8(uint8(c) >> uint64(d)))])
(Lsh64x64 (Const64 [0]) _) -> (Const64 [0])
(Rsh64x64 (Const64 [0]) _) -> (Const64 [0])

View File

@ -6185,7 +6185,7 @@ func rewriteValuegeneric_OpRsh16Ux64(v *Value, config *Config) bool {
_ = b
// match: (Rsh16Ux64 (Const16 [c]) (Const64 [d]))
// cond:
// result: (Const16 [int64(uint16(c) >> uint64(d))])
// result: (Const16 [int64(int16(uint16(c) >> uint64(d)))])
for {
v_0 := v.Args[0]
if v_0.Op != OpConst16 {
@ -6198,7 +6198,7 @@ func rewriteValuegeneric_OpRsh16Ux64(v *Value, config *Config) bool {
}
d := v_1.AuxInt
v.reset(OpConst16)
v.AuxInt = int64(uint16(c) >> uint64(d))
v.AuxInt = int64(int16(uint16(c) >> uint64(d)))
return true
}
// match: (Rsh16Ux64 (Const16 [0]) _)
@ -6547,7 +6547,7 @@ func rewriteValuegeneric_OpRsh32Ux64(v *Value, config *Config) bool {
_ = b
// match: (Rsh32Ux64 (Const32 [c]) (Const64 [d]))
// cond:
// result: (Const32 [int64(uint32(c) >> uint64(d))])
// result: (Const32 [int64(int32(uint32(c) >> uint64(d)))])
for {
v_0 := v.Args[0]
if v_0.Op != OpConst32 {
@ -6560,7 +6560,7 @@ func rewriteValuegeneric_OpRsh32Ux64(v *Value, config *Config) bool {
}
d := v_1.AuxInt
v.reset(OpConst32)
v.AuxInt = int64(uint32(c) >> uint64(d))
v.AuxInt = int64(int32(uint32(c) >> uint64(d)))
return true
}
// match: (Rsh32Ux64 (Const32 [0]) _)
@ -7353,7 +7353,7 @@ func rewriteValuegeneric_OpRsh8Ux64(v *Value, config *Config) bool {
_ = b
// match: (Rsh8Ux64 (Const8 [c]) (Const64 [d]))
// cond:
// result: (Const8 [int64(uint8(c) >> uint64(d))])
// result: (Const8 [int64(int8(uint8(c) >> uint64(d)))])
for {
v_0 := v.Args[0]
if v_0.Op != OpConst8 {
@ -7366,7 +7366,7 @@ func rewriteValuegeneric_OpRsh8Ux64(v *Value, config *Config) bool {
}
d := v_1.AuxInt
v.reset(OpConst8)
v.AuxInt = int64(uint8(c) >> uint64(d))
v.AuxInt = int64(int8(uint8(c) >> uint64(d)))
return true
}
// match: (Rsh8Ux64 (Const8 [0]) _)

View File

@ -0,0 +1,66 @@
// run
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Make sure unsigned shift results get sign-extended correctly.
package main
import "fmt"
func main() {
failed := false
a6 := uint8(253)
if got := a6 >> 0; got != 253 {
fmt.Printf("uint8(253)>>0 = %v, wanted 253\n", got)
failed = true
}
if got := f1(0, 2, 1, 0, 0, 1, true); got != 255 {
fmt.Printf("f1(...) = %v, wanted 255\n", got)
failed = true
}
if got := f2(1); got != 242 {
fmt.Printf("f2(...) = %v, wanted 242\n", got)
failed = true
}
if got := f3(false, 0, 0); got != 254 {
fmt.Printf("f3(...) = %v, wanted 254\n", got)
failed = true
}
if failed {
panic("bad")
}
}
func f1(a1 uint, a2 int8, a3 int8, a4 int8, a5 uint8, a6 int, a7 bool) uint8 {
a5--
a4 += (a2 << a1 << 2) | (a4 ^ a4<<(a1&a1)) - a3 // int8
a6 -= a6 >> (2 + uint32(a2)>>3) // int
a1 += a1 // uint
a3 *= a4 << (a1 | a1) << (uint16(3) >> 2 & (1 - 0) & (uint16(1) << a5 << 3)) // int8
a7 = a7 || ((a2 == a4) || (a7 && a7) || ((a5 == a5) || (a7 || a7))) // bool
return a5 >> a1
}
func f2(a1 uint8) uint8 {
a1--
a1--
a1 -= a1 + (a1 << 1) - (a1*a1*a1)<<(2-0+(3|3)-1) // uint8
v1 := 0 * ((2 * 1) ^ 1) & ((uint(0) >> a1) + (2+0)*(uint(2)+0)) // uint
_ = v1
return a1 >> (((2 ^ 2) >> (v1 | 2)) + 0)
}
func f3(a1 bool, a2 uint, a3 int64) uint8 {
a3--
v1 := 1 & (2 & 1 * (1 ^ 2) & (uint8(3*1) >> 0)) // uint8
_ = v1
v1 += v1 - (v1 >> a2) + (v1 << (a2 ^ a2) & v1) // uint8
v1 *= v1 // uint8
a3--
v1 += v1 & v1 // uint8
v1--
v1 = ((v1 << 0) | v1>>0) + v1 // uint8
return v1 >> 0
}