]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.14] cmd/compile: sign extend constant folding properly
authorKeith Randall <khr@golang.org>
Fri, 20 Nov 2020 22:02:37 +0000 (14:02 -0800)
committerCarlos Amedee <carlos@golang.org>
Thu, 3 Dec 2020 13:58:36 +0000 (13:58 +0000)
MOVLconst must have a properly sign-extended auxint constant.
The bit operations in these rules don't enforce that invariant.

Fixes #42755

Change-Id: I729afcad18752d9b7739e49709020e3be7b3653e
Reviewed-on: https://go-review.googlesource.com/c/go/+/272030
Trust: Keith Randall <khr@golang.org>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: David Chase <drchase@google.com>
src/cmd/compile/internal/ssa/gen/AMD64.rules
src/cmd/compile/internal/ssa/rewriteAMD64.go
test/fixedbugs/issue42753.go [new file with mode: 0644]

index 2d662d5ae681d0eed704853bd615ecb709a02fbd..f819aefcfdeaa00ccff109bf3f9c3ca9d13d4772 100644 (file)
 (NOTQ (MOVQconst [c])) -> (MOVQconst [^c])
 (NOTL (MOVLconst [c])) -> (MOVLconst [^c])
 (BTSQconst [c] (MOVQconst [d])) -> (MOVQconst [d|(1<<uint32(c))])
-(BTSLconst [c] (MOVLconst [d])) -> (MOVLconst [d|(1<<uint32(c))])
+(BTSLconst [c] (MOVLconst [d])) -> (MOVLconst [int64(int32(d|(1<<uint32(c))))])
 (BTRQconst [c] (MOVQconst [d])) -> (MOVQconst [d&^(1<<uint32(c))])
-(BTRLconst [c] (MOVLconst [d])) -> (MOVLconst [d&^(1<<uint32(c))])
+(BTRLconst [c] (MOVLconst [d])) -> (MOVLconst [int64(int32(d&^(1<<uint32(c))))])
 (BTCQconst [c] (MOVQconst [d])) -> (MOVQconst [d^(1<<uint32(c))])
-(BTCLconst [c] (MOVLconst [d])) -> (MOVLconst [d^(1<<uint32(c))])
+(BTCLconst [c] (MOVLconst [d])) -> (MOVLconst [int64(int32(d^(1<<uint32(c))))])
 
 // generic simplifications
 // TODO: more of this
index c104157cec0091e1d050444b32155c4197c2c159..366eac070618e9e7c406e2d8b2bd85e25278f082 100644 (file)
@@ -4326,7 +4326,7 @@ func rewriteValueAMD64_OpAMD64BTCLconst_0(v *Value) bool {
                return true
        }
        // match: (BTCLconst [c] (MOVLconst [d]))
-       // result: (MOVLconst [d^(1<<uint32(c))])
+       // result: (MOVLconst [int64(int32(d^(1<<uint32(c))))])
        for {
                c := v.AuxInt
                v_0 := v.Args[0]
@@ -4335,7 +4335,7 @@ func rewriteValueAMD64_OpAMD64BTCLconst_0(v *Value) bool {
                }
                d := v_0.AuxInt
                v.reset(OpAMD64MOVLconst)
-               v.AuxInt = d ^ (1 << uint32(c))
+               v.AuxInt = int64(int32(d ^ (1 << uint32(c))))
                return true
        }
        return false
@@ -4825,7 +4825,7 @@ func rewriteValueAMD64_OpAMD64BTRLconst_0(v *Value) bool {
                return true
        }
        // match: (BTRLconst [c] (MOVLconst [d]))
-       // result: (MOVLconst [d&^(1<<uint32(c))])
+       // result: (MOVLconst [int64(int32(d&^(1<<uint32(c))))])
        for {
                c := v.AuxInt
                v_0 := v.Args[0]
@@ -4834,7 +4834,7 @@ func rewriteValueAMD64_OpAMD64BTRLconst_0(v *Value) bool {
                }
                d := v_0.AuxInt
                v.reset(OpAMD64MOVLconst)
-               v.AuxInt = d &^ (1 << uint32(c))
+               v.AuxInt = int64(int32(d &^ (1 << uint32(c))))
                return true
        }
        return false
@@ -5181,7 +5181,7 @@ func rewriteValueAMD64_OpAMD64BTSLconst_0(v *Value) bool {
                return true
        }
        // match: (BTSLconst [c] (MOVLconst [d]))
-       // result: (MOVLconst [d|(1<<uint32(c))])
+       // result: (MOVLconst [int64(int32(d|(1<<uint32(c))))])
        for {
                c := v.AuxInt
                v_0 := v.Args[0]
@@ -5190,7 +5190,7 @@ func rewriteValueAMD64_OpAMD64BTSLconst_0(v *Value) bool {
                }
                d := v_0.AuxInt
                v.reset(OpAMD64MOVLconst)
-               v.AuxInt = d | (1 << uint32(c))
+               v.AuxInt = int64(int32(d | (1 << uint32(c))))
                return true
        }
        return false
diff --git a/test/fixedbugs/issue42753.go b/test/fixedbugs/issue42753.go
new file mode 100644 (file)
index 0000000..a998d1d
--- /dev/null
@@ -0,0 +1,13 @@
+// compile -d=ssa/check/on
+
+// Copyright 2020 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.
+
+package main
+
+func f() uint32 {
+       s := "\x01"
+       x := -int32(s[0])
+       return uint32(x) & 0x7fffffff
+}