Skip to content

Commit

Permalink
[release-branch.go1.15] cmd/compile: sign extend consant folding prop…
Browse files Browse the repository at this point in the history
…erly

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>
  • Loading branch information
randall77 authored and cagedmantis committed Dec 3, 2020
1 parent 16ddb8b commit a2adbc8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/cmd/compile/internal/ssa/gen/AMD64.rules
Expand Up @@ -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.
Expand Down
18 changes: 9 additions & 9 deletions src/cmd/compile/internal/ssa/rewriteAMD64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions 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
}

0 comments on commit a2adbc8

Please sign in to comment.