aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeith Randall <khr@golang.org>2020-11-20 14:02:37 -0800
committerCarlos Amedee <carlos@golang.org>2020-12-03 13:58:36 +0000
commit679482fc602445b9b2d37739e7b048e2d4913646 (patch)
treec56a2148d8cc3067141bea561b21cf17eafcae1d
parent9e27a089d644950cba52108f6634badc5880c6e0 (diff)
downloadgo-679482fc602445b9b2d37739e7b048e2d4913646.tar.gz
go-679482fc602445b9b2d37739e7b048e2d4913646.zip
[release-branch.go1.14] cmd/compile: sign extend constant folding properly
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>
-rw-r--r--src/cmd/compile/internal/ssa/gen/AMD64.rules6
-rw-r--r--src/cmd/compile/internal/ssa/rewriteAMD64.go12
-rw-r--r--test/fixedbugs/issue42753.go13
3 files changed, 22 insertions, 9 deletions
diff --git a/src/cmd/compile/internal/ssa/gen/AMD64.rules b/src/cmd/compile/internal/ssa/gen/AMD64.rules
index 2d662d5ae6..f819aefcfd 100644
--- a/src/cmd/compile/internal/ssa/gen/AMD64.rules
+++ b/src/cmd/compile/internal/ssa/gen/AMD64.rules
@@ -1465,11 +1465,11 @@
(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
diff --git a/src/cmd/compile/internal/ssa/rewriteAMD64.go b/src/cmd/compile/internal/ssa/rewriteAMD64.go
index c104157cec..366eac0706 100644
--- a/src/cmd/compile/internal/ssa/rewriteAMD64.go
+++ b/src/cmd/compile/internal/ssa/rewriteAMD64.go
@@ -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
index 0000000000..a998d1d3b3
--- /dev/null
+++ b/test/fixedbugs/issue42753.go
@@ -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
+}