From 68ac1f774624faf99e7f6ec6592acb50f23b7874 Mon Sep 17 00:00:00 2001 From: Keith Randall Date: Thu, 7 Apr 2016 10:21:35 -0700 Subject: [PATCH] cmd/compile: Fix constant-folding of unsigned shifts MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 Run-TryBot: Keith Randall TryBot-Result: Gobot Gobot --- .../compile/internal/ssa/gen/generic.rules | 6 +- .../compile/internal/ssa/rewritegeneric.go | 12 ++-- test/fixedbugs/issue15175.go | 66 +++++++++++++++++++ 3 files changed, 75 insertions(+), 9 deletions(-) create mode 100644 test/fixedbugs/issue15175.go diff --git a/src/cmd/compile/internal/ssa/gen/generic.rules b/src/cmd/compile/internal/ssa/gen/generic.rules index b56e3f1b2d..dacc2007c8 100644 --- a/src/cmd/compile/internal/ssa/gen/generic.rules +++ b/src/cmd/compile/internal/ssa/gen/generic.rules @@ -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]) diff --git a/src/cmd/compile/internal/ssa/rewritegeneric.go b/src/cmd/compile/internal/ssa/rewritegeneric.go index 932cb42235..9b0f43c414 100644 --- a/src/cmd/compile/internal/ssa/rewritegeneric.go +++ b/src/cmd/compile/internal/ssa/rewritegeneric.go @@ -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]) _) diff --git a/test/fixedbugs/issue15175.go b/test/fixedbugs/issue15175.go new file mode 100644 index 0000000000..c6cab532f8 --- /dev/null +++ b/test/fixedbugs/issue15175.go @@ -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 +} -- 2.48.1