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

[release-branch.go1.15] cmd/compile: sign extend consant folding properly

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>
This commit is contained in:
Keith Randall 2020-11-20 13:53:05 -08:00 committed by Carlos Amedee
parent 16ddb8bc76
commit a2adbc876e
3 changed files with 25 additions and 12 deletions

View File

@ -1429,11 +1429,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 [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.

View 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

View File

@ -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
}