Skip to content

Commit f2eea4c

Browse files
committed
cmd/compile: mask SLL,SRL,SRAconst shift amount
mips SRA/SLL/SRL shift amounts are used mod 32; this change aligns the XXXconst rules to mask the shift amount by &31. Passes $ GOARCH=mips go build -toolexec 'toolstash -cmp' -a std $ GOARCH=mipsle go build -toolexec 'toolstash -cmp' -a std Fixes #42587 Change-Id: I6003ebd0bc500fba4cf6fb10254e1b557bf8c48f Reviewed-on: https://go-review.googlesource.com/c/go/+/270117 Trust: Alberto Donizetti <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Cherry Zhang <[email protected]>
1 parent 92c732e commit f2eea4c

File tree

4 files changed

+27
-30
lines changed

4 files changed

+27
-30
lines changed

src/cmd/compile/internal/ssa/gen/MIPS.rules

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,9 @@
567567
(XOR x (MOVWconst [c])) => (XORconst [c] x)
568568
(NOR x (MOVWconst [c])) => (NORconst [c] x)
569569

570-
(SRA x (MOVWconst [c])) && c >= 32 => (SRAconst x [31])
571-
(SLL x (MOVWconst [c])) => (SLLconst x [c])
572-
(SRL x (MOVWconst [c])) => (SRLconst x [c])
573-
(SRA x (MOVWconst [c])) => (SRAconst x [c])
570+
(SLL x (MOVWconst [c])) => (SLLconst x [c&31])
571+
(SRL x (MOVWconst [c])) => (SRLconst x [c&31])
572+
(SRA x (MOVWconst [c])) => (SRAconst x [c&31])
574573

575574
(SGT (MOVWconst [c]) x) => (SGTconst [c] x)
576575
(SGTU (MOVWconst [c]) x) => (SGTUconst [c] x)

src/cmd/compile/internal/ssa/gen/MIPSOps.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,11 @@ func init() {
185185

186186
// shifts
187187
{name: "SLL", argLength: 2, reg: gp21, asm: "SLL"}, // arg0 << arg1, shift amount is mod 32
188-
{name: "SLLconst", argLength: 1, reg: gp11, asm: "SLL", aux: "Int32"}, // arg0 << auxInt
188+
{name: "SLLconst", argLength: 1, reg: gp11, asm: "SLL", aux: "Int32"}, // arg0 << auxInt, shift amount must be 0 through 31 inclusive
189189
{name: "SRL", argLength: 2, reg: gp21, asm: "SRL"}, // arg0 >> arg1, unsigned, shift amount is mod 32
190-
{name: "SRLconst", argLength: 1, reg: gp11, asm: "SRL", aux: "Int32"}, // arg0 >> auxInt, unsigned
190+
{name: "SRLconst", argLength: 1, reg: gp11, asm: "SRL", aux: "Int32"}, // arg0 >> auxInt, shift amount must be 0 through 31 inclusive
191191
{name: "SRA", argLength: 2, reg: gp21, asm: "SRA"}, // arg0 >> arg1, signed, shift amount is mod 32
192-
{name: "SRAconst", argLength: 1, reg: gp11, asm: "SRA", aux: "Int32"}, // arg0 >> auxInt, signed
192+
{name: "SRAconst", argLength: 1, reg: gp11, asm: "SRA", aux: "Int32"}, // arg0 >> auxInt, signed, shift amount must be 0 through 31 inclusive
193193

194194
{name: "CLZ", argLength: 1, reg: gp11, asm: "CLZ"},
195195

src/cmd/compile/internal/ssa/rewriteMIPS.go

Lines changed: 6 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixedbugs/issue42587.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// compile
2+
3+
// Copyright 2020 The Go Authors. All rights reserved. Use of this
4+
// source code is governed by a BSD-style license that can be found in
5+
// the LICENSE file.
6+
7+
package p
8+
9+
func f() {
10+
var i, j int
11+
_ = func() {
12+
i = 32
13+
j = j>>i | len([]int{})
14+
}
15+
}

0 commit comments

Comments
 (0)