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

The easiest fix is just to turn on properly typed auxint fields
(which is what fixed this issue at tip).

Fixes #42753

Change-Id: I264245fad45067a6ade65326f7fe681feb5f3739
Reviewed-on: https://go-review.googlesource.com/c/go/+/272028
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 ee9ccfb41c64cbcc03cddb89af6bcbee34b40b29..f7eb92dc8b638cb7c78b311ffed96375cd406118 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 [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 [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 [d^(1<<uint32(c))])
 
 // If c or d doesn't fit into 32 bits, then we can't construct ORQconst,
 // but we can still constant-fold.
index 72ed1eb62c8692514e44862c9184ab352feff262..e59fa208c1ec41953bcf4dcf36c0bcd85d3bc4ce 100644 (file)
@@ -3526,13 +3526,13 @@ func rewriteValueAMD64_OpAMD64BTCLconst(v *Value) bool {
        // match: (BTCLconst [c] (MOVLconst [d]))
        // result: (MOVLconst [d^(1<<uint32(c))])
        for {
-               c := v.AuxInt
+               c := auxIntToInt8(v.AuxInt)
                if v_0.Op != OpAMD64MOVLconst {
                        break
                }
-               d := v_0.AuxInt
+               d := auxIntToInt32(v_0.AuxInt)
                v.reset(OpAMD64MOVLconst)
-               v.AuxInt = d ^ (1 << uint32(c))
+               v.AuxInt = int32ToAuxInt(d ^ (1 << uint32(c)))
                return true
        }
        return false
@@ -4010,13 +4010,13 @@ func rewriteValueAMD64_OpAMD64BTRLconst(v *Value) bool {
        // match: (BTRLconst [c] (MOVLconst [d]))
        // result: (MOVLconst [d&^(1<<uint32(c))])
        for {
-               c := v.AuxInt
+               c := auxIntToInt8(v.AuxInt)
                if v_0.Op != OpAMD64MOVLconst {
                        break
                }
-               d := v_0.AuxInt
+               d := auxIntToInt32(v_0.AuxInt)
                v.reset(OpAMD64MOVLconst)
-               v.AuxInt = d &^ (1 << uint32(c))
+               v.AuxInt = int32ToAuxInt(d &^ (1 << uint32(c)))
                return true
        }
        return false
@@ -4356,13 +4356,13 @@ func rewriteValueAMD64_OpAMD64BTSLconst(v *Value) bool {
        // match: (BTSLconst [c] (MOVLconst [d]))
        // result: (MOVLconst [d|(1<<uint32(c))])
        for {
-               c := v.AuxInt
+               c := auxIntToInt8(v.AuxInt)
                if v_0.Op != OpAMD64MOVLconst {
                        break
                }
-               d := v_0.AuxInt
+               d := auxIntToInt32(v_0.AuxInt)
                v.reset(OpAMD64MOVLconst)
-               v.AuxInt = d | (1 << uint32(c))
+               v.AuxInt = int32ToAuxInt(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
+}